summaryrefslogtreecommitdiffstats
path: root/src/translators/referencerimporter.cpp
blob: e3cb72c1322b1e3cf4c2bb24c1d9b996a5d484d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/***************************************************************************
    copyright            : (C) 2007 by Robby Stephenson
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include "referencerimporter.h"
#include "../collection.h"
#include "../core/netaccess.h"
#include "../imagefactory.h"

#include <kstandarddirs.h>

using Tellico::Import::ReferencerImporter;

ReferencerImporter::ReferencerImporter(const KURL& url_) : XSLTImporter(url_) {
  TQString xsltFile = locate("appdata", TQString::fromLatin1("referencer2tellico.xsl"));
  if(!xsltFile.isEmpty()) {
    KURL u;
    u.setPath(xsltFile);
    XSLTImporter::setXSLTURL(u);
  } else {
    kdWarning() << "ReferencerImporter() - unable to find referencer2tellico.xml!" << endl;
  }
}

bool ReferencerImporter::canImport(int type) const {
  return type == Data::Collection::Bibtex;
}

Tellico::Data::CollPtr ReferencerImporter::collection() {
  Data::CollPtr coll = XSLTImporter::collection();
  if(!coll) {
    return 0;
  }

  Data::FieldPtr field = coll->fieldByName(TQString::fromLatin1("cover"));
  if(!field && !coll->imageFields().isEmpty()) {
    field = coll->imageFields().front();
  } else if(!field) {
    field = new Data::Field(TQString::fromLatin1("cover"), i18n("Front Cover"), Data::Field::Image);
    coll->addField(field);
  }

  Data::EntryVec entries = coll->entries();
  for(Data::EntryVecIt entry = entries.begin(); entry != entries.end(); ++entry) {
    TQString url = entry->field(TQString::fromLatin1("url"));
    if(url.isEmpty()) {
      continue;
    }
    TQPixmap pix = NetAccess::filePreview(url);
    if(pix.isNull()) {
      continue;
    }
    TQString id = ImageFactory::addImage(pix, TQString::fromLatin1("PNG"));
    if(id.isEmpty()) {
      continue;
    }
    entry->setField(field, id);
  }
  return coll;
}

#include "referencerimporter.moc"