00001
00002 #include "settingsdialog.h"
00003 #include "opiemail.h"
00004 #include "editaccounts.h"
00005 #include "composemail.h"
00006 #include "mailistviewitem.h"
00007 #include "viewmail.h"
00008 #include "selectstore.h"
00009 #include "selectsmtp.h"
00010
00011 #include <libmailwrapper/smtpwrapper.h>
00012 #include <libmailwrapper/mailtypes.h>
00013 #include <libmailwrapper/abstractmail.h>
00014
00015
00016 #include <opie2/odebug.h>
00017 #include <qpe/qpeapplication.h>
00018 #include <qpe/config.h>
00019 using namespace Opie::Core;
00020
00021
00022 #include <qmap.h>
00023 #include <qvaluelist.h>
00024
00025
00026 #include <signal.h>
00027
00028 typedef QMapNode<QString,QString> tkeyvalues;
00029 typedef QValueList<tkeyvalues> tvaluelist;
00030
00031 class ValueExplode
00032 {
00033 protected:
00035 tvaluelist m_LastParsed;
00037 QString mDelemiter;
00039 QString m2Delemiter;
00041 void splitit();
00043 QString m_Command;
00045 ValueExplode(){}
00046 public:
00048
00053 ValueExplode(const QString&aCommand,const char aDelemiter = '&',const char a2Delemiter='=');
00055 virtual ~ValueExplode();
00057
00060 operator const tvaluelist& (){return m_LastParsed;}
00061 };
00062
00063 ValueExplode::~ValueExplode()
00064 {
00065 }
00066
00067 ValueExplode::ValueExplode(const QString&aCommand,const char aDelemiter,const char a2Delemiter)
00068 :m_LastParsed(),m_Command(aCommand)
00069 {
00070 mDelemiter = aDelemiter;
00071 m2Delemiter = a2Delemiter;
00072 splitit();
00073 }
00074
00075 void ValueExplode::splitit()
00076 {
00077 QString iLine;
00078 m_LastParsed.clear();
00079 if (mDelemiter.isEmpty()||m2Delemiter.isEmpty()) {
00080 m_LastParsed.append(tkeyvalues(m_Command,""));
00081 return;
00082 }
00083 int pos,pos2;
00084 unsigned startpos = 0;
00085 iLine = m_Command;
00086 while ( (pos = iLine.find(mDelemiter,startpos))!=-1) {
00087 pos2 = iLine.find(m2Delemiter,startpos);
00088 if (pos2==-1||pos2>pos) {
00089 m_LastParsed.append(tkeyvalues(iLine.mid(startpos,pos-startpos),""));
00090 } else {
00091 m_LastParsed.append(tkeyvalues(iLine.mid(startpos,pos2-startpos),iLine.mid(pos2+1,pos-pos2-1)));
00092 }
00093 startpos = pos+1;
00094 }
00095 if (startpos<iLine.length()) {
00096 pos2 = iLine.find(m2Delemiter,startpos);
00097 if (pos2==-1) {
00098 m_LastParsed.append(tkeyvalues(iLine.mid(startpos),""));
00099 } else {
00100 m_LastParsed.append(tkeyvalues(iLine.mid(startpos,pos2-startpos),iLine.mid(pos2+1)));
00101 }
00102 }
00103 }
00104
00105 OpieMail::OpieMail( QWidget *parent, const char *name, WFlags )
00106 : MainWindow( parent, name, WStyle_ContextHelp )
00107 {
00108 setup_signalblocking();
00109 Config cfg("mail");
00110 cfg.setGroup( "Settings" );
00111 m_clickopens = cfg.readBoolEntry("clickOpensMail",true);
00112
00113 settings = new Settings();
00114 folderView->populate( settings->getAccounts() );
00115 connect(folderView,SIGNAL(refreshMenues(int)),this,SLOT(refreshMenu(int)));
00116 }
00117
00118 OpieMail::~OpieMail()
00119 {
00120 if (settings) delete settings;
00121 }
00122
00123 void OpieMail::setup_signalblocking()
00124 {
00125
00126 struct sigaction blocking_action,temp_action;
00127 blocking_action.sa_handler = SIG_IGN;
00128 sigemptyset(&(blocking_action.sa_mask));
00129 blocking_action.sa_flags = 0;
00130 sigaction(SIGPIPE,&blocking_action,&temp_action);
00131 }
00132
00133 void OpieMail::appMessage(const QCString &msg, const QByteArray &data)
00134 {
00135
00136 if (msg == "writeMail(QString,QString)")
00137 {
00138 QDataStream stream(data,IO_ReadOnly);
00139 QString name, email;
00140 stream >> name >> email;
00141
00142 slotwriteMail(name.stripWhiteSpace(),email.stripWhiteSpace());
00143 }
00144 else if (msg == "newMail()")
00145 {
00146 slotComposeMail();
00147 }
00148 }
00149
00153 void OpieMail::setDocument(const QString& mail)
00154 {
00155
00156
00157
00158 if( mail.startsWith(QString::fromLatin1("mailto:")) )
00159 slotwriteMail(QString::null, mail.mid(7));
00160 }
00161
00162 void OpieMail::slotwriteMail(const QString&name,const QString&email)
00163 {
00164 ComposeMail compose( settings, this, 0, true , WStyle_ContextHelp );
00165 if (!email.isEmpty())
00166 {
00167 if (!name.isEmpty())
00168 {
00169 compose.setTo("\"" + name + "\"" + " " + "<"+ email + ">");
00170 }
00171 else
00172 {
00173 compose.setTo(email);
00174 }
00175 }
00176 compose.slotAdjustColumns();
00177 QPEApplication::execDialog( &compose );
00178 }
00179
00180 void OpieMail::slotComposeMail()
00181 {
00182 odebug << "Compose Mail" << oendl;
00183 slotwriteMail(0l,0l);
00184 }
00185
00186 void OpieMail::slotSendQueued()
00187 {
00188 odebug << "Send Queued" << oendl;
00189 SMTPaccount *smtp = 0;
00190
00191 QList<Account> list = settings->getAccounts();
00192 QList<SMTPaccount> smtpList;
00193 smtpList.setAutoDelete(false);
00194 Account *it;
00195 for ( it = list.first(); it; it = list.next() )
00196 {
00197 if ( it->getType() == MAILLIB::A_SMTP )
00198 {
00199 smtp = static_cast<SMTPaccount *>(it);
00200 smtpList.append(smtp);
00201 }
00202 }
00203 if (smtpList.count()==0)
00204 {
00205 QMessageBox::information(0,tr("Info"),tr("Define a smtp account first"));
00206 return;
00207 }
00208 if (smtpList.count()==1)
00209 {
00210 smtp = smtpList.at(0);
00211 }
00212 else
00213 {
00214 smtp = 0;
00215 selectsmtp selsmtp;
00216 selsmtp.setSelectionlist(&smtpList);
00217 if ( QPEApplication::execDialog( &selsmtp ) == QDialog::Accepted )
00218 {
00219 smtp = selsmtp.selected_smtp();
00220 }
00221 }
00222 if (smtp)
00223 {
00224 SMTPwrapper * wrap = new SMTPwrapper(smtp);
00225 if ( wrap->flushOutbox() )
00226 {
00227 QMessageBox::information(0,tr("Info"),tr("Mail queue flushed"));
00228 }
00229 delete wrap;
00230 }
00231 }
00232
00233 void OpieMail::slotSearchMails()
00234 {
00235 odebug << "Search Mails" << oendl;
00236 }
00237
00238 void OpieMail::slotEditSettings()
00239 {
00240 SettingsDialog settingsDialog( this, 0, true, WStyle_ContextHelp );
00241 if (QPEApplication::execDialog( &settingsDialog )) {
00242 Config cfg("mail");
00243 cfg.setGroup( "Settings" );
00244 m_clickopens = cfg.readBoolEntry("clickOpensMail",true);
00245 emit settingsChanged();
00246 }
00247 }
00248
00249 void OpieMail::slotEditAccounts()
00250 {
00251 odebug << "Edit Accounts" << oendl;
00252 EditAccounts eaDialog( settings, this, 0, true, WStyle_ContextHelp );
00253 eaDialog.slotAdjustColumns();
00254 if (QPEApplication::execDialog( &eaDialog )==QDialog::Rejected);
00255
00256 if ( settings ) delete settings;
00257 settings = new Settings();
00258 mailView->clear();
00259 folderView->populate( settings->getAccounts() );
00260 }
00261
00262 void OpieMail::displayMail()
00263 {
00264 QListViewItem*item = mailView->currentItem();
00265 if (!item) return;
00266 RecMailP mail = ((MailListViewItem*)item)->data();
00267 RecBodyP body = folderView->fetchBody(mail);
00268 ViewMail readMail( this,"", Qt::WType_Modal | WStyle_ContextHelp );
00269 readMail.setBody( body );
00270 readMail.setMail( mail );
00271 readMail.showMaximized();
00272 readMail.exec();
00273
00274 if ( readMail.deleted )
00275 {
00276 folderView->refreshCurrent();
00277 }
00278 else
00279 {
00280 ( (MailListViewItem*)item )->setPixmap( 0, QPixmap() );
00281 }
00282 }
00283
00284 void OpieMail::slotDeleteMail()
00285 {
00286 if (!mailView->currentItem()) return;
00287 RecMailP mail = ((MailListViewItem*)mailView->currentItem() )->data();
00288 if ( QMessageBox::warning(this, tr("Delete Mail"), QString( tr("<p>Do you really want to delete this mail? <br><br>" ) + mail->getFrom() + " - " + mail->getSubject() ) , QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )
00289 {
00290 mail->Wrapper()->deleteMail( mail );
00291 folderView->refreshCurrent();
00292 }
00293 }
00294
00295 void OpieMail::mailHold(int button, QListViewItem *item,const QPoint&,int )
00296 {
00297 if (!mailView->currentItem()) return;
00298 MAILLIB::ATYPE mailtype = ((MailListViewItem*)mailView->currentItem() )->wrapperType();
00299
00300 if (button!=2) {return;}
00301 odebug << "Event right/hold" << oendl;
00302 if (!item) return;
00303 QPopupMenu *m = new QPopupMenu(0);
00304 if (m)
00305 {
00306 if (mailtype==MAILLIB::A_NNTP) {
00307 m->insertItem(tr("Read this posting"),this,SLOT(displayMail()));
00308
00309 } else {
00310 if (folderView->currentisDraft()) {
00311 m->insertItem(tr("Edit this mail"),this,SLOT(reEditMail()));
00312 }
00313 m->insertItem(tr("Read this mail"),this,SLOT(displayMail()));
00314 m->insertItem(tr("Delete this mail"),this,SLOT(slotDeleteMail()));
00315 m->insertItem(tr("Copy/Move this mail"),this,SLOT(slotMoveCopyMail()));
00316 }
00317 m->setFocus();
00318 m->exec( QPoint( QCursor::pos().x(), QCursor::pos().y()) );
00319 delete m;
00320 }
00321 }
00322
00323 void OpieMail::slotShowFolders( bool show )
00324 {
00325 odebug << "Show Folders" << oendl;
00326 if ( show && folderView->isHidden() )
00327 {
00328 odebug << "-> showing" << oendl;
00329 folderView->show();
00330 }
00331 else if ( !show && !folderView->isHidden() )
00332 {
00333 odebug << "-> hiding" << oendl;
00334 folderView->hide();
00335 }
00336 }
00337
00338 void OpieMail::refreshMailView(const QValueList<RecMailP>&list)
00339 {
00340 MailListViewItem*item = 0;
00341 mailView->clear();
00342
00343 QValueList<RecMailP>::ConstIterator it;
00344 for (it = list.begin(); it != list.end();++it)
00345 {
00346 item = new MailListViewItem(mailView,item);
00347 item->storeData((*it));
00348 item->showEntry();
00349 }
00350 }
00351
00352 void OpieMail::mailLeftClicked(int button, QListViewItem *item,const QPoint&,int )
00353 {
00354 if (!m_clickopens) return;
00355
00356 if (button!=1) return;
00357 if (!item) return;
00358 if (folderView->currentisDraft()) {
00359 reEditMail();
00360 } else {
00361 displayMail();
00362 }
00363 }
00364
00365 void OpieMail::slotMoveCopyMail()
00366 {
00367 if (!mailView->currentItem()) return;
00368 RecMailP mail = ((MailListViewItem*)mailView->currentItem() )->data();
00369 AbstractMail*targetMail = 0;
00370 QString targetFolder = "";
00371 Selectstore sels;
00372 folderView->setupFolderselect(&sels);
00373 if (!sels.exec()) return;
00374 targetMail = sels.currentMail();
00375 targetFolder = sels.currentFolder();
00376 if ( (mail->Wrapper()==targetMail && mail->getMbox()==targetFolder) ||
00377 targetFolder.isEmpty())
00378 {
00379 return;
00380 }
00381 if (sels.newFolder() && !targetMail->createMbox(targetFolder))
00382 {
00383 QMessageBox::critical(0,tr("Error creating new Folder"),
00384 tr("<center>Error while creating<br>new folder - breaking.</center>"));
00385 return;
00386 }
00387 mail->Wrapper()->mvcpMail(mail,targetFolder,targetMail,sels.moveMails());
00388 folderView->refreshCurrent();
00389 }
00390
00391 void OpieMail::reEditMail()
00392 {
00393 if (!mailView->currentItem()) return;
00394
00395 ComposeMail compose( settings, this, 0, true , WStyle_ContextHelp );
00396 compose.reEditMail(((MailListViewItem*)mailView->currentItem() )->data());
00397 compose.slotAdjustColumns();
00398 QPEApplication::execDialog( &compose );
00399 }
00400
00401 void OpieMail::refreshMenu(int m_isFolder)
00402 {
00403 if (QApplication::desktop()->width()<330) {
00404 mailMenu->setItemEnabled(m_ServerMenuId,m_isFolder&1);
00405 mailMenu->setItemEnabled(m_FolderMenuId,m_isFolder&2);
00406 } else {
00407 menuBar->setItemEnabled(m_ServerMenuId,m_isFolder&1);
00408 menuBar->setItemEnabled(m_FolderMenuId,m_isFolder&2);
00409 }
00410
00411 QMap<int,QString>::ConstIterator it;
00412 QMap<int,QString> server_entries=folderView->currentServerMenu();
00413 QMap<int,QString> folder_entries=folderView->currentFolderMenu();
00414
00415 int id;
00416 unsigned int i;
00417 for (i=0; i<folderMenu->count();++i) {
00418 id = folderMenu->idAt(i);
00419 folderMenu->setItemEnabled(id,false);
00420 }
00421 for (it=folder_entries.begin();it!=folder_entries.end();++it) {
00422 folderMenu->changeItem(it.key(),it.data());
00423 folderMenu->setItemEnabled(it.key(),true);
00424 }
00425 for (i=0; i<serverMenu->count();++i) {
00426 id = serverMenu->idAt(i);
00427 serverMenu->setItemEnabled(id,false);
00428 }
00429 for (it=server_entries.begin();it!=server_entries.end();++it) {
00430 serverMenu->changeItem(it.key(),it.data());
00431 serverMenu->setItemEnabled(it.key(),true);
00432 }
00433 }
00434
00435 void OpieMail::serverSelected(int m_isFolder)
00436 {
00437 mailView->clear();
00438 refreshMenu(m_isFolder);
00439 }