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

Tidied code

parent fe6c5ce5
...@@ -19,33 +19,39 @@ ...@@ -19,33 +19,39 @@
#include "conversation.h" #include "conversation.h"
#include "conversation-model.h" #include "conversation-model.h"
#include <TelepathyQt4/TextChannel> #include <TelepathyQt4/TextChannel>
#include <KDebug>
class Conversation::ConversationPrivate { class Conversation::ConversationPrivate
{
public: public:
ConversationModel* model; ConversationModel* model;
Tp::AccountPtr account; Tp::AccountPtr account;
}; };
Conversation::Conversation(Tp::TextChannelPtr channel, Tp::AccountPtr account) : Conversation::Conversation ( Tp::TextChannelPtr channel, Tp::AccountPtr account ) :
d(new ConversationPrivate) d ( new ConversationPrivate )
{ {
d->model = new ConversationModel(); d->model = new ConversationModel();
d->model->setTextChannel(channel); 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 const ConversationModel* Conversation::model() const
{ {
return d->model; return d->model;
} }
Conversation::~Conversation() Conversation::~Conversation()
{ {
delete d->model; delete d->model;
} }
...@@ -36,16 +36,17 @@ Q_PROPERTY(const ConversationModel* model READ model NOTIFY modelChanged) ...@@ -36,16 +36,17 @@ Q_PROPERTY(const ConversationModel* model READ model NOTIFY modelChanged)
public: public:
Conversation(Tp::TextChannelPtr channel, Tp::AccountPtr account); Conversation(Tp::TextChannelPtr channel, Tp::AccountPtr account);
Conversation(QObject* parent = 0);
virtual ~Conversation(); virtual ~Conversation();
const ConversationModel* model() const; const ConversationModel* model() const;
Q_SIGNALS: Q_SIGNALS:
void modelChanged(ConversationModel* newModel); void modelChanged(ConversationModel* newModel);
private: private:
class ConversationPrivate; class ConversationPrivate;
ConversationPrivate *d; ConversationPrivate *d;
}; };
#endif // CONVERSATION_H #endif // CONVERSATION_H
...@@ -25,12 +25,12 @@ ...@@ -25,12 +25,12 @@
class MessageItem { class MessageItem {
public: public:
QString user; QString user;
QString text; QString text;
QDateTime time; QDateTime time;
//FIXME : replace with Tp::ChannelTextMessageType //FIXME : replace with Tp::ChannelTextMessageType
enum MessageType { enum MessageType {
Incoming, Incoming,
Outgoing, Outgoing,
Status Status
...@@ -39,121 +39,121 @@ public: ...@@ -39,121 +39,121 @@ public:
class ConversationModel::ConversationModelPrivate { class ConversationModel::ConversationModelPrivate {
public: public:
Tp::TextChannelPtr textChannel; Tp::TextChannelPtr textChannel;
QList<MessageItem> messages; QList<MessageItem> messages;
}; };
ConversationModel::ConversationModel(QObject* parent): ConversationModel::ConversationModel(QObject* parent):
QAbstractListModel(parent), QAbstractListModel(parent),
d(new ConversationModelPrivate) d(new ConversationModelPrivate)
{ {
} }
Tp::TextChannelPtr ConversationModel::textChannel() Tp::TextChannelPtr ConversationModel::textChannel()
{ {
return d->textChannel; return d->textChannel;
} }
void ConversationModel::setupChannelSignals(Tp::TextChannelPtr channel) void ConversationModel::setupChannelSignals(Tp::TextChannelPtr channel)
{ {
QObject::connect(channel.constData(), QObject::connect(channel.constData(),
SIGNAL(messageReceived(Tp::ReceivedMessage)), SIGNAL(messageReceived(Tp::ReceivedMessage)),
SLOT(messageReceived(Tp::ReceivedMessage))); SLOT(messageReceived(Tp::ReceivedMessage)));
QObject::connect(channel.constData(), QObject::connect(channel.constData(),
SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)), SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)),
SLOT(messageSent(Tp::Message,Tp::MessageSendingFlags,QString))); SLOT(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)));
} }
void ConversationModel::setTextChannel(Tp::TextChannelPtr channel) void ConversationModel::setTextChannel(Tp::TextChannelPtr channel)
{ {
setupChannelSignals(channel); setupChannelSignals(channel);
if(d->textChannel) { if(d->textChannel) {
removeChannelSignals(channel); 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) void ConversationModel::onMessageReceived(Tp::ReceivedMessage message)
{ {
beginInsertRows(QModelIndex(), d->messages.count(), d->messages.count()); beginInsertRows(QModelIndex(), d->messages.count(), d->messages.count());
MessageItem newMessage = { MessageItem newMessage = {
message.sender()->alias(), message.sender()->alias(),
message.text(), message.text(),
message.sent(), message.sent(),
MessageItem::Incoming MessageItem::Incoming
}; };
d->messages.append(newMessage); d->messages.append(newMessage);
endInsertRows(); endInsertRows();
} }
void ConversationModel::onMessageSent(Tp::Message message, Tp::MessageSendingFlags flags, QString token) void ConversationModel::onMessageSent(Tp::Message message, Tp::MessageSendingFlags flags, QString token)
{ {
Q_UNUSED(flags); Q_UNUSED(flags);
Q_UNUSED(token); Q_UNUSED(token);
beginInsertRows(QModelIndex(), d->messages.count(), d->messages.count()); beginInsertRows(QModelIndex(), d->messages.count(), d->messages.count());
MessageItem newMessage = { MessageItem newMessage = {
tr("Me"), tr("Me"), //FIXME : use actual nickname from Tp::AccountPtr
message.text(), message.text(),
message.sent(), message.sent(),
MessageItem::Outgoing MessageItem::Outgoing
}; };
d->messages.append(newMessage); d->messages.append(newMessage);
endInsertRows(); endInsertRows();
} }
QVariant ConversationModel::data(const QModelIndex& index, int role) const QVariant ConversationModel::data(const QModelIndex& index, int role) const
{ {
QVariant result; QVariant result;
if(!index.isValid()) { if(!index.isValid()) {
kError() << "Attempting to access data at invalid index (" << index << ")"; kError() << "Attempting to access data at invalid index (" << index << ")";
} else { } else {
MessageItem* requestedData = &d->messages[index.row()]; MessageItem* requestedData = &d->messages[index.row()];
switch(role) { switch(role) {
case UserRole: case UserRole:
result = requestedData->user; result = requestedData->user;
break; break;
case TextRole: case TextRole:
result = requestedData->text; result = requestedData->text;
break; break;
case TypeRole: case TypeRole:
result = requestedData->type; result = requestedData->type;
break; break;
case TimeRole: case TimeRole:
result = requestedData->time; result = requestedData->time;
break; break;
}; };
} }
return result; return result;
} }
int ConversationModel::rowCount(const QModelIndex& parent) const int ConversationModel::rowCount(const QModelIndex& parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent);
return d->messages.count(); return d->messages.count();
} }
void ConversationModel::removeChannelSignals(Tp::TextChannelPtr channel) void ConversationModel::removeChannelSignals(Tp::TextChannelPtr channel)
{ {
QObject::disconnect(channel.constData(), QObject::disconnect(channel.constData(),
SIGNAL(messageReceived(Tp::ReceivedMessage)), SIGNAL(messageReceived(Tp::ReceivedMessage)),
this, this,
SLOT(onMessageReceived(Tp::ReceivedMessage)) SLOT(onMessageReceived(Tp::ReceivedMessage))
); );
QObject::disconnect(channel.constData(), QObject::disconnect(channel.constData(),
SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)), SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)),
this, this,
SLOT(onMessageSent(Tp::Message,Tp::MessageSendingFlags,QString)) SLOT(onMessageSent(Tp::Message,Tp::MessageSendingFlags,QString))
); );
} }
#include "moc_conversation-model.cpp" #include "moc_conversation-model.cpp"
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <TelepathyQt4/ChannelClassSpec> #include <TelepathyQt4/ChannelClassSpec>
#include <TelepathyQt4/TextChannel> #include <TelepathyQt4/TextChannel>
#include "conversation.h" #include "conversation.h"
#include <TelepathyQt4/ClientRegistrar>
static inline Tp::ChannelClassSpecList channelClassList() static inline Tp::ChannelClassSpecList channelClassList()
...@@ -34,40 +35,87 @@ static inline Tp::ChannelClassSpecList channelClassList() ...@@ -34,40 +35,87 @@ static inline Tp::ChannelClassSpecList channelClassList()
<< Tp::ChannelClassSpec::textChatroom(); << Tp::ChannelClassSpec::textChatroom();
} }
ConversationWatcher::ConversationWatcher() : class ConversationWatcher::ConversationClientObserver :
AbstractClientObserver(channelClassList()) public Tp::AbstractClientObserver
{ {
kDebug(); public:
qFatal("Derp!"); virtual void observeChannels(
*(int*)0=0; 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, Q_ASSERT(textChannel);
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; Conversation con(textChannel, account);
Q_FOREACH(const Tp::ChannelPtr & channel, channels) { m_parent->newConversation(&con);
textChannel = Tp::TextChannelPtr::dynamicCast(channel);
if (textChannel) {
break;
}
} }
Q_ASSERT(textChannel); ConversationClientObserver(ConversationWatcher *parent) :
AbstractClientObserver(channelClassList()),
m_parent(parent)
{
}
Conversation con(textChannel, account); ConversationWatcher *m_parent;
newConversation(con); 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() ConversationWatcher::~ConversationWatcher()
{ {
} }
#include "moc_conversation-watcher.cpp" #include "moc_conversation-watcher.cpp"
\ No newline at end of file
...@@ -26,24 +26,19 @@ ...@@ -26,24 +26,19 @@
class Conversation; class Conversation;
class KDE_TELEPATHY_CHAT_EXPORT ConversationWatcher : public QObject, public Tp::AbstractClientObserver class KDE_TELEPATHY_CHAT_EXPORT ConversationWatcher : public QObject
{ {
Q_OBJECT Q_OBJECT
public: 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();
~ConversationWatcher(); ~ConversationWatcher();
Q_SIGNALS: Q_SIGNALS:
void newConversation(Conversation&); void newConversation(Conversation *con);
private:
class ConversationClientObserver;
Tp::SharedPtr<ConversationClientObserver> d;
}; };
#endif // CONVERSATION_WATCHER_H #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