kmail
distributionlistdialog.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "distributionlistdialog.h"
00023
00024 #include <libemailfunctions/email.h>
00025 #include <kabc/resource.h>
00026 #include <kabc/stdaddressbook.h>
00027 #include <kabc/distributionlist.h>
00028
00029 #include <klistview.h>
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032 #include <kmessagebox.h>
00033 #include <kinputdialog.h>
00034
00035 #include <qlayout.h>
00036 #include <qlabel.h>
00037 #include <qlineedit.h>
00038
00039 class DistributionListItem : public QCheckListItem
00040 {
00041 public:
00042 DistributionListItem( QListView *list )
00043 : QCheckListItem( list, QString::null, CheckBox )
00044 {
00045 }
00046
00047 void setAddressee( const KABC::Addressee &a, const QString &email )
00048 {
00049 mIsTransient = false;
00050 init( a, email );
00051 }
00052
00053 void setTransientAddressee( const KABC::Addressee &a, const QString &email )
00054 {
00055 mIsTransient = true;
00056 init( a, email );
00057 }
00058
00059 void init( const KABC::Addressee &a, const QString &email )
00060 {
00061 mAddressee = a;
00062 mEmail = email;
00063 setText( 1, mAddressee.realName() );
00064 setText( 2, mEmail );
00065 }
00066
00067 KABC::Addressee addressee() const
00068 {
00069 return mAddressee;
00070 }
00071
00072 QString email() const
00073 {
00074 return mEmail;
00075 }
00076
00077 bool isTransient() const
00078 {
00079 return mIsTransient;
00080 }
00081
00082 private:
00083 KABC::Addressee mAddressee;
00084 QString mEmail;
00085 bool mIsTransient;
00086 };
00087
00088
00089 DistributionListDialog::DistributionListDialog( QWidget *parent )
00090 : KDialogBase( Plain, i18n("Save Distribution List"), User1 | Cancel,
00091 User1, parent, 0, false, false, i18n("Save List") )
00092 {
00093 QFrame *topFrame = plainPage();
00094
00095 QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00096 topLayout->setSpacing( spacingHint() );
00097
00098 QBoxLayout *titleLayout = new QHBoxLayout( topLayout );
00099
00100 QLabel *label = new QLabel( i18n("Name:"), topFrame );
00101 titleLayout->addWidget( label );
00102
00103 mTitleEdit = new QLineEdit( topFrame );
00104 titleLayout->addWidget( mTitleEdit );
00105 mTitleEdit->setFocus();
00106
00107 mRecipientsList = new KListView( topFrame );
00108 mRecipientsList->addColumn( QString::null );
00109 mRecipientsList->addColumn( i18n("Name") );
00110 mRecipientsList->addColumn( i18n("Email") );
00111 topLayout->addWidget( mRecipientsList );
00112 }
00113
00114 void DistributionListDialog::setRecipients( const Recipient::List &recipients )
00115 {
00116 Recipient::List::ConstIterator it;
00117 for( it = recipients.begin(); it != recipients.end(); ++it ) {
00118 QStringList emails = KPIM::splitEmailAddrList( (*it).email() );
00119 QStringList::ConstIterator it2;
00120 for( it2 = emails.begin(); it2 != emails.end(); ++it2 ) {
00121 QString name;
00122 QString email;
00123 KABC::Addressee::parseEmailAddress( *it2, name, email );
00124 if ( !email.isEmpty() ) {
00125 DistributionListItem *item = new DistributionListItem( mRecipientsList );
00126 KABC::Addressee::List addressees =
00127 KABC::StdAddressBook::self( true )->findByEmail( email );
00128 if ( addressees.isEmpty() ) {
00129 KABC::Addressee a;
00130 a.setNameFromString( name );
00131 a.insertEmail( email );
00132 item->setTransientAddressee( a, email );
00133 item->setOn( true );
00134 } else {
00135 KABC::Addressee::List::ConstIterator it3;
00136 for( it3 = addressees.begin(); it3 != addressees.end(); ++it3 ) {
00137 item->setAddressee( *it3, email );
00138 if ( it3 == addressees.begin() ) item->setOn( true );
00139 }
00140 }
00141 }
00142 }
00143 }
00144 }
00145
00146 void DistributionListDialog::slotUser1()
00147 {
00148 bool isEmpty = true;
00149
00150 KABC::AddressBook *ab = KABC::StdAddressBook::self( true );
00151
00152 QListViewItem *i = mRecipientsList->firstChild();
00153 while( i ) {
00154 DistributionListItem *item = static_cast<DistributionListItem *>( i );
00155 if ( item->isOn() ) {
00156 isEmpty = false;
00157 break;
00158 }
00159 i = i->nextSibling();
00160 }
00161
00162 if ( isEmpty ) {
00163 KMessageBox::information( this,
00164 i18n("There are no recipients in your list. "
00165 "First select some recipients, "
00166 "then try again.") );
00167 return;
00168 }
00169
00170 KABC::DistributionListManager manager( ab );
00171 manager.load();
00172
00173 QString name = mTitleEdit->text();
00174
00175 if ( name.isEmpty() ) {
00176 bool ok = false;
00177 name = KInputDialog::getText( i18n("New Distribution List"),
00178 i18n("Please enter name:"), QString::null, &ok, this );
00179 if ( !ok || name.isEmpty() )
00180 return;
00181 }
00182
00183 if ( manager.list( name ) ) {
00184 KMessageBox::information( this,
00185 i18n( "<qt>Distribution list with the given name <b>%1</b> "
00186 "already exists. Please select a different name.</qt>" ).arg( name ) );
00187 return;
00188 }
00189
00190 KABC::DistributionList *dlist = new KABC::DistributionList( &manager, name );
00191 i = mRecipientsList->firstChild();
00192 while( i ) {
00193 DistributionListItem *item = static_cast<DistributionListItem *>( i );
00194 if ( item->isOn() ) {
00195 kdDebug() << " " << item->addressee().fullEmail() << endl;
00196 if ( item->isTransient() ) {
00197 ab->insertAddressee( item->addressee() );
00198 }
00199 if ( item->email() == item->addressee().preferredEmail() ) {
00200 dlist->insertEntry( item->addressee() );
00201 } else {
00202 dlist->insertEntry( item->addressee(), item->email() );
00203 }
00204 }
00205 i = i->nextSibling();
00206 }
00207
00208
00209 bool saveError = true;
00210 KABC::Ticket *ticket = ab->requestSaveTicket( 0 );
00211 if ( ticket )
00212 if ( ab->save( ticket ) )
00213 saveError = false;
00214 else
00215 ab->releaseSaveTicket( ticket );
00216
00217 if ( saveError )
00218 kdWarning(5006) << k_funcinfo << " Couldn't save new addresses in the distribution list just created to the address book" << endl;
00219
00220 manager.save();
00221
00222 close();
00223 }
|