~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to libs/plasma/dataenginemanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU Library General Public License as
 
6
 *   published by the Free Software Foundation; either version 2, or
 
7
 *   (at your option) any later version.
 
8
 *
 
9
 *   This program is distributed in the hope that it will be useful,
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *   GNU General Public License for more details
 
13
 *
 
14
 *   You should have received a copy of the GNU Library General Public
 
15
 *   License along with this program; if not, write to the
 
16
 *   Free Software Foundation, Inc.,
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "dataenginemanager.h"
 
21
 
 
22
#include <KDebug>
 
23
#include <KServiceTypeTrader>
 
24
 
 
25
namespace Plasma
 
26
{
 
27
 
 
28
class NullEngine : public DataEngine
 
29
{
 
30
    public:
 
31
        NullEngine(QObject* parent = 0)
 
32
            : DataEngine(parent)
 
33
        {
 
34
            setObjectName(i18n("Null Engine"));
 
35
            setValid(false);
 
36
 
 
37
            // ref() ourselves to ensure we never get deleted
 
38
            ref();
 
39
        }
 
40
};
 
41
 
 
42
class DataEngineManager::Private
 
43
{
 
44
    public:
 
45
        Private()
 
46
            : nullEng(0)
 
47
        {}
 
48
 
 
49
        ~Private()
 
50
        {
 
51
            foreach (Plasma::DataEngine* engine, engines) {
 
52
                delete engine;
 
53
            }
 
54
            engines.clear();
 
55
            delete nullEng;
 
56
        }
 
57
 
 
58
        DataEngine* nullEngine()
 
59
        {
 
60
            if (!nullEng) {
 
61
                nullEng = new NullEngine;
 
62
            }
 
63
 
 
64
            return nullEng;
 
65
        }
 
66
 
 
67
        DataEngine::Dict engines;
 
68
        DataEngine* nullEng;
 
69
};
 
70
 
 
71
class DataEngineManagerSingleton
 
72
{
 
73
    public:
 
74
        DataEngineManager self;
 
75
};
 
76
 
 
77
K_GLOBAL_STATIC(DataEngineManagerSingleton, privateDataEngineManagerSelf)
 
78
 
 
79
DataEngineManager* DataEngineManager::self()
 
80
{
 
81
    return &privateDataEngineManagerSelf->self;
 
82
}
 
83
 
 
84
DataEngineManager::DataEngineManager()
 
85
    : d(new Private())
 
86
{
 
87
}
 
88
 
 
89
DataEngineManager::~DataEngineManager()
 
90
{
 
91
    delete d;
 
92
}
 
93
 
 
94
Plasma::DataEngine* DataEngineManager::dataEngine(const QString& name) const
 
95
{
 
96
    Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
 
97
    if (it != d->engines.end()) {
 
98
        // ref and return the engine
 
99
        //Plasma::DataEngine *engine = *it;
 
100
        return *it;
 
101
    }
 
102
 
 
103
    return d->nullEngine();
 
104
}
 
105
 
 
106
Plasma::DataEngine* DataEngineManager::loadDataEngine(const QString& name)
 
107
{
 
108
    Plasma::DataEngine* engine = 0;
 
109
    Plasma::DataEngine::Dict::const_iterator it = d->engines.find(name);
 
110
 
 
111
    if (it != d->engines.end()) {
 
112
        engine = *it;
 
113
        engine->ref();
 
114
        return engine;
 
115
    }
 
116
 
 
117
    // load the engine, add it to the engines
 
118
    QString constraint = QString("[X-EngineName] == '%1'").arg(name);
 
119
    KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine",
 
120
                                                              constraint);
 
121
    QString error;
 
122
 
 
123
    if (offers.isEmpty()) {
 
124
        kDebug() << "offers are empty for " << name << " with constraint " << constraint;
 
125
    } else {
 
126
        engine = KService::createInstance<Plasma::DataEngine>(offers.first(), 0, QVariantList(), &error);
 
127
    }
 
128
 
 
129
    if (!engine) {
 
130
        kDebug() << "Couldn't load engine \"" << name << "\". Error given: " << error;
 
131
        return d->nullEngine();
 
132
    }
 
133
 
 
134
    engine->ref();
 
135
    engine->setObjectName(offers.first()->name());
 
136
    engine->setIcon(offers.first()->icon());
 
137
    d->engines[name] = engine;
 
138
    return engine;
 
139
}
 
140
 
 
141
void DataEngineManager::unloadDataEngine(const QString& name)
 
142
{
 
143
    Plasma::DataEngine::Dict::iterator it = d->engines.find(name);
 
144
 
 
145
    if (it != d->engines.end()) {
 
146
        Plasma::DataEngine* engine = *it;
 
147
        engine->deref();
 
148
 
 
149
        if (!engine->isUsed()) {
 
150
            d->engines.erase(it);
 
151
            delete engine;
 
152
        }
 
153
    }
 
154
}
 
155
 
 
156
QStringList DataEngineManager::knownEngines()
 
157
{
 
158
    QStringList engines;
 
159
    KService::List offers = KServiceTypeTrader::self()->query("Plasma/DataEngine");
 
160
    foreach (KService::Ptr service, offers) {
 
161
        engines.append(service->property("X-EngineName").toString());
 
162
    }
 
163
 
 
164
    return engines;
 
165
}
 
166
 
 
167
} // namespace Plasma
 
168
 
 
169
#include "dataenginemanager.moc"