summaryrefslogtreecommitdiffstats
blob: 87ec94d09161d998c567cfff187da913008a0ecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
 * Copyright (c) 2010-2012 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

/*
 * Copyright (c) 2013 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swift/Controllers/MainController.h>

#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <string>
#include <stdlib.h>

#include <Swiften/Base/format.h>
#include <Swiften/Base/Algorithm.h>
#include <Swiften/Base/String.h>
#include <Swiften/StringCodecs/Base64.h>
#include <Swift/Controllers/Intl.h>
#include <Swift/Controllers/UIInterfaces/UIFactory.h>
#include "Swiften/Network/TimerFactory.h"
#include "Swift/Controllers/BuildVersion.h"
#include "Swiften/Client/Storages.h"
#include "Swiften/VCards/VCardManager.h"
#include "Swift/Controllers/Chat/UserSearchController.h"
#include "Swift/Controllers/Chat/ChatsManager.h"
#include "Swift/Controllers/XMPPEvents/EventController.h"
#include "Swift/Controllers/EventWindowController.h"
#include "Swift/Controllers/UIInterfaces/LoginWindow.h"
#include "Swift/Controllers/UIInterfaces/LoginWindowFactory.h"
#include "Swift/Controllers/UIInterfaces/MainWindow.h"
#include "Swift/Controllers/Chat/MUCController.h"
#include "Swiften/Client/NickResolver.h"
#include "Swift/Controllers/Roster/RosterController.h"
#include "Swift/Controllers/SoundEventController.h"
#include "Swift/Controllers/SoundPlayer.h"
#include "Swift/Controllers/StatusTracker.h"
#include "Swift/Controllers/SystemTray.h"
#include "Swift/Controllers/SystemTrayController.h"
#include "Swift/Controllers/XMLConsoleController.h"
#include <Swift/Controllers/HistoryController.h>
#include <Swift/Controllers/HistoryViewController.h>
#include "Swift/Controllers/FileTransferListController.h"
#include "Swift/Controllers/UIEvents/UIEventStream.h"
#include "Swift/Controllers/PresenceNotifier.h"
#include "Swift/Controllers/EventNotifier.h"
#include "Swift/Controllers/Storages/StoragesFactory.h"
#include "Swift/Controllers/WhiteboardManager.h"
#include "SwifTools/Dock/Dock.h"
#include "SwifTools/Notifier/TogglableNotifier.h"
#include "Swiften/Base/foreach.h"
#include "Swiften/Client/Client.h"
#include "Swiften/Presence/PresenceSender.h"
#include "Swiften/Elements/ChatState.h"
#include "Swiften/Elements/Presence.h"
#include "Swiften/Elements/VCardUpdate.h"
#include "Swift/Controllers/Settings/SettingsProvider.h"
#include "Swiften/Elements/DiscoInfo.h"
#include "Swiften/Disco/CapsInfoGenerator.h"
#include "Swiften/Disco/GetDiscoInfoRequest.h"
#include "Swiften/Disco/ClientDiscoManager.h"
#include "Swiften/VCards/GetVCardRequest.h"
#include "Swiften/StringCodecs/SHA1.h"
#include "Swiften/StringCodecs/Hexify.h"
#include "Swift/Controllers/UIEvents/RequestChatUIEvent.h"
#include "Swift/Controllers/UIEvents/JoinMUCUIEvent.h"
#include "Swift/Controllers/Storages/CertificateStorageFactory.h"
#include "Swift/Controllers/Storages/CertificateStorageTrustChecker.h"
#include "Swiften/Network/NetworkFactories.h"
#include <Swift/Controllers/ProfileController.h>
#include <Swift/Controllers/ShowProfileController.h>
#include <Swift/Controllers/ContactEditController.h>
#include <Swift/Controllers/XMPPURIController.h>
#include "Swift/Controllers/AdHocManager.h"
#include <SwifTools/Idle/IdleDetector.h>
#include <Swift/Controllers/FileTransfer/FileTransferOverview.h>
#include <Swiften/FileTransfer/FileTransferManager.h>
#include <Swiften/Client/ClientXMLTracer.h>
#include <Swift/Controllers/SettingConstants.h>
#include <Swiften/Client/StanzaChannel.h>
#include <Swift/Controllers/HighlightManager.h>
#include <Swift/Controllers/HighlightEditorController.h>

namespace Swift {

static const std::string CLIENT_NAME = "Swift";
static const std::string CLIENT_NODE = "http://swift.im";


MainController::MainController(
		EventLoop* eventLoop,
		NetworkFactories* networkFactories,
		UIFactory* uiFactories,
		SettingsProvider* settings,
		SystemTray* systemTray,
		SoundPlayer* soundPlayer,
		StoragesFactory* storagesFactory,
		CertificateStorageFactory* certificateStorageFactory,
		Dock* dock,
		Notifier* notifier,
		URIHandler* uriHandler,
		IdleDetector* idleDetector,
		bool useDelayForLatency) :
			eventLoop_(eventLoop),
			networkFactories_(networkFactories),
			uiFactory_(uiFactories),
			storagesFactory_(storagesFactory),
			certificateStorageFactory_(certificateStorageFactory),
			settings_(settings),
			uriHandler_(uriHandler),
			idleDetector_(idleDetector),
			loginWindow_(NULL) ,
			useDelayForLatency_(useDelayForLatency),
			ftOverview_(NULL) {
	storages_ = NULL;
	certificateStorage_ = NULL;
	statusTracker_ = NULL;
	presenceNotifier_ = NULL;
	eventNotifier_ = NULL;
	rosterController_ = NULL;
	chatsManager_ = NULL;
	historyController_ = NULL;
	historyViewController_ = NULL;
	eventWindowController_ = NULL;
	profileController_ = NULL;
	showProfileController_ = NULL;
	contactEditController_ = NULL;
	userSearchControllerChat_ = NULL;
	userSearchControllerAdd_ = NULL;
	whiteboardManager_ = NULL;
	adHocManager_ = NULL;
	quitRequested_ = false;
	clientInitialized_ = false;
	offlineRequested_ = false;

	timeBeforeNextReconnect_ = -1;
	dock_ = dock;
	uiEventStream_ = new UIEventStream();

	notifier_ = new TogglableNotifier(notifier);
	notifier_->setPersistentEnabled(settings_->getSetting(SettingConstants::SHOW_NOTIFICATIONS));
	eventController_ = new EventController();
	eventController_->onEventQueueLengthChange.connect(boost::bind(&MainController::handleEventQueueLengthChange, this, _1));

	systemTrayController_ = new SystemTrayController(eventController_, systemTray);
	loginWindow_ = uiFactory_->createLoginWindow(uiEventStream_);
	loginWindow_->setShowNotificationToggle(!notifier->isExternallyConfigured());

	highlightManager_ = new HighlightManager(settings_);
	highlightEditorController_ = new HighlightEditorController(uiEventStream_, uiFactory_, highlightManager_);

	soundEventController_ = new SoundEventController(eventController_, soundPlayer, settings, highlightManager_);

	xmppURIController_ = new XMPPURIController(uriHandler_, uiEventStream_);

	std::string selectedLoginJID = settings_->getSetting(SettingConstants::LAST_LOGIN_JID);
	bool loginAutomatically = settings_->getSetting(SettingConstants::LOGIN_AUTOMATICALLY);
	std::string cachedPassword;
	std::string cachedCertificate;
	ClientOptions cachedOptions;
	bool eagle = settings_->getSetting(SettingConstants::FORGET_PASSWORDS);
	if (!eagle) {
		foreach (std::string profile, settings->getAvailableProfiles()) {
			ProfileSettingsProvider profileSettings(profile, settings);
			std::string password = profileSettings.getStringSetting("pass");
			std::string certificate = profileSettings.getStringSetting("certificate");
			std::string jid = profileSettings.getStringSetting("jid");
			ClientOptions clientOptions = parseClientOptions(profileSettings.getStringSetting("options"));
			loginWindow_->addAvailableAccount(jid, password, certificate, clientOptions);
			if (jid == selectedLoginJID) {
				cachedPassword = password;
				cachedCertificate = certificate;
				cachedOptions = clientOptions;
			}
		}
		loginWindow_->selectUser(selectedLoginJID);
		loginWindow_->setLoginAutomatically(loginAutomatically);
	}


	loginWindow_->onLoginRequest.connect(boost::bind(&MainController::handleLoginRequest, this, _1, _2, _3, _4, _5, _6, _7));
	loginWindow_->onPurgeSavedLoginRequest.connect(boost::bind(&MainController::handlePurgeSavedLoginRequest, this, _1));
	loginWindow_->onCancelLoginRequest.connect(boost::bind(&MainController::handleCancelLoginRequest, this));
	loginWindow_->onQuitRequest.connect(boost::bind(&MainController::handleQuitRequest, this));

	idleDetector_->setIdleTimeSeconds(settings->getSetting(SettingConstants::IDLE_TIMEOUT));
	idleDetector_->onIdleChanged.connect(boost::bind(&MainController::handleInputIdleChanged, this, _1));

	xmlConsoleController_ = new XMLConsoleController(uiEventStream_, uiFactory_);

	fileTransferListController_ = new FileTransferListController(uiEventStream_, uiFactory_);

	settings_->onSettingChanged.connect(boost::bind(&MainController::handleSettingChanged, this, _1));

	if (loginAutomatically) {
		profileSettings_ = new ProfileSettingsProvider(selectedLoginJID, settings_);
		/* FIXME: deal with autologin with a cert*/
		handleLoginRequest(selectedLoginJID, cachedPassword, cachedCertificate, CertificateWithKey::ref(), cachedOptions, true, true);
	} else {
		profileSettings_ = NULL;
	}
}

MainController::~MainController() {
	idleDetector_->onIdleChanged.disconnect(boost::bind(&MainController::handleInputIdleChanged, this, _1));

	purgeCachedCredentials();
	//setManagersOffline();
	eventController_->disconnectAll();

	resetClient();
	delete highlightEditorController_;
	delete highlightManager_;
	delete fileTransferListController_;
	delete xmlConsoleController_;
	delete xmppURIController_;
	delete soundEventController_;
	delete systemTrayController_;
	delete eventController_;
	delete notifier_;
	delete uiEventStream_;
}

void MainController::purgeCachedCredentials() {
	safeClear(password_);
}

void MainController::resetClient() {
	purgeCachedCredentials();
	resetCurrentError();
	resetPendingReconnects();
	vCardPhotoHash_.clear();
	delete contactEditController_;
	contactEditController_ = NULL;
	delete profileController_;
	profileController_ = NULL;
	delete showProfileController_;
	showProfileController_ = NULL;
	delete eventWindowController_;
	eventWindowController_ = NULL;
	delete chatsManager_;
	chatsManager_ = NULL;
#ifdef SWIFT_EXPERIMENTAL_HISTORY
	delete historyViewController_;
	historyViewController_ = NULL;
	delete historyController_;
	historyController_ = NULL;
#endif
	delete ftOverview_;
	ftOverview_ = NULL;
	delete rosterController_;
	rosterController_ = NULL;
	delete eventNotifier_;
	eventNotifier_ = NULL;
	delete presenceNotifier_;
	presenceNotifier_ = NULL;
	delete certificateStorage_;
	certificateStorage_ = NULL;
	delete storages_;
	storages_ = NULL;
	delete statusTracker_;
	statusTracker_ = NULL;
	delete profileSettings_;
	profileSettings_ = NULL;
	delete userSearchControllerChat_;
	userSearchControllerChat_ = NULL;
	delete userSearchControllerAdd_;
	userSearchControllerAdd_ = NULL;
	delete adHocManager_;
	adHocManager_ = NULL;
	delete whiteboardManager_;
	whiteboardManager_ = NULL;
	clientInitialized_ = false;
}

void MainController::handleSettingChanged(const std::string& settingPath) {
	if (settingPath == SettingConstants::SHOW_NOTIFICATIONS.getKey()) {
		notifier_->setPersistentEnabled(settings_->getSetting(SettingConstants::SHOW_NOTIFICATIONS));
	}
}

void MainController::resetPendingReconnects() {
	timeBeforeNextReconnect_ = -1;
	if (reconnectTimer_) {
		reconnectTimer_->stop();
		reconnectTimer_.reset();
	}
	resetCurrentError();
}

void MainController::resetCurrentError() {
	if (lastDisconnectError_) {
		lastDisconnectError_->conclude();
		lastDisconnectError_ = boost::shared_ptr<ErrorEvent>();
	}
}

void MainController::handleConnected() {
	boundJID_ = client_->getJID();
	resetCurrentError();
	resetPendingReconnects();

	if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) {
		purgeCachedCredentials();
	}

	bool freshLogin = rosterController_ == NULL;
	myStatusLooksOnline_ = true;
	if (freshLogin) {
		profileController_ = new ProfileController(client_->getVCardManager(), uiFactory_, uiEventStream_);
		showProfileController_ = new ShowProfileController(client_->getVCardManager(), uiFactory_, uiEventStream_);
		srand(static_cast<unsigned int>(time(NULL)));
		int randomPort = 10000 + rand() % 10000;
		client_->getFileTransferManager()->startListeningOnPort(randomPort);
		ftOverview_ = new FileTransferOverview(client_->getFileTransferManager());
		fileTransferListController_->setFileTransferOverview(ftOverview_);
		rosterController_ = new RosterController(jid_, client_->getRoster(), client_->getAvatarManager(), uiFactory_, client_->getNickManager(), client_->getNickResolver(), client_->getPresenceOracle(), client_->getSubscriptionManager(), eventController_, uiEventStream_, client_->getIQRouter(), settings_, client_->getEntityCapsProvider(), ftOverview_);
		rosterController_->onChangeStatusRequest.connect(boost::bind(&MainController::handleChangeStatusRequest, this, _1, _2));
		rosterController_->onSignOutRequest.connect(boost::bind(&MainController::signOut, this));
		rosterController_->getWindow()->onShowCertificateRequest.connect(boost::bind(&MainController::handleShowCertificateRequest, this));

		contactEditController_ = new ContactEditController(rosterController_, client_->getVCardManager(), uiFactory_, uiEventStream_);
		whiteboardManager_ = new WhiteboardManager(uiFactory_, uiEventStream_, client_->getNickResolver(), client_->getWhiteboardSessionManager());

		/* Doing this early as an ordering fix. Various things later will
		 * want to have the user's nick available and this means it will
		 * be before they receive stanzas that need it (e.g. bookmarks).*/
		client_->getVCardManager()->requestOwnVCard();

#ifdef SWIFT_EXPERIMENTAL_HISTORY
		historyController_ = new HistoryController(storages_->getHistoryStorage());
		historyViewController_ = new HistoryViewController(jid_, uiEventStream_, historyController_, client_->getNickResolver(), client_->getAvatarManager(), client_->getPresenceOracle(), uiFactory_);
		chatsManager_ = new ChatsManager(jid_, client_->getStanzaChannel(), client_->getIQRouter(), eventController_, uiFactory_, uiFactory_, client_->getNickResolver(), client_->getPresenceOracle(), client_->getPresenceSender(), uiEventStream_, uiFactory_, useDelayForLatency_, networkFactories_->getTimerFactory(), client_->getMUCRegistry(), client_->getEntityCapsProvider(), client_->getMUCManager(), uiFactory_, profileSettings_, ftOverview_, client_->getRoster(), !settings_->getSetting(SettingConstants::REMEMBER_RECENT_CHATS), settings_, historyController_, whiteboardManager_, highlightManager_);
#else
		chatsManager_ = new ChatsManager(jid_, client_->getStanzaChannel(), client_->getIQRouter(), eventController_, uiFactory_, uiFactory_, client_->getNickResolver(), client_->getPresenceOracle(), client_->getPresenceSender(), uiEventStream_, uiFactory_, useDelayForLatency_, networkFactories_->getTimerFactory(), client_->getMUCRegistry(), client_->getEntityCapsProvider(), client_->getMUCManager(), uiFactory_, profileSettings_, ftOverview_, client_->getRoster(), !settings_->getSetting(SettingConstants::REMEMBER_RECENT_CHATS), settings_, NULL, whiteboardManager_, highlightManager_);
#endif
		
		client_->onMessageReceived.connect(boost::bind(&ChatsManager::handleIncomingMessage, chatsManager_, _1));
		chatsManager_->setAvatarManager(client_->getAvatarManager());

		eventWindowController_ = new EventWindowController(eventController_, uiFactory_);

		loginWindow_->morphInto(rosterController_->getWindow());

		DiscoInfo discoInfo;
		discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc"));
		discoInfo.addFeature(DiscoInfo::ChatStatesFeature);
		discoInfo.addFeature(DiscoInfo::SecurityLabelsFeature);
		discoInfo.addFeature(DiscoInfo::MessageCorrectionFeature);
#ifdef SWIFT_EXPERIMENTAL_FT
		discoInfo.addFeature(DiscoInfo::JingleFeature);
		discoInfo.addFeature(DiscoInfo::JingleFTFeature);
		discoInfo.addFeature(DiscoInfo::JingleTransportsIBBFeature);
		discoInfo.addFeature(DiscoInfo::JingleTransportsS5BFeature);
#endif
#ifdef SWIFT_EXPERIMENTAL_WB
		discoInfo.addFeature(DiscoInfo::WhiteboardFeature);
#endif
		discoInfo.addFeature(DiscoInfo::MessageDeliveryReceiptsFeature);
		client_->getDiscoManager()->setCapsNode(CLIENT_NODE);
		client_->getDiscoManager()->setDiscoInfo(discoInfo);

		userSearchControllerChat_ = new UserSearchController(UserSearchController::StartChat, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_);
		userSearchControllerAdd_ = new UserSearchController(UserSearchController::AddContact, jid_, uiEventStream_, client_->getVCardManager(), uiFactory_, client_->getIQRouter(), rosterController_);
		adHocManager_ = new AdHocManager(JID(boundJID_.getDomain()), uiFactory_, client_->getIQRouter(), uiEventStream_, rosterController_->getWindow());
		
	}
	loginWindow_->setIsLoggingIn(false);

	client_->requestRoster();

	GetDiscoInfoRequest::ref discoInfoRequest = GetDiscoInfoRequest::create(JID(boundJID_.getDomain()), client_->getIQRouter());
	discoInfoRequest->onResponse.connect(boost::bind(&MainController::handleServerDiscoInfoResponse, this, _1, _2));
	discoInfoRequest->send();

	client_->getVCardManager()->requestOwnVCard();
	
	rosterController_->setEnabled(true);
	rosterController_->getWindow()->setStreamEncryptionStatus(client_->isStreamEncrypted());
	profileController_->setAvailable(true);
	contactEditController_->setAvailable(true);
	/* Send presence later to catch all the incoming presences. */
	sendPresence(statusTracker_->getNextPresence());

	/* Enable chats last of all, so rejoining MUCs has the right sent presence */
	assert(chatsManager_);
	chatsManager_->setOnline(true);

}

void MainController::handleEventQueueLengthChange(int count) {
	dock_->setNumberOfPendingMessages(count);
}

void MainController::reconnectAfterError() {
	if (reconnectTimer_) {
		reconnectTimer_->stop();
	}
	performLoginFromCachedCredentials();
}

void MainController::handleChangeStatusRequest(StatusShow::Type show, const std::string &statusText) {
	boost::shared_ptr<Presence> presence(new Presence());
	if (show == StatusShow::None) {
		// Note: this is misleading, None doesn't mean unavailable on the wire.
		presence->setType(Presence::Unavailable);
		resetPendingReconnects();
		myStatusLooksOnline_ = false;
		offlineRequested_ = true;
	}
	else {
		offlineRequested_ = false;
		presence->setShow(show);
	}
	presence->setStatus(statusText);
	statusTracker_->setRequestedPresence(presence);
	if (presence->getType() != Presence::Unavailable) {
		profileSettings_->storeInt("lastShow", presence->getShow());
		profileSettings_->storeString("lastStatus", presence->getStatus());
	}
	if (presence->getType() != Presence::Unavailable && !client_->isAvailable()) {
		performLoginFromCachedCredentials();
	} else {
		sendPresence(presence);
	}
}

void MainController::sendPresence(boost::shared_ptr<Presence> presence) {
	rosterController_->getWindow()->setMyStatusType(presence->getShow());
	rosterController_->getWindow()->setMyStatusText(presence->getStatus());
	systemTrayController_->setMyStatusType(presence->getShow());
	notifier_->setTemporarilyDisabled(presence->getShow() == StatusShow::DND);

	// Add information and send
	if (!vCardPhotoHash_.empty()) {
		presence->updatePayload(boost::make_shared<VCardUpdate>(vCardPhotoHash_));
	}
	client_->getPresenceSender()->sendPresence(presence);
	if (presence->getType() == Presence::Unavailable) {
		logout();
	}
}

void MainController::handleInputIdleChanged(bool idle) {
	if (!statusTracker_) {
		//Haven't logged in yet.
		return;
	}

	if (settings_->getSetting(SettingConstants::IDLE_GOES_OFFLINE)) {
		if (idle) {
			logout();
		}
	}
	else {
		if (idle) {
			if (statusTracker_->goAutoAway()) {
				if (client_ && client_->isAvailable()) {
					sendPresence(statusTracker_->getNextPresence());
				}
			}
		} else {
			if (statusTracker_->goAutoUnAway()) {
				if (client_ && client_->isAvailable()) {
					sendPresence(statusTracker_->getNextPresence());
				}
			}
		}
	}
}

void MainController::handleShowCertificateRequest() {
	std::vector<Certificate::ref> chain = client_->getStanzaChannel()->getPeerCertificateChain();
	rosterController_->getWindow()->openCertificateDialog(chain);
}

void MainController::handleLoginRequest(const std::string &username, const std::string &password, const std::string& certificatePath, CertificateWithKey::ref certificate, const ClientOptions& options, bool remember, bool loginAutomatically) {
	jid_ = JID(username);
	if (!jid_.isValid() || jid_.getNode().empty()) {
		loginWindow_->setMessage(QT_TRANSLATE_NOOP("", "User address invalid. User address should be of the form 'alice@wonderland.lit'"));
		loginWindow_->setIsLoggingIn(false);
	} else {
		loginWindow_->setMessage("");
		loginWindow_->setIsLoggingIn(true);
		profileSettings_ = new ProfileSettingsProvider(username, settings_);
		if (!settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) {
			profileSettings_->storeString("jid", username);
			profileSettings_->storeString("certificate", certificatePath);
			profileSettings_->storeString("pass", (remember || loginAutomatically) ? password : "");
			std::string optionString = serializeClientOptions(options);
			profileSettings_->storeString("options", optionString);
			settings_->storeSetting(SettingConstants::LAST_LOGIN_JID, username);
			settings_->storeSetting(SettingConstants::LOGIN_AUTOMATICALLY, loginAutomatically);
			loginWindow_->addAvailableAccount(profileSettings_->getStringSetting("jid"), profileSettings_->getStringSetting("pass"), profileSettings_->getStringSetting("certificate"), options);
		}

		password_ = password;
		certificate_ = certificate;
		clientOptions_ = options;
		performLoginFromCachedCredentials();
	}
}

void MainController::handlePurgeSavedLoginRequest(const std::string& username) {
	settings_->removeProfile(username);
	loginWindow_->removeAvailableAccount(username);
}

void MainController::performLoginFromCachedCredentials() {
	if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS) && password_.empty()) {
		/* Then we can't try to login again. */
		return;
	}
	/* If we logged in with a bare JID, and we have a full bound JID, re-login with the
	 * bound JID to try and keep dynamically assigned resources */
	JID clientJID = jid_;
	if (boundJID_.isValid() && jid_.isBare() && boundJID_.toBare() == jid_) {
		clientJID = boundJID_;
	}

	if (!statusTracker_) {
		statusTracker_  = new StatusTracker();
	}
	if (!clientInitialized_) {
		storages_ = storagesFactory_->createStorages(jid_.toBare());
		certificateStorage_ = certificateStorageFactory_->createCertificateStorage(jid_.toBare());
		certificateTrustChecker_ = new CertificateStorageTrustChecker(certificateStorage_);

		client_ = boost::make_shared<Swift::Client>(clientJID, createSafeByteArray(password_.c_str()), networkFactories_, storages_);
		clientInitialized_ = true;
		client_->setCertificateTrustChecker(certificateTrustChecker_);
		client_->onDataRead.connect(boost::bind(&XMLConsoleController::handleDataRead, xmlConsoleController_, _1));
		client_->onDataWritten.connect(boost::bind(&XMLConsoleController::handleDataWritten, xmlConsoleController_, _1));
		client_->onDisconnected.connect(boost::bind(&MainController::handleDisconnected, this, _1));
		client_->onConnected.connect(boost::bind(&MainController::handleConnected, this));

		client_->setSoftwareVersion(CLIENT_NAME, buildVersion);

		client_->getVCardManager()->onVCardChanged.connect(boost::bind(&MainController::handleVCardReceived, this, _1, _2));
		presenceNotifier_ = new PresenceNotifier(client_->getStanzaChannel(), notifier_, client_->getMUCRegistry(), client_->getAvatarManager(), client_->getNickResolver(), client_->getPresenceOracle(), networkFactories_->getTimerFactory());
		presenceNotifier_->onNotificationActivated.connect(boost::bind(&MainController::handleNotificationClicked, this, _1));
		eventNotifier_ = new EventNotifier(eventController_, notifier_, client_->getAvatarManager(), client_->getNickResolver());
		eventNotifier_->onNotificationActivated.connect(boost::bind(&MainController::handleNotificationClicked, this, _1));
		if (certificate_ && !certificate_->isNull()) {
			client_->setCertificate(certificate_);
		}
		boost::shared_ptr<Presence> presence(new Presence());
		presence->setShow(static_cast<StatusShow::Type>(profileSettings_->getIntSetting("lastShow", StatusShow::Online)));
		presence->setStatus(profileSettings_->getStringSetting("lastStatus"));
		statusTracker_->setRequestedPresence(presence);
	} else {
		/* In case we're in the middle of another login, make sure they don't overlap */
		client_->disconnect();
	}
	systemTrayController_->setConnecting();
	if (rosterController_) {
		rosterController_->getWindow()->setConnecting();
	}
	ClientOptions clientOptions = clientOptions_;
	bool eagle = settings_->getSetting(SettingConstants::FORGET_PASSWORDS);
	clientOptions.forgetPassword = eagle;
	clientOptions.useTLS = eagle ? ClientOptions::RequireTLS : ClientOptions::UseTLSWhenAvailable;
	client_->connect(clientOptions);
}

void MainController::handleDisconnected(const boost::optional<ClientError>& error) {
	if (rosterController_) {
		rosterController_->getWindow()->setStreamEncryptionStatus(false);
	}
	if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) {
		purgeCachedCredentials();
	}
	if (quitRequested_) {
		resetClient();
		loginWindow_->quit();
	}
	else if (error) {
		std::string message;
		std::string certificateErrorMessage;
		bool forceSignout = false;
		switch(error->getType()) {
			case ClientError::UnknownError: message = QT_TRANSLATE_NOOP("", "Unknown Error"); break;
			case ClientError::DomainNameResolveError: message = QT_TRANSLATE_NOOP("", "Unable to find server"); break;
			case ClientError::ConnectionError: message = QT_TRANSLATE_NOOP("", "Error connecting to server"); break;
			case ClientError::ConnectionReadError: message = QT_TRANSLATE_NOOP("", "Error while receiving server data"); break;
			case ClientError::ConnectionWriteError: message = QT_TRANSLATE_NOOP("", "Error while sending data to the server"); break;
			case ClientError::XMLError: message = QT_TRANSLATE_NOOP("", "Error parsing server data"); break;
			case ClientError::AuthenticationFailedError: message = QT_TRANSLATE_NOOP("", "Login/password invalid"); break;
			case ClientError::CompressionFailedError: message = QT_TRANSLATE_NOOP("", "Error while compressing stream"); break;
			case ClientError::ServerVerificationFailedError: message = QT_TRANSLATE_NOOP("", "Server verification failed"); break;
			case ClientError::NoSupportedAuthMechanismsError: message = QT_TRANSLATE_NOOP("", "Authentication mechanisms not supported"); break;
			case ClientError::UnexpectedElementError: message = QT_TRANSLATE_NOOP("", "Unexpected response"); break;
			case ClientError::ResourceBindError: message = QT_TRANSLATE_NOOP("", "Error binding resource"); break;
			case ClientError::SessionStartError: message = QT_TRANSLATE_NOOP("", "Error starting session"); break;
			case ClientError::StreamError: message = QT_TRANSLATE_NOOP("", "Stream error"); break;
			case ClientError::TLSError: message = QT_TRANSLATE_NOOP("", "Encryption error"); break;
			case ClientError::ClientCertificateLoadError: message = QT_TRANSLATE_NOOP("", "Error loading certificate (Invalid password?)"); break;
			case ClientError::ClientCertificateError: message = QT_TRANSLATE_NOOP("", "Certificate not authorized"); break;
			case ClientError::CertificateCardRemoved: message = QT_TRANSLATE_NOOP("", "Certificate card removed"); forceSignout = true; break;

			case ClientError::UnknownCertificateError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Unknown certificate"); break;
			case ClientError::CertificateExpiredError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate has expired"); break;
			case ClientError::CertificateNotYetValidError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate is not yet valid"); break;
			case ClientError::CertificateSelfSignedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate is self-signed"); break;
			case ClientError::CertificateRejectedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate has been rejected"); break;
			case ClientError::CertificateUntrustedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate is not trusted"); break;
			case ClientError::InvalidCertificatePurposeError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate cannot be used for encrypting your connection"); break;
			case ClientError::CertificatePathLengthExceededError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate path length constraint exceeded"); break;
			case ClientError::InvalidCertificateSignatureError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Invalid certificate signature"); break;
			case ClientError::InvalidCAError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Invalid Certificate Authority"); break;
			case ClientError::InvalidServerIdentityError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate does not match the host identity"); break;
			case ClientError::RevokedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Certificate has been revoked"); break;
			case ClientError::RevocationCheckFailedError: certificateErrorMessage = QT_TRANSLATE_NOOP("", "Unable to determine certificate revocation state"); break;
		}
		bool forceReconnectAfterCertificateTrust = false;
		if (!certificateErrorMessage.empty()) {
			std::vector<Certificate::ref> certificates = certificateTrustChecker_->getLastCertificateChain();
			if (!certificates.empty() && loginWindow_->askUserToTrustCertificatePermanently(certificateErrorMessage, certificates)) {
				certificateStorage_->addCertificate(certificates[0]);
				forceReconnectAfterCertificateTrust = true;
			}
			else {
				message = QT_TRANSLATE_NOOP("", "Certificate error");
			}
		}

		if (forceReconnectAfterCertificateTrust && settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) {
			forceReconnectAfterCertificateTrust = false;
			forceSignout = true;
			message = QT_TRANSLATE_NOOP("", "Re-enter credentials and retry");
		}

		if (forceReconnectAfterCertificateTrust) {
			performLoginFromCachedCredentials();
		}
		else if (forceSignout || !rosterController_) { //hasn't been logged in yet or permanent error
			signOut();
			loginWindow_->setMessage(message);
			loginWindow_->setIsLoggingIn(false);
		} else {
			logout();
			if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) {
				message = str(format(QT_TRANSLATE_NOOP("", "Disconnected from %1%: %2%. To reconnect, Sign Out and provide your password again.")) % jid_.getDomain() % message);
			} else {
				if (!offlineRequested_) {
					setReconnectTimer();
				}
				if (lastDisconnectError_) {
					message = str(format(QT_TRANSLATE_NOOP("", "Reconnect to %1% failed: %2%. Will retry in %3% seconds.")) % jid_.getDomain() % message % boost::lexical_cast<std::string>(timeBeforeNextReconnect_));
					lastDisconnectError_->conclude();
				} else {
					message = str(format(QT_TRANSLATE_NOOP("", "Disconnected from %1%: %2%.")) % jid_.getDomain() % message);
				}
				lastDisconnectError_ = boost::make_shared<ErrorEvent>(JID(jid_.getDomain()), message);
				eventController_->handleIncomingEvent(lastDisconnectError_);
			}
		}
	}
	else if (!rosterController_) { //hasn't been logged in yet
		loginWindow_->setIsLoggingIn(false);
	}
}

void MainController::setReconnectTimer() {
	if (timeBeforeNextReconnect_ < 0) {
		timeBeforeNextReconnect_ = 1;
	} else {
		timeBeforeNextReconnect_ = timeBeforeNextReconnect_ >= 150 ? 300 : timeBeforeNextReconnect_ * 2; // Randomly selected by roll of a die, as required by 3920bis
	}
	if (reconnectTimer_) {
		reconnectTimer_->stop();
	}
	reconnectTimer_ = networkFactories_->getTimerFactory()->createTimer(timeBeforeNextReconnect_ * 1000);
	reconnectTimer_->onTick.connect(boost::bind(&MainController::reconnectAfterError, this));
	reconnectTimer_->start();
}

void MainController::handleCancelLoginRequest() {
	signOut();
}

void MainController::signOut() {
	if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) {
		purgeCachedCredentials();
	}
	eventController_->clear();
	logout();
	loginWindow_->loggedOut();
	resetClient();
}

void MainController::logout() {
	if (settings_->getSetting(SettingConstants::FORGET_PASSWORDS)) {
		purgeCachedCredentials();
	}
	systemTrayController_->setMyStatusType(StatusShow::None);
	if (clientInitialized_ /*&& client_->isAvailable()*/) {
		client_->disconnect();
	}
	if (rosterController_ && myStatusLooksOnline_) {
		rosterController_->getWindow()->setMyStatusType(StatusShow::None);
		rosterController_->getWindow()->setMyStatusText("");
		myStatusLooksOnline_ = false;
	}
	setManagersOffline();
}

void MainController::setManagersOffline() {
	if (chatsManager_) {
		chatsManager_->setOnline(false);
	}
	if (rosterController_) {
		rosterController_->setEnabled(false);
	}
	if (profileController_) {
		profileController_->setAvailable(false);
	}
	if (contactEditController_) {
		contactEditController_->setAvailable(false);
	}
}

void MainController::handleServerDiscoInfoResponse(boost::shared_ptr<DiscoInfo> info, ErrorPayload::ref error) {
	if (!error) {
		chatsManager_->setServerDiscoInfo(info);
		adHocManager_->setServerDiscoInfo(info);
	}
}

void MainController::handleVCardReceived(const JID& jid, VCard::ref vCard) {
	if (!jid.equals(jid_, JID::WithoutResource) || !vCard || vCard->getPhoto().empty()) {
		return;
	}
	std::string hash = Hexify::hexify(SHA1::getHash(vCard->getPhoto()));
	if (hash != vCardPhotoHash_) {
		vCardPhotoHash_ = hash;
		if (client_ && client_->isAvailable()) {
			sendPresence(statusTracker_->getNextPresence());
		}
	}
}

void MainController::handleNotificationClicked(const JID& jid) {
	assert(chatsManager_);
	if (clientInitialized_) {
		if (client_->getMUCRegistry()->isMUC(jid)) {
			uiEventStream_->send(boost::make_shared<JoinMUCUIEvent>(jid));
		}
		else {
			uiEventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(jid)));
		}
	}
}

void MainController::handleQuitRequest() {
	if (client_ && client_->isActive()) {
		quitRequested_ = true;
		client_->disconnect();
	}
	else {
		resetClient();
		loginWindow_->quit();
	}
}

#define SERIALIZE_BOOL(option) result += options.option ? "1" : "0"; result += ",";
#define SERIALIZE_INT(option) result += boost::lexical_cast<std::string>(options.option); result += ",";
#define SERIALIZE_STRING(option) result += Base64::encode(createByteArray(options.option)); result += ",";
#define SERIALIZE_SAFE_STRING(option) result += safeByteArrayToString(Base64::encode(options.option)); result += ",";
#define SERIALIZE_URL(option) SERIALIZE_STRING(option.toString())

std::string MainController::serializeClientOptions(const ClientOptions& options) {
	std::string result;
	SERIALIZE_BOOL(useStreamCompression);
	switch (options.useTLS) {
		case ClientOptions::NeverUseTLS: result += "1";break;
		case ClientOptions::UseTLSWhenAvailable: result += "2";break;
		case ClientOptions::RequireTLS: result += "3";break;
	}
	result += ",";
	SERIALIZE_BOOL(allowPLAINWithoutTLS);
	SERIALIZE_BOOL(useStreamResumption);
	SERIALIZE_BOOL(useAcks);
	SERIALIZE_STRING(manualHostname);
	SERIALIZE_INT(manualPort);
	switch (options.proxyType) {
		case ClientOptions::NoProxy: result += "1";break;
		case ClientOptions::SystemConfiguredProxy: result += "2";break;
		case ClientOptions::SOCKS5Proxy: result += "3";break;
		case ClientOptions::HTTPConnectProxy: result += "4";break;
	}
	result += ",";
	SERIALIZE_STRING(manualProxyHostname);
	SERIALIZE_INT(manualProxyPort);
	SERIALIZE_URL(boshURL);
	SERIALIZE_URL(boshHTTPConnectProxyURL);
	SERIALIZE_SAFE_STRING(boshHTTPConnectProxyAuthID);
	SERIALIZE_SAFE_STRING(boshHTTPConnectProxyAuthPassword);
	return result;
}

#define CHECK_PARSE_LENGTH if (i >= segments.size()) {return result;} 
#define PARSE_INT_RAW(defaultValue) CHECK_PARSE_LENGTH intVal = defaultValue; try {intVal = boost::lexical_cast<int>(segments[i]);} catch(const boost::bad_lexical_cast&) {};i++;
#define PARSE_STRING_RAW CHECK_PARSE_LENGTH stringVal = byteArrayToString(Base64::decode(segments[i]));i++;

#define PARSE_BOOL(option, defaultValue) PARSE_INT_RAW(defaultValue); result.option = (intVal == 1);
#define PARSE_INT(option, defaultValue) PARSE_INT_RAW(defaultValue); result.option = intVal;
#define PARSE_STRING(option) PARSE_STRING_RAW; result.option = stringVal;
#define PARSE_SAFE_STRING(option) PARSE_STRING_RAW; result.option = SafeString(createSafeByteArray(stringVal));
#define PARSE_URL(option) {PARSE_STRING_RAW; result.option = URL::fromString(stringVal);}


ClientOptions MainController::parseClientOptions(const std::string& optionString) {
	ClientOptions result;
	size_t i = 0;
	int intVal = 0;
	std::string stringVal;
	std::vector<std::string> segments = String::split(optionString, ',');

	PARSE_BOOL(useStreamCompression, 1);
	PARSE_INT_RAW(-1);
	switch (intVal) {
		case 1: result.useTLS = ClientOptions::NeverUseTLS;break;
		case 2: result.useTLS = ClientOptions::UseTLSWhenAvailable;break;
		case 3: result.useTLS = ClientOptions::RequireTLS;break;
		default:;
	}
	PARSE_BOOL(allowPLAINWithoutTLS, 0);
	PARSE_BOOL(useStreamResumption, 0);
	PARSE_BOOL(useAcks, 1);
	PARSE_STRING(manualHostname);
	PARSE_INT(manualPort, -1);
	PARSE_INT_RAW(-1);
	switch (intVal) {
		case 1: result.proxyType = ClientOptions::NoProxy;break;
		case 2: result.proxyType = ClientOptions::SystemConfiguredProxy;break;
		case 3: result.proxyType = ClientOptions::SOCKS5Proxy;break;
		case 4: result.proxyType = ClientOptions::HTTPConnectProxy;break;
	}
	PARSE_STRING(manualProxyHostname);
	PARSE_INT(manualProxyPort, -1);
	PARSE_URL(boshURL);
	PARSE_URL(boshHTTPConnectProxyURL);
	PARSE_SAFE_STRING(boshHTTPConnectProxyAuthID);
	PARSE_SAFE_STRING(boshHTTPConnectProxyAuthPassword);

	return result;
}

}