Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Verified Commit 80023b26 authored by Jonah Brüchert's avatar Jonah Brüchert 🌳

Refactor RosterManager

parent c7c9f0a0
Pipeline #25268 canceled with stages
......@@ -46,33 +46,33 @@ RosterManager::RosterManager(Kaidan *kaidan,
VCardManager *vCardManager,
QObject *parent)
: QObject(parent),
kaidan(kaidan),
client(client),
model(model),
avatarStorage(avatarStorage),
vCardManager(vCardManager),
manager(client->findExtension<QXmppRosterManager>())
m_kaidan(kaidan),
m_client(client),
m_model(model),
m_avatarStorage(avatarStorage),
m_vCardManager(vCardManager),
m_manager(client->findExtension<QXmppRosterManager>())
{
connect(manager, &QXmppRosterManager::rosterReceived,
connect(m_manager, &QXmppRosterManager::rosterReceived,
this, &RosterManager::populateRoster);
connect(manager, &QXmppRosterManager::itemAdded,
connect(m_manager, &QXmppRosterManager::itemAdded,
this, [this, vCardManager, model] (const QString &jid) {
emit model->addItemRequested(RosterItem(manager->getRosterEntry(jid)));
emit model->addItemRequested(RosterItem(m_manager->getRosterEntry(jid)));
vCardManager->fetchVCard(jid);
});
connect(manager, &QXmppRosterManager::itemChanged,
connect(m_manager, &QXmppRosterManager::itemChanged,
this, [this, model] (const QString &jid) {
emit model->updateItemRequested(jid, [=] (RosterItem &item) {
item.setName(manager->getRosterEntry(jid).name());
item.setName(m_manager->getRosterEntry(jid).name());
});
});
connect(manager, &QXmppRosterManager::itemRemoved, model, &RosterModel::removeItemRequested);
connect(m_manager, &QXmppRosterManager::itemRemoved, model, &RosterModel::removeItemRequested);
connect(manager, &QXmppRosterManager::subscriptionReceived,
connect(m_manager, &QXmppRosterManager::subscriptionReceived,
this, [kaidan] (const QString &jid) {
// emit signal to ask user
emit kaidan->subscriptionRequestReceived(jid, QString());
......@@ -80,14 +80,14 @@ RosterManager::RosterManager(Kaidan *kaidan,
connect(kaidan, &Kaidan::subscriptionRequestAnswered,
this, [=] (QString jid, bool accepted) {
if (accepted) {
manager->acceptSubscription(jid);
m_manager->acceptSubscription(jid);
// do not send a subscription request if both users have already subscribed
// each others presence
if (manager->getRosterEntry(jid).subscriptionType() != QXmppRosterIq::Item::Both)
manager->subscribe(jid);
if (m_manager->getRosterEntry(jid).subscriptionType() != QXmppRosterIq::Item::Both)
m_manager->subscribe(jid);
} else {
manager->refuseSubscription(jid);
m_manager->refuseSubscription(jid);
}
});
......@@ -107,17 +107,17 @@ void RosterManager::populateRoster()
qDebug() << "[client] [RosterManager] Populating roster";
// create a new list of contacts
QHash<QString, RosterItem> items;
const QStringList bareJids = manager->getRosterBareJids();
const QStringList bareJids = m_manager->getRosterBareJids();
const auto currentTime = QDateTime::currentDateTimeUtc();
for (const auto &jid : bareJids) {
items[jid] = RosterItem(manager->getRosterEntry(jid), currentTime);
items[jid] = RosterItem(m_manager->getRosterEntry(jid), currentTime);
if (avatarStorage->getHashOfJid(jid).isEmpty())
vCardManager->fetchVCard(jid);
if (m_avatarStorage->getHashOfJid(jid).isEmpty())
m_vCardManager->fetchVCard(jid);
}
// replace current contacts with new ones from server
emit model->replaceItemsRequested(items);
emit m_model->replaceItemsRequested(items);
}
void RosterManager::setCurrentChatJid(const QString &currentChatJid)
......@@ -127,11 +127,11 @@ void RosterManager::setCurrentChatJid(const QString &currentChatJid)
void RosterManager::addContact(const QString &jid, const QString &name, const QString &msg)
{
if (client->state() == QXmppClient::ConnectedState) {
manager->addItem(jid, name);
manager->subscribe(jid, msg);
if (m_client->state() == QXmppClient::ConnectedState) {
m_manager->addItem(jid, name);
m_manager->subscribe(jid, msg);
} else {
emit kaidan->passiveNotificationRequested(
emit m_kaidan->passiveNotificationRequested(
tr("Could not add contact, as a result of not being connected.")
);
qWarning() << "[client] [RosterManager] Could not add contact, as a result of "
......@@ -141,11 +141,11 @@ void RosterManager::addContact(const QString &jid, const QString &name, const QS
void RosterManager::removeContact(const QString &jid)
{
if (client->state() == QXmppClient::ConnectedState) {
manager->unsubscribe(jid);
manager->removeItem(jid);
if (m_client->state() == QXmppClient::ConnectedState) {
m_manager->unsubscribe(jid);
m_manager->removeItem(jid);
} else {
emit kaidan->passiveNotificationRequested(
emit m_kaidan->passiveNotificationRequested(
tr("Could not remove contact, as a result of not being connected.")
);
qWarning() << "[client] [RosterManager] Could not remove contact, as a result of "
......@@ -155,10 +155,10 @@ void RosterManager::removeContact(const QString &jid)
void RosterManager::renameContact(const QString &jid, const QString &newContactName)
{
if (client->state() == QXmppClient::ConnectedState) {
manager->renameItem(jid, newContactName);
if (m_client->state() == QXmppClient::ConnectedState) {
m_manager->renameItem(jid, newContactName);
} else {
emit kaidan->passiveNotificationRequested(
emit m_kaidan->passiveNotificationRequested(
tr("Could not rename contact, as a result of not being connected.")
);
qWarning() << "[client] [RosterManager] Could not rename contact, as a result of "
......@@ -169,15 +169,15 @@ void RosterManager::renameContact(const QString &jid, const QString &newContactN
void RosterManager::handleSendMessage(const QString &jid, const QString &message,
bool isSpoiler, const QString &spoilerHint)
{
if (client->state() == QXmppClient::ConnectedState) {
if (m_client->state() == QXmppClient::ConnectedState) {
// update roster item
const QString lastMessage =
isSpoiler ? spoilerHint.isEmpty() ? tr("Spoiler")
: spoilerHint
: message;
// sorting order in contact list
emit model->setLastExchangedRequested(jid, QDateTime::currentDateTimeUtc());
emit model->setLastMessageRequested(jid, lastMessage);
emit m_model->setLastExchangedRequested(jid, QDateTime::currentDateTimeUtc());
emit m_model->setLastMessageRequested(jid, lastMessage);
}
}
......@@ -188,21 +188,21 @@ void RosterManager::handleMessage(const QXmppMessage &msg)
// msg.from() can be our JID, if it's a carbon/forward from another client
QString fromJid = QXmppUtils::jidToBareJid(msg.from());
bool sentByMe = fromJid == client->configuration().jidBare();
bool sentByMe = fromJid == m_client->configuration().jidBare();
QString contactJid = sentByMe ? QXmppUtils::jidToBareJid(msg.to())
: fromJid;
// update last exchanged datetime (sorting order in contact list)
emit model->setLastExchangedRequested(contactJid, QDateTime::currentDateTimeUtc());
emit m_model->setLastExchangedRequested(contactJid, QDateTime::currentDateTimeUtc());
// update unread message counter, if chat is not active
if (sentByMe) {
// if we sent a message (with another device), reset counter
emit model->updateItemRequested(contactJid, [](RosterItem &item) {
emit m_model->updateItemRequested(contactJid, [](RosterItem &item) {
item.setUnreadMessages(0);
});
} else if (m_currentChatJid != contactJid) {
emit model->updateItemRequested(contactJid, [](RosterItem &item) {
emit m_model->updateItemRequested(contactJid, [](RosterItem &item) {
item.setUnreadMessages(item.unreadMessages() + 1);
});
}
......
......@@ -49,7 +49,7 @@ class RosterManager : public QObject
public:
RosterManager(Kaidan *kaidan, QXmppClient *client, RosterModel *rosterModel,
AvatarFileStorage *avatarStorage, VCardManager *vCardManager,
AvatarFileStorage *avatarStorage, VCardManager *vCardManager,
QObject *parent = nullptr);
public slots:
......@@ -65,13 +65,13 @@ private slots:
void handleMessage(const QXmppMessage &msg);
private:
Kaidan *kaidan;
QXmppClient *client;
RosterModel *model;
AvatarFileStorage *avatarStorage;
VCardManager *vCardManager;
QXmppRosterManager *manager;
QString m_currentChatJid;
Kaidan *m_kaidan;
QXmppClient *m_client;
RosterModel *m_model;
AvatarFileStorage *m_avatarStorage;
VCardManager *m_vCardManager;
QXmppRosterManager *m_manager;
QString m_chatPartner;
};
#endif // ROSTERMANAGER_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