juk.cpp 14.4 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 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
    setCentralWidget(splitter);

    // playlist item activation connection
74 75
    connect(splitter, SIGNAL(doubleClicked()), this, SLOT(playSelectedFile()));
    connect(splitter, SIGNAL(listBoxDoubleClicked()), this, SLOT(playFirstFile()));
76

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

81
    connect(splitter, SIGNAL(selectedPlaylistCountChanged(int)), statusLabel, SLOT(setPlaylistCount(int)));
82
    connect(statusLabel, SIGNAL(jumpButtonClicked()), splitter, SLOT(selectPlaying()));
83

84 85
    updatePlaylistInfo();

86 87 88
    splitter->setFocus();
}

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

    // edit menu
99
    KStdAction::cut(splitter, SLOT(clearSelectedItems()), actionCollection());
100 101
    KStdAction::copy(this, SLOT(copy()), actionCollection());
    KStdAction::paste(this, SLOT(paste()), actionCollection());
102
    KStdAction::selectAll(splitter, SLOT(selectAll()), actionCollection());
103

104 105
    // view menu
    showEditorAction = new KToggleAction(i18n("Show Tag Editor"), 0, actionCollection(), "showEditor");
106 107
    connect(showEditorAction, SIGNAL(toggled(bool)), splitter, SLOT(setEditorVisible(bool)));
    KStdAction::redisplay(splitter, SLOT(refresh()), actionCollection());
108

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

117
    // playlist menu
118 119 120 121 122 123 124 125 126
    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");
127 128
    deleteItemPlaylistAction = new KAction(i18n("Delete"), "editdelete", 0, splitter, SLOT(deleteItemPlaylist()), 
					   actionCollection(), "deleteItemPlaylist");
129
    
130 131
    // settings menu
    restoreOnLoadAction = new KToggleAction(i18n("Restored Playlists on Load"),  0, actionCollection(), "restoreOnLoad"); 
132
    new KAction(i18n("Genre List Editor"), 0, this, SLOT(showGenreListEditor()), actionCollection(), "showGenreListEditor");
133

134
    connect(splitter, SIGNAL(playlistChanged()), this, SLOT(playlistChanged()));
135

136

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

    createGUI();
141 142 143 144

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

void JuK::setupPlayer()
{
149 150
    trackPositionDragging = false;
    noSeek = false;
151 152
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
153 154
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
155

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

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

169

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

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

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

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

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

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

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

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

247 248 249 250
////////////////////////////////////////////////////////////////////////////////
// private slot definitions
////////////////////////////////////////////////////////////////////////////////

251
void JuK::playlistChanged()
252
{
253
    if(splitter->collectionListSelected()) {
254 255 256 257 258 259 260 261 262 263 264
	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);
    }
265 266 267 268 269 270

    updatePlaylistInfo();
}

void JuK::updatePlaylistInfo()
{
271
    statusLabel->setPlaylistInfo(splitter->selectedPlaylistName(), splitter->selectedPlaylistCount());
272 273 274 275 276 277 278 279
}

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

void JuK::playFile()
{
280 281
    if(player.paused()) {
        player.play();
282 283 284 285

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

286 287 288 289 290 291
        if(player.playing()) {
            pauseAction->setEnabled(true);
            stopAction->setEnabled(true);
            playTimer->start(pollInterval);
        }
    }
292 293
    else if(player.playing())
	player.seekPosition(0);
294 295
    else
	playFile(splitter->playNextFile(randomPlayAction->isChecked()));
296 297 298 299
}

void JuK::pauseFile()
{
300 301 302
    playTimer->stop();
    player.pause();
    pauseAction->setEnabled(false);
303 304 305 306
}

void JuK::stopFile()
{
307 308
    playTimer->stop();
    player.stop();
309

310 311
    pauseAction->setEnabled(false);
    stopAction->setEnabled(false);
312 313
    backAction->setEnabled(false);
    forwardAction->setEnabled(false);
314

315 316
    sliderAction->getTrackPositionSlider()->setValue(0);
    sliderAction->getTrackPositionSlider()->setEnabled(false);
317 318

    splitter->stop();
319

320
    statusLabel->clear();
321 322
}

323 324
void JuK::backFile()
{
325
    playFile(splitter->playPreviousFile(randomPlayAction->isChecked()));
326 327 328 329
}

void JuK::forwardFile()
{
330
    playFile(splitter->playNextFile(randomPlayAction->isChecked()));
331 332
}

333 334 335 336 337 338 339 340 341
////////////////////////////////////////////////////////////////////////////////
// settings menu
////////////////////////////////////////////////////////////////////////////////

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

343 344 345 346
////////////////////////////////////////////////////////////////////////////////
// additional player slots
////////////////////////////////////////////////////////////////////////////////

347 348
void JuK::trackPositionSliderClick()
{
349
    trackPositionDragging = true;
350 351 352 353
}

void JuK::trackPositionSliderRelease()
{
354
    trackPositionDragging = false;
355
    player.seekPosition(sliderAction->getTrackPositionSlider()->value());
356 357 358 359
}

void JuK::trackPositionSliderUpdate(int position)
{
360
    if(player.playing() && !trackPositionDragging && !noSeek)
361
        player.seekPosition(position);
362 363
}

364 365
// This method is called when the play timer has expired.

366 367
void JuK::pollPlay()
{
368 369
    // Our locking mechanism.  Since this method adjusts the play slider, we 
    // want to make sure that our adjustments
370
    noSeek = true;
371

372
    if(!player.playing()) {
373

374
        playTimer->stop();
375

376 377 378
	if(!player.paused())
	    playFile(splitter->playNextFile(randomPlayAction->isChecked()));

379
    }
380
    else if(!trackPositionDragging) {
381
        sliderAction->getTrackPositionSlider()->setValue(player.position());
382 383 384 385 386 387 388 389
	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.
390

391
    if(player.playing() && player.totalTime() > 0 && float(player.totalTime() - player.currentTime()) < pollInterval * 2)
392
        playTimer->changeInterval(50);
393

394
    noSeek = false;
395 396 397 398
}

void JuK::setVolume(int volume)
{
399 400 401
    if(sliderAction && sliderAction->getVolumeSlider() &&
       sliderAction->getVolumeSlider()->maxValue() > 0 &&
       volume >= 0 && sliderAction->getVolumeSlider()->maxValue() >= volume)
402
    {
403
        player.setVolume(float(volume) / float(sliderAction->getVolumeSlider()->maxValue()));
404 405
    }
}
406

407
void JuK::playFile(const QString &file)
408
{
409 410 411
    float volume = float(sliderAction->getVolumeSlider()->value()) / float(sliderAction->getVolumeSlider()->maxValue());
    
    player.play(file, volume);
412

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

415 416 417 418 419 420 421
    if(player.playing()) {
	pauseAction->setEnabled(true);
	stopAction->setEnabled(true);
	
	backAction->setEnabled(true);
	forwardAction->setEnabled(true);
	
422
	sliderAction->getTrackPositionSlider()->setValue(0);
423 424
	sliderAction->getTrackPositionSlider()->setEnabled(true);
	playTimer->start(pollInterval);
425

426
	statusLabel->setPlayingItemInfo(splitter->playingTrack(), splitter->playingArtist(), splitter->playingList());
427 428
    }
}
429

430
#include "juk.moc"