libkdepim

kconfigwizard.cpp

00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kconfigwizard.h"
00023 
00024 #include <klocale.h>
00025 #include <kdebug.h>
00026 #include <kconfigskeleton.h>
00027 #include <kmessagebox.h>
00028 #include <kapplication.h>
00029 
00030 #include <qlistview.h>
00031 #include <qlayout.h>
00032 #include <qtimer.h>
00033 
00034 KConfigWizard::KConfigWizard( QWidget *parent,
00035                               char *name, bool modal )
00036   : KDialogBase( TreeList, i18n("Configuration Wizard"), Ok|Cancel, Ok, parent,
00037                  name, modal ),
00038     mPropagator( 0 ), mChangesPage( 0 )
00039 {
00040   init();
00041 }
00042 
00043 KConfigWizard::KConfigWizard( KConfigPropagator *propagator, QWidget *parent,
00044                               char *name, bool modal )
00045   : KDialogBase( TreeList, i18n("Configuration Wizard"), Ok|Cancel, Ok, parent,
00046                  name, modal ),
00047     mPropagator( propagator ), mChangesPage( 0 )
00048 {
00049   init();
00050 }
00051 
00052 KConfigWizard::~KConfigWizard()
00053 {
00054   delete mPropagator;
00055 }
00056 
00057 void KConfigWizard::init()
00058 {
00059   connect( this, SIGNAL( aboutToShowPage( QWidget * ) ),
00060            SLOT( slotAboutToShowPage( QWidget * ) ) );
00061 
00062   QTimer::singleShot( 0, this, SLOT( readConfig() ) );
00063 }
00064 
00065 void KConfigWizard::setPropagator( KConfigPropagator *p )
00066 {
00067   mPropagator = p;
00068 }
00069 
00070 void KConfigWizard::slotAboutToShowPage( QWidget *page )
00071 {
00072   if ( page == mChangesPage ) {
00073     updateChanges();
00074   }
00075 }
00076 
00077 QFrame *KConfigWizard::createWizardPage( const QString &title )
00078 {
00079   return addPage( title );
00080 }
00081 
00082 void KConfigWizard::setupRulesPage()
00083 {
00084   QFrame *topFrame = addPage( i18n("Rules") );
00085   QVBoxLayout *topLayout = new QVBoxLayout( topFrame );
00086 
00087   mRuleView = new QListView( topFrame );
00088   topLayout->addWidget( mRuleView );
00089 
00090   mRuleView->addColumn( i18n("Source") );
00091   mRuleView->addColumn( i18n("Target") );
00092   mRuleView->addColumn( i18n("Condition") );
00093 
00094   updateRules();
00095 }
00096 
00097 void KConfigWizard::updateRules()
00098 {
00099   if ( !mPropagator ) {
00100     kdError() << "KConfigWizard: No KConfigPropagator set." << endl;
00101     return;
00102   }
00103 
00104   mRuleView->clear();
00105 
00106   KConfigPropagator::Rule::List rules = mPropagator->rules();
00107   KConfigPropagator::Rule::List::ConstIterator it;
00108   for( it = rules.begin(); it != rules.end(); ++it ) {
00109     KConfigPropagator::Rule r = *it;
00110     QString source = r.sourceFile + "/" + r.sourceGroup + "/" +
00111                      r.sourceEntry;
00112     QString target = r.targetFile + "/" + r.targetGroup + "/" +
00113                      r.targetEntry;
00114     QString condition;
00115     KConfigPropagator::Condition c = r.condition;
00116     if ( c.isValid ) {
00117       condition = c.file + "/" + c.group + "/" + c.key + " = " + c.value;
00118     }
00119     new QListViewItem( mRuleView, source, target, condition );
00120   }
00121 }
00122 
00123 void KConfigWizard::setupChangesPage()
00124 {
00125   QFrame *topFrame = addPage( i18n("Changes") );
00126   QVBoxLayout *topLayout = new QVBoxLayout( topFrame );
00127 
00128   mChangeView = new QListView( topFrame );
00129   topLayout->addWidget( mChangeView );
00130 
00131   mChangeView->addColumn( i18n("Action") );
00132   mChangeView->addColumn( i18n("Option") );
00133   mChangeView->addColumn( i18n("Value") );
00134   mChangeView->setSorting( -1 );
00135 
00136   mChangesPage = topFrame;
00137 }
00138 
00139 void KConfigWizard::updateChanges()
00140 {
00141   kdDebug() << "KConfigWizard::updateChanges()" << endl;
00142 
00143   if ( !mPropagator ) {
00144     kdError() << "KConfigWizard: No KConfigPropagator set." << endl;
00145     return;
00146   }
00147 
00148   usrWriteConfig();
00149 
00150   mPropagator->updateChanges();
00151 
00152   mChangeView->clear();
00153 
00154   KConfigPropagator::Change::List changes = mPropagator->changes();
00155   KConfigPropagator::Change *c;
00156   for( c = changes.first(); c; c = changes.next() ) {
00157     new QListViewItem( mChangeView, mChangeView->lastItem(), c->title(), c->arg1(), c->arg2() );
00158   }
00159 }
00160 
00161 void KConfigWizard::readConfig()
00162 {
00163   kdDebug() << "KConfigWizard::readConfig()" << endl;
00164 
00165   int result = KMessageBox::warningContinueCancel( this,
00166       i18n("Please make sure that the programs which are "
00167            "configured by the wizard do not run in parallel to the wizard; "
00168            "otherwise, changes done by the wizard could be lost."),
00169       i18n("Warning"), i18n("Run Wizard Now"), "warning_running_instances" );
00170   if ( result != KMessageBox::Continue ) kapp->quit();
00171 
00172   usrReadConfig();
00173 }
00174 
00175 void KConfigWizard::slotOk()
00176 {
00177   QString error = validate();
00178   if ( error.isNull() ) {
00179     usrWriteConfig();
00180 
00181     if ( !mPropagator ) {
00182       kdError() << "KConfigWizard: No KConfigPropagator set." << endl;
00183       return;
00184     } else {
00185       if ( mPropagator->skeleton() ) {
00186         mPropagator->skeleton()->writeConfig();
00187       }
00188       mPropagator->commit();
00189     }
00190 
00191     accept();
00192   } else {
00193     KMessageBox::sorry( this, error );
00194   }
00195 }
00196 
00197 #include "kconfigwizard.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys