RosterManager.cpp 6.73 KB
Newer Older
1 2
/*
 *  Kaidan - A user-friendly XMPP client for every device!
Linus Jahn's avatar
Linus Jahn committed
3
 *
Linus Jahn's avatar
Linus Jahn committed
4
 *  Copyright (C) 2016-2019 Kaidan developers and contributors
Linus Jahn's avatar
Linus Jahn committed
5 6
 *  (see the LICENSE file for a full list of copyright authors)
 *
7 8 9 10
 *  Kaidan is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
Linus Jahn's avatar
Linus Jahn committed
11 12 13 14 15 16 17 18 19 20 21
 *
 *  In addition, as a special exception, the author of Kaidan gives
 *  permission to link the code of its release with the OpenSSL
 *  project's "OpenSSL" library (or with modified versions of it that
 *  use the same license as the "OpenSSL" library), and distribute the
 *  linked executables. You must obey the GNU General Public License in
 *  all respects for all of the code used other than "OpenSSL". If you
 *  modify this file, you may extend this exception to your version of
 *  the file, but you are not obligated to do so.  If you do not wish to
 *  do so, delete this exception statement from your version.
 *
22 23 24 25
 *  Kaidan 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 General Public License for more details.
Linus Jahn's avatar
Linus Jahn committed
26
 *
27
 *  You should have received a copy of the GNU General Public License
Linus Jahn's avatar
Linus Jahn committed
28
 *  along with Kaidan.  If not, see <http://www.gnu.org/licenses/>.
29 30 31
 */

#include "RosterManager.h"
32 33
// Kaidan
#include "ClientWorker.h"
34
#include "Globals.h"
35
#include "Kaidan.h"
Linus Jahn's avatar
Linus Jahn committed
36
#include "VCardManager.h"
Linus Jahn's avatar
Linus Jahn committed
37
// QXmpp
38 39
#include <QXmppClient.h>
#include <QXmppRosterManager.h>
40
#include <QXmppUtils.h>
41

42 43 44 45 46 47 48 49 50 51 52 53 54
RosterManager::RosterManager(Kaidan *kaidan,
                             QXmppClient *client,
                             RosterModel *model,
                             AvatarFileStorage *avatarStorage,
                             VCardManager *vCardManager,
                             QObject *parent)
    : QObject(parent),
      kaidan(kaidan),
      client(client),
      model(model),
      avatarStorage(avatarStorage),
      vCardManager(vCardManager),
      manager(client->rosterManager())
55
{
56 57
	connect(&manager, &QXmppRosterManager::rosterReceived,
	        this, &RosterManager::populateRoster);
58

59 60 61
	connect(&manager, &QXmppRosterManager::itemAdded,
	        [this, vCardManager, model] (const QString &jid) {
		emit model->addItemRequested(RosterItem(manager.getRosterEntry(jid)));
62

Linus Jahn's avatar
Linus Jahn committed
63
		vCardManager->fetchVCard(jid);
64 65
	});

66 67 68 69 70 71
	connect(&manager, &QXmppRosterManager::itemChanged,
	        this, [this, model] (QString jid) {
		emit model->updateItemRequested(m_chatPartner,
		                                [this, &jid] (RosterItem &item) {
			item.setName(manager.getRosterEntry(jid).name());
		});
72 73
	});

74
	connect(&manager, &QXmppRosterManager::itemRemoved, model, &RosterModel::removeItemRequested);
75

76 77
	connect(&manager, &QXmppRosterManager::subscriptionReceived,
	        this, [kaidan] (const QString &jid) {
78 79 80
		// emit signal to ask user
		emit kaidan->subscriptionRequestReceived(jid, QString());
	});
81 82
	connect(kaidan, &Kaidan::subscriptionRequestAnswered,
	        this, [=] (QString jid, bool accepted) {
83 84 85 86 87 88
		if (accepted)
			manager.acceptSubscription(jid);
		else
			manager.refuseSubscription(jid);
	});

Linus Jahn's avatar
Linus Jahn committed
89
	// user actions
90 91
	connect(kaidan, &Kaidan::addContact, this, &RosterManager::addContact);
	connect(kaidan, &Kaidan::removeContact, this, &RosterManager::removeContact);
92
	connect(kaidan, &Kaidan::sendMessage, this, &RosterManager::handleSendMessage);
93

Linus Jahn's avatar
Linus Jahn committed
94
	connect(client, &QXmppClient::messageReceived, this, &RosterManager::handleMessage);
95 96
}

97 98 99 100
void RosterManager::populateRoster()
{
	qDebug() << "[client] [RosterManager] Populating roster";
	// create a new list of contacts
101 102 103
	QHash<QString, RosterItem> items;
	for (const auto &jid : manager.getRosterBareJids()) {
		items[jid] = RosterItem(manager.getRosterEntry(jid));
104

Linus Jahn's avatar
Linus Jahn committed
105 106
		if (avatarStorage->getHashOfJid(jid).isEmpty())
			vCardManager->fetchVCard(jid);
107 108
	}

109 110 111 112
	if (!items.isEmpty()) {
		// replace current contacts with new ones from server
		emit model->replaceItemsRequested(items);
	}
113 114
}

115
void RosterManager::addContact(const QString &jid, const QString &name, const QString &msg)
116
{
117
	if (client->state() == QXmppClient::ConnectedState) {
Linus Jahn's avatar
Linus Jahn committed
118
		manager.addItem(jid, name);
119 120 121 122 123 124 125 126
		manager.subscribe(jid, msg);
	} else {
		emit 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 "
		              "not being connected.";
	}
127 128
}

129
void RosterManager::removeContact(const QString &jid)
130
{
131 132
	if (client->state() == QXmppClient::ConnectedState) {
		manager.unsubscribe(jid);
133
		manager.removeItem(jid);
134 135 136 137 138 139 140
	} else {
		emit 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 "
		              "not being connected.";
	}
141
}
Linus Jahn's avatar
Linus Jahn committed
142

Xavier's avatar
Xavier committed
143
void RosterManager::handleSendMessage(const QString &jid, const QString &message,
144
                                      bool isSpoiler, const QString &spoilerHint)
145 146
{
	if (client->state() == QXmppClient::ConnectedState) {
147 148 149 150 151 152 153 154 155 156 157 158 159
		// update roster item
		const QString lastMessage =
		        isSpoiler ? spoilerHint.isEmpty() ? tr("Spoiler")
		                                          : spoilerHint
		                  : message;
		// sorting order in contact list
		const QDateTime dateTime = QDateTime::currentDateTimeUtc();

		emit model->updateItemRequested(jid,
		                                [=] (RosterItem &item) {
			item.setLastMessage(lastMessage);
			item.setLastExchanged(dateTime);
		});
160 161 162
	}
}

Linus Jahn's avatar
Linus Jahn committed
163 164
void RosterManager::handleMessage(const QXmppMessage &msg)
{
Linus Jahn's avatar
Linus Jahn committed
165
    if (msg.body().isEmpty() || msg.type() == QXmppMessage::Error)
Linus Jahn's avatar
Linus Jahn committed
166 167 168
		return;

	// msg.from() can be our JID, if it's a carbon/forward from another client
169 170
	QString fromJid = QXmppUtils::jidToBareJid(msg.from());
	bool sentByMe = fromJid == client->configuration().jidBare();
Linus Jahn's avatar
Linus Jahn committed
171
	QString contactJid = sentByMe ? QXmppUtils::jidToBareJid(msg.to())
172
	                              : fromJid;
Linus Jahn's avatar
Linus Jahn committed
173 174

	// update last exchanged datetime (sorting order in contact list)
175
	const QDateTime dateTime = QDateTime::currentDateTimeUtc();
Linus Jahn's avatar
Linus Jahn committed
176

177
	// update unread message counter, if chat is not active
178 179 180 181 182 183 184 185 186 187 188 189 190 191
	if (sentByMe) {
		// if we sent a message (with another device), reset counter
		emit model->updateItemRequested(contactJid,
		                                [dateTime] (RosterItem &item) {
			item.setLastExchanged(dateTime);
			item.setUnreadMessages(0);
		});
	} else if (m_chatPartner != contactJid) {
		emit model->updateItemRequested(contactJid,
		                                [dateTime] (RosterItem &item) {
			item.setLastExchanged(dateTime);
			item.setUnreadMessages(item.unreadMessages() + 1);
		});
	}
Linus Jahn's avatar
Linus Jahn committed
192
}