juk.cpp 24.3 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
	int v = int(volume / 100 * m_sliderAction->volumeSlider()->maxValue());
85
	m_player->slotSetVolume(v);
86
	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()
{
145
    m_player->forward();
146 147 148 149
}

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

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

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

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

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

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

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

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

212 213 214 215
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

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

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

227
    // create status bar
228

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

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

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

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

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

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

248
    m_splitter->setFocus();
249

250
    resize(750, 500);
251 252
}

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

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

262 263 264 265 266 267
    // 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
268 269 270
    newMenu->insert(createSplitterAction(i18n("Empty Playlist..."),
					 SLOT(slotCreatePlaylist()), 0, 0, 0));

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

    createSplitterAction(
	i18n("Open..."), SLOT(slotOpen()), "file_open", "fileopen", "CTRL+o");
    createSplitterAction(
277
	i18n("Open &Folder..."), SLOT(slotOpenDirectory()), "openDirectory", "fileopen");
Scott Wheeler's avatar
Scott Wheeler committed
278 279 280 281 282 283 284 285 286 287 288 289
    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");
290

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

293
    //////////////////////////////////////////////////
294
    // edit menu
295 296
    //////////////////////////////////////////////////

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

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

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

307
    //////////////////////////////////////////////////
308
    // view menu
309
    //////////////////////////////////////////////////
310

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

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

320
    //////////////////////////////////////////////////
321
    // play menu
322 323
    //////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
324 325 326 327 328 329 330 331 332 333 334 335
    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");

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

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

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

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

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

351
    //////////////////////////////////////////////////
352
    // tagger menu
353 354
    //////////////////////////////////////////////////

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

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

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

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

375
    //////////////////////////////////////////////////
376
    // settings menu
377 378
    //////////////////////////////////////////////////

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

382
    setStandardToolBarMenuEnabled(true);
383

Stephan Binner's avatar
Stephan Binner committed
384
    m_toggleSplashAction = new KToggleAction(i18n("Show Splash Screen on Startup"),
Scott Wheeler's avatar
Scott Wheeler committed
385 386 387 388 389 390 391
					     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");
392

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

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

Scott Wheeler's avatar
Scott Wheeler committed
399

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

402
    if(m_outputSelectAction)
Frerich Raabe's avatar
Frerich Raabe committed
403
        m_outputSelectAction->setCurrentItem(0);
404

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

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

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

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

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

    createGUI();
421 422

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

426 427 428 429 430 431
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
432 433 434 435
    connect(m_showSearchAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetSearchVisible(bool)));
    connect(m_showEditorAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetEditorVisible(bool)));
436 437
    connect(m_showHistoryAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetHistoryVisible(bool)));
438
    connect(this, SIGNAL(dockWindowPositionChanged(QDockWindow *)),
Scott Wheeler's avatar
Scott Wheeler committed
439 440 441
	    m_sliderAction, SLOT(slotUpdateOrientation(QDockWindow *)));
    connect(m_splitter, SIGNAL(signalPlaylistChanged()),
	    this, SLOT(slotPlaylistChanged()));
442 443
}

444
void JuK::setupSystemTray()
445
{
446
    if(m_toggleSystemTrayAction && m_toggleSystemTrayAction->isChecked()) {
Frerich Raabe's avatar
Frerich Raabe committed
447 448
        m_systemTray = new SystemTray(this, "systemTray");
        m_systemTray->show();
449

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

Frerich Raabe's avatar
Frerich Raabe committed
453
        m_toggleDockOnCloseAction->setEnabled(true);
454
	m_togglePopupsAction->setEnabled(true);
455

Frerich Raabe's avatar
Frerich Raabe committed
456
        connect(m_systemTray, SIGNAL(quitSelected()), this, SLOT(slotQuit()));
457
    }
458
    else {
Frerich Raabe's avatar
Frerich Raabe committed
459 460
        m_systemTray = 0;
        m_toggleDockOnCloseAction->setEnabled(false);
461
	m_togglePopupsAction->setEnabled(false);
462
    }
463 464
}

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

469
    if(m_sliderAction) {
470 471
    }

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

476

477 478 479
void JuK::setupGlobalAccels()
{
    m_accel = new KGlobalAccel(this);
480 481 482 483 484 485 486 487 488
    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()));
489

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

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

500
    for(int i = 0; i < args->count(); i++)
Frerich Raabe's avatar
Frerich Raabe committed
501
        files.append(args->arg(i));
502

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

506 507 508
void JuK::keyPressEvent(QKeyEvent *e)
{
    if (e->key() >= Qt::Key_Back && e->key() <= Qt::Key_MediaLast)
Frerich Raabe's avatar
Frerich Raabe committed
509
        e->accept();
510 511 512
    KMainWindow::keyPressEvent(e);
}

513 514 515 516 517 518 519 520 521
/**
 * 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");
Frerich Raabe's avatar
Frerich Raabe committed
522
        m_showSplash = config->readBoolEntry("ShowSplashScreen", true);
523
        m_startDocked = config->readBoolEntry("StartDocked", false);
524 525 526
    }
}

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

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

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

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

Frerich Raabe's avatar
Frerich Raabe committed
553 554 555
        bool showEditor = config->readBoolEntry("ShowEditor", false);
        m_showEditorAction->setChecked(showEditor);
        m_splitter->slotSetEditorVisible(showEditor);
556

557 558 559
	// 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.
560

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

Frerich Raabe's avatar
Frerich Raabe committed
567 568
        bool dockInSystemTray = config->readBoolEntry("DockInSystemTray", true);
        m_toggleSystemTrayAction->setChecked(dockInSystemTray);
569

Frerich Raabe's avatar
Frerich Raabe committed
570 571
        bool dockOnClose = config->readBoolEntry("DockOnClose", true);
        m_toggleDockOnCloseAction->setChecked(dockOnClose);
572

Frerich Raabe's avatar
Frerich Raabe committed
573 574
        bool showPopups = config->readBoolEntry("TrackPopup", false);
        m_togglePopupsAction->setChecked(showPopups);
575

Frerich Raabe's avatar
Frerich Raabe committed
576 577
        if(m_outputSelectAction)
            m_outputSelectAction->setCurrentItem(config->readNumEntry("MediaSystem", 0));
578
    }
579

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

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

Frerich Raabe's avatar
Frerich Raabe committed
598
        config->writeEntry("ShowEditor", m_showEditorAction->isChecked());
599
        config->writeEntry("ShowSearch", m_showSearchAction->isChecked());
600
	config->writeEntry("ShowHistory", m_showHistoryAction->isChecked());
601
    }
602 603
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
Frerich Raabe's avatar
Frerich Raabe committed
604
        config->writeEntry("ShowSplashScreen", m_toggleSplashAction->isChecked());
605
        config->writeEntry("StartDocked", m_startDocked);
Frerich Raabe's avatar
Frerich Raabe committed
606 607 608 609 610
        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());
611
    }
612
    config->sync();
613 614
}

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

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

623
    stop();
624

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

631 632
bool JuK::queryClose()
{
633 634 635 636 637
    if(!m_shuttingDown &&
       !kapp->sessionSaving() &&
       m_systemTray &&
       m_toggleDockOnCloseAction->isChecked())
    {
638
	KMessageBox::information(this,
Scott Wheeler's avatar
Scott Wheeler committed
639 640 641
	    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");
642 643 644 645 646 647 648
	hide();
	return false;
    }
    else
	return true;
}

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

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

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

    return s;
}

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

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

Scott Wheeler's avatar
Scott Wheeler committed
676 677 678
KAction *JuK::createSplitterAction(const QString &text, const char *slot,
				   const char *name, const QString &pix,
				   const KShortcut &shortcut)
679 680 681 682 683 684 685 686 687
{
    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));
688

689 690 691
    return action;
}

692 693 694 695
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

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

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

724 725 726
    updatePlaylistInfo();
}

727 728 729 730
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

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

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

748
////////////////////////////////////////////////////////////////////////////////
749
// additional player slots
750 751
////////////////////////////////////////////////////////////////////////////////

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

759 760
void JuK::slotConfigureTagGuesser()
{
761
    TagGuesserConfigDlg(this).exec();
762 763
}

764 765
void JuK::slotConfigureFileRenamer()
{
766
    FileRenamerConfigDlg(this).exec();
767 768
}

769
#include "juk.moc"