Commit 1f298f02 authored by Martin Klapetek's avatar Martin Klapetek Committed by Alexander Akulich
Browse files

[declarative] Refactor



First bigger (and much needed) refactor, here are the changes:
* Conversation can now exist without an open channel
* Conversation is created right away for every logger entity
* Make Conversation an uncreatable type
* The whole Conversation handling is greatly simplified
* KPeople::PersonData is now part of Conversation
* Some methods from Conversation are now removed (not needed)
Signed-off-by: Simon Schmeißer's avatarSimon Schmeisser <s.schmeisser@gmx.net>
Signed-off-by: Alexander Akulich's avatarAlexander Akulich <akulichalexander@gmail.com>
parent 5f651e05
......@@ -46,9 +46,9 @@ class Conversation::ConversationPrivate
//and not handling it.
bool delegated;
bool valid;
QString contactAlias;
Tp::AccountPtr account;
QTimer *pausedStateTimer;
KPeople::PersonData *personData;
// May be null for group chats.
KTp::ContactPtr targetContact;
bool isGroupChat;
......@@ -61,6 +61,8 @@ Conversation::Conversation(const Tp::TextChannelPtr &channel,
d (new ConversationPrivate)
{
qCDebug(KTP_DECLARATIVE);
d->valid = false;
d->isGroupChat = false;
d->account = account;
connect(d->account.data(), SIGNAL(connectionChanged(Tp::ConnectionPtr)), SLOT(onAccountConnectionChanged(Tp::ConnectionPtr)));
......@@ -77,13 +79,26 @@ Conversation::Conversation(const Tp::TextChannelPtr &channel,
connect(d->pausedStateTimer, SIGNAL(timeout()), this, SLOT(onChatPausedTimerExpired()));
}
Conversation::Conversation(QObject *parent)
Conversation::Conversation(const QString &contactId,
const Tp::AccountPtr &account,
QObject *parent)
: QObject(parent),
d(new ConversationPrivate)
{
d->messages = new MessagesModel(Tp::AccountPtr(), this);
d->valid = true;
d->isGroupChat = false;
d->account = account;
d->personData = new KPeople::PersonData(QStringLiteral("ktp://") + d->account->objectPath().mid(35) + QStringLiteral("?") + contactId);
d->messages = new MessagesModel(account, this);
connect(d->messages, &MessagesModel::unreadCountChanged, this, &Conversation::unreadMessagesChanged);
connect(d->messages, &MessagesModel::lastMessageChanged, this, &Conversation::lastMessageChanged);
d->messages->setContactData(contactId, d->personData->name());
Q_EMIT avatarChanged();
Q_EMIT titleChanged();
Q_EMIT presenceIconChanged();
Q_EMIT validityChanged(d->valid);
}
void Conversation::setTextChannel(const Tp::TextChannelPtr &channel)
......@@ -102,6 +117,7 @@ void Conversation::setTextChannel(const Tp::TextChannelPtr &channel)
} else {
d->isGroupChat = false;
d->targetContact = KTp::ContactPtr::qObjectCast(channel->targetContact());
d->personData = new KPeople::PersonData(QStringLiteral("ktp://") + d->account->objectPath().mid(35) + QStringLiteral("?") + d->targetContact->id());
connect(d->targetContact.constData(), SIGNAL(aliasChanged(QString)), SIGNAL(titleChanged()));
connect(d->targetContact.constData(), SIGNAL(presenceChanged(Tp::Presence)), SIGNAL(presenceIconChanged()));
......@@ -115,20 +131,6 @@ void Conversation::setTextChannel(const Tp::TextChannelPtr &channel)
}
}
void Conversation::setContactData(const QString &contactId, const QString &contactAlias)
{
if (!d->messages) {
qWarning() << "Needs messages model first!";
return;
}
qDebug() << "Setting contact data";
d->contactAlias = contactAlias;
d->messages->setContactData(contactId, contactAlias);
Q_EMIT titleChanged();
}
Tp::TextChannelPtr Conversation::textChannel() const
{
return d->messages->textChannel();
......@@ -144,11 +146,9 @@ QString Conversation::title() const
if (d->isGroupChat) {
QString roomName = textChannel()->targetId();
return roomName.left(roomName.indexOf(QLatin1Char('@')));
} else if (!d->targetContact.isNull()) {
return d->targetContact->alias();
} else {
return d->personData->name();
}
return d->contactAlias;
}
QIcon Conversation::presenceIcon() const
......@@ -202,12 +202,6 @@ Tp::Account* Conversation::accountObject() const
return 0;
}
void Conversation::setAccount(const Tp::AccountPtr &account)
{
d->messages->setAccount(account);
d->account = account;
}
bool Conversation::isValid() const
{
return d->valid;
......@@ -306,3 +300,8 @@ bool Conversation::hasUnreadMessages() const
return false;
}
KPeople::PersonData* Conversation::personData() const
{
return d->personData;
}
......@@ -26,6 +26,8 @@
#include <TelepathyQt/Account>
#include <TelepathyQt/TextChannel>
#include <KPeople/PersonData>
#include "messages-model.h"
class MessagesModel;
......@@ -40,11 +42,12 @@ class Conversation : public QObject
Q_PROPERTY(QIcon avatar READ avatar NOTIFY avatarChanged)
Q_PROPERTY(Tp::Account *account READ accountObject CONSTANT)
Q_PROPERTY(KTp::ContactPtr targetContact READ targetContact CONSTANT)
Q_PROPERTY(KPeople::PersonData *personData READ personData CONSTANT)
Q_PROPERTY(bool hasUnreadMessages READ hasUnreadMessages NOTIFY unreadMessagesChanged)
public:
Conversation(const Tp::TextChannelPtr &channel, const Tp::AccountPtr &account, QObject *parent = 0);
Conversation(QObject *parent = 0);
Conversation(const QString &contactId, const Tp::AccountPtr &account, QObject *parent = 0);
~Conversation() override;
void setTextChannel(const Tp::TextChannelPtr &channel);
......@@ -67,7 +70,8 @@ public:
Tp::AccountPtr account() const;
Tp::Account* accountObject() const;
void setAccount(const Tp::AccountPtr &account);
KPeople::PersonData* personData() const;
bool isValid() const;
bool hasUnreadMessages() const;
......
......@@ -55,7 +55,6 @@ void QmlPlugins::registerTypes(const char *uri)
qmlRegisterType<KTp::AccountsListModel> (uri, 0, 1, "AccountsListModel");
qmlRegisterType<ConversationsModel> (uri, 0, 1, "ConversationsModel");
qmlRegisterType<Conversation>(uri, 0, 1, "Conversation");
qmlRegisterType<PinnedContactsModel>(uri, 0, 1, "PinnedContactsModel");
qmlRegisterType<ContactPin>(uri, 0, 1, "ContactPin");
qmlRegisterType<FilteredPinnedContactsProxyModel>(uri, 0, 1, "FilteredPinnedContactsProxyModel");
......@@ -67,7 +66,8 @@ void QmlPlugins::registerTypes(const char *uri)
QLatin1String("It will be created once the conversation is created"));
qmlRegisterType<TelepathyManager>();
qmlRegisterType<ConversationsModel>();
qmlRegisterUncreatableType<ConversationsModel>(uri, 0, 1, "Conversation",
QStringLiteral("Conversation type cannot be created from QML"));
qmlRegisterType<Tp::PendingChannelRequest>();
qmlRegisterType<Tp::PendingOperation>();
qRegisterMetaType<Tp::Presence>();
......
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