MythTV  master
markuputils.cpp
Go to the documentation of this file.
1 
2 // C++ includes
3 #include <iostream> // for cout
4 using std::cout;
5 
6 // QT
7 #include <QFile>
8 #include <QTextStream>
9 #include <QVector>
10 #include <QDomDocument>
11 
12 // libmyth* includes
13 #include "exitcodes.h"
14 #include "mythlogging.h"
15 
16 // Local includes
17 #include "markuputils.h"
18 
20  const QString &type)
21 {
22  ProgramInfo pginfo;
23  if (!GetProgramInfo(cmdline, pginfo))
25 
26  frm_dir_map_t cutlist;
27  frm_dir_map_t::const_iterator it;
28  QString result;
29 
30  if (type == "cutlist")
31  pginfo.QueryCutList(cutlist);
32  else
33  pginfo.QueryCommBreakList(cutlist);
34 
35  uint64_t lastStart = 0;
36  for (it = cutlist.begin(); it != cutlist.end(); ++it)
37  {
38  if ((*it == MARK_COMM_START) ||
39  (*it == MARK_CUT_START))
40  {
41  if (!result.isEmpty())
42  result += ",";
43  lastStart = it.key();
44  result += QString("%1-").arg(lastStart);
45  }
46  else
47  {
48  if (result.isEmpty())
49  result += "0-";
50  result += QString("%1").arg(it.key());
51  }
52  }
53 
54  if (result.endsWith('-'))
55  {
56  uint64_t lastFrame = pginfo.QueryLastFrameInPosMap() + 60;
57  if (lastFrame > lastStart)
58  result += QString("%1").arg(lastFrame);
59  }
60 
61  if (type == "cutlist")
62  cout << QString("Cutlist: %1\n").arg(result).toLocal8Bit().constData();
63  else
64  {
65  cout << QString("Commercial Skip List: %1\n")
66  .arg(result).toLocal8Bit().constData();
67  }
68 
69  return GENERIC_EXIT_OK;
70 }
71 
73  const QString &type, QString newList)
74 {
75  ProgramInfo pginfo;
76  if (!GetProgramInfo(cmdline, pginfo))
78 
79  bool isCutlist = (type == "cutlist");
80  frm_dir_map_t markuplist;
81 
82  newList.replace(QRegExp(" "), "");
83 
84  QStringList tokens = newList.split(",", QString::SkipEmptyParts);
85 
86  if (newList.isEmpty())
87  newList = "(EMPTY)";
88 
89  for (int i = 0; i < tokens.size(); i++)
90  {
91  QStringList cutpair = tokens[i].split("-", QString::SkipEmptyParts);
92  if (isCutlist)
93  {
94  markuplist[cutpair[0].toInt()] = MARK_CUT_START;
95  markuplist[cutpair[1].toInt()] = MARK_CUT_END;
96  }
97  else
98  {
99  markuplist[cutpair[0].toInt()] = MARK_COMM_START;
100  markuplist[cutpair[1].toInt()] = MARK_COMM_END;
101  }
102  }
103 
104  if (isCutlist)
105  {
106  pginfo.SaveCutList(markuplist);
107  cout << QString("Cutlist set to: %1\n")
108  .arg(newList).toLocal8Bit().constData();
109  LOG(VB_GENERAL, LOG_NOTICE, QString("Cutlist set to: %1").arg(newList));
110  }
111  else
112  {
113  pginfo.SaveCommBreakList(markuplist);
114  cout << QString("Commercial Skip List set to: %1\n")
115  .arg(newList).toLocal8Bit().constData();
116  LOG(VB_GENERAL, LOG_NOTICE, QString("Commercial Skip List set to: %1").arg(newList));
117  }
118 
119  return GENERIC_EXIT_OK;
120 }
121 
123 {
124  return GetMarkupList(cmdline, "cutlist");
125 }
126 
128 {
129  return SetMarkupList(cmdline, QString("cutlist"),
130  cmdline.toString("setcutlist"));
131 }
132 
134 {
135  return SetMarkupList(cmdline, QString("cutlist"), QString(""));
136 }
137 
139 {
140  ProgramInfo pginfo;
141  if (!GetProgramInfo(cmdline, pginfo))
143 
144  frm_dir_map_t cutlist;
145  frm_dir_map_t::const_iterator it;
146 
147  pginfo.QueryCommBreakList(cutlist);
148  for (it = cutlist.begin(); it != cutlist.end(); ++it)
149  if (*it == MARK_COMM_START)
150  cutlist[it.key()] = MARK_CUT_START;
151  else
152  cutlist[it.key()] = MARK_CUT_END;
153  pginfo.SaveCutList(cutlist);
154 
155  cout << "Commercial Skip List copied to Cutlist\n";
156  LOG(VB_GENERAL, LOG_NOTICE, "Commercial Skip List copied to Cutlist");
157 
158  return GENERIC_EXIT_OK;
159 }
160 
162 {
163  return GetMarkupList(cmdline, "skiplist");
164 }
165 
167 {
168  return SetMarkupList(cmdline, QString("skiplist"),
169  cmdline.toString("setskiplist"));
170 }
171 
173 {
174  return SetMarkupList(cmdline, QString("skiplist"), QString(""));
175 }
176 
178 {
179  ProgramInfo pginfo;
180  if (!GetProgramInfo(cmdline, pginfo))
182 
183  cout << "Clearing Seek Table\n";
184  LOG(VB_GENERAL, LOG_NOTICE, pginfo.IsVideo() ?
185  QString("Clearing Seek Table for Video %1").arg(pginfo.GetPathname()) :
186  QString("Clearing Seek Table for Channel ID %1 @ %2")
187  .arg(pginfo.GetChanID())
188  .arg(pginfo.GetScheduledStartTime().toString()));
195 
196  return GENERIC_EXIT_OK;
197 }
198 
200 {
201  ProgramInfo pginfo;
202  if (!GetProgramInfo(cmdline, pginfo))
204 
205  cout << "Clearing bookmarks\n";
206  LOG(VB_GENERAL, LOG_NOTICE, pginfo.IsVideo() ?
207  QString("Clearing bookmarks for video %1").arg(pginfo.GetPathname()) :
208  QString("Clearing bookmarks for channel id %1 @ %2")
209  .arg(pginfo.GetChanID())
210  .arg(pginfo.GetScheduledStartTime().toString()));
214 
215  return GENERIC_EXIT_OK;
216 }
217 
219 {
220  ProgramInfo pginfo;
221  if (!GetProgramInfo(cmdline, pginfo))
223 
224  QString filename = cmdline.toString("getmarkup");
225  if (filename.isEmpty())
226  {
227  LOG(VB_STDIO|VB_FLUSH, LOG_ERR, "Missing --getmarkup filename\n");
229  }
230  QVector<ProgramInfo::MarkupEntry> mapMark, mapSeek;
231  pginfo.QueryMarkup(mapMark, mapSeek);
232  QFile outfile(filename);
233  if (!outfile.open(QIODevice::WriteOnly))
234  {
235  LOG(VB_STDIO|VB_FLUSH, LOG_ERR,
236  QString("Couldn't open output file %1\n").arg(filename));
238  }
239  QTextStream stream(&outfile);
240  QDomDocument xml;
241  QDomProcessingInstruction processing =
242  xml.createProcessingInstruction("xml",
243  "version='1.0' encoding='utf-8'");
244  xml.appendChild(processing);
245  QDomElement root = xml.createElement("metadata");
246  xml.appendChild(root);
247  QDomElement item = xml.createElement("item");
248  root.appendChild(item);
249  QDomElement markup = xml.createElement("markup");
250  item.appendChild(markup);
251  for (int i = 0; i < mapMark.size(); ++i)
252  {
253  ProgramInfo::MarkupEntry &entry = mapMark[i];
254  QDomElement child = xml.createElement("mark");
255  child.setAttribute("type", entry.type);
256  child.setAttribute("frame", (qulonglong)entry.frame);
257  if (!entry.isDataNull)
258  child.setAttribute("data", (qulonglong)entry.data);
259  markup.appendChild(child);
260  }
261  for (int i = 0; i < mapSeek.size(); ++i)
262  {
263  ProgramInfo::MarkupEntry &entry = mapSeek[i];
264  QDomElement child = xml.createElement("seek");
265  child.setAttribute("type", entry.type);
266  child.setAttribute("frame", (qulonglong)entry.frame);
267  if (!entry.isDataNull)
268  child.setAttribute("data", (qulonglong)entry.data);
269  markup.appendChild(child);
270  }
271 
272  stream << xml.toString(2);
273  outfile.close();
274  return GENERIC_EXIT_OK;
275 }
276 
278 {
279  ProgramInfo pginfo;
280  if (!GetProgramInfo(cmdline, pginfo))
282 
283  QString filename = cmdline.toString("setmarkup");
284  if (filename.isEmpty())
285  {
286  LOG(VB_STDIO|VB_FLUSH, LOG_ERR, "Missing --setmarkup filename\n");
288  }
289  QVector<ProgramInfo::MarkupEntry> mapMark, mapSeek;
290  QFile infile(filename);
291  if (!infile.open(QIODevice::ReadOnly))
292  {
293  LOG(VB_STDIO|VB_FLUSH, LOG_ERR,
294  QString("Couldn't open input file %1\n").arg(filename));
296  }
297  QDomDocument xml;
298  if (!xml.setContent(&infile))
299  {
300  LOG(VB_STDIO|VB_FLUSH, LOG_ERR,
301  QString("Failed to read valid XML from file %1\n").arg(filename));
303  }
304  QDomElement metadata = xml.documentElement();
305  if (metadata.tagName() != "metadata")
306  {
307  LOG(VB_STDIO|VB_FLUSH, LOG_ERR,
308  QString("Expected top-level 'metadata' element "
309  "in file %1\n").arg(filename));
311  }
312  QDomNode item = metadata.firstChild();
313  if (!item.isElement() || item.toElement().tagName() != "item")
314  {
315  LOG(VB_STDIO|VB_FLUSH, LOG_ERR,
316  QString("Expected 'item' element within 'metadata' element "
317  "in file %1\n").arg(filename));
319  }
320  QDomNode markup = item.firstChild();
321  if (!markup.isElement() || markup.toElement().tagName() != "markup")
322  {
323  LOG(VB_STDIO|VB_FLUSH, LOG_ERR,
324  QString("Expected 'markup' element within 'item' element "
325  "in file %1\n").arg(filename));
327  }
328  for (QDomNode n = markup.firstChild(); !n.isNull(); n = n.nextSibling())
329  {
330  if (n.isElement())
331  {
332  QDomElement e = n.toElement();
333  QString tagName = e.tagName();
334  bool isMark;
335  if (tagName == "mark")
336  isMark = true;
337  else if (tagName == "seek")
338  isMark = false;
339  else
340  {
341  LOG(VB_STDIO|VB_FLUSH, LOG_ERR,
342  QString("Weird tag '%1', expected 'mark' or 'seek'\n")
343  .arg(tagName));
344  continue;
345  }
346  int type = e.attribute("type").toInt();
347  uint64_t frame = e.attribute("frame").toULongLong();
348  QString dataString = e.attribute("data");
349  uint64_t data = dataString.toULongLong();
350  bool isDataNull = dataString.isNull();
351  ProgramInfo::MarkupEntry entry(type, frame,
352  data, isDataNull);
353  if (isMark)
354  mapMark.append(entry);
355  else
356  mapSeek.append(entry);
357  }
358  }
359  pginfo.SaveMarkup(mapMark, mapSeek);
360 
361  return GENERIC_EXIT_OK;
362 }
363 
365 {
366  utilMap["gencutlist"] = &CopySkipListToCutList;
367  utilMap["getcutlist"] = &GetCutList;
368  utilMap["setcutlist"] = &SetCutList;
369  utilMap["clearcutlist"] = &ClearCutList;
370  utilMap["getskiplist"] = &GetSkipList;
371  utilMap["setskiplist"] = &SetSkipList;
372  utilMap["clearskiplist"] = &ClearSkipList;
373  utilMap["clearseektable"] = &ClearSeekTable;
374  utilMap["clearbookmarks"] = &ClearBookmarks;
375  utilMap["getmarkup"] = &GetMarkup;
376  utilMap["setmarkup"] = &SetMarkup;
377 }
378 
379 /* vim: set expandtab tabstop=4 shiftwidth=4: */
bool GetProgramInfo(const MythUtilCommandLineParser &cmdline, ProgramInfo &pginfo)
Definition: mythutil.cpp:11
static int CopySkipListToCutList(const MythUtilCommandLineParser &cmdline)
void SaveMarkup(const QVector< MarkupEntry > &mapMark, const QVector< MarkupEntry > &mapSeek) const
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
bool IsVideo(void) const
Definition: programinfo.h:476
static int GetSkipList(const MythUtilCommandLineParser &cmdline)
static int GetCutList(const MythUtilCommandLineParser &cmdline)
void registerMarkupUtils(UtilMap &utilMap)
static int ClearBookmarks(const MythUtilCommandLineParser &cmdline)
bool QueryCutList(frm_dir_map_t &, bool loadAutosave=false) const
void QueryCommBreakList(frm_dir_map_t &) const
QMap< uint64_t, MarkTypes > frm_dir_map_t
Frame # -> Mark map.
Definition: programtypes.h:81
void SaveCommBreakList(frm_dir_map_t &) const
static int SetMarkupList(const MythUtilCommandLineParser &cmdline, const QString &type, QString newList)
Definition: markuputils.cpp:72
uint64_t QueryLastFrameInPosMap(void) const
Returns last frame in position map or 0.
static int GetMarkup(const MythUtilCommandLineParser &cmdline)
static int ClearSkipList(const MythUtilCommandLineParser &cmdline)
static int SetMarkup(const MythUtilCommandLineParser &cmdline)
QDateTime GetScheduledStartTime(void) const
The scheduled start time of program.
Definition: programinfo.h:382
Holds information on recordings and videos.
Definition: programinfo.h:66
void ClearMarkupFlag(MarkTypes type) const
Definition: programinfo.h:621
QMap< QString, UtilFunc > UtilMap
Definition: mythutil.h:16
#define GENERIC_EXIT_NO_RECORDING_DATA
No program/recording data.
Definition: exitcodes.h:29
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
static int SetSkipList(const MythUtilCommandLineParser &cmdline)
MythCommFlagCommandLineParser cmdline
static int ClearCutList(const MythUtilCommandLineParser &cmdline)
void SaveCutList(frm_dir_map_t &, bool isAutoSave=false) const
static int SetCutList(const MythUtilCommandLineParser &cmdline)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
static int ClearSeekTable(const MythUtilCommandLineParser &cmdline)
uint GetChanID(void) const
This is the unique key used in the database to locate tuning information.
Definition: programinfo.h:364
static int GetMarkupList(const MythUtilCommandLineParser &cmdline, const QString &type)
Definition: markuputils.cpp:19
void QueryMarkup(QVector< MarkupEntry > &mapMark, QVector< MarkupEntry > &mapSeek) const
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
QString GetPathname(void) const
Definition: programinfo.h:335
void ClearPositionMap(MarkTypes type) const