Commit 4810cdf8 authored by Lasath Fernando's avatar Lasath Fernando Committed by David Edmundson
Browse files

Implemented data retrival for MessageModel.

parent 53cd664a
......@@ -20,18 +20,139 @@
#include "conversation-model.h"
ConversationModel::ConversationModel(QObject* parent): QAbstractListModel(parent)
#include <KDebug>
#include <TelepathyQt4/ReceivedMessage>
struct MessageItem {
QString user;
QString text;
QDateTime time;
//FIXME : replace with Tp::ChannelTextMessageType
enum MessageType {
Incoming,
Outgoing,
Status
} type;
};
class ConversationModel::ConversationModelPrivate {
public:
Tp::TextChannelPtr textChannel;
QList<MessageItem> messages;
};
ConversationModel::ConversationModel(QObject* parent):
QAbstractListModel(parent),
d(new ConversationModelPrivate)
{
}
Tp::TextChannelPtr ConversationModel::textChannel()
{
return d->textChannel;
}
void ConversationModel::setupChannelSignals(Tp::TextChannelPtr channel)
{
QObject::connect(channel.constData(),
SIGNAL(messageReceived(Tp::ReceivedMessage)),
SLOT(messageReceived(Tp::ReceivedMessage)));
QObject::connect(channel.constData(),
SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)),
SLOT(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)));
}
void ConversationModel::setTextChannel(Tp::TextChannelPtr channel)
{
setupChannelSignals(channel);
if(d->textChannel) {
removeChannelSignals(channel);
}
d->textChannel = channel; //NOTE : is it necessary to delete the old one?
textChannelChanged(channel);
}
void ConversationModel::onMessageReceived(Tp::ReceivedMessage message)
{
beginInsertRows(QModelIndex(), d->messages.count(), d->messages.count());
MessageItem newMessage = {
message.sender()->alias(),
message.text(),
message.sent(),
MessageItem::Incoming
};
d->messages.append(newMessage);
endInsertRows();
}
void ConversationModel::onMessageSent(Tp::Message message, Tp::MessageSendingFlags flags, QString token)
{
Q_UNUSED(flags);
Q_UNUSED(token);
beginInsertRows(QModelIndex(), d->messages.count(), d->messages.count());
MessageItem newMessage = {
tr("Me"),
message.text(),
message.sent(),
MessageItem::Outgoing
};
d->messages.append(newMessage);
endInsertRows();
}
QVariant ConversationModel::data(const QModelIndex& index, int role) const
{
QVariant result;
if(!index.isValid()) {
kError() << "Attempting to access data at invalid index (" << index << ")";
} else {
MessageItem* requestedData = &d->messages[index.row()];
switch(role) {
case UserRole:
result = requestedData->user;
break;
case TextRole:
result = requestedData->text;
break;
case TypeRole:
result = requestedData->type;
break;
case TimeRole:
result = requestedData->time;
break;
};
}
return result;
}
int ConversationModel::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return d->messages.count();
}
void ConversationModel::removeChannelSignals(Tp::TextChannelPtr channel)
{
QObject::disconnect(channel.constData(),
SIGNAL(messageReceived(Tp::ReceivedMessage)),
this,
SLOT(onMessageReceived(Tp::ReceivedMessage))
);
QObject::disconnect(channel.constData(),
SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)),
this,
SLOT(onMessageSent(Tp::Message,Tp::MessageSendingFlags,QString))
);
}
#include "moc_conversation-model.cpp"
......@@ -18,19 +18,53 @@
*/
#ifndef CONVERSATIONMODEL_H
#define CONVERSATIONMODEL_H
#ifndef CONVERSATION_MODEL_H
#define CONVERSATION_MODEL_H
#include "kdetelepathychat_export.h"
#include <QAbstractItemModel>
#include <TelepathyQt4/TextChannel>
class ConversationModel : public QAbstractListModel
class KDE_TELEPATHY_CHAT_EXPORT ConversationModel : public QAbstractListModel
{
Q_OBJECT
// Q_PROPERTY(Tp::TextChannelPtr textChannel
// READ textChannel
// WRITE setTextChannel
// NOTIFY textChannelChanged
// )
public:
explicit ConversationModel(QObject* parent = 0);
ConversationModel(QObject* parent = 0);
enum Roles {
UserRole = Qt::UserRole,
TextRole,
TypeRole,
TimeRole
};
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
Tp::TextChannelPtr textChannel();
void setTextChannel(Tp::TextChannelPtr channel);
Q_SIGNALS:
void textChannelChanged(Tp::TextChannelPtr newChannel);
private Q_SLOTS:
void onMessageReceived(Tp::ReceivedMessage);
void onMessageSent(Tp::Message,Tp::MessageSendingFlags,QString);
private:
void setupChannelSignals(Tp::TextChannelPtr channel);
void removeChannelSignals(Tp::TextChannelPtr channel);
class ConversationModelPrivate;
ConversationModelPrivate *d;
};
#endif // CONVERSATIONMODEL_H
#endif // CONVERSATION_MODEL_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment