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   kdDebug(5970) << "Entering Preferences::slotOk" << endl;
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   kdDebug(5970) << "Entering Preferences::slotCancel" << endl;
00213   KDialogBase::slotCancel();
00214 }
00215 
00216 void Preferences::idleDetectCheckBoxChanged()
00217 {
00218   _idleDetectValueW->setEnabled(_doIdleDetectionW->isChecked());
00219 }
00220 
00221 void Preferences::autoSaveCheckBoxChanged()
00222 {
00223   _autoSaveValueW->setEnabled(_doAutoSaveW->isChecked());
00224 }
00225 
00226 void Preferences::emitSignals()
00227 {
00228   kdDebug(5970) << "Entering Preferences::emitSignals" << endl;
00229   emit iCalFile( _iCalFileV );
00230   emit detectIdleness( _doIdleDetectionV );
00231   emit idlenessTimeout( _idleDetectValueV );
00232   emit autoSave( _doAutoSaveV );
00233   emit autoSavePeriod( _autoSaveValueV );
00234   emit setupChanged();
00235 }
00236 
00237 QString Preferences::iCalFile()                  const { return _iCalFileV; }
00238 QString Preferences::activeCalendarFile()        const { return _iCalFileV; }
00239 bool    Preferences::detectIdleness()                const { return _doIdleDetectionV; }
00240 int     Preferences::idlenessTimeout()               const { return _idleDetectValueV; }
00241 bool    Preferences::autoSave()                      const { return _doAutoSaveV; }
00242 int     Preferences::autoSavePeriod()                const { return _autoSaveValueV; }
00243 bool    Preferences::logging()                       const { return _loggingV; }
00244 bool    Preferences::promptDelete()                  const { return _promptDeleteV; }
00245 QString Preferences::setPromptDelete(bool prompt)    { _promptDeleteV=prompt; return ""; }
00246 bool    Preferences::displayColumn(int n)            const { return _displayColumnV[n]; }
00247 QString Preferences::userRealName()                  const { return _userRealName; }
00248 
00249 //---------------------------------------------------------------------------
00250 //                                  Load and Save
00251 //---------------------------------------------------------------------------
00252 void Preferences::load()
00253 {
00254   KConfig &config = *kapp->config();
00255 
00256   config.setGroup( QString::fromLatin1("Idle detection") );
00257   _doIdleDetectionV = config.readBoolEntry( QString::fromLatin1("enabled"),
00258      true );
00259   _idleDetectValueV = config.readNumEntry(QString::fromLatin1("period"), 15);
00260 
00261   config.setGroup( QString::fromLatin1("Saving") );
00262   _iCalFileV = config.readPathEntry
00263     ( QString::fromLatin1("ical file"), 
00264       locateLocal( "appdata", QString::fromLatin1( "karm.ics")));
00265   _doAutoSaveV = config.readBoolEntry
00266     ( QString::fromLatin1("auto save"), true);
00267   _autoSaveValueV = config.readNumEntry
00268     ( QString::fromLatin1("auto save period"), 5);
00269   _promptDeleteV = config.readBoolEntry
00270     ( QString::fromLatin1("prompt delete"), true);
00271   _loggingV = config.readBoolEntry
00272     ( QString::fromLatin1("logging"), true);
00273 
00274   _displayColumnV[0] = config.readBoolEntry
00275     ( QString::fromLatin1("display session time"), true);
00276   _displayColumnV[1] = config.readBoolEntry
00277     ( QString::fromLatin1("display time"), true);
00278   _displayColumnV[2] = config.readBoolEntry
00279     ( QString::fromLatin1("display total session time"), true);
00280   _displayColumnV[3] = config.readBoolEntry
00281     ( QString::fromLatin1("display total time"), true);
00282 
00283   KEMailSettings settings;
00284   _userRealName = settings.getSetting( KEMailSettings::RealName );
00285 }
00286 
00287 void Preferences::save()
00288 {
00289   KConfig &config = *KGlobal::config();
00290 
00291   config.setGroup( QString::fromLatin1("Idle detection"));
00292   config.writeEntry( QString::fromLatin1("enabled"), _doIdleDetectionV);
00293   config.writeEntry( QString::fromLatin1("period"), _idleDetectValueV);
00294 
00295   config.setGroup( QString::fromLatin1("Saving"));
00296   config.writePathEntry( QString::fromLatin1("ical file"), _iCalFileV);
00297   config.writeEntry( QString::fromLatin1("auto save"), _doAutoSaveV);
00298   config.writeEntry( QString::fromLatin1("logging"), _loggingV);
00299   config.writeEntry( QString::fromLatin1("auto save period"), _autoSaveValueV);
00300   config.writeEntry( QString::fromLatin1("prompt delete"), _promptDeleteV);
00301 
00302   config.writeEntry( QString::fromLatin1("display session time"),
00303       _displayColumnV[0]);
00304   config.writeEntry( QString::fromLatin1("display time"),
00305       _displayColumnV[1]);
00306   config.writeEntry( QString::fromLatin1("display total session time"),
00307       _displayColumnV[2]);
00308   config.writeEntry( QString::fromLatin1("display total time"),
00309       _displayColumnV[3]);
00310 
00311   config.sync();
00312 }
00313 
00314 // HACK: this entire config dialog should be upgraded to KConfigXT
00315 bool Preferences::readBoolEntry( const QString& key )
00316 {
00317   KConfig &config = *KGlobal::config();
00318   return config.readBoolEntry ( key, true );
00319 }
00320 
00321 void Preferences::writeEntry( const QString &key, bool value)
00322 {
00323   KConfig &config = *KGlobal::config();
00324   config.writeEntry( key, value );
00325   config.sync();
00326 }
00327 
00328 void Preferences::deleteEntry( const QString &key )
00329 {
00330   KConfig &config = *KGlobal::config();
00331   config.deleteEntry( key );
00332   config.sync();
00333 }
00334 
00335 #include "preferences.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys