-
Notifications
You must be signed in to change notification settings - Fork 343
/
custompriority.cpp
478 lines (395 loc) · 15.2 KB
/
custompriority.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#include "custompriority.h"
// qt
#include <QSqlError>
// libmythbase
#include "mythdb.h"
#include "mythlogging.h"
// libmyth
#include "mythcorecontext.h"
// libmythtv
#include "scheduledrecording.h"
#include "channelutil.h"
// libmythui
#include "mythuibuttonlist.h"
#include "mythuispinbox.h"
#include "mythuitextedit.h"
#include "mythuibutton.h"
#include "mythdialogbox.h"
#include "mythmainwindow.h"
//mythfrontend
#include "viewschedulediff.h"
CustomPriority::CustomPriority(MythScreenStack *parent, ProgramInfo *proginfo)
: MythScreenType(parent, "CustomPriority")
{
if (proginfo)
m_pginfo = new ProgramInfo(*proginfo);
else
m_pginfo = new ProgramInfo();
gCoreContext->addListener(this);
}
CustomPriority::~CustomPriority(void)
{
delete m_pginfo;
gCoreContext->removeListener(this);
}
bool CustomPriority::Create()
{
if (!LoadWindowFromXML("schedule-ui.xml", "custompriority", this))
return false;
m_ruleList = dynamic_cast<MythUIButtonList *>(GetChild("rules"));
m_clauseList = dynamic_cast<MythUIButtonList *>(GetChild("clauses"));
m_prioritySpin = dynamic_cast<MythUISpinBox *>(GetChild("priority"));
m_titleEdit = dynamic_cast<MythUITextEdit *>(GetChild("title"));
m_descriptionEdit = dynamic_cast<MythUITextEdit *>(GetChild("description"));
m_addButton = dynamic_cast<MythUIButton *>(GetChild("add"));
m_installButton = dynamic_cast<MythUIButton *>(GetChild("install"));
m_testButton = dynamic_cast<MythUIButton *>(GetChild("test"));
m_deleteButton = dynamic_cast<MythUIButton *>(GetChild("delete"));
m_cancelButton = dynamic_cast<MythUIButton *>(GetChild("cancel"));
if (!m_ruleList || !m_clauseList || !m_prioritySpin || !m_titleEdit ||
!m_descriptionEdit || !m_addButton || !m_installButton ||
!m_testButton || !m_deleteButton || !m_cancelButton)
{
LOG(VB_GENERAL, LOG_ERR,
"CustomPriority, theme is missing required elements");
return false;
}
connect(m_ruleList, SIGNAL(itemSelected(MythUIButtonListItem *)),
SLOT(ruleChanged(MythUIButtonListItem *)));
connect(m_titleEdit, SIGNAL(valueChanged()), SLOT(textChanged()));
m_titleEdit->SetMaxLength(128);
connect(m_descriptionEdit, SIGNAL(valueChanged()), SLOT(textChanged()));
m_descriptionEdit->SetMaxLength(0);
connect(m_addButton, SIGNAL(Clicked()), SLOT(addClicked()));
connect(m_testButton, SIGNAL(Clicked()), SLOT(testClicked()));
connect(m_installButton, SIGNAL(Clicked()), SLOT(installClicked()));
connect(m_deleteButton, SIGNAL(Clicked()), SLOT(deleteClicked()));
connect(m_cancelButton, SIGNAL(Clicked()), SLOT(Close()));
loadData();
BuildFocusList();
return true;
}
void CustomPriority::loadData()
{
QString baseTitle = m_pginfo->GetTitle();
baseTitle.remove(QRegExp(" \\(.*\\)$"));
QString quoteTitle = baseTitle;
quoteTitle.replace("\'","\'\'");
m_prioritySpin->SetRange(-99,99,1);
m_prioritySpin->SetValue(1);
RuleInfo rule;
rule.priority = QString().setNum(1);
new MythUIButtonListItem(m_ruleList, tr("<New priority rule>"),
qVariantFromValue(rule));
MSqlQuery result(MSqlQuery::InitCon());
result.prepare("SELECT priorityname, recpriority, selectclause "
"FROM powerpriority ORDER BY priorityname;");
if (result.exec())
{
MythUIButtonListItem *item = NULL;
while (result.next())
{
QString trimTitle = result.value(0).toString();
trimTitle.remove(QRegExp(" \\(.*\\)$"));
rule.title = trimTitle;
rule.priority = result.value(1).toString();
rule.description = result.value(2).toString();
item = new MythUIButtonListItem(m_ruleList, rule.title,
qVariantFromValue(rule));
if (trimTitle == baseTitle)
m_ruleList->SetItemCurrent(item);
}
}
else
MythDB::DBError("Get power search rules query", result);
loadExampleRules();
if (!m_pginfo->GetTitle().isEmpty())
{
m_titleEdit->SetText(baseTitle);
m_descriptionEdit->SetText("program.title = '" + quoteTitle + "' ");
textChanged();
}
if (m_titleEdit->GetText().isEmpty())
SetFocusWidget(m_ruleList);
else
SetFocusWidget(m_clauseList);
}
void CustomPriority::loadExampleRules()
{
QMap<QString, QString> examples;
examples.insert(tr("Modify priority for an input (Input priority)"),
"cardinput.cardinputid = 1");
examples.insert(tr("Modify priority for all inputs on a card"),
"cardinput.cardid = 2");
examples.insert(tr("Modify priority for every card on a host"),
"capturecard.hostname = 'mythbox'");
examples.insert(tr("Only one specific channel ID (Channel priority)"),
"channel.chanid = '1003' ");
examples.insert(tr("Only a certain channel number"),
"channel.channum = '3' ");
examples.insert(tr("Only channels that carry a specific station"),
"channel.callsign = 'ESPN' ");
examples.insert(tr("Match related callsigns"),
"channel.callsign LIKE 'HBO%' ");
examples.insert(tr("Only channels marked as commercial free"),
QString("channel.commmethod = %1 ")
.arg(COMM_DETECT_COMMFREE));
examples.insert(tr("Modify priority for a station on an input"),
"channel.callsign = 'ESPN' AND cardinput.cardinputid = 2");
examples.insert(tr("Priority for all matching titles"),
"program.title LIKE 'CSI: %' ");
examples.insert(tr("Only shows marked as HDTV"),
"program.hdtv > 0 ");
examples.insert(tr("Close Captioned priority"),
"program.closecaptioned > 0 ");
examples.insert(tr("New episodes only"),
"program.previouslyshown = 0 ");
examples.insert(tr("Modify unidentified episodes"),
"program.generic = 0 ");
examples.insert(tr("First showing of each episode"),
"program.first > 0 ");
examples.insert(tr("Last showing of each episode"),
"program.last > 0 ");
examples.insert(tr("Priority for any show with End Late time"),
"RECTABLE.endoffset > 0 ");
examples.insert(tr("Priority for a category"),
"program.category = 'Reality' ");
examples.insert(QString("%1 ('movie', 'series', 'sports', 'tvshow')")
.arg(tr("Priority for a category type")),
"program.category_type = 'sports' ");
examples.insert(tr("Modify priority by star rating (0.0 to 1.0 for "
"movies only)"),
"program.stars >= 0.75 ");
examples.insert(tr("Priority when shown once (complete example)"),
"program.first > 0 AND program.last > 0");
examples.insert(tr("Prefer a host for a storage group (complete example)"),
QString("RECTABLE.storagegroup = 'Archive' "
"AND capturecard.hostname = 'mythbox' "));
examples.insert(tr("Priority for HD shows under two hours (complete "
"example)"),
"program.hdtv > 0 AND program.starttime > "
"DATE_SUB(program.endtime, INTERVAL 2 HOUR) ");
examples.insert(tr("Priority for movies by the year of release (complete "
"example)"),
"program.category_type = 'movie' "
"AND program.airdate >= 2006 ");
examples.insert(tr("Prefer movies when shown at night (complete example)"),
"program.category_type = 'movie' "
"AND HOUR(program.starttime) < 6 ");
examples.insert(tr("Prefer a host for live sports with overtime (complete "
"example)"),
"RECTABLE.endoffset > 0 "
"AND program.category = 'Sports event' "
"AND capturecard.hostname = 'mythbox' ");
examples.insert(tr("Avoid poor signal quality (complete example)"),
"cardinput.cardinputid = 1 AND "
"channel.channum IN (3, 5, 39, 66) ");
QMapIterator<QString, QString> it(examples);
while (it.hasNext())
{
it.next();
new MythUIButtonListItem(m_clauseList, it.key(),
qVariantFromValue(it.value()));
}
}
void CustomPriority::ruleChanged(MythUIButtonListItem *item)
{
if (!item)
return;
RuleInfo rule = qVariantValue<RuleInfo>(item->GetData());
m_titleEdit->SetText(rule.title);
m_descriptionEdit->SetText(rule.description);
m_prioritySpin->SetValue(rule.priority);
m_deleteButton->SetEnabled((bool)m_ruleList->GetCurrentPos());
textChanged();
}
void CustomPriority::textChanged(void)
{
bool hastitle = !m_titleEdit->GetText().isEmpty();
bool hasdesc = !m_descriptionEdit->GetText().isEmpty();
m_testButton->SetEnabled(hasdesc);
m_installButton->SetEnabled(hastitle && hasdesc);
}
void CustomPriority::addClicked(void)
{
MythUIButtonListItem *item = m_clauseList->GetItemCurrent();
if (!item)
return;
QString clause;
QString desc = m_descriptionEdit->GetText();
if (desc.contains(QRegExp("\\S")))
clause = "AND ";
clause = item->GetData().toString();
m_descriptionEdit->SetText(desc.append(clause));
}
void CustomPriority::testClicked(void)
{
if (!checkSyntax())
return;
testSchedule();
}
void CustomPriority::installClicked(void)
{
if (!checkSyntax())
return;
MythUIButtonListItem *item = m_prioritySpin->GetItemCurrent();
if (!item)
return;
MSqlQuery query(MSqlQuery::InitCon());
query.prepare("DELETE FROM powerpriority WHERE priorityname = :NAME;");
query.bindValue(":NAME", m_titleEdit->GetText());
if (!query.exec())
MythDB::DBError("Install power search delete", query);
query.prepare("INSERT INTO powerpriority "
"(priorityname, recpriority, selectclause) "
"VALUES(:NAME,:VALUE,:CLAUSE);");
query.bindValue(":NAME", m_titleEdit->GetText());
query.bindValue(":VALUE", item->GetText());
query.bindValue(":CLAUSE", m_descriptionEdit->GetText());
if (!query.exec())
MythDB::DBError("Install power search insert", query);
else
ScheduledRecording::signalChange(0);
Close();
}
void CustomPriority::deleteClicked(void)
{
if (!checkSyntax())
return;
MSqlQuery query(MSqlQuery::InitCon());
query.prepare("DELETE FROM powerpriority "
"WHERE priorityname=:NAME;");
query.bindValue(":NAME", m_titleEdit->GetText());
if (!query.exec())
MythDB::DBError("Delete power search query", query);
else
ScheduledRecording::signalChange(0);
Close();
}
bool CustomPriority::checkSyntax(void)
{
bool ret = false;
QString msg;
QString desc = m_descriptionEdit->GetText();
if (desc.contains(QRegExp("^\\s*AND\\s", Qt::CaseInsensitive)))
{
msg = "Power Priority rules do not reqiure a leading \"AND\"";
}
else if (desc.contains(';'))
{
msg = "Power Priority rules cannot include semicolon ( ; ) ";
msg += "statement terminators.";
}
else
{
QString qstr = QString("SELECT (%1) FROM (recordmatch, record, "
"program, channel, cardinput, capturecard, "
"oldrecorded) WHERE NULL").arg(desc);
while (1)
{
int i = qstr.indexOf("RECTABLE");
if (i == -1) break;
qstr = qstr.replace(i, strlen("RECTABLE"), "record");
}
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(qstr);
if (query.exec())
{
ret = true;
}
else
{
msg = tr("An error was found when checking") + ":\n\n";
msg += query.executedQuery();
msg += "\n\n" + tr("The database error was") + ":\n";
msg += query.lastError().databaseText();
ret = false;
}
}
if (!msg.isEmpty())
ShowOkPopup(msg);
return ret;
}
void CustomPriority::testSchedule(void)
{
MythUIButtonListItem *item = m_prioritySpin->GetItemCurrent();
if (!item)
return;
QString ttable = "powerpriority_tmp";
MSqlQueryInfo dbcon = MSqlQuery::SchedCon();
MSqlQuery query(dbcon);
QString thequery;
thequery = "SELECT GET_LOCK(:LOCK, 2);";
query.prepare(thequery);
query.bindValue(":LOCK", "DiffSchedule");
if (!query.exec())
{
QString msg =
QString("DB Error (Obtaining lock in testRecording): \n"
"Query was: %1 \nError was: %2 \n")
.arg(thequery)
.arg(MythDB::DBErrorMessage(query.lastError()));
LOG(VB_GENERAL, LOG_ERR, msg);
return;
}
thequery = QString("DROP TABLE IF EXISTS %1;").arg(ttable);
query.prepare(thequery);
if (!query.exec())
{
QString msg =
QString("DB Error (deleting old table in testRecording): \n"
"Query was: %1 \nError was: %2 \n")
.arg(thequery)
.arg(MythDB::DBErrorMessage(query.lastError()));
LOG(VB_GENERAL, LOG_ERR, msg);
return;
}
thequery = QString("CREATE TABLE %1 SELECT * FROM powerpriority;")
.arg(ttable);
query.prepare(thequery);
if (!query.exec())
{
QString msg =
QString("DB Error (create new table): \n"
"Query was: %1 \nError was: %2 \n")
.arg(thequery)
.arg(MythDB::DBErrorMessage(query.lastError()));
LOG(VB_GENERAL, LOG_ERR, msg);
return;
}
query.prepare(QString("DELETE FROM %1 WHERE priorityname = :NAME;")
.arg(ttable));
query.bindValue(":NAME", m_titleEdit->GetText());
if (!query.exec())
MythDB::DBError("Test power search delete", query);
thequery = QString("INSERT INTO %1 "
"(priorityname, recpriority, selectclause) "
"VALUES(:NAME,:VALUE,:CLAUSE);").arg(ttable);
query.prepare(thequery);
query.bindValue(":NAME", m_titleEdit->GetText());
query.bindValue(":VALUE", item->GetText());
query.bindValue(":CLAUSE", m_descriptionEdit->GetText());
if (!query.exec())
MythDB::DBError("Test power search insert", query);
QString ltitle = tr("Power Priority");
if (!m_titleEdit->GetText().isEmpty())
ltitle = m_titleEdit->GetText();
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
ViewScheduleDiff *vsd = new ViewScheduleDiff(mainStack, ttable, 0, ltitle);
if (vsd->Create())
mainStack->AddScreen(vsd);
else
delete vsd;
thequery = "SELECT RELEASE_LOCK(:LOCK);";
query.prepare(thequery);
query.bindValue(":LOCK", "DiffSchedule");
if (!query.exec())
{
QString msg =
QString("DB Error (free lock): \n"
"Query was: %1 \nError was: %2 \n")
.arg(thequery)
.arg(MythDB::DBErrorMessage(query.lastError()));
LOG(VB_GENERAL, LOG_ERR, msg);
}
}