juk.cpp 24.2 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
#include "actioncollection.h"
38

39 40
using namespace ActionCollection;

41 42 43 44
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

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

52
    readSettings();
53

54
    if(m_showSplash && !m_startDocked) {
55 56 57 58
	SplashScreen::instance()->show();
	kapp->processEvents();
    }

59
    setupActions();
60 61
    setupLayout();
    setupSplitterConnections();
62
    slotPlaylistChanged();
63
    readConfig();
64
    setupPlayer();
65
    setupSystemTray();
66
    setupGlobalAccels();
67
    processArgs();
68

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

JuK::~JuK()
{
75

76 77
}

78 79 80 81 82
KActionCollection *JuK::actionCollection() const
{
    return ActionCollection::actions();
}

83 84 85 86 87 88
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

98 99 100 101 102
void JuK::setTime(int time)
{
    m_player->seek(time);
}

103 104
void JuK::startPlayingPlaylist()
{
105 106
    m_player->stop();
    action("play")->activate();
107 108
}

109
void JuK::slotGuessTagInfoFromFile()
110
{
111 112 113 114 115 116
    m_splitter->slotGuessTagInfo(TagGuesser::FileName);
}

void JuK::slotGuessTagInfoFromInternet()
{
    m_splitter->slotGuessTagInfo(TagGuesser::MusicBrainz);
117 118
}

119 120
void JuK::play()
{
121
    m_player->play();
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
}

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
150 151
    m_splitter->populatePlayHistoryMenu(m_backAction->popupMenu(),
					m_randomPlayAction->isChecked());
152 153 154 155
}

void JuK::forward()
{
156
    m_player->forward();
157 158 159 160
}

void JuK::seekBack()
{
161 162 163
    int position = m_sliderAction->trackPositionSlider()->value();
    position = QMAX(m_sliderAction->trackPositionSlider()->minValue(), position - 10);
    emit m_sliderAction->trackPositionSlider()->setValue(position);
164 165 166 167
}

void JuK::seekForward()
{
168 169 170
    int position = m_sliderAction->trackPositionSlider()->value();
    position = QMIN(m_sliderAction->trackPositionSlider()->maxValue(), position + 10);
    emit m_sliderAction->trackPositionSlider()->setValue(position);
171 172 173 174 175
}

void JuK::playPause()
{
    if(m_player->playing())
176
	action("pause")->activate();
177
    else
178
	action("play")->activate();
179 180 181 182
}

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

void JuK::volumeDown()
{
193 194 195
    if(m_sliderAction && m_sliderAction->volumeSlider()) {
	int volume = m_sliderAction->volumeSlider()->value() -
	    m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
196
	m_player->slotSetVolume(volume);
197
	m_sliderAction->volumeSlider()->setValue(volume);
198 199 200 201 202
    }
}

void JuK::volumeMute()
{
203
    if(m_sliderAction && m_sliderAction->volumeSlider()) {
204
	if(m_muted)
205
	    m_player->slotSetVolume(m_sliderAction->volumeSlider()->value());
206
	else
207
	    m_player->slotSetVolume(0);
208 209 210 211 212 213 214 215 216
	    m_muted = !m_muted;
    }
}

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

217 218 219 220 221 222
void JuK::openFile(const QStringList &files)
{
    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
	openFile(*it);
}

223 224 225 226
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

227 228
void JuK::setupLayout()
{
229
    m_splitter = new PlaylistSplitter(this, "playlistSplitter");
230
    setCentralWidget(m_splitter);
231 232

    // playlist item activation connection
Scott Wheeler's avatar
Scott Wheeler committed
233 234 235 236
    connect(m_splitter, SIGNAL(signalActivated()),
	    this, SLOT(slotPlaySelectedFile()));
    connect(m_splitter, SIGNAL(signalListBoxDoubleClicked()),
	    this, SLOT(startPlayingPlaylist()));
237

238
    // create status bar
239

240
    m_statusLabel = new StatusLabel(statusBar());
241

242
    statusBar()->addWidget(m_statusLabel, 1);
243

Scott Wheeler's avatar
Scott Wheeler committed
244 245
    connect(m_splitter, SIGNAL(signalSelectedPlaylistCountChanged(int)),
	    m_statusLabel, SLOT(setPlaylistCount(int)));
246 247
    connect(m_splitter, SIGNAL(signalSelectedPlaylistTimeChanged(int)),
	    m_statusLabel, SLOT(setPlaylistTime(int)));
Scott Wheeler's avatar
Scott Wheeler committed
248 249
    connect(m_statusLabel, SIGNAL(jumpButtonClicked()),
	    m_splitter, SLOT(slotSelectPlaying()));
250

251 252
    PlayerManager::instance()->setStatusLabel(m_statusLabel);

253 254
    // Needs to be here because m_splitter is not called before setupActions
    // (because PlaylistSplitter itself accesses the actionCollection)
255

256 257 258
    new KAction(i18n("&Rename File"), 0, "CTRL+r", m_splitter, SLOT(slotRenameFile()),
                actionCollection(), "renameFile"); // 4

259
    m_splitter->setFocus();
260

261
    resize(750, 500);
262 263
}

264 265
void JuK::setupActions()
{
266
    //////////////////////////////////////////////////
267
    // file menu
268
    //////////////////////////////////////////////////
269

Scott Wheeler's avatar
Scott Wheeler committed
270 271 272
    KActionMenu *newMenu = new KActionMenu(i18n("&New"), "filenew",
					   actionCollection(), "file_new");

273 274 275 276 277 278
    // 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
279 280 281
    newMenu->insert(createSplitterAction(i18n("Empty Playlist..."),
					 SLOT(slotCreatePlaylist()), 0, 0, 0));

282
    newMenu->insert(createSplitterAction(i18n("Playlist From Folder..."),
Scott Wheeler's avatar
Scott Wheeler committed
283 284 285 286 287
					 SLOT(slotCreatePlaylistFromDir()), 0, 0, 0));

    createSplitterAction(
	i18n("Open..."), SLOT(slotOpen()), "file_open", "fileopen", "CTRL+o");
    createSplitterAction(
288
	i18n("Open &Folder..."), SLOT(slotOpenDirectory()), "openDirectory", "fileopen");
Scott Wheeler's avatar
Scott Wheeler committed
289 290 291 292 293 294 295 296 297 298 299 300
    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");
301

302
    KStdAction::quit(this, SLOT(slotQuit()), actionCollection());
303

304
    //////////////////////////////////////////////////
305
    // edit menu
306 307
    //////////////////////////////////////////////////

308 309 310 311
    KStdAction::cut(kapp,   SLOT(cut()),   actionCollection());
    KStdAction::copy(kapp,  SLOT(copy()),  actionCollection());
    KStdAction::paste(kapp, SLOT(paste()), actionCollection());
    KStdAction::clear(kapp, SLOT(clear()), actionCollection());
312

313
    KStdAction::selectAll(kapp, SLOT(selectAll()), actionCollection());
314

315
    createSplitterAction(
316
	i18n("Advanced Search"), SLOT(slotAdvancedSearch()), "advancedSearch", "find", "s");
317

318
    //////////////////////////////////////////////////
319
    // view menu
320
    //////////////////////////////////////////////////
321

Scott Wheeler's avatar
Scott Wheeler committed
322 323 324 325
    m_showSearchAction = new KToggleAction(i18n("Show &Search Bar"), "filefind",
					   0, actionCollection(), "showSearch");
    m_showEditorAction = new KToggleAction(i18n("Show &Tag Editor"), "edit",
					   0, actionCollection(), "showEditor");
326
    m_showHistoryAction = new KToggleAction(i18n("Show &History"), "history",
327
					   0, actionCollection(), "showHistory");
328

329
    createSplitterAction(i18n("Refresh Items"), SLOT(slotRefresh()), "refresh", "reload");
330

331
    //////////////////////////////////////////////////
332
    // play menu
333 334
    //////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
335 336 337 338 339 340 341 342 343 344 345 346
    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");

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

Scott Wheeler's avatar
Scott Wheeler committed
350 351
    connect(m_backAction->popupMenu(), SIGNAL(aboutToShow()),
	    this, SLOT(slotPopulateBackMenu()));
352

Scott Wheeler's avatar
Scott Wheeler committed
353 354
    connect(m_backAction->popupMenu(), SIGNAL(activated(int)),
	    this, SLOT(back(int)));
355

356
    new KAction(i18n("&Next Track"), "player_end", 0, this, SLOT(forward()),
Scott Wheeler's avatar
Scott Wheeler committed
357
		actionCollection(), "forward");
358

Scott Wheeler's avatar
Scott Wheeler committed
359 360
    m_loopPlaylistAction = new KToggleAction(i18n("&Loop Playlist"), 0, 0,
					     actionCollection(), "loopPlaylist");
361

362
    //////////////////////////////////////////////////
363
    // tagger menu
364 365
    //////////////////////////////////////////////////

366
    createSplitterAction(i18n("&Save"), SLOT(slotSaveTag()),
Scott Wheeler's avatar
Scott Wheeler committed
367 368 369
			 "saveItem",   "filesave", "CTRL+t");
    createSplitterAction(i18n("&Delete"), SLOT(slotDeleteSelectedItems()),
			 "removeItem", "editdelete");
370

371
    m_guessMenu = new KActionMenu(i18n("&Guess Tag Information"),
Scott Wheeler's avatar
Scott Wheeler committed
372 373
					     QString::null, actionCollection(),
					     "guessTag");
374

Scott Wheeler's avatar
Scott Wheeler committed
375
    KAction *a;
376

377 378
    a = new KAction(i18n("From &Filename"), 0, "CTRL+f", this, SLOT(slotGuessTagInfoFromFile()),
                actionCollection(), "guessTagFile");
379
    m_guessMenu->insert(a);
380
#if HAVE_MUSICBRAINZ
381 382
    a = new KAction(i18n("From &Internet"), 0, "CTRL+i", this, SLOT(slotGuessTagInfoFromInternet()),
                actionCollection(), "guessTagInternet");
383
    m_guessMenu->insert(a);
384
#endif
Scott Wheeler's avatar
Scott Wheeler committed
385

386
    //////////////////////////////////////////////////
387
    // settings menu
388 389
    //////////////////////////////////////////////////

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

393
    setStandardToolBarMenuEnabled(true);
394

Stephan Binner's avatar
Stephan Binner committed
395
    m_toggleSplashAction = new KToggleAction(i18n("Show Splash Screen on Startup"),
Scott Wheeler's avatar
Scott Wheeler committed
396 397 398 399 400 401 402
					     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");
403

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

Scott Wheeler's avatar
Scott Wheeler committed
407 408
    connect(m_toggleSystemTrayAction, SIGNAL(toggled(bool)),
	    this, SLOT(slotToggleSystemTray(bool)));
409

Scott Wheeler's avatar
Scott Wheeler committed
410

411
    m_outputSelectAction = Player::playerSelectAction(actionCollection());
412

413
    if(m_outputSelectAction)
414
        m_outputSelectAction->setCurrentItem(0);
415

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

419 420 421
    new KAction(i18n("&File Renamer..."), 0, 0, this, SLOT(slotConfigureFileRenamer()),
		actionCollection(), "fileRenamerConfig");

422 423
    KStdAction::keyBindings(this, SLOT(slotEditKeys()), actionCollection());

424
    //////////////////////////////////////////////////
425
    // just in the toolbar
426 427
    //////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
428 429
    m_sliderAction = new SliderAction(i18n("Track Position"), actionCollection(),
				      "trackPositionAction");
430 431

    createGUI();
432 433

    // set the slider to the proper orientation and make it stay that way
434
    m_sliderAction->slotUpdateOrientation();
435 436
}

437 438 439 440 441 442
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
443 444 445 446
    connect(m_showSearchAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetSearchVisible(bool)));
    connect(m_showEditorAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetEditorVisible(bool)));
447 448
    connect(m_showHistoryAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetHistoryVisible(bool)));
449
    connect(this, SIGNAL(dockWindowPositionChanged(QDockWindow *)),
Scott Wheeler's avatar
Scott Wheeler committed
450 451 452
	    m_sliderAction, SLOT(slotUpdateOrientation(QDockWindow *)));
    connect(m_splitter, SIGNAL(signalPlaylistChanged()),
	    this, SLOT(slotPlaylistChanged()));
453 454
}

455
void JuK::setupSystemTray()
456
{
457
    if(m_toggleSystemTrayAction && m_toggleSystemTrayAction->isChecked()) {
458 459
        m_systemTray = new SystemTray(this, "systemTray");
        m_systemTray->show();
460

461
        connect(this, SIGNAL(signalNewSong(const QString&)),
Scott Wheeler's avatar
Scott Wheeler committed
462
		m_systemTray, SLOT(slotNewSong(const QString&)));
463

464
        m_toggleDockOnCloseAction->setEnabled(true);
465
	m_togglePopupsAction->setEnabled(true);
466

467
        connect(m_systemTray, SIGNAL(quitSelected()), this, SLOT(slotQuit()));
468
    }
469
    else {
470 471
        m_systemTray = 0;
        m_toggleDockOnCloseAction->setEnabled(false);
472
	m_togglePopupsAction->setEnabled(false);
473
    }
474 475
}

476 477
void JuK::setupPlayer()
{
Scott Wheeler's avatar
Scott Wheeler committed
478
    m_muted = false;
479

480
    if(m_sliderAction) {
481 482
    }

483 484
    m_player = PlayerManager::instance();
    PlayerManager::instance()->setPlaylistInterface(m_splitter);
485 486
}

487

488 489 490
void JuK::setupGlobalAccels()
{
    m_accel = new KGlobalAccel(this);
491 492 493 494 495 496 497 498 499
    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()));
500

501 502 503 504 505
    m_accel->setConfigGroup("Shortcuts");
    m_accel->readSettings();
    m_accel->updateConnections();
}

506 507 508 509
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
510

511
    for(int i = 0; i < args->count(); i++)
512
        files.append(args->arg(i));
513

514
    m_splitter->open(files);
515 516
}

517 518 519
void JuK::keyPressEvent(QKeyEvent *e)
{
    if (e->key() >= Qt::Key_Back && e->key() <= Qt::Key_MediaLast)
520
        e->accept();
521 522 523
    KMainWindow::keyPressEvent(e);
}

524 525 526 527 528 529 530 531 532
/**
 * 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");
533
        m_showSplash = config->readBoolEntry("ShowSplashScreen", true);
534
        m_startDocked = config->readBoolEntry("StartDocked", false);
535 536 537
    }
}

538 539
void JuK::readConfig()
{
540
    // Automagically save and m_restore many window settings.
541 542
    setAutoSaveSettings();

543
    KConfig *config = KGlobal::config();
544
    { // player settings
545
        KConfigGroupSaver saver(config, "Player");
546 547
        if(m_sliderAction->volumeSlider()) {
	    int maxVolume = m_sliderAction->volumeSlider()->maxValue();
Scott Wheeler's avatar
Scott Wheeler committed
548
	    int volume = config->readNumEntry("Volume", maxVolume);
549
            m_sliderAction->volumeSlider()->setValue(volume);
550
        }
551 552
        bool randomPlay = config->readBoolEntry("RandomPlay", false);
        m_randomPlayAction->setChecked(randomPlay);
553

554 555
        bool loopPlaylist = config->readBoolEntry("LoopPlaylist", false);
        m_loopPlaylistAction->setChecked(loopPlaylist);
556
    }
557
    { // view settings
558
        KConfigGroupSaver saver(config, "View");
559 560 561 562 563

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

564 565 566
        bool showEditor = config->readBoolEntry("ShowEditor", false);
        m_showEditorAction->setChecked(showEditor);
        m_splitter->slotSetEditorVisible(showEditor);
567

568 569 570
	// 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.
571

572 573
	bool showHistory = config->readBoolEntry("ShowHistory", false);
	m_showHistoryAction->setChecked(showHistory);
574
    }
575 576
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
577

578 579
        bool dockInSystemTray = config->readBoolEntry("DockInSystemTray", true);
        m_toggleSystemTrayAction->setChecked(dockInSystemTray);
580

581 582
        bool dockOnClose = config->readBoolEntry("DockOnClose", true);
        m_toggleDockOnCloseAction->setChecked(dockOnClose);
583

584 585
        bool showPopups = config->readBoolEntry("TrackPopup", false);
        m_togglePopupsAction->setChecked(showPopups);
586

587 588
        if(m_outputSelectAction)
            m_outputSelectAction->setCurrentItem(config->readNumEntry("MediaSystem", 0));
589
    }
590

591
    m_toggleSplashAction->setChecked(m_showSplash);
592 593 594 595
}

void JuK::saveConfig()
{
596
    KConfig *config = KGlobal::config();
597
    { // player settings
598
        KConfigGroupSaver saver(config, "Player");
599 600
        if(m_sliderAction && m_sliderAction->volumeSlider())
            config->writeEntry("Volume", m_sliderAction->volumeSlider()->value());
601 602 603 604
        if(m_randomPlayAction)
            config->writeEntry("RandomPlay", m_randomPlayAction->isChecked());
        if(m_loopPlaylistAction)
            config->writeEntry("LoopPlaylist", m_loopPlaylistAction->isChecked());
605
    }
606 607
    { // view settings
        KConfigGroupSaver saver(config, "View");
608

609
        config->writeEntry("ShowEditor", m_showEditorAction->isChecked());
610
        config->writeEntry("ShowSearch", m_showSearchAction->isChecked());
611
	config->writeEntry("ShowHistory", m_showHistoryAction->isChecked());
612
    }
613 614
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
615
        config->writeEntry("ShowSplashScreen", m_toggleSplashAction->isChecked());
616
        config->writeEntry("StartDocked", m_startDocked);
617 618 619 620 621
        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());
622
    }
623
    config->sync();
624 625
}

626
bool JuK::queryExit()
627
{
628 629
    m_startDocked = !isVisible();

630 631 632 633
    hide();
    delete m_systemTray;
    m_systemTray = 0;

634
    stop();
635

636
    Cache::instance()->save();
637
    saveConfig();
638
    delete m_splitter;
639
    return true;
640 641
}

642 643
bool JuK::queryClose()
{
644 645 646 647 648
    if(!m_shuttingDown &&
       !kapp->sessionSaving() &&
       m_systemTray &&
       m_toggleDockOnCloseAction->isChecked())
    {
649
	KMessageBox::information(this,
Scott Wheeler's avatar
Scott Wheeler committed
650 651 652
	    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");
653 654 655 656 657 658 659
	hide();
	return false;
    }
    else
	return true;
}

660 661 662
QString JuK::playingString() const
{
    QString s;
663 664
    if(!m_player->playing() && !m_player->paused())
	return i18n("No song playing");
665

666 667 668 669
    if(!m_splitter->playingArtist().isEmpty())
	s = m_splitter->playingArtist().simplifyWhiteSpace() + " - ";

    s += m_splitter->playingTrack().simplifyWhiteSpace();
670 671 672 673

    return s;
}

674 675
void JuK::updatePlaylistInfo()
{
Scott Wheeler's avatar
Scott Wheeler committed
676
    m_statusLabel->setPlaylistInfo(m_splitter->visiblePlaylistName(),
677 678
				   m_splitter->selectedPlaylistCount(),
				   m_splitter->selectedPlaylistTotalTime());
679 680 681 682
}

void JuK::play(const QString &file)
{
683
    m_player->play(file);
684
    emit signalNewSong(playingString());
685 686
}

Scott Wheeler's avatar
Scott Wheeler committed
687 688 689
KAction *JuK::createSplitterAction(const QString &text, const char *slot,
				   const char *name, const QString &pix,
				   const KShortcut &shortcut)
690 691 692 693 694 695 696 697 698
{
    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));
699

700 701 702
    return action;
}

703 704 705 706
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

707
void JuK::slotPlaylistChanged()
708
{
709 710 711 712
    if(m_splitter->collectionListSelected() ||
       !m_splitter->hasListSelected() ||
       m_splitter->readOnlyListSelected())
    {
713 714 715 716 717
        action("file_save")->setEnabled(false);
        action("file_save_as")->setEnabled(false);
        action("renamePlaylist")->setEnabled(false);
        action("reloadPlaylist")->setEnabled(false);
        action("deleteItemPlaylist")->setEnabled(false);
718 719
    }
    else {
720 721 722 723
        action("file_save")->setEnabled(true);
        action("file_save_as")->setEnabled(true);
        action("renamePlaylist")->setEnabled(true);
        action("deleteItemPlaylist")->setEnabled(true);
724 725

	if(m_splitter->fileBasedListSelected() || m_splitter->dynamicListSelected())
726
	    action("reloadPlaylist")->setEnabled(true);
727
	else
728
	    action("reloadPlaylist")->setEnabled(false);
729
    }
730
    if(m_splitter->hasListSelected())
731
        action("duplicatePlaylist")->setEnabled(true);
732
    else
733
        action("duplicatePlaylist")->setEnabled(false);
734

735 736 737
    updatePlaylistInfo();
}

738 739 740 741 742
void JuK::slotQuit()
{
    kapp->quit();
}

743 744 745 746
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

747
void JuK::slotToggleSystemTray(bool enabled)
748
{
749
    if(enabled && !m_systemTray)
750
	setupSystemTray();
751 752 753
    else if(!enabled && m_systemTray) {
	delete m_systemTray;
	m_systemTray = 0;
754
	m_toggleDockOnCloseAction->setEnabled(false);
755
	m_togglePopupsAction->setEnabled(false);
756 757 758
    }
}

759 760 761 762 763
void JuK::slotEditKeys()
{
    KeyDialog::configure(m_accel, actionCollection(), this);
}

764
////////////////////////////////////////////////////////////////////////////////
765
// additional player slots
766 767
////////////////////////////////////////////////////////////////////////////////

768 769
void JuK::slotPlaySelectedFile()
{
770 771
    m_player->stop();
    action("play")->activate();
772 773
}

774 775
void JuK::slotConfigureTagGuesser()
{
776
    TagGuesserConfigDlg(this).exec();
777 778
}

779 780
void JuK::slotConfigureFileRenamer()
{
781
    FileRenamerConfigDlg(this).exec();
782 783
}

784
#include "juk.moc"