playermanager.cpp 15.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/***************************************************************************
    begin                : Sat Feb 14 2004
    copyright            : (C) 2004 by Scott Wheeler
    email                : wheeler@kde.org
***************************************************************************/

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

16 17 18 19 20 21 22 23 24
/**
 * Note to those who work here.  The preprocessor variables HAVE_ARTS and HAVE_GSTREAMER
 * are ::ALWAYS DEFINED::.  You can't use #ifdef to see if they're present, you should just
 * use #if.
 *
 * However, HAVE_AKODE is #define'd if present, and undefined if not present.
 * - mpyne
 */

25
#include <kdebug.h>
26
#include <klocale.h>
27 28

#include <qslider.h>
29
#include <QTimer>
Laurent Montel's avatar
Laurent Montel committed
30 31
//Added by qt3to4:
#include <QPixmap>
Laurent Montel's avatar
Poirt  
Laurent Montel committed
32 33 34
#include <ktoggleaction.h>
#include <kactioncollection.h>
#include <kselectaction.h>
35 36
#include <math.h>

37
#include "playermanager.h"
38
#include "playlistinterface.h"
39
#include "slideraction.h"
40
#include "statuslabel.h"
41
#include "actioncollection.h"
42
#include "collectionlist.h"
Michael Pyne's avatar
Michael Pyne committed
43
#include "coverinfo.h"
44
#include "tag.h"
45

46 47
#include "config.h"

Laurent Montel's avatar
Port it  
Laurent Montel committed
48 49 50
#include <dbus/qdbus.h>
#include "playeradaptor.h"

51
using namespace ActionCollection;
52

Stephan Kulow's avatar
Stephan Kulow committed
53
enum PlayerManagerStatus { StatusStopped = -1, StatusPaused = 1, StatusPlaying = 2 };
54

55 56 57 58 59
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlayerManager::PlayerManager() :
Scott Wheeler's avatar
Scott Wheeler committed
60
    Player(),
61
    m_sliderAction(0),
62
    m_playlistInterface(0),
63
    m_statusLabel(0),
64 65
    m_player(0),
    m_timer(0),
66
    m_noSeek(false),
67 68
    m_muted(false),
    m_setup(false)
69
{
Michael Pyne's avatar
Michael Pyne committed
70 71 72 73 74
// This class is the first thing constructed during program startup, and
// therefore has no access to the widgets needed by the setup() method.
// Since the setup() method will be called indirectly by the player() method
// later, just disable it here. -- mpyne
//    setup();
Laurent Montel's avatar
Port it  
Laurent Montel committed
75 76 77
    new PlayerAdaptor( this );
    QDBus::sessionBus().registerObject("/Player", this);

78 79 80 81
}

PlayerManager::~PlayerManager()
{
82
    delete m_player;
83 84 85 86 87 88 89 90
}

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

PlayerManager *PlayerManager::instance() // static
{
91 92
    static PlayerManager manager;
    return &manager;
93 94
}

95 96
bool PlayerManager::playing() const
{
97
    if(!player())
98 99
        return false;

100
    return player()->playing();
101 102 103 104
}

bool PlayerManager::paused() const
{
105
    if(!player())
106 107
        return false;

108
    return player()->paused();
109 110 111 112
}

float PlayerManager::volume() const
{
113
    if(!player())
114 115
        return 0;

116
    return player()->volume();
117 118
}

119 120 121
int PlayerManager::status() const
{
    if(!player())
122
        return StatusStopped;
123 124

    if(player()->paused())
125
        return StatusPaused;
126 127

    if(player()->playing())
128
        return StatusPlaying;
129

130 131 132
    return 0;
}

Scott Wheeler's avatar
Scott Wheeler committed
133
int PlayerManager::totalTime() const
134
{
135
    if(!player())
136 137
        return 0;

138
    return player()->totalTime();
139 140
}

Scott Wheeler's avatar
Scott Wheeler committed
141
int PlayerManager::currentTime() const
142
{
143
    if(!player())
144 145
        return 0;

146
    return player()->currentTime();
147 148 149 150
}

int PlayerManager::position() const
{
151
    if(!player())
152 153
        return 0;

154
    return player()->position();
155 156
}

157 158 159 160 161 162 163
QStringList PlayerManager::trackProperties()
{
    return FileHandle::properties();
}

QString PlayerManager::trackProperty(const QString &property) const
{
Michael Pyne's avatar
Michael Pyne committed
164
    if(!playing() && !paused())
165 166 167 168 169
        return QString::null;

    return m_file.property(property);
}

Michael Pyne's avatar
Michael Pyne committed
170 171 172 173 174 175 176 177 178 179 180 181 182
QPixmap PlayerManager::trackCover(const QString &size) const
{
    if(!playing() && !paused())
        return QPixmap();

    if(size.lower() == "small")
        return m_file.coverInfo()->pixmap(CoverInfo::Thumbnail);
    if(size.lower() == "large")
        return m_file.coverInfo()->pixmap(CoverInfo::FullSize);

    return QPixmap();
}

183 184 185 186 187
FileHandle PlayerManager::playingFile() const
{
    return m_file;
}

188 189 190 191 192
QString PlayerManager::playingString() const
{
    if(!playing())
        return QString::null;

193 194 195 196 197
    QString str = m_file.tag()->artist() + " - " + m_file.tag()->title();
    if(m_file.tag()->artist().isEmpty())
        str = m_file.tag()->title();

    return str;
198 199
}

200
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
201 202 203 204
{
    m_playlistInterface = interface;
}

205 206 207 208 209
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

210 211 212 213
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

214
void PlayerManager::play(const FileHandle &file)
215
{
216
    if(!player() || !m_playlistInterface)
217 218
        return;

219 220
    if(file.isNull()) {
        if(player()->paused())
221
            player()->play();
222
        else if(player()->playing()) {
223 224
            if(m_sliderAction->trackPositionSlider())
                m_sliderAction->trackPositionSlider()->setValue(0);
225
            player()->seekPosition(0);
226
        }
227
        else {
228
            m_playlistInterface->playNext();
229
            m_file = m_playlistInterface->currentFile();
230

231
            if(!m_file.isNull())
232
                player()->play(m_file);
233
        }
234
    }
235
    else {
236
        m_file = file;
237
        player()->play(file);
238
    }
239

240
    // Make sure that the player() actually starts before doing anything.
241

242
    if(!player()->playing()) {
Scott Wheeler's avatar
Scott Wheeler committed
243
        kWarning(65432) << "Unable to play " << file.absFilePath() << endl;
244 245 246
        stop();
        return;
    }
247

248 249 250
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
251 252
    if(action<KToggleAction>("albumRandomPlay")->isChecked())
        action("forwardAlbum")->setEnabled(true);
253
    action("back")->setEnabled(true);
254

255 256
    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setEnabled(true);
257 258

    m_timer->start(m_pollInterval);
259 260

    emit signalPlay();
261 262
}

263 264 265 266 267 268 269 270 271
void PlayerManager::play(const QString &file)
{
    CollectionListItem *item = CollectionList::instance()->lookup(file);
    if(item) {
        Playlist::setPlaying(item);
        play(item->file());
    }
}

Scott Wheeler's avatar
Scott Wheeler committed
272 273 274 275 276
void PlayerManager::play()
{
    play(FileHandle::null());
}

277 278
void PlayerManager::pause()
{
279
    if(!player())
280 281
        return;

282 283 284 285 286 287
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
288
    action("pause")->setEnabled(false);
289 290

    player()->pause();
291 292

    emit signalPause();
293 294 295 296
}

void PlayerManager::stop()
{
297
    if(!player() || !m_playlistInterface)
298 299
        return;

300 301
    m_timer->stop();

302 303 304 305
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
306
    action("forwardAlbum")->setEnabled(false);
307

308 309 310 311
    if(m_sliderAction->trackPositionSlider()) {
        m_sliderAction->trackPositionSlider()->setValue(0);
        m_sliderAction->trackPositionSlider()->setEnabled(false);
    }
312 313

    player()->stop();
314
    m_playlistInterface->stop();
315

316 317
    m_file = FileHandle::null();

318
    emit signalStop();
319 320
}

Laurent Montel's avatar
Laurent Montel committed
321
void PlayerManager::setVolume(float volume)
322
{
323
    if(!player())
324 325
        return;

326
    player()->setVolume(volume);
327 328
}

Scott Wheeler's avatar
Scott Wheeler committed
329
void PlayerManager::seek(int seekTime)
330
{
331
    if(!player())
332
        return;
333

334
    player()->seek(seekTime);
335 336
}

337
void PlayerManager::seekPosition(int position)
338
{
339 340 341 342
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
343
        return;
344

345 346
    slotUpdateTime(position);
    player()->seekPosition(position);
347 348 349

    if(m_sliderAction->trackPositionSlider())
        m_sliderAction->trackPositionSlider()->setValue(position);
350 351 352 353
}

void PlayerManager::seekForward()
{
Laurent Montel's avatar
Laurent Montel committed
354
    seekPosition(qMin(SliderAction::maxPosition, position() + 10));
355 356 357 358
}

void PlayerManager::seekBack()
{
Laurent Montel's avatar
Laurent Montel committed
359
    seekPosition(qMax(SliderAction::minPosition, position() - 10));
360 361
}

362 363
void PlayerManager::playPause()
{
David Faure's avatar
David Faure committed
364
    playing() ? action("pause")->trigger() : action("play")->trigger();
365 366
}

367 368
void PlayerManager::forward()
{
369 370
    m_playlistInterface->playNext();
    FileHandle file = m_playlistInterface->currentFile();
371

372 373 374 375 376 377 378 379
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
380 381
    m_playlistInterface->playPrevious();
    FileHandle file = m_playlistInterface->currentFile();
382

383 384 385 386 387 388
    if(!file.isNull())
        play(file);
    else
        stop();
}

389 390
void PlayerManager::volumeUp()
{
391
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
392
        return;
393

394
    int volume = m_sliderAction->volumeSlider()->volume() +
395
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up
396 397

    slotSetVolume(volume);
398
    m_sliderAction->volumeSlider()->setVolume(volume);
399 400 401 402
}

void PlayerManager::volumeDown()
{
403
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
404
        return;
405 406

    int volume = m_sliderAction->volumeSlider()->value() -
407
        m_sliderAction->volumeSlider()->maxValue() / 25; // 4% down
408 409

    slotSetVolume(volume);
410
    m_sliderAction->volumeSlider()->setVolume(volume);
411 412 413 414
}

void PlayerManager::mute()
{
415
    if(!player() || !m_sliderAction || !m_sliderAction->volumeSlider())
416 417 418 419 420 421
        return;

    slotSetVolume(m_muted ? m_sliderAction->volumeSlider()->value() : 0);
    m_muted = !m_muted;
}

422 423 424
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
425

426
void PlayerManager::slotPollPlay()
427
{
428
    if(!player() || !m_playlistInterface)
429
        return;
430

431
    m_noSeek = true;
432

433
    if(!player()->playing()) {
434
        m_timer->stop();
435

436 437
        m_playlistInterface->playNext();
        FileHandle nextFile = m_playlistInterface->currentFile();
438
        if(!nextFile.isNull())
439
            play(nextFile);
440 441
        else
            stop();
442 443
    }
    else if(!m_sliderAction->dragging()) {
444 445
        if(m_sliderAction->trackPositionSlider())
            m_sliderAction->trackPositionSlider()->setValue(player()->position());
446

447 448 449 450
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
451
    }
452

453 454 455 456 457 458 459 460
    // This call is done because when the user adds the slider to the toolbar
    // while playback is occuring the volume slider generally defaults to 0,
    // and doesn't get updated to the correct volume.  It might be better to
    // have the SliderAction class fill in the correct volume, but I'm trying
    // to avoid having it depend on PlayerManager since it may not be
    // constructed in time during startup. -mpyne

    if(!m_sliderAction->volumeDragging() && m_sliderAction->volumeSlider())
461 462
    {
        int maxV = m_sliderAction->volumeSlider()->maxValue();
463
        float v = sqrt(sqrt(volume())); // Cancel out exponential scaling
464 465 466 467 468

        m_sliderAction->volumeSlider()->blockSignals(true);
        m_sliderAction->volumeSlider()->setVolume((int)((v) * maxV));
        m_sliderAction->volumeSlider()->blockSignals(false);
    }
469

470 471 472 473
    // 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.
474

475 476 477
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
478 479 480
    {
        m_timer->changeInterval(50);
    }
481

482
    m_noSeek = false;
483 484
}

485
void PlayerManager::slotSetOutput(const QString &system)
486 487
{
    stop();
488 489 490 491 492 493
    setOutput(system);
    setup();
}

void PlayerManager::setOutput(const QString &system)
{
494
    delete m_player;
495
    m_player = new Player;
496 497 498 499
}

void PlayerManager::slotSetVolume(int volume)
{
500 501
    float scaledVolume;

502
    if(m_sliderAction->volumeSlider())
503 504 505
        scaledVolume = float(volume) / m_sliderAction->volumeSlider()->maxValue();
    else {
        scaledVolume = float(volume) / 100.0; // Hopefully this is accurate
Laurent Montel's avatar
Laurent Montel committed
506
        scaledVolume = qMin(1.0f, scaledVolume);
507 508 509 510 511 512 513 514
    }

    // Perform exponential scaling to counteract the fact that humans perceive
    // volume changes logarithmically.

    scaledVolume *= scaledVolume;
    scaledVolume *= scaledVolume;
    setVolume(scaledVolume); // scaledVolume ^ 4
515 516 517 518 519 520 521
}

void PlayerManager::slotUpdateTime(int position)
{
    if(!m_statusLabel)
        return;

522
    float positionFraction = float(position) / SliderAction::maxPosition;
523
    float totalTime = float(player()->totalTime());
Scott Wheeler's avatar
Scott Wheeler committed
524
    int seekTime = int(positionFraction * totalTime + 0.5); // "+0.5" for rounding
525 526 527 528

    m_statusLabel->setItemCurrentTime(seekTime);
}

529 530 531 532
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

533 534 535 536 537 538 539 540
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

541 542 543 544
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

545 546 547
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
548
       !action("forwardAlbum") ||
549 550
       !action("forward") ||
       !action("trackPositionAction"))
551
    {
Laurent Montel's avatar
Laurent Montel committed
552
        kWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
553 554 555
        return;
    }

556 557 558 559
    if(m_setup)
        return;
    m_setup = true;

560 561
    // initialize action states

562 563 564 565
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
566
    action("forwardAlbum")->setEnabled(false);
567

568 569
    // setup sliders

570
    m_sliderAction = action<SliderAction>("trackPositionAction");
571

572 573 574 575
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
576
    connect(m_sliderAction, SIGNAL(signalVolumeChanged(int)),
577 578
            this, SLOT(slotSetVolume(int)));

579
    // Call this method manually to avoid warnings.
580

581
    KAction *outputAction = ActionCollection::actions()->action("outputSelect");
582 583

    if(outputAction) {
584
        setOutput(static_cast<KSelectAction *>(outputAction)->currentText());
585
        connect(outputAction, SIGNAL(activated(const QString &)), this, SLOT(slotSetOutput(const QString &)));
586 587
    }
    else
588
        m_player = new Player;
589

590
    float volume;
591

592
    if(m_sliderAction->volumeSlider()) {
593
        volume =
594 595 596 597 598
            float(m_sliderAction->volumeSlider()->volume()) /
            float(m_sliderAction->volumeSlider()->maxValue());
    }
    else
        volume = 1; // Assume user wants full volume
599 600 601

    m_player->setVolume(volume);

602 603
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
604 605
}

Michael Pyne's avatar
Michael Pyne committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
QString PlayerManager::randomPlayMode() const
{
    if(action<KToggleAction>("randomPlay")->isChecked())
        return "Random";
    if(action<KToggleAction>("albumRandomPlay")->isChecked())
        return "AlbumRandom";
    return "NoRandom";
}

void PlayerManager::setRandomPlayMode(const QString &randomMode)
{
    if(randomMode.lower() == "random")
        action<KToggleAction>("randomPlay")->setChecked(true);
    if(randomMode.lower() == "albumrandom")
        action<KToggleAction>("albumRandomPlay")->setChecked(true);
    if(randomMode.lower() == "norandom")
        action<KToggleAction>("disableRandomPlay")->setChecked(true);
}

625
#include "playermanager.moc"
626

627
// vim: set et sw=4 tw=0 sta: