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 14.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
    setupPlayer();
54
    setupSystemTray();
55
    readConfig();
56
    processArgs();
57
	
58
    SplashScreen::finishedLoading();
59 60 61 62
}

JuK::~JuK()
{
63
    delete(playTimer);
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 129 130
    // tagger menu
    new KAction(i18n("Save"), "filesave", 0, splitter, SLOT(saveItem()), actionCollection(), "saveItem");
    new KAction(i18n("Delete"), "editdelete", 0, splitter, SLOT(removeSelectedItems()), actionCollection(), "removeItem");
131
    
132 133
    // settings menu
    restoreOnLoadAction = new KToggleAction(i18n("Restored Playlists on Load"),  0, actionCollection(), "restoreOnLoad"); 
134
    new KAction(i18n("Genre List Editor"), 0, this, SLOT(showGenreListEditor()), actionCollection(), "showGenreListEditor");
135

136
    connect(splitter, SIGNAL(playlistChanged()), this, SLOT(playlistChanged()));
137

138

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

    createGUI();
143 144 145 146

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

149 150 151 152 153 154 155 156 157 158 159 160
void JuK::setupSystemTray()
{	
    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()));
}

161 162
void JuK::setupPlayer()
{
163 164
    trackPositionDragging = false;
    noSeek = false;
165 166
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
167 168
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
169

170
    playTimer = new QTimer(this);
171 172 173 174 175 176 177
    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);
178

179 180
        connect(sliderAction->getVolumeSlider(), SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    }
181 182
}

183

184 185 186 187 188 189 190 191
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
    
    for(int i = 0; i < args->count(); i++)
	files.append(args->arg(i));

192
    splitter->open(files);
193 194
}

195 196
void JuK::readConfig()
{
197 198 199
    // Automagically save and restore many window settings.
    setAutoSaveSettings();

200 201 202 203 204 205 206
    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);
        }
207 208 209 210
	if(randomPlayAction) {
	    bool randomPlay = config->readBoolEntry("RandomPlay", false);
	    randomPlayAction->setChecked(randomPlay);
	}
211
    }
212
    { // view settings
213
        KConfigGroupSaver saver(config, "View");
214
	bool showEditor = config->readBoolEntry("ShowEditor", false);
215 216 217
	showEditorAction->setChecked(showEditor);
	splitter->setEditorVisible(showEditor);
    }
218 219 220 221 222 223 224 225 226 227 228 229

    if(restoreOnLoadAction)
	restoreOnLoadAction->setChecked(restore);
}

void JuK::readSettings()
{
    KConfig *config = KGlobal::config();
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	restore = config->readBoolEntry("RestoreOnLoad", true);
    }
230 231 232 233
}

void JuK::saveConfig()
{
234 235 236 237 238
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider())
            config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
239 240
	if(randomPlayAction)
	    config->writeEntry("RandomPlay", randomPlayAction->isChecked());
241
    }
242 243 244 245
    { // view settings
        KConfigGroupSaver saver(config, "View");
	config->writeEntry("ShowEditor", showEditorAction->isChecked());
    }
246 247 248 249 250
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	if(restoreOnLoadAction)
	    config->writeEntry("RestoreOnLoad", restoreOnLoadAction->isChecked());
    }
251 252
}

253 254
bool JuK::queryClose()
{
255
    Cache::instance()->save();
256
    saveConfig();
257
    delete(splitter);
258 259 260
    return(true);
}

261 262 263 264
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

265
void JuK::playlistChanged()
266
{
267
    if(splitter->collectionListSelected()) {
268 269 270 271 272 273 274 275 276 277 278
	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);
    }
279 280 281 282 283 284

    updatePlaylistInfo();
}

void JuK::updatePlaylistInfo()
{
285
    statusLabel->setPlaylistInfo(splitter->selectedPlaylistName(), splitter->selectedPlaylistCount());
286 287 288 289 290 291 292 293
}

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

void JuK::playFile()
{
294 295
    if(player.paused()) {
        player.play();
296 297 298 299

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

300 301 302 303
        if(player.playing()) {
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
304
	    systemTray->slotPlay();
305 306
        }
    }
307 308
    else if(player.playing())
	player.seekPosition(0);
309 310
    else
	playFile(splitter->playNextFile(randomPlayAction->isChecked()));
311 312 313 314
}

void JuK::pauseFile()
{
315 316 317
    playTimer->stop();
    player.pause();
    pauseAction->setEnabled(false);
318
    systemTray->slotPause();
319 320 321 322
}

void JuK::stopFile()
{
323 324
    playTimer->stop();
    player.stop();
325

326 327
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
328 329
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
330

331 332
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
333 334

    splitter->stop();
335

336
    statusLabel->clear();
337 338

    systemTray->slotStop();
339 340
}

341 342
void JuK::backFile()
{
343
    playFile(splitter->playPreviousFile(randomPlayAction->isChecked()));
344 345 346 347
}

void JuK::forwardFile()
{
348
    playFile(splitter->playNextFile(randomPlayAction->isChecked()));
349 350
}

351 352 353 354 355 356 357 358 359
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

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

361 362 363 364
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

365 366
void JuK::trackPositionSliderClick()
{
367
    trackPositionDragging = true;
368 369 370 371
}

void JuK::trackPositionSliderRelease()
{
372
    trackPositionDragging = false;
373
    player.seekPosition(sliderAction->getTrackPositionSlider()->value());
374 375 376 377
}

void JuK::trackPositionSliderUpdate(int position)
{
378
    if(player.playing() && !trackPositionDragging && !noSeek)
379
        player.seekPosition(position);
380 381
}

382 383
// This method is called when the play timer has expired.

384 385
void JuK::pollPlay()
{
386 387
    // Our locking mechanism.  Since this method adjusts the play slider, we 
    // want to make sure that our adjustments
388
    noSeek = true;
389

390
    if(!player.playing()) {
391

392
        playTimer->stop();
393

394 395 396
	if(!player.paused())
	    playFile(splitter->playNextFile(randomPlayAction->isChecked()));

397
    }
398
    else if(!trackPositionDragging) {
399
        sliderAction->getTrackPositionSlider()->setValue(player.position());
400 401 402 403 404 405 406 407
	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.
408

409
    if(player.playing() && player.totalTime() > 0 && float(player.totalTime() - player.currentTime()) < pollInterval * 2)
410
        playTimer->changeInterval(50);
411

412
    noSeek = false;
413 414 415 416
}

void JuK::setVolume(int volume)
{
417 418 419
    if(sliderAction && sliderAction->getVolumeSlider() &&
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
420
    {
421
        player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
422 423
    }
}
424

425
void JuK::playFile(const QString &file)
426
{
427
    float volume = float(sliderAction->getVolumeSlider()->value()) / float(sliderAction->getVolumeSlider()->maxValue());
428 429 430

    if(player.paused())
	player.stop();
431 432
    
    player.play(file, volume);
433

434
    // Make sure that the player actually starts before doing anything.
435

436 437 438 439 440 441 442
    if(player.playing()) {
	pauseAction->setEnabled(true);
	stopAction->setEnabled(true);
	
	backAction->setEnabled(true);
	forwardAction->setEnabled(true);
	
443
	sliderAction->getTrackPositionSlider()->setValue(0);
444 445
	sliderAction->getTrackPositionSlider()->setEnabled(true);
	playTimer->start(pollInterval);
446

447
	statusLabel->setPlayingItemInfo(splitter->playingTrack(), splitter->playingArtist(), splitter->playingList());
448 449

	systemTray->slotPlay();
450
    }
451 452
    else
	stopFile();
453
}
454

455
#include "juk.moc"