MythTV  master
videometadatalistmanager.cpp
Go to the documentation of this file.
1 #include <map>
2 
3 #include "libmythbase/mythdb.h"
6 
8 {
9  public:
12 
13  private:
14  using int_to_meta = std::map<unsigned int, metadata_list::iterator>;
15  using string_to_meta = std::map<QString, metadata_list::iterator>;
16 
17  public:
18  void setList(metadata_list &list)
19  {
20  m_idMap.clear();
21  m_fileMap.clear();
22  m_metaList.swap(list);
23 
24  for (auto p = m_metaList.begin(); p != m_metaList.end(); ++p)
25  {
26  m_idMap.insert(int_to_meta::value_type((*p)->GetID(), p));
27  m_fileMap.insert(
28  string_to_meta::value_type((*p)->GetFilename(), p));
29  }
30  }
31 
32  const metadata_list &getList() const
33  {
34  return m_metaList;
35  }
36 
37 
38  VideoMetadataPtr byFilename(const QString &file_name) const
39  {
40  //NOLINTNEXTLINE(modernize-use-auto)
41  string_to_meta::const_iterator p = m_fileMap.find(file_name);
42  if (p != m_fileMap.end())
43  {
44  return *(p->second);
45  }
46  return {};
47  }
48 
49  VideoMetadataPtr byID(unsigned int db_id) const
50  {
51  //NOLINTNEXTLINE(modernize-use-auto)
52  int_to_meta::const_iterator p = m_idMap.find(db_id);
53  if (p != m_idMap.end())
54  {
55  return *(p->second);
56  }
57  return {};
58  }
59 
60  bool purgeByFilename(const QString &file_name)
61  {
62  return purge_entry(byFilename(file_name));
63  }
64 
65  bool purgeByID(unsigned int db_id)
66  {
67  return purge_entry(byID(db_id));
68  }
69 
70  private:
71  bool purge_entry(const VideoMetadataPtr& metadata)
72  {
73  if (metadata)
74  {
75  auto im = m_idMap.find(metadata->GetID());
76 
77  if (im != m_idMap.end())
78  {
79  auto mdi = im->second;
80  (*mdi)->DeleteFromDatabase();
81 
82  m_idMap.erase(im);
83  auto sm = m_fileMap.find(metadata->GetFilename());
84  if (sm != m_fileMap.end())
85  m_fileMap.erase(sm);
86  m_metaList.erase(mdi);
87  return true;
88  }
89  }
90 
91  return false;
92  }
93 
94  private:
98 };
99 
101  : m_imp(new VideoMetadataListManagerImp())
102 {
103 }
104 
106 {
107  delete m_imp;
108 }
109 
112 {
113  QString sql = QString("WHERE intid = %1 LIMIT 1").arg(id);
114  metadata_list item;
115  loadAllFromDatabase(item, sql);
116  if (!item.empty())
117  {
118  return item.front();
119  }
120 
121  return {new VideoMetadata()};
122 }
123 
129  const QString &sql,
130  const QString &bindValue)
131 {
132  MSqlQuery query(MSqlQuery::InitCon());
133  query.setForwardOnly(true);
134  QString BaseMetadataQuery(
135  "SELECT title, director, studio, plot, rating, year, releasedate,"
136  "userrating, length, playcount, filename, hash, showlevel, "
137  "coverfile, inetref, collectionref, homepage, childid, browse, watched, "
138  "playcommand, category, intid, trailer, screenshot, banner, fanart, "
139  "subtitle, tagline, season, episode, host, insertdate, processed, "
140  "contenttype FROM videometadata ");
141 
142  if (!sql.isEmpty())
143  BaseMetadataQuery.append(sql);
144 
145  query.prepare(BaseMetadataQuery);
146  if (!bindValue.isEmpty())
147  query.bindValue(":BINDVALUE", bindValue);
148 
149  if (query.exec() && query.isActive())
150  {
151  while (query.next())
152  {
153  items.push_back(VideoMetadataPtr(new VideoMetadata(query)));
154  }
155  }
156  else
157  {
158  MythDB::DBError("Querying video metadata", query);
159  }
160 }
161 
163 {
164  m_imp->setList(list);
165 }
166 
169 {
170  return m_imp->getList();
171 }
172 
174 VideoMetadataListManager::byFilename(const QString &file_name) const
175 {
176  return m_imp->byFilename(file_name);
177 }
178 
180 VideoMetadataListManager::byID(unsigned int db_id) const
181 {
182  return m_imp->byID(db_id);
183 }
184 
185 bool VideoMetadataListManager::purgeByFilename(const QString &file_name)
186 {
187  return m_imp->purgeByFilename(file_name);
188 }
189 
190 bool VideoMetadataListManager::purgeByID(unsigned int db_id)
191 {
192  return m_imp->purgeByID(db_id);
193 }
194 
195 const QString meta_node::kEmptyPath;
196 
197 const QString& meta_node::getPath() const
198 {
199  return kEmptyPath;
200 }
201 
202 const QString& meta_node::getFQPath()
203 {
204  if (!m_fqPath.isEmpty())
205  return m_fqPath;
206 
207  if (m_parent && !m_pathRoot)
208  m_fqPath = m_parent->getFQPath() + "/" + getPath();
209  else
210  {
211  QString p = getPath();
212  if (p.startsWith("myth://"))
213  m_fqPath = p;
214  else
215  m_fqPath = ((!p.isEmpty() && p[0] != '/') ? "/" : "") + p;
216  }
217 
218  return m_fqPath;
219 }
220 
222 {
223  m_parent = parent;
224 }
225 
226 void meta_node::setPathRoot(bool is_root)
227 {
228  m_pathRoot = is_root;
229 }
230 
231 const QString meta_data_node::kMetaBug = "Bug";
232 
233 const QString& meta_data_node::getName() const
234 {
235  if (m_data)
236  {
237  return m_data->GetTitle();
238  }
239 
240  return kMetaBug;
241 }
242 
244 {
245  return m_data;
246 }
247 
249 {
250  return m_data;
251 }
252 
253 meta_dir_node::meta_dir_node(const QString &path, const QString &name,
254  meta_dir_node *parent, bool is_path_root,
255  QString host, QString prefix,
256  QVariant data)
257  : meta_node(parent, is_path_root), m_path(path), m_name(name),
258  m_host(std::move(host)), m_prefix(std::move(prefix)), m_data(std::move(data))
259 {
260  if (name.isEmpty())
261  m_name = path;
263 }
264 
266 {
267  std::shared_ptr<MythSortHelper>sh = getMythSortHelper();
268 
269  if (m_sortPath.isEmpty() and not m_path.isEmpty())
271  }
272 
273 void meta_dir_node::setName(const QString &name)
274 {
275  m_name = name;
276 }
277 
278 const QString &meta_dir_node::getName() const
279 {
280  return m_name;
281 }
282 
283 void meta_dir_node::SetHost(const QString &host)
284 {
285  m_host = host;
286 }
287 
288 const QString &meta_dir_node::GetHost() const
289 {
290  return m_host;
291 }
292 
293 void meta_dir_node::SetPrefix(const QString &prefix)
294 {
295  m_prefix = prefix;
296 }
297 
298 const QString &meta_dir_node::GetPrefix() const
299 {
300  return m_prefix;
301 }
302 
303 const QString &meta_dir_node::getPath() const
304 {
305  return m_path;
306 }
307 
308 const QString &meta_dir_node::getSortPath() const
309 {
310  return m_sortPath;
311 }
312 
313 void meta_dir_node::setPath(const QString &path, const QString &sortPath)
314 {
315  m_path = path;
316  m_sortPath = sortPath;
318 }
319 
320 void meta_dir_node::SetData(const QVariant &data)
321 {
322  m_data = data;
323 }
324 
325 const QVariant &meta_dir_node::GetData() const
326 {
327  return m_data;
328 }
329 
331 {
332  return m_data.isValid();
333 }
334 
336  const QString &name,
337  const QString &host,
338  const QString &prefix,
339  const QVariant &data)
340 {
341  return getSubDir(subdir, name, true, host, prefix, data);
342 }
343 
345 {
346  m_subdirs.push_back(subdir);
347 }
348 
350  const QString &name,
351  bool create,
352  const QString &host,
353  const QString &prefix,
354  const QVariant &data)
355 {
356  for (auto & entry : m_subdirs)
357  {
358  if (entry && (subdir == entry->getPath()))
359  {
360  return entry;
361  }
362  }
363 
364  if (create)
365  {
366  smart_dir_node node(new meta_dir_node(subdir, name, this, false,
367  host, prefix, data));
368  m_subdirs.push_back(node);
369  return node;
370  }
371 
372  return {};
373 }
374 
376 {
377  entry->setParent(this);
378  m_entries.push_back(entry);
379 }
380 
382 {
383  m_subdirs.clear();
384  m_entries.clear();
385 }
386 
388 {
389  return m_subdirs.empty() && m_entries.empty();
390 }
391 
393 {
394  return m_subdirs.size();
395 }
396 
397 meta_dir_list::iterator meta_dir_node::dirs_begin()
398 {
399  return m_subdirs.begin();
400 }
401 
402 meta_dir_list::iterator meta_dir_node::dirs_end()
403 {
404  return m_subdirs.end();
405 }
406 
407 meta_dir_list::const_iterator meta_dir_node::dirs_begin() const
408 {
409  return m_subdirs.begin();
410 }
411 
412 meta_dir_list::const_iterator meta_dir_node::dirs_end() const
413 {
414  return m_subdirs.end();
415 }
416 
417 meta_data_list::iterator meta_dir_node::entries_begin()
418 {
419  return m_entries.begin();
420 }
421 
422 meta_data_list::iterator meta_dir_node::entries_end()
423 {
424  return m_entries.end();
425 }
426 
427 meta_data_list::const_iterator meta_dir_node::entries_begin() const
428 {
429  return m_entries.begin();
430 }
431 
432 meta_data_list::const_iterator meta_dir_node::entries_end() const
433 {
434  return m_entries.end();
435 }
436 
437 // Returns true if this directory or any of its subdirectories
438 // have entries. TODO: cache this value
440 {
441  bool ret = !m_entries.empty();
442 
443  if (!ret)
444  {
445  for (const auto & subdir : m_subdirs)
446  {
447  if (subdir == nullptr)
448  continue;
449  ret = subdir->has_entries();
450  if (ret) break;
451  }
452  }
453 
454  return ret;
455 }
meta_node
Definition: videometadatalistmanager.h:39
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:215
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:812
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:127
VideoMetadata
Definition: videometadata.h:24
meta_dir_node::entries_begin
entry_iterator entries_begin()
Definition: videometadatalistmanager.cpp:417
meta_dir_node::SetData
void SetData(const QVariant &data)
Definition: videometadatalistmanager.cpp:320
meta_dir_node::m_name
QString m_name
Definition: videometadatalistmanager.h:154
meta_dir_node::meta_dir_node
meta_dir_node()
Definition: videometadatalistmanager.h:97
meta_dir_node::has_entries
bool has_entries() const
Definition: videometadatalistmanager.cpp:439
getMythSortHelper
std::shared_ptr< MythSortHelper > getMythSortHelper(void)
Get a pointer to the MythSortHelper singleton.
Definition: mythsorthelper.cpp:129
meta_dir_node::m_sortPath
QString m_sortPath
Definition: videometadatalistmanager.h:153
VideoMetadataListManagerImp::purge_entry
bool purge_entry(const VideoMetadataPtr &metadata)
Definition: videometadatalistmanager.cpp:71
mythdb.h
meta_node::setPathRoot
void setPathRoot(bool is_root=true)
Definition: videometadatalistmanager.cpp:226
meta_dir_node::m_host
QString m_host
Definition: videometadatalistmanager.h:155
VideoMetadataListManagerImp::purgeByFilename
bool purgeByFilename(const QString &file_name)
Definition: videometadatalistmanager.cpp:60
meta_dir_node::GetPrefix
const QString & GetPrefix() const
Definition: videometadatalistmanager.cpp:298
simple_ref_ptr
Definition: quicksp.h:24
meta_dir_node::m_subdirs
meta_dir_list m_subdirs
Definition: videometadatalistmanager.h:157
meta_node::kEmptyPath
static const QString kEmptyPath
Definition: videometadatalistmanager.h:58
VideoMetadataListManagerImp::metadata_list
VideoMetadataListManager::metadata_list metadata_list
Definition: videometadatalistmanager.cpp:11
meta_data_node::getData
const VideoMetadata * getData() const
Definition: videometadatalistmanager.cpp:243
meta_dir_node::dirs_end
dir_iterator dirs_end()
Definition: videometadatalistmanager.cpp:402
meta_dir_node
Definition: videometadatalistmanager.h:83
meta_dir_node::setName
void setName(const QString &name)
Definition: videometadatalistmanager.cpp:273
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:618
VideoMetadataListManager::byFilename
VideoMetadataPtr byFilename(const QString &file_name) const
Definition: videometadatalistmanager.cpp:174
VideoMetadataListManagerImp::purgeByID
bool purgeByID(unsigned int db_id)
Definition: videometadatalistmanager.cpp:65
meta_dir_node::m_entries
meta_data_list m_entries
Definition: videometadatalistmanager.h:158
VideoMetadataListManager::getList
const metadata_list & getList() const
Definition: videometadatalistmanager.cpp:168
meta_dir_node::addEntry
void addEntry(const smart_meta_node &entry)
Definition: videometadatalistmanager.cpp:375
VideoMetadataListManager::setList
void setList(metadata_list &list)
Definition: videometadatalistmanager.cpp:162
meta_dir_node::getSubDir
smart_dir_node getSubDir(const QString &subdir, const QString &name="", bool create=true, const QString &host="", const QString &prefix="", const QVariant &data=QVariant())
Definition: videometadatalistmanager.cpp:349
mythsorthelper.h
meta_dir_node::DataIsValid
bool DataIsValid(void) const
Definition: videometadatalistmanager.cpp:330
meta_dir_node::entries_end
entry_iterator entries_end()
Definition: videometadatalistmanager.cpp:422
VideoMetadataListManager::~VideoMetadataListManager
~VideoMetadataListManager()
Definition: videometadatalistmanager.cpp:105
meta_dir_node::subdir_count
int subdir_count() const
Definition: videometadatalistmanager.cpp:392
hardwareprofile.distros.mythtv_data.data_mythtv.prefix
string prefix
Definition: data_mythtv.py:40
meta_data_node::kMetaBug
static const QString kMetaBug
Definition: videometadatalistmanager.h:72
VideoMetadataListManagerImp::byID
VideoMetadataPtr byID(unsigned int db_id) const
Definition: videometadatalistmanager.cpp:49
MythSortHelper::doPathname
QString doPathname(const QString &pathname) const
Create the sortable form of an item.
Definition: mythsorthelper.cpp:196
meta_data_node::getName
const QString & getName() const override
Definition: videometadatalistmanager.cpp:233
VideoMetadataListManager::loadAllFromDatabase
static void loadAllFromDatabase(metadata_list &items, const QString &sql="", const QString &bindValue="")
Load videometadata database into memory.
Definition: videometadatalistmanager.cpp:128
VideoMetadataListManagerImp::setList
void setList(metadata_list &list)
Definition: videometadatalistmanager.cpp:18
meta_dir_node::ensureSortFields
void ensureSortFields()
Definition: videometadatalistmanager.cpp:265
meta_node::m_parent
meta_node * m_parent
Definition: videometadatalistmanager.h:53
VideoMetadataListManagerImp::m_fileMap
string_to_meta m_fileMap
Definition: videometadatalistmanager.cpp:97
meta_dir_node::addSubDir
smart_dir_node addSubDir(const QString &subdir, const QString &name="", const QString &host="", const QString &prefix="", const QVariant &data=QVariant())
Definition: videometadatalistmanager.cpp:335
VideoMetadataListManagerImp::getList
const metadata_list & getList() const
Definition: videometadatalistmanager.cpp:32
meta_dir_node::m_prefix
QString m_prefix
Definition: videometadatalistmanager.h:156
hardwareprofile.config.p
p
Definition: config.py:33
meta_node::getFQPath
const QString & getFQPath()
Definition: videometadatalistmanager.cpp:202
VideoMetadataListManager::purgeByFilename
bool purgeByFilename(const QString &file_name)
Definition: videometadatalistmanager.cpp:185
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:550
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:225
meta_dir_node::empty
bool empty() const
Definition: videometadatalistmanager.cpp:387
meta_node::setParent
void setParent(meta_node *parent)
Definition: videometadatalistmanager.cpp:221
MSqlQuery::setForwardOnly
void setForwardOnly(bool f)
Definition: mythdbcon.h:218
meta_dir_node::setPath
void setPath(const QString &path, const QString &sortPath=nullptr)
Definition: videometadatalistmanager.cpp:313
meta_node::m_pathRoot
bool m_pathRoot
Definition: videometadatalistmanager.h:57
meta_dir_node::clear
void clear()
Definition: videometadatalistmanager.cpp:381
VideoMetadataListManagerImp::byFilename
VideoMetadataPtr byFilename(const QString &file_name) const
Definition: videometadatalistmanager.cpp:38
meta_dir_node::m_data
QVariant m_data
Definition: videometadatalistmanager.h:160
uint
unsigned int uint
Definition: compat.h:81
VideoMetadataListManagerImp::string_to_meta
std::map< QString, metadata_list::iterator > string_to_meta
Definition: videometadatalistmanager.cpp:15
meta_dir_node::GetHost
const QString & GetHost() const
Definition: videometadatalistmanager.cpp:288
videometadatalistmanager.h
VideoMetadataListManagerImp::int_to_meta
std::map< unsigned int, metadata_list::iterator > int_to_meta
Definition: videometadatalistmanager.cpp:14
VideoMetadataListManagerImp::m_metaList
metadata_list m_metaList
Definition: videometadatalistmanager.cpp:95
VideoMetadataListManagerImp::m_idMap
int_to_meta m_idMap
Definition: videometadatalistmanager.cpp:96
VideoMetadataListManager::purgeByID
bool purgeByID(unsigned int db_id)
Definition: videometadatalistmanager.cpp:190
meta_dir_node::getName
const QString & getName() const override
Definition: videometadatalistmanager.cpp:278
meta_data_node::m_data
VideoMetadata * m_data
Definition: videometadatalistmanager.h:71
meta_node::getPath
virtual const QString & getPath() const
Definition: videometadatalistmanager.cpp:197
meta_dir_node::dirs_begin
dir_iterator dirs_begin()
Definition: videometadatalistmanager.cpp:397
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:888
meta_dir_node::m_path
QString m_path
Definition: videometadatalistmanager.h:152
VideoMetadataListManager::VideoMetadataPtr
simple_ref_ptr< VideoMetadata > VideoMetadataPtr
Definition: videometadatalistmanager.h:13
VideoMetadataListManagerImp
Definition: videometadatalistmanager.cpp:7
VideoMetadataListManager::loadOneFromDatabase
static VideoMetadataPtr loadOneFromDatabase(uint id)
Definition: videometadatalistmanager.cpp:111
meta_dir_node::getSortPath
const QString & getSortPath() const
Definition: videometadatalistmanager.cpp:308
VideoMetadataListManager::VideoMetadataListManager
VideoMetadataListManager()
Definition: videometadatalistmanager.cpp:100
meta_node::m_fqPath
QString m_fqPath
Definition: videometadatalistmanager.h:56
VideoMetadataListManager::m_imp
class VideoMetadataListManagerImp * m_imp
Definition: videometadatalistmanager.h:36
meta_dir_node::GetData
const QVariant & GetData() const
Definition: videometadatalistmanager.cpp:325
VideoMetadata::GetTitle
const QString & GetTitle() const
Definition: videometadata.cpp:1544
VideoMetadataListManager::metadata_list
std::list< VideoMetadataPtr > metadata_list
Definition: videometadatalistmanager.h:14
meta_dir_node::SetPrefix
void SetPrefix(const QString &prefix)
Definition: videometadatalistmanager.cpp:293
meta_dir_node::getPath
const QString & getPath() const override
Definition: videometadatalistmanager.cpp:303
meta_dir_node::SetHost
void SetHost(const QString &host)
Definition: videometadatalistmanager.cpp:283
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:837
VideoMetadataListManager::byID
VideoMetadataPtr byID(unsigned int db_id) const
Definition: videometadatalistmanager.cpp:180