File: FunctionRepository.cpp

package info (click to toggle)
calligra 1%3A2.9.11%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 189,332 kB
  • sloc: cpp: 919,806; xml: 27,759; ansic: 10,472; python: 8,190; perl: 2,724; yacc: 2,557; sh: 1,675; lex: 1,431; java: 1,304; sql: 903; ruby: 734; makefile: 48
file content (197 lines) | stat: -rw-r--r-- 6,067 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* This file is part of the KDE project
   Copyright (C) 2003,2004 Ariya Hidayat <ariya@kde.org>
   Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; only
   version 2 of the License.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// Local
#include "FunctionRepository.h"

#include "Function.h"
#include "FunctionDescription.h"
#include "FunctionModuleRegistry.h"

#include <QDomElement>
#include <QDomNode>
#include <QFile>
#include <QHash>

#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>

using namespace Calligra::Sheets;

class  FunctionRepository::Private
{
public:
    QHash<QString, QSharedPointer<Function> > functions;
    QHash<QString, QSharedPointer<Function> > alternates;
    QHash<QString, FunctionDescription*> descriptions;
    QStringList groups;
    bool initialized;
};

FunctionRepository* FunctionRepository::self()
{
    K_GLOBAL_STATIC(FunctionRepository, s_instance)
    if (!s_instance.exists()) {
        *s_instance; // creates the global instance

        // register all existing functions
        FunctionModuleRegistry::instance()->registerFunctions();

#ifndef NDEBUG
        kDebug(36005) << "functions registered:" << s_instance->d->functions.count()
                      << "descriptions loaded:" << s_instance->d->descriptions.count();

        // Verify, that every function has a description.
        QStringList missingDescriptions;
        typedef QHash<QString, QSharedPointer<Function> > Functions;
        Functions::ConstIterator end = s_instance->d->functions.constEnd();
        for (Functions::ConstIterator it = s_instance->d->functions.constBegin(); it != end; ++it) {
            if (!s_instance->d->descriptions.contains(it.key()))
                missingDescriptions << it.key();
        }
        if (missingDescriptions.count() > 0) {
            kDebug(36005) << "No function descriptions found for:";
            foreach(const QString& missingDescription, missingDescriptions) {
                kDebug(36005) << "\t" << missingDescription;
            }
        }
#endif
    }
    return s_instance;
}

FunctionRepository::FunctionRepository()
        : d(new Private)
{
    d->initialized = false;
}

FunctionRepository::~FunctionRepository()
{
    qDeleteAll(d->descriptions);
    delete d;
}

void FunctionRepository::add(const QSharedPointer<Function>& function)
{
    if (!function) return;
    d->functions.insert(function->name().toUpper(), function);

    if (!function->alternateName().isNull()) {
        d->alternates.insert(function->alternateName().toUpper(), function);
    }
}

void FunctionRepository::add(FunctionDescription *desc)
{
    if (!desc) return;
    if (!d->functions.contains(desc->name())) return;
    d->descriptions.insert(desc->name(), desc);
}

void FunctionRepository::remove(const QSharedPointer<Function>& function)
{
    const QString functionName = function->name().toUpper();
    delete d->descriptions.take(functionName);
    if (d->functions.contains(functionName)) {
        QSharedPointer<Function> function = d->functions.take(functionName);
        d->alternates.remove(function->alternateName().toUpper());
    }
}

QSharedPointer<Function> FunctionRepository::function(const QString& name)
{
    const QString n = name.toUpper();
    QSharedPointer<Function> f = d->functions.value(n);
    return !f.isNull() ? f : d->alternates.value(n);
}

FunctionDescription *FunctionRepository::functionInfo(const QString& name)
{
    return d->descriptions.value(name.toUpper());
}

// returns names of function in certain group
QStringList FunctionRepository::functionNames(const QString& group)
{
    QStringList lst;

    foreach(FunctionDescription* description, d->descriptions) {
        if (group.isNull() || (description->group() == group))
            lst.append(description->name());
    }

    lst.sort();
    return lst;
}

const QStringList& FunctionRepository::groups() const
{
    return d->groups;
}

void FunctionRepository::addGroup(const QString& groupname)
{
    d->groups.append(groupname);
    d->groups.sort();
}

void FunctionRepository::loadFunctionDescriptions(const QString& filename)
{
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly))
        return;

    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QString group;

    QDomNode n = doc.documentElement().firstChild();
    for (; !n.isNull(); n = n.nextSibling()) {
        if (!n.isElement())
            continue;
        QDomElement e = n.toElement();
        if (e.tagName() == "Group") {
            group = i18n(e.namedItem("GroupName").toElement().text().toUtf8());
            addGroup(group);

            QDomNode n2 = e.firstChild();
            for (; !n2.isNull(); n2 = n2.nextSibling()) {
                if (!n2.isElement())
                    continue;
                QDomElement e2 = n2.toElement();
                if (e2.tagName() == "Function") {
                    FunctionDescription* desc = new FunctionDescription(e2);
                    desc->setGroup(group);
                    if (d->functions.contains(desc->name()))
                        d->descriptions.insert(desc->name(), desc);
                    else {
                        kDebug(36005) << "Description for unknown function" << desc->name() << "found.";
                        delete desc;
                    }
                }
            }
            group.clear();
        }
    }
}