kontact

mainwindow.cpp

00001 /*
00002     This file is part of KDE Kontact.
00003 
00004     Copyright (c) 2001 Matthias Hoelzer-Kluepfel <mhk@kde.org>
00005     Copyright (c) 2002-2005 Daniel Molkentin <molkentin@kde.org>
00006     Copyright (c) 2003-2005 Cornelius Schumacher <schumacher@kde.org>
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include <qcombobox.h>
00024 #include <qdockarea.h>
00025 #include <qguardedptr.h>
00026 #include <qhbox.h>
00027 #include <qimage.h>
00028 #include <qobjectlist.h>
00029 #include <qprogressbar.h>
00030 #include <qpushbutton.h>
00031 #include <qsplitter.h>
00032 #include <qtimer.h>
00033 #include <qwhatsthis.h>
00034 
00035 #include <dcopclient.h>
00036 #include <kapplication.h>
00037 #include <kconfig.h>
00038 #include <kdebug.h>
00039 #include <kedittoolbar.h>
00040 #include <kguiitem.h>
00041 #include <khelpmenu.h>
00042 #include <kiconloader.h>
00043 #include <kkeydialog.h>
00044 #include <klibloader.h>
00045 #include <klistbox.h>
00046 #include <klocale.h>
00047 #include <kmessagebox.h>
00048 #include <kparts/componentfactory.h>
00049 #include <kplugininfo.h>
00050 #include <kpopupmenu.h>
00051 #include <ksettings/dialog.h>
00052 #include <ksettings/dispatcher.h>
00053 #include <kshortcut.h>
00054 #include <kstandarddirs.h>
00055 #include <kstatusbar.h>
00056 #include <kstdaction.h>
00057 #include <ktip.h>
00058 #include <ktrader.h>
00059 #include <ksettings/componentsdialog.h>
00060 #include <kstringhandler.h>
00061 #include <krsqueezedtextlabel.h>
00062 #include <khtml_part.h>
00063 #include <khtmlview.h>
00064 #include <libkdepim/kfileio.h>
00065 #include <kcursor.h>
00066 #include <krun.h>
00067 #include <kaboutdata.h>
00068 #include <kmenubar.h>
00069 #include <kstdaccel.h>
00070 #include <kcmultidialog.h>
00071 #include <kipc.h>
00072 
00073 #include "aboutdialog.h"
00074 #include "iconsidepane.h"
00075 #include "mainwindow.h"
00076 #include "plugin.h"
00077 #include "prefs.h"
00078 #include "profiledialog.h"
00079 #include "profilemanager.h"
00080 #include "progressdialog.h"
00081 #include "statusbarprogresswidget.h"
00082 #include "broadcaststatus.h"
00083 
00084 using namespace Kontact;
00085 
00086 class SettingsDialogWrapper : public KSettings::Dialog
00087 {
00088   public:
00089     SettingsDialogWrapper( ContentInListView content, QWidget * parent = 0 )
00090       : KSettings::Dialog( content, parent, 0 )
00091     {
00092     }
00093 
00094 
00095     void fixButtonLabel( QWidget *widget )
00096     {
00097       QObject *object = widget->child( "KJanusWidget::buttonBelowList" );
00098       QPushButton *button = static_cast<QPushButton*>( object );
00099       if ( button )
00100         button->setText( i18n( "Select Components ..." ) );
00101     }
00102 };
00103 
00104 MainWindow::MainWindow()
00105   : Kontact::Core(), mTopWidget( 0 ), mSplitter( 0 ),
00106     mCurrentPlugin( 0 ), mAboutDialog( 0 ), mReallyClose( false ), mSyncActionsEnabled( true )
00107 {
00108   // Set this to be the group leader for all subdialogs - this means
00109   // modal subdialogs will only affect this dialog, not the other windows
00110   setWFlags( getWFlags() | WGroupLeader );
00111 
00112   initGUI();
00113   initObject();
00114 }
00115 
00116 void MainWindow::initGUI()
00117 {
00118   initWidgets();
00119   setupActions();
00120   setHelpMenuEnabled( false );
00121   KHelpMenu *helpMenu = new KHelpMenu( this, 0, true, actionCollection() );
00122   connect( helpMenu, SIGNAL( showAboutApplication() ),
00123            SLOT( showAboutDialog() ) );
00124 
00125   KStdAction::keyBindings( this, SLOT( configureShortcuts() ), actionCollection() );
00126   KStdAction::configureToolbars( this, SLOT( configureToolbars() ), actionCollection() );
00127   setXMLFile( "kontactui.rc" );
00128 
00129   setStandardToolBarMenuEnabled( true );
00130 
00131   createGUI( 0 );
00132 
00133   resize( 700, 520 ); // initial size to prevent a scrollbar in sidepane
00134   setAutoSaveSettings();
00135 
00136   connect( Kontact::ProfileManager::self(), SIGNAL( profileLoaded( const QString& ) ),
00137            this, SLOT( slotLoadProfile( const QString& ) ) );
00138   connect( Kontact::ProfileManager::self(), SIGNAL( saveToProfileRequested( const QString& ) ),
00139            this, SLOT( slotSaveToProfile( const QString& ) ) );
00140 }
00141 
00142 
00143 void MainWindow::initObject()
00144 {
00145   KTrader::OfferList offers = KTrader::self()->query(
00146       QString::fromLatin1( "Kontact/Plugin" ),
00147       QString( "[X-KDE-KontactPluginVersion] == %1" ).arg( KONTACT_PLUGIN_VERSION ) );
00148   mPluginInfos = KPluginInfo::fromServices( offers, Prefs::self()->config(), "Plugins" );
00149 
00150   KPluginInfo::List::Iterator it;
00151   for ( it = mPluginInfos.begin(); it != mPluginInfos.end(); ++it ) {
00152     ( *it )->load();
00153   }
00154 
00155   // prepare the part manager
00156   mPartManager = new KParts::PartManager( this );
00157   connect( mPartManager, SIGNAL( activePartChanged( KParts::Part* ) ),
00158            this, SLOT( slotActivePartChanged( KParts::Part* ) ) );
00159 
00160   loadPlugins();
00161 
00162   if ( mSidePane ) {
00163     mSidePane->updatePlugins();
00164     plugActionList( "navigator_actionlist", mSidePane->actions() );
00165   }
00166 
00167   KSettings::Dispatcher::self()->registerInstance( instance(), this,
00168                                                    SLOT( updateConfig() ) );
00169 
00170   loadSettings();
00171 
00172   statusBar()->show();
00173 
00174   showTip( false );
00175 
00176   // done initializing
00177   slotShowStatusMsg( QString::null );
00178 
00179   connect( KPIM::BroadcastStatus::instance(), SIGNAL( statusMsg( const QString& ) ),
00180            this, SLOT( slotShowStatusMsg( const QString&  ) ) );
00181 
00182   // launch commandline specified module if any
00183   activatePluginModule();
00184 
00185   if ( Prefs::lastVersionSeen() == kapp->aboutData()->version() ) {
00186     selectPlugin( mCurrentPlugin );
00187   }
00188 
00189   paintAboutScreen( introductionString() );
00190   Prefs::setLastVersionSeen( kapp->aboutData()->version() );
00191 }
00192 
00193 MainWindow::~MainWindow()
00194 {
00195   saveSettings();
00196 
00197   QPtrList<KParts::Part> parts = *mPartManager->parts();
00198 
00199   for ( KParts::Part *p = parts.last(); p; p = parts.prev() ) {
00200     delete p;
00201     p = 0;
00202   }
00203 
00204   Prefs::self()->writeConfig();
00205 }
00206 
00207 void MainWindow::setActivePluginModule( const QString &module )
00208 {
00209   mActiveModule = module;
00210   activatePluginModule();
00211 }
00212 
00213 void MainWindow::activatePluginModule()
00214 {
00215   if ( !mActiveModule.isEmpty() ) {
00216     PluginList::ConstIterator end = mPlugins.end();
00217     for ( PluginList::ConstIterator it = mPlugins.begin(); it != end; ++it )
00218       if ( ( *it )->identifier().contains( mActiveModule ) ) {
00219         selectPlugin( *it );
00220         return;
00221       }
00222   }
00223 }
00224 
00225 void MainWindow::initWidgets()
00226 {
00227   // includes sidebar and part stack
00228   mTopWidget = new QHBox( this );
00229   mTopWidget->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00230   setCentralWidget( mTopWidget );
00231 
00232   QHBox *mBox = 0;
00233   mSplitter = new QSplitter( mTopWidget );
00234   mBox = new QHBox( mTopWidget );
00235   mSidePane = new IconSidePane( this, mSplitter );
00236   mSidePane->setSizePolicy( QSizePolicy( QSizePolicy::Maximum,
00237                                          QSizePolicy::Preferred ) );
00238   // donÄt occupy screen estate on load
00239   QValueList<int> sizes;
00240   sizes << 0;
00241   mSplitter->setSizes(sizes);
00242 
00243   mSidePane->setActionCollection( actionCollection() );
00244 
00245   connect( mSidePane, SIGNAL( pluginSelected( Kontact::Plugin * ) ),
00246            SLOT( selectPlugin( Kontact::Plugin * ) ) );
00247 
00248   QVBox *vBox;
00249   if ( mSplitter ) {
00250     vBox = new QVBox( mSplitter );
00251   } else {
00252     vBox = new QVBox( mBox );
00253   }
00254 
00255   vBox->setSpacing( 0 );
00256 
00257   mPartsStack = new QWidgetStack( vBox );
00258   initAboutScreen();
00259 
00260   QString loading = i18n( "<h2 style='text-align:center; margin-top: 0px; margin-bottom: 0px'>%1</h2>" )
00261                     .arg( i18n("Loading Kontact...") );
00262 
00263   paintAboutScreen( loading );
00264 
00265   /* Create a progress dialog and hide it. */
00266   KPIM::ProgressDialog *progressDialog = new KPIM::ProgressDialog( statusBar(), this );
00267   progressDialog->hide();
00268 
00269   mLittleProgress = new KPIM::StatusbarProgressWidget( progressDialog, statusBar() );
00270 
00271   mStatusMsgLabel = new KRSqueezedTextLabel( i18n( " Initializing..." ), statusBar() );
00272   mStatusMsgLabel->setAlignment( AlignLeft | AlignVCenter );
00273 
00274   statusBar()->addWidget( mStatusMsgLabel, 10 , false );
00275   statusBar()->addWidget( mLittleProgress, 0 , true );
00276   mLittleProgress->show();
00277 }
00278 
00279 
00280 void MainWindow::paintAboutScreen( const QString& msg )
00281 {
00282   QString location = locate( "data", "kontact/about/main.html" );
00283   QString content = KPIM::kFileToString( location );
00284   content = content.arg( locate( "data", "libkdepim/about/kde_infopage.css" ) );
00285   if ( kapp->reverseLayout() )
00286     content = content.arg( "@import \"%1\";" ).arg( locate( "data", "libkdepim/about/kde_infopage_rtl.css" ) );
00287   else
00288     content = content.arg( "" );
00289 
00290   mIntroPart->begin( KURL( location ) );
00291 
00292   QString appName( i18n( "KDE Kontact" ) );
00293   QString catchPhrase( i18n( "Get Organized!" ) );
00294   QString quickDescription( i18n( "The KDE Personal Information Management Suite" ) );
00295 
00296   mIntroPart->write( content.arg( QFont().pointSize() + 2 ).arg( appName )
00297       .arg( catchPhrase ).arg( quickDescription ).arg( msg ) );
00298   mIntroPart->end();
00299 }
00300 
00301 void MainWindow::initAboutScreen()
00302 {
00303   QHBox *introbox = new QHBox( mPartsStack );
00304   mPartsStack->addWidget( introbox );
00305   mPartsStack->raiseWidget( introbox );
00306   mIntroPart = new KHTMLPart( introbox );
00307   mIntroPart->widget()->setFocusPolicy( WheelFocus );
00308   // Let's better be paranoid and disable plugins (it defaults to enabled):
00309   mIntroPart->setPluginsEnabled( false );
00310   mIntroPart->setJScriptEnabled( false ); // just make this explicit
00311   mIntroPart->setJavaEnabled( false );    // just make this explicit
00312   mIntroPart->setMetaRefreshEnabled( false );
00313   mIntroPart->setURLCursor( KCursor::handCursor() );
00314   mIntroPart->view()->setLineWidth( 0 );
00315 
00316   connect( mIntroPart->browserExtension(),
00317            SIGNAL( openURLRequest( const KURL&, const KParts::URLArgs& ) ),
00318            SLOT( slotOpenUrl( const KURL& ) ) );
00319 
00320   connect( mIntroPart->browserExtension(),
00321            SIGNAL( createNewWindow( const KURL&, const KParts::URLArgs& ) ),
00322            SLOT( slotOpenUrl( const KURL& ) ) );
00323 }
00324 
00325 void MainWindow::setupActions()
00326 {
00327   KStdAction::quit( this, SLOT( slotQuit() ), actionCollection() );
00328   mNewActions = new KToolBarPopupAction( KGuiItem( i18n( "New" ), "" ),
00329                                          KStdAccel::shortcut(KStdAccel::New), this, SLOT( slotNewClicked() ),
00330                                          actionCollection(), "action_new" );
00331 
00332   KConfig* const cfg = Prefs::self()->config();
00333   cfg->setGroup( "Kontact Groupware Settings" );
00334   mSyncActionsEnabled = cfg->readBoolEntry( "GroupwareMailFoldersEnabled", true );
00335 
00336   if ( mSyncActionsEnabled ) {
00337     mSyncActions = new KToolBarPopupAction( KGuiItem( i18n( "Synchronize" ), "kitchensync" ),
00338                                             KStdAccel::shortcut(KStdAccel::Reload), this, SLOT( slotSyncClicked() ),
00339                                             actionCollection(), "action_sync" );
00340   }
00341   new KAction( i18n( "Configure Kontact..." ), "configure", 0, this, SLOT( slotPreferences() ),
00342                actionCollection(), "settings_configure_kontact" );
00343 
00344   new KAction( i18n( "Configure &Profiles..." ), 0, this, SLOT( slotConfigureProfiles() ),
00345                actionCollection(), "settings_configure_kontact_profiles" );
00346 
00347   new KAction( i18n( "&Kontact Introduction" ), 0, this, SLOT( slotShowIntroduction() ),
00348                actionCollection(), "help_introduction" );
00349   new KAction( i18n( "&Tip of the Day" ), 0, this, SLOT( slotShowTip() ),
00350                actionCollection(), "help_tipofday" );
00351 
00352   KWidgetAction* spacerAction = new KWidgetAction( new QWidget( this ), "SpacerAction", "", 0, 0, actionCollection(), "navigator_spacer_item" );
00353   spacerAction->setAutoSized( true );
00354 }
00355 
00356 void MainWindow::slotConfigureProfiles()
00357 {
00358   QGuardedPtr<Kontact::ProfileDialog> dlg = new Kontact::ProfileDialog( this );
00359   dlg->setModal( true );
00360   dlg->exec();
00361   delete dlg;
00362 }
00363 
00364 namespace {
00365     void copyConfigEntry( KConfig* source, KConfig* dest, const QString& group, const QString& key, const QString& defaultValue=QString() )
00366     {
00367         source->setGroup( group );
00368         dest->setGroup( group );
00369         dest->writeEntry( key, source->readEntry( key, defaultValue ) );
00370     }
00371 }
00372 
00373 void MainWindow::slotSaveToProfile( const QString& id )
00374 {
00375   const QString path = Kontact::ProfileManager::self()->profileById( id ).saveLocation();
00376   if ( path.isNull() )
00377     return;
00378 
00379   KConfig* const cfg = Prefs::self()->config();
00380   Prefs::self()->writeConfig();
00381   saveMainWindowSettings( cfg );
00382   saveSettings();
00383 
00384   KConfig profile( path+"/kontactrc", /*read-only=*/false, /*useglobals=*/false );
00385   ::copyConfigEntry( cfg, &profile, "MainWindow Toolbar navigatorToolBar", "Hidden", "true" );
00386   ::copyConfigEntry( cfg, &profile, "View", "SidePaneSplitter" );
00387   ::copyConfigEntry( cfg, &profile, "Icons", "Theme" );
00388 
00389   for ( PluginList::Iterator it = mPlugins.begin(); it != mPlugins.end(); ++it ) {
00390     if ( !(*it)->isRunningStandalone() ) {
00391         (*it)->part();
00392     }
00393     (*it)->saveToProfile( path );
00394   }
00395 }
00396 
00397 void MainWindow::slotLoadProfile( const QString& id )
00398 {
00399   const QString path = Kontact::ProfileManager::self()->profileById( id ).saveLocation();
00400   if ( path.isNull() )
00401     return;
00402 
00403   KConfig* const cfg = Prefs::self()->config();
00404   Prefs::self()->writeConfig();
00405   saveMainWindowSettings( cfg );
00406   saveSettings();
00407 
00408   const KConfig profile( path+"/kontactrc", /*read-only=*/false, /*useglobals=*/false );
00409   const QStringList groups = profile.groupList();
00410   for ( QStringList::ConstIterator it = groups.begin(), end = groups.end(); it != end; ++it )
00411   {
00412     cfg->setGroup( *it );
00413     typedef QMap<QString, QString> StringMap;
00414     const StringMap entries = profile.entryMap( *it );
00415     for ( StringMap::ConstIterator it2 = entries.begin(), end = entries.end(); it2 != end; ++it2 )
00416     {
00417       if ( it2.data() == "KONTACT_PROFILE_DELETE_KEY" )
00418         cfg->deleteEntry( it2.key() );
00419       else
00420         cfg->writeEntry( it2.key(), it2.data() );
00421     }
00422   }
00423 
00424   cfg->sync();
00425   Prefs::self()->readConfig();
00426   applyMainWindowSettings( cfg );
00427   KIconTheme::reconfigure();
00428   const WId wid = winId();
00429   KIPC::sendMessage( KIPC::PaletteChanged, wid );
00430   KIPC::sendMessage( KIPC::FontChanged, wid );
00431   KIPC::sendMessage( KIPC::StyleChanged, wid );
00432   KIPC::sendMessage( KIPC::SettingsChanged, wid );
00433   for ( int i = 0; i < KIcon::LastGroup; ++i )
00434       KIPC::sendMessage( KIPC::IconChanged, wid, i );
00435 
00436   loadSettings();
00437 
00438   for ( PluginList::Iterator it = mPlugins.begin(); it != mPlugins.end(); ++it ) {
00439     if ( !(*it)->isRunningStandalone() ) {
00440         kdDebug() << "Ensure loaded: " << (*it)->identifier() << endl;
00441         (*it)->part();
00442     }
00443     (*it)->loadProfile( path );
00444   }
00445 }
00446 
00447 bool MainWindow::isPluginLoaded( const KPluginInfo *info )
00448 {
00449   return (pluginFromInfo( info ) != 0);
00450 }
00451 
00452 Plugin *MainWindow::pluginFromInfo( const KPluginInfo *info )
00453 {
00454   PluginList::ConstIterator end = mPlugins.end();
00455   for ( PluginList::ConstIterator it = mPlugins.begin(); it != end; ++it )
00456     if ( (*it)->identifier() == info->pluginName() )
00457       return *it;
00458 
00459   return 0;
00460 }
00461 
00462 void MainWindow::loadPlugins()
00463 {
00464   QPtrList<Plugin> plugins;
00465   QPtrList<KParts::Part> loadDelayed;
00466 
00467   uint i;
00468   KPluginInfo::List::ConstIterator it;
00469   for ( it = mPluginInfos.begin(); it != mPluginInfos.end(); ++it ) {
00470     if ( !(*it)->isPluginEnabled() )
00471       continue;
00472     if ( isPluginLoaded( *it ) ) {
00473       Plugin *plugin = pluginFromInfo( *it );
00474       if ( plugin )
00475         plugin->configUpdated();
00476       continue;
00477     }
00478 
00479     kdDebug(5600) << "Loading Plugin: " << (*it)->name() << endl;
00480     Kontact::Plugin *plugin =
00481       KParts::ComponentFactory::createInstanceFromService<Kontact::Plugin>(
00482           (*it)->service(), this );
00483 
00484     if ( !plugin )
00485       continue;
00486 
00487     plugin->setIdentifier( (*it)->pluginName() );
00488     plugin->setTitle( (*it)->name() );
00489     plugin->setIcon( (*it)->icon() );
00490 
00491     QVariant libNameProp = (*it)->property( "X-KDE-KontactPartLibraryName" );
00492     QVariant exeNameProp = (*it)->property( "X-KDE-KontactPartExecutableName" );
00493     QVariant loadOnStart = (*it)->property( "X-KDE-KontactPartLoadOnStart" );
00494     QVariant hasPartProp = (*it)->property( "X-KDE-KontactPluginHasPart" );
00495 
00496     if ( !loadOnStart.isNull() && loadOnStart.toBool() )
00497       mDelayedPreload.append( plugin );
00498 
00499     kdDebug(5600) << "LIBNAMEPART: " << libNameProp.toString() << endl;
00500 
00501     plugin->setPartLibraryName( libNameProp.toString().utf8() );
00502     plugin->setExecutableName( exeNameProp.toString() );
00503     if ( hasPartProp.isValid() )
00504       plugin->setShowInSideBar( hasPartProp.toBool() );
00505 
00506     for ( i = 0; i < plugins.count(); ++i ) {
00507       Plugin *p = plugins.at( i );
00508       if ( plugin->weight() < p->weight() )
00509         break;
00510     }
00511 
00512     plugins.insert( i, plugin );
00513   }
00514 
00515   for ( i = 0; i < plugins.count(); ++ i ) {
00516     Plugin *plugin = plugins.at( i );
00517 
00518     KAction *action;
00519     QPtrList<KAction> *actionList = plugin->newActions();
00520 
00521     for ( action = actionList->first(); action; action = actionList->next() ) {
00522       kdDebug(5600) << "Plugging " << action->name() << endl;
00523       action->plug( mNewActions->popupMenu() );
00524     }
00525 
00526     if ( mSyncActionsEnabled ) {
00527       actionList = plugin->syncActions();
00528       for ( action = actionList->first(); action; action = actionList->next() ) {
00529         kdDebug(5600) << "Plugging " << action->name() << endl;
00530         action->plug( mSyncActions->popupMenu() );
00531       }
00532     }
00533     addPlugin( plugin );
00534   }
00535 
00536   mNewActions->setEnabled( mPlugins.size() != 0 );
00537   if ( mSyncActionsEnabled )
00538     mSyncActions->setEnabled( mPlugins.size() != 0 );
00539 }
00540 
00541 void MainWindow::unloadPlugins()
00542 {
00543   KPluginInfo::List::ConstIterator end = mPluginInfos.end();
00544   KPluginInfo::List::ConstIterator it;
00545   for ( it = mPluginInfos.begin(); it != end; ++it ) {
00546     if ( !(*it)->isPluginEnabled() )
00547       removePlugin( *it );
00548   }
00549 }
00550 
00551 bool MainWindow::removePlugin( const KPluginInfo *info )
00552 {
00553   PluginList::Iterator end = mPlugins.end();
00554   for ( PluginList::Iterator it = mPlugins.begin(); it != end; ++it )
00555     if ( ( *it )->identifier() == info->pluginName() ) {
00556       Plugin *plugin = *it;
00557 
00558       KAction *action;
00559       QPtrList<KAction> *actionList = plugin->newActions();
00560 
00561       for ( action = actionList->first(); action; action = actionList->next() ) {
00562         kdDebug(5600) << "Unplugging " << action->name() << endl;
00563         action->unplug( mNewActions->popupMenu() );
00564       }
00565 
00566       if ( mSyncActionsEnabled ) {
00567         actionList = plugin->syncActions();
00568         for ( action = actionList->first(); action; action = actionList->next() ) {
00569           kdDebug(5600) << "Unplugging " << action->name() << endl;
00570           action->unplug( mSyncActions->popupMenu() );
00571         }
00572       }
00573       removeChildClient( plugin );
00574 
00575       if ( mCurrentPlugin == plugin )
00576         mCurrentPlugin = 0;
00577 
00578       delete plugin; // removes the part automatically
00579       mPlugins.remove( it );
00580 
00581       if ( mCurrentPlugin == 0 ) {
00582         PluginList::Iterator it;
00583         for ( it = mPlugins.begin(); it != mPlugins.end(); ++it ) {
00584           if ( (*it)->showInSideBar() ) {
00585             selectPlugin( *it );
00586             return true;
00587           }
00588         }
00589       }
00590 
00591       return true;
00592     }
00593 
00594   return false;
00595 }
00596 
00597 void MainWindow::addPlugin( Kontact::Plugin *plugin )
00598 {
00599   kdDebug(5600) << "Added plugin" << endl;
00600 
00601   mPlugins.append( plugin );
00602 
00603   // merge the plugins GUI into the main window
00604   insertChildClient( plugin );
00605 }
00606 
00607 void MainWindow::partLoaded( Kontact::Plugin*, KParts::ReadOnlyPart *part )
00608 {
00609   // See if we have this part already (e.g. due to two plugins sharing it)
00610   if ( mPartsStack->id( part->widget() ) != -1 )
00611     return;
00612 
00613   mPartsStack->addWidget( part->widget() );
00614 
00615   mPartManager->addPart( part, false );
00616   // Workaround for KParts misbehavior: addPart calls show!
00617   part->widget()->hide();
00618 }
00619 
00620 void MainWindow::slotActivePartChanged( KParts::Part *part )
00621 {
00622   if ( !part ) {
00623     createGUI( 0 );
00624     return;
00625   }
00626 
00627   kdDebug(5600) << "Part activated: " << part << " with stack id. "
00628       << mPartsStack->id( part->widget() )<< endl;
00629 
00630   //createGUI( part ); // moved to selectPlugin()
00631 
00632   statusBar()->clear();
00633 }
00634 
00635 void MainWindow::slotNewClicked()
00636 {
00637   KAction *action = mCurrentPlugin->newActions()->first();
00638   if ( action ) {
00639     action->activate();
00640   } else {
00641     PluginList::Iterator it;
00642     for ( it = mPlugins.begin(); it != mPlugins.end(); ++it ) {
00643       action = (*it)->newActions()->first();
00644       if ( action ) {
00645         action->activate();
00646         return;
00647       }
00648     }
00649   }
00650 }
00651 
00652 void MainWindow::slotSyncClicked()
00653 {
00654   KAction *action = mCurrentPlugin->syncActions()->first();
00655   if ( action ) {
00656     action->activate();
00657   } else {
00658     PluginList::Iterator it;
00659     for ( it = mPlugins.begin(); it != mPlugins.end(); ++it ) {
00660       action = (*it)->syncActions()->first();
00661       if ( action ) {
00662         action->activate();
00663         return;
00664       }
00665     }
00666   }
00667 }
00668 
00669 KToolBar* Kontact::MainWindow::findToolBar(const char* name)
00670 {
00671   // like KMainWindow::toolBar, but which doesn't create the toolbar if not found
00672   return static_cast<KToolBar *>(child(name, "KToolBar"));
00673 }
00674 
00675 void MainWindow::selectPlugin( Kontact::Plugin *plugin )
00676 {
00677   if ( !plugin )
00678     return;
00679 
00680   if ( plugin->isRunningStandalone() ) {
00681     statusBar()->message( i18n( "Application is running standalone. Foregrounding..." ), 1000 );
00682     mSidePane->indicateForegrunding( plugin );
00683     plugin->bringToForeground();
00684     return;
00685   }
00686 
00687   KApplication::setOverrideCursor( QCursor( Qt::WaitCursor ) );
00688 
00689   KParts::Part *part = plugin->part();
00690 
00691   if ( !part ) {
00692     KApplication::restoreOverrideCursor();
00693     KMessageBox::error( this, i18n( "Cannot load part for %1." )
00694                               .arg( plugin->title() )
00695                         + "\n" + lastErrorMessage() );
00696     plugin->setDisabled( true );
00697     mSidePane->updatePlugins();
00698     return;
00699   }
00700 
00701   // store old focus widget
00702   QWidget *focusWidget = kapp->focusWidget();
00703   if ( mCurrentPlugin && focusWidget ) {
00704     // save the focus widget only when it belongs to the activated part
00705     QWidget *parent = focusWidget->parentWidget();
00706     while ( parent ) {
00707       if ( parent == mCurrentPlugin->part()->widget() )
00708         mFocusWidgets.insert( mCurrentPlugin->identifier(), QGuardedPtr<QWidget>( focusWidget ) );
00709 
00710       parent = parent->parentWidget();
00711     }
00712   }
00713 
00714   if ( mSidePane )
00715     mSidePane->selectPlugin( plugin );
00716 
00717   plugin->select();
00718 
00719   mPartManager->setActivePart( part );
00720   QWidget *view = part->widget();
00721   Q_ASSERT( view );
00722 
00723   if ( view ) {
00724     mPartsStack->raiseWidget( view );
00725     view->show();
00726 
00727     if ( mFocusWidgets.contains( plugin->identifier() ) ) {
00728       focusWidget = mFocusWidgets[ plugin->identifier() ];
00729       if ( focusWidget )
00730         focusWidget->setFocus();
00731     } else
00732       view->setFocus();
00733 
00734     mCurrentPlugin = plugin;
00735     KAction *newAction = plugin->newActions()->first();
00736     KAction *syncAction = plugin->syncActions()->first();
00737 
00738     createGUI( plugin->part() );
00739 
00740     KToolBar* navigatorToolBar = findToolBar( "navigatorToolBar" );
00741     // Let the navigator toolbar be always the last one, if it's in the top dockwindow
00742     if ( navigatorToolBar && !navigatorToolBar->isHidden() &&
00743          navigatorToolBar->barPos() == KToolBar::Top ) {
00744       topDock()->moveDockWindow( navigatorToolBar, -1 );
00745     }
00746 
00747     setCaption( i18n( "Plugin dependent window title" ,"%1 - Kontact" ).arg( plugin->title() ) );
00748 
00749     if ( newAction ) {
00750       mNewActions->setIcon( newAction->icon() );
00751       mNewActions->setText( newAction->text() );
00752     } else { // we'll use the action of the first plugin which offers one
00753       PluginList::Iterator it;
00754       for ( it = mPlugins.begin(); it != mPlugins.end(); ++it ) {
00755         newAction = (*it)->newActions()->first();
00756         if ( newAction ) {
00757           mNewActions->setIcon( newAction->icon() );
00758           mNewActions->setText( newAction->text() );
00759           break;
00760         }
00761       }
00762     }
00763     if ( mSyncActionsEnabled ) {
00764       if ( syncAction ) {
00765         mSyncActions->setIcon( syncAction->icon() );
00766         mSyncActions->setText( syncAction->text() );
00767       } else { // we'll use the action of the first plugin which offers one
00768         PluginList::Iterator it;
00769         for ( it = mPlugins.begin(); it != mPlugins.end(); ++it ) {
00770           syncAction = (*it)->syncActions()->first();
00771           if ( syncAction ) {
00772             mSyncActions->setIcon( syncAction->icon() );
00773             mSyncActions->setText( syncAction->text() );
00774             break;
00775           }
00776         }
00777       }
00778     }
00779   }
00780   QStringList invisibleActions = plugin->invisibleToolbarActions();
00781 
00782   QStringList::ConstIterator it;
00783   for ( it = invisibleActions.begin(); it != invisibleActions.end(); ++it ) {
00784     KAction *action = part->actionCollection()->action( (*it).latin1() );
00785     if ( action ) {
00786       QPtrListIterator<KToolBar> it(  toolBarIterator() );
00787       for (  ; it.current() ; ++it ) {
00788         action->unplug( it.current() );
00789       }
00790     }
00791   }
00792 
00793   KApplication::restoreOverrideCursor();
00794 }
00795 
00796 void MainWindow::selectPlugin( const QString &pluginName )
00797 {
00798   PluginList::ConstIterator end = mPlugins.end();
00799   for ( PluginList::ConstIterator it = mPlugins.begin(); it != end; ++it )
00800     if ( ( *it )->identifier() == pluginName ) {
00801       selectPlugin( *it );
00802       return;
00803     }
00804 }
00805 
00806 void MainWindow::loadSettings()
00807 {
00808   if ( mSplitter )
00809     mSplitter->setSizes( Prefs::self()->mSidePaneSplitter );
00810 
00811   // Preload Plugins. This _must_ happen before the default part is loaded
00812   PluginList::ConstIterator it;
00813   for ( it = mDelayedPreload.begin(); it != mDelayedPreload.end(); ++it )
00814     selectPlugin( *it );
00815 
00816   selectPlugin( Prefs::self()->mActivePlugin );
00817 }
00818 
00819 void MainWindow::saveSettings()
00820 {
00821   if ( mSplitter )
00822     Prefs::self()->mSidePaneSplitter = mSplitter->sizes();
00823 
00824   if ( mCurrentPlugin )
00825     Prefs::self()->mActivePlugin = mCurrentPlugin->identifier();
00826 }
00827 
00828 void MainWindow::slotShowTip()
00829 {
00830   showTip( true );
00831 }
00832 
00833 void MainWindow::slotShowIntroduction()
00834 {
00835   mPartsStack->raiseWidget( 0 ); // ###
00836 }
00837 
00838 void MainWindow::showTip( bool force )
00839 {
00840   QStringList tips;
00841   PluginList::ConstIterator end = mPlugins.end();
00842   for ( PluginList::ConstIterator it = mPlugins.begin(); it != end; ++it ) {
00843     QString file = (*it)->tipFile();
00844     if ( !file.isEmpty() )
00845       tips.append( file );
00846   }
00847 
00848   KTipDialog::showMultiTip( this, tips, force );
00849 }
00850 
00851 void MainWindow::slotQuit()
00852 {
00853   mReallyClose = true;
00854   close();
00855 }
00856 
00857 void MainWindow::slotPreferences()
00858 {
00859   static SettingsDialogWrapper *dlg = 0;
00860   if ( !dlg ) {
00861     // do not show settings of components running standalone
00862     QValueList<KPluginInfo*> filteredPlugins = mPluginInfos;
00863     PluginList::ConstIterator it;
00864     for ( it = mPlugins.begin(); it != mPlugins.end(); ++it )
00865       if ( (*it)->isRunningStandalone() ) {
00866         QValueList<KPluginInfo*>::ConstIterator infoIt;
00867         for ( infoIt = filteredPlugins.begin(); infoIt != filteredPlugins.end(); ++infoIt ) {
00868           if ( (*infoIt)->pluginName() == (*it)->identifier() ) {
00869             filteredPlugins.remove( *infoIt );
00870             break;
00871           }
00872         }
00873       }
00874     dlg = new SettingsDialogWrapper( KSettings::Dialog::Configurable, this );
00875     dlg->addPluginInfos( filteredPlugins );
00876     connect( dlg, SIGNAL( pluginSelectionChanged() ),
00877              SLOT( pluginsChanged() ) );
00878   }
00879 
00880   dlg->show();
00881   dlg->fixButtonLabel( this );
00882 }
00883 
00884 int MainWindow::startServiceFor( const QString& serviceType,
00885                                  const QString& constraint,
00886                                  const QString& preferences,
00887                                  QString *error, QCString* dcopService,
00888                                  int flags )
00889 {
00890   PluginList::ConstIterator end = mPlugins.end();
00891   for ( PluginList::ConstIterator it = mPlugins.begin(); it != end; ++it ) {
00892     if ( (*it)->createDCOPInterface( serviceType ) ) {
00893       kdDebug(5600) << "found interface for " << serviceType << endl;
00894       if ( dcopService )
00895         *dcopService = (*it)->dcopClient()->appId();
00896       kdDebug(5600) << "appId=" << (*it)->dcopClient()->appId() << endl;
00897       return 0; // success
00898     }
00899   }
00900 
00901   kdDebug(5600) <<
00902     "Didn't find dcop interface, falling back to external process" << endl;
00903 
00904   return KDCOPServiceStarter::startServiceFor( serviceType, constraint,
00905       preferences, error, dcopService, flags );
00906 }
00907 
00908 void MainWindow::pluginsChanged()
00909 {
00910   unplugActionList( "navigator_actionlist" );
00911   unloadPlugins();
00912   loadPlugins();
00913   mSidePane->updatePlugins();
00914   plugActionList( "navigator_actionlist", mSidePane->actions() );
00915 }
00916 
00917 void MainWindow::updateConfig()
00918 {
00919   kdDebug( 5600 ) << k_funcinfo << endl;
00920 
00921   saveSettings();
00922   loadSettings();
00923 }
00924 
00925 void MainWindow::showAboutDialog()
00926 {
00927   KApplication::setOverrideCursor( QCursor( Qt::WaitCursor ) );
00928 
00929   if ( !mAboutDialog )
00930     mAboutDialog = new AboutDialog( this );
00931 
00932   mAboutDialog->show();
00933   mAboutDialog->raise();
00934   KApplication::restoreOverrideCursor();
00935 }
00936 
00937 void MainWindow::configureShortcuts()
00938 {
00939   KKeyDialog dialog( true, this );
00940   dialog.insert( actionCollection() );
00941 
00942   if ( mCurrentPlugin && mCurrentPlugin->part() )
00943     dialog.insert( mCurrentPlugin->part()->actionCollection() );
00944 
00945   dialog.configure();
00946 }
00947 
00948 void MainWindow::configureToolbars()
00949 {
00950   saveMainWindowSettings( KGlobal::config(), "MainWindow" );
00951 
00952   KEditToolbar edit( factory() );
00953   connect( &edit, SIGNAL( newToolbarConfig() ),
00954            this, SLOT( slotNewToolbarConfig() ) );
00955   edit.exec();
00956 }
00957 
00958 void MainWindow::slotNewToolbarConfig()
00959 {
00960   if ( mCurrentPlugin && mCurrentPlugin->part() )
00961     createGUI( mCurrentPlugin->part() );
00962   applyMainWindowSettings( KGlobal::config(), "MainWindow" );
00963 }
00964 
00965 void MainWindow::slotOpenUrl( const KURL &url )
00966 {
00967   if ( url.protocol() == "exec" ) {
00968     if ( url.path() == "/switch" ) {
00969       selectPlugin( mCurrentPlugin );
00970     }
00971     if ( url.path() == "/gwwizard" ) {
00972       KRun::runCommand( "groupwarewizard" );
00973       slotQuit();
00974     }
00975     if ( url.path().startsWith( "/help" ) ) {
00976       QString app( "kontact" );
00977       if ( !url.query().isEmpty() ) {
00978         app = url.query().mid( 1 );
00979       }
00980       kapp->invokeHelp( QString::null, app );
00981     }
00982   } else {
00983     new KRun( url, this );
00984   }
00985 }
00986 
00987 void MainWindow::readProperties( KConfig *config )
00988 {
00989   Core::readProperties( config );
00990 
00991   QStringList activePlugins = config->readListEntry( "ActivePlugins" );
00992   QValueList<Plugin*>::ConstIterator it = mPlugins.begin();
00993   QValueList<Plugin*>::ConstIterator end = mPlugins.end();
00994   for ( ; it != end; ++it ) {
00995     Plugin *plugin = *it;
00996     if ( !plugin->isRunningStandalone() ) {
00997       QStringList::ConstIterator activePlugin = activePlugins.find( plugin->identifier() );
00998       if ( activePlugin != activePlugins.end() ) {
00999         plugin->readProperties( config );
01000       }
01001     }
01002   }
01003 }
01004 
01005 void MainWindow::saveProperties( KConfig *config )
01006 {
01007   Core::saveProperties( config );
01008 
01009   QStringList activePlugins;
01010 
01011   KPluginInfo::List::Iterator it = mPluginInfos.begin();
01012   KPluginInfo::List::Iterator end = mPluginInfos.end();
01013   for ( ; it != end; ++it ) {
01014     KPluginInfo *info = *it;
01015     if ( info->isPluginEnabled() ) {
01016       Plugin *plugin = pluginFromInfo( info );
01017       if ( plugin ) {
01018         activePlugins.append( plugin->identifier() );
01019         plugin->saveProperties( config );
01020       }
01021     }
01022   }
01023 
01024   config->writeEntry( "ActivePlugins", activePlugins );
01025 }
01026 
01027 bool MainWindow::queryClose()
01028 {
01029   if ( kapp->sessionSaving() || mReallyClose )
01030     return true;
01031 
01032   bool localClose = true;
01033   QValueList<Plugin*>::ConstIterator end = mPlugins.end();
01034   QValueList<Plugin*>::ConstIterator it = mPlugins.begin();
01035   for ( ; it != end; ++it ) {
01036     Plugin *plugin = *it;
01037     if ( !plugin->isRunningStandalone() )
01038       if ( !plugin->queryClose() )
01039         localClose = false;
01040   }
01041 
01042   return localClose;
01043 }
01044 
01045 void MainWindow::slotShowStatusMsg( const QString &msg )
01046 {
01047   if ( !statusBar() || !mStatusMsgLabel )
01048      return;
01049 
01050   mStatusMsgLabel->setText( msg );
01051 }
01052 
01053 QString MainWindow::introductionString()
01054 {
01055   KIconLoader *iconloader = KGlobal::iconLoader();
01056   int iconSize = iconloader->currentSize( KIcon::Desktop );
01057 
01058   QString handbook_icon_path = iconloader->iconPath( "contents2",  KIcon::Desktop );
01059   QString html_icon_path = iconloader->iconPath( "html",  KIcon::Desktop );
01060   QString wizard_icon_path = iconloader->iconPath( "wizard",  KIcon::Desktop );
01061 
01062   QString info = i18n( "<h2 style='text-align:center; margin-top: 0px;'>Welcome to Kontact %1</h2>"
01063       "<p>%1</p>"
01064       "<table align=\"center\">"
01065       "<tr><td><a href=\"%1\"><img width=\"%1\" height=\"%1\" src=\"%1\" /></a></td>"
01066       "<td><a href=\"%1\">%1</a><br><span id=\"subtext\"><nobr>%1</td></tr>"
01067       "<tr><td><a href=\"%1\"><img width=\"%1\" height=\"%1\" src=\"%1\" /></a></td>"
01068       "<td><a href=\"%1\">%1</a><br><span id=\"subtext\"><nobr>%1</td></tr>"
01069       "<tr><td><a href=\"%1\"><img width=\"%1\" height=\"%1\" src=\"%1\" /></a></td>"
01070       "<td><a href=\"%1\">%1</a><br><span id=\"subtext\"><nobr>%1</td></tr>"
01071       "</table>"
01072       "<p style=\"margin-bottom: 0px\"> <a href=\"%1\">Skip this introduction</a></p>" )
01073       .arg( kapp->aboutData()->version() )
01074       .arg( i18n( "Kontact handles your e-mail, addressbook, calendar, to-do list and more." ) )
01075       .arg( "exec:/help?kontact" )
01076       .arg( iconSize )
01077       .arg( iconSize )
01078       .arg( handbook_icon_path )
01079       .arg( "exec:/help?kontact" )
01080       .arg( i18n( "Read Manual" ) )
01081       .arg( i18n( "Learn more about Kontact and its components" ) )
01082       .arg( "http://kontact.org" )
01083       .arg( iconSize )
01084       .arg( iconSize )
01085       .arg( html_icon_path )
01086       .arg( "http://kontact.org" )
01087       .arg( i18n( "Visit Kontact Website" ) )
01088       .arg( i18n( "Access online resources and tutorials" ) )
01089       .arg( "exec:/gwwizard" )
01090       .arg( iconSize )
01091       .arg( iconSize )
01092       .arg( wizard_icon_path )
01093       .arg( "exec:/gwwizard" )
01094       .arg( i18n( "Configure Kontact as Groupware Client" ) )
01095       .arg( i18n( "Prepare Kontact for use in corporate networks" ) )
01096       .arg( "exec:/switch" );
01097   return info;
01098 }
01099 
01100 #include "mainwindow.moc"