1 /*
2  * %kadu copyright begin%
3  * Copyright 2015 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
4  * %kadu copyright end%
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "jabber-resource-service.h"
21 
22 #include "chat/chat.h"
23 #include "chat/chat-details-room.h"
24 #include "chat/type/chat-type-manager.h"
25 #include "contacts/contact.h"
26 #include "contacts/contact-set.h"
27 
JabberResourceService(QObject * parent)28 JabberResourceService::JabberResourceService(QObject *parent) :
29 		QObject{parent}
30 {
31 }
32 
~JabberResourceService()33 JabberResourceService::~JabberResourceService()
34 {
35 }
36 
setChatTypeManager(ChatTypeManager * chatTypeManager)37 void JabberResourceService::setChatTypeManager(ChatTypeManager *chatTypeManager)
38 {
39 	m_chatTypeManager = chatTypeManager;
40 }
41 
updateResource(JabberResource resource)42 void JabberResourceService::updateResource(JabberResource resource)
43 {
44 	auto it = std::find_if(std::begin(m_resources), std::end(m_resources), [&resource](const JabberResource &item) {
45 		return item.jid().full() == resource.jid().full();
46 	});
47 
48 	if (it == std::end(m_resources))
49 		m_resources.append(resource);
50 	else
51 		*it = resource;
52 }
53 
removeResource(const Jid & jid)54 void JabberResourceService::removeResource(const Jid& jid)
55 {
56 	auto it = std::find_if(std::begin(m_resources), std::end(m_resources), [&jid](const JabberResource &item) {
57 		return item.jid().full() == jid.full();
58 	});
59 
60 	if (it != std::end(m_resources))
61 		m_resources.erase(it);
62 }
63 
removeResources(const QString & bareJid)64 void JabberResourceService::removeResources(const QString& bareJid)
65 {
66 	auto it = std::begin(m_resources);
67 	while (it != std::end(m_resources))
68 		if (it->jid().bare() == bareJid)
69 			it = m_resources.erase(it);
70 }
71 
clear()72 void JabberResourceService::clear()
73 {
74 	m_resources.clear();
75 }
76 
bestResource(const QString & bareJid) const77 JabberResource JabberResourceService::bestResource(const QString &bareJid) const
78 {
79 	auto result = JabberResource{};
80 	for (auto &&resource : m_resources)
81 		if (resource.jid().bare() == bareJid && resource.priority() > result.priority())
82 			result = resource;
83 	return result;
84 }
85 
bestChatJid(const Chat & chat) const86 Jid JabberResourceService::bestChatJid(const Chat &chat) const
87 {
88 	if (!chat)
89 		return Jid{};
90 
91 	auto chatType = m_chatTypeManager->chatType(chat.type());
92 	if (!chatType)
93 		return Jid{};
94 
95 	if (chatType->name() == "Contact")
96 	{
97 		Q_ASSERT(1 == chat.contacts().size());
98 		return bestContactJid(chat.contacts().toContact());
99 	}
100 
101 	if (chatType->name() == "Room")
102 	{
103 		auto details = qobject_cast<ChatDetailsRoom *>(chat.details());
104 		Q_ASSERT(details);
105 
106 		return Jid::parse(details->room());
107 	}
108 
109 	return Jid{};
110 }
111 
bestContactJid(const Contact & contact) const112 Jid JabberResourceService::bestContactJid(const Contact &contact) const
113 {
114 	if (!contact)
115 		return Jid{};
116 
117 	auto resource = contact.property("jabber:chat-resource", QString{}).toString();
118 	if (resource.isEmpty())
119 		resource = bestResource(contact.id()).jid().resource();
120 	return Jid::parse(contact.id()).withResource(resource);
121 }
122 
123 #include "moc_jabber-resource-service.cpp"
124