Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UserEdit.cpp « mumble « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d1305dcf90311162cb688f38a7d41a9ff6193e94 (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
// Copyright 2005-2019 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "UserEdit.h"

#include "Channel.h"
#include "ServerHandler.h"
#include "User.h"
#include "UserListModel.h"

#include <QtCore/QItemSelectionModel>
#include <QtWidgets/QMenu>

// We define a global macro called 'g'. This can lead to issues when included code uses 'g' as a type or parameter name (like protobuf 3.7 does). As such, for now, we have to make this our last include.
#include "Global.h"

UserEdit::UserEdit(const MumbleProto::UserList &userList, QWidget *p)
	: QDialog(p)
	, m_model(new UserListModel(userList, this))
	, m_filter(new UserListFilterProxyModel(this)) {

	setupUi(this);

	const int userCount = userList.users_size();
	setWindowTitle(tr("Registered users: %n account(s)", "", userCount));

	m_filter->setSourceModel(m_model);
	qtvUserList->setModel(m_filter);

	QItemSelectionModel *selectionModel = qtvUserList->selectionModel();
	connect(selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
	        this, SLOT(onSelectionChanged(QItemSelection,QItemSelection)));
	connect(selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
	        this, SLOT(onCurrentRowChanged(QModelIndex,QModelIndex)));

	qtvUserList->setFocus();
	qtvUserList->setContextMenuPolicy(Qt::CustomContextMenu);

#if QT_VERSION >= 0x050000
	qtvUserList->header()->setSectionResizeMode(UserListModel::COL_NICK, QHeaderView::Stretch);
	if (!m_model->isLegacy()) {
		qtvUserList->header()->setSectionResizeMode(UserListModel::COL_INACTIVEDAYS, QHeaderView::ResizeToContents);
		qtvUserList->header()->setSectionResizeMode(UserListModel::COL_LASTCHANNEL, QHeaderView::Stretch);
	}
#else
	qtvUserList->header()->setResizeMode(UserListModel::COL_NICK, QHeaderView::Stretch);
	if (!m_model->isLegacy()) {
		qtvUserList->header()->setResizeMode(UserListModel::COL_INACTIVEDAYS, QHeaderView::ResizeToContents);
		qtvUserList->header()->setResizeMode(UserListModel::COL_LASTCHANNEL, QHeaderView::Stretch);
	}
#endif

	if (m_model->isLegacy()) {
		qlInactive->hide();
		qsbInactive->hide();
		qcbInactive->hide();
	}

	qtvUserList->sortByColumn(UserListModel::COL_NICK, Qt::AscendingOrder);
}

void UserEdit::accept() {
	if (m_model->isUserListDirty()) {
		MumbleProto::UserList userList = m_model->getUserListUpdate();
		g.sh->sendMessage(userList);
	}

	QDialog::accept();
}

void UserEdit::on_qlSearch_textChanged(QString pattern) {
	m_filter->setFilterWildcard(pattern);
}


void UserEdit::on_qpbRename_clicked() {
	QModelIndex current = qtvUserList->selectionModel()->currentIndex();
	if (!current.isValid())
		return;

	QModelIndex nickIndex = current.sibling(current.row(), UserListModel::COL_NICK);
	qtvUserList->edit(nickIndex);
}

void UserEdit::on_qpbRemove_clicked() {
	m_filter->removeRowsInSelection(qtvUserList->selectionModel()->selection());
}

void UserEdit::on_qtvUserList_customContextMenuRequested(const QPoint &point) {
	QMenu *menu = new QMenu(this);

	if (qtvUserList->selectionModel()->currentIndex().isValid()) {
		QAction *renameAction = menu->addAction(tr("Rename"));
		connect(renameAction, SIGNAL(triggered()),
		        this, SLOT(on_qpbRename_clicked()));

		menu->addSeparator();
	}

	QAction *removeMenuAction = menu->addAction(tr("Remove"));
	connect(removeMenuAction, SIGNAL(triggered()),
	        this, SLOT(on_qpbRemove_clicked()));

	menu->exec(qtvUserList->mapToGlobal(point));
	delete menu;
}

void UserEdit::onSelectionChanged(const QItemSelection& /*selected*/, const QItemSelection& /*deselected*/) {
	const bool somethingSelected = !(qtvUserList->selectionModel()->selection().empty());
	qpbRemove->setEnabled(somethingSelected);
}

void UserEdit::onCurrentRowChanged(const QModelIndex &current, const QModelIndex &) {
	qpbRename->setEnabled(current.isValid());
}

void UserEdit::on_qsbInactive_valueChanged(int) {
	updateInactiveDaysFilter();
}

void UserEdit::on_qcbInactive_currentIndexChanged(int) {
	updateInactiveDaysFilter();
}

void UserEdit::updateInactiveDaysFilter() {
	const int timespanUnit = qcbInactive->currentIndex();
	const int timespanCount = qsbInactive->value();

	int minimumInactiveDays = 0;
	switch (timespanUnit) {
		case TU_DAYS:
			minimumInactiveDays = timespanCount;
			break;
		case TU_WEEKS:
			minimumInactiveDays = timespanCount * 7;
			break;
		case TU_MONTHS:
			minimumInactiveDays = timespanCount * 30;
			break;
		case TU_YEARS:
			minimumInactiveDays = timespanCount * 365;
			break;
		default:
			break;
	}

	m_filter->setFilterMinimumInactiveDays(minimumInactiveDays);
}


UserListFilterProxyModel::UserListFilterProxyModel(QObject *parent_)
	: QSortFilterProxyModel(parent_)
	, m_minimumInactiveDays(0) {

	setFilterKeyColumn(UserListModel::COL_NICK);
	setFilterCaseSensitivity(Qt::CaseInsensitive);
	setSortLocaleAware(true);
	setDynamicSortFilter(true);
}

bool UserListFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
	if(!QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent)) {
		return false;
	}

	const QModelIndex inactiveDaysIdx = sourceModel()->index(source_row,
	                                                         UserListModel::COL_INACTIVEDAYS,
	                                                         source_parent);

	bool ok;
	const int inactiveDays = inactiveDaysIdx.data().toInt(&ok);

	// If inactiveDaysIdx doesn't store an int the account hasn't been seen yet and mustn't be filtered
	if (ok && inactiveDays < m_minimumInactiveDays)
		return false;

	return true;
}

void UserListFilterProxyModel::setFilterMinimumInactiveDays(int minimumInactiveDays) {
	m_minimumInactiveDays = minimumInactiveDays;
	invalidateFilter();
}

void UserListFilterProxyModel::removeRowsInSelection(const QItemSelection &selection) {
	QItemSelection sourceSelection = mapSelectionToSource(selection);
	qobject_cast<UserListModel*>(sourceModel())->removeRowsInSelection(sourceSelection);
}