kaddressbook

addresseewidget.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
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 <qcstring.h>
00025 #include <qgroupbox.h>
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 #include <qlistbox.h>
00029 #include <qpushbutton.h>
00030 
00031 #include <dcopclient.h>
00032 
00033 #include <kbuttonbox.h>
00034 #include <kcombobox.h>
00035 #include <kconfig.h>
00036 #include <kdialog.h>
00037 #include <kinputdialog.h>
00038 #include <klocale.h>
00039 #include <klineedit.h>
00040 
00041 #include "addresseewidget.h"
00042 
00043 NamePartWidget::NamePartWidget( const QString &title, const QString &label,
00044                                 QWidget *parent, const char *name )
00045   : QWidget( parent, name ), mTitle( title ), mLabel( label )
00046 {
00047   QHBoxLayout *layout = new QHBoxLayout( this );
00048 
00049   QGroupBox *group = new QGroupBox( 0, Qt::Vertical, title, this );
00050   QGridLayout *groupLayout = new QGridLayout( group->layout(), 2, 2,
00051                                               KDialog::spacingHint() );
00052 
00053   mBox = new QListBox( group );
00054   connect( mBox, SIGNAL( selectionChanged( QListBoxItem* ) ),
00055            SLOT( selectionChanged( QListBoxItem* ) ) );
00056   groupLayout->addWidget( mBox, 0, 0 );
00057 
00058   KButtonBox *bbox = new KButtonBox( group, Qt::Vertical );
00059   mAddButton = bbox->addButton( i18n( "Add..." ), this,  SLOT( add() ) );
00060   mEditButton = bbox->addButton( i18n( "Edit..." ), this,  SLOT( edit() ) );
00061   mEditButton->setEnabled( false );
00062   mRemoveButton = bbox->addButton( i18n( "Remove" ), this,  SLOT( remove() ) );
00063   mRemoveButton->setEnabled( false );
00064   bbox->layout();
00065   groupLayout->addWidget( bbox, 0, 1 );
00066 
00067   layout->addWidget( group );
00068 }
00069 
00070 NamePartWidget::~NamePartWidget()
00071 {
00072 }
00073 
00074 void NamePartWidget::setNameParts( const QStringList &list )
00075 {
00076   mBox->clear();
00077   mBox->insertStringList( list );
00078 }
00079 
00080 QStringList NamePartWidget::nameParts() const
00081 {
00082   QStringList parts;
00083   for ( uint i = 0; i < mBox->count(); ++i )
00084     parts.append( mBox->text( i ) );
00085 
00086   return parts;
00087 }
00088 
00089 void NamePartWidget::add()
00090 {
00091   bool ok;
00092 
00093   QString namePart = KInputDialog::getText( i18n( "New" ), mLabel,
00094                                             QString::null, &ok );
00095   if ( ok && !namePart.isEmpty() ) {
00096     mBox->insertItem( namePart );
00097     emit modified();
00098   }
00099 }
00100 
00101 void NamePartWidget::edit()
00102 {
00103   bool ok;
00104 
00105   int index = mBox->currentItem();
00106   if ( index == -1 )
00107     return;
00108 
00109   QString namePart = KInputDialog::getText( i18n( "Edit" ), mLabel,
00110                                             mBox->text( index ), &ok );
00111   if ( ok && !namePart.isEmpty() ) {
00112     mBox->changeItem( namePart, index );
00113     emit modified();
00114   }
00115 }
00116 
00117 void NamePartWidget::remove()
00118 {
00119   mBox->removeItem( mBox->currentItem() );
00120   if ( mBox->count() == 0 )
00121     selectionChanged( 0 );
00122 
00123   emit modified();
00124 }
00125 
00126 void NamePartWidget::selectionChanged( QListBoxItem *item )
00127 {
00128   mEditButton->setEnabled( item != 0 );
00129   mRemoveButton->setEnabled( item != 0 );
00130 }
00131 
00132 
00133 
00134 AddresseeWidget::AddresseeWidget( QWidget *parent, const char *name )
00135   : QWidget( parent, name )
00136 {
00137   QGridLayout *layout = new QGridLayout( this, 2, 3, KDialog::marginHint(),
00138                                          KDialog::spacingHint() );
00139 
00140   mPrefix = new NamePartWidget( i18n( "Prefixes"), i18n( "Enter prefix:" ), this );
00141   layout->addWidget( mPrefix, 0, 0 );
00142 
00143   mInclusion = new NamePartWidget( i18n( "Inclusions"), i18n( "Enter inclusion:" ), this );
00144   layout->addWidget( mInclusion, 0, 1 );
00145 
00146   mSuffix = new NamePartWidget( i18n( "Suffixes" ), i18n( "Enter suffix:" ), this );
00147   layout->addWidget( mSuffix, 0, 2 );
00148 
00149   QLabel *label = new QLabel( i18n( "Default formatted name:" ), this );
00150   layout->addWidget( label, 1, 0 );
00151 
00152   mFormattedNameCombo = new KComboBox( this );
00153   mFormattedNameCombo->insertItem( i18n( "Empty" ) );
00154   mFormattedNameCombo->insertItem( i18n( "Simple Name" ) );
00155   mFormattedNameCombo->insertItem( i18n( "Full Name" ) );
00156   mFormattedNameCombo->insertItem( i18n( "Reverse Name with Comma" ) );
00157   mFormattedNameCombo->insertItem( i18n( "Reverse Name" ) );
00158   layout->addMultiCellWidget( mFormattedNameCombo, 1, 1, 1, 2 );
00159 
00160   connect( mPrefix, SIGNAL( modified() ), SIGNAL( modified() ) );
00161   connect( mInclusion, SIGNAL( modified() ), SIGNAL( modified() ) );
00162   connect( mSuffix, SIGNAL( modified() ), SIGNAL( modified() ) );
00163   connect( mFormattedNameCombo, SIGNAL( activated( int ) ), SIGNAL( modified() ) );
00164 }
00165 
00166 AddresseeWidget::~AddresseeWidget()
00167 {
00168 }
00169 
00170 void AddresseeWidget::restoreSettings()
00171 {
00172   KConfig config( "kabcrc" );
00173   config.setGroup( "General" );
00174 
00175   mPrefix->setNameParts( config.readListEntry( "Prefixes" ) );
00176   mInclusion->setNameParts( config.readListEntry( "Inclusions" ) );
00177   mSuffix->setNameParts( config.readListEntry( "Suffixes" ) );
00178 
00179   KConfig cfg( "kaddressbookrc" );
00180   cfg.setGroup( "General" );
00181   mFormattedNameCombo->setCurrentItem( cfg.readNumEntry( "FormattedNameType", 1 ) );
00182 }
00183 
00184 void AddresseeWidget::saveSettings()
00185 {
00186   KConfig config( "kabcrc" );
00187   config.setGroup( "General" );
00188 
00189   config.writeEntry( "Prefixes", mPrefix->nameParts() );
00190   config.writeEntry( "Inclusions", mInclusion->nameParts() );
00191   config.writeEntry( "Suffixes", mSuffix->nameParts() );
00192 
00193   KConfig cfg( "kaddressbookrc" );
00194   cfg.setGroup( "General" );
00195   cfg.writeEntry( "FormattedNameType", mFormattedNameCombo->currentItem() );
00196 
00197   DCOPClient *client = DCOPClient::mainClient();
00198   if ( client )
00199       client->emitDCOPSignal( "KABC::AddressBookConfig", "changed()", QByteArray() );
00200 }
00201 
00202 #include "addresseewidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys