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

List:       kde-commits
Subject:    [ktp-common-internals/mklapetek/SpaceBar] KTp/Declarative: [declarative] Split the observer out of t
From:       Martin Klapetek <mklapetek () kde ! org>
Date:       2016-03-17 22:25:50
Message-ID: E1aggMY-0005mw-CU () scm ! kde ! org
[Download RAW message or body]

Git commit 149195d01506f71b802f8fb8c65efc862b812f35 by Martin Klapetek.
Committed on 17/03/2016 at 22:22.
Pushed by mklapetek into branch 'mklapetek/SpaceBar'.

[declarative] Split the observer out of the MainLogModel

The reason for the split is that an Observer and a Handler cannot
be registered under the same client name if the Observer is not to
be autostarted and only monitor things once the app is executed.

So this is a tiny proxy class that gets registered as
SpaceBarObserverProxy and forwards all observerChannels calls to the
model which then merges them with the existing conversations

M  +38   -23   KTp/Declarative/mainlogmodel.cpp
M  +37   -10   KTp/Declarative/mainlogmodel.h

http://commits.kde.org/telepathy-common-internals/149195d01506f71b802f8fb8c65efc862b812f35


diff --git a/KTp/Declarative/mainlogmodel.cpp b/KTp/Declarative/mainlogmodel.cpp
index 2cce096..a8c4909 100644
--- a/KTp/Declarative/mainlogmodel.cpp
+++ b/KTp/Declarative/mainlogmodel.cpp
@@ -43,10 +43,42 @@ static inline Tp::ChannelClassSpecList channelClassList()
     return Tp::ChannelClassSpecList() << Tp::ChannelClassSpec::textChat();
 }
 
+ObserverProxy::ObserverProxy(MainLogModel *model)
+    : QObject(model),
+      Tp::AbstractClientObserver(channelClassList(), true),
+      m_model(model)
+{
+
+}
+
+void ObserverProxy::observeChannels(const Tp::MethodInvocationContextPtr<> &context,
+                                    const Tp::AccountPtr &account,
+                                    const Tp::ConnectionPtr &connection,
+                                    const QList<Tp::ChannelPtr> &channels,
+                                    const Tp::ChannelDispatchOperationPtr \
&dispatchOperation, +                                    const \
QList<Tp::ChannelRequestPtr> &requestsSatisfied, +                                    \
const Tp::AbstractClientObserver::ObserverInfo &observerInfo) +{
+    Q_UNUSED(context)
+    Q_UNUSED(connection)
+    Q_UNUSED(requestsSatisfied)
+    Q_UNUSED(observerInfo)
+
+    Q_FOREACH(const Tp::ChannelPtr &channel, channels) {
+        Tp::TextChannelPtr textChannel = Tp::TextChannelPtr::dynamicCast(channel);
+        if (textChannel) {
+            textChannel.data()->setProperty("dispatchOperation", \
QVariant::fromValue(dispatchOperation)); +            m_model->handleChannel(account, \
textChannel); +        }
+    }
+}
+
+// -----------------------------------------------------------------------
+
 MainLogModel::MainLogModel(QObject *parent)
     : QAbstractListModel(parent),
       Tp::AbstractClientHandler(channelClassList()),
-      Tp::AbstractClientObserver(channelClassList(), true)
+      m_observerProxy(new ObserverProxy(this))
 {
     const QString dbLocation = \
QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + \
QStringLiteral("/ktp-mobile-logger/");  
@@ -257,6 +289,11 @@ void MainLogModel::setAccountManager(const Tp::AccountManagerPtr \
&accountManager  processQueryResults(m_query);
 }
 
+QObject* MainLogModel::observerProxy() const
+{
+    return m_observerProxy;
+}
+
 void MainLogModel::handleChannels(const Tp::MethodInvocationContextPtr<> &context,
                                         const Tp::AccountPtr &account,
                                         const Tp::ConnectionPtr &connection,
@@ -316,28 +353,6 @@ void MainLogModel::handleChannels(const \
Tp::MethodInvocationContextPtr<> &contex  context->setFinished();
 }
 
-void MainLogModel::observeChannels(const Tp::MethodInvocationContextPtr<> &context,
-                                   const Tp::AccountPtr &account,
-                                   const Tp::ConnectionPtr &connection,
-                                   const QList<Tp::ChannelPtr> &channels,
-                                   const Tp::ChannelDispatchOperationPtr \
                &dispatchOperation,
-                                   const QList<Tp::ChannelRequestPtr> \
                &requestsSatisfied,
-                                   const Tp::AbstractClientObserver::ObserverInfo \
                &observerInfo)
-{
-    Q_UNUSED(context)
-    Q_UNUSED(connection)
-    Q_UNUSED(requestsSatisfied)
-    Q_UNUSED(observerInfo)
-
-    Q_FOREACH(const Tp::ChannelPtr &channel, channels) {
-        Tp::TextChannelPtr textChannel = Tp::TextChannelPtr::dynamicCast(channel);
-        if (textChannel) {
-            textChannel.data()->setProperty("dispatchOperation", \
                QVariant::fromValue(dispatchOperation));
-            handleChannel(account, textChannel);
-        }
-    }
-}
-
 bool MainLogModel::bypassApproval() const
 {
     return true;
diff --git a/KTp/Declarative/mainlogmodel.h b/KTp/Declarative/mainlogmodel.h
index a586e23..8d4cd06 100644
--- a/KTp/Declarative/mainlogmodel.h
+++ b/KTp/Declarative/mainlogmodel.h
@@ -23,14 +23,15 @@
 #include <QAbstractListModel>
 #include <QSqlQuery>
 
-#include <TelepathyQt/AbstractClientHandler>
 #include <TelepathyQt/AbstractClientObserver>
+#include <TelepathyQt/AbstractClientHandler>
 #include <TelepathyQt/ChannelDispatchOperation>
 
 #include <KTp/persistent-contact.h>
 #include <KTp/types.h>
 
 class Conversation;
+class MainLogModel; // Cause of ObserverProxy
 
 class LogItem {
 public:
@@ -41,7 +42,37 @@ public:
     Conversation *conversation;
 };
 
-class MainLogModel : public QAbstractListModel, public Tp::AbstractClientHandler, \
public Tp::AbstractClientObserver +/**
+ * The reason for this class is that an Observer and a Handler cannot
+ * be registered under the same client name if the Observer is not to
+ * be autostarted and only monitor things once the app is executed.
+ *
+ * So this is a tiny proxy class that gets registered as SpaceBarObserverProxy
+ * and forwards all observerChannels calls to the model which then merges
+ * them with the existing conversations
+ */
+class ObserverProxy : public QObject, public Tp::AbstractClientObserver
+{
+    Q_OBJECT
+
+public:
+    ObserverProxy(MainLogModel *model);
+
+    void observeChannels(const Tp::MethodInvocationContextPtr<> &context,
+                         const Tp::AccountPtr &account,
+                         const Tp::ConnectionPtr &connection,
+                         const QList<Tp::ChannelPtr> &channels,
+                         const Tp::ChannelDispatchOperationPtr &dispatchOperation,
+                         const QList<Tp::ChannelRequestPtr> &requestsSatisfied,
+                         const Tp::AbstractClientObserver::ObserverInfo \
&observerInfo); +
+private:
+    MainLogModel *m_model;
+};
+
+//-----------------------------------------------------------------------------
+
+class MainLogModel : public QAbstractListModel, public Tp::AbstractClientHandler
 {
     Q_OBJECT
 
@@ -72,6 +103,7 @@ public:
     Q_INVOKABLE void startChat(const QString &accountId, const QString &contactId);
     Q_INVOKABLE void setAccountManager(const Tp::AccountManagerPtr &accountManager);
     Q_INVOKABLE QVariant data(int index, QByteArray role) const;
+    Q_INVOKABLE QObject* observerProxy() const;
 
     void handleChannels(const Tp::MethodInvocationContextPtr<> &context,
                         const Tp::AccountPtr &account,
@@ -81,14 +113,6 @@ public:
                         const QDateTime &userActionTime,
                         const HandlerInfo &handlerInfo);
 
-    void observeChannels(const Tp::MethodInvocationContextPtr<> &context,
-                         const Tp::AccountPtr &account,
-                         const Tp::ConnectionPtr &connection,
-                         const QList<Tp::ChannelPtr> &channels,
-                         const Tp::ChannelDispatchOperationPtr &dispatchOperation,
-                         const QList<Tp::ChannelRequestPtr> &requestsSatisfied,
-                         const Tp::AbstractClientObserver::ObserverInfo \
                &observerInfo);
-
     bool bypassApproval() const;
 
 Q_SIGNALS:
@@ -107,6 +131,9 @@ private:
     QSqlQuery m_query;
     QSqlDatabase m_db;
     Tp::AccountManagerPtr m_accountManager;
+    ObserverProxy *m_observerProxy;
+
+    friend class ObserverProxy;
 };
 
 Q_DECLARE_METATYPE(Tp::ChannelDispatchOperationPtr)


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

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