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>
27
#include "fileexporterris.h"
29
FileExporterRIS::FileExporterRIS() : FileExporter()
34
FileExporterRIS::~FileExporterRIS()
39
bool FileExporterRIS::save(QIODevice* iodevice, const Element* element, QStringList* /*errorLog*/)
42
QTextStream stream(iodevice);
44
const Entry *entry = dynamic_cast<const Entry*>(element);
46
result = writeEntry(stream, entry);
48
return result && !m_cancelFlag;
51
bool FileExporterRIS::save(QIODevice* iodevice, const File* bibtexfile, QStringList* /*errorLog*/)
55
QTextStream stream(iodevice);
57
for (File::ConstIterator it = bibtexfile->begin(); it != bibtexfile->end() && result && !m_cancelFlag; it++) {
58
const Entry *entry = dynamic_cast<const Entry*>(*it);
60
// FIXME Entry *myEntry = bibtexfile->completeReferencedFieldsConst( entry );
61
Entry *myEntry = new Entry(*entry);
62
result &= writeEntry(stream, myEntry);
67
return result && !m_cancelFlag;
70
void FileExporterRIS::cancel()
75
bool FileExporterRIS::writeEntry(QTextStream &stream, const Entry* entry, const File* bibtexfile)
78
QString type = entry->type();
80
if (type == Entry::etBook)
81
writeKeyValue(stream, "TY", "BOOK");
82
else if (type == Entry::etInBook)
83
writeKeyValue(stream, "TY", "CHAP");
84
else if (type == Entry::etInProceedings)
85
writeKeyValue(stream, "TY", "CONF");
86
else if (type == Entry::etArticle)
87
writeKeyValue(stream, "TY", "JOUR");
88
else if (type == Entry::etTechReport)
89
writeKeyValue(stream, "TY", "RPRT");
90
else if (type == Entry::etPhDThesis)
91
writeKeyValue(stream, "TY", "THES");
93
writeKeyValue(stream, "TY", "GEN");
98
for (Entry::ConstIterator it = entry->begin(); result && it != entry->end(); it++) {
99
const QString key = it.key();
100
const Value value = it.value();
101
QString plainText = PlainTextValue::text(value, bibtexfile);
103
if (key.startsWith("RISfield_"))
104
result &= writeKeyValue(stream, key.right(2), plainText);
105
else if (key == Entry::ftAuthor) {
106
for (QList<ValueItem*>::ConstIterator it = value.begin(); result && it != value.end(); ++it) {
107
Person *person = dynamic_cast<Person*>(*it);
109
result &= writeKeyValue(stream, "AU", PlainTextValue::text(**it, bibtexfile));
111
kWarning() << "Cannot write value " << PlainTextValue::text(**it, bibtexfile) << " for field AU (author), not supported by RIS format" << endl;
113
} else if (key.toLower() == Entry::ftEditor) {
114
for (QList<ValueItem*>::ConstIterator it = value.begin(); result && it != value.end(); ++it) {
115
Person *person = dynamic_cast<Person*>(*it);
117
result &= writeKeyValue(stream, "ED", PlainTextValue::text(**it, bibtexfile));
119
kWarning() << "Cannot write value " << PlainTextValue::text(**it, bibtexfile) << " for field ED (editor), not supported by RIS format" << endl;
121
} else if (key == Entry::ftTitle)
122
result &= writeKeyValue(stream, "TI", PlainTextValue::text(value, bibtexfile));
123
else if (key == Entry::ftJournal)
124
result &= writeKeyValue(stream, "JO", PlainTextValue::text(value, bibtexfile));
125
else if (key == Entry::ftChapter)
126
result &= writeKeyValue(stream, "CP", PlainTextValue::text(value, bibtexfile));
127
else if (key == Entry::ftISSN)
128
result &= writeKeyValue(stream, "SN", PlainTextValue::text(value, bibtexfile));
129
else if (key == Entry::ftISBN)
130
result &= writeKeyValue(stream, "SN", PlainTextValue::text(value, bibtexfile));
131
else if (key == Entry::ftVolume)
132
result &= writeKeyValue(stream, "VL", PlainTextValue::text(value, bibtexfile));
133
else if (key == Entry::ftNumber)
134
result &= writeKeyValue(stream, "IS", PlainTextValue::text(value, bibtexfile));
135
else if (key == Entry::ftNote)
136
result &= writeKeyValue(stream, "N1", PlainTextValue::text(value, bibtexfile));
137
else if (key == Entry::ftAbstract)
138
result &= writeKeyValue(stream, "N2", PlainTextValue::text(value, bibtexfile));
139
else if (key == Entry::ftPublisher)
140
result &= writeKeyValue(stream, "PB", PlainTextValue::text(value, bibtexfile));
141
else if (key == Entry::ftLocation)
142
result &= writeKeyValue(stream, "CY", PlainTextValue::text(value, bibtexfile));
143
else if (key == Entry::ftKeywords)
144
result &= writeKeyValue(stream, "KW", PlainTextValue::text(value, bibtexfile));
145
else if (key == Entry::ftYear)
146
year = PlainTextValue::text(value, bibtexfile);
147
else if (key == Entry::ftMonth)
148
month = PlainTextValue::text(value, bibtexfile);
149
else if (key == Entry::ftAddress)
150
result &= writeKeyValue(stream, "AD", PlainTextValue::text(value, bibtexfile));
151
else if (key == Entry::ftUrl)
152
result &= writeKeyValue(stream, "UR", PlainTextValue::text(value, bibtexfile));
153
else if (key == Entry::ftPages) {
154
QStringList pageRange = PlainTextValue::text(value, bibtexfile).split(QRegExp(QString("--|-|%1").arg(QChar(0x2013))));
155
if (pageRange.count() == 2) {
156
result &= writeKeyValue(stream, "SP", pageRange[ 0 ]);
157
result &= writeKeyValue(stream, "EP", pageRange[ 1 ]);
159
} else if (key == Entry::ftDOI)
160
result &= writeKeyValue(stream, "UR", PlainTextValue::text(value, bibtexfile));
163
if (!year.isEmpty() || !month.isEmpty()) {
164
result &= writeKeyValue(stream, "PY", QString("%1/%2//").arg(year).arg(month));
167
result &= writeKeyValue(stream, "ER", QString());
173
bool FileExporterRIS::writeKeyValue(QTextStream &stream, const QString& key, const QString& value)
175
stream << key << " - ";
176
if (!value.isEmpty())