summaryrefslogtreecommitdiff
path: root/core/launcher/serverapp.cpp
blob: be6b9724595dfd5e5bc030de0c60ac9a5602b9db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/**********************************************************************
** Copyright (C) 2000-2003 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "serverapp.h"
#include "screensaver.h"

/* OPIE */
#include <opie2/odebug.h>
#include <opie2/odevice.h>
#include <opie2/multiauthpassword.h>

#include <qtopia/config.h>
#include <qtopia/power.h>

#ifdef Q_WS_QWS
#include <qtopia/qcopenvelope_qws.h>
#endif
#include <qtopia/global.h>
using namespace Opie::Core;

/* QT */
#ifdef Q_WS_QWS
#include <qgfx_qws.h>
#endif
#include <qmessagebox.h>
#include <qtimer.h>
#include <qpainter.h>
#include <qfile.h>
#include <qpixmapcache.h>

/* STD */
#ifdef Q_OS_WIN32
#include <io.h>
#include <process.h>
#else
#include <unistd.h>
#endif
#include <stdlib.h>

static ServerApplication *serverApp = 0;
static int loggedin=0;

QCopKeyRegister::QCopKeyRegister()
    : m_keyCode( 0 )
{

}

QCopKeyRegister::QCopKeyRegister( int k, const QCString& c, const QCString& m )
    :m_keyCode( k ), m_channel( c ), m_message( m )
{

}

int QCopKeyRegister::keyCode() const
{
    return m_keyCode;
}

QCString QCopKeyRegister::channel() const
{
    return m_channel;
}

QCString QCopKeyRegister::message() const
{
    return m_message;
}

bool QCopKeyRegister::send()
{
    if (m_channel.isNull() )
        return false;

    QCopEnvelope e( m_channel, m_message );

    return true;
}

//---------------------------------------------------------------------------

/*
  Priority is number of alerts that are needed to pop up
  alert.
 */
class DesktopPowerAlerter : public QMessageBox
{
    Q_OBJECT
public:
    DesktopPowerAlerter( QWidget *parent, const char *name = 0 )
    : QMessageBox( tr("Battery Status"), tr("Low Battery"),
               QMessageBox::Critical,
               QMessageBox::Ok | QMessageBox::Default,
               QMessageBox::NoButton, QMessageBox::NoButton,
               parent, name, FALSE )
    {
    currentPriority = INT_MAX;
    alertCount = 0;
    }

    void alert( const QString &text, int priority );
    void hideEvent( QHideEvent * );
private:
    int currentPriority;
    int alertCount;
};

void DesktopPowerAlerter::alert( const QString &text, int priority )
{
    alertCount++;
    if ( alertCount < priority )
    return;
    if ( priority > currentPriority )
    return;
    currentPriority = priority;
    setText( text );
    show();
}


void DesktopPowerAlerter::hideEvent( QHideEvent *e )
{
    QMessageBox::hideEvent( e );
    alertCount = 0;
    currentPriority = INT_MAX;
}

//---------------------------------------------------------------------------

KeyFilter::KeyFilter(QObject* parent) : QObject(parent), held_tid(0), heldButton(0)
{
    /* We don't do this cause it would interfere with ODevice */
#if 0
    qwsServer->setKeyboardFilter(this);
#endif
}

void KeyFilter::timerEvent(QTimerEvent* e)
{
    if ( e->timerId() == held_tid ) {
    killTimer(held_tid);
    // button held
    if ( heldButton ) {
        emit activate(heldButton, TRUE);
        heldButton = 0;
    }
    held_tid = 0;
    }
}

void KeyFilter::registerKey( const QCopKeyRegister& key )
{

    m_keys.insert( key.keyCode(), key );
}

void KeyFilter::unregisterKey( const QCopKeyRegister& key )
{
    m_keys.remove( key.keyCode() );
}

bool KeyFilter::keyRegistered( int key )
{
    /*
     * Check if we've a key registered
     */
    if ( !m_keys[key].send())
        return false;
    else
        return true;
}

bool KeyFilter::checkButtonAction(bool db, int keycode,  int press, int autoRepeat)
{
    if ( !loggedin
        // Permitted keys
        && keycode != Key_F34 // power
        && keycode != Key_F30 // select
        && keycode != Key_Enter
        && keycode != Key_Return
        && keycode != Key_Space
        && keycode != Key_Left
        && keycode != Key_Right
        && keycode != Key_Up
        && keycode != Key_Down )
    return TRUE;

        /* check if it was registered */
    if (!db ) {
        if (keycode != 0 &&press && !autoRepeat && keyRegistered(keycode) )
            return true;
    } else {

        // First check to see if DeviceButtonManager knows something about this button:
        const ODeviceButton* button = ODevice::inst()->buttonForKeycode(keycode);
        if (button && !autoRepeat) {
            if ( held_tid ) {
                killTimer(held_tid);
                held_tid = 0;
            }
            if ( button->heldAction().isNull() ) {
                if ( press )
                    emit activate(button, FALSE);
            } else if ( press ) {
                heldButton = button;
                held_tid = startTimer( ODevice::inst ()->buttonHoldTime () );
            } else if ( heldButton ) {
                heldButton = 0;
                emit activate(button, FALSE);
            }
            QWSServer::screenSaverActivate(FALSE);
            return TRUE;
        }
        return false;
    }
    if ( keycode == HardKey_Suspend ) {
        if ( press ) emit power();
        return TRUE;
    }
    if ( keycode == HardKey_Backlight ) {
        if ( press ) emit backlight();
        return TRUE;
    }
    if ( keycode == Key_F32 ) {
#ifndef QT_NO_COP
        if ( press ) QCopEnvelope e( "QPE/Desktop", "startSync()" );
#endif
        return TRUE;
    }
    if ( keycode == Key_F31 ) {
        if ( press ) emit symbol();
        QWSServer::screenSaverActivate(FALSE);
        return TRUE;
    }

    if ( keycode == Key_NumLock )
        if ( press ) emit numLockStateToggle();

    if ( keycode == Key_CapsLock )
        if ( press ) emit capsLockStateToggle();

    if ( serverApp )
        serverApp->keyClick(keycode,press,autoRepeat);

    return FALSE;
}

enum MemState { MemUnknown, MemVeryLow, MemLow, MemNormal } memstate=MemUnknown;

#if defined(QPE_HAVE_MEMALERTER)
QPE_MEMALERTER_IMPL
#endif



//---------------------------------------------------------------------------

bool ServerApplication::doRestart = FALSE;
bool ServerApplication::allowRestart = TRUE;
bool ServerApplication::ms_is_starting  = TRUE;

void ServerApplication::switchLCD( bool on )
{
    if ( !qApp )
        return;

    ServerApplication *dapp = ServerApplication::me() ;

    if ( !dapp-> m_screensaver )
        return;

    if ( on ) {
        dapp-> m_screensaver-> setDisplayState ( true );
        dapp-> m_screensaver-> setBacklight ( -3 );
    } else
        dapp-> m_screensaver-> setDisplayState ( false );


}

ServerApplication::ServerApplication( int& argc, char **argv, Type t )
    : QPEApplication( argc, argv, t )
{
    ms_is_starting = true;

    // We know we'll have lots of cached pixmaps due to App/DocLnks
    QPixmapCache::setCacheLimit(512);

    m_ps = new PowerStatus;
    m_ps_last = new PowerStatus;
    pa = new DesktopPowerAlerter( 0 );

    m_apm_timer = new QTimer( this );
    connect(m_apm_timer, SIGNAL( timeout() ),
            this, SLOT( apmTimeout() ) );

    reloadPowerWarnSettings();

    QCopChannel *channel = new QCopChannel( "QPE/System", this );
    connect(channel, SIGNAL(received(const QCString&,const QByteArray&) ),
            this, SLOT(systemMessage(const QCString&,const QByteArray&) ) );

    channel = new QCopChannel("QPE/Launcher", this );
    connect(channel, SIGNAL(received(const QCString&,const QByteArray&) ),
            this, SLOT(launcherMessage(const QCString&,const QByteArray&) ) );

    m_screensaver = new OpieScreenSaver();
    m_screensaver->setInterval( -1 );
    QWSServer::setScreenSaver( m_screensaver );

    connect( qApp, SIGNAL( volumeChanged(bool) ),
             this, SLOT( rereadVolumes() ) );


    /* ### PluginLoader libqtopia SafeMode */
#if 0
    if ( PluginLoader::inSafeMode() )
    QTimer::singleShot(500, this, SLOT(showSafeMode()) );
    QTimer::singleShot(20*1000, this, SLOT(clearSafeMode()) );
#endif

    kf = new KeyFilter(this);

    connect( kf, SIGNAL(launch()), this, SIGNAL(launch()) );
    connect( kf, SIGNAL(power()), this, SIGNAL(power()) );
    connect( kf, SIGNAL(backlight()), this, SIGNAL(backlight()) );
    connect( kf, SIGNAL(symbol()), this, SIGNAL(symbol()));
    connect( kf, SIGNAL(numLockStateToggle()), this,SIGNAL(numLockStateToggle()));
    connect( kf, SIGNAL(capsLockStateToggle()), this,SIGNAL(capsLockStateToggle()));
    connect( kf, SIGNAL(activate(const Opie::Core::ODeviceButton*,bool)),
             this,SIGNAL(activate(const Opie::Core::ODeviceButton*,bool)));


    connect( kf, SIGNAL(backlight()), this, SLOT(toggleLight()) );

    connect( this, SIGNAL(power() ),
             SLOT(togglePower() ) );

    rereadVolumes();

    serverApp = this;

    apmTimeout();
    grabKeyboard();

    /* make sure the event filter is installed */  /* std::limits<short>::max() when you've stdc++ */
    const ODeviceButton* but = ODevice::inst()->buttonForKeycode( SHRT_MAX );
    Q_CONST_UNUSED( but )
}


ServerApplication::~ServerApplication()
{
    ungrabKeyboard();

    delete pa;
    delete m_ps;
    delete m_ps_last;
}

void ServerApplication::apmTimeout()
{
    serverApp-> checkMemory( ); // in case no events are generated
    *m_ps_last = *m_ps;
    *m_ps = PowerStatusManager::readStatus();

    if ( m_ps->acStatus() != m_ps_last-> acStatus() )
        m_screensaver-> powerStatusChanged( *m_ps );

    if ( m_ps->acStatus() == PowerStatus::Online ) {
        return;
    }

    int bat = m_ps-> batteryPercentRemaining();

    if ( bat < m_ps_last-> batteryPercentRemaining() ) {
        if ( bat <= m_powerCritical ) {
        QMessageBox battlow(
        tr("WARNING"),
        tr("<p>The battery level is critical!"
            "<p>Keep power off until AC is restored"),
        QMessageBox::Warning,
        QMessageBox::Cancel, QMessageBox::NoButton, QMessageBox::NoButton,
        0, QString::null, TRUE, WStyle_StaysOnTop);
        battlow.setButtonText(QMessageBox::Cancel, tr("Ok"));
        battlow.exec();
        } else if ( bat <= m_powerVeryLow )
            pa->alert( tr( "The battery is running very low. "), 2 );
    }

    if ( m_ps-> backupBatteryStatus() == PowerStatus::VeryLow ) {
        QMessageBox battlow(
        tr("WARNING"),
        tr("<p>The Back-up battery is very low"
            "<p>Please charge the back-up battery"),
        QMessageBox::Warning,
        QMessageBox::Cancel, QMessageBox::NoButton, QMessageBox::NoButton,
        0, QString::null, TRUE, WStyle_StaysOnTop);
        battlow.setButtonText(QMessageBox::Cancel, tr("Ok"));
        battlow.exec();
    }
}

void ServerApplication::systemMessage( const QCString& msg,
                                       const QByteArray& data )
{
    QDataStream stream ( data, IO_ReadOnly );

    if ( msg == "setScreenSaverInterval(int)" ) {
        int time;
        stream >> time;
        m_screensaver-> setInterval( time );
    }
    else if ( msg == "setScreenSaverIntervals(int,int,int)" ) {
        int t1, t2, t3;
        stream >> t1 >> t2 >> t3;
        m_screensaver-> setIntervals( t1, t2, t3 );
    }
    else if ( msg == "setBacklight(int)" ) {
        int bright;
        stream >> bright;
        m_screensaver-> setBacklight( bright );
    }
    else if ( msg == "setScreenSaverMode(int)" ) {
        int mode;
        stream >> mode;
        m_screensaver-> setMode ( mode );
    }
    else if ( msg == "reloadPowerWarnSettings()" ) {
        reloadPowerWarnSettings();
    }
    else if ( msg == "setDisplayState(int)" ) {
        int state;
        stream >> state;
        m_screensaver-> setDisplayState ( state != 0 );
    }
    else if ( msg == "suspend()" ) {
        emit power();
    }
    else if ( msg == "sendBusinessCard()" ) {
        QString card = ::getenv ( "HOME" );
        card += "/Applications/addressbook/businesscard.vcf";

        if ( QFile::exists( card ) ) {
            QCopEnvelope e ( "QPE/Obex", "send(QString,QString,QString)" );
            QString mimetype = "text/x-vCard";
            e << tr( "business card" ) << card << mimetype;
        }
    }
}

void ServerApplication::reloadPowerWarnSettings ( )
{
    Config cfg ( "apm" );
    cfg. setGroup ( "Warnings" );

    int iv = cfg. readNumEntry ( "checkinterval", 10000 );

    m_apm_timer-> stop ( );
    if ( iv )
        m_apm_timer-> start ( iv );

    m_powerVeryLow  = cfg. readNumEntry ( "powerverylow", 10 );
    m_powerCritical = cfg. readNumEntry ( "powervcritical", 5 );
}

void ServerApplication::launcherMessage( const QCString & msg, const QByteArray & data )
{
    QDataStream stream ( data, IO_ReadOnly );

    if ( msg == "deviceButton(int,int,int)" ) {
        int keycode, press, autoRepeat;
        stream >> keycode >> press >> autoRepeat;

        kf->checkButtonAction ( true, keycode, press, autoRepeat );
    }
    else if ( msg == "keyRegister(int,QCString,QCString)" ) {
        int k;
        QCString c, m;
        stream >> k >> c >> m;

        kf -> registerKey( QCopKeyRegister(k, c, m) );
    }
}


bool ServerApplication::screenLocked()
{
    return loggedin == 0;
}

void ServerApplication::login(bool at_poweron)
{
    if ( !loggedin ) {
        Global::terminateBuiltin("calibrate"); // No tr
        int lockMode = at_poweron ? Opie::Security::IfPowerOn : Opie::Security::IfResume;
        Opie::Security::MultiauthPassword::authenticate(lockMode);
        loggedin=1;
#ifndef QT_NO_COP
        QCopEnvelope e( "QPE/Desktop", "unlocked()" );
#endif
    }
}

#if defined(QPE_HAVE_TOGGLELIGHT)
#include <qtopia/config.h>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <linux/ioctl.h>
#include <time.h>
#endif

namespace {
    void execAutoStart(const QDateTime& suspendTime ) {
        QString appName;
        int delay;
        QDateTime now = QDateTime::currentDateTime();

        Config cfg( "autostart" );
        cfg.setGroup( "AutoStart" );
        appName = cfg.readEntry( "Apps", "" );
        delay = cfg.readNumEntry( "Delay", 0  );

        // If the time between suspend and resume was longer then the
        // value saved as delay, start the app
        if ( suspendTime.secsTo( now ) >= ( delay * 60 ) && !appName.isEmpty() ) {
            QCopEnvelope e( "QPE/System", "execute(QString)" );
            e << QString( appName );
        }
    }
}


void ServerApplication::togglePower()
{
    static bool excllock = false;

    if ( excllock )
        return ;

    excllock = true;

    bool wasloggedin = loggedin;
    loggedin = 0;
    m_suspendTime = QDateTime::currentDateTime();

#ifdef QWS
    if ( Opie::Security::MultiauthPassword::needToAuthenticate ( true ) && qt_screen ) {
        // Should use a big black window instead.
        // But this would not show up fast enough
        QGfx *g = qt_screen-> screenGfx ( );
        g-> fillRect ( 0, 0, qt_screen-> width ( ), qt_screen-> height ( ));
        delete g;
    }
#endif

    ODevice::inst ( )-> suspend ( );

    ServerApplication::switchLCD ( true ); // force LCD on without slow qcop call
    QWSServer::screenSaverActivate ( false );

    {
        QCopEnvelope( "QPE/Card", "mtabChanged()" ); // might have changed while asleep
    }

    if ( wasloggedin )
        login ( true );

    execAutoStart(m_suspendTime);
    //qcopBridge->closeOpenConnections();

    excllock = false;
}

void ServerApplication::toggleLight()
{
#ifndef QT_NO_COP
    QCopEnvelope e("QPE/System", "setBacklight(int)");
    e << -2; // toggle
#endif
}


/*
 * We still listen to key events but handle them in
 * a special class
 */

bool ServerApplication::eventFilter( QObject *o, QEvent *e) {
    if ( e->type() != QEvent::KeyPress &&
         e->type() != QEvent::KeyRelease )
        return QPEApplication::eventFilter( o, e );

    QKeyEvent *ke = static_cast<QKeyEvent*>( e );
    if ( kf->checkButtonAction( true, ke->key(),
                                e->type() == QEvent::KeyPress,
                                ke-> isAutoRepeat() ))
        return true;

    return QPEApplication::eventFilter( o, e );

}

#ifdef Q_WS_QWS
bool ServerApplication::qwsEventFilter( QWSEvent *e )
{
    checkMemory();

    if ( e->type == QWSEvent::Mouse ) {
        QWSMouseEvent *me = (QWSMouseEvent *)e;
        static bool up = TRUE;
        if ( me->simpleData.state&LeftButton ) {
            if ( up ) {
              up = FALSE;
              screenClick(TRUE);
            }
        } else if ( !up ) {
            up = TRUE;
            screenClick(FALSE);
        }
    } else
    if ( e->type == QWSEvent::Key ) {
        QWSKeyEvent * ke = static_cast<QWSKeyEvent*>( e );
        if ( kf->checkButtonAction( false,
                                    ke-> simpleData.keycode,
                                    ke-> simpleData.is_press,
                                    ke-> simpleData.is_auto_repeat ) )
            return true;
    }

    return QPEApplication::qwsEventFilter( e );
}
#endif


/* ### FIXME libqtopia Plugin Safe Mode */

void ServerApplication::showSafeMode()
{
#if 0
    if ( QMessageBox::warning(0, tr("Safe Mode"), tr("<P>A system startup error occurred, "
        "and the system is now in Safe Mode. "
        "Plugins are not loaded in Safe Mode. "
        "You can use the Plugin Manager to "
        "disable plugins that cause system error."), tr("OK"), tr("Plugin Manager..."), 0) == 1 )
    {
        Global::execute( "pluginmanager" );
    }
#endif
}

void ServerApplication::clearSafeMode()
{
#if 0
    // If we've been running OK for a while then we won't bother going into
    // safe mode immediately on the next crash.
    Config cfg( "PluginLoader" );
    cfg.setGroup( "Global" );
    QString mode = cfg.readEntry( "Mode", "Normal" );
    if ( mode == "MaybeSafe" ) {
        cfg.writeEntry( "Mode", "Normal" );
    }
#endif
}


void ServerApplication::shutdown()
{
    if ( type() != GuiServer )
        return;
    ShutdownImpl *sd = new ShutdownImpl( 0, 0, WDestructiveClose );
    connect( sd, SIGNAL(shutdown(ShutdownImpl::Type)),
         this, SLOT(shutdown(ShutdownImpl::Type)) );
    QPEApplication::showWidget( sd );
}

void ServerApplication::shutdown( ShutdownImpl::Type t )
{
    char *opt = 0;

    switch ( t ) {
    case ShutdownImpl::ShutdownSystem:
        opt = "-h";
        // fall through
    case ShutdownImpl::RebootSystem:
        if ( opt == 0 )
            opt = "-r";

        if ( execl( "/sbin/shutdown", "shutdown", opt, "now", ( void* ) 0) < 0 )
            perror("shutdown");
//                      ::syslog ( LOG_ERR, "Erroring execing shutdown\n" );

        break;
    case ShutdownImpl::RestartDesktop:
        restart();
        break;
    case ShutdownImpl::TerminateDesktop:
        prepareForTermination( FALSE );

        // This is a workaround for a Qt bug
        // clipboard applet has to stop its poll timer, or Qt/E
        // will hang on quit() right before it emits aboutToQuit()
        emit aboutToQuit ( );

        quit();
        break;
    }
}

void ServerApplication::restart()
{
    if ( allowRestart ) {

        /*
         * Applets and restart is a problem. Some applets delete
         * their widgets even if ownership gets transfered to the
         * parent (Systray ) but deleting the applet may be unsafe
         * as well ( double deletion ). Some have topLevel widgets
         * and when we dlclose and then delete the widget we will
         * crash and an crash during restart is not nice
         */
#ifdef ALL_APPLETS_ON_THIS_WORLD_ARE_FIXED
        /* same as above */
        emit aboutToQuit();
        prepareForTermination(TRUE);
        doRestart = TRUE;
        quit();
#else
        prepareForTermination( true );
        for ( int fd = 3; fd < 100; fd++ )
        close( fd );
        execl( ( qpeDir() + "/bin/qpe" ).local8Bit(), "qpe", NULL );
        exit( 1 );
#endif
    }
}

void ServerApplication::rereadVolumes()
{
    Config cfg( "qpe" );
    cfg. setGroup ( "Volume" );

    m_screentap_sound = cfg. readBoolEntry ( "TouchSound" );
    m_keyclick_sound  = cfg. readBoolEntry ( "KeySound" );
    m_alarm_sound     = cfg. readBoolEntry ( "AlarmSound" );
}


void ServerApplication::checkMemory()
{
#if defined(QPE_HAVE_MEMALERTER)
    static bool ignoreNormal=TRUE;
    static bool existingMessage=FALSE;

    if(existingMessage)
    return; // don't show a second message while still on first

    existingMessage = TRUE;
    switch ( memstate ) {
    case MemUnknown:
        break;
    case MemLow:
        memstate = MemUnknown;
        if ( !recoverMemory() ) {
        QMessageBox::warning( 0 , tr("Memory Status"),
            tr("Memory Low\nPlease save data.") );
        ignoreNormal = FALSE;
        }
        break;
    case MemNormal:
        memstate = MemUnknown;
        if ( !ignoreNormal ) {
        ignoreNormal = TRUE;
        QMessageBox::information ( 0 , tr("Memory Status"),
            "Memory OK" );
        }
        break;
    case MemVeryLow:
        memstate = MemUnknown;
        QMessageBox::critical( 0 , tr("Memory Status"),
        tr("Critical Memory Shortage\n"
        "Please end this application\n"
        "immediately.") );
        recoverMemory();
    }
    existingMessage = FALSE;
#endif
}

bool ServerApplication::recoverMemory()
{
    return FALSE;
}

void ServerApplication::keyClick(int , bool press, bool )
{
    if ( press && m_keyclick_sound )
        ODevice::inst() -> playKeySound();

}

void ServerApplication::screenClick(bool press)
{
    if ( press && m_screentap_sound )
        ODevice::inst() -> playTouchSound();
}

void ServerApplication::soundAlarm() {
    if ( me ()->m_alarm_sound )
        ODevice::inst()->playAlarmSound();
}

ServerApplication *ServerApplication::me ( )
{
    return static_cast<ServerApplication*>( qApp );
}

bool ServerApplication::isStarting()
{
    return ms_is_starting;
}

int ServerApplication::exec()
{
    ms_is_starting = true;
    odebug << "Serverapp - exec" << oendl;
    return QPEApplication::exec();
}

#include "serverapp.moc"