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.
9 #include "XbtManager.h"
12 #include "guilib/XBTF.h"
13 #include "guilib/XBTFReader.h"
20 CXbtManager::CXbtManager() = default;
22 CXbtManager::~CXbtManager() = default;
24 CXbtManager
& CXbtManager::GetInstance()
26 static CXbtManager 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())
41 files
= reader
->second
.reader
->GetFiles();
45 bool CXbtManager::GetReader(const CURL
& path
, CXBTFReaderPtr
& reader
) const
47 const auto& it
= ProcessFile(path
);
48 if (it
== m_readers
.end())
51 reader
= it
->second
.reader
;
55 void CXbtManager::Release(const CURL
& path
)
57 const auto& it
= GetReader(path
);
58 if (it
== m_readers
.end())
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
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())
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
)
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
106 // try to read the file
107 CXBTFReaderPtr
reader(new CXBTFReader());
108 if (!reader
->Open(filePath
))
109 return m_readers
.end();
111 XBTFReader xbtfReader
= {
113 reader
->GetLastModificationTimestamp()
115 std::pair
<XBTFReaders::iterator
, bool> result
= m_readers
.insert(std::make_pair(filePath
, xbtfReader
));
119 std::string
CXbtManager::NormalizePath(const CURL
& path
)
121 if (path
.IsProtocol("xbt"))
122 return path
.GetHostName();