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

#include <Swift/QtUI/QtAffiliationEditor.h>

#include <QInputDialog>
#include <QListWidgetItem>

#include <Swift/QtUI/QtSwiftUtil.h>

namespace Swift {
QtAffiliationEditor::QtAffiliationEditor(QWidget* parent) : QDialog(parent){
	ui_.setupUi(this);
	setAttribute(Qt::WA_DeleteOnClose);
	connect(ui_.affiliation, SIGNAL(currentIndexChanged(int)), this, SLOT(handleCurrentIndexChanged(int)));
	connect(ui_.addJID, SIGNAL(clicked()), this, SLOT(handleAddClicked()));
	connect(ui_.removeJID, SIGNAL(clicked()), this, SLOT(handleRemoveClicked()));
}

QtAffiliationEditor::~QtAffiliationEditor() {
	
}

void QtAffiliationEditor::setAffiliations(MUCOccupant::Affiliation affiliation, const std::vector<JID>& jids) {
	affiliations_[affiliation] = jids;
	if (affiliationFromIndex(ui_.affiliation->currentIndex()) == affiliation) {
		handleCurrentIndexChanged(ui_.affiliation->currentIndex());
	}
}

const std::vector<std::pair<MUCOccupant::Affiliation, JID> >& QtAffiliationEditor::getChanges() const {
	return changes_;
}

void QtAffiliationEditor::handleCurrentIndexChanged(int index) {
	ui_.list->clear();
	foreach (const JID& jid, affiliations_[affiliationFromIndex(index)]) {
		ui_.list->addItem(P2QSTRING(jid.toString()));
	}
}

void QtAffiliationEditor::handleAddClicked() {
	bool ok = false;
	JID jid = JID(Q2PSTRING(QInputDialog::getText(this, tr("Add User"), tr("Added User's Address:"), QLineEdit::Normal, "", &ok)));
	if (ok && jid.isValid()) {
		//FIXME: validation
		MUCOccupant::Affiliation affiliation = affiliationFromIndex(ui_.affiliation->currentIndex());
		changes_.push_back(ChangePair(affiliation, jid));
		ui_.list->addItem(P2QSTRING(jid.toString()));
		affiliations_[affiliation].push_back(jid);
	}
}

void QtAffiliationEditor::handleRemoveClicked() {
	QListWidgetItem* item = ui_.list->currentItem();
	if (item) {
		JID jid(Q2PSTRING(item->text()));
		changes_.push_back(ChangePair(MUCOccupant::NoAffiliation, jid));
		std::vector<JID>& jids = affiliations_[affiliationFromIndex(ui_.affiliation->currentIndex())];
		jids.erase(std::remove(jids.begin(), jids.end(), jid), jids.end());
		handleCurrentIndexChanged(ui_.affiliation->currentIndex());
	}
}

MUCOccupant::Affiliation QtAffiliationEditor::affiliationFromIndex(int affiliation) {
	switch (affiliation) {
		case 0: return MUCOccupant::Owner;
		case 1: return MUCOccupant::Admin;
		case 2: return MUCOccupant::Member;
		case 3: return MUCOccupant::Outcast;
		default: return MUCOccupant::Outcast;
	}
}

}