MythTV  master
videometadatalistmanager.cpp
Go to the documentation of this file.
1 #include <map>
2 
3 #include "mythdb.h"
5 #include "mythsorthelper.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 VideoMetadataPtr();
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 VideoMetadataPtr();
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 {
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 VideoMetadataPtr(new VideoMetadata());
122 }
123 
129  const QString &sql)
130 {
132  query.setForwardOnly(true);
133  QString BaseMetadataQuery(
134  "SELECT title, director, studio, plot, rating, year, releasedate,"
135  "userrating, length, playcount, filename, hash, showlevel, "
136  "coverfile, inetref, collectionref, homepage, childid, browse, watched, "
137  "playcommand, category, intid, trailer, screenshot, banner, fanart, "
138  "subtitle, tagline, season, episode, host, insertdate, processed, "
139  "contenttype FROM videometadata ");
140 
141  if (!sql.isEmpty())
142  BaseMetadataQuery.append(sql);
143 
144  query.prepare(BaseMetadataQuery);
145 
146  if (query.exec() && query.isActive())
147  {
148  while (query.next())
149  {
150  items.push_back(VideoMetadataPtr(new VideoMetadata(query)));
151  }
152  }
153  else
154  {
155  MythDB::DBError("Querying video metadata", query);
156  }
157 }
158 
160 {
161  m_imp->setList(list);
162 }
163 
166 {
167  return m_imp->getList();
168 }
169 
171 VideoMetadataListManager::byFilename(const QString &file_name) const
172 {
173  return m_imp->byFilename(file_name);
174 }
175 
177 VideoMetadataListManager::byID(unsigned int db_id) const
178 {
179  return m_imp->byID(db_id);
180 }
181 
182 bool VideoMetadataListManager::purgeByFilename(const QString &file_name)
183 {
184  return m_imp->purgeByFilename(file_name);
185 }
186 
187 bool VideoMetadataListManager::purgeByID(unsigned int db_id)
188 {
189  return m_imp->purgeByID(db_id);
190 }
191 
192 const QString meta_node::kEmptyPath;
193 
194 const QString& meta_node::getPath() const
195 {
196  return kEmptyPath;
197 }
198 
199 const QString& meta_node::getFQPath()
200 {
201  if (!m_fqPath.isEmpty())
202  return m_fqPath;
203 
204  if (m_parent && !m_pathRoot)
205  m_fqPath = m_parent->getFQPath() + "/" + getPath();
206  else
207  {
208  QString p = getPath();
209  if (p.startsWith("myth://"))
210  m_fqPath = p;
211  else
212  m_fqPath = ((!p.isEmpty() && p[0] != '/') ? "/" : "") + p;
213  }
214 
215  return m_fqPath;
216 }
217 
219 {
220  m_parent = parent;
221 }
222 
223 void meta_node::setPathRoot(bool is_root)
224 {
225  m_pathRoot = is_root;
226 }
227 
228 const QString meta_data_node::kMetaBug = "Bug";
229 
230 const QString& meta_data_node::getName() const
231 {
232  if (m_data)
233  {
234  return m_data->GetTitle();
235  }
236 
237  return kMetaBug;
238 }
239 
241 {
242  return m_data;
243 }
244 
246 {
247  return m_data;
248 }
249 
250 meta_dir_node::meta_dir_node(const QString &path, const QString &name,
251  meta_dir_node *parent, bool is_path_root,
252  QString host, QString prefix,
253  QVariant data)
254  : meta_node(parent, is_path_root), m_path(path), m_name(name),
255  m_host(std::move(host)), m_prefix(std::move(prefix)), m_data(std::move(data))
256 {
257  if (name.isEmpty())
258  m_name = path;
260 }
261 
263 {
264  std::shared_ptr<MythSortHelper>sh = getMythSortHelper();
265 
266  if (m_sortPath.isEmpty() and not m_path.isEmpty())
267  m_sortPath = sh->doPathname(m_path);
268  }
269 
270 void meta_dir_node::setName(const QString &name)
271 {
272  m_name = name;
273 }
274 
275 const QString &meta_dir_node::getName() const
276 {
277  return m_name;
278 }
279 
280 void meta_dir_node::SetHost(const QString &host)
281 {
282  m_host = host;
283 }
284 
285 const QString &meta_dir_node::GetHost() const
286 {
287  return m_host;
288 }
289 
290 void meta_dir_node::SetPrefix(const QString &prefix)
291 {
292  m_prefix = prefix;
293 }
294 
295 const QString &meta_dir_node::GetPrefix() const
296 {
297  return m_prefix;
298 }
299 
300 const QString &meta_dir_node::getPath() const
301 {
302  return m_path;
303 }
304 
305 const QString &meta_dir_node::getSortPath() const
306 {
307  return m_sortPath;
308 }
309 
310 void meta_dir_node::setPath(const QString &path, const QString &sortPath)
311 {
312  m_path = path;
313  m_sortPath = sortPath;
315 }
316 
317 void meta_dir_node::SetData(const QVariant &data)
318 {
319  m_data = data;
320 }
321 
322 const QVariant &meta_dir_node::GetData() const
323 {
324  return m_data;
325 }
326 
328 {
329  return m_data.isValid();
330 }
331 
333  const QString &name,
334  const QString &host,
335  const QString &prefix,
336  const QVariant &data)
337 {
338  return getSubDir(subdir, name, true, host, prefix, data);
339 }
340 
342 {
343  m_subdirs.push_back(subdir);
344 }
345 
347  const QString &name,
348  bool create,
349  const QString &host,
350  const QString &prefix,
351  const QVariant &data)
352 {
353  for (auto & entry : m_subdirs)
354  {
355  if (entry && (subdir == entry->getPath()))
356  {
357  return entry;
358  }
359  }
360 
361  if (create)
362  {
363  smart_dir_node node(new meta_dir_node(subdir, name, this, false,
364  host, prefix, data));
365  m_subdirs.push_back(node);
366  return node;
367  }
368 
369  return smart_dir_node();
370 }
371 
373 {
374  entry->setParent(this);
375  m_entries.push_back(entry);
376 }
377 
379 {
380  m_subdirs.clear();
381  m_entries.clear();
382 }
383 
385 {
386  return m_subdirs.empty() && m_entries.empty();
387 }
388 
390 {
391  return m_subdirs.size();
392 }
393 
394 meta_dir_list::iterator meta_dir_node::dirs_begin()
395 {
396  return m_subdirs.begin();
397 }
398 
399 meta_dir_list::iterator meta_dir_node::dirs_end()
400 {
401  return m_subdirs.end();
402 }
403 
404 meta_dir_list::const_iterator meta_dir_node::dirs_begin() const
405 {
406  return m_subdirs.begin();
407 }
408 
409 meta_dir_list::const_iterator meta_dir_node::dirs_end() const
410 {
411  return m_subdirs.end();
412 }
413 
414 meta_data_list::iterator meta_dir_node::entries_begin()
415 {
416  return m_entries.begin();
417 }
418 
419 meta_data_list::iterator meta_dir_node::entries_end()
420 {
421  return m_entries.end();
422 }
423 
424 meta_data_list::const_iterator meta_dir_node::entries_begin() const
425 {
426  return m_entries.begin();
427 }
428 
429 meta_data_list::const_iterator meta_dir_node::entries_end() const
430 {
431  return m_entries.end();
432 }
433 
434 // Returns true if this directory or any of its subdirectories
435 // have entries. TODO: cache this value
437 {
438  bool ret = !m_entries.empty();
439 
440  if (!ret)
441  {
442  for (const auto & subdir : m_subdirs)
443  {
444  ret = subdir->has_entries();
445  if (ret) break;
446  }
447  }
448 
449  return ret;
450 }
meta_node
Definition: videometadatalistmanager.h:39
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:204
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:126
VideoMetadata
Definition: videometadata.h:25
meta_dir_node::SetData
void SetData(const QVariant &data)
Definition: videometadatalistmanager.cpp:317
meta_dir_node::m_name
QString m_name
Definition: videometadatalistmanager.h:151
meta_dir_node::meta_dir_node
meta_dir_node()
Definition: videometadatalistmanager.h:96
meta_dir_node::has_entries
bool has_entries() const
Definition: videometadatalistmanager.cpp:436
getMythSortHelper
std::shared_ptr< MythSortHelper > getMythSortHelper(void)
Get a pointer to the MythSortHelper singleton.
Definition: mythsorthelper.cpp:132
meta_dir_node::m_sortPath
QString m_sortPath
Definition: videometadatalistmanager.h:150
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:223
meta_dir_node::m_host
QString m_host
Definition: videometadatalistmanager.h:152
VideoMetadataListManagerImp::purgeByFilename
bool purgeByFilename(const QString &file_name)
Definition: videometadatalistmanager.cpp:60
meta_dir_node::GetPrefix
const QString & GetPrefix() const
Definition: videometadatalistmanager.cpp:295
meta_data_node::getData
const VideoMetadata * getData() const
Definition: videometadatalistmanager.cpp:240
simple_ref_ptr
Definition: quicksp.h:25
meta_dir_node::m_subdirs
meta_dir_list m_subdirs
Definition: videometadatalistmanager.h:154
meta_node::kEmptyPath
static const QString kEmptyPath
Definition: videometadatalistmanager.h:57
VideoMetadataListManagerImp::metadata_list
VideoMetadataListManager::metadata_list metadata_list
Definition: videometadatalistmanager.cpp:11
VideoMetadataListManagerImp::VideoMetadataPtr
VideoMetadataListManager::VideoMetadataPtr VideoMetadataPtr
Definition: videometadatalistmanager.cpp:10
meta_dir_node::entries_end
entry_iterator entries_end()
Definition: videometadatalistmanager.cpp:419
meta_dir_node
Definition: videometadatalistmanager.h:83
meta_dir_node::setName
void setName(const QString &name)
Definition: videometadatalistmanager.cpp:270
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
VideoMetadataListManager::byFilename
VideoMetadataPtr byFilename(const QString &file_name) const
Definition: videometadatalistmanager.cpp:171
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:155
VideoMetadataListManager::getList
const metadata_list & getList() const
Definition: videometadatalistmanager.cpp:165
meta_dir_node::addEntry
void addEntry(const smart_meta_node &entry)
Definition: videometadatalistmanager.cpp:372
VideoMetadataListManager::setList
void setList(metadata_list &list)
Definition: videometadatalistmanager.cpp:159
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:346
mythsorthelper.h
meta_dir_node::DataIsValid
bool DataIsValid(void) const
Definition: videometadatalistmanager.cpp:327
meta_dir_node::dirs_begin
dir_iterator dirs_begin()
Definition: videometadatalistmanager.cpp:394
VideoMetadataListManager::~VideoMetadataListManager
~VideoMetadataListManager()
Definition: videometadatalistmanager.cpp:105
meta_dir_node::subdir_count
int subdir_count() const
Definition: videometadatalistmanager.cpp:389
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:71
VideoMetadataListManager::loadAllFromDatabase
static void loadAllFromDatabase(metadata_list &items, const QString &sql="")
Load videometadata database into memory.
Definition: videometadatalistmanager.cpp:128
VideoMetadataListManagerImp::byID
VideoMetadataPtr byID(unsigned int db_id) const
Definition: videometadatalistmanager.cpp:49
meta_data_node::getName
const QString & getName() const override
Definition: videometadatalistmanager.cpp:230
VideoMetadataListManagerImp::setList
void setList(metadata_list &list)
Definition: videometadatalistmanager.cpp:18
meta_dir_node::ensureSortFields
void ensureSortFields()
Definition: videometadatalistmanager.cpp:262
meta_node::m_parent
meta_node * m_parent
Definition: videometadatalistmanager.h:52
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:332
VideoMetadataListManagerImp::getList
const metadata_list & getList() const
Definition: videometadatalistmanager.cpp:32
meta_dir_node::m_prefix
QString m_prefix
Definition: videometadatalistmanager.h:153
hardwareprofile.config.p
p
Definition: config.py:33
meta_node::getFQPath
const QString & getFQPath()
Definition: videometadatalistmanager.cpp:199
VideoMetadataListManager::purgeByFilename
bool purgeByFilename(const QString &file_name)
Definition: videometadatalistmanager.cpp:182
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:178
meta_dir_node::empty
bool empty() const
Definition: videometadatalistmanager.cpp:384
meta_node::setParent
void setParent(meta_node *parent)
Definition: videometadatalistmanager.cpp:218
MSqlQuery::setForwardOnly
void setForwardOnly(bool f)
Definition: mythdbcon.h:207
meta_dir_node::setPath
void setPath(const QString &path, const QString &sortPath=nullptr)
Definition: videometadatalistmanager.cpp:310
meta_node::m_pathRoot
bool m_pathRoot
Definition: videometadatalistmanager.h:56
meta_dir_node::clear
void clear()
Definition: videometadatalistmanager.cpp:378
VideoMetadataListManagerImp::byFilename
VideoMetadataPtr byFilename(const QString &file_name) const
Definition: videometadatalistmanager.cpp:38
meta_dir_node::m_data
QVariant m_data
Definition: videometadatalistmanager.h:157
uint
unsigned int uint
Definition: compat.h:141
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:285
videometadatalistmanager.h
VideoMetadataListManagerImp::int_to_meta
std::map< unsigned int, metadata_list::iterator > int_to_meta
Definition: videometadatalistmanager.cpp:14
meta_dir_node::entries_begin
entry_iterator entries_begin()
Definition: videometadatalistmanager.cpp:414
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:187
meta_dir_node::getName
const QString & getName() const override
Definition: videometadatalistmanager.cpp:275
meta_data_node::m_data
VideoMetadata * m_data
Definition: videometadatalistmanager.h:70
meta_node::getPath
virtual const QString & getPath() const
Definition: videometadatalistmanager.cpp:194
smart_dir_node
simple_ref_ptr< meta_dir_node > smart_dir_node
Definition: videometadatalistmanager.h:76
meta_dir_node::m_path
QString m_path
Definition: videometadatalistmanager.h:149
VideoMetadataListManager::VideoMetadataPtr
simple_ref_ptr< VideoMetadata > VideoMetadataPtr
Definition: videometadatalistmanager.h:13
VideoMetadataListManagerImp
Definition: videometadatalistmanager.cpp:8
VideoMetadataListManager::loadOneFromDatabase
static VideoMetadataPtr loadOneFromDatabase(uint id)
Definition: videometadatalistmanager.cpp:111
meta_dir_node::getSortPath
const QString & getSortPath() const
Definition: videometadatalistmanager.cpp:305
VideoMetadataListManager::VideoMetadataListManager
VideoMetadataListManager()
Definition: videometadatalistmanager.cpp:100
meta_node::m_fqPath
QString m_fqPath
Definition: videometadatalistmanager.h:55
VideoMetadataListManager::m_imp
class VideoMetadataListManagerImp * m_imp
Definition: videometadatalistmanager.h:35
meta_dir_node::GetData
const QVariant & GetData() const
Definition: videometadatalistmanager.cpp:322
VideoMetadata::GetTitle
const QString & GetTitle() const
Definition: videometadata.cpp:1337
VideoMetadataListManager::metadata_list
std::list< VideoMetadataPtr > metadata_list
Definition: videometadatalistmanager.h:14
meta_dir_node::SetPrefix
void SetPrefix(const QString &prefix)
Definition: videometadatalistmanager.cpp:290
meta_dir_node::dirs_end
dir_iterator dirs_end()
Definition: videometadatalistmanager.cpp:399
meta_dir_node::getPath
const QString & getPath() const override
Definition: videometadatalistmanager.cpp:300
query
MSqlQuery query(MSqlQuery::InitCon())
meta_dir_node::SetHost
void SetHost(const QString &host)
Definition: videometadatalistmanager.cpp:280
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808
VideoMetadataListManager::byID
VideoMetadataPtr byID(unsigned int db_id) const
Definition: videometadatalistmanager.cpp:177