File indexing completed on 2024-05-12 05:12:41

0001 /*
0002  * Copyright (C) 2014  Bhaskar Kandiyal <bkandiyal@gmail.com>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License along
0015  * with this program; if not, write to the Free Software Foundation, Inc.,
0016  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0017  *
0018  */
0019 
0020 #include "exportcommand.h"
0021 
0022 #include <Akonadi/XmlWriteJob>
0023 
0024 #include <klocalizedstring.h>
0025 
0026 #include "commandfactory.h"
0027 #include "collectionresolvejob.h"
0028 
0029 using namespace Akonadi;
0030 
0031 DEFINE_COMMAND("export", ExportCommand, I18N_NOOP("Export a collection to an XML file"));
0032 
0033 ExportCommand::ExportCommand(QObject *parent)
0034     : AbstractCommand(parent)
0035 {
0036 }
0037 
0038 void ExportCommand::setupCommandOptions(QCommandLineParser *parser)
0039 {
0040     addOptionsOption(parser);
0041     addDryRunOption(parser);
0042 
0043     parser->addPositionalArgument("collection", i18nc("@info:shell", "The collection to export: an ID, path or Akonadi URL"));
0044     parser->addPositionalArgument("file", i18nc("@info:shell", "The file to export to"));
0045 }
0046 
0047 int ExportCommand::initCommand(QCommandLineParser *parser)
0048 {
0049     const QStringList args = parser->positionalArguments();
0050     if (!checkArgCount(args, 1, i18nc("@info:shell", "No collection specified"))) return InvalidUsage;
0051     if (!checkArgCount(args, 2, i18nc("@info:shell", "No export file specified"))) return InvalidUsage;
0052 
0053     if (!getCommonOptions(parser)) return InvalidUsage;
0054 
0055     QString collectionArg = args.at(0);
0056     mFileArg = args.at(1);
0057 
0058     if (!getResolveJob(collectionArg)) return InvalidUsage;
0059 
0060     return NoError;
0061 }
0062 
0063 void ExportCommand::start()
0064 {
0065     connect(resolveJob(), &KJob::result, this, &ExportCommand::onCollectionFetched);
0066     resolveJob()->start();
0067 }
0068 
0069 void ExportCommand::onCollectionFetched(KJob *job)
0070 {
0071     if (!checkJobResult(job)) return;
0072 
0073     if (!isDryRun()) {
0074         XmlWriteJob *writeJob = new XmlWriteJob(resolveJob()->collection(), mFileArg);
0075         connect(writeJob, &KJob::result, this, &ExportCommand::onWriteFinished);
0076     } else {
0077         emit finished(NoError);
0078     }
0079 }
0080 
0081 void ExportCommand::onWriteFinished(KJob *job)
0082 {
0083     if (!checkJobResult(job)) return;
0084     emit finished(NoError);
0085 }