Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

mhwrapper.cpp

Go to the documentation of this file.
00001 #include "mhwrapper.h"
00002 #include "mailtypes.h"
00003 #include "mailwrapper.h"
00004 #include <libetpan/libetpan.h>
00005 #include <qdir.h>
00006 #include <qmessagebox.h>
00007 #include <stdlib.h>
00008 #include <qpe/global.h>
00009 #include <opie2/oprocess.h>
00010 #include <opie2/odebug.h>
00011 
00012 using namespace Opie::Core;
00013 MHwrapper::MHwrapper(const QString & mbox_dir,const QString&mbox_name)
00014     : Genericwrapper(),MHPath(mbox_dir),MHName(mbox_name)
00015 {
00016     if (MHPath.length()>0) {
00017         if (MHPath[MHPath.length()-1]=='/') {
00018             MHPath=MHPath.left(MHPath.length()-1);
00019         }
00020         odebug << MHPath << oendl;
00021         QDir dir(MHPath);
00022         if (!dir.exists()) {
00023             dir.mkdir(MHPath);
00024         }
00025         init_storage();
00026     }
00027 }
00028 
00029 void MHwrapper::init_storage()
00030 {
00031     int r;
00032     QString pre = MHPath;
00033     if (!m_storage) {
00034         m_storage = mailstorage_new(NULL);
00035         r = mh_mailstorage_init(m_storage,(char*)pre.latin1(),0,0,0);
00036         if (r != MAIL_NO_ERROR) {
00037             odebug << "error initializing storage" << oendl;
00038             mailstorage_free(m_storage);
00039             m_storage = 0;
00040             return;
00041         }
00042     }
00043     r = mailstorage_connect(m_storage);
00044     if (r!=MAIL_NO_ERROR) {
00045         odebug << "error connecting storage" << oendl;
00046         mailstorage_free(m_storage);
00047         m_storage = 0;
00048     }
00049 }
00050 
00051 void MHwrapper::clean_storage()
00052 {
00053     if (m_storage) {
00054         mailstorage_disconnect(m_storage);
00055         mailstorage_free(m_storage);
00056         m_storage = 0;
00057     }
00058 }
00059 
00060 MHwrapper::~MHwrapper()
00061 {
00062     clean_storage();
00063 }
00064 
00065 void MHwrapper::listMessages(const QString & mailbox, QValueList<Opie::Core::OSmartPointer<RecMail> > &target )
00066 {
00067     init_storage();
00068     if (!m_storage) {
00069         return;
00070     }
00071     QString f = buildPath(mailbox);
00072     int r = mailsession_select_folder(m_storage->sto_session,(char*)f.latin1());
00073     if (r!=MAIL_NO_ERROR) {
00074         odebug << "listMessages: error selecting folder!" << oendl;
00075         return;
00076     }
00077     parseList(target,m_storage->sto_session,f);
00078     Global::statusMessage(tr("Mailbox has %1 mail(s)").arg(target.count()));
00079 }
00080 
00081 QValueList<Opie::Core::OSmartPointer<Folder> >* MHwrapper::listFolders()
00082 {
00083     QValueList<Opie::Core::OSmartPointer<Folder> >* folders = new QValueList<Opie::Core::OSmartPointer<Folder> >();
00084     /* this is needed! */
00085     if (m_storage) mailstorage_disconnect(m_storage);
00086     init_storage();
00087     if (!m_storage) {
00088         return folders;
00089     }
00090     mail_list*flist = 0;
00091     clistcell*current=0;
00092     int r = mailsession_list_folders(m_storage->sto_session,NULL,&flist);
00093     if (r != MAIL_NO_ERROR || !flist) {
00094         odebug << "error getting folder list" << oendl;
00095         return folders;
00096     }
00097     for (current=clist_begin(flist->mb_list);current!=0;current=clist_next(current)) {
00098         QString t = (char*)current->data;
00099         t.replace(0,MHPath.length(),"");
00100         folders->append(new MHFolder(t,MHPath));
00101     }
00102     mail_list_free(flist);
00103     return folders;
00104 }
00105 
00106 void MHwrapper::deleteMail(const RecMailP&mail)
00107 {
00108     init_storage();
00109     if (!m_storage) {
00110         return;
00111     }
00112     int r = mailsession_select_folder(m_storage->sto_session,(char*)mail->getMbox().latin1());
00113     if (r!=MAIL_NO_ERROR) {
00114         odebug << "error selecting folder!" << oendl;
00115         return;
00116     }
00117     r = mailsession_remove_message(m_storage->sto_session,mail->getNumber());
00118     if (r != MAIL_NO_ERROR) {
00119         odebug << "error deleting mail" << oendl;
00120     }
00121 }
00122 
00123 void MHwrapper::answeredMail(const RecMailP&)
00124 {
00125 }
00126 
00127 RecBodyP MHwrapper::fetchBody( const RecMailP &mail )
00128 {
00129     RecBodyP body = new RecBody();
00130     init_storage();
00131     if (!m_storage) {
00132         return body;
00133     }
00134     mailmessage * msg;
00135     char*data=0;
00136 
00137     /* mail should hold the complete path! */
00138     int r = mailsession_select_folder(m_storage->sto_session,(char*)mail->getMbox().latin1());
00139     if (r != MAIL_NO_ERROR) {
00140         return body;
00141     }
00142     r = mailsession_get_message(m_storage->sto_session, mail->getNumber(), &msg);
00143     if (r != MAIL_NO_ERROR) {
00144         odebug << "Error fetching mail " << mail->getNumber() << "" << oendl;
00145         return body;
00146     }
00147     body = parseMail(msg);
00148     mailmessage_fetch_result_free(msg,data);
00149     return body;
00150 }
00151 
00152 void MHwrapper::mbox_progress( size_t current, size_t maximum )
00153 {
00154     odebug << "MH " << current << " von " << maximum << "" << oendl;
00155 }
00156 
00157 QString MHwrapper::buildPath(const QString&p)
00158 {
00159     QString f="";
00160     if (p.length()==0||p=="/")
00161         return MHPath;
00162     if (!p.startsWith(MHPath)) {
00163         f+=MHPath;
00164     }
00165     if (!p.startsWith("/")) {
00166         f+="/";
00167     }
00168     f+=p;
00169     return f;
00170 }
00171 
00172 int MHwrapper::createMbox(const QString&folder,const FolderP&pfolder,const QString&,bool )
00173 {
00174     init_storage();
00175     if (!m_storage) {
00176         return 0;
00177     }
00178     QString f;
00179     if (!pfolder) {
00180         // toplevel folder
00181         f  = buildPath(folder);
00182     } else {
00183         f = pfolder->getName();
00184         f+="/";
00185         f+=folder;
00186     }
00187     odebug << f << oendl;
00188     int r = mailsession_create_folder(m_storage->sto_session,(char*)f.latin1());
00189     if (r != MAIL_NO_ERROR) {
00190         odebug << "error creating folder " << r << "" << oendl;
00191         return 0;
00192     }
00193     odebug << "Folder created" << oendl;
00194     return 1;
00195 }
00196 
00197 void MHwrapper::storeMessage(const char*msg,size_t length, const QString&Folder)
00198 {
00199     init_storage();
00200     if (!m_storage) {
00201         return;
00202     }
00203     QString f = buildPath(Folder);
00204     int r = mailsession_select_folder(m_storage->sto_session,(char*)f.latin1());
00205     if (r!=MAIL_NO_ERROR) {
00206         odebug << "error selecting folder!" << oendl;
00207         return;
00208     }
00209     r = mailsession_append_message(m_storage->sto_session,(char*)msg,length);
00210     if (r!=MAIL_NO_ERROR) {
00211         odebug << "error storing mail" << oendl;
00212     }
00213     return;
00214 }
00215 
00216 encodedString* MHwrapper::fetchRawBody(const RecMailP&mail)
00217 {
00218     encodedString*result = 0;
00219     init_storage();
00220     if (!m_storage) {
00221         return result;
00222     }
00223     mailmessage * msg = 0;
00224     char*data=0;
00225     size_t size;
00226     int r = mailsession_select_folder(m_storage->sto_session,(char*)mail->getMbox().latin1());
00227     if (r!=MAIL_NO_ERROR) {
00228         odebug << "error selecting folder!" << oendl;
00229         return result;
00230     }
00231     r = mailsession_get_message(m_storage->sto_session, mail->getNumber(), &msg);
00232     if (r != MAIL_NO_ERROR) {
00233         Global::statusMessage(tr("Error fetching mail %i").arg(mail->getNumber()));
00234         return 0;
00235     }
00236     r = mailmessage_fetch(msg,&data,&size);
00237     if (r != MAIL_NO_ERROR) {
00238         Global::statusMessage(tr("Error fetching mail %i").arg(mail->getNumber()));
00239         if (msg) mailmessage_free(msg);
00240         return 0;
00241     }
00242     result = new encodedString(data,size);
00243     if (msg) mailmessage_free(msg);
00244     return result;
00245 }
00246 
00247 void MHwrapper::deleteMails(const QString & mailbox,const QValueList<RecMailP> &target)
00248 {
00249     QString f = buildPath(mailbox);
00250     int r = mailsession_select_folder(m_storage->sto_session,(char*)f.latin1());
00251     if (r!=MAIL_NO_ERROR) {
00252         odebug << "deleteMails: error selecting folder!" << oendl;
00253         return;
00254     }
00255     QValueList<RecMailP>::ConstIterator it;
00256     for (it=target.begin(); it!=target.end();++it) {
00257         r = mailsession_remove_message(m_storage->sto_session,(*it)->getNumber());
00258         if (r != MAIL_NO_ERROR) {
00259             odebug << "error deleting mail" << oendl;
00260             break;
00261         }
00262     }
00263 }
00264 
00265 int MHwrapper::deleteAllMail(const FolderP&tfolder)
00266 {
00267     init_storage();
00268     if (!m_storage) {
00269         return 0;
00270     }
00271     int res = 1;
00272     if (!tfolder) return 0;
00273     int r = mailsession_select_folder(m_storage->sto_session,(char*)tfolder->getName().latin1());
00274     if (r!=MAIL_NO_ERROR) {
00275         odebug << "error selecting folder!" << oendl;
00276         return 0;
00277     }
00278     mailmessage_list*l=0;
00279     r = mailsession_get_messages_list(m_storage->sto_session,&l);
00280     if (r != MAIL_NO_ERROR) {
00281         odebug << "Error message list" << oendl;
00282         res = 0;
00283     }
00284     unsigned j = 0;
00285     for(unsigned int i = 0 ; l!= 0 && res==1 && i < carray_count(l->msg_tab) ; ++i) {
00286         mailmessage * msg;
00287         msg = (mailmessage*)carray_get(l->msg_tab, i);
00288         j = msg->msg_index;
00289         r = mailsession_remove_message(m_storage->sto_session,j);
00290         if (r != MAIL_NO_ERROR) {
00291             Global::statusMessage(tr("Error deleting mail %1").arg(i+1));
00292             res = 0;
00293             break;
00294         }
00295     }
00296     if (l) mailmessage_list_free(l);
00297     return res;
00298 }
00299 
00300 int MHwrapper::deleteMbox(const FolderP&tfolder)
00301 {
00302     init_storage();
00303     if (!m_storage) {
00304         return 0;
00305     }
00306     if (!tfolder) return 0;
00307     if (tfolder->getName()=="/" || tfolder->getName().isEmpty()) return 0;
00308 
00309     int r = mailsession_delete_folder(m_storage->sto_session,(char*)tfolder->getName().latin1());
00310 
00311     if (r != MAIL_NO_ERROR) {
00312         odebug << "error deleting mail box" << oendl;
00313         return 0;
00314     }
00315     QString cmd = "rm -rf "+tfolder->getName();
00316     QStringList command;
00317     command << "/bin/sh";
00318     command << "-c";
00319     command << cmd.latin1();
00320     OProcess *process = new OProcess();
00321 
00322     connect(process, SIGNAL(processExited(Opie::Core::OProcess*)),
00323             this, SLOT( processEnded(Opie::Core::OProcess*)));
00324     connect(process, SIGNAL( receivedStderr(Opie::Core::OProcess*,char*,int)),
00325             this, SLOT( oprocessStderr(Opie::Core::OProcess*,char*,int)));
00326 
00327     *process << command;
00328     removeMboxfailed = false;
00329     if(!process->start(OProcess::Block, OProcess::All) ) {
00330         odebug << "could not start process" << oendl;
00331         return 0;
00332     }
00333     odebug << "mail box deleted" << oendl;
00334     return 1;
00335 }
00336 
00337 void MHwrapper::processEnded(OProcess *p)
00338 {
00339     if (p) delete p;
00340 }
00341 
00342 void MHwrapper::oprocessStderr(OProcess*, char *buffer, int )
00343 {
00344     QString lineStr = buffer;
00345     QMessageBox::warning( 0, tr("Error"), lineStr ,tr("Ok") );
00346     removeMboxfailed = true;
00347 }
00348 
00349 void MHwrapper::statusFolder(folderStat&target_stat,const QString & mailbox)
00350 {
00351     init_storage();
00352     if (!m_storage) {
00353         return;
00354     }
00355     target_stat.message_count = 0;
00356     target_stat.message_unseen = 0;
00357     target_stat.message_recent = 0;
00358     QString f = buildPath(mailbox);
00359     int r = mailsession_status_folder(m_storage->sto_session,(char*)f.latin1(),&target_stat.message_count,
00360             &target_stat.message_recent,&target_stat.message_unseen);
00361     if (r != MAIL_NO_ERROR) {
00362         Global::statusMessage(tr("Error retrieving status"));
00363     }
00364 }
00365 
00366 MAILLIB::ATYPE MHwrapper::getType()const
00367 {
00368     return MAILLIB::A_MH;
00369 }
00370 
00371 const QString&MHwrapper::getName()const
00372 {
00373     return MHName;
00374 }
00375 void MHwrapper::mvcpMail(const RecMailP&mail,const QString&targetFolder,AbstractMail*targetWrapper,bool moveit)
00376 {
00377     init_storage();
00378     if (!m_storage) {
00379         return;
00380     }
00381     if (targetWrapper != this) {
00382         odebug << "Using generic" << oendl;
00383         Genericwrapper::mvcpMail(mail,targetFolder,targetWrapper,moveit);
00384         return;
00385     }
00386     odebug << "Using internal routines for move/copy" << oendl;
00387     QString tf = buildPath(targetFolder);
00388     int r = mailsession_select_folder(m_storage->sto_session,(char*)mail->getMbox().latin1());
00389     if (r != MAIL_NO_ERROR) {
00390         odebug << "Error selecting source mailbox" << oendl;
00391         return;
00392     }
00393     if (moveit) {
00394         r = mailsession_move_message(m_storage->sto_session,mail->getNumber(),(char*)tf.latin1());
00395     } else {
00396         r = mailsession_copy_message(m_storage->sto_session,mail->getNumber(),(char*)tf.latin1());
00397     }
00398     if (r != MAIL_NO_ERROR) {
00399         odebug << "Error copy/moving mail internal (" << r << ")" << oendl;
00400     }
00401 }
00402 
00403 void MHwrapper::mvcpAllMails(const FolderP&fromFolder,
00404     const QString&targetFolder,AbstractMail*targetWrapper,bool moveit)
00405 {
00406     init_storage();
00407     if (!m_storage) {
00408         return;
00409     }
00410     if (targetWrapper != this) {
00411         odebug << "Using generic" << oendl;
00412         Genericwrapper::mvcpAllMails(fromFolder,targetFolder,targetWrapper,moveit);
00413         return;
00414     }
00415     if (!fromFolder) return;
00416     int r = mailsession_select_folder(m_storage->sto_session,(char*)fromFolder->getName().latin1());
00417     if (r!=MAIL_NO_ERROR) {
00418         odebug << "error selecting source folder!" << oendl;
00419         return;
00420     }
00421     QString tf = buildPath(targetFolder);
00422     mailmessage_list*l=0;
00423     r = mailsession_get_messages_list(m_storage->sto_session,&l);
00424     if (r != MAIL_NO_ERROR) {
00425         odebug << "Error message list" << oendl;
00426     }
00427     unsigned j = 0;
00428     for(unsigned int i = 0 ; l!= 0 && i < carray_count(l->msg_tab) ; ++i) {
00429         mailmessage * msg;
00430         msg = (mailmessage*)carray_get(l->msg_tab, i);
00431         j = msg->msg_index;
00432         if (moveit) {
00433             r = mailsession_move_message(m_storage->sto_session,j,(char*)tf.latin1());
00434         } else {
00435             r = mailsession_copy_message(m_storage->sto_session,j,(char*)tf.latin1());
00436         }
00437         if (r != MAIL_NO_ERROR) {
00438             odebug << "Error copy/moving mail internal (" << r << ")" << oendl;
00439             break;
00440         }
00441     }
00442     if (l) mailmessage_list_free(l);
00443 }

Generated on Sat Nov 5 16:17:38 2005 for OPIE by  doxygen 1.4.2