24 #include <tqcheckbox.h>
25 #include <tqdatetimeedit.h>
29 #include <tqpushbutton.h>
30 #include <tqvalidator.h>
31 #include <tqspinbox.h>
33 #include <tdeaccelmanager.h>
34 #include <kcombobox.h>
35 #include <kinputdialog.h>
36 #include <klineedit.h>
37 #include <tdemessagebox.h>
39 #include "addresseeconfig.h"
42 #include "customfieldswidget.h"
45 AddFieldDialog::AddFieldDialog( TQWidget *parent,
const char *name )
46 : KDialogBase( Plain, i18n(
"Add Field" ), Ok | Cancel,
47 Ok, parent, name, true, true )
49 TQWidget *page = plainPage();
51 TQGridLayout *layout =
new TQGridLayout( page, 3, 2, marginHint(), spacingHint() );
53 TQLabel *label =
new TQLabel( i18n(
"Title:" ), page );
54 layout->addWidget( label, 0, 0 );
56 mTitle =
new KLineEdit( page );
57 mTitle->setValidator(
new TQRegExpValidator( TQRegExp(
"([a-zA-Z]|\\d|-)+" ), mTitle ) );
58 label->setBuddy( mTitle );
59 layout->addWidget( mTitle, 0, 1 );
61 label =
new TQLabel( i18n(
"Type:" ), page );
62 layout->addWidget( label, 1, 0 );
64 mType =
new KComboBox( page );
65 label->setBuddy( mType );
66 layout->addWidget( mType, 1, 1 );
68 mGlobal =
new TQCheckBox( i18n(
"Is available for all contacts" ), page );
69 mGlobal->setChecked(
true );
70 layout->addMultiCellWidget( mGlobal, 2, 2, 0, 1 );
72 connect( mTitle, TQ_SIGNAL( textChanged(
const TQString& ) ),
73 this, TQ_SLOT( nameChanged(
const TQString& ) ) );
75 TDEAcceleratorManager::manage(
this );
77 mTypeList.append(
"text" );
78 mTypeName.append( i18n(
"Text" ) );
79 mTypeList.append(
"integer" );
80 mTypeName.append( i18n(
"Numeric Value" ) );
81 mTypeList.append(
"boolean" );
82 mTypeName.append( i18n(
"Boolean" ) );
83 mTypeList.append(
"date" );
84 mTypeName.append( i18n(
"Date" ) );
85 mTypeList.append(
"time" );
86 mTypeName.append( i18n(
"Time" ) );
87 mTypeList.append(
"datetime" );
88 mTypeName.append( i18n(
"Date & Time" ) );
90 for ( uint i = 0; i < mTypeName.count(); ++i )
91 mType->insertItem( mTypeName[ i ] );
98 TQString AddFieldDialog::title()
const
100 return mTitle->text();
103 TQString AddFieldDialog::identifier()
const
105 TQString
id = mTitle->text().lower();
106 return id.replace(
",",
"_" ).replace(
" ",
"_" );
109 TQString AddFieldDialog::type()
const
111 return mTypeList[ mType->currentItem() ];
114 bool AddFieldDialog::isGlobal()
const
116 return mGlobal->isChecked();
119 void AddFieldDialog::nameChanged(
const TQString &name )
121 enableButton( Ok, !name.isEmpty() );
124 FieldWidget::FieldWidget( TQWidget *parent,
const char *name )
125 : TQWidget( parent, name )
127 TQVBoxLayout *layout =
new TQVBoxLayout(
this, KDialog::marginHint(),
128 KDialog::spacingHint() );
130 mGlobalLayout =
new TQVBoxLayout( layout, KDialog::spacingHint() );
131 mGlobalLayout->setAlignment( TQt::AlignTop );
133 mSeparator =
new TQFrame(
this );
134 mSeparator->setFrameStyle( TQFrame::HLine | TQFrame::Sunken );
136 layout->addWidget( mSeparator );
138 mLocalLayout =
new TQVBoxLayout( layout, KDialog::spacingHint() );
139 mLocalLayout->setAlignment( TQt::AlignTop );
142 void FieldWidget::addField(
const TQString &identifier,
const TQString &title,
143 const TQString &type,
bool isGlobal )
147 record.mIdentifier = identifier;
148 record.mTitle = title;
149 record.mLabel =
new TQLabel( title +
":",
this );
150 record.mGlobal = isGlobal;
151 if ( type ==
"integer" ) {
152 TQSpinBox *wdg =
new TQSpinBox( 0, 1000, 1,
this );
153 record.mWidget = wdg;
154 connect( wdg, TQ_SIGNAL( valueChanged(
int ) ),
155 this, TQ_SIGNAL( changed() ) );
156 }
else if ( type ==
"boolean" ) {
157 TQCheckBox *wdg =
new TQCheckBox(
this );
158 record.mWidget = wdg;
159 connect( wdg, TQ_SIGNAL( toggled(
bool ) ),
160 this, TQ_SIGNAL( changed() ) );
161 }
else if ( type ==
"date" ) {
162 TQDateEdit *wdg =
new TQDateEdit(
this );
163 record.mWidget = wdg;
164 connect( wdg, TQ_SIGNAL( valueChanged(
const TQDate& ) ),
165 this, TQ_SIGNAL( changed() ) );
166 }
else if ( type ==
"time" ) {
167 TQTimeEdit *wdg =
new TQTimeEdit(
this );
168 record.mWidget = wdg;
169 connect( wdg, TQ_SIGNAL( valueChanged(
const TQTime& ) ),
170 this, TQ_SIGNAL( changed() ) );
171 }
else if ( type ==
"datetime" ) {
172 TQDateTimeEdit *wdg =
new TQDateTimeEdit(
this );
173 record.mWidget = wdg;
174 connect( wdg, TQ_SIGNAL( valueChanged(
const TQDateTime& ) ),
175 this, TQ_SIGNAL( changed() ) );
176 }
else if ( type ==
"text" ) {
177 TQLineEdit *wdg =
new TQLineEdit(
this );
178 record.mWidget = wdg;
179 connect( wdg, TQ_SIGNAL( textChanged(
const TQString& ) ),
180 this, TQ_SIGNAL( changed() ) );
183 record.mLabel->show();
184 record.mWidget->show();
187 record.mLayout =
new TQHBoxLayout( mGlobalLayout );
188 record.mLayout->addWidget( record.mLabel );
189 record.mLayout->addWidget( record.mWidget, TQt::AlignLeft );
191 record.mLayout =
new TQHBoxLayout( mLocalLayout );
192 record.mLayout->addWidget( record.mLabel );
193 record.mLayout->addWidget( record.mWidget, TQt::AlignLeft );
197 mFieldList.append( record );
202 void FieldWidget::removeField(
const TQString &identifier )
204 FieldRecordList::Iterator it;
205 for ( it = mFieldList.begin(); it != mFieldList.end(); ++it ) {
206 if ( (*it).mIdentifier == identifier ) {
208 delete (*it).mWidget;
209 delete (*it).mLayout;
211 mFieldList.remove( it );
214 bool hasLocal =
false;
215 for ( it = mFieldList.begin(); it != mFieldList.end(); ++it )
216 hasLocal = hasLocal || !(*it).mGlobal;
226 void FieldWidget::clearFields()
228 FieldRecordList::ConstIterator fieldIt;
229 for ( fieldIt = mFieldList.begin(); fieldIt != mFieldList.end(); ++fieldIt ) {
230 if ( (*fieldIt).mWidget->isA(
"TQLineEdit" ) ) {
231 TQLineEdit *wdg =
static_cast<TQLineEdit*
>( (*fieldIt).mWidget );
232 wdg->setText( TQString() );
233 }
else if ( (*fieldIt).mWidget->isA(
"TQSpinBox" ) ) {
234 TQSpinBox *wdg =
static_cast<TQSpinBox*
>( (*fieldIt).mWidget );
236 }
else if ( (*fieldIt).mWidget->isA(
"TQCheckBox" ) ) {
237 TQCheckBox *wdg =
static_cast<TQCheckBox*
>( (*fieldIt).mWidget );
238 wdg->setChecked(
true );
239 }
else if ( (*fieldIt).mWidget->isA(
"TQDateEdit" ) ) {
240 TQDateEdit *wdg =
static_cast<TQDateEdit*
>( (*fieldIt).mWidget );
241 wdg->setDate( TQDate::currentDate() );
242 }
else if ( (*fieldIt).mWidget->isA(
"TQTimeEdit" ) ) {
243 TQTimeEdit *wdg =
static_cast<TQTimeEdit*
>( (*fieldIt).mWidget );
244 wdg->setTime( TQTime::currentTime() );
245 }
else if ( (*fieldIt).mWidget->isA(
"TQDateTimeEdit" ) ) {
246 TQDateTimeEdit *wdg =
static_cast<TQDateTimeEdit*
>( (*fieldIt).mWidget );
247 wdg->setDateTime( TQDateTime::currentDateTime() );
252 void FieldWidget::loadContact( TDEABC::Addressee *addr )
254 const TQStringList customs = addr->customs();
258 TQStringList::ConstIterator it;
259 for ( it = customs.begin(); it != customs.end(); ++it ) {
260 TQString app, name, value;
261 splitField( *it, app, name, value );
262 if ( app !=
"KADDRESSBOOK" )
265 FieldRecordList::ConstIterator fieldIt;
266 for ( fieldIt = mFieldList.begin(); fieldIt != mFieldList.end(); ++fieldIt ) {
267 if ( (*fieldIt).mIdentifier == name ) {
268 if ( (*fieldIt).mWidget->isA(
"TQLineEdit" ) ) {
269 TQLineEdit *wdg =
static_cast<TQLineEdit*
>( (*fieldIt).mWidget );
270 wdg->setText( value );
271 }
else if ( (*fieldIt).mWidget->isA(
"TQSpinBox" ) ) {
272 TQSpinBox *wdg =
static_cast<TQSpinBox*
>( (*fieldIt).mWidget );
273 wdg->setValue( value.toInt() );
274 }
else if ( (*fieldIt).mWidget->isA(
"TQCheckBox" ) ) {
275 TQCheckBox *wdg =
static_cast<TQCheckBox*
>( (*fieldIt).mWidget );
276 wdg->setChecked( value ==
"true" || value ==
"1" );
277 }
else if ( (*fieldIt).mWidget->isA(
"TQDateEdit" ) ) {
278 TQDateEdit *wdg =
static_cast<TQDateEdit*
>( (*fieldIt).mWidget );
279 wdg->setDate( TQDate::fromString( value, TQt::ISODate ) );
280 }
else if ( (*fieldIt).mWidget->isA(
"TQTimeEdit" ) ) {
281 TQTimeEdit *wdg =
static_cast<TQTimeEdit*
>( (*fieldIt).mWidget );
282 wdg->setTime( TQTime::fromString( value, TQt::ISODate ) );
283 }
else if ( (*fieldIt).mWidget->isA(
"TQDateTimeEdit" ) ) {
284 TQDateTimeEdit *wdg =
static_cast<TQDateTimeEdit*
>( (*fieldIt).mWidget );
285 wdg->setDateTime( TQDateTime::fromString( value, TQt::ISODate ) );
292 void FieldWidget::setReadOnly(
bool readOnly )
294 FieldRecordList::ConstIterator it;
295 for ( it = mFieldList.begin(); it != mFieldList.end(); ++it ) {
297 if ( (*it).mWidget->isA(
"TQLineEdit" ) ) {
298 TQLineEdit *wdg =
static_cast<TQLineEdit*
>( (*it).mWidget );
299 wdg->setReadOnly(readOnly);
300 }
else if ( (*it).mWidget->isA(
"TQSpinBox" ) ) {
301 TQSpinBox *wdg =
static_cast<TQSpinBox*
>( (*it).mWidget );
302 wdg->setEnabled( !readOnly );
303 }
else if ( (*it).mWidget->isA(
"TQCheckBox" ) ) {
304 TQCheckBox *wdg =
static_cast<TQCheckBox*
>( (*it).mWidget );
305 wdg->setEnabled( !readOnly );
306 }
else if ( (*it).mWidget->isA(
"TQDateEdit" ) ) {
307 TQDateEdit *wdg =
static_cast<TQDateEdit*
>( (*it).mWidget );
308 wdg->setEnabled( !readOnly );
309 }
else if ( (*it).mWidget->isA(
"TQTimeEdit" ) ) {
310 TQTimeEdit *wdg =
static_cast<TQTimeEdit*
>( (*it).mWidget );
311 wdg->setEnabled( !readOnly );
312 }
else if ( (*it).mWidget->isA(
"TQDateTimeEdit" ) ) {
313 TQDateTimeEdit *wdg =
static_cast<TQDateTimeEdit*
>( (*it).mWidget );
314 wdg->setEnabled( !readOnly );
319 void FieldWidget::storeContact( TDEABC::Addressee *addr )
321 FieldRecordList::ConstIterator it;
322 for ( it = mFieldList.begin(); it != mFieldList.end(); ++it ) {
324 if ( (*it).mWidget->isA(
"TQLineEdit" ) ) {
325 TQLineEdit *wdg =
static_cast<TQLineEdit*
>( (*it).mWidget );
327 }
else if ( (*it).mWidget->isA(
"TQSpinBox" ) ) {
328 TQSpinBox *wdg =
static_cast<TQSpinBox*
>( (*it).mWidget );
329 value = TQString::number( wdg->value() );
330 }
else if ( (*it).mWidget->isA(
"TQCheckBox" ) ) {
331 TQCheckBox *wdg =
static_cast<TQCheckBox*
>( (*it).mWidget );
332 value = ( wdg->isChecked() ?
"true" :
"false" );
333 }
else if ( (*it).mWidget->isA(
"TQDateEdit" ) ) {
334 TQDateEdit *wdg =
static_cast<TQDateEdit*
>( (*it).mWidget );
335 value = wdg->date().toString( TQt::ISODate );
336 }
else if ( (*it).mWidget->isA(
"TQTimeEdit" ) ) {
337 TQTimeEdit *wdg =
static_cast<TQTimeEdit*
>( (*it).mWidget );
338 value = wdg->time().toString( TQt::ISODate );
339 }
else if ( (*it).mWidget->isA(
"TQDateTimeEdit" ) ) {
340 TQDateTimeEdit *wdg =
static_cast<TQDateTimeEdit*
>( (*it).mWidget );
341 value = wdg->dateTime().toString( TQt::ISODate );
344 if ( value.isEmpty() )
345 addr->removeCustom(
"KADDRESSBOOK", (*it).mIdentifier );
347 addr->insertCustom(
"KADDRESSBOOK", (*it).mIdentifier, value );
351 void FieldWidget::removeLocalFields()
353 FieldRecordList::Iterator it;
354 for ( it = mFieldList.begin(); it != mFieldList.end(); ++it ) {
355 if ( !(*it).mGlobal ) {
357 delete (*it).mWidget;
358 delete (*it).mLayout;
360 it = mFieldList.remove( it );
367 void FieldWidget::recalculateLayout()
371 FieldRecordList::ConstIterator it;
372 for ( it = mFieldList.begin(); it != mFieldList.end(); ++it )
373 maxWidth = TQMAX( maxWidth, (*it).mLabel->minimumSizeHint().width() );
375 for ( it = mFieldList.begin(); it != mFieldList.end(); ++it )
376 (*it).mLabel->setMinimumWidth( maxWidth );
379 CustomFieldsWidget::CustomFieldsWidget( TDEABC::AddressBook *ab,
380 TQWidget *parent,
const char *name )
381 : KAB::ContactEditorWidget( ab, parent, name )
385 connect( mAddButton, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( addField() ) );
386 connect( mRemoveButton, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( removeField() ) );
388 connect( mFieldWidget, TQ_SIGNAL( changed() ),
this, TQ_SLOT( setModified() ) );
391 void CustomFieldsWidget::loadContact( TDEABC::Addressee *addr )
395 mFieldWidget->removeLocalFields();
397 AddresseeConfig addrConfig( mAddressee );
398 TQStringList fields = addrConfig.customFields();
400 if ( !fields.isEmpty() ) {
401 for ( uint i = 0; i < fields.count(); i += 3 ) {
402 mFieldWidget->addField( fields[ i ], fields[ i + 1 ],
403 fields[ i + 2 ] ,
false );
404 mRemoveButton->setEnabled(
true );
408 mFieldWidget->loadContact( addr );
411 void CustomFieldsWidget::storeContact( TDEABC::Addressee *addr )
413 mFieldWidget->storeContact( addr );
416 void CustomFieldsWidget::setReadOnly(
bool readOnly )
418 mAddButton->setEnabled( !readOnly );
419 mRemoveButton->setEnabled( !readOnly && !mFieldWidget->fields().isEmpty() );
420 mFieldWidget->setReadOnly( readOnly );
423 void CustomFieldsWidget::addField()
425 AddFieldDialog dlg(
this );
428 FieldRecordList list = mFieldWidget->fields();
430 FieldRecordList::ConstIterator it;
431 for ( it = list.begin(); it != list.end(); ++it )
432 if ( (*it).mIdentifier == dlg.identifier() ) {
433 KMessageBox::sorry(
this, i18n(
"A field with the same name already exists, please choose another one." ) );
437 mFieldWidget->addField( dlg.identifier(), dlg.title(),
438 dlg.type(), dlg.isGlobal() );
440 if ( dlg.isGlobal() ) {
441 KABPrefs::instance()->setGlobalCustomFields( marshallFields(
true ) );
443 AddresseeConfig addrConfig( mAddressee );
444 addrConfig.setCustomFields( marshallFields(
false ) );
447 mRemoveButton->setEnabled(
true );
451 void CustomFieldsWidget::removeField()
453 const FieldRecordList list = mFieldWidget->fields();
457 FieldRecordList::ConstIterator it;
458 for ( it = list.begin(); it != list.end(); ++it )
459 fields.append( (*it).mTitle );
462 TQString title = KInputDialog::getItem( i18n(
"Remove Field" ),
463 i18n(
"Select the field you want to remove:" ),
464 fields, 0,
false, &ok,
this );
467 for ( it = list.begin(); it != list.end(); ++it )
468 if ( (*it).mTitle == title ) {
469 mFieldWidget->removeField( (*it).mIdentifier );
471 if ( list.count() == 1 )
472 mRemoveButton->setEnabled(
false );
474 if ( (*it).mGlobal ) {
475 KABPrefs::instance()->setGlobalCustomFields( marshallFields(
true ) );
477 AddresseeConfig addrConfig( mAddressee );
478 addrConfig.setCustomFields( marshallFields(
false ) );
486 void CustomFieldsWidget::initGUI()
488 TQGridLayout *layout =
new TQGridLayout(
this, 2, 3, KDialog::marginHint(),
489 KDialog::spacingHint() );
491 mFieldWidget =
new FieldWidget(
this );
492 layout->addMultiCellWidget( mFieldWidget, 0, 0, 0, 2 );
494 mAddButton =
new TQPushButton( i18n(
"Add Field..." ),
this );
495 layout->addWidget( mAddButton, 1, 1, TQt::AlignRight );
497 mRemoveButton =
new TQPushButton( i18n(
"Remove Field..." ),
this );
498 mRemoveButton->setEnabled(
false );
499 layout->addWidget( mRemoveButton, 1, 2, TQt::AlignRight );
502 TQStringList globalFields = KABPrefs::instance()->globalCustomFields();
504 if ( globalFields.isEmpty() )
507 for ( uint i = 0; i < globalFields.count(); i += 3 ) {
508 mFieldWidget->addField( globalFields[ i ], globalFields[ i + 1 ],
509 globalFields[ i + 2 ] ,
true );
510 mRemoveButton->setEnabled(
true );
514 TQStringList CustomFieldsWidget::marshallFields(
bool global )
const
518 const FieldRecordList list = mFieldWidget->fields();
519 FieldRecordList::ConstIterator it;
520 for ( it = list.begin(); it != list.end(); ++it ) {
521 if ( (*it).mGlobal == global ) {
522 retval.append( (*it).mIdentifier );
523 retval.append( (*it).mTitle );
525 TQString type =
"text";
526 if ( (*it).mWidget->isA(
"TQSpinBox" ) ) {
528 }
else if ( (*it).mWidget->isA(
"TQCheckBox" ) ) {
530 }
else if ( (*it).mWidget->isA(
"TQDateEdit" ) ) {
532 }
else if ( (*it).mWidget->isA(
"TQTimeEdit" ) ) {
534 }
else if ( (*it).mWidget->isA(
"TQDateTimeEdit" ) ) {
536 }
else if ( (*it).mWidget->isA(
"TQLineEdit" ) ) {
540 retval.append( type );
548 void splitField(
const TQString &str, TQString &app, TQString &name, TQString &value )
550 int colon = str.find(
':' );
552 TQString tmp = str.left( colon );
553 value = str.mid( colon + 1 );
555 int dash = tmp.find(
'-' );
557 app = tmp.left( dash );
558 name = tmp.mid( dash + 1 );
563 #include "customfieldswidget.moc"