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 "battery.h"
00027 #include "version.h"
00028 #include "portable.h"
00029
00030
00031 #include <klocale.h>
00032 #include <kconfig.h>
00033 #include <knuminput.h>
00034 #include <kiconloader.h>
00035 #include <kicondialog.h>
00036 #include <kapplication.h>
00037 #include <kmessagebox.h>
00038 #include <krichtextlabel.h>
00039
00040
00041 #include <QLayout>
00042 #include <QLabel>
00043 #include <QCheckBox>
00044 #include <q3hbox.h>
00045 #include <q3grid.h>
00046 #include <Q3GroupBox>
00047 #include <QPushButton>
00048
00049
00050 #include <QPixmap>
00051 #include <QHBoxLayout>
00052 #include <QTimerEvent>
00053 #include <QVBoxLayout>
00054 extern void wake_laptop_daemon();
00055
00056
00057 BatteryConfig::BatteryConfig (const KComponentData &inst,QWidget * parent)
00058 : KCModule(inst,parent),
00059 editPoll(0),
00060 iconloader(0),
00061 buttonNoBattery(0),
00062 buttonNoCharge(0),
00063 buttonCharge(0),
00064 instance("klaptopdaemon")
00065 {
00066 KGlobal::locale()->insertCatalog("klaptopdaemon");
00067
00068 apm = laptop_portable::has_power_management();
00069 config = new KConfig("kcmlaptoprc");
00070
00071 QVBoxLayout *top_layout = new QVBoxLayout( this );
00072 top_layout->setMargin( KDialog::marginHint());
00073 top_layout->setSpacing( KDialog::spacingHint() );
00074
00075
00076 runMonitor = new QCheckBox( i18n("&Show battery monitor"), this );
00077 top_layout->addWidget( runMonitor );
00078 runMonitor->setToolTip( i18n( "This box enables the battery state icon in the panel" ) );
00079 connect( runMonitor, SIGNAL(clicked()), this, SLOT(configChanged()) );
00080
00081 notifyMe = new QCheckBox( i18n("&Notify me whenever my battery becomes fully charged"), this );
00082 top_layout->addWidget( notifyMe );
00083 notifyMe->setToolTip( i18n( "This box enables a dialog box that pops up when your battery becomes fully charged" ) );
00084 connect( notifyMe, SIGNAL(clicked()), this, SLOT(configChanged()) );
00085
00086 blankSaver = new QCheckBox( i18n("&Use a blank screen saver when running on battery"), this );
00087 top_layout->addWidget( blankSaver );
00088 connect( blankSaver, SIGNAL(clicked()), this, SLOT(configChanged()) );
00089
00090 if (!apm) {
00091 top_layout->addWidget( laptop_portable::no_power_management_explanation(this) );
00092 } else {
00093 iconloader = new KIconLoader("klaptopdaemon");
00094
00095
00096 QWidget *hb = new QWidget(this );
00097 QHBoxLayout *hboxLayout1 = new QHBoxLayout(hb);
00098 hb->setLayout(hboxLayout1);
00099 hboxLayout1->setSpacing( KDialog::spacingHint() );
00100 top_layout->addWidget( hb );
00101
00102 QLabel* poll_label = new QLabel( i18n("&Check status every:"), hb );
00103 editPoll = new QSpinBox( 1, 3600, 1, hb );
00104 editPoll->setToolTip( i18n( "Choose how responsive the laptop software will be when it checks the battery status" ) );
00105 editPoll->setSuffix( i18nc("keep short, unit in spinbox", "sec") );
00106 poll_label->setBuddy( editPoll );
00107 connect( editPoll, SIGNAL(valueChanged(int)),
00108 this, SLOT(configChanged()) );
00109 QWidget* spacer = new QWidget( hb );
00110 hboxLayout1->setStretchFactor( spacer, 1 );
00111
00112
00113 Q3GroupBox* icons_groupbox = new Q3GroupBox (1, Qt::Horizontal, i18n("Select Battery Icons"), this );
00114 icons_groupbox->layout()->setSpacing( KDialog::spacingHint() );
00115 top_layout->addWidget( icons_groupbox, 0, Qt::AlignLeft );
00116
00117
00118 Q3Grid *icon_grid = new Q3Grid( 3 , icons_groupbox );
00119 icon_grid->setSpacing( KDialog::spacingHint() );
00120
00121 buttonNoBattery = new KIconButton( iconloader, icon_grid );
00122 buttonNoCharge = new KIconButton( iconloader, icon_grid );
00123 buttonCharge = new KIconButton( iconloader, icon_grid );
00124 (void)new QLabel( buttonNoBattery, i18n("No &battery"), icon_grid);
00125 (void)new QLabel( buttonNoCharge, i18n("&Not charging"), icon_grid);
00126 (void)new QLabel( buttonCharge, i18n("Char&ging"), icon_grid);
00127 buttonNoBattery->setIconType( KIconLoader::NoGroup, KIconLoader::Any, 1);
00128 buttonNoCharge->setIconType( KIconLoader::NoGroup, KIconLoader::Any, 1);
00129 buttonCharge->setIconType( KIconLoader::NoGroup, KIconLoader::Any, 1);
00130 connect(buttonNoBattery, SIGNAL(iconChanged(QString)), this, SLOT(iconChanged()));
00131 connect(buttonNoCharge, SIGNAL(iconChanged(QString)), this, SLOT(iconChanged()));
00132 connect(buttonCharge, SIGNAL(iconChanged(QString)), this, SLOT(configChanged()));
00133
00134
00135 int num_batteries;
00136 QStringList battery_names, battery_states, battery_values;
00137 laptop_portable::get_battery_status(num_batteries, battery_names, battery_states, battery_values);
00138 if (num_batteries > 0) {
00139 QHBoxLayout *hl = new QHBoxLayout();
00140 top_layout->addLayout(hl);
00141
00142 Q3GroupBox *hb = new Q3GroupBox(1, Qt::Vertical ,i18n("Current Battery Status"), this);
00143 for (int i = 0; i < num_batteries; i++) {
00144
00145 QWidget *wp;
00146 if (num_batteries == 1) {
00147 wp = new QWidget(hb);
00148 } else {
00149 wp = new Q3GroupBox(battery_names[i], hb);
00150 }
00151 QVBoxLayout *vb = new QVBoxLayout(wp);
00152
00153 QLabel *l;
00154
00155 l = new QLabel(wp);
00156 vb->addWidget(l);
00157 batt_label_1.append(l);
00158
00159 l = new QLabel(QString(""), wp);
00160 vb->addWidget(l);
00161 batt_label_2.append(l);
00162
00163 l = new QLabel(QString(""), wp);
00164 vb->addWidget(l);
00165 batt_label_3.append(l);
00166 }
00167 hl->addWidget(hb);
00168 hl->addStretch(1);
00169 (void)startTimer(30*1000);
00170 }
00171
00172
00173 QLabel* explain = new KRichTextLabel( i18n("This panel controls whether the battery status monitor\nappears in the system tray and what it looks like.").replace("\n"," "), this);
00174 top_layout->addWidget(explain, 0);
00175 laptop_portable::extra_config(this, config, top_layout);
00176 }
00177
00178 top_layout->addStretch(1);
00179 startMonitor = new QPushButton( i18n("&Start Battery Monitor"), this);
00180 connect(startMonitor, SIGNAL(clicked()), this, SLOT(slotStartMonitor()));
00181 top_layout->addWidget( startMonitor, 0, Qt::AlignRight );
00182
00183 load();
00184 }
00185
00186 BatteryConfig::~BatteryConfig()
00187 {
00188 delete config;
00189 }
00190
00191
00192 void BatteryConfig::save()
00193 {
00194 enablemonitor = runMonitor->isChecked();
00195 useblanksaver = blankSaver->isChecked();
00196 notifyme = notifyMe->isChecked();
00197
00198 if (apm) {
00199 poll_time = editPoll->value();
00200 nobattery = buttonNoBattery->icon();
00201 chargebattery = buttonCharge->icon();
00202 nochargebattery = buttonNoCharge->icon();
00203 }
00204 config->setGroup("BatteryDefault");
00205
00206 config->writeEntry("Enable", enablemonitor);
00207 config->writeEntry("NotifyMe", notifyme);
00208 config->writeEntry("BlankSaver", useblanksaver);
00209 config->writeEntry("Poll", poll_time);
00210 config->writeEntry("NoBatteryPixmap", nobattery);
00211 config->writeEntry("ChargePixmap", chargebattery);
00212 config->writeEntry("NoChargePixmap", nochargebattery);
00213 config->sync();
00214 changed(false);
00215 wake_laptop_daemon();
00216 }
00217
00218 void BatteryConfig::load()
00219 {
00220 config->setGroup("BatteryDefault");
00221
00222 poll_time = config->readEntry("Poll", 20);
00223 enablemonitor = config->readEntry("Enable", true);
00224 notifyme = config->readEntry("NotifyMe", false);
00225 useblanksaver = config->readEntry("BlankSaver", false);
00226
00227 nobattery = config->readEntry("NoBatteryPixmap", "battery-missing");
00228 nochargebattery = config->readEntry("NoChargePixmap", "laptop_nocharge");
00229 chargebattery = config->readEntry("ChargePixmap", "battery-charging");
00230
00231 runMonitor->setChecked(enablemonitor);
00232 blankSaver->setChecked(useblanksaver);
00233 notifyMe->setChecked(notifyme);
00234 if (apm) {
00235 editPoll->setValue(poll_time);
00236 buttonNoCharge->setIcon(nochargebattery);
00237 buttonCharge->setIcon(chargebattery);
00238 buttonNoBattery->setIcon(nobattery);
00239 }
00240 battery_pm = SmallIcon(nochargebattery, 20, KIconLoader::DefaultState, instance);
00241 battery_nopm = SmallIcon(nobattery, 20, KIconLoader::DefaultState, instance);
00242 changed(false);
00243 BatteryStateUpdate();
00244 }
00245
00246 void BatteryConfig::defaults()
00247 {
00248 poll_time = 20;
00249 enablemonitor = true;
00250 useblanksaver = false;
00251 notifyme = false;
00252
00253 nobattery = "battery-missing";
00254 nochargebattery = "laptop_nocharge";
00255 chargebattery = "battery-charging";
00256
00257 runMonitor->setChecked(enablemonitor);
00258 notifyMe->setChecked(notifyme);
00259 blankSaver->setChecked(useblanksaver);
00260 if (apm) {
00261 editPoll->setValue(poll_time);
00262 buttonNoCharge->setIcon(nochargebattery);
00263 buttonCharge->setIcon(chargebattery);
00264 buttonNoBattery->setIcon(nobattery);
00265 }
00266 battery_pm = SmallIcon(nochargebattery, 20, KIconLoader::DefaultState, instance);
00267 battery_nopm = SmallIcon(nobattery, 20, KIconLoader::DefaultState, instance);
00268 changed(true);
00269 BatteryStateUpdate();
00270 }
00271
00272
00273 void BatteryConfig::configChanged()
00274 {
00275 emit changed(true);
00276 }
00277
00278
00279 QString BatteryConfig::quickHelp() const
00280 {
00281 return i18n("<h1>Laptop Battery</h1>This module allows you to monitor "
00282 "your batteries. To make use of this module, you must have power management system software "
00283 "installed. (And, of course, you should have batteries in your machine.)");
00284 }
00285
00286
00287 void BatteryConfig::slotStartMonitor()
00288 {
00289 wake_laptop_daemon();
00290 if (!enablemonitor) {
00291 KMessageBox::information(0, i18n("<qt>The battery monitor has been started, but the tray icon is currently disabled. You can make it appear by selecting the <b>Show battery monitor</b> entry on this page and applying your changes.</qt>"), QString(), "howToEnableMonitor");
00292 }
00293 }
00294
00295 void
00296 BatteryConfig::ConvertIcon(int percent, QPixmap &pm, QPixmap &result)
00297 {
00298 QImage image = pm.toImage();
00299
00300 int w = image.width();
00301 int h = image.height();
00302 int count = 0;
00303 QRgb rgb;
00304 int x, y;
00305 for (x = 0; x < w; x++)
00306 for (y = 0; y < h; y++) {
00307 rgb = image.pixel(x, y);
00308 if (qRed(rgb) == 0xff &&
00309 qGreen(rgb) == 0xff &&
00310 qBlue(rgb) == 0xff)
00311 count++;
00312 }
00313 int c = (count*percent)/100;
00314 if (percent == 100) {
00315 c = count;
00316 } else
00317 if (percent != 100 && c == count)
00318 c = count-1;
00319
00320
00321 if (c) {
00322 uint ui;
00323 QRgb blue = qRgb(0x00,0x00,0xff);
00324
00325 if (image.depth() <= 8) {
00326 ui = image.numColors();
00327 image.setNumColors(ui+1);
00328 image.setColor(ui, blue);
00329 } else {
00330 ui = 0xff000000|blue;
00331 }
00332
00333 for (y = h-1; y >= 0; y--)
00334 for (x = 0; x < w; x++) {
00335 rgb = image.pixel(x, y);
00336 if (qRed(rgb) == 0xff &&
00337 qGreen(rgb) == 0xff &&
00338 qBlue(rgb) == 0xff) {
00339 image.setPixel(x, y, ui);
00340 c--;
00341 if (c <= 0)
00342 goto quit;
00343 }
00344 }
00345 }
00346 quit:
00347
00348 result = QPixmap::fromImage(image);
00349 }
00350
00351 void
00352 BatteryConfig::BatteryStateUpdate()
00353 {
00354 int num_batteries;
00355 QStringList battery_names, battery_states, battery_values;
00356 laptop_portable::get_battery_status(num_batteries, battery_names, battery_states, battery_values);
00357 if (num_batteries > 0) {
00358 for (int i = 0; i < num_batteries; i++) {
00359 if (battery_states[i] == "yes") {
00360 QPixmap result;
00361 ConvertIcon(battery_values[i].toInt(), battery_pm, result);
00362 batt_label_1.at(i)->setPixmap(result);
00363
00364 batt_label_2.at(i)->setText(battery_values[i]+"%");
00365
00366 batt_label_3.at(i)->setText(i18n("Present"));
00367 } else {
00368 batt_label_1.at(i)->setPixmap(battery_nopm);
00369
00370 batt_label_2.at(i)->setText("");
00371
00372 batt_label_3.at(i)->setText(i18n("Not present"));
00373 }
00374 }
00375 }
00376 }
00377
00378 void BatteryConfig::iconChanged()
00379 {
00380 nobattery = buttonNoBattery->icon();
00381 nochargebattery = buttonNoCharge->icon();
00382 battery_pm = SmallIcon(nochargebattery, 20, KIconLoader::DefaultState, instance);
00383 battery_nopm = SmallIcon(nobattery, 20, KIconLoader::DefaultState, instance);
00384 emit changed(true);
00385 BatteryStateUpdate();
00386 }
00387
00388 void BatteryConfig::timerEvent(QTimerEvent *)
00389 {
00390 BatteryStateUpdate();
00391 }
00392
00393 #include "battery.moc"
00394
00395