Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

juk.cpp 16.2 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
    delete(player);
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
{
    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()));
	
163
	if(player && player->paused())
164
	    systemTray->slotPause();
165
	else if(player && player->playing())
166 167 168 169
	    systemTray->slotPlay();
    }
    else
	systemTray = 0;
170 171
}

172 173
void JuK::setupPlayer()
{
174 175
    player = Player::createPlayer();

176 177
    trackPositionDragging = false;
    noSeek = false;
178 179
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
180 181
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
182

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

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

196

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

205
    splitter->open(files);
206 207
}

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

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

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

    if(restoreOnLoadAction)
	restoreOnLoadAction->setChecked(restore);
    
253 254 255 256
}

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

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

286 287 288 289
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

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

    updatePlaylistInfo();
}

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

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

void JuK::playFile()
{
319 320 321 322 323
    if(!player)
	return;

    if(player->paused()) {
        player->play();
324 325 326 327

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

328
        if(player->playing()) {
329 330 331
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
332 333
	    if(systemTray)
		systemTray->slotPlay();
334 335
        }
    }
336 337
    else if(player->playing())
	player->seekPosition(0);
338 339
    else
	playFile(splitter->playNextFile(randomPlayAction->isChecked()));
340 341 342 343
}

void JuK::pauseFile()
{
344 345 346
    if(!player)
	return;

347
    playTimer->stop();
348
    player->pause();
349
    pauseAction->setEnabled(false);
350 351
    if(systemTray)
	systemTray->slotPause();
352 353 354 355
}

void JuK::stopFile()
{
356 357 358
    if(!player)
	return;

359
    playTimer->stop();
360
    player->stop();
361

362 363
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
364 365
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
366

367 368
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
369 370

    splitter->stop();
371

372
    statusLabel->clear();
373 374 375
    
    if(systemTray)
	systemTray->slotStop();
376 377
}

378 379
void JuK::backFile()
{
380
    playFile(splitter->playPreviousFile(randomPlayAction->isChecked()));
381 382 383 384
}

void JuK::forwardFile()
{
385
    playFile(splitter->playNextFile(randomPlayAction->isChecked()));
386 387
}

388 389 390 391 392 393 394 395 396
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

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

398 399 400 401 402 403 404 405 406 407
void JuK::toggleSystemTray(bool enabled)
{
    if(enabled && !systemTray)
	setupSystemTray();
    else if(!enabled && systemTray) {
	delete(systemTray);
	systemTray = 0;
    }
}

408 409 410 411
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

412 413
void JuK::trackPositionSliderClick()
{
414
    trackPositionDragging = true;
415 416 417 418
}

void JuK::trackPositionSliderRelease()
{
419 420 421
    if(!player)
	return;

422
    trackPositionDragging = false;
423
    player->seekPosition(sliderAction->getTrackPositionSlider()->value());
424 425 426 427
}

void JuK::trackPositionSliderUpdate(int position)
{
428 429 430 431 432
    if(!player)
	return;

    if(player->playing() && !trackPositionDragging && !noSeek)
        player->seekPosition(position);
433 434
}

435 436
// This method is called when the play timer has expired.

437 438
void JuK::pollPlay()
{
439 440 441
    if(!player)
	return;

442 443
    // Our locking mechanism.  Since this method adjusts the play slider, we 
    // want to make sure that our adjustments
444
    noSeek = true;
445

446
    if(!player->playing()) {
447

448
        playTimer->stop();
449

450
	if(!player->paused())
451 452
	    playFile(splitter->playNextFile(randomPlayAction->isChecked()));

453
    }
454
    else if(!trackPositionDragging) {
455 456 457
        sliderAction->getTrackPositionSlider()->setValue(player->position());
	statusLabel->setItemTotalTime(player->totalTime());
	statusLabel->setItemCurrentTime(player->currentTime());
458 459 460 461 462 463
    }

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

465
    if(player->playing() && player->totalTime() > 0 && float(player->totalTime() - player->currentTime()) < pollInterval * 2)
466
        playTimer->changeInterval(50);
467

468
    noSeek = false;
469 470 471 472
}

void JuK::setVolume(int volume)
{
473
    if(player && sliderAction && sliderAction->getVolumeSlider() &&
474 475
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
476
    {
477
        player->setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
478 479
    }
}
480

481
void JuK::playFile(const QString &file)
482
{
483 484 485
    if(!player)
	return;

486
    float volume = float(sliderAction->getVolumeSlider()->value()) / float(sliderAction->getVolumeSlider()->maxValue());
487

488 489
    if(player->paused())
	player->stop();
490
    
491
    player->play(file, volume);
492

493
    // Make sure that the player actually starts before doing anything.
494

495
    if(player->playing()) {
496 497 498 499 500 501
	pauseAction->setEnabled(true);
	stopAction->setEnabled(true);
	
	backAction->setEnabled(true);
	forwardAction->setEnabled(true);
	
502
	sliderAction->getTrackPositionSlider()->setValue(0);
503 504
	sliderAction->getTrackPositionSlider()->setEnabled(true);
	playTimer->start(pollInterval);
505

506
	statusLabel->setPlayingItemInfo(splitter->playingTrack(), splitter->playingArtist(), splitter->playingList());
507

508 509
	if(systemTray)
	    systemTray->slotPlay();
510
    }
511 512
    else
	stopFile();
513
}
514

515
#include "juk.moc"