summaryrefslogtreecommitdiffstats
path: root/xbmc/filesystem/IDirectory.cpp
blob: b2ad59ab739867934caa78484ec5479413a21c31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "IDirectory.h"

#include "PasswordManager.h"
#include "URL.h"
#include "guilib/GUIKeyboardFactory.h"
#include "messaging/helpers/DialogOKHelper.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"

using namespace KODI::MESSAGING;
using namespace XFILE;

const CProfileManager *IDirectory::m_profileManager = nullptr;

void IDirectory::RegisterProfileManager(const CProfileManager &profileManager)
{
  m_profileManager = &profileManager;
}

void IDirectory::UnregisterProfileManager()
{
  m_profileManager = nullptr;
}

IDirectory::IDirectory()
{
  m_flags = DIR_FLAG_DEFAULTS;
}

IDirectory::~IDirectory(void) = default;

/*!
 \brief Test if file have an allowed extension, as specified with SetMask()
 \param strFile File to test
 \return \e true if file is allowed
 \note If extension is ".ifo", filename format must be "vide_ts.ifo" or
       "vts_##_0.ifo". If extension is ".dat", filename format must be
       "AVSEQ##(#).DAT", "ITEM###(#).DAT" or "MUSIC##(#).DAT".
 */
bool IDirectory::IsAllowed(const CURL& url) const
{
  if (m_strFileMask.empty())
    return true;

  // Check if strFile have an allowed extension
  if (!URIUtils::HasExtension(url, m_strFileMask))
    return false;

  // We should ignore all non dvd/vcd related ifo and dat files.
  if (URIUtils::HasExtension(url, ".ifo"))
  {
    std::string fileName = URIUtils::GetFileName(url);

    // Allow filenames of the form video_ts.ifo or vts_##_0.ifo

    return StringUtils::EqualsNoCase(fileName, "video_ts.ifo") ||
          (fileName.length() == 12 &&
           StringUtils::StartsWithNoCase(fileName, "vts_") &&
           StringUtils::EndsWithNoCase(fileName, "_0.ifo"));
  }

  if (URIUtils::HasExtension(url, ".dat"))
  {
    std::string fileName = URIUtils::GetFileName(url);
    std::string folder = URIUtils::GetDirectory(fileName);
    URIUtils::RemoveSlashAtEnd(folder);
    folder = URIUtils::GetFileName(folder);
    if (StringUtils::EqualsNoCase(folder, "vcd") ||
        StringUtils::EqualsNoCase(folder, "mpegav") ||
        StringUtils::EqualsNoCase(folder, "cdda"))
    {
      // Allow filenames of the form AVSEQ##(#).DAT, ITEM###(#).DAT
      // and MUSIC##(#).DAT
      return (fileName.length() == 11 || fileName.length() == 12) &&
             (StringUtils::StartsWithNoCase(fileName, "AVSEQ") ||
              StringUtils::StartsWithNoCase(fileName, "MUSIC") ||
              StringUtils::StartsWithNoCase(fileName, "ITEM"));
    }
  }
  return true;
}

/*!
 \brief Set a mask of extensions for the files in the directory.
 \param strMask Mask of file extensions that are allowed.

 The mask has to look like the following: \n
 \verbatim
 .m4a|.flac|.aac|
 \endverbatim
 So only *.m4a, *.flac, *.aac files will be retrieved with GetDirectory().
 */
void IDirectory::SetMask(const std::string& strMask)
{
  m_strFileMask = strMask;
  // ensure it's completed with a | so that filtering is easy.
  StringUtils::ToLower(m_strFileMask);
  if (m_strFileMask.size() && m_strFileMask[m_strFileMask.size() - 1] != '|')
    m_strFileMask += '|';
}

/*!
 \brief Set the flags for this directory handler.
 \param flags - \sa XFILE::DIR_FLAG for a description.
 */
void IDirectory::SetFlags(int flags)
{
  m_flags = flags;
}

bool IDirectory::ProcessRequirements()
{
  std::string type = m_requirements["type"].asString();
  if (type == "keyboard")
  {
    std::string input;
    if (CGUIKeyboardFactory::ShowAndGetInput(input, m_requirements["heading"], false, m_requirements["hidden"].asBoolean()))
    {
      m_requirements["input"] = input;
      return true;
    }
  }
  else if (type == "authenticate")
  {
    CURL url(m_requirements["url"].asString());
    if (CPasswordManager::GetInstance().PromptToAuthenticateURL(url))
    {
      m_requirements.clear();
      return true;
    }
  }
  else if (type == "error")
  {
    HELPERS::ShowOKDialogLines(CVariant{m_requirements["heading"]}, CVariant{m_requirements["line1"]}, CVariant{m_requirements["line2"]}, CVariant{m_requirements["line3"]});
  }
  m_requirements.clear();
  return false;
}

bool IDirectory::GetKeyboardInput(const CVariant &heading, std::string &input, bool hiddenInput)
{
  if (!m_requirements["input"].asString().empty())
  {
    input = m_requirements["input"].asString();
    return true;
  }
  m_requirements.clear();
  m_requirements["type"] = "keyboard";
  m_requirements["heading"] = heading;
  m_requirements["hidden"] = hiddenInput;
  return false;
}

void IDirectory::SetErrorDialog(const CVariant &heading, const CVariant &line1, const CVariant &line2, const CVariant &line3)
{
  m_requirements.clear();
  m_requirements["type"] = "error";
  m_requirements["heading"] = heading;
  m_requirements["line1"] = line1;
  m_requirements["line2"] = line2;
  m_requirements["line3"] = line3;
}

void IDirectory::RequireAuthentication(const CURL &url)
{
  m_requirements.clear();
  m_requirements["type"] = "authenticate";
  m_requirements["url"] = url.Get();
}