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

« back to all changes in this revision

Viewing changes to src/imagefactory.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:
25
25
#include <kimageeffect.h>
26
26
 
27
27
#include <qfile.h>
 
28
#include <qdir.h>
28
29
 
29
30
#define RELEASE_IMAGES
30
31
 
44
45
Tellico::StringSet ImageFactory::s_imagesInTmpDir;
45
46
Tellico::StringSet ImageFactory::s_imagesToRelease;
46
47
KTempDir* ImageFactory::s_tmpDir = 0;
 
48
QString ImageFactory::s_localDir;
47
49
 
48
50
void ImageFactory::init() {
49
51
  if(!s_needInit) {
69
71
  return dataDir;
70
72
}
71
73
 
72
 
QString ImageFactory::addImage(const KURL& url_, bool quiet_, const KURL& refer_) {
73
 
  return addImageImpl(url_, quiet_, refer_).id();
74
 
}
75
 
 
76
 
const Tellico::Data::Image& ImageFactory::addImageImpl(const KURL& url_, bool quiet_, const KURL& refer_) {
 
74
QString ImageFactory::localDir() {
 
75
  if(s_localDir.isEmpty()) {
 
76
    return dataDir();
 
77
  }
 
78
  return s_localDir;
 
79
}
 
80
 
 
81
QString ImageFactory::addImage(const KURL& url_, bool quiet_, const KURL& refer_, bool link_) {
 
82
  return addImageImpl(url_, quiet_, refer_, link_).id();
 
83
}
 
84
 
 
85
const Tellico::Data::Image& ImageFactory::addImageImpl(const KURL& url_, bool quiet_, const KURL& refer_, bool link_) {
77
86
  if(url_.isEmpty() || !url_.isValid()) {
78
87
    return s_null;
79
88
  }
90
99
    return s_null;
91
100
  }
92
101
 
 
102
  if(link_) {
 
103
    img->setLinkOnly(true);
 
104
    img->setID(url_.url());
 
105
  }
 
106
 
93
107
  if(hasImage(img->id())) {
94
108
//    myDebug() << "### ImageFactory::addImageImpl() - hasImage() is true!" << endl;
95
109
    const Data::Image& img2 = imageById(img->id());
175
189
  KURL u;
176
190
  if(dir_ == DataDir) {
177
191
    u.setPath(dataDir() + id_);
178
 
  } else { // Temp
 
192
  } else if(dir_ == LocalDir) {
 
193
    u.setPath(localDir() + id_);
 
194
  } else{ // Temp
179
195
    u.setPath(tempDir() + id_);
180
196
  }
181
197
 
232
248
    return false;
233
249
  }
234
250
 
 
251
  if(img.linkOnly()) {
 
252
    myDebug() << "ImageFactory::writeImage() - " << id_ << ": link only, not writing!" << endl;
 
253
    return true;
 
254
  }
 
255
 
235
256
  KURL target = targetDir_;
236
257
  target.addPath(id_);
237
258
 
245
266
//  myLog() << "ImageFactory::writeCachedImage() - dir = " << (dir_ == DataDir ? "DataDir" : "TmpDir" )
246
267
//                                                         << "; id = " << id_ << endl;
247
268
 
248
 
  QString path = ( dir_ == DataDir ? dataDir() : tempDir() );
 
269
  QString path = ( dir_ == DataDir ? dataDir() : dir_ == TempDir ? tempDir() : localDir() );
249
270
 
250
271
  // images in the temp directory are erased every session, so we can track
251
272
  // whether they've already been written with a simple string set.
252
273
  // images in the data directory are persistent, so we have to check the
253
274
  // actual file existence
254
 
  bool exists = ( dir_ == DataDir ? QFile::exists(path + id_) : s_imagesInTmpDir.has(id_) );
 
275
  bool exists = ( dir_ == TempDir ? s_imagesInTmpDir.has(id_) : QFile::exists(path + id_));
255
276
 
256
277
  if(!force_ && exists) {
257
278
//    myDebug() << "...writeCachedImage() - exists = true: " << id_ << endl;
 
279
  } else if(!force_ && !exists && dir_ == LocalDir) {
 
280
    QDir dir(localDir());
 
281
    if(!dir.exists()) {
 
282
      myDebug() << "ImageFactory::writeCachedImage() - creating " << s_localDir << endl;
 
283
      dir.mkdir(localDir());
 
284
    }
258
285
  } else {
259
286
//    myLog() << "ImageFactory::writeCachedImage() - dir = " << (dir_ == DataDir ? "DataDir" : "TmpDir" )
260
287
//                                                           << "; id = " << id_ << endl;
311
338
    return *img;
312
339
  }
313
340
 
 
341
  // if the image is link only, we need to load it
 
342
  // but can't call imageInfo() since that might recurse into imageById()
 
343
  // also, the image info cache might not have it so check if the
 
344
  // id is a valid absolute url
 
345
  // yeah, it's probably slow
 
346
  if((s_imageInfoMap.contains(id_) && s_imageInfoMap[id_].linkOnly) || !KURL::isRelativeURL(id_)) {
 
347
    KURL u = id_;
 
348
    if(u.isValid()) {
 
349
      return addImageImpl(u, false, KURL(), true);
 
350
    }
 
351
  }
 
352
 
314
353
  // the document does a delayed loading of the images, sometimes
315
354
  // so an image could be in the tmp dir and not be in the cache
316
355
  // or it could be too big for the cache
352
391
    // if we're loading from the application data dir, but images are being saved in the
353
392
    // data file instead, then consider the document to be modified since it needs
354
393
    // the image saved
355
 
    if(Config::writeImagesInFile()) {
 
394
    if(Config::imageLocation() != Config::ImagesInAppDir) {
356
395
      Data::Document::self()->slotSetModified(true);
357
396
    }
358
397
    const Data::Image& img2 = addCachedImageImpl(id_, DataDir);
362
401
//      myLog() << "...imageById() - found in data dir" << endl;
363
402
      return img2;
364
403
    }
365
 
  } else {
366
 
    myDebug() << "***ImageFactory::imageById() - not found: " << id_ << endl;
367
 
  }
 
404
  }
 
405
  // if localDir() == DataDir(), then there's nothing left to check
 
406
  if(localDir() == dataDir()) {
 
407
    return s_null;
 
408
  }
 
409
  exists = QFile::exists(localDir() + id_);
 
410
  if(exists) {
 
411
    // if we're loading from the application data dir, but images are being saved in the
 
412
    // data file instead, then consider the document to be modified since it needs
 
413
    // the image saved
 
414
    if(Config::imageLocation() != Config::ImagesInLocalDir) {
 
415
      Data::Document::self()->slotSetModified(true);
 
416
    }
 
417
    const Data::Image& img2 = addCachedImageImpl(id_, LocalDir);
 
418
    if(img2.isNull()) {
 
419
      myDebug() << "ImageFactory::imageById() - tried to add from LocalDir, but failed: " << id_ << endl;
 
420
    } else {
 
421
//      myLog() << "...imageById() - found in data dir" << endl;
 
422
      return img2;
 
423
    }
 
424
  }
 
425
  myDebug() << "***ImageFactory::imageById() - not found: " << id_ << endl;
368
426
  return s_null;
369
427
}
370
428
 
531
589
#endif
532
590
}
533
591
 
 
592
void ImageFactory::setLocalDirectory(const KURL& url_) {
 
593
  if(url_.isEmpty()) {
 
594
    return;
 
595
  }
 
596
  if(!url_.isLocalFile()) {
 
597
    myWarning() << "ImageFactory::setLocalDirectory() - Tellico can only save images to local disk" << endl;
 
598
    myWarning() << "unable to save to " << url_ << endl;
 
599
  } else {
 
600
    s_localDir = url_.directory(false);
 
601
    // could have already been set once
 
602
    if(!url_.fileName().contains(QString::fromLatin1("_files"))) {
 
603
      s_localDir += url_.fileName().section('.', 0, 0) + QString::fromLatin1("_files/");
 
604
    }
 
605
    myLog() << "ImageFactory::setLocalDirectory() - local dir = " << s_localDir << endl;
 
606
  }
 
607
}
 
608
 
534
609
#undef RELEASE_IMAGES