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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
/*
A KIPI plugin to generate HTML image galleries
Copyright 2006 Aurelien Gateau <aurelien dot gateau at free.fr>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// Self
#include "generator.moc"
// Qt
#include <qdir.h>
#include <qfile.h>
#include <qpainter.h>
#include <qregexp.h>
// KDE
#include <kaboutdata.h>
#include <kapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <kio/netaccess.h>
#include <kstandarddirs.h>
#include <kurl.h>
// KIPI
#include <libkipi/batchprogressdialog.h>
#include <libkipi/imageinfo.h>
#include <libkipi/interface.h>
// libxslt
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <libxslt/xslt.h>
#include <libexslt/exslt.h>
// Local
#include "abstractthemeparameter.h"
#include "galleryinfo.h"
#include "theme.h"
#include "xmlutils.h"
namespace KIPIHTMLExport {
typedef QMap<QCString,QCString> XsltParameterMap;
/**
* Produce a web-friendly file name
*/
QString webifyFileName(QString fileName) {
fileName=fileName.lower();
// Remove potentially troublesome chars
fileName=fileName.replace(QRegExp("[^-0-9a-z]+"), "_");
return fileName;
}
/**
* This helper class is used to make sure we use unique filenames
*/
class UniqueNameHelper {
public:
QString makeNameUnique(QString name) {
QString nameBase = name;
int count=2;
while (mList.findIndex(name)!=-1) {
name = nameBase + QString::number(count);
++count;
};
mList.append(name);
return name;
}
private:
QStringList mList;
};
/**
* Prepare an XSLT param, managing quote mess.
* abc => 'abc'
* a"bc => 'a"bc'
* a'bc => "a'bc"
* a"'bc => concat('a"', "'", 'bc')
*/
QCString makeXsltParam(const QString& txt) {
QString param;
static const char apos='\'';
static const char quote='"';
if (txt.find(apos)==-1) {
// First or second case: no apos
param= apos + txt + apos;
} else if (txt.find(quote)==-1) {
// Third case: only apos, no quote
param= quote + txt + quote;
} else {
// Forth case: both apos and quote :-(
QStringList lst=QStringList::split(apos, txt, true /*allowEmptyEntries*/);
QStringList::Iterator it=lst.begin(), end=lst.end();
param= "concat(";
param+= apos + *it + apos;
++it;
for (;it!=end; ++it) {
param+= ", \"'\", ";
param+= apos + *it + apos;
}
param+= ")";
}
//kdDebug() << "param: " << txt << " => " << param << endl;
return param.utf8();
}
/**
* Genearate a square thumbnail from @fullImage of @size x @size pixels
*/
QImage generateSquareThumbnail(const QImage& fullImage, int size) {
QImage image = fullImage.smoothScale(size, size, QImage::ScaleMax);
if (image.width() == size && image.height() == size) {
return image;
}
QPixmap croppedPix(size, size);
QPainter painter(&croppedPix);
int sx=0, sy=0;
if (image.width()>size) {
sx=(image.width() - size)/2;
} else {
sy=(image.height() - size)/2;
}
painter.drawImage(0, 0, image, sx, sy, size, size);
painter.end();
return croppedPix.convertToImage();
}
struct Generator::Private {
KIPI::Interface* mInterface;
GalleryInfo* mInfo;
KIPI::BatchProgressDialog* mProgressDialog;
Theme::Ptr mTheme;
// State info
bool mWarnings;
QString mXMLFileName;
UniqueNameHelper mUniqueNameHelper;
bool init() {
mTheme=Theme::findByInternalName(mInfo->theme());
if (!mTheme) {
logError( i18n("Could not find theme in '%1'").arg(mInfo->theme()) );
return false;
}
return true;
}
bool copyTheme() {
mProgressDialog->addedAction(i18n("Copying theme"), KIPI::ProgressMessage);
KURL srcURL=KURL(mTheme->directory());
KURL destURL=mInfo->destKURL();
destURL.addPath(srcURL.filename());
if (QFile::exists(destURL.path())) {
KIO::NetAccess::del(destURL, mProgressDialog);
}
bool ok=KIO::NetAccess::dircopy(srcURL, destURL, mProgressDialog);
if (!ok) {
logError(i18n("Could not copy theme"));
return false;
}
return true;
}
bool writeDataToFile(const QByteArray& data, const QString& destPath) {
QFile destFile(destPath);
if (!destFile.open(IO_WriteOnly)) {
logWarning(i18n("Could not open file '%1' for writing").arg(destPath));
return false;
}
if (destFile.writeBlock(data) != (Q_LONG)data.size()) {
logWarning(i18n("Could not save image to file '%1'").arg(destPath));
return false;
}
return true;
}
/**
* Helper class for generateImageAndXMLForURL
*/
void appendImageElementToXML(XMLWriter& xmlWriter, const QString& elementName, const QString& fileName, const QImage& image) {
XMLAttributeList attrList;
attrList.append("fileName", fileName);
attrList.append("width", image.width());
attrList.append("height", image.height());
XMLElement elem(xmlWriter, elementName, &attrList);
}
/**
* Generate images (full and thumbnail) for imageURL
* Fills xmlWriter with info about this image
*/
void generateImageAndXMLForURL(XMLWriter& xmlWriter, const QString& destDir, const KURL& imageURL) {
KIPI::ImageInfo info=mInterface->info(imageURL);
// Load image
QString path=imageURL.path();
QFile imageFile(path);
if (!imageFile.open(IO_ReadOnly)) {
logWarning(i18n("Could not read image '%1'").arg(path));
return;
}
QString imageFormat = QImageIO::imageFormat(&imageFile);
if (imageFormat.isEmpty()) {
logWarning(i18n("Format of image '%1' is unknown").arg(path));
return;
}
imageFile.close();
imageFile.open(IO_ReadOnly);
QByteArray imageData = imageFile.readAll();
QImage originalImage;
if (!originalImage.loadFromData(imageData) ) {
logWarning(i18n("Error loading image '%1'").arg(path));
return;
}
// Process images
QImage fullImage = originalImage;
if (!mInfo->useOriginalImageAsFullImage()) {
if (mInfo->fullResize()) {
int size = mInfo->fullSize();
fullImage = fullImage.smoothScale(size, size, QImage::ScaleMin);
}
if (info.angle() != 0) {
QWMatrix matrix;
matrix.rotate(info.angle());
fullImage = fullImage.xForm(matrix);
}
}
QImage thumbnail = generateSquareThumbnail(fullImage, mInfo->thumbnailSize());
// Save images
QString baseFileName = webifyFileName(info.title());
baseFileName = mUniqueNameHelper.makeNameUnique(baseFileName);
// Save full
QString fullFileName;
if (mInfo->useOriginalImageAsFullImage()) {
fullFileName = baseFileName + "." + imageFormat.lower();
if (!writeDataToFile(imageData, destDir + "/" + fullFileName)) {
return;
}
} else {
fullFileName = baseFileName + "." + mInfo->fullFormatString().lower();
QString destPath = destDir + "/" + fullFileName;
if (!fullImage.save(destPath, mInfo->fullFormatString().ascii(), mInfo->fullQuality())) {
logWarning(i18n("Could not save image '%1' to '%2'").arg(path).arg(destPath));
return;
}
}
// Save original
QString originalFileName;
if (mInfo->copyOriginalImage()) {
originalFileName = "original_" + fullFileName;
if (!writeDataToFile(imageData, destDir + "/" + originalFileName)) {
return;
}
}
// Save thumbnail
QString thumbnailFileName = "thumb_" + baseFileName + "." + mInfo->thumbnailFormatString().lower();
QString destPath = destDir + "/" + thumbnailFileName;
if (!thumbnail.save(destPath, mInfo->thumbnailFormatString().ascii(), mInfo->thumbnailQuality())) {
logWarning(i18n("Could not save thumbnail for image '%1' to '%2'").arg(path).arg(destPath));
return;
}
// Write XML
XMLElement imageX(xmlWriter, "image");
xmlWriter.writeElement("title", info.title());
xmlWriter.writeElement("description", info.description());
appendImageElementToXML(xmlWriter, "full", fullFileName, fullImage);
appendImageElementToXML(xmlWriter, "thumbnail", thumbnailFileName, thumbnail);
if (mInfo->copyOriginalImage()) {
appendImageElementToXML(xmlWriter, "original", originalFileName, originalImage);
}
}
bool generateImagesAndXML() {
QString baseDestDir=mInfo->destKURL().path();
if (!createDir(baseDestDir)) return false;
mXMLFileName=baseDestDir + "/gallery.xml";
XMLWriter xmlWriter;
if (!xmlWriter.open(mXMLFileName)) {
logError(i18n("Could not create gallery.xml"));
return false;
}
XMLElement collectionsX(xmlWriter, "collections");
// Loop on collections
QValueList<KIPI::ImageCollection>::Iterator collectionIt=mInfo->mCollectionList.begin();
QValueList<KIPI::ImageCollection>::Iterator collectionEnd=mInfo->mCollectionList.end();
for (; collectionIt!=collectionEnd; ++collectionIt) {
KIPI::ImageCollection collection=*collectionIt;
logInfo( i18n("Generating files for \"%1\"").arg(collection.name()) );
QString collectionFileName = webifyFileName(collection.name());
QString destDir = baseDestDir + "/" + collectionFileName;
if (!createDir(destDir)) return false;
XMLElement collectionX(xmlWriter, "collection");
xmlWriter.writeElement("name", collection.name());
xmlWriter.writeElement("fileName", collectionFileName);
// Loop on image in collection
KURL::List imageList = collection.images();
KURL::List::Iterator it = imageList.begin();
KURL::List::Iterator end = imageList.end();
int pos = 1;
int count = imageList.count();
for (; it!=end; ++it, ++pos) {
mProgressDialog->setProgress(pos, count);
qApp->processEvents();
generateImageAndXMLForURL(xmlWriter, destDir, *it);
}
}
return true;
}
/**
* Add to map all the i18n parameters.
*/
void addI18nParameters(XsltParameterMap& map) {
map["i18nPrevious"] = makeXsltParam(i18n("Previous"));
map["i18nNext"] = makeXsltParam(i18n("Next"));
map["i18nCollectionList"] = makeXsltParam(i18n("Collection List"));
map["i18nOriginalImage"] = makeXsltParam(i18n("Original Image"));
map["i18nUp"] = makeXsltParam(i18n("Go Up"));
}
/**
* Add to map all the theme parameters, as specified by the user.
*/
void addThemeParameters(XsltParameterMap& map) {
Theme::ParameterList parameterList = mTheme->parameterList();
QString themeInternalName = mTheme->internalName();
Theme::ParameterList::ConstIterator
it = parameterList.begin(),
end = parameterList.end();
for (; it!=end; ++it) {
AbstractThemeParameter* themeParameter = *it;
QCString internalName = themeParameter->internalName();
QString value = mInfo->getThemeParameterValue(
themeInternalName,
internalName,
themeParameter->defaultValue());
map[internalName] = makeXsltParam(value);
}
}
bool generateHTML() {
logInfo(i18n("Generating HTML files"));
QString xsltFileName=mTheme->directory() + "/template.xsl";
CWrapper<xsltStylesheetPtr, xsltFreeStylesheet> xslt= xsltParseStylesheetFile( (const xmlChar*) xsltFileName.local8Bit().data() );
if (!xslt) {
logError(i18n("Could not load XSL file '%1'").arg(xsltFileName));
return false;
}
CWrapper<xmlDocPtr, xmlFreeDoc> xmlGallery=xmlParseFile( mXMLFileName.local8Bit().data() );
if (!xmlGallery) {
logError(i18n("Could not load XML file '%1'").arg(mXMLFileName));
return false;
}
// Prepare parameters
XsltParameterMap map;
addI18nParameters(map);
addThemeParameters(map);
const char** params=new const char*[map.size()*2+1];
XsltParameterMap::Iterator it=map.begin(), end=map.end();
const char** ptr=params;
for (;it!=end; ++it) {
*ptr=it.key().data();
++ptr;
*ptr=it.data().data();
++ptr;
}
*ptr=0;
// Move to the destination dir, so that external documents get correctly
// produced
QString oldCD=QDir::currentDirPath();
QDir::setCurrent(mInfo->destKURL().path());
CWrapper<xmlDocPtr, xmlFreeDoc> xmlOutput= xsltApplyStylesheet(xslt, xmlGallery, params);
QDir::setCurrent(oldCD);
//delete []params;
if (!xmlOutput) {
logError(i18n("Error processing XML file"));
return false;
}
QString destFileName=mInfo->destKURL().path() + "/index.html";
FILE* file=fopen(destFileName.local8Bit().data(), "w");
if (!file) {
logError(i18n("Could not open '%1' for writing").arg(destFileName));
return false;
}
xsltSaveResultToFile(file, xmlOutput, xslt);
fclose(file);
return true;
}
bool createDir(const QString& dirName) {
QStringList parts = QStringList::split('/', dirName);
QStringList::ConstIterator it = parts.begin(), end = parts.end();
QDir dir = QDir::root();
for( ;it!=end; ++it) {
QString part = *it;
if (!dir.exists(part)) {
if (!dir.mkdir(part)) {
logError(i18n("Could not create folder '%1' in '%2'").arg(part).arg(dir.absPath()));
return false;
}
}
dir.cd(part);
}
return true;
}
void logInfo(const QString& msg) {
mProgressDialog->addedAction(msg, KIPI::ProgressMessage);
}
void logError(const QString& msg) {
mProgressDialog->addedAction(msg, KIPI::ErrorMessage);
}
void logWarning(const QString& msg) {
mProgressDialog->addedAction(msg, KIPI::WarningMessage);
mWarnings=true;
}
};
Generator::Generator(KIPI::Interface* interface, GalleryInfo* info, KIPI::BatchProgressDialog* progressDialog)
: QObject() {
d=new Private;
d->mInterface=interface;
d->mInfo=info;
d->mProgressDialog=progressDialog;
d->mWarnings=false;
}
Generator::~Generator() {
delete d;
}
bool Generator::run() {
if (!d->init()) return false;
QString destDir=d->mInfo->destKURL().path();
if (!d->createDir(destDir)) return false;
if (!d->copyTheme()) return false;
if (!d->generateImagesAndXML()) return false;
exsltRegisterAll();
bool result=d->generateHTML();
xsltCleanupGlobals();
xmlCleanupParser();
return result;
}
bool Generator::warnings() const {
return d->mWarnings;
}
} // namespace
|