summaryrefslogtreecommitdiffstats
path: root/src/translators/amcimporter.cpp
blob: 0209ddc0b96b6dc27539b54d02b424f41dc9c269 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/***************************************************************************
    copyright            : (C) 2006 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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;                            *
 *                                                                         *
 ***************************************************************************/

// The information about the AMC file format was taken from the source code for
// GCfilms, (GPL) (c) 2005 Tian
// Monotheka, (GPL) (c) 2004, 2005 Michael Dominic K.
//                      2005 Aurelien Mino

#include "amcimporter.h"
#include "../collections/videocollection.h"
#include "../imagefactory.h"
#include "../latin1literal.h"
#include "../progressmanager.h"
#include "../tellico_debug.h"

#include <kapplication.h>

#include <tqfile.h>
#include <tqimage.h>

#include <limits.h>

namespace {
  static const TQCString AMC_FILE_ID = " AMC_X.Y Ant Movie Catalog 3.5.x   www.buypin.com    www.antp.be ";
}

using Tellico::Import::AMCImporter;

AMCImporter::AMCImporter(const KURL& url_) : DataImporter(url_), m_coll(0), m_cancelled(false) {
}

AMCImporter::~AMCImporter() {
}

bool AMCImporter::canImport(int type) const {
  return type == Data::Collection::Video;
}

Tellico::Data::CollPtr AMCImporter::collection() {
  if(m_coll) {
    return m_coll;
  }

  if(!fileRef().open()) {
    return 0;
  }

  TQIODevice* f = fileRef().file();
  m_ds.setDevice(f);
  // AMC is always little-endian? can't confirm
  m_ds.setByteOrder(TQDataStream::LittleEndian);

  const uint l = AMC_FILE_ID.length();
  TQMemArray<char> buffer(l+1);
  m_ds.readRawBytes(buffer.data(), l);
  TQString version = TQString::fromLocal8Bit(buffer, l);
  TQRegExp versionRx(TQString::tqfromLatin1(".+AMC_(\\d+)\\.(\\d+).+"));
  if(version.find(versionRx) == -1) {
    myDebug() << "AMCImporter::collection() - no file id match" << endl;
    return 0;
  }

  ProgressItem& item = ProgressManager::self()->newProgressItem(this, progressLabel(), true);
  item.setTotalSteps(f->size());
  connect(&item, TQT_SIGNAL(signalCancelled(ProgressItem*)), TQT_SLOT(slotCancel()));
  ProgressItem::Done done(this);

  m_coll = new Data::VideoCollection(true);

  m_majVersion = versionRx.cap(1).toInt();
  m_minVersion = versionRx.cap(2).toInt();
//  myDebug() << m_majVersion << "::" << m_minVersion << endl;

  readString(); // name
  readString(); // email
  if(m_majVersion <= 3 && m_minVersion < 5) {
    readString(); // icq
  }
  readString(); // webpage
  readString(); // description

  const bool showProgress = options() & ImportProgress;

  while(!m_cancelled && !f->atEnd()) {
    readEntry();
    if(showProgress) {
      ProgressManager::self()->setProgress(this, f->at());
      kapp->processEvents();
    }
  }

  return m_coll;
}

bool AMCImporter::readBool() {
  TQ_UINT8 b;
  m_ds >> b;
  return b;
}

TQ_UINT32 AMCImporter::readInt() {
  TQ_UINT32 i;
  m_ds >> i;
  if(i >= UINT_MAX) {
    i = 0;
  }
  return i;
}

TQString AMCImporter::readString() {
  // The serialization format is a length specifier first, then l bytes of data
  uint l = readInt();
  if(l == 0) {
    return TQString();
  }
  TQMemArray<char> buffer(l+1);
  m_ds.readRawBytes(buffer.data(), l);
  TQString s = TQString::fromLocal8Bit(buffer, l);
//  myDebug() << "string: " << s << endl;
  return s;
}

TQString AMCImporter::readImage(const TQString& format_) {
  uint l = readInt();
  if(l == 0) {
    return TQString();
  }
  TQMemArray<char> buffer(l+1);
  m_ds.readRawBytes(buffer.data(), l);
  TQByteArray bytes;
  bytes.setRawData(buffer.data(), l);
  TQImage img(bytes);
  bytes.resetRawData(buffer.data(), l);
  if(img.isNull()) {
    myDebug() << "AMCImporter::readImage() - null image" << endl;
    return TQString();
  }
  TQString format = TQString::tqfromLatin1("PNG");
  if(format_ == Latin1Literal(".jpg")) {
    format = TQString::tqfromLatin1("JPEG");
  } else if(format_ == Latin1Literal(".gif")) {
    format = TQString::tqfromLatin1("GIF");
  }
  return ImageFactory::addImage(img, format);
}

void AMCImporter::readEntry() {
  Data::EntryPtr e = new Data::Entry(m_coll);

  int id = readInt();
  if(id > 0) {
    e->setId(id);
  }
  readInt(); // add date

  int rating = readInt();
  if(m_majVersion >= 3 && m_minVersion >= 5) {
    rating /= 10;
  }
  e->setField(TQString::tqfromLatin1("rating"), TQString::number(rating));
  int year = readInt();
  if(year > 0) {
    e->setField(TQString::tqfromLatin1("year"), TQString::number(year));
  }
  int time = readInt();
  if(time > 0) {
    e->setField(TQString::tqfromLatin1("running-time"), TQString::number(time));
  }

  readInt(); // video bitrate
  readInt(); // audio bitrate
  readInt(); // number of files
  readBool(); // checked
  readString(); // media label
  e->setField(TQString::tqfromLatin1("medium"), readString());
  readString(); // source
  readString(); // borrower
  TQString s = readString(); // title
  if(!s.isEmpty()) {
    e->setField(TQString::tqfromLatin1("title"), s);
  }
  TQString s2 = readString(); // translated title
  if(s.isEmpty()) {
    e->setField(TQString::tqfromLatin1("title"), s2);
  }

  e->setField(TQString::tqfromLatin1("director"), readString());
  s = readString();
  TQRegExp roleRx(TQString::tqfromLatin1("(.+) \\(([^(]+)\\)"));
  roleRx.setMinimal(true);
  if(s.find(roleRx) > -1) {
    TQString role = roleRx.cap(2).lower();
    if(role == Latin1Literal("story") || role == Latin1Literal("written by")) {
      e->setField(TQString::tqfromLatin1("writer"), roleRx.cap(1));
    } else {
      e->setField(TQString::tqfromLatin1("producer"), s);
    }
  } else {
    e->setField(TQString::tqfromLatin1("producer"), s);
  }
  e->setField(TQString::tqfromLatin1("nationality"), readString());
  e->setField(TQString::tqfromLatin1("genre"), readString().replace(TQString::tqfromLatin1(", "), TQString::tqfromLatin1("; ")));

  e->setField(TQString::tqfromLatin1("cast"), parseCast(readString()).join(TQString::tqfromLatin1("; ")));

  readString(); // url
  e->setField(TQString::tqfromLatin1("plot"), readString());
  e->setField(TQString::tqfromLatin1("comments"), readString());
  s = readString(); // video format
  TQRegExp regionRx(TQString::tqfromLatin1("Region \\d"));
  if(s.find(regionRx) > -1) {
    e->setField(TQString::tqfromLatin1("region"), regionRx.cap(0));
  }
  e->setField(TQString::tqfromLatin1("audio-track"), readString()); // audio format
  readString(); // resolution
  readString(); // frame rate
  e->setField(TQString::tqfromLatin1("language"), readString()); // audio language
  e->setField(TQString::tqfromLatin1("subtitle"), readString()); // subtitle
  readString(); // file size
  s = readString(); // picture extension
  s = readImage(s); // picture
  if(!s.isEmpty()) {
    e->setField(TQString::tqfromLatin1("cover"), s);
  }

  m_coll->addEntries(e);
}

TQStringList AMCImporter::parseCast(const TQString& text_) {
  TQStringList cast;
  int nPar = 0;
  TQRegExp castRx(TQString::tqfromLatin1("[,()]"));
  TQString person, role;
  int oldPos = 0;
  for(int pos = text_.find(castRx); pos > -1; pos = text_.find(castRx, pos+1)) {
    if(text_.at(pos) == ',' && nPar%2 == 0) {
      // we're done with this one
      person += text_.mid(oldPos, pos-oldPos).stripWhiteSpace();
      TQString all = person;
      if(!role.isEmpty()) {
        if(role.startsWith(TQString::tqfromLatin1("as "))) {
          role = role.mid(3);
        }
        all += "::" + role;
      }
      cast << all;
      person.truncate(0);
      role.truncate(0);
      oldPos = pos+1; // add one to go past comma
    } else if(text_.at(pos) == '(') {
      if(nPar == 0) {
        person = text_.mid(oldPos, pos-oldPos).stripWhiteSpace();
        oldPos = pos+1; // add one to go past parenthesis
      }
      ++nPar;
    } else if(text_.at(pos) == ')') {
      --nPar;
      if(nPar == 0) {
        role = text_.mid(oldPos, pos-oldPos).stripWhiteSpace();
        oldPos = pos+1; // add one to go past parenthesis
      }
    }
  }
  // grab the last one
  if(nPar%2 == 0) {
    int pos = text_.length();
    person += text_.mid(oldPos, pos-oldPos).stripWhiteSpace();
    TQString all = person;
    if(!role.isEmpty()) {
      if(role.startsWith(TQString::tqfromLatin1("as "))) {
        role = role.mid(3);
      }
      all += "::" + role;
    }
    cast << all;
  }
  return cast;
}

void AMCImporter::slotCancel() {
  m_cancelled = true;
}

#include "amcimporter.moc"