summaryrefslogtreecommitdiff
path: root/TelepathyQt/types-internal.h
blob: f6ed06b56332108646de1c65d0445f3bcd9f3ce5 (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
/**
 * This file is part of TelepathyQt
 *
 * @copyright Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
 * @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
 */

#ifndef _TelepathyQt_types_internal_h_HEADER_GUARD_
#define _TelepathyQt_types_internal_h_HEADER_GUARD_

#include <QDBusVariant>
#include <QDBusArgument>
#include <TelepathyQt/Types>

namespace Tp
{

/**
 * Private structure used to circumvent QtDBus' strict demarshalling
 */
struct TP_QT_EXPORT SUSocketAddress
{
    /**
     * A dotted-quad IPv4 address literal: four ASCII decimal numbers, each
     * between 0 and 255 inclusive, e.g. &quot;192.168.0.1&quot;.
     */
    QString address;
    /**
     * The TCP or UDP port number.
     */
    uint port;
};

TP_QT_EXPORT bool operator==(const SUSocketAddress& v1, const SUSocketAddress& v2);
TP_QT_EXPORT inline bool operator!=(const SUSocketAddress& v1, const SUSocketAddress& v2)
{
    return !operator==(v1, v2);
}
TP_QT_EXPORT QDBusArgument& operator<<(QDBusArgument& arg, const SUSocketAddress& val);
TP_QT_EXPORT const QDBusArgument& operator>>(const QDBusArgument& arg, SUSocketAddress& val);

} // Tp

// specialise for Tp::SocketAddressIPv4, allowing it to be used in place of QDBusVariant
template<> inline Tp::SocketAddressIPv4 qdbus_cast<Tp::SocketAddressIPv4>(const QDBusArgument &arg,
Tp::SocketAddressIPv4 *)
{
    if (arg.currentSignature() == QLatin1String("(su)")) {
        // Use Tp::SUSocketAddress
        Tp::SUSocketAddress saddr = qdbus_cast<Tp::SUSocketAddress>(arg);
        Tp::SocketAddressIPv4 addr;
        addr.address = saddr.address;
        addr.port = saddr.port;
        return addr;
    } else if (arg.currentSignature() == QLatin1String("(sq)")) {
        // Keep it standard
        Tp::SocketAddressIPv4 item;
        arg >> item;
        return item;
    } else {
        // This should never happen...
        return Tp::SocketAddressIPv4();
    }
}

template<> inline Tp::SocketAddressIPv4 qdbus_cast<Tp::SocketAddressIPv4>(const QVariant &v, Tp::SocketAddressIPv4 *)
{
    int id = v.userType();
    if (id == qMetaTypeId<QDBusArgument>()) {
        QDBusArgument arg = qvariant_cast<QDBusArgument>(v);

        if (arg.currentSignature() == QLatin1String("(su)")) {
            // Use Tp::SUSocketAddress
            Tp::SUSocketAddress saddr = qdbus_cast<Tp::SUSocketAddress>(arg);
            Tp::SocketAddressIPv4 addr;
            addr.address = saddr.address;
            addr.port = saddr.port;
            return addr;
        } else if (arg.currentSignature() == QLatin1String("(sq)")) {
            // Keep it standard
            Tp::SocketAddressIPv4 item;
            arg >> item;
            return item;
        } else {
            // This should never happen...
            return Tp::SocketAddressIPv4();
        }
    } else
        return qvariant_cast<Tp::SocketAddressIPv4>(v);
}

// specialise for Tp::SocketAddressIPv6, allowing it to be used in place of QDBusVariant
template<> inline Tp::SocketAddressIPv6 qdbus_cast<Tp::SocketAddressIPv6>(const QDBusArgument &arg,
Tp::SocketAddressIPv6 *)
{
    if (arg.currentSignature() == QLatin1String("(su)")) {
        // Use Tp::SUSocketAddress
        Tp::SUSocketAddress saddr = qdbus_cast<Tp::SUSocketAddress>(arg);
        Tp::SocketAddressIPv6 addr;
        addr.address = saddr.address;
        addr.port = saddr.port;
        return addr;
    } else if (arg.currentSignature() == QLatin1String("(sq)")) {
        // Keep it standard
        Tp::SocketAddressIPv6 item;
        arg >> item;
        return item;
    } else {
        // This should never happen...
        return Tp::SocketAddressIPv6();
    }
}

template<> inline Tp::SocketAddressIPv6 qdbus_cast<Tp::SocketAddressIPv6>(const QVariant &v, Tp::SocketAddressIPv6 *)
{
    int id = v.userType();
    if (id == qMetaTypeId<QDBusArgument>()) {
        QDBusArgument arg = qvariant_cast<QDBusArgument>(v);

        if (arg.currentSignature() == QLatin1String("(su)")) {
            // Use Tp::SUSocketAddress
            Tp::SUSocketAddress saddr = qdbus_cast<Tp::SUSocketAddress>(arg);
            Tp::SocketAddressIPv6 addr;
            addr.address = saddr.address;
            addr.port = saddr.port;
            return addr;
        } else if (arg.currentSignature() == QLatin1String("(sq)")) {
            // Keep it standard
            Tp::SocketAddressIPv6 item;
            arg >> item;
            return item;
        } else {
            // This should never happen...
            return Tp::SocketAddressIPv6();
        }
    } else
        return qvariant_cast<Tp::SocketAddressIPv6>(v);
}

Q_DECLARE_METATYPE(Tp::SUSocketAddress)

#endif