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

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

#include "kabchandler.h"

#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 <ktempfile.h>

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

KDapiHandler::KDapiHandler()
    {
    setupSocket();
    kabchandler = new KABCHandler(this);
    }

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;
        case DAPI_COMMAND_ADDRESSBOOKLIST:
            processCommandAddressBookList( conn, seq );
            return;
        case DAPI_COMMAND_ADDRESSBOOKGETNAME:
            processCommandAddressBookGetName( conn, seq );
            return;
        case DAPI_COMMAND_ADDRESSBOOKGETEMAILS:
            processCommandAddressBookGetEmails( conn, seq );
            return;
        case DAPI_COMMAND_ADDRESSBOOKFINDBYNAME:
            processCommandAddressBookFindByName( conn, seq );
            return;
        case DAPI_COMMAND_ADDRESSBOOKOWNER:
            processCommandAddressBookOwner( conn, seq );
            return;
        case DAPI_COMMAND_ADDRESSBOOKGETVCARD30:
            processCommandAddressBookGetVCard30( 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 );
    }

static QValueList< KTempFile* > tempfiles;

static void removeTempFile( const QString file )
    {
    for( QValueList< KTempFile* >::Iterator it = tempfiles.begin();
         it != tempfiles.end();
         ++it )
        if( (*it)->name() == file )
            {
            delete *it;
            tempfiles.remove( it );
            return;
            }
    }

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 if( allow_download )
        {
        QString target = QString::fromUtf8( local );
        KTempFile* tmp = NULL;
        KURL dest;
        if( !target.isEmpty())
            dest.setPath( target );
        else
            {
            tmp = new KTempFile;
            dest.setPath( tmp->name());
            tempfiles.append( tmp );
            }
        KIO::FileCopyJob* job = KIO::file_copy( url, dest, -1, true, false );
        KDapiFakeWidget* widget = winfo.window != 0 ? new KDapiFakeWidget( winfo.window ) : NULL;
        job->setWindow( widget );
        KDapiDownloadJob* upload = new KDapiDownloadJob( job, conn.conn, seq, widget );
        connect( job, SIGNAL( result( KIO::Job* )), upload, SLOT( done()));
        dapi_freeWindowInfo( winfo );
        // will write reply asynchronously
        return;
        }
    dapi_writeReplyLocalFile( conn.conn, seq, result.utf8());
    dapi_freeWindowInfo( winfo );
    }

void KDapiDownloadJob::done()
    {
    if( job->error() == 0 )
        dapi_writeReplyLocalFile( conn, seq, job->destURL().path().utf8());
    else
        dapi_writeReplyLocalFile( conn, seq, NULL );
    delete widget;
    deleteLater();
    }

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() && url.path() == localfile )
        ok = 1; // no-op
    else
        {
        KURL src;
        src.setPath( localfile );
        KIO::FileCopyJob* job = KIO::file_copy( src, url, -1, true, false );
        KDapiFakeWidget* widget = winfo.window != 0 ? new KDapiFakeWidget( winfo.window ) : NULL;
        job->setWindow( widget );
        KDapiUploadJob* upload = new KDapiUploadJob( job, conn.conn, seq, remove_local, widget );
        connect( job, SIGNAL( result( KIO::Job* )), upload, SLOT( done()));
        dapi_freeWindowInfo( winfo );
        // will write reply asynchronously
        return;
        }
    dapi_writeReplyUploadFile( conn.conn, seq, ok );
    dapi_freeWindowInfo( winfo );
    }

void KDapiUploadJob::done()
    {
    // TODO this apparently returns success even with e.g. http - check somehow
    dapi_writeReplyUploadFile( conn, seq, job->error() == 0 );
    if( job->error() == 0 && remove_local )
        removeTempFile( job->srcURL().path());
    delete widget;
    }

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

void KDapiHandler::processCommandAddressBookList( ConnectionData& conn, int seq )
    {
    if( !dapi_readCommandAddressBookList( conn.conn ) )
        {
        closeSocket( conn );
        return;
        }

    QStringList kabcUIDs = kabchandler->listUIDs();

    stringarr idList;
    idList.data = 0;
    idList.count = 0;
    if (kabcUIDs.count() > 0)
        {
        idList.data = (char**) malloc( sizeof( char* ) * (kabcUIDs.count() + 1 ) );
        QStringList::const_iterator it    = kabcUIDs.begin();
        QStringList::const_iterator endIt = kabcUIDs.end();
        for ( uint i = 0; it != endIt; ++it, ++i )
            {
            QCString string = (*it).utf8();
            idList.data[i] = strndup( string.data(), string.count() );
            }
        idList.data[kabcUIDs.count()] = NULL;
        idList.count = kabcUIDs.count();
        }

    dapi_writeReplyAddressBookList( conn.conn, seq, idList, kabcUIDs.count() );

    dapi_freestringarr( idList );
    }

void KDapiHandler::processCommandAddressBookGetName( ConnectionData& conn, int seq )
    {
    char* id;
    if( !dapi_readCommandAddressBookGetName( conn.conn, &id ) )
        {
        closeSocket( conn );
        return;
        }

    QCString firstname;
    QCString lastname;
    QCString fullname;

    bool ok = kabchandler->getNames( QString::fromUtf8( id ),
                                     firstname, lastname, fullname );
    free( id );

    dapi_writeReplyAddressBookGetName( conn.conn, seq, firstname.data(), lastname.data(),
                                       fullname.data(), ok );

    }

void KDapiHandler::processCommandAddressBookGetEmails( ConnectionData& conn, int seq )
    {
    char* id;
    if( !dapi_readCommandAddressBookGetEmails( conn.conn, &id ) )
        {
        closeSocket( conn );
        return;
        }

    QStringList emails;
    bool ok = kabchandler->getEmails( QString::fromUtf8( id ), emails );

    free( id );

    stringarr emailList;
    emailList.data = 0;
    emailList.count = 0;
    if (emails.count() > 0)
        {
        emailList.data = (char**) malloc( sizeof( char* ) * ( emails.count() + 1 ) );
        QStringList::const_iterator it    = emails.begin();
        QStringList::const_iterator endIt = emails.end();
        for ( uint i = 0; it != endIt; ++it, ++i )
            {
            QCString string = (*it).utf8();
            emailList.data[i] = strdup( string.data() );
            }
        emailList.data[emails.count()] = NULL;
        emailList.count = emails.count();
        }

    dapi_writeReplyAddressBookGetEmails( conn.conn, seq, emailList, ok );

    dapi_freestringarr( emailList );
    }

void KDapiHandler::processCommandAddressBookFindByName( ConnectionData& conn, int seq )
    {
    char* id;
    if( !dapi_readCommandAddressBookFindByName( conn.conn, &id ) )
        {
        closeSocket( conn );
        return;
        }

    QStringList kabcUIDs = kabchandler->findByName( QString::fromUtf8( id ) );

    free( id );

    stringarr idList;
    idList.data = 0;
    idList.count = 0;
    if (kabcUIDs.count() > 0)
        {
        idList.data = (char**) malloc( sizeof( char* ) * (kabcUIDs.count() + 1 ) );
        QStringList::const_iterator it    = kabcUIDs.begin();
        QStringList::const_iterator endIt = kabcUIDs.end();
        for ( uint i = 0; it != endIt; ++it, ++i )
            {
            QCString string = (*it).utf8();
            idList.data[i] = strndup( string.data(), string.count() );
            }
        idList.data[kabcUIDs.count()] = NULL;
        idList.count = kabcUIDs.count();
        }

    dapi_writeReplyAddressBookFindByName( conn.conn, seq, idList, kabcUIDs.count() );

    dapi_freestringarr( idList );
    }

void KDapiHandler::processCommandAddressBookOwner( ConnectionData& conn, int seq )
    {
    if( !dapi_readCommandAddressBookOwner( conn.conn ) )
        {
        closeSocket( conn );
        return;
        }

    QString ownerUID = kabchandler->owner();

    bool ok = !ownerUID.isEmpty();

    dapi_writeReplyAddressBookOwner( conn.conn, seq, (ok ? ownerUID.utf8().data() : 0), ok );
    }

void KDapiHandler::processCommandAddressBookGetVCard30( ConnectionData& conn, int seq )
    {
    char* id;
    if( !dapi_readCommandAddressBookGetVCard30( conn.conn, &id ) )
        {
        closeSocket( conn );
        return;
        }

    QString vcard = kabchandler->vcard30( QString::fromUtf8( id ) );

    free( id );

    bool ok = !vcard.isEmpty();

    dapi_writeReplyAddressBookGetVCard30( conn.conn, seq, (ok ? vcard.utf8().data() : 0), ok );

    }

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();
    }

// some KDE APIs accept only QWidget* instead of WId
KDapiFakeWidget::KDapiFakeWidget( WId window )
    {
    create( window, false ); // don't init
    }

KDapiFakeWidget::~KDapiFakeWidget()
    {
    destroy( false ); // no cleanup
    }