Merge pull request #2033 from PhenX/master
[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
58   if (URIUtils::IsMultiPath(strPath))
59     m_iDriveType = SOURCE_TYPE_VPATH;
60   else if (strPath.Left(4).Equals("udf:"))
61   {
62     m_iDriveType = SOURCE_TYPE_VIRTUAL_DVD;
63     strPath = "D:\\";
64   }
65   else if (URIUtils::IsISO9660(strPath))
66     m_iDriveType = SOURCE_TYPE_VIRTUAL_DVD;
67   else if (URIUtils::IsDVD(strPath))
68     m_iDriveType = SOURCE_TYPE_DVD;
69   else if (URIUtils::IsRemote(strPath))
70     m_iDriveType = SOURCE_TYPE_REMOTE;
71   else if (URIUtils::IsHD(strPath))
72     m_iDriveType = SOURCE_TYPE_LOCAL;
73   else
74     m_iDriveType = SOURCE_TYPE_UNKNOWN;
75   // check - convert to url and back again to make sure strPath is accurate
76   // in terms of what we expect
77   strPath = CURL(strPath).Get();
78 }
79
80 bool CMediaSource::operator==(const CMediaSource &share) const
81 {
82   // NOTE: we may wish to filter this through CURL to enable better "fuzzy" matching
83   if (strPath != share.strPath)
84     return false;
85   if (strName != share.strName)
86     return false;
87   return true;
88 }
89
90 void AddOrReplace(VECSOURCES& sources, const VECSOURCES& extras)
91 {
92   unsigned int i;
93   for( i=0;i<extras.size();++i )
94   {
95     unsigned int j;
96     for ( j=0;j<sources.size();++j)
97     {
98       if (sources[j].strPath.Equals(extras[i].strPath))
99       {
100         sources[j] = extras[i];
101         break;
102       }
103     }
104     if (j == sources.size())
105       sources.push_back(extras[i]);
106   }
107 }
108
109 void AddOrReplace(VECSOURCES& sources, const CMediaSource& source)
110 {
111   unsigned int i;
112   for( i=0;i<sources.size();++i )
113   {
114     if (sources[i].strPath.Equals(source.strPath))
115     {
116       sources[i] = source;
117       break;
118     }
119   }
120   if (i == sources.size())
121     sources.push_back(source);
122 }