File indexing completed on 2024-05-12 04:19:59

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2012 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 // Local
0022 #include <../auto/testutils.h>
0023 #include <lib/about.h>
0024 #include <lib/thumbnailprovider/thumbnailprovider.h>
0025 
0026 // KF
0027 #include <KAboutData>
0028 #include <KLocalizedString>
0029 
0030 // Qt
0031 #include <QCommandLineParser>
0032 #include <QDir>
0033 #include <QtDebug>
0034 
0035 using namespace Gwenview;
0036 
0037 int main(int argc, char **argv)
0038 {
0039     KLocalizedString::setApplicationDomain("thumbnailgen");
0040     QScopedPointer<KAboutData> aboutData(Gwenview::createAboutData(QStringLiteral("thumbnailgen"), /* component name */
0041                                                                    i18n("thumbnailgen") /* display name */
0042                                                                    ));
0043 
0044     QApplication app(argc, argv);
0045 
0046     QCommandLineParser parser;
0047     aboutData->setupCommandLine(&parser);
0048     parser.addHelpOption();
0049     parser.addVersionOption();
0050     parser.addPositionalArgument("image-dir", i18n("Image dir to open"));
0051     parser.addPositionalArgument("size", i18n("What size of thumbnails to generate. Can be either 'normal' or 'large'"));
0052     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("t") << QStringLiteral("thumbnail-dir"),
0053                                         i18n("Use <dir> instead of ~/.thumbnails to store thumbnails"),
0054                                         "thumbnail-dir"));
0055     parser.process(app);
0056     aboutData->processCommandLine(&parser);
0057 
0058     // Read cmdline options
0059     const QStringList args = parser.positionalArguments();
0060     if (args.count() != 2) {
0061         qFatal("Wrong number of arguments");
0062         return 1;
0063     }
0064     const QString imageDirName = args.first();
0065     ThumbnailGroup::Enum group = ThumbnailGroup::Normal;
0066     if (args.last() == "large") {
0067         group = ThumbnailGroup::Large;
0068     } else if (args.last() == "normal") {
0069         // group is already set to the right value
0070     } else {
0071         qFatal("Invalid thumbnail size: %s", qPrintable(args.last()));
0072     }
0073     QString thumbnailBaseDirName = parser.value(QStringLiteral("thumbnail-dir"));
0074 
0075     // Set up thumbnail base dir
0076     if (!thumbnailBaseDirName.isEmpty()) {
0077         const QDir dir = QDir(thumbnailBaseDirName);
0078         thumbnailBaseDirName = dir.absolutePath();
0079         if (!dir.exists()) {
0080             bool ok = QDir::root().mkpath(thumbnailBaseDirName);
0081             if (!ok) {
0082                 qFatal("Could not create %s", qPrintable(thumbnailBaseDirName));
0083                 return 1;
0084             }
0085         }
0086         if (!thumbnailBaseDirName.endsWith(QLatin1Char('/'))) {
0087             thumbnailBaseDirName += QLatin1Char('/');
0088         }
0089         ThumbnailProvider::setThumbnailBaseDir(thumbnailBaseDirName);
0090     }
0091 
0092     // List dir
0093     QDir dir(imageDirName);
0094     KFileItemList list;
0095     const auto entryList = dir.entryList();
0096     for (const QString &name : entryList) {
0097         const QUrl url = QUrl::fromLocalFile(dir.absoluteFilePath(name));
0098         KFileItem item(url);
0099         list << item;
0100     }
0101     qWarning() << "Generating thumbnails for" << list.count() << "files";
0102 
0103     // Start the job
0104     QElapsedTimer chrono;
0105     ThumbnailProvider job;
0106     job.setThumbnailGroup(group);
0107     job.appendItems(list);
0108 
0109     chrono.start();
0110 
0111     QEventLoop loop;
0112     QObject::connect(&job, SIGNAL(finished()), &loop, SLOT(quit()));
0113     loop.exec();
0114 
0115     qWarning() << "Time to generate thumbnails:" << chrono.restart();
0116 
0117     waitForDeferredDeletes();
0118     while (!ThumbnailProvider::isThumbnailWriterEmpty()) {
0119         QCoreApplication::processEvents();
0120     }
0121     qWarning() << "Time to save pending thumbnails:" << chrono.restart();
0122 
0123     return 0;
0124 }