~paulbrianstewart/ubuntu/oneiric/tellico/852247-Formatting-Fix

« back to all changes in this revision

Viewing changes to src/translators/risimporter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-31 19:33:05 UTC
  • mfrom: (0.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080131193305-9l01m5gfhykl6pkl
Tags: 1.3-1ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/control: build-dep on kdepim-dev
  - debian/control: drop versioned python from tellico-data suggests
  - debian/rules: call dh_icons

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#include "../field.h"
19
19
#include "../latin1literal.h"
20
20
#include "../progressmanager.h"
 
21
#include "../filehandler.h"
21
22
#include "../tellico_debug.h"
22
23
 
23
24
#include <kglobal.h> // for KMAX
106
107
  }
107
108
}
108
109
 
109
 
RISImporter::RISImporter(const KURL& url_) : Tellico::Import::TextImporter(url_), m_coll(0), m_cancelled(false) {
 
110
RISImporter::RISImporter(const KURL::List& urls_) : Tellico::Import::Importer(urls_), m_coll(0), m_cancelled(false) {
110
111
  initTagMap();
111
112
  initTypeMap();
112
113
}
144
145
    risFields.insert(ris, f);
145
146
  }
146
147
 
147
 
  QString str = text();
 
148
  ProgressItem& item = ProgressManager::self()->newProgressItem(this, progressLabel(), true);
 
149
  item.setTotalSteps(urls().count() * 100);
 
150
  connect(&item, SIGNAL(signalCancelled(ProgressItem*)), SLOT(slotCancel()));
 
151
  ProgressItem::Done done(this);
 
152
 
 
153
  int count = 0;
 
154
  KURL::List urls = this->urls();
 
155
  for(KURL::List::ConstIterator it = urls.begin(); it != urls.end() && !m_cancelled; ++it, ++count) {
 
156
    readURL(*it, count, risFields);
 
157
  }
 
158
 
 
159
  if(m_cancelled) {
 
160
    m_coll = 0;
 
161
  }
 
162
  return m_coll;
 
163
}
 
164
 
 
165
void RISImporter::readURL(const KURL& url_, int n, const QDict<Data::Field>& risFields_) {
 
166
  QString str = FileHandler::readTextFile(url_);
 
167
  if(str.isEmpty()) {
 
168
    return;
 
169
  }
 
170
 
148
171
  QTextIStream t(&str);
149
172
 
150
173
  const uint length = str.length();
151
174
  const uint stepSize = KMAX(s_stepSize, length/100);
152
175
  const bool showProgress = options() & ImportProgress;
153
176
 
154
 
  ProgressItem& item = ProgressManager::self()->newProgressItem(this, progressLabel(), true);
155
 
  item.setTotalSteps(length);
156
 
  connect(&item, SIGNAL(signalCancelled(ProgressItem*)), SLOT(slotCancel()));
157
 
  ProgressItem::Done done(this);
 
177
  bool needToAddFinal = false;
158
178
 
159
179
  uint j = 0;
160
180
  Data::EntryPtr entry = new Data::Entry(m_coll);
161
181
  // technically, the spec requires a space immediately after the hyphen
162
182
  // however, at least one website (Springer) outputs RIS with no space after the final "ER -"
163
183
  // so just strip the white space later
164
 
  QRegExp rx(QString::fromLatin1("^(\\w\\w)\\s\\s-(.*)$"));
 
184
  // also be gracious and allow only any amount of space before hyphen
 
185
  QRegExp rx(QString::fromLatin1("^(\\w\\w)\\s+-(.*)$"));
165
186
  QString currLine, nextLine;
166
187
  for(currLine = t.readLine(); !m_cancelled && !currLine.isNull(); currLine = nextLine, j += currLine.length()) {
167
188
    nextLine = t.readLine();
171
192
    if(tag.isEmpty()) {
172
193
      continue;
173
194
    }
174
 
//    kdDebug() << tag << ": " << value << endl;
 
195
//    myDebug() << tag << ": " << value << endl;
175
196
    // if the next line is not empty and does not match start regexp, append to value
176
197
    while(!nextLine.isEmpty() && nextLine.find(rx) == -1) {
177
198
      value += nextLine.stripWhiteSpace();
182
203
    if(tag == Latin1Literal("ER")) {
183
204
      m_coll->addEntries(entry);
184
205
      entry = new Data::Entry(m_coll);
 
206
      needToAddFinal = false;
185
207
      continue;
186
208
    } else if(tag == Latin1Literal("TY") && s_typeMap->contains(value)) {
187
209
      // for entry-type, switch it to normalized type name
193
215
    // the lookup scheme is:
194
216
    // 1. any field has an RIS property that matches the tag name
195
217
    // 2. default field mapping tag -> field name
196
 
    Data::FieldPtr f = risFields.find(tag);
 
218
    Data::FieldPtr f = risFields_.find(tag);
197
219
    if(!f) {
198
220
      // special case for BT
199
221
      // primary title for books, secondary for everything else
210
232
    if(!f) {
211
233
      continue;
212
234
    }
 
235
    needToAddFinal = true;
213
236
 
214
237
    // harmless for non-choice fields
215
238
    // for entry-type, want it in lower case
221
244
    entry->setField(f, value);
222
245
 
223
246
    if(showProgress && j%stepSize == 0) {
224
 
      ProgressManager::self()->setProgress(this, j);
 
247
      ProgressManager::self()->setProgress(this, n*100 + 100*j/length);
225
248
      kapp->processEvents();
226
249
    }
227
250
  }
228
 
 
229
 
  if(m_cancelled) {
230
 
    m_coll = 0;
 
251
  if(needToAddFinal) {
 
252
    m_coll->addEntries(entry);
231
253
  }
232
 
  return m_coll;
233
254
}
234
255
 
235
256
Tellico::Data::FieldPtr RISImporter::fieldByTag(const QString& tag_) {