File: ControlManager.cpp

package info (click to toggle)
kmix 4%3A20.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,504 kB
  • sloc: cpp: 14,313; xml: 408; sh: 97; makefile: 7
file content (190 lines) | stat: -rw-r--r-- 6,267 bytes parent folder | download
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
/*
 KMix -- KDE's full featured mini mixer
 Copyright (C) 2012  Christian Esken <esken@kde.org>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "ControlManager.h"
#include "core/GlobalConfig.h"
#include "kmix_debug.h"

#include <QMutableListIterator>


ControlManager ControlManager::instanceSingleton;

ControlManager &ControlManager::instance()
{
	return instanceSingleton;
}

ControlManager::ControlManager()
{
	listenersChanged = false;
}

/**
 * Announce a change for one or all mixers.
 *
 * @param mixerId The mixerId. Use an empty QString() to announce a change for all mixers
 * @param changeType A bit array of ControlChangeType flags
 * @param sourceId Only for logging
 *
 */
void ControlManager::announce(const QString &mixerId, ControlManager::ChangeType changeType, const QString &sourceId)
{
	bool listenersModified = false;
	QSet<Listener *> processedListeners;
	do
	{
		listenersModified = false;
		for (QList<Listener *>::const_iterator it = listeners.constBegin(); it!=listeners.constEnd(); ++it)
		{
			Listener *listener = (*it);

			bool mixerIsOfInterest = listener->getMixerId().isEmpty() || mixerId.isEmpty()
				|| listener->getMixerId() == mixerId;

			bool listenerAlreadyProcesed = processedListeners.contains(listener);
			if ( listenerAlreadyProcesed )
			{
				if (GlobalConfig::instance().data.debugControlManager)
					qCDebug(KMIX_LOG) << "Skipping already processed listener";
				continue;
			}
			if (mixerIsOfInterest && listener->getChangeType() == changeType)
			{
				bool success = QMetaObject::invokeMethod(listener->getTarget(),
									 "controlsChange",
									 Qt::DirectConnection,
									 Q_ARG(ControlManager::ChangeType, changeType));
				if (GlobalConfig::instance().data.debugControlManager)
				{
					qCDebug(KMIX_LOG) << "Listener" << listener->getSourceId()
							  << "is interested in" << mixerId
							  << "type" << changeType;
				}

				if (!success)
				{
					qCCritical(KMIX_LOG) << "Listener failed to send to " << listener->getTarget()->metaObject()->className();
				}
				processedListeners.insert(listener);
				if (listenersChanged)
				{
					// The invokeMethod() above has changed the listeners => my Iterator is invalid => restart loop
					if (GlobalConfig::instance().data.debugControlManager)
						qCDebug(KMIX_LOG) << "Listeners modified => restart loop";
					listenersChanged = false;
					listenersModified = true;
					break; // break inner loop => restart via outer loop
				}

			}
		}
	}
	while (listenersModified);

	if (GlobalConfig::instance().data.debugControlManager)
	{
		qCDebug(KMIX_LOG)
		<< "Announcing" << changeType << "for"
		<< (mixerId.isEmpty() ? "all cards" : mixerId) << "by" << sourceId;
	}
}

/**
 * Adds a listener for the given mixerId and changeType.
 * Listeners are informed about all corresponding changes via a signal.
 * Listeners are not informed about changes that originate from oneself (according to sourceId).
 *
 * @param mixerId The id of the Mixer you are interested in
 * @param changetType The changeType of interest
 * @param target The QObject, where the notification signal is sent to. It must implement the SLOT controlChanged(QString mixerId,ControlManager::ChangeType changeType).
 * @param sourceId Only for logging
 */
void ControlManager::addListener(const QString &mixerId, ControlManager::ChangeTypes changeTypes,
				 QObject *target, const QString &sourceId)
{
	if (GlobalConfig::instance().data.debugControlManager)
	{
		qCDebug(KMIX_LOG)
		<< "Listening to" << changeTypes << "for"
		<< (mixerId.isEmpty() ? "all cards" : mixerId) << "by" << sourceId
		<< "sent to" << target;
	}

	for (ControlManager::ChangeType ct = ChangeType::First; ct!=ChangeType::Last;
	      ct = static_cast<ControlManager::ChangeType>(ct << 1))
	{
		if (changeTypes & ct)
		{
			// Add all listeners.
			Listener *listener = new Listener(mixerId, ct, target, sourceId);
			listeners.append(listener);
			listenersChanged = true;
		}
	}
	if (GlobalConfig::instance().data.debugControlManager)
	{
		qCDebug(KMIX_LOG) << "We now have" << listeners.size() << "listeners";
	}
}

/**
 * Removes all listeners of the given target.
 * @param target The QObject that was used to register via addListener()
 * @param sourceId Optional: Only for logging
 */
void ControlManager::removeListener(QObject *target, const QString &sourceId)
{
	QString src = sourceId;
	if (src.isEmpty()) src = target->metaObject()->className();

	QMutableListIterator<Listener *> it(listeners);
	while (it.hasNext())
	{
		Listener *listener = it.next();
		if (listener->getTarget() == target)
		{
			if (GlobalConfig::instance().data.debugControlManager)
				qCDebug(KMIX_LOG)
				<< "Stop Listening of" << listener->getSourceId() << "requested by" << src << "from" << target;
			it.remove();
			// Hint: As we have actual objects no explicit delete is needed
			listenersChanged = true;
		}
	}
}

void ControlManager::warnUnexpectedChangeType(ControlManager::ChangeType type, QObject *obj)
{
	qCWarning(KMIX_LOG) << "Unexpected type " << type << " received by " << obj->metaObject()->className();
}

void ControlManager::shutdownNow()
{
	if (GlobalConfig::instance().data.debugControlManager)
		qCDebug(KMIX_LOG) << "Shutting down ControlManager";
	for (QList<Listener *>::const_iterator it = listeners.constBegin(); it!=listeners.constEnd(); ++it)
	{
		Listener *listener = (*it);
		if (GlobalConfig::instance().data.debugControlManager)
			qCDebug(KMIX_LOG)
			<< "Listener still connected. Closing it. source" << listener->getSourceId()
			<< "target" << listener->getTarget()->metaObject()->className();
	}
}