~ubuntu-branches/ubuntu/precise/kbibtex/precise

« back to all changes in this revision

Viewing changes to src/libkbibtexio/fileexporterris.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 <QRegExp>
 
21
#include <QStringList>
 
22
 
 
23
#include <KDebug>
 
24
 
 
25
#include <entry.h>
 
26
 
 
27
#include "fileexporterris.h"
 
28
 
 
29
FileExporterRIS::FileExporterRIS() : FileExporter()
 
30
{
 
31
    // nothing
 
32
}
 
33
 
 
34
FileExporterRIS::~FileExporterRIS()
 
35
{
 
36
    // nothing
 
37
}
 
38
 
 
39
bool FileExporterRIS::save(QIODevice* iodevice, const Element* element, QStringList* /*errorLog*/)
 
40
{
 
41
    bool result = false;
 
42
    QTextStream stream(iodevice);
 
43
 
 
44
    const Entry *entry = dynamic_cast<const Entry*>(element);
 
45
    if (entry != NULL)
 
46
        result = writeEntry(stream, entry);
 
47
 
 
48
    return result && !m_cancelFlag;
 
49
}
 
50
 
 
51
bool FileExporterRIS::save(QIODevice* iodevice, const File* bibtexfile, QStringList* /*errorLog*/)
 
52
{
 
53
    bool result = true;
 
54
    m_cancelFlag = false;
 
55
    QTextStream stream(iodevice);
 
56
 
 
57
    for (File::ConstIterator it = bibtexfile->begin(); it != bibtexfile->end() && result && !m_cancelFlag; it++) {
 
58
        const Entry *entry = dynamic_cast<const Entry*>(*it);
 
59
        if (entry != NULL) {
 
60
//                 FIXME Entry *myEntry = bibtexfile->completeReferencedFieldsConst( entry );
 
61
            Entry *myEntry = new Entry(*entry);
 
62
            result &= writeEntry(stream, myEntry);
 
63
            delete myEntry;
 
64
        }
 
65
    }
 
66
 
 
67
    return result && !m_cancelFlag;
 
68
}
 
69
 
 
70
void FileExporterRIS::cancel()
 
71
{
 
72
    m_cancelFlag = true;
 
73
}
 
74
 
 
75
bool FileExporterRIS::writeEntry(QTextStream &stream, const Entry* entry, const File* bibtexfile)
 
76
{
 
77
    bool result = true;
 
78
    QString type = entry->type();
 
79
 
 
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");
 
92
    else
 
93
        writeKeyValue(stream, "TY", "GEN");
 
94
 
 
95
    QString year = "";
 
96
    QString month = "";
 
97
 
 
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);
 
102
 
 
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);
 
108
                if (person != NULL)
 
109
                    result &= writeKeyValue(stream, "AU", PlainTextValue::text(**it, bibtexfile));
 
110
                else
 
111
                    kWarning() << "Cannot write value " << PlainTextValue::text(**it, bibtexfile) << " for field AU (author), not supported by RIS format" << endl;
 
112
            }
 
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);
 
116
                if (person != NULL)
 
117
                    result &= writeKeyValue(stream, "ED", PlainTextValue::text(**it, bibtexfile));
 
118
                else
 
119
                    kWarning() << "Cannot write value " << PlainTextValue::text(**it, bibtexfile) << " for field ED (editor), not supported by RIS format" << endl;
 
120
            }
 
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 ]);
 
158
            }
 
159
        } else if (key == Entry::ftDOI)
 
160
            result &= writeKeyValue(stream, "UR", PlainTextValue::text(value, bibtexfile));
 
161
    }
 
162
 
 
163
    if (!year.isEmpty() || !month.isEmpty()) {
 
164
        result &= writeKeyValue(stream, "PY", QString("%1/%2//").arg(year).arg(month));
 
165
    }
 
166
 
 
167
    result &= writeKeyValue(stream, "ER", QString());
 
168
    stream << endl;
 
169
 
 
170
    return result;
 
171
}
 
172
 
 
173
bool FileExporterRIS::writeKeyValue(QTextStream &stream, const QString& key, const QString& value)
 
174
{
 
175
    stream << key << "  - ";
 
176
    if (!value.isEmpty())
 
177
        stream << value;
 
178
    stream << endl;
 
179
 
 
180
    return true;
 
181
}