summaryrefslogtreecommitdiff
path: root/src/mcd-account-stats.c
blob: 00daf431971ee3d3d50849256fe90cf0cf5d7690 (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) 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
 * 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 <string.h>
#include <glib.h>
#include <config.h>

#include <libmcclient/mc-gtypes.h>
#include <libmcclient/mc-interfaces.h>

#include "mcd-account.h"
#include "mcd-account-priv.h"
#include "mcd-account-manager.h"
#include <telepathy-glib/svc-generic.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/util.h>

static GHashTable *
mcd_account_get_channel_count (McdAccount *account)
{
    GHashTable *stats;
    McdConnection *connection;

    stats = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
    connection = mcd_account_get_connection (account);
    if (connection)
    {
        const GList *channels;

        channels = mcd_operation_get_missions (MCD_OPERATION (connection));
        for (; channels != NULL; channels = channels->next)
        {
            McdChannel *channel;
            const gchar *channel_type;
            guint count;

            channel = MCD_CHANNEL (channels->data);
            channel_type = mcd_channel_get_channel_type (channel);
            if (G_UNLIKELY (!channel_type)) continue;

            count = GPOINTER_TO_UINT (g_hash_table_lookup (stats,
                                                           channel_type));
            count++;
            g_hash_table_insert (stats, g_strdup (channel_type),
                                 GUINT_TO_POINTER (count));
        }
    }

    return stats;
}

static void
get_channel_count (TpSvcDBusProperties *self, const gchar *name, GValue *value)
{
    GHashTable *stats;

    stats = mcd_account_get_channel_count (MCD_ACCOUNT (self));

    g_value_init (value, MC_HASH_TYPE_CHANNEL_COUNT_MAP);
    g_value_take_boxed (value, stats);
}


const McdDBusProp account_stats_properties[] = {
    { "ChannelCount", NULL, get_channel_count },
    { 0 },
};

void
account_stats_iface_init (McSvcAccountInterfaceStatsClass *iface,
                          gpointer iface_data)
{
}

static void
on_channel_count_changed (McdConnection *connection, McdChannel *channel,
                          McdAccount *account)
{
    GHashTable *stats, *properties;
    GValue value = { 0 };

    stats = mcd_account_get_channel_count (account);

    g_value_init (&value, MC_HASH_TYPE_CHANNEL_COUNT_MAP);
    g_value_take_boxed (&value, stats);

    properties = g_hash_table_new (g_str_hash, g_str_equal);
    g_hash_table_insert (properties, (gpointer)"ChannelCount", &value);

    mc_svc_account_interface_stats_emit_stats_changed (account, properties);

    g_hash_table_destroy (properties);
    g_value_unset (&value);
}

static void
watch_connection (McdAccount *account)
{
    McdConnection *connection;

    connection = mcd_account_get_connection (account);
    if (G_UNLIKELY (!connection)) return;

    g_signal_connect (connection, "mission-taken",
                      G_CALLBACK (on_channel_count_changed), account);
    g_signal_connect (connection, "mission-removed",
                      G_CALLBACK (on_channel_count_changed), account);
}

static void
on_account_connection_status_changed (McdAccount *account,
                                      TpConnectionStatus status,
                                      TpConnectionStatusReason reason)
{
    if (status == TP_CONNECTION_STATUS_CONNECTED)
        watch_connection (account);
}

void
account_stats_instance_init (TpSvcDBusProperties *self)
{
    McdAccount *account = MCD_ACCOUNT (self);

    if (mcd_account_get_connection_status (account) ==
        TP_CONNECTION_STATUS_CONNECTED)
    {
        watch_connection (account);
    }

    g_signal_connect (account, "connection-status-changed",
                      G_CALLBACK (on_account_connection_status_changed), NULL);
}