summaryrefslogtreecommitdiffstats
blob: 49818b0781cdeddbb01e274b74658fb3d8f5a2a8 (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
/*
 * Copyright (c) 2010-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/Controllers/ProfileController.h>

#include <boost/bind.hpp>

#include <Swiften/VCards/VCardManager.h>

#include <Swift/Controllers/Intl.h>
#include <Swift/Controllers/UIEvents/RequestProfileEditorUIEvent.h>
#include <Swift/Controllers/UIEvents/UIEventStream.h>
#include <Swift/Controllers/UIInterfaces/ProfileWindowFactory.h>

namespace Swift {

ProfileController::ProfileController(VCardManager* vcardManager, ProfileWindowFactory* profileWindowFactory, UIEventStream* uiEventStream) : vcardManager(vcardManager), profileWindowFactory(profileWindowFactory), uiEventStream(uiEventStream), available(true), profileWindow(NULL), gettingVCard(false) {
	uiEventStream->onUIEvent.connect(boost::bind(&ProfileController::handleUIEvent, this, _1));
}

ProfileController::~ProfileController() {
	if (profileWindow) {
		vcardManager->onOwnVCardChanged.disconnect(boost::bind(&ProfileController::handleOwnVCardChanged, this, _1));
		profileWindow->onVCardChangeRequest.disconnect(boost::bind(&ProfileController::handleVCardChangeRequest, this, _1));
		profileWindow->onWindowAboutToBeClosed.disconnect(boost::bind(&ProfileController::handleProfileWindowAboutToBeClosed, this, _1));
	}
	uiEventStream->onUIEvent.disconnect(boost::bind(&ProfileController::handleUIEvent, this, _1));
	delete profileWindow;
}

void ProfileController::handleUIEvent(UIEvent::ref event) {
	if (!boost::dynamic_pointer_cast<RequestProfileEditorUIEvent>(event)) {
		return;
	}

	if (!profileWindow) {
		profileWindow = profileWindowFactory->createProfileWindow();
		profileWindow->setEditable(true);
		profileWindow->onVCardChangeRequest.connect(boost::bind(&ProfileController::handleVCardChangeRequest, this, _1));
		profileWindow->onWindowAboutToBeClosed.connect(boost::bind(&ProfileController::handleProfileWindowAboutToBeClosed, this, _1));
		vcardManager->onOwnVCardChanged.connect(boost::bind(&ProfileController::handleOwnVCardChanged, this, _1));
	}
	gettingVCard = true;
	updateDialogStatus();
	vcardManager->requestOwnVCard();
	profileWindow->show();
}

void ProfileController::handleVCardChangeRequest(VCard::ref vcard) {
	assert(!pendingSetVCardRequest);
	profileWindow->setError("");
	pendingSetVCardRequest = vcardManager->createSetVCardRequest(vcard);
	pendingSetVCardRequest->onResponse.connect(boost::bind(&ProfileController::handleSetVCardResponse, this, _2));
	pendingSetVCardRequest->send();
	updateDialogStatus();
}

void ProfileController::handleSetVCardResponse(ErrorPayload::ref error) {
	pendingSetVCardRequest.reset();
	updateDialogStatus();
	if (error) {
		profileWindow->setVCard(vcardBeforeEdit);
		profileWindow->setError(QT_TRANSLATE_NOOP("", "There was an error publishing your profile data"));
	}
	else {
		profileWindow->setError("");
		profileWindow->hide();
	}
}

void ProfileController::handleOwnVCardChanged(VCard::ref vcard) {
	if (profileWindow) {
		profileWindow->setVCard(vcard);
		vcardBeforeEdit = vcard;
		gettingVCard = false;
		updateDialogStatus();
	}
}

void ProfileController::handleProfileWindowAboutToBeClosed(const JID&) {
	profileWindow = NULL;
}

void ProfileController::setAvailable(bool b) {
	available = b;
	if (!available) {
		pendingSetVCardRequest.reset();
	}
	updateDialogStatus();
}


void ProfileController::updateDialogStatus() {
	if (profileWindow) {
		profileWindow->setEnabled(available && !gettingVCard && !pendingSetVCardRequest);
		profileWindow->setProcessing(gettingVCard || pendingSetVCardRequest);
	}
}

}