summaryrefslogtreecommitdiffstats
path: root/src/nfc/maemo6/socketrequestor.cpp
blob: 3ff527cbad95c1dfa37fa3783acd4f0de60d29dd (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
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtNfc module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "socketrequestor_p.h"

#include <QtCore/QMutex>
#include <QtCore/QHash>
#include <QtCore/QSocketNotifier>
#include <QtCore/QStringList>
#include <QtCore/QElapsedTimer>
#include <QtCore/QCoreApplication>
#include <QtDBus/QDBusObjectPath>

#include <dbus/dbus.h>

#include <sys/select.h>
#include <errno.h>

struct WatchNotifier
{
    DBusWatch *watch;
    QSocketNotifier *readNotifier;
    QSocketNotifier *writeNotifier;
};

static QVariant getVariantFromDBusMessage(DBusMessageIter *iter)
{
    dbus_bool_t bool_data;
    dbus_int32_t int32_data;
    dbus_uint32_t uint32_data;
    dbus_int64_t int64_data;
    dbus_uint64_t uint64_data;
    char *str_data;
    char char_data;
    int argtype = dbus_message_iter_get_arg_type(iter);

    switch (argtype) {
    case DBUS_TYPE_BOOLEAN: {
        dbus_message_iter_get_basic(iter, &bool_data);
        QVariant variant((bool)bool_data);
        return variant;
    }
    case DBUS_TYPE_ARRAY: {
        // Handle all arrays here
        int elem_type = dbus_message_iter_get_element_type(iter);
        DBusMessageIter array_iter;

        dbus_message_iter_recurse(iter, &array_iter);

        if (elem_type == DBUS_TYPE_BYTE) {
            QByteArray byte_array;
            do {
                dbus_message_iter_get_basic(&array_iter, &char_data);
                byte_array.append(char_data);
            } while (dbus_message_iter_next(&array_iter));
            QVariant variant(byte_array);
            return variant;
        } else if (elem_type == DBUS_TYPE_STRING) {
            QStringList str_list;
            do {
                dbus_message_iter_get_basic(&array_iter, &str_data);
                str_list.append(str_data);
            } while (dbus_message_iter_next(&array_iter));
            QVariant variant(str_list);
            return variant;
        } else {
            QVariantList variantList;
            do {
                variantList << getVariantFromDBusMessage(&array_iter);
            } while (dbus_message_iter_next(&array_iter));
            QVariant variant(variantList);
            return variant;
        }
        break;
    }
    case DBUS_TYPE_BYTE: {
        dbus_message_iter_get_basic(iter, &char_data);
        QChar ch(char_data);
        QVariant variant(ch);
        return variant;
    }
    case DBUS_TYPE_INT32: {
        dbus_message_iter_get_basic(iter, &int32_data);
        QVariant variant((int)int32_data);
        return variant;
    }
    case DBUS_TYPE_UINT32: {
        dbus_message_iter_get_basic(iter, &uint32_data);
        QVariant variant((uint)uint32_data);
        return variant;
    }
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_STRING: {
        dbus_message_iter_get_basic(iter, &str_data);
        QString str(str_data);
        QVariant variant(str);
        return variant;
    }
    case DBUS_TYPE_INT64: {
        dbus_message_iter_get_basic(iter, &int64_data);
        QVariant variant((qlonglong)int64_data);
        return variant;
    }
    case DBUS_TYPE_UINT64: {
        dbus_message_iter_get_basic(iter, &uint64_data);
        QVariant variant((qulonglong)uint64_data);
        return variant;
    }
    case DBUS_TYPE_DICT_ENTRY:
    case DBUS_TYPE_STRUCT: {
        // Handle all structs here
        DBusMessageIter struct_iter;
        dbus_message_iter_recurse(iter, &struct_iter);

        QVariantList variantList;
        do {
            variantList << getVariantFromDBusMessage(&struct_iter);
        } while (dbus_message_iter_next(&struct_iter));
        QVariant variant(variantList);
        return variant;
    }
    case DBUS_TYPE_VARIANT: {
        DBusMessageIter variant_iter;
        dbus_message_iter_recurse(iter, &variant_iter);

        return getVariantFromDBusMessage(&variant_iter);
    }
    case DBUS_TYPE_UNIX_FD: {
        dbus_message_iter_get_basic(iter, &uint32_data);
        QVariant variant((uint)uint32_data);
        return variant;
    }

    default:
        qWarning("Unsupported DBUS type: %d\n", argtype);
    }

    return QVariant();
}

class SocketRequestorPrivate : public QObject
{
    Q_OBJECT

public:
    SocketRequestorPrivate();
    ~SocketRequestorPrivate();

    DBusHandlerResult messageFilter(DBusConnection *connection, DBusMessage *message);
    void addWatch(DBusWatch *watch);

    void registerObject(const QString &path, SocketRequestor *object);
    void unregisterObject(const QString &path);

    Q_INVOKABLE void sendRequestAccess(const QString &adaptor, const QString &path,
                                       const QString &kind);
    Q_INVOKABLE void sendCancelAccessRequest(const QString &adaptor, const QString &path,
                                             const QString &kind);

    bool waitForDBusSignal(int msecs);

private:
    bool parseAccessFailed(DBusMessage *message, SocketRequestor *socketRequestor);
    bool parseAccessGranted(DBusMessage *message, SocketRequestor *socketRequestor);
    bool parseAcceptConnect(DBusMessage *message, SocketRequestor *socketRequestor,
                            const char *member);
    bool parseSocket(DBusMessage *message, SocketRequestor *socketRequestor, const char *member);
    bool parseErrorDenied(DBusMessage *message, SocketRequestor *socketRequestor);

private slots:
    void socketRead();
    void socketWrite();

private:
    QMutex m_mutex;
    DBusConnection *m_dbusConnection;
    QHash<QString, SocketRequestor *> m_dbusObjects;
    QMap<quint32, SocketRequestor *> m_pendingCalls;
    QList<WatchNotifier> m_watchNotifiers;
};

Q_GLOBAL_STATIC(SocketRequestorPrivate, socketRequestorPrivate)

static DBusHandlerResult dbusFilter(DBusConnection *connection, DBusMessage *message,
                                    void *userData)
{
    SocketRequestorPrivate *s = static_cast<SocketRequestorPrivate *>(userData);
    return s->messageFilter(connection, message);
}

static dbus_bool_t dbusWatch(DBusWatch *watch, void *data)
{
    SocketRequestorPrivate *s = static_cast<SocketRequestorPrivate *>(data);
    s->addWatch(watch);

    return true;
}

SocketRequestorPrivate::SocketRequestorPrivate()
{
    DBusError error;
    dbus_error_init(&error);
    m_dbusConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
    dbus_connection_add_filter(m_dbusConnection, &dbusFilter, this, 0);
    dbus_connection_set_watch_functions(m_dbusConnection, dbusWatch, 0, 0, this, 0);
    dbus_error_free(&error);
}

SocketRequestorPrivate::~SocketRequestorPrivate()
{
    dbus_connection_close(m_dbusConnection);
    dbus_connection_unref(m_dbusConnection);
}

DBusHandlerResult SocketRequestorPrivate::messageFilter(DBusConnection *connection,
                                                        DBusMessage *message)
{
    QMutexLocker locker(&m_mutex);

    foreach (const WatchNotifier &watchNotifier, m_watchNotifiers)
        watchNotifier.writeNotifier->setEnabled(true);

    if (connection != m_dbusConnection)
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

    SocketRequestor *socketRequestor;
    const QString path = QString::fromUtf8(dbus_message_get_path(message));
    quint32 serial = dbus_message_get_reply_serial(message);
    if (!path.isEmpty() && serial == 0)
        socketRequestor = m_dbusObjects.value(path);
    else if (path.isEmpty() && serial != 0)
        socketRequestor = m_pendingCalls.take(serial);
    else
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

    enum {
        NotHandled,
        Handled,
        HandledSendReply
    } handled;

    if (dbus_message_is_method_call(message, "com.nokia.nfc.AccessRequestor", "AccessFailed"))
        handled = parseAccessFailed(message, socketRequestor) ? HandledSendReply : NotHandled;
    else if (dbus_message_is_method_call(message, "com.nokia.nfc.AccessRequestor", "AccessGranted"))
        handled = parseAccessGranted(message, socketRequestor) ? HandledSendReply : NotHandled;
    else if (dbus_message_is_method_call(message, "com.nokia.nfc.LLCPRequestor", "Accept"))
        handled = parseAcceptConnect(message, socketRequestor, "accept") ? HandledSendReply : NotHandled;
    else if (dbus_message_is_method_call(message, "com.nokia.nfc.LLCPRequestor", "Connect"))
        handled = parseAcceptConnect(message, socketRequestor, "connect") ? HandledSendReply : NotHandled;
    else if (dbus_message_is_method_call(message, "com.nokia.nfc.LLCPRequestor", "Socket"))
        handled = parseSocket(message, socketRequestor, "socket") ? HandledSendReply : NotHandled;
    else if (dbus_message_is_error(message, "com.nokia.nfc.Error.Denied"))
        handled = parseErrorDenied(message, socketRequestor) ? Handled : NotHandled;
    else
        handled = NotHandled;

    if (handled == NotHandled)
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

    if (handled == HandledSendReply) {
        DBusMessage *reply = dbus_message_new_method_return(message);
        quint32 serial;
        dbus_connection_send(connection, reply, &serial);
    }

    return DBUS_HANDLER_RESULT_HANDLED;
}

bool SocketRequestorPrivate::parseErrorDenied(DBusMessage *message,
                                              SocketRequestor *socketRequestor)
{
    Q_UNUSED(message);

    QMetaObject::invokeMethod(socketRequestor, "accessFailed",
                              Q_ARG(QDBusObjectPath, QDBusObjectPath()),
                              Q_ARG(QString, QLatin1String("")),
                              Q_ARG(QString, QLatin1String("Access denied")));
    return true;
}

bool SocketRequestorPrivate::parseAccessFailed(DBusMessage *message,
                                               SocketRequestor *socketRequestor)
{
    Q_UNUSED(message);

    // m_mutex is locked in messageFilter()

    DBusMessageIter args;

    if (!dbus_message_iter_init(message, &args))
        return false;

    // read DBus Object Path
    QVariant objectPath = getVariantFromDBusMessage(&args);

    if (!dbus_message_iter_next(&args))
        return false;

    // read DBus kind string
    QVariant kind = getVariantFromDBusMessage(&args);

    if (!dbus_message_iter_next(&args))
        return false;

    // read DBus error string
    QVariant errorString = getVariantFromDBusMessage(&args);

    QMetaObject::invokeMethod(socketRequestor, "accessFailed",
                              Q_ARG(QDBusObjectPath, QDBusObjectPath(objectPath.toString())),
                              Q_ARG(QString, kind.toString()),
                              Q_ARG(QString, errorString.toString()));
    return true;
}

bool SocketRequestorPrivate::parseAccessGranted(DBusMessage *message,
                                                SocketRequestor *socketRequestor)
{
    Q_UNUSED(message);

    // m_mutex is locked in messageFilter()

    DBusMessageIter args;

    if (!dbus_message_iter_init(message, &args))
        return false;

    // read DBus Object Path
    QVariant objectPath = getVariantFromDBusMessage(&args);

    if (!dbus_message_iter_next(&args))
        return false;

    // read access kind
    QVariant kind = getVariantFromDBusMessage(&args);

    QMetaObject::invokeMethod(socketRequestor, "accessGranted",
                              Q_ARG(QDBusObjectPath, QDBusObjectPath(objectPath.toString())),
                              Q_ARG(QString, kind.toString()));
    return true;
}

bool SocketRequestorPrivate::parseAcceptConnect(DBusMessage *message,
                                                SocketRequestor *socketRequestor,
                                                const char *member)
{
    // m_mutex is locked in messageFilter()

    DBusMessageIter args;

    if (!dbus_message_iter_init(message, &args))
        return false;

    // read DBus Variant (lsap)
    QVariant lsap = getVariantFromDBusMessage(&args);

    if (!dbus_message_iter_next(&args))
        return false;
    // read DBus Variant (rsap)
    QVariant rsap = getVariantFromDBusMessage(&args);

    if (!dbus_message_iter_next(&args))
        return false;

    // read socket fd
    QVariant fd = getVariantFromDBusMessage(&args);

    if (!dbus_message_iter_next(&args))
        return false;

    // read DBus a{sv} into QVariantMap
    QVariant prop = getVariantFromDBusMessage(&args);
    QVariantMap properties;
    foreach (const QVariant &v, prop.toList()) {
        QVariantList vl = v.toList();
        if (vl.length() != 2)
            continue;

        properties.insert(vl.first().toString(), vl.at(1));
    }

    QMetaObject::invokeMethod(socketRequestor, member, Q_ARG(QDBusVariant, QDBusVariant(lsap)),
                              Q_ARG(QDBusVariant, QDBusVariant(rsap)),
                              Q_ARG(int, fd.toInt()), Q_ARG(QVariantMap, properties));

    return true;
}

bool SocketRequestorPrivate::parseSocket(DBusMessage *message, SocketRequestor *socketRequestor,
                                         const char *member)
{
    // m_mutex is locked in messageFilter()

    DBusMessageIter args;

    if (!dbus_message_iter_init(message, &args))
        return false;

    // read DBus Variant (lsap)
    QVariant lsap = getVariantFromDBusMessage(&args);

    if (!dbus_message_iter_next(&args))
        return false;

    // read socket fd
    QVariant fd = getVariantFromDBusMessage(&args);

    if (!dbus_message_iter_next(&args))
        return false;

    // read DBus a{sv} into QVariantMap
    QVariant prop = getVariantFromDBusMessage(&args);
    QVariantMap properties;
    foreach (const QVariant &v, prop.toList()) {
        QVariantList vl = v.toList();
        if (vl.length() != 2)
            continue;

        properties.insert(vl.first().toString(), vl.at(1));
    }

    QMetaObject::invokeMethod(socketRequestor, member, Q_ARG(QDBusVariant, QDBusVariant(lsap)),
                              Q_ARG(int, fd.toInt()), Q_ARG(QVariantMap, properties));

    return true;
}

void SocketRequestorPrivate::addWatch(DBusWatch *watch)
{
    QMutexLocker locker(&m_mutex);

    int fd = dbus_watch_get_unix_fd(watch);

    WatchNotifier watchNotifier;
    watchNotifier.watch = watch;

    watchNotifier.readNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
    connect(watchNotifier.readNotifier, SIGNAL(activated(int)), this, SLOT(socketRead()));
    watchNotifier.writeNotifier = new QSocketNotifier(fd, QSocketNotifier::Write, this);
    connect(watchNotifier.writeNotifier, SIGNAL(activated(int)), this, SLOT(socketWrite()));

    m_watchNotifiers.append(watchNotifier);
}

void SocketRequestorPrivate::socketRead()
{
    QMutexLocker locker(&m_mutex);

    QList<DBusWatch *> pendingWatches;

    foreach (const WatchNotifier &watchNotifier, m_watchNotifiers) {
        if (watchNotifier.readNotifier != sender())
            continue;

        pendingWatches.append(watchNotifier.watch);
    }

    DBusConnection *connection = m_dbusConnection;
    locker.unlock();

    foreach (DBusWatch *watch, pendingWatches)
        dbus_watch_handle(watch, DBUS_WATCH_READABLE);

    while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS);
}

void SocketRequestorPrivate::socketWrite()
{
    QMutexLocker locker(&m_mutex);

    QList<DBusWatch *> pendingWatches;

    foreach (const WatchNotifier &watchNotifier, m_watchNotifiers) {
        if (watchNotifier.writeNotifier != sender())
            continue;

        watchNotifier.writeNotifier->setEnabled(false);
        pendingWatches.append(watchNotifier.watch);
    }

    locker.unlock();

    foreach (DBusWatch *watch, pendingWatches)
        dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
}

void SocketRequestorPrivate::registerObject(const QString &path, SocketRequestor *object)
{
    QMutexLocker locker(&m_mutex);

    m_dbusObjects.insert(path, object);
}

void SocketRequestorPrivate::unregisterObject(const QString &path)
{
    QMutexLocker locker(&m_mutex);

    m_dbusObjects.remove(path);
}

void SocketRequestorPrivate::sendRequestAccess(const QString &adaptor, const QString &path,
                                               const QString &kind)
{
    QMutexLocker locker(&m_mutex);

    foreach (const WatchNotifier &watchNotifier, m_watchNotifiers)
        watchNotifier.writeNotifier->setEnabled(true);

    DBusMessage *message;
    DBusMessageIter args;

    message = dbus_message_new_method_call("com.nokia.nfc", adaptor.toLocal8Bit(),
                                           "com.nokia.nfc.Adapter", "RequestAccess");

    if (!message)
        return;

    dbus_message_iter_init_append(message, &args);
    const QByteArray p = path.toUtf8();
    const char *pData = p.constData();
    if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_OBJECT_PATH, &pData)) {
        dbus_message_unref(message);
        return;
    }

    const QByteArray k = kind.toUtf8();
    const char *kData = k.constData();
    if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &kData)) {
        dbus_message_unref(message);
        return;
    }

    quint32 serial;
    dbus_connection_send(m_dbusConnection, message, &serial);

    dbus_message_unref(message);

    m_pendingCalls.insert(serial, m_dbusObjects.value(path));
}

void SocketRequestorPrivate::sendCancelAccessRequest(const QString &adaptor, const QString &path,
                                                     const QString &kind)
{
    QMutexLocker locker(&m_mutex);

    foreach (const WatchNotifier &watchNotifier, m_watchNotifiers)
        watchNotifier.writeNotifier->setEnabled(true);

    DBusMessage *message;
    DBusMessageIter args;

    message = dbus_message_new_method_call("com.nokia.nfc", adaptor.toLocal8Bit(),
                                           "com.nokia.nfc.Adapter", "CancelAccessRequest");

    if (!message)
        return;

    dbus_message_iter_init_append(message, &args);
    const QByteArray p = path.toUtf8();
    const char *pData = p.constData();
    if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_OBJECT_PATH, &pData)) {
        dbus_message_unref(message);
        return;
    }

    const QByteArray k = kind.toUtf8();
    const char *kData = k.constData();
    if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &kData)) {
        dbus_message_unref(message);
        return;
    }

    quint32 serial;
    dbus_connection_send(m_dbusConnection, message, &serial);

    dbus_message_unref(message);
}

bool SocketRequestorPrivate::waitForDBusSignal(int msecs)
{
    dbus_connection_flush(m_dbusConnection);

    fd_set rfds;
    FD_ZERO(&rfds);

    int nfds = -1;
    foreach (const WatchNotifier &watchNotifier, m_watchNotifiers) {
        FD_SET(watchNotifier.readNotifier->socket(), &rfds);
        nfds = qMax(nfds, watchNotifier.readNotifier->socket());
    }

    timeval timeout;
    timeout.tv_sec = msecs / 1000;
    timeout.tv_usec = (msecs % 1000) * 1000;

    // timeout can not be 0 or else select will return an error
    if (msecs == 0)
        timeout.tv_usec = 1000;

    int result = -1;
    // on Linux timeout will be updated by select, but _not_ on other systems
    result = ::select(nfds + 1, &rfds, 0, 0, &timeout);
    if (result == -1 && errno != EINTR)
        return false;

    foreach (const WatchNotifier &watchNotifier, m_watchNotifiers) {
        if (FD_ISSET(watchNotifier.readNotifier->socket(), &rfds)) {
            QMetaObject::invokeMethod(watchNotifier.readNotifier, "activated",
                                      Q_ARG(int, watchNotifier.readNotifier->socket()));
        }
    }

    return true;
}


SocketRequestor::SocketRequestor(const QString &adaptor, QObject *parent)
:   QObject(parent), m_adaptor(adaptor)
{


}

SocketRequestor::~SocketRequestor()
{
}

void SocketRequestor::requestAccess(const QString &path, const QString &kind)
{
    SocketRequestorPrivate *s = socketRequestorPrivate();

    s->registerObject(path, this);

    QMetaObject::invokeMethod(s, "sendRequestAccess", Qt::QueuedConnection,
                              Q_ARG(QString, m_adaptor), Q_ARG(QString, path),
                              Q_ARG(QString, kind));
}

void SocketRequestor::cancelAccessRequest(const QString &path, const QString &kind)
{
    SocketRequestorPrivate *s = socketRequestorPrivate();

    s->unregisterObject(path);

    QMetaObject::invokeMethod(s, "sendCancelAccessRequest", Qt::QueuedConnection,
                              Q_ARG(QString, m_adaptor), Q_ARG(QString, path),
                              Q_ARG(QString, kind));
}

bool SocketRequestor::waitForDBusSignal(int msecs)
{
    SocketRequestorPrivate *s = socketRequestorPrivate();

    // Send queued method calls, i.e. requestAccess() and cancelAccessRequest().
    QCoreApplication::sendPostedEvents(s, QEvent::MetaCall);

    // Wait for DBus signal.
    bool result = socketRequestorPrivate()->waitForDBusSignal(msecs);

    // Send queued method calls, i.e. those from DBus.
    QCoreApplication::sendPostedEvents(this, QEvent::MetaCall);

    return result;
}

#include <moc_socketrequestor_p.cpp>
#include <socketrequestor.moc>