juk.cpp 15.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 18

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

#include <klocale.h>
19
#include <kiconloader.h>
20
#include <kcmdlineargs.h>
21
#include <kstatusbar.h>
22 23
#include <kdebug.h>

24 25
#include <qinputdialog.h>

26
#include "juk.h"
27 28 29
#include "playlist.h"
#include "playlistsplitter.h"
#include "collectionlist.h"
30
#include "slideraction.h"
31
#include "cache.h"
32
#include "statuslabel.h"
33 34 35 36 37

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

38
JuK::JuK(QWidget *parent, const char *name) : KMainWindow(parent, name, WDestructiveClose)
39
{
40 41
    // Expect segfaults if you change this order.

42
    readSettings();
43
    setupLayout();
44
    setupActions();
45 46
    setupPlayer();
    readConfig();
47
    processArgs();
48 49 50 51
}

JuK::~JuK()
{
52
    delete(playTimer);
53 54 55 56 57 58
}

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

59 60
void JuK::setupLayout()
{
61
    splitter = new PlaylistSplitter(this, restore, "playlistSplitter");
62 63 64 65 66
    setCentralWidget(splitter);

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

67
    // create status bar
68
    statusLabel = new StatusLabel(statusBar());
69 70
    statusBar()->addWidget(statusLabel, 1);

71 72 73
    splitter->setFocus();
}

74 75
void JuK::setupActions()
{
76
    // file menu
77 78 79 80
    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");
81 82 83 84 85 86
    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());
87
    KStdAction::selectAll(splitter, SLOT(selectAll()), actionCollection());
88

89 90
    // view menu
    showEditorAction = new KToggleAction(i18n("Show Tag Editor"), 0, actionCollection(), "showEditor");
91 92
    connect(showEditorAction, SIGNAL(toggled(bool)), splitter, SLOT(setEditorVisible(bool)));
    KStdAction::redisplay(splitter, SLOT(refresh()), actionCollection());
93

94
    // play menu
95
    randomPlayAction = new KToggleAction(i18n("Random Play"), 0, actionCollection(), "randomPlay");
96
    playAction = new KAction(i18n("&Play"), "player_play", 0, this, SLOT(playFile()), actionCollection(), "playFile");
97 98
    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");
99 100
    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");
101

102
    // playlist menu
103 104 105 106 107 108 109 110 111
    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");
112 113
    deleteItemPlaylistAction = new KAction(i18n("Delete"), "editdelete", 0, splitter, SLOT(deleteItemPlaylist()), 
					   actionCollection(), "deleteItemPlaylist");
114
    
115 116 117
    // settings menu
    restoreOnLoadAction = new KToggleAction(i18n("Restored Playlists on Load"),  0, actionCollection(), "restoreOnLoad"); 

118 119 120
    playlistChanged(0);
    connect(splitter, SIGNAL(playlistChanged(Playlist *)), this, SLOT(playlistChanged(Playlist *)));

121

122 123 124 125
    // just in the toolbar
    sliderAction = new SliderAction(i18n("Track Position"), actionCollection(), "trackPositionAction");

    createGUI();
126 127 128 129

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

void JuK::setupPlayer()
{
134 135
    playingItem = 0;

136 137
    trackPositionDragging = false;
    noSeek = false;
138 139
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
140 141
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
142

143
    playTimer = new QTimer(this);
144 145 146 147 148 149 150
    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);
151

152 153
        connect(sliderAction->getVolumeSlider(), SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
    }
154 155
}

156

157 158 159 160 161 162 163 164
void JuK::processArgs()
{
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    QStringList files;
    
    for(int i = 0; i < args->count(); i++)
	files.append(args->arg(i));

165
    splitter->open(files);
166 167
}

168 169
void JuK::readConfig()
{
170 171 172
    // Automagically save and restore many window settings.
    setAutoSaveSettings();

173 174 175 176 177 178 179
    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);
        }
180 181 182 183
	if(randomPlayAction) {
	    bool randomPlay = config->readBoolEntry("RandomPlay", false);
	    randomPlayAction->setChecked(randomPlay);
	}
184
    }
185
    { // view settings
186 187 188 189 190
        KConfigGroupSaver saver(config, "View");
	bool showEditor = config->readBoolEntry("ShowEditor", true);
	showEditorAction->setChecked(showEditor);
	splitter->setEditorVisible(showEditor);
    }
191 192 193 194 195 196 197 198 199 200 201 202

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

void JuK::readSettings()
{
    KConfig *config = KGlobal::config();
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	restore = config->readBoolEntry("RestoreOnLoad", true);
    }
203 204 205 206
}

void JuK::saveConfig()
{
207 208 209 210 211
    KConfig *config = KGlobal::config();
    { // player settings
        KConfigGroupSaver saver(config, "Player");
        if(sliderAction && sliderAction->getVolumeSlider())
            config->writeEntry("Volume", sliderAction->getVolumeSlider()->value());
212 213
	if(randomPlayAction)
	    config->writeEntry("RandomPlay", randomPlayAction->isChecked());
214
    }
215 216 217 218
    { // view settings
        KConfigGroupSaver saver(config, "View");
	config->writeEntry("ShowEditor", showEditorAction->isChecked());
    }
219 220 221 222 223
    { // general settings
        KConfigGroupSaver saver(config, "Settings");
	if(restoreOnLoadAction)
	    config->writeEntry("RestoreOnLoad", restoreOnLoadAction->isChecked());
    }
224 225
}

226 227
bool JuK::queryClose()
{
228
    Cache::instance()->save();
229
    saveConfig();
230
    delete(splitter);
231 232 233
    return(true);
}

234 235 236 237
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

238
void JuK::playlistChanged(Playlist *list)
239
{
240 241 242 243 244 245 246 247 248 249 250 251
    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);
    }
252 253
}

254 255 256
////////////////////////////////////////////////////////////////////////////////
// private action slot implementations - file menu
////////////////////////////////////////////////////////////////////////////////
257

258
void JuK::remove()
259
{
260
    PlaylistItemList items(splitter->playlistSelection());
261
    PlaylistItem *item = items.first();
262
    while(item) {
263 264 265
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
266
    }
267 268
    
    splitter->remove();
269 270
}

271 272
void JuK::quit()
{
273 274 275
    // delete(this);
    // kapp->quit();
    close();
276 277
}

278 279 280 281 282 283
////////////////////////////////////////////////////////////////////////////////
// edit menu
////////////////////////////////////////////////////////////////////////////////

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

286 287 288 289 290
    PlaylistItem *item = items.first();
    while(item) {
	if(item == playingItem)
	    playingItem = 0;
	item = items.next();
291 292
    }

293
    splitter->clearSelectedItems();
294 295
}

296 297 298 299 300 301
////////////////////////////////////////////////////////////////////////////////
// player menu
////////////////////////////////////////////////////////////////////////////////

void JuK::playFile()
{
302 303
    if(player.paused()) {
        player.play();
304 305 306 307

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

308 309 310 311 312 313
        if(player.playing()) {
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
        }
    }
314 315
    else if(player.playing())
	player.seekPosition(0);
316
    else if(splitter) {
317
        PlaylistItemList items = splitter->playlistSelection();
318
        if(items.count() > 0)
319
            playItem(items.at(0));
320
        else
321
            playItem(splitter->playlistFirstItem());
322
    }
323 324 325 326
}

void JuK::pauseFile()
{
327 328 329
    playTimer->stop();
    player.pause();
    pauseAction->setEnabled(false);
330 331 332 333
}

void JuK::stopFile()
{
334 335 336 337
    playTimer->stop();
    player.stop();
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
338 339
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
340 341
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
342 343
    if(playingItem)
        playingItem->setPixmap(0, 0);
344
    playingItem = 0;
345

346
    statusLabel->clear();
347 348
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362
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);
}


363 364 365 366
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

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

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

void JuK::trackPositionSliderUpdate(int position)
{
380
    if(player.playing() && !trackPositionDragging && !noSeek)
381
        player.seekPosition(position);
382 383 384 385
}

void JuK::pollPlay()
{
386 387
    noSeek = true;
    if(!player.playing()) {
388

389
        playTimer->stop();
390

391
        if(player.paused())
392
            pauseFile();
393
        else if(playingItem) {
394

395
	    PlaylistItem *next = Playlist::nextItem(playingItem, randomPlayAction->isChecked());
396 397
	    playingItem->setPixmap(0, 0);

398 399
	    if(next) {
		playingItem = next;
400 401 402
		
		backAction->setEnabled(true);
		
403 404 405 406 407 408 409
		sliderAction->getTrackPositionSlider()->setValue(0);
		player.play(playingItem->absFilePath(), player.getVolume());
		if(player.playing()) {
		    playTimer->start(pollInterval);
		    playingItem->setPixmap(0, QPixmap(UserIcon("playing")));
		}
	    }
410
	    statusLabel->setPlayingItem(playingItem);
411 412 413
	}
	else
	    stopFile();
414
    }
415
    else if(!trackPositionDragging) {
416
        sliderAction->getTrackPositionSlider()->setValue(player.position());
417 418 419 420 421 422 423 424
	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.
425

426 427
    if(player.playing() && float(player.totalTime() - player.currentTime()) < pollInterval * 2)
        playTimer->changeInterval(50);
428

429
    noSeek = false;
430 431 432 433
}

void JuK::setVolume(int volume)
{
434 435 436
    if(sliderAction && sliderAction->getVolumeSlider() &&
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
437
    {
438
        player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
439 440
    }
}
441 442 443

void JuK::playItem(QListViewItem *item)
{
444
    playItem(dynamic_cast<PlaylistItem *>(item));
445 446
}

447
void JuK::playItem(PlaylistItem *item)
448
{
449 450 451 452 453
    if(player.playing() || player.paused())
        stopFile();

    if(item) {
        float volume = float(sliderAction->getVolumeSlider()->value()) / float(sliderAction->getVolumeSlider()->maxValue());
454 455 456 457
        player.play(item->absFilePath(), volume);

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

458 459 460
        if(player.playing()) {
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
461 462 463 464 465 466

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

	    playingItem = item;

467
            sliderAction->getTrackPositionSlider()->setEnabled(true);
468
            item->setPixmap(0, QPixmap(UserIcon("playing")));
469
            playTimer->start(pollInterval);
470

471
	    statusLabel->setPlayingItem(item);
472
        }
473 474
    }
}
475

476
#include "juk.moc"