[SCM] ktp-common-internals packaging branch, master, updated. debian/15.12.1-2-1839-gf0635e9

Maximiliano Curia maxy at moszumanska.debian.org
Mon May 9 09:06:12 UTC 2016


Gitweb-URL: http://git.debian.org/?p=pkg-kde/applications/ktp-common-internals.git;a=commitdiff;h=8379844

The following commit has been merged in the master branch:
commit 837984454c9d2b87424dd49ff560101be78762f5
Author: Aleix Pol <aleixpol at kde.org>
Date:   Tue Jan 15 01:25:56 2013 +0100

    Make it possible to have some pinned contacts
    
    Provide the tools to make it possible to have a list of favorite contacts
    to chat with in the plasmoid for more fluid conversations.
    
    REVIEW: 108401
---
 KTp/CMakeLists.txt                                 |   1 +
 KTp/Declarative/CMakeLists.txt                     |   2 +
 KTp/Declarative/contact-pin.cpp                    |  79 ++++++
 KTp/Declarative/contact-pin.h                      |  58 +++++
 KTp/Declarative/conversation-target.cpp            |  16 +-
 KTp/Declarative/conversation-target.h              |   9 +-
 KTp/Declarative/conversation.cpp                   |   2 +-
 KTp/Declarative/conversation.h                     |   6 +-
 KTp/Declarative/conversations-model.cpp            |   7 +
 KTp/Declarative/conversations-model.h              |   3 +
 KTp/Declarative/messages-model.cpp                 |  10 +-
 KTp/Declarative/messages-model.h                   |   8 +-
 KTp/Declarative/pinned-contacts-model.cpp          | 290 +++++++++++++++++++++
 KTp/Declarative/pinned-contacts-model.h            |  79 ++++++
 KTp/Declarative/qml-plugins.cpp                    |  18 +-
 KTp/Declarative/telepathy-text-observer.cpp        |  10 +-
 KTp/Declarative/telepathy-text-observer.h          |   8 +-
 KTp/Models/contacts-model.h                        |   4 +-
 .../hide-window-component.cpp => ktp-metatypes.h}  |  27 +-
 19 files changed, 591 insertions(+), 46 deletions(-)

diff --git a/KTp/CMakeLists.txt b/KTp/CMakeLists.txt
index ac75572..7cae74b 100644
--- a/KTp/CMakeLists.txt
+++ b/KTp/CMakeLists.txt
@@ -45,6 +45,7 @@ set (ktp_common_internals_private_HDRS
      pending-wallet.h
      wallet-interface.h
      wallet-utils.h
+     ktp-metatypes.h
      ktp-export.h
 )
 
diff --git a/KTp/Declarative/CMakeLists.txt b/KTp/Declarative/CMakeLists.txt
index 17fea5a..f42252d 100644
--- a/KTp/Declarative/CMakeLists.txt
+++ b/KTp/Declarative/CMakeLists.txt
@@ -12,6 +12,8 @@ set (ktp_qml_plugin_SRCS
     hide-window-component.cpp
     messages-model.cpp
     telepathy-text-observer.cpp
+    pinned-contacts-model.cpp
+    contact-pin.cpp
 
     qml-plugins.cpp
 )
diff --git a/KTp/Declarative/contact-pin.cpp b/KTp/Declarative/contact-pin.cpp
new file mode 100644
index 0000000..d0da388
--- /dev/null
+++ b/KTp/Declarative/contact-pin.cpp
@@ -0,0 +1,79 @@
+/*
+    Copyright (C) 2012 Aleix Pol <aleixpol at kde.org>
+    
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library 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
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "contact-pin.h"
+#include <TelepathyQt/Account>
+#include <TelepathyQt/Contact>
+#include "pinned-contacts-model.h"
+
+ContactPin::ContactPin(QObject* parent)
+    : QObject(parent)
+    , m_model(0)
+{}
+
+Tp::AccountPtr ContactPin::account() const
+{
+    return m_account;
+}
+
+Tp::ContactPtr ContactPin::contact() const
+{
+    return m_contact;
+}
+
+PinnedContactsModel* ContactPin::model() const
+{
+    return m_model;
+}
+
+bool ContactPin::isPinned() const
+{
+    bool ret = false;
+    if(m_model && m_account && m_contact) {
+        QModelIndex idx = m_model->indexForContact(m_account, m_contact);
+        ret = idx.isValid();
+    }
+    return ret;
+}
+
+void ContactPin::toggle()
+{
+    Q_ASSERT(m_model && m_account && m_contact);
+    m_model->setPinning(m_account, m_contact, !isPinned());
+    Q_EMIT pinnedChanged();
+}
+
+
+void ContactPin::setAccount(const Tp::AccountPtr& v)
+{
+    Q_ASSERT(v);
+    m_account = v;
+    Q_EMIT pinnedChanged();
+}
+
+void ContactPin::setContact(const Tp::ContactPtr& v)
+{
+    m_contact = v;
+    Q_EMIT pinnedChanged();
+}
+
+void ContactPin::setModel(PinnedContactsModel* m)
+{
+    m_model = m;
+    Q_EMIT pinnedChanged();
+}
diff --git a/KTp/Declarative/contact-pin.h b/KTp/Declarative/contact-pin.h
new file mode 100644
index 0000000..65aeb32
--- /dev/null
+++ b/KTp/Declarative/contact-pin.h
@@ -0,0 +1,58 @@
+/*
+    Copyright (C) 2012 Aleix Pol <aleixpol at kde.org>
+    
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library 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
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef CONTACTPIN_H
+#define CONTACTPIN_H
+
+#include <QObject>
+#include "ktp-metatypes.h"
+
+class PinnedContactsModel;
+
+class ContactPin : public QObject
+{
+    Q_OBJECT
+    Q_PROPERTY(Tp::ContactPtr contact READ contact WRITE setContact)
+    Q_PROPERTY(Tp::AccountPtr account READ account WRITE setAccount)
+    Q_PROPERTY(PinnedContactsModel* model READ model WRITE setModel)
+    Q_PROPERTY(bool pinned READ isPinned NOTIFY pinnedChanged)
+
+  public:
+    explicit ContactPin(QObject* parent = 0);
+
+    Tp::ContactPtr contact() const;
+    Tp::AccountPtr account() const;
+    PinnedContactsModel* model() const;
+    bool isPinned() const;
+
+    void setContact(const Tp::ContactPtr& v);
+    void setAccount(const Tp::AccountPtr& v);
+    void setModel(PinnedContactsModel* m);
+
+    Q_SCRIPTABLE void toggle();
+
+  Q_SIGNALS:
+    void pinnedChanged();
+
+  private:
+    PinnedContactsModel* m_model;
+    Tp::ContactPtr m_contact;
+    Tp::AccountPtr m_account;
+};
+
+#endif // CONTACTPIN_H
diff --git a/KTp/Declarative/conversation-target.cpp b/KTp/Declarative/conversation-target.cpp
index 99d2176..224b9f8 100644
--- a/KTp/Declarative/conversation-target.cpp
+++ b/KTp/Declarative/conversation-target.cpp
@@ -18,6 +18,7 @@
 
 #include "conversation-target.h"
 #include <TelepathyQt/AvatarData>
+#include <TelepathyQt/Contact>
 #include <KDebug>
 #include <KTp/presence.h>
 
@@ -26,9 +27,10 @@ class  ConversationTarget::ConversationTargetPrivate
   public:
     Tp::ContactPtr contact;
     KIcon avatar;
+    Tp::AccountPtr account;
 };
 
-ConversationTarget::ConversationTarget(const Tp::ContactPtr &contact, QObject *parent) :
+ConversationTarget::ConversationTarget(const Tp::AccountPtr& account, const Tp::ContactPtr& contact, QObject* parent) :
     QObject(parent),
     d(new ConversationTargetPrivate)
 {
@@ -39,6 +41,7 @@ ConversationTarget::ConversationTarget(const Tp::ContactPtr &contact, QObject *p
     }
 
     d->contact = contact;
+    d->account = account;
     updateAvatar();
 }
 
@@ -90,14 +93,14 @@ QString ConversationTarget::presenceIconName() const
     if (d->contact) {
        return KTp::Presence(d->contact->presence()).iconName();
     } else {
-       return QString();                                                                    
-    }                                                                                        
+       return QString();
+    }
 }
 
 void ConversationTarget::onPresenceChanged(const Tp::Presence&)
 {
     Q_EMIT presenceIconChanged(presenceIcon());
-    Q_EMIT presenceIconNameChanged(presenceIconName());                                      
+    Q_EMIT presenceIconNameChanged(presenceIconName());
 }
 
 void ConversationTarget::onAvatarDataChanged(const Tp::AvatarData&)
@@ -125,6 +128,11 @@ Tp::ContactPtr ConversationTarget::contact() const
     return d->contact;
 }
 
+Tp::AccountPtr ConversationTarget::account() const
+{
+    return d->account;
+}
+
 ConversationTarget::~ConversationTarget()
 {
     delete d;
diff --git a/KTp/Declarative/conversation-target.h b/KTp/Declarative/conversation-target.h
index cce2138..9086978 100644
--- a/KTp/Declarative/conversation-target.h
+++ b/KTp/Declarative/conversation-target.h
@@ -23,7 +23,7 @@
 #include <QObject>
 #include <QIcon>
 
-#include <TelepathyQt/Contact>
+#include "ktp-metatypes.h"
 
 class ConversationTarget : public QObject
 {
@@ -33,10 +33,12 @@ class ConversationTarget : public QObject
     Q_PROPERTY(QString nick READ nick NOTIFY nickChanged);
     Q_PROPERTY(QIcon presenceIcon READ presenceIcon NOTIFY presenceIconChanged);
     Q_PROPERTY(QString presenceIconName READ presenceIconName NOTIFY presenceIconNameChanged)
-    Q_PROPERTY(QString id READ id)
+    Q_PROPERTY(QString id READ id CONSTANT)
+    Q_PROPERTY(Tp::ContactPtr contact READ contact CONSTANT)
+    Q_PROPERTY(Tp::AccountPtr account READ account CONSTANT)
 
   public:
-    explicit ConversationTarget(const Tp::ContactPtr &contact, QObject *parent = 0);
+    explicit ConversationTarget(const Tp::AccountPtr &account, const Tp::ContactPtr &contact, QObject *parent = 0);
     virtual ~ConversationTarget();
 
     QIcon   avatar() const;
@@ -46,6 +48,7 @@ class ConversationTarget : public QObject
     QString presenceIconName() const;
 
     Tp::ContactPtr contact() const;
+    Tp::AccountPtr account() const;
 
   Q_SIGNALS:
     void avatarChanged(QIcon avatar);
diff --git a/KTp/Declarative/conversation.cpp b/KTp/Declarative/conversation.cpp
index afd58e3..ce27230 100644
--- a/KTp/Declarative/conversation.cpp
+++ b/KTp/Declarative/conversation.cpp
@@ -49,7 +49,7 @@ Conversation::Conversation(const Tp::TextChannelPtr& channel,
     d->messages = new MessagesModel(this);
     d->messages->setTextChannel(channel);
 
-    d->target = new ConversationTarget(channel->targetContact(), this);
+    d->target = new ConversationTarget(account, channel->targetContact(), this);
 
     d->valid = channel->isValid();
     connect(channel.data(), SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
diff --git a/KTp/Declarative/conversation.h b/KTp/Declarative/conversation.h
index 4b277eb..f31ab29 100644
--- a/KTp/Declarative/conversation.h
+++ b/KTp/Declarative/conversation.h
@@ -36,9 +36,9 @@ class Conversation : public QObject
 {
     Q_OBJECT
 
-    Q_PROPERTY(QObject* target READ target CONSTANT);
-    Q_PROPERTY(QObject* messages READ messages CONSTANT);
-    Q_PROPERTY(bool valid READ isValid NOTIFY validityChanged);
+    Q_PROPERTY(ConversationTarget* target READ target CONSTANT)
+    Q_PROPERTY(MessagesModel* messages READ messages CONSTANT)
+    Q_PROPERTY(bool valid READ isValid NOTIFY validityChanged)
 
   public:
     Conversation(const Tp::TextChannelPtr &channel, const Tp::AccountPtr &account, QObject *parent = 0);
diff --git a/KTp/Declarative/conversations-model.cpp b/KTp/Declarative/conversations-model.cpp
index fe564ac..a8a3ab8 100644
--- a/KTp/Declarative/conversations-model.cpp
+++ b/KTp/Declarative/conversations-model.cpp
@@ -140,6 +140,13 @@ bool ConversationsModel::bypassApproval() const
     return true;
 }
 
+void ConversationsModel::startChat(const Tp::AccountPtr& account, const Tp::ContactPtr& contact)
+{
+    Q_ASSERT(account);
+    account->ensureTextChat(contact, QDateTime::currentDateTime(),
+                            QLatin1String("org.freedesktop.Telepathy.Client.KDE.TextUi.ConversationWatcher"));
+}
+
 void ConversationsModel::handleValidityChange(bool valid)
 {
     if(!valid) {
diff --git a/KTp/Declarative/conversations-model.h b/KTp/Declarative/conversations-model.h
index c0d0128..11a3c26 100644
--- a/KTp/Declarative/conversations-model.h
+++ b/KTp/Declarative/conversations-model.h
@@ -50,6 +50,9 @@ class ConversationsModel : public QAbstractListModel, public Tp::AbstractClientH
                         const HandlerInfo &handlerInfo);
     bool bypassApproval() const;
 
+  public Q_SLOTS:
+    void startChat(const Tp::AccountPtr& account, const Tp::ContactPtr& contact);
+
   private:
     class ConversationsModelPrivate;
     ConversationsModelPrivate *d;
diff --git a/KTp/Declarative/messages-model.cpp b/KTp/Declarative/messages-model.cpp
index 0553f0b..011694d 100644
--- a/KTp/Declarative/messages-model.cpp
+++ b/KTp/Declarative/messages-model.cpp
@@ -299,13 +299,7 @@ MessagesModel::~MessagesModel()
     delete d;
 }
 
-void MessagesModel::printallmessages()
+bool MessagesModel::shouldStartOpened() const
 {
-    Q_FOREACH(const MessageItem &msg, d->messages) {
-        kDebug() << msg.text;
-    }
+    return d->textChannel->isRequested();
 }
-
-
-
-#include "moc_messages-model.cpp"
diff --git a/KTp/Declarative/messages-model.h b/KTp/Declarative/messages-model.h
index 4ad2ff8..ef5a960 100644
--- a/KTp/Declarative/messages-model.h
+++ b/KTp/Declarative/messages-model.h
@@ -29,8 +29,9 @@ class MessagesModel : public QAbstractListModel, public Queueable
 {
     Q_OBJECT
     Q_ENUMS(MessageType)
-    Q_PROPERTY(bool visibleToUser READ isVisibleToUser WRITE setVisibleToUser NOTIFY visibleToUserChanged);
-    Q_PROPERTY(int unreadCount READ unreadCount NOTIFY unreadCountChanged);
+    Q_PROPERTY(bool visibleToUser READ isVisibleToUser WRITE setVisibleToUser NOTIFY visibleToUserChanged)
+    Q_PROPERTY(int unreadCount READ unreadCount NOTIFY unreadCountChanged)
+    Q_PROPERTY(bool shouldStartOpened READ shouldStartOpened CONSTANT)
 
   public:
     MessagesModel(QObject *parent = 0);
@@ -62,8 +63,7 @@ class MessagesModel : public QAbstractListModel, public Queueable
     int  unreadCount() const;
     void acknowledgeAllMessages();
 
-    //debug function. will do whatever I feel like at the time ;-)
-    Q_INVOKABLE void printallmessages();
+    bool shouldStartOpened() const;
 
   Q_SIGNALS:
     void visibleToUserChanged(bool visible);
diff --git a/KTp/Declarative/pinned-contacts-model.cpp b/KTp/Declarative/pinned-contacts-model.cpp
new file mode 100644
index 0000000..db87587
--- /dev/null
+++ b/KTp/Declarative/pinned-contacts-model.cpp
@@ -0,0 +1,290 @@
+/*
+    Copyright (C) 2012 Aleix Pol <aleixpol at kde.org>
+    
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library 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
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "pinned-contacts-model.h"
+#include "conversations-model.h"
+#include "conversation.h"
+#include <TelepathyQt/Types>
+#include <TelepathyQt/Contact>
+#include <TelepathyQt/AvatarData>
+#include <TelepathyQt/Presence>
+#include <TelepathyQt/Account>
+#include <TelepathyQt/AccountManager>
+#include <TelepathyQt/PendingReady>
+#include <TelepathyQt/ContactManager>
+#include <TelepathyQt/PendingContacts>
+#include <KIcon>
+#include <KGlobal>
+#include <KComponentData>
+#include <KConfigGroup>
+#include <KStandardDirs>
+#include <KDebug>
+#include <KTp/presence.h>
+
+struct Pin {
+    Tp::ContactPtr contact;
+    Tp::AccountPtr account;
+};
+
+class PinnedContactsModelPrivate {
+public:
+    QVector<Pin> m_pins;
+    Tp::AccountManagerPtr accountManager;
+    ConversationsModel* convesations;
+
+    QStringList pinsToString() const {
+        QStringList ret;
+        Q_FOREACH(const Pin& p, m_pins) {
+            ret += p.account->uniqueIdentifier();
+            ret += p.contact->id();
+        }
+        return ret;
+    }
+};
+
+static Tp::AccountPtr findAccountByUniqueId(Tp::AccountManagerPtr manager, const QString& id)
+{
+    QList<Tp::AccountPtr> accounts = manager->allAccounts();
+    Q_FOREACH(const Tp::AccountPtr account, accounts) {
+        if (account->uniqueIdentifier()==id)
+            return account;
+    }
+    Q_ASSERT(false && "account not found");
+    return Tp::AccountPtr();
+}
+
+PinnedContactsModel::PinnedContactsModel(QObject* parent)
+    : QAbstractListModel(parent)
+    , d(new PinnedContactsModelPrivate)
+{
+    QHash<int, QByteArray> roles = roleNames();
+    roles[PresenceIconRole] = "presenceIcon";
+    roles[AvailabilityRole] = "available";
+    roles[ContactRole] = "contact";
+    roles[AccountRole] = "account";
+    roles[AlreadyChattingRole] = "alreadyChatting";
+    setRoleNames(roles);
+
+    connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), SIGNAL(countChanged()));
+    connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SIGNAL(countChanged()));
+}
+
+PinnedContactsModel::~PinnedContactsModel()
+{
+    delete d;
+}
+
+QStringList PinnedContactsModel::state() const
+{
+    return d->pinsToString();
+}
+
+void PinnedContactsModel::setState(const QStringList& pins)
+{
+    Q_ASSERT(pins.size()%2==0 && "the state should be a pair number of account id and contact name");
+    if(!d->accountManager->isReady()) {
+        Tp::PendingReady* r = d->accountManager->becomeReady();
+        r->setProperty("newState", pins);
+        connect(r, SIGNAL(finished(Tp::PendingOperation*)), SLOT(initializeState(Tp::PendingOperation*)));
+    } else {
+        kDebug() << "loading pinned...." << pins;
+        for (int i=0; i<pins.count(); i+=2) {
+            Tp::AccountPtr account = findAccountByUniqueId(d->accountManager, pins[i]);
+            if (account->connection()) {
+                Tp::PendingContacts* pending = account->connection()->contactManager()->contactsForIdentifiers(QStringList(pins[i+1]));
+                pending->setProperty("account", qVariantFromValue<Tp::AccountPtr>(account));
+                connect(pending, SIGNAL(finished(Tp::PendingOperation*)), SLOT(pinPendingContacts(Tp::PendingOperation*)));
+            }
+        }
+    }
+}
+
+void PinnedContactsModel::pinPendingContacts(Tp::PendingOperation* j)
+{
+    if (j->isValid()) {
+        Tp::PendingContacts* job = qobject_cast<Tp::PendingContacts*>(j);
+        Tp::AccountPtr account = job->property("account").value<Tp::AccountPtr>();
+        Tp::ContactPtr contact = job->contacts().first();
+        setPinning(account, contact, true);
+    } else
+        kDebug() << "error:" << j->errorName() << j->errorMessage();
+}
+
+QModelIndex PinnedContactsModel::indexForContact(Tp::AccountPtr account, Tp::ContactPtr contact) const
+{
+    int i = 0;
+    Q_FOREACH(const Pin& p, d->m_pins) {
+        if (p.account->uniqueIdentifier()==account->uniqueIdentifier() && p.contact->id()==contact->id()) {
+            break;
+        }
+        i++;
+    }
+    if (i<d->m_pins.count())
+        return index(i);
+    else
+        return QModelIndex();
+}
+
+void PinnedContactsModel::setPinning(const Tp::AccountPtr& account, const Tp::ContactPtr& contact, bool newState)
+{
+    QModelIndex idx = indexForContact(account, contact);
+    bool found = idx.isValid();
+    if (newState && !found) {
+        Pin p;
+        p.account = account;
+        p.contact = contact;
+        appendContact(p);
+    } else if (!newState && found) {
+        removeRow(idx.row());
+    }
+}
+
+QVariant PinnedContactsModel::data(const QModelIndex& index, int role) const
+{
+    if (index.isValid()) {
+        const Pin& p = d->m_pins[index.row()];
+        switch(role) {
+            case Qt::DisplayRole:
+                return p.contact->alias();
+            case PresenceIconRole:
+                return KTp::Presence(p.contact->presence()).icon();
+            case AvailabilityRole:
+                return p.contact->presence().type()!=Tp::ConnectionPresenceTypeOffline
+                    && p.contact->presence().type()!=Tp::ConnectionPresenceTypeError
+                    && p.contact->presence().type()!=Tp::ConnectionPresenceTypeUnset
+                    && p.contact->presence().type()!=Tp::ConnectionPresenceTypeUnknown;
+            case ContactRole:
+                return QVariant::fromValue<Tp::ContactPtr>(p.contact);
+            case AccountRole:
+                return QVariant::fromValue<Tp::AccountPtr>(p.account);
+            case AlreadyChattingRole: {
+                bool found = false;
+                for(int i=0; !found && i<d->convesations->rowCount(); i++) {
+                    QModelIndex idx = d->convesations->index(i, 0);
+                    Tp::ContactPtr contact = idx.data(ConversationsModel::ConversationRole).value<Conversation*>()->target()->contact();
+                    found |= contact->id() == p.contact->id();
+                }
+                return found;
+            }
+            case Qt::DecorationRole: {
+                KIcon icon;
+                if (p.contact) {
+                    icon = KIcon(p.contact->avatarData().fileName);
+                }
+                if (icon.isNull()) {
+                    icon = KIcon(QLatin1String("im-user"));
+                }
+                return icon;
+            }
+        }
+    }
+    return QVariant();
+}
+
+int PinnedContactsModel::rowCount(const QModelIndex& parent) const
+{
+    if (parent.isValid()) {
+        return 0;
+    }
+    return d->m_pins.count();
+}
+
+bool PinnedContactsModel::removeRows(int row, int count, const QModelIndex& parent)
+{
+    if (parent.isValid() || (row+count)>d->m_pins.count()) {
+        return false;
+    }
+    beginRemoveRows(parent, row, row+count-1);
+    d->m_pins.remove(row, count);
+    endRemoveRows();
+    return true;
+}
+
+void PinnedContactsModel::appendContact(const Pin& p)
+{
+    int s = d->m_pins.size();
+    beginInsertRows(QModelIndex(), s, s);
+    d->m_pins += p;
+    endInsertRows();
+    
+    connect(p.contact.data(),
+            SIGNAL(avatarDataChanged(Tp::AvatarData)),
+            SLOT(contactDataChanged()));
+    connect(p.contact.data(),
+            SIGNAL(aliasChanged(QString)),
+            SLOT(contactDataChanged()));
+    connect(p.contact.data(),
+            SIGNAL(presenceChanged(Tp::Presence)),
+            SLOT(contactDataChanged()));
+}
+
+void PinnedContactsModel::contactDataChanged()
+{
+    Tp::Contact* c = qobject_cast<Tp::Contact*>(sender());
+    int i=0;
+    Q_FOREACH(const Pin& p, d->m_pins) {
+        if (p.contact==c) {
+            QModelIndex idx = index(i);
+            Q_EMIT dataChanged(idx, idx);
+            return;
+        }
+        i++;
+    }
+}
+
+void PinnedContactsModel::setConversationsModel(ConversationsModel* model)
+{
+    beginResetModel();
+    d->convesations = model;
+    connect(d->convesations, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), SLOT(conversationsStateChanged(QModelIndex, int, int)));
+    connect(d->convesations, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(conversationsStateChanged(QModelIndex, int, int)));
+    endResetModel();
+}
+
+void PinnedContactsModel::conversationsStateChanged(const QModelIndex& parent, int start, int end)
+{
+    for(int i=start; i<=end; i++) {
+        QModelIndex idx = d->convesations->index(i, 0, parent);
+        Tp::ContactPtr contact = idx.data(ConversationsModel::ConversationRole).value<Conversation*>()->target()->contact();
+        Q_FOREACH(const Pin& p, d->m_pins) {
+            if (p.contact->id()==contact->id())
+                QMetaObject::invokeMethod(this, "dataChanged", Qt::QueuedConnection, Q_ARG(QModelIndex, idx), Q_ARG(QModelIndex, idx));
+        }
+    }
+}
+
+ConversationsModel* PinnedContactsModel::conversationsModel() const
+{
+    return d->convesations;
+}
+
+Tp::AccountManagerPtr PinnedContactsModel::accountManager() const
+{
+    return d->accountManager;
+}
+
+void PinnedContactsModel::setAccountManager(const Tp::AccountManagerPtr& accounts)
+{
+    accounts->becomeReady();
+    d->accountManager = accounts;
+}
+
+void PinnedContactsModel::initializeState(Tp::PendingOperation* op)
+{
+    setState(op->property("newState").toStringList());
+}
diff --git a/KTp/Declarative/pinned-contacts-model.h b/KTp/Declarative/pinned-contacts-model.h
new file mode 100644
index 0000000..42a9e91
--- /dev/null
+++ b/KTp/Declarative/pinned-contacts-model.h
@@ -0,0 +1,79 @@
+/*
+    Copyright (C) 2012 Aleix Pol <aleixpol at kde.org>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library 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
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef PINNEDCONTACTSMODEL_H
+#define PINNEDCONTACTSMODEL_H
+
+#include <QModelIndex>
+#include <QVector>
+#include "ktp-metatypes.h"
+
+struct Pin;
+class ConversationsModel;
+class PinnedContactsModelPrivate;
+
+class PinnedContactsModel : public QAbstractListModel
+{
+    Q_OBJECT
+    Q_PROPERTY(ConversationsModel* conversations READ conversationsModel WRITE setConversationsModel)
+    Q_PROPERTY(Tp::AccountManagerPtr accountManager READ accountManager WRITE setAccountManager)
+    Q_PROPERTY(QStringList state READ state WRITE setState)
+    Q_PROPERTY(int count READ rowCount NOTIFY countChanged);
+  public:
+    explicit PinnedContactsModel(QObject* parent = 0);
+    virtual ~PinnedContactsModel();
+
+    enum role {
+        PresenceIconRole = Qt::UserRole+1,
+        AvailabilityRole,
+        ContactRole,
+        AccountRole,
+        AlreadyChattingRole
+    };
+
+    virtual QVariant data(const QModelIndex& index, int role) const;
+    virtual int rowCount(const QModelIndex& parent=QModelIndex()) const;
+    virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex());
+
+    Q_SLOT void setPinning(const Tp::AccountPtr& account, const Tp::ContactPtr& contact, bool newState);
+    QModelIndex indexForContact(Tp::AccountPtr account, Tp::ContactPtr contact) const;
+
+    ConversationsModel* conversationsModel() const;
+    void setConversationsModel(ConversationsModel* model);
+
+    Tp::AccountManagerPtr accountManager() const;
+    void setAccountManager(const Tp::AccountManagerPtr& accounts);
+
+    QStringList state() const;
+    void setState(const QStringList& s);
+
+  private Q_SLOTS:
+    void contactDataChanged();
+    void pinPendingContacts(Tp::PendingOperation* job);
+    void initializeState(Tp::PendingOperation* op);
+    void conversationsStateChanged(const QModelIndex& parent, int start, int end);
+
+  Q_SIGNALS:
+    void countChanged();
+
+  private:
+    void appendContact(const Pin& p);
+    PinnedContactsModelPrivate * const d;
+};
+
+#endif // PINNEDCONTACTSMODEL_H
diff --git a/KTp/Declarative/qml-plugins.cpp b/KTp/Declarative/qml-plugins.cpp
index d1dfbea..2184e4f 100644
--- a/KTp/Declarative/qml-plugins.cpp
+++ b/KTp/Declarative/qml-plugins.cpp
@@ -1,5 +1,7 @@
 /*
+    Copyright (C) 2011  Lasath Fernando <kde at lasath.org>
     Copyright (C) 2013  Dan Vrátil <dvratil at redhat.com>
+    Copyright (C) 2013  Aleix Pol Gonzalez <aleixpol at kde.org>
 
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
@@ -28,6 +30,8 @@
 #include "hide-window-component.h"
 #include "messages-model.h"
 #include "telepathy-text-observer.h"
+#include "pinned-contacts-model.h"
+#include "contact-pin.h"
 
 #include "Models/accounts-filter-model.h"
 
@@ -40,11 +44,15 @@ void QmlPlugins::registerTypes(const char *uri)
     qmlRegisterType<TelepathyTextObserver> (uri, 0, 1, "TelepathyTextObserver");
     qmlRegisterType<Conversation>(uri, 0, 1, "Conversation");
     qmlRegisterType<HideWindowComponent>(uri, 0, 1, "HideWindowComponent");
-
-    //needed for MessageType enum
-    qmlRegisterUncreatableType<MessagesModel>(uri, 0, 1, "MessagesModel",
-        QLatin1String("MessagesModel can not be instanitized directly. Use a TelepathyTextObserver instead"));
+    qmlRegisterType<PinnedContactsModel>(uri, 0, 1, "PinnedContactsModel");
+    qmlRegisterType<ContactPin>(uri, 0, 1, "ContactPin");
+
+    qmlRegisterType<MessagesModel>();
+    qmlRegisterType<ConversationTarget>();
+    qmlRegisterType<ConversationsModel>();
+    qRegisterMetaType<Tp::AccountManagerPtr>();
+    qRegisterMetaType<Tp::ContactPtr>();
+    qRegisterMetaType<Tp::AccountPtr>();
 }
 
 Q_EXPORT_PLUGIN2(conversation, QmlPlugins);
-
diff --git a/KTp/Declarative/telepathy-text-observer.cpp b/KTp/Declarative/telepathy-text-observer.cpp
index 98dfced..c232308 100644
--- a/KTp/Declarative/telepathy-text-observer.cpp
+++ b/KTp/Declarative/telepathy-text-observer.cpp
@@ -64,6 +64,9 @@ TelepathyTextObserver::TelepathyTextObserver(QObject* parent) :
     m_registrar = Tp::ClientRegistrar::create(accountFactory, connectionFactory,
                                             channelFactory, contactFactory);
     m_registrar->registerClient(m_handler, QLatin1String("KDE.TextUi.ConversationWatcher")); //KTp.ChatPlasmoid
+    
+    m_accountManager = Tp::AccountManager::create(accountFactory, connectionFactory, channelFactory, contactFactory);
+    m_accountManager->becomeReady();
 }
 
 TelepathyTextObserver::~TelepathyTextObserver()
@@ -71,8 +74,13 @@ TelepathyTextObserver::~TelepathyTextObserver()
     kDebug() << "deleting text observer";
 }
 
-QAbstractListModel * TelepathyTextObserver::conversationModel()
+ConversationsModel* TelepathyTextObserver::conversationModel() const
 {
     Q_ASSERT(!m_handler.isNull());
     return m_handler.data();
 }
+
+Tp::AccountManagerPtr TelepathyTextObserver::accountManager() const
+{
+    return m_accountManager;
+}
diff --git a/KTp/Declarative/telepathy-text-observer.h b/KTp/Declarative/telepathy-text-observer.h
index 4f2a028..cb90144 100644
--- a/KTp/Declarative/telepathy-text-observer.h
+++ b/KTp/Declarative/telepathy-text-observer.h
@@ -23,23 +23,27 @@
 #include "conversations-model.h"
 
 #include <TelepathyQt/AbstractClientHandler>
+#include "ktp-metatypes.h"
 
 
 class TelepathyTextObserver : public QObject
 {
     Q_OBJECT
 
-    Q_PROPERTY(QObject* conversations READ conversationModel CONSTANT)
+Q_PROPERTY(ConversationsModel* conversations READ conversationModel CONSTANT)
+Q_PROPERTY(Tp::AccountManagerPtr accountManager READ accountManager CONSTANT)
 
   public:
     TelepathyTextObserver(QObject* parent=0);
     ~TelepathyTextObserver();
 
-    QAbstractListModel* conversationModel();
+    ConversationsModel* conversationModel() const;
+    Tp::AccountManagerPtr accountManager() const;
 
   private:
     Tp::SharedPtr<ConversationsModel> m_handler;
     Tp::ClientRegistrarPtr m_registrar;
+    Tp::AccountManagerPtr m_accountManager;
 };
 
 #endif // CONVERSATION_WATCHER_H
diff --git a/KTp/Models/contacts-model.h b/KTp/Models/contacts-model.h
index e352569..2a9cf44 100644
--- a/KTp/Models/contacts-model.h
+++ b/KTp/Models/contacts-model.h
@@ -30,6 +30,7 @@
 #include <TelepathyQt/TextChannel>
 #include <TelepathyQt/Types>
 
+#include <KTp/ktp-metatypes.h>
 #include <KTp/ktp-export.h>
 
 class ContactModelItem;
@@ -152,7 +153,4 @@ private:
     Private *mPriv;
 };
 
-Q_DECLARE_METATYPE(Tp::ContactPtr);
-Q_DECLARE_METATYPE(Tp::AccountPtr);
-
 #endif // TELEPATHY_ACCOUNTS_MODEL_H
diff --git a/KTp/Declarative/hide-window-component.cpp b/KTp/ktp-metatypes.h
similarity index 72%
copy from KTp/Declarative/hide-window-component.cpp
copy to KTp/ktp-metatypes.h
index 67c8e8d..9488ee5 100644
--- a/KTp/Declarative/hide-window-component.cpp
+++ b/KTp/ktp-metatypes.h
@@ -16,15 +16,18 @@
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
-#include "hide-window-component.h"
-#include <kwindowsystem.h>
-#include <QDebug>
-
-HideWindowComponent::HideWindowComponent(QObject* parent)
-    : QObject(parent)
-{}
-
-void HideWindowComponent::hideWindowFromTaskbar(qulonglong winId)
-{
-    KWindowSystem::setState(winId, NET::SkipTaskbar | NET::SkipPager);
-}
+
+#ifndef KTP_METATYPES
+#define KTP_METATYPES
+
+#include <QVariant>
+#include <TelepathyQt/Types>
+#include <TelepathyQt/Contact>
+#include <TelepathyQt/Account>
+#include <TelepathyQt/AccountManager>
+
+Q_DECLARE_METATYPE(Tp::ContactPtr);
+Q_DECLARE_METATYPE(Tp::AccountPtr);
+Q_DECLARE_METATYPE(Tp::AccountManagerPtr);
+
+#endif

-- 
ktp-common-internals packaging



More information about the pkg-kde-commits mailing list