/
main.cpp
565 lines (479 loc) · 15.7 KB
/
main.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <memory>
#include <QString>
#include <QDir>
#include <QMap>
#include <QApplication>
#include <QFileInfo>
#include "mythconfig.h"
#include "mythcontext.h"
#include "mythdbcon.h"
#include "dbutil.h"
#include "mythlogging.h"
#include "mythversion.h"
#include "langsettings.h"
#include "mythtranslation.h"
#include "exitcodes.h"
#include "exitprompt.h"
#include "storagegroupeditor.h"
#include "myththemedmenu.h"
#include "mythuihelper.h"
#include "mythdirs.h"
#include "channelscanner_cli.h"
#include "scanwizardconfig.h"
#include "scaninfo.h"
#include "channelimporter.h"
#include "cardutil.h"
#include "dbcheck.h"
#include "videosource.h"
#include "channeleditor.h"
#include "remoteutil.h"
#include "backendsettings.h"
#include "checksetup.h"
#include "startprompt.h"
#include "mythsystemevent.h"
#include "expertsettingseditor.h"
#include "commandlineparser.h"
#include "profilegroup.h"
#include "signalhandling.h"
using namespace std;
ExitPrompter *exitPrompt = NULL;
StartPrompter *startPrompt = NULL;
static MythThemedMenu *menu;
static QString logfile;
class CleanupGuard
{
public:
typedef void (*CleanupFunc)();
public:
CleanupGuard(CleanupFunc cleanFunction) :
m_cleanFunction(cleanFunction) {}
~CleanupGuard()
{
m_cleanFunction();
}
private:
CleanupFunc m_cleanFunction;
};
static void cleanup()
{
DestroyMythMainWindow();
delete gContext;
gContext = NULL;
delete qApp;
}
static void SetupMenuCallback(void* data, QString& selection)
{
(void)data;
QString sel = selection.toLower();
if (sel == "general")
{
BackendSettings be;
be.exec();
}
else if (sel == "capture cards")
{
CaptureCardEditor cce;
cce.exec();
}
else if (sel == "video sources")
{
VideoSourceEditor vse;
vse.exec();
}
else if (sel == "card inputs")
{
CardInputEditor cie;
cie.exec();
}
else if (sel == "recording profile")
{
ProfileGroupEditor editor;
editor.exec();
}
else if (sel == "channel editor")
{
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
ChannelEditor *chanedit = new ChannelEditor(mainStack);
if (chanedit->Create())
mainStack->AddScreen(chanedit);
else
delete chanedit;
}
else if (sel == "storage groups")
{
StorageGroupListEditor sge;
sge.exec();
}
else if (sel == "systemeventeditor")
{
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
MythSystemEventEditor *msee = new MythSystemEventEditor(
mainStack, "System Event Editor");
if (msee->Create())
mainStack->AddScreen(msee);
else
delete msee;
}
else if (sel.startsWith("exiting_app"))
{
if (!exitPrompt)
exitPrompt = new ExitPrompter();
exitPrompt->handleExit();
}
else
LOG(VB_GENERAL, LOG_ERR, "Unknown menu action: " + selection);
}
static bool RunMenu(QString themedir, QString themename)
{
QByteArray tmp = themedir.toLocal8Bit();
menu = new MythThemedMenu(
QString(tmp.constData()), "setup.xml",
GetMythMainWindow()->GetMainStack(), "mainmenu", false);
if (menu->foundTheme())
{
menu->setCallback(SetupMenuCallback, gContext);
GetMythMainWindow()->GetMainStack()->AddScreen(menu);
return true;
}
LOG(VB_GENERAL, LOG_ERR, QString("Couldn't use theme '%1'").arg(themename));
delete menu;
menu = NULL;
return false;
}
// If the theme specified in the DB is somehow broken, try a standard one:
//
static bool resetTheme(QString themedir, const QString badtheme)
{
QString themename = DEFAULT_UI_THEME;
if (badtheme == DEFAULT_UI_THEME)
themename = FALLBACK_UI_THEME;
LOG(VB_GENERAL, LOG_ERR,
QString("Overriding broken theme '%1' with '%2'")
.arg(badtheme).arg(themename));
gCoreContext->OverrideSettingForSession("Theme", themename);
themedir = GetMythUI()->FindThemeDir(themename);
MythTranslation::reload();
GetMythUI()->LoadQtConfig();
GetMythMainWindow()->Init();
GetMythMainWindow()->ReinitDone();
return RunMenu(themedir, themename);
}
static int reloadTheme(void)
{
QString themename = gCoreContext->GetSetting("Theme", DEFAULT_UI_THEME);
QString themedir = GetMythUI()->FindThemeDir(themename);
if (themedir.isEmpty())
{
LOG(VB_GENERAL, LOG_ERR, QString("Couldn't find theme '%1'")
.arg(themename));
return GENERIC_EXIT_NO_THEME;
}
MythTranslation::reload();
GetMythMainWindow()->SetEffectsEnabled(false);
GetMythUI()->LoadQtConfig();
menu->Close();
GetMythMainWindow()->Init();
GetMythMainWindow()->ReinitDone();
GetMythMainWindow()->SetEffectsEnabled(true);
if (!RunMenu(themedir, themename) && !resetTheme(themedir, themename))
return GENERIC_EXIT_NO_THEME;
return 0;
}
int main(int argc, char *argv[])
{
QString geometry = QString::null;
QString display = QString::null;
bool doScan = false;
bool doScanList = false;
bool doScanSaveOnly = false;
bool scanInteractive = true;
bool expertMode = false;
uint scanImport = 0;
bool scanFTAOnly = false;
ServiceRequirements scanServiceRequirements = kRequireAV;
uint scanCardId = 0;
QString scanTableName = "atsc-vsb8-us";
QString scanInputName = "";
MythTVSetupCommandLineParser cmdline;
if (!cmdline.Parse(argc, argv))
{
cmdline.PrintHelp();
return GENERIC_EXIT_INVALID_CMDLINE;
}
if (cmdline.toBool("showhelp"))
{
cmdline.PrintHelp();
return GENERIC_EXIT_OK;
}
if (cmdline.toBool("showversion"))
{
cmdline.PrintVersion();
return GENERIC_EXIT_OK;
}
bool quiet = false, use_display = true;
if (cmdline.toBool("scan"))
{
quiet = true;
use_display = false;
}
CleanupGuard callCleanup(cleanup);
#ifdef Q_WS_MACX
// Without this, we can't set focus to any of the CheckBoxSetting, and most
// of the MythPushButton widgets, and they don't use the themed background.
QApplication::setDesktopSettingsAware(FALSE);
#endif
new QApplication(argc, argv, use_display);
QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHTV_SETUP);
#ifndef _WIN32
QList<int> signallist;
signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT;
SignalHandler handler(signallist);
signal(SIGHUP, SIG_IGN);
#endif
if (cmdline.toBool("display"))
display = cmdline.toString("display");
if (cmdline.toBool("geometry"))
geometry = cmdline.toString("geometry");
int retval;
QString mask("general");
if ((retval = cmdline.ConfigureLogging(mask, quiet)) != GENERIC_EXIT_OK)
return retval;
if (cmdline.toBool("expert"))
expertMode = true;
if (cmdline.toBool("scanlist"))
doScanList = true;
if (cmdline.toBool("savescan"))
doScanSaveOnly = true;
if (cmdline.toBool("scannoninteractive"))
scanInteractive = false;
if (cmdline.toBool("importscan"))
scanImport = cmdline.toUInt("importscan");
if (cmdline.toBool("ftaonly"))
scanFTAOnly = true;
if (cmdline.toBool("servicetype"))
{
scanServiceRequirements = kRequireNothing;
if (cmdline.toString("servicetype").contains("radio"))
scanServiceRequirements = kRequireAudio;
if (cmdline.toString("servicetype").contains("tv"))
scanServiceRequirements = kRequireAV;
if (cmdline.toString("servicetype").contains("tv+radio") ||
cmdline.toString("servicetype").contains("radio+tv"))
scanServiceRequirements = kRequireAudio;
if (cmdline.toString("servicetype").contains("all"))
scanServiceRequirements = kRequireNothing;
}
if (cmdline.toBool("scan"))
{
scanCardId = cmdline.toUInt("scan");
doScan = true;
}
if (cmdline.toBool("freqtable"))
scanTableName = cmdline.toString("freqtable");
if (cmdline.toBool("inputname"))
scanInputName = cmdline.toString("inputname");
if (!display.isEmpty())
{
MythUIHelper::SetX11Display(display);
}
if (!geometry.isEmpty())
{
MythUIHelper::ParseGeometryOverride(geometry);
}
gContext = new MythContext(MYTH_BINARY_VERSION);
if (!gContext->Init(use_display)) // No Upnp, Prompt for db
{
LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext, exiting.");
return GENERIC_EXIT_NO_MYTHCONTEXT;
}
cmdline.ApplySettingsOverride();
if (!GetMythDB()->HaveSchema())
{
if (!InitializeMythSchema())
return GENERIC_EXIT_DB_ERROR;
}
if (use_display)
{
GetMythUI()->LoadQtConfig();
QString fileprefix = GetConfDir();
QDir dir(fileprefix);
if (!dir.exists())
dir.mkdir(fileprefix);
}
if (doScan)
{
bool okCardID = scanCardId;
QStringList inputnames = CardUtil::GetInputNames(scanCardId);
okCardID &= !inputnames.empty();
if (scanInputName.isEmpty())
scanInputName = CardUtil::GetStartInput(scanCardId);
bool okInputName = inputnames.contains(scanInputName);
doScan = (okCardID && okInputName);
if (!okCardID)
{
cerr << "You must enter a valid cardid to scan." << endl;
vector<uint> cardids = CardUtil::GetCardIDs();
if (cardids.empty())
{
cerr << "But no cards have been defined on this host"
<< endl;
return GENERIC_EXIT_INVALID_CMDLINE;
}
cerr << "Valid cards: " << endl;
for (uint i = 0; i < cardids.size(); i++)
{
fprintf(stderr, "%5i: %s %s\n",
cardids[i],
CardUtil::GetRawCardType(cardids[i])
.toAscii().constData(),
CardUtil::GetVideoDevice(cardids[i])
.toAscii().constData());
}
return GENERIC_EXIT_INVALID_CMDLINE;
}
if (!okInputName)
{
cerr << "You must enter a valid input to scan this card."
<< endl;
cerr << "Valid inputs: " << endl;
for (int i = 0; i < inputnames.size(); i++)
{
cerr << inputnames[i].toAscii().constData() << endl;
}
return GENERIC_EXIT_INVALID_CMDLINE;
}
}
if (doScan)
{
int ret = 0;
int firstBreak = scanTableName.indexOf("-");
int secondBreak = scanTableName.lastIndexOf("-");
if (!firstBreak || !secondBreak || firstBreak == secondBreak)
{
cerr << "Failed to parse the frequence table parameter "
<< scanTableName.toLocal8Bit().constData() << endl
<< "Please make sure it is in the format freq_std-"
"modulation-country." << endl;
return GENERIC_EXIT_INVALID_CMDLINE;
}
QString freq_std = scanTableName.mid(0, firstBreak).toLower();
QString mod = scanTableName.mid(
firstBreak+1, secondBreak-firstBreak-1).toLower();
QString tbl = scanTableName.mid(secondBreak+1).toLower();
uint inputid = CardUtil::GetInputID(scanCardId, scanInputName);
uint sourceid = CardUtil::GetSourceID(inputid);
QMap<QString,QString> startChan;
{
ChannelScannerCLI scanner(doScanSaveOnly, scanInteractive);
scanner.Scan(
(freq_std=="atsc") ?
ScanTypeSetting::FullScan_ATSC :
((freq_std=="dvbt") ?
ScanTypeSetting::FullScan_DVBT :
ScanTypeSetting::FullScan_ATSC),
/* cardid */ scanCardId,
/* inputname */ scanInputName,
/* sourceid */ sourceid,
/* ignore signal timeout */ false,
/* follow_nit */ true,
/* test decryption */ true,
scanFTAOnly,
scanServiceRequirements,
// stuff needed for particular scans
/* mplexid */ 0,
startChan, freq_std, mod, tbl);
ret = qApp->exec();
}
return (ret) ? GENERIC_EXIT_NOT_OK : GENERIC_EXIT_OK;
}
if (doScanList)
{
vector<ScanInfo> scans = LoadScanList();
cout<<" scanid cardid sourceid processed date"<<endl;
for (uint i = 0; i < scans.size(); i++)
{
printf("%5i %6i %8i %8s %20s\n",
scans[i].scanid, scans[i].cardid,
scans[i].sourceid, (scans[i].processed) ? "yes" : "no",
scans[i].scandate.toString(Qt::ISODate)
.toAscii().constData());
}
cout<<endl;
return GENERIC_EXIT_OK;
}
if (scanImport)
{
vector<ScanInfo> scans = LoadScanList();
cout<<"*** SCAN IMPORT START ***"<<endl;
{
ScanDTVTransportList list = LoadScan(scanImport);
ChannelImporter ci(false, true, true, true, false,
scanFTAOnly, scanServiceRequirements);
ci.Process(list);
}
cout<<"*** SCAN IMPORT END ***"<<endl;
return GENERIC_EXIT_OK;
}
MythTranslation::load("mythfrontend");
QString themename = gCoreContext->GetSetting("Theme", DEFAULT_UI_THEME);
QString themedir = GetMythUI()->FindThemeDir(themename);
if (themedir.isEmpty())
{
LOG(VB_GENERAL, LOG_ERR, QString("Couldn't find theme '%1'")
.arg(themename));
return GENERIC_EXIT_NO_THEME;
}
MythMainWindow *mainWindow = GetMythMainWindow();
mainWindow->Init();
mainWindow->setWindowTitle(QObject::tr("MythTV Setup"));
// We must reload the translation after a language change and this
// also means clearing the cached/loaded theme strings, so reload the
// theme which also triggers a translation reload
if (LanguageSelection::prompt())
{
if (!reloadTheme())
return GENERIC_EXIT_NO_THEME;
}
if (!DBUtil::CheckTimeZoneSupport())
{
LOG(VB_GENERAL, LOG_ERR, "MySQL time zone support is not missing. "
"Please install it and try again.");
return GENERIC_EXIT_DB_NOTIMEZONE;
}
if (!UpgradeTVDatabaseSchema(true))
{
LOG(VB_GENERAL, LOG_ERR, "Couldn't upgrade database to new schema.");
return GENERIC_EXIT_DB_OUTOFDATE;
}
// Refresh Global/Main Menu keys after DB update in case there was no DB
// when they were written originally
mainWindow->ReloadKeys();
if (!startPrompt)
startPrompt = new StartPrompter();
startPrompt->handleStart();
// Let the user select buttons, type values, scan for channels, etc.
if (!RunMenu(themedir, themename) && !resetTheme(themedir, themename))
return GENERIC_EXIT_NO_THEME;
ExpertSettingsEditor *expertEditor = NULL;
if (expertMode)
{
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
expertEditor =
new ExpertSettingsEditor(mainStack, "Expert Settings Editor");
if (expertEditor->Create())
mainStack->AddScreen(expertEditor);
else
{
delete expertEditor;
expertEditor = NULL;
LOG(VB_GENERAL, LOG_ERR,
"Unable to create expert settings editor window");
return GENERIC_EXIT_OK;
}
}
qApp->exec();
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */