[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / filesystem / XbtManager.cpp
blobbb091f1cd9f31d5d6453a5287e93980a76f77a47
1 /*
2 * Copyright (C) 2015-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.
7 */
9 #include "XbtManager.h"
11 #include "URL.h"
12 #include "guilib/XBTF.h"
13 #include "guilib/XBTFReader.h"
15 #include <utility>
17 namespace XFILE
20 CXbtManager::CXbtManager() = default;
22 CXbtManager::~CXbtManager() = default;
24 CXbtManager& CXbtManager::GetInstance()
26 static CXbtManager xbtManager;
27 return xbtManager;
30 bool CXbtManager::HasFiles(const CURL& path) const
32 return ProcessFile(path) != m_readers.end();
35 bool CXbtManager::GetFiles(const CURL& path, std::vector<CXBTFFile>& files) const
37 const auto& reader = ProcessFile(path);
38 if (reader == m_readers.end())
39 return false;
41 files = reader->second.reader->GetFiles();
42 return true;
45 bool CXbtManager::GetReader(const CURL& path, CXBTFReaderPtr& reader) const
47 const auto& it = ProcessFile(path);
48 if (it == m_readers.end())
49 return false;
51 reader = it->second.reader;
52 return true;
55 void CXbtManager::Release(const CURL& path)
57 const auto& it = GetReader(path);
58 if (it == m_readers.end())
59 return;
61 RemoveReader(it);
64 CXbtManager::XBTFReaders::iterator CXbtManager::GetReader(const CURL& path) const
66 return GetReader(NormalizePath(path));
69 CXbtManager::XBTFReaders::iterator CXbtManager::GetReader(const std::string& path) const
71 if (path.empty())
72 return m_readers.end();
74 return m_readers.find(path);
77 void CXbtManager::RemoveReader(XBTFReaders::iterator readerIterator) const
79 if (readerIterator == m_readers.end())
80 return;
82 // close the reader
83 readerIterator->second.reader->Close();
85 // and remove it from the map
86 m_readers.erase(readerIterator);
89 CXbtManager::XBTFReaders::const_iterator CXbtManager::ProcessFile(const CURL& path) const
91 std::string filePath = NormalizePath(path);
93 // check if the file is known
94 auto it = GetReader(filePath);
95 if (it != m_readers.end())
97 // check if the XBT file has been modified
98 if (it->second.reader->GetLastModificationTimestamp() <= it->second.lastModification)
99 return it;
101 // the XBT file has been modified so close and remove it from the cache
102 // it will be re-opened by the following logic
103 RemoveReader(it);
106 // try to read the file
107 CXBTFReaderPtr reader(new CXBTFReader());
108 if (!reader->Open(filePath))
109 return m_readers.end();
111 XBTFReader xbtfReader = {
112 reader,
113 reader->GetLastModificationTimestamp()
115 std::pair<XBTFReaders::iterator, bool> result = m_readers.insert(std::make_pair(filePath, xbtfReader));
116 return result.first;
119 std::string CXbtManager::NormalizePath(const CURL& path)
121 if (path.IsProtocol("xbt"))
122 return path.GetHostName();
124 return path.Get();