~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/nowplaying/playerinterface/pollingwatcher.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "pollingwatcher.h"
 
2
 
 
3
#include <QTimer>
 
4
 
 
5
#include <KDebug>
 
6
 
 
7
#include "player.h"
 
8
#include "playerfactory.h"
 
9
 
 
10
PollingWatcher::PollingWatcher(QObject* parent)
 
11
    : QObject(parent),
 
12
      m_timer(0)
 
13
{
 
14
    setObjectName( QLatin1String("PollingWatcher" ));
 
15
}
 
16
 
 
17
QList<Player::Ptr> PollingWatcher::players()
 
18
{
 
19
    return m_players.values();
 
20
}
 
21
 
 
22
void PollingWatcher::addFactory(PollingPlayerFactory* factory)
 
23
{
 
24
    if (factory->exists()) {
 
25
        Player::Ptr player = factory->create();
 
26
        if (!player.isNull()) {
 
27
            m_players.insert(player);
 
28
            m_usedFactories.insert(factory);
 
29
            emit newPlayer(player);
 
30
        } else {
 
31
            kWarning() << "Failed to create a player";
 
32
            m_polledFactories.insert(factory);
 
33
        }
 
34
    } else {
 
35
        m_polledFactories.insert(factory);
 
36
    }
 
37
 
 
38
    if (!m_timer) {
 
39
        m_timer = new QTimer(this);
 
40
        m_timer->setInterval(5000);
 
41
        connect(m_timer, SIGNAL(timeout()), this, SLOT(checkPlayers()));
 
42
        m_timer->start();
 
43
    }
 
44
}
 
45
 
 
46
void PollingWatcher::checkPlayers()
 
47
{
 
48
    foreach (Player::Ptr player, m_players) {
 
49
        if (!player->isRunning()) {
 
50
            m_players.remove(player);
 
51
            PollingPlayerFactory* factory =
 
52
                    qobject_cast<PollingPlayerFactory*>(player->factory());
 
53
            if (factory) {
 
54
                m_usedFactories.remove(factory);
 
55
                m_polledFactories.insert(factory);
 
56
            } else {
 
57
                kWarning() << "Missing factory for player" << player->name();
 
58
            }
 
59
            emit playerDisappeared(player);
 
60
        }
 
61
    }
 
62
    foreach (PollingPlayerFactory* factory, m_polledFactories) {
 
63
        if (factory->exists()) {
 
64
            Player::Ptr player = factory->create();
 
65
            if (!player.isNull()) {
 
66
                m_players.insert(player);
 
67
                m_polledFactories.remove(factory);
 
68
                m_usedFactories.insert(factory);
 
69
                emit newPlayer(player);
 
70
            } else {
 
71
                kWarning() << "Failed to create a player";
 
72
            }
 
73
        }
 
74
    }
 
75
    m_timer->start();
 
76
}
 
77