kmail Library API Documentation

startupwizard.cpp

00001 /*
00002     This file is part of KMail.
00003 
00004     Copyright (c) 2003 Bo Thorsen <bo@klaralvdalens-datakonsult.se>
00005     Copyright (c) 2003 Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020     Boston, MA 02111-1307, USA.
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include "startupwizard.h"
00028 
00029 #include "kmfoldercombobox.h"
00030 #include "configuredialog_p.h"
00031 #include "kmacctmgr.h"
00032 #include "kmcomposewin.h"
00033 #include "kmfoldermgr.h"
00034 #include "kmacctcachedimap.h"
00035 #include "kmfoldercachedimap.h"
00036 #include "kmidentity.h"
00037 #include "identitymanager.h"
00038 #include "kmtransport.h"
00039 #include "kmsender.h"
00040 #include "kmgroupware.h"
00041 #include "kmkernel.h"
00042 #include "kmailicalifaceimpl.h"
00043 
00044 #include <kdebug.h>
00045 #include <klocale.h>
00046 #include <knuminput.h>
00047 #include <kapplication.h>
00048 #include <kconfig.h>
00049 
00050 #include <qvbox.h>
00051 #include <qvbuttongroup.h>
00052 #include <qtextbrowser.h>
00053 #include <qwhatsthis.h>
00054 #include <qvalidator.h>
00055 
00056 WizardIdentityPage::WizardIdentityPage( QWidget * parent, const char * name )
00057   : QWidget( parent, name )
00058 {
00059   // First either get the default identity or make a new one
00060   IdentityManager *im = kmkernel->identityManager();
00061   if( im->identities().count() > 0 )
00062     mIdentity = im->defaultIdentity().uoid();
00063   else {
00064     mIdentity = im->newFromScratch( "Kolab Identity" ).uoid();
00065     im->setAsDefault( mIdentity );
00066   }
00067 
00068   KMIdentity & ident = im->identityForUoid( mIdentity );
00069 
00070   QGridLayout *grid = new QGridLayout( this, 3, 2, KDialog::marginHint(),
00071                        KDialog::spacingHint() );
00072   grid->addColSpacing( 1, fontMetrics().maxWidth()*15 );
00073   grid->setRowStretch( 15, 10 );
00074   grid->setColStretch( 1, 10 );
00075 
00076   QLabel *label = new QLabel( i18n("&Your name:"), this );
00077   QWhatsThis::add( label, i18n("Write your name here.") );
00078   grid->addWidget( label, 0, 0 );
00079   nameEdit = new QLineEdit( ident.fullName(), this );
00080   nameEdit->setFocus();
00081   label->setBuddy( nameEdit );
00082   grid->addWidget( nameEdit, 0, 1 );
00083 
00084   label = new QLabel( i18n("Organi&zation:"), this );
00085   QWhatsThis::add( label, i18n("You can write the company or organization you work for.") );
00086   grid->addWidget( label, 1, 0 );
00087   orgEdit = new QLineEdit( ident.organization(), this );
00088   label->setBuddy( orgEdit );
00089   grid->addWidget( orgEdit, 1, 1 );
00090 
00091   label = new QLabel( i18n("&Email address:"), this );
00092   grid->addWidget( label, 2, 0 );
00093   emailEdit = new QLineEdit( ident.emailAddr(), this );
00094   label->setBuddy( emailEdit );
00095   grid->addWidget( emailEdit, 2, 1 );
00096 }
00097 
00098 void WizardIdentityPage::apply() const {
00099   // Save the identity settings
00100   KMIdentity & ident = identity();
00101   ident.setFullName( nameEdit->text().stripWhiteSpace() );
00102   ident.setOrganization( orgEdit->text().stripWhiteSpace() );
00103   ident.setEmailAddr( emailEdit->text().stripWhiteSpace() );
00104   kmkernel->identityManager()->sort();
00105   kmkernel->identityManager()->commit();
00106 }
00107 
00108 KMIdentity & WizardIdentityPage::identity() const {
00109   return kmkernel->identityManager()->identityForUoid( mIdentity );
00110 }
00111 
00112 WizardKolabPage::WizardKolabPage( QWidget * parent, const char * name )
00113   : QWidget( parent, name ), mFolder(0), mAccount(0), mTransport( 0 )
00114 {
00115   QGridLayout *grid = new QGridLayout( this, 7, 2, KDialog::marginHint(),
00116                        KDialog::spacingHint() );
00117   grid->addColSpacing( 1, fontMetrics().maxWidth()*15 );
00118   grid->setRowStretch( 15, 10 );
00119   grid->setColStretch( 1, 10 );
00120 
00121   QLabel *label = new QLabel( i18n("&Login:"), this );
00122   QWhatsThis::add( label, i18n("Your Internet Service Provider gave you a <em>user name</em> which is used to authenticate you with their servers. It usually is the first part of your email address (the part before <em>@</em>).") );
00123   grid->addWidget( label, 0, 0 );
00124   loginEdit = new QLineEdit( this );
00125   label->setBuddy( loginEdit );
00126   grid->addWidget( loginEdit, 0, 1 );
00127 
00128   label = new QLabel( i18n("P&assword:"), this );
00129   grid->addWidget( label, 1, 0 );
00130   passwordEdit = new QLineEdit( this );
00131   passwordEdit->setEchoMode( QLineEdit::Password );
00132   label->setBuddy( passwordEdit );
00133   grid->addWidget( passwordEdit, 1, 1 );
00134 
00135   label = new QLabel( i18n("Ho&st:"), this );
00136   grid->addWidget( label, 2, 0 );
00137   hostEdit = new QLineEdit( this );
00138   // only letters, digits, '-', '.', ':' (IPv6) and '_' (for Windows
00139   // compatibility) are allowed
00140   hostEdit->setValidator(new QRegExpValidator( QRegExp( "[A-Za-z0-9-_:.]*" ), 0 ) );
00141   label->setBuddy( hostEdit );
00142   grid->addWidget( hostEdit, 2, 1 );
00143 
00144   storePasswordCheck =
00145     new QCheckBox( i18n("Sto&re IMAP password in configuration file"), this );
00146   storePasswordCheck->setChecked( true );
00147   grid->addMultiCellWidget( storePasswordCheck, 3, 3, 0, 1 );
00148 
00149   excludeCheck = new QCheckBox( i18n("E&xclude from \"Check Mail\""), this );
00150   grid->addMultiCellWidget( excludeCheck, 4, 4, 0, 1 );
00151 
00152   intervalCheck = new QCheckBox( i18n("Enable &interval mail checking"), this );
00153   intervalCheck->setChecked( true );
00154   grid->addMultiCellWidget( intervalCheck, 5, 5, 0, 2 );
00155   intervalLabel = new QLabel( i18n("Check inter&val:"), this );
00156   grid->addWidget( intervalLabel, 6, 0 );
00157   intervalSpin = new KIntNumInput( this );
00158   intervalSpin->setRange( 1, 60, 1, FALSE );
00159   intervalSpin->setValue( 1 );
00160   intervalSpin->setSuffix( i18n( " min" ) );
00161   intervalLabel->setBuddy( intervalSpin );
00162   connect( intervalCheck, SIGNAL(toggled(bool)), intervalSpin, SLOT(setEnabled(bool)) );
00163   grid->addWidget( intervalSpin, 6, 1 );
00164 }
00165 
00166 void WizardKolabPage::init( const QString &email )
00167 {
00168   static bool first = true;
00169   if( !first ) return;
00170   first = false;
00171 
00172   // Workaround since Qt can't have default focus on more than one page
00173   loginEdit->setFocus();
00174 
00175   int at = email.find('@');
00176   if( at > 1 && email.length() > (uint)at ) {
00177     // Set reasonable login and host defaults
00178     loginEdit->setText( email );
00179     hostEdit->setText( email.mid( at + 1 ) );
00180   }
00181 }
00182 
00183 void WizardKolabPage::apply()
00184 {
00185   // Handle the account
00186   if( mAccount == 0 ) {
00187     // Create the account
00188     mAccount = static_cast<KMAcctCachedImap*>
00189       ( kmkernel->acctMgr()->create( QString("cachedimap"), "Kolab" ) );
00190     mAccount->init(); // fill the account fields with good default values
00191     kmkernel->acctMgr()->add(mAccount);
00192 
00193     // Set all default settings
00194     // TODO: read these from a system wide settings file
00195     mAccount->setAuth( "PLAIN" );
00196     mAccount->setPrefix( "/" );
00197     mAccount->setUseSSL( false );
00198     mAccount->setUseTLS( true );
00199     mAccount->setSieveConfig( KMail::SieveConfig( true ) );
00200     kmkernel->cleanupImapFolders();
00201     assert( mAccount->folder() );
00202 
00203     // This Must Be False!!
00204     mAccount->setAutoExpunge( false );
00205   }
00206 
00207   mAccount->setLogin( loginEdit->text().stripWhiteSpace() );
00208   mAccount->setPasswd( passwordEdit->text() );
00209   mAccount->setHost( hostEdit->text().stripWhiteSpace() );
00210   mAccount->setStorePasswd( storePasswordCheck->isChecked() );
00211   mAccount->setCheckExclude( excludeCheck->isChecked() );
00212 
00213   kmkernel->acctMgr()->writeConfig( false );
00214 
00215   // Sync new IMAP account ASAP
00216   kdDebug(5006) << mAccount->folder()->name() << endl;
00217 
00218   if( mFolder == 0 ) {
00219     KMFolderDir *child = mAccount->folder()->child();
00220     if( child == 0 )
00221       child = mAccount->folder()->createChildFolder();
00222 
00223     mFolder = kmkernel->dimapFolderMgr()->
00224       createFolder( "INBOX", false, KMFolderTypeCachedImap, child );
00225     static_cast<KMFolderCachedImap*>(mFolder)->setSilentUpload( true );
00226   }
00227   mAccount->processNewMail(false);
00228 
00229   // Handle SMTP transport
00230   if( mTransport == 0 ) {
00231     mTransport = new KMTransportInfo();
00232     mTransport->type = "smtp";
00233     mTransport->name = "Kolab";
00234     mTransport->port = "25";
00235     mTransport->encryption = "TLS";
00236     mTransport->authType = "PLAIN";
00237     mTransport->auth = true;
00238     mTransport->precommand = "";
00239   }
00240 
00241   mTransport->host = hostEdit->text().stripWhiteSpace();
00242   mTransport->user = loginEdit->text().stripWhiteSpace();
00243   mTransport->pass = passwordEdit->text();
00244   mTransport->storePass = storePasswordCheck->isChecked();
00245 
00246   // Save transports:
00247   KConfigGroup general( KMKernel::config(), "General" );
00248   KConfigGroup composer( KMKernel::config(), "Composer" );
00249   // TODO: Set more transports
00250   general.writeEntry( "transports", 1/*mTransportInfoList.count()*/ );
00251 //   QPtrListIterator<KMTransportInfo> it( mTransportInfoList );
00252 //   for ( int i = 1 ; it.current() ; ++it, ++i )
00253 //     (*it)->writeConfig(i);
00254   mTransport->writeConfig(1);
00255 
00256   // Save common options:
00257   general.writeEntry( "sendOnCheck", false );
00258   kmkernel->msgSender()->setSendImmediate( true );
00259 }
00260 
00261 
00262 StartupWizard::StartupWizard( QWidget* parent, const char* name, bool modal )
00263   : QWizard( parent, name, modal ), mGroupwareEnabled(true)
00264 {
00265   addPage( mIntroPage = createIntroPage(), i18n("Groupware Functionality for KMail") );
00266   addPage( mIdentityPage = createIdentityPage(), i18n("Your Identity") );
00267   addPage( mKolabPage = createKolabPage(), i18n("Kolab Groupware Settings") );
00268   addPage( mAccountPage = createAccountPage(), i18n("Accounts") );
00269   addPage( mFolderSelectionPage = createFolderSelectionPage(), i18n("Folder Selection") );
00270   addPage( mLanguagePage = createLanguagePage(), i18n("Folder Language") );
00271   addPage( mFolderCreationPage = createFolderCreationPage(), i18n("Folder Creation") );
00272   addPage( mOutroPage = createOutroPage(), i18n("Done") );
00273 }
00274 
00275 int StartupWizard::language() const
00276 {
00277   return mLanguageCombo->currentItem();
00278 }
00279 
00280 KMFolder* StartupWizard::folder() const
00281 {
00282   if( groupwareEnabled() && useDefaultKolabSettings() )
00283     return mKolabWidget->folder();
00284   else
00285     return mFolderCombo->getFolder();
00286 }
00287 
00288 void StartupWizard::setAppropriatePages()
00289 {
00290   setAppropriate( mKolabPage, groupwareEnabled() && useDefaultKolabSettings() );
00291   setAppropriate( mAccountPage, !groupwareEnabled() || !useDefaultKolabSettings() );
00292   setAppropriate( mLanguagePage, groupwareEnabled() );
00293   setAppropriate( mFolderSelectionPage, groupwareEnabled() && !useDefaultKolabSettings() );
00294   setAppropriate( mFolderCreationPage, groupwareEnabled() );
00295   setAppropriate( mOutroPage, !groupwareEnabled() );
00296   setNextEnabled( mOutroPage, false);
00297   setFinishEnabled( mOutroPage, true );
00298   setFinishEnabled( mFolderCreationPage, true );
00299 }
00300 
00301 void StartupWizard::slotGroupwareEnabled( int i )
00302 {
00303   mGroupwareEnabled = (i == 0);
00304   serverSettings->setEnabled( mGroupwareEnabled );
00305 }
00306 
00307 void StartupWizard::slotServerSettings( int i )
00308 {
00309   mUseDefaultKolabSettings = (i == 0);
00310 }
00311 
00312 QWidget* StartupWizard::createIntroPage()
00313 {
00314   QWidget* page = new QWidget(this, "intro_page");
00315   QBoxLayout* top = new QHBoxLayout( page );
00316   QTextBrowser* text = new QTextBrowser( page );
00317   text->setText( i18n("<b>You don't seem to have any groupware folders "
00318             "configured in KMail.</b><br>"
00319             "This is probably because you are running KMail for the first time, or "
00320             "because you have enabled the groupware functionality for the first time.<br>"
00321             "You now have the choice between disabling the groupware functionality, "
00322             "or leaving it enabled and going through this wizard.<br>"
00323             "If you disable the groupware functionality for now, you can always enable it again from "
00324             "the KMail config dialog."));
00325   top->addWidget( text );
00326 
00327   QVBox* rightSide = new QVBox( page );
00328   top->addWidget( rightSide, 1 );
00329 
00330   QButtonGroup* bg = new QVButtonGroup( i18n("Groupware Functions"), rightSide );
00331 
00332   (new QRadioButton( i18n("Enable groupware functions"), bg ))->setChecked( TRUE );
00333   (void)new QRadioButton( i18n("Disable groupware functions"), bg );
00334   connect( bg, SIGNAL( clicked(int) ), this, SLOT( slotGroupwareEnabled(int) ) );
00335 
00336   bg = serverSettings = new QVButtonGroup( i18n("Groupware Server Setup"), rightSide );
00337   (new QRadioButton( i18n("Use standard groupware server settings"), bg ))->setChecked(TRUE);
00338   (void)new QRadioButton( i18n("Advanced server setup"), bg );
00339   connect( bg, SIGNAL( clicked(int) ), this, SLOT(slotServerSettings(int) ) );
00340 
00341   // Set the groupware setup to the right settings
00342   slotGroupwareEnabled( 0 );
00343   slotServerSettings( 0 );
00344   setHelpEnabled( page, false );
00345   setBackEnabled( page, false );
00346   return page;
00347 }
00348 
00349 QWidget* StartupWizard::createIdentityPage()
00350 {
00351   QWidget* page = new QWidget( this, "identity_page" );
00352   QBoxLayout* top = new QHBoxLayout( page );
00353   QTextBrowser* text = new QTextBrowser( page );
00354   text->setText( i18n("Please set at least your name and email address.") );
00355   top->addWidget( text );
00356 
00357   mIdentityWidget = new WizardIdentityPage( page, "identity_page" );
00358   top->addWidget( mIdentityWidget, 1 );
00359   setHelpEnabled( page, false );
00360   return page;
00361 }
00362 
00363 QWidget* StartupWizard::createKolabPage()
00364 {
00365   QWidget* page = new QWidget( this, "kolabserver_page" );
00366   QBoxLayout* top = new QHBoxLayout( page );
00367   QTextBrowser* text = new QTextBrowser( page );
00368   text->setText( i18n("If the groupware server is a kolab server with default"
00369               " settings, you only need to set these settings.") );
00370   top->addWidget( text );
00371 
00372   mKolabWidget = new WizardKolabPage( page, "kolabserver_page" );
00373   top->addWidget( mKolabWidget, 1 );
00374   setHelpEnabled( page, false );
00375   return page;
00376 }
00377 
00378 QWidget* StartupWizard::createAccountPage()
00379 {
00380   QWidget* page = new QWidget( this, "account_page");
00381   QBoxLayout* top = new QHBoxLayout( page );
00382   QTextBrowser* text = new QTextBrowser( page );
00383   text->setText(i18n("If you want, you can create new accounts before going on with"
00384              " groupware configuration."));
00385   top->addWidget( text );
00386 
00387   mAccountWidget = new NetworkPage( page, "account_page" );
00388   mAccountWidget->setup();
00389   top->addWidget( mAccountWidget, 1 );
00390   setHelpEnabled( page, false );
00391   return page;
00392 }
00393 
00394 QWidget* StartupWizard::createLanguagePage()
00395 {
00396   QWidget* page = new QWidget(this, "language_page");
00397   QBoxLayout* top = new QHBoxLayout( page );
00398   QTextBrowser* text = new QTextBrowser(  page );
00399   text->setText( i18n("If you want to make your groupware folders work with other "
00400             "applications, you might want to select a different language "
00401             "than English.<br>"
00402             "If this is not an issue, leave the language as is."));
00403   top->addWidget( text );
00404 
00405   QVBox* rightSide = new QVBox( page );
00406   top->addWidget( rightSide, 1 );
00407 
00408   mLanguageLabel = new QLabel( rightSide );
00409 
00410   mLanguageCombo = new QComboBox( false, rightSide );
00411 
00412   QStringList lst;
00413   lst << i18n("English") << i18n("German") << i18n("French") << i18n("Dutch");
00414   mLanguageCombo->insertStringList( lst );
00415 
00416   setLanguage( 0, false );
00417   setHelpEnabled( page, false );
00418   return page;
00419 }
00420 
00421 QWidget* StartupWizard::createFolderSelectionPage()
00422 {
00423   QWidget* page = new QWidget(this, "foldersel_page");
00424   QBoxLayout* top = new QHBoxLayout( page );
00425   QTextBrowser* text = new QTextBrowser( page );
00426   text->setText(i18n("The groupware functionality needs some special folders to store "
00427            "the contents of the calendar, contacts, tasks etc.<br>"
00428            "Please select the folder that the groupware folders should "
00429            "be subfolders of."));
00430   top->addWidget( text );
00431   mFolderCombo = new KMFolderComboBox( page );
00432   top->addWidget( mFolderCombo, 1 );
00433   connect( mFolderCombo, SIGNAL( activated(int) ),
00434        this, SLOT( slotUpdateParentFolderName() ) );
00435   setHelpEnabled( page, false );
00436   return page;
00437 }
00438 
00439 void StartupWizard::slotUpdateParentFolderName()
00440 {
00441   KMFolder* folder = mFolderCombo->getFolder();
00442   QString fldrName = i18n("<unnamed>");
00443   if( folder ) fldrName = folder->name();
00444   mFolderCreationText
00445     ->setText( i18n("You have chosen the folder <b>%1</b> as parent of the "
00446             "groupware folders. When pressing the Finish button, "
00447             "those folders will be created if "
00448             "they do not already exist").arg( fldrName ));
00449 }
00450 
00451 void StartupWizard::setLanguage( int language, bool guessed )
00452 {
00453   mLanguageCombo->setCurrentItem( language );
00454   if( guessed ) {
00455     mLanguageLabel->setText( i18n("The folders present indicates that you want to use the selected folder language"));
00456   } else {
00457     mLanguageLabel->setText( i18n("The folder language can't be guessed, please select a language:"));
00458   }
00459 }
00460 
00461 QWidget* StartupWizard::createFolderCreationPage()
00462 {
00463   QHBox* page = new QHBox(this, "foldercre_page");
00464   mFolderCreationText = new QTextBrowser( page );
00465   slotUpdateParentFolderName();
00466   setFinishEnabled( page, true );
00467   setNextEnabled( page, false);
00468   setHelpEnabled( page, false );
00469   return page;
00470 }
00471 
00472 QWidget* StartupWizard::createOutroPage()
00473 {
00474   QHBox* page = new QHBox(this, "outtro_page");
00475   QTextBrowser* text = new QTextBrowser( page );
00476   text->setText( i18n("The groupware functionality has been disabled.") );
00477   setFinishEnabled( page, true );
00478   setNextEnabled( page, false);
00479   setHelpEnabled( page, false );
00480   return page;
00481 }
00482 
00483 void StartupWizard::back()
00484 {
00485   QWizard::back();
00486 }
00487 
00488 void StartupWizard::next()
00489 {
00490   if( currentPage() == mAccountPage ) {
00491     kdDebug(5006) << "AccountPage appropriate: " << appropriate(mAccountPage) << endl;
00492     mAccountWidget->apply();
00493   } else if( currentPage() == mFolderSelectionPage ) {
00494     /* Find the list of folders and guess the language */
00495     guessExistingFolderLanguage();
00496   } else if( currentPage() == mKolabPage ) {
00497     // Apply all settings to the account
00498     mKolabWidget->apply();
00499     /* Find the list of folders and guess the language */
00500     // TODO: guessExistingFolderLanguage();
00501 
00502     // Finally, just set the message at the end of the wizard
00503     mFolderCreationText->setText( i18n("You have chosen to use standard kolab settings.\nPress Finish to proceed.") );
00504 
00505   } else if( currentPage() == mIdentityPage ) {
00506     mIdentityWidget->apply();
00507     mKolabWidget->init( userIdentity().emailAddr() );
00508   }
00509 
00510   // Set which ones apply, given the present state of answers
00511   setAppropriatePages();
00512 
00513   QWizard::next();
00514 }
00515 
00516 static bool checkSubfolders( KMFolderDir* dir, int language )
00517 {
00518   KMailICalIfaceImpl& ical = kmkernel->iCalIface();
00519   return dir->hasNamedFolder( ical.folderName( KFolderTreeItem::Inbox, language ) ) &&
00520     dir->hasNamedFolder( ical.folderName( KFolderTreeItem::Calendar, language ) ) &&
00521     dir->hasNamedFolder( ical.folderName( KFolderTreeItem::Contacts, language ) ) &&
00522     dir->hasNamedFolder( ical.folderName( KFolderTreeItem::Notes, language ) ) &&
00523     dir->hasNamedFolder( ical.folderName( KFolderTreeItem::Tasks, language ) );
00524 }
00525 
00526 void StartupWizard::guessExistingFolderLanguage()
00527 {
00528   KMFolderDir* dir = folder()->child();
00529 
00530   if(  checkSubfolders( dir, 0 ) ) {
00531     // Check English
00532     setLanguage( 0, true );
00533   } else if( checkSubfolders( dir, 1 ) ) {
00534     // Check German
00535     setLanguage( 1, true );
00536   } else {
00537     setLanguage( 0, false );
00538   }
00539 }
00540 
00541 KMIdentity &StartupWizard::userIdentity()
00542 {
00543   return mIdentityWidget->identity();
00544 }
00545 
00546 const KMIdentity &StartupWizard::userIdentity() const
00547 {
00548   return mIdentityWidget->identity();
00549 }
00550 
00551 QString StartupWizard::name() const
00552 {
00553   return userIdentity().fullName();
00554 }
00555 
00556 QString StartupWizard::login() const
00557 {
00558   return mKolabWidget->loginEdit->text().stripWhiteSpace();
00559 }
00560 
00561 QString StartupWizard::host() const
00562 {
00563   return mKolabWidget->hostEdit->text().stripWhiteSpace();
00564 }
00565 
00566 QString StartupWizard::email() const
00567 {
00568   return userIdentity().emailAddr();
00569 }
00570 
00571 QString StartupWizard::passwd() const
00572 {
00573   return KMAccount::encryptStr( mKolabWidget->passwordEdit->text() );
00574 }
00575 
00576 bool StartupWizard::storePasswd() const
00577 {
00578   return mKolabWidget->storePasswordCheck->isChecked();
00579 }
00580 
00581 
00582 void StartupWizard::run()
00583 {
00584   KConfigGroup options( KMKernel::config(), "Groupware" );
00585 
00586   // Check if this wizard was previously run
00587   if( options.readEntry( "Enabled", "notset" ) != "notset" )
00588     return;
00589 
00590   StartupWizard wiz(0, "groupware wizard", TRUE );
00591   int rc = wiz.exec();
00592 
00593   options.writeEntry( "Enabled", rc == QDialog::Accepted && wiz.groupwareEnabled() );
00594   if( rc == QDialog::Accepted ) {
00595     options.writeEntry( "FolderLanguage", wiz.language() );
00596     options.writeEntry( "GroupwareFolder", wiz.folder()->idString() );
00597 
00598     kmkernel->groupware().readConfig();
00599 
00600     if( wiz.groupwareEnabled() && wiz.useDefaultKolabSettings() ) {
00601       // Write the apps configs
00602       writeKOrganizerConfig( wiz );
00603       writeKAbcConfig();
00604       writeKAddressbookConfig( wiz );
00605     }
00606   }
00607 }
00608 
00609 
00610 // Write the KOrganizer settings
00611 void StartupWizard::writeKOrganizerConfig( const StartupWizard& wiz ) {
00612   KConfig config( "korganizerrc" );
00613 
00614   KConfigGroup optionsKOrgGeneral( &config, "Personal Settings" );
00615   optionsKOrgGeneral.writeEntry( "user_name", wiz.name() );
00616   optionsKOrgGeneral.writeEntry( "user_email", wiz.email() );
00617 
00618   KConfigGroup optionsKOrgGroupware( &config, "Groupware" );
00619   optionsKOrgGroupware.writeEntry( "Publish FreeBusy lists", true );
00620   optionsKOrgGroupware.writeEntry( "Publish FreeBusy days", 60 );
00621   optionsKOrgGroupware.writeEntry( "Publish to Kolab server", true );
00622   optionsKOrgGroupware.writeEntry( "Publish to Kolab server name", wiz.host() );
00623   optionsKOrgGroupware.writeEntry( "Publish user name", wiz.login() );
00624   optionsKOrgGroupware.writeEntry( "Remember publish password", wiz.storePasswd() );
00625   if( wiz.storePasswd() ) {
00626     optionsKOrgGroupware.writeEntry( "Publish Server Password", wiz.passwd() );
00627     optionsKOrgGroupware.writeEntry( "Retrieve Server Password", wiz.passwd() );
00628   }
00629   optionsKOrgGroupware.writeEntry( "Retrieve FreeBusy lists", true );
00630   optionsKOrgGroupware.writeEntry( "Retrieve from Kolab server", true );
00631   optionsKOrgGroupware.writeEntry( "Retrieve from Kolab server name", wiz.host() );
00632   optionsKOrgGroupware.writeEntry( "Retrieve user name", wiz.login() );
00633   optionsKOrgGroupware.writeEntry( "Remember retrieve password", wiz.storePasswd() );
00634 
00635   config.sync();
00636 }
00637 
00638 
00639 // Write the KABC settings
00640 void StartupWizard::writeKAbcConfig() {
00641   KConfig config( "kabcrc" );
00642   KConfigGroup optionsKAbcGeneral( &config, "General" );
00643   QString standardKey = optionsKAbcGeneral.readEntry( "Standard" );
00644   QString newStandardKey;
00645 
00646   QStringList activeKeys = optionsKAbcGeneral.readListEntry( "ResourceKeys" );
00647   QStringList passiveKeys = optionsKAbcGeneral.readListEntry( "PassiveResourceKeys" );
00648   QStringList keys = activeKeys + passiveKeys;
00649   for ( QStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) {
00650     KConfigGroup entry( &config, "Resource_" + (*it) );
00651     if( entry.readEntry( "ResourceType" ) == "imap" && newStandardKey.isNull() ) {
00652       // This is the IMAP resource that must now be the standard
00653       newStandardKey = *it;
00654 
00655       // We want to be able to write to this
00656       entry.writeEntry( "ResourceIsReadOnly", false );
00657     } else
00658       // Not an IMAP resource, so don't write to it anymore
00659       entry.writeEntry( "ResourceIsReadOnly", true );
00660   }
00661 
00662   if( newStandardKey.isNull() ) {
00663     // No IMAP resource was found, make one
00664     newStandardKey = KApplication::randomString( 10 );
00665     KConfigGroup entry( &config, "Resource_" + newStandardKey );
00666     entry.writeEntry( "ResourceName", "imap-resource" );
00667     entry.writeEntry( "ResourceType", "imap" );
00668     entry.writeEntry( "ResourceIsReadOnly", false );
00669     entry.writeEntry( "ResourceIsFast", true );
00670     activeKeys += newStandardKey;
00671   } else if( passiveKeys.remove( newStandardKey ) > 0 )
00672     // This used to be passive. Make it active
00673     activeKeys += newStandardKey;
00674 
00675   // Set the keys
00676   optionsKAbcGeneral.writeEntry( "ResourceKeys", activeKeys );
00677   optionsKAbcGeneral.writeEntry( "PassiveResourceKeys", passiveKeys );
00678   optionsKAbcGeneral.writeEntry( "Standard", newStandardKey );
00679 
00680   config.sync();
00681 }
00682 
00683 
00684 // Write the KAddressbook settings
00685 void StartupWizard::writeKAddressbookConfig( const StartupWizard& wiz ) {
00686   KConfig config( "kaddressbookrc" );
00687   KConfigGroup options( &config, "LDAP" );
00688 
00689   QString hostBase = QString( "dc=" ) + wiz.host();
00690   hostBase.replace( '.', ",dc=" );
00691 
00692   // Read all servers and try finding one that matches us
00693   uint count = options.readUnsignedNumEntry( "NumSelectedHosts");
00694   for ( uint i = 0; i < count; ++i ) {
00695     QString host = options.readEntry( QString( "SelectedHost%1").arg( i ) );
00696     int port = options.readUnsignedNumEntry( QString( "SelectedPort%1" ).arg( i ) );
00697     QString base = options.readEntry( QString( "SelectedBase%1" ).arg( i ) );
00698 
00699     if( host == wiz.host() && port == 389 && base == hostBase )
00700       // We found a match, and it's selected
00701       return;
00702   }
00703 
00704   // No match among the selected ones, try the unselected
00705   count = options.readUnsignedNumEntry( "NumHosts" );
00706   for ( uint i = 0; i < count; ++i ) {
00707     QString host = options.readEntry( QString( "SelectedHost%1").arg( i ) );
00708     int port = options.readUnsignedNumEntry( QString( "SelectedPort%1" ).arg( i ) );
00709     QString base = options.readEntry( QString( "SelectedBase%1" ).arg( i ) );
00710 
00711     if( host == wiz.host() && port == 389 && base == hostBase ) {
00712       // We found a match. Remove it from this list
00713       for( ++i; i < count; ++i ) {
00714     host = options.readEntry( QString( "Host%1" ).arg( i ) );
00715     port = options.readUnsignedNumEntry( QString( "Port%1" ).arg( i ) );
00716     base = options.readEntry( QString( "Base%1" ).arg( i ) );
00717     options.writeEntry( QString( "Host%1" ).arg( i-1 ), host );
00718     options.writeEntry( QString( "Port%1" ).arg( i-1 ), port );
00719     options.writeEntry( QString( "Base%1" ).arg( i-1 ), base );
00720       }
00721 
00722       // Now all the previous ones were overwritten, so remove the last one
00723       --count;
00724       options.deleteEntry( QString( "Host%1" ).arg( count ) );
00725       options.deleteEntry( QString( "Port%1" ).arg( count ) );
00726       options.deleteEntry( QString( "Base%1" ).arg( count ) );
00727       options.writeEntry( "NumHosts", count );
00728       break;
00729     }
00730   }
00731 
00732   // Now write the selected ldap server
00733   count = options.readUnsignedNumEntry( "NumSelectedHosts");
00734   options.writeEntry( QString( "SelectedHost%1" ).arg( count ), wiz.host() );
00735   options.writeEntry( QString( "SelectedPort%1" ).arg( count ), 389 );
00736   options.writeEntry( QString( "SelectedBase%1" ).arg( count ), hostBase );
00737   options.writeEntry( "NumSelectedHosts", count+1 );
00738 }
00739 
00740 
00741 #include "startupwizard.moc"
KDE Logo
This file is part of the documentation for kmail Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:35 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003