MythTV  master
playbackstate.cpp
Go to the documentation of this file.
1 #include "playbackstate.h"
2 
3 // MythTV
4 #include "libmyth/mythcontext.h"
5 #include "libmythbase/mythdb.h"
10 
12 {
13  m_alwaysShowWatchedProgress = gCoreContext->GetBoolSetting("AlwaysShowWatchedProgress", false);
14 }
15 
17 {
18  LOG(VB_GENERAL, LOG_INFO, "Query playback state for all videos from database");
19 
20  m_fileMarkup.clear();
21 
22  QueryData();
23 
24  LOG(VB_GENERAL, LOG_INFO, QString("Collected playbackstate for %1 videos").arg(m_fileMarkup.count()));
25 }
26 
27 void PlaybackState::Update(const QString &filename)
28 {
29  if (!filename.isEmpty())
30  {
32  }
33 }
34 
35 void PlaybackState::QueryData(const QString &filterFilename)
36 {
38 
39  if (filterFilename.isEmpty())
40  {
41  query.prepare("SELECT filename, type, mark, `offset` "
42  "FROM filemarkup "
43  "WHERE type IN (:BOOKMARK, :FRAMES, :PLAYPOS) "
44  "ORDER BY filename");
45  }
46  else
47  {
48  query.prepare("SELECT filename, type, mark, `offset` "
49  "FROM filemarkup "
50  "WHERE type IN (:BOOKMARK, :FRAMES, :PLAYPOS) "
51  "AND filename = :FILENAME "
52  "ORDER BY filename");
53  query.bindValue(":FILENAME", filterFilename);
54  }
55  query.bindValue(":BOOKMARK", MARK_BOOKMARK);
56  query.bindValue(":FRAMES", MARK_TOTAL_FRAMES);
57  query.bindValue(":PLAYPOS", MARK_UTIL_LASTPLAYPOS);
58 
59  if (!query.exec())
60  {
61  MythDB::DBError("PlaybackState", query);
62  return;
63  }
64 
65  QString lastFilename;
66  Markup currMarks;
67  while (query.next())
68  {
69  const QString filename = query.value(0).toString();
70  if (!lastFilename.isEmpty() && filename != lastFilename)
71  {
72  m_fileMarkup[lastFilename] = currMarks;
73  currMarks = Markup();
74  }
75  switch(query.value(1).toInt())
76  {
77  case MARK_BOOKMARK:
78  currMarks.bookmarkPos = query.value(2).toLongLong();
79  break;
80  case MARK_TOTAL_FRAMES:
81  currMarks.totalFrames = query.value(3).toLongLong();
82  break;
84  currMarks.lastPlayPos = query.value(2).toLongLong();
85  break;
86  }
87  lastFilename = filename;
88  }
89  m_fileMarkup[lastFilename] = currMarks;
90 }
91 
92 bool PlaybackState::HasBookmark(const QString &filename) const
93 {
94  auto it = m_fileMarkup.constFind(filename);
95  if (it == m_fileMarkup.constEnd())
96  {
97  return false;
98  }
99  return it.value().bookmarkPos > 0;
100 }
101 
102 uint64_t PlaybackState::GetLastPlayPos(const QString &filename) const
103 {
104  auto it = m_fileMarkup.constFind(filename);
105  if (it == m_fileMarkup.constEnd())
106  {
107  return 0;
108  }
109  return it.value().lastPlayPos;
110 }
111 
113 {
114  auto it = m_fileMarkup.constFind(filename);
115  if (it == m_fileMarkup.constEnd())
116  {
117  return 0;
118  }
119  const auto pos = it.value().lastPlayPos;
120  const auto total = it.value().totalFrames;
121  if (0 == total)
122  {
123  return 0;
124  }
125  return std::clamp(100 * pos / total, (uint64_t)0, (uint64_t)100);
126 }
127 
129 {
131 }
PlaybackState::m_fileMarkup
QMap< QString, Markup > m_fileMarkup
maps filename to markup
Definition: playbackstate.h:45
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
MARK_BOOKMARK
@ MARK_BOOKMARK
Definition: programtypes.h:56
videometadata.h
mythdb.h
PlaybackState::Initialize
void Initialize()
Initializes playback state from database.
Definition: playbackstate.cpp:16
PlaybackState::Markup::totalFrames
uint64_t totalFrames
total frames
Definition: playbackstate.h:40
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:204
mythdbcon.h
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:618
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
PlaybackState::HasBookmark
bool HasBookmark(const QString &filename) const
Query bookmark of video with the specified filename.
Definition: playbackstate.cpp:92
programtypes.h
mythlogging.h
PlaybackState::m_alwaysShowWatchedProgress
bool m_alwaysShowWatchedProgress
Definition: playbackstate.h:46
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
MARK_TOTAL_FRAMES
@ MARK_TOTAL_FRAMES
Definition: programtypes.h:74
PlaybackState::GetWatchedPercent
uint GetWatchedPercent(const QString &filename) const
Query watched percent of video with the specified filename.
Definition: playbackstate.cpp:112
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
clamp
static eu8 clamp(eu8 value, eu8 low, eu8 high)
Definition: pxsup2dast.c:204
MythCoreContext::GetBoolSetting
bool GetBoolSetting(const QString &key, bool defaultval=false)
Definition: mythcorecontext.cpp:910
playbackstate.h
PlaybackState::GetLastPlayPos
uint64_t GetLastPlayPos(const QString &filename) const
Query last playback position of video with the specified filename.
Definition: playbackstate.cpp:102
PlaybackState::Update
void Update(const QString &filename)
Updates playback state of video with specified filename.
Definition: playbackstate.cpp:27
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:888
mythcontext.h
MARK_UTIL_LASTPLAYPOS
@ MARK_UTIL_LASTPLAYPOS
Definition: programtypes.h:76
PlaybackState::PlaybackState
PlaybackState()
Definition: playbackstate.cpp:11
PlaybackState::Markup::bookmarkPos
uint64_t bookmarkPos
bookmark position
Definition: playbackstate.h:42
PlaybackState::Markup
Markup for a video file.
Definition: playbackstate.h:39
PlaybackState::AlwaysShowWatchedProgress
bool AlwaysShowWatchedProgress() const
Returns cached setting "AlwaysShowWatchedProgress".
Definition: playbackstate.cpp:128
PlaybackState::Markup::lastPlayPos
uint64_t lastPlayPos
last playing position
Definition: playbackstate.h:41
build_compdb.filename
filename
Definition: build_compdb.py:21
uint
unsigned int uint
Definition: freesurround.h:24
PlaybackState::QueryData
void QueryData(const QString &filterFilename=QString())
Query playback state from database, only for single video if a filename is specified.
Definition: playbackstate.cpp:35
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:837