playermanager.cpp 7.67 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 27 28 29 30 31 32 33 34 35 36 37 38 39

#include "playermanager.h"
#include "slideraction.h"

PlayerManager *PlayerManager::m_instance = 0;

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

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

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

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

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

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
bool PlayerManager::playing() const
{
    if(!m_player)
        return false;

    return m_player->playing();
}

bool PlayerManager::paused() const
{
    if(!m_player)
        return false;

    return m_player->paused();
}

float PlayerManager::volume() const
{
    if(!m_player)
        return 0;

    return m_player->volume();
}

long PlayerManager::totalTime() const
{
    if(!m_player)
        return 0;

    return m_player->totalTime();
}

long PlayerManager::currentTime() const
97 98
{
    if(!m_player)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        return 0;

    return m_player->currentTime();
}

int PlayerManager::position() const
{
    if(!m_player)
        return 0;

    return m_player->position();
}

void PlayerManager::setPlaylistInterface(const PlaylistInterface *interface)
{
    m_playlistInterface = interface;
}

////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////

void PlayerManager::play(const QString &fileName)
{
    if(!m_player || !m_playlistInterface)
124 125
        return;

126 127 128 129 130
    if(fileName.isNull()) {
	if(m_player->paused())
            m_player->play();
        if(m_player->playing())
            m_player->seekPosition(0);
131 132
        else
            m_player->play(m_playlistInterface->nextFile());
133 134 135 136 137 138
    }
    else {
        if(m_player->paused())
            m_player->stop();
        m_player->play(fileName);
    }
139 140 141

    // Make sure that the m_player actually starts before doing anything.

142 143 144 145
    if(!m_player->playing()) {
        stop();
        return;
    }
146

147 148 149 150
    m_actionCollection->action("pause")->setEnabled(true);
    m_actionCollection->action("stop")->setEnabled(true);
    m_actionCollection->action("forward")->setEnabled(true);
    m_actionCollection->action("back")->setEnabled(true);
151

152 153
    m_sliderAction->trackPositionSlider()->setValue(0);
    m_sliderAction->trackPositionSlider()->setEnabled(true);
154 155

    m_timer->start(m_pollInterval);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
}

void PlayerManager::pause()
{
    if(!m_player)
        return;

    m_player->pause();
}

void PlayerManager::stop()
{
    if(!m_player)
        return;

    m_player->stop();
}

Laurent Montel's avatar
Laurent Montel committed
174
void PlayerManager::setVolume(float volume)
175 176 177 178 179 180 181
{
    if(!m_player)
        return;

    m_player->setVolume(volume);
}

182
void PlayerManager::seek(long seekTime)
183 184
{
    if(!m_player)
185
        return;
186

187
    m_player->seek(seekTime);
188 189
}

190
void PlayerManager::seekPosition(int position)
191 192
{
    if(!m_player)
193
        return;
194

195
    m_player->seekPosition(position);
196 197
}

198 199 200
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
201

202
void PlayerManager::slotPollPlay()
203
{
204 205
    if(!m_player || !m_playlistInterface)
        return;
206

207
    m_noSeek = true;
208

209 210 211 212 213 214
    if(!m_player->playing()) {
        m_timer->stop();
        play(m_playlistInterface->nextFile());
    }
    else if(!m_sliderAction->dragging()) {
        m_sliderAction->trackPositionSlider()->setValue(m_player->position());
215

216
        // TODO: figure out something to do with the status bar interface
217

218 219 220
        // m_statusLabel->setItemTotalTime(m_player->totalTime());
        // m_statusLabel->setItemCurrentTime(m_player->currentTime());
    }
221

222 223 224 225
    // 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.
226

227 228 229 230 231 232
    if(m_player->playing() &&
       m_player->totalTime() > 0 &&
       float(m_player->totalTime() - m_player->currentTime()) < m_pollInterval * 2)
    {
        m_timer->changeInterval(50);
    }
233

234
    m_noSeek = false;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}

////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

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;
    }

    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 = static_cast<SliderAction *>(m_actionCollection->action("trackPositionAction"));
280 281 282 283 284 285 286 287 288

    KAction *outputAction = m_actionCollection->action("outputSelect");

    if(outputAction) {
        int mediaSystem = static_cast<KSelectAction *>(outputAction)->currentItem();
        m_player = Player::createPlayer(mediaSystem);
    }
    else
        m_player = Player::createPlayer();
289 290 291

    m_timer = new QTimer(this, "play timer");
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
292 293 294
}

#include "playermanager.moc"