~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to noatun/noatun/library/pluginloader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <qfile.h>
 
2
#include <kglobal.h>
 
3
#include <qdir.h>
 
4
#include <ksimpleconfig.h>
 
5
#include <kstddirs.h>
 
6
#include <knotifyclient.h>
 
7
#include <klocale.h>
 
8
#include <kurl.h>
 
9
#include <pluginloader.h>
 
10
#include <plugin.h>
 
11
#include <kdebug.h>
 
12
 
 
13
bool operator ==(const NoatunLibraryInfo &a, const NoatunLibraryInfo &b)
 
14
{
 
15
        // Feels like cheating, doesn't it?
 
16
        return a.specfile == b.specfile;
 
17
}
 
18
 
 
19
LibraryLoader::LibraryLoader() : mPlaylist(0)
 
20
{
 
21
}
 
22
 
 
23
LibraryLoader::~LibraryLoader()
 
24
{
 
25
        QValueList<NoatunLibraryInfo> l;
 
26
 
 
27
        l = loaded();
 
28
        for(QValueList<NoatunLibraryInfo>::Iterator i = l.begin(); i != l.end(); ++i)
 
29
        {
 
30
                if((*i).type != "userinterface" && (*i).type != "playlist" && (*i).type != "systray")
 
31
                {
 
32
                        removeNow((*i).specfile);
 
33
                }
 
34
        }
 
35
        
 
36
        l = loaded();
 
37
        for(QValueList<NoatunLibraryInfo>::Iterator i = l.begin(); i != l.end(); ++i)
 
38
        {
 
39
                if((*i).type == "userinterface")
 
40
                {
 
41
                        removeNow((*i).specfile);
 
42
                }
 
43
        }
 
44
        
 
45
        l = loaded();
 
46
        for(QValueList<NoatunLibraryInfo>::Iterator i = l.begin(); i != l.end(); ++i)
 
47
        {
 
48
                removeNow((*i).specfile);
 
49
        }
 
50
}
 
51
 
 
52
QValueList<NoatunLibraryInfo> LibraryLoader::available() const
 
53
{
 
54
        QValueList<NoatunLibraryInfo> items;
 
55
        QStringList files=KGlobal::dirs()->findAllResources("appdata", "*.plugin", false, true);
 
56
        for (QStringList::Iterator i=files.begin(); i!=files.end(); ++i)
 
57
                items.append(getInfo(*i));
 
58
 
 
59
        return items;
 
60
}
 
61
 
 
62
QList<Plugin> LibraryLoader::plugins() const
 
63
{
 
64
        QList<Plugin> list;
 
65
        for (QDictIterator<LibraryLoader::PluginLibrary> i(mLibHash); i.current(); ++i)
 
66
                list.append(i.current()->plugin);
 
67
        return list;
 
68
}
 
69
 
 
70
bool LibraryLoader::loadAll(void)
 
71
{
 
72
        KConfig *config=KGlobal::config();
 
73
        config->setGroup("");
 
74
        QStringList modules = config->readListEntry("Modules");
 
75
        return loadAll(modules);
 
76
}
 
77
 
 
78
bool LibraryLoader::loadAll(const QStringList &modules)
 
79
{
 
80
        // Session management...
 
81
        for(QStringList::ConstIterator i=modules.begin(); i!=modules.end(); ++i)
 
82
        {
 
83
                NoatunLibraryInfo info=getInfo(*i);
 
84
                if (!info.type.contains("sm"))
 
85
                        continue;
 
86
 
 
87
                loadSO(*i);
 
88
        }
 
89
 
 
90
        // load all the playlists in the first
 
91
        for(QStringList::ConstIterator i=modules.begin(); i!=modules.end(); ++i)
 
92
        {
 
93
                NoatunLibraryInfo info=getInfo(*i);
 
94
                if (!info.type.contains("playlist"))
 
95
                        continue;
 
96
 
 
97
                loadSO(*i);
 
98
        }
 
99
 
 
100
        // load all the user interfaces now
 
101
        for(QStringList::ConstIterator i=modules.begin(); i!=modules.end(); ++i)
 
102
        {
 
103
                NoatunLibraryInfo info=getInfo(*i);
 
104
                if (!info.type.contains("userinterface"))
 
105
                        continue;
 
106
                loadSO(*i);
 
107
        }
 
108
 
 
109
        for(QStringList::ConstIterator i=modules.begin(); i!=modules.end(); ++i)
 
110
        {
 
111
                NoatunLibraryInfo info=getInfo(*i);
 
112
                if((!info.type.contains("playlist")) 
 
113
                && (!info.type.contains("userinterface"))
 
114
                && (!info.type.contains("sm")))
 
115
                {
 
116
                    loadSO(*i);
 
117
                }
 
118
        }
 
119
        return true;
 
120
}
 
121
 
 
122
NoatunLibraryInfo LibraryLoader::getInfo(const QString &spec) const
 
123
{
 
124
        NoatunLibraryInfo info;
 
125
        QString specPath = (spec[0]=='/') ? spec : KGlobal::dirs()->findResource("appdata", spec);
 
126
        if (!QFile::exists(specPath))
 
127
                return info;
 
128
        KSimpleConfig file(specPath);
 
129
        if (spec.find('/')>=0)
 
130
                info.specfile=KURL(spec).fileName();
 
131
        else
 
132
                info.specfile=spec;
 
133
        info.filename=file.readEntry("Filename");
 
134
        info.author=file.readEntry("Author");
 
135
        info.site=file.readEntry("Site");
 
136
        info.email=file.readEntry("Email");
 
137
        info.type=file.readEntry("Type");
 
138
        info.name=file.readEntry("Name");
 
139
        info.comment=file.readEntry("Comment");
 
140
        info.require=file.readListEntry("Require");
 
141
        info.license=file.readEntry("License");
 
142
        return info;
 
143
}
 
144
 
 
145
bool LibraryLoader::isLoaded(const QString &spec) const
 
146
{
 
147
        PluginLibrary *lib=mLibHash[spec];
 
148
        if (!lib) return false;
 
149
        return lib->plugin;
 
150
}
 
151
 
 
152
bool LibraryLoader::loadSO(const QString &spec)
 
153
{
 
154
        if(!isLoaded(spec))
 
155
        {
 
156
                NoatunLibraryInfo info = getInfo(spec);
 
157
                if (info.specfile != spec)
 
158
                        return false;
 
159
        
 
160
                for (QStringList::ConstIterator it = info.require.begin(); it != info.require.end(); ++it)
 
161
                        loadSO(*it);
 
162
        
 
163
                // get the library loader instance
 
164
                KLibLoader *loader = KLibLoader::self();
 
165
 
 
166
                PluginLibrary *listitem=mLibHash[spec];
 
167
        
 
168
                if (!listitem)
 
169
                {
 
170
                        KLibrary *lib = loader->library(QFile::encodeName(info.filename));
 
171
                        if (!lib)
 
172
                                return false;
 
173
                        listitem=new PluginLibrary;
 
174
                        listitem->library=lib;
 
175
                        mLibHash.insert(spec, listitem);
 
176
                }
 
177
 
 
178
                void *create=listitem->library->symbol("create_plugin");
 
179
                if (!create)
 
180
                        return false;
 
181
 
 
182
                Plugin* (*plugInStart)();
 
183
                plugInStart= (Plugin* (*)()) create;
 
184
                listitem->plugin=plugInStart();
 
185
 
 
186
                if (getInfo(spec).type=="playlist")
 
187
                        mPlaylist=listitem->plugin->playlist();
 
188
                listitem->plugin->init();
 
189
        
 
190
                return true;
 
191
        }
 
192
        else
 
193
                return false;
 
194
}
 
195
 
 
196
void LibraryLoader::add(const QString &spec)
 
197
{
 
198
        PluginLibrary *lib=mLibHash[spec];
 
199
        if (lib)
 
200
                if (lib->plugin) return;
 
201
 
 
202
        loadSO(spec);
 
203
}
 
204
 
 
205
void LibraryLoader::setModules(const QStringList &mods)
 
206
{
 
207
        KConfig *config=KGlobal::config();
 
208
        config->setGroup(0);
 
209
        config->writeEntry("Modules", mods);
 
210
        KGlobal::config()->sync();
 
211
}
 
212
 
 
213
bool LibraryLoader::remove(const QString &spec)
 
214
{
 
215
        removeNow(spec);
 
216
 
 
217
        // exit if this is the last UI
 
218
        if (getInfo(spec).type=="userinterface")
 
219
        {
 
220
                QValueList<NoatunLibraryInfo> l=loaded();
 
221
                bool isanotherui=false;
 
222
                for (QValueList<NoatunLibraryInfo>::Iterator i=l.begin(); i!=l.end(); ++i)
 
223
                {
 
224
                        if ((*i).specfile!=spec && (*i).type=="userinterface")
 
225
                                isanotherui=true;
 
226
                }               
 
227
                if (!isanotherui)
 
228
                        kapp->exit();
 
229
        }
 
230
 
 
231
        return true;
 
232
}
 
233
 
 
234
bool LibraryLoader::remove(const PluginLibrary *pl)
 
235
{
 
236
        for (QDictIterator<PluginLibrary> i(mLibHash); i.current(); ++i)
 
237
        {
 
238
                if (i.current()==pl)
 
239
                        return remove(i.currentKey());
 
240
        }
 
241
        return false;
 
242
}
 
243
 
 
244
bool LibraryLoader::remove(const Plugin *plugin)
 
245
{
 
246
        for (QDictIterator<PluginLibrary> i(mLibHash); i.current(); ++i)
 
247
        {
 
248
                if (i.current()->plugin==plugin)
 
249
                        return remove(i.currentKey());
 
250
        }
 
251
        return false;
 
252
 
 
253
}
 
254
 
 
255
Playlist *LibraryLoader::playlist() const
 
256
{
 
257
        return mPlaylist;
 
258
}
 
259
 
 
260
QValueList<NoatunLibraryInfo> LibraryLoader::loaded() const
 
261
{
 
262
        QValueList<NoatunLibraryInfo> items;
 
263
 
 
264
        for (QDictIterator<PluginLibrary> i(mLibHash); i.current(); ++i)
 
265
                if (isLoaded(i.currentKey()))
 
266
                        items.append(getInfo(i.currentKey()));
 
267
 
 
268
        return items;
 
269
}
 
270
 
 
271
void LibraryLoader::removeNow(const QString &spec)
 
272
{
 
273
        NoatunLibraryInfo info = getInfo(spec);
 
274
        if (info.specfile == spec)
 
275
        {
 
276
                QValueList<NoatunLibraryInfo> l = loaded();
 
277
                for (QValueList<NoatunLibraryInfo>::Iterator i = l.begin(); i != l.end(); ++i)
 
278
                {
 
279
                        for (QStringList::ConstIterator it = (*i).require.begin(); it != (*i).require.end(); ++it)
 
280
                                if (*it == spec)
 
281
                                removeNow((*i).specfile);
 
282
                }
 
283
        }
 
284
        
 
285
        PluginLibrary *lib=mLibHash[spec];
 
286
 
 
287
        if (!lib) return;
 
288
 
 
289
        delete lib->plugin;
 
290
        lib->plugin=0;
 
291
        mLibHash.remove(spec);
 
292
//      delete lib->library;
 
293
//      delete lib;
 
294
}
 
295
 
 
296