00001 #include <qlayout.h>
00002 #include <qpixmap.h>
00003 #include <qlabel.h>
00004 #include <qsound.h>
00005 #include <qtimer.h>
00006 #include <qdir.h>
00007
00008 #include <qpe/qcopenvelope_qws.h>
00009 #include <qpe/resource.h>
00010 #include <qpe/config.h>
00011
00012 #include <opie2/odevice.h>
00013
00014 #include "imapresponse.h"
00015 #include "imaphandler.h"
00016 #include "configfile.h"
00017 #include "bend.h"
00018
00019 using namespace Opie;
00020
00021 BenD::BenD(QWidget *parent, const char *name, WFlags fl)
00022 : QButton(parent, name, fl)
00023 {
00024 _config = new Config("mail");
00025 _config->setGroup("Settings");
00026
00027 QVBoxLayout *layout = new QVBoxLayout(this);
00028 layout->addItem(new QSpacerItem(0,0));
00029
00030 QLabel *pixmap = new QLabel(this);
00031 pixmap->setPixmap(Resource::loadPixmap("mail/mailchecker"));
00032 layout->addWidget(pixmap);
00033
00034 layout->addItem(new QSpacerItem(0,0));
00035
00036 hide();
00037
00038 connect(this, SIGNAL(clicked()), SLOT(slotClicked()));
00039
00040 if (!_config->readBoolEntry("Disabled", false)) {
00041 _intervalMs = _config->readNumEntry("CheckEvery", 5) * 60000;
00042 _intervalTimer = new QTimer();
00043 _intervalTimer->start(_intervalMs);
00044 connect(_intervalTimer, SIGNAL(timeout()), SLOT(slotCheck()));
00045
00046 QTimer::singleShot(0, this, SLOT(slotCheck()));
00047 }
00048 }
00049
00050 void BenD::drawButton(QPainter *) { }
00051 void BenD::drawButtonText(QPainter *) { }
00052
00053 void BenD::slotClicked()
00054 {
00055 QCopEnvelope e("QPE/System", "execute(QString)");
00056 e << QString("mail");
00057
00058 ODevice *device = ODevice::inst();
00059 if ( !device-> ledList ( ). isEmpty ( )) {
00060 OLed led = ( device-> ledList ( ). contains ( Led_Mail )) ? Led_Mail : device-> ledList ( ) [0];
00061
00062 device->setLedState(led, Led_Off);
00063 }
00064 }
00065
00066 void BenD::slotCheck()
00067 {
00068
00069 int newIntervalMs = _config->readNumEntry("CheckEvery", 5) * 60000;
00070 if (newIntervalMs != _intervalMs) {
00071 _intervalTimer->changeInterval(newIntervalMs);
00072 _intervalMs = newIntervalMs;
00073 #ifndef QT_NO_DEBUG
00074 qWarning("BenD: Detected interval change");
00075 #endif
00076 }
00077
00078 QValueList<Account> acList = ConfigFile::getAccounts();
00079 QValueList<Account>::Iterator ot;
00080 for (ot = acList.begin(); ot != acList.end(); ot++) {
00081 if (!((*ot).imapServer().isEmpty() ||
00082 (*ot).imapPort().isEmpty() ||
00083 (*ot).user().isEmpty() ||
00084 (*ot).pass().isEmpty())) {
00085 if (!((*ot).imapSsl() &&
00086 (*ot).imapSslPort().isEmpty())) {
00087 IMAPHandler *handler = new IMAPHandler(*ot);
00088 handler->iStatus("INBOX", "RECENT");
00089 connect(handler, SIGNAL(gotResponse(IMAPResponse&)), SLOT(slotIMAPStatus(IMAPResponse&)));
00090 }
00091 }
00092 }
00093 }
00094
00095 void BenD::slotIMAPStatus(IMAPResponse &response)
00096 {
00097 disconnect(response.imapHandler(), SIGNAL(gotResponse(IMAPResponse&)), this, SLOT(slotIMAPStatus(IMAPResponse&)));
00098
00099 if (response.statusResponse().status() == IMAPResponseEnums::OK) {
00100 if (response.STATUS()[0].recent().toInt() > 0) {
00101 ODevice *device = ODevice::inst();
00102 if (isHidden()) show();
00103 if (_config->readBoolEntry("BlinkLed", true)) {
00104 if ( !device-> ledList ( ). isEmpty ( )) {
00105 OLed led = ( device-> ledList ( ). contains ( Led_Mail )) ? Led_Mail : device-> ledList ( ) [0];
00106
00107 device->setLedState(led, device-> ledStateList ( led ). contains ( Led_BlinkSlow ) ? Led_BlinkSlow : Led_On );
00108 }
00109 }
00110 if (_config->readBoolEntry("PlaySound", false))
00111 device->alarmSound();
00112 } else {
00113 ODevice *device = ODevice::inst();
00114 if (!isHidden()) hide();
00115 if ( !device-> ledList ( ). isEmpty ( )) {
00116 OLed led = ( device-> ledList ( ). contains ( Led_Mail )) ? Led_Mail : device-> ledList ( ) [0];
00117
00118 device->setLedState(led, Led_Off);
00119 }
00120 }
00121 response.imapHandler()->iLogout();
00122 } else qWarning("BenD: WARNING: Couldn't retrieve INBOX status.");
00123 }
00124