summaryrefslogtreecommitdiff
path: root/kde/daemon/handler.cpp
blob: 20460e4eeb0af9953b89d144658fb991ffe6fca7 (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
#include <config.h>

#include "handler.h"
#include "handler.moc"

#include <dcopref.h>
#include <qsocketnotifier.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kglobalsettings.h>
#include <kio/netaccess.h>
#include <kprocess.h>
#include <krun.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#ifdef HAVE_DPMS
#include <X11/extensions/dpms.h>
#endif

KDapiHandler::KDapiHandler()
    {
    setupSocket();
    }

KDapiHandler::~KDapiHandler()
    {
    while( !connections.isEmpty())
        closeSocket( *connections.begin());
    }

void KDapiHandler::setupSocket()
    {
    mainsocket = dapi_bindSocket();
    if( mainsocket < 0 )
        return;
    QSocketNotifier* notif = new QSocketNotifier( mainsocket, QSocketNotifier::Read, this );
    connect( notif, SIGNAL( activated( int )), SLOT( processMainSocketData()));
    }

void KDapiHandler::processMainSocketData()
    {
    DapiConnection* conn = dapi_acceptSocket( mainsocket );
    if( conn == NULL )
        return;
    ConnectionData data;
    data.conn = conn;
    data.notifier = new QSocketNotifier( dapi_socket( data.conn ), QSocketNotifier::Read, this );
    connect( data.notifier, SIGNAL( activated( int )), SLOT( processSocketData( int )));
    data.screensaver_suspend = false;
    connections.append( data );
    }

void KDapiHandler::processSocketData( int sock )
    {
    for( ConnectionList::Iterator it = connections.begin();
         it != connections.end();
         ++it )
        if( dapi_socket((*it).conn ) == sock )
            {
            processCommand( *it );
            break;
            }
    }
    
void KDapiHandler::processCommand( ConnectionData& conn )
    {
    int command;
    int seq;
    if( !dapi_readCommand( conn.conn, &command, &seq ))
        {
        closeSocket( conn );
        return;
        }
    switch( command )
        {
        case DAPI_COMMAND_INIT:
            processCommandInit( conn, seq );
            return;
        case DAPI_COMMAND_CAPABILITIES:
            processCommandCapabilities( conn, seq );
            return;
        case DAPI_COMMAND_OPENURL:
            processCommandOpenUrl( conn, seq );
            return;
        case DAPI_COMMAND_EXECUTEURL:
            processCommandExecuteUrl( conn, seq );
            return;
        case DAPI_COMMAND_BUTTONORDER:
            processCommandButtonOrder( conn, seq );
            return;
        case DAPI_COMMAND_RUNASUSER:
            processCommandRunAsUser( conn, seq );
            return;
        case DAPI_COMMAND_SUSPENDSCREENSAVING:
            processCommandSuspendScreensaving( conn, seq );
            return;
        case DAPI_COMMAND_MAILTO:
            processCommandMailTo( conn, seq );
            return;
        case DAPI_COMMAND_LOCALFILE:
            processCommandLocalFile( conn, seq );
            return;
        case DAPI_COMMAND_UPLOADFILE:
            processCommandUploadFile( conn, seq );
            return;
        case DAPI_COMMAND_REMOVETEMPORARYLOCALFILE:
            processCommandRemoveTemporaryLocalFile( conn, seq );
            return;
        }
    }

void KDapiHandler::closeSocket( ConnectionData& conn )
    {
    dapi_close( conn.conn );
    delete conn.notifier;
    for( ConnectionList::Iterator it = connections.begin();
         it != connections.end();
         ++it )
        if( (*it).conn == conn.conn )
            {
            connections.remove( it );
            break;
            }
    updateScreensaving();
    }

void KDapiHandler::processCommandInit( ConnectionData& conn, int seq )
    {
    if( !dapi_readCommandInit( conn.conn ))
        {
        closeSocket( conn );
        return;
        }
    dapi_writeReplyInit( conn.conn, seq, 1 );
    }

/* TODO */
static int caps[] =
    {    
    DAPI_COMMAND_INIT,
    DAPI_COMMAND_CAPABILITIES,
    DAPI_COMMAND_OPENURL,
    DAPI_COMMAND_EXECUTEURL,
    DAPI_COMMAND_BUTTONORDER,
    DAPI_COMMAND_RUNASUSER,
    DAPI_COMMAND_SUSPENDSCREENSAVING,
    DAPI_COMMAND_MAILTO,
    DAPI_COMMAND_LOCALFILE,
    DAPI_COMMAND_UPLOADFILE,
    DAPI_COMMAND_REMOVETEMPORARYLOCALFILE
    };

void KDapiHandler::processCommandCapabilities( ConnectionData& conn, int seq )
    {
    intarr capabilities;
    if( !dapi_readCommandInit( conn.conn ))
        {
        closeSocket( conn );
        return;
        }
    capabilities.count = sizeof( caps ) / sizeof( caps[ 0 ] );
    capabilities.data = caps;
    dapi_writeReplyCapabilities( conn.conn, seq, capabilities, 1 );
    }

void KDapiHandler::processCommandOpenUrl( ConnectionData& conn, int seq )
    {
    char* url;
    DapiWindowInfo winfo;
    if( !dapi_readCommandOpenUrl( conn.conn, &url, &winfo ))
        {
        closeSocket( conn );
        return;
        }
    kapp->invokeBrowser( url, makeStartupInfo( winfo ));
    dapi_writeReplyOpenUrl( conn.conn, seq, 1 );
    free( url );
    dapi_freeWindowInfo( winfo );
    }

void KDapiHandler::processCommandExecuteUrl( ConnectionData& conn, int seq )
    {
    char* url;
    DapiWindowInfo winfo;
    if( !dapi_readCommandExecuteUrl( conn.conn, &url, &winfo ))
        {
        closeSocket( conn );
        return;
        }
    new KRun( url ); // TODO startup info
    dapi_writeReplyExecuteUrl( conn.conn, seq, 1 );
    free( url );
    dapi_freeWindowInfo( winfo );
    }

void KDapiHandler::processCommandButtonOrder( ConnectionData& conn, int seq )
    {
    if( !dapi_readCommandButtonOrder( conn.conn ))
        {
        closeSocket( conn );
        return;
        }
    int order = KGlobalSettings::buttonLayout();
    // TODO KDE has actually more layouts, but I have no idea what they're supposed to mean
    dapi_writeReplyButtonOrder( conn.conn, seq, order == 1 ? 2 : 1 );
    }

void KDapiHandler::processCommandRunAsUser( ConnectionData& conn, int seq )
    {
    char* user;
    char* command;
    DapiWindowInfo winfo;
    if( !dapi_readCommandRunAsUser( conn.conn, &user, &command, &winfo ))
        {
        closeSocket( conn );
        return;
        }
    KProcess proc; // TODO startup info
    proc.setUseShell( true ); // TODO quoting
    proc << "kdesu";
    if( user[ 0 ] != '\0' )
        proc << "-u" << user;
    proc << "--" << command;
    bool ret = proc.start( KProcess::DontCare );
    dapi_writeReplyRunAsUser( conn.conn, seq, ret ? 1 : 0 );
    free( user );
    free( command );
    dapi_freeWindowInfo( winfo );
    }

void KDapiHandler::processCommandSuspendScreensaving( ConnectionData& conn, int seq )
    {
    int suspend;
    if( !dapi_readCommandSuspendScreensaving( conn.conn, &suspend ))
        {
        closeSocket( conn );
        return;
        }
    conn.screensaver_suspend = suspend;
    updateScreensaving();
    dapi_writeReplySuspendScreensaving( conn.conn, seq, 1 );
    }

void KDapiHandler::updateScreensaving()
    {
    bool suspend = false;
    for( ConnectionList::ConstIterator it = connections.begin();
         it != connections.end();
         ++it )
        {
        if( (*it).screensaver_suspend )
            {
            suspend = true;
            break;
            }
        }
#ifdef HAVE_DPMS
    if( suspend )
        DPMSDisable( qt_xdisplay());
    else
        DPMSEnable( qt_xdisplay());
#endif
    DCOPRef ref( "kdesktop", "KScreensaverIface" );
    ref.call( "enable", !suspend );
    }

void KDapiHandler::processCommandMailTo( ConnectionData& conn, int seq )
    {
    char* subject;
    char* body;
    char* to;
    char* cc;
    char* bcc;
    stringarr attachments;
    DapiWindowInfo winfo;
    if( !dapi_readCommandMailTo( conn.conn, &subject, &body, &to, &cc, &bcc, &attachments, &winfo ))
        {
        closeSocket( conn );
        return;
        }
    QStringList attachurls;
    for( int i = 0;
         i < attachments.count;
         ++i )
        attachurls.append( QString::fromUtf8( attachments.data[ i ] ));
    kapp->invokeMailer( QString::fromUtf8( to ), QString::fromUtf8( cc ), QString::fromUtf8( bcc ),
        QString::fromUtf8( subject ), QString::fromUtf8( body ), QString(), attachurls, makeStartupInfo( winfo ));
    dapi_writeReplyMailTo( conn.conn, seq, 1 );
    free( subject );
    free( body );
    free( to );
    free( cc );
    free( bcc );
    dapi_freestringarr( attachments );
    dapi_freeWindowInfo( winfo );
    }

void KDapiHandler::processCommandLocalFile( ConnectionData& conn, int seq )
    {
    char* file;
    char* local;
    int allow_download;
    DapiWindowInfo winfo;
    if( !dapi_readCommandLocalFile( conn.conn, &file, &local, &allow_download, &winfo ))
        {
        closeSocket( conn );
        return;
        }
    KURL url = KURL::fromPathOrURL( QString::fromUtf8( file ));
    free( file );
    QString result;
    if( !url.isValid())
        ; // result is empty
    else if( url.isLocalFile())
        result = url.path();
    else
        {
        QString target = QString::fromUtf8( local );
        // TODO use KIO directly instead of NetAccess
        if( allow_download && KIO::NetAccess::download( url, target, 0 )) // TODO window
            result = target;
        }
    dapi_writeReplyLocalFile( conn.conn, seq, result.utf8());
    dapi_freeWindowInfo( winfo );
    }

void KDapiHandler::processCommandUploadFile( ConnectionData& conn, int seq )
    {
    char* local;
    char* file;
    int remove_local;
    DapiWindowInfo winfo;
    if( !dapi_readCommandUploadFile( conn.conn, &local, &file, &remove_local, &winfo ))
        {
        closeSocket( conn );
        return;
        }
    KURL url = KURL::fromPathOrURL( QString::fromUtf8( file ));
    QString localfile = QString::fromUtf8( local );
    free( local );
    free( file );
    int ok = 0;
    if( !url.isValid())
        ok = 0;
    else if( url.isLocalFile())
        ok = 1; // no-op
    else
        {
        // TODO use KIO directly instead of NetAccess
        if( KIO::NetAccess::upload( localfile, url, 0 )) // TODO window
        // TODO this apparently returns success even with e.g. http - check somehow
            {
            if( remove_local )
                KIO::NetAccess::removeTempFile( localfile );
            ok = 1;
            }
        }
    dapi_writeReplyUploadFile( conn.conn, seq, ok );
    dapi_freeWindowInfo( winfo );
    }

void KDapiHandler::processCommandRemoveTemporaryLocalFile( ConnectionData& conn, int seq )
    {
    char* file;
    if( !dapi_readCommandRemoveTemporaryLocalFile( conn.conn, &file ))
        {
        closeSocket( conn );
        return;
        }
    KIO::NetAccess::removeTempFile( QString::fromUtf8( file ));
    dapi_writeReplyRemoveTemporaryLocalFile( conn.conn, seq, 1 );
    free( file );
    }

QCString KDapiHandler::makeStartupInfo( const DapiWindowInfo& winfo )
    {
    WId window = winfo.window;
    if( window != 0 )
        {
        // TODO for the window !=0 case KStartupInfo API needs to be extended to accept
        // external timestamp for creating new startup info
        kapp->updateUserTimestamp();
        }
    else
        {
        kapp->updateUserTimestamp();
        }
    return KStartupInfo::createNewStartupId();
    }