Merge pull request #2752 from Rawk/fix_broken_gtest
[vuplus_xbmc] / xbmc / MediaSource.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "MediaSource.h"
22 #include "settings/AdvancedSettings.h"
23 #include "Util.h"
24 #include "URL.h"
25 #include "filesystem/MultiPathDirectory.h"
26 #include "utils/URIUtils.h"
27
28 using namespace std;
29 using namespace XFILE;
30
31 bool CMediaSource::IsWritable() const
32 {
33   return CUtil::SupportsWriteFileOperations(strPath);
34 }
35
36 void CMediaSource::FromNameAndPaths(const CStdString &category, const CStdString &name, const vector<CStdString> &paths)
37 {
38   vecPaths = paths;
39   if (paths.size() == 0)
40   { // no paths - return
41     strPath.Empty();
42   }
43   else if (paths.size() == 1)
44   { // only one valid path? make it the strPath
45     strPath = paths[0];
46   }
47   else
48   { // multiple valid paths?
49     strPath = CMultiPathDirectory::ConstructMultiPath(vecPaths);
50   }
51
52   strName = name;
53   m_iLockMode = LOCK_MODE_EVERYONE;
54   m_strLockCode = "0";
55   m_iBadPwdCount = 0;
56   m_iHasLock = 0;
57   m_allowSharing = true;
58
59   if (URIUtils::IsMultiPath(strPath))
60     m_iDriveType = SOURCE_TYPE_VPATH;
61   else if (strPath.Left(4).Equals("udf:"))
62   {
63     m_iDriveType = SOURCE_TYPE_VIRTUAL_DVD;
64     strPath = "D:\\";
65   }
66   else if (URIUtils::IsISO9660(strPath))
67     m_iDriveType = SOURCE_TYPE_VIRTUAL_DVD;
68   else if (URIUtils::IsDVD(strPath))
69     m_iDriveType = SOURCE_TYPE_DVD;
70   else if (URIUtils::IsRemote(strPath))
71     m_iDriveType = SOURCE_TYPE_REMOTE;
72   else if (URIUtils::IsHD(strPath))
73     m_iDriveType = SOURCE_TYPE_LOCAL;
74   else
75     m_iDriveType = SOURCE_TYPE_UNKNOWN;
76   // check - convert to url and back again to make sure strPath is accurate
77   // in terms of what we expect
78   strPath = CURL(strPath).Get();
79 }
80
81 bool CMediaSource::operator==(const CMediaSource &share) const
82 {
83   // NOTE: we may wish to filter this through CURL to enable better "fuzzy" matching
84   if (strPath != share.strPath)
85     return false;
86   if (strName != share.strName)
87     return false;
88   return true;
89 }
90
91 void AddOrReplace(VECSOURCES& sources, const VECSOURCES& extras)
92 {
93   unsigned int i;
94   for( i=0;i<extras.size();++i )
95   {
96     unsigned int j;
97     for ( j=0;j<sources.size();++j)
98     {
99       if (sources[j].strPath.Equals(extras[i].strPath))
100       {
101         sources[j] = extras[i];
102         break;
103       }
104     }
105     if (j == sources.size())
106       sources.push_back(extras[i]);
107   }
108 }
109
110 void AddOrReplace(VECSOURCES& sources, const CMediaSource& source)
111 {
112   unsigned int i;
113   for( i=0;i<sources.size();++i )
114   {
115     if (sources[i].strPath.Equals(source.strPath))
116     {
117       sources[i] = source;
118       break;
119     }
120   }
121   if (i == sources.size())
122     sources.push_back(source);
123 }