Commit d179984e authored by Leon Handreke's avatar Leon Handreke

Remove ConversationTarget, put as title/avatar/presenceIcon into Conversation

REVIEW: 114718
parent 095e30df
......@@ -5,7 +5,6 @@ include_directories (${CMAKE_CURRENT_BINARY_DIR}
set (ktp_qml_plugin_SRCS
${KTP_GLOBAL_SOURCES}
conversation-target.cpp
conversation.cpp
conversations-model.cpp
hide-window-component.cpp
......
/*
Copyright (C) 2011 Lasath Fernando <kde@lasath.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 "conversation-target.h"
#include <TelepathyQt/AvatarData>
#include <TelepathyQt/Contact>
#include <KDebug>
#include <KTp/presence.h>
class ConversationTarget::ConversationTargetPrivate
{
public:
KTp::ContactPtr contact;
KIcon avatar;
Tp::AccountPtr account;
};
ConversationTarget::ConversationTarget(const Tp::AccountPtr &account, const KTp::ContactPtr &contact, QObject *parent) :
QObject(parent),
d(new ConversationTargetPrivate)
{
kDebug();
if (contact) {
setupContactSignals(contact);
}
d->contact = contact;
d->account = account;
updateAvatar();
}
void ConversationTarget::setupContactSignals(KTp::ContactPtr contact)
{
connect(contact.constData(), SIGNAL(aliasChanged(QString)), SIGNAL(nickChanged(QString)));
connect(contact.constData(), SIGNAL(avatarDataChanged(Tp::AvatarData)), SLOT(onAvatarDataChanged()));
connect(contact.constData(), SIGNAL(presenceChanged(Tp::Presence)), SLOT(onPresenceChanged()));
}
QIcon ConversationTarget::avatar() const
{
if (d->contact) {
return d->avatar;
} else {
return QIcon();
}
}
QString ConversationTarget::id() const
{
if (d->contact) {
return d->contact->id();
} else {
return QString();
}
}
QString ConversationTarget::nick() const
{
if (d->contact) {
return d->contact->alias();
} else {
return QString();
}
}
QIcon ConversationTarget::presenceIcon() const
{
if (d->contact) {
return KTp::Presence(d->contact->presence()).icon();
} else {
return QIcon();
}
}
QString ConversationTarget::presenceIconName() const
{
if (d->contact) {
return KTp::Presence(d->contact->presence()).iconName();
} else {
return QString();
}
}
void ConversationTarget::onPresenceChanged()
{
Q_EMIT presenceIconChanged(presenceIcon());
Q_EMIT presenceIconNameChanged(presenceIconName());
}
void ConversationTarget::onAvatarDataChanged()
{
updateAvatar();
Q_EMIT avatarChanged(avatar());
}
void ConversationTarget::updateAvatar()
{
QString path;
if (d->contact) {
path = d->contact->avatarData().fileName;
}
if (path.isEmpty()) {
path = QLatin1String("im-user");
}
d->avatar = KIcon(path);
}
KTp::ContactPtr ConversationTarget::contact() const
{
return d->contact;
}
Tp::AccountPtr ConversationTarget::account() const
{
return d->account;
}
ConversationTarget::~ConversationTarget()
{
delete d;
}
/*
Copyright (C) 2011 Lasath Fernando <kde@lasath.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 CONVERSATION_TARGET_H
#define CONVERSATION_TARGET_H
#include <QObject>
#include <QIcon>
#include "KTp/types.h"
class ConversationTarget : public QObject
{
Q_OBJECT
Q_PROPERTY(QIcon avatar READ avatar NOTIFY avatarChanged);
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 CONSTANT)
Q_PROPERTY(KTp::ContactPtr contact READ contact CONSTANT)
Q_PROPERTY(Tp::AccountPtr account READ account CONSTANT)
public:
explicit ConversationTarget(const Tp::AccountPtr &account, const KTp::ContactPtr &contact, QObject *parent = 0);
virtual ~ConversationTarget();
QIcon avatar() const;
QString id() const;
QString nick() const;
QIcon presenceIcon() const;
QString presenceIconName() const;
KTp::ContactPtr contact() const;
Tp::AccountPtr account() const;
Q_SIGNALS:
void avatarChanged(QIcon avatar);
void nickChanged(QString nick);
void presenceIconChanged(QIcon icon);
void presenceIconNameChanged(QString icon);
private Q_SLOTS:
void onAvatarDataChanged();
void onPresenceChanged();
private:
void setupContactSignals(KTp::ContactPtr contact);
void updateAvatar();
class ConversationTargetPrivate;
ConversationTargetPrivate *d;
};
Q_DECLARE_METATYPE(ConversationTarget*)
#endif // CONVERSATION_TARGET_H
......@@ -26,7 +26,6 @@
#include <TelepathyQt/PendingChannel>
#include <KDebug>
#include "conversation-target.h"
#include "channel-delegator.h"
......@@ -34,13 +33,15 @@ class Conversation::ConversationPrivate
{
public:
MessagesModel *messages;
ConversationTarget *target;
//stores if the conversation has been delegated to another client and we are only observing the channel
//and not handling it.
bool delegated;
bool valid;
Tp::AccountPtr account;
QTimer *pausedStateTimer;
// May be null for group chats.
KTp::ContactPtr targetContact;
bool isGroupChat;
};
Conversation::Conversation(const Tp::TextChannelPtr &channel,
......@@ -56,13 +57,25 @@ Conversation::Conversation(const Tp::TextChannelPtr &channel,
d->messages = new MessagesModel(account, this);
setTextChannel(channel);
d->target = new ConversationTarget(account, KTp::ContactPtr::qObjectCast(channel->targetContact()), this);
d->delegated = false;
d->pausedStateTimer = new QTimer(this);
d->pausedStateTimer->setSingleShot(true);
connect(d->pausedStateTimer, SIGNAL(timeout()), this, SLOT(onChatPausedTimerExpired()));
if (channel->targetContact().isNull()) {
d->isGroupChat = true;
} else {
d->isGroupChat = false;
d->targetContact = KTp::ContactPtr::qObjectCast(channel->targetContact());
connect(d->targetContact.constData(), SIGNAL(aliasChanged(QString)),
this, SLOT(onTargetContactAliasChanged(QString)));
connect(d->targetContact.constData(), SIGNAL(avatarDataChanged(Tp::AvatarData)),
this, SLOT(onTargetContactAvatarDataChanged()));
connect(d->targetContact.constData(), SIGNAL(presenceChanged(Tp::Presence)),
this, SLOT(onTargetContactPresenceChanged()));
}
}
Conversation::Conversation(QObject *parent) : QObject(parent)
......@@ -92,9 +105,48 @@ MessagesModel* Conversation::messages() const
return d->messages;
}
ConversationTarget* Conversation::target() const
QString Conversation::title() const
{
if (d->isGroupChat) {
QString roomName = textChannel()->targetId();
return roomName.left(roomName.indexOf(QLatin1Char('@')));
} else {
return d->targetContact->alias();
}
}
QIcon Conversation::presenceIcon() const
{
return d->target;
if (d->isGroupChat) {
return KTp::Presence(Tp::Presence::available()).icon();
} else {
return KTp::Presence(d->targetContact->presence()).icon();
}
}
QIcon Conversation::avatar() const
{
if (d->isGroupChat) {
return QIcon();
} else {
QString path = d->targetContact->avatarData().fileName;
if (path.isEmpty()) {
path = QLatin1String("im-user");
}
return KIcon(path);
}
}
KTp::ContactPtr Conversation::targetContact() const {
if (d->isGroupChat) {
return KTp::ContactPtr();
} else {
return d->targetContact;
}
}
Tp::AccountPtr Conversation::account() const {
return d->account;
}
bool Conversation::isValid()
......@@ -176,6 +228,21 @@ void Conversation::onChatPausedTimerExpired()
d->messages->textChannel()->requestChatState(Tp::ChannelChatStatePaused);
}
void Conversation::onTargetContactAvatarDataChanged()
{
Q_EMIT avatarChanged(avatar());
}
void Conversation::onTargetContactAliasChanged()
{
Q_EMIT titleChanged(title());
}
void Conversation::onTargetContactPresenceChanged()
{
Q_EMIT presenceIconChanged(presenceIcon());
}
Conversation::~Conversation()
{
kDebug();
......
......@@ -26,18 +26,20 @@
#include <KIcon>
#include "conversation-target.h"
#include "messages-model.h"
class ConversationTarget;
class MessagesModel;
class Conversation : public QObject
{
Q_OBJECT
Q_PROPERTY(ConversationTarget *target READ target CONSTANT)
Q_PROPERTY(MessagesModel *messages READ messages CONSTANT)
Q_PROPERTY(bool valid READ isValid NOTIFY validityChanged)
Q_PROPERTY(QString title READ title CONSTANT)
Q_PROPERTY(QIcon presenceIcon READ presenceIcon CONSTANT)
Q_PROPERTY(QIcon avatar READ avatar CONSTANT)
Q_PROPERTY(Tp::AccountPtr account READ account CONSTANT)
Q_PROPERTY(KTp::ContactPtr targetContact READ targetContact CONSTANT)
public:
Conversation(const Tp::TextChannelPtr &channel, const Tp::AccountPtr &account, QObject *parent = 0);
......@@ -48,12 +50,23 @@ public:
Tp::TextChannelPtr textChannel() const;
MessagesModel* messages() const;
ConversationTarget* target() const;
QString title() const;
QIcon presenceIcon() const;
QIcon avatar() const;
/**
* Target contact of this conversation. May be null if conversation is a group chat.
*/
KTp::ContactPtr targetContact() const;
Tp::AccountPtr account() const;
bool isValid();
Q_SIGNALS:
void validityChanged(bool isValid);
void avatarChanged(QIcon avatar);
void titleChanged(QString title);
void presenceIconChanged(QIcon icon);
void conversationCloseRequested();
public Q_SLOTS:
......@@ -66,12 +79,15 @@ private Q_SLOTS:
void onAccountConnectionChanged(const Tp::ConnectionPtr &connection);
void onCreateChannelFinished(Tp::PendingOperation *op);
void onChatPausedTimerExpired();
void onTargetContactAvatarDataChanged();
void onTargetContactAliasChanged();
void onTargetContactPresenceChanged();
private:
class ConversationPrivate;
ConversationPrivate *d;
};
Q_DECLARE_METATYPE(Conversation*);
Q_DECLARE_METATYPE(Conversation*)
#endif // CONVERSATION_H
......@@ -19,7 +19,6 @@
#include "conversations-model.h"
#include "conversation.h"
#include "conversation-target.h"
#include "messages-model.h"
#include <KDebug>
......@@ -115,7 +114,7 @@ void ConversationsModel::handleChannels(const Tp::MethodInvocationContextPtr<> &
//if we are handling but should delegate, call delegate channel
int i = 0;
Q_FOREACH(Conversation *convo, d->conversations) {
if (convo->target()->id() == textChannel->targetId() &&
if (convo->textChannel()->targetId() == textChannel->targetId() &&
convo->textChannel()->targetHandleType() == textChannel->targetHandleType())
{
if (!shouldDelegate) {
......
......@@ -153,7 +153,7 @@ QVariant PinnedContactsModel::data(const QModelIndex &index, int role) const
bool found = false;
for (int i = 0; !found && i < d->conversations->rowCount(); i++) {
QModelIndex idx = d->conversations->index(i, 0);
KTp::ContactPtr contact = idx.data(ConversationsModel::ConversationRole).value<Conversation*>()->target()->contact();
KTp::ContactPtr contact = idx.data(ConversationsModel::ConversationRole).value<Conversation*>()->targetContact();
found |= contact->id() == p->contact()->id();
}
return found;
......@@ -248,7 +248,7 @@ void PinnedContactsModel::conversationsStateChanged(const QModelIndex &parent, i
for (int i = start; i <= end; i++) {
QModelIndex idx = d->conversations->index(i, 0, parent);
Conversation* conv = idx.data(ConversationsModel::ConversationRole).value<Conversation*>();
KTp::ContactPtr contact = conv->target()->contact();
KTp::ContactPtr contact = conv->targetContact();
Q_FOREACH(const KTp::PersistentContactPtr &p, d->m_pins) {
if (p->contact() == contact) {
QModelIndex contactIdx = indexForContact(p->contact());
......
......@@ -28,7 +28,6 @@
#include "conversation.h"
#include "conversations-model.h"
#include "conversation-target.h"
#include "hide-window-component.h"
#include "messages-model.h"
#include "pinned-contacts-model.h"
......@@ -65,7 +64,6 @@ void QmlPlugins::registerTypes(const char *uri)
QLatin1String("It will be created once the conversation is created"));
qmlRegisterType<TelepathyManager>();
qmlRegisterType<ConversationTarget>();
qmlRegisterType<ConversationsModel>();
qRegisterMetaType<Tp::AccountManagerPtr>();
qRegisterMetaType<KTp::ContactPtr>();
......
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