KWMailMergeTDEABCConfig.cpp
1/*
2 This file is part of the KDE project
3 Copyright (C) 2004 Dirk Schmidt <fs@dirk-schmidt.net>
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21
22#include <tqvbox.h>
23#include <tqlayout.h>
24#include <tqlineedit.h>
25
26#include <tdeapplication.h>
27#include <kdebug.h>
28#include <tdeglobal.h>
29#include <kinputdialog.h>
30#include <tdelocale.h>
31#include <tdemessagebox.h>
32#include <kpushbutton.h>
33#include <krun.h>
34#include <tdeabc/stdaddressbook.h>
35#include <tdeabc/distributionlist.h>
36
37#include "addresspicker.h"
38#include "KWMailMergeTDEABC.h"
39#include "KWMailMergeTDEABCConfig.h"
40
41
42KWMailMergeTDEABCConfig::KWMailMergeTDEABCConfig( TQWidget *parent, KWMailMergeTDEABC *db_)
43 :KDialogBase( Plain, i18n( "Mail Merge - Editor" ),
44 Ok | Cancel, Ok, parent, "", true)
45{
46 _db = db_;
47
48 (new TQVBoxLayout(plainPage()))->setAutoAdd(true);
49 setMainWidget( _ui=new AddressPickerUI( plainPage() ) );
50
51 updateAvailable();
52 initSelectedAddressees();
53 initSelectedLists();
54 initSlotSignalConnections();
55}
56
57
58KWMailMergeTDEABCConfig::~KWMailMergeTDEABCConfig()
59{
60 ;
61}
62
63
64void KWMailMergeTDEABCConfig::acceptSelection()
65{
66 _db->clear();
67
68 TQListViewItem* top = _ui->mSelectedView->firstChild();
69 while(top)
70 {
71 kdDebug() << "acceptSelection(): " << top->text(0) << endl;
72 if( top->text(0) == i18n("Distribution Lists") )
73 {
74 TQListViewItem* item = top->firstChild();
75 while(item)
76 {
77 kdDebug() << "acceptSelection(): " << item->text(0) << endl;
78 _db->addList( item->text(0) );
79 item = item->nextSibling();
80 }
81 }
82 else if( top->text(0) == i18n("Single Entries") )
83 {
84 TQListViewItem* item = top->firstChild();
85 while(item)
86 {
87 kdDebug() << "acceptSelection(): " << item->text(0) << endl;
88 _db->addEntry( item->text(-1) );
89 item = item->nextSibling();
90 }
91 }
92 top = top->nextSibling();
93 }
94
95}
96
97
98void KWMailMergeTDEABCConfig::addSelectedContacts()
99{
100 TQListViewItemIterator it( _ui->mAvailableView, TQListViewItemIterator::Selected );
101 TQListViewItem* selected = _ui->mSelectedView->findItem(
102 i18n("Single Entries"), 0, TQt::ExactMatch );
103 TQListViewItem* selectedLists = _ui->mSelectedView->findItem(
104 i18n("Distribution Lists"), 0, TQt::ExactMatch );
105 while ( it.current() )
106 {
107 if( it.current()->depth() > 0 )
108 {
109 TQString uid = it.current()->text( -1 );
110 kdDebug() << "addSelectedContacts(): uid :" << uid << endl;
111 if( !uid.isEmpty() )
112 {
113 KWMailMergeTDEABCConfigListItem *item =
114 static_cast<KWMailMergeTDEABCConfigListItem*> ( it.current() );
115 if( selected )
116 {
117 selected->insertItem( item );
118 selected->setOpen( true );
119 destroyAvailableClones( uid );
120 }
121 }
122 else if( it.current()->parent()->text(0) == i18n("Distribution Lists") )
123 {
124 if( selectedLists )
125 {
126 selectedLists->insertItem( it.current() );
127 selectedLists->setOpen( true );
128 }
129 }
130 }
131 ++it;
132 }
133 _ui->mSelectedView->selectAll( false );
134}
135
136
137void KWMailMergeTDEABCConfig::destroyAvailableClones( const TQString& uid )
138{
139 if( uid.isEmpty() )
140 return;
141
142 TQListViewItemIterator it( _ui->mAvailableView );
143
144 while ( it.current() )
145 {
146 if( it.current()->depth() > 0)
147 {
148 if( it.current()->text(-1)== uid )
149 {
150 delete it.current();
151 }
152 }
153 ++it;
154 }
155}
156
157
158void KWMailMergeTDEABCConfig::filterChanged( const TQString& txt )
159{
160 kdDebug() << "KWMailMergeTDEABCConfig::filterChanged( " << txt << " )" << endl;
161
162 bool showAll = txt.isEmpty();
163
164 TQListViewItem* category = _ui->mAvailableView->firstChild();
165 while(category)
166 {
167 if( category->text(0)!=i18n("Distribution Lists") )
168 {
169 TQListViewItem* item = category->firstChild();
170 while(item)
171 {
172 if(showAll)
173 {
174 item->setVisible( true );
175 }
176 else
177 {
178 item->setVisible( item->text(0).contains( txt, false ) );
179 }
180 item = item->nextSibling();
181 }
182 category->setOpen( !showAll );
183 }
184 else
185 {
186 category->setVisible( showAll );
187 }
188 category = category->nextSibling();
189 }
190}
191
192
193void KWMailMergeTDEABCConfig::initSelectedAddressees()
194{
195 TQStringList records = _db->singleRecords();
196
197 TQListViewItem* category = _ui->mAvailableView->firstChild();
198 TQListViewItem* selected = _ui->mSelectedView->findItem(
199 i18n("Single Entries"), 0, TQt::ExactMatch );
200 while ( category && (records.count()>0) )
201 {
202 if( category->text(0) != i18n("Distribution Lists") )
203 {
204 KWMailMergeTDEABCConfigListItem* item =
205 static_cast<KWMailMergeTDEABCConfigListItem*> ( category->firstChild() );
206 while( item && (records.count()>0) )
207 {
208 // Need some temporary item, because after selected->insertItem( item )
209 // the item->nextSibling() is not the one we want.
210 KWMailMergeTDEABCConfigListItem* nextItem =
211 static_cast<KWMailMergeTDEABCConfigListItem*> ( item->nextSibling() );
212
213 for( TQStringList::Iterator itRecords = records.begin();
214 itRecords != records.end(); ++itRecords )
215 {
216 TQString uid = *itRecords;
217 if( item->text(-1) == uid )
218 {
219 selected->insertItem( item );
220
221 // downsize records to speed up iterations
222 itRecords = records.remove( itRecords );
223 --itRecords;
224
225 destroyAvailableClones( uid );
226 }
227 }
228 item = nextItem;
229 }
230 }
231 category = category->nextSibling();
232 }
233}
234
235
236void KWMailMergeTDEABCConfig::initSelectedLists()
237{
238 TQStringList lists = _db->lists();
239
240 kdDebug() << "::initSelectedLists()" << lists.join(",") << endl;
241
242 TQListViewItem* l = _ui->mAvailableView->findItem(
243 i18n("Distribution Lists"), 0, TQt::ExactMatch );
244 TQListViewItem* selected = _ui->mSelectedView->findItem(
245 i18n("Distribution Lists"), 0, TQt::ExactMatch );
246
247 TQListViewItem* item = ( l->firstChild() );
248 while( item && (lists.count()>0) )
249 {
250 TQListViewItem* nextItem = item->nextSibling();
251
252 for( TQStringList::Iterator itLists = lists.begin();
253 itLists != lists.end(); ++itLists )
254 {
255 TQString id = *itLists;
256 if( item->text(0) == id )
257 {
258 selected->insertItem( item );
259 itLists = lists.remove( itLists );
260 --itLists;
261 }
262 }
263 item = nextItem;
264 }
265}
266
267
268void KWMailMergeTDEABCConfig::initSlotSignalConnections()
269{
270 connect( this, TQ_SIGNAL( okClicked() ), TQ_SLOT( acceptSelection() ) );
271 connect( _ui->mAddButton, TQ_SIGNAL( clicked() ), TQ_SLOT( addSelectedContacts() ) );
272 connect( _ui->mAddressBook, TQ_SIGNAL( clicked() ), TQ_SLOT( launchAddressbook() ) );
273
274 connect( _ui->mAvailableView, TQ_SIGNAL( doubleClicked( TQListViewItem *, const TQPoint &, int ) ),
275 TQ_SLOT( addSelectedContacts() ) );
276
277 connect( _ui->mFilterEdit, TQ_SIGNAL( textChanged(const TQString &) ),
278 TQ_SLOT( filterChanged(const TQString &) ) );
279 connect( _ui->mRemoveButton, TQ_SIGNAL( clicked() ), TQ_SLOT( removeSelectedContacts() ) );
280 connect( _ui->mSaveList, TQ_SIGNAL( clicked() ), TQ_SLOT( saveDistributionList() ) );
281 connect( _ui->mSelectedView, TQ_SIGNAL( doubleClicked( TQListViewItem *, const TQPoint &, int ) ),
282 TQ_SLOT( removeSelectedContacts() ) );
283}
284
285
286void KWMailMergeTDEABCConfig::launchAddressbook() const
287{
288 kapp->startServiceByDesktopName( "kaddressbook", TQString() );
289}
290
291
292
293void KWMailMergeTDEABCConfig::removeContact( TQListViewItem* item )
294{
295 TQStringList& categories = _usedCategories;
296 TQListViewItem* availableLists = _ui->mAvailableView->findItem(
297 i18n("Distribution Lists"), 0, TQt::ExactMatch );
298 if( item->depth() > 0 )
299 {
300 if( !item->text( -1 ).isEmpty() ) // remove selected single entry here
301 {
302 KWMailMergeTDEABCConfigListItem* rightItem =
303 static_cast<KWMailMergeTDEABCConfigListItem*> ( item );
304
305 TQStringList entryCategories = rightItem->addressee().categories();
306 for ( TQStringList::Iterator itEntryCat = entryCategories.begin();
307 itEntryCat != entryCategories.end(); ++itEntryCat )
308 {
309 int i = categories.findIndex(*itEntryCat);
310 if( i == -1 )
311 {
312 TQListViewItem* category = new TQListViewItem( _ui->mAvailableView,
313 *itEntryCat );
314 categories.append( *itEntryCat );
315
316 KWMailMergeTDEABCConfigListItem* leftItem = new KWMailMergeTDEABCConfigListItem(
317 category, rightItem->addressee() );
318 }
319 else
320 {
321 KWMailMergeTDEABCConfigListItem* leftItem = new
322 KWMailMergeTDEABCConfigListItem(
323 _ui->mAvailableView->findItem(
324 *itEntryCat, 0,
325 TQt::ExactMatch),
326 rightItem->addressee() );
327 }
328 }
329 if( entryCategories.isEmpty() )
330 {
331 TQString noCat = i18n("no category");
332 KWMailMergeTDEABCConfigListItem* leftItem = new KWMailMergeTDEABCConfigListItem(
333 _ui->mAvailableView->findItem(
334 noCat, 0, TQt::ExactMatch),
335 rightItem->addressee() );
336 }
337 delete item;
338 }
339 else if( item->parent()->text(0) == i18n("Distribution Lists") ) // remove a list
340 {
341 if( availableLists )
342 availableLists->insertItem( item );
343 }
344 }
345}
346
347void KWMailMergeTDEABCConfig::removeSelectedContacts()
348{
349 TQListViewItemIterator it( _ui->mSelectedView, TQListViewItemIterator::Selected );
350
351 while( it.current() )
352 {
353 kdDebug() << "removeSelectedContacts(): text: " << it.current()->text(-1) << endl;
354 removeContact( it.current() );
355 ++it;
356 }
357 _ui->mAvailableView->selectAll( false );
358}
359
360
361void KWMailMergeTDEABCConfig::saveDistributionList()
362{
363 TDEABC::DistributionListManager dlm( TDEABC::StdAddressBook::self() );
364 dlm.load();
365
366 bool ok = false;
367 TQString listName = KInputDialog::getText( i18n("New Distribution List"),
368 i18n("Please enter name:"),
369 TQString(), &ok,
370 this );
371 if ( !ok || listName.isEmpty() )
372 return;
373
374 if ( dlm.list( listName ) )
375 {
376 KMessageBox::information( 0,
377 i18n( "<qt>Distribution list with the given name <b>%1</b> "
378 "already exists. Please select a different name.</qt>" )
379 .arg( listName ) );
380 return;
381 }
382 TDEABC::DistributionList *distList = new TDEABC::DistributionList( &dlm, listName );
383
384 TQListViewItem* newListItem = new TQListViewItem( _ui->mSelectedView->findItem(
385 i18n("Distribution Lists"),0 , TQt::ExactMatch), listName );
386
387 TQListViewItem* category = _ui->mSelectedView->firstChild();
388 while(category)
389 {
390 if( category->text(0)==i18n("Single Entries") )
391 {
392 KWMailMergeTDEABCConfigListItem* item =
393 static_cast<KWMailMergeTDEABCConfigListItem*> ( category->firstChild() );
394
395 while(item)
396 {
397 distList->insertEntry( item->addressee() );
398
399 TDEABC::Addressee addr = item->addressee();
400 TQString formattedName = addr.formattedName();
401 TQListViewItem* newItem = new TQListViewItem(
402 newListItem, item->addressee().formattedName() );
403 newItem->setEnabled( false );
404
405 item = static_cast<KWMailMergeTDEABCConfigListItem*>( item->nextSibling() );
406 }
407
408 TQListViewItemIterator it ( category->firstChild() );
409 while( it.current() )
410 {
411 removeContact( it.current() );
412 ++it;
413 }
414 }
415 category = category->nextSibling();
416 }
417
418 dlm.save();
419 newListItem->setOpen( true );
420}
421
422
423void KWMailMergeTDEABCConfig::updateAvailable()
424{
425 _ui->mAvailableView->clear();
426 _ui->mAvailableView->setRootIsDecorated( true );
427
428 //
429 // First append the addressees.
430 //
431 TQListViewItem* noCategory = new TQListViewItem( _ui->mAvailableView,
432 i18n("no category") );
433
434 TQStringList& categories = _usedCategories ;
435 categories.clear();
436
437 TDEABC::AddressBook *addressBook = TDEABC::StdAddressBook::self();
438 for( TDEABC::AddressBook::Iterator itAddr = addressBook->begin();
439 itAddr != addressBook->end(); ++itAddr )
440 {
441
442 TQStringList entryCategories = itAddr->categories();
443 for ( TQStringList::Iterator itCat = entryCategories.begin();
444 itCat != entryCategories.end(); ++itCat )
445 {
446 int i = categories.findIndex(*itCat);
447
448 // Create category, if not yet in listview and append item to it.
449 if( i == -1 )
450 {
451 TQListViewItem* category = new TQListViewItem( _ui->mAvailableView, *itCat );
452 categories.append( *itCat );
453
454 KWMailMergeTDEABCConfigListItem* item = new KWMailMergeTDEABCConfigListItem(
455 category, *itAddr );
456 }
457 // Append item to existing category in listview.
458 else
459 {
460 KWMailMergeTDEABCConfigListItem* item = new KWMailMergeTDEABCConfigListItem(
461 _ui->mAvailableView->findItem(
462 *itCat, 0, TQt::ExactMatch),
463 *itAddr );
464 }
465
466 }
467 // If Addressee does not belong to any category, append it to "no category".
468 if( entryCategories.isEmpty() )
469 {
470 KWMailMergeTDEABCConfigListItem* item = new KWMailMergeTDEABCConfigListItem(
471 noCategory, *itAddr );
472 }
473 }
474
475 //
476 // Now append the distribution lists
477 //
478 TDEABC::DistributionListManager dlm ( addressBook );
479 dlm.load();
480
481 TQStringList distributionLists = dlm.listNames();
482 TQListViewItem* distributionListsItem = new TQListViewItem( _ui->mAvailableView,
483 i18n("Distribution Lists") );
484
485 TQStringList::Iterator itDistributionLists;
486
487 for( itDistributionLists = distributionLists.begin();
488 itDistributionLists != distributionLists.end(); ++itDistributionLists )
489 {
490 TDEABC::DistributionList* list = dlm.list( *itDistributionLists );
491
492 TDEABC::DistributionList::Entry::List entries = list->entries();
493
494 TQListViewItem* listItem = new TQListViewItem( distributionListsItem,
495 *itDistributionLists );
496
497 TDEABC::DistributionList::Entry::List::Iterator itList;
498 for ( itList = entries.begin(); itList != entries.end(); ++itList )
499 {
500 // Create a normal TQListViewItem and disable it, because this is not a
501 // distribution-list-editor. KAddressbook should be used instead.
502 TQListViewItem* item = new TQListViewItem(
503 listItem, (*itList).addressee.formattedName() );
504 item->setEnabled( false );
505 }
506
507 }
508}
509
510
511
512KWMailMergeTDEABCConfigListItem::KWMailMergeTDEABCConfigListItem( TQListView *parent,
513 const TDEABC::Addressee& addressEntry ) : TQListViewItem( parent )
514{
515 setText( 0, addressEntry.formattedName() );
516 _addressEntry = addressEntry;
517}
518
519KWMailMergeTDEABCConfigListItem::KWMailMergeTDEABCConfigListItem( TQListViewItem *parent,
520 const TDEABC::Addressee& addressEntry ) : TQListViewItem( parent )
521{
522 setText( 0, addressEntry.formattedName() );
523 _addressEntry = addressEntry;
524}
525
526KWMailMergeTDEABCConfigListItem::~KWMailMergeTDEABCConfigListItem()
527{}
528
529TDEABC::Addressee KWMailMergeTDEABCConfigListItem::addressee() const
530{
531 return _addressEntry;
532}
533
534TQString KWMailMergeTDEABCConfigListItem::text( int column ) const
535{
536 if( column == -1 )
537 {
538 return _addressEntry.uid();
539 }
540 else
541 {
542 return TQListViewItem::text( column );
543 }
544}
545
546#include "KWMailMergeTDEABCConfig.moc"