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