This file is indexed.

/usr/include/mediascanner-1.0/mediascanner/dbusutils.h is in libmediascanner-dev 0.3.93+14.04.20131024.1-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
/*
 * This file is part of the Ubuntu TV Media Scanner
 * Copyright (C) 2012-2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contact: Jim Hodapp <jim.hodapp@canonical.com>
 * Authored by: Mathias Hasselmann <mathias@openismus.com>
 */
#ifndef MEDIASCANNER_DBUSUTILS_H
#define MEDIASCANNER_DBUSUTILS_H

// Boost C++
#include <boost/tuple/tuple.hpp>

// C++ Standard Library
#include <map>
#include <string>
#include <memory>

// Media Scanner Library
#include "mediascanner/dbustypes.h"
#include "mediascanner/glibutils.h"
#include "mediascanner/utilities.h"

namespace mediascanner {
namespace dbus {

////////////////////////////////////////////////////////////////////////////////

using boost::tuples::element;

template<typename InfoType> class Member;

typedef Member<GDBusMethodInfo> MethodInfo;
typedef std::shared_ptr<MethodInfo> MethodInfoPtr;

typedef Member<GDBusPropertyInfo> PropertyInfo;
typedef std::shared_ptr<PropertyInfo> PropertyInfoPtr;

typedef Member<GDBusSignalInfo> SignalInfo;
typedef std::shared_ptr<SignalInfo> SignalInfoPtr;

typedef std::shared_ptr<class InterfaceProxy> InterfaceProxyPtr;
typedef std::shared_ptr<class InterfaceSkeleton> InterfaceSkeletonPtr;
typedef std::shared_ptr<class MethodInvocation> MethodInvocationPtr;

////////////////////////////////////////////////////////////////////////////////

template<typename InfoType>
struct SkeletonTrait;

template<typename InfoType>
class Member {
public:
    /**
     * @brief Type of the GDBus introspection structure for this members.
     */
    typedef InfoType dbus_info_type;

    typedef typename SkeletonTrait<dbus_info_type>::type skeleton_type;

protected:
    Member(const std::string &name, const skeleton_type *skeleton)
        : name_(name)
        , skeleton_(skeleton) {
    }

public:
    virtual ~Member() {
    }

    const std::string& name() const {
        return name_;
    }

    const skeleton_type* skeleton() const {
        return skeleton_;
    }

    virtual Wrapper<dbus_info_type> info() const = 0;

private:
    std::string name_;
    const skeleton_type *const skeleton_;
};

////////////////////////////////////////////////////////////////////////////////

/**
 * @brief Description of an D-Bus interface's method or signal argument.
 */
template<typename T>
class Argument {
public:
    typedef T value_type;
    typedef Type<T> dbus_type;

    explicit Argument(const std::string &name)
        : name_(name) {
    }

    const std::string& name() const {
        return name_;
    }

    const Signature& signature() const {
        return dbus_type::signature();
    }

    static GVariant* make_variant(const value_type &value) {
        return Type<value_type>::make_variant(value);
    }

    Wrapper<GDBusArgInfo> info() const {
        if (info_ == nullptr) {
            const GDBusArgInfo info = {
                1, g_strdup(name().c_str()),
                g_variant_type_dup_string(signature()),
                nullptr
            };

            info_ = wrap_static(&info);
        }

        return info_;
    }

private:
    std::string name_;
    mutable Wrapper<GDBusArgInfo> info_;
};

////////////////////////////////////////////////////////////////////////////////

namespace internal {

template<typename T, size_t N>
class ArgumentTail {
    typedef ArgumentTail<T, N - 1> predecessor;

public:
    static void ref(const T *args, GDBusArgInfo **info) {
        info[N - 1] = boost::tuples::get<N - 1>(*args).info().dup();
        predecessor::ref(args, info);
    }

    static void ref(GDBusArgInfo *const *other, GDBusArgInfo **info) {
        info[N - 1] = g_dbus_arg_info_ref(other[N - 1]);
        predecessor::ref(other, info);
    }

    static void unref(GDBusArgInfo **info) {
        g_dbus_arg_info_unref(info[N - 1]);
        predecessor::unref(info);
    }
};

template<typename T>
struct ArgumentTail<T, 0> {
    static void ref(const T *, GDBusArgInfo **) {
    }

    static void ref(GDBusArgInfo *const *, GDBusArgInfo **) {
    }

    static void unref(GDBusArgInfo **/*info*/) {
    }
};

template<typename T>
struct ArgumentTrait {
    typedef Argument<T> type;
};

template<>
struct ArgumentTrait<boost::tuples::null_type> {
    typedef boost::tuples::null_type type;
};

} // namespace internal

////////////////////////////////////////////////////////////////////////////////

template<typename T0 = boost::tuples::null_type,
         typename T1 = boost::tuples::null_type,
         typename T2 = boost::tuples::null_type,
         typename T3 = boost::tuples::null_type,
         typename T4 = boost::tuples::null_type,
         typename T5 = boost::tuples::null_type,
         typename T6 = boost::tuples::null_type,
         typename T7 = boost::tuples::null_type>
class ArgumentList : public boost::tuples::tuple
        <typename internal::ArgumentTrait<T0>::type,
         typename internal::ArgumentTrait<T1>::type,
         typename internal::ArgumentTrait<T2>::type,
         typename internal::ArgumentTrait<T3>::type,
         typename internal::ArgumentTrait<T4>::type,
         typename internal::ArgumentTrait<T5>::type,
         typename internal::ArgumentTrait<T6>::type,
         typename internal::ArgumentTrait<T7>::type> {
public:
    typedef boost::tuples::tuple
            <typename internal::ArgumentTrait<T0>::type,
             typename internal::ArgumentTrait<T1>::type,
             typename internal::ArgumentTrait<T2>::type,
             typename internal::ArgumentTrait<T3>::type,
             typename internal::ArgumentTrait<T4>::type,
             typename internal::ArgumentTrait<T5>::type,
             typename internal::ArgumentTrait<T6>::type,
             typename internal::ArgumentTrait<T7>::type> inherited;

    typedef boost::tuples::tuple<T0, T1, T2, T3, T4, T5, T6, T7> value_type;
    typedef boost::tuples::length<inherited> length;

    typedef T0 value0_type;
    typedef T1 value1_type;
    typedef T2 value2_type;
    typedef T3 value3_type;
    typedef T4 value4_type;
    typedef T5 value5_type;
    typedef T6 value6_type;
    typedef T7 value7_type;

private:
    typedef internal::ArgumentTail<ArgumentList, length::value> tail;
    typedef boost::tuples::null_type null_type;

public:
    ArgumentList()
        : inherited()
        , info_(nullptr) {
    }

    explicit ArgumentList
            (const typename internal::ArgumentTrait<T0>::type &a0,
             const typename internal::ArgumentTrait<T1>::type &a1 = null_type(),
             const typename internal::ArgumentTrait<T2>::type &a2 = null_type(),
             const typename internal::ArgumentTrait<T3>::type &a3 = null_type(),
             const typename internal::ArgumentTrait<T4>::type &a4 = null_type(),
             const typename internal::ArgumentTrait<T5>::type &a5 = null_type(),
             const typename internal::ArgumentTrait<T6>::type &a6 = null_type(),
             const typename internal::ArgumentTrait<T7>::type &a7 = null_type())
        : inherited(a0, a1, a2, a3, a4, a5, a6, a7)
        , info_(nullptr) {
    }

    ~ArgumentList() {
        if (info_) {
            tail::unref(info_);
            g_free(info_);
        }
    }

    GDBusArgInfo** info() const {
        if (info_ == nullptr) {
            GDBusArgInfo **info = g_new(GDBusArgInfo *, length::value + 1);
            info[length::value] = nullptr;
            tail::ref(this, info);
            info_ = info;
        }

        return info_;
    }

    static GDBusArgInfo** dup(GDBusArgInfo *const *const other) {
        GDBusArgInfo **info = g_new(GDBusArgInfo *, length::value + 1);
        info[length::value] = nullptr;
        tail::ref(other, info);
        return info;
    }

    static value_type make_value(GVariant *variant) {
        return Type<value_type>::make_value(variant);
    }

    static GVariant* make_variant(const value_type &value) {
        return Type<value_type>::make_variant(value);
    }

    static const Signature& signature() {
        return Type<value_type>::signature();
    }

private:
    mutable GDBusArgInfo **info_;
};

////////////////////////////////////////////////////////////////////////////////

class MethodSkeleton {
public:
    virtual ~MethodSkeleton() {
    }

    virtual void Invoke(MethodInvocation call) const = 0;
};

template<> struct SkeletonTrait<GDBusMethodInfo> {
    typedef MethodSkeleton type;
};

template< typename InputArguments = ArgumentList<>,
          typename OutputArguments = ArgumentList<> >
class Method : public Member<GDBusMethodInfo> {
public:
    typedef InputArguments input_args_type;
    typedef OutputArguments output_args_type;

protected:
    Method(const std::string &name, MethodSkeleton *skeleton,
           const input_args_type &input = input_args_type(),
           const output_args_type &output = output_args_type())
        : Member(name, skeleton)
        , input_(input)
        , output_(output) {
    }

public:
    Wrapper<dbus_info_type> info() const {
        if (not info_) {
            const GDBusMethodInfo static_info = {
                1, g_strdup(name().c_str()),
                input_args_type::dup(input_.info()),
                output_args_type::dup(output_.info()),
                nullptr
            };

            info_ = wrap_static(&static_info);
        }

        return info_;
    }

private:
    const input_args_type input_;
    const output_args_type output_;
    mutable Wrapper<dbus_info_type> info_;
};

class MethodInvocation {
public:
    explicit MethodInvocation(GDBusMethodInvocation *invocation)
        : invocation_(invocation)
        , origin_(g_thread_self()) {
    }

    bool is_active() const {
        return invocation_;
    }

    uint32_t serial() const {
        return g_dbus_message_get_serial(message());
    }

    std::string sender() const {
        BOOST_ASSERT(is_active());
        return g_dbus_method_invocation_get_sender(invocation_);
    }

    std::string target() const {
        BOOST_ASSERT(is_active());
        return g_dbus_method_invocation_get_object_path(invocation_);
    }

    std::string interface_name() const {
        BOOST_ASSERT(is_active());
        return g_dbus_method_invocation_get_interface_name(invocation_);
    }

    std::string method_name() const {
        BOOST_ASSERT(is_active());
        return g_dbus_method_invocation_get_method_name(invocation_);
    }

    GVariant* parameters() const {
        BOOST_ASSERT(is_active());
        return g_dbus_method_invocation_get_parameters(invocation_);
    }

    void return_error(GQuark domain, int code, const std::string &message);
    void return_dbus_error(const std::string &error_name,
                           const std::string &error_message);
    void return_error(Wrapper<GError> error);

    void emit_signal(const SignalInfo *signal, GVariant *args,
                     GError **error) const;

    GDBusMethodInvocation* dup() const;
    GDBusMethodInvocation* swap(GDBusMethodInvocation *other = nullptr); // NOLINT:build/include_what_you_use

protected:
    GDBusConnection* connection() const {
        BOOST_ASSERT(is_active());
        return g_dbus_method_invocation_get_connection(invocation_);
    }

    GDBusMessage* message() const {
        BOOST_ASSERT(is_active());
        return g_dbus_method_invocation_get_message(invocation_);
    }

    void assert_called_from_original_thread() const {
        BOOST_ASSERT(g_thread_self() == origin_);
    }

private:
    GDBusMethodInvocation *invocation_;
    GThread *const origin_;
};

template<typename A0, typename A1, typename A2, typename A3,
         typename A4, typename A5, typename A6, typename A7,
         typename R0, typename R1, typename R2, typename R3,
         typename R4, typename R5, typename R6, typename R7,
         typename MethodProxy>
class MethodProxyCall {
};

using boost::tuples::null_type;

template<typename A0, typename MethodProxy>
class MethodProxyCall
        <A0, null_type, null_type, null_type,
         null_type, null_type, null_type, null_type,
         null_type, null_type, null_type, null_type,
         null_type, null_type, null_type, null_type, MethodProxy> {
public:
    void operator()(const A0 &a0, GError **error) const {
        const typename MethodProxy::input_value_type args(a0);
        static_cast<const MethodProxy *>(this)->CallAndWait(args, error);
    }
};

template<typename A0, typename A1, typename MethodProxy>
class MethodProxyCall
        <A0, A1, null_type, null_type,
         null_type, null_type, null_type, null_type,
         null_type, null_type, null_type, null_type,
         null_type, null_type, null_type, null_type, MethodProxy> {
public:
    void operator()(const A0 &a0, const A1 &a1, GError **error) const {
        const typename MethodProxy::input_value_type args(a0, a1);
        static_cast<const MethodProxy *>(this)->CallAndWait(args, error);
    }
};

template<typename A0, typename R0, typename MethodProxy>
class MethodProxyCall
        <A0, null_type, null_type, null_type,
         null_type, null_type, null_type, null_type,
         R0, null_type, null_type, null_type,
         null_type, null_type, null_type, null_type, MethodProxy> {
public:
    R0 operator()(const A0 &a0, GError **error) const {
        const typename MethodProxy::input_value_type args(a0);

        return boost::tuples::get<0>
                (static_cast<const MethodProxy *>(this)->
                 CallAndWait(args, error));
    }
};

template<typename MethodProxy,
         typename InputArguments,
         typename OutputArguments>
struct MethodProxyCallTrait {
    typedef Method<InputArguments, OutputArguments> method_type;
    typedef typename method_type::input_args_type input_args_type;
    typedef typename method_type::output_args_type output_args_type;

    typedef MethodProxyCall
            <typename input_args_type::value0_type,
             typename input_args_type::value1_type,
             typename input_args_type::value2_type,
             typename input_args_type::value3_type,
             typename input_args_type::value4_type,
             typename input_args_type::value5_type,
             typename input_args_type::value6_type,
             typename input_args_type::value7_type,
             typename output_args_type::value0_type,
             typename output_args_type::value1_type,
             typename output_args_type::value2_type,
             typename output_args_type::value3_type,
             typename output_args_type::value4_type,
             typename output_args_type::value5_type,
             typename output_args_type::value6_type,
             typename output_args_type::value7_type,
             MethodProxy> type;
};

template< typename InputArguments = ArgumentList<>,
          typename OutputArguments = ArgumentList<> >
class MethodProxy
        : public Method<InputArguments, OutputArguments>
        , public MethodProxyCallTrait<MethodProxy<InputArguments,
                                                  OutputArguments>,
                                      InputArguments, OutputArguments>::type {
public:
    typedef Method<InputArguments, OutputArguments> method_type;
    typedef typename method_type::input_args_type input_args_type;
    typedef typename input_args_type::value_type input_value_type;
    typedef typename method_type::output_args_type output_args_type;
    typedef typename output_args_type::value_type output_value_type;
    typedef boost::posix_time::time_duration timeout_type;

    MethodProxy(InterfaceProxy *proxy, const std::string &name,
                const input_args_type &input = input_args_type(),
                const output_args_type &output = output_args_type())
        : method_type(name, nullptr, input, output)
        , proxy_(proxy)
        , flags_(G_DBUS_CALL_FLAGS_NONE)
        , timeout_(boost::date_time::not_a_date_time) {
    }

    output_value_type CallAndWait(const input_value_type &args,
                                  GError **error) const;

    template<typename T>
    MethodProxy& operator[](const T &value) {
        set_attribute(value);
        return *this;
    }

    template<typename T>
    MethodProxy operator[](const T &value) const {
        MethodProxy dup(*this);
        dup.set_attribute(value);
        return dup;
    }

private:
    void set_attribute(Wrapper<GCancellable> cancellable) {
        cancellable_ = cancellable;
    }

    void set_attribute(GDBusCallFlags flags) {
        flags_ = flags;
    }

    void set_attribute(timeout_type timeout) {
        timeout_ = timeout;
    }

    int timeout_ms() const {
        if (timeout_.is_not_a_date_time())
            return -1;

        return timeout_.total_milliseconds();
    }

    InterfaceProxy *const proxy_;
    Wrapper<GCancellable> cancellable_;
    GDBusCallFlags flags_;
    timeout_type timeout_;
};

template< typename InputArguments = ArgumentList<>,
          typename OutputArguments = ArgumentList<> >
class MethodImplementation
        : public Method<InputArguments, OutputArguments>
        , protected MethodSkeleton {
public:
    typedef Method<InputArguments, OutputArguments> method_type;
    typedef typename method_type::input_args_type input_args_type;
    typedef typename input_args_type::value_type input_value_type;
    typedef typename method_type::output_args_type output_args_type;
    typedef typename output_args_type::value_type output_value_type;

    class Invocation : public MethodInvocation {
    public:
        explicit Invocation(GDBusMethodInvocation *other)
            : MethodInvocation(other) {
        }

        explicit Invocation(MethodInvocation *other)
            : MethodInvocation(other->swap()) {
        }

        template<size_t N>
        typename element<N, input_value_type>::type arg() const {
            const Wrapper<GVariant> child_variant =
                    take(g_variant_get_child_value(parameters(), N));

            typedef typename element<N, input_value_type>::type return_type;
            return Type<return_type>::make_value(child_variant.get());
        }

        template<size_t N, typename ErrorDetailType>
        typename element<N, input_value_type>::type arg
                                            (ErrorDetailType *error) const {
            const Wrapper<GVariant> child_variant =
                    take(g_variant_get_child_value(parameters(), N));

            typedef typename element<N, input_value_type>::type return_type;
            return Type<return_type>::make_value(child_variant.get(), error);
        }

        void return_value(const output_value_type &value) {
            GVariant *const variant = output_args_type::make_variant(value);
            g_dbus_method_invocation_return_value(swap(), variant);
        }
    };

    typedef std::shared_ptr<Invocation> InvocationPtr;

protected:
    explicit MethodImplementation
        (InterfaceSkeleton *, const std::string &name,
         const input_args_type &input = input_args_type(),
         const output_args_type &output = output_args_type())
        : method_type(name, this, input, output) {
    }

    void Invoke(MethodInvocation invocation) const {
        Invoke(InvocationPtr(new Invocation(&invocation)));
    }

    virtual void Invoke(InvocationPtr invocation) const = 0;
};

////////////////////////////////////////////////////////////////////////////////

class PropertySkeleton {
public:
    virtual ~PropertySkeleton() {
    }

    virtual bool readable() const = 0;
    virtual bool writable() const = 0;

    /**
     * @brief Retrieves the value of this D-Bus property.
     * @param[in] sender The sender of this D-Bus request.
     * @param[in] target The target of this D-Bus request.
     * @param[out] error Location for storing a possible error.
     * @return The current value of this property.
     */
    virtual GVariant* GetValue(const std::string &sender,
                               const std::string &target,
                               GError **error) const = 0;

    /**
     * @brief Updates the value of this D-Bus property.
     * @param[in] sender The sender of this D-Bus request.
     * @param[in] target The target of this D-Bus request.
     * @param[in] value The new value for this property.
     * @param[out] error Location for storing a possible error.
     * @return @c true on success
     */
    virtual bool SetValue(const std::string &sender, const std::string &target,
                          GVariant *value, GError **error) const = 0;
};

template<> struct SkeletonTrait<GDBusPropertyInfo> {
    typedef PropertySkeleton type;
};

template<typename T, GDBusPropertyInfoFlags Flags>
class Property : public Member<GDBusPropertyInfo> {
public:
    typedef T value_type;
    typedef Type<T> dbus_type;

protected:
    Property(const std::string &name, PropertySkeleton *skeleton)
        : Member(name, skeleton) {
    }

public:
    /**
     * @brief Predicate if this property can be read.
     * @see GetValue(), @flags()
     */
    static bool readable() {
        return Flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE;
    }

    /**
     * @brief Predicate if this property can be written.
     * @see SetValue(), @flags()
     */
    static bool writable() {
        return Flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
    }

    Wrapper<dbus_info_type> info() const {
        if (not info_) {
            const GDBusPropertyInfo static_info = {
                1, g_strdup(name().c_str()),
                g_variant_type_dup_string(dbus_type::signature()),
                Flags, nullptr
            };

            info_ = wrap_static(&static_info);
        }

        return info_;
    }

private:
    mutable Wrapper<dbus_info_type> info_;
};

template<typename T, GDBusPropertyInfoFlags Flags>
class PropertyProxy
        : public Property<T, Flags> {
public:
    typedef Property<T, Flags> property_type;

    explicit PropertyProxy(const std::string &name)
        : property_type(name, nullptr) {
    }
};

template<typename T>
class ReadOnlyPropertyProxy
        : public PropertyProxy<T, G_DBUS_PROPERTY_INFO_FLAGS_READABLE> {
public:
    typedef
        PropertyProxy<T, G_DBUS_PROPERTY_INFO_FLAGS_READABLE>
        inherited;

    explicit ReadOnlyPropertyProxy(const std::string &name)
        : inherited(name) {
    }

    T ReadCachedValue(const InterfaceProxy *proxy) const;
    bool ReadValue(const InterfaceProxy *proxy, T *value) const;
};

template<typename T, GDBusPropertyInfoFlags Flags>
class PropertyImplementation
        : public Property<T, Flags>
        , protected PropertySkeleton {
public:
    typedef Property<T, Flags> property_type;

    explicit PropertyImplementation(const std::string &name)
        : property_type(name, this) {
    }

    bool readable() const {
        return property_type::readable();
    }

    bool writable() const {
        return property_type::writable();
    }
};

template<typename T>
class ReadOnlyPropertyImplementation
        : public PropertyImplementation
                <T, G_DBUS_PROPERTY_INFO_FLAGS_READABLE> {
public:
    typedef
        PropertyImplementation<T, G_DBUS_PROPERTY_INFO_FLAGS_READABLE>
        inherited;

    explicit ReadOnlyPropertyImplementation(const std::string &name)
        : inherited(name) {
    }

    bool SetValue(const std::string &, const std::string &,
                  GVariant *, GError **error) const {
        g_set_error(error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
                    "Property \"%s\" is for reading only",
                    inherited::name().c_str());
        return false;
    }
};

////////////////////////////////////////////////////////////////////////////////

class SignalSkeleton {
};

template<> struct SkeletonTrait<GDBusSignalInfo> {
    typedef SignalSkeleton type;
};

template< typename Arguments = ArgumentList<> >
class Signal : public Member<GDBusSignalInfo> {
public:
    typedef Arguments args_type;

protected:
    Signal(const std::string &name, SignalSkeleton *skeleton,
           const args_type &args = args_type())
        : Member(name, skeleton)
        , args_(args) {
    }

protected:
    Wrapper<dbus_info_type> info() const {
        if (not info_) {
            const GDBusSignalInfo static_info = {
                1, g_strdup(name().c_str()),
                args_type::dup(args_.info()),
                nullptr
            };

            info_ = wrap_static(&static_info);
        }

        return info_;
    }

private:
    const args_type args_;
    mutable Wrapper<dbus_info_type> info_;
};

template< typename Arguments = ArgumentList<> >
class SignalProxy
        : public Signal<Arguments> {
public:
    typedef Signal<Arguments> signal_type;
    typedef typename signal_type::args_type args_type;
    typedef typename args_type::value_type value_type;

    explicit SignalProxy(const std::string &name,
                         const args_type &args = args_type())
        : Signal<Arguments>(name, nullptr, args) {
    }
};

template< typename Arguments = ArgumentList<> >
class SignalImplementation
        : public Signal<Arguments>
        , protected SignalSkeleton {
public:
    typedef Signal<Arguments> signal_type;
    typedef typename signal_type::args_type args_type;
    typedef typename args_type::value_type value_type;

    explicit SignalImplementation(const std::string &name,
                                  const args_type &args = args_type())
        : Signal<Arguments>(name, this, args) {
    }

    void emit_result(const value_type &args,
                     MethodInvocationPtr invocation) const {
        // FIXME(M5): Handle errors on signal emission
        invocation->emit_signal(this, args_type::make_variant(args), nullptr);
    }

    void emit_result_on_idle(const value_type &args,
                             MethodInvocationPtr invocation) const {
        Idle::AddOnce(std::bind(&SignalImplementation::emit_result,
                                  this, args, invocation));
    }

    void emit_signal(const value_type &args,
                     const std::string &object_path,
                     const std::string &interface_name,
                     Wrapper<GDBusConnection> connection) const {
        // FIXME(M5): Handle errors on signal emission
        // FIXME(M5): Find better way to pass object_path and interface_name.
        Wrapper<GError> error;

        if (not g_dbus_connection_emit_signal(connection.get(), nullptr,
                                              object_path.c_str(),
                                              interface_name.c_str(),
                                              Signal<Arguments>::name().c_str(),
                                              args_type::make_variant(args),
                                              error.out_param()))
            std::cerr << to_string(error)<< std::endl;
    }

    void emit_signal_on_idle(const value_type &args,
                             const std::string &object_path,
                             const std::string &interface_name,
                             Wrapper<GDBusConnection> connection) const {
        Idle::AddOnce(std::bind(&SignalImplementation::emit_signal,
                                  this, args, object_path,
                                  interface_name, connection));
    }
};

////////////////////////////////////////////////////////////////////////////////

class InterfaceInfo {
protected:
    explicit InterfaceInfo(const std::string &name)
        : name_(name) {
    }

public:
    const std::string& name() const {
        return name_;
    }

    Wrapper<GDBusInterfaceInfo> info() const;

protected:
    template<typename T, typename A0>
    std::shared_ptr<T> register_method(const A0 &a0) {
        const std::shared_ptr<T> method(new T(a0));
        add_method(method);
        return method;
    }

    template<typename T, typename A0>
    std::shared_ptr<T> register_property(const A0 &a0) {
        const std::shared_ptr<T> property(new T(a0));
        add_property(property);
        return property;
    }

    template<typename T>
    std::shared_ptr<T> register_signal() {
        const std::shared_ptr<T> signal(new T);
        add_signal(signal);
        return signal;
    }

    void add_method(MethodInfoPtr method);
    void add_property(PropertyInfoPtr property);
    void add_signal(SignalInfoPtr signal);

    MethodInfoPtr find_method(const std::string &name) const;
    PropertyInfoPtr find_property(const std::string &name) const;
    SignalInfoPtr find_signal(const std::string &name) const;

private:
    typedef std::map<std::string, MethodInfoPtr> MethodInfoMap;
    typedef std::map<std::string, PropertyInfoPtr> PropertyInfoMap;
    typedef std::map<std::string, SignalInfoPtr> SignalInfoMap;

    std::string name_;
    MethodInfoMap methods_;
    PropertyInfoMap properties_;
    SignalInfoMap signals_;

    mutable Wrapper<GDBusInterfaceInfo> info_;
};

////////////////////////////////////////////////////////////////////////////////

class InterfaceProxy : public InterfaceInfo {
public:
    explicit InterfaceProxy(const std::string &name)
        : InterfaceInfo(name) {
    }
    virtual ~InterfaceProxy() {}

    bool ConnectAndWait(Wrapper<GDBusConnection> connection,
                        const std::string &service_name,
                        const std::string &object_path,
                        Wrapper<GCancellable> cancellable,
                        GError **error);

    bool ConnectAndWait(Wrapper<GDBusConnection> connection,
                        const std::string &service_name,
                        const std::string &object_path,
                        GError **error) {
        return ConnectAndWait(connection, service_name, object_path,
                              Wrapper<GCancellable>(), error);
    }

    void connect(GBusType bus_type,
                 const std::string &service_name,
                 const std::string &object_path,
                 Wrapper<GCancellable> cancellable = Wrapper<GCancellable>());

    void connect(Wrapper<GDBusConnection> connection,
                 const std::string &service_name,
                 const std::string &object_path,
                 Wrapper<GCancellable> cancellable = Wrapper<GCancellable>());

    Wrapper<GVariant> ReadPropertyValue(const std::string &property_name) const;

    Wrapper<GDBusProxy> handle() const {
        return handle_;
    }

    Wrapper<GDBusConnection> connection() const;
    std::string interface_name() const;
    std::string service_name() const;
    std::string object_path() const;

protected:
    virtual void connected();
    virtual void connect_failed(Wrapper<GError> error) const;

private:
    static void on_connect_for_bus(GObject *object,
                                   GAsyncResult *result, void *data);
    static void on_connect(GObject *object, GAsyncResult *result, void *data);

    Wrapper<GDBusProxy> handle_;
};

////////////////////////////////////////////////////////////////////////////////

class InterfaceSkeleton : public InterfaceInfo {
public:
    explicit InterfaceSkeleton(const std::string &name)
        : InterfaceInfo(name) {
    }

    const MethodSkeleton *find_method_skeleton(const std::string &name) const;
    const PropertySkeleton* find_property_skeleton
                                            (const std::string &name) const;

    void InvokeMethod(MethodInvocation invocation) const;

    GVariant* GetProperty(const std::string &sender,
                          const std::string &target,
                          const std::string &name,
                          GError **error) const;

    bool SetProperty(const std::string &sender,
                     const std::string &target,
                     const std::string &name,
                     GVariant *value,
                     GError **error) const;
};

////////////////////////////////////////////////////////////////////////////////

template< typename InterfaceType,
          typename InputArguments = ArgumentList<>,
          typename OutputArguments = ArgumentList<> >
struct MethodTrait;

template<typename InputArguments,
         typename OutputArguments>
struct MethodTrait<InterfaceProxy, InputArguments, OutputArguments> {
    typedef MethodProxy<InputArguments, OutputArguments> type;
};

template<typename InputArguments,
         typename OutputArguments>
struct MethodTrait<InterfaceSkeleton, InputArguments, OutputArguments> {
    typedef MethodImplementation<InputArguments, OutputArguments> type;
};

// TODO(M5): Maybe also use for PropertyInfo class?
enum PropertyAccess {
    ReadOnly = 1, WriteOnly = 2, ReadWrite = 3
};

template<typename InterfaceType,
         PropertyAccess AccessMode,
         typename ValueType>
struct PropertyTrait;

template<typename ValueType>
struct PropertyTrait<InterfaceProxy, ReadOnly, ValueType> {
    typedef ReadOnlyPropertyProxy<ValueType> type;
};

template<typename ValueType>
struct PropertyTrait<InterfaceSkeleton, ReadOnly, ValueType> {
    typedef ReadOnlyPropertyImplementation<ValueType> type;
};

template< typename InterfaceType,
          typename Arguments = ArgumentList<> >
struct SignalTrait;

template<typename Arguments>
struct SignalTrait<InterfaceProxy, Arguments> {
    typedef SignalProxy<Arguments> type;
};

template<typename Arguments>
struct SignalTrait<InterfaceSkeleton, Arguments> {
    typedef SignalImplementation<Arguments> type;
};

////////////////////////////////////////////////////////////////////////////////

class Service {
public:
    Service();
    virtual ~Service();

    Wrapper<GDBusConnection> connection() const;

    void Connect(const std::string &service_name);
    void Disconnect();

    void RegisterObject(const std::string &path,
                        InterfaceSkeletonPtr skeleton);

    InterfaceSkeletonPtr find_object(const std::string &name) const;

protected:
    virtual void Connected();
    virtual void NameAcquired();
    virtual void NameLost();

    virtual void InvokeMethod(GDBusMethodInvocation *call);

    virtual GVariant* GetProperty(const std::string &sender,
                                  const std::string &target,
                                  const std::string &interface,
                                  const std::string &property,
                                  GError **error);

    virtual bool SetProperty(const std::string &sender,
                             const std::string &target,
                             const std::string &interface,
                             const std::string &property,
                             GVariant *value, GError **error);

private:
    static void OnDBusConnected(GDBusConnection *connection,
                                const char *name, void *data);
    static void OnDBusNameAcquired(GDBusConnection *connection,
                                   const char *name, void *data);
    static void OnDBusNameLost(GDBusConnection *connection,
                               const char *name, void *data);

    static void OnMethodCall(GDBusConnection *, const char *sender,
                             const char *target, const char *interface,
                             const char *method, GVariant *parameters,
                             GDBusMethodInvocation *call, void *data);
    static GVariant* OnGetProperty(GDBusConnection *, const char *sender,
                                   const char *target, const char *interface,
                                   const char *property, GError **error,
                                   void *data);
    static gboolean OnSetProperty(GDBusConnection *, const char *sender,
                                  const char *target, const char *interface,
                                  const char *property, GVariant *value,
                                  GError **error, void *data);

private:
    unsigned name_id_;
    Wrapper<GDBusConnection> connection_;

    struct ObjectInfo {
        ObjectInfo(unsigned object_id, InterfaceSkeletonPtr skeleton)
            : object_id(object_id)
            , skeleton(skeleton) {
        }

        unsigned object_id;
        InterfaceSkeletonPtr skeleton;
    };

    typedef std::map<std::string, ObjectInfo> ObjectMap;
    ObjectMap objects_;
};

////////////////////////////////////////////////////////////////////////////////

template< typename In, typename Out>
inline typename MethodProxy<In, Out>::output_value_type
MethodProxy<In, Out>::CallAndWait(const input_value_type &args,
                                  GError **error) const {
    const Wrapper<GVariant> result = take
            (g_dbus_proxy_call_sync(proxy_->handle().get(),
                                    method_type::name().c_str(),
                                    input_args_type::make_variant(args),
                                    flags_, timeout_ms(), cancellable_.get(),
                                    error));

    return output_args_type::make_value(result.get());
}

template<typename T>
inline T ReadOnlyPropertyProxy<T>::ReadCachedValue
                                        (const InterfaceProxy *proxy) const {
    return Type<T>::make_value(g_dbus_proxy_get_cached_property
            (proxy->handle().get(), PropertyInfo::name().c_str()));
}

template<typename T>
inline bool ReadOnlyPropertyProxy<T>::ReadValue(const InterfaceProxy *proxy,
                                                T *value) const {
    const Wrapper<GVariant> boxed_value =
            proxy->ReadPropertyValue(PropertyInfo::name());

    if (not boxed_value)
        return false;

    const Wrapper<GVariant> unboxed_value =
            take(g_variant_get_variant(boxed_value.get()));

    *value = Type<T>::make_value(unboxed_value.get());

    return true;
}

std::string ConnectionName(Wrapper<GDBusConnection> connection);

} // namespace dbus
} // namespace mediascanner

#endif // MEDIASCANNER_DBUSUTILS_H