File indexing completed on 2024-05-05 17:04:28

0001 /*
0002  * Assists in creating thumbnails for Gemini's file views
0003  * Copyright (C) 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include <QFile>
0022 #include <QDir>
0023 #include <QProcessEnvironment>
0024 #include <QDebug>
0025 #include <QTimer>
0026 #include <QCommandLineParser>
0027 #include <QApplication>
0028 
0029 #include <KAboutData>
0030 #include <KLocalizedString>
0031 #include <KCrash>
0032 
0033 #include <calligraversion.h>
0034 
0035 #include "ThumbnailHelperImpl.h"
0036 
0037 #ifdef Q_OS_WIN
0038 #include <windows.h>
0039 #endif
0040 
0041 int main( int argc, char** argv )
0042 {
0043 #ifdef Q_OS_WIN
0044     SetErrorMode(SEM_NOGPFAULTERRORBOX); 
0045 #endif
0046 
0047 #if defined HAVE_X11
0048     QApplication::setAttribute(Qt::AA_X11InitThreads);
0049 #endif
0050 
0051     // needed as kdelibs4support linking plugins seem to inject activated drkonqi
0052     // TODO: fix it, that seems very wrong
0053     KCrash::setDrKonqiEnabled(false);
0054 
0055     QApplication app(argc, argv);
0056 
0057     KAboutData aboutData("calligrageminithumbnailer",
0058                          i18n("Calligra Gemini Thumbnailer"),
0059                          QStringLiteral(CALLIGRA_VERSION_STRING),
0060                          i18n("Calligra Gemini: Writing and Presenting at Home and on the Go"),
0061                          KAboutLicense::GPL,
0062                          i18n("(c) 1999-%1 The Calligra team and KO GmbH.\n", QStringLiteral(CALLIGRA_YEAR)),
0063                          QString(),
0064                          QStringLiteral("https://www.calligra.org"),
0065                          QStringLiteral("submit@bugs.kde.org"));
0066 
0067     KAboutData::setApplicationData(aboutData);
0068 
0069     QCommandLineParser parser;
0070     aboutData.setupCommandLine(&parser);
0071 
0072     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("in"), i18n("Document to thumbnail"), QStringLiteral("local-url")));
0073     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("out"), i18n("The full path for the thumbnail file"), QStringLiteral("local-url")));
0074     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("width"), i18n("The width in pixels of the thumbnail"), QStringLiteral("pixels")));
0075     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("height"), i18n("The height in pixels of the thumbnail"), QStringLiteral("pixels")));
0076 
0077     parser.process(app);
0078     aboutData.processCommandLine(&parser);
0079 
0080 #ifdef Q_OS_WIN
0081     QDir appdir(app.applicationDirPath());
0082     appdir.cdUp();
0083 
0084     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
0085     if (!env.contains("KDESYCOCA")) {
0086         _putenv_s("KDESYCOCA", QString(appdir.absolutePath() + "/sycoca").toLocal8Bit());
0087     }
0088     if (!env.contains("XDG_DATA_DIRS")) {
0089         _putenv_s("XDG_DATA_DIRS", QString(appdir.absolutePath() + "/share").toLocal8Bit());
0090     }
0091     _putenv_s("PATH", QString(appdir.absolutePath() + "/bin" + ";"
0092               + appdir.absolutePath() + "/lib" + ";"
0093               + appdir.absolutePath() + "/lib"  +  "/kde4" + ";"
0094               + appdir.absolutePath()).toLocal8Bit());
0095 
0096     app.addLibraryPath(appdir.absolutePath());
0097     app.addLibraryPath(appdir.absolutePath() + "/bin");
0098     app.addLibraryPath(appdir.absolutePath() + "/lib");
0099     app.addLibraryPath(appdir.absolutePath() + "/lib/kde4");
0100 #endif
0101 
0102     QString inFile = parser.value("in");
0103     QString outFile = parser.value("out");
0104     // Only create the thunbnail if:
0105     // 1) The infile exists and
0106     // 2) The outfile does /not/ exist
0107     if(!QFile::exists(inFile)) {
0108         qDebug() << "The document you are attempting to create a thumbnail of does not exist on disk:" << inFile;
0109     }
0110     else if(QFile::exists(outFile)) {
0111         qDebug() << "The thumbnail file you are asking to have used already exists on disk. We will refuse to overwrite it." << outFile;
0112     }
0113     else {
0114         ThumbnailHelperImpl helper;
0115         helper.convert(inFile, outFile, parser.value("width").toInt(), parser.value("height").toInt());
0116     }
0117     QTimer::singleShot(0, &app, SLOT(quit()));
0118 
0119     return app.exec();
0120 }