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