00001 #include <qpainter.h>
00002 #include <qtimer.h>
00003
00004 #include <qpe/qcopenvelope_qws.h>
00005 #include <qpe/resource.h>
00006 #include <qpe/config.h>
00007 #include <qpe/applnk.h>
00008 #include <opie2/odebug.h>
00009 #include <opie2/odevice.h>
00010
00011 #include <libmailwrapper/settings.h>
00012
00013 #include "mailapplet.h"
00014
00015
00016 #include <signal.h>
00017
00018 using namespace Opie::Core;
00019
00020 MailApplet::MailApplet( QWidget *parent )
00021 : QLabel( parent ) {
00022
00023 setFixedHeight(AppLnk::smallIconSize());
00024 setMinimumWidth(AppLnk::smallIconSize());
00025
00026 hide();
00027
00028 m_newMails = 0;
00029 m_statusMail = 0l;
00030
00031
00032 struct sigaction blocking_action,temp_action;
00033 blocking_action.sa_handler = SIG_IGN;
00034 sigemptyset(&(blocking_action.sa_mask));
00035 blocking_action.sa_flags = 0;
00036 sigaction(SIGPIPE,&blocking_action,&temp_action);
00037
00038 QTimer::singleShot( 5000, this, SLOT( startup() ) );
00039 }
00040
00041
00042 MailApplet::~MailApplet() {
00043 if ( m_statusMail )
00044 delete m_statusMail;
00045 }
00046
00047 void MailApplet::paintEvent( QPaintEvent*ev )
00048 {
00049 QPainter p( this );
00050 p.drawPixmap( 0, 0, Resource::loadPixmap( "mail/inbox" ) );
00051 QLabel::paintEvent(ev);
00052 #if 0
00053 QFont f( "vera", AppLnk::smallIconSize() );
00054 QFontMetrics fm( f );
00055 p.setFont( f );
00056 p.setPen( Qt::blue );
00057 p.drawText( AppLnk::smallIconSize()/3, AppLnk::smallIconSize() - 2, QString::number( m_newMails ) );
00058 #endif
00059 return;
00060 }
00061
00062 void MailApplet::mouseReleaseEvent( QMouseEvent* e ) {
00063 slotClicked();
00064 }
00065
00066 void MailApplet::slotClicked()
00067 {
00068 {
00069 QCopEnvelope e( "QPE/System", "execute(QString)" );
00070 e << QString( "opiemail" );
00071 }
00072
00073 ledOnOff(false);
00074 if (m_statusMail)
00075 m_statusMail->reset_status();
00076 hide();
00077 }
00078
00079 void MailApplet::startup()
00080 {
00081 Settings *settings = new Settings();
00082 QList<Account> ma = settings->getAccounts();
00083 m_statusMail = new StatusMail( ma );
00084 delete settings;
00085
00086
00087 m_intervalMs = 100;
00088 m_intervalTimer = new QTimer();
00089 m_intervalTimer->start( m_intervalMs );
00090 connect( m_intervalTimer, SIGNAL( timeout() ), this, SLOT( slotCheck() ) );
00091 }
00092
00093 void MailApplet::ledOnOff(bool how)
00094 {
00095 ODevice *device = ODevice::inst();
00096 if ( !device->ledList().isEmpty() ) {
00097 OLed led = ( device->ledList().contains( Led_Mail ) ) ? Led_Mail : device->ledList()[0];
00098 device->setLedState( led, (how?(device->ledStateList( led ).contains( Led_BlinkSlow )?Led_BlinkSlow:Led_On):Led_Off) );
00099 }
00100 }
00101
00102 void MailApplet::slotCheck() {
00103
00104 odebug << "MailApplet::slotCheck()" << oendl;
00105 Config m_config( "mail" );
00106 m_config.setGroup( "Applet" );
00107
00108 int newIntervalMs = m_config.readNumEntry( "CheckEvery", 5 ) * 60000;
00109 if ( newIntervalMs != m_intervalMs ) {
00110 m_intervalTimer->changeInterval( newIntervalMs );
00111 m_intervalMs = newIntervalMs;
00112 }
00113 if ( m_config.readBoolEntry( "Disabled", false ) ) {
00114 hide();
00115 ledOnOff(false);
00116 odebug << "MailApplet::slotCheck() - disabled" << oendl;
00117 return;
00118 }
00119 if (m_statusMail == 0) {
00120 odebug << "MailApplet::slotCheck() - no mailhandle" << oendl;
00121 return;
00122 }
00123 folderStat stat;
00124 m_statusMail->check_current_stat( stat );
00125 int newMailsOld = m_newMails;
00126 m_newMails = stat.message_unseen;
00127 odebug << QString( "test %1" ).arg( m_newMails ) << oendl;
00128 if ( m_newMails > 0) {
00129 if (isHidden())
00130 show();
00131 if (newMailsOld != m_newMails) {
00132 if ( m_config.readBoolEntry( "BlinkLed", true ) ) {
00133 ledOnOff(true);
00134 }
00135 if ( m_config.readBoolEntry( "PlaySound", false ) )
00136 ODevice::inst()->playAlarmSound();
00137 m_config.setGroup( "Status" );
00138 m_config.writeEntry( "newMails", m_newMails );
00139 {
00140 QCopEnvelope env( "QPE/Pim", "newMails(int)" );
00141 env << m_newMails;
00142 }
00143 setText(QString::number( m_newMails ));
00144 }
00145 } else {
00146 ODevice *device = ODevice::inst();
00147 if ( !isHidden() )
00148 hide();
00149 if ( newMailsOld != m_newMails ) {
00150 ledOnOff(false);
00151 m_config.setGroup( "Status" );
00152 m_config.writeEntry( "newMails", m_newMails );
00153 QCopEnvelope env( "QPE/Pim", "newMails(int)" );
00154 env << m_newMails;
00155 }
00156 }
00157 }