[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [akonadiclient] src: Add update command
From:       Bhaskar Kandiyal <bkandiyal () gmail ! com>
Date:       2014-06-30 18:51:02
Message-ID: E1X1gfO-0004wg-AG () scm ! kde ! org
[Download RAW message or body]

Git commit fd6a910096fcef00ed9d47c6dab6a48b7e51a5e6 by Bhaskar Kandiyal.
Committed on 29/06/2014 at 18:36.
Pushed by bkandiyal into branch 'master'.

Add update command

REVIEW: 118773

M  +1    -0    src/CMakeLists.txt
M  +3    -0    src/commandfactory.cpp
A  +150  -0    src/updatecommand.cpp     [License: GPL (v2+)]
A  +57   -0    src/updatecommand.h     [License: GPL (v2+)]

http://commits.kde.org/akonadiclient/fd6a910096fcef00ed9d47c6dab6a48b7e51a5e6

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 99d3624..b32e7a8 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -28,6 +28,7 @@ set(akonadiclient_SRCS
     main.cpp
     errorreporter.cpp
     renamecommand.cpp
+    updatecommand.cpp
 )
 
 kde4_add_executable(akonadiclient ${akonadiclient_SRCS})
diff --git a/src/commandfactory.cpp b/src/commandfactory.cpp
index 9639e09..e573edc 100644
--- a/src/commandfactory.cpp
+++ b/src/commandfactory.cpp
@@ -29,6 +29,7 @@
 #include "movecommand.h"
 #include "renamecommand.h"
 #include "showcommand.h"
+#include "updatecommand.h"
 
 #include <KCmdLineArgs>
 
@@ -90,6 +91,8 @@ void CommandFactory::registerCommands()
   mCommands.insert( command->name(), command );
   command = new RenameCommand;
   mCommands.insert( command->name(), command );
+  command = new UpdateCommand;
+  mCommands.insert( command->name(), command );
 }
 
 
diff --git a/src/updatecommand.cpp b/src/updatecommand.cpp
new file mode 100644
index 0000000..05ecaf2
--- /dev/null
+++ b/src/updatecommand.cpp
@@ -0,0 +1,150 @@
+/*
+ * 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 "updatecommand.h"
+
+#include <akonadi/itemfetchjob.h>
+#include <akonadi/itemfetchscope.h>
+#include <akonadi/itemmodifyjob.h>
+
+#include <kcmdlineargs.h>
+#include <kurl.h>
+#include <qfile.h>
+
+#include <iostream>
+
+using namespace Akonadi;
+
+UpdateCommand::UpdateCommand(QObject *parent)
+    : AbstractCommand(parent),
+      mDryRun(false),
+      mFile(0)
+{
+    mShortHelp = ki18nc("@info:shell", "Updates an item's payload with the file \
specified").toString(); +}
+
+UpdateCommand::~UpdateCommand()
+{
+    delete mFile;
+}
+
+void UpdateCommand::setupCommandOptions(KCmdLineOptions &options)
+{
+    options.add("+item", ki18nc("@info:shell", "The item to update"));
+    options.add("+file", ki18nc("@info:shell", "File to update the item from"));
+    options.add(":", ki18nc("@info:shell", "Options for command"));
+    options.add("n").add("dryrun", ki18nc("@info:shell", "Run command without making \
any actual changes")); +}
+
+int UpdateCommand::initCommand(KCmdLineArgs *parsedArgs)
+{
+    if (parsedArgs->count() < 2) {
+        emitErrorSeeHelp(ki18nc("@info:shell", "No item specified"));
+        return InvalidUsage;
+    }
+
+    if (parsedArgs->count() == 2) {
+        emitErrorSeeHelp(ki18nc("@info:shell", "No file specified"));
+        return InvalidUsage;
+    }
+
+    mItemArg = parsedArgs->arg(1);
+    mFileArg = parsedArgs->arg(2);
+    mDryRun = parsedArgs->isSet("dryrun");
+
+    return NoError;
+}
+
+void UpdateCommand::start()
+{
+    Item item;
+    bool ok;
+    int id = mItemArg.toInt(&ok);
+    if (ok) {
+        item = Item(id);
+    } else {
+        const KUrl url = QUrl::fromUserInput(mItemArg);
+        if (url.isValid() && url.scheme() == QLatin1String("akonadi")) {
+            item = Item::fromUrl(url);
+        } else {
+            emit error(i18nc("@info:shell", "Invalid item syntax '%1'", mItemArg));
+            emit finished(RuntimeError);
+            return;
+        }
+    }
+
+    if (!QFile::exists(mFileArg)) {
+        emit error(i18nc("@info:shell", "'%1' no such file or directory", \
mFileArg)); +        emit finished(RuntimeError);
+        return;
+    }
+
+    mFile = new QFile(mFileArg);
+    if (!mFile->open(QIODevice::ReadOnly)) {
+        emit error(i18nc("@info:shell", "Error opening file '%1' for reading", \
mFileArg)); +        emit finished(RuntimeError);
+        return;
+    }
+
+    ItemFetchJob *job = new ItemFetchJob(item, this);
+    job->fetchScope().setFetchModificationTime(false);
+    job->fetchScope().fetchAllAttributes(false);
+    connect(job, SIGNAL(result(KJob *)), SLOT(onItemFetched(KJob *)));
+}
+
+void UpdateCommand::onItemFetched(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() < 1) {
+        emit error(i18nc("@info:shell", "No result returned for item '%1'", \
job->property("arg").toString())); +        emit finished(RuntimeError);
+        return;
+    }
+
+    if (!mDryRun) {
+        Item item = items.first();
+        QByteArray data = mFile->readAll();
+        item.setPayloadFromData(data);
+        ItemModifyJob *modifyJob = new ItemModifyJob(item, this);
+        connect(modifyJob, SIGNAL(result(KJob *)), SLOT(onItemUpdated(KJob *)));
+    } else {
+        onItemUpdated(job);
+    }
+}
+
+void UpdateCommand::onItemUpdated(KJob *job)
+{
+    if (job->error() != 0) {
+        emit error(job->errorString());
+        emit finished(RuntimeError);
+        return;
+    }
+
+    std::cout << i18nc("@info:shell", "Item updated \
successfully").toLocal8Bit().data() << std::endl; +    emit finished(NoError);
+}
diff --git a/src/updatecommand.h b/src/updatecommand.h
new file mode 100644
index 0000000..649ea3b
--- /dev/null
+++ b/src/updatecommand.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 UPDATECOMMAND_H
+#define UPDATECOMMAND_H
+
+#include <abstractcommand.h>
+
+class KJob;
+class QFile;
+
+class UpdateCommand : public AbstractCommand
+{
+    Q_OBJECT
+
+public:
+    explicit UpdateCommand(QObject *parent = 0);
+    ~UpdateCommand();
+    QString name() const {
+        return (QLatin1String("update"));
+    }
+
+public Q_SLOTS:
+    void start();
+
+protected:
+    int initCommand(KCmdLineArgs *parsedArgs);
+    void setupCommandOptions(KCmdLineOptions &options);
+
+private:
+    bool mDryRun;
+    QFile *mFile;
+    QString mFileArg;
+    QString mItemArg;
+
+private Q_SLOTS:
+    void onItemFetched(KJob *job);
+    void onItemUpdated(KJob *job);
+};
+
+#endif // UPDATECOMMAND_H


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic