[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / filesystem / IDirectory.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
22 #include "IDirectory.h"
23 #include "Util.h"
24 #include "dialogs/GUIDialogOK.h"
25 #include "guilib/GUIKeyboardFactory.h"
26 #include "URL.h"
27 #include "PasswordManager.h"
28 #include "utils/URIUtils.h"
29
30 using namespace XFILE;
31
32 IDirectory::IDirectory(void)
33 {
34   m_strFileMask = "";
35   m_flags = DIR_FLAG_DEFAULTS;
36 }
37
38 IDirectory::~IDirectory(void)
39 {}
40
41 /*!
42  \brief Test a file for an extension specified with SetMask().
43  \param strFile File to test
44  \return Returns \e true, if file is allowed.
45  */
46 bool IDirectory::IsAllowed(const CStdString& strFile) const
47 {
48   CStdString strExtension;
49   if ( !m_strFileMask.size() ) return true;
50   if ( !strFile.size() ) return true;
51
52   URIUtils::GetExtension(strFile, strExtension);
53
54   if (!strExtension.size()) return false;
55
56   strExtension.ToLower();
57   strExtension += '|'; // ensures that we have a | at the end of it
58   if (m_strFileMask.Find(strExtension) != -1)
59   { // it's allowed, but we should also ignore all non dvd related ifo files.
60     if (strExtension.Equals(".ifo|"))
61     {
62       CStdString fileName = URIUtils::GetFileName(strFile);
63       if (fileName.Equals("video_ts.ifo")) return true;
64       if (fileName.length() == 12 && fileName.Left(4).Equals("vts_") && fileName.Right(6).Equals("_0.ifo")) return true;
65       return false;
66     }
67     if (strExtension.Equals(".dat|"))
68     {
69       CStdString fileName = URIUtils::GetFileName(strFile);
70       /* VCD filenames are of the form AVSEQ##(#).DAT, ITEM###(#).DAT, MUSIC##(#).DAT - i.e. all 11 or 12 characters long
71          starting with AVSEQ, MUSIC or ITEM */
72       if ((fileName.length() == 11 || fileName.length() == 12) &&
73           (fileName.Left(5).Equals("AVSEQ") || fileName.Left(5).Equals("MUSIC") || fileName.Left(4).Equals("ITEM")))
74         return true;
75       return false;
76     }
77     return true;
78   }
79   return false;
80 }
81
82 /*!
83  \brief Set a mask of extensions for the files in the directory.
84  \param strMask Mask of file extensions that are allowed.
85
86  The mask has to look like the following: \n
87  \verbatim
88  .m4a|.flac|.aac|
89  \endverbatim
90  So only *.m4a, *.flac, *.aac files will be retrieved with GetDirectory().
91  */
92 void IDirectory::SetMask(const CStdString& strMask)
93 {
94   m_strFileMask = strMask;
95   // ensure it's completed with a | so that filtering is easy.
96   m_strFileMask.ToLower();
97   if (m_strFileMask.size() && m_strFileMask[m_strFileMask.size() - 1] != '|')
98     m_strFileMask += '|';
99 }
100
101 /*!
102  \brief Set the flags for this directory handler.
103  \param flags - \sa XFILE::DIR_FLAG for a description.
104  */
105 void IDirectory::SetFlags(int flags)
106 {
107   m_flags = flags;
108 }
109
110 bool IDirectory::ProcessRequirements()
111 {
112   CStdString type = m_requirements["type"].asString();
113   if (type == "keyboard")
114   {
115     CStdString input;
116     if (CGUIKeyboardFactory::ShowAndGetInput(input, m_requirements["heading"], false))
117     {
118       m_requirements["input"] = input;
119       return true;
120     }
121   }
122   else if (type == "authenticate")
123   {
124     CURL url(m_requirements["url"].asString());
125     if (CPasswordManager::GetInstance().PromptToAuthenticateURL(url))
126     {
127       m_requirements.clear();
128       return true;
129     }
130   }
131   else if (type == "error")
132   {
133     CGUIDialogOK::ShowAndGetInput(m_requirements["heading"], m_requirements["line1"], m_requirements["line2"], m_requirements["line3"]);
134   }
135   m_requirements.clear();
136   return false;
137 }
138
139 bool IDirectory::GetKeyboardInput(const CVariant &heading, CStdString &input)
140 {
141   if (!CStdString(m_requirements["input"].asString()).IsEmpty())
142   {
143     input = m_requirements["input"].asString();
144     return true;
145   }
146   m_requirements.clear();
147   m_requirements["type"] = "keyboard";
148   m_requirements["heading"] = heading;
149   return false;
150 }
151
152 void IDirectory::SetErrorDialog(const CVariant &heading, const CVariant &line1, const CVariant &line2, const CVariant &line3)
153 {
154   m_requirements.clear();
155   m_requirements["type"] = "error";
156   m_requirements["heading"] = heading;
157   m_requirements["line1"] = line1;
158   m_requirements["line2"] = line2;
159   m_requirements["line3"] = line3;
160 }
161
162 void IDirectory::RequireAuthentication(const CStdString &url)
163 {
164   m_requirements.clear();
165   m_requirements["type"] = "authenticate";
166   m_requirements["url"] = url;
167 }