juk.cpp 23.4 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
#include <kcmdlineargs.h>
#include <kstatusbar.h>
#include <kdebug.h>
#include <kmessagebox.h>

#include <qslider.h>

25 26 27 28 29 30 31
#include "juk.h"
#include "slideraction.h"
#include "statuslabel.h"
#include "splashscreen.h"
#include "systemtray.h"
#include "keydialog.h"
#include "tagguesserconfigdlg.h"
32
#include "filerenamerconfigdlg.h"
33
#include "actioncollection.h"
34

35 36
using namespace ActionCollection;

37 38 39 40
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
41 42
JuK::JuK(QWidget *parent, const char *name) :
    KMainWindow(parent, name, WDestructiveClose),
43 44 45
    m_player(PlayerManager::instance()),
    m_shuttingDown(false),
    m_muted(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
    setupSystemTray();
62
    setupGlobalAccels();
63
    processArgs();
64

65 66
    m_player->setPlaylistInterface(m_splitter);

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

JuK::~JuK()
{
73

74 75
}

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

81 82 83 84 85 86
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

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

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

101
void JuK::slotGuessTagInfoFromFile()
102
{
103 104 105 106 107 108
    m_splitter->slotGuessTagInfo(TagGuesser::FileName);
}

void JuK::slotGuessTagInfoFromInternet()
{
    m_splitter->slotGuessTagInfo(TagGuesser::MusicBrainz);
109 110
}

111 112 113 114 115 116 117 118 119 120
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
121 122
    m_splitter->populatePlayHistoryMenu(m_backAction->popupMenu(),
					m_randomPlayAction->isChecked());
123 124 125 126
}

void JuK::forward()
{
127
    m_player->forward();
128 129 130 131
}

void JuK::seekBack()
{
132 133 134
    int position = m_sliderAction->trackPositionSlider()->value();
    position = QMAX(m_sliderAction->trackPositionSlider()->minValue(), position - 10);
    emit m_sliderAction->trackPositionSlider()->setValue(position);
135 136 137 138
}

void JuK::seekForward()
{
139 140 141
    int position = m_sliderAction->trackPositionSlider()->value();
    position = QMIN(m_sliderAction->trackPositionSlider()->maxValue(), position + 10);
    emit m_sliderAction->trackPositionSlider()->setValue(position);
142 143 144 145
}

void JuK::volumeUp()
{
146 147 148
    if(m_sliderAction && m_sliderAction->volumeSlider()) {
	int volume = m_sliderAction->volumeSlider()->value() +
	    m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
149
	m_player->slotSetVolume(volume);
150
	m_sliderAction->volumeSlider()->setValue(volume);
151 152 153 154 155
    }
}

void JuK::volumeDown()
{
156 157 158
    if(m_sliderAction && m_sliderAction->volumeSlider()) {
	int volume = m_sliderAction->volumeSlider()->value() -
	    m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
159
	m_player->slotSetVolume(volume);
160
	m_sliderAction->volumeSlider()->setValue(volume);
161 162 163 164 165
    }
}

void JuK::volumeMute()
{
166
    if(m_sliderAction && m_sliderAction->volumeSlider()) {
167
	if(m_muted)
168
	    m_player->slotSetVolume(m_sliderAction->volumeSlider()->value());
169
	else
170
	    m_player->slotSetVolume(0);
171 172 173 174 175 176 177 178 179
	    m_muted = !m_muted;
    }
}

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

180 181 182 183 184 185
void JuK::openFile(const QStringList &files)
{
    for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
	openFile(*it);
}

186 187 188 189
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

190 191
void JuK::setupLayout()
{
192
    m_splitter = new PlaylistSplitter(this, "playlistSplitter");
193
    setCentralWidget(m_splitter);
194 195

    // playlist item activation connection
Scott Wheeler's avatar
Scott Wheeler committed
196
    connect(m_splitter, SIGNAL(signalActivated()),
197
	    this, SLOT(slotPlayCurrent()));
Scott Wheeler's avatar
Scott Wheeler committed
198
    connect(m_splitter, SIGNAL(signalListBoxDoubleClicked()),
199
	    this, SLOT(slotPlayCurrent()));
200

201
    // create status bar
202

203
    m_statusLabel = new StatusLabel(statusBar());
204

205
    statusBar()->addWidget(m_statusLabel, 1);
206

Scott Wheeler's avatar
Scott Wheeler committed
207 208
    connect(m_splitter, SIGNAL(signalSelectedPlaylistCountChanged(int)),
	    m_statusLabel, SLOT(setPlaylistCount(int)));
209 210
    connect(m_splitter, SIGNAL(signalSelectedPlaylistTimeChanged(int)),
	    m_statusLabel, SLOT(setPlaylistTime(int)));
Scott Wheeler's avatar
Scott Wheeler committed
211 212
    connect(m_statusLabel, SIGNAL(jumpButtonClicked()),
	    m_splitter, SLOT(slotSelectPlaying()));
213

214 215
    PlayerManager::instance()->setStatusLabel(m_statusLabel);

216 217
    // Needs to be here because m_splitter is not called before setupActions
    // (because PlaylistSplitter itself accesses the actionCollection)
218

219 220 221
    new KAction(i18n("&Rename File"), 0, "CTRL+r", m_splitter, SLOT(slotRenameFile()),
                actionCollection(), "renameFile"); // 4

222
    m_splitter->setFocus();
223

224
    resize(750, 500);
225 226
}

227 228
void JuK::setupActions()
{
229
    //////////////////////////////////////////////////
230
    // file menu
231
    //////////////////////////////////////////////////
232

Scott Wheeler's avatar
Scott Wheeler committed
233 234 235
    KActionMenu *newMenu = new KActionMenu(i18n("&New"), "filenew",
					   actionCollection(), "file_new");

236 237 238 239 240 241
    // 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
242 243 244
    newMenu->insert(createSplitterAction(i18n("Empty Playlist..."),
					 SLOT(slotCreatePlaylist()), 0, 0, 0));

245
    newMenu->insert(createSplitterAction(i18n("Playlist From Folder..."),
Scott Wheeler's avatar
Scott Wheeler committed
246 247 248 249 250
					 SLOT(slotCreatePlaylistFromDir()), 0, 0, 0));

    createSplitterAction(
	i18n("Open..."), SLOT(slotOpen()), "file_open", "fileopen", "CTRL+o");
    createSplitterAction(
251
	i18n("Open &Folder..."), SLOT(slotOpenDirectory()), "openDirectory", "fileopen");
Scott Wheeler's avatar
Scott Wheeler committed
252 253 254 255 256 257 258 259 260 261 262 263
    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");
264

265
    KStdAction::quit(this, SLOT(slotQuit()), actionCollection());
266

267
    //////////////////////////////////////////////////
268
    // edit menu
269 270
    //////////////////////////////////////////////////

271 272 273 274
    KStdAction::cut(kapp,   SLOT(cut()),   actionCollection());
    KStdAction::copy(kapp,  SLOT(copy()),  actionCollection());
    KStdAction::paste(kapp, SLOT(paste()), actionCollection());
    KStdAction::clear(kapp, SLOT(clear()), actionCollection());
275

276
    KStdAction::selectAll(kapp, SLOT(selectAll()), actionCollection());
277

278
    createSplitterAction(
279
	i18n("Advanced Search"), SLOT(slotAdvancedSearch()), "advancedSearch", "find", "s");
280

281
    //////////////////////////////////////////////////
282
    // view menu
283
    //////////////////////////////////////////////////
284

Scott Wheeler's avatar
Scott Wheeler committed
285 286 287 288
    m_showSearchAction = new KToggleAction(i18n("Show &Search Bar"), "filefind",
					   0, actionCollection(), "showSearch");
    m_showEditorAction = new KToggleAction(i18n("Show &Tag Editor"), "edit",
					   0, actionCollection(), "showEditor");
289
    m_showHistoryAction = new KToggleAction(i18n("Show &History"), "history",
290
					   0, actionCollection(), "showHistory");
291

292
    createSplitterAction(i18n("Refresh Items"), SLOT(slotRefresh()), "refresh", "reload");
293

294
    //////////////////////////////////////////////////
295
    // play menu
296 297
    //////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
298 299 300
    m_randomPlayAction = new KToggleAction(i18n("&Random Play"), 0,
					   actionCollection(), "randomPlay");

301
    new KAction(i18n("&Play"),  "player_play",  0, m_player, SLOT(play()),
Scott Wheeler's avatar
Scott Wheeler committed
302 303
		actionCollection(), "play");

304
    new KAction(i18n("P&ause"), "player_pause", 0, m_player, SLOT(pause()),
Scott Wheeler's avatar
Scott Wheeler committed
305 306
		actionCollection(), "pause");

307
    new KAction(i18n("&Stop"),  "player_stop",  0, m_player, SLOT(stop()),
Scott Wheeler's avatar
Scott Wheeler committed
308 309
		actionCollection(), "stop");

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

Scott Wheeler's avatar
Scott Wheeler committed
313 314
    connect(m_backAction->popupMenu(), SIGNAL(aboutToShow()),
	    this, SLOT(slotPopulateBackMenu()));
315

Scott Wheeler's avatar
Scott Wheeler committed
316 317
    connect(m_backAction->popupMenu(), SIGNAL(activated(int)),
	    this, SLOT(back(int)));
318

319
    new KAction(i18n("&Next Track"), "player_end", 0, this, SLOT(forward()),
Scott Wheeler's avatar
Scott Wheeler committed
320
		actionCollection(), "forward");
321

Scott Wheeler's avatar
Scott Wheeler committed
322 323
    m_loopPlaylistAction = new KToggleAction(i18n("&Loop Playlist"), 0, 0,
					     actionCollection(), "loopPlaylist");
324

325
    //////////////////////////////////////////////////
326
    // tagger menu
327 328
    //////////////////////////////////////////////////

329
    createSplitterAction(i18n("&Save"), SLOT(slotSaveTag()),
Scott Wheeler's avatar
Scott Wheeler committed
330 331 332
			 "saveItem",   "filesave", "CTRL+t");
    createSplitterAction(i18n("&Delete"), SLOT(slotDeleteSelectedItems()),
			 "removeItem", "editdelete");
333

334
    m_guessMenu = new KActionMenu(i18n("&Guess Tag Information"),
Scott Wheeler's avatar
Scott Wheeler committed
335 336
					     QString::null, actionCollection(),
					     "guessTag");
337

Scott Wheeler's avatar
Scott Wheeler committed
338
    KAction *a;
339

340 341
    a = new KAction(i18n("From &Filename"), 0, "CTRL+f", this, SLOT(slotGuessTagInfoFromFile()),
                actionCollection(), "guessTagFile");
342
    m_guessMenu->insert(a);
343
#if HAVE_MUSICBRAINZ
344 345
    a = new KAction(i18n("From &Internet"), 0, "CTRL+i", this, SLOT(slotGuessTagInfoFromInternet()),
                actionCollection(), "guessTagInternet");
346
    m_guessMenu->insert(a);
347
#endif
Scott Wheeler's avatar
Scott Wheeler committed
348

349
    //////////////////////////////////////////////////
350
    // settings menu
351 352
    //////////////////////////////////////////////////

353
    setStandardToolBarMenuEnabled(true);
354

Stephan Binner's avatar
Stephan Binner committed
355
    m_toggleSplashAction = new KToggleAction(i18n("Show Splash Screen on Startup"),
Scott Wheeler's avatar
Scott Wheeler committed
356 357 358 359 360 361 362
					     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");
363

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

Scott Wheeler's avatar
Scott Wheeler committed
367 368
    connect(m_toggleSystemTrayAction, SIGNAL(toggled(bool)),
	    this, SLOT(slotToggleSystemTray(bool)));
369

Scott Wheeler's avatar
Scott Wheeler committed
370

371
    m_outputSelectAction = Player::playerSelectAction(actionCollection());
372

373
    if(m_outputSelectAction)
374
        m_outputSelectAction->setCurrentItem(0);
375

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

379 380 381
    new KAction(i18n("&File Renamer..."), 0, 0, this, SLOT(slotConfigureFileRenamer()),
		actionCollection(), "fileRenamerConfig");

382 383
    KStdAction::keyBindings(this, SLOT(slotEditKeys()), actionCollection());

384
    //////////////////////////////////////////////////
385
    // just in the toolbar
386 387
    //////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
388 389
    m_sliderAction = new SliderAction(i18n("Track Position"), actionCollection(),
				      "trackPositionAction");
390 391

    createGUI();
392 393

    // set the slider to the proper orientation and make it stay that way
394
    m_sliderAction->slotUpdateOrientation();
395 396
}

397 398 399 400 401 402
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
403 404 405 406
    connect(m_showSearchAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetSearchVisible(bool)));
    connect(m_showEditorAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetEditorVisible(bool)));
407 408
    connect(m_showHistoryAction, SIGNAL(toggled(bool)),
	    m_splitter, SLOT(slotSetHistoryVisible(bool)));
409
    connect(this, SIGNAL(dockWindowPositionChanged(QDockWindow *)),
Scott Wheeler's avatar
Scott Wheeler committed
410 411 412
	    m_sliderAction, SLOT(slotUpdateOrientation(QDockWindow *)));
    connect(m_splitter, SIGNAL(signalPlaylistChanged()),
	    this, SLOT(slotPlaylistChanged()));
413 414
}

415
void JuK::setupSystemTray()
416
{
417
    if(m_toggleSystemTrayAction && m_toggleSystemTrayAction->isChecked()) {
418 419
        m_systemTray = new SystemTray(this, "systemTray");
        m_systemTray->show();
420

421
        connect(this, SIGNAL(signalNewSong(const QString&)),
Scott Wheeler's avatar
Scott Wheeler committed
422
		m_systemTray, SLOT(slotNewSong(const QString&)));
423

424
        m_toggleDockOnCloseAction->setEnabled(true);
425
	m_togglePopupsAction->setEnabled(true);
426

427
        connect(m_systemTray, SIGNAL(quitSelected()), this, SLOT(slotQuit()));
428
    }
429
    else {
430 431
        m_systemTray = 0;
        m_toggleDockOnCloseAction->setEnabled(false);
432
	m_togglePopupsAction->setEnabled(false);
433
    }
434 435
}

436 437 438
void JuK::setupGlobalAccels()
{
    m_accel = new KGlobalAccel(this);
439 440 441

    KeyDialog::insert(m_accel, "PlayPause",   i18n("Play/Pause"),   action("playPause"), SLOT(activate()));
    KeyDialog::insert(m_accel, "Stop",        i18n("Stop Playing"), action("stop"),      SLOT(activate()));
442 443 444 445 446 447 448
    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()));
449

450 451 452 453 454
    m_accel->setConfigGroup("Shortcuts");
    m_accel->readSettings();
    m_accel->updateConnections();
}

455 456 457 458
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
459

460
    for(int i = 0; i < args->count(); i++)
461
        files.append(args->arg(i));
462

463
    m_splitter->open(files);
464 465
}

466 467 468
void JuK::keyPressEvent(QKeyEvent *e)
{
    if (e->key() >= Qt::Key_Back && e->key() <= Qt::Key_MediaLast)
469
        e->accept();
470 471 472
    KMainWindow::keyPressEvent(e);
}

473 474 475 476 477 478 479 480 481
/**
 * 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");
482
        m_showSplash = config->readBoolEntry("ShowSplashScreen", true);
483
        m_startDocked = config->readBoolEntry("StartDocked", false);
484 485 486
    }
}

487 488
void JuK::readConfig()
{
489
    // Automagically save and m_restore many window settings.
490 491
    setAutoSaveSettings();

492
    KConfig *config = KGlobal::config();
493
    { // player settings
494
        KConfigGroupSaver saver(config, "Player");
495 496
        if(m_sliderAction->volumeSlider()) {
	    int maxVolume = m_sliderAction->volumeSlider()->maxValue();
Scott Wheeler's avatar
Scott Wheeler committed
497
	    int volume = config->readNumEntry("Volume", maxVolume);
498
            m_sliderAction->volumeSlider()->setValue(volume);
499
        }
500 501
        bool randomPlay = config->readBoolEntry("RandomPlay", false);
        m_randomPlayAction->setChecked(randomPlay);
502

503 504
        bool loopPlaylist = config->readBoolEntry("LoopPlaylist", false);
        m_loopPlaylistAction->setChecked(loopPlaylist);
505
    }
506
    { // view settings
507
        KConfigGroupSaver saver(config, "View");
508 509 510 511 512

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

513 514 515
        bool showEditor = config->readBoolEntry("ShowEditor", false);
        m_showEditorAction->setChecked(showEditor);
        m_splitter->slotSetEditorVisible(showEditor);
516

517 518 519
	// 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.
520

521 522
	bool showHistory = config->readBoolEntry("ShowHistory", false);
	m_showHistoryAction->setChecked(showHistory);
523
    }
524 525
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
526

527 528
        bool dockInSystemTray = config->readBoolEntry("DockInSystemTray", true);
        m_toggleSystemTrayAction->setChecked(dockInSystemTray);
529

530 531
        bool dockOnClose = config->readBoolEntry("DockOnClose", true);
        m_toggleDockOnCloseAction->setChecked(dockOnClose);
532

533 534
        bool showPopups = config->readBoolEntry("TrackPopup", false);
        m_togglePopupsAction->setChecked(showPopups);
535

536 537
        if(m_outputSelectAction)
            m_outputSelectAction->setCurrentItem(config->readNumEntry("MediaSystem", 0));
538
    }
539

540
    m_toggleSplashAction->setChecked(m_showSplash);
541 542 543 544
}

void JuK::saveConfig()
{
545
    KConfig *config = KGlobal::config();
546
    { // player settings
547
        KConfigGroupSaver saver(config, "Player");
548 549
        if(m_sliderAction && m_sliderAction->volumeSlider())
            config->writeEntry("Volume", m_sliderAction->volumeSlider()->value());
550 551 552 553
        if(m_randomPlayAction)
            config->writeEntry("RandomPlay", m_randomPlayAction->isChecked());
        if(m_loopPlaylistAction)
            config->writeEntry("LoopPlaylist", m_loopPlaylistAction->isChecked());
554
    }
555 556
    { // view settings
        KConfigGroupSaver saver(config, "View");
557

558
        config->writeEntry("ShowEditor", m_showEditorAction->isChecked());
559
        config->writeEntry("ShowSearch", m_showSearchAction->isChecked());
560
	config->writeEntry("ShowHistory", m_showHistoryAction->isChecked());
561
    }
562 563
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
564
        config->writeEntry("ShowSplashScreen", m_toggleSplashAction->isChecked());
565
        config->writeEntry("StartDocked", m_startDocked);
566 567 568 569 570
        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());
571
    }
572
    config->sync();
573 574
}

575
bool JuK::queryExit()
576
{
577 578
    m_startDocked = !isVisible();

579 580 581 582
    hide();
    delete m_systemTray;
    m_systemTray = 0;

583
    Cache::instance()->save();
584
    saveConfig();
585
    delete m_splitter;
586
    return true;
587 588
}

589 590
bool JuK::queryClose()
{
591 592 593 594 595
    if(!m_shuttingDown &&
       !kapp->sessionSaving() &&
       m_systemTray &&
       m_toggleDockOnCloseAction->isChecked())
    {
596
	KMessageBox::information(this,
Scott Wheeler's avatar
Scott Wheeler committed
597 598 599
	    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");
600 601 602 603 604 605 606
	hide();
	return false;
    }
    else
	return true;
}

607 608 609
QString JuK::playingString() const
{
    QString s;
610 611
    if(!m_player->playing() && !m_player->paused())
	return i18n("No song playing");
612

613 614 615 616
    if(!m_splitter->playingArtist().isEmpty())
	s = m_splitter->playingArtist().simplifyWhiteSpace() + " - ";

    s += m_splitter->playingTrack().simplifyWhiteSpace();
617 618 619 620

    return s;
}

621 622
void JuK::updatePlaylistInfo()
{
Scott Wheeler's avatar
Scott Wheeler committed
623
    m_statusLabel->setPlaylistInfo(m_splitter->visiblePlaylistName(),
624 625
				   m_splitter->selectedPlaylistCount(),
				   m_splitter->selectedPlaylistTotalTime());
626 627 628 629
}

void JuK::play(const QString &file)
{
630
    m_player->play(file);
631
    emit signalNewSong(playingString());
632 633
}

Scott Wheeler's avatar
Scott Wheeler committed
634 635 636
KAction *JuK::createSplitterAction(const QString &text, const char *slot,
				   const char *name, const QString &pix,
				   const KShortcut &shortcut)
637 638 639 640 641 642 643 644 645
{
    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));
646

647 648 649
    return action;
}

650 651 652 653
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

654
void JuK::slotPlaylistChanged()
655
{
656 657 658 659
    if(m_splitter->collectionListSelected() ||
       !m_splitter->hasListSelected() ||
       m_splitter->readOnlyListSelected())
    {
660 661 662 663 664
        action("file_save")->setEnabled(false);
        action("file_save_as")->setEnabled(false);
        action("renamePlaylist")->setEnabled(false);
        action("reloadPlaylist")->setEnabled(false);
        action("deleteItemPlaylist")->setEnabled(false);
665 666
    }
    else {
667 668 669 670
        action("file_save")->setEnabled(true);
        action("file_save_as")->setEnabled(true);
        action("renamePlaylist")->setEnabled(true);
        action("deleteItemPlaylist")->setEnabled(true);
671 672

	if(m_splitter->fileBasedListSelected() || m_splitter->dynamicListSelected())
673
	    action("reloadPlaylist")->setEnabled(true);
674
	else
675
	    action("reloadPlaylist")->setEnabled(false);
676
    }
677
    if(m_splitter->hasListSelected())
678
        action("duplicatePlaylist")->setEnabled(true);
679
    else
680
        action("duplicatePlaylist")->setEnabled(false);
681

682 683 684
    updatePlaylistInfo();
}

685 686 687 688 689
void JuK::slotQuit()
{
    kapp->quit();
}

690 691 692 693
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

694
void JuK::slotToggleSystemTray(bool enabled)
695
{
696
    if(enabled && !m_systemTray)
697
	setupSystemTray();
698 699 700
    else if(!enabled && m_systemTray) {
	delete m_systemTray;
	m_systemTray = 0;
701
	m_toggleDockOnCloseAction->setEnabled(false);
702
	m_togglePopupsAction->setEnabled(false);
703 704 705
    }
}

706 707 708 709 710
void JuK::slotEditKeys()
{
    KeyDialog::configure(m_accel, actionCollection(), this);
}

711
////////////////////////////////////////////////////////////////////////////////
712
// additional player slots
713 714
////////////////////////////////////////////////////////////////////////////////

715
void JuK::slotPlayCurrent()
716
{
717 718
    m_player->stop();
    action("play")->activate();
719 720
}

721 722
void JuK::slotConfigureTagGuesser()
{
723
    TagGuesserConfigDlg(this).exec();
724 725
}

726 727
void JuK::slotConfigureFileRenamer()
{
728
    FileRenamerConfigDlg(this).exec();
729 730
}

731
#include "juk.moc"