juk.cpp 18.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
#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 <qstrlist.h>
#include <qmetaobject.h>
32

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

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

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

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

60
    SplashScreen::finishedLoading();
61 62 63 64
}

JuK::~JuK()
{
65

66 67 68 69 70 71
}

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

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

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

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

85
    connect(splitter, SIGNAL(selectedPlaylistCountChanged(int)), statusLabel, SLOT(setPlaylistCount(int)));
86
    connect(statusLabel, SIGNAL(jumpButtonClicked()), splitter, SLOT(selectPlaying()));
87

88 89
    updatePlaylistInfo();

90
    splitter->setFocus();
91

92
    resize(750, 500);
93 94
}

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

    renamePlaylistAction = new KAction(i18n("Rename..."), 0, splitter, SLOT(renamePlaylist()), 
				       actionCollection(), "renamePlaylist");
    new KAction(i18n("Duplicate..."), "editcopy", 0, splitter, SLOT(duplicatePlaylist()), actionCollection(), "duplicatePlaylist");
105
    
106 107
    savePlaylistAction = KStdAction::save(splitter, SLOT(savePlaylist()), actionCollection());
    saveAsPlaylistAction = KStdAction::saveAs(splitter, SLOT(saveAsPlaylist()), actionCollection());
108
    deleteItemPlaylistAction = new KAction(i18n("Remove"), "edittrash", 0, splitter, SLOT(deleteItemPlaylist()), 
109 110
					   actionCollection(), "deleteItemPlaylist");

111
    KStdAction::quit(this, SLOT(close()), actionCollection());
112 113

    // edit menu
114
    KStdAction::cut(this, SLOT(cut()), actionCollection());
115 116 117 118
    KStdAction::copy(this, SLOT(copy()), actionCollection());
    KStdAction::paste(this, SLOT(paste()), actionCollection());
    new KAction(i18n("Clear"), "editclear", 0, this, SLOT(clear()), actionCollection(), "clear");
    KStdAction::selectAll(this, SLOT(selectAll()), actionCollection());
119

120
    // view menu
121
    showEditorAction = new KToggleAction(i18n("Show Tag Editor"), "edit", 0, actionCollection(), "showEditor");
122 123
    connect(showEditorAction, SIGNAL(toggled(bool)), splitter, SLOT(setEditorVisible(bool)));
    KStdAction::redisplay(splitter, SLOT(refresh()), actionCollection());
124 125
    actionCollection()->insert(splitter->columnVisibleAction());
    
126
    // play menu
127
    randomPlayAction = new KToggleAction(i18n("Random Play"), 0, actionCollection(), "randomPlay");
Scott Wheeler's avatar
Scott Wheeler committed
128 129 130 131 132
    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");
133

134
    // tagger menu
135
    new KAction(i18n("Save"), "filesave", "CTRL+t", splitter, SLOT(saveItem()), actionCollection(), "saveItem");
136
    new KAction(i18n("Delete"), "editdelete", 0, splitter, SLOT(removeSelectedItems()), actionCollection(), "removeItem");
137
    
138
    // settings menu
Scott Wheeler's avatar
Scott Wheeler committed
139
    restoreOnLoadAction = new KToggleAction(i18n("Restore Playlists on Load"),  0, actionCollection(), "restoreOnLoad");
140

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

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

146

147 148 149 150 151
    outputSelectAction = Player::playerSelectAction(actionCollection());
    if(outputSelectAction) {
	outputSelectAction->setCurrentItem(0);
	connect(outputSelectAction, SIGNAL(activated(int)), this, SLOT(setOutput(int)));
    }
152

153 154 155 156
    // just in the toolbar
    sliderAction = new SliderAction(i18n("Track Position"), actionCollection(), "trackPositionAction");

    createGUI();
157 158 159 160

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

    connect(splitter, SIGNAL(playlistChanged()), this, SLOT(playlistChanged()));
163 164
}

165
void JuK::setupSystemTray()
166 167 168 169 170
{
    if(toggleSystemTrayAction && toggleSystemTrayAction->isChecked()) {
	systemTray = new SystemTray(this, "systemTray");
	systemTray->show();
	
Scott Wheeler's avatar
Scott Wheeler committed
171 172 173 174 175
	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()));
176 177

	connect(this, SIGNAL(newSongSignal(const QString&)), systemTray, SLOT(slotNewSong(const QString&)));
178
	
179
	if(player && player->paused())
180
	    systemTray->slotPause();
181
	else if(player && player->playing())
182 183 184 185
	    systemTray->slotPlay();
    }
    else
	systemTray = 0;
186 187
}

188 189
void JuK::setupPlayer()
{
190 191
    trackPositionDragging = false;
    noSeek = false;
192 193
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
194 195
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
196

197
    playTimer = new QTimer(this);
198 199 200 201 202 203 204
    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);
205

206 207
        connect(sliderAction->getVolumeSlider(), SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    }
208 209 210 211 212 213 214 215
    
    int playerType = 0;
    if(outputSelectAction) {
	playerType = outputSelectAction->currentItem();
	connect(outputSelectAction, SIGNAL(activated(int)), this, SLOT(setOutput(int)));
    }

    player = Player::createPlayer(playerType);
216 217
}

218

219 220 221 222 223 224 225 226
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
    
    for(int i = 0; i < args->count(); i++)
	files.append(args->arg(i));

227
    splitter->open(files);
228 229
}

230 231 232 233 234 235 236 237 238 239 240 241 242
/**
 * 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);
    }
}

243 244
void JuK::readConfig()
{
245 246 247
    // Automagically save and restore many window settings.
    setAutoSaveSettings();

248 249 250 251 252 253 254
    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);
        }
255 256 257 258
	if(randomPlayAction) {
	    bool randomPlay = config->readBoolEntry("RandomPlay", false);
	    randomPlayAction->setChecked(randomPlay);
	}
259
    }
260
    { // view settings
261
        KConfigGroupSaver saver(config, "View");
262
	bool showEditor = config->readBoolEntry("ShowEditor", false);
263 264 265
	showEditorAction->setChecked(showEditor);
	splitter->setEditorVisible(showEditor);
    }
266 267
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
268 269
	bool dockInSystemTray = config->readBoolEntry("DockInSystemTray", true);
	toggleSystemTrayAction->setChecked(dockInSystemTray);
270 271 272 273
	
	if(outputSelectAction)
	    outputSelectAction->setCurrentItem(config->readNumEntry("MediaSystem", 0));
	
274
    }
275 276 277 278

    if(restoreOnLoadAction)
	restoreOnLoadAction->setChecked(restore);
    
279 280 281 282
}

void JuK::saveConfig()
{
283 284 285 286 287
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider())
            config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
288 289
	if(randomPlayAction)
	    config->writeEntry("RandomPlay", randomPlayAction->isChecked());
290
    }
291 292 293 294
    { // view settings
        KConfigGroupSaver saver(config, "View");
	config->writeEntry("ShowEditor", showEditorAction->isChecked());
    }
295 296 297 298
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	if(restoreOnLoadAction)
	    config->writeEntry("RestoreOnLoad", restoreOnLoadAction->isChecked());
299 300
	if(toggleSystemTrayAction)
	    config->writeEntry("DockInSystemTray", toggleSystemTrayAction->isChecked());
301 302
	if(outputSelectAction)
	    config->writeEntry("MediaSystem", outputSelectAction->currentItem());
303
    }
304 305
}

306 307
bool JuK::queryClose()
{
Scott Wheeler's avatar
Scott Wheeler committed
308
    stop();
309
    delete player;
310
    Cache::instance()->save();
311
    saveConfig();
312
    delete splitter;
313
    return true;
314 315
}

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
void JuK::invokeEditSlot( const char *slotName, const char *slot )
{
    QObject *object = focusWidget();
    
    if(!object || !slotName || !slot)
	return;
    
    QMetaObject *meta = object->metaObject();
    QStrList l = meta->slotNames(true);
  
    if(l.find(slotName) == -1)
	return;
    
    connect(this, SIGNAL( editSignal() ), object, slot);
    emit editSignal();
    disconnect(this, SIGNAL(editSignal()), object, slot);
}

334 335 336 337 338 339 340 341 342 343 344 345
QString JuK::playingString() const
{
    QString s;
    
    if(splitter->playingArtist().isEmpty())
	s = splitter->playingTrack().simplifyWhiteSpace();
    else
	s = splitter->playingArtist().simplifyWhiteSpace() + " - " + splitter->playingTrack().simplifyWhiteSpace();

    return s;
}

346 347 348 349
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

350
void JuK::playlistChanged()
351
{
352
    if(splitter->collectionListSelected()) {
353 354 355 356 357 358 359 360 361 362 363
	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);
    }
364 365 366 367 368 369

    updatePlaylistInfo();
}

void JuK::updatePlaylistInfo()
{
370
    statusLabel->setPlaylistInfo(splitter->selectedPlaylistName(), splitter->selectedPlaylistCount());
371 372
}

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
////////////////////////////////////////////////////////////////////////////////
// edit menu
////////////////////////////////////////////////////////////////////////////////

void JuK::cut()
{
    invokeEditSlot("cut()", SLOT(cut()));
}

void JuK::copy()
{
    invokeEditSlot("copy()", SLOT(copy()));
}

void JuK::paste()
{
    invokeEditSlot("paste()", SLOT(paste()));
}

void JuK::clear()
{
    invokeEditSlot("clear()", SLOT(clear()));
}

void JuK::selectAll()
{
    invokeEditSlot("selectAll()", SLOT(selectAll()));
}

402 403 404 405
////////////////////////////////////////////////////////////////////////////////
// player menu
////////////////////////////////////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
406
void JuK::play()
407
{
408 409 410 411 412
    if(!player)
	return;

    if(player->paused()) {
        player->play();
413 414 415 416

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

417
        if(player->playing()) {
418 419 420
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
421 422
	    if(systemTray)
		systemTray->slotPlay();
423 424
        }
    }
425 426
    else if(player->playing())
	player->seekPosition(0);
427
    else
Scott Wheeler's avatar
Scott Wheeler committed
428
	play(splitter->playNextFile(randomPlayAction->isChecked()));
429 430
}

Scott Wheeler's avatar
Scott Wheeler committed
431
void JuK::pause()
432
{
433 434 435
    if(!player)
	return;

436
    playTimer->stop();
437
    player->pause();
438
    pauseAction->setEnabled(false);
439 440
    if(systemTray)
	systemTray->slotPause();
441 442
}

Scott Wheeler's avatar
Scott Wheeler committed
443
void JuK::stop()
444
{
445 446 447
    if(!player)
	return;

448
    playTimer->stop();
449
    player->stop();
450

451 452
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
453 454
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
455

456 457
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
458 459

    splitter->stop();
460

461
    statusLabel->clear();
462 463 464
    
    if(systemTray)
	systemTray->slotStop();
465 466
}

Scott Wheeler's avatar
Scott Wheeler committed
467
void JuK::back()
468
{
Scott Wheeler's avatar
Scott Wheeler committed
469
    play(splitter->playPreviousFile(randomPlayAction->isChecked()));
470 471
}

Scott Wheeler's avatar
Scott Wheeler committed
472
void JuK::forward()
473
{
Scott Wheeler's avatar
Scott Wheeler committed
474
    play(splitter->playNextFile(randomPlayAction->isChecked()));
475 476
}

477 478 479 480 481 482 483 484 485
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

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

487 488 489 490 491
void JuK::toggleSystemTray(bool enabled)
{
    if(enabled && !systemTray)
	setupSystemTray();
    else if(!enabled && systemTray) {
492
	delete systemTray;
493 494 495 496
	systemTray = 0;
    }
}

497 498
void JuK::setOutput(int output)
{
Scott Wheeler's avatar
Scott Wheeler committed
499
    stop();
500
    delete player;
501 502 503
    player = Player::createPlayer(output);
}

504 505 506 507
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

508 509
void JuK::trackPositionSliderClick()
{
510
    trackPositionDragging = true;
511 512 513 514
}

void JuK::trackPositionSliderRelease()
{
515 516 517
    if(!player)
	return;

518
    trackPositionDragging = false;
519
    player->seekPosition(sliderAction->getTrackPositionSlider()->value());
520 521 522 523
}

void JuK::trackPositionSliderUpdate(int position)
{
524 525 526 527 528
    if(!player)
	return;

    if(player->playing() && !trackPositionDragging && !noSeek)
        player->seekPosition(position);
529 530
}

531 532
// This method is called when the play timer has expired.

533 534
void JuK::pollPlay()
{
535 536 537
    if(!player)
	return;

538 539
    // Our locking mechanism.  Since this method adjusts the play slider, we 
    // want to make sure that our adjustments
540
    noSeek = true;
541

542
    if(!player->playing()) {
543

544
        playTimer->stop();
545

546
	if(!player->paused())
Scott Wheeler's avatar
Scott Wheeler committed
547
	    play(splitter->playNextFile(randomPlayAction->isChecked()));
548

549
    }
550
    else if(!trackPositionDragging) {
551 552 553
        sliderAction->getTrackPositionSlider()->setValue(player->position());
	statusLabel->setItemTotalTime(player->totalTime());
	statusLabel->setItemCurrentTime(player->currentTime());
554 555 556 557 558 559
    }

    // 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.
560

561
    if(player->playing() && player->totalTime() > 0 && float(player->totalTime() - player->currentTime()) < pollInterval * 2)
562
        playTimer->changeInterval(50);
563

564
    noSeek = false;
565 566 567 568
}

void JuK::setVolume(int volume)
{
569
    if(player && sliderAction && sliderAction->getVolumeSlider() &&
570 571
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
572
    {
573
        player->setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
574 575
    }
}
576

Scott Wheeler's avatar
Scott Wheeler committed
577
void JuK::play(const QString &file)
578
{
579 580 581
    if(!player)
	return;

582
    float volume = float(sliderAction->getVolumeSlider()->value()) / float(sliderAction->getVolumeSlider()->maxValue());
583

584 585
    if(player->paused())
	player->stop();
586
    
587
    player->play(file, volume);
588

589
    // Make sure that the player actually starts before doing anything.
590

591
    if(player->playing()) {
592 593 594 595 596 597
	pauseAction->setEnabled(true);
	stopAction->setEnabled(true);
	
	backAction->setEnabled(true);
	forwardAction->setEnabled(true);
	
598
	sliderAction->getTrackPositionSlider()->setValue(0);
599 600
	sliderAction->getTrackPositionSlider()->setEnabled(true);
	playTimer->start(pollInterval);
601

602 603 604
	statusLabel->setPlayingItemInfo(playingString(), splitter->playingList());

	emit newSongSignal(playingString());
605

606 607
	if(systemTray)
	    systemTray->slotPlay();
608
    }
609
    else
Scott Wheeler's avatar
Scott Wheeler committed
610
	stop();
611
}
612

613
#include "juk.moc"