Merge pull request #4775 from jmarshallnz/empty_episode_playcount
[vuplus_xbmc] / xbmc / MediaSource.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://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 #include "utils/StringUtils.h"
28
29 using namespace std;
30 using namespace XFILE;
31
32 bool CMediaSource::IsWritable() const
33 {
34   return CUtil::SupportsWriteFileOperations(strPath);
35 }
36
37 void CMediaSource::FromNameAndPaths(const CStdString &category, const CStdString &name, const vector<CStdString> &paths)
38 {
39   vecPaths = paths;
40   if (paths.size() == 0)
41   { // no paths - return
42     strPath.clear();
43   }
44   else if (paths.size() == 1)
45   { // only one valid path? make it the strPath
46     strPath = paths[0];
47   }
48   else
49   { // multiple valid paths?
50     strPath = CMultiPathDirectory::ConstructMultiPath(vecPaths);
51   }
52
53   strName = name;
54   m_iLockMode = LOCK_MODE_EVERYONE;
55   m_strLockCode = "0";
56   m_iBadPwdCount = 0;
57   m_iHasLock = 0;
58   m_allowSharing = true;
59
60   if (URIUtils::IsMultiPath(strPath))
61     m_iDriveType = SOURCE_TYPE_VPATH;
62   else if (StringUtils::StartsWithNoCase(strPath, "udf:"))
63   {
64     m_iDriveType = SOURCE_TYPE_VIRTUAL_DVD;
65     strPath = "D:\\";
66   }
67   else if (URIUtils::IsISO9660(strPath))
68     m_iDriveType = SOURCE_TYPE_VIRTUAL_DVD;
69   else if (URIUtils::IsDVD(strPath))
70     m_iDriveType = SOURCE_TYPE_DVD;
71   else if (URIUtils::IsRemote(strPath))
72     m_iDriveType = SOURCE_TYPE_REMOTE;
73   else if (URIUtils::IsHD(strPath))
74     m_iDriveType = SOURCE_TYPE_LOCAL;
75   else
76     m_iDriveType = SOURCE_TYPE_UNKNOWN;
77   // check - convert to url and back again to make sure strPath is accurate
78   // in terms of what we expect
79   strPath = CURL(strPath).Get();
80 }
81
82 bool CMediaSource::operator==(const CMediaSource &share) const
83 {
84   // NOTE: we may wish to filter this through CURL to enable better "fuzzy" matching
85   if (strPath != share.strPath)
86     return false;
87   if (strName != share.strName)
88     return false;
89   return true;
90 }
91
92 void AddOrReplace(VECSOURCES& sources, const VECSOURCES& extras)
93 {
94   unsigned int i;
95   for( i=0;i<extras.size();++i )
96   {
97     unsigned int j;
98     for ( j=0;j<sources.size();++j)
99     {
100       if (sources[j].strPath.Equals(extras[i].strPath))
101       {
102         sources[j] = extras[i];
103         break;
104       }
105     }
106     if (j == sources.size())
107       sources.push_back(extras[i]);
108   }
109 }
110
111 void AddOrReplace(VECSOURCES& sources, const CMediaSource& source)
112 {
113   unsigned int i;
114   for( i=0;i<sources.size();++i )
115   {
116     if (sources[i].strPath.Equals(source.strPath))
117     {
118       sources[i] = source;
119       break;
120     }
121   }
122   if (i == sources.size())
123     sources.push_back(source);
124 }