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

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 "renamecommand.h"
0021 
0022 #include <Akonadi/CollectionModifyJob>
0023 
0024 #include <klocalizedstring.h>
0025 
0026 #include <iostream>
0027 
0028 #include "collectionresolvejob.h"
0029 #include "commandfactory.h"
0030 
0031 using namespace Akonadi;
0032 
0033 DEFINE_COMMAND("rename", RenameCommand, I18N_NOOP("Rename a collection"));
0034 
0035 RenameCommand::RenameCommand(QObject *parent)
0036     : AbstractCommand(parent)
0037 {
0038 }
0039 
0040 void RenameCommand::setupCommandOptions(QCommandLineParser *parser)
0041 {
0042     addOptionsOption(parser);
0043     addDryRunOption(parser);
0044 
0045     parser->addPositionalArgument("collection", i18nc("@info:shell", "The collection to rename"));
0046     parser->addPositionalArgument("name", i18nc("@info:shell", "New name for collection"));
0047 }
0048 
0049 int RenameCommand::initCommand(QCommandLineParser *parser)
0050 {
0051     const QStringList args = parser->positionalArguments();
0052     if (!checkArgCount(args, 1, i18nc("@info:shell", "Missing collection argument"))) return InvalidUsage;
0053     if (!checkArgCount(args, 2, i18nc("@info:shell", "New name not specified"))) return InvalidUsage;
0054 
0055     if (!getCommonOptions(parser)) return InvalidUsage;
0056 
0057     QString oldCollectionNameArg = args.first();
0058     mNewCollectionNameArg = args.at(1);
0059 
0060     if (!getResolveJob(oldCollectionNameArg)) return InvalidUsage;
0061 
0062     return NoError;
0063 }
0064 
0065 void RenameCommand::start()
0066 {
0067     connect(resolveJob(), &KJob::result, this, &RenameCommand::onCollectionFetched);
0068     resolveJob()->start();
0069 }
0070 
0071 void RenameCommand::onCollectionFetched(KJob *job)
0072 {
0073     if (!checkJobResult(job)) return;
0074     CollectionResolveJob *res = resolveJob();
0075     Q_ASSERT(job == res && res->collection().isValid());
0076 
0077     if (!isDryRun()) {
0078         Collection collection = res->collection();
0079         collection.setName(mNewCollectionNameArg);
0080 
0081         CollectionModifyJob *modifyJob = new CollectionModifyJob(collection);
0082         connect(modifyJob, &KJob::result, this, &RenameCommand::onCollectionModified);
0083     } else {
0084         onCollectionModified(job);
0085     }
0086 }
0087 
0088 void RenameCommand::onCollectionModified(KJob *job)
0089 {
0090     if (!checkJobResult(job)) return;
0091     std::cout << qPrintable(i18nc("@info:shell", "Collection renamed successfully")) << std::endl;
0092     emit finished(NoError);
0093 }