summaryrefslogtreecommitdiffstats
blob: d19a55d2f4912174237f99d22dcb82ef815cb039 (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
/*
 * Copyright (c) 2010 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "QtSetGroupsDialog.h"

#include <algorithm>

#include <QScrollArea>
#include <QBoxLayout>
#include <QLabel>
#include <QDialogButtonBox>

#include "Swift/QtUI/QtSwiftUtil.h"

namespace Swift {

QtSetGroupsDialog::QtSetGroupsDialog(ContactRosterItem* contact, const QList<QString>& allGroups) : contact_(contact) {
	//resize(300,300);
	setWindowTitle("Edit contact");
	QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
	setContentsMargins(0,0,0,0);
	QScrollArea* scrollArea = new QScrollArea(this);
	layout->addWidget(scrollArea);
	scrollArea->setWidgetResizable(true);
	scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	QWidget* scroll = new QWidget(scrollArea);
	scrollArea->setWidget(scroll);
	QVBoxLayout* scrollLayout = new QVBoxLayout(scroll);
	QLabel* label = new QLabel(scroll);
	label->setText("Choose groups for " + P2QSTRING(contact->getDisplayName()) + " (" + P2QSTRING(contact->getJID().toString()) + ")");
	scrollLayout->addWidget(label);
	foreach (QString group, allGroups) {
		QCheckBox* check = new QCheckBox(scroll);
		check->setText(group);
		check->setCheckState(Qt::Unchecked);
		checkBoxes_[Q2PSTRING(group)] = check;
		scrollLayout->addWidget(check);
	}
	foreach (String group, contact->getGroups()) {
		checkBoxes_[group]->setCheckState(Qt::Checked);
	}

	QHBoxLayout* newGroupLayout = new QHBoxLayout();
	newGroup_ = new QCheckBox(scroll);
	newGroup_->setText("New Group:");
	newGroup_->setCheckState(Qt::Unchecked);
	newGroupLayout->addWidget(newGroup_);
	newGroupName_ = new QLineEdit(scroll);
	newGroupLayout->addWidget(newGroupName_);
	scrollLayout->addLayout(newGroupLayout);

	scrollLayout->addItem(new QSpacerItem(20, 73, QSizePolicy::Minimum, QSizePolicy::Expanding));

	QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
	layout->addWidget(buttons);
	connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
	connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
}

QtSetGroupsDialog::~QtSetGroupsDialog() {
	// TODO Auto-generated destructor stub
}

typedef std::pair<String, QCheckBox*> CheckStringPair;

boost::shared_ptr<RegroupRosterItemUIEvent> QtSetGroupsDialog::getRegroupEvent() {
	std::vector<String> addedGroups;
	std::vector<String> removedGroups;
	std::vector<String> existingGroups = contact_->getGroups();
	int tickedCount = 0;
	bool wantsContacts = false;
	foreach (CheckStringPair pair, checkBoxes_) {
		bool existing = std::find(existingGroups.begin(), existingGroups.end(), pair.first) != existingGroups.end();
		if (pair.second->checkState() == Qt::Checked) {
			tickedCount++;
			if (pair.first == "Contacts") {
				wantsContacts = true;
			}
			if (!existing && pair.first != "Contacts") {
				addedGroups.push_back(pair.first);
			}
		} else {
			if (existing) {
				removedGroups.push_back(pair.first);
			}
		}
	}
	if (newGroup_->checkState() == Qt::Checked) {
		tickedCount++;
		String name = Q2PSTRING(newGroupName_->text());
		if (std::find(existingGroups.begin(), existingGroups.end(), name) == existingGroups.end()) {
			addedGroups.push_back(name);
		}
	}
	if (tickedCount > 1 && wantsContacts) {
		addedGroups.push_back("Contacts");
	}
	boost::shared_ptr<RegroupRosterItemUIEvent> result(new RegroupRosterItemUIEvent(contact_->getJID(), addedGroups, removedGroups));
	return result;
}

}