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

PlayerManager *PlayerManager::m_instance = 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

187 188
    m_timer->stop();

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

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

    m_statusLabel->clear();

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

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

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

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

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

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

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

226 227
    slotUpdateTime(position);
    player()->seekPosition(position);
228 229 230 231 232 233 234 235 236 237 238 239 240
    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));
241 242
}

243 244
void PlayerManager::playPause()
{
245
    playing() ? action("pause")->activate() : action("play")->activate();
246 247
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
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();
}

266 267
void PlayerManager::volumeUp()
{
268
    if(!player() || !m_sliderAction)
269 270
	return;

271
    int volume = m_sliderAction->volumeSlider()->volume() +
272 273 274
	m_sliderAction->volumeSlider()->maxValue() / 25; // 4% up

    slotSetVolume(volume);
275
    m_sliderAction->volumeSlider()->setVolume(volume);
276 277 278 279
}

void PlayerManager::volumeDown()
{
280
    if(!player() || !m_sliderAction)
281 282 283 284 285 286
	return;

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

    slotSetVolume(volume);
287
    m_sliderAction->volumeSlider()->setVolume(volume);
288 289 290 291
}

void PlayerManager::mute()
{
292
    if(!player() || !m_sliderAction)
293 294 295 296 297 298
        return;

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

299 300 301
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
302

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

308
    m_noSeek = true;
309

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

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

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

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

339
    m_noSeek = false;
340 341
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
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();
360
    float totalTime = float(player()->totalTime());
361 362 363 364 365
    long seekTime = long(positionFraction * totalTime + 0.5); // "+0.5" for rounding

    m_statusLabel->setItemCurrentTime(seekTime);
}

366 367 368 369
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

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

    return m_player;
}

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

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

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

393 394
    // initialize action states

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

400 401
    // setup sliders

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

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

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

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

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

    m_player->setVolume(volume);

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

#include "playermanager.moc"