not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / bell / bell.cpp
blob87dbd49197a1da1bcc9434728275f8ab74b3388f
1 /*
2 Copyright (c) 1997 Christian Czezatke (e9025461@student.tuwien.ac.at)
3 1998 Bernd Wuebben <wuebben@kde.org>
4 2000 Matthias Elter <elter@kde.org>
5 2001 Carsten PFeiffer <pfeiffer@kde.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <QCheckBox>
23 #include <QPushButton>
26 //Added by qt3to4:
27 #include <QVBoxLayout>
28 #include <QHBoxLayout>
29 #include <QGridLayout>
30 #include <QBoxLayout>
31 #include <QGroupBox>
33 #include <kcmodule.h>
34 #include <kaboutdata.h>
35 #include <kapplication.h>
36 #include <kconfig.h>
37 #include <kdialog.h>
38 #include <knotification.h>
39 #include <klocale.h>
40 #include <knuminput.h>
42 #include "bell.h"
43 #include "bell.moc"
45 #include <X11/Xlib.h>
46 #include <QX11Info>
47 #include <kpluginfactory.h>
48 #include <kpluginloader.h>
52 K_PLUGIN_FACTORY(KBellConfigFactory, registerPlugin<KBellConfig>();)
53 K_EXPORT_PLUGIN(KBellConfigFactory("kcmbell"))
57 extern "C"
59 KDE_EXPORT void kcminit_bell()
61 XKeyboardState kbd;
62 XKeyboardControl kbdc;
64 XGetKeyboardControl(QX11Info::display(), &kbd);
66 KConfig _config( "kcmbellrc", KConfig::NoGlobals );
67 KConfigGroup config(&_config, "General");
69 kbdc.bell_percent = config.readEntry("Volume", kbd.bell_percent);
70 kbdc.bell_pitch = config.readEntry("Pitch", kbd.bell_pitch);
71 kbdc.bell_duration = config.readEntry("Duration", kbd.bell_duration);
72 XChangeKeyboardControl(QX11Info::display(),
73 KBBellPercent | KBBellPitch | KBBellDuration,
74 &kbdc);
78 KBellConfig::KBellConfig(QWidget *parent, const QVariantList &args):
79 KCModule(KBellConfigFactory::componentData(), parent, args)
81 QBoxLayout *layout = new QVBoxLayout(this);
82 layout->setMargin(0);
83 layout->setSpacing(KDialog::spacingHint());
85 int row = 0;
86 QGroupBox *box = new QGroupBox(i18n("Bell Settings"), this );
87 new QVBoxLayout( box );
88 layout->addWidget(box);
89 layout->addStretch();
90 QGridLayout *grid = new QGridLayout();
91 grid->setSpacing( KDialog::spacingHint() );
92 box->layout()->addItem( grid );
93 grid->setColumnStretch(0, 0);
94 grid->setColumnStretch(1, 1);
95 grid->addItem(new QSpacerItem(30, 0), 0, 0);
97 m_useBell = new QCheckBox( i18n("&Use system bell instead of system notification" ), box );
98 m_useBell->setWhatsThis( i18n("You can use the standard system bell (PC speaker) or a "
99 "more sophisticated system notification, see the "
100 "\"System Notifications\" control module for the "
101 "\"Something Special Happened in the Program\" event."));
102 connect(m_useBell, SIGNAL( toggled( bool )), SLOT( useBell( bool )));
103 row++;
104 grid->addWidget(m_useBell, row, 0, 1, 2 );
106 setQuickHelp( i18n("<h1>System Bell</h1> Here you can customize the sound of the standard system bell,"
107 " i.e. the \"beep\" you always hear when there is something wrong. Note that you can further"
108 " customize this sound using the \"Accessibility\" control module; for example, you can choose"
109 " a sound file to be played instead of the standard bell."));
111 m_volume = new KIntNumInput(50, box);
112 m_volume->setLabel(i18n("&Volume:"));
113 m_volume->setRange(0, 100, 5);
114 m_volume->setSuffix("%");
115 m_volume->setSteps(5,25);
116 grid->addWidget(m_volume, ++row, 1);
117 m_volume->setWhatsThis( i18n("Here you can customize the volume of the system bell. For further"
118 " customization of the bell, see the \"Accessibility\" control module.") );
120 m_pitch = new KIntNumInput(800, box);
121 m_pitch->setLabel(i18n("&Pitch:"));
122 m_pitch->setRange(20, 2000, 20);
123 m_pitch->setSuffix(i18n(" Hz"));
124 m_pitch->setSteps(40,200);
125 grid->addWidget(m_pitch, ++row, 1);
126 m_pitch->setWhatsThis( i18n("Here you can customize the pitch of the system bell. For further"
127 " customization of the bell, see the \"Accessibility\" control module.") );
129 m_duration = new KIntNumInput(100, box);
130 m_duration->setLabel(i18n("&Duration:"));
131 m_duration->setRange(1, 1000, 50);
132 m_duration->setSuffix(i18n(" msec"));
133 m_duration->setSteps(20,100);
134 grid->addWidget(m_duration, ++row, 1);
135 m_duration->setWhatsThis( i18n("Here you can customize the duration of the system bell. For further"
136 " customization of the bell, see the \"Accessibility\" control module.") );
138 QBoxLayout *boxLayout = new QHBoxLayout();
139 m_testButton = new QPushButton(i18n("&Test"), box);
140 m_testButton->setObjectName("test");
141 boxLayout->addWidget(m_testButton, 0, Qt::AlignRight);
142 grid->addLayout( boxLayout, ++row, 1 );
143 connect( m_testButton, SIGNAL(clicked()), SLOT(ringBell()));
144 m_testButton->setWhatsThis( i18n("Click \"Test\" to hear how the system bell will sound using your changed settings.") );
146 // watch for changes
147 connect(m_volume, SIGNAL(valueChanged(int)), SLOT(changed()));
148 connect(m_pitch, SIGNAL(valueChanged(int)), SLOT(changed()));
149 connect(m_duration, SIGNAL(valueChanged(int)), SLOT(changed()));
151 KAboutData *about =
152 new KAboutData(I18N_NOOP("kcmbell"), 0, ki18n("KDE Bell Control Module"),
153 0, KLocalizedString(), KAboutData::License_GPL,
154 ki18n("(c) 1997 - 2001 Christian Czezatke, Matthias Elter"));
156 about->addAuthor(ki18n("Christian Czezatke"), ki18n("Original author"), "e9025461@student.tuwien.ac.at");
157 about->addAuthor(ki18n("Bernd Wuebben"), KLocalizedString(), "wuebben@kde.org");
158 about->addAuthor(ki18n("Matthias Elter"), ki18n("Current maintainer"), "elter@kde.org");
159 about->addAuthor(ki18n("Carsten Pfeiffer"), KLocalizedString(), "pfeiffer@kde.org");
160 setAboutData(about);
163 void KBellConfig::load()
165 XKeyboardState kbd;
166 XGetKeyboardControl(QX11Info::display(), &kbd);
168 m_volume->setValue(kbd.bell_percent);
169 m_pitch->setValue(kbd.bell_pitch);
170 m_duration->setValue(kbd.bell_duration);
172 KConfig _cfg("kdeglobals", KConfig::NoGlobals);
173 KConfigGroup cfg(&_cfg, "General");
174 m_useBell->setChecked(cfg.readEntry("UseSystemBell", false));
175 useBell(m_useBell->isChecked());
176 emit changed(false);
179 void KBellConfig::save()
181 XKeyboardControl kbd;
183 int bellVolume = m_volume->value();
184 int bellPitch = m_pitch->value();
185 int bellDuration = m_duration->value();
187 kbd.bell_percent = bellVolume;
188 kbd.bell_pitch = bellPitch;
189 kbd.bell_duration = bellDuration;
190 XChangeKeyboardControl(QX11Info::display(),
191 KBBellPercent | KBBellPitch | KBBellDuration,
192 &kbd);
194 KConfig _config("kcmbellrc", KConfig::NoGlobals);
195 KConfigGroup config(&_config, "General");
196 config.writeEntry("Volume",bellVolume);
197 config.writeEntry("Pitch",bellPitch);
198 config.writeEntry("Duration",bellDuration);
200 config.sync();
202 KConfig _cfg("kdeglobals", KConfig::NoGlobals);
203 KConfigGroup cfg(&_cfg, "General");
204 cfg.writeEntry("UseSystemBell", m_useBell->isChecked());
205 cfg.sync();
207 if (!m_useBell->isChecked())
209 KConfig config("kaccessrc");
211 KConfigGroup group = config.group("Bell");
212 group.writeEntry("SystemBell", false);
213 group.writeEntry("ArtsBell", false);
214 group.writeEntry("VisibleBell", false);
218 void KBellConfig::ringBell()
220 if (!m_useBell->isChecked()) {
221 KNotification::beep(QString(), this);
222 return;
225 // store the old state
226 XKeyboardState old_state;
227 XGetKeyboardControl(QX11Info::display(), &old_state);
229 // switch to the test state
230 XKeyboardControl kbd;
231 kbd.bell_percent = m_volume->value();
232 kbd.bell_pitch = m_pitch->value();
233 if (m_volume->value() > 0)
234 kbd.bell_duration = m_duration->value();
235 else
236 kbd.bell_duration = 0;
237 XChangeKeyboardControl(QX11Info::display(),
238 KBBellPercent | KBBellPitch | KBBellDuration,
239 &kbd);
240 // ring bell
241 XBell(QX11Info::display(),0);
243 // restore old state
244 kbd.bell_percent = old_state.bell_percent;
245 kbd.bell_pitch = old_state.bell_pitch;
246 kbd.bell_duration = old_state.bell_duration;
247 XChangeKeyboardControl(QX11Info::display(),
248 KBBellPercent | KBBellPitch | KBBellDuration,
249 &kbd);
252 void KBellConfig::defaults()
254 m_volume->setValue(100);
255 m_pitch->setValue(800);
256 m_duration->setValue(100);
257 m_useBell->setChecked( false );
258 useBell( false );
261 void KBellConfig::useBell( bool on )
263 m_volume->setEnabled( on );
264 m_pitch->setEnabled( on );
265 m_duration->setEnabled( on );
266 m_testButton->setEnabled( on );
267 changed();