playermanager.cpp 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/***************************************************************************
                          playermanager.cpp
                             -------------------
    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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kdebug.h>

#include <qslider.h>
21
#include <qtimer.h>
22 23

#include "playermanager.h"
24
#include "playlistinterface.h"
25
#include "slideraction.h"
26
#include "statuslabel.h"
27 28 29
#include "actioncollection.h"

using namespace ActionCollection;
30 31 32 33 34 35 36 37 38 39 40

PlayerManager *PlayerManager::m_instance = 0;

////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

PlayerManager::PlayerManager() :
    QObject(0, "PlayerManager"),
    Player(),
    m_sliderAction(0),
41
    m_playlistInterface(0),
42
    m_statusLabel(0),
43 44
    m_player(0),
    m_timer(0),
45 46
    m_noSeek(false),
    m_muted(false)
47 48 49 50 51 52
{
    setup();
}

PlayerManager::~PlayerManager()
{
53
    delete m_player;
54 55 56 57 58 59 60 61 62 63 64 65 66
}

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

PlayerManager *PlayerManager::instance() // static
{
    if(!m_instance)
        m_instance = new PlayerManager;
    return m_instance;
}

67 68
bool PlayerManager::playing() const
{
69
    if(!player())
70 71
        return false;

72
    return player()->playing();
73 74 75 76
}

bool PlayerManager::paused() const
{
77
    if(!player())
78 79
        return false;

80
    return player()->paused();
81 82 83 84
}

float PlayerManager::volume() const
{
85
    if(!player())
86 87
        return 0;

88
    return player()->volume();
89 90 91 92
}

long PlayerManager::totalTime() const
{
93
    if(!player())
94 95
        return 0;

96
    return player()->totalTime();
97 98 99
}

long PlayerManager::currentTime() const
100
{
101
    if(!player())
102 103
        return 0;

104
    return player()->currentTime();
105 106 107 108
}

int PlayerManager::position() const
{
109
    if(!player())
110 111
        return 0;

112
    return player()->position();
113 114
}

115
void PlayerManager::setPlaylistInterface(PlaylistInterface *interface)
116 117 118 119
{
    m_playlistInterface = interface;
}

120 121 122 123 124
void PlayerManager::setStatusLabel(StatusLabel *label)
{
    m_statusLabel = label;
}

125 126 127 128 129 130
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

void PlayerManager::play(const QString &fileName)
{
131
    if(!player() || !m_playlistInterface)
132 133
        return;

134
    if(fileName.isNull()) {
135 136
	if(player()->paused())
            player()->play();
137 138
        else if(player()->playing()) {
	    m_sliderAction->trackPositionSlider()->setValue(0);
139
            player()->seekPosition(0);
140
	}
141 142 143 144 145
        else {
            QString file = m_playlistInterface->currentFile();
            if(!file.isNull())
                player()->play(file);
	}
146
    }
147
    else
148
        player()->play(fileName);
149

150
    // Make sure that the player() actually starts before doing anything.
151

152
    if(!player()->playing()) {
153 154 155
        stop();
        return;
    }
156

157 158 159 160
    action("pause")->setEnabled(true);
    action("stop")->setEnabled(true);
    action("forward")->setEnabled(true);
    action("back")->setEnabled(true);
161

162
    m_sliderAction->trackPositionSlider()->setEnabled(true);
163 164

    m_timer->start(m_pollInterval);
165 166 167 168
}

void PlayerManager::pause()
{
169
    if(!player())
170 171
        return;

172 173 174 175 176 177
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
178
    action("pause")->setEnabled(false);
179 180

    player()->pause();
181 182 183 184
}

void PlayerManager::stop()
{
185
    if(!player())
186 187
        return;

188 189
    m_timer->stop();

190 191 192 193
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
194 195 196 197 198 199 200

    m_sliderAction->trackPositionSlider()->setValue(0);
    m_sliderAction->trackPositionSlider()->setEnabled(false);

    m_statusLabel->clear();

    player()->stop();
201 202
}

Laurent Montel's avatar
Laurent Montel committed
203
void PlayerManager::setVolume(float volume)
204
{
205
    if(!player())
206 207
        return;

208
    player()->setVolume(volume);
209 210
}

211
void PlayerManager::seek(long seekTime)
212
{
213
    if(!player())
214
        return;
215

216
    player()->seek(seekTime);
217 218
}

219
void PlayerManager::seekPosition(int position)
220
{
221 222 223 224
    if(!player())
        return;

    if(!player()->playing() || m_noSeek)
225
        return;
226

227 228
    slotUpdateTime(position);
    player()->seekPosition(position);
229 230 231 232 233 234 235 236 237 238 239 240 241
    m_sliderAction->trackPositionSlider()->setValue(position);
}

void PlayerManager::seekForward()
{
    int position = m_sliderAction->trackPositionSlider()->value();
    seekPosition(kMin(m_sliderAction->trackPositionSlider()->maxValue(), position + 10));
}

void PlayerManager::seekBack()
{
    int position = m_sliderAction->trackPositionSlider()->value();
    seekPosition(kMax(m_sliderAction->trackPositionSlider()->minValue(), position - 10));
242 243
}

244 245 246 247 248
void PlayerManager::playPause()
{
    playing() ? pause() : play();
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
void PlayerManager::forward()
{
    QString file = m_playlistInterface->nextFile();
    if(!file.isNull())
        play(file);
    else
        stop();
}

void PlayerManager::back()
{
    QString file = m_playlistInterface->previousFile();
    if(!file.isNull())
        play(file);
    else
        stop();
}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
void PlayerManager::volumeUp()
{
    if(!m_player || !m_sliderAction)
	return;

    int volume = m_sliderAction->volumeSlider()->value() +
	m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up

    slotSetVolume(volume);
    m_sliderAction->volumeSlider()->setValue(volume);
}

void PlayerManager::volumeDown()
{
    if(!m_player || !m_sliderAction)
	return;

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

    slotSetVolume(volume);
    m_sliderAction->volumeSlider()->setValue(volume);
}

void PlayerManager::mute()
{
    if(!m_player || !m_sliderAction)
        return;

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

300 301 302
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
303

304
void PlayerManager::slotPollPlay()
305
{
306
    if(!player() || !m_playlistInterface)
307
        return;
308

309
    m_noSeek = true;
310

311
    if(!player()->playing()) {
312
        m_timer->stop();
313 314
        QString nextFile = m_playlistInterface->nextFile();
        if(!nextFile.isNull())
315
            play(nextFile);
316 317
        else
            stop();
318 319
    }
    else if(!m_sliderAction->dragging()) {
320
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
321

322 323 324 325
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
326
    }
327

328 329 330 331
    // 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.
332

333 334 335
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
336 337 338
    {
        m_timer->changeInterval(50);
    }
339

340
    m_noSeek = false;
341 342
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
void PlayerManager::slotSetOutput(int system)
{
    stop();
    delete m_player;
    m_player = Player::createPlayer(system);
}

void PlayerManager::slotSetVolume(int volume)
{
    setVolume(float(volume) / float(m_sliderAction->volumeSlider()->maxValue()));
}

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

    float positionFraction = float(position) / m_sliderAction->trackPositionSlider()->maxValue();
    float totalTime = float(m_player->totalTime());
    long seekTime = long(positionFraction * totalTime + 0.5); // "+0.5" for rounding

    m_statusLabel->setItemCurrentTime(seekTime);
}

367 368 369 370
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

371 372 373 374 375 376 377 378
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

379 380 381 382
void PlayerManager::setup()
{
    // All of the actions required by this class should be listed here.

383 384 385 386 387
    if(!action("pause") ||
       !action("stop") ||
       !action("back") ||
       !action("forward") ||
       !action("trackPositionAction"))
388 389 390 391 392 393

    {
        kdWarning(65432) << k_funcinfo << "Could not find all of the required actions." << endl;
        return;
    }

394 395
    // initialize action states

396 397 398 399
    action("pause")->setEnabled(false);
    action("stop")->setEnabled(false);
    action("back")->setEnabled(false);
    action("forward")->setEnabled(false);
400

401 402
    // setup sliders

403
    m_sliderAction = action<SliderAction>("trackPositionAction");
404

405 406 407 408 409 410 411
    connect(m_sliderAction, SIGNAL(signalPositionChanged(int)),
            this, SLOT(seekPosition(int)));
    connect(m_sliderAction->trackPositionSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotUpdateTime(int)));
    connect(m_sliderAction->volumeSlider(), SIGNAL(valueChanged(int)),
            this, SLOT(slotSetVolume(int)));

412
    KAction *outputAction = action("outputSelect");
413 414 415 416

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
        m_player = Player::createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
417
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
418 419 420
    }
    else
        m_player = Player::createPlayer();
421

422 423
    float volume =
        float(m_sliderAction->volumeSlider()->value()) /
424
        float(m_sliderAction->volumeSlider()->maxValue());
425 426 427

    m_player->setVolume(volume);

428 429
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
430 431 432
}

#include "playermanager.moc"