24 #include "distributionlistdialog.h"
26 #include <libemailfunctions/email.h>
27 #include <tdeabc/resource.h>
28 #include <tdeabc/stdaddressbook.h>
29 #include <tdeabc/distributionlist.h>
31 #ifdef TDEPIM_NEW_DISTRLISTS
32 #include <libtdepim/distributionlist.h>
34 #include <libtdepim/kaddrbook.h>
36 #include <tdelistview.h>
37 #include <tdelocale.h>
39 #include <tdemessagebox.h>
40 #include <kinputdialog.h>
44 #include <tqlineedit.h>
46 class DistributionListItem :
public TQCheckListItem
49 DistributionListItem( TQListView *list )
50 : TQCheckListItem( list, TQString(), CheckBox )
54 void setAddressee(
const TDEABC::Addressee &a,
const TQString &email )
60 void setTransientAddressee(
const TDEABC::Addressee &a,
const TQString &email )
66 void init(
const TDEABC::Addressee &a,
const TQString &email )
70 setText( 1, mAddressee.realName() );
74 TDEABC::Addressee addressee()
const
79 TQString email()
const
84 bool isTransient()
const
90 TDEABC::Addressee mAddressee;
96 DistributionListDialog::DistributionListDialog( TQWidget *parent )
97 : KDialogBase( Plain, i18n(
"Save Distribution List"), User1 | Cancel,
98 User1, parent, 0, false, false, i18n(
"Save List") )
100 TQFrame *topFrame = plainPage();
102 TQBoxLayout *topLayout =
new TQVBoxLayout( topFrame );
103 topLayout->setSpacing( spacingHint() );
105 TQBoxLayout *titleLayout =
new TQHBoxLayout( topLayout );
107 TQLabel *label =
new TQLabel( i18n(
"Name:"), topFrame );
108 titleLayout->addWidget( label );
110 mTitleEdit =
new TQLineEdit( topFrame );
111 titleLayout->addWidget( mTitleEdit );
112 mTitleEdit->setFocus();
114 mRecipientsList =
new TDEListView( topFrame );
115 mRecipientsList->addColumn( TQString() );
116 mRecipientsList->addColumn( i18n(
"Name") );
117 mRecipientsList->addColumn( i18n(
"Email") );
118 topLayout->addWidget( mRecipientsList );
121 void DistributionListDialog::setRecipients(
const Recipient::List &recipients )
123 Recipient::List::ConstIterator it;
124 for( it = recipients.begin(); it != recipients.end(); ++it ) {
125 TQStringList emails = KPIM::splitEmailAddrList( (*it).email() );
126 TQStringList::ConstIterator it2;
127 for( it2 = emails.begin(); it2 != emails.end(); ++it2 ) {
130 TDEABC::Addressee::parseEmailAddress( *it2, name, email );
131 if ( !email.isEmpty() ) {
132 DistributionListItem *item =
new DistributionListItem( mRecipientsList );
133 TDEABC::Addressee::List addressees =
134 TDEABC::StdAddressBook::self(
true )->findByEmail( email );
135 if ( addressees.isEmpty() ) {
137 a.setNameFromString( name );
138 a.insertEmail( email );
139 item->setTransientAddressee( a, email );
142 TDEABC::Addressee::List::ConstIterator it3;
143 for( it3 = addressees.begin(); it3 != addressees.end(); ++it3 ) {
144 item->setAddressee( *it3, email );
145 if ( it3 == addressees.begin() ) item->setOn(
true );
153 void DistributionListDialog::slotUser1()
157 TQListViewItem *i = mRecipientsList->firstChild();
159 DistributionListItem *item =
static_cast<DistributionListItem *
>( i );
160 if ( item->isOn() ) {
164 i = i->nextSibling();
168 KMessageBox::information(
this,
169 i18n(
"There are no recipients in your list. "
170 "First select some recipients, "
171 "then try again.") );
175 #ifndef TDEPIM_NEW_DISTRLISTS
176 TDEABC::DistributionListManager manager( ab );
180 TQString name = mTitleEdit->text();
182 if ( name.isEmpty() ) {
184 name = KInputDialog::getText( i18n(
"New Distribution List"),
185 i18n(
"Please enter name:"), TQString(), &ok,
this );
186 if ( !ok || name.isEmpty() )
190 TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self(
true );
192 #ifdef TDEPIM_NEW_DISTRLISTS
193 if ( !KPIM::DistributionList::findByName( ab, name ).isEmpty() ) {
195 if ( manager.list( name ) ) {
197 KMessageBox::information(
this,
198 i18n(
"<qt>Distribution list with the given name <b>%1</b> "
199 "already exists. Please select a different name.</qt>" ).arg( name ) );
203 TDEABC::Resource*
const resource = KAddrBookExternal::selectResourceForSaving( ab );
209 TDEABC::Ticket *ticket = ab->requestSaveTicket( resource );
211 kdWarning(5006) <<
"Unable to get save ticket!" << endl;
215 #ifdef TDEPIM_NEW_DISTRLISTS
216 KPIM::DistributionList dlist;
217 dlist.setName( name );
219 i = mRecipientsList->firstChild();
221 DistributionListItem *item =
static_cast<DistributionListItem *
>( i );
222 if ( item->isOn() ) {
223 kdDebug() <<
" " << item->addressee().fullEmail() << endl;
224 if ( item->isTransient() ) {
225 resource->insertAddressee( item->addressee() );
227 if ( item->email() == item->addressee().preferredEmail() ) {
228 dlist.insertEntry( item->addressee() );
230 dlist.insertEntry( item->addressee(), item->email() );
233 i = i->nextSibling();
236 resource->insertAddressee( dlist );
238 TDEABC::DistributionList *dlist =
new TDEABC::DistributionList( &manager, name );
239 i = mRecipientsList->firstChild();
241 DistributionListItem *item =
static_cast<DistributionListItem *
>( i );
242 if ( item->isOn() ) {
243 kdDebug() <<
" " << item->addressee().fullEmail() << endl;
244 if ( item->isTransient() ) {
245 resource->insertAddressee( item->addressee() );
247 if ( item->email() == item->addressee().preferredEmail() ) {
248 dlist->insertEntry( item->addressee() );
250 dlist->insertEntry( item->addressee(), item->email() );
253 i = i->nextSibling();
257 if ( !ab->save( ticket ) ) {
258 kdWarning(5006) << k_funcinfo <<
" Couldn't save new addresses in the distribution list just created to the address book" << endl;
259 ab->releaseSaveTicket( ticket );
263 #ifndef TDEPIM_NEW_DISTRLISTS
269 #ifdef TDEPIM_NEW_DISTRLISTS
270 if ( !KPIM::DistributionList::findByName( ab, name ).isEmpty() ) {
272 if ( manager.list( name ) ) {