00001
00002 #include "composemail.h"
00003
00004 #include <libmailwrapper/smtpwrapper.h>
00005 #include <libmailwrapper/storemail.h>
00006 #include <libmailwrapper/abstractmail.h>
00007 #include <libmailwrapper/mailtypes.h>
00008
00009
00010 #include <opie2/ofiledialog.h>
00011 #include <opie2/odebug.h>
00012 #include <opie2/oresource.h>
00013 #include <qpe/config.h>
00014 #include <qpe/global.h>
00015 #include <qpe/contact.h>
00016 using namespace Opie::Core;
00017 using namespace Opie::Ui;
00018
00019
00020 #include <qt.h>
00021
00022 ComposeMail::ComposeMail( Settings *s, QWidget *parent, const char *name, bool modal, WFlags flags )
00023 : ComposeMailUI( parent, name, modal, flags )
00024 {
00025 settings = s;
00026 m_replyid = "";
00027
00028 QString vfilename = Global::applicationFileName("addressbook",
00029 "businesscard.vcf");
00030 Contact c;
00031 if (QFile::exists(vfilename)) {
00032 c = Contact::readVCard( vfilename )[0];
00033 }
00034
00035 QStringList mails = c.emailList();
00036 QString defmail = c.defaultEmail();
00037
00038 if (defmail.length()!=0) {
00039 fromBox->insertItem(defmail);
00040 }
00041 QStringList::ConstIterator sit = mails.begin();
00042 for (;sit!=mails.end();++sit) {
00043 if ( (*sit)==defmail)
00044 continue;
00045 fromBox->insertItem((*sit));
00046 }
00047 senderNameEdit->setText(c.firstName()+" "+c.lastName());
00048 Config cfg( "mail" );
00049 cfg.setGroup( "Compose" );
00050 checkBoxLater->setChecked( cfg.readBoolEntry( "sendLater", false ) );
00051
00052 attList->addColumn( tr( "Name" ) );
00053 attList->addColumn( tr( "Size" ) );
00054
00055 QList<Account> accounts = settings->getAccounts();
00056
00057 Account *it;
00058 for ( it = accounts.first(); it; it = accounts.next() ) {
00059 if ( it->getType()==MAILLIB::A_SMTP ) {
00060 SMTPaccount *smtp = static_cast<SMTPaccount *>(it);
00061 smtpAccountBox->insertItem( smtp->getAccountName() );
00062 smtpAccounts.append( smtp );
00063 }
00064 }
00065
00066 if ( smtpAccounts.count() > 0 ) {
00067 fillValues( smtpAccountBox->currentItem() );
00068 } else {
00069 QMessageBox::information( this, tr( "Problem" ),
00070 tr( "<p>Please create an SMTP account first.</p>" ),
00071 tr( "Ok" ) );
00072 return;
00073 }
00074
00075 connect( smtpAccountBox, SIGNAL( activated(int) ), SLOT( fillValues(int) ) );
00076 connect( toButton, SIGNAL( clicked() ), SLOT( pickAddressTo() ) );
00077 connect( ccButton, SIGNAL( clicked() ), SLOT( pickAddressCC() ) );
00078 connect( bccButton, SIGNAL( clicked() ), SLOT( pickAddressBCC() ) );
00079 connect( replyButton, SIGNAL( clicked() ), SLOT( pickAddressReply() ) );
00080 connect( addButton, SIGNAL( clicked() ), SLOT( addAttachment() ) );
00081 connect( deleteButton, SIGNAL( clicked() ), SLOT( removeAttachment() ) );
00082 }
00083
00084 void ComposeMail::pickAddress( QLineEdit *line )
00085 {
00086 QString names = AddressPicker::getNames();
00087 if ( line->text().isEmpty() ) {
00088 line->setText( names );
00089 } else if ( !names.isEmpty() ) {
00090 line->setText( line->text() + ", " + names );
00091 }
00092 }
00093
00094
00095 void ComposeMail::setTo( const QString & to )
00096 {
00097 toLine->setText( to );
00098 }
00099
00100 void ComposeMail::setSubject( const QString & subject )
00101 {
00102 subjectLine->setText( subject );
00103 }
00104
00105 void ComposeMail::setInReplyTo( const QString & messageId )
00106 {
00107 m_replyid = messageId;
00108 }
00109
00110 void ComposeMail::setMessage( const QString & text )
00111 {
00112 message->setText( text );
00113 }
00114
00115
00116 void ComposeMail::pickAddressTo()
00117 {
00118 pickAddress( toLine );
00119 }
00120
00121 void ComposeMail::pickAddressCC()
00122 {
00123 pickAddress( ccLine );
00124 }
00125
00126 void ComposeMail::pickAddressBCC()
00127 {
00128 pickAddress( bccLine );
00129 }
00130
00131 void ComposeMail::pickAddressReply()
00132 {
00133 pickAddress( replyLine );
00134 }
00135
00136 void ComposeMail::fillValues( int )
00137 {
00138 #if 0
00139 SMTPaccount *smtp = smtpAccounts.at( current );
00140 ccLine->clear();
00141 if ( smtp->getUseCC() ) {
00142 ccLine->setText( smtp->getCC() );
00143 }
00144 bccLine->clear();
00145 if ( smtp->getUseBCC() ) {
00146 bccLine->setText( smtp->getBCC() );
00147 }
00148 replyLine->clear();
00149 if ( smtp->getUseReply() ) {
00150 replyLine->setText( smtp->getReply() );
00151 }
00152 sigMultiLine->setText( smtp->getSignature() );
00153 #endif
00154 }
00155
00156 void ComposeMail::slotAdjustColumns()
00157 {
00158 int currPage = tabWidget->currentPageIndex();
00159
00160 tabWidget->showPage( attachTab );
00161 attList->setColumnWidth( 0, attList->visibleWidth() - 80 );
00162 attList->setColumnWidth( 1, 80 );
00163
00164 tabWidget->setCurrentPage( currPage );
00165 }
00166
00167 void ComposeMail::addAttachment()
00168 {
00169 DocLnk lnk = OFileDialog::getOpenFileName( 1, "/" );
00170 if ( !lnk.name().isEmpty() ) {
00171 Attachment *att = new Attachment( lnk );
00172 (void) new AttachViewItem( attList, att );
00173 }
00174 }
00175
00176 void ComposeMail::removeAttachment()
00177 {
00178 if ( !attList->currentItem() ) {
00179 QMessageBox::information( this, tr( "Error" ),
00180 tr( "<p>Please select a File.</p>" ),
00181 tr( "Ok" ) );
00182 } else {
00183 attList->takeItem( attList->currentItem() );
00184 }
00185 }
00186
00187 void ComposeMail::accept()
00188 {
00189 if ( checkBoxLater->isChecked() ) {
00190 odebug << "Send later" << oendl;
00191 }
00192
00193 #if 0
00194 odebug << "Sending Mail with "
00195 << smtpAccounts.at( smtpAccountBox->currentItem() )->getAccountName() << oendl;
00196 #endif
00197 Opie::Core::OSmartPointer<Mail> mail=new Mail;
00198
00199 SMTPaccount *smtp = smtpAccounts.at( smtpAccountBox->currentItem() );
00200 mail->setMail(fromBox->currentText());
00201
00202 if ( !toLine->text().isEmpty() ) {
00203 mail->setTo( toLine->text() );
00204 } else {
00205 QMessageBox::warning(0,tr("Sending mail"),
00206 tr("No Receiver specified" ) );
00207 return;
00208 }
00209 mail->setName(senderNameEdit->text());
00210 mail->setCC( ccLine->text() );
00211 mail->setBCC( bccLine->text() );
00212 mail->setReply( replyLine->text() );
00213 mail->setSubject( subjectLine->text() );
00214 if (!m_replyid.isEmpty()) {
00215 QStringList ids;
00216 ids.append(m_replyid);
00217 mail->setInreply(ids);
00218 }
00219 QString txt = message->text();
00220 if ( !sigMultiLine->text().isEmpty() ) {
00221 txt.append( "\n--\n" );
00222 txt.append( sigMultiLine->text() );
00223 }
00224 mail->setMessage( txt );
00225 AttachViewItem *it = (AttachViewItem *) attList->firstChild();
00226 while ( it != NULL ) {
00227 mail->addAttachment( it->getAttachment() );
00228 it = (AttachViewItem *) it->nextSibling();
00229 }
00230
00231 SMTPwrapper wrapper( smtp );
00232 wrapper.sendMail( mail,checkBoxLater->isChecked() );
00233
00234 QDialog::accept();
00235 }
00236
00237 void ComposeMail::reject()
00238 {
00239 int yesno = QMessageBox::warning(0,tr("Store message"),
00240 tr("Store message into drafts?"),
00241 tr("Yes"),
00242 tr("No"),QString::null,0,1);
00243
00244 if (yesno == 0) {
00245 Opie::Core::OSmartPointer<Mail> mail=new Mail();
00246 mail->setMail(fromBox->currentText());
00247 mail->setTo( toLine->text() );
00248 mail->setName(senderNameEdit->text());
00249 mail->setCC( ccLine->text() );
00250 mail->setBCC( bccLine->text() );
00251 mail->setReply( replyLine->text() );
00252 mail->setSubject( subjectLine->text() );
00253 if (!m_replyid.isEmpty()) {
00254 QStringList ids;
00255 ids.append(m_replyid);
00256 mail->setInreply(ids);
00257 }
00258 QString txt = message->text();
00259 if ( !sigMultiLine->text().isEmpty() ) {
00260 txt.append( "\n--\n" );
00261 txt.append( sigMultiLine->text() );
00262 }
00263 odebug << txt << oendl;
00264 mail->setMessage( txt );
00265
00266
00267 Storemail wrapper(AbstractMail::draftFolder());
00268 wrapper.storeMail(mail);
00269
00270 AttachViewItem *it = (AttachViewItem *) attList->firstChild();
00271
00272 if ( it != NULL ) {
00273 QMessageBox::warning(0,tr("Store message"),
00274 tr("<center>Attachments will not be stored in \"Draft\" folder</center>"));
00275 }
00276 }
00277 QDialog::reject();
00278 }
00279
00280 ComposeMail::~ComposeMail()
00281 {
00282 }
00283
00284 void ComposeMail::reEditMail(const RecMailP¤t)
00285 {
00286 RecMailP data = current;
00287 message->setText(data->Wrapper()->fetchBody(current)->Bodytext());
00288 subjectLine->setText( data->getSubject());
00289 toLine->setText(data->To().join(","));
00290 ccLine->setText(data->CC().join(","));
00291 bccLine->setText(data->Bcc().join(","));
00292 replyLine->setText(data->Replyto());
00293 }
00294
00295 AttachViewItem::AttachViewItem( QListView *parent, Attachment *att )
00296 : QListViewItem( parent )
00297 {
00298 attachment = att;
00299 odebug << att->getMimeType() << oendl;
00300 setPixmap( 0, attachment->getDocLnk().pixmap().isNull() ?
00301 OResource::loadPixmap( "UnknownDocument", OResource::SmallIcon ) :
00302 attachment->getDocLnk().pixmap() );
00303 setText( 0, att->getName().isEmpty() ? att->getFileName() : att->getName() );
00304 setText( 1, QString::number( att->getSize() ) );
00305 }
00306