diff options
author | Mateu Batle <mateu.batle@collabora.co.uk> | 2011-03-22 19:17:19 -0500 |
---|---|---|
committer | Mateu Batle <mateu.batle@collabora.co.uk> | 2011-04-04 16:19:34 -0700 |
commit | 7cef5980200762b96b93f8b655ee3f09cd56a8e1 (patch) | |
tree | eb9b61342b566c7543f2cead576922e92f45c6d7 | |
parent | 854560c755c94cb44ba2119c21e7d9a6c4dbccda (diff) |
Refactored ConversationModel into two classes AbstractConversationModel and SessionConversationModel
-rw-r--r-- | TelepathyQt4Yell/Models/AbstractConversationModel | 14 | ||||
-rw-r--r-- | TelepathyQt4Yell/Models/CMakeLists.txt | 9 | ||||
-rw-r--r-- | TelepathyQt4Yell/Models/ConversationModel | 14 | ||||
-rw-r--r-- | TelepathyQt4Yell/Models/SessionConversationModel | 14 | ||||
-rw-r--r-- | TelepathyQt4Yell/Models/abstract-conversation-model.cpp | 154 | ||||
-rw-r--r-- | TelepathyQt4Yell/Models/abstract-conversation-model.h (renamed from TelepathyQt4Yell/Models/conversation-model.h) | 27 | ||||
-rw-r--r-- | TelepathyQt4Yell/Models/session-conversation-model.cpp (renamed from TelepathyQt4Yell/Models/conversation-model.cpp) | 116 | ||||
-rw-r--r-- | TelepathyQt4Yell/Models/session-conversation-model.h | 70 |
8 files changed, 278 insertions, 140 deletions
diff --git a/TelepathyQt4Yell/Models/AbstractConversationModel b/TelepathyQt4Yell/Models/AbstractConversationModel new file mode 100644 index 0000000..95a5100 --- /dev/null +++ b/TelepathyQt4Yell/Models/AbstractConversationModel @@ -0,0 +1,14 @@ +#ifndef _TelepathyQt4Yell_model_AbstractConversationModel_HEADER_GUARD_ +#define _TelepathyQt4Yell_model_AbstractConversationModel_HEADER_GUARD_ + +#ifndef IN_TELEPATHY_QT4_YELL_MODELS_HEADER +#define IN_TELEPATHY_QT4_YELL_MODELS_HEADER +#endif + +#include <TelepathyQt4Yell/Models/abstract-conversation-model.h> + +#undef IN_TELEPATHY_QT4_YELL_MODELS_HEADER + +#endif // _TelepathyQt4Yell_model_AbstractConversationModel_HEADER_GUARD_ + +// vim:set ft=cpp: diff --git a/TelepathyQt4Yell/Models/CMakeLists.txt b/TelepathyQt4Yell/Models/CMakeLists.txt index f8cd446..bc23cee 100644 --- a/TelepathyQt4Yell/Models/CMakeLists.txt +++ b/TelepathyQt4Yell/Models/CMakeLists.txt @@ -10,7 +10,8 @@ set(telepathy_qt4_yell_models_SRCS avatar-image-provider.cpp contact-model-item.cpp conversation-item.cpp - conversation-model.cpp + abstract-conversation-model.cpp + session-conversation-model.cpp flat-model-proxy.cpp tree-node.cpp ) @@ -21,7 +22,8 @@ set(telepathy_qt4_yell_models_MOC_SRCS avatar-image-provider.h contact-model-item.h conversation-item.h - conversation-model.h + abstract-conversation-model.h + session-conversation-model.h flat-model-proxy.h tree-node.h ) @@ -32,7 +34,8 @@ set(telepathy_qt4_yell_models_HEADERS AvatarImageProvider ContactModelItem ConversationItem - ConversationModel + AbstractConversationModel + SessionConversationModel FlatModelProxy Global TreeNode diff --git a/TelepathyQt4Yell/Models/ConversationModel b/TelepathyQt4Yell/Models/ConversationModel deleted file mode 100644 index b567165..0000000 --- a/TelepathyQt4Yell/Models/ConversationModel +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef _TelepathyQt4Yell_model_ConversationModel_HEADER_GUARD_ -#define _TelepathyQt4Yell_model_ConversationModel_HEADER_GUARD_ - -#ifndef IN_TELEPATHY_QT4_YELL_MODELS_HEADER -#define IN_TELEPATHY_QT4_YELL_MODELS_HEADER -#endif - -#include <TelepathyQt4Yell/Models/conversation-model.h> - -#undef IN_TELEPATHY_QT4_YELL_MODELS_HEADER - -#endif // _TelepathyQt4Yell_model_ConversationModel_HEADER_GUARD_ - -// vim:set ft=cpp: diff --git a/TelepathyQt4Yell/Models/SessionConversationModel b/TelepathyQt4Yell/Models/SessionConversationModel new file mode 100644 index 0000000..676f8ac --- /dev/null +++ b/TelepathyQt4Yell/Models/SessionConversationModel @@ -0,0 +1,14 @@ +#ifndef _TelepathyQt4Yell_model_SessionConversationModel_HEADER_GUARD_ +#define _TelepathyQt4Yell_model_SessionConversationModel_HEADER_GUARD_ + +#ifndef IN_TELEPATHY_QT4_YELL_MODELS_HEADER +#define IN_TELEPATHY_QT4_YELL_MODELS_HEADER +#endif + +#include <TelepathyQt4Yell/Models/session-conversation-model.h> + +#undef IN_TELEPATHY_QT4_YELL_MODELS_HEADER + +#endif // _TelepathyQt4Yell_model_SessionConversationModel_HEADER_GUARD_ + +// vim:set ft=cpp: diff --git a/TelepathyQt4Yell/Models/abstract-conversation-model.cpp b/TelepathyQt4Yell/Models/abstract-conversation-model.cpp new file mode 100644 index 0000000..4e831f9 --- /dev/null +++ b/TelepathyQt4Yell/Models/abstract-conversation-model.cpp @@ -0,0 +1,154 @@ +/* + * This file is part of TelepathyQt4 + * + * Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/> + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <TelepathyQt4Yell/Models/AbstractConversationModel> + +#include "TelepathyQt4Yell/Models/_gen/abstract-conversation-model.moc.hpp" + +#include <TelepathyQt4Yell/Models/ConversationItem> + +#include <TelepathyQt4/AvatarData> +#include <TelepathyQt4/PendingReady> +#include <TelepathyQt4/ReceivedMessage> + +#include <QPixmap> +#include <QtAlgorithms> + +namespace Tpy +{ + +struct TELEPATHY_QT4_YELL_MODELS_NO_EXPORT AbstractConversationModel::Private +{ + Private() + { + } + + QList<const ConversationItem *> mItems; +}; + +AbstractConversationModel::AbstractConversationModel(QObject *parent) + : QAbstractListModel(parent), + mPriv(new Private()) +{ + QHash<int, QByteArray> roles; + roles[TextRole] = "text"; + roles[ContactRole] = "contact"; + roles[ContactAvatarRole] = "contactAvatar"; + roles[TimeRole] = "time"; + roles[TypeRole] = "type"; + roles[ItemRole] = "item"; + setRoleNames(roles); +} + +AbstractConversationModel::~AbstractConversationModel() +{ + qDeleteAll(mPriv->mItems); + mPriv->mItems.clear(); + delete mPriv; +} + +QVariant AbstractConversationModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) { + return QVariant(); + } + + if (index.row() >= mPriv->mItems.count()) { + return QVariant(); + } + + const ConversationItem *item = mPriv->mItems[index.row()]; + switch (role) { + case TextRole: + return item->text(); + case ContactRole: + return item->contact()->alias(); + case ContactAvatarRole: + return item->contact()->avatarData().fileName; + case TimeRole: + return item->time(); + case TypeRole: + switch (item->type()) { + case ConversationItem::INCOMING_MESSAGE: + return QString::fromLatin1("incoming_message"); + case ConversationItem::OUTGOING_MESSAGE: + return QString::fromLatin1("outgoing_message"); + case ConversationItem::EVENT: + return QString::fromLatin1("event"); + default: + return QString(); + } + case ItemRole: + return QVariant::fromValue( + const_cast<QObject *>( + static_cast<const QObject *>(item))); + default: + return QVariant(); + } + +} + +int AbstractConversationModel::rowCount(const QModelIndex &parent) const +{ + return mPriv->mItems.count(); +} + +void AbstractConversationModel::addItem(const ConversationItem *item) +{ + beginInsertRows(QModelIndex(), mPriv->mItems.count(), mPriv->mItems.count()); + mPriv->mItems.append(item); + endInsertRows(); +} + +bool AbstractConversationModel::deleteItem(const ConversationItem *item) +{ + int num = mPriv->mItems.indexOf(item); + if (num != -1) { + beginRemoveRows(QModelIndex(), num, num); + mPriv->mItems.removeAt(num); + endRemoveRows(); + return true; + } + + return false; +} + +QModelIndex AbstractConversationModel::index(const ConversationItem *item) const +{ + int num = mPriv->mItems.indexOf(item); + if (num != -1) { + return QAbstractListModel::index(num); + } + + return QModelIndex(); +} + +void AbstractConversationModel::insertItems(QList<const ConversationItem *> items, int index) +{ + beginInsertRows(QModelIndex(), index, index + items.count() - 1); + const Tpy::ConversationItem *item; + int i = 0; + foreach(item, items) { + mPriv->mItems.insert(index + i, item); + } + endInsertRows(); +} + +} diff --git a/TelepathyQt4Yell/Models/conversation-model.h b/TelepathyQt4Yell/Models/abstract-conversation-model.h index 2a2e6e9..4cafdc3 100644 --- a/TelepathyQt4Yell/Models/conversation-model.h +++ b/TelepathyQt4Yell/Models/abstract-conversation-model.h @@ -18,8 +18,8 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef _TelepathyQt4Yell_Models_conversation_model_h_HEADER_GUARD_ -#define _TelepathyQt4Yell_Models_conversation_model_h_HEADER_GUARD_ +#ifndef _TelepathyQt4Yell_Models_abstract_conversation_model_h_HEADER_GUARD_ +#define _TelepathyQt4Yell_Models_abstract_conversation_model_h_HEADER_GUARD_ #ifndef IN_TELEPATHY_QT4_YELL_MODELS_HEADER #error IN_TELEPATHY_QT4_YELL_MODELS_HEADER @@ -37,10 +37,10 @@ namespace Tpy class ConversationItem; -class TELEPATHY_QT4_YELL_MODELS_EXPORT ConversationModel : public QAbstractListModel +class TELEPATHY_QT4_YELL_MODELS_EXPORT AbstractConversationModel : public QAbstractListModel { Q_OBJECT - Q_DISABLE_COPY(ConversationModel) + Q_DISABLE_COPY(AbstractConversationModel) public: enum Role { @@ -52,16 +52,11 @@ public: ItemRole }; - explicit ConversationModel(const Tp::ContactPtr &self, const Tp::TextChannelPtr &channel, QObject *parent = 0); - virtual ~ConversationModel(); + explicit AbstractConversationModel(QObject *parent = 0); + virtual ~AbstractConversationModel(); virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; virtual QVariant data(const QModelIndex &index, int role) const; - Q_INVOKABLE void sendMessage(const QString &text); - Q_INVOKABLE void disconnectChannelQueue(); - Q_INVOKABLE void connectChannelQueue(); - - Tp::ContactPtr selfContact() const; protected: @@ -69,17 +64,11 @@ protected: typedef Tp::ChannelChatState ChannelChatState; void addItem(const ConversationItem *item); + void insertItems(QList<const ConversationItem *> items, int index = 0); bool deleteItem(const ConversationItem *item); QModelIndex index(const ConversationItem *item) const; -protected Q_SLOTS: - virtual void onChatStateChanged(const Tp::ContactPtr &contact, Tp::ChannelChatState state); - -private Q_SLOTS: - void onChannelReady(Tp::PendingOperation *op); - void onMessageReceived(const Tp::ReceivedMessage &message); - private: struct Private; friend struct Private; @@ -88,4 +77,4 @@ private: } -#endif // _TelepathyQt4Yell_Models_conversation_model_h_HEADER_GUARD_ +#endif // _TelepathyQt4Yell_Models_abstract_conversation_model_h_HEADER_GUARD_ diff --git a/TelepathyQt4Yell/Models/conversation-model.cpp b/TelepathyQt4Yell/Models/session-conversation-model.cpp index cfa41eb..619035d 100644 --- a/TelepathyQt4Yell/Models/conversation-model.cpp +++ b/TelepathyQt4Yell/Models/session-conversation-model.cpp @@ -18,9 +18,9 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include <TelepathyQt4Yell/Models/ConversationModel> +#include <TelepathyQt4Yell/Models/SessionConversationModel> -#include "TelepathyQt4Yell/Models/_gen/conversation-model.moc.hpp" +#include "TelepathyQt4Yell/Models/_gen/session-conversation-model.moc.hpp" #include <TelepathyQt4Yell/Models/ConversationItem> @@ -34,7 +34,7 @@ namespace Tpy { -struct TELEPATHY_QT4_YELL_MODELS_NO_EXPORT ConversationModel::Private +struct TELEPATHY_QT4_YELL_MODELS_NO_EXPORT SessionConversationModel::Private { Private(const Tp::ContactPtr &self, const Tp::TextChannelPtr &channel) : mSelf(self), @@ -44,81 +44,23 @@ struct TELEPATHY_QT4_YELL_MODELS_NO_EXPORT ConversationModel::Private Tp::ContactPtr mSelf; Tp::TextChannelPtr mChannel; - QList<const ConversationItem *> mItems; }; -ConversationModel::ConversationModel(const Tp::ContactPtr &self, const Tp::TextChannelPtr &channel, QObject *parent) - : QAbstractListModel(parent), +SessionConversationModel::SessionConversationModel(const Tp::ContactPtr &self, const Tp::TextChannelPtr &channel, QObject *parent) + : AbstractConversationModel(parent), mPriv(new Private(self,channel)) { connect(mPriv->mChannel.data(), SIGNAL(chatStateChanged(Tp::ContactPtr,Tp::ChannelChatState)), SLOT(onChatStateChanged(Tp::ContactPtr,Tp::ChannelChatState))); - - QHash<int, QByteArray> roles; - roles[TextRole] = "text"; - roles[ContactRole] = "contact"; - roles[ContactAvatarRole] = "contactAvatar"; - roles[TimeRole] = "time"; - roles[TypeRole] = "type"; - roles[ItemRole] = "item"; - setRoleNames(roles); } -ConversationModel::~ConversationModel() +SessionConversationModel::~SessionConversationModel() { - qDeleteAll(mPriv->mItems); - mPriv->mItems.clear(); delete mPriv; } -QVariant ConversationModel::data(const QModelIndex &index, int role) const -{ - if (!index.isValid()) { - return QVariant(); - } - - if (index.row() >= mPriv->mItems.count()) { - return QVariant(); - } - - const ConversationItem *item = mPriv->mItems[index.row()]; - switch (role) { - case TextRole: - return item->text(); - case ContactRole: - return item->contact()->alias(); - case ContactAvatarRole: - return item->contact()->avatarData().fileName; - case TimeRole: - return item->time(); - case TypeRole: - switch (item->type()) { - case ConversationItem::INCOMING_MESSAGE: - return QString::fromLatin1("incoming_message"); - case ConversationItem::OUTGOING_MESSAGE: - return QString::fromLatin1("outgoing_message"); - case ConversationItem::EVENT: - return QString::fromLatin1("event"); - default: - return QString(); - } - case ItemRole: - return QVariant::fromValue( - const_cast<QObject *>( - static_cast<const QObject *>(item))); - default: - return QVariant(); - } - -} - -int ConversationModel::rowCount(const QModelIndex &parent) const -{ - return mPriv->mItems.count(); -} - -void ConversationModel::sendMessage(const QString &text) +void SessionConversationModel::sendMessage(const QString &text) { ConversationItem *item = new ConversationItem(mPriv->mSelf, QDateTime::currentDateTime(), text, ConversationItem::OUTGOING_MESSAGE, this); @@ -127,46 +69,12 @@ void ConversationModel::sendMessage(const QString &text) mPriv->mChannel->send(item->text()); } -Tp::ContactPtr ConversationModel::selfContact() const +Tp::ContactPtr SessionConversationModel::selfContact() const { return mPriv->mSelf; } -void ConversationModel::addItem(const ConversationItem *item) -{ - beginInsertRows(QModelIndex(), mPriv->mItems.count(), mPriv->mItems.count()); - mPriv->mItems.append(item); - endInsertRows(); -} - -bool ConversationModel::deleteItem(const ConversationItem *item) -{ - int num = mPriv->mItems.indexOf(item); - if (num != -1) { - beginRemoveRows(QModelIndex(), num, num); - mPriv->mItems.removeAt(num); - endRemoveRows(); - return true; - } - - return false; -} - -QModelIndex ConversationModel::index(const ConversationItem *item) const -{ - int num = mPriv->mItems.indexOf(item); - if (num != -1) { - return QAbstractListModel::index(num); - } - - return QModelIndex(); -} - -void ConversationModel::onChannelReady(Tp::PendingOperation *op) -{ -} - -void ConversationModel::onMessageReceived(const Tp::ReceivedMessage &message) +void SessionConversationModel::onMessageReceived(const Tp::ReceivedMessage &message) { // TODO: For the moment skip if the message is a delivery report // Later they could be used to report status on sent messages @@ -178,7 +86,7 @@ void ConversationModel::onMessageReceived(const Tp::ReceivedMessage &message) mPriv->mChannel->acknowledge(QList<Tp::ReceivedMessage>() << message); } -void ConversationModel::onChatStateChanged(const Tp::ContactPtr &contact, ChannelChatState state) +void SessionConversationModel::onChatStateChanged(const Tp::ContactPtr &contact, ChannelChatState state) { // ignore events originating from self if (contact == mPriv->mSelf) { @@ -197,7 +105,7 @@ void ConversationModel::onChatStateChanged(const Tp::ContactPtr &contact, Channe * Disconnect the model from the channel messages queue so that messages on the queue will not * be acknowledged and entered into the model automatically */ -void ConversationModel::disconnectChannelQueue() +void SessionConversationModel::disconnectChannelQueue() { disconnect(mPriv->mChannel.data(), SIGNAL(messageReceived(Tp::ReceivedMessage)), this, SLOT(onMessageReceived(Tp::ReceivedMessage))); @@ -206,7 +114,7 @@ void ConversationModel::disconnectChannelQueue() /** * Reconnect the model to the channel queue and acknowledge messages on the queue */ -void ConversationModel::connectChannelQueue() +void SessionConversationModel::connectChannelQueue() { // reconnect the signal connect(mPriv->mChannel.data(), diff --git a/TelepathyQt4Yell/Models/session-conversation-model.h b/TelepathyQt4Yell/Models/session-conversation-model.h new file mode 100644 index 0000000..798c8d1 --- /dev/null +++ b/TelepathyQt4Yell/Models/session-conversation-model.h @@ -0,0 +1,70 @@ +/* + * This file is part of TelepathyQt4 + * + * Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/> + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _TelepathyQt4Yell_Models_session_conversation_model_h_HEADER_GUARD_ +#define _TelepathyQt4Yell_Models_conversation_model_h_HEADER_GUARD_ + +#ifndef IN_TELEPATHY_QT4_YELL_MODELS_HEADER +#error IN_TELEPATHY_QT4_YELL_MODELS_HEADER +#endif + +#include <TelepathyQt4Yell/Models/Global> + +#include <TelepathyQt4/TextChannel> +#include <TelepathyQt4/Types> +#include <TelepathyQt4Yell/Models/AbstractConversationModel> + +#include <QAbstractListModel> + +namespace Tpy +{ + +class ConversationItem; + +class TELEPATHY_QT4_YELL_MODELS_EXPORT SessionConversationModel : public AbstractConversationModel +{ + Q_OBJECT + Q_DISABLE_COPY(SessionConversationModel) + +public: + explicit SessionConversationModel(const Tp::ContactPtr &self, const Tp::TextChannelPtr &channel, QObject *parent = 0); + virtual ~SessionConversationModel(); + + Q_INVOKABLE void sendMessage(const QString &text); + Q_INVOKABLE void disconnectChannelQueue(); + Q_INVOKABLE void connectChannelQueue(); + + Tp::ContactPtr selfContact() const; + +protected Q_SLOTS: + virtual void onChatStateChanged(const Tp::ContactPtr &contact, Tp::ChannelChatState state); + +private Q_SLOTS: + void onMessageReceived(const Tp::ReceivedMessage &message); + +private: + struct Private; + friend struct Private; + Private *mPriv; +}; + +} + +#endif // _TelepathyQt4Yell_Models_session_conversation_model_h_HEADER_GUARD_ |