summaryrefslogtreecommitdiffstats
path: root/src/translators/deliciousimporter.cpp
blob: be6fe3eda04d42c9ff0f59f2918eb8abcb14ade8 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***************************************************************************
    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 "deliciousimporter.h"
#include "../collection.h"
#include "../rtf2html/rtf2html.h"
#include "../imagefactory.h"
#include "../tellico_debug.h"

#include <kstandarddirs.h>

#include <tqfile.h>

using Tellico::Import::DeliciousImporter;

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

bool DeliciousImporter::canImport(int type) const {
  return type == Data::Collection::Book ||
         type == Data::Collection::Video ||
         type == Data::Collection::Game;
}

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

  KURL libraryDir = url();
  libraryDir.setPath(url().directory() + "Images/");
  const TQStringList imageDirs = TQStringList()
                              << TQString::fromLatin1("Large Covers/")
                              << TQString::fromLatin1("Medium Covers/")
                              << TQString::fromLatin1("Small Covers/")
                              << TQString::fromLatin1("Plain Covers/");
  TQString commField;
  switch(coll->type()) {
    case Data::Collection::Book:
      commField = TQString::fromLatin1("comments"); break;
    case Data::Collection::Video:
      commField = TQString::fromLatin1("plot"); break;
    case Data::Collection::Game:
      commField = TQString::fromLatin1("description"); break;
    default:
      myWarning() << "bad collection type:" << coll->type() << endl;
  }

  const TQString uuidField = TQString::fromLatin1("uuid");
  const TQString coverField = TQString::fromLatin1("cover");
  const bool isLocal = url().isLocalFile();

  Data::EntryVec entries = coll->entries();
  for(Data::EntryVecIt entry = entries.begin(); entry != entries.end(); ++entry) {
    TQString comments = entry->field(commField);
    if(!comments.isEmpty()) {
      RTF2HTML rtf2html(comments);
      entry->setField(commField, rtf2html.toHTML());
    }

    //try to add images
    TQString uuid = entry->field(uuidField);
    if(!uuid.isEmpty() && isLocal) {
      for(TQStringList::ConstIterator it = imageDirs.begin(); it != imageDirs.end(); ++it) {
        TQString imgPath = libraryDir.path() + *it + uuid;
        if(!TQFile::exists(imgPath)) {
          continue;
        }
        TQString imgID = ImageFactory::addImage(imgPath, true);
        if(!imgID.isEmpty()) {
          entry->setField(coverField, imgID);
        }
        break;
      }
    }
  }
  coll->removeField(uuidField);
  return coll;
}

#include "deliciousimporter.moc"