summaryrefslogtreecommitdiffstats
path: root/kword/mailmerge/kabc/KWMailMergeKABCConfig.cpp
blob: a75a8417aca0a20b8fd2287edde4f762bc8f8428 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
   This file is part of the KDE project
   Copyright (C) 2004 Dirk Schmidt <fs@dirk-schmidt.net>
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
 
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
 
   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/


#include <tqvbox.h>
#include <tqlayout.h>
#include <tqlineedit.h>

#include <tdeapplication.h>
#include <kdebug.h>
#include <tdeglobal.h>
#include <kinputdialog.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kpushbutton.h>
#include <krun.h>
#include <tdeabc/stdaddressbook.h>
#include <tdeabc/distributionlist.h>

#include "addresspicker.h"
#include "KWMailMergeKABC.h"
#include "KWMailMergeKABCConfig.h"


KWMailMergeKABCConfig::KWMailMergeKABCConfig( TQWidget *parent, KWMailMergeKABC *db_)
        :KDialogBase( Plain, i18n( "Mail Merge - Editor" ),
                      Ok | Cancel, Ok, parent, "", true)
{
    _db = db_;

    (new TQVBoxLayout(plainPage()))->setAutoAdd(true);
    setMainWidget( _ui=new AddressPickerUI( plainPage() ) );

    updateAvailable();
    initSelectedAddressees();
    initSelectedLists();
    initSlotSignalConnections();
}


KWMailMergeKABCConfig::~KWMailMergeKABCConfig()
{
    ;
}


void KWMailMergeKABCConfig::acceptSelection()
{
    _db->clear();

    TQListViewItem* top = _ui->mSelectedView->firstChild();
    while(top)
    {
        kdDebug() << "acceptSelection(): " << top->text(0) << endl;
        if( top->text(0) == i18n("Distribution Lists") )
        {
            TQListViewItem* item = top->firstChild();
            while(item)
            {
                kdDebug() << "acceptSelection(): " << item->text(0) << endl;
                _db->addList( item->text(0) );
                item = item->nextSibling();
            }
        }
        else if( top->text(0) == i18n("Single Entries") )
        {
            TQListViewItem* item = top->firstChild();
            while(item)
            {
                kdDebug() << "acceptSelection(): " << item->text(0) << endl;
                _db->addEntry( item->text(-1) );
                item = item->nextSibling();
            }
        }
        top = top->nextSibling();
    }

}


void KWMailMergeKABCConfig::addSelectedContacts()
{
    TQListViewItemIterator it( _ui->mAvailableView, TQListViewItemIterator::Selected  );
    TQListViewItem* selected = _ui->mSelectedView->findItem(
                                  i18n("Single Entries"), 0, TQt::ExactMatch );
    TQListViewItem* selectedLists = _ui->mSelectedView->findItem(
                                       i18n("Distribution Lists"), 0, TQt::ExactMatch );
    while ( it.current() )
    {
        if( it.current()->depth() > 0 )
        {
            TQString uid = it.current()->text( -1 );
            kdDebug() << "addSelectedContacts(): uid :" << uid << endl;
            if( !uid.isEmpty() )
            {
                KWMailMergeKABCConfigListItem *item =
                    static_cast<KWMailMergeKABCConfigListItem*> ( it.current() );
                if( selected )
                {
                    selected->insertItem( item );
                    selected->setOpen( true );
                    destroyAvailableClones( uid );
                }
            }
            else if( it.current()->parent()->text(0) == i18n("Distribution Lists") )
            {
                if( selectedLists )
                {
                    selectedLists->insertItem( it.current() );
                    selectedLists->setOpen( true );
                }
            }
        }
        ++it;
    }
    _ui->mSelectedView->selectAll( false );
}


void KWMailMergeKABCConfig::destroyAvailableClones( const TQString& uid )
{
    if( uid.isEmpty() )
        return;

    TQListViewItemIterator it( _ui->mAvailableView  );

    while ( it.current() )
    {
        if( it.current()->depth() > 0)
        {
            if( it.current()->text(-1)== uid )
            {
                delete it.current();
            }
        }
        ++it;
    }
}


void KWMailMergeKABCConfig::filterChanged( const TQString& txt )
{
    kdDebug() << "KWMailMergeKABCConfig::filterChanged( " << txt << " )" << endl;

    bool showAll = txt.isEmpty();

    TQListViewItem* category = _ui->mAvailableView->firstChild();
    while(category)
    {
        if( category->text(0)!=i18n("Distribution Lists") )
        {
            TQListViewItem* item = category->firstChild();
            while(item)
            {
                if(showAll)
                {
                    item->setVisible( true );
                }
                else
                {
                    item->setVisible( item->text(0).contains( txt, false ) );
                }
                item = item->nextSibling();
            }
            category->setOpen( !showAll );
        }
        else
        {
            category->setVisible( showAll );
        }
        category = category->nextSibling();
    }
}


void KWMailMergeKABCConfig::initSelectedAddressees()
{
    TQStringList records = _db->singleRecords();

    TQListViewItem* category = _ui->mAvailableView->firstChild();
    TQListViewItem* selected = _ui->mSelectedView->findItem(
                                  i18n("Single Entries"), 0, TQt::ExactMatch );
    while ( category && (records.count()>0) )
    {
        if( category->text(0) != i18n("Distribution Lists") )
        {
            KWMailMergeKABCConfigListItem* item =
                static_cast<KWMailMergeKABCConfigListItem*> ( category->firstChild() );
            while( item && (records.count()>0) )
            {
                // Need some temporary item, because after selected->insertItem( item )
                // the item->nextSibling() is not the one we want.
                KWMailMergeKABCConfigListItem* nextItem =
                    static_cast<KWMailMergeKABCConfigListItem*> ( item->nextSibling() );

                for( TQStringList::Iterator itRecords = records.begin();
                        itRecords != records.end(); ++itRecords )
                {
                    TQString uid = *itRecords;
                    if( item->text(-1) == uid )
                    {
                        selected->insertItem( item );

                        // downsize records to speed up iterations
                        itRecords = records.remove( itRecords );
                        --itRecords;

                        destroyAvailableClones( uid );
                    }
                }
                item = nextItem;
            }
        }
        category = category->nextSibling();
    }
}


void KWMailMergeKABCConfig::initSelectedLists()
{
    TQStringList lists = _db->lists();

    kdDebug() << "::initSelectedLists()" << lists.join(",") << endl;

    TQListViewItem* l = _ui->mAvailableView->findItem(
                           i18n("Distribution Lists"), 0, TQt::ExactMatch );
    TQListViewItem* selected = _ui->mSelectedView->findItem(
                                  i18n("Distribution Lists"), 0, TQt::ExactMatch );

    TQListViewItem* item = ( l->firstChild() );
    while( item && (lists.count()>0) )
    {
        TQListViewItem* nextItem = item->nextSibling();

        for( TQStringList::Iterator itLists = lists.begin();
                itLists != lists.end(); ++itLists )
        {
            TQString id = *itLists;
            if( item->text(0) == id )
            {
                selected->insertItem( item );
                itLists = lists.remove( itLists );
                --itLists;
            }
        }
        item = nextItem;
    }
}


void KWMailMergeKABCConfig::initSlotSignalConnections()
{
    connect( this, TQT_SIGNAL( okClicked() ), TQT_SLOT( acceptSelection() ) );
    connect( _ui->mAddButton, TQT_SIGNAL( clicked() ), TQT_SLOT( addSelectedContacts() ) );
    connect( _ui->mAddressBook, TQT_SIGNAL( clicked() ), TQT_SLOT( launchAddressbook() ) );

    connect( _ui->mAvailableView, TQT_SIGNAL( doubleClicked( TQListViewItem *, const TQPoint &, int ) ),
             TQT_SLOT( addSelectedContacts() ) );

    connect( _ui->mFilterEdit, TQT_SIGNAL( textChanged(const TQString &) ),
             TQT_SLOT( filterChanged(const TQString &) ) );
    connect( _ui->mRemoveButton, TQT_SIGNAL( clicked() ), TQT_SLOT( removeSelectedContacts() ) );
    connect( _ui->mSaveList, TQT_SIGNAL( clicked() ), TQT_SLOT( saveDistributionList() ) );
    connect( _ui->mSelectedView, TQT_SIGNAL( doubleClicked( TQListViewItem *, const TQPoint &, int ) ),
             TQT_SLOT( removeSelectedContacts() ) );
}


void KWMailMergeKABCConfig::launchAddressbook() const
{
    kapp->startServiceByDesktopName( "kaddressbook", TQString() );
}



void KWMailMergeKABCConfig::removeContact( TQListViewItem* item )
{
    TQStringList& categories = _usedCategories;
    TQListViewItem* availableLists = _ui->mAvailableView->findItem(
                                        i18n("Distribution Lists"), 0, TQt::ExactMatch );
    if( item->depth() > 0 )
    {
        if( !item->text( -1 ).isEmpty() ) // remove selected single entry here
        {
            KWMailMergeKABCConfigListItem* rightItem =
                static_cast<KWMailMergeKABCConfigListItem*> ( item );

            TQStringList entryCategories = rightItem->addressee().categories();
            for ( TQStringList::Iterator itEntryCat = entryCategories.begin();
                    itEntryCat != entryCategories.end(); ++itEntryCat )
            {
                int i = categories.findIndex(*itEntryCat);
                if(  i == -1 )
                {
                    TQListViewItem* category = new TQListViewItem( _ui->mAvailableView,
                                              *itEntryCat );
                    categories.append( *itEntryCat );

                    KWMailMergeKABCConfigListItem* leftItem = new KWMailMergeKABCConfigListItem(
                                category, rightItem->addressee() );
                }
                else
                {
                    KWMailMergeKABCConfigListItem* leftItem = new
                            KWMailMergeKABCConfigListItem(
                                _ui->mAvailableView->findItem(
                                    *itEntryCat, 0,
                                    TQt::ExactMatch),
                                rightItem->addressee() );
                }
            }
            if( entryCategories.isEmpty() )
            {
                TQString noCat = i18n("no category");
                KWMailMergeKABCConfigListItem* leftItem = new KWMailMergeKABCConfigListItem(
                            _ui->mAvailableView->findItem(
                                noCat, 0, TQt::ExactMatch),
                            rightItem->addressee() );
            }
            delete item;
        }
        else if( item->parent()->text(0) == i18n("Distribution Lists") ) // remove a list
        {
            if( availableLists )
                availableLists->insertItem( item );
        }
    }
}

void KWMailMergeKABCConfig::removeSelectedContacts()
{
    TQListViewItemIterator it( _ui->mSelectedView, TQListViewItemIterator::Selected  );

    while( it.current() )
    {
        kdDebug() << "removeSelectedContacts(): text: " << it.current()->text(-1) << endl;
        removeContact( it.current() );
        ++it;
    }
    _ui->mAvailableView->selectAll( false );
}


void KWMailMergeKABCConfig::saveDistributionList()
{
    KABC::DistributionListManager dlm( KABC::StdAddressBook::self() );
    dlm.load();

    bool ok = false;
    TQString listName = KInputDialog::getText( i18n("New Distribution List"),
                       i18n("Please enter name:"),
                       TQString(), &ok,
                       this );
    if ( !ok || listName.isEmpty() )
        return;

    if ( dlm.list( listName ) )
    {
        KMessageBox::information( 0,
                                  i18n( "<qt>Distribution list with the given name <b>%1</b> "
                                        "already exists. Please select a different name.</qt>" )
                                  .arg( listName ) );
        return;
    }
    KABC::DistributionList *distList = new KABC::DistributionList( &dlm, listName );

    TQListViewItem* newListItem = new TQListViewItem( _ui->mSelectedView->findItem(
                                     i18n("Distribution Lists"),0 , TQt::ExactMatch), listName );

    TQListViewItem* category = _ui->mSelectedView->firstChild();
    while(category)
    {
        if( category->text(0)==i18n("Single Entries") )
        {
            KWMailMergeKABCConfigListItem* item =
                static_cast<KWMailMergeKABCConfigListItem*> ( category->firstChild() );

            while(item)
            {
                distList->insertEntry( item->addressee() );

                KABC::Addressee addr = item->addressee();
                TQString formattedName = addr.formattedName();
                TQListViewItem* newItem = new TQListViewItem(
                                             newListItem, item->addressee().formattedName() );
                newItem->setEnabled( false );

                item = static_cast<KWMailMergeKABCConfigListItem*>( item->nextSibling() );
            }

            TQListViewItemIterator it ( category->firstChild() );
            while( it.current() )
            {
                removeContact( it.current() );
                ++it;
            }
        }
        category = category->nextSibling();
    }

    dlm.save();
    newListItem->setOpen( true );
}


void KWMailMergeKABCConfig::updateAvailable()
{
    _ui->mAvailableView->clear();
    _ui->mAvailableView->setRootIsDecorated( true );

    //
    // First append the addressees.
    //
    TQListViewItem* noCategory = new TQListViewItem( _ui->mAvailableView,
                                i18n("no category") );

    TQStringList& categories = _usedCategories ;
    categories.clear();

    KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
    for( KABC::AddressBook::Iterator itAddr = addressBook->begin();
            itAddr != addressBook->end(); ++itAddr )
    {

        TQStringList entryCategories = itAddr->categories();
        for ( TQStringList::Iterator itCat = entryCategories.begin();
                itCat != entryCategories.end(); ++itCat )
        {
            int i = categories.findIndex(*itCat);

            // Create category, if not yet in listview and append item to it.
            if(  i == -1 )
            {
                TQListViewItem* category = new TQListViewItem( _ui->mAvailableView, *itCat );
                categories.append( *itCat );

                KWMailMergeKABCConfigListItem* item = new KWMailMergeKABCConfigListItem(
                                                          category, *itAddr );
            }
            // Append item to existing category in listview.
            else
            {
                KWMailMergeKABCConfigListItem* item = new KWMailMergeKABCConfigListItem(
                                                          _ui->mAvailableView->findItem(
                                                              *itCat, 0, TQt::ExactMatch),
                                                          *itAddr );
            }

        }
        // If Addressee does not belong to any category, append it to "no category".
        if( entryCategories.isEmpty() )
        {
            KWMailMergeKABCConfigListItem* item = new KWMailMergeKABCConfigListItem(
                                                      noCategory, *itAddr );
        }
    }

    //
    // Now append the distribution lists
    //
    KABC::DistributionListManager dlm ( addressBook );
    dlm.load();

    TQStringList distributionLists = dlm.listNames();
    TQListViewItem* distributionListsItem = new TQListViewItem( _ui->mAvailableView,
                                           i18n("Distribution Lists") );

    TQStringList::Iterator itDistributionLists;

    for( itDistributionLists = distributionLists.begin();
            itDistributionLists != distributionLists.end(); ++itDistributionLists )
    {
        KABC::DistributionList* list = dlm.list( *itDistributionLists );

        KABC::DistributionList::Entry::List entries = list->entries();

        TQListViewItem* listItem = new TQListViewItem( distributionListsItem,
                                  *itDistributionLists );

        KABC::DistributionList::Entry::List::Iterator itList;
        for ( itList = entries.begin(); itList != entries.end(); ++itList )
        {
            // Create a normal TQListViewItem and disable it, because this is not a
            // distribution-list-editor. KAddressbook should be used instead.
            TQListViewItem* item = new TQListViewItem(
                                      listItem, (*itList).addressee.formattedName() );
            item->setEnabled( false );
        }

    }
}



KWMailMergeKABCConfigListItem::KWMailMergeKABCConfigListItem( TQListView *parent,
        const KABC::Addressee& addressEntry ) : TQListViewItem( parent )
{
    setText( 0, addressEntry.formattedName() );
    _addressEntry = addressEntry;
}

KWMailMergeKABCConfigListItem::KWMailMergeKABCConfigListItem( TQListViewItem *parent,
        const KABC::Addressee& addressEntry ) : TQListViewItem( parent )
{
    setText( 0, addressEntry.formattedName() );
    _addressEntry = addressEntry;
}

KWMailMergeKABCConfigListItem::~KWMailMergeKABCConfigListItem()
{}

KABC::Addressee KWMailMergeKABCConfigListItem::addressee() const
{
    return _addressEntry;
}

TQString KWMailMergeKABCConfigListItem::text( int column ) const
{
    if( column == -1 )
    {
        return _addressEntry.uid();
    }
    else
    {
        return TQListViewItem::text( column );
    }
}

#include "KWMailMergeKABCConfig.moc"