1 /*
2 * %kadu copyright begin%
3 * Copyright 2014 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 extern "C" {
21 # include <libotr/instag.h>
22 }
23
24 #include "accounts/account-manager.h"
25 #include "chat/chat-manager.h"
26 #include "chat/chat-storage.h"
27 #include "chat/type/chat-type-contact.h"
28 #include "contacts/contact-manager.h"
29 #include "contacts/contact-set.h"
30
31 #include "otr-user-state-service.h"
32
33 #include "otr-context-converter.h"
34
OtrContextConverter(QObject * parent)35 OtrContextConverter::OtrContextConverter(QObject *parent) :
36 QObject{parent}
37 {
38 }
39
~OtrContextConverter()40 OtrContextConverter::~OtrContextConverter()
41 {
42 }
43
setAccountManager(AccountManager * accountManager)44 void OtrContextConverter::setAccountManager(AccountManager *accountManager)
45 {
46 m_accountManager = accountManager;
47 }
48
setChatManager(ChatManager * chatManager)49 void OtrContextConverter::setChatManager(ChatManager *chatManager)
50 {
51 m_chatManager = chatManager;
52 }
53
setChatStorage(ChatStorage * chatStorage)54 void OtrContextConverter::setChatStorage(ChatStorage *chatStorage)
55 {
56 m_chatStorage = chatStorage;
57 }
58
setContactManager(ContactManager * contactManager)59 void OtrContextConverter::setContactManager(ContactManager *contactManager)
60 {
61 m_contactManager = contactManager;
62 }
63
setUserStateService(OtrUserStateService * userStateService)64 void OtrContextConverter::setUserStateService(OtrUserStateService *userStateService)
65 {
66 m_userStateService = userStateService;
67 }
68
connectionContextToChat(ConnContext * context) const69 Chat OtrContextConverter::connectionContextToChat(ConnContext *context) const
70 {
71 auto contact = connectionContextToContact(context);
72 return ChatTypeContact::findChat(m_chatManager, m_chatStorage, contact, ActionCreateAndAdd);
73 }
74
connectionContextToContact(ConnContext * context) const75 Contact OtrContextConverter::connectionContextToContact(ConnContext *context) const
76 {
77 auto account = m_accountManager->byId(QString::fromUtf8(context->protocol), QString::fromUtf8(context->accountname));
78 return m_contactManager->byId(account, QString::fromUtf8(context->username), ActionReturnNull);
79 }
80
chatToContextConverter(const Chat & chat,NotFoundAction notFoundAction) const81 ConnContext * OtrContextConverter::chatToContextConverter(const Chat &chat, NotFoundAction notFoundAction) const
82 {
83 return chat
84 ? contactToContextConverter(chat.contacts().toContact(), notFoundAction)
85 : nullptr;
86 }
87
contactToContextConverter(const Contact & contact,NotFoundAction notFoundAction) const88 ConnContext * OtrContextConverter::contactToContextConverter(const Contact &contact, NotFoundAction notFoundAction) const
89 {
90 if (!contact)
91 return 0;
92
93 return otrl_context_find(m_userStateService->userState(), qPrintable(contact.id()), qPrintable(contact.contactAccount().id()),
94 qPrintable(contact.contactAccount().protocolName()), OTRL_INSTAG_BEST, notFoundAction == ActionCreateAndAdd,
95 0, 0, 0);
96 }
97
98 #include "moc_otr-context-converter.cpp"
99