summaryrefslogtreecommitdiffstats
path: root/src/libraries/qmfclient/support/qcopadaptor.cpp
blob: a8f84c757d9a4a593bcbe7baf785d36e08f27ecc (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
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Messaging Framework.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qcopadaptor_p.h"
#include "qcopchannel_p.h"
#include <QtCore/qmap.h>
#include <QtCore/qmetaobject.h>
#include <QtCore/qdatastream.h>
#include <QtCore/qatomic.h>
#include "qmaillog.h"
#include <QtCore/qcoreapplication.h>
#include <QtCore/qvarlengtharray.h>
#include <QIODevice>
#include <stdlib.h>

/* ! - documentation comments in this file are diasabled:
    \class QCopAdaptor
    \inpublicgroup QtBaseModule

    \ingroup ipc
    \brief The QCopAdaptor class provides an interface to messages on a QCop IPC channel which simplifies remote signal and slot invocations.

    Using this class, it is very easy to convert a signal emission into an IPC
    message on a channel, and to convert an IPC message on a channel into a
    slot invocation.  In the following example, when the signal \c{valueChanged(int)}
    is emitted from the object \c source, the IPC message \c{changeValue(int)} will
    be sent on the channel \c{QPE/Foo}:

    \code
    QCopAdaptor *adaptor = new QCopAdaptor("QPE/Foo");
    QCopAdaptor::connect
        (source, SIGNAL(valueChanged(int)), adaptor, MESSAGE(changeValue(int)));
    \endcode

    Note that we use QCopAdaptor::connect() to connect the signal to the
    IPC message, not QObject::connect().  A common error is to use \c{connect()}
    without qualifying it with \c{QCopAdaptor::} and picking up
    QObject::connect() by mistake.

    On the server side of an IPC protocol, the \c{changeValue(int)} message can
    be connected to the slot \c{setValue()} on \c dest:

    \code
    QCopAdaptor *adaptor = new QCopAdaptor("QPE/Foo");
    QCopAdaptor::connect
        (adaptor, MESSAGE(changeValue(int)), dest, SLOT(setValue(int)));
    \endcode

    Now, whenever the client emits the \c{valueChanged(int)} signal, the
    \c{setValue(int)} slot will be automatically invoked on the server side,
    with the \c int parameter passed as its argument.

    Only certain parameter types can be passed across an IPC boundary in this fashion.
    The type must be visible to QVariant as a meta-type.  Many simple built-in
    types are already visible; for user-defined types, use Q_DECLARE_METATYPE()
    and qRegisterMetaTypeStreamOperators().

    \sa QCopChannel
*/

class QCopAdaptorChannel : public QCopChannel
{
    Q_OBJECT
public:
    QCopAdaptorChannel(const QString& channel, QCopAdaptor *adapt)
        : QCopChannel(channel, adapt), adaptor(adapt) {}
    ~QCopAdaptorChannel() {}

    void receive(const QString& msg, const QByteArray &data) override;

private:
    QCopAdaptor *adaptor;
};

void QCopAdaptorChannel::receive(const QString& msg, const QByteArray &data)
{
    adaptor->received(msg, data);
}

class QCopAdaptorSignalInfo
{
public:
    QObject *sender;
    int signalIndex;
    int destroyIndex;
    QString message;
    int *types;
    int numArgs;
};

class QCopAdaptorSlotInfo
{
public:
    ~QCopAdaptorSlotInfo()
    {
        free(types);
    }

    QObject *receiver;
    int memberIndex;
    bool destroyed;
    int returnType;
    int *types;
    int numArgs;
};

class QCopAdaptorPrivate : public QObject
{
    // Do not put Q_OBJECT here.
public:
    QCopAdaptorPrivate(QCopAdaptor *obj, const QString& chan);
    ~QCopAdaptorPrivate();

    QAtomicInt ref;
    QCopAdaptor *parent;
    QString channelName;
    bool connected;
    const QMetaObject *publishedTo;
    QMultiMap<QString, QCopAdaptorSlotInfo *> invokers;
    QList<QCopAdaptorSignalInfo *> signalList;
    int slotIndex;

    static const int QVariantId = -243;
    static int *connectionTypes(const QByteArray& member, int& nargs);
    static int typeFromName(const QByteArray& name);

protected:
    int qt_metacall(QMetaObject::Call c, int id, void **a) override;
};

QCopAdaptorPrivate::QCopAdaptorPrivate(QCopAdaptor *obj, const QString& chan)
    : ref(1), channelName(chan)
{
    parent = obj;
    connected = false;
    publishedTo = 0;

    // Fake slots start at this index in the QMetaObject.
    slotIndex = staticMetaObject.methodCount();
}

QCopAdaptorPrivate::~QCopAdaptorPrivate()
{
    qDeleteAll(invokers);

    // Disconnect all of the signals associated with this adaptor.
    int index = slotIndex;
    foreach (QCopAdaptorSignalInfo *info, signalList) {
        if (info->signalIndex >= 0) {
            QMetaObject::disconnect(info->sender, info->signalIndex,
                                    this, index);
        }
        if (info->destroyIndex >= 0) {
            QMetaObject::disconnect(info->sender, info->destroyIndex,
                                    this, index + 1);
        }
        free(info->types);
        delete info;
        index += 2;
    }
}

// Get the QVariant type number for a type name.
int QCopAdaptorPrivate::typeFromName( const QByteArray& type )
{
    int id;
    if (type.endsWith('*'))
        return QMetaType::VoidStar;
    else if ( type.size() == 0 || type == "void" )
        return QMetaType::Void;
    else if ( type == "QVariant" )
        return QCopAdaptorPrivate::QVariantId;
    id = QMetaType::type( type.constData() );
    if ( id != (int)QMetaType::Void )
        return id;
    return QVariant::nameToType(type);
}

// Returns the connection types associated with a signal or slot member.
int *QCopAdaptorPrivate::connectionTypes( const QByteArray& member, int& nargs )
{
    // Based on Qt's internal queuedConnectionTypes function.
    nargs = 0;
    int *types = 0;
    const char *s = member.constData();
    while (*s != '\0' && *s != '(') { ++s; }
    if ( *s == '\0' )
        return 0;
    ++s;
    const char *e = s;
    while (*e != ')') {
        ++e;
        if (*e == ')' || *e == ',')
            ++nargs;
    }

    types = (int *) malloc((nargs+1)*sizeof(int));
    types[nargs] = 0;
    for (int n = 0; n < nargs; ++n) {
        e = s;
        while (*s != ',' && *s != ')')
            ++s;
        QByteArray type(e, s-e);
        ++s;

        types[n] = typeFromName(type);
        if (!types[n]) {
            qWarning("QCopAdaptorPrivate::connectionTypes: Cannot marshal arguments of type '%s'", type.constData());
            free(types);
            return 0;
        }
    }
    return types;
}

int QCopAdaptorPrivate::qt_metacall(QMetaObject::Call c, int id, void **a)
{
    id = QObject::qt_metacall(c, id, a);
    if (id < 0)
        return id;
    if (c == QMetaObject::InvokeMetaMethod) {
        // Each signal that we have intercepted has two fake slots
        // associated with it.  The first is the activation slot.
        // The second is the destroyed() slot for the signal's object.
        if (id < signalList.size() * 2) {
            QCopAdaptorSignalInfo *info = signalList[id / 2];
            if ((id % 2) == 0) {
                // The signal we are interested in has been activated.
                if (info->types) {
                    QList<QVariant> args;
                    for (int i = 0; i < info->numArgs; ++i) {
                        if (info->types[i] != QCopAdaptorPrivate::QVariantId) {
                            QVariant arg(info->types[i], a[i + 1]);
                            args.append(arg);
                        } else {
                            args.append(*((const QVariant *)(a[i + 1])));
                        }
                    }
                    parent->sendMessage(info->message, args);
                }
            } else {
                // The sender has been destroyed.  Clear the signal indices
                // so that we don't try to do a manual disconnect when our
                // own destructor is called.
                info->signalIndex = -1;
                info->destroyIndex = -1;
            }
        }
        id -= signalList.size() * 2;
    }
    return id;
}

// Special variant class that can perform QDataStream operations
// without the QVariant header information.
class QCopAdaptorVariant : public QVariant
{
public:
    QCopAdaptorVariant() : QVariant() {}
    explicit QCopAdaptorVariant(const QVariant& value)
        : QVariant(value) {}

    void load(QDataStream& stream, int typeOrMetaType)
    {
        clear();
        create(typeOrMetaType, 0);
        d.is_null = false;
        QMetaType::load(stream, d.type, const_cast<void *>(constData()));
    }

    void save(QDataStream& stream) const
    {
        QMetaType::save(stream, d.type, constData());
    }
};

/* !
    Construct a Qt Extended IPC message object for \a channel and attach it to \a parent.
    If \a channel is empty, then messages are taken from the application's
    \c{appMessage} channel.
*/
QCopAdaptor::QCopAdaptor(const QString& channel, QObject *parent)
    : QObject(parent)
{
    d = new QCopAdaptorPrivate(this, channel);
}

/* !
    Destroy this Qt Extended IPC messaging object.
*/
QCopAdaptor::~QCopAdaptor()
{
    if (!d->ref.deref())
        delete d;
    d = 0;
}

/* !
    Returns the name of the channel that this adaptor is associated with.
*/
QString QCopAdaptor::channel() const
{
    return d->channelName;
}

/* !
    Connects \a signal on \a sender to \a member on \a receiver.  Returns true
    if the connection was possible; otherwise returns false.

    If either \a sender or \a receiver are instances of
    QCopAdaptor, this function will arrange for the signal
    to be delivered over a Qt Extended IPC channel.  If both \a sender and
    \a receiver are local, this function is identical
    to QObject::connect().

    If the same signal is connected to same slot multiple times,
    then signal delivery will happen that many times.
*/
bool QCopAdaptor::connect(QObject *sender, const QByteArray& signal,
                          QObject *receiver, const QByteArray& member)
{
    QCopAdaptor *senderProxy;
    QCopAdaptor *receiverProxy;

    // Bail out if the parameters are invalid.
    if (!sender || signal.isEmpty() || !receiver || member.isEmpty())
        return false;

    // Resolve the objects to find the remote proxies.
    senderProxy = qobject_cast<QCopAdaptor *>(sender);
    receiverProxy = qobject_cast<QCopAdaptor *>(receiver);

    // Remove proxies if the signal or member is not tagged with MESSAGE().
    if (!member.startsWith(QMESSAGE_CODE + '0'))
        receiverProxy = 0;
    if (!signal.startsWith(QMESSAGE_CODE + '0'))
        senderProxy = 0;

    // If neither has a proxy, then use a local connect.
    if (!senderProxy && !receiverProxy)
        return QObject::connect(sender, signal, receiver, member);

    // If both are still remote proxies, then fail the request.
    if (senderProxy && receiverProxy) {
        qWarning("QCopAdaptor::connect: cannot connect MESSAGE() to MESSAGE()");
        return false;
    }

    // Determine which direction the connect needs to happen in.
    if (receiverProxy) {
        // Connecting a local signal to a remote slot.
        return receiverProxy->connectLocalToRemote(sender, signal, member);
    } else {
        // Connecting a remote signal to a local slot.
        return senderProxy->connectRemoteToLocal(signal, receiver, member);
    }
}

/* !
    Publishes the signal or slot called \a member on this object on
    the Qt Extended IPC channel represented by this QCopAdaptor.

    If \a member is a slot, then whenever an application sends a
    message to the channel with that name, the system will arrange
    for the slot to be invoked.

    If \a member is a signal, then whenever this object emits that
    signal, the system will arrange for a message with that name to
    be sent on the channel.

    Returns false if \a member does not refer to a valid signal or slot.

    \sa publishAll()
*/
bool QCopAdaptor::publish(const QByteArray& member)
{
    // '1' is QSLOT_CODE in Qt 4.4 and below,
    // '5' is QSLOT_CODE in Qt 4.5 and higher.
    if (member.size() >= 1 && (member[0] == '1' || member[0] == '5')) {
        // Exporting a slot.
        return connectRemoteToLocal("3" + member.mid(1), this, member);
    } else {
        // Exporting a signal.
        return connectLocalToRemote(this, member, member);
    }
}

/* !
    \enum QCopAdaptor::PublishType
    Type of members to publish via QCopAdaptor.

    \value Signals Publish only signals.
    \value Slots Publish only public slots.
    \value SignalsAndSlots Publish both signals and public slots.
*/

/* !
    Publishes all signals or public slots on this object within subclasses of
    QCopAdaptor.  This is typically called from a subclass constructor.
    The \a type indicates if all signals, all public slots, or both, should
    be published.  Private and protected slots will never be published.

    \sa publish()
*/
void QCopAdaptor::publishAll(QCopAdaptor::PublishType type)
{
    const QMetaObject *meta = metaObject();
    if (meta != d->publishedTo) {
        int count = meta->methodCount();
        int index;
        if (d->publishedTo)
            index = d->publishedTo->methodCount();
        else
            index = QCopAdaptor::staticMetaObject.methodCount();
        for (; index < count; ++index) {

            QMetaMethod method = meta->method(index);
            if (method.methodType() == QMetaMethod::Slot &&
                 method.access() == QMetaMethod::Public &&
                 (type == Slots || type == SignalsAndSlots)) {
                QByteArray name = method.methodSignature();
                connectRemoteToLocal("3" + name, this, "1" + name);
            } else if (method.methodType() == QMetaMethod::Signal &&
                        (type == Signals || type == SignalsAndSlots)) {
                QByteArray name = method.methodSignature();
                connectLocalToRemote(this, "2" + name, "3" + name);
            }
        }
        d->publishedTo = meta;
    }
}

/* !
    Sends a message on the Qt Extended IPC channel which will cause the invocation
    of \a member on receiving objects.  The return value can be used
    to add arguments to the message before transmission.
*/
QCopAdaptorEnvelope QCopAdaptor::send(const QByteArray& member)
{
    return QCopAdaptorEnvelope
        (sendChannels(d->channelName), memberToMessage(member));
}

/* !
    Sends a message on the Qt Extended IPC channel which will cause the invocation
    of the single-argument \a member on receiving objects, with the
    argument \a arg1.
*/
void QCopAdaptor::send(const QByteArray& member, const QVariant &arg1)
{
    QList<QVariant> args;
    args.append(arg1);
    sendMessage(memberToMessage(member), args);
}

/* !
    Sends a message on the Qt Extended IPC channel which will cause the invocation
    of the double-argument \a member on receiving objects, with the
    arguments \a arg1 and \a arg2.
*/
void QCopAdaptor::send(const QByteArray& member, const QVariant &arg1, const QVariant &arg2)
{
    QList<QVariant> args;
    args.append(arg1);
    args.append(arg2);
    sendMessage(memberToMessage(member), args);
}

/* !
    Sends a message on the Qt Extended IPC channel which will cause the invocation
    of the triple-argument \a member on receiving objects, with the
    arguments \a arg1, \a arg2, and \a arg3.
*/
void QCopAdaptor::send(const QByteArray& member, const QVariant &arg1,
                       const QVariant &arg2, const QVariant &arg3)
{
    QList<QVariant> args;
    args.append(arg1);
    args.append(arg2);
    args.append(arg3);
    sendMessage(memberToMessage(member), args);
}

/* !
    Sends a message on the Qt Extended IPC channel which will cause the invocation
    of the quad-argument \a member on receiving objects, with the
    arguments \a arg1, \a arg2, \a arg3 and \a arg4.
*/
void QCopAdaptor::send(const QByteArray& member, const QVariant &arg1,
                       const QVariant &arg2, const QVariant &arg3,
                       const QVariant &arg4)
{
    QList<QVariant> args;
    args.append(arg1);
    args.append(arg2);
    args.append(arg3);
    args.append(arg4);
    sendMessage(memberToMessage(member), args);
}

/* !
    Sends a message on the Qt Extended IPC channel which will cause the invocation
    of the multi-argument \a member on receiving objects, with the
    argument list \a args.
*/
void QCopAdaptor::send(const QByteArray& member, const QList<QVariant>& args)
{
    sendMessage(memberToMessage(member), args);
}

/* !
    Returns true if the message on the Qt Extended IPC channel corresponding to \a signal
    has been connected to a local slot; otherwise returns false.
*/
bool QCopAdaptor::isConnected(const QByteArray& signal)
{
    return d->invokers.contains(memberToMessage(signal));
}

/* !
    Converts a signal or slot \a member name into a Qt Extended IPC message name.
    The default implementation strips the signal or slot prefix number
    from \a member and then normalizes the name to convert types
    such as \c{const QString&} into QString.

    \sa QMetaObject::normalizedSignature()
*/
QString QCopAdaptor::memberToMessage(const QByteArray& member)
{
    if (member.size() >= 1 && member[0] >= '0' && member[0] <= '9') {
        return QString::fromLatin1
            (QMetaObject::normalizedSignature(member.constData() + 1));
    } else {
        return QString::fromLatin1(member.data(), member.size());
    }
}

/* !
    Converts \a channel into a list of names to use for sending messages.
    The default implementation returns a list containing just \a channel.

    \sa receiveChannel()
*/
QStringList QCopAdaptor::sendChannels(const QString& channel)
{
    QStringList list;
    list << channel;
    return list;
}

/* !
    Converts \a channel into a new name to use for receiving messages.
    The default implementation returns \a channel.

    \sa sendChannels()
*/
QString QCopAdaptor::receiveChannel(const QString& channel)
{
    return channel;
}

void QCopAdaptor::received(const QString& msg, const QByteArray& data)
{
    // Increase the reference count on the private data just
    // in case the QCopAdaptor is deleted by one of the slots.
    QCopAdaptorPrivate *priv = d;
    priv->ref.ref();

    // Iterate through the slots for the message and invoke them.
    QMultiMap<QString, QCopAdaptorSlotInfo *>::ConstIterator iter;
    for (iter = priv->invokers.find(msg);
         iter != priv->invokers.end() && iter.key() == msg; ++iter) {
        QCopAdaptorSlotInfo *info = iter.value();
        if (info->destroyed)
            continue;

        // Convert "data" into a set of arguments suitable for qt_metacall.
        QDataStream stream(data);
        QList<QVariant> args;
        QVariant returnValue;
        QVarLengthArray<void *, 32> a(info->numArgs + 1);
        if (info->returnType != (int)QVariant::Invalid && info->returnType != (int)QMetaType::Void) {
            returnValue = QVariant(info->returnType, (const void *)0);
            a[0] = returnValue.data();
        } else {
            a[0] = 0;
        }
        for (int arg = 0; arg < info->numArgs; ++arg) {
            if (info->types[arg] != QCopAdaptorPrivate::QVariantId) {
                QCopAdaptorVariant temp;
                temp.load(stream, info->types[arg]);
                args.append(temp);
                a[arg + 1] = (void *)(args[arg].data());
            } else {
                // We need to handle QVariant specially because we actually
                // need the type header in this case.
                QVariant temp;
                stream >> temp;
                args.append(temp);
                a[arg + 1] = (void *)&(args[arg]);
            }
        }

        // Invoke the specified slot.
    #if !defined(QT_NO_EXCEPTIONS)
        try {
    #endif
            info->receiver->qt_metacall
                (QMetaObject::InvokeMetaMethod, info->memberIndex, a.data());
    #if !defined(QT_NO_EXCEPTIONS)
        } catch (...) {
        }
    #endif
    }

    // Decrease the reference count and delete if necessary.
    if (!priv->ref.deref())
        delete priv;
}

void QCopAdaptor::receiverDestroyed()
{
    // Mark all slot information blocks that match the receiver
    // as destroyed so that we don't try to invoke them again.
    QObject *obj = sender();
    QMultiMap<QString, QCopAdaptorSlotInfo *>::Iterator it;
    for (it = d->invokers.begin(); it != d->invokers.end(); ++it) {
        if (it.value()->receiver == obj)
            it.value()->destroyed = true;
    }
}

bool QCopAdaptor::connectLocalToRemote
    (QObject *sender, const QByteArray& signal, const QByteArray& member)
{
    QCopAdaptorSignalInfo *info = new QCopAdaptorSignalInfo();
    info->sender = sender;
    info->message = memberToMessage(member);

    // Resolve the signal name on the sender object.
    if (signal.size() > 0) {
        if (signal[0] != (QSIGNAL_CODE + '0')) {
            qWarning("QCopAdaptor: `%s' is not a valid signal "
                     "specification", signal.constData());
            delete info;
            return false;
        }
        QByteArray signalName =
            QMetaObject::normalizedSignature(signal.constData() + 1);
        info->signalIndex
            = sender->metaObject()->indexOfSignal(signalName.constData());
        if (info->signalIndex < 0) {
            qWarning("QCopAdaptor: no such signal: %s::%s",
                     sender->metaObject()->className(), signalName.constData());
            delete info;
            return false;
        }
        info->destroyIndex
            = sender->metaObject()->indexOfSignal("destroyed()");
        info->types = QCopAdaptorPrivate::connectionTypes
            (signalName, info->numArgs);
    } else {
        delete info;
        return false;
    }

    // Connect up the signals.
    int index = d->slotIndex + d->signalList.size() * 2;
    QMetaObject::connect(sender, info->signalIndex, d, index,
                         Qt::DirectConnection, 0);
    if (info->destroyIndex >= 0) {
        QMetaObject::connect(sender, info->destroyIndex, d, index + 1,
                             Qt::DirectConnection, 0);
    }

    // Add the signal information to the active list.
    d->signalList.append(info);
    return true;
}

bool QCopAdaptor::connectRemoteToLocal
    (const QByteArray& signal, QObject *receiver, const QByteArray& member)
{
    // Make sure that we are actively monitoring the channel for messages.
    if (!d->connected) {
        QString chan = receiveChannel(d->channelName);
        if (chan.isEmpty()) {
            // Qt Extended uses an empty channel name to indicate the
            // "application channel".  Messages on this special channel
            // are made available via the appMessage() signal on the
            // application object.
            QObject::connect
                (qApp, SIGNAL(appMessage(QString,QByteArray)),
                 this, SLOT(received(QString,QByteArray)));
        } else {
            // Short-cut the signal emits in QCopChannel for greater
            // performance when dispatching incoming messages.
            QCopAdaptorChannel *channel = new QCopAdaptorChannel(chan, this);
            QObject::connect(channel, SIGNAL(reconnectionTimeout()),
                             this, SIGNAL(reconnectionTimeout()));
            QObject::connect(channel, SIGNAL(connectionDown()),
                             this, SIGNAL(connectionDown()));
        }
        d->connected = true;
    }

    // Create a slot invoker to handle executing the member when necessary.
    QCopAdaptorSlotInfo *info = new QCopAdaptorSlotInfo();
    QByteArray name;
    if (member.size() > 0 && member[0] >= '0' && member[0] <= '9') {
        // Strip off the member type code.
        name = QMetaObject::normalizedSignature(member.constData() + 1);
    } else {
        name = QMetaObject::normalizedSignature(member.constData());
    }
    info->receiver = receiver;
    info->destroyed = false;
    info->returnType = 0;
    info->types = 0;
    info->numArgs = 0;
    if (receiver && name.size() > 0) {
        info->memberIndex
            = receiver->metaObject()->indexOfMethod(name.constData());
        if (info->memberIndex != -1) {
            connect(receiver, SIGNAL(destroyed()), this, SLOT(receiverDestroyed()));
            QMetaMethod method = receiver->metaObject()->method(info->memberIndex);
            info->returnType = QCopAdaptorPrivate::typeFromName(method.typeName());
            info->types = QCopAdaptorPrivate::connectionTypes(name, info->numArgs);
            if (!(info->types))
                info->destroyed = true;
        } else {
            qWarning("QCopAdaptor: no such member: %s::%s",
                     receiver->metaObject()->className(), name.constData());
        }
    } else {
        info->memberIndex = -1;
    }
    if (info->memberIndex == -1) {
        delete info;
        return false;
    }
    d->invokers.insert(memberToMessage(signal), info);
    return true;
}

void QCopAdaptor::sendMessage(const QString& msg, const QList<QVariant>& args)
{
    send(sendChannels(d->channelName), msg, args);
}

void QCopAdaptor::send
        (const QStringList& channels,
         const QString& msg, const QList<QVariant>& args)
{
    QByteArray array;
    {
        QDataStream stream
            (&array, QIODevice::WriteOnly | QIODevice::Append);
        QList<QVariant>::ConstIterator iter;
        if (!msg.contains(QLatin1String("QVariant"))) {
            for (iter = args.begin(); iter != args.end(); ++iter) {
                QCopAdaptorVariant copy(*iter);
                copy.save(stream);
            }
        } else {
            QByteArray name = msg.toLatin1();
            name = QMetaObject::normalizedSignature(name.constData());
            int numParams = 0;
            int *params = QCopAdaptorPrivate::connectionTypes
                    (name, numParams);
            int index = 0;
            for (iter = args.begin(); iter != args.end(); ++iter, ++index) {
                if (index < numParams &&
                     params[index] == QCopAdaptorPrivate::QVariantId) {
                    // We need to handle QVariant specially because we actually
                    // need the type header in this case.
                    stream << *iter;
                } else {
                    QCopAdaptorVariant copy(*iter);
                    copy.save(stream);
                }
            }
            if (params)
                free(params);
        }
        // Stream is flushed and closed at this point.
    }
    QStringList::ConstIterator iter;
    for (iter = channels.begin(); iter != channels.end(); ++iter)
        QCopChannel::send(*iter, msg, array);
}

/* !
    \class QCopAdaptorEnvelope
    \inpublicgroup QtBaseModule

    \ingroup ipc
    \brief The QCopAdaptorEnvelope class provides a mechanism to send Qt Extended IPC messages with an argument number of arguments.


    The most common way to use this class is to call QCopAdaptor::send(),
    as demonstrated in the following example:

    \code
    QCopAdaptor *channel = ...;
    QCopAdaptorEnvelope env = channel->send(MESSAGE(foo(QString)));
    env << "Hello";
    \endcode

    \sa QCopAdaptor
*/

class QCopAdaptorEnvelopePrivate
{
public:
    QStringList channels;
    QString message;
    bool shouldBeSent;
    QList<QVariant> arguments;
};

QCopAdaptorEnvelope::QCopAdaptorEnvelope
        (const QStringList& channels, const QString& message)
{
    d = new QCopAdaptorEnvelopePrivate();
    d->channels = channels;
    d->message = message;
    d->shouldBeSent = true;
}

/* !
    Construct an empty QCopAdaptorEnvelope.
*/
QCopAdaptorEnvelope::QCopAdaptorEnvelope()
{
    d = new QCopAdaptorEnvelopePrivate();
    d->shouldBeSent = false;
}

/* !
    Construct a copy of \a value.
*/
QCopAdaptorEnvelope::QCopAdaptorEnvelope(const QCopAdaptorEnvelope& value)
{
    d = new QCopAdaptorEnvelopePrivate();
    d->channels = value.d->channels;
    d->message = value.d->message;
    d->arguments = value.d->arguments;
    d->shouldBeSent = true;

    // If we make a copy of another object, that other object
    // must not be transmitted.  This typically happens when
    // we do the following:
    //
    //  QCopAdaptorEnvelope env = channel->send(MESSAGE(foo(QString)));
    //  env << "Hello";
    //
    // The intermediate copies of the envelope, prior to the arguments
    // being added, must not be transmitted.  Only the final version is.
    value.d->shouldBeSent = false;
}

/* !
    Destroy this envelope object and send the message.
*/
QCopAdaptorEnvelope::~QCopAdaptorEnvelope()
{
    if (d->shouldBeSent)
        QCopAdaptor::send(d->channels, d->message, d->arguments);
    delete d;
}

/* !
    Copy \a value into this object.
*/
QCopAdaptorEnvelope& QCopAdaptorEnvelope::operator=(const QCopAdaptorEnvelope& value)
{
    if (&value == this)
        return *this;

    d->channels = value.d->channels;
    d->message = value.d->message;
    d->arguments = value.d->arguments;

    // Don't transmit the original copy.  See above for details.
    d->shouldBeSent = true;
    value.d->shouldBeSent = false;

    return *this;
}

/* !
    \fn QCopAdaptorEnvelope& QCopAdaptorEnvelope::operator<<(const char *value)

    \overload
    Add \a value to the arguments for this Qt Extended IPC message.
*/

/* !
    \fn QCopAdaptorEnvelope& QCopAdaptorEnvelope::operator<<(const T &value)
    Add \a value to the arguments for this Qt Extended IPC message.
 */

void QCopAdaptorEnvelope::addArgument(const QVariant& value)
{
    d->arguments.append(value);
}

#include "qcopadaptor.moc"