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

List:       kde-commits
Subject:    [sink/develop] /: Share special purpose preprocessor implementation.
From:       Christian Mollekopf <chrigi_1 () fastmail ! fm>
Date:       2016-06-30 19:49:32
Message-ID: E1bIhxs-0005c3-PY () code ! kde ! org
[Download RAW message or body]

Git commit b29a17465d1e52bd7dd5c57f08e7af53e915eee6 by Christian Mollekopf.
Committed on 25/06/2016 at 21:34.
Pushed by cmollekopf into branch 'develop'.

Share special purpose preprocessor implementation.

M  +1    -0    common/CMakeLists.txt
A  +88   -0    common/specialpurposepreprocessor.cpp     [License: UNKNOWN]  *
A  +43   -0    common/specialpurposepreprocessor.h     [License: GPL (v2+)]
M  +5    -83   examples/imapresource/imapresource.cpp

The files marked with a * at the end have a non valid license. Please read: \
http://techbase.kde.org/Policies/Licensing_Policy and use the headers which are \
listed at that page.


http://commits.kde.org/akonadi-next/b29a17465d1e52bd7dd5c57f08e7af53e915eee6

diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt
index 3083a2e..5eb15ba 100644
--- a/common/CMakeLists.txt
+++ b/common/CMakeLists.txt
@@ -74,6 +74,7 @@ set(command_SRCS
     sourcewriteback.cpp
     entityreader.cpp
     mailpreprocessor.cpp
+    specialpurposepreprocessor.cpp
     ${storage_SRCS})
 
 add_library(${PROJECT_NAME} SHARED ${command_SRCS})
diff --git a/common/specialpurposepreprocessor.cpp \
b/common/specialpurposepreprocessor.cpp new file mode 100644
index 0000000..2892105
--- /dev/null
+++ b/common/specialpurposepreprocessor.cpp
@@ -0,0 +1,88 @@
+#include "specialpurposepreprocessor.h"
+#include "entityreader.h"
+#include "query.h"
+#include "applicationdomaintype.h"
+
+using namespace Sink;
+
+static QHash<QByteArray, QString> specialPurposeFolders()
+{
+    QHash<QByteArray, QString> hash;
+        //FIXME localize
+    hash.insert("drafts", "Drafts");
+    hash.insert("trash", "Trash");
+    return hash;
+}
+
+static QHash<QString, QByteArray> specialPurposeNames()
+{
+    QHash<QString, QByteArray> hash;
+    for (const auto &value : specialPurposeFolders().values()) {
+        hash.insert(value.toLower(), specialPurposeFolders().key(value));
+    }
+    return hash;
+}
+
+//specialpurpose, name
+static QHash<QByteArray, QString> sSpecialPurposeFolders = specialPurposeFolders();
+//Lowercase-name, specialpurpose
+static QHash<QString, QByteArray> sSpecialPurposeNames = specialPurposeNames();
+
+namespace SpecialPurpose {
+bool isSpecialPurposeFolderName(const QString &name)
+{
+    return sSpecialPurposeNames.contains(name.toLower());
+}
+
+QByteArray getSpecialPurposeType(const QString &name)
+{
+    return sSpecialPurposeNames.value(name.toLower());
+}
+}
+
+SpecialPurposeProcessor::SpecialPurposeProcessor(const QByteArray &resourceType, \
const QByteArray &resourceInstanceIdentifier) : mResourceType(resourceType), \
mResourceInstanceIdentifier(resourceInstanceIdentifier) {} +
+QByteArray SpecialPurposeProcessor::ensureFolder(Sink::Storage::Transaction \
&transaction, const QByteArray &specialPurpose) +{
+    if (!mSpecialPurposeFolders.contains(specialPurpose)) {
+        //Try to find an existing drafts folder
+        Sink::EntityReader<ApplicationDomain::Folder> reader(mResourceType, \
mResourceInstanceIdentifier, transaction); +        \
reader.query(Sink::Query().filter<ApplicationDomain::Folder::SpecialPurpose>(Query::Comparator(specialPurpose, \
Query::Comparator::Contains)), +            [this, specialPurpose](const \
ApplicationDomain::Folder &f) -> bool{ +                \
mSpecialPurposeFolders.insert(specialPurpose, f.identifier()); +                \
return false; +            });
+        if (!mSpecialPurposeFolders.contains(specialPurpose)) {
+            Trace() << "Failed to find a drafts folder, creating a new one";
+            auto folder = \
ApplicationDomain::Folder::create(mResourceInstanceIdentifier); +            \
folder.setSpecialPurpose(QByteArrayList() << specialPurpose); +            \
folder.setName(sSpecialPurposeFolders.value(specialPurpose)); +            \
folder.setIcon("folder"); +            //This processes the pipeline synchronously
+            createEntity(folder);
+            mSpecialPurposeFolders.insert(specialPurpose, folder.identifier());
+        }
+    }
+    return mSpecialPurposeFolders.value(specialPurpose);
+}
+
+void SpecialPurposeProcessor::moveToFolder(Sink::ApplicationDomain::BufferAdaptor \
&newEntity, Sink::Storage::Transaction &transaction) +{
+    if (newEntity.getProperty("trash").toBool()) {
+        newEntity.setProperty("folder", ensureFolder(transaction, "trash"));
+        return;
+    }
+    if (newEntity.getProperty("draft").toBool()) {
+        newEntity.setProperty("folder", ensureFolder(transaction, "drafts"));
+    }
+}
+
+void SpecialPurposeProcessor::newEntity(const QByteArray &uid, qint64 revision, \
Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction \
&transaction) +{
+    moveToFolder(newEntity, transaction);
+}
+
+void SpecialPurposeProcessor::modifiedEntity(const QByteArray &uid, qint64 revision, \
const Sink::ApplicationDomain::BufferAdaptor &oldEntity, \
Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction \
&transaction) +{
+    moveToFolder(newEntity, transaction);
+}
diff --git a/common/specialpurposepreprocessor.h \
b/common/specialpurposepreprocessor.h new file mode 100644
index 0000000..a33701b
--- /dev/null
+++ b/common/specialpurposepreprocessor.h
@@ -0,0 +1,43 @@
+/*
+ *   Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm>
+ *
+ *   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 "sink_export.h"
+
+#include "pipeline.h"
+
+namespace SpecialPurpose {
+    bool SINK_EXPORT isSpecialPurposeFolderName(const QString &name);
+    QByteArray SINK_EXPORT getSpecialPurposeType(const QString &name);
+}
+
+class SINK_EXPORT SpecialPurposeProcessor : public Sink::Preprocessor
+{
+public:
+    SpecialPurposeProcessor(const QByteArray &resourceType, const QByteArray \
&resourceInstanceIdentifier); +
+    QByteArray ensureFolder(Sink::Storage::Transaction &transaction, const \
QByteArray &specialPurpose); +
+    void moveToFolder(Sink::ApplicationDomain::BufferAdaptor &newEntity, \
Sink::Storage::Transaction &transaction); +
+    void newEntity(const QByteArray &uid, qint64 revision, \
Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction \
&transaction) Q_DECL_OVERRIDE; +    void modifiedEntity(const QByteArray &uid, qint64 \
revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, \
Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction \
&transaction) Q_DECL_OVERRIDE; +
+    QHash<QByteArray, QByteArray> mSpecialPurposeFolders;
+    QByteArray mResourceType;
+    QByteArray mResourceInstanceIdentifier;
+};
diff --git a/examples/imapresource/imapresource.cpp \
b/examples/imapresource/imapresource.cpp index 18747bf..605dbc2 100644
--- a/examples/imapresource/imapresource.cpp
+++ b/examples/imapresource/imapresource.cpp
@@ -48,6 +48,7 @@
 #include "imapserverproxy.h"
 #include "entityreader.h"
 #include "mailpreprocessor.h"
+#include "specialpurposepreprocessor.h"
 
 //This is the resources entity type, and not the domain type
 #define ENTITY_TYPE_MAIL "mail"
@@ -59,85 +60,6 @@
 using namespace Imap;
 using namespace Sink;
 
-static QHash<QByteArray, QString> specialPurposeFolders()
-{
-    QHash<QByteArray, QString> hash;
-        //FIXME localize
-    hash.insert("drafts", "Drafts");
-    hash.insert("trash", "Trash");
-    return hash;
-}
-
-static QHash<QString, QByteArray> specialPurposeNames()
-{
-    QHash<QString, QByteArray> hash;
-    for (const auto &value : specialPurposeFolders().values()) {
-        hash.insert(value.toLower(), specialPurposeFolders().key(value));
-    }
-    return hash;
-}
-
-//specialpurpose, name
-static QHash<QByteArray, QString> sSpecialPurposeFolders = specialPurposeFolders();
-//Lowercase-name, specialpurpose
-static QHash<QString, QByteArray> sSpecialPurposeNames = specialPurposeNames();
-
-class SpecialPurposeProcessor : public Sink::Preprocessor
-{
-public:
-    SpecialPurposeProcessor(const QByteArray &resourceType, const QByteArray \
&resourceInstanceIdentifier) : mResourceType(resourceType), \
                mResourceInstanceIdentifier(resourceInstanceIdentifier) {}
-
-    QByteArray ensureFolder(Sink::Storage::Transaction &transaction, const \
                QByteArray &specialPurpose)
-    {
-        if (!mSpecialPurposeFolders.contains(specialPurpose)) {
-            //Try to find an existing drafts folder
-            Sink::EntityReader<ApplicationDomain::Folder> reader(mResourceType, \
                mResourceInstanceIdentifier, transaction);
-            reader.query(Sink::Query().filter<ApplicationDomain::Folder::SpecialPurpose>(Query::Comparator("drafts", \
                Query::Comparator::Contains)),
-                [this, specialPurpose](const ApplicationDomain::Folder &f) -> bool{
-                    mSpecialPurposeFolders.insert(specialPurpose, f.identifier());
-                    return false;
-                });
-            if (!mSpecialPurposeFolders.contains(specialPurpose)) {
-                Trace() << "Failed to find a drafts folder, creating a new one";
-                auto folder = \
                ApplicationDomain::Folder::create(mResourceInstanceIdentifier);
-                folder.setSpecialPurpose(QByteArrayList() << specialPurpose);
-                folder.setName(sSpecialPurposeFolders.value(specialPurpose));
-                folder.setIcon("folder");
-                //This processes the pipeline synchronously
-                createEntity(folder);
-                mSpecialPurposeFolders.insert(specialPurpose, folder.identifier());
-            }
-        }
-        return mSpecialPurposeFolders.value(specialPurpose);
-    }
-
-    void moveToFolder(Sink::ApplicationDomain::BufferAdaptor &newEntity, \
                Sink::Storage::Transaction &transaction)
-    {
-        if (newEntity.getProperty("trash").toBool()) {
-            newEntity.setProperty("folder", ensureFolder(transaction, "trash"));
-            return;
-        }
-        if (newEntity.getProperty("draft").toBool()) {
-            newEntity.setProperty("folder", ensureFolder(transaction, "drafts"));
-        }
-    }
-
-    void newEntity(const QByteArray &uid, qint64 revision, \
Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction \
                &transaction) Q_DECL_OVERRIDE
-    {
-        moveToFolder(newEntity, transaction);
-    }
-
-    void modifiedEntity(const QByteArray &uid, qint64 revision, const \
Sink::ApplicationDomain::BufferAdaptor &oldEntity, \
Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction \
                &transaction) Q_DECL_OVERRIDE
-    {
-        moveToFolder(newEntity, transaction);
-    }
-
-    QHash<QByteArray, QByteArray> mSpecialPurposeFolders;
-    QByteArray mResourceType;
-    QByteArray mResourceInstanceIdentifier;
-};
-
-
 static qint64 uidFromMailRid(const QByteArray &remoteId)
 {
     auto ridParts = remoteId.split(':');
@@ -180,8 +102,8 @@ public:
         folder.setProperty(ApplicationDomain::Folder::Name::name, folderName);
         folder.setProperty(ApplicationDomain::Folder::Icon::name, icon);
         QHash<QByteArray, Query::Comparator> mergeCriteria;
-        if (sSpecialPurposeNames.contains(folderName.toLower())) {
-            auto type = sSpecialPurposeNames.value(folderName.toLower());
+        if (SpecialPurpose::isSpecialPurposeFolderName(folderName)) {
+            auto type = SpecialPurpose::getSpecialPurposeType(folderName);
             folder.setProperty(ApplicationDomain::Folder::SpecialPurpose::name, \
                QVariant::fromValue(QByteArrayList() << type));
             mergeCriteria.insert(ApplicationDomain::Folder::SpecialPurpose::name, \
Query::Comparator(type, Query::Comparator::Contains));  }
@@ -481,8 +403,8 @@ public:
                 auto mergeJob = imap->login(mUser, mPassword)
                     .then<void>(imap->fetchFolders([=](const QVector<Imap::Folder> \
&folders) {  for (const auto &f : folders) {
-                            if \
                (sSpecialPurposeNames.contains(f.pathParts.last().toLower())) {
-                                \
specialPurposeFolders->insert(sSpecialPurposeNames.value(f.pathParts.last().toLower()), \
f.path); +                            if \
(SpecialPurpose::isSpecialPurposeFolderName(f.pathParts.last())) { +                  \
specialPurposeFolders->insert(SpecialPurpose::getSpecialPurposeType(f.pathParts.last()), \
f.path);  };
                         }
                     }))


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

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