author | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
commit | b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff) | |
tree | 2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /kabc/distributionlisteditor.cpp | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
Diffstat (limited to 'kabc/distributionlisteditor.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kabc/distributionlisteditor.cpp | 335 |
1 files changed, 335 insertions, 0 deletions
diff --git a/kabc/distributionlisteditor.cpp b/kabc/distributionlisteditor.cpp new file mode 100644 index 0000000..569dc96 --- a/dev/null +++ b/kabc/distributionlisteditor.cpp | |||
@@ -0,0 +1,335 @@ | |||
1 | /* | ||
2 | This file is part of libkabc. | ||
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library 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 GNU | ||
13 | Library General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | #include <qlistview.h> | ||
22 | #include <qlayout.h> | ||
23 | #include <qpushbutton.h> | ||
24 | #include <qcombobox.h> | ||
25 | #include <qinputdialog.h> | ||
26 | #include <qbuttongroup.h> | ||
27 | #include <qradiobutton.h> | ||
28 | |||
29 | #include <klocale.h> | ||
30 | #include <kdebug.h> | ||
31 | |||
32 | #include "addressbook.h" | ||
33 | #include "addresseedialog.h" | ||
34 | #include "distributionlist.h" | ||
35 | |||
36 | #include "distributionlisteditor.h" | ||
37 | |||
38 | #ifndef KAB_EMBEDDED | ||
39 | #include "distributionlisteditor.moc" | ||
40 | #endif //KAB_EMBEDDED | ||
41 | |||
42 | using namespace KABC; | ||
43 | |||
44 | EmailSelectDialog::EmailSelectDialog( const QStringList &emails, const QString ¤t, | ||
45 | QWidget *parent ) : | ||
46 | KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok, | ||
47 | parent ) | ||
48 | { | ||
49 | QFrame *topFrame = plainPage(); | ||
50 | QBoxLayout *topLayout = new QVBoxLayout( topFrame ); | ||
51 | |||
52 | mButtonGroup = new QButtonGroup( 1, Horizontal, i18n("Email Addresses"), | ||
53 | topFrame ); | ||
54 | topLayout->addWidget( mButtonGroup ); | ||
55 | |||
56 | QStringList::ConstIterator it; | ||
57 | for( it = emails.begin(); it != emails.end(); ++it ) { | ||
58 | QRadioButton *button = new QRadioButton( *it, mButtonGroup ); | ||
59 | if ( (*it) == current ) { | ||
60 | button->setDown( true ); | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | QString EmailSelectDialog::selected() | ||
66 | { | ||
67 | QButton *button = mButtonGroup->selected(); | ||
68 | if ( button ) return button->text(); | ||
69 | return QString::null; | ||
70 | } | ||
71 | |||
72 | QString EmailSelectDialog::getEmail( const QStringList &emails, const QString ¤t, | ||
73 | QWidget *parent ) | ||
74 | { | ||
75 | EmailSelectDialog *dlg = new EmailSelectDialog( emails, current, parent ); | ||
76 | dlg->exec(); | ||
77 | |||
78 | QString result = dlg->selected(); | ||
79 | |||
80 | delete dlg; | ||
81 | |||
82 | return result; | ||
83 | } | ||
84 | |||
85 | class EditEntryItem : public QListViewItem | ||
86 | { | ||
87 | public: | ||
88 | EditEntryItem( QListView *parent, const Addressee &addressee, | ||
89 | const QString &email=QString::null ) : | ||
90 | QListViewItem( parent ), | ||
91 | mAddressee( addressee ), | ||
92 | mEmail( email ) | ||
93 | { | ||
94 | setText( 0, addressee.realName() ); | ||
95 | if( email.isEmpty() ) { | ||
96 | setText( 1, addressee.preferredEmail() ); | ||
97 | setText( 2, i18n("Yes") ); | ||
98 | } else { | ||
99 | setText( 1, email ); | ||
100 | setText( 2, i18n("No") ); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | Addressee addressee() const | ||
105 | { | ||
106 | return mAddressee; | ||
107 | } | ||
108 | |||
109 | QString email() const | ||
110 | { | ||
111 | return mEmail; | ||
112 | } | ||
113 | |||
114 | private: | ||
115 | Addressee mAddressee; | ||
116 | QString mEmail; | ||
117 | }; | ||
118 | |||
119 | DistributionListEditor::DistributionListEditor( AddressBook *addressBook, QWidget *parent) : | ||
120 | QWidget( parent ), | ||
121 | mAddressBook( addressBook ) | ||
122 | { | ||
123 | kdDebug(5700) << "DistributionListEditor()" << endl; | ||
124 | |||
125 | QBoxLayout *topLayout = new QVBoxLayout( this ); | ||
126 | topLayout->setMargin( KDialog::marginHint() ); | ||
127 | topLayout->setSpacing( KDialog::spacingHint() ); | ||
128 | |||
129 | QBoxLayout *nameLayout = new QHBoxLayout( topLayout) ; | ||
130 | |||
131 | mNameCombo = new QComboBox( this ); | ||
132 | nameLayout->addWidget( mNameCombo ); | ||
133 | connect( mNameCombo, SIGNAL( activated( int ) ), SLOT( updateEntryView() ) ); | ||
134 | |||
135 | newButton = new QPushButton( i18n("New List"), this ); | ||
136 | nameLayout->addWidget( newButton ); | ||
137 | connect( newButton, SIGNAL( clicked() ), SLOT( newList() ) ); | ||
138 | |||
139 | removeButton = new QPushButton( i18n("Remove List"), this ); | ||
140 | nameLayout->addWidget( removeButton ); | ||
141 | connect( removeButton, SIGNAL( clicked() ), SLOT( removeList() ) ); | ||
142 | |||
143 | mEntryView = new QListView( this ); | ||
144 | mEntryView->addColumn( i18n("Name") ); | ||
145 | mEntryView->addColumn( i18n("Email") ); | ||
146 | mEntryView->addColumn( i18n("Use Preferred") ); | ||
147 | topLayout->addWidget( mEntryView ); | ||
148 | connect(mEntryView,SIGNAL(selectionChanged ()),this, SLOT(slotSelectionEntryViewChanged())); | ||
149 | |||
150 | changeEmailButton = new QPushButton( i18n("Change Email"), this ); | ||
151 | topLayout->addWidget( changeEmailButton ); | ||
152 | connect( changeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) ); | ||
153 | |||
154 | removeEntryButton = new QPushButton( i18n("Remove Entry"), this ); | ||
155 | topLayout->addWidget( removeEntryButton ); | ||
156 | connect( removeEntryButton, SIGNAL( clicked() ), SLOT( removeEntry() ) ); | ||
157 | |||
158 | addEntryButton = new QPushButton( i18n("Add Entry"), this ); | ||
159 | topLayout->addWidget( addEntryButton ); | ||
160 | connect( addEntryButton, SIGNAL( clicked() ), SLOT( addEntry() ) ); | ||
161 | |||
162 | mAddresseeView = new QListView( this ); | ||
163 | mAddresseeView->addColumn( i18n("Name") ); | ||
164 | mAddresseeView->addColumn( i18n("Preferred Email") ); | ||
165 | topLayout->addWidget( mAddresseeView ); | ||
166 | |||
167 | |||
168 | connect(mAddresseeView,SIGNAL(selectionChanged ()),this, SLOT(slotSelectionAddresseeViewChanged())); | ||
169 | |||
170 | mManager = new DistributionListManager( mAddressBook ); | ||
171 | mManager->load(); | ||
172 | |||
173 | updateAddresseeView(); | ||
174 | updateNameCombo(); | ||
175 | removeButton->setEnabled(!mManager->listNames().isEmpty()); | ||
176 | } | ||
177 | |||
178 | DistributionListEditor::~DistributionListEditor() | ||
179 | { | ||
180 | kdDebug(5700) << "~DistributionListEditor()" << endl; | ||
181 | |||
182 | mManager->save(); | ||
183 | delete mManager; | ||
184 | } | ||
185 | |||
186 | void DistributionListEditor::slotSelectionEntryViewChanged() | ||
187 | { | ||
188 | #ifndef KAB_EMBEDDED | ||
189 | EditEntryItem *entryItem = dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() ); | ||
190 | #else //KAB_EMBEDDED | ||
191 | EditEntryItem *entryItem = (EditEntryItem *)( mEntryView->selectedItem() ); | ||
192 | #endif //KAB_EMBEDDED | ||
193 | bool state = (entryItem != 0L); | ||
194 | |||
195 | changeEmailButton->setEnabled(state); | ||
196 | removeEntryButton->setEnabled(state); | ||
197 | } | ||
198 | |||
199 | void DistributionListEditor::newList() | ||
200 | { | ||
201 | bool ok = false; | ||
202 | QString name = QInputDialog::getText( i18n("New Distribution List"), | ||
203 | i18n("Please enter name:"), | ||
204 | QLineEdit::Normal, QString::null, &ok, | ||
205 | this ); | ||
206 | if ( !ok || name.isEmpty() ) return; | ||
207 | |||
208 | new DistributionList( mManager, name ); | ||
209 | |||
210 | mNameCombo->insertItem( name ); | ||
211 | removeButton->setEnabled(true); | ||
212 | updateEntryView(); | ||
213 | } | ||
214 | |||
215 | void DistributionListEditor::removeList() | ||
216 | { | ||
217 | delete mManager->list( mNameCombo->currentText() ); | ||
218 | mNameCombo->removeItem( mNameCombo->currentItem() ); | ||
219 | removeButton->setEnabled(!mManager->listNames().isEmpty()); | ||
220 | addEntryButton->setEnabled( !mNameCombo->currentText().isEmpty()); | ||
221 | updateEntryView(); | ||
222 | } | ||
223 | |||
224 | void DistributionListEditor::addEntry() | ||
225 | { | ||
226 | #ifndef KAB_EMBEDDED | ||
227 | AddresseeItem *addresseeItem = | ||
228 | dynamic_cast<AddresseeItem *>( mAddresseeView->selectedItem() ); | ||
229 | #else //KAB_EMBEDDED | ||
230 | AddresseeItem *addresseeItem = | ||
231 | (AddresseeItem *)( mAddresseeView->selectedItem() ); | ||
232 | #endif //KAB_EMBEDDED | ||
233 | |||
234 | |||
235 | if( !addresseeItem ) { | ||
236 | kdDebug(5700) << "DLE::addEntry(): No addressee selected." << endl; | ||
237 | return; | ||
238 | } | ||
239 | |||
240 | DistributionList *list = mManager->list( mNameCombo->currentText() ); | ||
241 | if ( !list ) { | ||
242 | kdDebug(5700) << "DLE::addEntry(): No dist list '" << mNameCombo->currentText() << "'" << endl; | ||
243 | return; | ||
244 | } | ||
245 | |||
246 | list->insertEntry( addresseeItem->addressee() ); | ||
247 | updateEntryView(); | ||
248 | slotSelectionAddresseeViewChanged(); | ||
249 | } | ||
250 | |||
251 | void DistributionListEditor::removeEntry() | ||
252 | { | ||
253 | DistributionList *list = mManager->list( mNameCombo->currentText() ); | ||
254 | if ( !list ) return; | ||
255 | |||
256 | #ifndef KAB_EMBEDDED | ||
257 | EditEntryItem *entryItem = dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() ); | ||
258 | #else //KAB_EMBEDDED | ||
259 | EditEntryItem *entryItem = (EditEntryItem *)( mEntryView->selectedItem() ); | ||
260 | #endif //KAB_EMBEDDED | ||
261 | if ( !entryItem ) return; | ||
262 | |||
263 | list->removeEntry( entryItem->addressee(), entryItem->email() ); | ||
264 | delete entryItem; | ||
265 | } | ||
266 | |||
267 | void DistributionListEditor::changeEmail() | ||
268 | { | ||
269 | DistributionList *list = mManager->list( mNameCombo->currentText() ); | ||
270 | if ( !list ) return; | ||
271 | |||
272 | #ifndef KAB_EMBEDDED | ||
273 | EditEntryItem *entryItem = dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() ); | ||
274 | #else //KAB_EMBEDDED | ||
275 | EditEntryItem *entryItem = (EditEntryItem *)( mEntryView->selectedItem() ); | ||
276 | #endif //KAB_EMBEDDED | ||
277 | if ( !entryItem ) return; | ||
278 | |||
279 | QString email = EmailSelectDialog::getEmail( entryItem->addressee().emails(), | ||
280 | entryItem->email(), this ); | ||
281 | list->removeEntry( entryItem->addressee(), entryItem->email() ); | ||
282 | list->insertEntry( entryItem->addressee(), email ); | ||
283 | |||
284 | updateEntryView(); | ||
285 | } | ||
286 | |||
287 | void DistributionListEditor::updateEntryView() | ||
288 | { | ||
289 | DistributionList *list = mManager->list( mNameCombo->currentText() ); | ||
290 | if ( !list ) return; | ||
291 | |||
292 | mEntryView->clear(); | ||
293 | DistributionList::Entry::List entries = list->entries(); | ||
294 | DistributionList::Entry::List::ConstIterator it; | ||
295 | for( it = entries.begin(); it != entries.end(); ++it ) { | ||
296 | new EditEntryItem( mEntryView, (*it).addressee, (*it).email ); | ||
297 | } | ||
298 | #ifndef KAB_EMBEDDED | ||
299 | EditEntryItem *entryItem = dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() ); | ||
300 | #else //KAB_EMBEDDED | ||
301 | EditEntryItem *entryItem = (EditEntryItem *)( mEntryView->selectedItem() ); | ||
302 | #endif //KAB_EMBEDDED | ||
303 | bool state = (entryItem != 0L); | ||
304 | |||
305 | changeEmailButton->setEnabled(state); | ||
306 | removeEntryButton->setEnabled(state); | ||
307 | } | ||
308 | |||
309 | void DistributionListEditor::updateAddresseeView() | ||
310 | { | ||
311 | mAddresseeView->clear(); | ||
312 | |||
313 | AddressBook::Iterator it; | ||
314 | for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) { | ||
315 | new AddresseeItem( mAddresseeView, *it ); | ||
316 | } | ||
317 | } | ||
318 | |||
319 | void DistributionListEditor::updateNameCombo() | ||
320 | { | ||
321 | mNameCombo->insertStringList( mManager->listNames() ); | ||
322 | |||
323 | updateEntryView(); | ||
324 | } | ||
325 | |||
326 | void DistributionListEditor::slotSelectionAddresseeViewChanged() | ||
327 | { | ||
328 | #ifndef KAB_EMBEDDED | ||
329 | AddresseeItem *addresseeItem = dynamic_cast<AddresseeItem *>( mAddresseeView->selectedItem() ); | ||
330 | #else //KAB_EMBEDDED | ||
331 | AddresseeItem *addresseeItem = (AddresseeItem *)( mAddresseeView->selectedItem() ); | ||
332 | #endif //KAB_EMBEDDED | ||
333 | bool state = (addresseeItem != 0L); | ||
334 | addEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty()); | ||
335 | } | ||