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 std::lock_guard
l(m_lock
);
33 return ProcessFile(path
) != m_readers
.end();
36 bool CXbtManager::GetFiles(const CURL
& path
, std::vector
<CXBTFFile
>& files
) const
38 std::lock_guard
l(m_lock
);
39 const auto& reader
= ProcessFile(path
);
40 if (reader
== m_readers
.end())
43 files
= reader
->second
.reader
->GetFiles();
47 bool CXbtManager::GetReader(const CURL
& path
, CXBTFReaderPtr
& reader
) const
49 std::lock_guard
l(m_lock
);
50 const auto& it
= ProcessFile(path
);
51 if (it
== m_readers
.end())
54 reader
= it
->second
.reader
;
58 void CXbtManager::Release(const CURL
& path
)
60 std::lock_guard
l(m_lock
);
61 const auto& it
= GetReader(path
);
62 if (it
== m_readers
.end())
68 CXbtManager::XBTFReaders::iterator
CXbtManager::GetReader(const CURL
& path
) const
70 return GetReader(NormalizePath(path
));
73 CXbtManager::XBTFReaders::iterator
CXbtManager::GetReader(const std::string
& path
) const
76 return m_readers
.end();
78 return m_readers
.find(path
);
81 void CXbtManager::RemoveReader(XBTFReaders::iterator readerIterator
) const
83 if (readerIterator
== m_readers
.end())
87 readerIterator
->second
.reader
->Close();
89 // and remove it from the map
90 m_readers
.erase(readerIterator
);
93 CXbtManager::XBTFReaders::const_iterator
CXbtManager::ProcessFile(const CURL
& path
) const
95 std::string filePath
= NormalizePath(path
);
97 // check if the file is known
98 auto it
= GetReader(filePath
);
99 if (it
!= m_readers
.end())
101 // check if the XBT file has been modified
102 if (it
->second
.reader
->GetLastModificationTimestamp() <= it
->second
.lastModification
)
105 // the XBT file has been modified so close and remove it from the cache
106 // it will be re-opened by the following logic
110 // try to read the file
111 CXBTFReaderPtr
reader(new CXBTFReader());
112 if (!reader
->Open(filePath
))
113 return m_readers
.end();
115 XBTFReader xbtfReader
= {
117 reader
->GetLastModificationTimestamp()
119 std::pair
<XBTFReaders::iterator
, bool> result
= m_readers
.insert(std::make_pair(filePath
, xbtfReader
));
123 std::string
CXbtManager::NormalizePath(const CURL
& path
)
125 if (path
.IsProtocol("xbt"))
126 return path
.GetHostName();