kaddressbook

addresseewidget.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
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 <tqcstring.h>
25 #include <tqgroupbox.h>
26 #include <tqlabel.h>
27 #include <tqlayout.h>
28 #include <tqlistbox.h>
29 #include <tqpushbutton.h>
30 
31 #include <dcopclient.h>
32 
33 #include <kbuttonbox.h>
34 #include <kcombobox.h>
35 #include <tdeconfig.h>
36 #include <kdialog.h>
37 #include <kinputdialog.h>
38 #include <tdelocale.h>
39 #include <klineedit.h>
40 
41 #include "addresseewidget.h"
42 
43 NamePartWidget::NamePartWidget( const TQString &title, const TQString &label,
44  TQWidget *parent, const char *name )
45  : TQWidget( parent, name ), mTitle( title ), mLabel( label )
46 {
47  TQHBoxLayout *layout = new TQHBoxLayout( this );
48 
49  TQGroupBox *group = new TQGroupBox( 0, TQt::Vertical, title, this );
50  TQGridLayout *groupLayout = new TQGridLayout( group->layout(), 2, 2,
51  KDialog::spacingHint() );
52 
53  mBox = new TQListBox( group );
54  connect( mBox, TQ_SIGNAL( selectionChanged( TQListBoxItem* ) ),
55  TQ_SLOT( selectionChanged( TQListBoxItem* ) ) );
56  groupLayout->addWidget( mBox, 0, 0 );
57 
58  KButtonBox *bbox = new KButtonBox( group, TQt::Vertical );
59  mAddButton = bbox->addButton( i18n( "Add..." ), this, TQ_SLOT( add() ) );
60  mEditButton = bbox->addButton( i18n( "Edit..." ), this, TQ_SLOT( edit() ) );
61  mEditButton->setEnabled( false );
62  mRemoveButton = bbox->addButton( i18n( "Remove" ), this, TQ_SLOT( remove() ) );
63  mRemoveButton->setEnabled( false );
64  bbox->layout();
65  groupLayout->addWidget( bbox, 0, 1 );
66 
67  layout->addWidget( group );
68 }
69 
70 NamePartWidget::~NamePartWidget()
71 {
72 }
73 
74 void NamePartWidget::setNameParts( const TQStringList &list )
75 {
76  mBox->clear();
77  mBox->insertStringList( list );
78 }
79 
80 TQStringList NamePartWidget::nameParts() const
81 {
82  TQStringList parts;
83  for ( uint i = 0; i < mBox->count(); ++i )
84  parts.append( mBox->text( i ) );
85 
86  return parts;
87 }
88 
89 void NamePartWidget::add()
90 {
91  bool ok;
92 
93  TQString namePart = KInputDialog::getText( i18n( "New" ), mLabel,
94  TQString(), &ok );
95  if ( ok && !namePart.isEmpty() ) {
96  mBox->insertItem( namePart );
97  emit modified();
98  }
99 }
100 
101 void NamePartWidget::edit()
102 {
103  bool ok;
104 
105  int index = mBox->currentItem();
106  if ( index == -1 )
107  return;
108 
109  TQString namePart = KInputDialog::getText( i18n( "Edit" ), mLabel,
110  mBox->text( index ), &ok );
111  if ( ok && !namePart.isEmpty() ) {
112  mBox->changeItem( namePart, index );
113  emit modified();
114  }
115 }
116 
117 void NamePartWidget::remove()
118 {
119  mBox->removeItem( mBox->currentItem() );
120  if ( mBox->count() == 0 )
121  selectionChanged( 0 );
122 
123  emit modified();
124 }
125 
126 void NamePartWidget::selectionChanged( TQListBoxItem *item )
127 {
128  mEditButton->setEnabled( item != 0 );
129  mRemoveButton->setEnabled( item != 0 );
130 }
131 
132 
133 
134 AddresseeWidget::AddresseeWidget( TQWidget *parent, const char *name )
135  : TQWidget( parent, name )
136 {
137  TQGridLayout *layout = new TQGridLayout( this, 2, 3, KDialog::marginHint(),
138  KDialog::spacingHint() );
139 
140  mPrefix = new NamePartWidget( i18n( "Prefixes"), i18n( "Enter prefix:" ), this );
141  layout->addWidget( mPrefix, 0, 0 );
142 
143  mInclusion = new NamePartWidget( i18n( "Inclusions"), i18n( "Enter inclusion:" ), this );
144  layout->addWidget( mInclusion, 0, 1 );
145 
146  mSuffix = new NamePartWidget( i18n( "Suffixes" ), i18n( "Enter suffix:" ), this );
147  layout->addWidget( mSuffix, 0, 2 );
148 
149  TQLabel *label = new TQLabel( i18n( "Default formatted name:" ), this );
150  layout->addWidget( label, 1, 0 );
151 
152  mFormattedNameCombo = new KComboBox( this );
153  mFormattedNameCombo->insertItem( i18n( "Empty" ) );
154  mFormattedNameCombo->insertItem( i18n( "Simple Name" ) );
155  mFormattedNameCombo->insertItem( i18n( "Full Name" ) );
156  mFormattedNameCombo->insertItem( i18n( "Reverse Name with Comma" ) );
157  mFormattedNameCombo->insertItem( i18n( "Reverse Name" ) );
158  layout->addMultiCellWidget( mFormattedNameCombo, 1, 1, 1, 2 );
159 
160  connect( mPrefix, TQ_SIGNAL( modified() ), TQ_SIGNAL( modified() ) );
161  connect( mInclusion, TQ_SIGNAL( modified() ), TQ_SIGNAL( modified() ) );
162  connect( mSuffix, TQ_SIGNAL( modified() ), TQ_SIGNAL( modified() ) );
163  connect( mFormattedNameCombo, TQ_SIGNAL( activated( int ) ), TQ_SIGNAL( modified() ) );
164 }
165 
166 AddresseeWidget::~AddresseeWidget()
167 {
168 }
169 
170 void AddresseeWidget::restoreSettings()
171 {
172  TDEConfig config( "tdeabcrc" );
173  config.setGroup( "General" );
174 
175  mPrefix->setNameParts( config.readListEntry( "Prefixes" ) );
176  mInclusion->setNameParts( config.readListEntry( "Inclusions" ) );
177  mSuffix->setNameParts( config.readListEntry( "Suffixes" ) );
178 
179  TDEConfig cfg( "kaddressbookrc" );
180  cfg.setGroup( "General" );
181  mFormattedNameCombo->setCurrentItem( cfg.readNumEntry( "FormattedNameType", 1 ) );
182 }
183 
184 void AddresseeWidget::saveSettings()
185 {
186  TDEConfig config( "tdeabcrc" );
187  config.setGroup( "General" );
188 
189  config.writeEntry( "Prefixes", mPrefix->nameParts() );
190  config.writeEntry( "Inclusions", mInclusion->nameParts() );
191  config.writeEntry( "Suffixes", mSuffix->nameParts() );
192 
193  TDEConfig cfg( "kaddressbookrc" );
194  cfg.setGroup( "General" );
195  cfg.writeEntry( "FormattedNameType", mFormattedNameCombo->currentItem() );
196 
197  DCOPClient *client = DCOPClient::mainClient();
198  if ( client )
199  client->emitDCOPSignal( "TDEABC::AddressBookConfig", "changed()", TQByteArray() );
200 }
201 
202 #include "addresseewidget.moc"