00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <qvbox.h>
00023 #include <qlayout.h>
00024 #include <qlineedit.h>
00025
00026 #include <kapplication.h>
00027 #include <kdebug.h>
00028 #include <kglobal.h>
00029 #include <kinputdialog.h>
00030 #include <klocale.h>
00031 #include <kmessagebox.h>
00032 #include <kpushbutton.h>
00033 #include <krun.h>
00034 #include <kabc/stdaddressbook.h>
00035 #include <kabc/distributionlist.h>
00036
00037 #include "addresspicker.h"
00038 #include "KWMailMergeKABC.h"
00039 #include "KWMailMergeKABCConfig.h"
00040
00041
00042 KWMailMergeKABCConfig::KWMailMergeKABCConfig( QWidget *parent, KWMailMergeKABC *db_)
00043 :KDialogBase( Plain, i18n( "Mail Merge - Editor" ),
00044 Ok | Cancel, Ok, parent, "", true)
00045 {
00046 _db = db_;
00047
00048 (new QVBoxLayout(plainPage()))->setAutoAdd(true);
00049 setMainWidget( _ui=new AddressPickerUI( plainPage() ) );
00050
00051 updateAvailable();
00052 initSelectedAddressees();
00053 initSelectedLists();
00054 initSlotSignalConnections();
00055 }
00056
00057
00058 KWMailMergeKABCConfig::~KWMailMergeKABCConfig()
00059 {
00060 ;
00061 }
00062
00063
00064 void KWMailMergeKABCConfig::acceptSelection()
00065 {
00066 _db->clear();
00067
00068 QListViewItem* top = _ui->mSelectedView->firstChild();
00069 while(top)
00070 {
00071 kdDebug() << "acceptSelection(): " << top->text(0) << endl;
00072 if( top->text(0) == i18n("Distribution Lists") )
00073 {
00074 QListViewItem* item = top->firstChild();
00075 while(item)
00076 {
00077 kdDebug() << "acceptSelection(): " << item->text(0) << endl;
00078 _db->addList( item->text(0) );
00079 item = item->nextSibling();
00080 }
00081 }
00082 else if( top->text(0) == i18n("Single Entries") )
00083 {
00084 QListViewItem* item = top->firstChild();
00085 while(item)
00086 {
00087 kdDebug() << "acceptSelection(): " << item->text(0) << endl;
00088 _db->addEntry( item->text(-1) );
00089 item = item->nextSibling();
00090 }
00091 }
00092 top = top->nextSibling();
00093 }
00094
00095 }
00096
00097
00098 void KWMailMergeKABCConfig::addSelectedContacts()
00099 {
00100 QListViewItemIterator it( _ui->mAvailableView, QListViewItemIterator::Selected );
00101 QListViewItem* selected = _ui->mSelectedView->findItem(
00102 i18n("Single Entries"), 0, Qt::ExactMatch );
00103 QListViewItem* selectedLists = _ui->mSelectedView->findItem(
00104 i18n("Distribution Lists"), 0, Qt::ExactMatch );
00105 while ( it.current() )
00106 {
00107 if( it.current()->depth() > 0 )
00108 {
00109 QString uid = it.current()->text( -1 );
00110 kdDebug() << "addSelectedContacts(): uid :" << uid << endl;
00111 if( !uid.isEmpty() )
00112 {
00113 KWMailMergeKABCConfigListItem *item =
00114 static_cast<KWMailMergeKABCConfigListItem*> ( it.current() );
00115 if( selected )
00116 {
00117 selected->insertItem( item );
00118 selected->setOpen( true );
00119 destroyAvailableClones( uid );
00120 }
00121 }
00122 else if( it.current()->parent()->text(0) == i18n("Distribution Lists") )
00123 {
00124 if( selectedLists )
00125 {
00126 selectedLists->insertItem( it.current() );
00127 selectedLists->setOpen( true );
00128 }
00129 }
00130 }
00131 ++it;
00132 }
00133 _ui->mSelectedView->selectAll( false );
00134 }
00135
00136
00137 void KWMailMergeKABCConfig::destroyAvailableClones( const QString& uid )
00138 {
00139 if( uid.isEmpty() )
00140 return;
00141
00142 QListViewItemIterator it( _ui->mAvailableView );
00143
00144 while ( it.current() )
00145 {
00146 if( it.current()->depth() > 0)
00147 {
00148 if( it.current()->text(-1)== uid )
00149 {
00150 delete it.current();
00151 }
00152 }
00153 ++it;
00154 }
00155 }
00156
00157
00158 void KWMailMergeKABCConfig::filterChanged( const QString& txt )
00159 {
00160 kdDebug() << "KWMailMergeKABCConfig::filterChanged( " << txt << " )" << endl;
00161
00162 bool showAll = txt.isEmpty();
00163
00164 QListViewItem* category = _ui->mAvailableView->firstChild();
00165 while(category)
00166 {
00167 if( category->text(0)!=i18n("Distribution Lists") )
00168 {
00169 QListViewItem* item = category->firstChild();
00170 while(item)
00171 {
00172 if(showAll)
00173 {
00174 item->setVisible( true );
00175 }
00176 else
00177 {
00178 item->setVisible( item->text(0).contains( txt, false ) );
00179 }
00180 item = item->nextSibling();
00181 }
00182 category->setOpen( !showAll );
00183 }
00184 else
00185 {
00186 category->setVisible( showAll );
00187 }
00188 category = category->nextSibling();
00189 }
00190 }
00191
00192
00193 void KWMailMergeKABCConfig::initSelectedAddressees()
00194 {
00195 QStringList records = _db->singleRecords();
00196
00197 QListViewItem* category = _ui->mAvailableView->firstChild();
00198 QListViewItem* selected = _ui->mSelectedView->findItem(
00199 i18n("Single Entries"), 0, Qt::ExactMatch );
00200 while ( category && (records.count()>0) )
00201 {
00202 if( category->text(0) != i18n("Distribution Lists") )
00203 {
00204 KWMailMergeKABCConfigListItem* item =
00205 static_cast<KWMailMergeKABCConfigListItem*> ( category->firstChild() );
00206 while( item && (records.count()>0) )
00207 {
00208
00209
00210 KWMailMergeKABCConfigListItem* nextItem =
00211 static_cast<KWMailMergeKABCConfigListItem*> ( item->nextSibling() );
00212
00213 for( QStringList::Iterator itRecords = records.begin();
00214 itRecords != records.end(); ++itRecords )
00215 {
00216 QString uid = *itRecords;
00217 if( item->text(-1) == uid )
00218 {
00219 selected->insertItem( item );
00220
00221
00222 itRecords = records.remove( itRecords );
00223 --itRecords;
00224
00225 destroyAvailableClones( uid );
00226 }
00227 }
00228 item = nextItem;
00229 }
00230 }
00231 category = category->nextSibling();
00232 }
00233 }
00234
00235
00236 void KWMailMergeKABCConfig::initSelectedLists()
00237 {
00238 QStringList lists = _db->lists();
00239
00240 kdDebug() << "::initSelectedLists()" << lists.join(",") << endl;
00241
00242 QListViewItem* l = _ui->mAvailableView->findItem(
00243 i18n("Distribution Lists"), 0, Qt::ExactMatch );
00244 QListViewItem* selected = _ui->mSelectedView->findItem(
00245 i18n("Distribution Lists"), 0, Qt::ExactMatch );
00246
00247 QListViewItem* item = ( l->firstChild() );
00248 while( item && (lists.count()>0) )
00249 {
00250 QListViewItem* nextItem = item->nextSibling();
00251
00252 for( QStringList::Iterator itLists = lists.begin();
00253 itLists != lists.end(); ++itLists )
00254 {
00255 QString id = *itLists;
00256 if( item->text(0) == id )
00257 {
00258 selected->insertItem( item );
00259 itLists = lists.remove( itLists );
00260 --itLists;
00261 }
00262 }
00263 item = nextItem;
00264 }
00265 }
00266
00267
00268 void KWMailMergeKABCConfig::initSlotSignalConnections()
00269 {
00270 connect( this, SIGNAL( okClicked() ), SLOT( acceptSelection() ) );
00271 connect( _ui->mAddButton, SIGNAL( clicked() ), SLOT( addSelectedContacts() ) );
00272 connect( _ui->mAddressBook, SIGNAL( clicked() ), SLOT( launchAddressbook() ) );
00273
00274 connect( _ui->mAvailableView, SIGNAL( doubleClicked( QListViewItem *, const QPoint &, int ) ),
00275 SLOT( addSelectedContacts() ) );
00276
00277 connect( _ui->mFilterEdit, SIGNAL( textChanged(const QString &) ),
00278 SLOT( filterChanged(const QString &) ) );
00279 connect( _ui->mRemoveButton, SIGNAL( clicked() ), SLOT( removeSelectedContacts() ) );
00280 connect( _ui->mSaveList, SIGNAL( clicked() ), SLOT( saveDistributionList() ) );
00281 connect( _ui->mSelectedView, SIGNAL( doubleClicked( QListViewItem *, const QPoint &, int ) ),
00282 SLOT( removeSelectedContacts() ) );
00283 }
00284
00285
00286 void KWMailMergeKABCConfig::launchAddressbook() const
00287 {
00288 kapp->startServiceByDesktopName( "kaddressbook", QString() );
00289 }
00290
00291
00292
00293 void KWMailMergeKABCConfig::removeContact( QListViewItem* item )
00294 {
00295 QStringList& categories = _usedCategories;
00296 QListViewItem* availableLists = _ui->mAvailableView->findItem(
00297 i18n("Distribution Lists"), 0, Qt::ExactMatch );
00298 if( item->depth() > 0 )
00299 {
00300 if( !item->text( -1 ).isEmpty() )
00301 {
00302 KWMailMergeKABCConfigListItem* rightItem =
00303 static_cast<KWMailMergeKABCConfigListItem*> ( item );
00304
00305 QStringList entryCategories = rightItem->addressee().categories();
00306 for ( QStringList::Iterator itEntryCat = entryCategories.begin();
00307 itEntryCat != entryCategories.end(); ++itEntryCat )
00308 {
00309 int i = categories.findIndex(*itEntryCat);
00310 if( i == -1 )
00311 {
00312 QListViewItem* category = new QListViewItem( _ui->mAvailableView,
00313 *itEntryCat );
00314 categories.append( *itEntryCat );
00315
00316 KWMailMergeKABCConfigListItem* leftItem = new KWMailMergeKABCConfigListItem(
00317 category, rightItem->addressee() );
00318 }
00319 else
00320 {
00321 KWMailMergeKABCConfigListItem* leftItem = new
00322 KWMailMergeKABCConfigListItem(
00323 _ui->mAvailableView->findItem(
00324 *itEntryCat, 0,
00325 Qt::ExactMatch),
00326 rightItem->addressee() );
00327 }
00328 }
00329 if( entryCategories.isEmpty() )
00330 {
00331 QString noCat = i18n("no category");
00332 KWMailMergeKABCConfigListItem* leftItem = new KWMailMergeKABCConfigListItem(
00333 _ui->mAvailableView->findItem(
00334 noCat, 0, Qt::ExactMatch),
00335 rightItem->addressee() );
00336 }
00337 delete item;
00338 }
00339 else if( item->parent()->text(0) == i18n("Distribution Lists") )
00340 {
00341 if( availableLists )
00342 availableLists->insertItem( item );
00343 }
00344 }
00345 }
00346
00347 void KWMailMergeKABCConfig::removeSelectedContacts()
00348 {
00349 QListViewItemIterator it( _ui->mSelectedView, QListViewItemIterator::Selected );
00350
00351 while( it.current() )
00352 {
00353 kdDebug() << "removeSelectedContacts(): text: " << it.current()->text(-1) << endl;
00354 removeContact( it.current() );
00355 ++it;
00356 }
00357 _ui->mAvailableView->selectAll( false );
00358 }
00359
00360
00361 void KWMailMergeKABCConfig::saveDistributionList()
00362 {
00363 KABC::DistributionListManager dlm( KABC::StdAddressBook::self() );
00364 dlm.load();
00365
00366 bool ok = false;
00367 QString listName = KInputDialog::getText( i18n("New Distribution List"),
00368 i18n("Please enter name:"),
00369 QString::null, &ok,
00370 this );
00371 if ( !ok || listName.isEmpty() )
00372 return;
00373
00374 if ( dlm.list( listName ) )
00375 {
00376 KMessageBox::information( 0,
00377 i18n( "<qt>Distribution list with the given name <b>%1</b> "
00378 "already exists. Please select a different name.</qt>" )
00379 .arg( listName ) );
00380 return;
00381 }
00382 KABC::DistributionList *distList = new KABC::DistributionList( &dlm, listName );
00383
00384 QListViewItem* newListItem = new QListViewItem( _ui->mSelectedView->findItem(
00385 i18n("Distribution Lists"),0 , Qt::ExactMatch), listName );
00386
00387 QListViewItem* category = _ui->mSelectedView->firstChild();
00388 while(category)
00389 {
00390 if( category->text(0)==i18n("Single Entries") )
00391 {
00392 KWMailMergeKABCConfigListItem* item =
00393 static_cast<KWMailMergeKABCConfigListItem*> ( category->firstChild() );
00394
00395 while(item)
00396 {
00397 distList->insertEntry( item->addressee() );
00398
00399 KABC::Addressee addr = item->addressee();
00400 QString formattedName = addr.formattedName();
00401 QListViewItem* newItem = new QListViewItem(
00402 newListItem, item->addressee().formattedName() );
00403 newItem->setEnabled( false );
00404
00405 item = static_cast<KWMailMergeKABCConfigListItem*>( item->nextSibling() );
00406 }
00407
00408 QListViewItemIterator it ( category->firstChild() );
00409 while( it.current() )
00410 {
00411 removeContact( it.current() );
00412 ++it;
00413 }
00414 }
00415 category = category->nextSibling();
00416 }
00417
00418 dlm.save();
00419 newListItem->setOpen( true );
00420 }
00421
00422
00423 void KWMailMergeKABCConfig::updateAvailable()
00424 {
00425 _ui->mAvailableView->clear();
00426 _ui->mAvailableView->setRootIsDecorated( true );
00427
00428
00429
00430
00431 QListViewItem* noCategory = new QListViewItem( _ui->mAvailableView,
00432 i18n("no category") );
00433
00434 QStringList& categories = _usedCategories ;
00435 categories.clear();
00436
00437 KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
00438 for( KABC::AddressBook::Iterator itAddr = addressBook->begin();
00439 itAddr != addressBook->end(); ++itAddr )
00440 {
00441
00442 QStringList entryCategories = itAddr->categories();
00443 for ( QStringList::Iterator itCat = entryCategories.begin();
00444 itCat != entryCategories.end(); ++itCat )
00445 {
00446 int i = categories.findIndex(*itCat);
00447
00448
00449 if( i == -1 )
00450 {
00451 QListViewItem* category = new QListViewItem( _ui->mAvailableView, *itCat );
00452 categories.append( *itCat );
00453
00454 KWMailMergeKABCConfigListItem* item = new KWMailMergeKABCConfigListItem(
00455 category, *itAddr );
00456 }
00457
00458 else
00459 {
00460 KWMailMergeKABCConfigListItem* item = new KWMailMergeKABCConfigListItem(
00461 _ui->mAvailableView->findItem(
00462 *itCat, 0, Qt::ExactMatch),
00463 *itAddr );
00464 }
00465
00466 }
00467
00468 if( entryCategories.isEmpty() )
00469 {
00470 KWMailMergeKABCConfigListItem* item = new KWMailMergeKABCConfigListItem(
00471 noCategory, *itAddr );
00472 }
00473 }
00474
00475
00476
00477
00478 KABC::DistributionListManager dlm ( addressBook );
00479 dlm.load();
00480
00481 QStringList distributionLists = dlm.listNames();
00482 QListViewItem* distributionListsItem = new QListViewItem( _ui->mAvailableView,
00483 i18n("Distribution Lists") );
00484
00485 QStringList::Iterator itDistributionLists;
00486
00487 for( itDistributionLists = distributionLists.begin();
00488 itDistributionLists != distributionLists.end(); ++itDistributionLists )
00489 {
00490 KABC::DistributionList* list = dlm.list( *itDistributionLists );
00491
00492 KABC::DistributionList::Entry::List entries = list->entries();
00493
00494 QListViewItem* listItem = new QListViewItem( distributionListsItem,
00495 *itDistributionLists );
00496
00497 KABC::DistributionList::Entry::List::Iterator itList;
00498 for ( itList = entries.begin(); itList != entries.end(); ++itList )
00499 {
00500
00501
00502 QListViewItem* item = new QListViewItem(
00503 listItem, (*itList).addressee.formattedName() );
00504 item->setEnabled( false );
00505 }
00506
00507 }
00508 }
00509
00510
00511
00512 KWMailMergeKABCConfigListItem::KWMailMergeKABCConfigListItem( QListView *parent,
00513 const KABC::Addressee& addressEntry ) : QListViewItem( parent )
00514 {
00515 setText( 0, addressEntry.formattedName() );
00516 _addressEntry = addressEntry;
00517 }
00518
00519 KWMailMergeKABCConfigListItem::KWMailMergeKABCConfigListItem( QListViewItem *parent,
00520 const KABC::Addressee& addressEntry ) : QListViewItem( parent )
00521 {
00522 setText( 0, addressEntry.formattedName() );
00523 _addressEntry = addressEntry;
00524 }
00525
00526 KWMailMergeKABCConfigListItem::~KWMailMergeKABCConfigListItem()
00527 {}
00528
00529 KABC::Addressee KWMailMergeKABCConfigListItem::addressee() const
00530 {
00531 return _addressEntry;
00532 }
00533
00534 QString KWMailMergeKABCConfigListItem::text( int column ) const
00535 {
00536 if( column == -1 )
00537 {
00538 return _addressEntry.uid();
00539 }
00540 else
00541 {
00542 return QListViewItem::text( column );
00543 }
00544 }
00545
00546 #include "KWMailMergeKABCConfig.moc"