[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [akonadiclient] src: Add delete command
From: Bhaskar Kandiyal <bkandiyal () gmail ! com>
Date: 2014-07-03 19:48:44
Message-ID: E1X2mzs-00007T-Ou () scm ! kde ! org
[Download RAW message or body]
Git commit a90d71e87c0b73fccab714d2399f56a455107aef by Bhaskar Kandiyal.
Committed on 03/07/2014 at 19:47.
Pushed by bkandiyal into branch 'master'.
Add delete command
REVIEW: 118770
M +1 -0 src/CMakeLists.txt
M +3 -0 src/commandfactory.cpp
A +210 -0 src/deletecommand.cpp [License: GPL (v2+)]
A +71 -0 src/deletecommand.h [License: GPL (v2+)]
http://commits.kde.org/akonadiclient/a90d71e87c0b73fccab714d2399f56a455107aef
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b32e7a8..ab27e3a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -25,6 +25,7 @@ set(akonadiclient_SRCS
copycommand.cpp
movecommand.cpp
expandcommand.cpp
+ deletecommand.cpp
main.cpp
errorreporter.cpp
renamecommand.cpp
diff --git a/src/commandfactory.cpp b/src/commandfactory.cpp
index e573edc..1b869ff 100644
--- a/src/commandfactory.cpp
+++ b/src/commandfactory.cpp
@@ -21,6 +21,7 @@
#include "addcommand.h"
#include "copycommand.h"
#include "createcommand.h"
+#include "deletecommand.h"
#include "editcommand.h"
#include "errorreporter.h"
#include "expandcommand.h"
@@ -93,6 +94,8 @@ void CommandFactory::registerCommands()
mCommands.insert( command->name(), command );
command = new UpdateCommand;
mCommands.insert( command->name(), command );
+ command = new DeleteCommand;
+ mCommands.insert( command->name(), command );
}
diff --git a/src/deletecommand.cpp b/src/deletecommand.cpp
new file mode 100644
index 0000000..718bb50
--- /dev/null
+++ b/src/deletecommand.cpp
@@ -0,0 +1,210 @@
+/*
+ * 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 "deletecommand.h"
+
+#include "collectionresolvejob.h"
+
+#include <akonadi/item.h>
+#include <akonadi/itemdeletejob.h>
+#include <akonadi/itemfetchjob.h>
+
+#include <akonadi/collectiondeletejob.h>
+#include <akonadi/private/collectionpathresolver_p.h>
+
+#include <kcmdlineargs.h>
+#include <klocalizedstring.h>
+
+#include <QUrl>
+#include <iostream>
+
+using namespace Akonadi;
+
+DeleteCommand::DeleteCommand(QObject *parent)
+ : AbstractCommand(parent),
+ mDeleteJob(0),
+ mResolveJob(0),
+ mDryRun(false),
+ mIsCollection(false),
+ mIsItem(false)
+{
+ mShortHelp = ki18nc("@info:shell", "Deletes a collection or an item").toString();
+}
+
+DeleteCommand::~DeleteCommand()
+{
+ delete mResolveJob;
+}
+
+void DeleteCommand::setupCommandOptions(KCmdLineOptions &options)
+{
+ AbstractCommand::setupCommandOptions(options);
+
+ options.add("[+option]", ki18nc("@info:shell", "Options for command"));
+ options.add("+collection|item", ki18nc("@info:shell", "The collection or item"));
+ options.add(":", ki18nc("@info:shell", "Options for command:"));
+ options.add("c").add("collection", ki18nc("@info:shell", "Assume that a collection is specified"));
+ options.add("i").add("item", ki18nc("@info:shell", "Assume that an item is specified"));
+ options.add("n").add("dryrun", ki18nc("@info:shell", "Run without making any actual changes"));
+}
+
+
+int DeleteCommand::initCommand(KCmdLineArgs *parsedArgs)
+{
+ if (parsedArgs->count() < 2) {
+ emitErrorSeeHelp(ki18nc("@info:shell", "Missing collection / item argument"));
+ return InvalidUsage;
+ }
+
+ mIsItem = parsedArgs->isSet("item");
+ mIsCollection = parsedArgs->isSet("collection");
+ mDryRun = parsedArgs->isSet("dryrun");
+
+ if (mIsItem && mIsCollection) {
+ emit error(i18nc("@info:shell", "Cannot specify as both an item and collection"));
+ return InvalidUsage;
+ }
+
+ mEntityArg = parsedArgs->arg(1);
+ mResolveJob = new CollectionResolveJob(mEntityArg, this);
+ if (!mResolveJob->hasUsableInput()) {
+ emit error(mResolveJob->errorString());
+ delete mResolveJob;
+ mResolveJob = 0;
+ return InvalidUsage;
+ }
+
+ return NoError;
+}
+
+void DeleteCommand::start()
+{
+ Q_ASSERT(mResolveJob != 0);
+
+ if (mIsItem) {
+ fetchItems();
+ } else {
+ connect(mResolveJob, SIGNAL(result(KJob *)), SLOT(onBaseFetched(KJob *)));
+ mResolveJob->start();
+ }
+}
+
+void DeleteCommand::onBaseFetched(KJob *job)
+{
+ Q_ASSERT(job == mResolveJob);
+
+ if (job->error() != 0) {
+ if (job->error() == CollectionPathResolver::Unknown) {
+ if (!mIsCollection) {
+ fetchItems();
+ return;
+ }
+ }
+
+ emit error(job->errorString());
+ emit finished(RuntimeError);
+ return;
+ }
+
+ if (mResolveJob->collection() == Collection::root()) {
+ emit error(i18nc("@info:shell", "Cannot delete the root collection"));
+ emit finished(RuntimeError);
+ return;
+ }
+
+ if (!mDryRun) {
+ mDeleteJob = new CollectionDeleteJob(mResolveJob->collection());
+ connect(mDeleteJob, SIGNAL(result(KJob *)), SLOT(onCollectionDeleted(KJob *)));
+ } else {
+ onCollectionDeleted(job);
+ }
+}
+
+void DeleteCommand::onCollectionDeleted(KJob *job)
+{
+ if (job->error() != 0) {
+ emit error(job->errorString());
+ emit finished(RuntimeError);
+ return;
+ }
+
+ std::cout << i18n("Collection deleted successfully").toLocal8Bit().constData() << std::endl;
+ emit finished(NoError);
+}
+
+void DeleteCommand::fetchItems()
+{
+ Item item;
+ // See if user input is a valid integer as an item ID
+ bool ok;
+ int id = mEntityArg.toInt(&ok);
+ if (ok) {
+ item = Item(id); // conversion succeeded
+ } else {
+ // Otherwise check if we have an Akonadi URL
+ const KUrl url = QUrl::fromUserInput(mEntityArg);
+ if (url.isValid() && url.scheme() == QLatin1String("akonadi")) {
+ item = Item::fromUrl(url);
+ } else {
+ emit error(i18nc("@info:shell", "Invalid item/collection syntax"));
+ emit finished(RuntimeError);
+ return;
+ }
+ }
+
+ if (!mDryRun) {
+ ItemDeleteJob *deleteJob = new ItemDeleteJob(item, this);
+ connect(deleteJob, SIGNAL(result(KJob *)), SLOT(onItemsDeleted(KJob *)));
+ } else {
+ ItemFetchJob *fetchJob = new ItemFetchJob(item, this);
+ connect(fetchJob, SIGNAL(result(KJob *)), SLOT(onItemsFetched(KJob *)));
+ }
+}
+
+void DeleteCommand::onItemsFetched(KJob *job)
+{
+ if (job->error() != 0) {
+ emit error(job->errorString());
+ emit finished(RuntimeError);
+ return;
+ }
+
+ ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
+ Q_ASSERT(fetchJob!=0);
+ Item::List items = fetchJob->items();
+ if (items.count() < 0) {
+ emit error(i18nc("@info:shell", "Cannot find '%1' as a collection or item", mEntityArg));
+ emit finished(RuntimeError);
+ return;
+ }
+
+ emit finished(NoError);
+}
+
+void DeleteCommand::onItemsDeleted(KJob *job)
+{
+ if (job->error() != 0) {
+ emit error(job->errorString());
+ emit finished(RuntimeError);
+ return;
+ }
+
+ std::cout << i18n("Item deleted successfully").toLocal8Bit().constData() << std::endl;
+ emit finished(NoError);
+}
diff --git a/src/deletecommand.h b/src/deletecommand.h
new file mode 100644
index 0000000..0d4a123
--- /dev/null
+++ b/src/deletecommand.h
@@ -0,0 +1,71 @@
+/*
+ * 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 DELETECOMMAND_H
+#define DELETECOMMAND_H
+
+#include <abstractcommand.h>
+
+namespace Akonadi
+{
+class Collection;
+class Item;
+class CollectionDeleteJob;
+};
+
+class CollectionResolveJob;
+class KJob;
+
+class DeleteCommand : public AbstractCommand
+{
+ Q_OBJECT
+
+public:
+ explicit DeleteCommand(QObject *parent = 0);
+ ~DeleteCommand();
+ QString name() const {
+ return (QLatin1String("delete"));
+ };
+
+public Q_SLOTS:
+ void start();
+
+protected:
+ int initCommand(KCmdLineArgs *parsedArgs);
+ void setupCommandOptions(KCmdLineOptions &options);
+
+private:
+ Akonadi::CollectionDeleteJob *mDeleteJob;
+ CollectionResolveJob *mResolveJob;
+ bool mDryRun;
+ bool mIsCollection;
+ bool mIsItem;
+ QString mEntityArg;
+
+private:
+ void fetchItems();
+
+private Q_SLOTS:
+ void onBaseFetched(KJob *job);
+ void onCollectionDeleted(KJob *job);
+ void onItemsDeleted(KJob *job);
+ void onItemsFetched(KJob *job);
+};
+
+#endif // DELETECOMMAND_H
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic