Refactor the Variants factory to clearly separate KDE-specific stuff.
[tagua.git] / src / variantfactories / kdepluginvariants.cpp
blobee99ce8b80da4efadac4fba1ab88fdff110189f8
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "kdepluginvariants.h"
12 #include <memory>
13 #include <KDebug>
14 #include "foreach.h"
15 #include "variant.h"
17 void load_plugins();
19 typedef Repository* PluginFactory(IVariantLoader*);
21 Repository* KDEPluginVariants::load(const KService::Ptr& plugin) {
22 // load plugin
23 QString symbol = plugin->library() + "_initrepo";
24 KLibrary pluginLib(plugin->library());
26 // resolve initrepo function
27 void* initrepo_ = pluginLib.resolveSymbol(qPrintable(symbol));
28 if (!initrepo_) {
29 kWarning() << "Error loading plugin" << plugin->library();
30 kWarning() << pluginLib.errorString();
31 return false;
33 PluginFactory* initrepo = reinterpret_cast<PluginFactory*>(initrepo_);
35 return (*initrepo)(this);
38 KDEPluginVariants::KDEPluginVariants() { }
40 KDEPluginVariants& KDEPluginVariants::self() {
41 static KDEPluginVariants inst;
42 return inst;
45 Variant* KDEPluginVariants::create(const KService::Ptr& plugin) {
46 Repository* repo = load(plugin);
47 if (repo)
48 return new Variant(plugin->name(), repo,
49 plugin->property("X-Tagua-Proxy").toString());
51 return NULL;
54 Variant* KDEPluginVariants::create(const QString& name) {
55 KService::Ptr service = get(name);
56 return service ? create(service) : 0;
59 KService::Ptr KDEPluginVariants::get(const QString& name) {
60 KService::List services =
61 KServiceTypeTrader::self()->query("Tagua/Variant",
62 "Name =~ '" + name + "'");
63 if (services.empty())
64 return KService::Ptr();
65 else
66 return services[0];
69 QStringList KDEPluginVariants::all() const {
70 KService::List klist = KServiceTypeTrader::self()->query(
71 "Tagua/Variant", "[X-Tagua-Hidden] == false");
72 QStringList result;
73 foreach (KService::Ptr service, klist) {
74 result << service->name();
76 return result;
79 Repository* KDEPluginVariants::getRepository(const QString& variant) {
80 KService::Ptr service = get(variant);
81 if (!service) return 0;
83 return load(service);