kmail

distributionlistdialog.cpp
1 /*
2  This file is part of KMail.
3 
4  Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include <config.h> // for TDEPIM_NEW_DISTRLISTS
23 
24 #include "distributionlistdialog.h"
25 
26 #include <libemailfunctions/email.h>
27 #include <tdeabc/resource.h>
28 #include <tdeabc/stdaddressbook.h>
29 #include <tdeabc/distributionlist.h>
30 
31 #ifdef TDEPIM_NEW_DISTRLISTS
32 #include <libtdepim/distributionlist.h>
33 #endif
34 #include <libtdepim/kaddrbook.h>
35 
36 #include <tdelistview.h>
37 #include <tdelocale.h>
38 #include <kdebug.h>
39 #include <tdemessagebox.h>
40 #include <kinputdialog.h>
41 
42 #include <tqlayout.h>
43 #include <tqlabel.h>
44 #include <tqlineedit.h>
45 
46 class DistributionListItem : public TQCheckListItem
47 {
48  public:
49  DistributionListItem( TQListView *list )
50  : TQCheckListItem( list, TQString(), CheckBox )
51  {
52  }
53 
54  void setAddressee( const TDEABC::Addressee &a, const TQString &email )
55  {
56  mIsTransient = false;
57  init( a, email );
58  }
59 
60  void setTransientAddressee( const TDEABC::Addressee &a, const TQString &email )
61  {
62  mIsTransient = true;
63  init( a, email );
64  }
65 
66  void init( const TDEABC::Addressee &a, const TQString &email )
67  {
68  mAddressee = a;
69  mEmail = email;
70  setText( 1, mAddressee.realName() );
71  setText( 2, mEmail );
72  }
73 
74  TDEABC::Addressee addressee() const
75  {
76  return mAddressee;
77  }
78 
79  TQString email() const
80  {
81  return mEmail;
82  }
83 
84  bool isTransient() const
85  {
86  return mIsTransient;
87  }
88 
89  private:
90  TDEABC::Addressee mAddressee;
91  TQString mEmail;
92  bool mIsTransient;
93 };
94 
95 
96 DistributionListDialog::DistributionListDialog( TQWidget *parent )
97  : KDialogBase( Plain, i18n("Save Distribution List"), User1 | Cancel,
98  User1, parent, 0, false, false, i18n("Save List") )
99 {
100  TQFrame *topFrame = plainPage();
101 
102  TQBoxLayout *topLayout = new TQVBoxLayout( topFrame );
103  topLayout->setSpacing( spacingHint() );
104 
105  TQBoxLayout *titleLayout = new TQHBoxLayout( topLayout );
106 
107  TQLabel *label = new TQLabel( i18n("Name:"), topFrame );
108  titleLayout->addWidget( label );
109 
110  mTitleEdit = new TQLineEdit( topFrame );
111  titleLayout->addWidget( mTitleEdit );
112  mTitleEdit->setFocus();
113 
114  mRecipientsList = new TDEListView( topFrame );
115  mRecipientsList->addColumn( TQString() );
116  mRecipientsList->addColumn( i18n("Name") );
117  mRecipientsList->addColumn( i18n("Email") );
118  topLayout->addWidget( mRecipientsList );
119 }
120 
121 void DistributionListDialog::setRecipients( const Recipient::List &recipients )
122 {
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 ) {
128  TQString name;
129  TQString email;
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() ) {
136  TDEABC::Addressee a;
137  a.setNameFromString( name );
138  a.insertEmail( email );
139  item->setTransientAddressee( a, email );
140  item->setOn( true );
141  } else {
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 );
146  }
147  }
148  }
149  }
150  }
151 }
152 
153 void DistributionListDialog::slotUser1()
154 {
155  bool isEmpty = true;
156 
157  TQListViewItem *i = mRecipientsList->firstChild();
158  while( i ) {
159  DistributionListItem *item = static_cast<DistributionListItem *>( i );
160  if ( item->isOn() ) {
161  isEmpty = false;
162  break;
163  }
164  i = i->nextSibling();
165  }
166 
167  if ( isEmpty ) {
168  KMessageBox::information( this,
169  i18n("There are no recipients in your list. "
170  "First select some recipients, "
171  "then try again.") );
172  return;
173  }
174 
175 #ifndef TDEPIM_NEW_DISTRLISTS
176  TDEABC::DistributionListManager manager( ab );
177  manager.load();
178 #endif
179 
180  TQString name = mTitleEdit->text();
181 
182  if ( name.isEmpty() ) {
183  bool ok = false;
184  name = KInputDialog::getText( i18n("New Distribution List"),
185  i18n("Please enter name:"), TQString(), &ok, this );
186  if ( !ok || name.isEmpty() )
187  return;
188  }
189 
190  TDEABC::AddressBook *ab = TDEABC::StdAddressBook::self( true );
191 
192 #ifdef TDEPIM_NEW_DISTRLISTS
193  if ( !KPIM::DistributionList::findByName( ab, name ).isEmpty() ) {
194 #else
195  if ( manager.list( name ) ) {
196 #endif
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 ) );
200  return;
201  }
202 
203  TDEABC::Resource* const resource = KAddrBookExternal::selectResourceForSaving( ab );
204  if ( !resource )
205  return;
206 
207  // Ask for a save ticket here, we use it for inserting the recipients into the addressbook and
208  // also for saving the addressbook, see https://issues.kolab.org/issue4281
209  TDEABC::Ticket *ticket = ab->requestSaveTicket( resource );
210  if ( !ticket ) {
211  kdWarning(5006) << "Unable to get save ticket!" << endl;
212  return;
213  }
214 
215 #ifdef TDEPIM_NEW_DISTRLISTS
216  KPIM::DistributionList dlist;
217  dlist.setName( name );
218 
219  i = mRecipientsList->firstChild();
220  while( i ) {
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() );
226  }
227  if ( item->email() == item->addressee().preferredEmail() ) {
228  dlist.insertEntry( item->addressee() );
229  } else {
230  dlist.insertEntry( item->addressee(), item->email() );
231  }
232  }
233  i = i->nextSibling();
234  }
235 
236  resource->insertAddressee( dlist );
237 #else
238  TDEABC::DistributionList *dlist = new TDEABC::DistributionList( &manager, name );
239  i = mRecipientsList->firstChild();
240  while( i ) {
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() );
246  }
247  if ( item->email() == item->addressee().preferredEmail() ) {
248  dlist->insertEntry( item->addressee() );
249  } else {
250  dlist->insertEntry( item->addressee(), item->email() );
251  }
252  }
253  i = i->nextSibling();
254  }
255 #endif
256 
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 );
260  return;
261  }
262 
263 #ifndef TDEPIM_NEW_DISTRLISTS
264  manager.save();
265 #endif
266 
267  // Only accept when the dist list is really in the addressbook, since we can't detect if the
268  // user aborted saving in another way, since insertAddressee() lacks a return code.
269 #ifdef TDEPIM_NEW_DISTRLISTS
270  if ( !KPIM::DistributionList::findByName( ab, name ).isEmpty() ) {
271 #else
272  if ( manager.list( name ) ) {
273 #endif
274  accept();
275  }
276 }