playermanager.cpp 5.85 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/***************************************************************************
                          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>

#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),
    m_player(0)
{
    setup();
}

PlayerManager::~PlayerManager()
{

}

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

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

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

65 66 67 68 69 70 71 72 73 74 75
    if(fileName.isNull()) {
	if(m_player->paused())
            m_player->play();
        if(m_player->playing())
            m_player->seekPosition(0);
    }
    else {
        if(m_player->paused())
            m_player->stop();
        m_player->play(fileName);
    }
76 77 78

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

79 80 81 82
    if(!m_player->playing()) {
        stop();
        return;
    }
83

84 85 86 87
    m_actionCollection->action("pause")->setEnabled(true);
    m_actionCollection->action("stop")->setEnabled(true);
    m_actionCollection->action("forward")->setEnabled(true);
    m_actionCollection->action("back")->setEnabled(true);
88

89 90 91
    m_sliderAction->trackPositionSlider()->setValue(0);
    m_sliderAction->trackPositionSlider()->setEnabled(true);
    // m_playTimer->start(m_pollInterval);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
}

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
110
void PlayerManager::setVolume(float volume)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
{
    if(!m_player)
        return;

    m_player->setVolume(volume);
}

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

    return m_player->volume();
}

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

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

    return m_player->totalTime();
}

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

    return m_player->currentTime();
}

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

    return m_player->position();
}

void PlayerManager::seek(long seekTime)
{
    if(!m_player)
        return;

    m_player->seek(seekTime);
}

void PlayerManager::seekPosition(int position)
{
    if(!m_player)
        return;

    m_player->seekPosition(position);
}

////////////////////////////////////////////////////////////////////////////////
// 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"));
225 226 227 228 229 230 231 232 233

    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();
234 235 236
}

#include "playermanager.moc"