libkdepim Library API Documentation

addresseeview.cpp

00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <qpopupmenu.h>
00023 
00024 #include <kabc/address.h>
00025 #include <kabc/addressee.h>
00026 #include <kabc/phonenumber.h>
00027 #include <kactionclasses.h>
00028 #include <kapplication.h>
00029 #include <kconfig.h>
00030 #include <kglobal.h>
00031 #include <kglobalsettings.h>
00032 #include <kiconloader.h>
00033 #include <kio/job.h>
00034 #include <klocale.h>
00035 #include <kmessagebox.h>
00036 #include <krun.h>
00037 #include <kstringhandler.h>
00038 
00039 #include "addresseeview.h"
00040 
00041 using namespace KPIM;
00042 
00043 AddresseeView::AddresseeView( QWidget *parent, const char *name,
00044                               KConfig *config )
00045   : KTextBrowser( parent, name ), mDefaultConfig( false ), mImageJob( 0 )
00046 {
00047   setWrapPolicy( QTextEdit::AtWordBoundary );
00048   setLinkUnderline( false );
00049   setVScrollBarMode( QScrollView::AlwaysOff );
00050   setHScrollBarMode( QScrollView::AlwaysOff );
00051 
00052   QStyleSheet *sheet = styleSheet();
00053   QStyleSheetItem *link = sheet->item( "a" );
00054   link->setColor( KGlobalSettings::linkColor() );
00055 
00056   connect( this, SIGNAL( mailClick( const QString&, const QString& ) ),
00057            this, SLOT( slotMailClicked( const QString&, const QString& ) ) );
00058   connect( this, SIGNAL( urlClick( const QString& ) ),
00059            this, SLOT( slotUrlClicked( const QString& ) ) );
00060   connect( this, SIGNAL( highlighted( const QString& ) ),
00061            this, SLOT( slotHighlighted( const QString& ) ) );
00062 
00063   setNotifyClick( true );
00064 
00065   mActionShowBirthday = new KToggleAction( i18n( "Show Birthday" ) );
00066   mActionShowAddresses = new KToggleAction( i18n( "Show Postal Addresses" ) );
00067   mActionShowEmails = new KToggleAction( i18n( "Show Email Addresses" ) );
00068   mActionShowPhones = new KToggleAction( i18n( "Show Telephone Numbers" ) );
00069   mActionShowURLs = new KToggleAction( i18n( "Show Web Pages (URLs)" ) );
00070 
00071   connect( mActionShowBirthday, SIGNAL( toggled( bool ) ), SLOT( configChanged() ) );
00072   connect( mActionShowAddresses, SIGNAL( toggled( bool ) ), SLOT( configChanged() ) );
00073   connect( mActionShowEmails, SIGNAL( toggled( bool ) ), SLOT( configChanged() ) );
00074   connect( mActionShowPhones, SIGNAL( toggled( bool ) ), SLOT( configChanged() ) );
00075   connect( mActionShowURLs, SIGNAL( toggled( bool ) ), SLOT( configChanged() ) );
00076 
00077   if ( !config ) {
00078     mConfig = new KConfig( "kaddressbookrc" );
00079     mDefaultConfig = true;
00080   } else
00081     mConfig = config;
00082 
00083   load();
00084 }
00085 
00086 AddresseeView::~AddresseeView()
00087 {
00088   if ( mDefaultConfig )
00089     delete mConfig;
00090 
00091   mConfig = 0;
00092 }
00093 
00094 void AddresseeView::setAddressee( const KABC::Addressee& addr )
00095 {
00096   mAddressee = addr;
00097 
00098   if ( mImageJob ) {
00099     mImageJob->kill();
00100     mImageJob = 0;
00101   }
00102 
00103   mImageData.truncate( 0 );
00104 
00105   updateView();
00106 }
00107 
00108 void AddresseeView::updateView()
00109 {
00110   // clear view
00111   setText( QString::null );
00112 
00113   if ( mAddressee.isEmpty() ) {
00114     QMimeSourceFactory::defaultFactory()->setImage( "myimage", QByteArray() );
00115     return;
00116   }
00117 
00118   if ( mImageJob ) {
00119     mImageJob->kill();
00120     mImageJob = 0;
00121 
00122     mImageData.truncate( 0 );
00123   }
00124 
00125   QString name = ( mAddressee.formattedName().isEmpty() ?
00126                    mAddressee.assembledName() : mAddressee.formattedName() );
00127 
00128   QString dynamicPart;
00129 
00130   if ( mActionShowBirthday->isChecked() ) {
00131     QDate date = mAddressee.birthday().date();
00132 
00133     dynamicPart += QString(
00134       "<tr><td align=\"right\"><b>%1</b></td>"
00135       "<td align=\"left\">%2</td></tr>" )
00136       .arg( KABC::Addressee::birthdayLabel() )
00137       .arg( date.isValid() ? KGlobal::locale()->formatDate( date, true ) : i18n( "none" ) );
00138   }
00139 
00140   if ( mActionShowPhones->isChecked() ) {
00141     KABC::PhoneNumber::List phones = mAddressee.phoneNumbers();
00142     KABC::PhoneNumber::List::ConstIterator phoneIt;
00143     for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) {
00144       QString number = (*phoneIt).number();
00145 
00146       QString url;
00147       if ( (*phoneIt).type() & KABC::PhoneNumber::Fax )
00148         url = "fax:" + number;
00149       else
00150         url = "phone:" + number;
00151 
00152       dynamicPart += QString(
00153         "<tr><td align=\"right\"><b>%1</b></td>"
00154         "<td align=\"left\"><a href=\"%2\">%3</a></td></tr>" )
00155         .arg( KABC::PhoneNumber::typeLabel( (*phoneIt).type() ).replace( " ", "&nbsp;" ) )
00156         .arg( url )
00157         .arg( number );
00158     }
00159   }
00160 
00161   if ( mActionShowEmails->isChecked() ) {
00162     QStringList emails = mAddressee.emails();
00163     QStringList::ConstIterator emailIt;
00164     QString type = i18n( "Email" );
00165     for ( emailIt = emails.begin(); emailIt != emails.end(); ++emailIt ) {
00166       dynamicPart += QString(
00167         "<tr><td align=\"right\"><b>%1</b></td>"
00168         "<td align=\"left\"><a href=\"mailto:%2\">%3</a></td></tr>" )
00169         .arg( type )
00170         .arg( *emailIt )
00171         .arg( *emailIt );
00172       type = i18n( "Other" );
00173     }
00174   }
00175 
00176   if ( mActionShowURLs->isChecked() ) {
00177     if ( !mAddressee.url().url().isEmpty() ) {
00178       dynamicPart += QString(
00179         "<tr><td align=\"right\"><b>%1</b></td>"
00180         "<td align=\"left\">%2</td></tr>" )
00181         .arg( i18n( "Homepage" ) )
00182         .arg( KStringHandler::tagURLs( mAddressee.url().url() ) );
00183     }
00184   }
00185 
00186   if ( mActionShowAddresses->isChecked() ) {
00187     KABC::Address::List addresses = mAddressee.addresses();
00188     KABC::Address::List::ConstIterator addrIt;
00189     for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
00190       if ( (*addrIt).label().isEmpty() ) {
00191         QString formattedAddress;
00192 
00193 #if KDE_VERSION >= 319
00194         formattedAddress = (*addrIt).formattedAddress().stripWhiteSpace();
00195 #else
00196         if ( !(*addrIt).street().isEmpty() )
00197           formattedAddress += (*addrIt).street() + "\n";
00198 
00199         if ( !(*addrIt).postOfficeBox().isEmpty() )
00200           formattedAddress += (*addrIt).postOfficeBox() + "\n";
00201 
00202         formattedAddress += (*addrIt).locality() + QString(" ") + (*addrIt).region();
00203 
00204         if ( !(*addrIt).postalCode().isEmpty() )
00205           formattedAddress += QString(", ") + (*addrIt).postalCode();
00206 
00207         formattedAddress += "\n";
00208 
00209         if ( !(*addrIt).country().isEmpty() )
00210           formattedAddress += (*addrIt).country() + "\n";
00211 
00212         formattedAddress += (*addrIt).extended();
00213 #endif
00214 
00215         formattedAddress = formattedAddress.replace( '\n', "<br>" );
00216 
00217         dynamicPart += QString(
00218           "<tr><td align=\"right\"><b>%1</b></td>"
00219           "<td align=\"left\">%2</td></tr>" )
00220           .arg( KABC::Address::typeLabel( (*addrIt).type() ) )
00221           .arg( formattedAddress );
00222       } else {
00223         dynamicPart += QString(
00224           "<tr><td align=\"right\"><b>%1</b></td>"
00225           "<td align=\"left\">%2</td></tr>" )
00226           .arg( KABC::Address::typeLabel( (*addrIt).type() ) )
00227           .arg( (*addrIt).label().replace( '\n', "<br>" ) );
00228       }
00229     }
00230   }
00231 
00232   QString notes;
00233   if ( !mAddressee.note().isEmpty() ) {
00234     notes = QString(
00235       "<tr>"
00236       "<td align=\"right\" valign=\"top\"><b>%1:</b></td>"  // note label
00237       "<td align=\"left\" valign=\"top\">%2</td>"  // note
00238       "</tr>" ).arg( i18n( "Notes" ) ).arg( mAddressee.note().replace( '\n', "<br>" ) );
00239   }
00240 
00241   QString strAddr = QString::fromLatin1(
00242   "<html>"
00243   "<body text=\"%1\" bgcolor=\"%2\">" // text and background color
00244   "<table>"
00245   "<tr>"
00246   "<td rowspan=\"3\" align=\"right\" valign=\"top\">"
00247   "<img src=\"myimage\" width=\"50\" height=\"70\">"
00248   "</td>"
00249   "<td align=\"left\"><font size=\"+2\"><b>%3</b></font></td>"  // name
00250   "</tr>"
00251   "<tr>"
00252   "<td align=\"left\">%4</td>"  // role
00253   "</tr>"
00254   "<tr>"
00255   "<td align=\"left\">%5</td>"  // organization
00256   "</tr>"
00257   "<tr><td colspan=\"2\">&nbsp;</td></tr>"
00258   "%6"  // dynamic part
00259   "%7"  // notes
00260   "</table>"
00261   "</body>"
00262   "</html>").arg( KGlobalSettings::textColor().name() )
00263   .arg( KGlobalSettings::baseColor().name() )
00264   .arg( name )
00265   .arg( mAddressee.role() )
00266   .arg( mAddressee.organization() )
00267   .arg( dynamicPart )
00268   .arg( notes );
00269 
00270   KABC::Picture picture = mAddressee.photo();
00271   if ( picture.isIntern() && !picture.data().isNull() )
00272     QMimeSourceFactory::defaultFactory()->setImage( "myimage", picture.data() );
00273   else {
00274     if ( !picture.url().isEmpty() ) {
00275       if ( mImageData.count() > 0 )
00276         QMimeSourceFactory::defaultFactory()->setImage( "myimage", mImageData );
00277       else {
00278         mImageJob = KIO::get( KURL( picture.url() ), false, false );
00279         connect( mImageJob, SIGNAL( data( KIO::Job*, const QByteArray& ) ),
00280                  this, SLOT( data( KIO::Job*, const QByteArray& ) ) );
00281         connect( mImageJob, SIGNAL( result( KIO::Job* ) ),
00282                  this, SLOT( result( KIO::Job* ) ) );
00283       }
00284     } else {
00285       QMimeSourceFactory::defaultFactory()->setPixmap( "myimage",
00286         KGlobal::iconLoader()->loadIcon( "identity", KIcon::Desktop, 128 ) );
00287     }
00288   }
00289 
00290   // at last display it...
00291   setText( strAddr );
00292 }
00293 
00294 KABC::Addressee AddresseeView::addressee() const
00295 {
00296   return mAddressee;
00297 }
00298 
00299 void AddresseeView::urlClicked( const QString &url )
00300 {
00301   kapp->invokeBrowser( url );
00302 }
00303 
00304 void AddresseeView::emailClicked( const QString &email )
00305 {
00306   kapp->invokeMailer( email, QString::null );
00307 }
00308 
00309 void AddresseeView::phoneNumberClicked( const QString &number )
00310 {
00311   KConfig config( "kaddressbookrc" );
00312   config.setGroup( "General" );
00313   QString commandLine = config.readEntry( "PhoneHookApplication" );
00314 
00315   if ( commandLine.isEmpty() ) {
00316     KMessageBox::sorry( this, i18n( "There is no application set which could be executed. Please go to the settings dialog and configure one." ) );
00317     return;
00318   }
00319 
00320   commandLine.replace( "%N", number );
00321   KRun::runCommand( commandLine );
00322 }
00323 
00324 void AddresseeView::faxNumberClicked( const QString &number )
00325 {
00326   KConfig config( "kaddressbookrc" );
00327   config.setGroup( "General" );
00328   QString commandLine = config.readEntry( "FaxHookApplication", "kdeprintfax --phone %N" );
00329 
00330   if ( commandLine.isEmpty() ) {
00331     KMessageBox::sorry( this, i18n( "There is no application set which could be executed. Please go to the settings dialog and configure one." ) );
00332     return;
00333   }
00334 
00335   commandLine.replace( "%N", number );
00336   KRun::runCommand( commandLine );
00337 }
00338 
00339 QPopupMenu *AddresseeView::createPopupMenu( const QPoint& )
00340 {
00341   QPopupMenu *menu = new QPopupMenu( this );
00342   mActionShowBirthday->plug( menu );
00343   mActionShowAddresses->plug( menu );
00344   mActionShowEmails->plug( menu );
00345   mActionShowPhones->plug( menu );
00346   mActionShowURLs->plug( menu );
00347 
00348   return menu;
00349 }
00350 
00351 void AddresseeView::slotMailClicked( const QString&, const QString &email )
00352 {
00353   emailClicked( email );
00354 }
00355 
00356 void AddresseeView::slotUrlClicked( const QString &url )
00357 {
00358   if ( url.startsWith( "phone:" ) )
00359     phoneNumberClicked( strippedNumber( url.mid( 8 ) ) );
00360   else if ( url.startsWith( "fax:" ) )
00361     faxNumberClicked( strippedNumber( url.mid( 6 ) ) );
00362   else
00363     urlClicked( url );
00364 }
00365 
00366 void AddresseeView::slotHighlighted( const QString &link )
00367 {
00368   if ( link.startsWith( "mailto:" ) ) {
00369     QString email = link.mid( 7 );
00370 
00371     emit emailHighlighted( email );
00372     emit highlightedMessage( i18n( "Send mail to <%1>" ).arg( email ) );
00373   } else if ( link.startsWith( "phone:" ) ) {
00374     QString number = link.mid( 8 );
00375 
00376     emit phoneNumberHighlighted( strippedNumber( number ) );
00377     emit highlightedMessage( i18n( "Call number %1" ).arg( number ) );
00378   } else if ( link.startsWith( "fax:" ) ) {
00379     QString number = link.mid( 6 );
00380 
00381     emit faxNumberHighlighted( strippedNumber( number ) );
00382     emit highlightedMessage( i18n( "Send fax to %1" ).arg( number ) );
00383   } else if ( link.startsWith( "http:" ) ) {
00384     emit urlHighlighted( link );
00385     emit highlightedMessage( i18n( "Open URL %1" ).arg( link ) );
00386   } else
00387     emit highlightedMessage( "" );
00388 }
00389 
00390 void AddresseeView::configChanged()
00391 {
00392   save();
00393   updateView();
00394 }
00395 
00396 void AddresseeView::data( KIO::Job*, const QByteArray &d )
00397 {
00398   unsigned int oldSize = mImageData.size();
00399   mImageData.resize( oldSize + d.size() );
00400   memcpy( mImageData.data() + oldSize, d.data(), d.size() );
00401 }
00402 
00403 void AddresseeView::result( KIO::Job *job )
00404 {
00405   mImageJob = 0;
00406 
00407   if ( job->error() )
00408     mImageData.truncate( 0 );
00409 
00410   updateView();
00411 }
00412 
00413 void AddresseeView::load()
00414 {
00415   mConfig->setGroup( "AddresseeViewSettings" );
00416   mActionShowBirthday->setChecked( mConfig->readBoolEntry( "ShowBirthday", false ) );
00417   mActionShowAddresses->setChecked( mConfig->readBoolEntry( "ShowAddresses", true ) );
00418   mActionShowEmails->setChecked( mConfig->readBoolEntry( "ShowEmails", true ) );
00419   mActionShowPhones->setChecked( mConfig->readBoolEntry( "ShowPhones", true ) );
00420   mActionShowURLs->setChecked( mConfig->readBoolEntry( "ShowURLs", true ) );
00421 }
00422 
00423 void AddresseeView::save()
00424 {
00425   mConfig->setGroup( "AddresseeViewSettings" );
00426   mConfig->writeEntry( "ShowBirthday", mActionShowBirthday->isChecked() );
00427   mConfig->writeEntry( "ShowAddresses", mActionShowAddresses->isChecked() );
00428   mConfig->writeEntry( "ShowEmails", mActionShowEmails->isChecked() );
00429   mConfig->writeEntry( "ShowPhones", mActionShowPhones->isChecked() );
00430   mConfig->writeEntry( "ShowURLs", mActionShowURLs->isChecked() );
00431   mConfig->sync();
00432 }
00433 
00434 QString AddresseeView::strippedNumber( const QString &number )
00435 {
00436   QString retval;
00437 
00438   for ( uint i = 0; i < number.length(); ++i )
00439     if ( number[ i ].isDigit() )
00440       retval.append( number[ i ] );
00441 
00442   return retval;
00443 }
00444 
00445 #include "addresseeview.moc"
KDE Logo
This file is part of the documentation for libkdepim Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:31 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003