1 /*
2  * %kadu copyright begin%
3  * Copyright 2010, 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
4  * Copyright 2009 Wojciech Treter (juzefwt@gmail.com)
5  * Copyright 2012 Piotr Dąbrowski (ultr@ultr.pl)
6  * Copyright 2009 Bartłomiej Zimoń (uzi18@o2.pl)
7  * Copyright 2010, 2011, 2012, 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
8  * Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
9  * %kadu copyright end%
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #include "protocol.h"
26 
27 #include "accounts/account-manager.h"
28 #include "buddies/buddy-manager.h"
29 #include "chat/chat-manager.h"
30 #include "contacts/contact-manager.h"
31 #include "contacts/contact-set.h"
32 #include "contacts/contact.h"
33 #include "core/session-service.h"
34 #include "icons/icons-manager.h"
35 #include "icons/kadu-icon.h"
36 #include "plugin/plugin-injected-factory.h"
37 #include "protocols/protocol-factory.h"
38 #include "protocols/protocol-state-machine.h"
39 #include "roster/roster-service.h"
40 #include "services/chat-service.h"
41 #include "services/chat-state-service.h"
42 #include "status/status-adapter.h"
43 #include "status/status-type-manager.h"
44 #include "status/status.h"
45 #include "debug.h"
46 
47 #include <QtGui/QTextDocument>
48 
Protocol(Account account,ProtocolFactory * factory)49 Protocol::Protocol(Account account, ProtocolFactory *factory) :
50 		Factory(factory), CurrentAccount(account)
51 {
52 }
53 
~Protocol()54 Protocol::~Protocol()
55 {
56 }
57 
setChatService(ChatService * const chatService)58 void Protocol::setChatService(ChatService * const chatService)
59 {
60 	m_chatService = chatService;
61 }
62 
setChatStateService(ChatStateService * const chatStateService)63 void Protocol::setChatStateService(ChatStateService * const chatStateService)
64 {
65 	m_chatStateService = chatStateService;
66 }
67 
setContactManager(ContactManager * contactManager)68 void Protocol::setContactManager(ContactManager *contactManager)
69 {
70 	m_contactManager = contactManager;
71 }
72 
setPluginInjectedFactory(PluginInjectedFactory * pluginInjectedFactory)73 void Protocol::setPluginInjectedFactory(PluginInjectedFactory *pluginInjectedFactory)
74 {
75 	m_pluginInjectedFactory = pluginInjectedFactory;
76 }
77 
setSessionService(SessionService * sessionService)78 void Protocol::setSessionService(SessionService *sessionService)
79 {
80 	m_sessionService = sessionService;
81 }
82 
setRosterService(RosterService * const rosterService)83 void Protocol::setRosterService(RosterService * const rosterService)
84 {
85 	m_rosterService = rosterService;
86 }
87 
setStatusTypeManager(StatusTypeManager * statusTypeManager)88 void Protocol::setStatusTypeManager(StatusTypeManager *statusTypeManager)
89 {
90 	m_statusTypeManager = statusTypeManager;
91 }
92 
init()93 void Protocol::init()
94 {
95 	Machine = m_pluginInjectedFactory->makeInjected<ProtocolStateMachine>(this);
96 	/*
97 	 * after machine is started we need to re-call changeStatus
98 	 * so proper transition can be called
99 	 *
100 	 * changeStatus was probably called before machine was started by some StatusContainer
101 	 * that just restored status from configuration file
102 	 */
103 	connect(Machine, SIGNAL(started()), this, SLOT(prepareStateMachine()), Qt::QueuedConnection);
104 
105 	connect(Machine, SIGNAL(loggingInStateEntered()), this, SLOT(loggingInStateEntered()));
106 	connect(Machine, SIGNAL(loggedInStateEntered()), this, SLOT(loggedInStateEntered()));
107 	connect(Machine, SIGNAL(loggingOutStateEntered()), this, SLOT(loggingOutStateEntered()));
108 	connect(Machine, SIGNAL(loggedOutOnlineStateEntered()), this, SLOT(loggedOutAnyStateEntered()));
109 	connect(Machine, SIGNAL(loggedOutOfflineStateEntered()), this, SLOT(loggedOutAnyStateEntered()));
110 	connect(Machine, SIGNAL(wantToLogInStateEntered()), this, SLOT(wantToLogInStateEntered()));
111 	connect(Machine, SIGNAL(passwordRequiredStateEntered()), this, SLOT(passwordRequiredStateEntered()));
112 }
113 
contactManager() const114 ContactManager * Protocol::contactManager() const
115 {
116 	return m_contactManager;
117 }
118 
pluginInjectedFactory() const119 PluginInjectedFactory *Protocol::pluginInjectedFactory() const
120 {
121 	return m_pluginInjectedFactory;
122 }
123 
statusTypeManager() const124 StatusTypeManager * Protocol::statusTypeManager() const
125 {
126 	return m_statusTypeManager;
127 }
128 
chatService()129 ChatService * Protocol::chatService()
130 {
131 	return m_chatService.data();
132 }
133 
chatStateService()134 ChatStateService * Protocol::chatStateService()
135 {
136 	return m_chatStateService.data();
137 }
138 
rosterService() const139 RosterService * Protocol::rosterService() const
140 {
141 	return m_rosterService.data();
142 }
143 
icon()144 KaduIcon Protocol::icon()
145 {
146 	return Factory->icon();
147 }
148 
prepareStateMachine()149 void Protocol::prepareStateMachine()
150 {
151 	if (!CurrentStatus.isDisconnected())
152 		emit stateMachineChangeStatus();
153 }
154 
passwordProvided()155 void Protocol::passwordProvided()
156 {
157 	if (CurrentAccount.hasPassword())
158 	{
159 		emit stateMachinePasswordAvailable();
160 		return;
161 	}
162 
163 	LoginStatus = Status();
164 	emit stateMachinePasswordNotAvailable();
165 }
166 
setAllOffline()167 void Protocol::setAllOffline()
168 {
169 	if (m_sessionService->isClosing())
170 		return;
171 
172 	Status status;
173 	Status oldStatus;
174 
175 	foreach (const Contact &contact, m_contactManager->contacts(CurrentAccount))
176 	{
177 		oldStatus = contact.currentStatus();
178 
179 		if (oldStatus != status)
180 		{
181 			contact.setCurrentStatus(status);
182 			emit contactStatusChanged(contact, oldStatus);
183 		}
184 	}
185 }
186 
disconnectedCleanup()187 void Protocol::disconnectedCleanup()
188 {
189 	setAllOffline();
190 }
191 
setStatus(Status status,StatusChangeSource source)192 void Protocol::setStatus(Status status, StatusChangeSource source)
193 {
194 	if (SourceStatusChanger == source && !account().hasPassword())
195 		return;
196 
197 	LoginStatus = protocolFactory()->statusAdapter()->adapt(status);
198 	doSetStatus(LoginStatus);
199 }
200 
doSetStatus(Status status)201 void Protocol::doSetStatus(Status status)
202 {
203 	CurrentStatus = status;
204 
205 	if (!CurrentStatus.isDisconnected())
206 	{
207 		emit statusChanged(CurrentAccount, CurrentStatus);
208 		sendStatusToServer();
209 
210 		emit stateMachineChangeStatus();
211 	}
212 	else
213 		emit stateMachineLogout();
214 }
215 
loginStatus() const216 Status Protocol::loginStatus() const
217 {
218 	return LoginStatus;
219 }
220 
status() const221 Status Protocol::status() const
222 {
223 	return CurrentStatus;
224 }
225 
loggedIn()226 void Protocol::loggedIn()
227 {
228 	emit stateMachineLoggedIn();
229 }
230 
loggedOut()231 void Protocol::loggedOut()
232 {
233 	emit stateMachineLoggedOut();
234 }
235 
passwordRequired()236 void Protocol::passwordRequired()
237 {
238 	emit stateMachinePasswordRequired();
239 }
240 
connectionError()241 void Protocol::connectionError()
242 {
243 	statusChanged(Status());
244 
245 	emit stateMachineConnectionError();
246 }
247 
connectionClosed()248 void Protocol::connectionClosed()
249 {
250 	doSetStatus(Status());
251 	statusChanged(Status());
252 
253 	emit stateMachineConnectionClosed();
254 }
255 
sslError()256 void Protocol::sslError()
257 {
258 	statusChanged(Status());
259 
260 	emit stateMachineSslError();
261 }
262 
reconnect()263 void Protocol::reconnect()
264 {
265 	setStatus(loginStatus(), SourceUser);
266 }
267 
statusChanged(Status status)268 void Protocol::statusChanged(Status status)
269 {
270 	CurrentStatus = status;
271 	emit statusChanged(CurrentAccount, CurrentStatus);
272 }
273 
statusIcon()274 KaduIcon Protocol::statusIcon()
275 {
276 	return statusIcon(CurrentStatus);
277 }
278 
statusIcon(const Status & status)279 KaduIcon Protocol::statusIcon(const Status &status)
280 {
281 	return m_statusTypeManager->statusIcon(statusPixmapPath(), status);
282 }
283 
loggingInStateEntered()284 void Protocol::loggingInStateEntered()
285 {
286 	emit disconnected(CurrentAccount);
287 
288 	// this may be called from our connection error-handling code, when user wants to be logged in
289 	// at any cost, so we should assume that we were just disconnected
290 	// better do some cleanup then
291 	disconnectedCleanup();
292 
293 	if (!CurrentAccount.details() || account().id().isEmpty())
294 	{
295 		emit stateMachineConnectionClosed();
296 		return;
297 	}
298 
299 	if (!account().hasPassword())
300 	{
301 		emit stateMachinePasswordRequired();
302 		return;
303 	}
304 
305 	// just for status icon now, this signal need to be better
306 	emit statusChanged(CurrentAccount, CurrentStatus);
307 
308 	// call protocol implementation
309 	login();
310 }
311 
loggedInStateEntered()312 void Protocol::loggedInStateEntered()
313 {
314 	statusChanged(loginStatus());
315 	afterLoggedIn();
316 
317 	emit connected(CurrentAccount);
318 }
319 
loggingOutStateEntered()320 void Protocol::loggingOutStateEntered()
321 {
322 	emit disconnected(CurrentAccount);
323 
324 	// call protocol implementation
325 	logout();
326 }
327 
loggedOutAnyStateEntered()328 void Protocol::loggedOutAnyStateEntered()
329 {
330 	emit disconnected(CurrentAccount);
331 
332 	disconnectedCleanup();
333 	statusChanged(loginStatus());
334 }
335 
wantToLogInStateEntered()336 void Protocol::wantToLogInStateEntered()
337 {
338 	emit disconnected(CurrentAccount);
339 
340 	disconnectedCleanup();
341 	statusChanged(Status());
342 
343 	emit statusChanged(CurrentAccount, Status());
344 }
345 
passwordRequiredStateEntered()346 void Protocol::passwordRequiredStateEntered()
347 {
348 	emit disconnected(CurrentAccount);
349 
350 	disconnectedCleanup();
351 	statusChanged(Status());
352 
353 	emit invalidPassword(CurrentAccount);
354 }
355 
isConnected() const356 bool Protocol::isConnected() const
357 {
358 	return Machine->isLoggedIn();
359 }
360 
isConnecting() const361 bool Protocol::isConnecting() const
362 {
363 	return Machine->isLoggingIn();
364 }
365 
isDisconnecting() const366 bool Protocol::isDisconnecting() const
367 {
368 	return Machine->isLoggingOut();
369 }
370 
371 #include "moc_protocol.cpp"
372