kmilo
generic_monitor.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <kgenericfactory.h>
00027 #include <kdebug.h>
00028
00029 #include <sys/types.h>
00030 #include <unistd.h>
00031
00032 #include "generic_monitor.h"
00033 #include "kmilointerface.h"
00034 #include <qmessagebox.h>
00035 #include <QFile>
00036 #include <ktoolinvocation.h>
00037
00038
00039
00040
00041 using namespace KMilo;
00042
00043 GenericMonitor::GenericMonitor(QObject *parent, const QStringList& args)
00044 : Monitor(parent, args)
00045 {
00046 _poll = false;
00047 m_displayType = Monitor::None;
00048
00049 m_mute = false;
00050 m_progress = 0;
00051 m_minVolume = 0;
00052 m_maxVolume = 100;
00053 m_volume = 50;
00054 }
00055
00056 GenericMonitor::~GenericMonitor()
00057 {
00058 }
00059
00060 bool GenericMonitor::init()
00061 {
00062 static const ShortcutInfo shortcuts[] = {
00063 { "FastVolumeUp", Qt::Key_VolumeUp, SLOT(fastVolumeUp()) },
00064 { "FastVolumeDown", Qt::Key_VolumeDown, SLOT(fastVolumeDown()) },
00065 { "SlowVolumeUp", Qt::CTRL+Qt::Key_VolumeUp, SLOT(slowVolumeUp()) },
00066 { "SlowVolumeDown", Qt::CTRL+Qt::Key_VolumeDown, SLOT(slowVolumeDown()) },
00067 { "Mute", Qt::Key_VolumeMute, SLOT(mute()) }
00068 };
00069
00070 ShortcutInfo si;
00071 int len = (int)sizeof(shortcuts)/sizeof(ShortcutInfo);
00072 for (int i = 0; i < len; i++) {
00073 si = shortcuts[i];
00074
00075 #ifdef __GNUC__
00076 #warning This needs porting and testing for global short cuts in KAction
00077 #endif
00078 #if 0
00079 KGA::insert(si.name, QString(), QString(),
00080 si.symbol,
00081 this,
00082 si.slot, false);
00083 #endif
00084
00085 }
00086
00087 KGlobalAccel::self()->readSettings();
00088
00089 kmixClient = new DCOPRef("kmix", "Mixer0");
00090 kmixWindow = new DCOPRef("kmix", "kmix-mainwindow#1");
00091
00092 return true;
00093 }
00094
00095 bool GenericMonitor::retrieveVolume()
00096 {
00097 bool kmix_error = false;
00098
00099 DCOPReply reply = kmixClient->call("absoluteVolume", 0);
00100 if (reply.isValid())
00101 m_volume = reply;
00102 else
00103 kmix_error = true;
00104
00105 if (kmix_error)
00106 {
00107 _interface->displayText(i18n("Starting KMix..."));
00108 if (KToolInvocation::startServiceByDesktopName("kmix")==0)
00109 {
00110 reply = kmixClient->call("absoluteVolume", 0);
00111 if (reply.isValid())
00112 {
00113 m_volume = reply;
00114 kmix_error = false;
00115 kmixWindow->send("hide");
00116 }
00117 }
00118 }
00119
00120 if (kmix_error)
00121 {
00122 kDebug() << "KMilo: GenericMonitor could not access kmix/Mixer0 via dcop"
00123 << endl;
00124 _interface->displayText(i18n("It seems that KMix is not running."));
00125
00126 return false;
00127 } else {
00128 reply = kmixClient->call("absoluteVolumeMax", 0);
00129 m_maxVolume = reply;
00130 reply = kmixClient->call("absoluteVolumeMin", 0);
00131 m_minVolume = reply;
00132 return true;
00133 }
00134 }
00135
00136 void GenericMonitor::volumeUp(int step)
00137 {
00138 if (!retrieveVolume())
00139 return;
00140
00141 m_volume += (int)((m_maxVolume - m_minVolume) * step /100) +1;
00142 if (m_volume > m_maxVolume)
00143 m_volume = m_maxVolume;
00144
00145 displayVolume();
00146 }
00147
00148 void GenericMonitor::volumeDown(int step)
00149 {
00150 if (!retrieveVolume())
00151 return;
00152
00153 m_volume -= (int)((m_maxVolume - m_minVolume) * step /100) +1;
00154 if (m_volume < m_minVolume)
00155 m_volume = m_minVolume;
00156
00157 displayVolume();
00158 }
00159
00160 void GenericMonitor::slowVolumeUp() { volumeUp(1); }
00161 void GenericMonitor::slowVolumeDown() { volumeDown(1); }
00162 void GenericMonitor::fastVolumeUp() { volumeUp(10); }
00163 void GenericMonitor::fastVolumeDown() { volumeDown(10); }
00164
00165 void GenericMonitor::displayVolume()
00166 {
00167 _interface->displayProgress(i18n("Volume"), (int)(m_volume * 100 / (m_maxVolume - m_minVolume)));
00168
00169
00170
00171 kmixClient->send("setAbsoluteVolume", 0, m_volume);
00172
00173
00174 if (m_mute)
00175 {
00176 m_mute = false;
00177 kmixClient->send("setMute", 0, m_mute);
00178 }
00179 }
00180
00181 bool GenericMonitor::retrieveMute()
00182 {
00183 bool kmix_error = false;
00184
00185 DCOPReply reply = kmixClient->call("mute", 0);
00186 if (reply.isValid())
00187 m_mute = reply;
00188 else
00189 kmix_error = true;
00190
00191 if (kmix_error)
00192 {
00193
00194 _interface->displayText(i18n("Starting KMix..."));
00195 if (KToolInvocation::startServiceByDesktopName("kmix")==0)
00196 {
00197
00198 reply = kmixClient->call("mute", 0);
00199 if (reply.isValid())
00200 {
00201 m_mute = reply;
00202 kmix_error = false;
00203 kmixWindow->send("hide");
00204 }
00205 } else
00206 {
00207 kmixWindow->send("hide");
00208 kmix_error = true;
00209 }
00210 }
00211
00212 if (kmix_error)
00213 {
00214 kDebug() << "KMilo: GenericMonitor could not access kmix/Mixer0 via dcop"
00215 << endl;
00216 _interface->displayText(i18n("It seems that KMix is not running."));
00217
00218 return false;
00219 } else {
00220 return true;
00221 }
00222 }
00223
00224 void GenericMonitor::mute()
00225 {
00226 if (!retrieveMute())
00227 return;
00228
00229 m_mute = !m_mute;
00230 int newVolume;
00231 QString muteText;
00232 if (m_mute)
00233 {
00234 m_oldVolume = m_volume;
00235 newVolume = 0;
00236 muteText = i18n("Mute on");
00237 } else {
00238 newVolume = m_oldVolume;
00239 muteText = i18n("Mute off");
00240 }
00241
00242 kmixClient->send("setMute", 0, m_mute);
00243
00244 _interface->displayText(muteText);
00245 }
00246
00247 int GenericMonitor::progress() const
00248 {
00249 return m_progress;
00250 }
00251
00252 Monitor::DisplayType GenericMonitor::poll()
00253 {
00254 return m_displayType;
00255 }
00256
00257
00258 K_EXPORT_COMPONENT_FACTORY(kmilo_generic, KGenericFactory<GenericMonitor>("kmilo_generic"))
00259
00260 #include "generic_monitor.moc"