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