1
/***************************************************************************
2
* Copyright (C) 2004-2009 by Thomas Fischer *
3
* fischer@unix-ag.uni-kl.de *
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. *
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. *
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
***************************************************************************/
21
#include <QStringList>
28
Keyword::Keyword(const Keyword& other)
29
: m_text(other.m_text)
34
Keyword::Keyword(const QString& text)
40
void Keyword::setText(const QString& text)
45
QString Keyword::text() const
50
void Keyword::replace(const QString &before, const QString &after)
56
bool Keyword::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
58
return m_text.contains(pattern, caseSensitive);
61
bool Keyword::operator==(const ValueItem &other) const
63
const Keyword *otherKeyword = dynamic_cast<const Keyword*>(&other);
64
if (otherKeyword != NULL) {
65
return otherKeyword->text() == text();
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)
77
Person::Person(const Person& other)
78
: m_firstName(other.firstName()), m_lastName(other.lastName()), m_prefix(other.prefix()), m_suffix(other.suffix())
83
void Person::setName(const QString& firstName, const QString& lastName, const QString& prefix, const QString& suffix)
85
m_firstName = firstName;
86
m_lastName = lastName;
91
QString Person::firstName() const
96
QString Person::lastName() const
101
QString Person::prefix() const
106
QString Person::suffix() const
111
void Person::replace(const QString &before, const QString &after)
113
if (m_firstName == before)
115
if (m_lastName == before)
117
if (m_prefix == before)
119
if (m_suffix == before)
123
bool Person::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
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);
128
bool Person::operator==(const ValueItem &other) const
130
const Person *otherPerson = dynamic_cast<const Person*>(&other);
131
if (otherPerson != NULL) {
132
return otherPerson->firstName() == firstName() && otherPerson->lastName() == lastName();
137
const QRegExp MacroKey::validMacroKey = QRegExp("^[a-z][-.:/+_a-z0-9]*$|^[0-9]+$", Qt::CaseInsensitive);
139
MacroKey::MacroKey(const MacroKey& other)
140
: m_text(other.m_text)
145
MacroKey::MacroKey(const QString& text)
151
void MacroKey::setText(const QString& text)
156
QString MacroKey::text() const
161
bool MacroKey::isValid()
163
const QString t = text();
164
int idx = validMacroKey.indexIn(t);
165
return idx > -1 && validMacroKey.cap(0) == t;
168
void MacroKey::replace(const QString &before, const QString &after)
170
if (m_text == before)
174
bool MacroKey::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
176
return m_text.contains(pattern, caseSensitive);
179
bool MacroKey::operator==(const ValueItem &other) const
181
const MacroKey *otherMacroKey = dynamic_cast<const MacroKey*>(&other);
182
if (otherMacroKey != NULL) {
183
return otherMacroKey->text() == text();
189
PlainText::PlainText(const PlainText& other)
190
: m_text(other.text())
195
PlainText::PlainText(const QString& text)
201
void PlainText::setText(const QString& text)
206
QString PlainText::text() const
211
void PlainText::replace(const QString &before, const QString &after)
213
if (m_text == before)
217
bool PlainText::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
219
return m_text.contains(pattern, caseSensitive);
222
bool PlainText::operator==(const ValueItem &other) const
224
const PlainText *otherPlainText = dynamic_cast<const PlainText*>(&other);
225
if (otherPlainText != NULL) {
226
return otherPlainText->text() == text();
232
VerbatimText::VerbatimText(const VerbatimText& other)
233
: m_text(other.text())
238
VerbatimText::VerbatimText(const QString& text)
244
void VerbatimText::setText(const QString& text)
249
QString VerbatimText::text() const
254
void VerbatimText::replace(const QString &before, const QString &after)
256
if (m_text == before)
260
bool VerbatimText::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
262
return m_text.contains(pattern, caseSensitive);
265
bool VerbatimText::operator==(const ValueItem &other) const
267
const VerbatimText *otherVerbatimText = dynamic_cast<const VerbatimText*>(&other);
268
if (otherVerbatimText != NULL) {
269
return otherVerbatimText->text() == text();
276
: QList<ValueItem*>()
281
Value::Value(const Value& other)
282
: QList<ValueItem*>()
287
void Value::replace(const QString &before, const QString &after)
289
for (QList<ValueItem*>::Iterator it = begin(); it != end(); ++it)
290
(*it)->replace(before, after);
293
bool Value::containsPattern(const QString &pattern, Qt::CaseSensitivity caseSensitive) const
296
for (QList<ValueItem*>::ConstIterator it = begin(); !result && it != end(); ++it) {
297
result |= (*it)->containsPattern(pattern, caseSensitive);
302
bool Value::contains(const ValueItem& item) const
304
for (QList<ValueItem*>::ConstIterator it = begin(); it != end(); ++it)
305
if ((*it)->operator==(item))
310
Value& Value::operator=(const Value & rhs)
316
void Value::copyFrom(const Value& other)
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));
324
Person *person = dynamic_cast<Person*>(*it);
326
append(new Person(*person));
328
Keyword *keyword = dynamic_cast<Keyword*>(*it);
330
append(new Keyword(*keyword));
332
MacroKey *macroKey = dynamic_cast<MacroKey*>(*it);
333
if (macroKey != NULL)
334
append(new MacroKey(*macroKey));
336
VerbatimText *verbatimText = dynamic_cast<VerbatimText*>(*it);
337
if (verbatimText != NULL)
338
append(new VerbatimText(*verbatimText));
340
kError() << "cannot copy from unknown data type" << endl;
348
QRegExp PlainTextValue::removeCurlyBrackets = QRegExp("(^|[^\\\\])[{}]");
350
QString PlainTextValue::text(const Value& value, const File* file, bool debug)
352
ValueItemType vit = VITOther;
353
ValueItemType lastVit = VITOther;
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)
363
else if (!result.isEmpty())
365
result.append(nextText);
373
QString PlainTextValue::text(const ValueItem& valueItem, const File* file, bool debug)
376
return text(valueItem, vit, file, debug);
379
QString PlainTextValue::text(const ValueItem& valueItem, ValueItemType &vit, const File* /*file*/, bool debug)
381
QString result = QString::null;
384
const PlainText *plainText = dynamic_cast<const PlainText*>(&valueItem);
385
if (plainText != NULL) {
386
result = plainText->text();
387
if (debug) result = "[:" + result + ":PlainText]";
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]";
394
const Person *person = dynamic_cast<const Person*>(&valueItem);
395
if (person != NULL) {
396
result = person->firstName() + " " + person->lastName();
398
if (debug) result = "[:" + result + ":Person]";
400
const Keyword *keyword = dynamic_cast<const Keyword*>(&valueItem);
401
if (keyword != NULL) {
402
result = keyword->text();
404
if (debug) result = "[:" + result + ":Keyword]";
406
const VerbatimText *verbatimText = dynamic_cast<const VerbatimText*>(&valueItem);
407
if (verbatimText != NULL) {
408
result = verbatimText->text();
409
if (debug) result = "[:" + result + ":VerbatimText]";
416
/// remove curly brackets
418
while ((i = result.indexOf(removeCurlyBrackets, i + 1)) >= 0) {
419
result = result.replace(removeCurlyBrackets.cap(0), removeCurlyBrackets.cap(1));
421
/// remove hyphenation commands
422
result = result.replace(QLatin1String("\\-"), QLatin1String(""));
424
if (debug) result = "[:" + result + ":Debug]";