summaryrefslogtreecommitdiff
path: root/qt4/TelepathyQt4/abstract-client.cpp
blob: 8153faa13c3f5dd03a79260c66a982dfbd060d8d (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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/**
 * This file is part of TelepathyQt4
 *
 * @copyright Copyright (C) 2009-2010 Collabora Ltd. <http://www.collabora.co.uk/>
 * @copyright Copyright (C) 2009-2010 Nokia Corporation
 * @license LGPL 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <TelepathyQt4/AbstractClient>

#include <QSharedData>
#include <QString>

#include <TelepathyQt4/ChannelClassSpecList>

namespace Tp
{

/**
 * \class AbstractClient
 * \ingroup clientclient
 * \headerfile TelepathyQt4/abstract-client.h <TelepathyQt4/AbstractClient>
 *
 * \brief The AbstractClient class represents a Telepathy client.
 *
 * Clients are programs used to process channels, approving, handling or
 * observing them. User interface processes are the obvious example of clients,
 * but they can provide other functionality, such as address-book
 * synchronization, message logging, etc.
 *
 * Each client is either an observer, an approver, a handler, or some
 * combination of these.
 *
 * Clients can be activatable services (those with a D-Bus .service file)
 * so that they can run in response to channel creation, or non-activatable
 * services (those that do not register a D-Bus .service file
 * for their well-known name, but do request it at runtime) so
 * that they can process channels, but only if they are already
 * running - for instance, a full-screen media center application might do this.
 *
 * As an optimization, service-activatable clients should install a file
 * $XDG_DATA_DIRS/telepathy/clients/clientname.client containing a cached version
 * of their immutable properties. The syntax of these files is documented in the <a
 * href="http://telepathy.freedesktop.org/spec/org.freedesktop.Telepathy.Client.html">
 * Telepathy specification</a>.
 *
 * Non-activatable clients may install a .client file, but there's not much
 * point in them doing so.
 *
 * This is a base class and should not be used directly, use the
 * specialized classes AbstractClientObserver, AbstractClientApprover and
 * AbstractClientHandler instead.
 *
 * If the same process wants to be either a mix of observer, approver and
 * handler, or a combination of those it can multiple inherit the specialized
 * abstract classes.
 *
 * \sa AbstractClientObserver, AbstractClientApprover, AbstractClientHandler
 */

/**
 * Construct a new AbstractClient object.
 *
 * Note that this is a base class and should not be used directly, use the
 * specialized classes AbstractClientObserver, AbstractClientApprover and
 * AbstractClientHandler instead.
 */
AbstractClient::AbstractClient()
{
}

/**
 * Class destructor.
 */
AbstractClient::~AbstractClient()
{
}

struct TELEPATHY_QT4_NO_EXPORT AbstractClientObserver::Private
{
    Private(const ChannelClassList &channelFilter, bool shouldRecover)
        : channelFilter(channelFilter),
          shouldRecover(shouldRecover)
    {
    }

    ChannelClassList channelFilter;
    bool shouldRecover;
};

/**
 * \class AbstractClientObserver
 * \ingroup clientclient
 * \headerfile TelepathyQt4/abstract-client.h <TelepathyQt4/AbstractClientObserver>
 *
 * \brief The AbstractClientObserver class represents a Telepathy observer.
 *
 * Observers are clients that monitor the creation of new channels.
 * This functionality can be used for things like message logging.
 *
 * Observers should not modify the state of a channel except via user
 * interaction.
 *
 * Observers must not carry out actions that exactly one process must take
 * responsibility for (e.g. acknowledging text messages, or carrying out
 * the actual file transfer), since arbitrarily many observers can be
 * activated for each channel. The handler is responsible for such tasks.
 *
 * Handlers may, of course, delegate responsibility for these tasks to other
 * clients (including those run as observers), but this must be done
 * explicitly via a request from the handler to the observer.
 *
 * Whenever a collection of new channels is signalled, the channel dispatcher
 * will notify all running or activatable observers whose filter indicates that
 * they are interested in some of the channels.
 *
 * Observers are activated for all channels in which they have registered an
 * interest - incoming, outgoing or automatically created - although of course
 * the filter property can be set to filter specific channels.
 *
 * To become an observer one should inherit AbstractClientObserver and
 * implement the pure virtual observeChannels() method. After that the object
 * representing the observer must be registered using
 * ClientRegistrar::registerClient().
 *
 * When new channels in which the observer has registered an interest are
 * announced, the method observeChannels() is invoked. All observers are
 * notified simultaneously.
 *
 * \section observer_usage_sec Usage
 *
 * \subsection observer_create_sec Implementing an observer
 *
 * \code
 *
 * class MyObserver : public AbstractClientObserver
 * {
 * public:
 *     MyObserver(const ChannelClassSpecList &channelFilter);
 *     ~MyObserver() { }
 *
 *     void observeChannels(const MethodInvocationContextPtr<> &context,
 *             const AccountPtr &account,
 *             const ConnectionPtr &connection,
 *             const QList<ChannelPtr> &channels,
 *             const ChannelDispatchOperationPtr &dispatchOperation,
 *             const QList<ChannelRequestPtr> &requestsSatisfied,
 *             const AbstractClientObserver::ObserverInfo &observerInfo);
 * };
 *
 * MyObserver::MyObserver(const ChannelClassSpecList &channelFilter)
 *     : AbstractClientObserver(channelFilter)
 * {
 * }
 *
 * void MyObserver::observeChannels(const MethodInvocationContextPtr<> &context,
 *         const AccountPtr &account,
 *         const ConnectionPtr &connection,
 *         const QList<ChannelPtr> &channels,
 *         const ChannelDispatchOperationPtr &dispatchOperation,
 *         const QList<ChannelRequestPtr> &requestsSatisfied,
 *         const AbstractClientObserver::ObserverInfo &observerInfo)
 * {
 *     // do something, log messages, ...
 *
 *     context->setFinished();
 * }
 *
 * \endcode
 *
 * \subsection observer_register_sec Registering an observer
 *
 * \code
 *
 * ClientRegistrar registrar = ClientRegistrar::create();
 * AbstractClientPtr observer = AbstractClientPtr::dynamicCast(
 *         SharedPtr<MyObserver>(new MyObserver(
 *             ChannelClassSpecList() << ChannelClassSpec::textChat())));
 * registrar->registerClient(observer, "myobserver");
 *
 * \endcode
 *
 * \sa AbstractClient
 */

/**
 * \class AbstractClientObserver::ObserverInfo
 * \ingroup clientclient
 * \headerfile TelepathyQt4/abstract-client.h <TelepathyQt4/AbstractClientObserver>
 *
 * \brief The AbstractClientObserver::ObserverInfo class provides a wrapper
 * around the additional info about the channels passed to observeChannels().
 *
 * \sa AbstractClientObserver
 */

struct TELEPATHY_QT4_NO_EXPORT AbstractClientObserver::ObserverInfo::Private : public QSharedData
{
    Private(const QVariantMap &info)
        : info(info) {}

    QVariantMap info;
};

AbstractClientObserver::ObserverInfo::ObserverInfo(const QVariantMap &info)
    : mPriv(new Private(info))
{
}

AbstractClientObserver::ObserverInfo::ObserverInfo(const ObserverInfo &other)
    : mPriv(other.mPriv)
{
}

AbstractClientObserver::ObserverInfo::~ObserverInfo()
{
}

AbstractClientObserver::ObserverInfo &AbstractClientObserver::ObserverInfo::operator=(
        const ObserverInfo &other)
{
    if (this == &other) {
        return *this;
    }

    mPriv = other.mPriv;
    return *this;
}

QVariantMap AbstractClientObserver::ObserverInfo::allInfo() const
{
    return mPriv->info;
}

/**
 * Construct a new AbstractClientObserver object.
 *
 * \param channelFilter A specification of the channels in which this observer
 *                      is interested.
 * \param shouldRecover Whether upon the startup of this observer,
 *                      observeChannels() will be called for every already
 *                      existing channel matching its observerChannelFilter().
 */
AbstractClientObserver::AbstractClientObserver(
        const ChannelClassSpecList &channelFilter,
        bool shouldRecover)
    : mPriv(new Private(channelFilter.bareClasses(), shouldRecover))
      // The channel filter is converted here to the low-level class so that any warnings are
      // emitted immediately rather than only when the CD introspects this Client
{
}

/**
 * Class destructor.
 */
AbstractClientObserver::~AbstractClientObserver()
{
    delete mPriv;
}

/**
 * Return the property containing a specification of the channels that this
 * channel observer is interested. The observeChannels() method should be called
 * by the channel dispatcher whenever any of the newly created channels match
 * this description.
 *
 * See <a
 * href="http://telepathy.freedesktop.org/spec/org.freedesktop.Telepathy.Client.Observer.html#org.freedesktop.Telepathy.Client.Observer.ObserverChannelFilter">
 * the Telepathy specification</a> for documentation about the allowed
 * types and how to define filters.
 *
 * This property never changes while the observer process owns its client bus
 * name. If an observer wants to add extra channels to its list of interests at
 * runtime, it can register an additional client bus name using
 * ClientRegistrar::registerClient().
 * To remove those filters, it can release the bus name using
 * ClientRegistrar::unregisterClient().
 *
 * The same principle is applied to approvers and handlers.
 *
 * \return A specification of the channels that this channel observer is
 *         interested as a list of ChannelClassSpec objects.
 * \sa observeChannels()
 */
ChannelClassSpecList AbstractClientObserver::observerFilter() const
{
    return ChannelClassSpecList(mPriv->channelFilter);
}

/**
 * Return whether upon the startup of this observer, observeChannels()
 * will be called for every already existing channel matching its
 * observerChannelFilter().
 *
 * \param \c true if this observer observerChannels() will be called for every
 *        already existing channel matching its observerChannelFilter(),
 *        \c false otherwise.
 */
bool AbstractClientObserver::shouldRecover() const
{
    return mPriv->shouldRecover;
}

/**
 * \fn void AbstractClientObserver::observeChannels(
 *                  const MethodInvocationContextPtr<> &context,
 *                  const AccountPtr &account,
 *                  const ConnectionPtr &connection,
 *                  const QList<ChannelPtr> &channels,
 *                  const ChannelDispatchOperationPtr &dispatchOperation,
 *                  const QList<ChannelRequestPtr> &requestsSatisfied,
 *                  const ObserverInfo &observerInfo);
 *
 * Called by the channel dispatcher when channels in which the observer has
 * registered an interest are announced.
 *
 * If the announced channels contains channels that match the
 * observerChannelFilter(), and some that do not, then only a subset of the
 * channels (those that do match the filter) are passed to this method.
 *
 * If the channel dispatcher will split up the channels from a single
 * announcement and dispatch them separately (for instance because no
 * installed handler can handle all of them), it will call this method
 * several times.
 *
 * The observer must not call MethodInvocationContext::setFinished() until it
 * is ready for a handler for the channel to run (which may change the
 * channel's state). For instance the received \a context object should be
 * stored until this method is finished processing and then
 * MethodInvocationContext::setFinished() or
 * MethodInvocationContext::setFinishedWithError() should be called on the
 * received \a context object.
 *
 * Specialized observers must reimplement this method.
 *
 * \param context A MethodInvocationContextPtr object that must be used to
 *                indicate whether this method finished processing.
 * \param account The account with which the channels are associated.
 * \param connection The connection with which the channels are associated.
 * \param channels The channels to be observed.
 * \param dispatchOperation The dispatch operation for these channels.
 *                          The object will be invalid (DBusProxy::isValid()
 *                          will be false) if there is no dispatch
 *                          operation in place (because the channels were
 *                          requested, not incoming).
 *                          If the Observer calls
 *                          ChannelDispatchOperation::claim() or
 *                          ChannelDispatchOperation::handleWith() on this
 *                          object, it must be careful to avoid deadlock, since
 *                          these methods cannot return until the observer has
 *                          returned from observeChannels().
 * \param requestsSatisfied The requests satisfied by these channels.
 * \param observerInfo Additional information about these channels.
 */

struct TELEPATHY_QT4_NO_EXPORT AbstractClientApprover::Private
{
    Private(const ChannelClassList &channelFilter)
        : channelFilter(channelFilter)
    {
    }

    ChannelClassList channelFilter;
};

/**
 * \class AbstractClientApprover
 * \ingroup clientclient
 * \headerfile TelepathyQt4/abstract-client.h <TelepathyQt4/AbstractClientApprover>
 *
 * \brief The AbstractClientApprover class represents a Telepathy approver.
 *
 * Approvers are clients that notify the user that new channels have been
 * created, and allow the user to accept or reject those channels.
 *
 * Approvers can also select which channel handler will be used for the channel,
 * for instance by offering the user a list of possible handlers rather than
 * just an accept/reject choice. However, the channel dispatcher must be able to
 * prioritize possible handlers on its own using some reasonable heuristic,
 * probably based on user configuration.
 *
 * It is possible (and useful) to have an approver and a channel handler in the
 * same process; this is particularly useful if a channel handler wants to claim
 * responsibility for particular channels itself.
 *
 * All approvers are notified simultaneously. For instance, in a desktop system,
 * there might be one approver that displays a notification-area icon, one that
 * is part of a contact list window and highlights contacts there, and one that
 * is part of a full-screen media player.
 *
 * Any approver can approve the handling of a channel dispatch operation with a
 * particular channel handler by calling the
 * ChannelDispatchOperation::handleWith() method. Approvers can also attempt to
 * claim channels by calling ChannelDispatchOperation::claim(). If this
 * succeeds, the approver may handle the channels itself (if it is also a
 * handler), or close the channels in order to reject them.
 *
 * Approvers wishing to reject channels should call the
 * ChannelDispatchOperation::claim() method, then (if it succeeds) close the
 * channels in any way they see fit.
 *
 * The first approver to reply gets its decision acted on; any other approvers
 * that reply at approximately the same time will get an error, indicating that
 * the channel has already been dealt with.
 *
 * Approvers should usually prompt the user and ask for confirmation, rather
 * than dispatching the channel to a handler straight away.
 *
 * To become an approver one should inherit AbstractClientApprover and
 * implement the pure virtual addDispatchOperation() method. After that the
 * object representing the approver must be registered using
 * ClientRegistrar::registerClient().
 *
 * When new channels in which the approver has registered an interest are
 * ready to be dispatched, the method addDispatchOperation() is invoked.
 * The new channels are represented by a ChannelDispatchOperation object, which
 * is passed to the addDispatchOperation() method.
 * All approvers are notified simultaneously.
 *
 * \section approver_usage_sec Usage
 *
 * \subsection approver_create_sec Implementing an approver
 *
 * \code
 *
 * class MyApprover : public AbstractClientApprover
 * {
 * public:
 *     MyApprover(const ChannelClassSpecSpecList &channelFilter);
 *     ~MyApprover() { }
 *
 *     void addDispatchOperation(const MethodInvocationContextPtr<> &context,
 *             const ChannelDispatchOperationPtr &dispatchOperation);
 * };
 *
 * MyApprover::MyApprover(const ChannelClassSpecList &channelFilter)
 *     : AbstractClientApprover(channelFilter)
 * {
 * }
 *
 * void MyApprover::addDispatchOperation(
 *         const MethodInvocationContextPtr<> &context,
 *         const ChannelDispatchOperationPtr &dispatchOperation)
 * {
 *     // do something with dispatchOperation
 *
 *     context->setFinished();
 * }
 *
 * \endcode
 *
 * \subsection approver_register_sec Registering an approver
 *
 * \code
 *
 * ClientRegistrar registrar = ClientRegistrar::create();
 * AbstractClientPtr approver = AbstractClientPtr::dynamicCast(
 *         SharedPtr<MyApprover>(new MyApprover(
 *             ChannelClassSpecList() << ChannelClassSpec::textChat())));
 * registrar->registerClient(approver, "myapprover");
 *
 * \endcode
 *
 * \sa AbstractClient
 */

/**
 * Construct a new AbstractClientApprover object.
 *
 * \param channelFilter A specification of the channels in which this approver
 *                      is interested.
 */
AbstractClientApprover::AbstractClientApprover(
        const ChannelClassSpecList &channelFilter)
    : mPriv(new Private(channelFilter.bareClasses()))
{
}

/**
 * Class destructor.
 */
AbstractClientApprover::~AbstractClientApprover()
{
    delete mPriv;
}

/**
 * Return the property containing a specification of the channels that this
 * channel approver is interested. The addDispatchOperation() method should be
 * called by the channel dispatcher whenever at least one of the channels in
 * a channel dispatch operation matches this description.
 *
 * This method works in exactly the same way as the
 * AbstractClientObserver::observerChannelFilter() method. In particular, the
 * returned value cannot change while the handler process continues to own the
 * corresponding client bus name.
 *
 * In the .client file, represented in the same way as observer channel
 * filter, the group is #TP_QT4_IFACE_CLIENT_APPROVER followed by
 * ApproverChannelFilter instead.
 *
 * \return A specification of the channels that this channel approver is
 *         interested as a list of ChannelClassSpec objects.
 * \sa addDispatchOperation()
 */
ChannelClassSpecList AbstractClientApprover::approverFilter() const
{
    return ChannelClassSpecList(mPriv->channelFilter);
}

/**
 * \fn void AbstractClientApprover::addDispatchOperation(
 *                  const MethodInvocationContextPtr<> &context,
 *                  const ChannelDispatchOperationPtr &dispatchOperation);
 *
 * Called by the channel dispatcher when a dispatch operation in which the
 * approver has registered an interest is created, or when the approver starts
 * up while such channel dispatch operations already exist.
 *
 * The received \a context object should be stored until this
 * method is finished processing and then MethodInvocationContext::setFinished()
 * or MethodInvocationContext::setFinishedWithError() should be called on the
 * received \a context object.
 *
 * Specialized approvers must reimplement this method.
 *
 * \param context A MethodInvocationContextPtr object that must be used to
 *                indicate whether this method finished processing.
 * \param dispatchOperation The dispatch operation to be processed.
 */

struct TELEPATHY_QT4_NO_EXPORT AbstractClientHandler::Private
{
    Private(const ChannelClassList &channelFilter,
            const Capabilities &capabilities,
            bool wantsRequestNotification)
        : channelFilter(channelFilter),
          capabilities(capabilities),
          wantsRequestNotification(wantsRequestNotification),
          registered(false)
    {
    }

    ChannelClassList channelFilter;
    Capabilities capabilities;
    bool wantsRequestNotification;
    bool registered;
};

/**
 * \class AbstractClientHandler
 * \ingroup clientclient
 * \headerfile TelepathyQt4/abstract-client.h <TelepathyQt4/AbstractClientHandler>
 *
 * \brief The AbstractClientHandler class represents a Telepathy handler.
 *
 * Handlers are the user interface for a channel. They turn an abstract
 * channel into something the user wants to see, like a text message
 * stream or an audio and/or video call.
 *
 * For its entire lifetime, each channel on a connection known to the channel
 * dispatcher is either being processed by the channel dispatcher, or being
 * handled by precisely one handler.
 *
 * Because each channel is only handled by one handler, handlers may perform
 * actions that only make sense to do once, such as acknowledging text messages,
 * transferring the file, etc.
 *
 * When a new incoming channel is offered to approvers by the channel
 * dispatcher, it also offers the approvers a list of all the running or
 * activatable handlers whose filter indicates that they are able to handle
 * the channel. The approvers can choose one of those channel handlers to
 * handle the channel.
 *
 * When a new outgoing channel appears, the channel dispatcher passes it to
 * an appropriate channel handler automatically.
 *
 * To become an handler one should inherit AbstractClientHandler and
 * implement the pure virtual bypassApproval() and handleChannels() methods.
 * After that the object representing the handler must be registered using
 * ClientRegistrar::registerClient().
 *
 * When new channels in which the approver has registered an interest are
 * ready to be handled, the method handleChannels() is invoked.
 *
 * \section handler_usage_sec Usage
 *
 * \subsection handler_create_sec Implementing a handler
 *
 * \code
 *
 * class MyHandler : public AbstractClientHandler
 * {
 * public:
 *     MyHandler(const ChannelClassSpecList &channelFilter);
 *     ~MyHandler() { }
 *
 *     void bypassApproval() const;
 *
 *     void handleChannels(const MethodInvocationContextPtr<> &context,
 *             const AccountPtr &account,
 *             const ConnectionPtr &connection,
 *             const QList<ChannelPtr> &channels,
 *             const QList<ChannelRequestPtr> &requestsSatisfied,
 *             const QDateTime &userActionTime,
 *             const AbstractClientHandler::HandlerInfo &handlerInfo);
 * };
 *
 * MyHandler::MyHandler(const ChannelClassSpecList &channelFilter)
 *     : AbstractClientHandler(channelFilter)
 * {
 * }
 *
 * void MyHandler::bypassApproval() const
 * {
 *     return false;
 * }
 *
 * void MyHandler::handleChannels(const MethodInvocationContextPtr<> &context,
 *         const AccountPtr &account,
 *         const ConnectionPtr &connection,
 *         const QList<ChannelPtr> &channels,
 *         const QList<ChannelRequestPtr> &requestsSatisfied,
 *         const QDateTime &userActionTime,
 *         const AbstractClientHandler::HandlerInfo &handlerInfo)
 * {
 *     // do something
 *
 *     context->setFinished();
 * }
 *
 * \endcode
 *
 * \subsection handler_register_sec Registering a handler
 *
 * \code
 *
 * ClientRegistrar registrar = ClientRegistrar::create();
 * AbstractClientPtr handler = AbstractClientPtr::dynamicCast(
 *         SharedPtr<MyHandler>(new MyHandler(
 *             ChannelClassSpecList() << ChannelClassSpec::textChat())));
 * registrar->registerClient(handler, "myhandler");
 *
 * \endcode
 *
 * \sa AbstractClient
 */

/**
 * \class AbstractClientHandler::Capabilities
 * \ingroup clientclient
 * \headerfile TelepathyQt4/abstract-client.h <TelepathyQt4/AbstractClientHandler>
 *
 * \brief The AbstractClientHandler::Capabilities class provides a wrapper
 * around the capabilities of a handler.
 *
 * \sa AbstractClientHandler
 */

/**
 * \class AbstractClientHandler::HandlerInfo
 * \ingroup clientclient
 * \headerfile TelepathyQt4/abstract-client.h <TelepathyQt4/AbstractClientHandler>
 *
 * \brief The AbstractClientHandler::HandlerInfo class provides a wrapper
 * around the additional info about the channels passed to handleChannels().
 *
 * \sa AbstractClientHandler
 */

struct TELEPATHY_QT4_NO_EXPORT AbstractClientHandler::Capabilities::Private : public QSharedData
{
    Private(const QStringList &tokens)
        : tokens(QSet<QString>::fromList(tokens)) {}

    QSet<QString> tokens;
};

AbstractClientHandler::Capabilities::Capabilities(const QStringList &tokens)
    : mPriv(new Private(tokens))
{
}

AbstractClientHandler::Capabilities::Capabilities(const Capabilities &other)
    : mPriv(other.mPriv)
{
}

AbstractClientHandler::Capabilities::~Capabilities()
{
}

AbstractClientHandler::Capabilities &AbstractClientHandler::Capabilities::operator=(
        const Capabilities &other)
{
    if (this == &other) {
        return *this;
    }

    mPriv = other.mPriv;
    return *this;
}

bool AbstractClientHandler::Capabilities::hasToken(const QString &token) const
{
    return mPriv->tokens.contains(token);
}

void AbstractClientHandler::Capabilities::setToken(const QString &token)
{
    mPriv->tokens.insert(token);
}

void AbstractClientHandler::Capabilities::unsetToken(const QString &token)
{
    mPriv->tokens.remove(token);
}

QStringList AbstractClientHandler::Capabilities::allTokens() const
{
    return mPriv->tokens.toList();
}

struct TELEPATHY_QT4_NO_EXPORT AbstractClientHandler::HandlerInfo::Private : public QSharedData
{
    Private(const QVariantMap &info)
        : info(info) {}

    QVariantMap info;
};

AbstractClientHandler::HandlerInfo::HandlerInfo(const QVariantMap &info)
    : mPriv(new Private(info))
{
}

AbstractClientHandler::HandlerInfo::HandlerInfo(const HandlerInfo &other)
    : mPriv(other.mPriv)
{
}

AbstractClientHandler::HandlerInfo::~HandlerInfo()
{
}

AbstractClientHandler::HandlerInfo &AbstractClientHandler::HandlerInfo::operator=(
        const HandlerInfo &other)
{
    if (this == &other) {
        return *this;
    }

    mPriv = other.mPriv;
    return *this;
}

QVariantMap AbstractClientHandler::HandlerInfo::allInfo() const
{
    return mPriv->info;
}

/**
 * Construct a new AbstractClientHandler object.
 *
 * \param channelFilter A specification of the channels in which this observer
 *                      is interested.
 * \param wantsRequestNotification Whether this handler wants to receive channel
 *                                 requests notification via addRequest() and
 *                                 removeRequest().
 * \param capabilities The set of additional capabilities supported by this
 *                     handler.
 */
AbstractClientHandler::AbstractClientHandler(const ChannelClassSpecList &channelFilter,
        const Capabilities &capabilities,
        bool wantsRequestNotification)
    : mPriv(new Private(channelFilter.bareClasses(), capabilities, wantsRequestNotification))
{
}

/**
 * Class destructor.
 */
AbstractClientHandler::~AbstractClientHandler()
{
    delete mPriv;
}

/**
 * Return whether this handler is registered.
 *
 * \return \c true if registered, \c false otherwise.
 */
bool AbstractClientHandler::isRegistered() const
{
    return mPriv->registered;
}

/**
 * Return the property containing a specification of the channels that this
 * channel handler can deal with. It will be offered to approvers as a potential
 * channel handler for bundles that contain only suitable channels, or for
 * suitable channels that must be handled separately.
 *
 * This method works in exactly the same way as the
 * AbstractClientObserver::observerChannelFilter() method. In particular, the
 * returned value cannot change while the handler process continues to own the
 * corresponding client bus name.
 *
 * In the .client file, represented in the same way as observer channel
 * filter, the group is #TP_QT4_IFACE_CLIENT_HANDLER suffixed
 * by HandlerChannelFilter instead.
 *
 * \return A specification of the channels that this channel handler can deal
 *         with as a list of ChannelClassSpec objects.
 */
ChannelClassSpecList AbstractClientHandler::handlerFilter() const
{
    return ChannelClassSpecList(mPriv->channelFilter);
}

/**
 * Return the set of additional capabilities supported by this handler.
 *
 * \return The capabilities as an AbstractClientHandler::Capabilities object.
 */
AbstractClientHandler::Capabilities AbstractClientHandler::handlerCapabilities() const
{
    return mPriv->capabilities;
}

/**
 * \fn bool AbstractClientHandler::bypassApproval() const;
 *
 * Return whether channels destined for this handler are automatically
 * handled, without invoking approvers.
 *
 * \return \c true if automatically handled, \c false otherwise.
 */

/**
 * \fn void AbstractClientHandler::handleChannels(
 *                  const MethodInvocationContextPtr<> &context,
 *                  const AccountPtr &account,
 *                  const ConnectionPtr &connection,
 *                  const QList<ChannelPtr> &channels,
 *                  const QList<ChannelRequestPtr> &requestsSatisfied,
 *                  const QDateTime &userActionTime,
 *                  const HandlerInfo &handlerInfo);
 *
 * Called by the channel dispatcher when this handler should handle these
 * channels, or when this handler should present channels that it is already
 * handling to the user (e.g. bring them into the foreground).
 *
 * Clients are expected to know what channels they're already handling, and
 * which channel object corresponds to which window or tab.
 *
 * After handleChannels() replies successfully by calling
 * MethodInvocationContext::setFinished(), the client process is considered
 * to be responsible for the channel until it its unique name disappears from
 * the bus.
 *
 * If a process has multiple client bus names - some temporary and some
 * long-lived - and drops one of the temporary bus names in order to reduce the
 * set of channels that it will handle, any channels that it is already handling
 * will remain unaffected.
 *
 * The received \a context object should be stored until this
 * method is finished processing and then MethodInvocationContext::setFinished()
 * or MethodInvocationContext::setFinishedWithError() should be called on the
 * received \a context object.
 *
 * Specialized handlers must reimplement this method.
 *
 * \param context A MethodInvocationContextPtr object that must be used to
 *                indicate whether this method finished processing.
 * \param account The account with which the channels are associated.
 * \param connection The connection with which the channels are associated.
 * \param channels The channels to be handled.
 * \param dispatchOperation The dispatch operation for these channels.
 *                          The object will be invalid (DBusProxy::isValid()
 *                          will be false) if there is no dispatch
 *                          operation in place (because the channels were
 *                          requested, not incoming).
 * \param requestsSatisfied The requests satisfied by these channels.
 * \param userActionTime The time at which user action occurred, or 0 if this
 *                       channel is to be handled for some reason not involving
 *                       user action. Handlers should use this for
 *                       focus-stealing prevention, if applicable.
 * \param handlerInfo Additional information about these channels.
 */

/**
 * Return whether this handler wants to receive notification of channel requests
 * via addRequest() and removeRequest().
 *
 * This property is set by the constructor and cannot be changed after that.
 *
 * \return \c true if receiving channel requests notification is desired,
 *         \c false otherwise.
 */
bool AbstractClientHandler::wantsRequestNotification() const
{
    return mPriv->wantsRequestNotification;
}

/**
 * Called by the channel dispatcher to indicate that channels have been
 * requested, and that if the request is successful, they will probably be
 * handled by this handler.
 *
 * This allows the UI to start preparing to handle the channels in advance
 * (e.g. render a window with an "in progress" message), improving perceived
 * responsiveness.
 *
 * If the request succeeds and is given to the expected handler, the
 * requestsSatisfied parameter to handleChannels() can be used to match the
 * channel to a previous addRequest() call.
 *
 * This lets the UI direct the channels to the window that it already opened.
 *
 * If the request fails, the expected handler is notified by the channel
 * dispatcher calling its removeRequest() method.
 *
 * This lets the UI close the window or display the error.
 *
 * The channel dispatcher will attempt to ensure that handleChannels() is called
 * on the same handler that received addRequest(). If that isn't possible,
 * removeRequest() will be called on the handler that previously received
 * addRequest(), with the special error #TP_QT4_ERROR_NOT_YOURS, which
 * indicates that some other handler received the channel instead.
 *
 * Expected handling is for the UI to close the window it previously opened.
 *
 * Specialized handlers that want to be notified of newly requested channel
 * should reimplement this method.
 *
 * \param channelRequest The newly created channel request.
 * \sa removeRequest()
 */
void AbstractClientHandler::addRequest(
        const ChannelRequestPtr &channelRequest)
{
    // do nothing, subclasses that want to listen requests should reimplement
    // this method
}

/**
 * Called by the ChannelDispatcher to indicate that a request previously passed
 * to addRequest() has failed and should be disregarded.
 *
 * Specialized handlers that want to be notified of removed channel requests
 * should reimplement this method.
 *
 * \param channelRequest The channel request that failed.
 * \param errorName The name of the D-Bus error with which the request failed.
 *                  If this is #TP_QT4_ERROR_NOT_YOURS, this indicates that
 *                  the request succeeded, but all the resulting channels were
 *                  given to some other handler.
 * \param errorMessage Any message supplied with the D-Bus error.
 */
void AbstractClientHandler::removeRequest(
        const ChannelRequestPtr &channelRequest,
        const QString &errorName, const QString &errorMessage)
{
    // do nothing, subclasses that want to listen requests should reimplement
    // this method
}

void AbstractClientHandler::setRegistered(bool registered)
{
    mPriv->registered = registered;
}

} // Tp