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

serverapp.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2003 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #include "serverapp.h"
00022 #include "screensaver.h"
00023 
00024 /* OPIE */
00025 #include <opie2/odebug.h>
00026 #include <opie2/odevice.h>
00027 #include <opie2/multiauthpassword.h>
00028 
00029 #include <qtopia/config.h>
00030 #include <qtopia/power.h>
00031 
00032 #ifdef Q_WS_QWS
00033 #include <qtopia/qcopenvelope_qws.h>
00034 #endif
00035 #include <qtopia/global.h>
00036 using namespace Opie::Core;
00037 
00038 /* QT */
00039 #ifdef Q_WS_QWS
00040 #include <qgfx_qws.h>
00041 #endif
00042 #include <qmessagebox.h>
00043 #include <qtimer.h>
00044 #include <qpainter.h>
00045 #include <qfile.h>
00046 #include <qpixmapcache.h>
00047 
00048 /* STD */
00049 #ifdef Q_OS_WIN32
00050 #include <io.h>
00051 #include <process.h>
00052 #else
00053 #include <unistd.h>
00054 #endif
00055 #include <stdlib.h>
00056 
00057 static ServerApplication *serverApp = 0;
00058 static int loggedin=0;
00059 
00060 QCopKeyRegister::QCopKeyRegister()
00061     : m_keyCode( 0 )
00062 {
00063 
00064 }
00065 
00066 QCopKeyRegister::QCopKeyRegister( int k, const QCString& c, const QCString& m )
00067     :m_keyCode( k ), m_channel( c ), m_message( m )
00068 {
00069 
00070 }
00071 
00072 int QCopKeyRegister::keyCode() const
00073 {
00074     return m_keyCode;
00075 }
00076 
00077 QCString QCopKeyRegister::channel() const
00078 {
00079     return m_channel;
00080 }
00081 
00082 QCString QCopKeyRegister::message() const
00083 {
00084     return m_message;
00085 }
00086 
00087 bool QCopKeyRegister::send()
00088 {
00089     if (m_channel.isNull() )
00090         return false;
00091 
00092     QCopEnvelope e( m_channel, m_message );
00093 
00094     return true;
00095 }
00096 
00097 //---------------------------------------------------------------------------
00098 
00099 /*
00100   Priority is number of alerts that are needed to pop up
00101   alert.
00102  */
00103 class DesktopPowerAlerter : public QMessageBox
00104 {
00105     Q_OBJECT
00106 public:
00107     DesktopPowerAlerter( QWidget *parent, const char *name = 0 )
00108     : QMessageBox( tr("Battery Status"), tr("Low Battery"),
00109                QMessageBox::Critical,
00110                QMessageBox::Ok | QMessageBox::Default,
00111                QMessageBox::NoButton, QMessageBox::NoButton,
00112                parent, name, FALSE )
00113     {
00114     currentPriority = INT_MAX;
00115     alertCount = 0;
00116     }
00117 
00118     void alert( const QString &text, int priority );
00119     void hideEvent( QHideEvent * );
00120 private:
00121     int currentPriority;
00122     int alertCount;
00123 };
00124 
00125 void DesktopPowerAlerter::alert( const QString &text, int priority )
00126 {
00127     alertCount++;
00128     if ( alertCount < priority )
00129     return;
00130     if ( priority > currentPriority )
00131     return;
00132     currentPriority = priority;
00133     setText( text );
00134     show();
00135 }
00136 
00137 
00138 void DesktopPowerAlerter::hideEvent( QHideEvent *e )
00139 {
00140     QMessageBox::hideEvent( e );
00141     alertCount = 0;
00142     currentPriority = INT_MAX;
00143 }
00144 
00145 //---------------------------------------------------------------------------
00146 
00147 KeyFilter::KeyFilter(QObject* parent) : QObject(parent), held_tid(0), heldButton(0),firedHeldButton(0)
00148 {
00149     /* We don't do this cause it would interfere with ODevice */
00150 #if 0
00151     qwsServer->setKeyboardFilter(this);
00152 #endif
00153 }
00154 
00155 void KeyFilter::timerEvent(QTimerEvent* e)
00156 {
00157     if ( e->timerId() == held_tid ) {
00158     killTimer(held_tid);
00159     // button held
00160     if ( heldButton ) {
00161         emit activate(heldButton, TRUE);
00162         firedHeldButton = heldButton;
00163         heldButton = 0;
00164     }
00165     held_tid = 0;
00166     }
00167 }
00168 
00169 void KeyFilter::registerKey( const QCopKeyRegister& key )
00170 {
00171 
00172     m_keys.insert( key.keyCode(), key );
00173 }
00174 
00175 void KeyFilter::unregisterKey( const QCopKeyRegister& key )
00176 {
00177     m_keys.remove( key.keyCode() );
00178 }
00179 
00180 bool KeyFilter::keyRegistered( int key )
00181 {
00182     /*
00183      * Check if we've a key registered
00184      */
00185     if ( !m_keys[key].send())
00186         return false;
00187     else
00188         return true;
00189 }
00190 
00191 bool KeyFilter::checkButtonAction(bool db, int keycode,  int press, int autoRepeat)
00192 {
00193     if ( !loggedin
00194         // Permitted keys
00195         && keycode != Key_F34 // power
00196         && keycode != Key_F30 // select
00197         && keycode != Key_Enter
00198         && keycode != Key_Return
00199         && keycode != Key_Space
00200         && keycode != Key_Left
00201         && keycode != Key_Right
00202         && keycode != Key_Up
00203         && keycode != Key_Down )
00204     return TRUE;
00205 
00206 //    odebug << " KeyFilter::checkButtonAction("<<db<<","<<keycode<<","<<press<<","<<autoRepeat<<")"<<oendl;
00207         /* check if it was registered */
00208     if (!db ) {
00209         if (keycode != 0 &&press && !autoRepeat && keyRegistered(keycode) )
00210             return true;
00211     } else {
00212         // First check to see if DeviceButtonManager knows something about this button:
00213         const ODeviceButton* button = ODevice::inst()->buttonForKeycode(keycode);
00214         if (button && !autoRepeat) {
00215             if (firedHeldButton) {
00216                 if (held_tid) {
00217                     killTimer(held_tid);
00218                     held_tid = 0;
00219                 }
00220                 if (!press && button == firedHeldButton) {
00221                     firedHeldButton = 0;
00222                     return TRUE;
00223                 }
00224             } else if ( held_tid && heldButton != button) {
00225                 killTimer(held_tid);
00226                 held_tid = 0;
00227             }
00228             if ( button->heldAction().isNull() ) {
00229                 if ( press )
00230                     emit activate(button, FALSE);
00231             } else if ( press ) {
00232                 if (heldButton != button) {
00233                    heldButton = button;
00234                    held_tid = startTimer( ODevice::inst ()->buttonHoldTime () );
00235                 }
00236             } else if ( heldButton ) {
00237                 heldButton = 0;
00238                 emit activate(button, FALSE);
00239             }
00240             QWSServer::screenSaverActivate(FALSE);
00241             return TRUE;
00242         }
00243         return false;
00244     }
00245     if ( keycode == HardKey_Suspend ) {
00246         if ( press ) emit power();
00247         return TRUE;
00248     }
00249     if ( keycode == HardKey_Backlight ) {
00250         if ( press ) emit backlight();
00251         return TRUE;
00252     }
00253     if ( keycode == Key_F32 ) {
00254 #ifndef QT_NO_COP
00255         if ( press ) QCopEnvelope e( "QPE/Desktop", "startSync()" );
00256 #endif
00257         return TRUE;
00258     }
00259     if ( keycode == Key_F31 ) {
00260         if ( press ) emit symbol();
00261         QWSServer::screenSaverActivate(FALSE);
00262         return TRUE;
00263     }
00264 
00265     if ( keycode == Key_NumLock )
00266         if ( press ) emit numLockStateToggle();
00267 
00268     if ( keycode == Key_CapsLock )
00269         if ( press ) emit capsLockStateToggle();
00270 
00271     if ( serverApp )
00272         serverApp->keyClick(keycode,press,autoRepeat);
00273 
00274     return FALSE;
00275 }
00276 
00277 enum MemState { MemUnknown, MemVeryLow, MemLow, MemNormal } memstate=MemUnknown;
00278 
00279 #if defined(QPE_HAVE_MEMALERTER)
00280 QPE_MEMALERTER_IMPL
00281 #endif
00282 
00283 
00284 
00285 //---------------------------------------------------------------------------
00286 
00287 bool ServerApplication::doRestart = FALSE;
00288 bool ServerApplication::allowRestart = TRUE;
00289 bool ServerApplication::ms_is_starting  = TRUE;
00290 
00291 void ServerApplication::switchLCD( bool on )
00292 {
00293     if ( !qApp )
00294         return;
00295 
00296     ServerApplication *dapp = ServerApplication::me() ;
00297 
00298     if ( !dapp-> m_screensaver )
00299         return;
00300 
00301     if ( on ) {
00302         dapp-> m_screensaver-> setDisplayState ( true );
00303         dapp-> m_screensaver-> setBacklight ( -3 );
00304     } else
00305         dapp-> m_screensaver-> setDisplayState ( false );
00306 
00307 
00308 }
00309 
00310 ServerApplication::ServerApplication( int& argc, char **argv, Type t )
00311     : Opie::Core::OApplication( argc, argv, t )
00312 {
00313     ms_is_starting = true;
00314 
00315     // We know we'll have lots of cached pixmaps due to App/DocLnks
00316     QPixmapCache::setCacheLimit(512);
00317 
00318     m_ps = new PowerStatus;
00319     m_ps_last = new PowerStatus;
00320     pa = new DesktopPowerAlerter( 0 );
00321 
00322     m_apm_timer = new QTimer( this );
00323     connect(m_apm_timer, SIGNAL( timeout() ),
00324             this, SLOT( apmTimeout() ) );
00325 
00326     reloadPowerWarnSettings();
00327 
00328     QCopChannel *channel = new QCopChannel( "QPE/System", this );
00329     connect(channel, SIGNAL(received(const QCString&,const QByteArray&) ),
00330             this, SLOT(systemMessage(const QCString&,const QByteArray&) ) );
00331 
00332     channel = new QCopChannel("QPE/Launcher", this );
00333     connect(channel, SIGNAL(received(const QCString&,const QByteArray&) ),
00334             this, SLOT(launcherMessage(const QCString&,const QByteArray&) ) );
00335 
00336     m_screensaver = new OpieScreenSaver();
00337     m_screensaver->setInterval( -1 );
00338     QWSServer::setScreenSaver( m_screensaver );
00339 
00340     connect( qApp, SIGNAL( volumeChanged(bool) ),
00341              this, SLOT( rereadVolumes() ) );
00342 
00343 
00344     /* ### PluginLoader libqtopia SafeMode */
00345 #if 0
00346     if ( PluginLoader::inSafeMode() )
00347     QTimer::singleShot(500, this, SLOT(showSafeMode()) );
00348     QTimer::singleShot(20*1000, this, SLOT(clearSafeMode()) );
00349 #endif
00350 
00351     kf = new KeyFilter(this);
00352 
00353     connect( kf, SIGNAL(launch()), this, SIGNAL(launch()) );
00354     connect( kf, SIGNAL(power()), this, SIGNAL(power()) );
00355     connect( kf, SIGNAL(backlight()), this, SIGNAL(backlight()) );
00356     connect( kf, SIGNAL(symbol()), this, SIGNAL(symbol()));
00357     connect( kf, SIGNAL(numLockStateToggle()), this,SIGNAL(numLockStateToggle()));
00358     connect( kf, SIGNAL(capsLockStateToggle()), this,SIGNAL(capsLockStateToggle()));
00359     connect( kf, SIGNAL(activate(const Opie::Core::ODeviceButton*,bool)),
00360              this,SIGNAL(activate(const Opie::Core::ODeviceButton*,bool)));
00361 
00362 
00363     connect( kf, SIGNAL(backlight()), this, SLOT(toggleLight()) );
00364 
00365     connect( this, SIGNAL(power() ),
00366              SLOT(togglePower() ) );
00367 
00368     rereadVolumes();
00369 
00370     serverApp = this;
00371 
00372     apmTimeout();
00373     grabKeyboard();
00374 
00375     /* make sure the event filter is installed */  /* std::limits<short>::max() when you've stdc++ */
00376     const ODeviceButton* but = ODevice::inst()->buttonForKeycode( SHRT_MAX );
00377     Q_CONST_UNUSED( but )
00378 }
00379 
00380 
00381 ServerApplication::~ServerApplication()
00382 {
00383     ungrabKeyboard();
00384 
00385     delete pa;
00386     delete m_ps;
00387     delete m_ps_last;
00388 }
00389 
00390 void ServerApplication::apmTimeout()
00391 {
00392     serverApp-> checkMemory( ); // in case no events are generated
00393     *m_ps_last = *m_ps;
00394     *m_ps = PowerStatusManager::readStatus();
00395 
00396     if ( m_ps->acStatus() != m_ps_last-> acStatus() )
00397         m_screensaver-> powerStatusChanged( *m_ps );
00398 
00399     if ( m_ps->acStatus() == PowerStatus::Online ) {
00400         return;
00401     }
00402 
00403     int bat = m_ps-> batteryPercentRemaining();
00404 
00405     if ( bat < m_ps_last-> batteryPercentRemaining() ) {
00406         if ( bat <= m_powerCritical ) {
00407         QMessageBox battlow(
00408         tr("WARNING"),
00409         tr("<p>The battery level is critical!"
00410             "<p>Keep power off until AC is restored"),
00411         QMessageBox::Warning,
00412         QMessageBox::Cancel, QMessageBox::NoButton, QMessageBox::NoButton,
00413         0, QString::null, TRUE, WStyle_StaysOnTop);
00414         battlow.setButtonText(QMessageBox::Cancel, tr("Ok"));
00415         battlow.exec();
00416         } else if ( bat <= m_powerVeryLow )
00417             pa->alert( tr( "The battery is running very low. "), 2 );
00418     }
00419 
00420     if ( m_ps-> backupBatteryStatus() == PowerStatus::VeryLow ) {
00421         QMessageBox battlow(
00422         tr("WARNING"),
00423         tr("<p>The Back-up battery is very low"
00424             "<p>Please charge the back-up battery"),
00425         QMessageBox::Warning,
00426         QMessageBox::Cancel, QMessageBox::NoButton, QMessageBox::NoButton,
00427         0, QString::null, TRUE, WStyle_StaysOnTop);
00428         battlow.setButtonText(QMessageBox::Cancel, tr("Ok"));
00429         battlow.exec();
00430     }
00431 }
00432 
00433 void ServerApplication::systemMessage( const QCString& msg,
00434                                        const QByteArray& data )
00435 {
00436     QDataStream stream ( data, IO_ReadOnly );
00437 
00438     if ( msg == "setScreenSaverInterval(int)" ) {
00439         int time;
00440         stream >> time;
00441         m_screensaver-> setInterval( time );
00442     }
00443     else if ( msg == "setScreenSaverIntervals(int,int,int)" ) {
00444         int t1, t2, t3;
00445         stream >> t1 >> t2 >> t3;
00446         m_screensaver-> setIntervals( t1, t2, t3 );
00447     }
00448     else if ( msg == "setBacklight(int)" ) {
00449         int bright;
00450         stream >> bright;
00451         m_screensaver-> setBacklight( bright );
00452     }
00453     else if ( msg == "setScreenSaverMode(int)" ) {
00454         int mode;
00455         stream >> mode;
00456         m_screensaver-> setMode ( mode );
00457     }
00458     else if ( msg == "reloadPowerWarnSettings()" ) {
00459         reloadPowerWarnSettings();
00460     }
00461     else if ( msg == "setDisplayState(int)" ) {
00462         int state;
00463         stream >> state;
00464         m_screensaver-> setDisplayState ( state != 0 );
00465     }
00466     else if ( msg == "suspend()" ) {
00467         emit power();
00468     }
00469     else if ( msg == "sendBusinessCard()" ) {
00470         QString card = ::getenv ( "HOME" );
00471         card += "/Applications/addressbook/businesscard.vcf";
00472 
00473         if ( QFile::exists( card ) ) {
00474             QCopEnvelope e ( "QPE/Obex", "send(QString,QString,QString)" );
00475             QString mimetype = "text/x-vCard";
00476             e << tr( "business card" ) << card << mimetype;
00477         }
00478     }
00479 }
00480 
00481 void ServerApplication::reloadPowerWarnSettings ( )
00482 {
00483     Config cfg ( "apm" );
00484     cfg. setGroup ( "Warnings" );
00485 
00486     int iv = cfg. readNumEntry ( "checkinterval", 10000 );
00487 
00488     m_apm_timer-> stop ( );
00489     if ( iv )
00490         m_apm_timer-> start ( iv );
00491 
00492     m_powerVeryLow  = cfg. readNumEntry ( "powerverylow", 10 );
00493     m_powerCritical = cfg. readNumEntry ( "powervcritical", 5 );
00494 }
00495 
00496 void ServerApplication::launcherMessage( const QCString & msg, const QByteArray & data )
00497 {
00498     QDataStream stream ( data, IO_ReadOnly );
00499 
00500     if ( msg == "deviceButton(int,int,int)" ) {
00501         int keycode, press, autoRepeat;
00502         stream >> keycode >> press >> autoRepeat;
00503 
00504         kf->checkButtonAction ( true, keycode, press, autoRepeat );
00505     }
00506     else if ( msg == "keyRegister(int,QCString,QCString)" ) {
00507         int k;
00508         QCString c, m;
00509         stream >> k >> c >> m;
00510 
00511         kf -> registerKey( QCopKeyRegister(k, c, m) );
00512     }
00513 }
00514 
00515 
00516 bool ServerApplication::screenLocked()
00517 {
00518     return loggedin == 0;
00519 }
00520 
00521 void ServerApplication::login(bool at_poweron)
00522 {
00523     if ( !loggedin ) {
00524         Global::terminateBuiltin("calibrate"); // No tr
00525         int lockMode = at_poweron ? Opie::Security::IfPowerOn : Opie::Security::IfResume;
00526         Opie::Security::MultiauthPassword::authenticate(lockMode);
00527         loggedin=1;
00528 #ifndef QT_NO_COP
00529         QCopEnvelope e( "QPE/Desktop", "unlocked()" );
00530 #endif
00531     }
00532 }
00533 
00534 #if defined(QPE_HAVE_TOGGLELIGHT)
00535 #include <qtopia/config.h>
00536 
00537 #include <sys/ioctl.h>
00538 #include <sys/types.h>
00539 #include <fcntl.h>
00540 #include <unistd.h>
00541 #include <errno.h>
00542 #include <linux/ioctl.h>
00543 #include <time.h>
00544 #endif
00545 
00546 namespace {
00547     void execAutoStart(const QDateTime& suspendTime ) {
00548         QString appName;
00549         int delay;
00550         QDateTime now = QDateTime::currentDateTime();
00551 
00552         Config cfg( "autostart" );
00553         cfg.setGroup( "AutoStart" );
00554         appName = cfg.readEntry( "Apps", "" );
00555         delay = cfg.readNumEntry( "Delay", 0  );
00556 
00557         // If the time between suspend and resume was longer then the
00558         // value saved as delay, start the app
00559         if ( suspendTime.secsTo( now ) >= ( delay * 60 ) && !appName.isEmpty() ) {
00560             QCopEnvelope e( "QPE/System", "execute(QString)" );
00561             e << QString( appName );
00562         }
00563     }
00564 }
00565 
00566 
00567 void ServerApplication::togglePower()
00568 {
00569     static bool excllock = false;
00570 
00571     if ( excllock )
00572         return ;
00573 
00574     excllock = true;
00575 
00576     bool wasloggedin = loggedin;
00577     loggedin = 0;
00578     m_suspendTime = QDateTime::currentDateTime();
00579 
00580 #ifdef QWS
00581     if ( Opie::Security::MultiauthPassword::needToAuthenticate ( true ) && qt_screen ) {
00582         // Should use a big black window instead.
00583         // But this would not show up fast enough
00584         QGfx *g = qt_screen-> screenGfx ( );
00585         g-> fillRect ( 0, 0, qt_screen-> width ( ), qt_screen-> height ( ));
00586         delete g;
00587     }
00588 #endif
00589 
00590     ODevice::inst ( )-> suspend ( );
00591 
00592     ServerApplication::switchLCD ( true ); // force LCD on without slow qcop call
00593     QWSServer::screenSaverActivate ( false );
00594 
00595     {
00596         QCopEnvelope( "QPE/Card", "mtabChanged()" ); // might have changed while asleep
00597     }
00598 
00599     if ( wasloggedin )
00600         login ( true );
00601 
00602     execAutoStart(m_suspendTime);
00603     //qcopBridge->closeOpenConnections();
00604 
00605     excllock = false;
00606 }
00607 
00608 void ServerApplication::toggleLight()
00609 {
00610 #ifndef QT_NO_COP
00611     QCopEnvelope e("QPE/System", "setBacklight(int)");
00612     e << -2; // toggle
00613 #endif
00614 }
00615 
00616 
00617 /*
00618  * We still listen to key events but handle them in
00619  * a special class
00620  */
00621 
00622 bool ServerApplication::eventFilter( QObject *o, QEvent *e) {
00623     if ( e->type() != QEvent::KeyPress &&
00624          e->type() != QEvent::KeyRelease )
00625         return QPEApplication::eventFilter( o, e );
00626 
00627     QKeyEvent *ke = static_cast<QKeyEvent*>( e );
00628     if ( kf->checkButtonAction( true, ke->key(),
00629                                 e->type() == QEvent::KeyPress,
00630                                 ke-> isAutoRepeat() ))
00631         return true;
00632 
00633     return QPEApplication::eventFilter( o, e );
00634 
00635 }
00636 
00637 #ifdef Q_WS_QWS
00638 bool ServerApplication::qwsEventFilter( QWSEvent *e )
00639 {
00640     checkMemory();
00641 
00642     if ( e->type == QWSEvent::Mouse ) {
00643         QWSMouseEvent *me = (QWSMouseEvent *)e;
00644         static bool up = TRUE;
00645         if ( me->simpleData.state&LeftButton ) {
00646             if ( up ) {
00647               up = FALSE;
00648               screenClick(TRUE);
00649             }
00650         } else if ( !up ) {
00651             up = TRUE;
00652             screenClick(FALSE);
00653         }
00654     } else
00655     if ( e->type == QWSEvent::Key ) {
00656         QWSKeyEvent * ke = static_cast<QWSKeyEvent*>( e );
00657         if ( kf->checkButtonAction( false,
00658                                     ke-> simpleData.keycode,
00659                                     ke-> simpleData.is_press,
00660                                     ke-> simpleData.is_auto_repeat ) )
00661             return true;
00662     }
00663 
00664     return QPEApplication::qwsEventFilter( e );
00665 }
00666 #endif
00667 
00668 
00669 /* ### FIXME libqtopia Plugin Safe Mode */
00670 
00671 void ServerApplication::showSafeMode()
00672 {
00673 #if 0
00674     if ( QMessageBox::warning(0, tr("Safe Mode"), tr("<P>A system startup error occurred, "
00675         "and the system is now in Safe Mode. "
00676         "Plugins are not loaded in Safe Mode. "
00677         "You can use the Plugin Manager to "
00678         "disable plugins that cause system error."), tr("OK"), tr("Plugin Manager..."), 0) == 1 )
00679     {
00680         Global::execute( "pluginmanager" );
00681     }
00682 #endif
00683 }
00684 
00685 void ServerApplication::clearSafeMode()
00686 {
00687 #if 0
00688     // If we've been running OK for a while then we won't bother going into
00689     // safe mode immediately on the next crash.
00690     Config cfg( "PluginLoader" );
00691     cfg.setGroup( "Global" );
00692     QString mode = cfg.readEntry( "Mode", "Normal" );
00693     if ( mode == "MaybeSafe" ) {
00694         cfg.writeEntry( "Mode", "Normal" );
00695     }
00696 #endif
00697 }
00698 
00699 
00700 void ServerApplication::shutdown()
00701 {
00702     if ( type() != GuiServer )
00703         return;
00704     ShutdownImpl *sd = new ShutdownImpl( 0, 0, WDestructiveClose );
00705     connect( sd, SIGNAL(shutdown(ShutdownImpl::Type)),
00706          this, SLOT(shutdown(ShutdownImpl::Type)) );
00707     QPEApplication::showWidget( sd );
00708 }
00709 
00710 void ServerApplication::shutdown( ShutdownImpl::Type t )
00711 {
00712     char *opt = 0;
00713 
00714     switch ( t ) {
00715     case ShutdownImpl::ShutdownSystem:
00716         opt = "-h";
00717         // fall through
00718     case ShutdownImpl::RebootSystem:
00719         if ( opt == 0 )
00720             opt = "-r";
00721 
00722         if ( execl( "/sbin/shutdown", "shutdown", opt, "now", ( void* ) 0) < 0 )
00723             perror("shutdown");
00724 //                      ::syslog ( LOG_ERR, "Erroring execing shutdown\n" );
00725 
00726         break;
00727     case ShutdownImpl::RestartDesktop:
00728         restart();
00729         break;
00730     case ShutdownImpl::TerminateDesktop:
00731         prepareForTermination( FALSE );
00732 
00733         // This is a workaround for a Qt bug
00734         // clipboard applet has to stop its poll timer, or Qt/E
00735         // will hang on quit() right before it emits aboutToQuit()
00736         emit aboutToQuit ( );
00737 
00738         quit();
00739         break;
00740     }
00741 }
00742 
00743 void ServerApplication::restart()
00744 {
00745     if ( allowRestart ) {
00746 
00747         /*
00748          * Applets and restart is a problem. Some applets delete
00749          * their widgets even if ownership gets transfered to the
00750          * parent (Systray ) but deleting the applet may be unsafe
00751          * as well ( double deletion ). Some have topLevel widgets
00752          * and when we dlclose and then delete the widget we will
00753          * crash and an crash during restart is not nice
00754          */
00755 #ifdef ALL_APPLETS_ON_THIS_WORLD_ARE_FIXED
00756         /* same as above */
00757         emit aboutToQuit();
00758         prepareForTermination(TRUE);
00759         doRestart = TRUE;
00760         quit();
00761 #else
00762         prepareForTermination( true );
00763         for ( int fd = 3; fd < 100; fd++ )
00764         close( fd );
00765         execl( ( qpeDir() + "/bin/qpe" ).local8Bit(), "qpe", NULL );
00766         exit( 1 );
00767 #endif
00768     }
00769 }
00770 
00771 void ServerApplication::rereadVolumes()
00772 {
00773     Config cfg( "qpe" );
00774     cfg. setGroup ( "Volume" );
00775 
00776     m_screentap_sound = cfg. readBoolEntry ( "TouchSound" );
00777     m_keyclick_sound  = cfg. readBoolEntry ( "KeySound" );
00778     m_alarm_sound     = cfg. readBoolEntry ( "AlarmSound" );
00779 }
00780 
00781 
00782 void ServerApplication::checkMemory()
00783 {
00784 #if defined(QPE_HAVE_MEMALERTER)
00785     static bool ignoreNormal=TRUE;
00786     static bool existingMessage=FALSE;
00787 
00788     if(existingMessage)
00789     return; // don't show a second message while still on first
00790 
00791     existingMessage = TRUE;
00792     switch ( memstate ) {
00793     case MemUnknown:
00794         break;
00795     case MemLow:
00796         memstate = MemUnknown;
00797         if ( !recoverMemory() ) {
00798         QMessageBox::warning( 0 , tr("Memory Status"),
00799             tr("Memory Low\nPlease save data.") );
00800         ignoreNormal = FALSE;
00801         }
00802         break;
00803     case MemNormal:
00804         memstate = MemUnknown;
00805         if ( !ignoreNormal ) {
00806         ignoreNormal = TRUE;
00807         QMessageBox::information ( 0 , tr("Memory Status"),
00808             "Memory OK" );
00809         }
00810         break;
00811     case MemVeryLow:
00812         memstate = MemUnknown;
00813         QMessageBox::critical( 0 , tr("Memory Status"),
00814         tr("Critical Memory Shortage\n"
00815         "Please end this application\n"
00816         "immediately.") );
00817         recoverMemory();
00818     }
00819     existingMessage = FALSE;
00820 #endif
00821 }
00822 
00823 bool ServerApplication::recoverMemory()
00824 {
00825     return FALSE;
00826 }
00827 
00828 void ServerApplication::keyClick(int , bool press, bool )
00829 {
00830     if ( press && m_keyclick_sound )
00831         ODevice::inst() -> playKeySound();
00832 
00833 }
00834 
00835 void ServerApplication::screenClick(bool press)
00836 {
00837     if ( press && m_screentap_sound )
00838         ODevice::inst() -> playTouchSound();
00839 }
00840 
00841 void ServerApplication::soundAlarm() {
00842     if ( me ()->m_alarm_sound )
00843         ODevice::inst()->playAlarmSound();
00844 }
00845 
00846 ServerApplication *ServerApplication::me ( )
00847 {
00848     return static_cast<ServerApplication*>( qApp );
00849 }
00850 
00851 bool ServerApplication::isStarting()
00852 {
00853     return ms_is_starting;
00854 }
00855 
00856 int ServerApplication::exec()
00857 {
00858     ms_is_starting = true;
00859     odebug << "Serverapp - exec" << oendl;
00860     return QPEApplication::exec();
00861 }
00862 
00863 #include "serverapp.moc"

Generated on Sat Nov 5 16:15:32 2005 for OPIE by  doxygen 1.4.2