summaryrefslogtreecommitdiff
path: root/src/channel-utils.c
blob: 492b54be3702a49dcd3cf1a7d23b017dd1582354 (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
/* vi: set et sw=4 ts=8 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 8 -*- */
/*
 * This file is part of mission-control
 *
 * Copyright (C) 2007-2009 Nokia Corporation.
 * Copyright (C) 2009 Collabora Ltd.
 *
 * Contact: Naba Kumar  <naba.kumar@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * 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 "config.h"

#include "channel-utils.h"

#include <telepathy-glib/telepathy-glib.h>
#include <telepathy-glib/telepathy-glib-dbus.h>

#include "mcd-channel.h"
#include "mcd-debug.h"

gboolean
_mcd_tp_channel_should_close (TpChannel *channel,
                              const gchar *verb)
{
    const GError *invalidated;
    const gchar *object_path;
    GQuark channel_type;

    if (channel == NULL)
    {
        DEBUG ("Not %s NULL channel", verb);
        return FALSE;
    }

    invalidated = tp_proxy_get_invalidated (channel);
    object_path = tp_proxy_get_object_path (channel);

    if (invalidated != NULL)
    {
        DEBUG ("Not %s %p:%s, already invalidated: %s %d: %s",
               verb, channel, object_path,
               g_quark_to_string (invalidated->domain),
               invalidated->code, invalidated->message);
        return FALSE;
    }

    channel_type = tp_channel_get_channel_type_id (channel);

    if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CONTACT_LIST)
    {
        DEBUG ("Not %s %p:%s, it's a ContactList", verb, channel, object_path);
        return FALSE;
    }

    if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TUBES)
    {
        DEBUG ("Not %s %p:%s, it's an old Tubes channel", verb, channel,
               object_path);
        return FALSE;
    }

    return TRUE;
}

static void
_channel_details_array_append (GPtrArray *channel_array, TpChannel *channel)
{
    GType type = TP_STRUCT_TYPE_CHANNEL_DETAILS;
    GValue channel_val = G_VALUE_INIT;
    GHashTable *properties;
    const gchar *object_path;

    properties = tp_channel_borrow_immutable_properties (channel);
    object_path = tp_proxy_get_object_path (channel);

    g_value_init (&channel_val, type);
    g_value_take_boxed (&channel_val,
                        dbus_g_type_specialized_construct (type));
    dbus_g_type_struct_set (&channel_val,
                            0, object_path,
                            1, properties,
                            G_MAXUINT);

    g_ptr_array_add (channel_array, g_value_get_boxed (&channel_val));
}

/*
 * _mcd_tp_channel_details_build_from_list:
 * @channels: a #GList of #McdChannel elements.
 *
 * Returns: a #GPtrArray of Channel_Details, ready to be sent over D-Bus. Free
 * with _mcd_tp_channel_details_free().
 */
GPtrArray *
_mcd_tp_channel_details_build_from_list (const GList *channels)
{
    GPtrArray *channel_array;
    const GList *list;

    channel_array = g_ptr_array_sized_new (g_list_length ((GList *) channels));

    for (list = channels; list != NULL; list = list->next)
    {
        _channel_details_array_append (channel_array,
            mcd_channel_get_tp_channel (MCD_CHANNEL (list->data)));
    }

    return channel_array;
}

/*
 * _mcd_tp_channel_details_build_from_tp_chan:
 * @channel: a #TpChannel
 *
 * Returns: a #GPtrArray of Channel_Details, ready to be sent over D-Bus. Free
 * with _mcd_tp_channel_details_free().
 */
GPtrArray *
_mcd_tp_channel_details_build_from_tp_chan (TpChannel *channel)
{
    GPtrArray *channel_array = g_ptr_array_sized_new (1);

    _channel_details_array_append (channel_array, channel);
    return channel_array;
}

/*
 * _mcd_tp_channel_details_free:
 * @channels: a #GPtrArray of Channel_Details.
 *
 * Frees the memory used by @channels.
 */
void
_mcd_tp_channel_details_free (GPtrArray *channels)
{
    g_boxed_free (TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST, channels);
}