2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "IDirectory.h"
11 #include "PasswordManager.h"
13 #include "guilib/GUIKeyboardFactory.h"
14 #include "messaging/helpers/DialogOKHelper.h"
15 #include "utils/StringUtils.h"
16 #include "utils/URIUtils.h"
18 using namespace KODI::MESSAGING
;
19 using namespace XFILE
;
21 const CProfileManager
*IDirectory::m_profileManager
= nullptr;
23 void IDirectory::RegisterProfileManager(const CProfileManager
&profileManager
)
25 m_profileManager
= &profileManager
;
28 void IDirectory::UnregisterProfileManager()
30 m_profileManager
= nullptr;
33 IDirectory::IDirectory()
35 m_flags
= DIR_FLAG_DEFAULTS
;
38 IDirectory::~IDirectory(void) = default;
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".
48 bool IDirectory::IsAllowed(const CURL
& url
) const
50 if (m_strFileMask
.empty())
53 // Check if strFile have an allowed extension
54 if (!URIUtils::HasExtension(url
, m_strFileMask
))
57 // We should ignore all non dvd/vcd related ifo and dat files.
58 if (URIUtils::HasExtension(url
, ".ifo"))
60 std::string fileName
= URIUtils::GetFileName(url
);
62 // Allow filenames of the form video_ts.ifo or vts_##_0.ifo
64 return StringUtils::EqualsNoCase(fileName
, "video_ts.ifo") ||
65 (fileName
.length() == 12 &&
66 StringUtils::StartsWithNoCase(fileName
, "vts_") &&
67 StringUtils::EndsWithNoCase(fileName
, "_0.ifo"));
70 if (URIUtils::HasExtension(url
, ".dat"))
72 std::string fileName
= URIUtils::GetFileName(url
);
73 std::string folder
= URIUtils::GetDirectory(fileName
);
74 URIUtils::RemoveSlashAtEnd(folder
);
75 folder
= URIUtils::GetFileName(folder
);
76 if (StringUtils::EqualsNoCase(folder
, "vcd") ||
77 StringUtils::EqualsNoCase(folder
, "mpegav") ||
78 StringUtils::EqualsNoCase(folder
, "cdda"))
80 // Allow filenames of the form AVSEQ##(#).DAT, ITEM###(#).DAT
82 return (fileName
.length() == 11 || fileName
.length() == 12) &&
83 (StringUtils::StartsWithNoCase(fileName
, "AVSEQ") ||
84 StringUtils::StartsWithNoCase(fileName
, "MUSIC") ||
85 StringUtils::StartsWithNoCase(fileName
, "ITEM"));
92 \brief Set a mask of extensions for the files in the directory.
93 \param strMask Mask of file extensions that are allowed.
95 The mask has to look like the following: \n
99 So only *.m4a, *.flac, *.aac files will be retrieved with GetDirectory().
101 void IDirectory::SetMask(const std::string
& strMask
)
103 m_strFileMask
= strMask
;
104 // ensure it's completed with a | so that filtering is easy.
105 StringUtils::ToLower(m_strFileMask
);
106 if (m_strFileMask
.size() && m_strFileMask
[m_strFileMask
.size() - 1] != '|')
107 m_strFileMask
+= '|';
111 \brief Set the flags for this directory handler.
112 \param flags - \sa XFILE::DIR_FLAG for a description.
114 void IDirectory::SetFlags(int flags
)
119 bool IDirectory::ProcessRequirements()
121 std::string type
= m_requirements
["type"].asString();
122 if (type
== "keyboard")
125 if (CGUIKeyboardFactory::ShowAndGetInput(input
, m_requirements
["heading"], false, m_requirements
["hidden"].asBoolean()))
127 m_requirements
["input"] = input
;
131 else if (type
== "authenticate")
133 CURL
url(m_requirements
["url"].asString());
134 if (CPasswordManager::GetInstance().PromptToAuthenticateURL(url
))
136 m_requirements
.clear();
140 else if (type
== "error")
142 HELPERS::ShowOKDialogLines(CVariant
{m_requirements
["heading"]}, CVariant
{m_requirements
["line1"]}, CVariant
{m_requirements
["line2"]}, CVariant
{m_requirements
["line3"]});
144 m_requirements
.clear();
148 bool IDirectory::GetKeyboardInput(const CVariant
&heading
, std::string
&input
, bool hiddenInput
)
150 if (!m_requirements
["input"].asString().empty())
152 input
= m_requirements
["input"].asString();
155 m_requirements
.clear();
156 m_requirements
["type"] = "keyboard";
157 m_requirements
["heading"] = heading
;
158 m_requirements
["hidden"] = hiddenInput
;
162 void IDirectory::SetErrorDialog(const CVariant
&heading
, const CVariant
&line1
, const CVariant
&line2
, const CVariant
&line3
)
164 m_requirements
.clear();
165 m_requirements
["type"] = "error";
166 m_requirements
["heading"] = heading
;
167 m_requirements
["line1"] = line1
;
168 m_requirements
["line2"] = line2
;
169 m_requirements
["line3"] = line3
;
172 void IDirectory::RequireAuthentication(const CURL
&url
)
174 m_requirements
.clear();
175 m_requirements
["type"] = "authenticate";
176 m_requirements
["url"] = url
.Get();