00001 #include <qmessagebox.h>
00002 #include <qpushbutton.h>
00003 #include <qcombobox.h>
00004 #include <qlineedit.h>
00005
00006 #include <qpe/qpeapplication.h>
00007
00008 #include "imaphandler.h"
00009 #include "searchdiag.h"
00010 #include "viewmail.h"
00011
00012 #define INMENU_BODY 0
00013 #define INMENU_HEADERF 1
00014 #define INMENU_SUBJECT 2
00015 #define INMENU_FROM 3
00016 #define INMENU_TO 4
00017
00018 SearchDiag::SearchDiag(QWidget *parent, const char *name, WFlags fl)
00019 : SearchDiagBase(parent, name, fl)
00020 {
00021 _selected = false;
00022
00023 in->insertItem(tr("Body"), INMENU_BODY);
00024 in->insertItem(tr("Header Field"), INMENU_HEADERF);
00025 in->insertItem(tr("Subject"), INMENU_SUBJECT);
00026 in->insertItem(tr("From"), INMENU_FROM);
00027 in->insertItem(tr("To"), INMENU_TO);
00028
00029 connect(folderView, SIGNAL(folderSelected(Folder)), SLOT(folderSelected(Folder)));
00030 connect(in, SIGNAL(activated(int)), SLOT(slotInItemActivated(int)));
00031 connect(mailTable, SIGNAL(mailClicked(IMAPResponseFETCH,IMAPHandler*)), SLOT(slotMailClicked(IMAPResponseFETCH,IMAPHandler*)));
00032 }
00033
00034 void SearchDiag::accept()
00035 {
00036 if (searchFor->text().isEmpty()) {
00037 QMessageBox::information(this, tr("Error"), tr("<p>Please enter what to search for.</p>"), tr("Ok"));
00038 return;
00039 }
00040
00041 if (!_selected) {
00042 QMessageBox::information(this, tr("Error"), tr("<p>Please select a folder.</p>"), tr("Ok"));
00043 return;
00044 }
00045
00046 if (in->currentItem() == INMENU_HEADERF && other->currentText().isEmpty()) {
00047 QMessageBox::information(this, tr("Error"), tr("<p>Please enter a header field to search in.</p>"), tr("Ok"));
00048 return;
00049 }
00050
00051 _folder.topFolder().handler()->iSelect(_folder.fullName());
00052 connect(_folder.topFolder().handler(), SIGNAL(gotResponse(IMAPResponse&)), SLOT(slotIMAPSelect(IMAPResponse&)));
00053 }
00054
00055 void SearchDiag::folderSelected(Folder folder)
00056 {
00057 _selected = true;
00058 _folder = folder;
00059 }
00060
00061 void SearchDiag::slotIMAPSelect(IMAPResponse &response)
00062 {
00063 disconnect(response.imapHandler(), SIGNAL(gotResponse(IMAPResponse&)), this, SLOT(slotIMAPSelect(IMAPResponse&)));
00064
00065 if (response.statusResponse().status() == IMAPResponseEnums::OK) {
00066 if (in->currentItem() == INMENU_BODY) {
00067 response.imapHandler()->iSearch("BODY \"" + searchFor->text() + "\"");
00068 } else if (in->currentItem() == INMENU_HEADERF) {
00069 response.imapHandler()->iSearch("HEADER \""+ other->currentText() + "\" \"" + searchFor->text() + "\"");
00070 } else if (in->currentItem() == INMENU_SUBJECT) {
00071 response.imapHandler()->iSearch("SUBJECT \"" + searchFor->text() + "\"");
00072 } else if (in->currentItem() == INMENU_FROM) {
00073 response.imapHandler()->iSearch("FROM \"" + searchFor->text() + "\"");
00074 } else if (in->currentItem() == INMENU_TO) {
00075 response.imapHandler()->iSearch("TO \"" + searchFor->text() + "\"");
00076 } else return;
00077
00078 connect(response.imapHandler(), SIGNAL(gotResponse(IMAPResponse&)), SLOT(slotIMAPSearch(IMAPResponse&)));
00079 } else {
00080 QMessageBox::warning(this, tr("Error"), tr("<p>Could not select the folder. Aborting. (Server said: %1)").arg(response.statusResponse().comment()), tr("Ok"));
00081 }
00082 }
00083
00084 void SearchDiag::slotIMAPSearch(IMAPResponse &response)
00085 {
00086 disconnect(response.imapHandler(), SIGNAL(gotResponse(IMAPResponse&)), this, SLOT(slotIMAPSearch(IMAPResponse&)));
00087
00088 if (response.statusResponse().status() == IMAPResponseEnums::OK) {
00089 IMAPResponseSEARCH results = response.SEARCH()[0];
00090 if (results.mails().count() == 0) {
00091 QMessageBox::information(this, tr("Results"), tr("<p>No mails match your criteria.</p>"), tr("Ok"));
00092 return;
00093 }
00094
00095 response.imapHandler()->iFetch(results.mails().join(","), "ENVELOPE FLAGS UID");
00096 connect(response.imapHandler(), SIGNAL(gotResponse(IMAPResponse&)), SLOT(slotIMAPFetch(IMAPResponse&)));
00097 } else {
00098 QMessageBox::warning(this, tr("Error"), tr("<p>Search failed. (Server said: %1)").arg(response.statusResponse().comment()), tr("Ok"));
00099 }
00100 }
00101
00102 void SearchDiag::slotIMAPFetch(IMAPResponse &response)
00103 {
00104 disconnect(response.imapHandler(), SIGNAL(gotResponse(IMAPResponse&)), this, SLOT(slotIMAPSearch(IMAPResponse&)));
00105
00106 if (response.statusResponse().status() == IMAPResponseEnums::OK) {
00107 mailTable->setHeaders(response.FETCH());
00108 } else {
00109 QMessageBox::warning(this, tr("Error"), tr("<p>Couldn't fetch the mail headers. (Server said: %1)").arg(response.statusResponse().comment()));
00110 }
00111 }
00112
00113 void SearchDiag::slotMailClicked(IMAPResponseFETCH fetch, IMAPHandler *)
00114 {
00115 ViewMail viewMail(fetch, _folder.topFolder().handler(), this, 0, true);
00116 viewMail.showMaximized();
00117 viewMail.exec();
00118 }
00119
00120 void SearchDiag::slotInItemActivated(int index)
00121 {
00122 if (index == INMENU_HEADERF) {
00123 other->setEnabled(true);
00124 } else {
00125 other->setEnabled(false);
00126 }
00127 }
00128