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
#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 105 106 107 108 109 110
void JuK::startPlayingPlaylist()
{
    if(m_randomPlayAction->isChecked())
        play(m_splitter->playRandomFile());
    else
        play(m_splitter->playFirstFile());
}

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

void JuK::slotGuessTagInfoFromInternet()
{
    m_splitter->slotGuessTagInfo(TagGuesser::MusicBrainz);
119 120
}

121 122
void JuK::play()
{
123
    m_player->play();
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
}

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

void JuK::forward()
{
158
    m_player->forward();
159 160 161 162
}

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

void JuK::seekForward()
{
170 171 172
    int position = m_sliderAction->trackPositionSlider()->value();
    position = QMIN(m_sliderAction->trackPositionSlider()->maxValue(), position + 10);
    emit m_sliderAction->trackPositionSlider()->setValue(position);
173 174 175 176 177 178 179 180 181 182 183 184
}

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

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

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

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

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

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

225 226 227 228
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

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

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

240
    // create status bar
241

242
    m_statusLabel = new StatusLabel(statusBar());
243

244
    statusBar()->addWidget(m_statusLabel, 1);
245

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

253 254
    PlayerManager::instance()->setStatusLabel(m_statusLabel);

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

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

261
    m_splitter->setFocus();
262

263
    resize(750, 500);
264 265
}

266 267
void JuK::setupActions()
{
268
    //////////////////////////////////////////////////
269
    // file menu
270
    //////////////////////////////////////////////////
271

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

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

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

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

304
    KStdAction::quit(this, SLOT(slotQuit()), actionCollection());
305

306
    //////////////////////////////////////////////////
307
    // edit menu
308 309
    //////////////////////////////////////////////////

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

315
    KStdAction::selectAll(kapp, SLOT(selectAll()), actionCollection());
316

317
    createSplitterAction(
318
	i18n("Advanced Search"), SLOT(slotAdvancedSearch()), "advancedSearch", "find", "s");
319

320
    //////////////////////////////////////////////////
321
    // view menu
322
    //////////////////////////////////////////////////
323

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

331
    createSplitterAction(i18n("Refresh Items"), SLOT(slotRefresh()), "refresh", "reload");
332

333
    //////////////////////////////////////////////////
334
    // play menu
335 336
    //////////////////////////////////////////////////

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

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

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

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

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

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

364
    //////////////////////////////////////////////////
365
    // tagger menu
366 367
    //////////////////////////////////////////////////

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

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

Scott Wheeler's avatar
Scott Wheeler committed
377
    KAction *a;
378

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

388
    //////////////////////////////////////////////////
389
    // settings menu
390 391
    //////////////////////////////////////////////////

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

395
    setStandardToolBarMenuEnabled(true);
396

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

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

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

Scott Wheeler's avatar
Scott Wheeler committed
412

413
    m_outputSelectAction = Player::playerSelectAction(actionCollection());
414

415
    if(m_outputSelectAction)
Frerich Raabe's avatar
Frerich Raabe committed
416
        m_outputSelectAction->setCurrentItem(0);
417

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

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

424 425
    KStdAction::keyBindings(this, SLOT(slotEditKeys()), actionCollection());

426
    //////////////////////////////////////////////////
427
    // just in the toolbar
428 429
    //////////////////////////////////////////////////

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

    createGUI();
434 435

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

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

457
void JuK::setupSystemTray()
458
{
459
    if(m_toggleSystemTrayAction && m_toggleSystemTrayAction->isChecked()) {
Frerich Raabe's avatar
Frerich Raabe committed
460 461
        m_systemTray = new SystemTray(this, "systemTray");
        m_systemTray->show();
462

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

Frerich Raabe's avatar
Frerich Raabe committed
466
        m_toggleDockOnCloseAction->setEnabled(true);
467
	m_togglePopupsAction->setEnabled(true);
468

Frerich Raabe's avatar
Frerich Raabe committed
469
        connect(m_systemTray, SIGNAL(quitSelected()), this, SLOT(slotQuit()));
470
    }
471
    else {
Frerich Raabe's avatar
Frerich Raabe committed
472 473
        m_systemTray = 0;
        m_toggleDockOnCloseAction->setEnabled(false);
474
	m_togglePopupsAction->setEnabled(false);
475
    }
476 477
}

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

482
    if(m_sliderAction) {
483 484
    }

485 486
    m_player = PlayerManager::instance();
    PlayerManager::instance()->setPlaylistInterface(m_splitter);
487 488
}

489

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

503 504 505 506 507
    m_accel->setConfigGroup("Shortcuts");
    m_accel->readSettings();
    m_accel->updateConnections();
}

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

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

516
    m_splitter->open(files);
517 518
}

519 520 521
void JuK::keyPressEvent(QKeyEvent *e)
{
    if (e->key() >= Qt::Key_Back && e->key() <= Qt::Key_MediaLast)
Frerich Raabe's avatar
Frerich Raabe committed
522
        e->accept();
523 524 525
    KMainWindow::keyPressEvent(e);
}

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

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

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

Frerich Raabe's avatar
Frerich Raabe committed
556 557
        bool loopPlaylist = config->readBoolEntry("LoopPlaylist", false);
        m_loopPlaylistAction->setChecked(loopPlaylist);
558
    }
559
    { // view settings
560
        KConfigGroupSaver saver(config, "View");
561 562 563 564 565

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

Frerich Raabe's avatar
Frerich Raabe committed
566 567 568
        bool showEditor = config->readBoolEntry("ShowEditor", false);
        m_showEditorAction->setChecked(showEditor);
        m_splitter->slotSetEditorVisible(showEditor);
569

570 571 572
	// 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.
573

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

Frerich Raabe's avatar
Frerich Raabe committed
580 581
        bool dockInSystemTray = config->readBoolEntry("DockInSystemTray", true);
        m_toggleSystemTrayAction->setChecked(dockInSystemTray);
582

Frerich Raabe's avatar
Frerich Raabe committed
583 584
        bool dockOnClose = config->readBoolEntry("DockOnClose", true);
        m_toggleDockOnCloseAction->setChecked(dockOnClose);
585

Frerich Raabe's avatar
Frerich Raabe committed
586 587
        bool showPopups = config->readBoolEntry("TrackPopup", false);
        m_togglePopupsAction->setChecked(showPopups);
588

Frerich Raabe's avatar
Frerich Raabe committed
589 590
        if(m_outputSelectAction)
            m_outputSelectAction->setCurrentItem(config->readNumEntry("MediaSystem", 0));
591
    }
592

593
    m_toggleSplashAction->setChecked(m_showSplash);
594 595 596 597
}

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

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

628
bool JuK::queryExit()
629
{
630 631
    m_startDocked = !isVisible();

632 633 634 635
    hide();
    delete m_systemTray;
    m_systemTray = 0;

636
    stop();
637

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

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

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

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

    s += m_splitter->playingTrack().simplifyWhiteSpace();
672 673 674 675

    return s;
}

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

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

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

702 703 704
    return action;
}

705 706 707 708
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

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

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

737 738 739
    updatePlaylistInfo();
}

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

745 746 747 748
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

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

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

766
////////////////////////////////////////////////////////////////////////////////
767
// additional player slots
768 769
////////////////////////////////////////////////////////////////////////////////

770 771 772 773 774 775 776
void JuK::slotPlaySelectedFile()
{
    QString file = m_splitter->playSelectedFile();
    if(!file.isNull())
	play(m_splitter->playSelectedFile());
}

777 778
void JuK::slotConfigureTagGuesser()
{
779
    TagGuesserConfigDlg(this).exec();
780 781
}

782 783
void JuK::slotConfigureFileRenamer()
{
784
    FileRenamerConfigDlg(this).exec();
785 786
}

787
#include "juk.moc"