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

ChannelListener.cpp « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f0656d1933e656189c9ef03b43518b9a85256cb (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
// Copyright 2020-2021 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 "ChannelListener.h"
#include "Channel.h"
#include "User.h"

#ifdef MUMBLE
#	include "ServerHandler.h"
#	include "Database.h"
#	include "Global.h"
#endif

#include <QtCore/QReadLocker>
#include <QtCore/QWriteLocker>

// init static instance
ChannelListener ChannelListener::s_instance;

ChannelListener::ChannelListener()
	: QObject(nullptr), m_listenerLock(), m_listeningUsers(), m_listenedChannels()
#ifdef MUMBLE
	  ,
	  m_volumeLock(), m_listenerVolumeAdjustments(), m_initialSyncDone(false)
#endif
{
}

void ChannelListener::addListenerImpl(unsigned int userSession, int channelID) {
	QWriteLocker lock(&m_listenerLock);

	m_listeningUsers[userSession] << channelID;
	m_listenedChannels[channelID] << userSession;
}

void ChannelListener::removeListenerImpl(unsigned int userSession, int channelID) {
	QWriteLocker lock(&m_listenerLock);

	m_listeningUsers[userSession].remove(channelID);
	m_listenedChannels[channelID].remove(userSession);
}

bool ChannelListener::isListeningImpl(unsigned int userSession, int channelID) const {
	QReadLocker lock(&m_listenerLock);

	return m_listenedChannels[channelID].contains(userSession);
}

bool ChannelListener::isListeningToAnyImpl(unsigned int userSession) const {
	QReadLocker lock(&m_listenerLock);

	return !m_listeningUsers[userSession].isEmpty();
}

bool ChannelListener::isListenedByAnyImpl(int channelID) const {
	QReadLocker lock(&m_listenerLock);

	return !m_listenedChannels[channelID].isEmpty();
}

const QSet< unsigned int > ChannelListener::getListenersForChannelImpl(int channelID) const {
	QReadLocker lock(&m_listenerLock);

	return m_listenedChannels[channelID];
}

const QSet< int > ChannelListener::getListenedChannelsForUserImpl(unsigned int userSession) const {
	QReadLocker lock(&m_listenerLock);

	return m_listeningUsers[userSession];
}

int ChannelListener::getListenerCountForChannelImpl(int channelID) const {
	QReadLocker lock(&m_listenerLock);

	return m_listenedChannels[channelID].size();
}

int ChannelListener::getListenedChannelCountForUserImpl(unsigned int userSession) const {
	QReadLocker lock(&m_listenerLock);

	return m_listeningUsers[userSession].size();
}

#ifdef MUMBLE
void ChannelListener::setListenerLocalVolumeAdjustmentImpl(int channelID, float volumeAdjustment) {
	float oldValue;
	{
		QWriteLocker lock(&m_volumeLock);

		oldValue = m_listenerVolumeAdjustments.value(channelID, 1.0f);
		m_listenerVolumeAdjustments.insert(channelID, volumeAdjustment);
	}

	if (oldValue != volumeAdjustment) {
		emit localVolumeAdjustmentsChanged(channelID, volumeAdjustment, oldValue);
	}
}

float ChannelListener::getListenerLocalVolumeAdjustmentImpl(int channelID) const {
	QReadLocker lock(&m_volumeLock);

	return m_listenerVolumeAdjustments.value(channelID, 1.0f);
}

QHash< int, float > ChannelListener::getAllListenerLocalVolumeAdjustmentsImpl(bool filter) const {
	QReadLocker lock(&m_volumeLock);

	if (!filter) {
		return m_listenerVolumeAdjustments;
	} else {
		QHash< int, float > volumeMap;

		QHashIterator< int, float > it(m_listenerVolumeAdjustments);

		while (it.hasNext()) {
			it.next();

			if (it.value() != 1.0f) {
				volumeMap.insert(it.key(), it.value());
			}
		}

		return volumeMap;
	}
}

void ChannelListener::setInitialServerSyncDoneImpl(bool done) {
	m_initialSyncDone.store(done);
}
#endif

void ChannelListener::clearImpl(){ { QWriteLocker lock(&m_listenerLock);
m_listeningUsers.clear();
m_listenedChannels.clear();
}
#ifdef MUMBLE
{
	QWriteLocker lock(&m_volumeLock);
	m_listenerVolumeAdjustments.clear();
}
#endif
}


ChannelListener &ChannelListener::get() {
	return s_instance;
}


void ChannelListener::addListener(unsigned int userSession, int channelID) {
	get().addListenerImpl(userSession, channelID);
}

void ChannelListener::addListener(const User *user, const Channel *channel) {
	get().addListenerImpl(user->uiSession, channel->iId);
}

void ChannelListener::removeListener(unsigned int userSession, int channelID) {
	get().removeListenerImpl(userSession, channelID);
}

void ChannelListener::removeListener(const User *user, const Channel *channel) {
	get().removeListenerImpl(user->uiSession, channel->iId);
}

bool ChannelListener::isListening(unsigned int userSession, int channelID) {
	return get().isListeningImpl(userSession, channelID);
}

bool ChannelListener::isListening(const User *user, const Channel *channel) {
	return get().isListeningImpl(user->uiSession, channel->iId);
}

bool ChannelListener::isListeningToAny(unsigned int userSession) {
	return get().isListeningToAnyImpl(userSession);
}

bool ChannelListener::isListeningToAny(const User *user) {
	return get().isListeningToAnyImpl(user->uiSession);
}

bool ChannelListener::isListenedByAny(int channelID) {
	return get().isListenedByAnyImpl(channelID);
}

bool ChannelListener::isListenedByAny(const Channel *channel) {
	return get().isListenedByAnyImpl(channel->iId);
}

const QSet< unsigned int > ChannelListener::getListenersForChannel(int channelID) {
	return get().getListenersForChannelImpl(channelID);
}

const QSet< unsigned int > ChannelListener::getListenersForChannel(const Channel *channel) {
	return get().getListenersForChannelImpl(channel->iId);
}

const QSet< int > ChannelListener::getListenedChannelsForUser(unsigned int userSession) {
	return get().getListenedChannelsForUserImpl(userSession);
}

const QSet< int > ChannelListener::getListenedChannelsForUser(const User *user) {
	return get().getListenedChannelsForUserImpl(user->uiSession);
}

int ChannelListener::getListenerCountForChannel(int channelID) {
	return get().getListenerCountForChannelImpl(channelID);
}

int ChannelListener::getListenerCountForChannel(const Channel *channel) {
	return get().getListenerCountForChannelImpl(channel->iId);
}

int ChannelListener::getListenedChannelCountForUser(unsigned int userSession) {
	return get().getListenedChannelCountForUserImpl(userSession);
}

int ChannelListener::getListenedChannelCountForUser(const User *user) {
	return get().getListenedChannelCountForUserImpl(user->uiSession);
}

#ifdef MUMBLE
void ChannelListener::setListenerLocalVolumeAdjustment(int channelID, float volumeAdjustment) {
	get().setListenerLocalVolumeAdjustmentImpl(channelID, volumeAdjustment);
}

void ChannelListener::setListenerLocalVolumeAdjustment(const Channel *channel, float volumeAdjustment) {
	get().setListenerLocalVolumeAdjustmentImpl(channel->iId, volumeAdjustment);
}


float ChannelListener::getListenerLocalVolumeAdjustment(int channelID) {
	return get().getListenerLocalVolumeAdjustmentImpl(channelID);
}

float ChannelListener::getListenerLocalVolumeAdjustment(const Channel *channel) {
	return get().getListenerLocalVolumeAdjustmentImpl(channel->iId);
}

QHash< int, float > ChannelListener::getAllListenerLocalVolumeAdjustments(bool filter) {
	return get().getAllListenerLocalVolumeAdjustmentsImpl(filter);
}

void ChannelListener::setInitialServerSyncDone(bool done) {
	get().setInitialServerSyncDoneImpl(done);
}

void ChannelListener::saveToDB() {
	if (!Global::get().sh || Global::get().sh->qbaDigest.isEmpty() || Global::get().uiSession == 0) {
		// Can't save as we don't have enough context
		return;
	}

	if (!get().m_initialSyncDone.load()) {
		// If we were to save the listeners before the sync is done, we'd overwrite the list of listeners in the
		// DB with an empty set, effectively erasing all set-up listeners.
		qWarning("ChannelListener: Aborting save of user's listeners as initial ServerSync is not done yet");
		return;
	}

	// Save the currently listened channels
	Global::get().db->setChannelListeners(Global::get().sh->qbaDigest, ChannelListener::getListenedChannelsForUser(Global::get().uiSession));
	// And also the currently set volume adjustments (if they're not set to 1.0)
	Global::get().db->setChannelListenerLocalVolumeAdjustments(Global::get().sh->qbaDigest,
												   ChannelListener::getAllListenerLocalVolumeAdjustments(true));
}
#endif

void ChannelListener::clear() {
	get().clearImpl();
}