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