1 /*****************************************************************
2 ksmserver - the KDE session management server
4 Copyright 2000 Matthias Ettrich <ettrich@kde.org>
5 Copyright 2005 Lubos Lunak <l.lunak@kde.org>
7 relatively small extensions by Oswald Buddenhagen <ob6@inf.tu-dresden.de>
9 some code taken from the dcopserver (part of the KDE libraries), which is
10 Copyright 1999 Matthias Ettrich <ettrich@kde.org>
11 Copyright 1999 Preston Brown <pbrown@kde.org>
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
20 The above copyright notice and this permission notice shall be included in
21 all copies or substantial portions of the Software.
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
27 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 ******************************************************************/
32 #include <config-workspace.h>
38 #ifdef HAVE_SYS_TIME_H
47 extern KSMServer
* the_server
;
49 KSMClient::KSMClient( SmsConn conn
)
56 KSMClient::~KSMClient()
58 foreach( SmProp
*prop
, properties
)
59 SmFreeProperty( prop
);
60 if (id
) free((void*)id
);
63 SmProp
* KSMClient::property( const char* name
) const
65 foreach ( SmProp
*prop
, properties
) {
66 if ( !qstrcmp( prop
->name
, name
) )
72 void KSMClient::resetState()
74 saveYourselfDone
= false;
75 pendingInteraction
= false;
76 waitForPhase2
= false;
81 * This fakes SmsGenerateClientID() in case we can't read our own hostname.
82 * In this case SmsGenerateClientID() returns NULL, but we really want a
83 * client ID, so we fake one.
85 K_GLOBAL_STATIC(QString
, my_addr
)
86 char * safeSmsGenerateClientID( SmsConn
/*c*/ )
88 // Causes delays with misconfigured network :-/.
89 // char *ret = SmsGenerateClientID(c);
92 if (my_addr
->isEmpty()) {
93 // qWarning("Can't get own host name. Your system is severely misconfigured\n");
95 /* Faking our IP address, the 0 below is "unknown" address format
96 (1 would be IP, 2 would be DEC-NET format) */
98 if( gethostname( hostname
, 255 ) != 0 )
99 my_addr
->sprintf("0%.8x", KRandom::random());
101 // create some kind of hash for the hostname
102 int addr
[ 4 ] = { 0, 0, 0, 0 };
104 for( unsigned int i
= 0;
105 i
< strlen( hostname
);
107 addr
[ pos
% 4 ] += hostname
[ i
];
112 *my_addr
+= QString::number( addr
[ i
], 16 );
115 /* Needs to be malloc(), to look the same as libSM */
116 ret
= (char *)malloc(1+my_addr
->length()+13+10+4+1 + /*safeness*/ 10);
117 static int sequence
= 0;
122 sprintf(ret
, "1%s%.13ld%.10d%.4d", my_addr
->toLatin1().constData(), (long)time(NULL
),
124 sequence
= (sequence
+ 1) % 10000;
129 void KSMClient::registerClient( const char* previousId
)
133 id
= safeSmsGenerateClientID( smsConn
);
134 SmsRegisterClientReply( smsConn
, (char*) id
);
135 SmsSaveYourself( smsConn
, SmSaveLocal
, false, SmInteractStyleNone
, false );
136 SmsSaveComplete( smsConn
);
137 the_server
->clientRegistered( previousId
);
141 QString
KSMClient::program() const
143 SmProp
* p
= property( SmProgram
);
144 if ( !p
|| qstrcmp( p
->type
, SmARRAY8
) || p
->num_vals
< 1)
146 return QLatin1String( (const char*) p
->vals
[0].value
);
149 QStringList
KSMClient::restartCommand() const
152 SmProp
* p
= property( SmRestartCommand
);
153 if ( !p
|| qstrcmp( p
->type
, SmLISTofARRAY8
) || p
->num_vals
< 1)
155 for ( int i
= 0; i
< p
->num_vals
; i
++ )
156 result
+=QLatin1String( (const char*) p
->vals
[i
].value
);
160 QStringList
KSMClient::discardCommand() const
163 SmProp
* p
= property( SmDiscardCommand
);
164 if ( !p
|| qstrcmp( p
->type
, SmLISTofARRAY8
) || p
->num_vals
< 1)
166 for ( int i
= 0; i
< p
->num_vals
; i
++ )
167 result
+=QLatin1String( (const char*) p
->vals
[i
].value
);
171 int KSMClient::restartStyleHint() const
173 SmProp
* p
= property( SmRestartStyleHint
);
174 if ( !p
|| qstrcmp( p
->type
, SmCARD8
) || p
->num_vals
< 1)
175 return SmRestartIfRunning
;
176 return *((int*)p
->vals
[0].value
);
179 QString
KSMClient::userId() const
181 SmProp
* p
= property( SmUserID
);
182 if ( !p
|| qstrcmp( p
->type
, SmARRAY8
) || p
->num_vals
< 1)
184 return QLatin1String( (const char*) p
->vals
[0].value
);