kontact Library API Documentation

kabsummarywidget.cpp

00001 /*
00002     This file is part of Kontact.
00003     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qcursor.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 
00028 #include <dcopclient.h>
00029 #include <dcopref.h>
00030 #include <kabc/stdaddressbook.h>
00031 #include <kapplication.h>
00032 #include <kdialog.h>
00033 #include <kglobal.h>
00034 #include <kiconloader.h>
00035 #include <klocale.h>
00036 #include <kparts/part.h>
00037 #include <kpopupmenu.h>
00038 #include <kurllabel.h>
00039 
00040 #include "core.h"
00041 #include "plugin.h"
00042 
00043 #include "kabsummarywidget.h"
00044 
00045 class KABDateEntry
00046 {
00047   public:
00048     bool birthday;
00049     int yearsOld;
00050     int daysTo;
00051     QDate date;
00052     KABC::Addressee addressee;
00053 
00054     bool operator<( const KABDateEntry &entry ) const
00055     {
00056       return daysTo < entry.daysTo;
00057     }
00058 };
00059 
00060 KABSummaryWidget::KABSummaryWidget( Kontact::Plugin *plugin, QWidget *parent,
00061                                     const char *name )
00062   : Kontact::Summary( parent, name ), mPlugin( plugin )
00063 {
00064   QVBoxLayout *mainLayout = new QVBoxLayout( this, 3, 3 );
00065 
00066   QPixmap icon = KGlobal::iconLoader()->loadIcon( "kontact_contacts",
00067                     KIcon::Desktop, KIcon::SizeMedium );
00068 
00069   QWidget *header = createHeader( this, icon, i18n( "Birthdays and Anniversaries" ) );
00070   mainLayout->addWidget(header);
00071 
00072   mLayout = new QGridLayout( mainLayout, 7, 5, 3 );
00073 
00074   KABC::StdAddressBook *ab = KABC::StdAddressBook::self();
00075   connect( ab, SIGNAL( addressBookChanged( AddressBook* ) ),
00076            this, SLOT( updateView() ) );
00077 
00078   connect( mPlugin->core(), SIGNAL( dayChanged( const QDate& ) ),
00079            this, SLOT( updateView() ) );
00080 
00081   configUpdated();
00082 }
00083 
00084 void KABSummaryWidget::configUpdated()
00085 {
00086   KConfig config( "kcmkabsummaryrc" );
00087 
00088   config.setGroup( "Days" );
00089   mDaysAhead = config.readNumEntry( "DaysToShow", 7 );
00090 
00091   config.setGroup( "EventTypes" );
00092   mShowBirthdays = config.readBoolEntry( "ShowBirthdays", true );
00093   mShowAnniversaries = config.readBoolEntry( "ShowAnniversaries", true );
00094 
00095   updateView();
00096 }
00097 
00098 void KABSummaryWidget::updateView()
00099 {
00100   mLabels.setAutoDelete( true );
00101   mLabels.clear();
00102   mLabels.setAutoDelete( false );
00103 
00104   KABC::StdAddressBook *ab = KABC::StdAddressBook::self();
00105   QValueList<KABDateEntry> dates;
00106   QLabel *label = 0;
00107 
00108   KABC::AddressBook::Iterator it;
00109   for ( it = ab->begin(); it != ab->end(); ++it ) {
00110     QDate birthday = (*it).birthday().date();
00111     if ( birthday.isValid() && mShowBirthdays ) {
00112       KABDateEntry entry;
00113       entry.birthday = true;
00114       dateDiff( birthday, entry.daysTo, entry.yearsOld );
00115 
00116       entry.date = birthday;
00117       entry.addressee = *it;
00118       if ( entry.daysTo <= mDaysAhead )
00119         dates.append( entry );
00120     }
00121 
00122     QString anniversaryAsString = (*it).custom( "KADDRESSBOOK" , "X-Anniversary" );
00123     if ( !anniversaryAsString.isEmpty() ) {
00124       QDate anniversary = QDate::fromString( anniversaryAsString , Qt::ISODate );
00125       if ( anniversary.isValid() && mShowAnniversaries ) {
00126          KABDateEntry entry;
00127          entry.birthday = false;
00128          dateDiff( anniversary, entry.daysTo, entry.yearsOld );
00129 
00130          entry.date = anniversary;
00131          entry.addressee = *it;
00132          if ( entry.daysTo <= mDaysAhead )
00133            dates.append( entry );
00134        }
00135     }
00136   }
00137 
00138   qHeapSort( dates );
00139 
00140   if ( !dates.isEmpty() ) {
00141     int counter = 0;
00142     QValueList<KABDateEntry>::Iterator addrIt;
00143     QString lines;
00144     for ( addrIt = dates.begin(); addrIt != dates.end(); ++addrIt ) {
00145       bool makeBold = (*addrIt).daysTo < 5;
00146 
00147       label = new QLabel( this );
00148       if ( (*addrIt).birthday )
00149         label->setPixmap( KGlobal::iconLoader()->loadIcon( "cookie", KIcon::Small ) );
00150       else
00151         label->setPixmap( KGlobal::iconLoader()->loadIcon( "kdmconfig", KIcon::Small ) );
00152       mLayout->addWidget( label, counter, 0 );
00153       mLabels.append( label );
00154 
00155       label = new QLabel( this );
00156       if ( (*addrIt).daysTo == 0 )
00157         label->setText( i18n( "Today" ) );
00158       else
00159         label->setText( i18n( "in 1 day", "in %n days", (*addrIt).daysTo ) );
00160       mLayout->addWidget( label, counter, 1 );
00161       mLabels.append( label );
00162       if ( makeBold ) {
00163         QFont font = label->font();
00164         font.setBold( true );
00165         label->setFont( font );
00166       }
00167 
00168       label = new QLabel( KGlobal::locale()->formatDate( (*addrIt).date, true ), this );
00169       mLayout->addWidget( label, counter, 2 );
00170       mLabels.append( label );
00171 
00172       KURLLabel *urlLabel = new KURLLabel( this );
00173       urlLabel->installEventFilter(this);
00174       urlLabel->setURL( (*addrIt).addressee.uid() );
00175       urlLabel->setText( (*addrIt).addressee.realName() );
00176       mLayout->addWidget( urlLabel, counter, 3 );
00177       mLabels.append( urlLabel );
00178       if ( makeBold ) {
00179         QFont font = label->font();
00180         font.setBold( true );
00181         label->setFont( font );
00182       }
00183 
00184       connect( urlLabel, SIGNAL( leftClickedURL( const QString& ) ),
00185           this, SLOT( mailContact( const QString& ) ) );
00186       connect( urlLabel, SIGNAL( rightClickedURL( const QString& ) ),
00187           this, SLOT( popupMenu( const QString& ) ) );
00188 
00189       label = new QLabel( this );
00190       label->setText( i18n( "one year", "%n years", (*addrIt).yearsOld  ) );
00191       mLayout->addWidget( label, counter, 4 );
00192       mLabels.append( label );
00193       if ( makeBold ) {
00194         QFont font = label->font();
00195         font.setBold( true );
00196         label->setFont( font );
00197       }
00198 
00199       counter++;
00200     }
00201   } else {
00202     label = new QLabel(
00203         i18n( "No birthdays or anniversaries pending within the next 1 day",
00204               "No birthdays or anniversaries pending within the next %n days",
00205               mDaysAhead ), this, "nothing to see" );
00206     label->setAlignment( AlignCenter );
00207     label->setTextFormat( RichText );
00208     mLayout->addMultiCellWidget( label, 0, 0, 0, 4 );
00209   }
00210 
00211   for ( label = mLabels.first(); label; label = mLabels.next() )
00212     label->show();
00213 }
00214 
00215 void KABSummaryWidget::mailContact( const QString &uid )
00216 {
00217   QString app;
00218   if ( kapp->dcopClient()->isApplicationRegistered( "kmail" ) )
00219     app = QString::fromLatin1( "kmail" );
00220   else {
00221     mPlugin->core()->selectPlugin( "mails" );
00222     app = QString::fromLatin1( "kontact" );
00223   }
00224 
00225   KABC::StdAddressBook *ab = KABC::StdAddressBook::self();
00226   QString email = ab->findByUid( uid ).fullEmail();
00227 
00228   // FIXME: replace "DCOPRef, dcopCall.send..." with kapp->invokeMailer for kde 3.2
00229   // kapp->invokeMailer(addr, QString::null);
00230   DCOPRef dcopCall( app.latin1(), "KMailIface" );
00231   dcopCall.send( "openComposer(QString,QString,QString,QString,QString,bool)", email,
00232                  QString::null, QString::null, QString::null, QString::null, false );
00233 }
00234 
00235 void KABSummaryWidget::viewContact( const QString &uid )
00236 {
00237   if ( !mPlugin->isRunningStandalone() )
00238     mPlugin->core()->selectPlugin( mPlugin );
00239   else
00240     mPlugin->bringToForeground();
00241 
00242   DCOPRef dcopCall( "kaddressbook", "KAddressBookIface" );
00243   dcopCall.send( "showContactEditor(QString)", uid );
00244 }
00245 
00246 void KABSummaryWidget::popupMenu( const QString &uid )
00247 {
00248   KPopupMenu popup( this );
00249   popup.insertItem( KGlobal::iconLoader()->loadIcon( "kontact_mail", KIcon::Small ),
00250                     i18n( "Send &Mail" ), 0 );
00251   popup.insertItem( KGlobal::iconLoader()->loadIcon( "kontact_contact", KIcon::Small ),
00252                     i18n( "View &Contact" ), 1 );
00253 
00254   switch ( popup.exec( QCursor::pos() ) ) {
00255     case 0:
00256       mailContact( uid );
00257       break;
00258     case 1:
00259       viewContact( uid );
00260       break;
00261   }
00262 }
00263 
00264 bool KABSummaryWidget::eventFilter( QObject *obj, QEvent* e )
00265 {
00266   if ( obj->inherits( "KURLLabel" ) ) {
00267     KURLLabel* label = static_cast<KURLLabel*>( obj );
00268     if ( e->type() == QEvent::Enter )
00269       emit message( i18n( "Mail to %1" ).arg( label->text() ) );
00270     if ( e->type() == QEvent::Leave )
00271       emit message( QString::null );
00272   }
00273 
00274   return Kontact::Summary::eventFilter( obj, e );
00275 }
00276 
00277 void KABSummaryWidget::dateDiff( const QDate &date, int &days, int &years )
00278 {
00279   QDate currentDate;
00280   QDate eventDate;
00281 
00282   if ( QDate::leapYear( date.year() ) && date.month() == 2 && date.day() == 29 ) {
00283     currentDate = QDate( date.year(), QDate::currentDate().month(), QDate::currentDate().day() );
00284     if ( !QDate::leapYear( QDate::currentDate().year() ) )
00285       eventDate = QDate( date.year(), date.month(), 28 ); // celebrate one day earlier ;)
00286     else
00287       eventDate = QDate( date.year(), date.month(), date.day() );
00288   } else {
00289     currentDate = QDate( 0, QDate::currentDate().month(), QDate::currentDate().day() );
00290     eventDate = QDate( 0, date.month(), date.day() );
00291   }
00292 
00293   int offset = currentDate.daysTo( eventDate );
00294   if ( offset < 0 ) {
00295     days = 365 + offset;
00296     years = QDate::currentDate().year() + 1 - date.year();
00297   } else {
00298     days = offset;
00299     years = QDate::currentDate().year() - date.year();
00300   }
00301 }
00302 
00303 QStringList KABSummaryWidget::configModules() const
00304 {
00305   return QStringList( "kcmkabsummary.desktop" );
00306 }
00307 
00308 #include "kabsummarywidget.moc"
KDE Logo
This file is part of the documentation for kontact Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Aug 2 09:56:36 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003