playermanager.cpp 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/***************************************************************************
                          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 <kactioncollection.h>
#include <kmainwindow.h>
#include <kapplication.h>
#include <kdebug.h>

#include <qslider.h>
24
#include <qtimer.h>
25 26

#include "playermanager.h"
27
#include "playlistinterface.h"
28
#include "slideraction.h"
29
#include "statuslabel.h"
30 31 32 33 34 35 36 37 38 39 40 41

PlayerManager *PlayerManager::m_instance = 0;

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

PlayerManager::PlayerManager() :
    QObject(0, "PlayerManager"),
    Player(),
    m_actionCollection(0),
    m_sliderAction(0),
42
    m_playlistInterface(0),
43
    m_statusLabel(0),
44 45 46
    m_player(0),
    m_timer(0),
    m_noSeek(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
        else if(player()->playing())
138
            player()->seekPosition(0);
139 140 141 142 143
        else {
            QString file = m_playlistInterface->currentFile();
            if(!file.isNull())
                player()->play(file);
	}
144
    }
145
    else
146
        player()->play(fileName);
147

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

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

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

160 161
    m_sliderAction->trackPositionSlider()->setValue(0);
    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 177 178 179
    if(player()->paused()) {
        play();
        return;
    }

    m_timer->stop();
    m_actionCollection->action("pause")->setEnabled(false);

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

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

187 188 189 190 191 192 193 194 195 196 197 198 199
    m_timer->stop();

    m_actionCollection->action("pause")->setEnabled(false);
    m_actionCollection->action("stop")->setEnabled(false);
    m_actionCollection->action("back")->setEnabled(false);
    m_actionCollection->action("forward")->setEnabled(false);

    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 241 242 243 244 245 246 247
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();
}

248 249 250
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
251

252
void PlayerManager::slotPollPlay()
253
{
254
    if(!player() || !m_playlistInterface)
255
        return;
256

257
    m_noSeek = true;
258

259
    if(!player()->playing()) {
260
        m_timer->stop();
261 262 263 264 265
        QString nextFile = m_playlistInterface->nextFile();
        if(!nextFile.isNull())
            play();
        else
            stop();
266 267
    }
    else if(!m_sliderAction->dragging()) {
268
        m_sliderAction->trackPositionSlider()->setValue(player()->position());
269

270 271 272 273
        if(m_statusLabel) {
            m_statusLabel->setItemTotalTime(player()->totalTime());
            m_statusLabel->setItemCurrentTime(player()->currentTime());
        }
274
    }
275

276 277 278 279
    // 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.
280

281 282 283
    if(player()->playing() &&
       player()->totalTime() > 0 &&
       float(player()->totalTime() - player()->currentTime()) < m_pollInterval * 2)
284 285 286
    {
        m_timer->changeInterval(50);
    }
287

288
    m_noSeek = false;
289 290
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
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);
}

315 316 317 318
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

319 320 321 322 323 324 325 326
Player *PlayerManager::player() const
{
    if(!m_player)
        instance()->setup();

    return m_player;
}

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
void PlayerManager::setup()
{
    // Since we're doing a little wizardry to keep the interaction and API
    // requirements as light as possible we want to check everything we're
    // going to need to make sure that everything's in order.

    KMainWindow *mainWindow = dynamic_cast<KMainWindow *>(kapp->mainWidget());

    if(!mainWindow) {
        kdWarning(65432) << k_funcinfo << "Could not find main window." << endl;
        return;
    }

    m_actionCollection = mainWindow->actionCollection();

    if(!m_actionCollection) {
        kdWarning(65432) << k_funcinfo << "Action collection is null." << endl;
        return;
    }

    // All of the actions required by this class should be listed here.

    if(!m_actionCollection->action("pause") ||
       !m_actionCollection->action("stop") ||
       !m_actionCollection->action("back") ||
       !m_actionCollection->action("forward") ||
       !m_actionCollection->action("trackPositionAction"))

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

360 361
    // initialize action states

362 363 364 365 366
    m_actionCollection->action("pause")->setEnabled(false);
    m_actionCollection->action("stop")->setEnabled(false);
    m_actionCollection->action("back")->setEnabled(false);
    m_actionCollection->action("forward")->setEnabled(false);

367 368
    // setup sliders

369
    m_sliderAction = static_cast<SliderAction *>(m_actionCollection->action("trackPositionAction"));
370

371 372 373 374 375 376 377
    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)));

378 379 380 381 382
    KAction *outputAction = m_actionCollection->action("outputSelect");

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
        m_player = Player::createPlayer(mediaSystem);
Scott Wheeler's avatar
Scott Wheeler committed
383
        connect(outputAction, SIGNAL(activated(int)), this, SLOT(slotSetOutput(int)));
384 385 386
    }
    else
        m_player = Player::createPlayer();
387

388 389 390 391 392 393
    float volume =
        float(m_sliderAction->volumeSlider()->value()) /
	float(m_sliderAction->volumeSlider()->maxValue());

    m_player->setVolume(volume);

394 395
    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
396 397 398
}

#include "playermanager.moc"