~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kspread/FunctionModuleRegistry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
*/
19
19
 
20
20
#include "FunctionModuleRegistry.h"
21
 
#include "part/Factory.h" // FIXME detach from part
22
 
#include "FunctionModule.h"
23
 
#include "Functions.h"
 
21
 
 
22
#include "Function.h"
 
23
#include "FunctionRepository.h"
24
24
 
25
25
#include <KDebug>
26
26
#include <KGlobal>
30
30
 
31
31
using namespace KSpread;
32
32
 
33
 
class FunctionModuleRegistrySingleton
34
 
{
35
 
public:
36
 
    FunctionModuleRegistry instance;
37
 
};
38
 
 
39
 
K_GLOBAL_STATIC(FunctionModuleRegistrySingleton, s_singleton)
40
 
 
41
 
 
42
33
class FunctionModuleRegistry::Private
43
34
{
44
35
public:
45
36
    void registerFunctionModule(FunctionModule* module);
 
37
    void removeFunctionModule(FunctionModule* module);
46
38
 
47
39
public:
48
40
    bool repositoryInitialized;
50
42
 
51
43
void FunctionModuleRegistry::Private::registerFunctionModule(FunctionModule* module)
52
44
{
53
 
    module->registerFunctions();
 
45
    const QList<QSharedPointer<Function> > functions = module->functions();
 
46
    for (int i = 0; i < functions.count(); ++i) {
 
47
        FunctionRepository::self()->add(functions[i]);
 
48
    }
54
49
    Q_ASSERT(!module->descriptionFileName().isEmpty());
55
 
    const KStandardDirs* dirs = Factory::global().dirs();
 
50
    const KStandardDirs* dirs = KGlobal::activeComponent().dirs();
56
51
    const QString fileName = dirs->findResource("functions", module->descriptionFileName());
57
52
    if (fileName.isEmpty()) {
58
53
        kDebug(36002) << module->descriptionFileName() << "not found.";
 
54
        return;
59
55
    }
60
56
    FunctionRepository::self()->loadFunctionDescriptions(fileName);
61
57
}
62
58
 
 
59
void FunctionModuleRegistry::Private::removeFunctionModule(FunctionModule* module)
 
60
{
 
61
    const QList<QSharedPointer<Function> > functions = module->functions();
 
62
    for (int i = 0; i < functions.count(); ++i) {
 
63
        FunctionRepository::self()->remove(functions[i]);
 
64
    }
 
65
}
 
66
 
63
67
 
64
68
FunctionModuleRegistry::FunctionModuleRegistry()
65
69
        : d(new Private)
66
70
{
67
71
    d->repositoryInitialized = false;
68
 
    loadFunctions();
69
72
}
70
73
 
71
74
FunctionModuleRegistry::~FunctionModuleRegistry()
75
78
 
76
79
FunctionModuleRegistry* FunctionModuleRegistry::instance()
77
80
{
78
 
    return &s_singleton->instance;
 
81
    K_GLOBAL_STATIC(FunctionModuleRegistry, s_instance)
 
82
    return s_instance;
79
83
}
80
84
 
81
 
void FunctionModuleRegistry::loadFunctions()
 
85
void FunctionModuleRegistry::loadFunctionModules()
82
86
{
83
 
    const QString serviceType = QString::fromLatin1("KSpread/Plugin");
84
 
    const QString query = QString::fromLatin1("([X-KSpread-Version] >= 2) and "
85
 
                          "([X-KDE-PluginInfo-Category] == 'FunctionModule')");
 
87
    const quint32 minKSpreadVersion = KOFFICE_MAKE_VERSION(2, 1, 0);
 
88
    const QString serviceType = QLatin1String("KSpread/Plugin");
 
89
    const QString query = QLatin1String("([X-KSpread-InterfaceVersion] == 0) and "
 
90
                                        "([X-KDE-PluginInfo-Category] == 'FunctionModule')");
86
91
    const KService::List offers = KServiceTypeTrader::self()->query(serviceType, query);
87
92
    const KConfigGroup moduleGroup = KGlobal::config()->group("Plugins");
88
93
    const KPluginInfo::List pluginInfos = KPluginInfo::fromServices(offers, moduleGroup);
 
94
    kDebug(36002) << pluginInfos.count() << "function modules found.";
89
95
    foreach(KPluginInfo pluginInfo, pluginInfos) {
90
 
        KPluginFactory *factory = KPluginLoader(*pluginInfo.service()).factory();
91
 
        if (!factory) {
92
 
            kDebug(36002) << "Unable to create plugin factory for" << pluginInfo.name();
93
 
            continue;
94
 
        }
95
 
        FunctionModule* module = factory->create<FunctionModule>(this);
96
 
        if (!module) {
97
 
            kDebug(36002) << "Unable to create function module for" << pluginInfo.name();
98
 
            continue;
99
 
        }
100
96
        pluginInfo.load(); // load activation state
101
 
        if (pluginInfo.isPluginEnabled()) {
102
 
            // Module already registered?
103
 
            if (contains(module->id())) {
104
 
                continue;
105
 
            }
106
 
            add(module);
107
 
            // Is the function repository already initialized?
 
97
        KPluginLoader loader(*pluginInfo.service());
 
98
        // Let's be paranoid: do not believe the service type.
 
99
        if (loader.pluginVersion() < minKSpreadVersion) {
 
100
            kDebug(36002) << pluginInfo.name()
 
101
            << "was built against KSpread" << loader.pluginVersion()
 
102
            << "; required version >=" << minKSpreadVersion;
 
103
            continue;
 
104
        }
 
105
        if (pluginInfo.isPluginEnabled() && !contains(pluginInfo.pluginName())) {
 
106
            // Plugin enabled, but not registered. Add it.
 
107
            KPluginFactory* const factory = loader.factory();
 
108
            if (!factory) {
 
109
                kDebug(36002) << "Unable to create plugin factory for" << pluginInfo.name();
 
110
                continue;
 
111
            }
 
112
            FunctionModule* const module = factory->create<FunctionModule>(this);
 
113
            if (!module) {
 
114
                kDebug(36002) << "Unable to create function module for" << pluginInfo.name();
 
115
                continue;
 
116
            }
 
117
            add(pluginInfo.pluginName(), module);
 
118
 
 
119
            // Delays the function registration until the user needs one.
108
120
            if (d->repositoryInitialized) {
109
121
                d->registerFunctionModule(module);
110
122
            }
111
 
        } else {
112
 
            // Module not registered?
113
 
            if (!contains(module->id())) {
114
 
                continue;
115
 
            }
116
 
            module->removeFunctions();
117
 
            remove(module->id());
 
123
        } else if (!pluginInfo.isPluginEnabled() && contains(pluginInfo.pluginName())) {
 
124
            // Plugin disabled, but registered. Remove it.
 
125
            FunctionModule* const module = get(pluginInfo.pluginName());
 
126
            // Delay the function registration until the user needs one.
 
127
            if (d->repositoryInitialized) {
 
128
                d->removeFunctionModule(module);
 
129
            }
 
130
            remove(pluginInfo.pluginName());
 
131
            if (module->isRemovable()) {
 
132
                delete module;
 
133
                delete loader.factory();
 
134
                loader.unload();
 
135
            } else {
 
136
                // Put it back in.
 
137
                add(pluginInfo.pluginName(), module);
 
138
                // Delay the function registration until the user needs one.
 
139
                if (d->repositoryInitialized) {
 
140
                    d->registerFunctionModule(module);
 
141
                }
 
142
            }
118
143
        }
119
144
    }
120
145
}