juk.cpp 16.1 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 <qinputdialog.h>

28
#include "juk.h"
29 30 31
#include "playlist.h"
#include "playlistsplitter.h"
#include "collectionlist.h"
32
#include "slideraction.h"
33
#include "cache.h"
34
#include "statuslabel.h"
35 36
#include "splashscreen.h"
#include "genrelisteditor.h"
37 38 39 40 41

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

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

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

    SplashScreen::finishedLoading();
57 58 59 60
}

JuK::~JuK()
{
61
    delete(playTimer);
62 63 64 65 66 67
}

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

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

    // playlist item activation connection
    connect(splitter, SIGNAL(playlistDoubleClicked(QListViewItem *)), this, SLOT(playItem(QListViewItem *)));

76
    // create status bar
77
    statusLabel = new StatusLabel(statusBar());
78 79
    statusBar()->addWidget(statusLabel, 1);

80 81
    connect(splitter, SIGNAL(selectedPlaylistCountChanged(int)), statusLabel, SLOT(setPlaylistCount(int)));

82 83 84
    splitter->setFocus();
}

85 86
void JuK::setupActions()
{
87
    // file menu
88 89 90 91
    KStdAction::open(splitter, SLOT(open()), actionCollection());
    new KAction(i18n("Open &Directory..."), "fileopen", 0, splitter, SLOT(openDirectory()), actionCollection(), "openDirectory");
    KStdAction::save(splitter, SLOT(save()), actionCollection());
    new KAction(i18n("Delete"), "editdelete", 0, this, SLOT(remove()), actionCollection(), "remove");
92 93 94 95 96 97
    KStdAction::quit(this, SLOT(quit()), actionCollection());

    // edit menu
    KStdAction::cut(this, SLOT(cut()), actionCollection());
    KStdAction::copy(this, SLOT(copy()), actionCollection());
    KStdAction::paste(this, SLOT(paste()), actionCollection());
98
    KStdAction::selectAll(splitter, SLOT(selectAll()), actionCollection());
99

100 101
    // view menu
    showEditorAction = new KToggleAction(i18n("Show Tag Editor"), 0, actionCollection(), "showEditor");
102 103
    connect(showEditorAction, SIGNAL(toggled(bool)), splitter, SLOT(setEditorVisible(bool)));
    KStdAction::redisplay(splitter, SLOT(refresh()), actionCollection());
104

105
    // play menu
106
    randomPlayAction = new KToggleAction(i18n("Random Play"), 0, actionCollection(), "randomPlay");
107
    playAction = new KAction(i18n("&Play"), "player_play", 0, this, SLOT(playFile()), actionCollection(), "playFile");
108 109
    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");
110 111
    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");
112

113
    // playlist menu
114 115 116 117 118 119 120 121 122
    new KAction(i18n("New..."), "filenew", 0, splitter, SLOT(createPlaylist()), actionCollection(), "createPlaylist");
    new KAction(i18n("Open..."), "fileopen", 0, splitter, SLOT(openPlaylist()), actionCollection(), "openPlaylist");

    savePlaylistAction = new KAction(i18n("Save"), "filesave", 0, splitter, SLOT(savePlaylist()), actionCollection(), "savePlaylist");
    saveAsPlaylistAction = new KAction(i18n("Save As..."), "filesaveas", 0, splitter, SLOT(saveAsPlaylist()), 
				       actionCollection(), "saveAsPlaylist");
    renamePlaylistAction = new KAction(i18n("Rename..."), 0, splitter, SLOT(renamePlaylist()), 
				       actionCollection(), "renamePlaylist");
    new KAction(i18n("Duplicate..."), "editcopy", 0, splitter, SLOT(duplicatePlaylist()), actionCollection(), "duplicatePlaylist");
123 124
    deleteItemPlaylistAction = new KAction(i18n("Delete"), "editdelete", 0, splitter, SLOT(deleteItemPlaylist()), 
					   actionCollection(), "deleteItemPlaylist");
125
    
126 127
    // settings menu
    restoreOnLoadAction = new KToggleAction(i18n("Restored Playlists on Load"),  0, actionCollection(), "restoreOnLoad"); 
128
    new KAction(i18n("Genre List Editor"), 0, this, SLOT(showGenreListEditor()), actionCollection(), "showGenreListEditor");
129

130 131 132
    playlistChanged(0);
    connect(splitter, SIGNAL(playlistChanged(Playlist *)), this, SLOT(playlistChanged(Playlist *)));

133

134 135 136 137
    // just in the toolbar
    sliderAction = new SliderAction(i18n("Track Position"), actionCollection(), "trackPositionAction");

    createGUI();
138 139 140 141

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

void JuK::setupPlayer()
{
146 147
    playingItem = 0;

148 149
    trackPositionDragging = false;
    noSeek = false;
150 151
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
152 153
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
154

155
    playTimer = new QTimer(this);
156 157 158 159 160 161 162
    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);
163

164 165
        connect(sliderAction->getVolumeSlider(), SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    }
166 167
}

168

169 170 171 172 173 174 175 176
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
    
    for(int i = 0; i < args->count(); i++)
	files.append(args->arg(i));

177
    splitter->open(files);
178 179
}

180 181
void JuK::readConfig()
{
182 183 184
    // Automagically save and restore many window settings.
    setAutoSaveSettings();

185 186 187 188 189 190 191
    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);
        }
192 193 194 195
	if(randomPlayAction) {
	    bool randomPlay = config->readBoolEntry("RandomPlay", false);
	    randomPlayAction->setChecked(randomPlay);
	}
196
    }
197
    { // view settings
198 199 200 201 202
        KConfigGroupSaver saver(config, "View");
	bool showEditor = config->readBoolEntry("ShowEditor", true);
	showEditorAction->setChecked(showEditor);
	splitter->setEditorVisible(showEditor);
    }
203 204 205 206 207 208 209 210 211 212 213 214

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

void JuK::readSettings()
{
    KConfig *config = KGlobal::config();
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	restore = config->readBoolEntry("RestoreOnLoad", true);
    }
215 216 217 218
}

void JuK::saveConfig()
{
219 220 221 222 223
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider())
            config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
224 225
	if(randomPlayAction)
	    config->writeEntry("RandomPlay", randomPlayAction->isChecked());
226
    }
227 228 229 230
    { // view settings
        KConfigGroupSaver saver(config, "View");
	config->writeEntry("ShowEditor", showEditorAction->isChecked());
    }
231 232 233 234 235
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	if(restoreOnLoadAction)
	    config->writeEntry("RestoreOnLoad", restoreOnLoadAction->isChecked());
    }
236 237
}

238 239
bool JuK::queryClose()
{
240
    Cache::instance()->save();
241
    saveConfig();
242
    delete(splitter);
243 244 245
    return(true);
}

246 247 248 249
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

250
void JuK::playlistChanged(Playlist *list)
251
{
252 253 254 255 256 257 258 259 260 261 262 263
    if(!list || list == CollectionList::instance()) {
	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);
    }
264 265 266 267 268 269 270 271

    updatePlaylistInfo();
}

void JuK::updatePlaylistInfo()
{
    statusLabel->setPlaylistName(splitter->selectedPlaylistName());
    statusLabel->setPlaylistCount(splitter->selectedPlaylistCount());
272 273
}

274 275 276
////////////////////////////////////////////////////////////////////////////////
// private action slot implementations - file menu
////////////////////////////////////////////////////////////////////////////////
277

278
void JuK::remove()
279
{
280
    PlaylistItemList items(splitter->playlistSelection());
281
    PlaylistItem *item = items.first();
282
    while(item) {
283 284 285
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
286
    }
287 288
    
    splitter->remove();
289 290
}

291 292
void JuK::quit()
{
293 294 295
    // delete(this);
    // kapp->quit();
    close();
296 297
}

298 299 300 301 302 303
////////////////////////////////////////////////////////////////////////////////
// edit menu
////////////////////////////////////////////////////////////////////////////////

void JuK::cut()
{
304
    PlaylistItemList items = splitter->playlistSelection();
Scott Wheeler's avatar
Scott Wheeler committed
305

306 307 308 309 310
    PlaylistItem *item = items.first();
    while(item) {
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
311 312
    }

313
    splitter->clearSelectedItems();
314 315
}

316 317 318 319 320 321
////////////////////////////////////////////////////////////////////////////////
// player menu
////////////////////////////////////////////////////////////////////////////////

void JuK::playFile()
{
322 323
    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 329 330 331 332 333
        if(player.playing()) {
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
        }
    }
334 335
    else if(player.playing())
	player.seekPosition(0);
336
    else if(splitter) {
337
        PlaylistItemList items = splitter->playlistSelection();
338
        if(items.count() > 0)
339
            playItem(items.at(0));
340
        else
341
            playItem(splitter->playlistFirstItem());
342
    }
343 344 345 346
}

void JuK::pauseFile()
{
347 348 349
    playTimer->stop();
    player.pause();
    pauseAction->setEnabled(false);
350 351 352 353
}

void JuK::stopFile()
{
354 355 356 357
    playTimer->stop();
    player.stop();
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
358 359
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
360 361
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
362 363
    if(playingItem)
        playingItem->setPixmap(0, 0);
364
    playingItem = 0;
365

366
    statusLabel->clear();
367 368
}

369 370 371 372 373 374 375 376 377 378 379 380 381
void JuK::backFile()
{
    PlaylistItem *i = Playlist::previousItem(playingItem, randomPlayAction->isChecked());
    if(i)
	playItem(i);
}

void JuK::forwardFile()
{
    PlaylistItem *i = Playlist::nextItem(playingItem, randomPlayAction->isChecked());
    playItem(i);
}

382 383 384 385 386 387 388 389 390
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

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

392 393 394 395
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

396 397
void JuK::trackPositionSliderClick()
{
398
    trackPositionDragging = true;
399 400 401 402
}

void JuK::trackPositionSliderRelease()
{
403
    trackPositionDragging = false;
404
    player.seekPosition(sliderAction->getTrackPositionSlider()->value());
405 406 407 408
}

void JuK::trackPositionSliderUpdate(int position)
{
409
    if(player.playing() && !trackPositionDragging && !noSeek)
410
        player.seekPosition(position);
411 412 413 414
}

void JuK::pollPlay()
{
415 416
    noSeek = true;
    if(!player.playing()) {
417

418
        playTimer->stop();
419

420
        if(player.paused())
421
            pauseFile();
422
        else if(playingItem) {
423

424
	    PlaylistItem *next = Playlist::nextItem(playingItem, randomPlayAction->isChecked());
425 426
	    playingItem->setPixmap(0, 0);

427 428
	    if(next) {
		playingItem = next;
429 430 431
		
		backAction->setEnabled(true);
		
432 433 434 435 436 437 438
		sliderAction->getTrackPositionSlider()->setValue(0);
		player.play(playingItem->absFilePath(), player.getVolume());
		if(player.playing()) {
		    playTimer->start(pollInterval);
		    playingItem->setPixmap(0, QPixmap(UserIcon("playing")));
		}
	    }
439
	    statusLabel->setPlayingItem(playingItem);
440 441 442
	}
	else
	    stopFile();
443
    }
444
    else if(!trackPositionDragging) {
445
        sliderAction->getTrackPositionSlider()->setValue(player.position());
446 447 448 449 450 451 452 453
	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.
454

455 456
    if(player.playing() && float(player.totalTime() - player.currentTime()) < pollInterval * 2)
        playTimer->changeInterval(50);
457

458
    noSeek = false;
459 460 461 462
}

void JuK::setVolume(int volume)
{
463 464 465
    if(sliderAction && sliderAction->getVolumeSlider() &&
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
466
    {
467
        player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
468 469
    }
}
470 471 472

void JuK::playItem(QListViewItem *item)
{
473
    playItem(dynamic_cast<PlaylistItem *>(item));
474 475
}

476
void JuK::playItem(PlaylistItem *item)
477
{
478 479 480 481 482
    if(player.playing() || player.paused())
        stopFile();

    if(item) {
        float volume = float(sliderAction->getVolumeSlider()->value()) / float(sliderAction->getVolumeSlider()->maxValue());
483 484 485 486
        player.play(item->absFilePath(), volume);

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

487 488 489
        if(player.playing()) {
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
490 491 492 493 494 495

	    backAction->setEnabled(true);
	    forwardAction->setEnabled(true);

	    playingItem = item;

496
            sliderAction->getTrackPositionSlider()->setEnabled(true);
497
            item->setPixmap(0, QPixmap(UserIcon("playing")));
498
            playTimer->start(pollInterval);
499

500
	    statusLabel->setPlayingItem(item);
501
        }
502 503
    }
}
504

505
#include "juk.moc"