summaryrefslogtreecommitdiffstats
path: root/xbmc/filesystem/XbtManager.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 18:07:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 18:07:22 +0000
commitc04dcc2e7d834218ef2d4194331e383402495ae1 (patch)
tree7333e38d10d75386e60f336b80c2443c1166031d /xbmc/filesystem/XbtManager.cpp
parentInitial commit. (diff)
downloadkodi-c04dcc2e7d834218ef2d4194331e383402495ae1.tar.xz
kodi-c04dcc2e7d834218ef2d4194331e383402495ae1.zip
Adding upstream version 2:20.4+dfsg.upstream/2%20.4+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'xbmc/filesystem/XbtManager.cpp')
-rw-r--r--xbmc/filesystem/XbtManager.cpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/xbmc/filesystem/XbtManager.cpp b/xbmc/filesystem/XbtManager.cpp
new file mode 100644
index 0000000..bb091f1
--- /dev/null
+++ b/xbmc/filesystem/XbtManager.cpp
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2015-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 "XbtManager.h"
+
+#include "URL.h"
+#include "guilib/XBTF.h"
+#include "guilib/XBTFReader.h"
+
+#include <utility>
+
+namespace XFILE
+{
+
+CXbtManager::CXbtManager() = default;
+
+CXbtManager::~CXbtManager() = default;
+
+CXbtManager& CXbtManager::GetInstance()
+{
+ static CXbtManager xbtManager;
+ return xbtManager;
+}
+
+bool CXbtManager::HasFiles(const CURL& path) const
+{
+ return ProcessFile(path) != m_readers.end();
+}
+
+bool CXbtManager::GetFiles(const CURL& path, std::vector<CXBTFFile>& files) const
+{
+ const auto& reader = ProcessFile(path);
+ if (reader == m_readers.end())
+ return false;
+
+ files = reader->second.reader->GetFiles();
+ return true;
+}
+
+bool CXbtManager::GetReader(const CURL& path, CXBTFReaderPtr& reader) const
+{
+ const auto& it = ProcessFile(path);
+ if (it == m_readers.end())
+ return false;
+
+ reader = it->second.reader;
+ return true;
+}
+
+void CXbtManager::Release(const CURL& path)
+{
+ const auto& it = GetReader(path);
+ if (it == m_readers.end())
+ return;
+
+ RemoveReader(it);
+}
+
+CXbtManager::XBTFReaders::iterator CXbtManager::GetReader(const CURL& path) const
+{
+ return GetReader(NormalizePath(path));
+}
+
+CXbtManager::XBTFReaders::iterator CXbtManager::GetReader(const std::string& path) const
+{
+ if (path.empty())
+ return m_readers.end();
+
+ return m_readers.find(path);
+}
+
+void CXbtManager::RemoveReader(XBTFReaders::iterator readerIterator) const
+{
+ if (readerIterator == m_readers.end())
+ return;
+
+ // close the reader
+ readerIterator->second.reader->Close();
+
+ // and remove it from the map
+ m_readers.erase(readerIterator);
+}
+
+CXbtManager::XBTFReaders::const_iterator CXbtManager::ProcessFile(const CURL& path) const
+{
+ std::string filePath = NormalizePath(path);
+
+ // check if the file is known
+ auto it = GetReader(filePath);
+ if (it != m_readers.end())
+ {
+ // check if the XBT file has been modified
+ if (it->second.reader->GetLastModificationTimestamp() <= it->second.lastModification)
+ return it;
+
+ // the XBT file has been modified so close and remove it from the cache
+ // it will be re-opened by the following logic
+ RemoveReader(it);
+ }
+
+ // try to read the file
+ CXBTFReaderPtr reader(new CXBTFReader());
+ if (!reader->Open(filePath))
+ return m_readers.end();
+
+ XBTFReader xbtfReader = {
+ reader,
+ reader->GetLastModificationTimestamp()
+ };
+ std::pair<XBTFReaders::iterator, bool> result = m_readers.insert(std::make_pair(filePath, xbtfReader));
+ return result.first;
+}
+
+std::string CXbtManager::NormalizePath(const CURL& path)
+{
+ if (path.IsProtocol("xbt"))
+ return path.GetHostName();
+
+ return path.Get();
+}
+
+}