kaddressbook Library API Documentation

addresseeeditorwidget.cpp

00001 /*
00002     This file is part of KAddressBook.                                  
00003     Copyright (c) 2002 Mike Pilone <mpilone@slac.com>                   
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 <qcheckbox.h>
00025 #include <qhbox.h>
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 #include <qlistbox.h>
00029 #include <qpushbutton.h>
00030 #include <qtabwidget.h>
00031 #include <qtextedit.h>
00032 #include <qtoolbutton.h>
00033 #include <qtooltip.h>
00034 
00035 #include <kabc/resource.h>
00036 #include <kaccelmanager.h>
00037 #include <kapplication.h>
00038 #include <kconfig.h>
00039 #include <kcombobox.h>
00040 #include <kdebug.h>
00041 #include <kdialogbase.h>
00042 #include <kglobal.h>
00043 #include <kiconloader.h>
00044 #include <klineedit.h>
00045 #include <klocale.h>
00046 #include <kmessagebox.h>
00047 #include <kseparator.h>
00048 #include <ksqueezedtextlabel.h>
00049 #include <kstandarddirs.h>
00050 
00051 #include <libkdepim/addresseelineedit.h>
00052 #include <libkdepim/categoryeditdialog.h>
00053 #include <libkdepim/categoryselectdialog.h>
00054 #include <libkdepim/kdateedit.h>
00055 
00056 #include "addresseditwidget.h"
00057 #include "advancedcustomfields.h"
00058 #include "core.h"
00059 #include "emaileditwidget.h"
00060 #include "kabprefs.h"
00061 #include "keywidget.h"
00062 #include "nameeditdialog.h"
00063 #include "phoneeditwidget.h"
00064 #include "secrecywidget.h"
00065 
00066 #include "addresseeeditorwidget.h"
00067 
00068 AddresseeEditorWidget::AddresseeEditorWidget( KAB::Core *core, bool isExtension, 
00069                                               QWidget *parent, const char *name )
00070   : AddresseeEditorBase( core, isExtension, parent, name ),
00071     mBlockSignals( false ), mReadOnly( false )
00072 {
00073   kdDebug(5720) << "AddresseeEditorWidget()" << endl;
00074 
00075   initGUI();
00076   mCategoryDialog = 0;
00077   mCategoryEditDialog = 0;
00078 
00079   // Load the empty addressee as defaults
00080   load();
00081 
00082   mDirty = false;
00083 }
00084 
00085 AddresseeEditorWidget::~AddresseeEditorWidget()
00086 {
00087   kdDebug(5720) << "~AddresseeEditorWidget()" << endl;
00088 }  
00089 
00090 void AddresseeEditorWidget::setAddressee( const KABC::Addressee &addr )
00091 {
00092   if ( mAddressee.uid() == addr.uid() ) 
00093       return;
00094 
00095   mAddressee = addr;
00096 
00097   bool readOnly = ( !addr.resource() ? false : addr.resource()->readOnly() );
00098   setReadOnly( readOnly );
00099 
00100   load();
00101 }
00102 
00103 const KABC::Addressee &AddresseeEditorWidget::addressee()
00104 {
00105   return mAddressee;
00106 }
00107 
00108 void AddresseeEditorWidget::textChanged( const QString& )
00109 {
00110   emitModified();
00111 }
00112 
00113 void AddresseeEditorWidget::initGUI()
00114 {
00115   QVBoxLayout *layout = new QVBoxLayout( this );
00116 
00117   mTabWidget = new QTabWidget( this );
00118   layout->addWidget( mTabWidget );
00119 
00120   setupTab1();
00121   setupTab2();
00122   setupAdditionalTabs();
00123   setupCustomFieldsTabs();
00124 
00125   connect( mTabWidget, SIGNAL( currentChanged(QWidget*) ),
00126            SLOT( pageChanged(QWidget*) ) );
00127 }
00128 
00129 void AddresseeEditorWidget::setupTab1()
00130 {
00131   // This is the General tab
00132   QWidget *tab1 = new QWidget( mTabWidget );
00133 
00134   QGridLayout *layout = new QGridLayout( tab1, 11, 7 );
00135   layout->setMargin( KDialogBase::marginHint() );
00136   layout->setSpacing( KDialogBase::spacingHint() );
00137   
00138   QLabel *label;
00139   KSeparator* bar;
00140   QPushButton *button;
00141   
00143   // Upper left group (person info)
00144   
00145   // Person icon
00146   label = new QLabel( tab1 );
00147   label->setPixmap( KGlobal::iconLoader()->loadIcon( "personal", KIcon::Desktop,
00148                                                       KIcon::SizeMedium ) );
00149   layout->addMultiCellWidget( label, 0, 1, 0, 0 );
00150   
00151   // First name
00152   button = new QPushButton( i18n( "Edit Name..." ), tab1 );
00153   QToolTip::add( button, i18n( "Edit the contact's name" ) );
00154   mNameEdit = new KLineEdit( tab1, "mNameEdit" );
00155   connect( mNameEdit, SIGNAL( textChanged( const QString& ) ), 
00156            SLOT( nameTextChanged( const QString& ) ) );
00157   connect( button, SIGNAL( clicked() ), SLOT( nameButtonClicked() ) );
00158   mNameLabel = new KSqueezedTextLabel( tab1 );
00159 
00160   if ( KABPrefs::instance()->mAutomaticNameParsing ) {
00161     mNameLabel->hide();
00162     mNameEdit->show();
00163   } else {
00164     mNameEdit->hide();
00165     mNameLabel->show();
00166   }
00167 
00168   layout->addWidget( button, 0, 1 );
00169   layout->addWidget( mNameEdit, 0, 2 );
00170   layout->addWidget( mNameLabel, 0, 2 );
00171   label = new QLabel( i18n( "%1:" ).arg( KABC::Addressee::roleLabel() ), tab1 );
00172   mRoleEdit = new KLineEdit( tab1 );
00173   connect( mRoleEdit, SIGNAL( textChanged( const QString& ) ),
00174            SLOT( textChanged( const QString& ) ) );
00175   label->setBuddy( mRoleEdit );
00176   layout->addWidget( label, 1, 1 );
00177   layout->addWidget( mRoleEdit, 1, 2 );
00178   
00179   // Organization
00180   label = new QLabel( i18n( "%1:" ).arg( KABC::Addressee::organizationLabel() ), tab1 );
00181   mOrgEdit = new KLineEdit( tab1 );
00182   label->setBuddy( mOrgEdit );
00183   connect( mOrgEdit, SIGNAL( textChanged( const QString& ) ), 
00184            SLOT( organizationTextChanged( const QString& ) ) );
00185   layout->addWidget( label, 2, 1 );
00186   layout->addWidget( mOrgEdit, 2, 2 );
00187   
00188   // File as (formatted name)
00189   label = new QLabel( i18n( "Formatted name:" ), tab1 );
00190   mFormattedNameLabel = new KSqueezedTextLabel( tab1 );
00191   layout->addWidget( label, 3, 1 );
00192   layout->addWidget( mFormattedNameLabel, 3, 2 );
00193   
00194   // Left hand separator. This separator doesn't go all the way
00195   // across so the dialog still flows from top to bottom
00196   bar = new KSeparator( KSeparator::HLine, tab1 );
00197   layout->addMultiCellWidget( bar, 4, 4, 0, 2 );
00198   
00200   // Phone numbers (upper right)
00201   label = new QLabel( tab1 );
00202   label->setPixmap( KGlobal::iconLoader()->loadIcon( "kaddressbook",
00203                     KIcon::Desktop, KIcon::SizeMedium ) );
00204   layout->addMultiCellWidget( label, 0, 1, 3, 3 );
00205   
00206   mPhoneEditWidget = new PhoneEditWidget( tab1 );
00207   connect( mPhoneEditWidget, SIGNAL( modified() ), SLOT( emitModified() ) );
00208   layout->addMultiCellWidget( mPhoneEditWidget, 0, 3, 4, 6 ); 
00209 
00210   bar = new KSeparator( KSeparator::HLine, tab1 );
00211   layout->addMultiCellWidget( bar, 4, 4, 3, 6 );
00212   
00214   // Addresses (lower left)
00215   label = new QLabel( tab1 );
00216   label->setPixmap( KGlobal::iconLoader()->loadIcon( "gohome", KIcon::Desktop,
00217                                                      KIcon::SizeMedium ) );
00218   layout->addMultiCellWidget( label, 5, 6, 0, 0 );
00219   
00220   mAddressEditWidget = new AddressEditWidget( tab1 );
00221   connect( mAddressEditWidget, SIGNAL( modified() ), SLOT( emitModified() ) );
00222   layout->addMultiCellWidget( mAddressEditWidget, 5, 9, 1, 2 );
00223 
00225   // Email / Web (lower right)
00226   label = new QLabel( tab1 );
00227   label->setPixmap( KGlobal::iconLoader()->loadIcon( "email", KIcon::Desktop,
00228                                                      KIcon::SizeMedium ) );
00229   layout->addMultiCellWidget( label, 5, 6, 3, 3 );
00230 
00231   mEmailWidget = new EmailEditWidget( tab1 );
00232   connect( mEmailWidget, SIGNAL( modified() ), SLOT( emitModified() ) );
00233   layout->addMultiCellWidget( mEmailWidget, 5, 6, 4, 6 );
00234 
00235   // add the separator
00236   bar = new KSeparator( KSeparator::HLine, tab1 );
00237   layout->addMultiCellWidget( bar, 7, 7, 3, 6 );
00238 
00239   label = new QLabel( tab1 );
00240   label->setPixmap( KGlobal::iconLoader()->loadIcon( "homepage", KIcon::Desktop,
00241                                                      KIcon::SizeMedium ) );
00242   layout->addMultiCellWidget( label, 8, 9, 3, 3 );
00243 
00244   label = new QLabel( i18n( "%1:" ).arg( KABC::Addressee::urlLabel() ), tab1 );
00245   mURLEdit = new KLineEdit( tab1 );
00246   connect( mURLEdit, SIGNAL( textChanged( const QString& ) ), 
00247            SLOT( textChanged( const QString& ) ) );
00248   label->setBuddy( mURLEdit );
00249   layout->addWidget( label, 8, 4 );
00250   layout->addMultiCellWidget( mURLEdit, 8, 8, 5, 6 );
00251 
00252   label = new QLabel( i18n( "&IM address:" ), tab1 );
00253   mIMAddressEdit = new KLineEdit( tab1 );
00254   connect( mIMAddressEdit, SIGNAL( textChanged( const QString& ) ), 
00255            SLOT( textChanged( const QString& ) ) );
00256   label->setBuddy( mIMAddressEdit );
00257   layout->addWidget( label, 9, 4 );
00258   layout->addMultiCellWidget( mIMAddressEdit, 9, 9, 5, 6 );
00259   
00260   layout->addColSpacing( 6, 50 );
00261   
00262   bar = new KSeparator( KSeparator::HLine, tab1 );
00263   layout->addMultiCellWidget( bar, 10, 10, 0, 6 );
00264   
00266   QHBox *categoryBox = new QHBox( tab1 );
00267   categoryBox->setSpacing( KDialogBase::spacingHint() );
00268   
00269   // Categories
00270   mCategoryButton = new QPushButton( i18n( "Select Categories..." ), categoryBox );
00271   connect( mCategoryButton, SIGNAL( clicked() ), SLOT( categoryButtonClicked() ) );
00272 
00273   mCategoryEdit = new KLineEdit( categoryBox );
00274   mCategoryEdit->setReadOnly( true );
00275   connect( mCategoryEdit, SIGNAL( textChanged( const QString& ) ), 
00276            SLOT( textChanged( const QString& ) ) );
00277 
00278   mSecrecyWidget = new SecrecyWidget( categoryBox );
00279   connect( mSecrecyWidget, SIGNAL( changed() ), SLOT( emitModified() ) );
00280 
00281   layout->addMultiCellWidget( categoryBox, 11, 11, 0, 6 );
00282   
00283   // Build the layout and add to the tab widget
00284   layout->activate(); // required
00285 
00286   mTabWidget->addTab( tab1, i18n( "&General" ) );
00287 }
00288 
00289 void AddresseeEditorWidget::setupTab2()
00290 {
00291   // This is the Details tab
00292   QWidget *tab2 = new QWidget( mTabWidget );
00293 
00294   QGridLayout *layout = new QGridLayout( tab2, 6, 6 );
00295   layout->setMargin( KDialogBase::marginHint() );
00296   layout->setSpacing( KDialogBase::spacingHint() );
00297   
00298   QLabel *label;
00299   KSeparator* bar;
00300   
00302   // Office info
00303   
00304   // Department
00305   label = new QLabel( tab2 );
00306   label->setPixmap( KGlobal::iconLoader()->loadIcon( "folder", KIcon::Desktop,
00307                                                      KIcon::SizeMedium ) );
00308   layout->addMultiCellWidget( label, 0, 1, 0, 0 );
00309   
00310   label = new QLabel( i18n( "Department:" ), tab2 );
00311   layout->addWidget( label, 0, 1 );
00312   mDepartmentEdit = new KLineEdit( tab2 );
00313   connect( mDepartmentEdit, SIGNAL( textChanged( const QString& ) ),
00314            SLOT( textChanged( const QString& ) ) );
00315   label->setBuddy( mDepartmentEdit );
00316   layout->addWidget( mDepartmentEdit, 0, 2 );
00317   
00318   label = new QLabel( i18n( "Office:" ), tab2 );
00319   layout->addWidget( label, 1, 1 );
00320   mOfficeEdit = new KLineEdit( tab2 );
00321   connect( mOfficeEdit, SIGNAL( textChanged( const QString& ) ),
00322            SLOT( textChanged( const QString& ) ) );
00323   label->setBuddy( mOfficeEdit );
00324   layout->addWidget( mOfficeEdit, 1, 2 );
00325   
00326   label = new QLabel( i18n( "Profession:" ), tab2 );
00327   layout->addWidget( label, 2, 1 );
00328   mProfessionEdit = new KLineEdit( tab2 );
00329   connect( mProfessionEdit, SIGNAL( textChanged( const QString& ) ),
00330            SLOT( textChanged( const QString& ) ) );
00331   label->setBuddy( mProfessionEdit );
00332   layout->addWidget( mProfessionEdit, 2, 2 );
00333   
00334   label = new QLabel( i18n( "Manager\'s name:" ), tab2 );
00335   layout->addWidget( label, 0, 3 );
00336   mManagerEdit = new KPIM::AddresseeLineEdit( tab2 );
00337   connect( mManagerEdit, SIGNAL( textChanged( const QString& ) ),
00338            SLOT( textChanged( const QString& ) ) );
00339   label->setBuddy( mManagerEdit );
00340   layout->addMultiCellWidget( mManagerEdit, 0, 0, 4, 5 );
00341   
00342   label = new QLabel( i18n( "Assistant's name:" ), tab2 );
00343   layout->addWidget( label, 1, 3 );
00344   mAssistantEdit = new KPIM::AddresseeLineEdit( tab2 );
00345   connect( mAssistantEdit, SIGNAL( textChanged( const QString& ) ),
00346            SLOT( textChanged( const QString& ) ) );
00347   label->setBuddy( mAssistantEdit );
00348   layout->addMultiCellWidget( mAssistantEdit, 1, 1, 4, 5 );
00349   
00350   bar = new KSeparator( KSeparator::HLine, tab2 );
00351   layout->addMultiCellWidget( bar, 3, 3, 0, 5 );
00352   
00354   // Personal info
00355   
00356   label = new QLabel( tab2 );
00357   label->setPixmap( KGlobal::iconLoader()->loadIcon( "personal", KIcon::Desktop,
00358                                                      KIcon::SizeMedium ) );
00359   layout->addMultiCellWidget( label, 4, 5, 0, 0 );
00360   
00361   label = new QLabel( i18n( "Nickname:" ), tab2 );
00362   layout->addWidget( label, 4, 1 );
00363   mNicknameEdit = new KLineEdit( tab2 );
00364   connect( mNicknameEdit, SIGNAL( textChanged( const QString& ) ),
00365            SLOT( textChanged( const QString& ) ) );
00366   label->setBuddy( mNicknameEdit );
00367   layout->addWidget( mNicknameEdit, 4, 2 );
00368 
00369   label = new QLabel( i18n( "Spouse's name:" ), tab2 );
00370   layout->addWidget( label, 5, 1 );
00371   mSpouseEdit = new KPIM::AddresseeLineEdit( tab2 );
00372   connect( mSpouseEdit, SIGNAL( textChanged( const QString& ) ),
00373            SLOT( textChanged( const QString& ) ) );
00374   label->setBuddy( mSpouseEdit );
00375   layout->addWidget( mSpouseEdit, 5, 2 );
00376 
00377   label = new QLabel( i18n( "Birthdate:" ), tab2 );
00378   layout->addWidget( label, 4, 3 );
00379   mBirthdayPicker = new KDateEdit( tab2 );
00380   mBirthdayPicker->setHandleInvalid( true );
00381   connect( mBirthdayPicker, SIGNAL( dateChanged( QDate ) ),
00382            SLOT( dateChanged( QDate ) ) );
00383   connect( mBirthdayPicker, SIGNAL( invalidDateEntered() ),
00384            SLOT( invalidDate() ) );
00385   connect( mBirthdayPicker, SIGNAL( textChanged( const QString& ) ),
00386            SLOT( emitModified() ) );
00387   label->setBuddy( mBirthdayPicker );
00388   layout->addWidget( mBirthdayPicker, 4, 4 );
00389   
00390   label = new QLabel( i18n( "Anniversary:" ), tab2 );
00391   layout->addWidget( label, 5, 3 );
00392   mAnniversaryPicker = new KDateEdit( tab2 );
00393   mAnniversaryPicker->setHandleInvalid( true );
00394   connect( mAnniversaryPicker, SIGNAL( dateChanged( QDate ) ),
00395            SLOT( dateChanged( QDate ) ) );
00396   connect( mAnniversaryPicker, SIGNAL( invalidDateEntered() ),
00397            SLOT( invalidDate() ) );
00398   connect( mAnniversaryPicker, SIGNAL( textChanged( const QString& ) ),
00399            SLOT( emitModified() ) );
00400   label->setBuddy( mAnniversaryPicker );
00401   layout->addWidget( mAnniversaryPicker, 5, 4 );
00402   
00403   bar = new KSeparator( KSeparator::HLine, tab2 );
00404   layout->addMultiCellWidget( bar, 6, 6, 0, 5 );
00405   
00407   // Notes
00408   label = new QLabel( i18n( "Note:" ), tab2 );
00409   label->setAlignment( Qt::AlignTop | Qt::AlignLeft );
00410   layout->addWidget( label, 7, 0 );
00411   mNoteEdit = new QTextEdit( tab2 );
00412   mNoteEdit->setWordWrap( QTextEdit::WidgetWidth );
00413   mNoteEdit->setMinimumSize( mNoteEdit->sizeHint() );
00414   connect( mNoteEdit, SIGNAL( textChanged() ), SLOT( emitModified() ) );
00415   label->setBuddy( mNoteEdit );
00416   layout->addMultiCellWidget( mNoteEdit, 7, 7, 1, 5 );
00417 
00418    // Build the layout and add to the tab widget
00419   layout->activate(); // required
00420 
00421   mTabWidget->addTab( tab2, i18n( "&Details" ) );
00422 }
00423 
00424 void AddresseeEditorWidget::setupAdditionalTabs()
00425 {
00426   ContactEditorWidgetManager *manager = ContactEditorWidgetManager::self();
00427 
00428   // create all tab pages and add the widgets
00429   for ( int i = 0; i < manager->count(); ++i ) {
00430     QString pageIdentifier = manager->factory( i )->pageIdentifier();
00431     QString pageTitle = manager->factory( i )->pageTitle();
00432 
00433     if ( pageIdentifier == "misc" )
00434       pageTitle = i18n( "Misc" );
00435 
00436     ContactEditorTabPage *page = mTabPages[ pageIdentifier ];
00437     if ( page == 0 ) { // tab not yet available, create one
00438       page = new ContactEditorTabPage( mTabWidget );
00439       mTabPages.insert( pageIdentifier, page );
00440 
00441       mTabWidget->addTab( page, pageTitle );
00442 
00443       connect( page, SIGNAL( changed() ), SLOT( emitModified() ) );
00444     }
00445 
00446     KAB::ContactEditorWidget *widget
00447               = manager->factory( i )->createWidget( core()->addressBook(),
00448                                                      page );
00449     if ( widget )
00450       page->addWidget( widget );
00451   }
00452 
00453   // query the layout update
00454   QDictIterator<ContactEditorTabPage> it( mTabPages );
00455   for ( ; it.current(); ++it )
00456     it.current()->updateLayout();
00457 }
00458 
00459 void AddresseeEditorWidget::setupCustomFieldsTabs()
00460 {
00461   QStringList activePages = KABPrefs::instance()->mAdvancedCustomFields;
00462 
00463   QStringList list = KGlobal::dirs()->findAllResources( "data", "kaddressbook/contacteditorpages/*.ui", true, true );
00464   for ( QStringList::iterator it = list.begin(); it != list.end(); ++it ) {
00465     if ( activePages.find( (*it).mid( (*it).findRev('/') + 1 ) ) == activePages.end() )
00466       continue;
00467 
00468     ContactEditorTabPage *page = new ContactEditorTabPage( mTabWidget );
00469     AdvancedCustomFields *wdg = new AdvancedCustomFields( *it, core()->addressBook(), page );
00470     if ( wdg ) {
00471       mTabPages.insert( wdg->pageIdentifier(), page );
00472       mTabWidget->addTab( page, wdg->pageTitle() );
00473 
00474       page->addWidget( wdg );
00475       page->updateLayout();
00476 
00477       connect( page, SIGNAL( changed() ), SLOT( emitModified() ) );
00478     } else
00479       delete page;
00480   }
00481 }
00482 
00483 void AddresseeEditorWidget::load()
00484 {
00485   kdDebug(5720) << "AddresseeEditorWidget::load()" << endl;
00486 
00487   // Block signals in case anything tries to emit modified
00488   // CS: This doesn't seem to work.
00489   bool block = signalsBlocked();
00490   blockSignals( true ); 
00491   mBlockSignals = true; // used for internal signal blocking
00492 
00493   mNameEdit->blockSignals( true );
00494   mNameEdit->setText( mAddressee.assembledName() );
00495   mNameEdit->blockSignals( false );
00496 
00497   if ( mAddressee.formattedName().isEmpty() ) {
00498     KConfig config( "kaddressbookrc" );
00499     config.setGroup( "General" );
00500     mFormattedNameType = config.readNumEntry( "FormattedNameType", 1 );
00501     mAddressee.setFormattedName( NameEditDialog::formattedName( mAddressee, mFormattedNameType ) );
00502   } else {
00503     if ( mAddressee.formattedName() == NameEditDialog::formattedName( mAddressee, NameEditDialog::SimpleName ) )
00504       mFormattedNameType = NameEditDialog::SimpleName;
00505     else if ( mAddressee.formattedName() == NameEditDialog::formattedName( mAddressee, NameEditDialog::FullName ) )
00506       mFormattedNameType = NameEditDialog::FullName;
00507     else if ( mAddressee.formattedName() == NameEditDialog::formattedName( mAddressee, NameEditDialog::ReverseNameWithComma ) )
00508       mFormattedNameType = NameEditDialog::ReverseNameWithComma;
00509     else if ( mAddressee.formattedName() == NameEditDialog::formattedName( mAddressee, NameEditDialog::ReverseName ) )
00510       mFormattedNameType = NameEditDialog::ReverseName;
00511     else if ( mAddressee.formattedName() == NameEditDialog::formattedName( mAddressee, NameEditDialog::Organization ) )
00512       mFormattedNameType = NameEditDialog::Organization;
00513     else
00514       mFormattedNameType = NameEditDialog::CustomName;
00515   }
00516 
00517   mFormattedNameLabel->setText( mAddressee.formattedName() );
00518 
00519   mRoleEdit->setText( mAddressee.role() );
00520   mOrgEdit->setText( mAddressee.organization() );
00521   mURLEdit->setURL( mAddressee.url() );
00522   mURLEdit->home( false );
00523   mNoteEdit->setText( mAddressee.note() );
00524   mEmailWidget->setEmails( mAddressee.emails() );
00525   mPhoneEditWidget->setPhoneNumbers( mAddressee.phoneNumbers() );
00526   mAddressEditWidget->setAddresses( mAddressee, mAddressee.addresses() );
00527   mBirthdayPicker->setDate( mAddressee.birthday().date() );
00528   mAnniversaryPicker->setDate( QDate::fromString( mAddressee.custom(
00529                                "KADDRESSBOOK", "X-Anniversary" ), Qt::ISODate) );
00530   mNicknameEdit->setText( mAddressee.nickName() );
00531   mCategoryEdit->setText( mAddressee.categories().join( "," ) );
00532 
00533   mSecrecyWidget->setSecrecy( mAddressee.secrecy() );
00534 
00535   // Load customs
00536   mIMAddressEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-IMAddress" ) );
00537   mSpouseEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-SpousesName" ) );
00538   mManagerEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-ManagersName" ) );
00539   mAssistantEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-AssistantsName" ) );
00540   mDepartmentEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-Department" ) );
00541   mOfficeEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-Office" ) );
00542   mProfessionEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-Profession" ) );
00543 
00544   QDictIterator<ContactEditorTabPage> it( mTabPages );
00545   for ( ; it.current(); ++it )
00546     it.current()->loadContact( &mAddressee );
00547 
00548   blockSignals( block );
00549   mBlockSignals = false;
00550 
00551   mDirty = false;
00552 }
00553 
00554 void AddresseeEditorWidget::save()
00555 {
00556   if ( !mDirty ) return;
00557 
00558   mAddressee.setRole( mRoleEdit->text() );
00559   mAddressee.setOrganization( mOrgEdit->text() );
00560   mAddressee.setUrl( KURL( mURLEdit->text().stripWhiteSpace() ) );
00561   mAddressee.setNote( mNoteEdit->text() );
00562   if ( mBirthdayPicker->inputIsValid() )
00563     mAddressee.setBirthday( QDateTime( mBirthdayPicker->date() ) );
00564   else
00565     mAddressee.setBirthday( QDateTime() );
00566   
00567   mAddressee.setNickName( mNicknameEdit->text() );
00568   mAddressee.setCategories( QStringList::split( ",", mCategoryEdit->text() ) );
00569 
00570   mAddressee.setSecrecy( mSecrecyWidget->secrecy() );
00571 
00572   // save custom fields
00573   mAddressee.insertCustom( "KADDRESSBOOK", "X-IMAddress", mIMAddressEdit->text() );
00574   mAddressee.insertCustom( "KADDRESSBOOK", "X-SpousesName", mSpouseEdit->text() );
00575   mAddressee.insertCustom( "KADDRESSBOOK", "X-ManagersName", mManagerEdit->text() );
00576   mAddressee.insertCustom( "KADDRESSBOOK", "X-AssistantsName", mAssistantEdit->text() );
00577   mAddressee.insertCustom( "KADDRESSBOOK", "X-Department", mDepartmentEdit->text() );
00578   mAddressee.insertCustom( "KADDRESSBOOK", "X-Office", mOfficeEdit->text() );
00579   mAddressee.insertCustom( "KADDRESSBOOK", "X-Profession", mProfessionEdit->text() );
00580   if ( mAnniversaryPicker->inputIsValid() )
00581     mAddressee.insertCustom( "KADDRESSBOOK", "X-Anniversary",
00582                              mAnniversaryPicker->date().toString( Qt::ISODate ) );
00583   else
00584     mAddressee.removeCustom( "KADDRESSBOOK", "X-Anniversary" );
00585 
00586   // Save the email addresses
00587   mAddressee.setEmails( mEmailWidget->emails() );
00588 
00589   // Save the phone numbers
00590   KABC::PhoneNumber::List phoneNumbers;
00591   KABC::PhoneNumber::List::Iterator phoneIter;
00592   phoneNumbers = mAddressee.phoneNumbers();
00593   for ( phoneIter = phoneNumbers.begin(); phoneIter != phoneNumbers.end();
00594         ++phoneIter )
00595     mAddressee.removePhoneNumber( *phoneIter );
00596 
00597   phoneNumbers = mPhoneEditWidget->phoneNumbers();
00598   for ( phoneIter = phoneNumbers.begin(); phoneIter != phoneNumbers.end();
00599         ++phoneIter )
00600     mAddressee.insertPhoneNumber( *phoneIter );
00601 
00602   // Save the addresses
00603   KABC::Address::List addresses;
00604   KABC::Address::List::Iterator addressIter;
00605   addresses = mAddressee.addresses();
00606   for ( addressIter = addresses.begin(); addressIter != addresses.end();
00607         ++addressIter )
00608     mAddressee.removeAddress( *addressIter );
00609     
00610   addresses = mAddressEditWidget->addresses();
00611   for ( addressIter = addresses.begin(); addressIter != addresses.end();
00612         ++addressIter )
00613     mAddressee.insertAddress( *addressIter );
00614 
00615   QDictIterator<ContactEditorTabPage> it( mTabPages );
00616   for ( ; it.current(); ++it )
00617     it.current()->storeContact( &mAddressee );
00618 
00619   mDirty = false;
00620 }
00621 
00622 bool AddresseeEditorWidget::dirty()
00623 {
00624   return mDirty;
00625 }
00626 
00627 void AddresseeEditorWidget::nameTextChanged( const QString &text )
00628 {
00629   // use the addressee class to parse the name for us
00630   AddresseeConfig config( mAddressee );
00631   if ( config.automaticNameParsing() ) {
00632     if ( !mAddressee.formattedName().isEmpty() ) {
00633       QString fn = mAddressee.formattedName();
00634       mAddressee.setNameFromString( text );
00635       mAddressee.setFormattedName( fn );
00636     } else {
00637       // use extra addressee to avoid a formatted name assignment
00638       Addressee addr;
00639       addr.setNameFromString( text );
00640       mAddressee.setPrefix( addr.prefix() );
00641       mAddressee.setGivenName( addr.givenName() );
00642       mAddressee.setAdditionalName( addr.additionalName() );
00643       mAddressee.setFamilyName( addr.familyName() );
00644       mAddressee.setSuffix( addr.suffix() );
00645     }
00646   }
00647 
00648   nameBoxChanged();
00649 
00650   emitModified();
00651 }
00652 
00653 void AddresseeEditorWidget::organizationTextChanged( const QString &text )
00654 {
00655 
00656   AddresseeConfig config( mAddressee );
00657   if ( config.automaticNameParsing() )
00658     mAddressee.setOrganization( text );
00659 
00660   nameBoxChanged();
00661 
00662   emitModified();
00663 }
00664 
00665 void AddresseeEditorWidget::nameBoxChanged()
00666 {
00667   KABC::Addressee addr;
00668   AddresseeConfig config( mAddressee );
00669   if ( config.automaticNameParsing() ) {
00670     addr.setNameFromString( mNameEdit->text() );
00671     mNameLabel->hide();
00672     mNameEdit->show();
00673   } else {
00674     addr = mAddressee;
00675     mNameEdit->hide();
00676     mNameLabel->setText( mNameEdit->text() );
00677     mNameLabel->show();
00678   }
00679 
00680   if ( mFormattedNameType != NameEditDialog::CustomName ) {
00681     mFormattedNameLabel->setText( NameEditDialog::formattedName( mAddressee, mFormattedNameType ) );
00682     mAddressee.setFormattedName( NameEditDialog::formattedName( mAddressee, mFormattedNameType ) );
00683   }
00684 }
00685 
00686 void AddresseeEditorWidget::nameButtonClicked()
00687 {
00688   // show the name dialog.
00689   NameEditDialog dialog( mAddressee, mFormattedNameType, mReadOnly, this );
00690   
00691   if ( dialog.exec() ) {
00692     if ( dialog.changed() ) {
00693       mAddressee.setFamilyName( dialog.familyName() );
00694       mAddressee.setGivenName( dialog.givenName() );
00695       mAddressee.setPrefix( dialog.prefix() );
00696       mAddressee.setSuffix( dialog.suffix() );
00697       mAddressee.setAdditionalName( dialog.additionalName() );
00698       mFormattedNameType = dialog.formattedNameType();
00699       if ( mFormattedNameType == NameEditDialog::CustomName ) {
00700         mFormattedNameLabel->setText( dialog.customFormattedName() );
00701         mAddressee.setFormattedName( dialog.customFormattedName() );
00702       }
00703       // Update the name edit.
00704       bool block = mNameEdit->signalsBlocked();
00705       mNameEdit->blockSignals( true );
00706       mNameEdit->setText( mAddressee.assembledName() );
00707       mNameEdit->blockSignals( block );
00708 
00709       // Update the combo box.
00710       nameBoxChanged();
00711     
00712       emitModified();
00713     }
00714   }
00715 }
00716 
00717 void AddresseeEditorWidget::categoryButtonClicked()
00718 {
00719   // Show the category dialog
00720   if ( mCategoryDialog == 0 ) {
00721     mCategoryDialog = new KPIM::CategorySelectDialog( KABPrefs::instance(), this );
00722     connect( mCategoryDialog, SIGNAL( categoriesSelected( const QStringList& ) ),
00723              SLOT(categoriesSelected( const QStringList& ) ) );
00724     connect( mCategoryDialog, SIGNAL( editCategories() ), SLOT( editCategories() ) );
00725   }
00726 
00727   QStringList customCategories = QStringList::split( ",", mCategoryEdit->text() );
00728 
00729   mCategoryDialog->setCategories( customCategories );
00730   mCategoryDialog->setSelected( customCategories );
00731   mCategoryDialog->show();
00732   mCategoryDialog->raise();
00733 }
00734 
00735 void AddresseeEditorWidget::categoriesSelected( const QStringList &list )
00736 {
00737   mCategoryEdit->setText( list.join( "," ) );
00738 }
00739 
00740 void AddresseeEditorWidget::editCategories()
00741 {
00742   if ( mCategoryEditDialog == 0 ) {
00743     mCategoryEditDialog = new KPIM::CategoryEditDialog( KABPrefs::instance(), this );
00744     connect( mCategoryEditDialog, SIGNAL( categoryConfigChanged() ),
00745              SLOT( categoryButtonClicked() ) );
00746   }
00747   
00748   mCategoryEditDialog->show();
00749   mCategoryEditDialog->raise();
00750 }
00751 
00752 void AddresseeEditorWidget::emitModified()
00753 {
00754   mDirty = true;
00755 
00756   if ( !mBlockSignals ) {
00757     KABC::Addressee::List list;
00758 
00759     save();
00760     list.append( mAddressee );
00761 
00762     // I hate these hacks...
00763     if ( !isExtension() )
00764       mDirty = true;
00765 
00766     emit modified( list );
00767   }
00768 }
00769 
00770 void AddresseeEditorWidget::dateChanged( QDate )
00771 {
00772   emitModified();
00773 }
00774 
00775 void AddresseeEditorWidget::invalidDate()
00776 {
00777   KMessageBox::sorry( this, i18n( "You must specify a valid date" ) );
00778 }
00779 
00780 void AddresseeEditorWidget::pageChanged( QWidget *wdg )
00781 {
00782   if ( wdg )
00783     KAcceleratorManager::manage( wdg );
00784 }
00785 
00786 QString AddresseeEditorWidget::title() const
00787 {
00788   return i18n( "Contact Editor" );
00789 }
00790 
00791 QString AddresseeEditorWidget::identifier() const
00792 {
00793   return "contact_editor";
00794 }
00795 
00796 void AddresseeEditorWidget::setInitialFocus()
00797 {
00798   mNameEdit->setFocus();
00799 }
00800 
00801 void AddresseeEditorWidget::setReadOnly( bool readOnly )
00802 {
00803   mReadOnly = readOnly;
00804 
00805   mNameEdit->setReadOnly( readOnly );
00806   mRoleEdit->setReadOnly( readOnly );
00807   mOrgEdit->setReadOnly( readOnly );
00808   mPhoneEditWidget->setReadOnly( readOnly );
00809   mAddressEditWidget->setReadOnly( readOnly );
00810   mEmailWidget->setReadOnly( readOnly );
00811   mURLEdit->setReadOnly( readOnly );
00812   mIMAddressEdit->setReadOnly( readOnly );
00813   mCategoryButton->setEnabled( !readOnly );
00814   mSecrecyWidget->setReadOnly( readOnly );
00815   mDepartmentEdit->setReadOnly( readOnly );
00816   mOfficeEdit->setReadOnly( readOnly );
00817   mProfessionEdit->setReadOnly( readOnly );
00818   mManagerEdit->setReadOnly( readOnly );
00819   mAssistantEdit->setReadOnly( readOnly );
00820   mNicknameEdit->setReadOnly( readOnly );
00821   mSpouseEdit->setReadOnly( readOnly );
00822   mBirthdayPicker->setEnabled( !readOnly );
00823   mAnniversaryPicker->setEnabled( !readOnly );
00824   mNoteEdit->setReadOnly( mReadOnly );
00825 
00826   QDictIterator<ContactEditorTabPage> it( mTabPages );
00827   for ( ; it.current(); ++it )
00828     it.current()->setReadOnly( readOnly );
00829 }
00830 
00831 #include "addresseeeditorwidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jan 31 15:54:14 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003