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   mDepartmentEdit->setText( mAddressee.department() );
00533   // compatibility with older versions
00534   if ( mAddressee.department().isEmpty() )
00535     mDepartmentEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-Department" ) );
00536   mURLEdit->setURL( mAddressee.url() );
00537   mURLEdit->home( false );
00538   mBlogEdit->setURL( mAddressee.custom( "KADDRESSBOOK", "BlogFeed" ) );
00539   mNoteEdit->setText( mAddressee.note() );
00540   mEmailWidget->setEmails( mAddressee.emails() );
00541   mPhoneEditWidget->setPhoneNumbers( mAddressee.phoneNumbers() );
00542   mAddressEditWidget->setAddresses( mAddressee, mAddressee.addresses() );
00543   mBirthdayPicker->setDate( mAddressee.birthday().date() );
00544 
00545   QString anniversaryStr = mAddressee.custom( "KADDRESSBOOK", "X-Anniversary" );
00546   QDate anniversary = (anniversaryStr.isEmpty() ? QDate() : QDate::fromString( anniversaryStr, Qt::ISODate ));
00547   mAnniversaryPicker->setDate( anniversary );
00548   mNicknameEdit->setText( mAddressee.nickName() );
00549   mCategoryEdit->setText( mAddressee.categories().join( "," ) );
00550 
00551   mSecrecyWidget->setSecrecy( mAddressee.secrecy() );
00552 
00553   // Load customs
00554   mIMWidget->setPreferredIM( mAddressee.custom( "KADDRESSBOOK", "X-IMAddress" ) );
00555   mSpouseEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-SpousesName" ) );
00556   mManagerEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-ManagersName" ) );
00557   mAssistantEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-AssistantsName" ) );
00558   mOfficeEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-Office" ) );
00559   mProfessionEdit->setText( mAddressee.custom( "KADDRESSBOOK", "X-Profession" ) );
00560   mTitleEdit->setText( mAddressee.title() );
00561 
00562   QDictIterator<ContactEditorTabPage> it( mTabPages );
00563   for ( ; it.current(); ++it )
00564     it.current()->loadContact( &mAddressee );
00565 
00566   blockSignals( block );
00567   mBlockSignals = false;
00568 
00569   mDirty = false;
00570 }
00571 
00572 void AddresseeEditorWidget::save()
00573 {
00574   if ( !mDirty ) return;
00575 
00576   mAddressee.setRole( mRoleEdit->text() );
00577   mAddressee.setOrganization( mOrgEdit->text() );
00578   mAddressee.setDepartment( mDepartmentEdit->text() );
00579 
00580   QString homepage = mURLEdit->text().stripWhiteSpace();
00581   if ( homepage.isEmpty() )
00582      mAddressee.setUrl( KURL() );
00583   else {
00584      if( !homepage.startsWith("http") )
00585        homepage = "http://" + homepage;
00586      mAddressee.setUrl( KURL( homepage ) );
00587   }
00588   if ( !mBlogEdit->text().isEmpty() )
00589     mAddressee.insertCustom( "KADDRESSBOOK", "BlogFeed", mBlogEdit->text() );
00590   else
00591     mAddressee.removeCustom( "KADDRESSBOOK", "BlogFeed" );
00592 
00593   mAddressee.setNote( mNoteEdit->text() );
00594   if ( mBirthdayPicker->date().isValid() )
00595     mAddressee.setBirthday( QDateTime( mBirthdayPicker->date() ) );
00596   else
00597     mAddressee.setBirthday( QDateTime() );
00598 
00599   mAddressee.setNickName( mNicknameEdit->text() );
00600   mAddressee.setCategories( QStringList::split( ",", mCategoryEdit->text() ) );
00601 
00602   mAddressee.setSecrecy( mSecrecyWidget->secrecy() );
00603 
00604   // save custom fields
00605   if ( !mIMWidget->preferredIM().isEmpty() )
00606     mAddressee.insertCustom( "KADDRESSBOOK", "X-IMAddress", mIMWidget->preferredIM() );
00607   else
00608     mAddressee.removeCustom( "KADDRESSBOOK", "X-IMAddress" );
00609   if ( !mSpouseEdit->text().isEmpty() )
00610     mAddressee.insertCustom( "KADDRESSBOOK", "X-SpousesName", mSpouseEdit->text() );
00611   else
00612     mAddressee.removeCustom( "KADDRESSBOOK", "X-SpousesName" );
00613   if ( !mManagerEdit->text().isEmpty() )
00614     mAddressee.insertCustom( "KADDRESSBOOK", "X-ManagersName", mManagerEdit->text() );
00615   else
00616     mAddressee.removeCustom( "KADDRESSBOOK", "X-ManagersName" );
00617   if ( !mAssistantEdit->text().isEmpty() )
00618     mAddressee.insertCustom( "KADDRESSBOOK", "X-AssistantsName", mAssistantEdit->text() );
00619   else
00620     mAddressee.removeCustom( "KADDRESSBOOK", "X-AssistantsName" );
00621 
00622   if ( !mOfficeEdit->text().isEmpty() )
00623     mAddressee.insertCustom( "KADDRESSBOOK", "X-Office", mOfficeEdit->text() );
00624   else
00625     mAddressee.removeCustom( "KADDRESSBOOK", "X-Office" );
00626   if ( !mProfessionEdit->text().isEmpty() )
00627     mAddressee.insertCustom( "KADDRESSBOOK", "X-Profession", mProfessionEdit->text() );
00628   else
00629     mAddressee.removeCustom( "KADDRESSBOOK", "X-Profession" );
00630 
00631   if ( mAnniversaryPicker->date().isValid() )
00632     mAddressee.insertCustom( "KADDRESSBOOK", "X-Anniversary",
00633                              mAnniversaryPicker->date().toString( Qt::ISODate ) );
00634   else
00635     mAddressee.removeCustom( "KADDRESSBOOK", "X-Anniversary" );
00636 
00637   mAddressee.setTitle( mTitleEdit->text() );
00638 
00639   // Save the email addresses
00640   mAddressee.setEmails( mEmailWidget->emails() );
00641 
00642   // Save the phone numbers
00643   KABC::PhoneNumber::List phoneNumbers;
00644   KABC::PhoneNumber::List::ConstIterator phoneIter;
00645   phoneNumbers = mAddressee.phoneNumbers();
00646   for ( phoneIter = phoneNumbers.begin(); phoneIter != phoneNumbers.end();
00647         ++phoneIter )
00648     mAddressee.removePhoneNumber( *phoneIter );
00649 
00650   phoneNumbers = mPhoneEditWidget->phoneNumbers();
00651   for ( phoneIter = phoneNumbers.begin(); phoneIter != phoneNumbers.end();
00652         ++phoneIter )
00653     mAddressee.insertPhoneNumber( *phoneIter );
00654 
00655   // Save the addresses
00656   KABC::Address::List addresses;
00657   KABC::Address::List::ConstIterator addressIter;
00658   addresses = mAddressee.addresses();
00659   for ( addressIter = addresses.begin(); addressIter != addresses.end();
00660         ++addressIter )
00661     mAddressee.removeAddress( *addressIter );
00662 
00663   addresses = mAddressEditWidget->addresses();
00664   for ( addressIter = addresses.begin(); addressIter != addresses.end();
00665         ++addressIter )
00666     mAddressee.insertAddress( *addressIter );
00667 
00668   QDictIterator<ContactEditorTabPage> it( mTabPages );
00669   for ( ; it.current(); ++it )
00670     it.current()->storeContact( &mAddressee );
00671 
00672   mDirty = false;
00673 }
00674 
00675 bool AddresseeEditorWidget::dirty()
00676 {
00677   return mDirty;
00678 }
00679 
00680 void AddresseeEditorWidget::nameTextChanged( const QString &text )
00681 {
00682   // use the addressee class to parse the name for us
00683   AddresseeConfig config( mAddressee );
00684   if ( config.automaticNameParsing() ) {
00685     if ( !mAddressee.formattedName().isEmpty() ) {
00686       QString fn = mAddressee.formattedName();
00687       mAddressee.setNameFromString( text );
00688       mAddressee.setFormattedName( fn );
00689     } else {
00690       // use extra addressee to avoid a formatted name assignment
00691       Addressee addr;
00692       addr.setNameFromString( text );
00693       mAddressee.setPrefix( addr.prefix() );
00694       mAddressee.setGivenName( addr.givenName() );
00695       mAddressee.setAdditionalName( addr.additionalName() );
00696       mAddressee.setFamilyName( addr.familyName() );
00697       mAddressee.setSuffix( addr.suffix() );
00698     }
00699   }
00700 
00701   nameBoxChanged();
00702 
00703   emitModified();
00704 }
00705 
00706 void AddresseeEditorWidget::organizationTextChanged( const QString &text )
00707 {
00708 
00709   AddresseeConfig config( mAddressee );
00710   if ( config.automaticNameParsing() )
00711     mAddressee.setOrganization( text );
00712 
00713   nameBoxChanged();
00714 
00715   mAddressEditWidget->updateAddressee( mAddressee );
00716 
00717   emitModified();
00718 }
00719 
00720 void AddresseeEditorWidget::nameBoxChanged()
00721 {
00722   KABC::Addressee addr;
00723   AddresseeConfig config( mAddressee );
00724   if ( config.automaticNameParsing() ) {
00725     addr.setNameFromString( mNameEdit->text() );
00726     mNameLabel->hide();
00727     mNameEdit->show();
00728   } else {
00729     addr = mAddressee;
00730     mNameEdit->hide();
00731     mNameLabel->setText( mNameEdit->text() );
00732     mNameLabel->show();
00733   }
00734 
00735   if ( mFormattedNameType != NameEditDialog::CustomName ) {
00736     mFormattedNameLabel->setText( NameEditDialog::formattedName( mAddressee, mFormattedNameType ) );
00737     mAddressee.setFormattedName( NameEditDialog::formattedName( mAddressee, mFormattedNameType ) );
00738   }
00739 
00740   mAddressEditWidget->updateAddressee( mAddressee );
00741 }
00742 
00743 void AddresseeEditorWidget::nameButtonClicked()
00744 {
00745   // show the name dialog.
00746   NameEditDialog dialog( mAddressee, mFormattedNameType, mReadOnly, this );
00747 
00748   if ( dialog.exec() ) {
00749     if ( dialog.changed() ) {
00750       mAddressee.setFamilyName( dialog.familyName() );
00751       mAddressee.setGivenName( dialog.givenName() );
00752       mAddressee.setPrefix( dialog.prefix() );
00753       mAddressee.setSuffix( dialog.suffix() );
00754       mAddressee.setAdditionalName( dialog.additionalName() );
00755       mFormattedNameType = dialog.formattedNameType();
00756       if ( mFormattedNameType == NameEditDialog::CustomName ) {
00757         mFormattedNameLabel->setText( dialog.customFormattedName() );
00758         mAddressee.setFormattedName( dialog.customFormattedName() );
00759       }
00760       // Update the name edit.
00761       bool block = mNameEdit->signalsBlocked();
00762       mNameEdit->blockSignals( true );
00763       mNameEdit->setText( mAddressee.assembledName() );
00764       mNameEdit->blockSignals( block );
00765 
00766       // Update the combo box.
00767       nameBoxChanged();
00768 
00769       emitModified();
00770     }
00771   }
00772 }
00773 
00774 void AddresseeEditorWidget::selectCategories()
00775 {
00776   // Show the category dialog
00777   if ( mCategorySelectDialog == 0 ) {
00778     mCategorySelectDialog = new KPIM::CategorySelectDialog( KABPrefs::instance(), this );
00779     connect( mCategorySelectDialog, SIGNAL( categoriesSelected( const QStringList& ) ),
00780              this, SLOT( categoriesSelected( const QStringList& ) ) );
00781     connect( mCategorySelectDialog, SIGNAL( editCategories() ),
00782              this, SLOT( editCategories() ) );
00783   }
00784 
00785   mCategorySelectDialog->setSelected( QStringList::split( ",", mCategoryEdit->text() ) );
00786   mCategorySelectDialog->exec();
00787 }
00788 
00789 void AddresseeEditorWidget::categoriesSelected( const QStringList &list )
00790 {
00791   mCategoryEdit->setText( list.join( "," ) );
00792 }
00793 
00794 void AddresseeEditorWidget::editCategories()
00795 {
00796   if ( mCategoryEditDialog == 0 ) {
00797     mCategoryEditDialog = new KPIM::CategoryEditDialog( KABPrefs::instance(), this );
00798     connect( mCategoryEditDialog, SIGNAL( categoryConfigChanged() ),
00799              mCategorySelectDialog, SLOT( updateCategoryConfig() ) );
00800   }
00801 
00802   mCategoryEditDialog->exec();
00803 }
00804 
00805 void AddresseeEditorWidget::emitModified()
00806 {
00807   if ( mBlockSignals )
00808     return;
00809 
00810   mDirty = true;
00811 
00812   emit modified();
00813 }
00814 
00815 void AddresseeEditorWidget::dateChanged( const QDate& )
00816 {
00817   emitModified();
00818 }
00819 
00820 void AddresseeEditorWidget::invalidDate()
00821 {
00822   KMessageBox::sorry( this, i18n( "You must specify a valid date" ) );
00823 }
00824 
00825 void AddresseeEditorWidget::pageChanged( QWidget *wdg )
00826 {
00827   if ( wdg )
00828     KAcceleratorManager::manage( wdg );
00829 }
00830 
00831 void AddresseeEditorWidget::setInitialFocus()
00832 {
00833   mNameEdit->setFocus();
00834 }
00835 
00836 bool AddresseeEditorWidget::readyToClose()
00837 {
00838   bool ok = true;
00839 
00840   QDate date = mBirthdayPicker->date();
00841   if ( !date.isValid() && !mBirthdayPicker->currentText().isEmpty() ) {
00842     KMessageBox::error( this, i18n( "You have to enter a valid birthdate." ) );
00843     ok = false;
00844   }
00845 
00846   date = mAnniversaryPicker->date();
00847   if ( !date.isValid() && !mAnniversaryPicker->currentText().isEmpty() ) {
00848     KMessageBox::error( this, i18n( "You have to enter a valid anniversary." ) );
00849     ok = false;
00850   }
00851 
00852   return ok;
00853 }
00854 
00855 void AddresseeEditorWidget::setReadOnly( bool readOnly )
00856 {
00857   mReadOnly = readOnly;
00858 
00859   mNameEdit->setReadOnly( readOnly );
00860   mRoleEdit->setReadOnly( readOnly );
00861   mOrgEdit->setReadOnly( readOnly );
00862   mPhoneEditWidget->setReadOnly( readOnly );
00863   mAddressEditWidget->setReadOnly( readOnly );
00864   mEmailWidget->setReadOnly( readOnly );
00865   mURLEdit->setReadOnly( readOnly );
00866   mBlogEdit->setReadOnly( readOnly );
00867   mIMWidget->setReadOnly( readOnly );
00868   mCategoryButton->setEnabled( !readOnly );
00869   mSecrecyWidget->setReadOnly( readOnly );
00870   mDepartmentEdit->setReadOnly( readOnly );
00871   mOfficeEdit->setReadOnly( readOnly );
00872   mProfessionEdit->setReadOnly( readOnly );
00873   mManagerEdit->setReadOnly( readOnly );
00874   mAssistantEdit->setReadOnly( readOnly );
00875   mTitleEdit->setReadOnly( readOnly );
00876   mNicknameEdit->setReadOnly( readOnly );
00877   mSpouseEdit->setReadOnly( readOnly );
00878   mBirthdayPicker->setEnabled( !readOnly );
00879   mAnniversaryPicker->setEnabled( !readOnly );
00880   mNoteEdit->setReadOnly( mReadOnly );
00881 
00882   QDictIterator<ContactEditorTabPage> it( mTabPages );
00883   for ( ; it.current(); ++it )
00884     it.current()->setReadOnly( readOnly );
00885 }
00886 
00887 #include "addresseeeditorwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys