MythTV  master
recordingrule.cpp
Go to the documentation of this file.
1 
2 #include "recordingrule.h"
3 
4 #include <utility>
5 
6 // libmythbase
7 #include "mythdb.h"
8 
9 // libmyth
10 #include "mythcorecontext.h"
11 
12 // libmythtv
13 #include "scheduledrecording.h" // For RescheduleMatch()
14 #include "playgroup.h" // For GetInitialName()
15 #include "recordingprofile.h" // For constants
16 #include "mythdate.h"
17 #include "mythsorthelper.h"
18 
19 static inline QString null_to_empty(const QString &str)
20 {
21  return str.isEmpty() ? "" : str;
22 }
23 
24 // If the GetNumSetting() calls here are ever removed, update schema
25 // upgrade 1302 in dbcheck.cpp to manually apply them to the Default
26 // template. Failing to do so will cause users upgrading from older
27 // versions to lose those settings.
28 
30  : m_findtime(QTime::fromString("00:00:00", Qt::ISODate)),
31  m_findid(QDate(1970, 1, 1).daysTo(MythDate::current().toLocalTime().date())
32  + 719528),
33  m_transcoder(RecordingProfile::kTranscoderAutodetect)
34 {
35  QDateTime dt = MythDate::current();
36  m_enddate = m_startdate = dt.date();
37  m_endtime = m_starttime = dt.time();
38 
40 }
41 
46 {
47  std::shared_ptr<MythSortHelper>sh = getMythSortHelper();
48 
49  if (m_sortTitle.isEmpty() and not m_title.isEmpty())
50  m_sortTitle = sh->doTitle(m_title);
51  if (m_sortSubtitle.isEmpty() and not m_subtitle.isEmpty())
52  m_sortSubtitle = sh->doTitle(m_subtitle);
53 }
54 
58 bool RecordingRule::Load(bool asTemplate)
59 {
60  if (m_recordID <= 0)
61  return false;
62 
64  query.prepare("SELECT type, search, "
65  "recpriority, prefinput, startoffset, endoffset, dupmethod, dupin, "
66  "inactive, profile, recgroup, storagegroup, playgroup, autoexpire, "
67  "maxepisodes, maxnewest, autocommflag, autotranscode, transcoder, "
68  "autouserjob1, autouserjob2, autouserjob3, autouserjob4, "
69  "autometadata, parentid, title, subtitle, description, season, episode, "
70  "category, starttime, startdate, endtime, enddate, seriesid, programid, "
71  "inetref, chanid, station, findday, findtime, findid, "
72  "next_record, last_record, last_delete, avg_delay, filter, recgroupid "
73  "FROM record WHERE recordid = :RECORDID ;");
74 
75  query.bindValue(":RECORDID", m_recordID);
76 
77  if (!query.exec())
78  {
79  MythDB::DBError("SELECT record", query);
80  return false;
81  }
82 
83  if (!query.next())
84  return false;
85 
86  // Schedule
87  if (!asTemplate)
88  {
89  m_type = static_cast<RecordingType>(query.value(0).toInt());
90  m_searchType = static_cast<RecSearchType>(query.value(1).toInt());
91  }
92  m_recPriority = query.value(2).toInt();
93  m_prefInput = query.value(3).toInt();
94  m_startOffset = query.value(4).toInt();
95  m_endOffset = query.value(5).toInt();
96  m_dupMethod = static_cast<RecordingDupMethodType>
97  (query.value(6).toInt());
98  m_dupIn = static_cast<RecordingDupInType>(query.value(7).toInt());
99  m_filter = query.value(47).toUInt();
100  m_isInactive = query.value(8).toBool();
101 
102  // Storage
103  m_recProfile = query.value(9).toString();
104  m_recGroupID = query.value(48).toUInt();
105  m_storageGroup = query.value(11).toString();
106  m_playGroup = query.value(12).toString();
107  m_autoExpire = query.value(13).toBool();
108  m_maxEpisodes = query.value(14).toInt();
109  m_maxNewest = query.value(15).toBool();
110 
111  // Post Process
112  m_autoCommFlag = query.value(16).toBool();
113  m_autoTranscode = query.value(17).toBool();
114  m_transcoder = query.value(18).toInt();
115  m_autoUserJob1 = query.value(19).toBool();
116  m_autoUserJob2 = query.value(20).toBool();
117  m_autoUserJob3 = query.value(21).toBool();
118  m_autoUserJob4 = query.value(22).toBool();
119  m_autoMetadataLookup = query.value(23).toBool();
120 
121  // Original rule id for override rule
122  if (!asTemplate)
123  m_parentRecID = query.value(24).toInt();
124 
125  // Recording metadata
126  if (!asTemplate)
127  {
128  m_title = query.value(25).toString();
129  m_subtitle = query.value(26).toString();
130  m_description = query.value(27).toString();
131  m_season = query.value(28).toUInt();
132  m_episode = query.value(29).toUInt();
133  m_category = query.value(30).toString();
134  m_starttime = query.value(31).toTime();
135  m_startdate = query.value(32).toDate();
136  m_endtime = query.value(33).toTime();
137  m_enddate = query.value(34).toDate();
138  m_seriesid = query.value(35).toString();
139  m_programid = query.value(36).toString();
140  m_inetref = query.value(37).toString();
141  }
142 
143  // Associated data for rule types
144  if (!asTemplate)
145  {
146  m_channelid = query.value(38).toInt();
147  m_station = query.value(39).toString();
148  m_findday = query.value(40).toInt();
149  m_findtime = query.value(41).toTime();
150  m_findid = query.value(42).toInt();
151  }
152 
153  // Statistic fields - Used to generate statistics about particular rules
154  // and influence watch list weighting
155  if (!asTemplate)
156  {
157  m_nextRecording = MythDate::as_utc(query.value(43).toDateTime());
158  m_lastRecorded = MythDate::as_utc(query.value(44).toDateTime());
159  m_lastDeleted = MythDate::as_utc(query.value(45).toDateTime());
160  m_averageDelay = query.value(46).toInt();
161  }
162 
165  m_template = (asTemplate || m_isTemplate) ?
166  query.value(30).toString() : "";
167 
168  if (!asTemplate)
169  m_loaded = true;
170 
172  return true;
173 }
174 
176 {
177  if (!proginfo)
178  return false;
179 
180  m_progInfo = proginfo;
181 
182  m_recordID = proginfo->GetRecordingRuleID();
183  if (m_recordID)
184  {
185  if (!Load())
186  return false;
187  }
188  else
189  LoadTemplate(proginfo->GetCategory(), proginfo->GetCategoryTypeString());
190 
191  if (m_type != kTemplateRecord &&
193  {
195  if (!proginfo->GetRecordingRuleID())
197  }
198 
200  m_loaded = true;
201  return true;
202 }
203 
220 bool RecordingRule::LoadBySearch(RecSearchType lsearch, const QString& textname,
221  const QString& forwhat, QString joininfo,
222  ProgramInfo *pginfo)
223 {
224  MSqlQuery query(MSqlQuery::InitCon());
225 
226  int rid = 0;
227  query.prepare("SELECT recordid FROM record WHERE "
228  "search = :SEARCH AND description LIKE :FORWHAT");
229  query.bindValue(":SEARCH", lsearch);
230  query.bindValue(":FORWHAT", forwhat);
231 
232  if (query.exec())
233  {
234  if (query.next())
235  rid = query.value(0).toInt();
236  // else rid is zero, which is valid, we're looking at a new rule
237  }
238  else
239  {
240  MythDB::DBError("loadBySearch", query);
241  return false;
242  }
243 
244  if (rid)
245  {
246  m_recordID = rid;
247  if (!Load())
248  return false;
249  }
250  else
251  {
252  LoadTemplate("Default");
253 
254  QString searchType;
255  m_searchType = lsearch;
256  searchType = SearchTypeToString(m_searchType);
257 
258  QString ltitle = QString("%1 (%2)").arg(textname).arg(searchType);
259  m_title = ltitle;
260  m_sortTitle = nullptr;
261  m_subtitle = m_sortSubtitle = std::move(joininfo);
262  m_description = forwhat;
263 
264  if (pginfo)
265  {
266  m_findday =
267  (pginfo->GetScheduledStartTime().toLocalTime().date()
268  .dayOfWeek() + 1) % 7;
269  m_findtime = pginfo->GetScheduledStartTime().toLocalTime().time();
270  m_findid = QDate(1970, 1, 1).daysTo(
271  pginfo->GetScheduledStartTime().toLocalTime().date()) + 719528;
272  }
273  }
274 
276  m_loaded = true;
277  return true;
278 }
279 
280 bool RecordingRule::LoadTemplate(const QString& category, const QString& categoryType)
281 {
282  MSqlQuery query(MSqlQuery::InitCon());
283  query.prepare("SELECT recordid, category, "
284  " (category = :CAT1) AS catmatch, "
285  " (category = :CATTYPE1) AS typematch "
286  "FROM record "
287  "WHERE type = :TEMPLATE AND "
288  " (category = :CAT2 OR category = :CATTYPE2 "
289  " OR category = 'Default') "
290  "ORDER BY catmatch DESC, typematch DESC"
291  );
292  query.bindValue(":TEMPLATE", kTemplateRecord);
293  query.bindValue(":CAT1", category);
294  query.bindValue(":CAT2", category);
295  query.bindValue(":CATTYPE1", categoryType);
296  query.bindValue(":CATTYPE2", categoryType);
297 
298  if (!query.exec())
299  {
300  MythDB::DBError("LoadByTemplate", query);
301  return false;
302  }
303 
304  if (!query.next())
305  return false;
306 
307  int savedRecordID = m_recordID;
308  m_recordID = query.value(0).toInt();
309  bool result = Load(true);
310  m_recordID = savedRecordID;
311 
312  return result;
313 }
314 
315 bool RecordingRule::MakeTemplate(QString category)
316 {
317  if (m_recordID > 0)
318  return false;
319 
320  if (category.compare(tr("Default"), Qt::CaseInsensitive) == 0)
321  {
322  category = "Default";
323  m_title = tr("Default (Template)");
325  }
326  else
327  {
328  //: %1 is the category
329  m_title = tr("%1 (Template)").arg(category);
331  }
332 
333  LoadTemplate(category);
334  m_recordID = 0;
336  m_category = category;
337  m_loaded = true;
338  m_isTemplate = true;
339 
340  return true;
341 }
342 
343 bool RecordingRule::ModifyPowerSearchByID(int rid, const QString& textname,
344  QString forwhat, QString joininfo)
345 {
346  if (rid <= 0)
347  return false;
348 
349  m_recordID = rid;
350  if (!Load() || m_searchType != kPowerSearch)
351  return false;
352 
353  QString ltitle = QString("%1 (%2)").arg(textname)
354  .arg(tr("Power Search"));
355  m_title = ltitle;
356  m_sortTitle = nullptr;
357  m_subtitle = m_sortSubtitle = std::move(joininfo);
358  m_description = std::move(forwhat);
359 
361  m_loaded = true;
362  return true;
363 }
364 
366 {
367  if (m_recordID <= 0)
368  return false;
369 
371  return false;
372 
373  m_isOverride = true;
375  m_recordID = 0;
377  m_isInactive = false;
378 
381 
383 
384  return true;
385 }
386 
387 bool RecordingRule::Save(bool sendSig)
388 {
389  QString sql = "SET type = :TYPE, search = :SEARCHTYPE, "
390  "recpriority = :RECPRIORITY, prefinput = :INPUT, "
391  "startoffset = :STARTOFFSET, endoffset = :ENDOFFSET, "
392  "dupmethod = :DUPMETHOD, dupin = :DUPIN, "
393  "filter = :FILTER, "
394  "inactive = :INACTIVE, profile = :RECPROFILE, "
395  "recgroup = :RECGROUP, "
396  "recgroupid = :RECGROUPID, "
397  "storagegroup = :STORAGEGROUP, "
398  "playgroup = :PLAYGROUP, autoexpire = :AUTOEXPIRE, "
399  "maxepisodes = :MAXEPISODES, maxnewest = :MAXNEWEST, "
400  "autocommflag = :AUTOCOMMFLAG, "
401  "autotranscode = :AUTOTRANSCODE, "
402  "transcoder = :TRANSCODER, autouserjob1 = :AUTOUSERJOB1, "
403  "autouserjob2 = :AUTOUSERJOB2, "
404  "autouserjob3 = :AUTOUSERJOB3, "
405  "autouserjob4 = :AUTOUSERJOB4, "
406  "autometadata = :AUTOMETADATA, "
407  "parentid = :PARENTID, title = :TITLE, "
408  "subtitle = :SUBTITLE, season = :SEASON, episode = :EPISODE, "
409  "description = :DESCRIPTION, category = :CATEGORY, "
410  "starttime = :STARTTIME, startdate = :STARTDATE, "
411  "endtime = :ENDTIME, enddate = :ENDDATE, seriesid = :SERIESID, "
412  "programid = :PROGRAMID, inetref = :INETREF, chanid = :CHANID, "
413  "station = :STATION, findday = :FINDDAY, "
414  "findtime = :FINDTIME, findid = :FINDID, "
415  "next_record = :NEXTREC, last_record = :LASTREC, "
416  "last_delete = :LASTDELETE, avg_delay = :AVGDELAY ";
417 
418  QString sqlquery;
419  if (m_recordID > 0 || (m_recordTable != "record" && m_tempID > 0))
420  {
421  sqlquery = QString("UPDATE %1 %2 WHERE recordid = :RECORDID;")
422  .arg(m_recordTable).arg(sql);
423  }
424  else
425  {
426  sqlquery = QString("INSERT INTO %1 %2;").arg(m_recordTable).arg(sql);
427  }
428 
429  MSqlQuery query(MSqlQuery::InitCon());
430  query.prepare(sqlquery);
431  query.bindValue(":TYPE", m_type);
432  query.bindValue(":SEARCHTYPE", m_searchType);
433  query.bindValue(":RECPRIORITY", m_recPriority);
434  query.bindValue(":INPUT", m_prefInput);
435  query.bindValue(":STARTOFFSET", m_startOffset);
436  query.bindValue(":ENDOFFSET", m_endOffset);
437  query.bindValue(":DUPMETHOD", m_dupMethod);
438  query.bindValue(":DUPIN", m_dupIn);
439  query.bindValue(":FILTER", m_filter);
440  query.bindValue(":INACTIVE", m_isInactive);
441  query.bindValue(":RECPROFILE", null_to_empty(m_recProfile));
442  // Temporary, remove once transition to recgroupid is complete
444  query.bindValue(":RECGROUPID", m_recGroupID);
445  query.bindValue(":STORAGEGROUP", null_to_empty(m_storageGroup));
446  query.bindValue(":PLAYGROUP", null_to_empty(m_playGroup));
447  query.bindValue(":AUTOEXPIRE", m_autoExpire);
448  query.bindValue(":MAXEPISODES", m_maxEpisodes);
449  query.bindValue(":MAXNEWEST", m_maxNewest);
450  query.bindValue(":AUTOCOMMFLAG", m_autoCommFlag);
451  query.bindValue(":AUTOTRANSCODE", m_autoTranscode);
452  query.bindValue(":TRANSCODER", m_transcoder);
453  query.bindValue(":AUTOUSERJOB1", m_autoUserJob1);
454  query.bindValue(":AUTOUSERJOB2", m_autoUserJob2);
455  query.bindValue(":AUTOUSERJOB3", m_autoUserJob3);
456  query.bindValue(":AUTOUSERJOB4", m_autoUserJob4);
457  query.bindValue(":AUTOMETADATA", m_autoMetadataLookup);
458  query.bindValue(":PARENTID", m_parentRecID);
459  query.bindValue(":TITLE", m_title);
460  query.bindValue(":SUBTITLE", null_to_empty(m_subtitle));
461  query.bindValue(":DESCRIPTION", null_to_empty(m_description));
462  query.bindValue(":SEASON", m_season);
463  query.bindValue(":EPISODE", m_episode);
464  query.bindValue(":CATEGORY", null_to_empty(m_category));
465  query.bindValue(":STARTTIME", m_starttime);
466  query.bindValue(":STARTDATE", m_startdate);
467  query.bindValue(":ENDTIME", m_endtime);
468  query.bindValue(":ENDDATE", m_enddate);
469  query.bindValue(":SERIESID", null_to_empty(m_seriesid));
470  query.bindValue(":PROGRAMID", null_to_empty(m_programid));
471  query.bindValue(":INETREF", null_to_empty(m_inetref));
472  query.bindValue(":CHANID", m_channelid);
473  query.bindValue(":STATION", null_to_empty(m_station));
474  query.bindValue(":FINDDAY", m_findday);
475  query.bindValue(":FINDTIME", m_findtime);
476  query.bindValue(":FINDID", m_findid);
477  query.bindValue(":NEXTREC", m_nextRecording);
478  query.bindValue(":LASTREC", m_lastRecorded);
479  query.bindValue(":LASTDELETE", m_lastDeleted);
480  query.bindValue(":AVGDELAY", m_averageDelay);
481 
482  if (m_recordTable != "record" && m_tempID > 0)
483  query.bindValue(":RECORDID", m_tempID);
484  else if (m_recordID > 0)
485  query.bindValue(":RECORDID", m_recordID);
486 
487  if (!query.exec())
488  {
489  MythDB::DBError("UPDATE/INSERT record", query);
490  return false;
491  }
492 
493  if (m_recordTable != "record" && m_tempID <= 0)
494  m_tempID = query.lastInsertId().toInt();
495  else if (m_recordID <= 0)
496  m_recordID = query.lastInsertId().toInt();
497 
498  if (sendSig)
500  QString("SaveRule %1").arg(m_title));
501 
502  return true;
503 }
504 
505 bool RecordingRule::Delete(bool sendSig)
506 {
507  if (m_recordID < 0)
508  return false;
509 
510  QString querystr;
511  MSqlQuery query(MSqlQuery::InitCon());
512  query.prepare("DELETE FROM record WHERE recordid = :RECORDID");
513  query.bindValue(":RECORDID", m_recordID);
514  if (!query.exec())
515  {
516  MythDB::DBError("ScheduledRecording::remove -- record", query);
517  return false;
518  }
519 
520  query.prepare("DELETE FROM oldfind WHERE recordid = :RECORDID");
521  query.bindValue(":RECORDID", m_recordID);
522  if (!query.exec())
523  {
524  MythDB::DBError("ScheduledRecording::remove -- oldfind", query);
525  }
526 
528  {
529  query.prepare("DELETE FROM program WHERE manualid = :RECORDID");
530  query.bindValue(":RECORDID", m_recordID);
531  if (!query.exec())
532  {
533  MythDB::DBError("ScheduledRecording::remove -- oldfind", query);
534  }
535  }
536 
537  if (sendSig)
539  QString("DeleteRule %1").arg(m_title));
540 
541  // Set m_recordID to zero, the rule is no longer in the database so it's
542  // not valid. Should you want, this allows a rule to be removed from the
543  // database and then re-inserted with Save()
544  m_recordID = 0;
545 
546  return true;
547 }
548 
549 void RecordingRule::ToMap(InfoMap &infoMap) const
550 {
551  if (m_title == "Default (Template)")
552  {
553  infoMap["title"] = tr("Default (Template)");
554  infoMap["sorttitle"] = tr("Default (Template)");
555  }
556  else
557  {
558  infoMap["title"] = m_title;
559  infoMap["sorttitle"] = m_sortTitle;
560  }
561  infoMap["subtitle"] = m_subtitle;
562  infoMap["sortsubtitle"] = m_sortSubtitle;
563  infoMap["description"] = m_description;
564  infoMap["season"] = QString::number(m_season);
565  infoMap["episode"] = QString::number(m_episode);
566 
567  if (m_category == "Default")
568  infoMap["category"] = tr("Default", "category");
569  else
570  infoMap["category"] = m_category;
571  infoMap["callsign"] = m_station;
572 
573  QDateTime starttm(m_startdate, m_starttime, Qt::UTC);
574  infoMap["starttime"] = MythDate::toString(starttm, MythDate::kTime);
575  infoMap["startdate"] = MythDate::toString(
577 
578  QDateTime endtm(m_enddate, m_endtime, Qt::UTC);
579  infoMap["endtime"] = MythDate::toString(endtm, MythDate::kTime);
580  infoMap["enddate"] = MythDate::toString(
582 
583  infoMap["inetref"] = m_inetref;
584  infoMap["chanid"] = m_channelid;
585  infoMap["channel"] = m_station;
586 
587  QDateTime startts(m_startdate, m_starttime, Qt::UTC);
588  QDateTime endts(m_enddate, m_endtime, Qt::UTC);
589 
590  QString length;
591  int seconds = startts.secsTo(endts);
592  int minutes = seconds / 60;
593  int hours = minutes / 60;
594  minutes = minutes % 60;
595 
596  infoMap["lenmins"] = QCoreApplication::translate("(Common)", "%n minute(s)",
597  "", minutes);
598 
599  QString minstring = QCoreApplication::translate("(Common)", "%n minute(s)",
600  "", minutes);
601 
602  QString hourstring = QCoreApplication::translate("(Common)", "%n hour(s)",
603  "", hours);
604 
605  if (hours > 0)
606  {
607  //: Time duration, %1 is replaced by the hours, %2 by the minutes
608  infoMap["lentime"] = QCoreApplication::translate("(Common)", "%1 %2",
609  "Hours and minutes").arg(hourstring).arg(minstring);
610  }
611  else
612  infoMap["lentime"] = minstring;
613 
614 
615  infoMap["timedate"] = MythDate::toString(
616  startts, MythDate::kDateTimeFull | MythDate::kSimplify) + " - " +
618 
619  infoMap["shorttimedate"] =
621  startts, MythDate::kDateTimeShort | MythDate::kSimplify) + " - " +
623 
625  {
626  QDateTime ldt =
627  QDateTime(MythDate::current().toLocalTime().date(), m_findtime,
628  Qt::LocalTime);
629  QString findfrom = MythDate::toString(ldt, MythDate::kTime);
630  if (m_type == kWeeklyRecord)
631  {
632  int daynum = (m_findday + 5) % 7 + 1;
633  findfrom = QString("%1, %2")
634  .arg(gCoreContext->GetQLocale().dayName(daynum, QLocale::ShortFormat))
635  .arg(findfrom);
636  }
637  infoMap["subtitle"] = tr("(%1 or later) %3",
638  "e.g. (Sunday or later) program "
639  "subtitle").arg(findfrom)
640  .arg(m_subtitle);
641  }
642 
643  infoMap["searchtype"] = SearchTypeToString(m_searchType);
644  if (m_searchType != kNoSearch)
645  infoMap["searchforwhat"] = m_description;
646 
647  using namespace MythDate;
648  if (m_nextRecording.isValid())
649  {
650  infoMap["nextrecording"] = MythDate::toString(
652  }
653  if (m_lastRecorded.isValid())
654  {
655  infoMap["lastrecorded"] = MythDate::toString(
657  }
658  if (m_lastDeleted.isValid())
659  {
660  infoMap["lastdeleted"] = MythDate::toString(
662  }
663 
664  infoMap["ruletype"] = toString(m_type);
665  infoMap["rectype"] = toString(m_type);
666 
667  if (m_template == "Default")
668  infoMap["template"] = tr("Default", "Default template");
669  else
670  infoMap["template"] = m_template;
671 }
672 
673 void RecordingRule::UseTempTable(bool usetemp, const QString& table)
674 {
676 
677  if (usetemp)
678  {
679  m_recordTable = table;
680 
681  query.prepare("SELECT GET_LOCK(:LOCK, 2);");
682  query.bindValue(":LOCK", "DiffSchedule");
683  if (!query.exec())
684  {
685  MythDB::DBError("Obtaining lock in testRecording", query);
686  return;
687  }
688 
689  query.prepare(QString("DROP TABLE IF EXISTS %1;").arg(table));
690  if (!query.exec())
691  {
692  MythDB::DBError("Deleting old table in testRecording", query);
693  return;
694  }
695 
696  query.prepare(QString("CREATE TABLE %1 SELECT * FROM record;")
697  .arg(table));
698  if (!query.exec())
699  {
700  MythDB::DBError("Create new temp table", query);
701  return;
702  }
703 
704  query.prepare(QString("ALTER TABLE %1 MODIFY recordid int(10) "
705  "UNSIGNED NOT NULL AUTO_INCREMENT primary key;")
706  .arg(table));
707  if (!query.exec())
708  {
709  MythDB::DBError("Modify recordid column to include "
710  "auto-increment", query);
711  return;
712  }
713 
714  if (m_recordID > 0)
716 
717  Save(false);
718  }
719  else
720  {
721  query.prepare("SELECT RELEASE_LOCK(:LOCK);");
722  query.bindValue(":LOCK", "DiffSchedule");
723  if (!query.exec())
724  {
725  MythDB::DBError("Free lock", query);
726  return;
727  }
728  m_recordTable = "record";
729  m_tempID = 0;
730  }
731 }
732 
734 {
735  if (!m_progInfo)
736  return;
737 
751  if (m_recordID <= 0)
752  {
753  m_findday =
754  (m_progInfo->GetScheduledStartTime().toLocalTime().date()
755  .dayOfWeek() + 1) % 7;
756  m_findtime = m_progInfo->GetScheduledStartTime().toLocalTime().time();
757  m_findid = QDate(1970, 1, 1).daysTo(
758  m_progInfo->GetScheduledStartTime().toLocalTime().date()) + 719528;
759  }
760  else
761  {
762  if (m_findid > 0)
764  else
765  {
766  QDate epoch(1970, 1, 1);
767  m_findid = epoch.daysTo(
768  m_progInfo->GetScheduledStartTime().toLocalTime().date())
769  + 719528;
770  }
771  }
773 
774  if (m_inetref.isEmpty())
775  {
779  }
781 }
782 
784 {
785  MSqlQuery query(MSqlQuery::InitCon());
786  query.prepare("SELECT SUM(1 << filterid) FROM recordfilter "
787  "WHERE filterid >= 0 AND filterid < :NUMFILTERS AND "
788  " TRIM(clause) <> '' AND newruledefault <> 0");
789  query.bindValue(":NUMFILTERS", RecordingRule::kNumFilters);
790  if (!query.exec())
791  {
792  MythDB::DBError("GetDefaultFilter", query);
793  return 0;
794  }
795 
796  if (!query.next())
797  return 0;
798 
799  return query.value(0).toUInt();
800 }
801 
803 {
804  QString searchTypeString;
805 
806  switch (searchType)
807  {
808  case kNoSearch:
809  searchTypeString = ""; // Allow themers to decide what to display
810  break;
811  case kPowerSearch:
812  searchTypeString = tr("Power Search");
813  break;
814  case kTitleSearch:
815  searchTypeString = tr("Title Search");
816  break;
817  case kKeywordSearch:
818  searchTypeString = tr("Keyword Search");
819  break;
820  case kPeopleSearch:
821  searchTypeString = tr("People Search");
822  break;
823  default:
824  searchTypeString = tr("Unknown Search");
825  break;
826  }
827 
828  return searchTypeString;
829 }
830 
832 {
833  QStringList result;
834 
835  MSqlQuery query(MSqlQuery::InitCon());
836  query.prepare("SELECT category "
837  "FROM record "
838  "WHERE type = :TEMPLATE "
839  "ORDER BY category = 'Default' DESC, category"
840  );
841  query.bindValue(":TEMPLATE", kTemplateRecord);
842 
843  if (!query.exec())
844  {
845  MythDB::DBError("LoadByTemplate", query);
846  return result;
847  }
848 
849  while (query.next())
850  result << query.value(0).toString();
851 
852  return result;
853 }
854 
855 bool RecordingRule::IsValid(QString &msg)
856 {
857  bool isOverride = false;
858  switch (m_type)
859  {
860  case kSingleRecord:
861  case kDailyRecord:
862  case kAllRecord:
863  case kWeeklyRecord:
864  case kOneRecord:
865  case kTemplateRecord:
866  break;
867  case kOverrideRecord:
868  case kDontRecord:
869  isOverride = true;
870  break;
871  case kNotRecording:
872  default:
873  msg = QString("Invalid recording type.");
874  return false;
875  }
876 
877  bool isNormal = false;
878  bool isSearch = false;
879  bool isManual = false;
880  switch (m_searchType)
881  {
882  case kNoSearch:
883  isNormal = true;
884  break;
885  case kPowerSearch:
886  case kTitleSearch:
887  case kKeywordSearch:
888  case kPeopleSearch:
889  isSearch = true;
890  break;
891  case kManualSearch:
892  isManual = true;
893  break;
894  default:
895  msg = QString("Invalid search type.");
896  return false;
897  }
898 
899  if ((isNormal && (m_type == kDailyRecord || m_type == kWeeklyRecord)) ||
900  (isSearch && (m_type != kDailyRecord && m_type != kWeeklyRecord &&
901  m_type != kOneRecord && m_type != kAllRecord)) ||
902  (isManual && (m_type != kDailyRecord && m_type != kWeeklyRecord &&
904  {
905  msg = QString("Invalid recording type/search type.");
906  return false;
907  }
908 
909  if ((m_parentRecID && !isOverride) ||
910  (!m_parentRecID && isOverride))
911  {
912  msg = QString("Invalid parentID/override.");
913  return false;
914  }
915 
916  if (m_title.isEmpty())
917  {
918  msg = QString("Invalid title.");
919  return false;
920  }
921 
922  if (m_searchType == kPowerSearch)
923  {
924  MSqlQuery query(MSqlQuery::InitCon());
925  query.prepare(QString("SELECT NULL FROM (program, channel) "
926  "%1 WHERE %2")
927  .arg(m_subtitle).arg(m_description));
928  if (m_description.contains(';') || !query.exec())
929  {
930  msg = QString("Invalid custom search values.");
931  return false;
932  }
933  }
934  else if (isSearch)
935  {
936  if (m_description.isEmpty())
937  {
938  msg = QString("Invalid search value.");
939  return false;
940  }
941  }
942 
943  if (!isSearch)
944  {
945  if (!m_startdate.isValid() || !m_starttime.isValid() ||
946  !m_enddate.isValid() || !m_endtime.isValid())
947  {
948  msg = QString("Invalid start/end date/time.");
949  return false;
950  }
951  int secsto = QDateTime(m_startdate, m_starttime)
952  .secsTo(QDateTime(m_enddate, m_endtime));
953  if (secsto <= 0 || secsto > (8 * 3600))
954  {
955  msg = QString("Invalid duration.");
956  return false;
957  }
958 
959  if (!m_channelid || m_station.isEmpty())
960  {
961  msg = QString("Invalid channel.");
962  return false;
963  }
964  }
965 
966  if (m_findday < 0 || m_findday > 6 || !m_findtime.isValid())
967  {
968  msg = QString("Invalid find values.");
969  return false;
970  }
971 
972  if (m_recPriority < -99 || m_recPriority > 99)
973  {
974  msg = QString("Invalid priority.");
975  return false;
976  }
977 
978  if (m_startOffset < -480 || m_startOffset > 480 ||
979  m_endOffset < -480 || m_endOffset > 480)
980  {
981  msg = QString("Invalid start/end offset.");
982  return false;
983  }
984 
985  if (m_prefInput < 0)
986  {
987  msg = QString("Invalid preferred input.");
988  return false;
989  }
990 
991  unsigned valid_mask = (1 << kNumFilters) - 1;
992  if ((m_filter & valid_mask) != m_filter)
993  {
994  msg = QString("Invalid filter value.");
995  return false;
996  }
997 
998  if (m_recProfile.isEmpty() || (m_recGroupID == 0) ||
999  m_storageGroup.isEmpty() || m_playGroup.isEmpty())
1000  {
1001  msg = QString("Invalid group value.");
1002  return false;
1003  }
1004 
1005  if (m_maxEpisodes < 0)
1006  {
1007  msg = QString("Invalid max episodes value.");
1008  return false;
1009  }
1010 
1011  if (m_dupIn & ~(kDupsInAll | kDupsNewEpi))
1012  {
1013  msg = QString("Invalid duplicate scope.");
1014  return false;
1015  }
1016 
1019  {
1020  msg = QString("Invalid duplicate method.");
1021  return false;
1022  }
1023 
1024  if (m_transcoder < 0)
1025  {
1026  msg = QString("Invalid transcoder value.");
1027  return false;
1028  }
1029 
1030  return true;
1031 }
QString m_subtitle
Definition: recordingrule.h:81
QTime m_findtime
Time for timeslot rules.
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
bool m_isInactive
Recording rule is enabled?
Definition: recordingrule.h:76
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
QString m_programid
Definition: recordingrule.h:94
bool LoadByProgram(const ProgramInfo *proginfo)
QString m_sortTitle
Definition: recordingrule.h:80
bool Delete(bool sendSig=true)
QString toString(MarkTypes type)
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:30
void ToMap(InfoMap &infoMap) const
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
QString GetTitle(void) const
Definition: programinfo.h:355
QString m_storageGroup
QString m_station
int m_recordID
Unique Recording Rule ID.
Definition: recordingrule.h:72
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
RecordingType m_type
RecordingDupMethodType m_dupMethod
Do Today/Yesterday/Tomorrow transform.
Definition: mythdate.h:23
void ensureSortFields(void)
Ensure that the sortTitle and sortSubtitle fields are set.
QDateTime as_utc(const QDateTime &old_dt)
Returns copy of QDateTime with TimeSpec set to UTC.
Definition: mythdate.cpp:23
bool IsValid(QString &msg)
QString GetInetRef(void) const
Definition: programinfo.h:431
QDateTime m_nextRecording
static QString GetInitialName(const ProgramInfo *pi)
Definition: playgroup.cpp:224
static QString null_to_empty(const QString &str)
QDateTime GetScheduledStartTime(void) const
The scheduled start time of program.
Definition: programinfo.h:384
QVariant value(int i) const
Definition: mythdbcon.h:198
bool Save(bool sendSig=true)
Default local time.
Definition: mythdate.h:20
Holds information on recordings and videos.
Definition: programinfo.h:67
QDateTime m_lastRecorded
QString GetChannelSchedulingID(void) const
This is the unique programming identifier of a channel.
Definition: programinfo.h:377
QVariant lastInsertId()
Return the id of the last inserted row.
Definition: mythdbcon.cpp:888
QString GetDescription(void) const
Definition: programinfo.h:359
QString m_recordTable
bool m_autoMetadataLookup
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
int m_findday
callsign?
QString GetSortSubtitle(void) const
Definition: programinfo.h:358
Default local time.
Definition: mythdate.h:16
QString m_playGroup
QString GetSubtitle(void) const
Definition: programinfo.h:357
static QString SearchTypeToString(RecSearchType searchType)
QString m_description
Definition: recordingrule.h:83
bool LoadTemplate(const QString &category, const QString &categoryType="Default")
static MSqlQueryInfo SchedCon()
Returns dedicated connection. (Required for using temporary SQL tables.)
Definition: mythdbcon.cpp:565
QString m_template
Add year to string if not included.
Definition: mythdate.h:22
uint GetSeason(void) const
Definition: programinfo.h:360
RecSearchType
QString GetCategoryTypeString(void) const
Returns catType as a string.
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
QDateTime GetScheduledEndTime(void) const
The scheduled end time of the program.
Definition: programinfo.h:391
bool ModifyPowerSearchByID(int rid, const QString &textname, QString forwhat, QString joininfo="")
static void RescheduleMatch(uint recordid, uint sourceid, uint mplexid, const QDateTime &maxstarttime, const QString &why)
static unsigned GetDefaultFilter(void)
QString m_seriesid
Definition: recordingrule.h:93
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:101
RecordingDupInType m_dupIn
QString m_inetref
Definition: recordingrule.h:96
bool LoadBySearch(RecSearchType lsearch, const QString &textname, const QString &forwhat, QString joininfo="", ProgramInfo *pginfo=nullptr)
Load a recording rule based on search parameters.
QString GetSeriesID(void) const
Definition: programinfo.h:429
bool MakeTemplate(QString category)
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808
QString m_title
Definition: recordingrule.h:79
QLocale GetQLocale(void)
const ProgramInfo * m_progInfo
QString GetSortTitle(void) const
Definition: programinfo.h:356
QDateTime m_lastDeleted
void AssignProgramInfo()
uint GetChanID(void) const
This is the unique key used in the database to locate tuning information.
Definition: programinfo.h:366
QString m_sortSubtitle
Definition: recordingrule.h:82
static QString GetRecgroupString(uint recGroupID)
Temporary helper during transition from string to ID.
static const int kNumFilters
Definition: recordingrule.h:37
QString m_recProfile
uint GetRecordingRuleID(void) const
Definition: programinfo.h:443
QString GetProgramID(void) const
Definition: programinfo.h:430
uint GetFindID(void) const
Definition: programinfo.h:464
void UseTempTable(bool usetemp, const QString &table="record_tmp")
bool Load(bool asTemplate=false)
Load a single rule from the recorded table.
unsigned m_filter
QString m_category
Definition: recordingrule.h:86
static QStringList GetTemplateNames(void)
Default UTC.
Definition: mythdate.h:14
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
QString GetCategory(void) const
Definition: programinfo.h:363
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
bool MakeOverride(void)
Default local time.
Definition: mythdate.h:21
RecSearchType m_searchType
uint GetEpisode(void) const
Definition: programinfo.h:361
std::shared_ptr< MythSortHelper > getMythSortHelper(void)
Get a pointer to the MythSortHelper singleton.
Default local time.
Definition: mythdate.h:19