juk.cpp 16.8 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
#include <kapplication.h>
19
#include <klocale.h>
20
#include <kiconloader.h>
21
#include <kcmdlineargs.h>
22
#include <kstatusbar.h>
23
#include <kconfig.h>
24 25
#include <kdebug.h>

26 27
#include <qtimer.h>
#include <qlistview.h>
28
#include <qinputdialog.h>
29
#include <qslider.h>
30

31
#include "juk.h"
32
#include "slideraction.h"
33
#include "cache.h"
34
#include "statuslabel.h"
35 36
#include "splashscreen.h"
#include "genrelisteditor.h"
37
#include "systemtray.h"
38 39 40 41 42

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

43
JuK::JuK(QWidget *parent, const char *name) : KMainWindow(parent, name, WDestructiveClose)
44
{
45 46 47
    SplashScreen::instance()->show();
    kapp->processEvents();
 
48 49
    // Expect segfaults if you change this order.

50
    readSettings();
51
    setupLayout();
52
    setupActions();
53
    readConfig();
54
    setupPlayer();
55
    setupSystemTray();
56
    processArgs();
57

58
    SplashScreen::finishedLoading();
59 60 61 62
}

JuK::~JuK()
{
63

64 65 66 67 68 69
}

////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

70 71
void JuK::setupLayout()
{
72
    splitter = new PlaylistSplitter(this, restore, "playlistSplitter");
73 74 75
    setCentralWidget(splitter);

    // playlist item activation connection
76 77
    connect(splitter, SIGNAL(doubleClicked()), this, SLOT(playSelectedFile()));
    connect(splitter, SIGNAL(listBoxDoubleClicked()), this, SLOT(playFirstFile()));
78

79
    // create status bar
80
    statusLabel = new StatusLabel(statusBar());
81 82
    statusBar()->addWidget(statusLabel, 1);

83
    connect(splitter, SIGNAL(selectedPlaylistCountChanged(int)), statusLabel, SLOT(setPlaylistCount(int)));
84
    connect(statusLabel, SIGNAL(jumpButtonClicked()), splitter, SLOT(selectPlaying()));
85

86 87
    updatePlaylistInfo();

88 89 90
    splitter->setFocus();
}

91 92
void JuK::setupActions()
{
93
    // file menu
94
    KStdAction::openNew(splitter, SLOT(createPlaylist()), actionCollection());
95 96
    KStdAction::open(splitter, SLOT(open()), actionCollection());
    new KAction(i18n("Open &Directory..."), "fileopen", 0, splitter, SLOT(openDirectory()), actionCollection(), "openDirectory");
97 98 99 100

    renamePlaylistAction = new KAction(i18n("Rename..."), 0, splitter, SLOT(renamePlaylist()), 
				       actionCollection(), "renamePlaylist");
    new KAction(i18n("Duplicate..."), "editcopy", 0, splitter, SLOT(duplicatePlaylist()), actionCollection(), "duplicatePlaylist");
101
    
102 103 104 105 106
    savePlaylistAction = KStdAction::save(splitter, SLOT(savePlaylist()), actionCollection());
    saveAsPlaylistAction = KStdAction::saveAs(splitter, SLOT(saveAsPlaylist()), actionCollection());
    deleteItemPlaylistAction = new KAction(i18n("Delete"), "editdelete", 0, splitter, SLOT(deleteItemPlaylist()), 
					   actionCollection(), "deleteItemPlaylist");

107
    KStdAction::quit(this, SLOT(close()), actionCollection());
108 109

    // edit menu
110 111 112
    KStdAction::cut(this, SLOT(cut()), actionCollection());
    KStdAction::copy(splitter, SLOT(copy()), actionCollection());
    KStdAction::paste(splitter, SLOT(paste()), actionCollection());
113
    KStdAction::selectAll(splitter, SLOT(selectAll()), actionCollection());
114

115
    // view menu
116
    showEditorAction = new KToggleAction(i18n("Show Tag Editor"), "edit", 0, actionCollection(), "showEditor");
117 118
    connect(showEditorAction, SIGNAL(toggled(bool)), splitter, SLOT(setEditorVisible(bool)));
    KStdAction::redisplay(splitter, SLOT(refresh()), actionCollection());
119

120
    // play menu
121
    randomPlayAction = new KToggleAction(i18n("Random Play"), 0, actionCollection(), "randomPlay");
Scott Wheeler's avatar
Scott Wheeler committed
122 123 124 125 126
    playAction = new KAction(i18n("&Play"), "player_play", 0, this, SLOT(play()), actionCollection(), "play");
    pauseAction = new KAction(i18n("P&ause"), "player_pause", 0, this, SLOT(pause()), actionCollection(), "pause");
    stopAction = new KAction(i18n("&Stop"), "player_stop", 0, this, SLOT(stop()), actionCollection(), "stop");
    backAction = new KAction(i18n("Skip &Back"), "player_start", 0, this, SLOT(back()), actionCollection(), "back");
    forwardAction = new KAction(i18n("Skip &Forward"), "player_end", 0, this, SLOT(forward()), actionCollection(), "forward");
127

128
    // tagger menu
129
    new KAction(i18n("Save"), "filesave", "CTRL+t", splitter, SLOT(saveItem()), actionCollection(), "saveItem");
130
    new KAction(i18n("Delete"), "editdelete", 0, splitter, SLOT(removeSelectedItems()), actionCollection(), "removeItem");
131
    
132
    // settings menu
Scott Wheeler's avatar
Scott Wheeler committed
133
    restoreOnLoadAction = new KToggleAction(i18n("Restore Playlists on Load"),  0, actionCollection(), "restoreOnLoad");
134

Scott Wheeler's avatar
Scott Wheeler committed
135
    toggleSystemTrayAction = new KToggleAction(i18n("Dock in System Tray"), 0, actionCollection(), "toggleSystemTray");
136
    connect(toggleSystemTrayAction, SIGNAL(toggled(bool)), this, SLOT(toggleSystemTray(bool)));
137

Scott Wheeler's avatar
Scott Wheeler committed
138
    new KAction(i18n("Genre List Editor..."), 0, this, SLOT(showGenreListEditor()), actionCollection(), "showGenreListEditor");
139

140

141 142 143 144 145
    outputSelectAction = Player::playerSelectAction(actionCollection());
    if(outputSelectAction) {
	outputSelectAction->setCurrentItem(0);
	connect(outputSelectAction, SIGNAL(activated(int)), this, SLOT(setOutput(int)));
    }
146

147 148 149 150
    // just in the toolbar
    sliderAction = new SliderAction(i18n("Track Position"), actionCollection(), "trackPositionAction");

    createGUI();
151 152 153 154

    // set the slider to the proper orientation and make it stay that way
    sliderAction->updateOrientation();
    connect(this, SIGNAL(dockWindowPositionChanged(QDockWindow *)), sliderAction, SLOT(updateOrientation(QDockWindow *)));
155 156

    connect(splitter, SIGNAL(playlistChanged()), this, SLOT(playlistChanged()));
157 158
}

159
void JuK::setupSystemTray()
160 161 162 163 164
{
    if(toggleSystemTrayAction && toggleSystemTrayAction->isChecked()) {
	systemTray = new SystemTray(this, "systemTray");
	systemTray->show();
	
Scott Wheeler's avatar
Scott Wheeler committed
165 166 167 168 169
	connect(systemTray, SIGNAL(play()),    this, SLOT(play()));
	connect(systemTray, SIGNAL(stop()),    this, SLOT(stop()));
	connect(systemTray, SIGNAL(pause()),   this, SLOT(pause()));
	connect(systemTray, SIGNAL(back()),    this, SLOT(back()));
	connect(systemTray, SIGNAL(forward()), this, SLOT(forward()));
170
	
171
	if(player && player->paused())
172
	    systemTray->slotPause();
173
	else if(player && player->playing())
174 175 176 177
	    systemTray->slotPlay();
    }
    else
	systemTray = 0;
178 179
}

180 181
void JuK::setupPlayer()
{
182 183
    trackPositionDragging = false;
    noSeek = false;
184 185
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
186 187
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
188

189
    playTimer = new QTimer(this);
190 191 192 193 194 195 196
    connect(playTimer, SIGNAL(timeout()), this, SLOT(pollPlay()));

    if(sliderAction && sliderAction->getTrackPositionSlider() && sliderAction->getVolumeSlider()) {
        connect(sliderAction->getTrackPositionSlider(), SIGNAL(valueChanged(int)), this, SLOT(trackPositionSliderUpdate(int)));
        connect(sliderAction->getTrackPositionSlider(), SIGNAL(sliderPressed()), this, SLOT(trackPositionSliderClick()));
        connect(sliderAction->getTrackPositionSlider(), SIGNAL(sliderReleased()), this, SLOT(trackPositionSliderRelease()));
        sliderAction->getTrackPositionSlider()->setEnabled(false);
197

198 199
        connect(sliderAction->getVolumeSlider(), SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    }
200 201 202 203 204 205 206 207
    
    int playerType = 0;
    if(outputSelectAction) {
	playerType = outputSelectAction->currentItem();
	connect(outputSelectAction, SIGNAL(activated(int)), this, SLOT(setOutput(int)));
    }

    player = Player::createPlayer(playerType);
208 209
}

210

211 212 213 214 215 216 217 218
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
    
    for(int i = 0; i < args->count(); i++)
	files.append(args->arg(i));

219
    splitter->open(files);
220 221
}

222 223 224 225 226 227 228 229 230 231 232 233 234
/**
 * 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");
	restore = config->readBoolEntry("RestoreOnLoad", true);
    }
}

235 236
void JuK::readConfig()
{
237 238 239
    // Automagically save and restore many window settings.
    setAutoSaveSettings();

240 241 242 243 244 245 246
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider()) {
            int volume = config->readNumEntry("Volume", sliderAction->getVolumeSlider()->maxValue());
            sliderAction->getVolumeSlider()->setValue(volume);
        }
247 248 249 250
	if(randomPlayAction) {
	    bool randomPlay = config->readBoolEntry("RandomPlay", false);
	    randomPlayAction->setChecked(randomPlay);
	}
251
    }
252
    { // view settings
253
        KConfigGroupSaver saver(config, "View");
254
	bool showEditor = config->readBoolEntry("ShowEditor", false);
255 256 257
	showEditorAction->setChecked(showEditor);
	splitter->setEditorVisible(showEditor);
    }
258 259
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
260 261
	bool dockInSystemTray = config->readBoolEntry("DockInSystemTray", true);
	toggleSystemTrayAction->setChecked(dockInSystemTray);
262 263 264 265
	
	if(outputSelectAction)
	    outputSelectAction->setCurrentItem(config->readNumEntry("MediaSystem", 0));
	
266
    }
267 268 269 270

    if(restoreOnLoadAction)
	restoreOnLoadAction->setChecked(restore);
    
271 272 273 274
}

void JuK::saveConfig()
{
275 276 277 278 279
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider())
            config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
280 281
	if(randomPlayAction)
	    config->writeEntry("RandomPlay", randomPlayAction->isChecked());
282
    }
283 284 285 286
    { // view settings
        KConfigGroupSaver saver(config, "View");
	config->writeEntry("ShowEditor", showEditorAction->isChecked());
    }
287 288 289 290
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	if(restoreOnLoadAction)
	    config->writeEntry("RestoreOnLoad", restoreOnLoadAction->isChecked());
291 292
	if(toggleSystemTrayAction)
	    config->writeEntry("DockInSystemTray", toggleSystemTrayAction->isChecked());
293 294
	if(outputSelectAction)
	    config->writeEntry("MediaSystem", outputSelectAction->currentItem());
295
    }
296 297
}

298 299
bool JuK::queryClose()
{
Scott Wheeler's avatar
Scott Wheeler committed
300
    stop();
301
    delete(player);
302
    Cache::instance()->save();
303
    saveConfig();
304
    delete(splitter);
305 306 307
    return(true);
}

308 309 310 311
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

312
void JuK::playlistChanged()
313
{
314
    if(splitter->collectionListSelected()) {
315 316 317 318 319 320 321 322 323 324 325
	savePlaylistAction->setEnabled(false);
	saveAsPlaylistAction->setEnabled(false);
	renamePlaylistAction->setEnabled(false);
	deleteItemPlaylistAction->setEnabled(false);	
    }
    else {
	savePlaylistAction->setEnabled(true);
	saveAsPlaylistAction->setEnabled(true);
	renamePlaylistAction->setEnabled(true);
	deleteItemPlaylistAction->setEnabled(true);
    }
326 327 328 329 330 331

    updatePlaylistInfo();
}

void JuK::updatePlaylistInfo()
{
332
    statusLabel->setPlaylistInfo(splitter->selectedPlaylistName(), splitter->selectedPlaylistCount());
333 334 335 336 337 338
}

////////////////////////////////////////////////////////////////////////////////
// player menu
////////////////////////////////////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
339
void JuK::play()
340
{
341 342 343 344 345
    if(!player)
	return;

    if(player->paused()) {
        player->play();
346 347 348 349

	// Here, before doing anything, we want to make sure that the player did
	// in fact start.

350
        if(player->playing()) {
351 352 353
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
354 355
	    if(systemTray)
		systemTray->slotPlay();
356 357
        }
    }
358 359
    else if(player->playing())
	player->seekPosition(0);
360
    else
Scott Wheeler's avatar
Scott Wheeler committed
361
	play(splitter->playNextFile(randomPlayAction->isChecked()));
362 363
}

Scott Wheeler's avatar
Scott Wheeler committed
364
void JuK::pause()
365
{
366 367 368
    if(!player)
	return;

369
    playTimer->stop();
370
    player->pause();
371
    pauseAction->setEnabled(false);
372 373
    if(systemTray)
	systemTray->slotPause();
374 375
}

Scott Wheeler's avatar
Scott Wheeler committed
376
void JuK::stop()
377
{
378 379 380
    if(!player)
	return;

381
    playTimer->stop();
382
    player->stop();
383

384 385
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
386 387
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
388

389 390
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
391 392

    splitter->stop();
393

394
    statusLabel->clear();
395 396 397
    
    if(systemTray)
	systemTray->slotStop();
398 399
}

Scott Wheeler's avatar
Scott Wheeler committed
400
void JuK::back()
401
{
Scott Wheeler's avatar
Scott Wheeler committed
402
    play(splitter->playPreviousFile(randomPlayAction->isChecked()));
403 404
}

Scott Wheeler's avatar
Scott Wheeler committed
405
void JuK::forward()
406
{
Scott Wheeler's avatar
Scott Wheeler committed
407
    play(splitter->playNextFile(randomPlayAction->isChecked()));
408 409
}

410 411 412 413 414 415 416 417 418
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

void JuK::showGenreListEditor()
{
    GenreListEditor * editor = new GenreListEditor();
    editor->exec();
}
419

420 421 422 423 424 425 426 427 428 429
void JuK::toggleSystemTray(bool enabled)
{
    if(enabled && !systemTray)
	setupSystemTray();
    else if(!enabled && systemTray) {
	delete(systemTray);
	systemTray = 0;
    }
}

430 431
void JuK::setOutput(int output)
{
Scott Wheeler's avatar
Scott Wheeler committed
432
    stop();
433 434 435 436
    delete(player);
    player = Player::createPlayer(output);
}

437 438 439 440
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

441 442
void JuK::trackPositionSliderClick()
{
443
    trackPositionDragging = true;
444 445 446 447
}

void JuK::trackPositionSliderRelease()
{
448 449 450
    if(!player)
	return;

451
    trackPositionDragging = false;
452
    player->seekPosition(sliderAction->getTrackPositionSlider()->value());
453 454 455 456
}

void JuK::trackPositionSliderUpdate(int position)
{
457 458 459 460 461
    if(!player)
	return;

    if(player->playing() && !trackPositionDragging && !noSeek)
        player->seekPosition(position);
462 463
}

464 465
// This method is called when the play timer has expired.

466 467
void JuK::pollPlay()
{
468 469 470
    if(!player)
	return;

471 472
    // Our locking mechanism.  Since this method adjusts the play slider, we 
    // want to make sure that our adjustments
473
    noSeek = true;
474

475
    if(!player->playing()) {
476

477
        playTimer->stop();
478

479
	if(!player->paused())
Scott Wheeler's avatar
Scott Wheeler committed
480
	    play(splitter->playNextFile(randomPlayAction->isChecked()));
481

482
    }
483
    else if(!trackPositionDragging) {
484 485 486
        sliderAction->getTrackPositionSlider()->setValue(player->position());
	statusLabel->setItemTotalTime(player->totalTime());
	statusLabel->setItemCurrentTime(player->currentTime());
487 488 489 490 491 492
    }

    // Ok, this is weird stuff, but it works pretty well.  Ordinarily we don't
    // need to check up on our playing time very often, but in the span of the 
    // last interval, we want to check a lot -- to figure out that we've hit the
    // end of the song as soon as possible.
493

494
    if(player->playing() && player->totalTime() > 0 && float(player->totalTime() - player->currentTime()) < pollInterval * 2)
495
        playTimer->changeInterval(50);
496

497
    noSeek = false;
498 499 500 501
}

void JuK::setVolume(int volume)
{
502
    if(player && sliderAction && sliderAction->getVolumeSlider() &&
503 504
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
505
    {
506
        player->setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
507 508
    }
}
509

Scott Wheeler's avatar
Scott Wheeler committed
510
void JuK::play(const QString &file)
511
{
512 513 514
    if(!player)
	return;

515
    float volume = float(sliderAction->getVolumeSlider()->value()) / float(sliderAction->getVolumeSlider()->maxValue());
516

517 518
    if(player->paused())
	player->stop();
519
    
520
    player->play(file, volume);
521

522
    // Make sure that the player actually starts before doing anything.
523

524
    if(player->playing()) {
525 526 527 528 529 530
	pauseAction->setEnabled(true);
	stopAction->setEnabled(true);
	
	backAction->setEnabled(true);
	forwardAction->setEnabled(true);
	
531
	sliderAction->getTrackPositionSlider()->setValue(0);
532 533
	sliderAction->getTrackPositionSlider()->setEnabled(true);
	playTimer->start(pollInterval);
534

535
	statusLabel->setPlayingItemInfo(splitter->playingTrack(), splitter->playingArtist(), splitter->playingList());
536

537 538
	if(systemTray)
	    systemTray->slotPlay();
539
    }
540
    else
Scott Wheeler's avatar
Scott Wheeler committed
541
	stop();
542
}
543

544
#include "juk.moc"