kaddressbook

nameeditdialog.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2002 Mike Pilone <mpilone@slac.com>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of TQt, and distribute the resulting executable,
21  without including the source code for TQt in the source distribution.
22 */
23 
24 #include <tqlayout.h>
25 #include <tqlabel.h>
26 #include <tqlistbox.h>
27 #include <tqlistview.h>
28 #include <tqtooltip.h>
29 #include <tqpushbutton.h>
30 #include <tqcheckbox.h>
31 #include <tqstring.h>
32 #include <tqwhatsthis.h>
33 
34 #include <tdeaccelmanager.h>
35 #include <tdeapplication.h>
36 #include <kbuttonbox.h>
37 #include <tdeconfig.h>
38 #include <klineedit.h>
39 #include <tdelistview.h>
40 #include <kcombobox.h>
41 #include <tdelocale.h>
42 #include <kdebug.h>
43 #include <kiconloader.h>
44 #include <tdemessagebox.h>
45 
46 #include "nameeditdialog.h"
47 
48 NameEditDialog::NameEditDialog( const TDEABC::Addressee &addr, int type,
49  bool readOnly, TQWidget *parent, const char *name )
50  : KDialogBase( Plain, i18n( "Edit Contact Name" ), Help | Ok | Cancel,
51  Ok, parent, name, true ), mAddressee( addr )
52 {
53  TQWidget *page = plainPage();
54  TQGridLayout *layout = new TQGridLayout( page );
55  layout->setSpacing( spacingHint() );
56  layout->addColSpacing( 2, 100 );
57  TQLabel *label;
58 
59  label = new TQLabel( i18n( "Honorific prefixes:" ), page );
60  layout->addWidget( label, 0, 0 );
61  mPrefixCombo = new KComboBox( page );
62  mPrefixCombo->setDuplicatesEnabled( false );
63  mPrefixCombo->setEditable( true );
64  mPrefixCombo->setEnabled( !readOnly );
65  label->setBuddy( mPrefixCombo );
66  layout->addMultiCellWidget( mPrefixCombo, 0, 0, 1, 2 );
67 
68  TQWhatsThis::add( mPrefixCombo, i18n( "The predefined honorific prefixes can be extended in the settings dialog." ) );
69 
70  label = new TQLabel( i18n( "Given name:" ), page );
71  layout->addWidget( label, 1, 0 );
72  mGivenNameEdit = new KLineEdit( page );
73  mGivenNameEdit->setReadOnly( readOnly );
74  label->setBuddy( mGivenNameEdit );
75  layout->addMultiCellWidget( mGivenNameEdit, 1, 1, 1, 2 );
76 
77  label = new TQLabel( i18n( "Additional names:" ), page );
78  layout->addWidget( label, 2, 0 );
79  mAdditionalNameEdit = new KLineEdit( page );
80  mAdditionalNameEdit->setReadOnly( readOnly );
81  label->setBuddy( mAdditionalNameEdit );
82  layout->addMultiCellWidget( mAdditionalNameEdit, 2, 2, 1, 2 );
83 
84  label = new TQLabel( i18n( "Family names:" ), page );
85  layout->addWidget( label, 3, 0 );
86  mFamilyNameEdit = new KLineEdit( page );
87  mFamilyNameEdit->setReadOnly( readOnly );
88  label->setBuddy( mFamilyNameEdit );
89  layout->addMultiCellWidget( mFamilyNameEdit, 3, 3, 1, 2 );
90 
91  label = new TQLabel( i18n( "Honorific suffixes:" ), page );
92  layout->addWidget( label, 4, 0 );
93  mSuffixCombo = new KComboBox( page );
94  mSuffixCombo->setDuplicatesEnabled( false );
95  mSuffixCombo->setEditable( true );
96  mSuffixCombo->setEnabled( !readOnly );
97  label->setBuddy( mSuffixCombo );
98  layout->addMultiCellWidget( mSuffixCombo, 4, 4, 1, 2 );
99 
100  TQWhatsThis::add( mSuffixCombo, i18n( "The predefined honorific suffixes can be extended in the settings dialog." ) );
101 
102  label = new TQLabel( i18n( "Formatted name:" ), page );
103  layout->addWidget( label, 5, 0 );
104 
105  mFormattedNameCombo = new KComboBox( page );
106  mFormattedNameCombo->setEnabled( !readOnly );
107  layout->addWidget( mFormattedNameCombo, 5, 1 );
108  connect( mFormattedNameCombo, TQ_SIGNAL( activated( int ) ), TQ_SLOT( typeChanged( int ) ) );
109 
110  mFormattedNameEdit = new KLineEdit( page );
111  mFormattedNameEdit->setEnabled( type == CustomName && !readOnly );
112  layout->addWidget( mFormattedNameEdit, 5, 2 );
113 
114  mParseBox = new TQCheckBox( i18n( "Parse name automatically" ), page );
115  mParseBox->setEnabled( !readOnly );
116  connect( mParseBox, TQ_SIGNAL( toggled(bool) ), TQ_SLOT( parseBoxChanged(bool) ) );
117  connect( mParseBox, TQ_SIGNAL( toggled(bool) ), TQ_SLOT( modified() ) );
118  layout->addMultiCellWidget( mParseBox, 6, 6, 0, 1 );
119 
120  // Fill in the values
121  mFamilyNameEdit->setText( addr.familyName() );
122  mGivenNameEdit->setText( addr.givenName() );
123  mAdditionalNameEdit->setText( addr.additionalName() );
124  mFormattedNameEdit->setText( addr.formattedName() );
125 
126  // Prefix and suffix combos
127  TDEConfig config( "tdeabcrc" );
128  config.setGroup( "General" );
129 
130  TQStringList sTitle;
131  sTitle += "";
132  sTitle += i18n( "Dr." );
133  sTitle += i18n( "Miss" );
134  sTitle += i18n( "Mr." );
135  sTitle += i18n( "Mrs." );
136  sTitle += i18n( "Ms." );
137  sTitle += i18n( "Prof." );
138  sTitle += config.readListEntry( "Prefixes" );
139  sTitle.sort();
140 
141  TQStringList sSuffix;
142  sSuffix += "";
143  sSuffix += i18n( "I" );
144  sSuffix += i18n( "II" );
145  sSuffix += i18n( "III" );
146  sSuffix += i18n( "Jr." );
147  sSuffix += i18n( "Sr." );
148  sSuffix += config.readListEntry( "Suffixes" );
149  sSuffix.sort();
150 
151  mPrefixCombo->insertStringList( sTitle );
152  mSuffixCombo->insertStringList( sSuffix );
153 
154  mPrefixCombo->setCurrentText( addr.prefix() );
155  mSuffixCombo->setCurrentText( addr.suffix() );
156 
157  mAddresseeConfig.setAddressee( addr );
158  mParseBox->setChecked( mAddresseeConfig.automaticNameParsing() );
159 
160  TDEAcceleratorManager::manage( this );
161 
162  connect( mPrefixCombo, TQ_SIGNAL( textChanged( const TQString& ) ),
163  this, TQ_SLOT( modified() ) );
164  connect( mGivenNameEdit, TQ_SIGNAL( textChanged( const TQString& ) ),
165  this, TQ_SLOT( modified() ) );
166  connect( mAdditionalNameEdit, TQ_SIGNAL( textChanged( const TQString& ) ),
167  this, TQ_SLOT( modified() ) );
168  connect( mFamilyNameEdit, TQ_SIGNAL( textChanged( const TQString& ) ),
169  this, TQ_SLOT( modified() ) );
170  connect( mSuffixCombo, TQ_SIGNAL( textChanged( const TQString& ) ),
171  this, TQ_SLOT( modified() ) );
172  connect( mFormattedNameCombo, TQ_SIGNAL( activated( int ) ),
173  this, TQ_SLOT( modified() ) );
174  connect( mFormattedNameCombo, TQ_SIGNAL( activated( int ) ),
175  this, TQ_SLOT( formattedNameTypeChanged() ) );
176  connect( mFormattedNameEdit, TQ_SIGNAL( textChanged( const TQString& ) ),
177  this, TQ_SLOT( modified() ) );
178  connect( mFormattedNameEdit, TQ_SIGNAL( textChanged( const TQString& ) ),
179  this, TQ_SLOT( formattedNameChanged( const TQString& ) ) );
180 
181  initTypeCombo();
182  mFormattedNameCombo->setCurrentItem( type );
183  mPrefixCombo->lineEdit()->setFocus();
184  mChanged = false;
185 }
186 
187 NameEditDialog::~NameEditDialog()
188 {
189 }
190 
191 TQString NameEditDialog::familyName() const
192 {
193  return mFamilyNameEdit->text();
194 }
195 
196 TQString NameEditDialog::givenName() const
197 {
198  return mGivenNameEdit->text();
199 }
200 
201 TQString NameEditDialog::prefix() const
202 {
203  return mPrefixCombo->currentText();
204 }
205 
206 TQString NameEditDialog::suffix() const
207 {
208  return mSuffixCombo->currentText();
209 }
210 
211 TQString NameEditDialog::additionalName() const
212 {
213  return mAdditionalNameEdit->text();
214 }
215 
216 TQString NameEditDialog::customFormattedName() const
217 {
218  return mFormattedNameEdit->text();
219 }
220 
221 int NameEditDialog::formattedNameType() const
222 {
223  return mFormattedNameCombo->currentItem();
224 }
225 
226 bool NameEditDialog::changed() const
227 {
228  return mChanged;
229 }
230 
231 void NameEditDialog::formattedNameTypeChanged()
232 {
233  TQString name;
234 
235  if ( formattedNameType() == CustomName )
236  name = mCustomFormattedName;
237  else {
238  TDEABC::Addressee addr;
239  addr.setPrefix( prefix() );
240  addr.setFamilyName( familyName() );
241  addr.setAdditionalName( additionalName() );
242  addr.setGivenName( givenName() );
243  addr.setSuffix( suffix() );
244  addr.setOrganization( mAddressee.organization() );
245 
246  name = formattedName( addr, formattedNameType() );
247  }
248 
249  mFormattedNameEdit->setText( name );
250 }
251 
252 TQString NameEditDialog::formattedName( const TDEABC::Addressee &addr, int type )
253 {
254  TQString name;
255 
256  switch ( type ) {
257  case SimpleName:
258  name = addr.givenName() + " " + addr.familyName();
259  break;
260  case FullName:
261  name = addr.assembledName();
262  break;
263  case ReverseNameWithComma:
264  name = addr.familyName() + ", " + addr.givenName();
265  break;
266  case ReverseName:
267  name = addr.familyName() + " " + addr.givenName();
268  break;
269  case Organization:
270  name = addr.organization();
271  break;
272  default:
273  name = "";
274  break;
275  }
276 
277  return name.simplifyWhiteSpace();
278 }
279 
280 void NameEditDialog::parseBoxChanged( bool value )
281 {
282  mAddresseeConfig.setAutomaticNameParsing( value );
283 }
284 
285 void NameEditDialog::typeChanged( int pos )
286 {
287  mFormattedNameEdit->setEnabled( pos == 0 );
288 }
289 
290 void NameEditDialog::formattedNameChanged( const TQString &name )
291 {
292  if ( formattedNameType() == CustomName )
293  mCustomFormattedName = name;
294 }
295 
296 void NameEditDialog::modified()
297 {
298  mChanged = true;
299 }
300 
301 void NameEditDialog::initTypeCombo()
302 {
303  int pos = mFormattedNameCombo->currentItem();
304 
305  mFormattedNameCombo->clear();
306  mFormattedNameCombo->insertItem( i18n( "Custom" ) );
307  mFormattedNameCombo->insertItem( i18n( "Simple Name" ) );
308  mFormattedNameCombo->insertItem( i18n( "Full Name" ) );
309  mFormattedNameCombo->insertItem( i18n( "Reverse Name with Comma" ) );
310  mFormattedNameCombo->insertItem( i18n( "Reverse Name" ) );
311  mFormattedNameCombo->insertItem( i18n( "Organization" ) );
312 
313  mFormattedNameCombo->setCurrentItem( pos );
314 }
315 
316 void NameEditDialog::slotHelp()
317 {
318  kapp->invokeHelp( "managing-contacts-automatic-nameparsing" );
319 }
320 
321 #include "nameeditdialog.moc"