kaddressbook

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <kabc/stdaddressbook.h>
00037 #include <kaccelmanager.h>
00038 #include <kapplication.h>
00039 #include <kconfig.h>
00040 #include <kcombobox.h>
00041 #include <kdebug.h>
00042 #include <kdialogbase.h>
00043 #include <kglobal.h>
00044 #include <kiconloader.h>
00045 #include <klineedit.h>
00046 #include <klocale.h>
00047 #include <kmessagebox.h>
00048 #include <kseparator.h>
00049 #include <ksqueezedtextlabel.h>
00050 #include <kstandarddirs.h>
00051 
00052 #include <libkdepim/addresseelineedit.h>
00053 #include <libkdepim/categoryeditdialog.h>
00054 #include <libkdepim/categoryselectdialog.h>
00055 #include <libkdepim/kdateedit.h>
00056 
00057 #include "addresseditwidget.h"
00058 #include "advancedcustomfields.h"
00059 #include "emaileditwidget.h"
00060 #include "imeditwidget.h"
00061 #include "kabprefs.h"
00062 #include "keywidget.h"
00063 #include "nameeditdialog.h"
00064 #include "phoneeditwidget.h"
00065 #include "secrecywidget.h"
00066 
00067 #include "addresseeeditorwidget.h"
00068 
00069 AddresseeEditorWidget::AddresseeEditorWidget( QWidget *parent, const char *name )
00070   : AddresseeEditorBase( parent, name ),
00071     mBlockSignals( false ), mReadOnly( false )
00072 {
00073   kdDebug(5720) << "AddresseeEditorWidget()" << endl;
00074 
00075   initGUI();
00076   mCategorySelectDialog = 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()->automaticNameParsing() ) {
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( "<roleLabel>:", "%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( "<organizationLabel>:", "%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( "kfm_home", 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, 10, 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   QHBoxLayout *homePageLayout = new QHBoxLayout( 0, 11, 7 );
00240 
00241   label = new QLabel( tab1 );
00242   label->setPixmap( KGlobal::iconLoader()->loadIcon( "homepage", KIcon::Desktop,
00243                                                      KIcon::SizeMedium ) );
00244   homePageLayout->addWidget( label );
00245 
00246   label = new QLabel( i18n( "<urlLabel>:", "%1:" ).arg( KABC::Addressee::urlLabel() ), tab1 );
00247   mURLEdit = new KLineEdit( tab1 );
00248   connect( mURLEdit, SIGNAL( textChanged( const QString& ) ),
00249            SLOT( textChanged( const QString& ) ) );
00250   label->setBuddy( mURLEdit );
00251   homePageLayout->addWidget( label );
00252   homePageLayout->addWidget( mURLEdit );
00253   layout->addMultiCellLayout( homePageLayout, 8, 8, 3, 6 );
00254 
00255   QHBoxLayout *blogLayout = new QHBoxLayout( 0, 11, 7 );
00256   label = new QLabel( i18n("Blog feed:"), tab1 );
00257   blogLayout->addWidget( label );
00258   mBlogEdit = new KLineEdit( tab1 );
00259   blogLayout->addWidget( mBlogEdit );
00260   connect( mBlogEdit, SIGNAL( textChanged( const QString & ) ),
00261            SLOT( textChanged( const QString & ) ) );
00262   label->setBuddy( mBlogEdit );
00263   layout->addMultiCellLayout( blogLayout, 9, 9, 4, 6 );
00264 
00265   mIMWidget = new IMEditWidget( tab1, mAddressee );
00266   connect( mIMWidget, SIGNAL( modified() ), SLOT( emitModified() ) );
00267   layout->addMultiCellWidget( mIMWidget, 10, 10, 4, 6 );
00268 
00269   layout->addColSpacing( 6, 50 );
00270 
00271   bar = new KSeparator( KSeparator::HLine, tab1 );
00272   layout->addMultiCellWidget( bar, 11, 11, 0, 6 );
00273 
00275   QHBox *categoryBox = new QHBox( tab1 );
00276   categoryBox->setSpacing( KDialogBase::spacingHint() );
00277 
00278   // Categories
00279   mCategoryButton = new QPushButton( i18n( "Select Categories..." ), categoryBox );
00280   connect( mCategoryButton, SIGNAL( clicked() ), SLOT( selectCategories() ) );
00281 
00282   mCategoryEdit = new KLineEdit( categoryBox );
00283   mCategoryEdit->setReadOnly( true );
00284   connect( mCategoryEdit, SIGNAL( textChanged( const QString& ) ),
00285            SLOT( textChanged( const QString& ) ) );
00286 
00287   mSecrecyWidget = new SecrecyWidget( categoryBox );
00288   connect( mSecrecyWidget, SIGNAL( changed() ), SLOT( emitModified() ) );
00289 
00290   layout->addMultiCellWidget( categoryBox, 12, 12, 0, 6 );
00291 
00292   // Build the layout and add to the tab widget
00293   layout->activate(); // required
00294 
00295   mTabWidget->addTab( tab1, i18n( "&General" ) );
00296 }
00297 
00298 void AddresseeEditorWidget::setupTab2()
00299 {
00300   // This is the Details tab
00301   QWidget *tab2 = new QWidget( mTabWidget );
00302 
00303   QGridLayout *layout = new QGridLayout( tab2, 6, 6 );
00304   layout->setMargin( KDialogBase::marginHint() );
00305   layout->setSpacing( KDialogBase::spacingHint() );
00306 
00307   QLabel *label;
00308   KSeparator* bar;
00309 
00311   // Office info
00312 
00313   // Department
00314   label = new QLabel( tab2 );
00315   label->setPixmap( KGlobal::iconLoader()->loadIcon( "folder", KIcon::Desktop,
00316                                                      KIcon::SizeMedium ) );
00317   layout->addMultiCellWidget( label, 0, 1, 0, 0 );
00318 
00319   label = new QLabel( i18n( "Department:" ), tab2 );
00320   layout->addWidget( label, 0, 1 );
00321   mDepartmentEdit = new KLineEdit( tab2 );
00322   connect( mDepartmentEdit, SIGNAL( textChanged( const QString& ) ),
00323            SLOT( textChanged( const QString& ) ) );
00324   label->setBuddy( mDepartmentEdit );
00325   layout->addWidget( mDepartmentEdit, 0, 2 );
00326 
00327   label = new QLabel( i18n( "Office:" ), tab2 );
00328   layout->addWidget( label, 1, 1 );
00329   mOfficeEdit = new KLineEdit( tab2 );
00330   connect( mOfficeEdit, SIGNAL( textChanged( const QString& ) ),
00331            SLOT( textChanged( const QString& ) ) );
00332   label->setBuddy( mOfficeEdit );
00333   layout->addWidget( mOfficeEdit, 1, 2 );
00334 
00335   label = new QLabel( i18n( "Profession:" ), tab2 );
00336   layout->addWidget( label, 2, 1 );
00337   mProfessionEdit = new KLineEdit( tab2 );
00338   connect( mProfessionEdit, SIGNAL( textChanged( const QString& ) ),
00339            SLOT( textChanged( const QString& ) ) );
00340   label->setBuddy( mProfessionEdit );
00341   layout->addWidget( mProfessionEdit, 2, 2 );
00342 
00343   label = new QLabel( i18n( "Manager\'s name:" ), tab2 );
00344   layout->addWidget( label, 0, 3 );
00345   mManagerEdit = new KPIM::AddresseeLineEdit( tab2 );
00346   connect( mManagerEdit, SIGNAL( textChanged( const QString& ) ),
00347            SLOT( textChanged( const QString& ) ) );
00348   label->setBuddy( mManagerEdit );
00349   layout->addMultiCellWidget( mManagerEdit, 0, 0, 4, 5 );
00350 
00351   label = new QLabel( i18n( "Assistant's name:" ), tab2 );
00352   layout->addWidget( label, 1, 3 );
00353   mAssistantEdit = new KPIM::AddresseeLineEdit( tab2 );
00354   connect( mAssistantEdit, SIGNAL( textChanged( const QString& ) ),
00355            SLOT( textChanged( const QString& ) ) );
00356   label->setBuddy( mAssistantEdit );
00357   layout->addMultiCellWidget( mAssistantEdit, 1, 1, 4, 5 );
00358 
00359   label = new QLabel( i18n( "<titleLabel>:", "%1:" ).arg( KABC::Addressee::titleLabel() ), tab2 );
00360   layout->addWidget( label, 2, 3 );
00361   mTitleEdit = new KLineEdit( tab2 );
00362   connect( mTitleEdit, SIGNAL( textChanged( const QString& ) ),
00363            SLOT( textChanged( const QString& ) ) );
00364   label->setBuddy( mTitleEdit );
00365   layout->addMultiCellWidget( mTitleEdit, 2, 2, 4, 5 );
00366 
00367   bar = new KSeparator( KSeparator::HLine, tab2 );
00368   layout->addMultiCellWidget( bar, 3, 3, 0, 5 );
00369 
00371   // Personal info
00372 
00373   label = new QLabel( tab2 );
00374   label->setPixmap( KGlobal::iconLoader()->loadIcon( "personal", KIcon::Desktop,
00375                                                      KIcon::SizeMedium ) );
00376   layout->addMultiCellWidget( label, 4, 5, 0, 0 );
00377 
00378   label = new QLabel( i18n( "Nickname:" ), tab2 );
00379   layout->addWidget( label, 4, 1 );
00380   mNicknameEdit = new KLineEdit( tab2 );
00381   connect( mNicknameEdit, SIGNAL( textChanged( const QString& ) ),
00382            SLOT( textChanged( const QString& ) ) );
00383   label->setBuddy( mNicknameEdit );
00384   layout->addWidget( mNicknameEdit, 4, 2 );
00385 
00386   label = new QLabel( i18n( "Partner's name:" ), tab2 );
00387   layout->addWidget( label, 5, 1 );
00388   mSpouseEdit = new KPIM::AddresseeLineEdit( tab2 );
00389   connect( mSpouseEdit, SIGNAL( textChanged( const QString& ) ),
00390            SLOT( textChanged( const QString& ) ) );
00391   label->setBuddy( mSpouseEdit );
00392   layout->addWidget( mSpouseEdit, 5, 2 );
00393 
00394   label = new QLabel( i18n( "Birthdate:" ), tab2 );
00395   layout->addWidget( label, 4, 3 );
00396   mBirthdayPicker = new KDateEdit( tab2 );
00397   connect( mBirthdayPicker, SIGNAL( dateChanged( const QDate& ) ),
00398            SLOT( dateChanged( const QDate& ) ) );
00399   connect( mBirthdayPicker, SIGNAL( textChanged( const QString& ) ),
00400            SLOT( emitModified() ) );
00401   label->setBuddy( mBirthdayPicker );
00402   layout->addWidget( mBirthdayPicker, 4, 4 );
00403 
00404   label = new QLabel( i18n( "Anniversary:" ), tab2 );
00405   layout->addWidget( label, 5, 3 );
00406   mAnniversaryPicker = new KDateEdit( tab2 );
00407   connect( mAnniversaryPicker, SIGNAL( dateChanged( const QDate& ) ),
00408            SLOT( dateChanged( const QDate& ) ) );
00409   connect( mAnniversaryPicker, SIGNAL( textChanged( const QString& ) ),
00410            SLOT( emitModified() ) );
00411   label->setBuddy( mAnniversaryPicker );
00412   layout->addWidget( mAnniversaryPicker, 5, 4 );
00413 
00414   bar = new KSeparator( KSeparator::HLine, tab2 );
00415   layout->addMultiCellWidget( bar, 6, 6, 0, 5 );
00416 
00418   // Notes
00419   label = new QLabel( i18n( "Note:" ), tab2 );
00420   label->setAlignment( Qt::AlignTop | Qt::AlignLeft );
00421   layout->addWidget( label, 7, 0 );
00422   mNoteEdit = new QTextEdit( tab2 );
00423   mNoteEdit->setWordWrap( QTextEdit::WidgetWidth );
00424   mNoteEdit->setMinimumSize( mNoteEdit->sizeHint() );
00425   connect( mNoteEdit, SIGNAL( textChanged() ), SLOT( emitModified() ) );
00426   label->setBuddy( mNoteEdit );
00427   layout->addMultiCellWidget( mNoteEdit, 7, 7, 1, 5 );
00428 
00429    // Build the layout and add to the tab widget
00430   layout->activate(); // required
00431 
00432   mTabWidget->addTab( tab2, i18n( "&Details" ) );
00433 }
00434 
00435 void AddresseeEditorWidget::setupAdditionalTabs()
00436 {
00437   ContactEditorWidgetManager *manager = ContactEditorWidgetManager::self();
00438 
00439   // create all tab pages and add the widgets
00440   for ( int i = 0; i < manager->count(); ++i ) {
00441     QString pageIdentifier = manager->factory( i )->pageIdentifier();
00442     QString pageTitle = manager->factory( i )->pageTitle();
00443 
00444     if ( pageIdentifier == "misc" )
00445       pageTitle = i18n( "Misc" );
00446 
00447     ContactEditorTabPage *page = mTabPages[ pageIdentifier ];
00448     if ( page == 0 ) { // tab not yet available, create one
00449       page = new ContactEditorTabPage( mTabWidget );
00450       mTabPages.insert( pageIdentifier, page );
00451 
00452       mTabWidget->addTab( page, pageTitle );
00453 
00454       connect( page, SIGNAL( changed() ), SLOT( emitModified() ) );
00455     }
00456 
00457     KAB::ContactEditorWidget *widget
00458               = manager->factory( i )->createWidget( KABC::StdAddressBook::self( true ),
00459                                                      page );
00460     if ( widget )
00461       page->addWidget( widget );
00462   }
00463 
00464   // query the layout update
00465   QDictIterator<ContactEditorTabPage> it( mTabPages );
00466   for ( ; it.current(); ++it )
00467     it.current()->updateLayout();
00468 }
00469 
00470 void AddresseeEditorWidget::setupCustomFieldsTabs()
00471 {
00472   QStringList activePages = KABPrefs::instance()->advancedCustomFields();
00473 
00474   const QStringList list = KGlobal::dirs()->findAllResources( "data", "kaddressbook/contacteditorpages/*.ui", true, true );
00475   for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) {
00476     if ( activePages.find( (*it).mid( (*it).findRev('/') + 1 ) ) == activePages.end() )
00477       continue;
00478 
00479     ContactEditorTabPage *page = new ContactEditorTabPage( mTabWidget );
00480     AdvancedCustomFields *wdg = new AdvancedCustomFields( *it, KABC::StdAddressBook::self( true ), page );
00481     if ( wdg ) {
00482       mTabPages.insert( wdg->pageIdentifier(), page );
00483       mTabWidget->addTab( page, wdg->pageTitle() );
00484 
00485       page->addWidget( wdg );
00486       page->updateLayout();
00487 
00488       connect( page, SIGNAL( changed() ), SLOT( emitModified() ) );
00489     } else
00490       delete page;
00491   }
00492 }
00493 
00494 void AddresseeEditorWidget::load()
00495 {
00496   kdDebug(5720) << "AddresseeEditorWidget::load()" << endl;
00497 
00498   // Block signals in case anything tries to emit modified
00499   // CS: This doesn't seem to work.
00500   bool block = signalsBlocked();
00501   blockSignals( true );
00502   mBlockSignals = true; // used for internal signal blocking
00503 
00504   mNameEdit->blockSignals( true );
00505   mNameEdit->setText( mAddressee.assembledName() );
00506   mNameEdit->blockSignals( false );
00507 
00508   if ( mAddressee.formattedName().isEmpty() ) {
00509     KConfig config( "kaddressbookrc" );
00510     config.setGroup( "General" );
00511     mFormattedNameType = config.readNumEntry( "FormattedNameType", 1 );
00512     mAddressee.setFormattedName( NameEditDialog::formattedName( mAddressee, mFormattedNameType ) );
00513   } else {
00514     if ( mAddressee.formattedName() == NameEditDialog::formattedName( mAddressee, NameEditDialog::SimpleName ) )
00515       mFormattedNameType = NameEditDialog::SimpleName;
00516     else if ( mAddressee.formattedName() == NameEditDialog::formattedName( mAddressee, NameEditDialog::FullName ) )
00517       mFormattedNameType = NameEditDialog::FullName;
00518     else if ( mAddressee.formattedName() == NameEditDialog::formattedName( mAddressee, NameEditDialog::ReverseNameWithComma ) )
00519       mFormattedNameType = NameEditDialog::ReverseNameWithComma;
00520     else if ( mAddressee.formattedName() == NameEditDialog::formattedName( mAddressee, NameEditDialog::ReverseName ) )
00521       mFormattedNameType = NameEditDialog::ReverseName;
00522     else if ( mAddressee.formattedName() == NameEditDialog::formattedName( mAddressee, NameEditDialog::Organization ) )
00523       mFormattedNameType = NameEditDialog::Organization;
00524     else
00525       mFormattedNameType = NameEditDialog::CustomName;
00526   }
00527 
00528   mFormattedNameLabel->setText( mAddressee.formattedName() );
00529 
00530   mRoleEdit->setText( mAddressee.role() );
00531   mOrgEdit->setText( mAddressee.organization() );
00532   mURLEdit->setURL( mAddressee.url() );
00533   mURLEdit->home( false );
00534   mBlogEdit->setURL( mAddressee.custom( "KADDRESSBOOK", "BlogFeed" ) );
00535   mNoteEdit->setText( mAddressee.note() );
00536   mEmailWidget->setEmails( mAddressee.emails() );
00537   mPhoneEditWidget->setPhoneNumbers( mAddressee.phoneNumbers() );
00538   mAddressEditWidget->setAddresses( mAddressee, mAddressee.addresses() );
00539   mBirthdayPicker->setDate( mAddressee.birthday().date() );
00540 
00541   QString anniversaryStr = mAddressee.custom( "KADDRESSBOOK", "X-Anniversary" );
00542   QDate anniversary = (anniversaryStr.isEmpty() ? QDate() : QDate::fromString( anniversaryStr, Qt::ISODate ));
00543   mAnniversaryPicker->setDate( anniversary );
00544   mNicknameEdit->setText( mAddressee.nickName() );
00545   mCategoryEdit->setText( mAddressee.categories().join( "," ) );
00546 
00547   mSecrecyWidget->setSecrecy( mAddressee.secrecy() );
00548 
00549   // Load customs
00550   mIMWidget->setPreferredIM( mAddressee.custom( "KADDRESSBOOK", "X-IMAddress" ) );
00551   mSpouseEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-SpousesName" ) );
00552   mManagerEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-ManagersName" ) );
00553   mAssistantEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-AssistantsName" ) );
00554   mDepartmentEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-Department" ) );
00555   mOfficeEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-Office" ) );
00556   mProfessionEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-Profession" ) );
00557   mTitleEdit->setText( mAddressee.title() );
00558 
00559   QDictIterator<ContactEditorTabPage> it( mTabPages );
00560   for ( ; it.current(); ++it )
00561     it.current()->loadContact( &mAddressee );
00562 
00563   blockSignals( block );
00564   mBlockSignals = false;
00565 
00566   mDirty = false;
00567 }
00568 
00569 void AddresseeEditorWidget::save()
00570 {
00571   if ( !mDirty ) return;
00572 
00573   mAddressee.setRole( mRoleEdit->text() );
00574   mAddressee.setOrganization( mOrgEdit->text() );
00575   mAddressee.setUrl( KURL( mURLEdit->text().stripWhiteSpace() ) );
00576   if ( !mBlogEdit->text().isEmpty() )
00577     mAddressee.insertCustom( "KADDRESSBOOK", "BlogFeed", mBlogEdit->text() );
00578   else
00579     mAddressee.removeCustom( "KADDRESSBOOK", "BlogFeed" );
00580 
00581   mAddressee.setNote( mNoteEdit->text() );
00582   if ( mBirthdayPicker->date().isValid() )
00583     mAddressee.setBirthday( QDateTime( mBirthdayPicker->date() ) );
00584   else
00585     mAddressee.setBirthday( QDateTime() );
00586 
00587   mAddressee.setNickName( mNicknameEdit->text() );
00588   mAddressee.setCategories( QStringList::split( ",", mCategoryEdit->text() ) );
00589 
00590   mAddressee.setSecrecy( mSecrecyWidget->secrecy() );
00591 
00592   // save custom fields
00593   if ( !mIMWidget->preferredIM().isEmpty() )
00594     mAddressee.insertCustom( "KADDRESSBOOK", "X-IMAddress", mIMWidget->preferredIM() );
00595   else
00596     mAddressee.removeCustom( "KADDRESSBOOK", "X-IMAddress" );
00597   if ( !mSpouseEdit->text().isEmpty() )
00598     mAddressee.insertCustom( "KADDRESSBOOK", "X-SpousesName", mSpouseEdit->text() );
00599   else
00600     mAddressee.removeCustom( "KADDRESSBOOK", "X-SpousesName" );
00601   if ( !mManagerEdit->text().isEmpty() )
00602     mAddressee.insertCustom( "KADDRESSBOOK", "X-ManagersName", mManagerEdit->text() );
00603   else
00604     mAddressee.removeCustom( "KADDRESSBOOK", "X-ManagersName" );
00605   if ( !mAssistantEdit->text().isEmpty() )
00606     mAddressee.insertCustom( "KADDRESSBOOK", "X-AssistantsName", mAssistantEdit->text() );
00607   else
00608     mAddressee.removeCustom( "KADDRESSBOOK", "X-AssistantsName" );
00609 
00610   if ( !mDepartmentEdit->text().isEmpty() )
00611     mAddressee.insertCustom( "KADDRESSBOOK", "X-Department", mDepartmentEdit->text() );
00612   else
00613     mAddressee.removeCustom( "KADDRESSBOOK", "X-Department" );
00614   if ( !mOfficeEdit->text().isEmpty() )
00615     mAddressee.insertCustom( "KADDRESSBOOK", "X-Office", mOfficeEdit->text() );
00616   else
00617     mAddressee.removeCustom( "KADDRESSBOOK", "X-Office" );
00618   if ( !mProfessionEdit->text().isEmpty() )
00619     mAddressee.insertCustom( "KADDRESSBOOK", "X-Profession", mProfessionEdit->text() );
00620   else
00621     mAddressee.removeCustom( "KADDRESSBOOK", "X-Profession" );
00622 
00623   if ( mAnniversaryPicker->date().isValid() )
00624     mAddressee.insertCustom( "KADDRESSBOOK", "X-Anniversary",
00625                              mAnniversaryPicker->date().toString( Qt::ISODate ) );
00626   else
00627     mAddressee.removeCustom( "KADDRESSBOOK", "X-Anniversary" );
00628 
00629   mAddressee.setTitle( mTitleEdit->text() );
00630 
00631   // Save the email addresses
00632   mAddressee.setEmails( mEmailWidget->emails() );
00633 
00634   // Save the phone numbers
00635   KABC::PhoneNumber::List phoneNumbers;
00636   KABC::PhoneNumber::List::ConstIterator phoneIter;
00637   phoneNumbers = mAddressee.phoneNumbers();
00638   for ( phoneIter = phoneNumbers.begin(); phoneIter != phoneNumbers.end();
00639         ++phoneIter )
00640     mAddressee.removePhoneNumber( *phoneIter );
00641 
00642   phoneNumbers = mPhoneEditWidget->phoneNumbers();
00643   for ( phoneIter = phoneNumbers.begin(); phoneIter != phoneNumbers.end();
00644         ++phoneIter )
00645     mAddressee.insertPhoneNumber( *phoneIter );
00646 
00647   // Save the addresses
00648   KABC::Address::List addresses;
00649   KABC::Address::List::ConstIterator addressIter;
00650   addresses = mAddressee.addresses();
00651   for ( addressIter = addresses.begin(); addressIter != addresses.end();
00652         ++addressIter )
00653     mAddressee.removeAddress( *addressIter );
00654 
00655   addresses = mAddressEditWidget->addresses();
00656   for ( addressIter = addresses.begin(); addressIter != addresses.end();
00657         ++addressIter )
00658     mAddressee.insertAddress( *addressIter );
00659 
00660   QDictIterator<ContactEditorTabPage> it( mTabPages );
00661   for ( ; it.current(); ++it )
00662     it.current()->storeContact( &mAddressee );
00663 
00664   mDirty = false;
00665 }
00666 
00667 bool AddresseeEditorWidget::dirty()
00668 {
00669   return mDirty;
00670 }
00671 
00672 void AddresseeEditorWidget::nameTextChanged( const QString &text )
00673 {
00674   // use the addressee class to parse the name for us
00675   AddresseeConfig config( mAddressee );
00676   if ( config.automaticNameParsing() ) {
00677     if ( !mAddressee.formattedName().isEmpty() ) {
00678       QString fn = mAddressee.formattedName();
00679       mAddressee.setNameFromString( text );
00680       mAddressee.setFormattedName( fn );
00681     } else {
00682       // use extra addressee to avoid a formatted name assignment
00683       Addressee addr;
00684       addr.setNameFromString( text );
00685       mAddressee.setPrefix( addr.prefix() );
00686       mAddressee.setGivenName( addr.givenName() );
00687       mAddressee.setAdditionalName( addr.additionalName() );
00688       mAddressee.setFamilyName( addr.familyName() );
00689       mAddressee.setSuffix( addr.suffix() );
00690     }
00691   }
00692 
00693   nameBoxChanged();
00694 
00695   emitModified();
00696 }
00697 
00698 void AddresseeEditorWidget::organizationTextChanged( const QString &text )
00699 {
00700 
00701   AddresseeConfig config( mAddressee );
00702   if ( config.automaticNameParsing() )
00703     mAddressee.setOrganization( text );
00704 
00705   nameBoxChanged();
00706 
00707   mAddressEditWidget->updateAddressee( mAddressee );
00708 
00709   emitModified();
00710 }
00711 
00712 void AddresseeEditorWidget::nameBoxChanged()
00713 {
00714   KABC::Addressee addr;
00715   AddresseeConfig config( mAddressee );
00716   if ( config.automaticNameParsing() ) {
00717     addr.setNameFromString( mNameEdit->text() );
00718     mNameLabel->hide();
00719     mNameEdit->show();
00720   } else {
00721     addr = mAddressee;
00722     mNameEdit->hide();
00723     mNameLabel->setText( mNameEdit->text() );
00724     mNameLabel->show();
00725   }
00726 
00727   if ( mFormattedNameType != NameEditDialog::CustomName ) {
00728     mFormattedNameLabel->setText( NameEditDialog::formattedName( mAddressee, mFormattedNameType ) );
00729     mAddressee.setFormattedName( NameEditDialog::formattedName( mAddressee, mFormattedNameType ) );
00730   }
00731 
00732   mAddressEditWidget->updateAddressee( mAddressee );
00733 }
00734 
00735 void AddresseeEditorWidget::nameButtonClicked()
00736 {
00737   // show the name dialog.
00738   NameEditDialog dialog( mAddressee, mFormattedNameType, mReadOnly, this );
00739 
00740   if ( dialog.exec() ) {
00741     if ( dialog.changed() ) {
00742       mAddressee.setFamilyName( dialog.familyName() );
00743       mAddressee.setGivenName( dialog.givenName() );
00744       mAddressee.setPrefix( dialog.prefix() );
00745       mAddressee.setSuffix( dialog.suffix() );
00746       mAddressee.setAdditionalName( dialog.additionalName() );
00747       mFormattedNameType = dialog.formattedNameType();
00748       if ( mFormattedNameType == NameEditDialog::CustomName ) {
00749         mFormattedNameLabel->setText( dialog.customFormattedName() );
00750         mAddressee.setFormattedName( dialog.customFormattedName() );
00751       }
00752       // Update the name edit.
00753       bool block = mNameEdit->signalsBlocked();
00754       mNameEdit->blockSignals( true );
00755       mNameEdit->setText( mAddressee.assembledName() );
00756       mNameEdit->blockSignals( block );
00757 
00758       // Update the combo box.
00759       nameBoxChanged();
00760 
00761       emitModified();
00762     }
00763   }
00764 }
00765 
00766 void AddresseeEditorWidget::selectCategories()
00767 {
00768   // Show the category dialog
00769   if ( mCategorySelectDialog == 0 ) {
00770     mCategorySelectDialog = new KPIM::CategorySelectDialog( KABPrefs::instance(), this );
00771     connect( mCategorySelectDialog, SIGNAL( categoriesSelected( const QStringList& ) ),
00772              this, SLOT( categoriesSelected( const QStringList& ) ) );
00773     connect( mCategorySelectDialog, SIGNAL( editCategories() ),
00774              this, SLOT( editCategories() ) );
00775   }
00776 
00777   mCategorySelectDialog->setSelected( QStringList::split( ",", mCategoryEdit->text() ) );
00778   mCategorySelectDialog->show();
00779   mCategorySelectDialog->raise();
00780 }
00781 
00782 void AddresseeEditorWidget::categoriesSelected( const QStringList &list )
00783 {
00784   mCategoryEdit->setText( list.join( "," ) );
00785 }
00786 
00787 void AddresseeEditorWidget::editCategories()
00788 {
00789   if ( mCategoryEditDialog == 0 ) {
00790     mCategoryEditDialog = new KPIM::CategoryEditDialog( KABPrefs::instance(), this );
00791     connect( mCategoryEditDialog, SIGNAL( categoryConfigChanged() ),
00792              mCategorySelectDialog, SLOT( updateCategoryConfig() ) );
00793   }
00794 
00795   mCategoryEditDialog->show();
00796   mCategoryEditDialog->raise();
00797 }
00798 
00799 void AddresseeEditorWidget::emitModified()
00800 {
00801   if ( mBlockSignals )
00802     return;
00803 
00804   mDirty = true;
00805 
00806   emit modified();
00807 }
00808 
00809 void AddresseeEditorWidget::dateChanged( const QDate& )
00810 {
00811   emitModified();
00812 }
00813 
00814 void AddresseeEditorWidget::invalidDate()
00815 {
00816   KMessageBox::sorry( this, i18n( "You must specify a valid date" ) );
00817 }
00818 
00819 void AddresseeEditorWidget::pageChanged( QWidget *wdg )
00820 {
00821   if ( wdg )
00822     KAcceleratorManager::manage( wdg );
00823 }
00824 
00825 void AddresseeEditorWidget::setInitialFocus()
00826 {
00827   mNameEdit->setFocus();
00828 }
00829 
00830 bool AddresseeEditorWidget::readyToClose()
00831 {
00832   bool ok = true;
00833 
00834   QDate date = mBirthdayPicker->date();
00835   if ( !date.isValid() && !mBirthdayPicker->currentText().isEmpty() ) {
00836     KMessageBox::error( this, i18n( "You have to enter a valid birthdate." ) );
00837     ok = false;
00838   }
00839 
00840   date = mAnniversaryPicker->date();
00841   if ( !date.isValid() && !mAnniversaryPicker->currentText().isEmpty() ) {
00842     KMessageBox::error( this, i18n( "You have to enter a valid anniversary." ) );
00843     ok = false;
00844   }
00845 
00846   return ok;
00847 }
00848 
00849 void AddresseeEditorWidget::setReadOnly( bool readOnly )
00850 {
00851   mReadOnly = readOnly;
00852 
00853   mNameEdit->setReadOnly( readOnly );
00854   mRoleEdit->setReadOnly( readOnly );
00855   mOrgEdit->setReadOnly( readOnly );
00856   mPhoneEditWidget->setReadOnly( readOnly );
00857   mAddressEditWidget->setReadOnly( readOnly );
00858   mEmailWidget->setReadOnly( readOnly );
00859   mURLEdit->setReadOnly( readOnly );
00860   mBlogEdit->setReadOnly( readOnly );
00861   mIMWidget->setReadOnly( readOnly );
00862   mCategoryButton->setEnabled( !readOnly );
00863   mSecrecyWidget->setReadOnly( readOnly );
00864   mDepartmentEdit->setReadOnly( readOnly );
00865   mOfficeEdit->setReadOnly( readOnly );
00866   mProfessionEdit->setReadOnly( readOnly );
00867   mManagerEdit->setReadOnly( readOnly );
00868   mAssistantEdit->setReadOnly( readOnly );
00869   mTitleEdit->setReadOnly( readOnly );
00870   mNicknameEdit->setReadOnly( readOnly );
00871   mSpouseEdit->setReadOnly( readOnly );
00872   mBirthdayPicker->setEnabled( !readOnly );
00873   mAnniversaryPicker->setEnabled( !readOnly );
00874   mNoteEdit->setReadOnly( mReadOnly );
00875 
00876   QDictIterator<ContactEditorTabPage> it( mTabPages );
00877   for ( ; it.current(); ++it )
00878     it.current()->setReadOnly( readOnly );
00879 }
00880 
00881 #include "addresseeeditorwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys