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

« back to all changes in this revision

Viewing changes to src/libkbibtexio/entry.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-2010 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 <QList>
 
21
 
 
22
#include <entry.h>
 
23
#include <file.h>
 
24
 
 
25
#define max(a,b) ((a)>(b)?(a):(b))
 
26
 
 
27
// FIXME: Check if using those constants in the program is really necessary
 
28
// or can be replace by config files
 
29
const QLatin1String Entry::ftAbstract = QLatin1String("abstract");
 
30
const QLatin1String Entry::ftAddress = QLatin1String("address");
 
31
const QLatin1String Entry::ftAuthor = QLatin1String("author");
 
32
const QLatin1String Entry::ftBookTitle = QLatin1String("booktitle");
 
33
const QLatin1String Entry::ftChapter = QLatin1String("chapter");
 
34
const QLatin1String Entry::ftColor = QLatin1String("x-color");
 
35
const QLatin1String Entry::ftComment = QLatin1String("comment");
 
36
const QLatin1String Entry::ftCrossRef = QLatin1String("crossref");
 
37
const QLatin1String Entry::ftDOI = QLatin1String("doi");
 
38
const QLatin1String Entry::ftEditor = QLatin1String("editor");
 
39
const QLatin1String Entry::ftISSN = QLatin1String("issn");
 
40
const QLatin1String Entry::ftISBN = QLatin1String("isbn");
 
41
const QLatin1String Entry::ftJournal = QLatin1String("journal");
 
42
const QLatin1String Entry::ftKeywords = QLatin1String("keywords");
 
43
const QLatin1String Entry::ftLocalFile = QLatin1String("localfile");
 
44
const QLatin1String Entry::ftLocation = QLatin1String("location");
 
45
const QLatin1String Entry::ftMonth = QLatin1String("month");
 
46
const QLatin1String Entry::ftNote = QLatin1String("note");
 
47
const QLatin1String Entry::ftNumber = QLatin1String("number");
 
48
const QLatin1String Entry::ftPages = QLatin1String("pages");
 
49
const QLatin1String Entry::ftPublisher = QLatin1String("publisher");
 
50
const QLatin1String Entry::ftSchool = QLatin1String("school");
 
51
const QLatin1String Entry::ftSeries = QLatin1String("series");
 
52
const QLatin1String Entry::ftTitle = QLatin1String("title");
 
53
const QLatin1String Entry::ftUrl = QLatin1String("url");
 
54
const QLatin1String Entry::ftVolume = QLatin1String("volume");
 
55
const QLatin1String Entry::ftYear = QLatin1String("year");
 
56
 
 
57
const QLatin1String Entry::etArticle = QLatin1String("article");
 
58
const QLatin1String Entry::etBook = QLatin1String("book");
 
59
const QLatin1String Entry::etInBook = QLatin1String("inbook");
 
60
const QLatin1String Entry::etInProceedings = QLatin1String("inproceedings");
 
61
const QLatin1String Entry::etMisc = QLatin1String("misc");
 
62
const QLatin1String Entry::etPhDThesis = QLatin1String("phdthesis");
 
63
const QLatin1String Entry::etTechReport = QLatin1String("techreport");
 
64
const QLatin1String Entry::etUnpublished = QLatin1String("unpublished");
 
65
 
 
66
/**
 
67
 * Private class to store internal variables that should not be visible
 
68
 * in the interface as defined in the header file.
 
69
 */
 
70
class Entry::EntryPrivate
 
71
{
 
72
public:
 
73
    QString type;
 
74
    QString id;
 
75
};
 
76
 
 
77
Entry::Entry(const QString& type, const QString& id)
 
78
        : Element(), QMap<QString, Value>(), d(new Entry::EntryPrivate)
 
79
{
 
80
    d->type = type;
 
81
    d->id = id;
 
82
}
 
83
 
 
84
Entry::Entry(const Entry &other)
 
85
        : Element(), QMap<QString, Value>(), d(new Entry::EntryPrivate)
 
86
{
 
87
    operator=(other);
 
88
}
 
89
 
 
90
Entry::~Entry()
 
91
{
 
92
    // nothing
 
93
}
 
94
 
 
95
Entry& Entry::operator= (const Entry & other)
 
96
{
 
97
    if (this != &other) {
 
98
        d->type = other.type();
 
99
        d->id = other.id();
 
100
        clear();
 
101
        for (QMap<QString, Value>::ConstIterator it = other.begin(); it != other.end(); ++it)
 
102
            insert(it.key(), it.value());
 
103
    }
 
104
    return *this;
 
105
}
 
106
 
 
107
Value& Entry::operator[](const QString& key)
 
108
{
 
109
    const QString lcKey = key.toLower();
 
110
    for (Entry::ConstIterator it = constBegin(); it != constEnd(); ++it)
 
111
        if (it.key().toLower() == lcKey)
 
112
            return QMap<QString, Value>::operator[](it.key());
 
113
 
 
114
    return QMap<QString, Value>::operator[](key);
 
115
}
 
116
 
 
117
const  Value Entry::operator[](const QString& key) const
 
118
{
 
119
    const QString lcKey = key.toLower();
 
120
    for (Entry::ConstIterator it = constBegin(); it != constEnd(); ++it)
 
121
        if (it.key().toLower() == lcKey)
 
122
            return QMap<QString, Value>::operator[](it.key());
 
123
 
 
124
    return QMap<QString, Value>::operator[](key);
 
125
}
 
126
 
 
127
 
 
128
void Entry::setType(const QString& type)
 
129
{
 
130
    d->type = type;
 
131
}
 
132
 
 
133
QString Entry::type() const
 
134
{
 
135
    return d->type;
 
136
}
 
137
 
 
138
void Entry::setId(const QString& id)
 
139
{
 
140
    d->id = id;
 
141
}
 
142
 
 
143
QString Entry::id() const
 
144
{
 
145
    return d->id;
 
146
}
 
147
 
 
148
const Value Entry::value(const QString& key) const
 
149
{
 
150
    const QString lcKey = key.toLower();
 
151
    for (Entry::ConstIterator it = constBegin(); it != constEnd(); ++it)
 
152
        if (it.key().toLower() == lcKey)
 
153
            return QMap<QString, Value>::value(it.key());
 
154
 
 
155
    return QMap<QString, Value>::value(key);
 
156
}
 
157
 
 
158
int Entry::remove(const QString& key)
 
159
{
 
160
    const QString lcKey = key.toLower();
 
161
    for (Entry::ConstIterator it = constBegin(); it != constEnd(); ++it)
 
162
        if (it.key().toLower() == lcKey)
 
163
            return QMap<QString, Value>::remove(it.key());
 
164
 
 
165
    return QMap<QString, Value>::remove(key);
 
166
}
 
167
 
 
168
bool Entry::contains(const QString& key) const
 
169
{
 
170
    const QString lcKey = key.toLower();
 
171
    for (Entry::ConstIterator it = constBegin(); it != constEnd(); ++it)
 
172
        if (it.key().toLower() == lcKey)
 
173
            return true;
 
174
 
 
175
    return false;
 
176
}
 
177
 
 
178
Entry* Entry::resolveCrossref(const Entry &original, const File *bibTeXfile)
 
179
{
 
180
    Entry *result = new Entry(original);
 
181
 
 
182
    QString crossRef = PlainTextValue::text(original.value(QLatin1String("crossref")), bibTeXfile);
 
183
    const Entry *crossRefEntry = dynamic_cast<const Entry*>((bibTeXfile != NULL) ? bibTeXfile->containsKey(crossRef) : NULL);
 
184
    if (crossRefEntry != NULL) {
 
185
        /// copy all fields from crossref'ed entry to new entry which do not (yet) exist in the new entry
 
186
        for (Entry::ConstIterator it = crossRefEntry->constBegin(); it != crossRefEntry->constEnd(); ++it)
 
187
            if (!result->contains(it.key()))
 
188
                result->insert(it.key(), Value(it.value()));
 
189
 
 
190
        if (crossRefEntry->contains(ftTitle)) {
 
191
            /// translate crossref's title into new entry's booktitle
 
192
            result->insert(ftBookTitle, Value(crossRefEntry->operator [](ftTitle)));
 
193
        }
 
194
 
 
195
        /// remove crossref field (no longer of use)
 
196
        result->remove(ftCrossRef);
 
197
    }
 
198
 
 
199
    return result;
 
200
}