~ubuntu-branches/ubuntu/raring/kbibtex/raring

« back to all changes in this revision

Viewing changes to src/libkbibtexio/value.cpp

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2011-07-18 09:29:48 UTC
  • mfrom: (1.1.6) (2.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20110718092948-ksxjmg7kdfamolmg
Tags: 0.3-1
* First upstream release for KDE4 (Closes: #634255). A number of search
  engines are still missing, in comparison to the 0.2 series.
* Bumped Standards-Version to 3.9.2, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
*   Copyright (C) 2004-2009 by Thomas Fischer                             *
 
3
*   fischer@unix-ag.uni-kl.de                                             *
 
4
*                                                                         *
 
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
*                                                                         *
 
10
*   This program is distributed in the hope that it will be useful,       *
 
11
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
*   GNU General Public License for more details.                          *
 
14
*                                                                         *
 
15
*   You should have received a copy of the GNU General Public License     *
 
16
*   along with this program; if not, write to the                         *
 
17
*   Free Software Foundation, Inc.,                                       *
 
18
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
19
***************************************************************************/
 
20
#include <QString>
 
21
#include <QStringList>
 
22
 
 
23
#include <KDebug>
 
24
 
 
25
#include <file.h>
 
26
#include "value.h"
 
27
 
 
28
Keyword::Keyword(const Keyword& other)
 
29
        : m_text(other.m_text)
 
30
{
 
31
    // nothing
 
32
}
 
33
 
 
34
Keyword::Keyword(const QString& text)
 
35
        : m_text(text)
 
36
{
 
37
    // nothing
 
38
}
 
39
 
 
40
void Keyword::setText(const QString& text)
 
41
{
 
42
    m_text = text;
 
43
}
 
44
 
 
45
QString Keyword::text() const
 
46
{
 
47
    return m_text;
 
48
}
 
49
 
 
50
void Keyword::replace(const QString &before, const QString &after)
 
51
{
 
52
    if (m_text == before)
 
53
        m_text = after;
 
54
}
 
55
 
 
56
bool Keyword::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
 
57
{
 
58
    return m_text.contains(pattern, caseSensitive);
 
59
}
 
60
 
 
61
bool Keyword::operator==(const ValueItem &other) const
 
62
{
 
63
    const Keyword *otherKeyword = dynamic_cast<const Keyword*>(&other);
 
64
    if (otherKeyword != NULL) {
 
65
        return otherKeyword->text() == text();
 
66
    } else
 
67
        return false;
 
68
}
 
69
 
 
70
 
 
71
Person::Person(const QString& firstName, const QString& lastName, const QString& prefix, const QString& suffix)
 
72
        : m_firstName(firstName), m_lastName(lastName), m_prefix(prefix), m_suffix(suffix)
 
73
{
 
74
    // nothing
 
75
}
 
76
 
 
77
Person::Person(const Person& other)
 
78
        : m_firstName(other.firstName()), m_lastName(other.lastName()), m_prefix(other.prefix()), m_suffix(other.suffix())
 
79
{
 
80
    // nothing
 
81
}
 
82
 
 
83
void Person::setName(const QString& firstName, const QString& lastName, const QString& prefix, const QString& suffix)
 
84
{
 
85
    m_firstName = firstName;
 
86
    m_lastName = lastName;
 
87
    m_prefix = prefix;
 
88
    m_suffix = suffix;
 
89
}
 
90
 
 
91
QString Person::firstName() const
 
92
{
 
93
    return m_firstName;
 
94
}
 
95
 
 
96
QString Person::lastName() const
 
97
{
 
98
    return m_lastName;
 
99
}
 
100
 
 
101
QString Person::prefix() const
 
102
{
 
103
    return m_prefix;
 
104
}
 
105
 
 
106
QString Person::suffix() const
 
107
{
 
108
    return m_suffix;
 
109
}
 
110
 
 
111
void Person::replace(const QString &before, const QString &after)
 
112
{
 
113
    if (m_firstName == before)
 
114
        m_firstName = after;
 
115
    if (m_lastName == before)
 
116
        m_lastName = after;
 
117
    if (m_prefix == before)
 
118
        m_prefix = after;
 
119
    if (m_suffix == before)
 
120
        m_suffix = after;
 
121
}
 
122
 
 
123
bool Person::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
 
124
{
 
125
    return m_firstName.contains(pattern, caseSensitive) || m_lastName.contains(pattern, caseSensitive) ||  m_prefix.contains(pattern, caseSensitive) ||  m_suffix.contains(pattern, caseSensitive) || QString("%1 %2|%2, %1").arg(m_firstName).arg(m_lastName).contains(pattern, caseSensitive);
 
126
}
 
127
 
 
128
bool Person::operator==(const ValueItem &other) const
 
129
{
 
130
    const Person *otherPerson = dynamic_cast<const Person*>(&other);
 
131
    if (otherPerson != NULL) {
 
132
        return otherPerson->firstName() == firstName() && otherPerson->lastName() == lastName();
 
133
    } else
 
134
        return false;
 
135
}
 
136
 
 
137
const QRegExp MacroKey::validMacroKey = QRegExp("^[a-z][-.:/+_a-z0-9]*$|^[0-9]+$", Qt::CaseInsensitive);
 
138
 
 
139
MacroKey::MacroKey(const MacroKey& other)
 
140
        : m_text(other.m_text)
 
141
{
 
142
    // nothing
 
143
}
 
144
 
 
145
MacroKey::MacroKey(const QString& text)
 
146
        : m_text(text)
 
147
{
 
148
    // nothing
 
149
}
 
150
 
 
151
void MacroKey::setText(const QString& text)
 
152
{
 
153
    m_text = text;
 
154
}
 
155
 
 
156
QString MacroKey::text() const
 
157
{
 
158
    return m_text;
 
159
}
 
160
 
 
161
bool MacroKey::isValid()
 
162
{
 
163
    const QString t = text();
 
164
    int idx = validMacroKey.indexIn(t);
 
165
    return idx > -1 && validMacroKey.cap(0) == t;
 
166
}
 
167
 
 
168
void MacroKey::replace(const QString &before, const QString &after)
 
169
{
 
170
    if (m_text == before)
 
171
        m_text = after;
 
172
}
 
173
 
 
174
bool MacroKey::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
 
175
{
 
176
    return m_text.contains(pattern, caseSensitive);
 
177
}
 
178
 
 
179
bool MacroKey::operator==(const ValueItem &other) const
 
180
{
 
181
    const MacroKey *otherMacroKey = dynamic_cast<const MacroKey*>(&other);
 
182
    if (otherMacroKey != NULL) {
 
183
        return otherMacroKey->text() == text();
 
184
    } else
 
185
        return false;
 
186
}
 
187
 
 
188
 
 
189
PlainText::PlainText(const PlainText& other)
 
190
        : m_text(other.text())
 
191
{
 
192
    // nothing
 
193
}
 
194
 
 
195
PlainText::PlainText(const QString& text)
 
196
        : m_text(text)
 
197
{
 
198
    // nothing
 
199
}
 
200
 
 
201
void PlainText::setText(const QString& text)
 
202
{
 
203
    m_text = text;
 
204
}
 
205
 
 
206
QString PlainText::text() const
 
207
{
 
208
    return m_text;
 
209
}
 
210
 
 
211
void PlainText::replace(const QString &before, const QString &after)
 
212
{
 
213
    if (m_text == before)
 
214
        m_text = after;
 
215
}
 
216
 
 
217
bool PlainText::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
 
218
{
 
219
    return m_text.contains(pattern, caseSensitive);
 
220
}
 
221
 
 
222
bool PlainText::operator==(const ValueItem &other) const
 
223
{
 
224
    const PlainText *otherPlainText = dynamic_cast<const PlainText*>(&other);
 
225
    if (otherPlainText != NULL) {
 
226
        return otherPlainText->text() == text();
 
227
    } else
 
228
        return false;
 
229
}
 
230
 
 
231
 
 
232
VerbatimText::VerbatimText(const VerbatimText& other)
 
233
        : m_text(other.text())
 
234
{
 
235
    // nothing
 
236
}
 
237
 
 
238
VerbatimText::VerbatimText(const QString& text)
 
239
        : m_text(text)
 
240
{
 
241
    // nothing
 
242
}
 
243
 
 
244
void VerbatimText::setText(const QString& text)
 
245
{
 
246
    m_text = text;
 
247
}
 
248
 
 
249
QString VerbatimText::text() const
 
250
{
 
251
    return m_text;
 
252
}
 
253
 
 
254
void VerbatimText::replace(const QString &before, const QString &after)
 
255
{
 
256
    if (m_text == before)
 
257
        m_text = after;
 
258
}
 
259
 
 
260
bool VerbatimText::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
 
261
{
 
262
    return m_text.contains(pattern, caseSensitive);
 
263
}
 
264
 
 
265
bool VerbatimText::operator==(const ValueItem &other) const
 
266
{
 
267
    const VerbatimText *otherVerbatimText = dynamic_cast<const VerbatimText*>(&other);
 
268
    if (otherVerbatimText != NULL) {
 
269
        return otherVerbatimText->text() == text();
 
270
    } else
 
271
        return false;
 
272
}
 
273
 
 
274
 
 
275
Value::Value()
 
276
        : QList<ValueItem*>()
 
277
{
 
278
    // nothing
 
279
}
 
280
 
 
281
Value::Value(const Value& other)
 
282
        : QList<ValueItem*>()
 
283
{
 
284
    copyFrom(other);
 
285
}
 
286
 
 
287
void Value::replace(const QString &before, const QString &after)
 
288
{
 
289
    for (QList<ValueItem*>::Iterator it = begin(); it != end(); ++it)
 
290
        (*it)->replace(before, after);
 
291
}
 
292
 
 
293
bool Value::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
 
294
{
 
295
    bool result = false;
 
296
    for (QList<ValueItem*>::ConstIterator it = begin(); !result && it != end(); ++it) {
 
297
        result |= (*it)->containsPattern(pattern, caseSensitive);
 
298
    }
 
299
    return result;
 
300
}
 
301
 
 
302
bool Value::contains(const ValueItem& item) const
 
303
{
 
304
    for (QList<ValueItem*>::ConstIterator it = begin(); it != end(); ++it)
 
305
        if ((*it)->operator==(item))
 
306
            return true;
 
307
    return false;
 
308
}
 
309
 
 
310
Value& Value::operator=(const Value & rhs)
 
311
{
 
312
    copyFrom(rhs);
 
313
    return *this;
 
314
}
 
315
 
 
316
void Value::copyFrom(const Value& other)
 
317
{
 
318
    clear();
 
319
    for (QList<ValueItem*>::ConstIterator it = other.begin(); it != other.end(); ++it) {
 
320
        PlainText *plainText = dynamic_cast<PlainText*>(*it);
 
321
        if (plainText != NULL)
 
322
            append(new PlainText(*plainText));
 
323
        else {
 
324
            Person *person = dynamic_cast<Person*>(*it);
 
325
            if (person != NULL)
 
326
                append(new Person(*person));
 
327
            else {
 
328
                Keyword *keyword = dynamic_cast<Keyword*>(*it);
 
329
                if (keyword != NULL)
 
330
                    append(new Keyword(*keyword));
 
331
                else {
 
332
                    MacroKey *macroKey = dynamic_cast<MacroKey*>(*it);
 
333
                    if (macroKey != NULL)
 
334
                        append(new MacroKey(*macroKey));
 
335
                    else {
 
336
                        VerbatimText *verbatimText = dynamic_cast<VerbatimText*>(*it);
 
337
                        if (verbatimText != NULL)
 
338
                            append(new VerbatimText(*verbatimText));
 
339
                        else
 
340
                            kError() << "cannot copy from unknown data type" << endl;
 
341
                    }
 
342
                }
 
343
            }
 
344
        }
 
345
    }
 
346
}
 
347
 
 
348
QRegExp PlainTextValue::removeCurlyBrackets = QRegExp("(^|[^\\\\])[{}]");
 
349
 
 
350
QString PlainTextValue::text(const Value& value, const File* file, bool debug)
 
351
{
 
352
    ValueItemType vit = VITOther;
 
353
    ValueItemType lastVit = VITOther;
 
354
 
 
355
    QString result = "";
 
356
    for (QList<ValueItem*>::ConstIterator it = value.begin(); it != value.end(); ++it) {
 
357
        QString nextText = text(**it, vit, file, debug);
 
358
        if (!nextText.isNull()) {
 
359
            if (lastVit == VITPerson && vit == VITPerson)
 
360
                result.append(" and ");
 
361
            else if (lastVit == VITKeyword && vit == VITKeyword)
 
362
                result.append("; ");
 
363
            else if (!result.isEmpty())
 
364
                result.append(" ");
 
365
            result.append(nextText);
 
366
 
 
367
            lastVit = vit;
 
368
        }
 
369
    }
 
370
    return result;
 
371
}
 
372
 
 
373
QString PlainTextValue::text(const ValueItem& valueItem, const File* file, bool debug)
 
374
{
 
375
    ValueItemType vit;
 
376
    return text(valueItem, vit, file, debug);
 
377
}
 
378
 
 
379
QString PlainTextValue::text(const ValueItem& valueItem, ValueItemType &vit, const File* /*file*/, bool debug)
 
380
{
 
381
    QString result = QString::null;
 
382
    vit = VITOther;
 
383
 
 
384
    const PlainText *plainText = dynamic_cast<const PlainText*>(&valueItem);
 
385
    if (plainText != NULL) {
 
386
        result = plainText->text();
 
387
        if (debug) result = "[:" + result + ":PlainText]";
 
388
    } else {
 
389
        const MacroKey *macroKey = dynamic_cast<const MacroKey*>(&valueItem);
 
390
        if (macroKey != NULL) {
 
391
            result = macroKey->text(); // TODO Use File to resolve key to full text
 
392
            if (debug) result = "[:" + result + ":MacroKey]";
 
393
        } else {
 
394
            const Person *person = dynamic_cast<const Person*>(&valueItem);
 
395
            if (person != NULL) {
 
396
                result = person->firstName() + " " + person->lastName();
 
397
                vit = VITPerson;
 
398
                if (debug) result = "[:" + result + ":Person]";
 
399
            } else {
 
400
                const Keyword *keyword = dynamic_cast<const Keyword*>(&valueItem);
 
401
                if (keyword != NULL) {
 
402
                    result = keyword->text();
 
403
                    vit = VITKeyword;
 
404
                    if (debug) result = "[:" + result + ":Keyword]";
 
405
                } else {
 
406
                    const VerbatimText *verbatimText = dynamic_cast<const VerbatimText*>(&valueItem);
 
407
                    if (verbatimText != NULL) {
 
408
                        result = verbatimText->text();
 
409
                        if (debug) result = "[:" + result + ":VerbatimText]";
 
410
                    }
 
411
                }
 
412
            }
 
413
        }
 
414
    }
 
415
 
 
416
    /// remove curly brackets
 
417
    int i = -1;
 
418
    while ((i = result.indexOf(removeCurlyBrackets, i + 1)) >= 0) {
 
419
        result = result.replace(removeCurlyBrackets.cap(0), removeCurlyBrackets.cap(1));
 
420
    }
 
421
    /// remove hyphenation commands
 
422
    result = result.replace(QLatin1String("\\-"), QLatin1String(""));
 
423
 
 
424
    if (debug) result = "[:" + result + ":Debug]";
 
425
    return result;
 
426
}