summaryrefslogtreecommitdiff
path: root/libmcclient/mc-account-stats.c
blob: 9e87b4f3e4899e0ad7bc0ced3e75a565910ab3ad (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
/* vi: set et sw=4 ts=8 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 8 -*- */
/*
 * mc-account-stats.c - Telepathy Account D-Bus interface (client side)
 *
 * Copyright (C) 2008 Nokia Corporation
 *
 * Contact: Alberto Mardegan  <alberto.mardegan@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 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
 */

#define MC_INTERNAL

#include <stdio.h>
#include <string.h>
#include "mc-account.h"
#include "mc-account-priv.h"
#include "dbus-api.h"
#include "mc-signals-marshal.h"

struct _McAccountStatsProps {
    GHashTable *channel_count;
};

static void create_props (TpProxy *proxy, GHashTable *props);
static void setup_props_monitor (TpProxy *proxy, GQuark interface);

static McIfaceDescription iface_description = {
    G_STRUCT_OFFSET (McAccountPrivate, stats_props),
    create_props,
    setup_props_monitor,
};


void
_mc_account_stats_props_free (McAccountStatsProps *props)
{
    g_hash_table_unref (props->channel_count);
    g_slice_free (McAccountStatsProps, props);
}

static void
channel_count_changed (McAccount *account, GHashTable *channel_count)
{
    McAccountStatsProps *props = account->priv->stats_props;

    if (props->channel_count)
        g_hash_table_unref (props->channel_count);
    props->channel_count = channel_count;
}

void
_mc_account_stats_class_init (McAccountClass *klass)
{
    klass->stats_channel_count_changed = channel_count_changed;

    /**
     * McAccount::channel-count-changed:
     * @account: the #McAccount.
     * @channel_count: a #GHashTable with the new channel counters.
     *
     * Emitted when the stats changes.
     * The McAccount member data are updated in the signal closure, so use
     * g_signal_connect_after() if you need them to reflect the new status.
     */
    _mc_account_signals[CHANNEL_COUNT_CHANGED] =
        g_signal_new ("channel-count-changed",
                      G_OBJECT_CLASS_TYPE (klass),
                      G_SIGNAL_RUN_LAST,
                      G_STRUCT_OFFSET (McAccountClass,
                                       stats_channel_count_changed),
                      NULL, NULL,
                      g_cclosure_marshal_VOID__BOXED,
                      G_TYPE_NONE,
                      1, G_TYPE_HASH_TABLE);
}

static void
update_channel_count (const gchar *name, const GValue *value,
                      gpointer user_data)
{
    McAccount *account = MC_ACCOUNT (user_data);
    McAccountStatsProps *props = account->priv->stats_props;
    GHashTable *channel_count;

    channel_count = g_value_dup_boxed (value);
    if (props->channel_count)
        /* the signal closure will update the props->channel_count */
        g_signal_emit (account, _mc_account_signals[CHANNEL_COUNT_CHANGED],
                       0, channel_count);
    else
        props->channel_count = channel_count;
}

static const McIfaceProperty account_stats_properties[] =
{
    { "ChannelCount", "a{su}", update_channel_count },
    { NULL, NULL, NULL }
};

static void
create_props (TpProxy *proxy, GHashTable *props)
{
    McAccount *account = MC_ACCOUNT (proxy);
    McAccountPrivate *priv = account->priv;

    priv->stats_props = g_slice_new0 (McAccountStatsProps);
    _mc_iface_update_props (account_stats_properties, props, account);
}

static void
on_stats_changed (TpProxy *proxy, GHashTable *properties, gpointer user_data,
                  GObject *weak_object)
{
    McAccount *account = MC_ACCOUNT (proxy);
    McAccountPrivate *priv = account->priv;

    /* if the GetAll method hasn't returned yet, we do nothing */
    if (G_UNLIKELY (!priv->stats_props)) return;

    _mc_iface_update_props (account_stats_properties, properties,
                            account);
}

static void
setup_props_monitor (TpProxy *proxy, GQuark interface)
{
    McAccount *account = MC_ACCOUNT (proxy);

    mc_cli_account_interface_stats_connect_to_stats_changed (account,
                                                             on_stats_changed,
                                                             NULL, NULL,
                                                             NULL, NULL);
}

/**
 * mc_account_stats_get_channel_count:
 * @account: the #McAccount.
 *
 * Retrieves the number of account active channels, by channel type. This also
 * includes channel requests.
 *
 * Returns: a #GHashTable which maps channel types to their usage count (use
 * GPOINTER_TO_UINT() to convert the values into integers).
 */
GHashTable *
mc_account_stats_get_channel_count (McAccount *account)
{
    McAccountStatsProps *props;

    g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
    props = account->priv->stats_props;
    if (G_UNLIKELY (!props))
        return NULL;

    return props->channel_count;
}