[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [akonadiclient] src: Add rename command
From: Bhaskar Kandiyal <bkandiyal () gmail ! com>
Date: 2014-06-30 18:51:01
Message-ID: E1X1gfN-0004wg-OZ () scm ! kde ! org
[Download RAW message or body]
Git commit 7534a19343c6945c3e33b869541b069c10550108 by Bhaskar Kandiyal.
Committed on 29/06/2014 at 18:33.
Pushed by bkandiyal into branch 'master'.
Add rename command
REVIEW: 118772
M +1 -0 src/CMakeLists.txt
M +3 -0 src/commandfactory.cpp
A +122 -0 src/renamecommand.cpp [License: GPL (v2+)]
A +57 -0 src/renamecommand.h [License: GPL (v2+)]
http://commits.kde.org/akonadiclient/7534a19343c6945c3e33b869541b069c10550108
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index c18206b..99d3624 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -27,6 +27,7 @@ set(akonadiclient_SRCS
expandcommand.cpp
main.cpp
errorreporter.cpp
+ renamecommand.cpp
)
kde4_add_executable(akonadiclient ${akonadiclient_SRCS})
diff --git a/src/commandfactory.cpp b/src/commandfactory.cpp
index b6d8485..9639e09 100644
--- a/src/commandfactory.cpp
+++ b/src/commandfactory.cpp
@@ -27,6 +27,7 @@
#include "infocommand.h"
#include "listcommand.h"
#include "movecommand.h"
+#include "renamecommand.h"
#include "showcommand.h"
#include <KCmdLineArgs>
@@ -87,6 +88,8 @@ void CommandFactory::registerCommands()
mCommands.insert( command->name(), command );
command = new EditCommand;
mCommands.insert( command->name(), command );
+ command = new RenameCommand;
+ mCommands.insert( command->name(), command );
}
diff --git a/src/renamecommand.cpp b/src/renamecommand.cpp
new file mode 100644
index 0000000..8a75dc4
--- /dev/null
+++ b/src/renamecommand.cpp
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2014 Bhaskar Kandiyal <bkandiyal@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include "collectionresolvejob.h"
+#include "renamecommand.h"
+
+#include <akonadi/collectionmodifyjob.h>
+#include <kcmdlineargs.h>
+#include <klocalizedstring.h>
+
+#include <iostream>
+
+using namespace Akonadi;
+
+RenameCommand::RenameCommand(QObject *parent)
+ : AbstractCommand(parent),
+ mDryRun(false),
+ mResolveJob(0)
+{
+ mShortHelp = ki18nc("@info:shell", "Rename a collection").toString();
+}
+
+RenameCommand::~RenameCommand()
+{
+ delete mResolveJob;
+}
+
+void RenameCommand::setupCommandOptions(KCmdLineOptions &options)
+{
+ AbstractCommand::setupCommandOptions(options);
+
+ options.add("+collection", ki18nc("@info:shell", "The collection to raname"));
+ options.add("+name", ki18nc("@info:shell", "New name for collection"));
+ options.add(":", ki18nc("@info:shell", "Options"));
+ options.add("n").add("dryrun", ki18nc("@info:shell", "Run command without making \
any actual changes")); +}
+
+int RenameCommand::initCommand(KCmdLineArgs *parsedArgs)
+{
+ if (parsedArgs->count() < 2) {
+ emitErrorSeeHelp(ki18nc("@info:shell", "No collection specified"));
+ return InvalidUsage;
+ }
+
+ if (parsedArgs->count() < 3) {
+ emitErrorSeeHelp(ki18nc("@info:shell", "Please specify a new name for the \
collection")); + return InvalidUsage;
+ }
+
+ mDryRun = parsedArgs->isSet("dryrun");
+ QString oldCollectionNameArg = parsedArgs->arg(1);
+ mNewCollectionNameArg = parsedArgs->arg(2);
+
+ mResolveJob = new CollectionResolveJob(oldCollectionNameArg, this);
+ if (!mResolveJob->hasUsableInput()) {
+ emit error(ki18nc("@info:shell",
+ "Invalid collection argument '%1', '%2'" )
+ .subs(oldCollectionNameArg)
+ .subs(mResolveJob->errorString()).toString());
+ delete mResolveJob;
+ mResolveJob = 0;
+
+ return InvalidUsage;
+ }
+
+ return NoError;
+}
+
+void RenameCommand::start()
+{
+ Q_ASSERT(mResolveJob != 0);
+ connect(mResolveJob, SIGNAL(result(KJob*)), this, \
SLOT(onCollectionFetched(KJob*))); + mResolveJob->start();
+}
+
+void RenameCommand::onCollectionFetched(KJob *job)
+{
+ if (job->error() != 0) {
+ emit error(job->errorString());
+ emit finished(RuntimeError);
+ return;
+ }
+
+ Q_ASSERT(job == mResolveJob && mResolveJob->collection().isValid());
+
+ if (!mDryRun) {
+ Collection collection = mResolveJob->collection();
+ collection.setName(mNewCollectionNameArg);
+
+ CollectionModifyJob *modifyJob = new CollectionModifyJob(collection);
+ connect(modifyJob, SIGNAL(result(KJob*)), \
SLOT(onCollectionModified(KJob*))); + } else {
+ onCollectionModified(job);
+ }
+}
+
+void RenameCommand::onCollectionModified(KJob *job)
+{
+ if (job->error() != 0) {
+ emit error(job->errorString());
+ emit finished(RuntimeError);
+ } else {
+ std::cout << i18nc("@info:shell", "Collection renamed \
successfully").toLocal8Bit().data() << std::endl; + emit finished(NoError);
+ }
+}
diff --git a/src/renamecommand.h b/src/renamecommand.h
new file mode 100644
index 0000000..8df9578
--- /dev/null
+++ b/src/renamecommand.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2014 Bhaskar Kandiyal <bkandiyal@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#ifndef RENAMECOMMAND_H
+#define RENAMECOMMAND_H
+
+#include <abstractcommand.h>
+
+class CollectionResolveJob;
+class KJob;
+
+class RenameCommand : public AbstractCommand
+{
+ Q_OBJECT
+
+public:
+ explicit RenameCommand(QObject *parent = 0);
+ ~RenameCommand();
+
+ QString name() const {
+ return QLatin1String("rename");
+ }
+
+public Q_SLOTS:
+ void start();
+
+protected:
+ int initCommand(KCmdLineArgs *parsedArgs);
+ void setupCommandOptions(KCmdLineOptions &options);
+
+private:
+ bool mDryRun;
+ CollectionResolveJob *mResolveJob;
+ QString mNewCollectionNameArg;
+
+private Q_SLOTS:
+ void onCollectionFetched(KJob *job);
+ void onCollectionModified(KJob *job);
+};
+
+#endif // RENAMECOMMAND_H
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic