kaddressbook Library API Documentation

mergewidget.cpp

00001 /*
00002     This file is part of KAddressBook.                                  
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 <qlayout.h>
00025 #include <qpushbutton.h>
00026 
00027 #include <kaccelmanager.h>
00028 #include <kdebug.h>
00029 #include <klistview.h>
00030 #include <klocale.h>
00031 #include <kmessagebox.h>
00032 
00033 #include <kabc/addressbook.h>
00034 
00035 #include "core.h"
00036 
00037 #include "mergewidget.h"
00038 
00039 class MergeFactory : public KAB::ExtensionFactory
00040 {
00041   public:
00042     KAB::ExtensionWidget *extension( KAB::Core *core, QWidget *parent, const char *name )
00043     {
00044       return new MergeWidget( core, parent, name );
00045     }
00046 
00047     QString identifier() const
00048     {
00049       return "merge";
00050     }
00051 };
00052 
00053 extern "C" {
00054   void *init_libkaddrbk_merge()
00055   {
00056     return ( new MergeFactory );
00057   }
00058 }
00059 
00060 class ContactItem : public QListViewItem
00061 {
00062   public:
00063     ContactItem( KListView *parent, const KABC::Addressee &addressee )
00064       : QListViewItem( parent ), mAddressee( addressee )
00065     {
00066       KABC::Field::List fieldList = KABC::Field::defaultFields();
00067       KABC::Field::List::ConstIterator it;
00068 
00069       int i = 0;
00070       for ( it = fieldList.begin(); it != fieldList.end(); ++it )
00071         setText( i++, (*it)->value( mAddressee ) );
00072     }
00073 
00074     KABC::Addressee addressee() const
00075     {
00076       return mAddressee;
00077     }
00078 
00079   private:
00080     KABC::Addressee mAddressee;
00081 };
00082 
00083 MergeWidget::MergeWidget( KAB::Core *core, QWidget *parent, const char *name )
00084   : KAB::ExtensionWidget( core, parent, name ), mBlockUpdate( false )
00085 {
00086   QGridLayout *topLayout = new QGridLayout( this, 3, 2, KDialog::marginHint(),
00087                                             KDialog::spacingHint() );
00088 
00089   mContactView = new KListView( this );
00090   KABC::Field::List fieldList = KABC::Field::defaultFields();
00091   KABC::Field::List::ConstIterator it;
00092 
00093   for ( it = fieldList.begin(); it != fieldList.end(); ++it )
00094     mContactView->addColumn( (*it)->label() );
00095 
00096   mContactView->setEnabled( false );
00097   mContactView->setAllColumnsShowFocus( true );
00098   topLayout->addMultiCellWidget( mContactView, 0, 2, 0, 0 );
00099 
00100   connect( mContactView, SIGNAL( selectionChanged() ),
00101            SLOT( selectionContactViewChanged() ) );
00102 
00103   mMergeAndRemoveButton = new QPushButton( i18n( "Merge && Remove" ), this );
00104   mMergeAndRemoveButton->setEnabled( false );
00105   topLayout->addWidget( mMergeAndRemoveButton, 0, 1 );
00106   connect( mMergeAndRemoveButton, SIGNAL( clicked() ), SLOT( mergeAndRemove() ) );
00107 
00108   mMergeButton = new QPushButton( i18n( "Merge" ), this );
00109   mMergeButton->setEnabled( false );
00110   topLayout->addWidget( mMergeButton, 1, 1 );
00111   connect( mMergeButton, SIGNAL( clicked() ), SLOT( merge() ) );
00112 
00113   KAcceleratorManager::manage( this );
00114 }
00115 
00116 MergeWidget::~MergeWidget()
00117 {
00118 }
00119 
00120 void MergeWidget::selectionContactViewChanged()
00121 {
00122   ContactItem *contactItem =
00123                   dynamic_cast<ContactItem*>( mContactView->selectedItem() );
00124   bool state = (contactItem != 0);
00125 
00126   mMergeAndRemoveButton->setEnabled( state );
00127   mMergeButton->setEnabled( state );
00128 }
00129 
00130 void MergeWidget::contactsSelectionChanged()
00131 {
00132   if ( mBlockUpdate )
00133     return;
00134 
00135   if ( !contactsSelected() ) {
00136     mContactView->setEnabled( false );
00137     mContactView->clear();
00138     mMergeAndRemoveButton->setEnabled( false );
00139     mMergeButton->setEnabled( false );
00140   } else {
00141     KABC::Addressee::List list = selectedContacts();
00142     if ( list.count() > 1 ) {
00143       mContactView->setEnabled( false );
00144       mContactView->clear();
00145       mMergeAndRemoveButton->setEnabled( false );
00146       mMergeButton->setEnabled( false );
00147       return;
00148     } else {
00149       mContactView->setEnabled( true );
00150       mMasterAddressee = list[ 0 ];
00151       updateView();
00152     }
00153   }
00154 }
00155 
00156 void MergeWidget::updateView()
00157 {
00158   mContactView->clear();
00159 
00160   KABC::AddressBook::Iterator it;
00161   KABC::AddressBook *ab = core()->addressBook();
00162   if ( !ab )
00163     return;
00164 
00165   for ( it = ab->begin(); it != ab->end(); ++it )
00166     if ( (*it).uid() != mMasterAddressee.uid() )
00167       new ContactItem( mContactView, *it );
00168 }
00169 
00170 QString MergeWidget::title() const
00171 {
00172   return i18n( "Merge Contacts Editor" );
00173 }
00174 
00175 QString MergeWidget::identifier() const
00176 {
00177   return "merge";
00178 }
00179 
00180 void MergeWidget::mergeAndRemove()
00181 {
00182   ContactItem *item = dynamic_cast<ContactItem*>( mContactView->currentItem() );
00183   if ( !item )
00184     return;
00185 
00186   QString oldUID = item->addressee().uid();
00187 
00188   doMerge( item->addressee() );
00189 
00190   KABC::Addressee::List retval;
00191   retval << mMasterAddressee;
00192   emit modified( retval );
00193 
00194   mBlockUpdate = true;
00195   core()->deleteContacts( oldUID );
00196   core()->setContactSelected( mMasterAddressee.uid() );
00197   mBlockUpdate = false;
00198 
00199   updateView();
00200 }
00201 
00202 void MergeWidget::merge()
00203 {
00204   ContactItem *item = dynamic_cast<ContactItem*>( mContactView->currentItem() );
00205   if ( !item )
00206     return;
00207 
00208   doMerge( item->addressee() );
00209 
00210   KABC::Addressee::List retval;
00211   retval << mMasterAddressee;
00212   emit modified( retval );
00213 
00214   mBlockUpdate = true;
00215   core()->setContactSelected( mMasterAddressee.uid() );
00216   mBlockUpdate = false;
00217 
00218   updateView();
00219 }
00220 
00221 void MergeWidget::doMerge( const KABC::Addressee &addr )
00222 {
00223   // ADR + LABEL
00224   KABC::Address::List addresses = addr.addresses();
00225   KABC::Address::List masterAddresses = mMasterAddressee.addresses();
00226   KABC::Address::List::Iterator addrIt ;
00227   for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
00228     if ( !masterAddresses.contains( *addrIt ) )
00229       mMasterAddressee.insertAddress( *addrIt );
00230   }
00231 
00232   if ( mMasterAddressee.birthday().isNull() && !addr.birthday().isNull() )
00233     mMasterAddressee.setBirthday( addr.birthday() );
00234 
00235 
00236   // CATEGORIES
00237   QStringList::Iterator it;
00238   QStringList categories = addr.categories();
00239   QStringList masterCategories = mMasterAddressee.categories();
00240   QStringList newCategories( masterCategories );
00241   for ( it = categories.begin(); it != categories.end(); ++it )
00242     if ( !masterCategories.contains( *it ) )
00243       newCategories.append( *it );
00244   mMasterAddressee.setCategories( newCategories );
00245 
00246   // CLASS
00247   if ( !mMasterAddressee.secrecy().isValid() && addr.secrecy().isValid() )
00248     mMasterAddressee.setSecrecy( addr.secrecy() );
00249 
00250   // EMAIL
00251   QStringList emails = addr.emails();
00252   QStringList masterEmails = mMasterAddressee.emails();
00253   for ( it = emails.begin(); it != emails.end(); ++it )
00254     if ( !masterEmails.contains( *it ) )
00255       mMasterAddressee.insertEmail( *it, false );
00256 
00257   // FN
00258   if ( mMasterAddressee.formattedName().isEmpty() && !addr.formattedName().isEmpty() )
00259     mMasterAddressee.setFormattedName( addr.formattedName() );
00260 
00261   // GEO
00262   if ( !mMasterAddressee.geo().isValid() && addr.geo().isValid() )
00263     mMasterAddressee.setGeo( addr.geo() );
00264 
00265 /*
00266   // KEY
00267   // LOGO
00268 */
00269 
00270   // MAILER
00271   if ( mMasterAddressee.mailer().isEmpty() && !addr.mailer().isEmpty() )
00272     mMasterAddressee.setMailer( addr.mailer() );
00273 
00274   // N
00275   if ( mMasterAddressee.assembledName().isEmpty() && !addr.assembledName().isEmpty() )
00276     mMasterAddressee.setNameFromString( addr.assembledName() );
00277 
00278   // NICKNAME
00279   if ( mMasterAddressee.nickName().isEmpty() && !addr.nickName().isEmpty() )
00280     mMasterAddressee.setNickName( addr.nickName() );
00281 
00282   // NOTE
00283   if ( mMasterAddressee.note().isEmpty() && !addr.note().isEmpty() )
00284     mMasterAddressee.setNote( addr.note() );
00285 
00286   // ORG
00287   if ( mMasterAddressee.organization().isEmpty() && !addr.organization().isEmpty() )
00288     mMasterAddressee.setOrganization( addr.organization() );
00289 
00290 /*
00291   // PHOTO
00292 */
00293 
00294   // PROID
00295   if ( mMasterAddressee.productId().isEmpty() && !addr.productId().isEmpty() )
00296     mMasterAddressee.setProductId( addr.productId() );
00297 
00298   // REV
00299   if ( mMasterAddressee.revision().isNull() && !addr.revision().isNull() )
00300     mMasterAddressee.setRevision( addr.revision() );
00301 
00302   // ROLE
00303   if ( mMasterAddressee.role().isEmpty() && !addr.role().isEmpty() )
00304     mMasterAddressee.setRole( addr.role() );
00305 
00306   // SORT-STRING
00307   if ( mMasterAddressee.sortString().isEmpty() && !addr.sortString().isEmpty() )
00308     mMasterAddressee.setSortString( addr.sortString() );
00309 
00310 /*
00311   // SOUND
00312 */
00313 
00314   // TEL
00315   KABC::PhoneNumber::List phones = addr.phoneNumbers();
00316   KABC::PhoneNumber::List masterPhones = mMasterAddressee.phoneNumbers();
00317   KABC::PhoneNumber::List::ConstIterator phoneIt;
00318   for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt )
00319     if ( !masterPhones.contains( *it ) )
00320       mMasterAddressee.insertPhoneNumber( *it );
00321 
00322   // TITLE
00323   if ( mMasterAddressee.title().isEmpty() && !addr.title().isEmpty() )
00324     mMasterAddressee.setTitle( addr.title() );
00325 
00326   // TZ
00327   if ( !mMasterAddressee.timeZone().isValid() && addr.timeZone().isValid() )
00328     mMasterAddressee.setTimeZone( addr.timeZone() );
00329 
00330   // UID // ignore UID
00331 
00332   // URL
00333   if ( mMasterAddressee.url().isEmpty() && !addr.url().isEmpty() )
00334     mMasterAddressee.setUrl( addr.url() );
00335 
00336   // X-
00337   QStringList customs = addr.customs();
00338   QStringList masterCustoms = mMasterAddressee.customs();
00339   QStringList newCustoms( masterCustoms );
00340   for ( it = customs.begin(); it != customs.end(); ++it )
00341     if ( !masterCustoms.contains( *it ) )
00342       newCustoms.append( *it );
00343   mMasterAddressee.setCustoms( newCustoms );
00344 }
00345 
00346 #include "mergewidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:52 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003