Commit 8c087676 authored by Lasath Fernando's avatar Lasath Fernando Committed by David Edmundson

Tidied code

parent fe6c5ce5
......@@ -19,33 +19,39 @@
#include "conversation.h"
#include "conversation-model.h"
#include <TelepathyQt4/TextChannel>
#include <KDebug>
class Conversation::ConversationPrivate {
class Conversation::ConversationPrivate
{
public:
ConversationModel* model;
Tp::AccountPtr account;
ConversationModel* model;
Tp::AccountPtr account;
};
Conversation::Conversation(Tp::TextChannelPtr channel, Tp::AccountPtr account) :
d(new ConversationPrivate)
Conversation::Conversation ( Tp::TextChannelPtr channel, Tp::AccountPtr account ) :
d ( new ConversationPrivate )
{
d->model = new ConversationModel();
d->model->setTextChannel(channel);
d->model = new ConversationModel();
d->model->setTextChannel ( channel );
d->account = account;
d->account = account;
}
Conversation::Conversation ( QObject* parent ) : QObject ( parent )
{
kError() << "Conversation should not be created directly. Use ConversationWater instead.";
}
const ConversationModel* Conversation::model() const
{
return d->model;
return d->model;
}
Conversation::~Conversation()
{
delete d->model;
delete d->model;
}
......@@ -36,16 +36,17 @@ Q_PROPERTY(const ConversationModel* model READ model NOTIFY modelChanged)
public:
Conversation(Tp::TextChannelPtr channel, Tp::AccountPtr account);
Conversation(QObject* parent = 0);
virtual ~Conversation();
const ConversationModel* model() const;
const ConversationModel* model() const;
Q_SIGNALS:
void modelChanged(ConversationModel* newModel);
void modelChanged(ConversationModel* newModel);
private:
class ConversationPrivate;
ConversationPrivate *d;
class ConversationPrivate;
ConversationPrivate *d;
};
#endif // CONVERSATION_H
......@@ -25,12 +25,12 @@
class MessageItem {
public:
QString user;
QString text;
QDateTime time;
QString user;
QString text;
QDateTime time;
//FIXME : replace with Tp::ChannelTextMessageType
enum MessageType {
//FIXME : replace with Tp::ChannelTextMessageType
enum MessageType {
Incoming,
Outgoing,
Status
......@@ -39,121 +39,121 @@ public:
class ConversationModel::ConversationModelPrivate {
public:
Tp::TextChannelPtr textChannel;
QList<MessageItem> messages;
Tp::TextChannelPtr textChannel;
QList<MessageItem> messages;
};
ConversationModel::ConversationModel(QObject* parent):
QAbstractListModel(parent),
d(new ConversationModelPrivate)
QAbstractListModel(parent),
d(new ConversationModelPrivate)
{
}
Tp::TextChannelPtr ConversationModel::textChannel()
{
return d->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)));
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);
}
setupChannelSignals(channel);
if(d->textChannel) {
removeChannelSignals(channel);
}
d->textChannel = channel; //NOTE : is it necessary to delete the old one?
d->textChannel = channel;
textChannelChanged(channel);
textChannelChanged(channel);
}
void ConversationModel::onMessageReceived(Tp::ReceivedMessage message)
{
beginInsertRows(QModelIndex(), d->messages.count(), d->messages.count());
beginInsertRows(QModelIndex(), d->messages.count(), d->messages.count());
MessageItem newMessage = {
message.sender()->alias(),
message.text(),
message.sent(),
MessageItem::Incoming
};
MessageItem newMessage = {
message.sender()->alias(),
message.text(),
message.sent(),
MessageItem::Incoming
};
d->messages.append(newMessage);
endInsertRows();
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();
Q_UNUSED(flags);
Q_UNUSED(token);
beginInsertRows(QModelIndex(), d->messages.count(), d->messages.count());
MessageItem newMessage = {
tr("Me"), //FIXME : use actual nickname from Tp::AccountPtr
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;
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();
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))
);
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"
......@@ -25,6 +25,7 @@
#include <TelepathyQt4/ChannelClassSpec>
#include <TelepathyQt4/TextChannel>
#include "conversation.h"
#include <TelepathyQt4/ClientRegistrar>
static inline Tp::ChannelClassSpecList channelClassList()
......@@ -34,40 +35,87 @@ static inline Tp::ChannelClassSpecList channelClassList()
<< Tp::ChannelClassSpec::textChatroom();
}
ConversationWatcher::ConversationWatcher() :
AbstractClientObserver(channelClassList())
class ConversationWatcher::ConversationClientObserver :
public Tp::AbstractClientObserver
{
kDebug();
qFatal("Derp!");
*(int*)0=0;
}
public:
virtual 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)
{
kDebug();
Tp::TextChannelPtr textChannel;
Q_FOREACH(const Tp::ChannelPtr & channel, channels) {
textChannel = Tp::TextChannelPtr::dynamicCast(channel);
if (textChannel) {
break;
}
}
void ConversationWatcher::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)
{
kDebug();
Q_ASSERT(textChannel);
Tp::TextChannelPtr textChannel;
Q_FOREACH(const Tp::ChannelPtr & channel, channels) {
textChannel = Tp::TextChannelPtr::dynamicCast(channel);
if (textChannel) {
break;
}
Conversation con(textChannel, account);
m_parent->newConversation(&con);
}
Q_ASSERT(textChannel);
ConversationClientObserver(ConversationWatcher *parent) :
AbstractClientObserver(channelClassList()),
m_parent(parent)
{
}
Conversation con(textChannel, account);
newConversation(con);
ConversationWatcher *m_parent;
Tp::ClientRegistrarPtr registrar;
};
ConversationWatcher::ConversationWatcher() :
d(new ConversationClientObserver(this))
{
kDebug();
Tp::registerTypes();
Tp::AccountFactoryPtr accountFactory = Tp::AccountFactory::create(QDBusConnection::sessionBus(),
Tp::Account::FeatureCore);
Tp::ConnectionFactoryPtr connectionFactory = Tp::ConnectionFactory::create(
QDBusConnection::sessionBus(),
Tp::Features() << Tp::Connection::FeatureSelfContact
<< Tp::Connection::FeatureCore
);
Tp::ChannelFactoryPtr channelFactory = Tp::ChannelFactory::create(QDBusConnection::sessionBus());
channelFactory->addCommonFeatures(Tp::Channel::FeatureCore);
Tp::Features textFeatures = Tp::Features() << Tp::TextChannel::FeatureMessageQueue
<< Tp::TextChannel::FeatureMessageSentSignal
<< Tp::TextChannel::FeatureChatState
<< Tp::TextChannel::FeatureMessageCapabilities;
channelFactory->addFeaturesForTextChats(textFeatures);
channelFactory->addFeaturesForTextChatrooms(textFeatures);
Tp::ContactFactoryPtr contactFactory = Tp::ContactFactory::create(
Tp::Features() << Tp::Contact::FeatureAlias
<< Tp::Contact::FeatureAvatarToken
<< Tp::Contact::FeatureAvatarData
<< Tp::Contact::FeatureCapabilities
<< Tp::Contact::FeatureSimplePresence
);
d->registrar = Tp::ClientRegistrar::create(accountFactory, connectionFactory,
channelFactory, contactFactory);
d->registrar->registerClient(d, QLatin1String("KDE.TextUi.ConversationWatcher"));
}
ConversationWatcher::~ConversationWatcher()
{
}
#include "moc_conversation-watcher.cpp"
\ No newline at end of file
#include "moc_conversation-watcher.cpp"
......@@ -26,24 +26,19 @@
class Conversation;
class KDE_TELEPATHY_CHAT_EXPORT ConversationWatcher : public QObject, public Tp::AbstractClientObserver
class KDE_TELEPATHY_CHAT_EXPORT ConversationWatcher : public QObject
{
Q_OBJECT
public:
virtual 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
);
ConversationWatcher();
~ConversationWatcher();
Q_SIGNALS:
void newConversation(Conversation&);
void newConversation(Conversation *con);
private:
class ConversationClientObserver;
Tp::SharedPtr<ConversationClientObserver> d;
};
#endif // CONVERSATION_WATCHER_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