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( "kaddressbook", 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 mLayout->setRowStretch( 6, 1 ); 00074 00075 KABC::StdAddressBook *ab = KABC::StdAddressBook::self(); 00076 connect( ab, SIGNAL( addressBookChanged( AddressBook* ) ), 00077 this, SLOT( updateView() ) ); 00078 00079 mDaysAhead = 62; // ### make configurable 00080 00081 updateView(); 00082 } 00083 00084 void KABSummaryWidget::updateView() 00085 { 00086 mLabels.setAutoDelete( true ); 00087 mLabels.clear(); 00088 mLabels.setAutoDelete( false ); 00089 00090 KABC::StdAddressBook *ab = KABC::StdAddressBook::self(); 00091 QValueList<KABDateEntry> dates; 00092 00093 KABC::AddressBook::Iterator it; 00094 for ( it = ab->begin(); it != ab->end(); ++it ) { 00095 QDate birthday = (*it).birthday().date(); 00096 QDate anniversary = QDate::fromString( 00097 (*it).custom( "KADDRESSBOOK" , "X-Anniversary" ), Qt::ISODate ); 00098 00099 if ( birthday.isValid() ) { 00100 KABDateEntry entry; 00101 entry.birthday = true; 00102 dateDiff( birthday, entry.daysTo, entry.yearsOld ); 00103 00104 entry.date = birthday; 00105 entry.addressee = *it; 00106 if ( entry.daysTo <= mDaysAhead ) 00107 dates.append( entry ); 00108 } 00109 00110 if ( anniversary.isValid() ) { 00111 KABDateEntry entry; 00112 entry.birthday = false; 00113 dateDiff( anniversary, entry.daysTo, entry.yearsOld ); 00114 00115 entry.date = anniversary; 00116 entry.addressee = *it; 00117 if ( entry.daysTo <= mDaysAhead ) 00118 dates.append( entry ); 00119 } 00120 } 00121 00122 qHeapSort( dates ); 00123 00124 if ( !dates.isEmpty() ) { 00125 int counter = 0; 00126 QValueList<KABDateEntry>::Iterator addrIt; 00127 QString lines; 00128 for ( addrIt = dates.begin(); addrIt != dates.end() && counter < 6; ++addrIt ) { 00129 bool makeBold = (*addrIt).daysTo < 5; 00130 00131 QLabel *label = new QLabel( this ); 00132 if ( (*addrIt).birthday ) 00133 label->setPixmap( KGlobal::iconLoader()->loadIcon( "cookie", KIcon::Small ) ); 00134 else 00135 label->setPixmap( KGlobal::iconLoader()->loadIcon( "kdmconfig", KIcon::Small ) ); 00136 mLayout->addWidget( label, counter, 0 ); 00137 mLabels.append( label ); 00138 00139 label = new QLabel( this ); 00140 if ( (*addrIt).daysTo == 0 ) 00141 label->setText( i18n( "Today" ) ); 00142 else 00143 label->setText( i18n( "in 1 day", "in %n days", (*addrIt).daysTo ) ); 00144 mLayout->addWidget( label, counter, 1 ); 00145 mLabels.append( label ); 00146 if ( makeBold ) { 00147 QFont font = label->font(); 00148 font.setBold( true ); 00149 label->setFont( font ); 00150 } 00151 00152 label = new QLabel( KGlobal::locale()->formatDate( (*addrIt).date, true ), this ); 00153 mLayout->addWidget( label, counter, 2 ); 00154 mLabels.append( label ); 00155 00156 KURLLabel *urlLabel = new KURLLabel( this ); 00157 urlLabel->installEventFilter(this); 00158 urlLabel->setURL( (*addrIt).addressee.uid() ); 00159 urlLabel->setText( (*addrIt).addressee.realName() ); 00160 mLayout->addWidget( urlLabel, counter, 3 ); 00161 mLabels.append( urlLabel ); 00162 if ( makeBold ) { 00163 QFont font = label->font(); 00164 font.setBold( true ); 00165 label->setFont( font ); 00166 } 00167 00168 connect( urlLabel, SIGNAL( leftClickedURL( const QString& ) ), 00169 this, SLOT( mailContact( const QString& ) ) ); 00170 connect( urlLabel, SIGNAL( rightClickedURL( const QString& ) ), 00171 this, SLOT( popupMenu( const QString& ) ) ); 00172 00173 label = new QLabel( this ); 00174 label->setText( i18n( "one year", "%n years", (*addrIt).yearsOld ) ); 00175 mLayout->addWidget( label, counter, 4 ); 00176 mLabels.append( label ); 00177 if ( makeBold ) { 00178 QFont font = label->font(); 00179 font.setBold( true ); 00180 label->setFont( font ); 00181 } 00182 00183 counter++; 00184 } 00185 } else { 00186 QLabel *nothingtosee = new QLabel( 00187 i18n( "No birthdays or anniversaries pending within the next 1 day", 00188 "No birthdays or anniversaries pending within the next %n days", 00189 mDaysAhead ), this, "nothing to see" ); 00190 nothingtosee->setAlignment( AlignCenter ); 00191 nothingtosee->setTextFormat( RichText ); 00192 mLayout->addMultiCellWidget( nothingtosee, 0, 0, 0, 4 ); 00193 } 00194 00195 show(); 00196 } 00197 00198 void KABSummaryWidget::mailContact( const QString &uid ) 00199 { 00200 QString app; 00201 if ( kapp->dcopClient()->isApplicationRegistered( "kmail" ) ) 00202 app = QString::fromLatin1( "kmail" ); 00203 else { 00204 mPlugin->core()->selectPlugin( "mails" ); 00205 app = QString::fromLatin1( "kontact" ); 00206 } 00207 00208 KABC::StdAddressBook *ab = KABC::StdAddressBook::self(); 00209 QString email = ab->findByUid( uid ).fullEmail(); 00210 00211 // FIXME: replace "DCOPRef, dcopCall.send..." with kapp->invokeMailer for kde 3.2 00212 // kapp->invokeMailer(addr, QString::null); 00213 DCOPRef dcopCall( app.latin1(), "KMailIface" ); 00214 dcopCall.send( "openComposer(QString,QString,QString,QString,QString,bool)", email, 00215 QString::null, QString::null, QString::null, QString::null, false ); 00216 } 00217 00218 void KABSummaryWidget::viewContact( const QString &uid ) 00219 { 00220 if ( !mPlugin->isRunningStandalone() ) 00221 mPlugin->core()->selectPlugin( mPlugin ); 00222 else 00223 mPlugin->bringToForeground(); 00224 00225 DCOPRef dcopCall( "kaddressbook", "KAddressBookIface" ); 00226 dcopCall.send( "showContactEditor(QString)", uid ); 00227 } 00228 00229 void KABSummaryWidget::popupMenu( const QString &uid ) 00230 { 00231 KPopupMenu popup( this ); 00232 popup.insertItem( KGlobal::iconLoader()->loadIcon( "kmail", KIcon::Small ), 00233 i18n( "Send &Mail" ), 0 ); 00234 popup.insertItem( KGlobal::iconLoader()->loadIcon( "kaddressbook", KIcon::Small ), 00235 i18n( "View &Contact" ), 1 ); 00236 00237 switch ( popup.exec( QCursor::pos() ) ) { 00238 case 0: 00239 mailContact( uid ); 00240 break; 00241 case 1: 00242 viewContact( uid ); 00243 break; 00244 } 00245 } 00246 00247 bool KABSummaryWidget::eventFilter(QObject *obj, QEvent* e) 00248 { 00249 if ( obj->inherits( "KURLLabel" ) ) { 00250 KURLLabel* label = static_cast<KURLLabel*>( obj ); 00251 if ( e->type() == QEvent::Enter ) 00252 emit message( i18n( "Mail to %1" ).arg( label->text() ) ); 00253 if ( e->type() == QEvent::Leave ) 00254 emit message( QString::null ); 00255 } 00256 00257 return Kontact::Summary::eventFilter( obj, e ); 00258 } 00259 00260 void KABSummaryWidget::dateDiff( const QDate &date, int &days, int &years ) 00261 { 00262 QDate currentDate( QDate::currentDate().year(), QDate::currentDate().month(), QDate::currentDate().day() ); 00263 00264 QDate eventDate( QDate::currentDate().year(), date.month(), date.day() ); 00265 00266 int offset = currentDate.daysTo( eventDate ); 00267 if ( offset < 0 ) { 00268 days = 365 + offset; 00269 years = QDate::currentDate().year() + 1 - date.year(); 00270 } else { 00271 days = offset; 00272 years = QDate::currentDate().year() - date.year(); 00273 } 00274 } 00275 00276 #include "kabsummarywidget.moc"
KDE Logo
This file is part of the documentation for kontact Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 28 23:58:15 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003