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 "apm.h"
00027 #include "version.h"
00028 #include "portable.h"
00029 #include <stdlib.h>
00030 #include <unistd.h>
00031
00032
00033 #include <klocale.h>
00034 #include <kconfig.h>
00035 #include <knuminput.h>
00036 #include <kiconloader.h>
00037 #include <kicondialog.h>
00038 #include <kapplication.h>
00039 #include <kmessagebox.h>
00040 #include <kstandarddirs.h>
00041 #include <kprocess.h>
00042
00043
00044 #include <QLayout>
00045 #include <QLabel>
00046 #include <QCheckBox>
00047 #include <q3hbox.h>
00048
00049 #include <q3grid.h>
00050 #include <QPushButton>
00051
00052
00053 #include <QVBoxLayout>
00054 #include <QHBoxLayout>
00055 extern void wake_laptop_daemon();
00056
00057 ApmConfig::ApmConfig (const KComponentData &inst,QWidget * parent)
00058 : KCModule(inst,parent)
00059 {
00060 KGlobal::locale()->insertCatalog("klaptopdaemon");
00061
00062 config = new KConfig("kcmlaptoprc");
00063
00064 QVBoxLayout *top_layout = new QVBoxLayout( this );
00065 top_layout->setMargin( KDialog::marginHint() );
00066 top_layout->setSpacing( KDialog::spacingHint() );
00067
00068 QLabel *tmp_label = new QLabel( i18n("This panel lets you configure your APM system and lets "
00069 "you have access to some of the extra features provided by it"), this );
00070 tmp_label->setWordWrap(true);
00071 top_layout->addWidget( tmp_label );
00072
00073 tmp_label = new QLabel( i18n("NOTE: some APM implementations have buggy suspend/standby "
00074 "implementations. You should test these features very gingerly - save "
00075 "all your work, check them on and try a suspend/standby from "
00076 "the popup menu on the battery icon in the panel if it fails to come "
00077 "back successfully uncheck the box again."), this );
00078 tmp_label->setWordWrap(true);
00079 top_layout->addWidget( tmp_label );
00080
00081 tmp_label = new QLabel( i18n("Some changes made on this page may require you to quit the laptop panel "
00082 "and start it again to take effect"), this );
00083 tmp_label->setWordWrap(true);
00084 top_layout->addWidget( tmp_label );
00085
00086 bool can_enable = laptop_portable::has_apm(1);
00087 enableStandby = new QCheckBox( i18n("Enable standby"), this );
00088 top_layout->addWidget( enableStandby );
00089 enableStandby->setToolTip( i18n( "If checked this box enables transitions to the 'standby' state - a temporary powered down state" ) );
00090 enableStandby->setEnabled(can_enable);
00091 connect( enableStandby, SIGNAL(clicked()), this, SLOT(configChanged()) );
00092
00093 enableSuspend = new QCheckBox( i18n("Enable &suspend"), this );
00094 top_layout->addWidget( enableSuspend );
00095 enableSuspend->setToolTip( i18n( "If checked this box enables transitions to the 'suspend' state - a semi-powered down state, sometimes called 'suspend-to-ram'" ) );
00096 enableSuspend->setEnabled(can_enable);
00097 connect( enableSuspend, SIGNAL(clicked()), this, SLOT(configChanged()) );
00098 apm_name = "/usr/bin/apm";
00099 if (::access(apm_name, F_OK) != 0 && ::access("/usr/sbin/apm", F_OK) == 0)
00100 apm_name = "/usr/sbin/apm";
00101
00102 tmp_label = new QLabel(i18n("If the above boxes are disabled then there is no 'helper' "
00103 "application set up to help change APM states, there are two "
00104 "ways you can enable this application, either make the file "
00105 "/proc/apm writeable by anyone every time your system boots "
00106 "or use the button below to make the %1 application "
00107 "set-uid root", apm_name), this );
00108 tmp_label->setWordWrap(true);
00109 top_layout->addWidget( tmp_label );
00110 QHBoxLayout *ll = new QHBoxLayout();
00111 top_layout->addItem( ll );
00112 QPushButton *setupButton = new QPushButton(i18n("Setup Helper Application"), this);
00113 connect( setupButton, SIGNAL(clicked()), this, SLOT(setupHelper()) );
00114 setupButton->setToolTip( i18n( "This button can be used to enable the APM helper application" ) );
00115 ll->addStretch(2);
00116 ll->addWidget(setupButton);
00117 ll->addStretch(8);
00118 if (laptop_portable::has_software_suspend()) {
00119 tmp_label = new QLabel( i18n("Your system seems to have 'Software Suspend' installed, this can "
00120 "be used to hibernate or 'suspend to disk' your system if you want "
00121 "to use this for hibernation check the box below"), this );
00122 tmp_label->setWordWrap(true);
00123 top_layout->addWidget( tmp_label );
00124 enableSoftwareSuspendHibernate = new QCheckBox( i18n("Enable software suspend for hibernate"), this );
00125 top_layout->addWidget( enableSoftwareSuspendHibernate );
00126 enableSoftwareSuspendHibernate->setToolTip( i18n( "If checked this box enables transitions to the 'hibernate' state using the 'Software Suspend' mechanism" ) );
00127 enableSoftwareSuspendHibernate->setEnabled(laptop_portable::has_software_suspend(2));
00128 connect( enableSoftwareSuspendHibernate, SIGNAL(clicked()), this, SLOT(configChanged()) );
00129 tmp_label = new QLabel( i18n("If the above box is disabled then you need to be logged in "
00130 "as root or need a helper application to invoke the Software "
00131 "Suspend utility - KDE provides a utility to do this, if you "
00132 "wish to use it you must make it set-uid root, the button "
00133 "below will do this for you"), this );
00134 tmp_label->setWordWrap(true);
00135 top_layout->addWidget( tmp_label );
00136 ll = new QHBoxLayout(this);
00137 QPushButton *setupSSButton = new QPushButton(i18n("Setup SS Helper Application"), this);
00138 connect( setupSSButton, SIGNAL(clicked()), this, SLOT(setupHelper2()) );
00139 setupSSButton->setToolTip( i18n( "This button can be used to enable the Software Suspend helper application" ) );
00140 ll->addStretch(2);
00141 ll->addWidget(setupSSButton);
00142 ll->addStretch(8);
00143 } else {
00144 enableSoftwareSuspendHibernate = 0;
00145 }
00146
00147
00148 top_layout->addStretch(1);
00149 top_layout->addWidget( new QLabel( i18n("Version: %1", QString(LAPTOP_VERSION)), this), 0, Qt::AlignRight );
00150
00151
00152 load();
00153 }
00154
00155 ApmConfig::~ApmConfig()
00156 {
00157 delete config;
00158 }
00159
00160 void ApmConfig::setupHelper()
00161 {
00162 QString kdesu = KStandardDirs::findExe("kdesu");
00163 if (!kdesu.isEmpty()) {
00164 int rc = KMessageBox::warningContinueCancel(0,
00165 i18n("You will need to supply a root password "
00166 "to allow the privileges of the %1 application to change.", apm_name),
00167 "KLaptopDaemon", KStandardGuiItem::cont(),
00168 "");
00169 if (rc == KMessageBox::Continue) {
00170 KProcess proc;
00171 proc << kdesu;
00172 proc << "-u";
00173 proc << "root";
00174 proc << QString("chown root ")+apm_name+"; chmod +s "+apm_name;
00175 proc.execute();
00176 }
00177 } else {
00178 KMessageBox::sorry(0, i18n("%1 cannot be enabled because kdesu cannot be found. Please make sure that it is installed correctly.", QString(apm_name)),
00179 i18n("KLaptopDaemon"));
00180 }
00181 laptop_portable::apm_set_mask(enablestandby, enablesuspend);
00182 bool can_enable = laptop_portable::has_apm(1);
00183 enableStandby->setEnabled(can_enable);
00184 enableSuspend->setEnabled(can_enable);
00185 wake_laptop_daemon();
00186 }
00187
00188 #include "checkcrc.h"
00189 #include "crcresult.h"
00190 void ApmConfig::setupHelper2()
00191 {
00192 unsigned long len, crc;
00193 QString helper = KStandardDirs::findExe("klaptop_acpi_helper");
00194 checkcrc(helper.latin1(), len, crc);
00195 if (len != file_len || crc != file_crc) {
00196 QString str(i18n("The %1 application does not seem to have "
00197 "the same size or checksum as when it was compiled we do NOT recommend "
00198 "you proceed with making it setuid-root without further investigation", helper));
00199 int rc = KMessageBox::warningContinueCancel(0, str, i18n("KLaptopDaemon"), i18n("Run Nevertheless"));
00200 if (rc != KMessageBox::Continue)
00201 return;
00202 }
00203
00204 QString kdesu = KStandardDirs::findExe("kdesu");
00205 if (!kdesu.isEmpty()) {
00206 int rc = KMessageBox::warningContinueCancel(0,
00207 i18n("You will need to supply a root password "
00208 "to allow the privileges of the klaptop_acpi_helper to change."),
00209 i18n("KLaptopDaemon"), KStandardGuiItem::cont(),
00210 "");
00211 if (rc == KMessageBox::Continue) {
00212 KProcess proc;
00213 proc << kdesu;
00214 proc << "-u";
00215 proc << "root";
00216 proc << "chown root "+helper+"; chmod +s "+helper;
00217 proc.execute();
00218 }
00219 } else {
00220 KMessageBox::sorry(0, i18n("The Software Suspend helper cannot be enabled because kdesu cannot be found. Please make sure that it is installed correctly."),
00221 i18n("KLaptopDaemon"));
00222 }
00223 laptop_portable::software_suspend_set_mask(enablesoftwaresuspend);
00224 bool can_enable = laptop_portable::has_software_suspend(2);
00225 enableSoftwareSuspendHibernate->setEnabled(can_enable);
00226 wake_laptop_daemon();
00227 }
00228
00229
00230 void ApmConfig::save()
00231 {
00232 enablestandby = enableStandby->isChecked();
00233 enablesuspend = enableSuspend->isChecked();
00234 laptop_portable::apm_set_mask(enablestandby, enablesuspend);
00235 enablesoftwaresuspend = (enableSoftwareSuspendHibernate ? enableSoftwareSuspendHibernate->isChecked():0);
00236 laptop_portable::software_suspend_set_mask(enablesoftwaresuspend);
00237
00238 config->setGroup("ApmDefault");
00239
00240 config->writeEntry("EnableStandby", enablestandby);
00241 config->writeEntry("EnableSuspend", enablesuspend);
00242
00243 config->setGroup("SoftwareSuspendDefault");
00244 config->writeEntry("EnableHibernate", enablesoftwaresuspend);
00245 config->sync();
00246 changed(false);
00247 wake_laptop_daemon();
00248 }
00249
00250 void ApmConfig::load()
00251 {
00252 config->setGroup("ApmDefault");
00253 enablestandby = config->readEntry("EnableStandby", true);
00254 enableStandby->setChecked(enablestandby);
00255 enablesuspend = config->readEntry("EnableSuspend", true);
00256 enableSuspend->setChecked(enablesuspend);
00257 config->setGroup("SoftwareSuspendDefault");
00258 enablesoftwaresuspend = config->readEntry("EnableHibernate", false);
00259 if (enableSoftwareSuspendHibernate)
00260 enableSoftwareSuspendHibernate->setChecked(enablesoftwaresuspend);
00261 changed(false);
00262 }
00263
00264 void ApmConfig::defaults()
00265 {
00266 enablestandby = true;
00267 enableStandby->setChecked(enablestandby);
00268 enablesuspend = true;
00269 enableSuspend->setChecked(enablesuspend);
00270 enablesoftwaresuspend = false;
00271 if (enableSoftwareSuspendHibernate)
00272 enableSoftwareSuspendHibernate->setChecked(enablesoftwaresuspend);
00273
00274 changed(true);
00275 }
00276
00277
00278 void ApmConfig::configChanged()
00279 {
00280 emit changed(true);
00281 }
00282
00283
00284 QString ApmConfig::quickHelp() const
00285 {
00286 return i18n("<h1>APM Setup</h1>This module allows you to configure APM for your system");
00287 }
00288
00289
00290 #include "apm.moc"
00291
00292