File: generic_monitor.cpp

package info (click to toggle)
kdeutils 4%3A3.5.5-3etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 21,152 kB
  • ctags: 16,106
  • sloc: cpp: 114,950; sh: 10,061; ansic: 3,182; perl: 2,835; python: 1,308; makefile: 931; xml: 303; lex: 302; yacc: 167
file content (255 lines) | stat: -rw-r--r-- 5,852 bytes parent folder | download | duplicates (2)
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
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 2; -*-
/*
   This file is part of the KDE project

   Copyright (c) 2003 Willi Richert <w.richert@gmx.net>
   Pretty much ripped off from :
	 George Staikos <staikos@kde.org> :)

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

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.

*/

#include <kgenericfactory.h>
#include <kdebug.h>

#include <sys/types.h>
#include <unistd.h>

#include "generic_monitor.h"
#include "kmilointerface.h"
#include <qmessagebox.h>
#include <qfile.h>


// FIXME: use DCOPRef where possible instead of hand-rolled DCOP calls.

using namespace KMilo;

GenericMonitor::GenericMonitor(QObject *parent, const char *name, const QStringList& args)
: Monitor(parent, name, args)
{
	_poll = false;
	m_displayType = Monitor::None;

	m_mute = false;
	m_progress = 0;
	m_minVolume = 0;
	m_maxVolume = 100;
	m_volume = 50;
}

GenericMonitor::~GenericMonitor()
{
}

bool GenericMonitor::init()
{
	static const ShortcutInfo shortcuts[] = {
		{ "FastVolumeUp", Qt::Key_VolumeUp, SLOT(fastVolumeUp()) },
		{ "FastVolumeDown", Qt::Key_VolumeDown, SLOT(fastVolumeDown()) },
		{ "SlowVolumeUp", Qt::CTRL+Qt::Key_VolumeUp, SLOT(slowVolumeUp()) },
		{ "SlowVolumeDown", Qt::CTRL+Qt::Key_VolumeDown, SLOT(slowVolumeDown()) },
		{ "Mute", KShortcut("XF86AudioMute"), SLOT(mute()) }
	};

	ga = new KGlobalAccel(this, "miloGenericAccel");

	ShortcutInfo si;
	int len = (int)sizeof(shortcuts)/sizeof(ShortcutInfo);
	for (int i = 0; i < len; i++) {
		si = shortcuts[i];

		ga->insert(si.name, QString::null, QString::null,
							 si.symbol, si.symbol,
							 this,
							 si.slot, false);
	}

	ga->readSettings();
	ga->updateConnections();

	kmixClient = new DCOPRef("kmix", "Mixer0");
	kmixWindow = new DCOPRef("kmix", "kmix-mainwindow#1");

	return true;
}

bool GenericMonitor::retrieveVolume() 
{
	bool kmix_error = false;

	DCOPReply reply = kmixClient->call("masterVolume");
	if (reply.isValid())
		m_volume = reply;
	else
		kmix_error = true;

	if (kmix_error) // maybe the error occurred because kmix wasn't running
	{
		_interface->displayText(i18n("Starting KMix..."));
		if (kapp->startServiceByDesktopName("kmix")==0) // trying to start kmix
		{
			// trying again
			reply = kmixClient->call("masterVolume");
			if (reply.isValid()) 
			{
				m_volume = reply;
				kmix_error = false;
				kmixWindow->send("hide");
			}
		}
	}
		
	if (kmix_error)
	{
		kdDebug() << "KMilo: GenericMonitor could not access kmix/Mixer0 via dcop" 
							<< endl;
		_interface->displayText(i18n("It seems that KMix is not running."));
		
		return false;
	} else {
		return true;
	}
}

void GenericMonitor::volumeUp(int step) 
{	
	if (!retrieveVolume())
		return;

	// FIXME if the mixer doesn't support steps of the specified size it
	// could get stuck at one position
	m_volume += step;
	if (m_volume > m_maxVolume)
		m_volume = m_maxVolume;

	displayVolume();
}

void GenericMonitor::volumeDown(int step)
{
	if (!retrieveVolume())
		return;

	m_volume -= step;
	if (m_volume < m_minVolume)
		m_volume = m_minVolume;
							 
	displayVolume();
}

void GenericMonitor::slowVolumeUp() {	volumeUp(1); }
void GenericMonitor::slowVolumeDown() {	volumeDown(1); }
void GenericMonitor::fastVolumeUp() {	volumeUp(10); }
void GenericMonitor::fastVolumeDown() {	volumeDown(10); }

void GenericMonitor::displayVolume()
{
	_interface->displayProgress(i18n("Volume"), m_volume);

	// If we got this far, the DCOP communication with kmix works,
  // so we don't have to test the result.
	kmixClient->send("setMasterVolume", m_volume);

	// if mute then unmute
	if (m_mute)
	{
		m_mute = false;
		kmixClient->send("setMute",  0, m_mute);
	}
}

bool GenericMonitor::retrieveMute() 
{
	bool kmix_error = false;

	DCOPReply reply = kmixClient->call("mute", 0);
	if (reply.isValid())
		m_mute = reply;
	else
		kmix_error = true;

	if (kmix_error)
	{
		// maybe the error occurred because kmix wasn't running
		_interface->displayText(i18n("Starting KMix..."));
		if (kapp->startServiceByDesktopName("kmix")==0) // trying to start kmix
		{
			// trying again
			reply = kmixClient->call("mute", 0);
			if (reply.isValid()) 
			{
				m_mute = reply;
				kmix_error = false;
				kmixWindow->send("hide");
			}
		}	else 
		{
			kmixWindow->send("hide");
			kmix_error = true;
		}
	}

	if (kmix_error)
	{
		kdDebug() << "KMilo: GenericMonitor could not access kmix/Mixer0 via dcop" 
							<< endl;
		_interface->displayText(i18n("It seems that KMix is not running."));
		
		return false;
	} else {
		return true;
	}
}

void GenericMonitor::mute() 
{
	if (!retrieveMute())
		return;

	m_mute = !m_mute;
	int newVolume;
	QString muteText;
	if (m_mute)
	{
		m_oldVolume = m_volume;
		newVolume = 0;
		muteText = i18n("Mute on");
	} else {
		newVolume = m_oldVolume;
		muteText = i18n("Mute off");
	}

	kmixClient->send("setMute",  0, m_mute);

	_interface->displayText(muteText);
}

int GenericMonitor::progress() const 
{
	return m_progress;
}

Monitor::DisplayType GenericMonitor::poll() 
{
	return m_displayType;
}


K_EXPORT_COMPONENT_FACTORY(kmilo_generic, KGenericFactory<GenericMonitor>("kmilo_generic"))

#include "generic_monitor.moc"