nameeditdialog.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 TQt, and distribute the resulting executable, 00021 without including the source code for TQt in the source distribution. 00022 */ 00023 00024 #include <tqlayout.h> 00025 #include <tqlabel.h> 00026 #include <tqlistbox.h> 00027 #include <tqlistview.h> 00028 #include <tqtooltip.h> 00029 #include <tqpushbutton.h> 00030 #include <tqcheckbox.h> 00031 #include <tqstring.h> 00032 #include <tqwhatsthis.h> 00033 00034 #include <kaccelmanager.h> 00035 #include <kapplication.h> 00036 #include <kbuttonbox.h> 00037 #include <kconfig.h> 00038 #include <klineedit.h> 00039 #include <klistview.h> 00040 #include <kcombobox.h> 00041 #include <klocale.h> 00042 #include <kdebug.h> 00043 #include <kiconloader.h> 00044 #include <kmessagebox.h> 00045 00046 #include "nameeditdialog.h" 00047 00048 NameEditDialog::NameEditDialog( const KABC::Addressee &addr, int type, 00049 bool readOnly, TQWidget *parent, const char *name ) 00050 : KDialogBase( Plain, i18n( "Edit Contact Name" ), Help | Ok | Cancel, 00051 Ok, parent, name, true ), mAddressee( addr ) 00052 { 00053 TQWidget *page = plainPage(); 00054 TQGridLayout *layout = new TQGridLayout( page ); 00055 layout->setSpacing( spacingHint() ); 00056 layout->addColSpacing( 2, 100 ); 00057 TQLabel *label; 00058 00059 label = new TQLabel( i18n( "Honorific prefixes:" ), page ); 00060 layout->addWidget( label, 0, 0 ); 00061 mPrefixCombo = new KComboBox( page ); 00062 mPrefixCombo->setDuplicatesEnabled( false ); 00063 mPrefixCombo->setEditable( true ); 00064 mPrefixCombo->setEnabled( !readOnly ); 00065 label->setBuddy( mPrefixCombo ); 00066 layout->addMultiCellWidget( mPrefixCombo, 0, 0, 1, 2 ); 00067 00068 TQWhatsThis::add( mPrefixCombo, i18n( "The predefined honorific prefixes can be extended in the settings dialog." ) ); 00069 00070 label = new TQLabel( i18n( "Given name:" ), page ); 00071 layout->addWidget( label, 1, 0 ); 00072 mGivenNameEdit = new KLineEdit( page ); 00073 mGivenNameEdit->setReadOnly( readOnly ); 00074 label->setBuddy( mGivenNameEdit ); 00075 layout->addMultiCellWidget( mGivenNameEdit, 1, 1, 1, 2 ); 00076 00077 label = new TQLabel( i18n( "Additional names:" ), page ); 00078 layout->addWidget( label, 2, 0 ); 00079 mAdditionalNameEdit = new KLineEdit( page ); 00080 mAdditionalNameEdit->setReadOnly( readOnly ); 00081 label->setBuddy( mAdditionalNameEdit ); 00082 layout->addMultiCellWidget( mAdditionalNameEdit, 2, 2, 1, 2 ); 00083 00084 label = new TQLabel( i18n( "Family names:" ), page ); 00085 layout->addWidget( label, 3, 0 ); 00086 mFamilyNameEdit = new KLineEdit( page ); 00087 mFamilyNameEdit->setReadOnly( readOnly ); 00088 label->setBuddy( mFamilyNameEdit ); 00089 layout->addMultiCellWidget( mFamilyNameEdit, 3, 3, 1, 2 ); 00090 00091 label = new TQLabel( i18n( "Honorific suffixes:" ), page ); 00092 layout->addWidget( label, 4, 0 ); 00093 mSuffixCombo = new KComboBox( page ); 00094 mSuffixCombo->setDuplicatesEnabled( false ); 00095 mSuffixCombo->setEditable( true ); 00096 mSuffixCombo->setEnabled( !readOnly ); 00097 label->setBuddy( mSuffixCombo ); 00098 layout->addMultiCellWidget( mSuffixCombo, 4, 4, 1, 2 ); 00099 00100 TQWhatsThis::add( mSuffixCombo, i18n( "The predefined honorific suffixes can be extended in the settings dialog." ) ); 00101 00102 label = new TQLabel( i18n( "Formatted name:" ), page ); 00103 layout->addWidget( label, 5, 0 ); 00104 00105 mFormattedNameCombo = new KComboBox( page ); 00106 mFormattedNameCombo->setEnabled( !readOnly ); 00107 layout->addWidget( mFormattedNameCombo, 5, 1 ); 00108 connect( mFormattedNameCombo, TQT_SIGNAL( activated( int ) ), TQT_SLOT( typeChanged( int ) ) ); 00109 00110 mFormattedNameEdit = new KLineEdit( page ); 00111 mFormattedNameEdit->setEnabled( type == CustomName && !readOnly ); 00112 layout->addWidget( mFormattedNameEdit, 5, 2 ); 00113 00114 mParseBox = new TQCheckBox( i18n( "Parse name automatically" ), page ); 00115 mParseBox->setEnabled( !readOnly ); 00116 connect( mParseBox, TQT_SIGNAL( toggled(bool) ), TQT_SLOT( parseBoxChanged(bool) ) ); 00117 connect( mParseBox, TQT_SIGNAL( toggled(bool) ), TQT_SLOT( modified() ) ); 00118 layout->addMultiCellWidget( mParseBox, 6, 6, 0, 1 ); 00119 00120 // Fill in the values 00121 mFamilyNameEdit->setText( addr.familyName() ); 00122 mGivenNameEdit->setText( addr.givenName() ); 00123 mAdditionalNameEdit->setText( addr.additionalName() ); 00124 mFormattedNameEdit->setText( addr.formattedName() ); 00125 00126 // Prefix and suffix combos 00127 KConfig config( "kabcrc" ); 00128 config.setGroup( "General" ); 00129 00130 TQStringList sTitle; 00131 sTitle += ""; 00132 sTitle += i18n( "Dr." ); 00133 sTitle += i18n( "Miss" ); 00134 sTitle += i18n( "Mr." ); 00135 sTitle += i18n( "Mrs." ); 00136 sTitle += i18n( "Ms." ); 00137 sTitle += i18n( "Prof." ); 00138 sTitle += config.readListEntry( "Prefixes" ); 00139 sTitle.sort(); 00140 00141 TQStringList sSuffix; 00142 sSuffix += ""; 00143 sSuffix += i18n( "I" ); 00144 sSuffix += i18n( "II" ); 00145 sSuffix += i18n( "III" ); 00146 sSuffix += i18n( "Jr." ); 00147 sSuffix += i18n( "Sr." ); 00148 sSuffix += config.readListEntry( "Suffixes" ); 00149 sSuffix.sort(); 00150 00151 mPrefixCombo->insertStringList( sTitle ); 00152 mSuffixCombo->insertStringList( sSuffix ); 00153 00154 mPrefixCombo->setCurrentText( addr.prefix() ); 00155 mSuffixCombo->setCurrentText( addr.suffix() ); 00156 00157 mAddresseeConfig.setAddressee( addr ); 00158 mParseBox->setChecked( mAddresseeConfig.automaticNameParsing() ); 00159 00160 KAcceleratorManager::manage( this ); 00161 00162 connect( mPrefixCombo, TQT_SIGNAL( textChanged( const TQString& ) ), 00163 this, TQT_SLOT( modified() ) ); 00164 connect( mGivenNameEdit, TQT_SIGNAL( textChanged( const TQString& ) ), 00165 this, TQT_SLOT( modified() ) ); 00166 connect( mAdditionalNameEdit, TQT_SIGNAL( textChanged( const TQString& ) ), 00167 this, TQT_SLOT( modified() ) ); 00168 connect( mFamilyNameEdit, TQT_SIGNAL( textChanged( const TQString& ) ), 00169 this, TQT_SLOT( modified() ) ); 00170 connect( mSuffixCombo, TQT_SIGNAL( textChanged( const TQString& ) ), 00171 this, TQT_SLOT( modified() ) ); 00172 connect( mFormattedNameCombo, TQT_SIGNAL( activated( int ) ), 00173 this, TQT_SLOT( modified() ) ); 00174 connect( mFormattedNameCombo, TQT_SIGNAL( activated( int ) ), 00175 this, TQT_SLOT( formattedNameTypeChanged() ) ); 00176 connect( mFormattedNameEdit, TQT_SIGNAL( textChanged( const TQString& ) ), 00177 this, TQT_SLOT( modified() ) ); 00178 connect( mFormattedNameEdit, TQT_SIGNAL( textChanged( const TQString& ) ), 00179 this, TQT_SLOT( formattedNameChanged( const TQString& ) ) ); 00180 00181 initTypeCombo(); 00182 mFormattedNameCombo->setCurrentItem( type ); 00183 mPrefixCombo->lineEdit()->setFocus(); 00184 mChanged = false; 00185 } 00186 00187 NameEditDialog::~NameEditDialog() 00188 { 00189 } 00190 00191 TQString NameEditDialog::familyName() const 00192 { 00193 return mFamilyNameEdit->text(); 00194 } 00195 00196 TQString NameEditDialog::givenName() const 00197 { 00198 return mGivenNameEdit->text(); 00199 } 00200 00201 TQString NameEditDialog::prefix() const 00202 { 00203 return mPrefixCombo->currentText(); 00204 } 00205 00206 TQString NameEditDialog::suffix() const 00207 { 00208 return mSuffixCombo->currentText(); 00209 } 00210 00211 TQString NameEditDialog::additionalName() const 00212 { 00213 return mAdditionalNameEdit->text(); 00214 } 00215 00216 TQString NameEditDialog::customFormattedName() const 00217 { 00218 return mFormattedNameEdit->text(); 00219 } 00220 00221 int NameEditDialog::formattedNameType() const 00222 { 00223 return mFormattedNameCombo->currentItem(); 00224 } 00225 00226 bool NameEditDialog::changed() const 00227 { 00228 return mChanged; 00229 } 00230 00231 void NameEditDialog::formattedNameTypeChanged() 00232 { 00233 TQString name; 00234 00235 if ( formattedNameType() == CustomName ) 00236 name = mCustomFormattedName; 00237 else { 00238 KABC::Addressee addr; 00239 addr.setPrefix( prefix() ); 00240 addr.setFamilyName( familyName() ); 00241 addr.setAdditionalName( additionalName() ); 00242 addr.setGivenName( givenName() ); 00243 addr.setSuffix( suffix() ); 00244 addr.setOrganization( mAddressee.organization() ); 00245 00246 name = formattedName( addr, formattedNameType() ); 00247 } 00248 00249 mFormattedNameEdit->setText( name ); 00250 } 00251 00252 TQString NameEditDialog::formattedName( const KABC::Addressee &addr, int type ) 00253 { 00254 TQString name; 00255 00256 switch ( type ) { 00257 case SimpleName: 00258 name = addr.givenName() + " " + addr.familyName(); 00259 break; 00260 case FullName: 00261 name = addr.assembledName(); 00262 break; 00263 case ReverseNameWithComma: 00264 name = addr.familyName() + ", " + addr.givenName(); 00265 break; 00266 case ReverseName: 00267 name = addr.familyName() + " " + addr.givenName(); 00268 break; 00269 case Organization: 00270 name = addr.organization(); 00271 break; 00272 default: 00273 name = ""; 00274 break; 00275 } 00276 00277 return name.simplifyWhiteSpace(); 00278 } 00279 00280 void NameEditDialog::parseBoxChanged( bool value ) 00281 { 00282 mAddresseeConfig.setAutomaticNameParsing( value ); 00283 } 00284 00285 void NameEditDialog::typeChanged( int pos ) 00286 { 00287 mFormattedNameEdit->setEnabled( pos == 0 ); 00288 } 00289 00290 void NameEditDialog::formattedNameChanged( const TQString &name ) 00291 { 00292 if ( formattedNameType() == CustomName ) 00293 mCustomFormattedName = name; 00294 } 00295 00296 void NameEditDialog::modified() 00297 { 00298 mChanged = true; 00299 } 00300 00301 void NameEditDialog::initTypeCombo() 00302 { 00303 int pos = mFormattedNameCombo->currentItem(); 00304 00305 mFormattedNameCombo->clear(); 00306 mFormattedNameCombo->insertItem( i18n( "Custom" ) ); 00307 mFormattedNameCombo->insertItem( i18n( "Simple Name" ) ); 00308 mFormattedNameCombo->insertItem( i18n( "Full Name" ) ); 00309 mFormattedNameCombo->insertItem( i18n( "Reverse Name with Comma" ) ); 00310 mFormattedNameCombo->insertItem( i18n( "Reverse Name" ) ); 00311 mFormattedNameCombo->insertItem( i18n( "Organization" ) ); 00312 00313 mFormattedNameCombo->setCurrentItem( pos ); 00314 } 00315 00316 void NameEditDialog::slotHelp() 00317 { 00318 kapp->invokeHelp( "managing-contacts-automatic-nameparsing" ); 00319 } 00320 00321 #include "nameeditdialog.moc"