juk.cpp 15.9 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 54
    setupPlayer();
    readConfig();
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 101 102 103 104 105 106

    renamePlaylistAction = new KAction(i18n("Rename..."), 0, splitter, SLOT(renamePlaylist()), 
				       actionCollection(), "renamePlaylist");
    new KAction(i18n("Duplicate..."), "editcopy", 0, splitter, SLOT(duplicatePlaylist()), actionCollection(), "duplicatePlaylist");

    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");
122
    playAction = new KAction(i18n("&Play"), "player_play", 0, this, SLOT(playFile()), actionCollection(), "playFile");
123 124
    pauseAction = new KAction(i18n("P&ause"), "player_pause", 0, this, SLOT(pauseFile()), actionCollection(), "pauseFile");
    stopAction = new KAction(i18n("&Stop"), "player_stop", 0, this, SLOT(stopFile()), actionCollection(), "stopFile");
125 126
    backAction = new KAction(i18n("Skip &Back"), "player_start", 0, this, SLOT(backFile()), actionCollection(), "backFile");
    forwardAction = new KAction(i18n("Skip &Forward"), "player_end", 0, this, SLOT(forwardFile()), actionCollection(), "forwardFile");
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
133 134 135
    restoreOnLoadAction = new KToggleAction(i18n("Restored playlists on load"),  0, actionCollection(), "restoreOnLoad");
    toggleSystemTrayAction = new KToggleAction(i18n("Dock in system tray"), 0, actionCollection(), "toggleSystemTray");
    connect(toggleSystemTrayAction, SIGNAL(toggled(bool)), this, SLOT(toggleSystemTray(bool)));
136
    new KAction(i18n("Genre List Editor"), 0, this, SLOT(showGenreListEditor()), actionCollection(), "showGenreListEditor");
137

138
    connect(splitter, SIGNAL(playlistChanged()), this, SLOT(playlistChanged()));
139

140

141 142 143 144
    // just in the toolbar
    sliderAction = new SliderAction(i18n("Track Position"), actionCollection(), "trackPositionAction");

    createGUI();
145 146 147 148

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

151
void JuK::setupSystemTray()
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
{
    if(toggleSystemTrayAction && toggleSystemTrayAction->isChecked()) {
	systemTray = new SystemTray(this, "systemTray");
	systemTray->show();
	
	connect(systemTray, SIGNAL(play()),    this, SLOT(playFile()));
	connect(systemTray, SIGNAL(stop()),    this, SLOT(stopFile()));
	connect(systemTray, SIGNAL(pause()),   this, SLOT(pauseFile()));
	connect(systemTray, SIGNAL(back()),    this, SLOT(backFile()));
	connect(systemTray, SIGNAL(forward()), this, SLOT(forwardFile()));
	
	if(player.paused())
	    systemTray->slotPause();
	else if(player.playing())
	    systemTray->slotPlay();
    }
    else
	systemTray = 0;
170 171
}

172 173
void JuK::setupPlayer()
{
174 175
    trackPositionDragging = false;
    noSeek = false;
176 177
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
178 179
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
180

181
    playTimer = new QTimer(this);
182 183 184 185 186 187 188
    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);
189

190 191
        connect(sliderAction->getVolumeSlider(), SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    }
192 193
}

194

195 196 197 198 199 200 201 202
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
    
    for(int i = 0; i < args->count(); i++)
	files.append(args->arg(i));

203
    splitter->open(files);
204 205
}

206 207 208 209 210 211 212 213 214 215 216 217 218
/**
 * 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);
    }
}

219 220
void JuK::readConfig()
{
221 222 223
    // Automagically save and restore many window settings.
    setAutoSaveSettings();

224 225 226 227 228 229 230
    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);
        }
231 232 233 234
	if(randomPlayAction) {
	    bool randomPlay = config->readBoolEntry("RandomPlay", false);
	    randomPlayAction->setChecked(randomPlay);
	}
235
    }
236
    { // view settings
237
        KConfigGroupSaver saver(config, "View");
238
	bool showEditor = config->readBoolEntry("ShowEditor", false);
239 240 241
	showEditorAction->setChecked(showEditor);
	splitter->setEditorVisible(showEditor);
    }
242 243
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
244 245
	bool dockInSystemTray = config->readBoolEntry("DockInSystemTray", true);
	toggleSystemTrayAction->setChecked(dockInSystemTray);
246
    }
247 248 249 250

    if(restoreOnLoadAction)
	restoreOnLoadAction->setChecked(restore);
    
251 252 253 254
}

void JuK::saveConfig()
{
255 256 257 258 259
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider())
            config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
260 261
	if(randomPlayAction)
	    config->writeEntry("RandomPlay", randomPlayAction->isChecked());
262
    }
263 264 265 266
    { // view settings
        KConfigGroupSaver saver(config, "View");
	config->writeEntry("ShowEditor", showEditorAction->isChecked());
    }
267 268 269 270
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	if(restoreOnLoadAction)
	    config->writeEntry("RestoreOnLoad", restoreOnLoadAction->isChecked());
271 272
	if(toggleSystemTrayAction)
	    config->writeEntry("DockInSystemTray", toggleSystemTrayAction->isChecked());
273
    }
274 275
}

276 277
bool JuK::queryClose()
{
278
    Cache::instance()->save();
279
    saveConfig();
280
    delete(splitter);
281 282 283
    return(true);
}

284 285 286 287
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

288
void JuK::playlistChanged()
289
{
290
    if(splitter->collectionListSelected()) {
291 292 293 294 295 296 297 298 299 300 301
	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);
    }
302 303 304 305 306 307

    updatePlaylistInfo();
}

void JuK::updatePlaylistInfo()
{
308
    statusLabel->setPlaylistInfo(splitter->selectedPlaylistName(), splitter->selectedPlaylistCount());
309 310 311 312 313 314 315 316
}

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

void JuK::playFile()
{
317 318
    if(player.paused()) {
        player.play();
319 320 321 322

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

323 324 325 326
        if(player.playing()) {
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
327 328
	    if(systemTray)
		systemTray->slotPlay();
329 330
        }
    }
331 332
    else if(player.playing())
	player.seekPosition(0);
333 334
    else
	playFile(splitter->playNextFile(randomPlayAction->isChecked()));
335 336 337 338
}

void JuK::pauseFile()
{
339 340 341
    playTimer->stop();
    player.pause();
    pauseAction->setEnabled(false);
342 343
    if(systemTray)
	systemTray->slotPause();
344 345 346 347
}

void JuK::stopFile()
{
348 349
    playTimer->stop();
    player.stop();
350

351 352
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
353 354
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
355

356 357
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
358 359

    splitter->stop();
360

361
    statusLabel->clear();
362 363 364
    
    if(systemTray)
	systemTray->slotStop();
365 366
}

367 368
void JuK::backFile()
{
369
    playFile(splitter->playPreviousFile(randomPlayAction->isChecked()));
370 371 372 373
}

void JuK::forwardFile()
{
374
    playFile(splitter->playNextFile(randomPlayAction->isChecked()));
375 376
}

377 378 379 380 381 382 383 384 385
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

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

387 388 389 390 391 392 393 394 395 396
void JuK::toggleSystemTray(bool enabled)
{
    if(enabled && !systemTray)
	setupSystemTray();
    else if(!enabled && systemTray) {
	delete(systemTray);
	systemTray = 0;
    }
}

397 398 399 400
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

401 402
void JuK::trackPositionSliderClick()
{
403
    trackPositionDragging = true;
404 405 406 407
}

void JuK::trackPositionSliderRelease()
{
408
    trackPositionDragging = false;
409
    player.seekPosition(sliderAction->getTrackPositionSlider()->value());
410 411 412 413
}

void JuK::trackPositionSliderUpdate(int position)
{
414
    if(player.playing() && !trackPositionDragging && !noSeek)
415
        player.seekPosition(position);
416 417
}

418 419
// This method is called when the play timer has expired.

420 421
void JuK::pollPlay()
{
422 423
    // Our locking mechanism.  Since this method adjusts the play slider, we 
    // want to make sure that our adjustments
424
    noSeek = true;
425

426
    if(!player.playing()) {
427

428
        playTimer->stop();
429

430 431 432
	if(!player.paused())
	    playFile(splitter->playNextFile(randomPlayAction->isChecked()));

433
    }
434
    else if(!trackPositionDragging) {
435
        sliderAction->getTrackPositionSlider()->setValue(player.position());
436 437 438 439 440 441 442 443
	statusLabel->setItemTotalTime(player.totalTime());
	statusLabel->setItemCurrentTime(player.currentTime());
    }

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

445
    if(player.playing() && player.totalTime() > 0 && float(player.totalTime() - player.currentTime()) < pollInterval * 2)
446
        playTimer->changeInterval(50);
447

448
    noSeek = false;
449 450 451 452
}

void JuK::setVolume(int volume)
{
453 454 455
    if(sliderAction && sliderAction->getVolumeSlider() &&
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
456
    {
457
        player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
458 459
    }
}
460

461
void JuK::playFile(const QString &file)
462
{
463
    float volume = float(sliderAction->getVolumeSlider()->value()) / float(sliderAction->getVolumeSlider()->maxValue());
464 465 466

    if(player.paused())
	player.stop();
467 468
    
    player.play(file, volume);
469

470
    // Make sure that the player actually starts before doing anything.
471

472 473 474 475 476 477 478
    if(player.playing()) {
	pauseAction->setEnabled(true);
	stopAction->setEnabled(true);
	
	backAction->setEnabled(true);
	forwardAction->setEnabled(true);
	
479
	sliderAction->getTrackPositionSlider()->setValue(0);
480 481
	sliderAction->getTrackPositionSlider()->setEnabled(true);
	playTimer->start(pollInterval);
482

483
	statusLabel->setPlayingItemInfo(splitter->playingTrack(), splitter->playingArtist(), splitter->playingList());
484

485 486
	if(systemTray)
	    systemTray->slotPlay();
487
    }
488 489
    else
	stopFile();
490
}
491

492
#include "juk.moc"