distributionlistdialog.cpp
00001 /* 00002 This file is part of KMail. 00003 00004 Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include <config.h> // for KDEPIM_NEW_DISTRLISTS 00023 00024 #include "distributionlistdialog.h" 00025 00026 #include <libemailfunctions/email.h> 00027 #include <kabc/resource.h> 00028 #include <kabc/stdaddressbook.h> 00029 #include <kabc/distributionlist.h> 00030 00031 #ifdef KDEPIM_NEW_DISTRLISTS 00032 #include <libkdepim/distributionlist.h> 00033 #endif 00034 #include <libkdepim/kaddrbook.h> 00035 00036 #include <klistview.h> 00037 #include <klocale.h> 00038 #include <kdebug.h> 00039 #include <kmessagebox.h> 00040 #include <kinputdialog.h> 00041 00042 #include <tqlayout.h> 00043 #include <tqlabel.h> 00044 #include <tqlineedit.h> 00045 00046 class DistributionListItem : public TQCheckListItem 00047 { 00048 public: 00049 DistributionListItem( TQListView *list ) 00050 : TQCheckListItem( list, TQString(), CheckBox ) 00051 { 00052 } 00053 00054 void setAddressee( const KABC::Addressee &a, const TQString &email ) 00055 { 00056 mIsTransient = false; 00057 init( a, email ); 00058 } 00059 00060 void setTransientAddressee( const KABC::Addressee &a, const TQString &email ) 00061 { 00062 mIsTransient = true; 00063 init( a, email ); 00064 } 00065 00066 void init( const KABC::Addressee &a, const TQString &email ) 00067 { 00068 mAddressee = a; 00069 mEmail = email; 00070 setText( 1, mAddressee.realName() ); 00071 setText( 2, mEmail ); 00072 } 00073 00074 KABC::Addressee addressee() const 00075 { 00076 return mAddressee; 00077 } 00078 00079 TQString email() const 00080 { 00081 return mEmail; 00082 } 00083 00084 bool isTransient() const 00085 { 00086 return mIsTransient; 00087 } 00088 00089 private: 00090 KABC::Addressee mAddressee; 00091 TQString mEmail; 00092 bool mIsTransient; 00093 }; 00094 00095 00096 DistributionListDialog::DistributionListDialog( TQWidget *parent ) 00097 : KDialogBase( Plain, i18n("Save Distribution List"), User1 | Cancel, 00098 User1, parent, 0, false, false, i18n("Save List") ) 00099 { 00100 TQFrame *topFrame = plainPage(); 00101 00102 TQBoxLayout *topLayout = new TQVBoxLayout( topFrame ); 00103 topLayout->setSpacing( spacingHint() ); 00104 00105 TQBoxLayout *titleLayout = new TQHBoxLayout( topLayout ); 00106 00107 TQLabel *label = new TQLabel( i18n("Name:"), topFrame ); 00108 titleLayout->addWidget( label ); 00109 00110 mTitleEdit = new TQLineEdit( topFrame ); 00111 titleLayout->addWidget( mTitleEdit ); 00112 mTitleEdit->setFocus(); 00113 00114 mRecipientsList = new KListView( topFrame ); 00115 mRecipientsList->addColumn( TQString() ); 00116 mRecipientsList->addColumn( i18n("Name") ); 00117 mRecipientsList->addColumn( i18n("Email") ); 00118 topLayout->addWidget( mRecipientsList ); 00119 } 00120 00121 void DistributionListDialog::setRecipients( const Recipient::List &recipients ) 00122 { 00123 Recipient::List::ConstIterator it; 00124 for( it = recipients.begin(); it != recipients.end(); ++it ) { 00125 TQStringList emails = KPIM::splitEmailAddrList( (*it).email() ); 00126 TQStringList::ConstIterator it2; 00127 for( it2 = emails.begin(); it2 != emails.end(); ++it2 ) { 00128 TQString name; 00129 TQString email; 00130 KABC::Addressee::parseEmailAddress( *it2, name, email ); 00131 if ( !email.isEmpty() ) { 00132 DistributionListItem *item = new DistributionListItem( mRecipientsList ); 00133 KABC::Addressee::List addressees = 00134 KABC::StdAddressBook::self( true )->findByEmail( email ); 00135 if ( addressees.isEmpty() ) { 00136 KABC::Addressee a; 00137 a.setNameFromString( name ); 00138 a.insertEmail( email ); 00139 item->setTransientAddressee( a, email ); 00140 item->setOn( true ); 00141 } else { 00142 KABC::Addressee::List::ConstIterator it3; 00143 for( it3 = addressees.begin(); it3 != addressees.end(); ++it3 ) { 00144 item->setAddressee( *it3, email ); 00145 if ( it3 == addressees.begin() ) item->setOn( true ); 00146 } 00147 } 00148 } 00149 } 00150 } 00151 } 00152 00153 void DistributionListDialog::slotUser1() 00154 { 00155 bool isEmpty = true; 00156 00157 TQListViewItem *i = mRecipientsList->firstChild(); 00158 while( i ) { 00159 DistributionListItem *item = static_cast<DistributionListItem *>( i ); 00160 if ( item->isOn() ) { 00161 isEmpty = false; 00162 break; 00163 } 00164 i = i->nextSibling(); 00165 } 00166 00167 if ( isEmpty ) { 00168 KMessageBox::information( this, 00169 i18n("There are no recipients in your list. " 00170 "First select some recipients, " 00171 "then try again.") ); 00172 return; 00173 } 00174 00175 #ifndef KDEPIM_NEW_DISTRLISTS 00176 KABC::DistributionListManager manager( ab ); 00177 manager.load(); 00178 #endif 00179 00180 TQString name = mTitleEdit->text(); 00181 00182 if ( name.isEmpty() ) { 00183 bool ok = false; 00184 name = KInputDialog::getText( i18n("New Distribution List"), 00185 i18n("Please enter name:"), TQString(), &ok, this ); 00186 if ( !ok || name.isEmpty() ) 00187 return; 00188 } 00189 00190 KABC::AddressBook *ab = KABC::StdAddressBook::self( true ); 00191 00192 #ifdef KDEPIM_NEW_DISTRLISTS 00193 if ( !KPIM::DistributionList::findByName( ab, name ).isEmpty() ) { 00194 #else 00195 if ( manager.list( name ) ) { 00196 #endif 00197 KMessageBox::information( this, 00198 i18n( "<qt>Distribution list with the given name <b>%1</b> " 00199 "already exists. Please select a different name.</qt>" ).arg( name ) ); 00200 return; 00201 } 00202 00203 KABC::Resource* const resource = KAddrBookExternal::selectResourceForSaving( ab ); 00204 if ( !resource ) 00205 return; 00206 00207 // Ask for a save ticket here, we use it for inserting the recipients into the addressbook and 00208 // also for saving the addressbook, see https://issues.kolab.org/issue4281 00209 KABC::Ticket *ticket = ab->requestSaveTicket( resource ); 00210 if ( !ticket ) { 00211 kdWarning(5006) << "Unable to get save ticket!" << endl; 00212 return; 00213 } 00214 00215 #ifdef KDEPIM_NEW_DISTRLISTS 00216 KPIM::DistributionList dlist; 00217 dlist.setName( name ); 00218 00219 i = mRecipientsList->firstChild(); 00220 while( i ) { 00221 DistributionListItem *item = static_cast<DistributionListItem *>( i ); 00222 if ( item->isOn() ) { 00223 kdDebug() << " " << item->addressee().fullEmail() << endl; 00224 if ( item->isTransient() ) { 00225 resource->insertAddressee( item->addressee() ); 00226 } 00227 if ( item->email() == item->addressee().preferredEmail() ) { 00228 dlist.insertEntry( item->addressee() ); 00229 } else { 00230 dlist.insertEntry( item->addressee(), item->email() ); 00231 } 00232 } 00233 i = i->nextSibling(); 00234 } 00235 00236 resource->insertAddressee( dlist ); 00237 #else 00238 KABC::DistributionList *dlist = new KABC::DistributionList( &manager, name ); 00239 i = mRecipientsList->firstChild(); 00240 while( i ) { 00241 DistributionListItem *item = static_cast<DistributionListItem *>( i ); 00242 if ( item->isOn() ) { 00243 kdDebug() << " " << item->addressee().fullEmail() << endl; 00244 if ( item->isTransient() ) { 00245 resource->insertAddressee( item->addressee() ); 00246 } 00247 if ( item->email() == item->addressee().preferredEmail() ) { 00248 dlist->insertEntry( item->addressee() ); 00249 } else { 00250 dlist->insertEntry( item->addressee(), item->email() ); 00251 } 00252 } 00253 i = i->nextSibling(); 00254 } 00255 #endif 00256 00257 if ( !ab->save( ticket ) ) { 00258 kdWarning(5006) << k_funcinfo << " Couldn't save new addresses in the distribution list just created to the address book" << endl; 00259 ab->releaseSaveTicket( ticket ); 00260 return; 00261 } 00262 00263 #ifndef KDEPIM_NEW_DISTRLISTS 00264 manager.save(); 00265 #endif 00266 00267 // Only accept when the dist list is really in the addressbook, since we can't detect if the 00268 // user aborted saving in another way, since insertAddressee() lacks a return code. 00269 #ifdef KDEPIM_NEW_DISTRLISTS 00270 if ( !KPIM::DistributionList::findByName( ab, name ).isEmpty() ) { 00271 #else 00272 if ( manager.list( name ) ) { 00273 #endif 00274 accept(); 00275 } 00276 }