File: lists.cc

package info (click to toggle)
kig 4%3A25.08.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,716 kB
  • sloc: cpp: 41,465; xml: 851; python: 486; perl: 23; sh: 17; makefile: 3
file content (356 lines) | stat: -rw-r--r-- 10,702 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// SPDX-FileCopyrightText: 2003 Dominique Devriese <devriese@kde.org>

// SPDX-License-Identifier: GPL-2.0-or-later

#include "lists.h"

#include "../kig/kig_part.h"
#include "guiaction.h"
#include "kig_version.h"
#include "object_constructor.h"
#include "object_hierarchy.h"

#include <KMessageBox>
#include <QFile>
#include <QTextStream>
#include <algorithm>
#include <iterator>
#include <qdom.h>
using namespace std;

template<typename T>
void vect_remove(std::vector<T> &v, const T &t)
{
    typename std::vector<T>::iterator new_end = std::remove(v.begin(), v.end(), t);
    v.erase(new_end, v.end());
}

GUIActionList *GUIActionList::instance()
{
    static GUIActionList l;
    return &l;
}

GUIActionList::~GUIActionList()
{
    for (avectype::iterator i = mactions.begin(); i != mactions.end(); ++i)
        delete *i;
}

GUIActionList::GUIActionList()
{
}

void GUIActionList::regDoc(KigPart *d)
{
    mdocs.insert(d);
}

void GUIActionList::unregDoc(KigPart *d)
{
    mdocs.erase(d);
}

void GUIActionList::add(const std::vector<GUIAction *> &a)
{
    copy(a.begin(), a.end(), inserter(mactions, mactions.begin()));
    for (dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i) {
        KigPart::GUIUpdateToken t = (*i)->startGUIActionUpdate();
        for (uint j = 0; j < a.size(); ++j)
            (*i)->actionAdded(a[j], t);
        (*i)->endGUIActionUpdate(t);
    };
}

void GUIActionList::add(GUIAction *a)
{
    mactions.insert(a);
    for (dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i) {
        KigPart::GUIUpdateToken t = (*i)->startGUIActionUpdate();
        (*i)->actionAdded(a, t);
        (*i)->endGUIActionUpdate(t);
    };
}

void GUIActionList::remove(const std::vector<GUIAction *> &a)
{
    for (uint i = 0; i < a.size(); ++i) {
        mactions.erase(a[i]);
    };
    for (dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i) {
        KigPart::GUIUpdateToken t = (*i)->startGUIActionUpdate();
        for (uint j = 0; j < a.size(); ++j)
            (*i)->actionRemoved(a[j], t);
        (*i)->endGUIActionUpdate(t);
    };
    delete_all(a.begin(), a.end());
}

void GUIActionList::remove(GUIAction *a)
{
    mactions.erase(a);
    for (dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i) {
        KigPart::GUIUpdateToken t = (*i)->startGUIActionUpdate();
        (*i)->actionRemoved(a, t);
        (*i)->endGUIActionUpdate(t);
    };
    delete a;
}

ObjectConstructorList::ObjectConstructorList()
{
}

ObjectConstructorList::~ObjectConstructorList()
{
    for (vectype::iterator i = mctors.begin(); i != mctors.end(); ++i)
        delete *i;
}

ObjectConstructorList *ObjectConstructorList::instance()
{
    static ObjectConstructorList s;
    return &s;
}

ObjectConstructorList::vectype
ObjectConstructorList::ctorsThatWantArgs(const std::vector<ObjectCalcer *> &os, const KigDocument &d, const KigWidget &w, bool co) const
{
    vectype ret;
    for (vectype::const_iterator i = mctors.begin(); i != mctors.end(); ++i) {
        int r = (*i)->wantArgs(os, d, w);
        if (r == ArgsParser::Complete || (!co && r == ArgsParser::Valid))
            ret.push_back(*i);
    };
    return ret;
}

void ObjectConstructorList::remove(ObjectConstructor *a)
{
    vect_remove(mctors, a);
    delete a;
}

void ObjectConstructorList::add(ObjectConstructor *a)
{
    mctors.push_back(a);
}

Macro::Macro(GUIAction *a, MacroConstructor *c)
    : action(a)
    , ctor(c)
{
}

bool operator==(const Macro &l, const Macro &r)
{
    return (l.action->descriptiveName() == r.action->descriptiveName()) && (l.action->description() == r.action->description())
        && (l.action->iconFileName() == r.action->iconFileName());
}

MacroList::MacroList()
{
}

MacroList::~MacroList()
{
    std::vector<GUIAction *> actions;
    std::vector<ObjectConstructor *> ctors;
    for (vectype::iterator i = mdata.begin(); i != mdata.end(); ++i) {
        Macro *m = *i;
        GUIAction *a = m->action;
        actions.push_back(a);
        ObjectConstructor *c = m->ctor;
        ctors.push_back(c);
        delete m;
    };
    mdata.clear();
    GUIActionList::instance()->remove(actions);
    for (uint i = 0; i < ctors.size(); ++i)
        ObjectConstructorList::instance()->remove(ctors[i]);
}

MacroList *MacroList::instance()
{
    static MacroList t;
    return &t;
}

void MacroList::add(const std::vector<Macro *> &ms)
{
    copy(ms.begin(), ms.end(), back_inserter(mdata));
    std::vector<GUIAction *> acts;
    for (uint i = 0; i < ms.size(); ++i) {
        ObjectConstructorList::instance()->add(ms[i]->ctor);
        acts.push_back(ms[i]->action);
    };
    GUIActionList::instance()->add(acts);
}

void MacroList::add(Macro *m)
{
    mdata.push_back(m);
    ObjectConstructorList::instance()->add(m->ctor);
    GUIActionList::instance()->add(m->action);
}

void MacroList::remove(Macro *m)
{
    GUIAction *a = m->action;
    ObjectConstructor *c = m->ctor;
    mdata.erase(std::remove(mdata.begin(), mdata.end(), m), mdata.end());
    delete m;
    GUIActionList::instance()->remove(a);
    ObjectConstructorList::instance()->remove(c);
}

const MacroList::vectype &MacroList::macros() const
{
    return mdata;
}

Macro::~Macro()
{
}

bool MacroList::save(Macro *m, const QString &f)
{
    std::vector<Macro *> ms;
    ms.push_back(m);
    return save(ms, f);
}

bool MacroList::save(const std::vector<Macro *> &ms, const QString &f)
{
    QDomDocument doc(QStringLiteral("KigMacroFile"));

    QDomElement docelem = doc.createElement(QStringLiteral("KigMacroFile"));
    docelem.setAttribute(QStringLiteral("Version"), KIG_VERSION_STRING);
    docelem.setAttribute(QStringLiteral("Number"), static_cast<uint>(ms.size()));

    for (uint i = 0; i < ms.size(); ++i) {
        MacroConstructor *ctor = ms[i]->ctor;

        QDomElement macroelem = doc.createElement(QStringLiteral("Macro"));

        // name
        QDomElement nameelem = doc.createElement(QStringLiteral("Name"));
        nameelem.appendChild(doc.createTextNode(ctor->descriptiveName()));
        macroelem.appendChild(nameelem);

        // desc
        QDomElement descelem = doc.createElement(QStringLiteral("Description"));
        descelem.appendChild(doc.createTextNode(ctor->description()));
        macroelem.appendChild(descelem);

        // icon
        QString icon = ctor->iconFileName(true);
        if (!icon.isNull()) {
            QDomElement descelem = doc.createElement(QStringLiteral("IconFileName"));
            descelem.appendChild(doc.createTextNode(icon));
            macroelem.appendChild(descelem);
        }

        // data
        QDomElement hierelem = doc.createElement(QStringLiteral("Construction"));
        ctor->hierarchy().serialize(hierelem, doc);
        macroelem.appendChild(hierelem);

        docelem.appendChild(macroelem);
    };

    doc.appendChild(docelem);

    QFile file(f);
    if (!file.open(QIODevice::WriteOnly))
        return false;
    QTextStream stream(&file);
    stream << doc.toByteArray();
    return true;
}

bool MacroList::load(const QString &f, std::vector<Macro *> &ret, const KigPart &kdoc)
{
    QFile file(f);
    if (!file.open(QIODevice::ReadOnly)) {
        KMessageBox::error(nullptr, i18n("Could not open macro file '%1'", f));
        return false;
    }
    QDomDocument doc(QStringLiteral("KigMacroFile"));
    if (!doc.setContent(&file)) {
        KMessageBox::error(nullptr, i18n("Could not open macro file '%1'", f));
        return false;
    }
    file.close();
    QDomElement main = doc.documentElement();

    if (main.tagName() == QLatin1String("KigMacroFile"))
        return loadNew(main, ret, kdoc);
    else {
        KMessageBox::detailedError(nullptr,
                                   i18n("Kig cannot open the macro file \"%1\".", f),
                                   i18n("This file was created by a very old Kig version (pre-0.4). "
                                        "Support for this format has been removed from recent Kig versions. "
                                        "You can try to import this macro using a previous Kig version "
                                        "(0.4 to 0.6) and then export it again in the new format."),
                                   i18n("Not Supported"));
        return false;
    }
}

bool MacroList::loadNew(const QDomElement &docelem, std::vector<Macro *> &ret, const KigPart &)
{
    bool sok = true;
    // unused..
    //  int number = docelem.attribute( "Number" ).toInt( &sok );
    if (!sok)
        return false;


    int unnamedindex = 1;
    QString tmp;

    for (QDomElement macroelem = docelem.firstChild().toElement(); !macroelem.isNull(); macroelem = macroelem.nextSibling().toElement()) {
        QString name, description;
        ObjectHierarchy *hierarchy = nullptr;
        QByteArray actionname;
        QByteArray iconfile("system-run");
        if (macroelem.tagName() != QLatin1String("Macro"))
            continue; // forward compat ?
        for (QDomElement dataelem = macroelem.firstChild().toElement(); !dataelem.isNull(); dataelem = dataelem.nextSibling().toElement()) {
            if (dataelem.tagName() == QLatin1String("Name"))
                name = dataelem.text();
            else if (dataelem.tagName() == QLatin1String("Description"))
                description = dataelem.text();
            else if (dataelem.tagName() == QLatin1String("Construction"))
                hierarchy = ObjectHierarchy::buildSafeObjectHierarchy(dataelem, tmp);
            else if (dataelem.tagName() == QLatin1String("ActionName"))
                actionname = dataelem.text().toLatin1();
            else if (dataelem.tagName() == QLatin1String("IconFileName"))
                iconfile = dataelem.text().toLatin1();
            else
                continue;
        };
        assert(hierarchy);
        // if the macro has no name, we give it a bogus name...
        bool name_i18ned = false;
        if (name.isEmpty()) {
            name = i18n("Unnamed Macro #%1", unnamedindex++);
            name_i18ned = true;
        }
        MacroConstructor *ctor = new MacroConstructor(*hierarchy,
                                                      name_i18ned ? name : i18n(name.toUtf8()),
                                                      description.isEmpty() ? QString() : i18n(description.toUtf8()),
                                                      iconfile);
        delete hierarchy;
        GUIAction *act = new ConstructibleAction(ctor, actionname);
        Macro *macro = new Macro(act, ctor);
        ret.push_back(macro);
    };
    return true;
}

const ObjectConstructorList::vectype &ObjectConstructorList::constructors() const
{
    return mctors;
}