1 /*
2  * %kadu copyright begin%
3  * Copyright 2009, 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
4  * Copyright 2011 Bartosz Brachaczek (b.brachaczek@gmail.com)
5  * Copyright 2009, 2010, 2011, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
6  * %kadu copyright end%
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #pragma once
23 
24 #include "accounts/account.h"
25 #include "protocols/protocol-factory.h"
26 #include "protocols/protocol.h"
27 #include "storage/manager.h"
28 #include "exports.h"
29 
30 #include <QtCore/QMap>
31 #include <QtCore/QObject>
32 #include <QtCore/QUuid>
33 #include <injeqt/injeqt.h>
34 
35 class AccountStorage;
36 class BuddyManager;
37 class ChatManager;
38 class ConfigurationApi;
39 class ConfigurationManager;
40 class ContactManager;
41 class InjectedFactory;
42 class Myself;
43 class Status;
44 
45 class KADUAPI AccountManager : public Manager<Account>
46 {
47 	Q_OBJECT
48 
49 	QPointer<AccountStorage> m_accountStorage;
50 	QPointer<BuddyManager> m_buddyManager;
51 	QPointer<ChatManager> m_chatManager;
52 	QPointer<ConfigurationManager> m_configurationManager;
53 	QPointer<ContactManager> m_contactManager;
54 	QPointer<InjectedFactory> m_injectedFactory;
55 	QPointer<Myself> m_myself;
56 
57 private slots:
58 	INJEQT_SET void setAccountStorage(AccountStorage *accountStorage);
59 	INJEQT_SET void setBuddyManager(BuddyManager *buddyManager);
60 	INJEQT_SET void setChatManager(ChatManager *chatManager);
61 	INJEQT_SET void setConfigurationManager(ConfigurationManager *configurationManager);
62 	INJEQT_SET void setContactManager(ContactManager *contactManager);
63 	INJEQT_SET void setInjectedFactory(InjectedFactory *injectedFactory);
64 	INJEQT_SET void setMyself(Myself *myself);
65 	INJEQT_INIT void init();
66 	INJEQT_DONE void done();
67 
68 	void passwordProvided(const QVariant &data, const QString &password, bool permament);
69 
70 	void accountDataUpdated();
71 
72 protected:
73 	virtual Account loadStubFromStorage(const std::shared_ptr<StoragePoint> &storagePoint) override;
74 
75 	virtual void itemAdded(Account item) override;
76 	virtual void itemRemoved(Account item) override;
77 
78 	virtual void itemAboutToBeRegistered(Account item) override;
79 	virtual void itemRegistered(Account item) override;
80 	virtual void itemAboutToBeUnregisterd(Account item) override;
81 	virtual void itemUnregistered(Account item) override;
82 
83 	virtual void loaded() override;
84 
85 public:
86 	Q_INVOKABLE explicit AccountManager(QObject *parent = nullptr);
87 	virtual ~AccountManager();
88 
89 	template<template <class> class Container>
bestAccount(const Container<Account> & accounts)90 	static Account bestAccount(const Container<Account> &accounts)
91 	{
92 		Account result;
93 		if (accounts.isEmpty())
94 			return result;
95 
96 		foreach (const Account &account, accounts)
97 			if (account.details() && account.data())
98 			{
99 				// TODO: hack
100 				auto p = protocol(account);
101 				bool newConnected = p && p->isConnected();
102 				bool oldConnected = false;
103 				if (result)
104 					oldConnected = result.data()->protocolHandler() && result.data()->protocolHandler()->isConnected();
105 
106 				if (!result || (newConnected && !oldConnected)  || (account.protocolName() == QStringLiteral("gadu") && result.protocolName() != QStringLiteral("gadu")))
107 				{
108 					result = account;
109 					if (newConnected && result.protocolName() == QStringLiteral("gadu"))
110 						break;
111 				}
112 			}
113 
114 		return result;
115 	}
116 
storageNodeName()117 	virtual QString storageNodeName() override { return QStringLiteral("Accounts"); }
storageNodeItemName()118 	virtual QString storageNodeItemName() override { return QStringLiteral("Account"); }
119 
120 	Account defaultAccount();
121 	Account bestAccount();
122 
123 	const QVector<Account> byIdentity(Identity identity);
124 	Account byId(const QString &protocolName, const QString &id);
125 	const QVector<Account> byProtocolName(const QString &name);
126 
127 	void removeAccountAndBuddies(Account account);
128 
129 public slots:
130 	void providePassword(Account account);
131 
132 signals:
133 	void accountAboutToBeRegistered(Account);
134 	void accountRegistered(Account);
135 	void accountAboutToBeUnregistered(Account);
136 	void accountUnregistered(Account);
137 
138 	void accountAboutToBeRemoved(Account);
139 	void accountRemoved(Account);
140 
141 	void accountUpdated(Account);
142 
143 };
144