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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
|
/*****************************************************************
ksmserver - the KDE session management server
Copyright (C) 2000 Matthias Ettrich <[email protected]>
Copyright (C) 2005 Lubos Lunak <[email protected]>
relatively small extensions by Oswald Buddenhagen <[email protected]>
some code taken from the dcopserver (part of the KDE libraries), which is
Copyright (c) 1999 Matthias Ettrich <[email protected]>
Copyright (c) 1999 Preston Brown <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pwd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqdatastream.h>
#include <tqptrstack.h>
#include <tqpushbutton.h>
#include <tqmessagebox.h>
#include <tqguardedptr.h>
#include <tqtimer.h>
#include <tqregexp.h>
#include <tdelocale.h>
#include <tdeglobal.h>
#include <tdeconfig.h>
#include <kstandarddirs.h>
#include <unistd.h>
#include <tdeapplication.h>
#include <kstaticdeleter.h>
#include <tdetempfile.h>
#include <kprocess.h>
#include <dcopclient.h>
#include <dcopref.h>
#include <dmctl.h>
#include <kdebug.h>
#include <knotifyclient.h>
#include <libtdersync/tdersync.h>
#include "server.h"
#include "global.h"
#include "shutdowndlg.h"
#include "client.h"
#ifdef BUILD_PROFILE_SHUTDOWN
#define PROFILE_SHUTDOWN 1
#endif
#ifdef PROFILE_SHUTDOWN
#define SHUTDOWN_MARKER(x) printf("[ksmserver] '%s' [%s]\n", x, TQTime::currentTime().toString("hh:mm:ss:zzz").ascii()); fflush(stdout);
#else // PROFILE_SHUTDOWN
#define SHUTDOWN_MARKER(x)
#endif // PROFILE_SHUTDOWN
// Time to wait after close request for graceful application termination
// If set too high running applications may be ungracefully terminated on slow machines
#define KSMSERVER_SHUTDOWN_CLIENT_UNRESPONSIVE_TIMEOUT 60000
// Time to wait before showing manual termination options
// If set too low the user may be confused by buttons briefly flashing up on the screen during an otherwise normal logout process
#define KSMSERVER_NOTIFICATION_MANUAL_OPTIONS_TIMEOUT 1000
void KSMServer::logout( int confirm, int sdtype, int sdmode )
{
shutdown( (TDEApplication::ShutdownConfirm)confirm,
(TDEApplication::ShutdownType)sdtype,
(TDEApplication::ShutdownMode)sdmode );
}
bool KSMServer::checkStatus( bool &logoutConfirmed, bool &maysd, bool &mayrb,
TDEApplication::ShutdownConfirm confirm,
TDEApplication::ShutdownType sdtype,
TDEApplication::ShutdownMode sdmode )
{
pendingShutdown.stop();
if( dialogActive ) {
return false;
}
if( state >= Shutdown ) { // already performing shutdown
return false;
}
if( state != Idle ) { // performing startup
// perform shutdown as soon as startup is finished, in order to avoid saving partial session
if( !pendingShutdown.isActive()) {
pendingShutdown.start( 1000 );
pendingShutdown_confirm = confirm;
pendingShutdown_sdtype = sdtype;
pendingShutdown_sdmode = sdmode;
}
return false;
}
TDEConfig *config = TDEGlobal::config();
config->reparseConfiguration(); // config may have changed in the KControl module
config->setGroup("General" );
logoutConfirmed =
(confirm == TDEApplication::ShutdownConfirmYes) ? false :
(confirm == TDEApplication::ShutdownConfirmNo) ? true :
!config->readBoolEntry( "confirmLogout", true );
maysd = false;
mayrb = false;
if (config->readBoolEntry( "offerShutdown", true )) {
if (DM().canShutdown()) {
maysd = true;
mayrb = true;
}
else {
TDERootSystemDevice* rootDevice = hwDevices->rootSystemDevice();
if (rootDevice) {
if (rootDevice->canPowerOff()) {
maysd = true;
}
if (rootDevice->canReboot()) {
mayrb = true;
}
}
}
}
if (!maysd) {
if (sdtype != TDEApplication::ShutdownTypeNone &&
sdtype != TDEApplication::ShutdownTypeDefault &&
sdtype != TDEApplication::ShutdownTypeReboot &&
logoutConfirmed)
return false; /* unsupported fast shutdown */
}
if (!mayrb) {
if (sdtype != TDEApplication::ShutdownTypeNone &&
sdtype != TDEApplication::ShutdownTypeDefault &&
sdtype != TDEApplication::ShutdownTypeHalt &&
logoutConfirmed)
return false; /* unsupported fast shutdown */
}
return true;
}
void KSMServer::shutdownInternal( TDEApplication::ShutdownConfirm confirm,
TDEApplication::ShutdownType sdtype,
TDEApplication::ShutdownMode sdmode,
TQString bopt )
{
bool maysd = false;
bool mayrb = false;
bool logoutConfirmed = false;
if ( !checkStatus( logoutConfirmed, maysd, mayrb, confirm, sdtype, sdmode ) ) {
return;
}
TDEConfig *config = TDEGlobal::config();
config->setGroup("General" );
if ((!maysd) && (sdtype != TDEApplication::ShutdownTypeReboot)) {
sdtype = TDEApplication::ShutdownTypeNone;
}
if ((!mayrb) && (sdtype != TDEApplication::ShutdownTypeHalt)) {
sdtype = TDEApplication::ShutdownTypeNone;
}
if (sdtype == TDEApplication::ShutdownTypeDefault) {
sdtype = (TDEApplication::ShutdownType) config->readNumEntry( "shutdownType", (int)TDEApplication::ShutdownTypeNone );
}
if (sdmode == TDEApplication::ShutdownModeDefault) {
sdmode = TDEApplication::ShutdownModeInteractive;
}
// shall we show a logout status dialog box?
bool showLogoutStatusDlg = TDEConfigGroup(TDEGlobal::config(), "Logout").readBoolEntry("showLogoutStatusDlg", true);
if (showLogoutStatusDlg) {
KSMShutdownIPFeedback::start();
}
dialogActive = true;
if ( !logoutConfirmed ) {
int selection;
KSMShutdownFeedback::start(); // make the screen gray
logoutConfirmed =
KSMShutdownDlg::confirmShutdown( maysd, mayrb, sdtype, bopt, &selection );
// ###### We can't make the screen remain gray while talking to the apps,
// because this prevents interaction ("do you want to save", etc.)
// TODO: turn the feedback widget into a list of apps to be closed,
// with an indicator of the current status for each.
KSMShutdownFeedback::stop(); // make the screen become normal again
if (selection != 0) {
// respect lock on resume & disable suspend/hibernate settings
// from power-manager
TDEConfig config("power-managerrc");
bool lockOnResume = config.readBoolEntry("lockOnResume", true);
if (lockOnResume) {
TQCString replyType;
TQByteArray replyData;
// Block here until lock is complete
// If this is not done the desktop of the locked session will be shown after suspend/hibernate until the lock fully engages!
DCOPRef("kdesktop", "KScreensaverIface").call("lock()");
}
TDERootSystemDevice* rootDevice = hwDevices->rootSystemDevice();
if (rootDevice) {
if (selection == 1) { // Suspend
rootDevice->setPowerState(TDESystemPowerState::Suspend);
}
if (selection == 2) { // Hibernate
rootDevice->setPowerState(TDESystemPowerState::Hibernate);
}
}
}
}
if ( logoutConfirmed ) {
SHUTDOWN_MARKER("Shutdown initiated");
shutdownType = sdtype;
shutdownMode = sdmode;
bootOption = bopt;
shutdownNotifierIPDlg = 0;
if (showLogoutStatusDlg) {
shutdownNotifierIPDlg = KSMShutdownIPDlg::showShutdownIP();
if (shutdownNotifierIPDlg) {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->setStatusMessage(i18n("Notifying applications of logout request..."));
notificationTimer.start( KSMSERVER_NOTIFICATION_MANUAL_OPTIONS_TIMEOUT, true );
}
}
// shall we save the session on logout?
saveSession = ( config->readEntry( "loginMode", "restorePreviousLogout" ) == "restorePreviousLogout" );
if ( saveSession ) {
sessionGroup = TQString("Session: ") + SESSION_PREVIOUS_LOGOUT;
}
// Set the real desktop background to black so that exit looks
// clean regardless of what was on "our" desktop.
if (!showLogoutStatusDlg) {
TQT_TQWIDGET(kapp->desktop())->setBackgroundColor( Qt::black );
}
state = Shutdown;
wmPhase1WaitingCount = 0;
saveType = saveSession?SmSaveBoth:SmSaveGlobal;
performLegacySessionSave();
SHUTDOWN_MARKER("Legacy save complete");
startProtection();
for ( KSMClient* c = clients.first(); c; c = clients.next() ) {
c->resetState();
// Whoever came with the idea of phase 2 got it backwards
// unfortunately. Window manager should be the very first
// one saving session data, not the last one, as possible
// user interaction during session save may alter
// window positions etc.
// Moreover, KWin's focus stealing prevention would lead
// to undesired effects while session saving (dialogs
// wouldn't be activated), so it needs be assured that
// KWin will turn it off temporarily before any other
// user interaction takes place.
// Therefore, make sure the WM finishes its phase 1
// before others a chance to change anything.
// KWin will check if the session manager is ksmserver,
// and if yes it will save in phase 1 instead of phase 2.
if( isWM( c )) {
++wmPhase1WaitingCount;
SmsSaveYourself( c->connection(), saveType,
true, SmInteractStyleAny, false );
}
}
if( wmPhase1WaitingCount == 0 ) { // no WM, simply start them all
for ( KSMClient* c = clients.first(); c; c = clients.next() )
SmsSaveYourself( c->connection(), saveType,
true, SmInteractStyleAny, false );
}
if ( clients.isEmpty() ) {
completeShutdownOrCheckpoint();
}
}
else {
if (showLogoutStatusDlg) {
KSMShutdownIPFeedback::stop();
}
}
dialogActive = false;
}
void KSMServer::shutdown( TDEApplication::ShutdownConfirm confirm,
TDEApplication::ShutdownType sdtype, TDEApplication::ShutdownMode sdmode )
{
shutdownInternal( confirm, sdtype, sdmode );
}
#include <tdemessagebox.h>
void KSMServer::logoutTimed( int sdtype, int sdmode, TQString bootOption )
{
int confirmDelay;
TDEConfig* config = TDEGlobal::config();
config->setGroup( "General" );
if ( sdtype == TDEApplication::ShutdownTypeHalt ) {
confirmDelay = config->readNumEntry( "confirmShutdownDelay", 31 );
}
else if ( sdtype == TDEApplication::ShutdownTypeReboot ) {
confirmDelay = config->readNumEntry( "confirmRebootDelay", 31 );
}
else {
confirmDelay = config->readNumEntry( "confirmLogoutDelay", 31 );
}
bool result = true;
if (confirmDelay) {
KSMShutdownFeedback::start(); // make the screen gray
result = KSMDelayedMessageBox::showTicker( (TDEApplication::ShutdownType)sdtype, bootOption, confirmDelay );
KSMShutdownFeedback::stop(); // make the screen become normal again
}
if ( result )
shutdownInternal( TDEApplication::ShutdownConfirmNo,
(TDEApplication::ShutdownType)sdtype,
(TDEApplication::ShutdownMode)sdmode,
bootOption );
}
void KSMServer::pendingShutdownTimeout()
{
shutdown( pendingShutdown_confirm, pendingShutdown_sdtype, pendingShutdown_sdmode );
}
void KSMServer::saveCurrentSession()
{
if ( state != Idle || dialogActive )
return;
if ( currentSession().isEmpty() || currentSession() == SESSION_PREVIOUS_LOGOUT )
sessionGroup = TQString("Session: ") + SESSION_BY_USER;
state = Checkpoint;
wmPhase1WaitingCount = 0;
saveType = SmSaveLocal;
saveSession = true;
performLegacySessionSave();
for ( KSMClient* c = clients.first(); c; c = clients.next() ) {
c->resetState();
if( isWM( c )) {
++wmPhase1WaitingCount;
SmsSaveYourself( c->connection(), saveType, false, SmInteractStyleNone, false );
}
}
if( wmPhase1WaitingCount == 0 ) {
for ( KSMClient* c = clients.first(); c; c = clients.next() )
SmsSaveYourself( c->connection(), saveType, false, SmInteractStyleNone, false );
}
if ( clients.isEmpty() )
completeShutdownOrCheckpoint();
}
void KSMServer::saveCurrentSessionAs( TQString session )
{
if ( state != Idle || dialogActive )
return;
sessionGroup = "Session: " + session;
saveCurrentSession();
}
// callbacks
void KSMServer::saveYourselfDone( KSMClient* client, bool success )
{
if ( state == Idle ) {
// State saving when it's not shutdown or checkpoint. Probably
// a shutdown was cancelled and the client is finished saving
// only now. Discard the saved state in order to avoid
// the saved data building up.
TQStringList discard = client->discardCommand();
if( !discard.isEmpty())
executeCommand( discard );
return;
}
if ( success ) {
client->saveYourselfDone = true;
completeShutdownOrCheckpoint();
} else {
// fake success to make KDE's logout not block with broken
// apps. A perfect ksmserver would display a warning box at
// the very end.
client->saveYourselfDone = true;
completeShutdownOrCheckpoint();
}
startProtection();
if( isWM( client ) && !client->wasPhase2 && wmPhase1WaitingCount > 0 ) {
--wmPhase1WaitingCount;
// WM finished its phase1, save the rest
if( wmPhase1WaitingCount == 0 ) {
for ( KSMClient* c = clients.first(); c; c = clients.next() )
if( !isWM( c ))
SmsSaveYourself( c->connection(), saveType, saveType != SmSaveLocal,
saveType != SmSaveLocal ? SmInteractStyleAny : SmInteractStyleNone,
false );
}
}
}
void KSMServer::interactRequest( KSMClient* client, int /*dialogType*/ )
{
if ( state == Shutdown )
client->pendingInteraction = true;
else
SmsInteract( client->connection() );
handlePendingInteractions();
}
void KSMServer::interactDone( KSMClient* client, bool cancelShutdown_ )
{
if ( client != clientInteracting )
return; // should not happen
clientInteracting = 0;
if ( cancelShutdown_ )
cancelShutdown( client );
else
handlePendingInteractions();
}
void KSMServer::phase2Request( KSMClient* client )
{
client->waitForPhase2 = true;
client->wasPhase2 = true;
completeShutdownOrCheckpoint();
if( isWM( client ) && wmPhase1WaitingCount > 0 ) {
--wmPhase1WaitingCount;
// WM finished its phase1 and requests phase2, save the rest
if( wmPhase1WaitingCount == 0 ) {
for ( KSMClient* c = clients.first(); c; c = clients.next() )
if( !isWM( c ))
SmsSaveYourself( c->connection(), saveType, saveType != SmSaveLocal,
saveType != SmSaveLocal ? SmInteractStyleAny : SmInteractStyleNone,
false );
}
}
}
void KSMServer::handlePendingInteractions()
{
if ( clientInteracting )
return;
for ( KSMClient* c = clients.first(); c; c = clients.next() ) {
if ( c->pendingInteraction ) {
clientInteracting = c;
c->pendingInteraction = false;
break;
}
}
if ( clientInteracting ) {
endProtection();
SmsInteract( clientInteracting->connection() );
} else {
startProtection();
}
}
void KSMServer::cancelShutdown( TQString cancellationText )
{
if (shutdownNotifierIPDlg) {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->closeSMDialog();
shutdownNotifierIPDlg=0;
}
KNotifyClient::event( 0, "cancellogout", cancellationText);
clientInteracting = 0;
for ( KSMClient* c = clients.first(); c; c = clients.next() ) {
SmsShutdownCancelled( c->connection() );
if( c->saveYourselfDone ) {
// Discard also saved state.
TQStringList discard = c->discardCommand();
if( !discard.isEmpty())
executeCommand( discard );
}
}
state = Idle;
}
void KSMServer::cancelShutdown( KSMClient* c )
{
kdDebug( 1218 ) << "Client " << c->program() << " (" << c->clientId() << ") canceled shutdown." << endl;
cancelShutdown(i18n( "Logout canceled by '%1'" ).arg( c->program()));
}
void KSMServer::cancelShutdown()
{
kdDebug( 1218 ) << "User canceled shutdown." << endl;
cancelShutdown(i18n( "Logout canceled by user" ));
}
void KSMServer::startProtection()
{
protectionTimer.start( KSMSERVER_SHUTDOWN_CLIENT_UNRESPONSIVE_TIMEOUT, true );
}
void KSMServer::endProtection()
{
protectionTimer.stop();
}
/*
Internal protection slot, invoked when clients do not react during
shutdown.
*/
void KSMServer::protectionTimeout()
{
if ( ( state != Shutdown && state != Checkpoint ) || clientInteracting )
return;
handleProtectionTimeout();
startProtection();
}
void KSMServer::forceSkipSaveYourself()
{
SHUTDOWN_MARKER("forceSkipSaveYourself");
handleProtectionTimeout();
startProtection();
}
void KSMServer::handleProtectionTimeout()
{
SHUTDOWN_MARKER("handleProtectionTimeout");
notificationTimer.stop();
if (shutdownNotifierIPDlg) {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->hideNotificationActionButtons();
disconnect(shutdownNotifierIPDlg, SIGNAL(abortLogoutClicked()), this, SLOT(cancelShutdown()));
disconnect(shutdownNotifierIPDlg, SIGNAL(skipNotificationClicked()), this, SLOT(forceSkipSaveYourself()));
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->setStatusMessage(i18n("Forcing interacting application termination").append("..."));
}
for ( KSMClient* c = clients.first(); c; c = clients.next() ) {
if ( !c->saveYourselfDone && !c->waitForPhase2 ) {
kdDebug( 1218 ) << "protectionTimeout: client " << c->program() << "(" << c->clientId() << ")" << endl;
c->saveYourselfDone = true;
}
}
completeShutdownOrCheckpoint();
}
void KSMServer::notificationTimeout()
{
if (shutdownNotifierIPDlg) {
// Display the buttons in the logout dialog
connect(shutdownNotifierIPDlg, SIGNAL(abortLogoutClicked()), this, SLOT(cancelShutdown()));
connect(shutdownNotifierIPDlg, SIGNAL(skipNotificationClicked()), this, SLOT(forceSkipSaveYourself()));
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->showNotificationActionButtons();
}
}
void KSMServer::completeShutdownOrCheckpoint()
{
SHUTDOWN_MARKER("completeShutdownOrCheckpoint");
if ( state != Shutdown && state != Checkpoint ) {
SHUTDOWN_MARKER("completeShutdownOrCheckpoint state not Shutdown or Checkpoint");
return;
}
for ( KSMClient* c = clients.first(); c; c = clients.next() ) {
if ( !c->saveYourselfDone && !c->waitForPhase2 ) {
SHUTDOWN_MARKER("completeShutdownOrCheckpoint state not done yet");
return; // not done yet
}
}
// do phase 2
bool waitForPhase2 = false;
for ( KSMClient* c = clients.first(); c; c = clients.next() ) {
if ( !c->saveYourselfDone && c->waitForPhase2 ) {
c->waitForPhase2 = false;
SmsSaveYourselfPhase2( c->connection() );
waitForPhase2 = true;
}
}
if ( waitForPhase2 ) {
SHUTDOWN_MARKER("completeShutdownOrCheckpoint state still waiting for Phase 2");
if (shutdownNotifierIPDlg) {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->setStatusMessage(i18n("Notifying remaining applications of logout request..."));
notificationTimer.start( KSMSERVER_NOTIFICATION_MANUAL_OPTIONS_TIMEOUT, true );
}
return;
}
SHUTDOWN_MARKER("Phase 2 complete");
bool showLogoutStatusDlg = TDEConfigGroup(TDEGlobal::config(), "Logout").readBoolEntry("showLogoutStatusDlg", true);
if (showLogoutStatusDlg && state != Checkpoint) {
KSMShutdownIPFeedback::showit(); // hide the UGLY logout process from the user
if (!shutdownNotifierIPDlg) {
shutdownNotifierIPDlg = KSMShutdownIPDlg::showShutdownIP();
}
while (!KSMShutdownIPFeedback::ispainted()) {
tqApp->processEvents();
}
}
notificationTimer.stop();
if (shutdownNotifierIPDlg) {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->hideNotificationActionButtons();
disconnect(shutdownNotifierIPDlg, SIGNAL(abortLogoutClicked()), this, SLOT(cancelShutdown()));
disconnect(shutdownNotifierIPDlg, SIGNAL(skipNotificationClicked()), this, SLOT(forceSkipSaveYourself()));
}
// synchronize any folders that were requested for shutdown sync
if (shutdownNotifierIPDlg) {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->setStatusMessage(i18n("Synchronizing remote folders").append("..."));
}
KRsync krs(this, "");
krs.executeLogoutAutoSync();
if (shutdownNotifierIPDlg) {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->setStatusMessage(i18n("Saving your settings..."));
}
if ( saveSession ) {
storeSession();
SHUTDOWN_MARKER("Session stored");
}
else {
discardSession();
SHUTDOWN_MARKER("Session discarded");
}
if ( state == Shutdown ) {
bool waitForKNotify = true;
if( !kapp->dcopClient()->connectDCOPSignal( "knotify", "",
"notifySignal(TQString,TQString,TQString,TQString,TQString,int,int,int,int)",
"ksmserver", "notifySlot(TQString,TQString,TQString,TQString,TQString,int,int,int,int)", false )) {
waitForKNotify = false;
}
if( !kapp->dcopClient()->connectDCOPSignal( "knotify", "",
"playingFinished(int,int)",
"ksmserver", "logoutSoundFinished(int,int)", false )) {
waitForKNotify = false;
}
// event() can return -1 if KNotifyClient short-circuits and avoids KNotify
logoutSoundEvent = KNotifyClient::event( 0, "exitkde" ); // KDE says good bye
if( logoutSoundEvent <= 0 ) {
waitForKNotify = false;
}
initialClientCount = clients.count();
if (shutdownNotifierIPDlg) {
TQString nextClientToKill;
for( KSMClient* c = clients.first(); c; c = clients.next()) {
if( isWM( c ) || isCM( c ) || isNotifier( c ) ) {
continue;
}
nextClientToKill = c->program();
}
if (nextClientToKill == "") {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->setStatusMessage(i18n("Closing applications (%1/%2)...").arg(initialClientCount-clients.count()).arg(initialClientCount));
}
else {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->setStatusMessage(i18n("Closing applications (%1/%2, %3)...").arg(initialClientCount-clients.count()).arg(initialClientCount).arg(nextClientToKill));
}
}
if( waitForKNotify ) {
state = WaitingForKNotify;
knotifyTimeoutTimer.start( 20000, true );
return;
}
startKilling();
}
else if ( state == Checkpoint ) {
for ( KSMClient* c = clients.first(); c; c = clients.next() ) {
SmsSaveComplete( c->connection());
}
state = Idle;
}
SHUTDOWN_MARKER("Fully shutdown");
}
void KSMServer::startKilling()
{
SHUTDOWN_MARKER("startKilling");
knotifyTimeoutTimer.stop();
// kill all clients
state = Killing;
for ( KSMClient* c = clients.first(); c; c = clients.next() ) {
if( isWM( c ) || isCM( c ) || isNotifier( c ) ) // kill the WM and CM as the last one in order to reduce flicker. Also wait to kill knotify to avoid logout delays
continue;
kdDebug( 1218 ) << "completeShutdown: client " << c->program() << "(" << c->clientId() << ")" << endl;
SmsDie( c->connection() );
}
kdDebug( 1218 ) << " We killed all clients. We have now clients.count()=" << clients.count() << endl;
completeKilling();
shutdownTimer.start( KSMSERVER_SHUTDOWN_CLIENT_UNRESPONSIVE_TIMEOUT, TRUE );
}
void KSMServer::completeKilling()
{
// Activity detected; reset forcible shutdown timer...
if (shutdownTimer.isActive()) {
shutdownTimer.start( KSMSERVER_SHUTDOWN_CLIENT_UNRESPONSIVE_TIMEOUT, TRUE );
}
SHUTDOWN_MARKER("completeKilling");
kdDebug( 1218 ) << "KSMServer::completeKilling clients.count()=" << clients.count() << endl;
if( state == Killing ) {
bool wait = false;
TQString nextClientToKill;
for( KSMClient* c = clients.first(); c; c = clients.next()) {
if( isWM( c ) || isCM( c ) || isNotifier( c ) ) {
continue;
}
nextClientToKill = c->program();
wait = true; // still waiting for clients to go away
}
if( wait ) {
if (shutdownNotifierIPDlg) {
if (nextClientToKill == "") {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->setStatusMessage(i18n("Closing applications (%1/%2)...").arg(initialClientCount-clients.count()).arg(initialClientCount));
}
else {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->setStatusMessage(i18n("Closing applications (%1/%2, %3)...").arg(initialClientCount-clients.count()).arg(initialClientCount).arg(nextClientToKill));
}
}
return;
}
else {
if (shutdownNotifierIPDlg) {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->setStatusMessage(i18n("Terminating services..."));
}
}
killWM();
}
}
void KSMServer::killWM()
{
SHUTDOWN_MARKER("killWM");
state = KillingWM;
bool iswm = false;
if (shutdownNotifierIPDlg) {
static_cast<KSMShutdownIPDlg*>(shutdownNotifierIPDlg)->closeSMDialog();
shutdownNotifierIPDlg=0;
}
for ( KSMClient* c = clients.first(); c; c = clients.next() ) {
if( isNotifier( c )) {
iswm = true;
SmsDie( c->connection() );
}
if( isCM( c )) {
iswm = true;
SmsDie( c->connection() );
}
if( isWM( c )) {
iswm = true;
kdDebug( 1218 ) << "killWM: client " << c->program() << "(" << c->clientId() << ")" << endl;
SmsDie( c->connection() );
}
}
if( iswm ) {
completeKillingWM();
TQTimer::singleShot( 5000, this, TQT_SLOT( timeoutWMQuit() ) );
}
else {
killingCompleted();
}
}
void KSMServer::completeKillingWM()
{
SHUTDOWN_MARKER("completeKillingWM");
kdDebug( 1218 ) << "KSMServer::completeKillingWM clients.count()=" << clients.count() << endl;
if( state == KillingWM ) {
if( clients.isEmpty()) {
killingCompleted();
}
}
}
// shutdown is fully complete
void KSMServer::killingCompleted()
{
SHUTDOWN_MARKER("killingCompleted");
DM dmObject;
int dmType = dmObject.type();
if ((dmType == DM::NewTDM) || (dmType == DM::OldTDM) || (dmType == DM::GDM)) {
// Hide any remaining windows until the X server is terminated by the display manager
pid_t child;
child = fork();
if (child != 0) {
kapp->quit();
}
}
else {
kapp->quit();
}
}
// called when KNotify performs notification for logout (not when sound is finished though)
void KSMServer::notifySlot(TQString event ,TQString app,TQString,TQString,TQString,int present,int,int,int)
{
SHUTDOWN_MARKER("notifySlot");
if( state != WaitingForKNotify ) {
SHUTDOWN_MARKER("notifySlot state != WaitingForKNotify");
return;
}
if( event != "exitkde" || app != "ksmserver" ) {
SHUTDOWN_MARKER("notifySlot event != \"exitkde\" || app != \"ksmserver\"");
return;
}
if( present & KNotifyClient::Sound ) { // logoutSoundFinished() will be called
SHUTDOWN_MARKER("notifySlot present & KNotifyClient::Sound");
return;
}
startKilling();
}
// This is stupid. The normal DCOP signal connected to notifySlot() above should be simply
// emitted in KNotify only after the sound is finished playing.
void KSMServer::logoutSoundFinished( int event, int )
{
SHUTDOWN_MARKER("logoutSoundFinished");
if( state != WaitingForKNotify ) {
return;
}
if( event != logoutSoundEvent ) {
return;
}
startKilling();
}
void KSMServer::knotifyTimeout()
{
SHUTDOWN_MARKER("knotifyTimeout");
if( state != WaitingForKNotify ) {
return;
}
startKilling();
}
void KSMServer::timeoutQuit()
{
SHUTDOWN_MARKER("timeoutQuit");
for (KSMClient *c = clients.first(); c; c = clients.next()) {
SHUTDOWN_MARKER(TQString("SmsDie timeout, client %1 (%2)").arg(c->program()).arg(c->clientId()).ascii());
kdWarning( 1218 ) << "SmsDie timeout, client " << c->program() << "(" << c->clientId() << ")" << endl;
}
killWM();
}
void KSMServer::timeoutWMQuit()
{
SHUTDOWN_MARKER("timeoutWMQuit");
if( state == KillingWM ) {
kdWarning( 1218 ) << "SmsDie WM timeout" << endl;
}
killingCompleted();
}
|