juk.cpp 24.5 KB
Newer Older
1 2 3 4 5
/***************************************************************************
                          juk.cpp  -  description
                             -------------------
    begin                : Mon Feb  4 23:40:41 EST 2002
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7
***************************************************************************/
8 9 10 11 12 13 14 15 16 17

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

18 19 20 21 22 23 24 25 26 27
#include <kaction.h>
#include <kcmdlineargs.h>
#include <kstatusbar.h>
#include <kdebug.h>
#include <kmessagebox.h>

#include <qmetaobject.h>
#include <qslider.h>
#include <qmime.h>

28 29 30 31 32 33 34
#include "juk.h"
#include "slideraction.h"
#include "statuslabel.h"
#include "splashscreen.h"
#include "systemtray.h"
#include "keydialog.h"
#include "tagguesserconfigdlg.h"
35
#include "filerenamerconfigdlg.h"
36
#include "playermanager.h"
37

38 39 40 41
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
42 43 44 45
JuK::JuK(QWidget *parent, const char *name) :
    DCOPObject("Player"),
    KMainWindow(parent, name, WDestructiveClose),
    m_shuttingDown(false)
46
{
47 48
    // Expect segfaults if you change this order.

49
    readSettings();
50

51
    if(m_showSplash && !m_startDocked) {
52 53 54 55
	SplashScreen::instance()->show();
	kapp->processEvents();
    }

56
    setupActions();
57 58
    setupLayout();
    setupSplitterConnections();
59
    slotPlaylistChanged();
60
    readConfig();
61
    setupPlayer();
62
    setupSystemTray();
63
    setupGlobalAccels();
64
    processArgs();
65

66
    SplashScreen::finishedLoading();
Scott Wheeler's avatar
Scott Wheeler committed
67
    QTimer::singleShot(0, CollectionList::instance(), SLOT(slotCheckCache()));
68 69 70 71
}

JuK::~JuK()
{
72

73 74
}

75 76 77 78 79 80
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

void JuK::setVolume(float volume)
{
81 82
    if(m_sliderAction->volumeSlider()->maxValue() > 0 &&
       volume >= 0 && m_sliderAction->volumeSlider()->maxValue() >= volume)
83
    {
84 85 86
	int v = int(volume / 100 * m_sliderAction->volumeSlider()->maxValue());
	slotSetVolume(v);
	m_sliderAction->volumeSlider()->setValue(v);
87 88 89 90 91 92 93 94 95 96 97
    }
}

void JuK::startPlayingPlaylist()
{
    if(m_randomPlayAction->isChecked())
        play(m_splitter->playRandomFile());
    else
        play(m_splitter->playFirstFile());
}

98
void JuK::slotGuessTagInfoFromFile()
99
{
100 101 102 103 104 105
    m_splitter->slotGuessTagInfo(TagGuesser::FileName);
}

void JuK::slotGuessTagInfoFromInternet()
{
    m_splitter->slotGuessTagInfo(TagGuesser::MusicBrainz);
106 107
}

108 109
void JuK::play()
{
110
    m_player->play();
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
}

void JuK::pause()
{
    m_player->pause();
}

void JuK::stop()
{
    m_player->stop();
    m_splitter->stop();
}

void JuK::back()
{
    play(m_splitter->playPreviousFile(m_randomPlayAction->isChecked()));
}

void JuK::back(int howMany)
{
    for(--howMany; howMany > 0; --howMany)
        m_splitter->playPreviousFile(m_randomPlayAction->isChecked());

    play(m_splitter->playPreviousFile(m_randomPlayAction->isChecked()));
}

void JuK::slotPopulateBackMenu()
{
Scott Wheeler's avatar
Scott Wheeler committed
139 140
    m_splitter->populatePlayHistoryMenu(m_backAction->popupMenu(),
					m_randomPlayAction->isChecked());
141 142 143 144
}

void JuK::forward()
{
Scott Wheeler's avatar
Scott Wheeler committed
145 146
    play(m_splitter->playNextFile(m_randomPlayAction->isChecked(),
				  m_loopPlaylistAction->isChecked()));
147 148 149 150
}

void JuK::seekBack()
{
151 152 153
    int position = m_sliderAction->trackPositionSlider()->value();
    position = QMAX(m_sliderAction->trackPositionSlider()->minValue(), position - 10);
    emit m_sliderAction->trackPositionSlider()->setValue(position);
154 155 156 157
}

void JuK::seekForward()
{
158 159 160
    int position = m_sliderAction->trackPositionSlider()->value();
    position = QMIN(m_sliderAction->trackPositionSlider()->maxValue(), position + 10);
    emit m_sliderAction->trackPositionSlider()->setValue(position);
161 162 163 164 165 166 167 168 169 170 171 172
}

void JuK::playPause()
{
    if(m_player->playing())
	pause();
    else
	play();
}

void JuK::volumeUp()
{
173 174 175
    if(m_sliderAction && m_sliderAction->volumeSlider()) {
	int volume = m_sliderAction->volumeSlider()->value() +
	    m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
176
	slotSetVolume(volume);
177
	m_sliderAction->volumeSlider()->setValue(volume);
178 179 180 181 182
    }
}

void JuK::volumeDown()
{
183 184 185
    if(m_sliderAction && m_sliderAction->volumeSlider()) {
	int volume = m_sliderAction->volumeSlider()->value() -
	    m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
186
	slotSetVolume(volume);
187
	m_sliderAction->volumeSlider()->setValue(volume);
188 189 190 191 192
    }
}

void JuK::volumeMute()
{
193
    if(m_sliderAction && m_sliderAction->volumeSlider()) {
194
	if(m_muted)
195
	    slotSetVolume(m_sliderAction->volumeSlider()->value());
196 197 198 199 200 201 202 203 204 205 206
	else
	    slotSetVolume(0);
	    m_muted = !m_muted;
    }
}

void JuK::openFile(const QString &file)
{
    m_splitter->open(file);
}

207 208 209 210 211 212
void JuK::openFile(const QStringList &files)
{
    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
	openFile(*it);
}

213 214 215 216
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

217 218
void JuK::setupLayout()
{
219
    m_splitter = new PlaylistSplitter(this, "playlistSplitter");
220
    setCentralWidget(m_splitter);
221 222

    // playlist item activation connection
Scott Wheeler's avatar
Scott Wheeler committed
223 224 225 226
    connect(m_splitter, SIGNAL(signalActivated()),
	    this, SLOT(slotPlaySelectedFile()));
    connect(m_splitter, SIGNAL(signalListBoxDoubleClicked()),
	    this, SLOT(startPlayingPlaylist()));
227

228
    // create status bar
229

230
    m_statusLabel = new StatusLabel(statusBar());
231

232
    statusBar()->addWidget(m_statusLabel, 1);
233

Scott Wheeler's avatar
Scott Wheeler committed
234 235
    connect(m_splitter, SIGNAL(signalSelectedPlaylistCountChanged(int)),
	    m_statusLabel, SLOT(setPlaylistCount(int)));
236 237
    connect(m_splitter, SIGNAL(signalSelectedPlaylistTimeChanged(int)),
	    m_statusLabel, SLOT(setPlaylistTime(int)));
Scott Wheeler's avatar
Scott Wheeler committed
238 239
    connect(m_statusLabel, SIGNAL(jumpButtonClicked()),
	    m_splitter, SLOT(slotSelectPlaying()));
240

241 242
    PlayerManager::instance()->setStatusLabel(m_statusLabel);

243 244
    // Needs to be here because m_splitter is not called before setupActions
    // (because PlaylistSplitter itself accesses the actionCollection)
245

246 247 248
    new KAction(i18n("&Rename File"), 0, "CTRL+r", m_splitter, SLOT(slotRenameFile()),
                actionCollection(), "renameFile"); // 4

249
    m_splitter->setFocus();
250

251
    resize(750, 500);
252 253
}

254 255
void JuK::setupActions()
{
256
    //////////////////////////////////////////////////
257
    // file menu
258
    //////////////////////////////////////////////////
259

Scott Wheeler's avatar
Scott Wheeler committed
260 261 262
    KActionMenu *newMenu = new KActionMenu(i18n("&New"), "filenew",
					   actionCollection(), "file_new");

263 264 265 266 267 268
    // This connection will call the
    // PlaylistSplitter::slotCreatePlaylist(const QString &) slot - this is
    // possible because the QString parameter has a default value, so the
    // slot can be called without arguments (as required by the signal's
    // signature).

Scott Wheeler's avatar
Scott Wheeler committed
269 270 271
    newMenu->insert(createSplitterAction(i18n("Empty Playlist..."),
					 SLOT(slotCreatePlaylist()), 0, 0, 0));

272
    newMenu->insert(createSplitterAction(i18n("Playlist From Folder..."),
Scott Wheeler's avatar
Scott Wheeler committed
273 274 275 276 277
					 SLOT(slotCreatePlaylistFromDir()), 0, 0, 0));

    createSplitterAction(
	i18n("Open..."), SLOT(slotOpen()), "file_open", "fileopen", "CTRL+o");
    createSplitterAction(
278
	i18n("Open &Folder..."), SLOT(slotOpenDirectory()), "openDirectory", "fileopen");
Scott Wheeler's avatar
Scott Wheeler committed
279 280 281 282 283 284 285 286 287 288 289 290
    createSplitterAction(
	i18n("&Rename..."), SLOT(slotRenamePlaylist()), "renamePlaylist");
    createSplitterAction(
	i18n("D&uplicate..."), SLOT(slotDuplicatePlaylist()), "duplicatePlaylist");
    createSplitterAction(
	i18n("Save"), SLOT(slotSavePlaylist()), "file_save", "filesave", "CTRL+s");
    createSplitterAction(
	i18n("Save As..."), SLOT(slotSaveAsPlaylist()), "file_save_as", "filesaveas");
    createSplitterAction(
	i18n("R&emove"), SLOT(slotDeletePlaylist()), "deleteItemPlaylist", "edittrash");
    createSplitterAction(
	i18n("Reload"), SLOT(slotReloadPlaylist()), "reloadPlaylist", "reload");
291

292
    KStdAction::quit(this, SLOT(slotQuit()), actionCollection());
293

294
    //////////////////////////////////////////////////
295
    // edit menu
296 297
    //////////////////////////////////////////////////

298 299 300 301
    KStdAction::cut(kapp,   SLOT(cut()),   actionCollection());
    KStdAction::copy(kapp,  SLOT(copy()),  actionCollection());
    KStdAction::paste(kapp, SLOT(paste()), actionCollection());
    KStdAction::clear(kapp, SLOT(clear()), actionCollection());
302

303
    KStdAction::selectAll(kapp, SLOT(selectAll()), actionCollection());
304

305
    createSplitterAction(
306
	i18n("Advanced Search"), SLOT(slotAdvancedSearch()), "advancedSearch", "find", "s");
307

308
    //////////////////////////////////////////////////
309
    // view menu
310
    //////////////////////////////////////////////////
311

Scott Wheeler's avatar
Scott Wheeler committed
312 313 314 315
    m_showSearchAction = new KToggleAction(i18n("Show &Search Bar"), "filefind",
					   0, actionCollection(), "showSearch");
    m_showEditorAction = new KToggleAction(i18n("Show &Tag Editor"), "edit",
					   0, actionCollection(), "showEditor");
316
    m_showHistoryAction = new KToggleAction(i18n("Show &History"), "history",
317
					   0, actionCollection(), "showHistory");
318

319
    createSplitterAction(i18n("Refresh Items"), SLOT(slotRefresh()), "refresh", "reload");
320

321
    //////////////////////////////////////////////////
322
    // play menu
323 324
    //////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
325 326 327 328 329 330 331 332 333 334 335 336
    m_randomPlayAction = new KToggleAction(i18n("&Random Play"), 0,
					   actionCollection(), "randomPlay");

    new KAction(i18n("&Play"),  "player_play",  0, this, SLOT(play()),
		actionCollection(), "play");

    new KAction(i18n("P&ause"), "player_pause", 0, this, SLOT(pause()),
		actionCollection(), "pause");

    new KAction(i18n("&Stop"),  "player_stop",  0, this, SLOT(stop()),
		actionCollection(), "stop");

337
    m_backAction = new KToolBarPopupAction(i18n("Previous &Track"), "player_start", 0,
Scott Wheeler's avatar
Scott Wheeler committed
338
					   this, SLOT(back()), actionCollection(), "back");
339

Scott Wheeler's avatar
Scott Wheeler committed
340 341
    connect(m_backAction->popupMenu(), SIGNAL(aboutToShow()),
	    this, SLOT(slotPopulateBackMenu()));
342

Scott Wheeler's avatar
Scott Wheeler committed
343 344
    connect(m_backAction->popupMenu(), SIGNAL(activated(int)),
	    this, SLOT(back(int)));
345

346
    new KAction(i18n("&Next Track"), "player_end", 0, this, SLOT(forward()),
Scott Wheeler's avatar
Scott Wheeler committed
347
		actionCollection(), "forward");
348

Scott Wheeler's avatar
Scott Wheeler committed
349 350
    m_loopPlaylistAction = new KToggleAction(i18n("&Loop Playlist"), 0, 0,
					     actionCollection(), "loopPlaylist");
351

352
    //////////////////////////////////////////////////
353
    // tagger menu
354 355
    //////////////////////////////////////////////////

356
    createSplitterAction(i18n("&Save"), SLOT(slotSaveTag()),
Scott Wheeler's avatar
Scott Wheeler committed
357 358 359
			 "saveItem",   "filesave", "CTRL+t");
    createSplitterAction(i18n("&Delete"), SLOT(slotDeleteSelectedItems()),
			 "removeItem", "editdelete");
360

361
    m_guessMenu = new KActionMenu(i18n("&Guess Tag Information"),
Scott Wheeler's avatar
Scott Wheeler committed
362 363
					     QString::null, actionCollection(),
					     "guessTag");
364

Scott Wheeler's avatar
Scott Wheeler committed
365
    KAction *a;
366

367 368
    a = new KAction(i18n("From &Filename"), 0, "CTRL+f", this, SLOT(slotGuessTagInfoFromFile()),
                actionCollection(), "guessTagFile");
369
    m_guessMenu->insert(a);
370
#if HAVE_MUSICBRAINZ
371 372
    a = new KAction(i18n("From &Internet"), 0, "CTRL+i", this, SLOT(slotGuessTagInfoFromInternet()),
                actionCollection(), "guessTagInternet");
373
    m_guessMenu->insert(a);
374
#endif
Scott Wheeler's avatar
Scott Wheeler committed
375

376
    //////////////////////////////////////////////////
377
    // settings menu
378 379
    //////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
380 381
    new KToggleAction(i18n("Show Menu Bar"), "CTRL+m", this,
		      SLOT(slotToggleMenuBar()), actionCollection(), "toggleMenuBar");
382

383
    setStandardToolBarMenuEnabled(true);
384

Stephan Binner's avatar
Stephan Binner committed
385
    m_toggleSplashAction = new KToggleAction(i18n("Show Splash Screen on Startup"),
Scott Wheeler's avatar
Scott Wheeler committed
386 387 388 389 390 391 392
					     0, actionCollection(), "showSplashScreen");

    m_toggleSystemTrayAction = new KToggleAction(i18n("&Dock in System Tray"),
						 0, actionCollection(), "toggleSystemTray");

    m_toggleDockOnCloseAction = new KToggleAction(i18n("&Stay in System Tray on Close"),
						  0, actionCollection(), "dockOnClose");
393

394
    m_togglePopupsAction = new KToggleAction(i18n("Popup &Track Announcement"),
Scott Wheeler's avatar
Scott Wheeler committed
395
					     0, this, 0, actionCollection(), "togglePopups");
396

Scott Wheeler's avatar
Scott Wheeler committed
397 398
    connect(m_toggleSystemTrayAction, SIGNAL(toggled(bool)),
	    this, SLOT(slotToggleSystemTray(bool)));
399

Scott Wheeler's avatar
Scott Wheeler committed
400

401
    m_outputSelectAction = Player::playerSelectAction(actionCollection());
402

403
    if(m_outputSelectAction)
404
        m_outputSelectAction->setCurrentItem(0);
405

Scott Wheeler's avatar
Scott Wheeler committed
406 407
    new KAction(i18n("&Tag Guesser..."), 0, 0, this, SLOT(slotConfigureTagGuesser()),
		actionCollection(), "tagGuesserConfig");
408

409 410 411
    new KAction(i18n("&File Renamer..."), 0, 0, this, SLOT(slotConfigureFileRenamer()),
		actionCollection(), "fileRenamerConfig");

412 413
    KStdAction::keyBindings(this, SLOT(slotEditKeys()), actionCollection());

414
    //////////////////////////////////////////////////
415
    // just in the toolbar
416 417
    //////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
418 419
    m_sliderAction = new SliderAction(i18n("Track Position"), actionCollection(),
				      "trackPositionAction");
420 421

    createGUI();
422 423

    // set the slider to the proper orientation and make it stay that way
424
    m_sliderAction->slotUpdateOrientation();
425 426
}

427 428 429 430 431 432
void JuK::setupSplitterConnections()
{
    QValueListConstIterator<SplitterConnection> it = m_splitterConnections.begin();
    for(; it != m_splitterConnections.end(); ++it)
        connect((*it).first, SIGNAL(activated()), m_splitter, (*it).second);

Scott Wheeler's avatar
Scott Wheeler committed
433 434 435 436
    connect(m_showSearchAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetSearchVisible(bool)));
    connect(m_showEditorAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetEditorVisible(bool)));
437 438
    connect(m_showHistoryAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetHistoryVisible(bool)));
439
    connect(this, SIGNAL(dockWindowPositionChanged(QDockWindow *)),
Scott Wheeler's avatar
Scott Wheeler committed
440 441 442
	    m_sliderAction, SLOT(slotUpdateOrientation(QDockWindow *)));
    connect(m_splitter, SIGNAL(signalPlaylistChanged()),
	    this, SLOT(slotPlaylistChanged()));
443 444
}

445
void JuK::setupSystemTray()
446
{
447
    if(m_toggleSystemTrayAction && m_toggleSystemTrayAction->isChecked()) {
448 449
        m_systemTray = new SystemTray(this, "systemTray");
        m_systemTray->show();
450

451
        connect(this, SIGNAL(signalNewSong(const QString&)),
Scott Wheeler's avatar
Scott Wheeler committed
452
		m_systemTray, SLOT(slotNewSong(const QString&)));
453

454
        m_toggleDockOnCloseAction->setEnabled(true);
455
	m_togglePopupsAction->setEnabled(true);
456

457
        connect(m_systemTray, SIGNAL(quitSelected()), this, SLOT(slotQuit()));
458
    }
459
    else {
460 461
        m_systemTray = 0;
        m_toggleDockOnCloseAction->setEnabled(false);
462
	m_togglePopupsAction->setEnabled(false);
463
    }
464 465
}

466 467
void JuK::setupPlayer()
{
Scott Wheeler's avatar
Scott Wheeler committed
468
    m_muted = false;
469

470
    if(m_sliderAction) {
471 472
    }

473 474
    m_player = PlayerManager::instance();
    PlayerManager::instance()->setPlaylistInterface(m_splitter);
475 476
}

477

478 479 480
void JuK::setupGlobalAccels()
{
    m_accel = new KGlobalAccel(this);
481 482 483 484 485 486 487 488 489
    KeyDialog::insert(m_accel, "PlayPause",   i18n("Play/Pause"),   this, SLOT(playPause()));
    KeyDialog::insert(m_accel, "Stop",        i18n("Stop Playing"), this, SLOT(stop()));
    KeyDialog::insert(m_accel, "Back",        i18n("Back"),         this, SLOT(back()));
    KeyDialog::insert(m_accel, "Forward",     i18n("Forward"),      this, SLOT(forward()));
    KeyDialog::insert(m_accel, "SeekBack",    i18n("Seek Back"),    this, SLOT(seekBack()));
    KeyDialog::insert(m_accel, "SeekForward", i18n("Seek Forward"), this, SLOT(seekForward()));
    KeyDialog::insert(m_accel, "VolumeUp",    i18n("Volume Up"),    this, SLOT(volumeUp()));
    KeyDialog::insert(m_accel, "VolumeDown",  i18n("Volume Down"),  this, SLOT(volumeDown()));
    KeyDialog::insert(m_accel, "Mute",        i18n("Mute"),         this, SLOT(volumeMute()));
490

491 492 493 494 495
    m_accel->setConfigGroup("Shortcuts");
    m_accel->readSettings();
    m_accel->updateConnections();
}

496 497 498 499
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
500

501
    for(int i = 0; i < args->count(); i++)
502
        files.append(args->arg(i));
503

504
    m_splitter->open(files);
505 506
}

507 508 509
void JuK::keyPressEvent(QKeyEvent *e)
{
    if (e->key() >= Qt::Key_Back && e->key() <= Qt::Key_MediaLast)
510
        e->accept();
511 512 513
    KMainWindow::keyPressEvent(e);
}

514 515 516 517 518 519 520 521 522
/**
 * These are settings that need to be know before setting up the GUI.
 */

void JuK::readSettings()
{
    KConfig *config = KGlobal::config();
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
523
        m_showSplash = config->readBoolEntry("ShowSplashScreen", true);
524
        m_startDocked = config->readBoolEntry("StartDocked", false);
525 526 527
    }
}

528 529
void JuK::readConfig()
{
530
    // Automagically save and m_restore many window settings.
531 532
    setAutoSaveSettings();

533
    KConfig *config = KGlobal::config();
534
    { // player settings
535
        KConfigGroupSaver saver(config, "Player");
536 537
        if(m_sliderAction->volumeSlider()) {
	    int maxVolume = m_sliderAction->volumeSlider()->maxValue();
Scott Wheeler's avatar
Scott Wheeler committed
538
	    int volume = config->readNumEntry("Volume", maxVolume);
539
            m_sliderAction->volumeSlider()->setValue(volume);
540
        }
541 542
        bool randomPlay = config->readBoolEntry("RandomPlay", false);
        m_randomPlayAction->setChecked(randomPlay);
543

544 545
        bool loopPlaylist = config->readBoolEntry("LoopPlaylist", false);
        m_loopPlaylistAction->setChecked(loopPlaylist);
546
    }
547
    { // view settings
548
        KConfigGroupSaver saver(config, "View");
549 550 551 552 553

        bool showSearch = config->readBoolEntry("ShowSearch", true);
        m_showSearchAction->setChecked(showSearch);
        m_splitter->slotSetSearchVisible(showSearch);

554 555 556
        bool showEditor = config->readBoolEntry("ShowEditor", false);
        m_showEditorAction->setChecked(showEditor);
        m_splitter->slotSetEditorVisible(showEditor);
557

558 559 560
	// The history list will actually be created by the playlist restoration
	// code, but we want to remember the checkbox's setting and hope that
	// it's in synch with the code that does the real work.
561

562 563
	bool showHistory = config->readBoolEntry("ShowHistory", false);
	m_showHistoryAction->setChecked(showHistory);
564
    }
565 566
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
567

568 569
        bool dockInSystemTray = config->readBoolEntry("DockInSystemTray", true);
        m_toggleSystemTrayAction->setChecked(dockInSystemTray);
570

571 572
        bool dockOnClose = config->readBoolEntry("DockOnClose", true);
        m_toggleDockOnCloseAction->setChecked(dockOnClose);
573

574 575
        bool showPopups = config->readBoolEntry("TrackPopup", false);
        m_togglePopupsAction->setChecked(showPopups);
576

577 578
        if(m_outputSelectAction)
            m_outputSelectAction->setCurrentItem(config->readNumEntry("MediaSystem", 0));
579
    }
580

581
    m_toggleSplashAction->setChecked(m_showSplash);
582 583 584 585
}

void JuK::saveConfig()
{
586
    KConfig *config = KGlobal::config();
587
    { // m_player settings
588
        KConfigGroupSaver saver(config, "Player");
589 590
        if(m_sliderAction && m_sliderAction->volumeSlider())
            config->writeEntry("Volume", m_sliderAction->volumeSlider()->value());
591 592 593 594
        if(m_randomPlayAction)
            config->writeEntry("RandomPlay", m_randomPlayAction->isChecked());
        if(m_loopPlaylistAction)
            config->writeEntry("LoopPlaylist", m_loopPlaylistAction->isChecked());
595
    }
596 597
    { // view settings
        KConfigGroupSaver saver(config, "View");
598

599
        config->writeEntry("ShowEditor", m_showEditorAction->isChecked());
600
        config->writeEntry("ShowSearch", m_showSearchAction->isChecked());
601
	config->writeEntry("ShowHistory", m_showHistoryAction->isChecked());
602
    }
603 604
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
605
        config->writeEntry("ShowSplashScreen", m_toggleSplashAction->isChecked());
606
        config->writeEntry("StartDocked", m_startDocked);
607 608 609 610 611
        config->writeEntry("DockInSystemTray", m_toggleSystemTrayAction->isChecked());
        config->writeEntry("DockOnClose", m_toggleDockOnCloseAction->isChecked());
        config->writeEntry("TrackPopup", m_togglePopupsAction->isChecked());
        if(m_outputSelectAction)
            config->writeEntry("MediaSystem", m_outputSelectAction->currentItem());
612
    }
613
    config->sync();
614 615
}

616
bool JuK::queryExit()
617
{
618 619
    m_startDocked = !isVisible();

620 621 622 623
    hide();
    delete m_systemTray;
    m_systemTray = 0;

624
    stop();
625

626
    Cache::instance()->save();
627
    saveConfig();
628
    delete m_splitter;
629
    return true;
630 631
}

632 633
bool JuK::queryClose()
{
634 635 636 637 638
    if(!m_shuttingDown &&
       !kapp->sessionSaving() &&
       m_systemTray &&
       m_toggleDockOnCloseAction->isChecked())
    {
639
	KMessageBox::information(this,
Scott Wheeler's avatar
Scott Wheeler committed
640 641 642
	    i18n("<qt>Closing the main window will keep JuK running in the system tray. "
		 "Use Quit from the File menu to quit the application.</qt>"),
	    i18n("Docking in System Tray"), "hideOnCloseInfo");
643 644 645 646 647 648 649
	hide();
	return false;
    }
    else
	return true;
}

650 651 652
QString JuK::playingString() const
{
    QString s;
653 654
    if(!m_player->playing() && !m_player->paused())
	return i18n("No song playing");
655

656 657 658 659
    if(!m_splitter->playingArtist().isEmpty())
	s = m_splitter->playingArtist().simplifyWhiteSpace() + " - ";

    s += m_splitter->playingTrack().simplifyWhiteSpace();
660 661 662 663

    return s;
}

664 665
void JuK::updatePlaylistInfo()
{
Scott Wheeler's avatar
Scott Wheeler committed
666
    m_statusLabel->setPlaylistInfo(m_splitter->visiblePlaylistName(),
667 668
				   m_splitter->selectedPlaylistCount(),
				   m_splitter->selectedPlaylistTotalTime());
669 670 671 672
}

void JuK::play(const QString &file)
{
673
    m_player->play(file);
674
    emit signalNewSong(playingString());
675 676
}

Scott Wheeler's avatar
Scott Wheeler committed
677 678 679
KAction *JuK::createSplitterAction(const QString &text, const char *slot,
				   const char *name, const QString &pix,
				   const KShortcut &shortcut)
680 681 682 683 684 685 686 687 688
{
    KAction *action;

    if(pix.isNull())
	action = new KAction(text, shortcut, actionCollection(), name);
    else
	action = new KAction(text, pix, shortcut, actionCollection(), name);

    m_splitterConnections.append(SplitterConnection(action, slot));
689

690 691 692
    return action;
}

693 694 695 696
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

697
void JuK::slotPlaylistChanged()
698
{
699 700 701 702
    if(m_splitter->collectionListSelected() ||
       !m_splitter->hasListSelected() ||
       m_splitter->readOnlyListSelected())
    {
703 704 705
        actionCollection()->action("file_save")->setEnabled(false);
        actionCollection()->action("file_save_as")->setEnabled(false);
        actionCollection()->action("renamePlaylist")->setEnabled(false);
706
        actionCollection()->action("reloadPlaylist")->setEnabled(false);
707
        actionCollection()->action("deleteItemPlaylist")->setEnabled(false);
708 709
    }
    else {
710 711 712 713
        actionCollection()->action("file_save")->setEnabled(true);
        actionCollection()->action("file_save_as")->setEnabled(true);
        actionCollection()->action("renamePlaylist")->setEnabled(true);
        actionCollection()->action("deleteItemPlaylist")->setEnabled(true);
714 715 716 717 718

	if(m_splitter->fileBasedListSelected() || m_splitter->dynamicListSelected())
	    actionCollection()->action("reloadPlaylist")->setEnabled(true);
	else
	    actionCollection()->action("reloadPlaylist")->setEnabled(false);
719
    }
720 721 722 723
    if(m_splitter->hasListSelected())
        actionCollection()->action("duplicatePlaylist")->setEnabled(true);
    else
        actionCollection()->action("duplicatePlaylist")->setEnabled(false);
724

725 726 727
    updatePlaylistInfo();
}

728 729 730 731
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

732
void JuK::slotToggleSystemTray(bool enabled)
733
{
734
    if(enabled && !m_systemTray)
735
	setupSystemTray();
736 737 738
    else if(!enabled && m_systemTray) {
	delete m_systemTray;
	m_systemTray = 0;
739
	m_toggleDockOnCloseAction->setEnabled(false);
740
	m_togglePopupsAction->setEnabled(false);
741 742 743
    }
}

744 745 746 747 748
void JuK::slotEditKeys()
{
    KeyDialog::configure(m_accel, actionCollection(), this);
}

749
////////////////////////////////////////////////////////////////////////////////
750
// additional player slots
751 752
////////////////////////////////////////////////////////////////////////////////

753 754 755 756 757 758 759
void JuK::slotPlaySelectedFile()
{
    QString file = m_splitter->playSelectedFile();
    if(!file.isNull())
	play(m_splitter->playSelectedFile());
}

760
void JuK::slotSetVolume(int volume)
761
{
762
    m_player->slotSetVolume(volume);
763
}
764

765 766 767 768 769 770
void JuK::slotConfigureTagGuesser()
{
    TagGuesserConfigDlg dlg(this);
    dlg.exec();
}

771 772 773 774 775 776
void JuK::slotConfigureFileRenamer()
{
    FileRenamerConfigDlg dlg(this);
    dlg.exec();
}

777
#include "juk.moc"