/
main.cpp
623 lines (521 loc) · 17.5 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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// POSIX headers
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
// Qt headers
#include <QDir>
#include <QApplication>
#include <QRegExp>
// MythTV headers
#include <mythcontext.h>
#include <mythplugin.h>
#include <mythmediamonitor.h>
#include <mythdbcon.h>
#include <mythdb.h>
#include <mythpluginapi.h>
#include <mythversion.h>
#include <myththemedmenu.h>
#include <compat.h>
#include <mythuihelper.h>
#include <mythprogressdialog.h>
#include <lcddevice.h>
#include <musicmetadata.h>
#include <musicutils.h>
// MythMusic headers
#include "musicdata.h"
#include "decoder.h"
#include "playlisteditorview.h"
#include "playlistview.h"
#include "streamview.h"
#include "playlistcontainer.h"
#include "dbcheck.h"
#include "filescanner.h"
#include "musicplayer.h"
#include "config.h"
#include "mainvisual.h"
#include "generalsettings.h"
#include "playersettings.h"
#include "visualizationsettings.h"
#include "importsettings.h"
#include "ratingsettings.h"
#include "importmusic.h"
#ifdef HAVE_CDIO
#include "cdrip.h"
#endif
/**
* \brief Work out the best CD drive to use at this time
*/
static QString chooseCD(void)
{
if (gCDdevice.length())
return gCDdevice;
#ifdef Q_OS_MAC
return MediaMonitor::GetMountPath(MediaMonitor::defaultCDdevice());
#endif
return MediaMonitor::defaultCDdevice();
}
static void loadMusic()
{
// only do this once
if (gMusicData->initialized)
return;
MSqlQuery count_query(MSqlQuery::InitCon());
bool musicdata_exists = false;
if (count_query.exec("SELECT COUNT(*) FROM music_songs;"))
{
if(count_query.next() &&
0 != count_query.value(0).toInt())
{
musicdata_exists = true;
}
}
QString musicDir = getMusicDirectory();
// Only search music files if a directory was specified & there
// is no data in the database yet (first run). Otherwise, user
// can choose "Setup" option from the menu to force it.
if (!musicDir.isEmpty() && !musicdata_exists)
{
FileScanner *fscan = new FileScanner();
fscan->SearchDir(musicDir);
delete fscan;
}
MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
QString message = QObject::tr("Loading Music. Please wait ...");
MythUIBusyDialog *busy = new MythUIBusyDialog(message, popupStack,
"musicscanbusydialog");
if (busy->Create())
popupStack->AddScreen(busy, false);
else
busy = NULL;
// Set the various track formatting modes
MusicMetadata::setArtistAndTrackFormats();
AllMusic *all_music = new AllMusic();
// Load all playlists into RAM (once!)
PlaylistContainer *all_playlists = new PlaylistContainer(all_music);
gMusicData->all_music = all_music;
gMusicData->all_streams = new AllStream();
gMusicData->all_playlists = all_playlists;
gMusicData->initialized = true;
while (!gMusicData->all_playlists->doneLoading() || !gMusicData->all_music->doneLoading())
{
qApp->processEvents();
usleep(50000);
}
gPlayer->loadStreamPlaylist();
gPlayer->loadPlaylist();
if (busy)
busy->Close();
}
static void startPlayback(void)
{
loadMusic();
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
PlaylistView *view = new PlaylistView(mainStack);
if (view->Create())
mainStack->AddScreen(view);
else
delete view;
}
static void startStreamPlayback(void)
{
loadMusic();
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
StreamView *view = new StreamView(mainStack);
if (view->Create())
mainStack->AddScreen(view);
else
delete view;
}
static void startDatabaseTree(void)
{
loadMusic();
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
QString lastView = gCoreContext->GetSetting("MusicPlaylistEditorView", "tree");
PlaylistEditorView *view = new PlaylistEditorView(mainStack, lastView);
if (view->Create())
mainStack->AddScreen(view);
else
delete view;
}
static void startRipper(void)
{
loadMusic();
#if defined HAVE_CDIO
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
Ripper *rip = new Ripper(mainStack, chooseCD());
if (rip->Create())
{
mainStack->AddScreen(rip);
QObject::connect(rip, SIGNAL(ripFinished()),
gMusicData, SLOT(reloadMusic()),
Qt::QueuedConnection);
}
else
delete rip;
#endif
}
static void runScan(void)
{
// if we don't have a valid start dir warn the user and give up
if (getMusicDirectory().isEmpty())
{
ShowOkPopup(QObject::tr("You need to tell me where to find your music on the "
"'General Settings' page of MythMusic's settings pages."));
return;
}
if (!QFile::exists(getMusicDirectory()))
{
ShowOkPopup(QObject::tr("Can't find your music directory. Have you set it correctly on the "
"'General Settings' page of MythMusic's settings pages?"));
return;
}
LOG(VB_GENERAL, LOG_INFO, QString("Scanning '%1' for music files").arg(getMusicDirectory()));
FileScanner *fscan = new FileScanner();
QString musicDir = getMusicDirectory();
fscan->SearchDir(musicDir);
// save anything that may have changed
if (gMusicData->all_music && gMusicData->all_music->cleanOutThreads())
gMusicData->all_music->save();
if (gMusicData->all_playlists && gMusicData->all_playlists->cleanOutThreads())
{
gMusicData->all_playlists->save();
}
// force a complete reload of the tracks and playlists
gPlayer->stop(true);
delete gMusicData;
gMusicData = new MusicData;
loadMusic();
delete fscan;
}
static void startImport(void)
{
loadMusic();
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
ImportMusicDialog *import = new ImportMusicDialog(mainStack);
if (import->Create())
{
mainStack->AddScreen(import);
QObject::connect(import, SIGNAL(importFinished()),
gMusicData, SLOT(reloadMusic()),
Qt::QueuedConnection);
}
else
delete import;
}
static void MusicCallback(void *data, QString &selection)
{
(void) data;
QString sel = selection.toLower();
if (sel == "music_create_playlist")
startDatabaseTree();
else if (sel == "music_play")
startPlayback();
else if (sel == "stream_play")
startStreamPlayback();
else if (sel == "music_rip")
{
startRipper();
}
else if (sel == "music_import")
{
startImport();
}
else if (sel == "settings_scan")
{
runScan();
}
else if (sel == "settings_general")
{
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
GeneralSettings *gs = new GeneralSettings(mainStack, "general settings");
if (gs->Create())
mainStack->AddScreen(gs);
else
delete gs;
}
else if (sel == "settings_player")
{
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
PlayerSettings *ps = new PlayerSettings(mainStack, "player settings");
if (ps->Create())
mainStack->AddScreen(ps);
else
delete ps;
}
else if (sel == "settings_rating")
{
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
RatingSettings *rs = new RatingSettings(mainStack, "rating settings");
if (rs->Create())
mainStack->AddScreen(rs);
else
delete rs;
}
else if (sel == "settings_visualization")
{
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
VisualizationSettings *vs = new VisualizationSettings(mainStack, "visualization settings");
if (vs->Create())
mainStack->AddScreen(vs);
else
delete vs;
}
else if (sel == "settings_import")
{
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
ImportSettings *is = new ImportSettings(mainStack, "import settings");
if (is->Create())
mainStack->AddScreen(is);
else
delete is;
}
}
static int runMenu(QString which_menu)
{
QString themedir = GetMythUI()->GetThemeDir();
MythThemedMenu *diag = new MythThemedMenu(
themedir, which_menu, GetMythMainWindow()->GetMainStack(),
"music menu");
diag->setCallback(MusicCallback, NULL);
diag->setKillable();
if (diag->foundTheme())
{
if (LCD *lcd = LCD::Get())
{
lcd->switchToTime();
}
GetMythMainWindow()->GetMainStack()->AddScreen(diag);
return 0;
}
else
{
LOG(VB_GENERAL, LOG_ERR, QString("Couldn't find menu %1 or theme %2")
.arg(which_menu).arg(themedir));
delete diag;
return -1;
}
}
static void runMusicPlayback(void)
{
GetMythUI()->AddCurrentLocation("playmusic");
startPlayback();
GetMythUI()->RemoveCurrentLocation();
}
static void runMusicStreamPlayback(void)
{
GetMythUI()->AddCurrentLocation("streammusic");
startStreamPlayback();
GetMythUI()->RemoveCurrentLocation();
}
static void runMusicSelection(void)
{
GetMythUI()->AddCurrentLocation("musicplaylists");
startDatabaseTree();
GetMythUI()->RemoveCurrentLocation();
}
static void runRipCD(void)
{
loadMusic();
#if defined HAVE_CDIO
MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
Ripper *rip = new Ripper(mainStack, chooseCD());
if (rip->Create())
mainStack->AddScreen(rip);
else
delete rip;
QObject::connect(rip, SIGNAL(ripFinished()),
gMusicData, SLOT(reloadMusic()),
Qt::QueuedConnection);
#endif
}
static void showMiniPlayer(void)
{
if (!gMusicData->all_music)
return;
// only show the miniplayer if there isn't already a client attached
if (!gPlayer->hasClient())
gPlayer->showMiniPlayer();
}
#ifdef FIXME // the only call is likewise commented out and needs fixing
static void handleMedia(MythMediaDevice *cd)
{
// if the music player is already playing ignore the event
if (gPlayer->isPlaying())
{
LOG(VB_GENERAL, LOG_NOTICE, "Got a media changed event but ignoring since "
"the music player is already playing");
return;
}
else
LOG(VB_GENERAL, LOG_NOTICE, "Got a media changed event");
// Note that we should deal with other disks that may contain music.
// e.g. MEDIATYPE_MMUSIC or MEDIATYPE_MIXED
if (!cd)
return;
if (cd->isUsable())
{
QString newDevice;
#ifdef Q_OS_MAC
newDevice = cd->getMountPath();
#else
newDevice = cd->getDevicePath();
#endif
if (gCDdevice.length() && gCDdevice != newDevice)
{
// In the case of multiple audio CDs, clear the old stored device
// so the user has to choose (via MediaMonitor::defaultCDdevice())
gCDdevice = QString::null;
LOG(VB_MEDIA, LOG_INFO, "MythMusic: Forgetting existing CD");
}
else
{
gCDdevice = newDevice;
LOG(VB_MEDIA, LOG_INFO,
"MythMusic: Storing CD device " + gCDdevice);
}
}
else
{
gCDdevice = QString::null;
return;
}
GetMythMainWindow()->JumpTo("Main Menu");
if (gCoreContext->GetNumSetting("AutoPlayCD", 0))
{
// Empty the playlist to ensure CD is played first
if (gMusicData->all_music)
gMusicData->all_music->clearCDData();
if (gMusicData->all_playlists)
gMusicData->all_playlists->clearCDList();
runMusicPlayback();
}
else
mythplugin_run();
}
#endif
static void setupKeys(void)
{
REG_JUMP(QT_TRANSLATE_NOOP("MythControls", "Play music"),
"", "", runMusicPlayback);
REG_JUMP(QT_TRANSLATE_NOOP("MythControls", "Select music playlists"),
"", "", runMusicSelection);
REG_JUMP(QT_TRANSLATE_NOOP("MythControls", "Play radio stream"),
"", "", runMusicStreamPlayback);
REG_JUMP(QT_TRANSLATE_NOOP("MythControls", "Rip CD"),
"", "", runRipCD);
REG_JUMP(QT_TRANSLATE_NOOP("MythControls", "Scan music"),
"", "", runScan);
REG_JUMPEX(QT_TRANSLATE_NOOP("MythControls", "Show Music Miniplayer"),
"", "", showMiniPlayer, false);
REG_KEY("Music", "NEXTTRACK", QT_TRANSLATE_NOOP("MythControls",
"Move to the next track"), ">,.,Z,End");
REG_KEY("Music", "PREVTRACK", QT_TRANSLATE_NOOP("MythControls",
"Move to the previous track"), ",,<,Q,Home");
REG_KEY("Music", "FFWD", QT_TRANSLATE_NOOP("MythControls",
"Fast forward"), "PgDown");
REG_KEY("Music", "RWND", QT_TRANSLATE_NOOP("MythControls",
"Rewind"), "PgUp");
REG_KEY("Music", "PAUSE", QT_TRANSLATE_NOOP("MythControls",
"Pause/Start playback"), "P");
REG_KEY("Music", "PLAY", QT_TRANSLATE_NOOP("MythControls",
"Start playback"), "");
REG_KEY("Music", "STOP", QT_TRANSLATE_NOOP("MythControls",
"Stop playback"), "O");
REG_KEY("Music", "VOLUMEDOWN", QT_TRANSLATE_NOOP("MythControls",
"Volume down"), "[,{,F10,Volume Down");
REG_KEY("Music", "VOLUMEUP", QT_TRANSLATE_NOOP("MythControls",
"Volume up"), "],},F11,Volume Up");
REG_KEY("Music", "MUTE", QT_TRANSLATE_NOOP("MythControls",
"Mute"), "|,\\,F9,Volume Mute");
REG_KEY("Music", "TOGGLEUPMIX",QT_TRANSLATE_NOOP("MythControls",
"Toggle audio upmixer"), "Ctrl+U");
REG_KEY("Music", "CYCLEVIS", QT_TRANSLATE_NOOP("MythControls",
"Cycle visualizer mode"), "6");
REG_KEY("Music", "BLANKSCR", QT_TRANSLATE_NOOP("MythControls",
"Blank screen"), "5");
REG_KEY("Music", "THMBUP", QT_TRANSLATE_NOOP("MythControls",
"Increase rating"), "9");
REG_KEY("Music", "THMBDOWN", QT_TRANSLATE_NOOP("MythControls",
"Decrease rating"), "7");
REG_KEY("Music", "REFRESH", QT_TRANSLATE_NOOP("MythControls",
"Refresh music tree"), "8");
REG_KEY("Music", "SPEEDUP", QT_TRANSLATE_NOOP("MythControls",
"Increase Play Speed"), "W");
REG_KEY("Music", "SPEEDDOWN", QT_TRANSLATE_NOOP("MythControls",
"Decrease Play Speed"), "X");
REG_KEY("Music", "MARK", QT_TRANSLATE_NOOP("MythControls",
"Toggle track selection"), "T");
REG_KEY("Music", "TOGGLESHUFFLE", QT_TRANSLATE_NOOP("MythControls",
"Toggle shuffle mode"), "");
REG_KEY("Music", "TOGGLEREPEAT", QT_TRANSLATE_NOOP("MythControls",
"Toggle repeat mode"), "");
// switch to view key bindings
REG_KEY("Music", "SWITCHTOPLAYLIST", QT_TRANSLATE_NOOP("MythControls",
"Switch to the current playlist view"), "");
REG_KEY("Music", "SWITCHTOPLAYLISTEDITORTREE", QT_TRANSLATE_NOOP("MythControls",
"Switch to the playlist editor tree view"), "");
REG_KEY("Music", "SWITCHTOPLAYLISTEDITORGALLERY", QT_TRANSLATE_NOOP("MythControls",
"Switch to the playlist editor gallery view"), "");
REG_KEY("Music", "SWITCHTOSEARCH", QT_TRANSLATE_NOOP("MythControls",
"Switch to the search view"), "");
REG_KEY("Music", "SWITCHTOVISUALISER", QT_TRANSLATE_NOOP("MythControls",
"Switch to the fullscreen visualiser view"), "");
REG_KEY("Music", "SWITCHTORADIO", QT_TRANSLATE_NOOP("MythControls",
"Switch to the radio stream view"), "");
#ifdef FIXME
// FIXME need to find a way to stop the media monitor jumping to the main menu before
// calling the handler
REG_MEDIA_HANDLER(QT_TRANSLATE_NOOP("MythControls",
"MythMusic Media Handler 1/2"), "", "", handleMedia,
MEDIATYPE_AUDIO | MEDIATYPE_MIXED, QString::null);
REG_MEDIA_HANDLER(QT_TRANSLATE_NOOP("MythControls",
"MythMusic Media Handler 2/2"), "", "", handleMedia,
MEDIATYPE_MMUSIC, "mp3,mp2,ogg,oga,flac,wma,wav,ac3,"
"oma,omg,atp,ra,dts,aac,m4a,aa3,tta,"
"mka,aiff,swa,wv");
#endif
}
int mythplugin_init(const char *libversion)
{
if (!gCoreContext->TestPluginVersion("mythmusic", libversion,
MYTH_BINARY_VERSION))
return -1;
gCoreContext->ActivateSettingsCache(false);
bool upgraded = UpgradeMusicDatabaseSchema();
gCoreContext->ActivateSettingsCache(true);
if (!upgraded)
{
LOG(VB_GENERAL, LOG_ERR,
"Couldn't upgrade music database schema, exiting.");
return -1;
}
setupKeys();
gPlayer = new MusicPlayer(NULL);
gMusicData = new MusicData();
return 0;
}
int mythplugin_run(void)
{
return runMenu("musicmenu.xml");
}
int mythplugin_config(void)
{
return runMenu("music_settings.xml");
}
void mythplugin_destroy(void)
{
gPlayer->stop(true);
// TODO these should be saved when they are changed
// Automagically save all playlists and metadata (ratings) that have changed
if (gMusicData->all_music && gMusicData->all_music->cleanOutThreads())
{
gMusicData->all_music->save();
}
if (gMusicData->all_playlists && gMusicData->all_playlists->cleanOutThreads())
{
gMusicData->all_playlists->save();
}
delete gPlayer;
delete gMusicData;
}