karm

preferences.cpp

00001 #undef Unsorted // for --enable-final
00002 #include <qcheckbox.h>
00003 #include <qlabel.h>
00004 #include <qstring.h>
00005 #include <qspinbox.h>
00006 #include <qlayout.h>
00007 
00008 #include <kapplication.h>       // kapp
00009 #include <kconfig.h>
00010 #include <kdebug.h>
00011 #include <kemailsettings.h>
00012 #include <kiconloader.h>
00013 #include <klineedit.h>          // lineEdit()
00014 #include <klocale.h>            // i18n
00015 #include <kstandarddirs.h>
00016 #include <kurlrequester.h>
00017 
00018 #include "preferences.h"
00019 
00020 Preferences *Preferences::_instance = 0;
00021 
00022 Preferences::Preferences( const QString& icsFile )
00023   : KDialogBase( IconList, i18n("Preferences"), Ok|Cancel, Ok )
00024 {
00025 
00026   setIconListAllVisible( true );
00027 
00028   makeBehaviorPage();
00029   makeDisplayPage();
00030   makeStoragePage();
00031 
00032   load();
00033 
00034   // command-line option overrides what is stored in 
00035   if ( ! icsFile.isEmpty() ) _iCalFileV = icsFile;
00036 
00037 }
00038 
00039 Preferences *Preferences::instance( const QString &icsfile )
00040 {
00041   if (_instance == 0) {
00042     _instance = new Preferences( icsfile );
00043   }
00044   return _instance;
00045 }
00046 
00047 void Preferences::makeBehaviorPage()
00048 {
00049   QPixmap icon = SmallIcon( "kcmsystem", KIcon::SizeMedium);
00050   QFrame* behaviorPage = addPage( i18n("Behavior"), i18n("Behavior Settings"),
00051       icon );
00052 
00053   QVBoxLayout* topLevel = new QVBoxLayout( behaviorPage, 0, spacingHint() );
00054   QGridLayout* layout = new QGridLayout( topLevel, 2, 2 );
00055   layout->setColStretch( 1, 1 );
00056 
00057   _doIdleDetectionW = new QCheckBox
00058     ( i18n("Detect desktop as idle after"), behaviorPage, "_doIdleDetectionW");
00059   _idleDetectValueW = new QSpinBox
00060     (1,60*24, 1, behaviorPage, "_idleDetectValueW");
00061   _idleDetectValueW->setSuffix(i18n(" min"));
00062   _promptDeleteW = new QCheckBox
00063     ( i18n( "Prompt before deleting tasks" ), behaviorPage, "_promptDeleteW" );
00064 
00065   layout->addWidget(_doIdleDetectionW, 0, 0 );
00066   layout->addWidget(_idleDetectValueW, 0, 1 );
00067   layout->addWidget(_promptDeleteW, 1, 0 );
00068 
00069   topLevel->addStretch();
00070 
00071   connect( _doIdleDetectionW, SIGNAL( clicked() ), this,
00072       SLOT( idleDetectCheckBoxChanged() ));
00073 }
00074 
00075 void Preferences::makeDisplayPage()
00076 {
00077   QPixmap icon = SmallIcon( "viewmag", KIcon::SizeMedium );
00078   QFrame* displayPage = addPage( i18n("Display"), i18n("Display Settings"),
00079       icon );
00080 
00081   QVBoxLayout* topLevel = new QVBoxLayout( displayPage, 0, spacingHint() );
00082   QGridLayout* layout = new QGridLayout( topLevel, 5, 2 );
00083   layout->setColStretch( 1, 1 );
00084 
00085   QLabel* _displayColumnsLabelW = new QLabel( i18n("Columns displayed:"),
00086       displayPage );
00087   _displaySessionW = new QCheckBox ( i18n("Session time"),
00088       displayPage, "_displaySessionW");
00089   _displayTimeW = new QCheckBox ( i18n("Cumulative task time"),
00090       displayPage, "_displayTimeW");
00091   _displayTotalSessionW = new QCheckBox( i18n("Total session time"),
00092       displayPage, "_displayTotalSessionW");
00093   _displayTotalTimeW = new QCheckBox ( i18n("Total task time"),
00094       displayPage, "_displayTotalTimeW");
00095 
00096   layout->addMultiCellWidget( _displayColumnsLabelW, 0, 0, 0, 1 );
00097   layout->addWidget(_displaySessionW, 1, 1 );
00098   layout->addWidget(_displayTimeW, 2, 1 );
00099   layout->addWidget(_displayTotalSessionW, 3, 1 );
00100   layout->addWidget(_displayTotalTimeW, 4, 1 );
00101 
00102   topLevel->addStretch();
00103 }
00104 
00105 void Preferences::makeStoragePage()
00106 {
00107   QPixmap icon = SmallIcon( "kfm", KIcon::SizeMedium );
00108   QFrame* storagePage = addPage( i18n("Storage"), i18n("Storage Settings"),
00109       icon );
00110 
00111   QVBoxLayout* topLevel = new QVBoxLayout( storagePage, 0, spacingHint() );
00112   QGridLayout* layout = new QGridLayout( topLevel, 4, 2 );
00113   layout->setColStretch( 1, 1 );
00114 
00115   // autosave
00116   _doAutoSaveW = new QCheckBox
00117     ( i18n("Save tasks every"), storagePage, "_doAutoSaveW" );
00118   _autoSaveValueW = new QSpinBox(1, 60*24, 1, storagePage, "_autoSaveValueW");
00119   _autoSaveValueW->setSuffix(i18n(" min"));
00120 
00121   // iCalendar
00122   QLabel* _iCalFileLabel = new QLabel( i18n("iCalendar file:"), storagePage);
00123   _iCalFileW = new KURLRequester(storagePage, "_iCalFileW");
00124   _iCalFileW->setFilter(QString::fromLatin1("*.ics"));
00125   _iCalFileW->setMode(KFile::File);
00126 
00127   // Log time?
00128   _loggingW = new QCheckBox 
00129     ( i18n("Log history"), storagePage, "_loggingW" );
00130 
00131   // add widgets to layout
00132   layout->addWidget(_doAutoSaveW, 0, 0);
00133   layout->addWidget(_autoSaveValueW, 0, 1);
00134   layout->addWidget(_iCalFileLabel, 1, 0 );
00135   layout->addWidget(_iCalFileW, 1, 1 );
00136   layout->addWidget(_loggingW, 2, 0 );
00137 
00138   topLevel->addStretch();
00139 
00140   // checkboxes disable file selection controls
00141   connect( _doAutoSaveW, SIGNAL( clicked() ),
00142       this, SLOT( autoSaveCheckBoxChanged() ));
00143 }
00144 
00145 void Preferences::disableIdleDetection()
00146 {
00147   _doIdleDetectionW->setEnabled(false);
00148 }
00149 
00150 
00151 //---------------------------------------------------------------------------
00152 //                            SLOTS
00153 //---------------------------------------------------------------------------
00154 
00155 void Preferences::showDialog()
00156 {
00157 
00158   // set all widgets
00159   _iCalFileW->lineEdit()->setText(_iCalFileV);
00160 
00161   _doIdleDetectionW->setChecked(_doIdleDetectionV);
00162   _idleDetectValueW->setValue(_idleDetectValueV);
00163 
00164   _doAutoSaveW->setChecked(_doAutoSaveV);
00165   _autoSaveValueW->setValue(_autoSaveValueV);
00166   _loggingW->setChecked(_loggingV);
00167 
00168   _promptDeleteW->setChecked(_promptDeleteV);
00169 
00170   _displaySessionW->setChecked(_displayColumnV[0]);
00171   _displayTimeW->setChecked(_displayColumnV[1]);
00172   _displayTotalSessionW->setChecked(_displayColumnV[2]);
00173   _displayTotalTimeW->setChecked(_displayColumnV[3]);
00174 
00175   // adapt visibility of preference items according
00176   // to settings
00177   idleDetectCheckBoxChanged();
00178   autoSaveCheckBoxChanged();
00179 
00180   show();
00181 }
00182 
00183 void Preferences::slotOk()
00184 {
00185 
00186   // storage
00187   _iCalFileV = _iCalFileW->lineEdit()->text();
00188 
00189   _doIdleDetectionV = _doIdleDetectionW->isChecked();
00190   _idleDetectValueV = _idleDetectValueW->value();
00191 
00192   _doAutoSaveV = _doAutoSaveW->isChecked();
00193   _autoSaveValueV = _autoSaveValueW->value();
00194   _loggingV = _loggingW->isChecked();
00195 
00196   // behavior
00197   _promptDeleteV = _promptDeleteW->isChecked();
00198 
00199   // display
00200   _displayColumnV[0] = _displaySessionW->isChecked();
00201   _displayColumnV[1] = _displayTimeW->isChecked();
00202   _displayColumnV[2] = _displayTotalSessionW->isChecked();
00203   _displayColumnV[3] = _displayTotalTimeW->isChecked();
00204 
00205   emitSignals();
00206   save();
00207   KDialogBase::slotOk();
00208 }
00209 
00210 void Preferences::slotCancel()
00211 {
00212   KDialogBase::slotCancel();
00213 }
00214 
00215 void Preferences::idleDetectCheckBoxChanged()
00216 {
00217   _idleDetectValueW->setEnabled(_doIdleDetectionW->isChecked());
00218 }
00219 
00220 void Preferences::autoSaveCheckBoxChanged()
00221 {
00222   _autoSaveValueW->setEnabled(_doAutoSaveW->isChecked());
00223 }
00224 
00225 void Preferences::emitSignals()
00226 {
00227   emit iCalFile( _iCalFileV );
00228   emit detectIdleness( _doIdleDetectionV );
00229   emit idlenessTimeout( _idleDetectValueV );
00230   emit autoSave( _doAutoSaveV );
00231   emit autoSavePeriod( _autoSaveValueV );
00232   emit setupChanged();
00233 }
00234 
00235 QString Preferences::iCalFile()                  const { return _iCalFileV; }
00236 QString Preferences::activeCalendarFile()        const { return _iCalFileV; }
00237 bool    Preferences::detectIdleness()                const { return _doIdleDetectionV; }
00238 int     Preferences::idlenessTimeout()               const { return _idleDetectValueV; }
00239 bool    Preferences::autoSave()                      const { return _doAutoSaveV; }
00240 int     Preferences::autoSavePeriod()                const { return _autoSaveValueV; }
00241 bool    Preferences::logging()                       const { return _loggingV; }
00242 bool    Preferences::promptDelete()                  const { return _promptDeleteV; }
00243 QString Preferences::setPromptDelete(bool prompt)    { _promptDeleteV=prompt; return ""; }
00244 bool    Preferences::displayColumn(int n)            const { return _displayColumnV[n]; }
00245 QString Preferences::userRealName()                  const { return _userRealName; }
00246 
00247 //---------------------------------------------------------------------------
00248 //                                  Load and Save
00249 //---------------------------------------------------------------------------
00250 void Preferences::load()
00251 {
00252   KConfig &config = *kapp->config();
00253 
00254   config.setGroup( QString::fromLatin1("Idle detection") );
00255   _doIdleDetectionV = config.readBoolEntry( QString::fromLatin1("enabled"),
00256      true );
00257   _idleDetectValueV = config.readNumEntry(QString::fromLatin1("period"), 15);
00258 
00259   config.setGroup( QString::fromLatin1("Saving") );
00260   _iCalFileV = config.readPathEntry
00261     ( QString::fromLatin1("ical file"), 
00262       locateLocal( "appdata", QString::fromLatin1( "karm.ics")));
00263   _doAutoSaveV = config.readBoolEntry
00264     ( QString::fromLatin1("auto save"), true);
00265   _autoSaveValueV = config.readNumEntry
00266     ( QString::fromLatin1("auto save period"), 5);
00267   _promptDeleteV = config.readBoolEntry
00268     ( QString::fromLatin1("prompt delete"), true);
00269   _loggingV = config.readBoolEntry
00270     ( QString::fromLatin1("logging"), true);
00271 
00272   _displayColumnV[0] = config.readBoolEntry
00273     ( QString::fromLatin1("display session time"), true);
00274   _displayColumnV[1] = config.readBoolEntry
00275     ( QString::fromLatin1("display time"), true);
00276   _displayColumnV[2] = config.readBoolEntry
00277     ( QString::fromLatin1("display total session time"), true);
00278   _displayColumnV[3] = config.readBoolEntry
00279     ( QString::fromLatin1("display total time"), true);
00280 
00281   KEMailSettings settings;
00282   _userRealName = settings.getSetting( KEMailSettings::RealName );
00283 }
00284 
00285 void Preferences::save()
00286 {
00287   KConfig &config = *KGlobal::config();
00288 
00289   config.setGroup( QString::fromLatin1("Idle detection"));
00290   config.writeEntry( QString::fromLatin1("enabled"), _doIdleDetectionV);
00291   config.writeEntry( QString::fromLatin1("period"), _idleDetectValueV);
00292 
00293   config.setGroup( QString::fromLatin1("Saving"));
00294   config.writePathEntry( QString::fromLatin1("ical file"), _iCalFileV);
00295   config.writeEntry( QString::fromLatin1("auto save"), _doAutoSaveV);
00296   config.writeEntry( QString::fromLatin1("logging"), _loggingV);
00297   config.writeEntry( QString::fromLatin1("auto save period"), _autoSaveValueV);
00298   config.writeEntry( QString::fromLatin1("prompt delete"), _promptDeleteV);
00299 
00300   config.writeEntry( QString::fromLatin1("display session time"),
00301       _displayColumnV[0]);
00302   config.writeEntry( QString::fromLatin1("display time"),
00303       _displayColumnV[1]);
00304   config.writeEntry( QString::fromLatin1("display total session time"),
00305       _displayColumnV[2]);
00306   config.writeEntry( QString::fromLatin1("display total time"),
00307       _displayColumnV[3]);
00308 
00309   config.sync();
00310 }
00311 
00312 // HACK: this entire config dialog should be upgraded to KConfigXT
00313 bool Preferences::readBoolEntry( const QString& key )
00314 {
00315   KConfig &config = *KGlobal::config();
00316   return config.readBoolEntry ( key, true );
00317 }
00318 
00319 void Preferences::writeEntry( const QString &key, bool value)
00320 {
00321   KConfig &config = *KGlobal::config();
00322   config.writeEntry( key, value );
00323   config.sync();
00324 }
00325 
00326 void Preferences::deleteEntry( const QString &key )
00327 {
00328   KConfig &config = *KGlobal::config();
00329   config.deleteEntry( key );
00330   config.sync();
00331 }
00332 
00333 #include "preferences.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys