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
|
/*
* This file is part of Calligra
*
* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
*
* Contact: Thorsten Zachmann thorsten.zachmann@nokia.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include <KoDocument.h>
#include <KoPADocument.h>
#include <KWDocument.h>
#include <sheets/part/Doc.h>
#include <KMimeType>
#include <kmimetypetrader.h>
#include <kparts/componentfactory.h>
#include <kcmdlineargs.h>
#include <kdebug.h>
#include <QApplication>
#include <QBuffer>
#include <QDir>
#include <QFileInfo>
#include <QImage>
#include <QTimer>
#include "CSThumbProviderStage.h"
#include "CSThumbProviderTables.h"
#include "CSThumbProviderWords.h"
#include "config_cstester.h"
#ifdef BUILD_KARBON
#include <KarbonPart.h>
#include "CSThumbProviderKarbon.h"
#endif
KoDocument* openFile(const QString &filename)
{
const QString mimetype = KMimeType::findByPath(filename)->name();
QString error;
KoDocument *document = KMimeTypeTrader::self()->createPartInstanceFromQuery<KoDocument>(
mimetype, 0, 0, QString(),
QVariantList(), &error );
if (!error.isEmpty()) {
qWarning() << "Error cerating document" << mimetype << error;
return 0;
}
if (0 != document) {
KUrl url;
url.setPath(filename);
document->setCheckAutoSaveFile(false);
document->setAutoErrorHandlingEnabled(false);
if (document->openUrl(filename)) {
document->setReadWrite(false);
}
else {
kWarning(31000)<< "openUrl failed" << filename << mimetype << error;
delete document;
document = 0;
}
}
return document;
}
QString saveFile(KoDocument *document, const QString &filename, const QString &outname)
{
QString saveAs = outname;
// use the name and add -roundtrip
if (outname.isEmpty()) {
saveAs = filename;
int dotPos = saveAs.lastIndexOf('.');
if (dotPos != -1) {
saveAs.truncate(dotPos);
}
saveAs += "-roundtrip";
}
QByteArray mimetype = document->nativeFormatMimeType();
KMimeType::Ptr mime = KMimeType::mimeType(mimetype);
Q_ASSERT(mime);
QString extension = mime->mainExtension();
saveAs += extension;
KUrl url;
url.setPath(saveAs);
document->setOutputMimeType(mimetype, 0);
document->saveAs(url);
kDebug(31000) << "save done";
return saveAs;
}
QList<QImage> createThumbnails(KoDocument *document, const QSize &thumbSize)
{
CSThumbProvider *tp = 0;
if (KoPADocument *doc = qobject_cast<KoPADocument*>(document)) {
tp = new CSThumbProviderStage(doc);
}
else if (Calligra::Sheets::Doc *doc = qobject_cast<Calligra::Sheets::Doc*>(document)) {
tp = new CSThumbProviderTables(doc);
}
else if (KWDocument *doc = qobject_cast<KWDocument*>(document)) {
tp = new CSThumbProviderWords(doc);
}
#ifdef BUILD_KARBON
else if (KarbonPart *doc = qobject_cast<KarbonPart*>(document)) {
tp = new CSThumbProviderKarbon(doc);
}
#endif
return tp ? tp->createThumbnails(thumbSize) : QList<QImage>();
}
void saveThumbnails(const QList<QImage> &thumbnails, const QString &dir)
{
int i = 0;
for (QList<QImage>::const_iterator it(thumbnails.constBegin()); it != thumbnails.constEnd(); ++it) {
// it is not possible to use QString("%1/thumb_%2.png").arg(dir).arg(++i);
// as dir can contain % values which then might or might not be overwritten by the second arg
QString thumbFilename = dir + QString("/thumb_%2.png").arg(++i);
it->save(thumbFilename, "PNG");
}
}
void saveThumbnails(const QFileInfo &file, const QList<QImage> &thumbnails, const QString &outdir)
{
QDir dir(outdir);
QString checkSubDir(file.fileName() + ".check");
dir.mkdir(checkSubDir);
saveThumbnails(thumbnails, outdir + '/' + checkSubDir);
}
bool checkThumbnails(const QList<QImage> &thumbnails, const QString &dir, bool verbose)
{
bool success = true;
int i = 0;
for (QList<QImage>::const_iterator it(thumbnails.constBegin()); it != thumbnails.constEnd(); ++it) {
QString thumbFilename = dir + QString("/thumb_%2.png").arg(++i);
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
it->save(&buffer, "PNG");
QFile file(thumbFilename);
if (!file.open(QFile::ReadOnly)) {
return false;
}
QByteArray baCheck(file.readAll());
if (ba != baCheck) {
qDebug() << "Check failed:" << dir << "Page" << i << "differ";
success = false;
}
else if (verbose) {
qDebug() << "Check successful:" << dir << "Page" << i << "identical";
}
}
return success;
}
bool checkThumbnails(const QList<QImage> &thumbnails, const QList<QImage> &others, bool verbose)
{
bool success = true;
if (thumbnails.size() != others.size()) {
qDebug() << "Check failed: number of pages different" << thumbnails.size() << "!=" << others.size();
return false;
}
int i = 1;
QList<QImage>::const_iterator it(thumbnails.constBegin());
QList<QImage>::const_iterator oIt(others.constBegin());
for (; it != thumbnails.constEnd(); ++it, ++oIt, ++i) {
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
it->save(&buffer, "PNG");
QByteArray baCheck;
QBuffer oBuffer(&baCheck);
oBuffer.open(QIODevice::WriteOnly);
oIt->save(&oBuffer, "PNG");
if (ba != baCheck) {
qDebug() << "Check failed:" << "Page" << i << "differ";
success = false;
}
else if (verbose) {
qDebug() << "Check successful:" << "Page" << i << "identical";
}
}
return success;
}
int main(int argc, char *argv[])
{
KCmdLineArgs::init(argc, argv, "cstester", 0, KLocalizedString(), 0, KLocalizedString());
KCmdLineOptions options;
options.add("create", ki18n("create verification data for file"));
options.add("indir <dir>", ki18n("directory to read the data from"));
options.add("outdir <dir>", ki18n("directory to save the data to"));
options.add("roundtrip", ki18n("load/save/load and check the document is the same after load and save/load"));
options.add("verbose", ki18n("be verbose"));
options.add("!verify", ki18n("verify the file"));
options.add( "+file", ki18n("file to use"));
KCmdLineArgs::addCmdLineOptions(options);
QApplication app(argc, argv);
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
bool create = false;
bool roundtrip = false;
bool verify = false;
int optionCount = 0;
if (args->isSet("create")) {
create = true;
optionCount++;
}
if (args->isSet("roundtrip")) {
roundtrip = true;
optionCount++;
}
if (args->isSet("verify")) {
verify = true;
optionCount++;
}
if (optionCount > 1) {
kError() << "create, roundtrip and verify cannot be used the same time";
exit(1);
}
else if (optionCount < 1) {
kError() << "one of the options create, roundtrip or verify needs to be specified";
exit(1);
}
QString outDir;
if (args->isSet("outdir")) {
// check if it is a directory
QDir dir(args->getOption("outdir"));
if (!dir.exists()) {
kError() << "outdir" << args->getOption("outdir") << "does not exist";
exit(1);
}
outDir = dir.path();
}
QString inDir;
if (args->isSet("indir")) {
// check if it is a directory
QDir dir(args->getOption("indir"));
if (!dir.exists()) {
kError() << "indir" << args->getOption("indir") << "does not exist";
exit(1);
}
inDir = dir.path();
}
bool verbose = args->isSet("verbose");
int exitValue = 0;
int successful = 0;
int failed = 0;
for (int i=0; i < args->count(); ++i) {
QString filename(args->arg(i));
QFileInfo file(filename);
QString checkDir;
if (!args->isSet("indir")) {
checkDir = filename + ".check";
}
else {
checkDir = inDir + '/' + file.fileName() + ".check";
}
// this is wrong for multiple files in different dirs
if (!args->isSet("outdir")) {
outDir = file.path();
}
qDebug() << "filename" << filename << "path" << file.path() << file.completeBaseName() << checkDir << file.absoluteFilePath();
qDebug() << "inDir" << inDir << "outDir" << outDir << "checkDir" << checkDir;
// filename must be a absolute path
KoDocument* document = openFile(file.absoluteFilePath());
if (!document) {
exit(2);
}
QList<QImage> thumbnails(createThumbnails(document, QSize(800,800)));
qDebug() << "created" << thumbnails.size() << "thumbnails";
if (create) {
saveThumbnails(file, thumbnails, outDir);
}
else if (verify) {
if (args->isSet("outdir")) {
saveThumbnails(file, thumbnails, outDir);
}
if (checkThumbnails(thumbnails, checkDir, verbose)) {
++successful;
}
else {
++failed;
exitValue = 2;
}
}
else if (roundtrip) {
QString rFilename = saveFile(document, filename, "cstester-roundtrip");
delete document;
QFileInfo rFile(rFilename);
qDebug() << roundtrip << "rFilename" << rFilename << rFile.absoluteFilePath();
document = openFile(rFile.absoluteFilePath());
QList<QImage> others(createThumbnails(document, QSize(800,800)));
if (args->isSet("outdir")) {
saveThumbnails(file, others, outDir);
saveThumbnails(file, thumbnails, outDir + "/before");
}
if (checkThumbnails(thumbnails, others, verbose)) {
++successful;
}
else {
++failed;
exitValue = 2;
}
}
delete document;
}
if (verify || roundtrip) {
qDebug() << "Totals:" << successful << "passed" << failed << "failed";
}
QTimer::singleShot(1, &app, SLOT(quit()));
app.exec();
return exitValue;
}
|