File indexing completed on 2024-06-23 05:07:23

0001 /*
0002     SPDX-FileCopyrightText: 2006 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2007 Robert Zwerus <arzie@dds.nl>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "itemdumper.h"
0009 #include "collectionpathresolver.h"
0010 
0011 #include "item.h"
0012 
0013 #include <QApplication>
0014 #include <QDebug>
0015 #include <QFile>
0016 
0017 #include <KLocalizedString>
0018 
0019 #include <KAboutData>
0020 #include <QCommandLineOption>
0021 #include <QCommandLineParser>
0022 
0023 #include "itemcreatejob.h"
0024 #include "transactionjobs.h"
0025 
0026 #define GLOBAL_TRANSACTION 1
0027 
0028 using namespace Akonadi;
0029 
0030 ItemDumper::ItemDumper(const QString &path, const QString &filename, const QString &mimetype, int count)
0031     : mJobCount(0)
0032 {
0033     auto resolver = new CollectionPathResolver(path, this);
0034     Q_ASSERT(resolver->exec());
0035     const Collection collection = Collection(resolver->collection());
0036 
0037     QFile f(filename);
0038     Q_ASSERT(f.open(QIODevice::ReadOnly));
0039     QByteArray data = f.readAll();
0040     f.close();
0041     Item item;
0042     item.setMimeType(mimetype);
0043     item.setPayloadFromData(data);
0044     mTime.start();
0045 #ifdef GLOBAL_TRANSACTION
0046     auto begin = new TransactionBeginJob(this);
0047     connect(begin, &TransactionBeginJob::result, this, &ItemDumper::done);
0048     ++mJobCount;
0049 #endif
0050     for (int i = 0; i < count; ++i) {
0051         ++mJobCount;
0052         auto job = new ItemCreateJob(item, collection, this);
0053         connect(job, &ItemCreateJob::result, this, &ItemDumper::done);
0054     }
0055 #ifdef GLOBAL_TRANSACTION
0056     auto commit = new TransactionCommitJob(this);
0057     connect(commit, &TransactionCommitJob::result, this, &ItemDumper::done);
0058     ++mJobCount;
0059 #endif
0060 }
0061 
0062 void ItemDumper::done(KJob *job)
0063 {
0064     --mJobCount;
0065     if (job->error()) {
0066         qWarning() << "Error while creating item: " << job->errorString();
0067     }
0068     if (mJobCount <= 0) {
0069         qDebug() << "Time:" << mTime.elapsed() << "ms";
0070         qApp->quit();
0071     }
0072 }
0073 
0074 int main(int argc, char **argv)
0075 {
0076     QApplication app(argc, argv);
0077     KAboutData aboutData(QStringLiteral("test"), i18n("Test Application"), QStringLiteral("1.0"));
0078 
0079     QCommandLineParser parser;
0080     KAboutData::setApplicationData(aboutData);
0081     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("path"), i18n("IMAP destination path"), QStringLiteral("argument")));
0082     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("mimetype"),
0083                                         i18n("Source mimetype"),
0084                                         QStringLiteral("argument"),
0085                                         QStringLiteral("application/octet-stream")));
0086     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("file"), i18n("Source file"), QStringLiteral("argument")));
0087     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("count"),
0088                                         i18n("Number of times this file is added"),
0089                                         QStringLiteral("argument"),
0090                                         QStringLiteral("1")));
0091 
0092     aboutData.setupCommandLine(&parser);
0093     parser.process(app);
0094     aboutData.processCommandLine(&parser);
0095 
0096     QString path = parser.value(QStringLiteral("path"));
0097     QString mimetype = parser.value(QStringLiteral("mimetype"));
0098     QString file = parser.value(QStringLiteral("file"));
0099     int count = qMax(1, parser.value(QStringLiteral("count")).toInt());
0100     ItemDumper d(path, file, mimetype, count);
0101     return app.exec();
0102 }
0103 
0104 #include "moc_itemdumper.cpp"