kontact

summaryview_part.cpp

00001 /*
00002    This file is part of KDE Kontact.
00003 
00004    Copyright (C) 2003 Sven Lüppken <sven@kde.org>
00005    Copyright (C) 2003 Tobias König <tokoe@kde.org>
00006    Copyright (C) 2003 Daniel Molkentin <molkentin@kde.org>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License as published by the Free Software Foundation; either
00011    version 2 of the License, or (at your option) any later version.
00012 
00013    This library 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 GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public License
00019    along with this library; see the file COPYING.LIB.  If not, write to
00020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021    Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include <qframe.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qtimer.h>
00028 
00029 #include <dcopclient.h>
00030 #include <kaction.h>
00031 #include <kapplication.h>
00032 #include <kconfig.h>
00033 #include <kdcopservicestarter.h>
00034 #include <kdebug.h>
00035 #include <kdialog.h>
00036 #include <klocale.h>
00037 #include <kmessagebox.h>
00038 #include <kservice.h>
00039 #include <ktrader.h>
00040 #include <kstandarddirs.h>
00041 #include <qscrollview.h>
00042 #include <kglobal.h>
00043 #include <klocale.h>
00044 #include <kcmultidialog.h>
00045 
00046 #include <kparts/componentfactory.h>
00047 #include <kparts/event.h>
00048 
00049 #include <libkpimidentities/identity.h>
00050 #include <libkpimidentities/identitymanager.h>
00051 
00052 #include <infoextension.h>
00053 #include <sidebarextension.h>
00054 
00055 #include "plugin.h"
00056 #include "summary.h"
00057 
00058 #include "summaryview_part.h"
00059 
00060 #include "broadcaststatus.h"
00061 using KPIM::BroadcastStatus;
00062 
00063 namespace Kontact
00064 {
00065   class MainWindow;
00066 }
00067 
00068 SummaryViewPart::SummaryViewPart( Kontact::Core *core, const char*,
00069                                   const KAboutData *aboutData,
00070                                   QObject *parent, const char *name )
00071   : KParts::ReadOnlyPart( parent, name ),
00072     mCore( core ), mFrame( 0 ), mConfigAction( 0 )
00073 {
00074   setInstance( new KInstance( aboutData ) );
00075 
00076   loadLayout();
00077 
00078   initGUI( core );
00079 
00080   connect( kapp, SIGNAL( kdisplayPaletteChanged() ), SLOT( slotAdjustPalette() ) );
00081   slotAdjustPalette();
00082 
00083   setDate( QDate::currentDate() );
00084   connect( mCore, SIGNAL( dayChanged( const QDate& ) ),
00085            SLOT( setDate( const QDate& ) ) );
00086 
00087   KParts::InfoExtension *info = new KParts::InfoExtension( this, "Summary" );
00088   connect( this, SIGNAL( textChanged( const QString& ) ),
00089            info, SIGNAL( textChanged( const QString& ) ) );
00090 
00091   mConfigAction = new KAction( i18n( "&Configure Summary View..." ),
00092                                "configure", 0, this,
00093                                SLOT( slotConfigure() ), actionCollection(),
00094                                "summaryview_configure" );
00095 
00096   mRefreshAction = new KAction( i18n( "&Refresh Summary View..." ),
00097                                 "reload", 0, this,
00098                                 SLOT( updateSummaries() ), actionCollection(),
00099                                 "summaryview_refresh" );
00100 
00101   setXMLFile( "kontactsummary_part.rc" );
00102 
00103   QTimer::singleShot( 0, this, SLOT( slotTextChanged() ) );
00104 }
00105 
00106 SummaryViewPart::~SummaryViewPart()
00107 {
00108   saveLayout();
00109 }
00110 
00111 bool SummaryViewPart::openFile()
00112 {
00113   kdDebug(5006) << "SummaryViewPart:openFile()" << endl;
00114   return true;
00115 }
00116 
00117 void SummaryViewPart::partActivateEvent( KParts::PartActivateEvent *event )
00118 {
00119   // inform the plugins that the part has been activated so that they can
00120   // update the displayed information
00121   if ( event->activated() && ( event->part() == this ) ) {
00122     updateSummaries();
00123   }
00124 
00125   KParts::ReadOnlyPart::partActivateEvent( event );
00126 }
00127 
00128 void SummaryViewPart::updateSummaries()
00129 {
00130   QMap<QString, Kontact::Summary*>::Iterator it;
00131   for ( it = mSummaries.begin(); it != mSummaries.end(); ++it )
00132     it.data()->updateSummary( false );
00133 }
00134 
00135 void SummaryViewPart::updateWidgets()
00136 {
00137   mMainWidget->setUpdatesEnabled( false );
00138 
00139   delete mFrame;
00140 
00141   KPIM::IdentityManager idm( true, this );
00142   const KPIM::Identity &id = idm.defaultIdentity();
00143 
00144   QString currentUser = i18n( "Summary for %1" ).arg( id.fullName() );
00145   mUsernameLabel->setText( QString::fromLatin1( "<b>%1</b>" ).arg( currentUser ) );
00146 
00147   mSummaries.clear();
00148 
00149   mFrame = new DropWidget( mMainWidget );
00150   connect( mFrame, SIGNAL( summaryWidgetDropped( QWidget*, QWidget*, int ) ),
00151            this, SLOT( summaryWidgetMoved( QWidget*, QWidget*, int ) ) );
00152 
00153   mMainLayout->insertWidget( 2, mFrame );
00154 
00155   QStringList activeSummaries;
00156 
00157   KConfig config( "kontact_summaryrc" );
00158   if ( !config.hasKey( "ActiveSummaries" ) ) {
00159     activeSummaries << "kontact_kmailplugin";
00160     activeSummaries << "kontact_specialdatesplugin";
00161     activeSummaries << "kontact_korganizerplugin";
00162     activeSummaries << "kontact_todoplugin";
00163     activeSummaries << "kontact_newstickerplugin";
00164   } else {
00165     activeSummaries = config.readListEntry( "ActiveSummaries" );
00166   }
00167 
00168   // Collect all summary widgets with a summaryHeight > 0
00169   QStringList loadedSummaries;
00170 
00171   QValueList<Kontact::Plugin*> plugins = mCore->pluginList();
00172   QValueList<Kontact::Plugin*>::ConstIterator end = plugins.end();
00173   QValueList<Kontact::Plugin*>::ConstIterator it = plugins.begin();
00174   for ( ; it != end; ++it ) {
00175     Kontact::Plugin *plugin = *it;
00176     if ( activeSummaries.find( plugin->identifier() ) == activeSummaries.end() )
00177       continue;
00178 
00179     Kontact::Summary *summary = plugin->createSummaryWidget( mFrame );
00180     if ( summary ) {
00181       if ( summary->summaryHeight() > 0 ) {
00182         mSummaries.insert( plugin->identifier(), summary );
00183 
00184         connect( summary, SIGNAL( message( const QString& ) ),
00185                  BroadcastStatus::instance(), SLOT( setStatusMsg( const QString& ) ) );
00186         connect( summary, SIGNAL( summaryWidgetDropped( QWidget*, QWidget*, int ) ),
00187                  this, SLOT( summaryWidgetMoved( QWidget*, QWidget*, int ) ) );
00188 
00189         if ( !mLeftColumnSummaries.contains( plugin->identifier() ) &&
00190              !mRightColumnSummaries.contains( plugin->identifier() ) ) {
00191           mLeftColumnSummaries.append( plugin->identifier() );
00192         }
00193 
00194         loadedSummaries.append( plugin->identifier() );
00195       } else {
00196         summary->hide();
00197       }
00198     }
00199   }
00200 
00201   // Remove all unavailable summary widgets
00202   {
00203     QStringList::Iterator strIt;
00204     for ( strIt = mLeftColumnSummaries.begin(); strIt != mLeftColumnSummaries.end(); ++strIt ) {
00205       if ( loadedSummaries.find( *strIt ) == loadedSummaries.end() ) {
00206         strIt = mLeftColumnSummaries.remove( strIt );
00207         --strIt;
00208       }
00209     }
00210     for ( strIt = mRightColumnSummaries.begin(); strIt != mRightColumnSummaries.end(); ++strIt ) {
00211       if ( loadedSummaries.find( *strIt ) == loadedSummaries.end() ) {
00212         strIt = mRightColumnSummaries.remove( strIt );
00213         --strIt;
00214       }
00215     }
00216   }
00217 
00218   // Add vertical line between the two rows of summary widgets.
00219   QFrame *vline = new QFrame( mFrame );
00220   vline->setFrameStyle( QFrame::VLine | QFrame::Plain );
00221 
00222   QHBoxLayout *layout = new QHBoxLayout( mFrame );
00223 
00224   mLeftColumn = new QVBoxLayout( layout, KDialog::spacingHint() );
00225   layout->addWidget( vline );
00226   mRightColumn = new QVBoxLayout( layout, KDialog::spacingHint()  );
00227 
00228 
00229   QStringList::Iterator strIt;
00230   for ( strIt = mLeftColumnSummaries.begin(); strIt != mLeftColumnSummaries.end(); ++strIt ) {
00231     if ( mSummaries.find( *strIt ) != mSummaries.end() )
00232       mLeftColumn->addWidget( mSummaries[ *strIt ] );
00233   }
00234 
00235   for ( strIt = mRightColumnSummaries.begin(); strIt != mRightColumnSummaries.end(); ++strIt ) {
00236     if ( mSummaries.find( *strIt ) != mSummaries.end() )
00237       mRightColumn->addWidget( mSummaries[ *strIt ] );
00238   }
00239 
00240   mFrame->show();
00241 
00242   mMainWidget->setUpdatesEnabled( true );
00243   mMainWidget->update();
00244 
00245   mLeftColumn->addStretch();
00246   mRightColumn->addStretch();
00247 }
00248 
00249 void SummaryViewPart::summaryWidgetMoved( QWidget *target, QWidget *widget, int alignment )
00250 {
00251   if ( target == widget )
00252     return;
00253 
00254   if ( target == mFrame ) {
00255     if ( mLeftColumn->findWidget( widget ) == -1 && mRightColumn->findWidget( widget ) == -1 )
00256       return;
00257   } else {
00258     if ( mLeftColumn->findWidget( target ) == -1 && mRightColumn->findWidget( target ) == -1 ||
00259          mLeftColumn->findWidget( widget ) == -1 && mRightColumn->findWidget( widget ) == -1 )
00260       return;
00261   }
00262 
00263   if ( mLeftColumn->findWidget( widget ) != -1 ) {
00264     mLeftColumn->remove( widget );
00265     mLeftColumnSummaries.remove( widgetName( widget ) );
00266   } else if ( mRightColumn->findWidget( widget ) != -1 ) {
00267     mRightColumn->remove( widget );
00268     mRightColumnSummaries.remove( widgetName( widget ) );
00269   }
00270 
00271   if ( target == mFrame ) {
00272     int pos = 0;
00273 
00274     if ( alignment & Qt::AlignTop )
00275       pos = 0;
00276 
00277     if ( alignment & Qt::AlignLeft ) {
00278       if ( alignment & Qt::AlignBottom )
00279         pos = mLeftColumnSummaries.count();
00280 
00281       mLeftColumn->insertWidget( pos, widget );
00282       mLeftColumnSummaries.insert( mLeftColumnSummaries.at( pos ), widgetName( widget ) );
00283     } else {
00284       if ( alignment & Qt::AlignBottom )
00285         pos = mRightColumnSummaries.count();
00286 
00287       mRightColumn->insertWidget( pos, widget );
00288       mRightColumnSummaries.insert( mRightColumnSummaries.at( pos ), widgetName( widget ) );
00289     }
00290 
00291     return;
00292   }
00293 
00294   int targetPos = mLeftColumn->findWidget( target );
00295   if ( targetPos != -1 ) {
00296     if ( alignment == Qt::AlignBottom )
00297       targetPos++;
00298 
00299     mLeftColumn->insertWidget( targetPos, widget );
00300     mLeftColumnSummaries.insert( mLeftColumnSummaries.at( targetPos ), widgetName( widget ) );
00301   } else {
00302     targetPos = mRightColumn->findWidget( target );
00303 
00304     if ( alignment == Qt::AlignBottom )
00305       targetPos++;
00306 
00307     mRightColumn->insertWidget( targetPos, widget );
00308     mRightColumnSummaries.insert( mRightColumnSummaries.at( targetPos ), widgetName( widget ) );
00309   }
00310 }
00311 
00312 void SummaryViewPart::slotTextChanged()
00313 {
00314   emit textChanged( i18n( "What's next?" ) );
00315 }
00316 
00317 void SummaryViewPart::slotAdjustPalette()
00318 {
00319   mMainWidget->setPaletteBackgroundColor( kapp->palette().active().base() );
00320 }
00321 
00322 void SummaryViewPart::setDate( const QDate& newDate )
00323 {
00324   QString date( "<b>%1</b>" );
00325   date = date.arg( KGlobal::locale()->formatDate( newDate ) );
00326   mDateLabel->setText( date );
00327 }
00328 
00329 void SummaryViewPart::slotConfigure()
00330 {
00331   KCMultiDialog dlg( mMainWidget, "ConfigDialog", true );
00332 
00333   QStringList modules = configModules();
00334   modules.prepend( "kcmkontactsummary.desktop" );
00335   connect( &dlg, SIGNAL( configCommitted() ),
00336            this, SLOT( updateWidgets() ) );
00337 
00338   QStringList::ConstIterator strIt;
00339   for ( strIt = modules.begin(); strIt != modules.end(); ++strIt )
00340     dlg.addModule( *strIt );
00341 
00342   dlg.exec();
00343 }
00344 
00345 QStringList SummaryViewPart::configModules() const
00346 {
00347   QStringList modules;
00348 
00349   QMap<QString, Kontact::Summary*>::ConstIterator it;
00350   for ( it = mSummaries.begin(); it != mSummaries.end(); ++it ) {
00351     QStringList cm = it.data()->configModules();
00352     QStringList::ConstIterator strIt;
00353     for ( strIt = cm.begin(); strIt != cm.end(); ++strIt )
00354       if ( !(*strIt).isEmpty() && !modules.contains( *strIt ) )
00355         modules.append( *strIt );
00356   }
00357 
00358   return modules;
00359 }
00360 
00361 void SummaryViewPart::initGUI( Kontact::Core *core )
00362 {
00363   QScrollView *sv = new QScrollView( core );
00364 
00365   sv->setResizePolicy( QScrollView::AutoOneFit );
00366   sv->setFrameStyle( QFrame::NoFrame | QFrame::Plain );
00367   sv->setHScrollBarMode( QScrollView::AlwaysOff );
00368 
00369   mMainWidget = new QFrame( sv->viewport() );
00370   sv->addChild( mMainWidget );
00371   mMainWidget->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00372   sv->setFocusPolicy( QWidget::StrongFocus );
00373   setWidget( sv );
00374 
00375   mMainLayout = new QVBoxLayout( mMainWidget,KDialog::marginHint(),
00376                                  KDialog::spacingHint() );
00377 
00378   QHBoxLayout *hbl = new QHBoxLayout( mMainLayout );
00379   mUsernameLabel = new QLabel( mMainWidget );
00380   hbl->addWidget( mUsernameLabel );
00381   mDateLabel = new QLabel( mMainWidget );
00382   mDateLabel->setAlignment( AlignRight );
00383   hbl->addWidget( mDateLabel );
00384 
00385   QFrame *hline = new QFrame( mMainWidget );
00386   hline->setFrameStyle( QFrame::HLine | QFrame::Plain );
00387   mMainLayout->insertWidget( 1, hline );
00388 
00389   mFrame = new DropWidget( mMainWidget );
00390   mMainLayout->insertWidget( 2, mFrame );
00391 
00392   connect( mFrame, SIGNAL( summaryWidgetDropped( QWidget*, QWidget*, int ) ),
00393            this, SLOT( summaryWidgetMoved( QWidget*, QWidget*, int ) ) );
00394 
00395   updateWidgets();
00396 }
00397 
00398 void SummaryViewPart::loadLayout()
00399 {
00400   KConfig config( "kontact_summaryrc" );
00401 
00402   if ( !config.hasKey( "LeftColumnSummaries" ) ) {
00403     mLeftColumnSummaries << "kontact_korganizerplugin";
00404     mLeftColumnSummaries << "kontact_todoplugin";
00405     mLeftColumnSummaries << "kontact_kaddressbookplugin";
00406     mLeftColumnSummaries << "kontact_specialdatesplugin";
00407   } else {
00408     mLeftColumnSummaries = config.readListEntry( "LeftColumnSummaries" );
00409   }
00410 
00411   if ( !config.hasKey( "RightColumnSummaries" ) ) {
00412     mRightColumnSummaries << "kontact_newstickerplugin";
00413   } else {
00414     mRightColumnSummaries = config.readListEntry( "RightColumnSummaries" );
00415   }
00416 }
00417 
00418 void SummaryViewPart::saveLayout()
00419 {
00420   KConfig config( "kontact_summaryrc" );
00421 
00422   config.writeEntry( "LeftColumnSummaries", mLeftColumnSummaries );
00423   config.writeEntry( "RightColumnSummaries", mRightColumnSummaries );
00424 
00425   config.sync();
00426 }
00427 
00428 QString SummaryViewPart::widgetName( QWidget *widget ) const
00429 {
00430   QMap<QString, Kontact::Summary*>::ConstIterator it;
00431   for ( it = mSummaries.begin(); it != mSummaries.end(); ++it ) {
00432     if ( it.data() == widget )
00433       return it.key();
00434   }
00435 
00436   return QString::null;
00437 }
00438 
00439 #include "summaryview_part.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys