~rodrigo-moya/libubuntuone/location-property

« back to all changes in this revision

Viewing changes to libubuntuone/syncdaemon.c

  • Committer: Tarmac
  • Author(s): Rodrigo Moya
  • Date: 2010-03-01 22:43:13 UTC
  • mfrom: (41.2.13 poll-downloads)
  • Revision ID: rodrigo@megeve-20100301224313-z8quasd5crb50uez
Implement correct polling of downloads

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <dbus/dbus-glib.h>
24
24
#include "syncdaemon.h"
25
25
 
26
 
void
 
26
static DBusGConnection *bus = NULL;
 
27
static DBusGProxy *status_proxy = NULL;
 
28
 
 
29
void
 
30
syncdaemon_init (void)
 
31
{
 
32
        GError *error;
 
33
 
 
34
        if (status_proxy != NULL)
 
35
                return;
 
36
 
 
37
        /* Initialize connection to DBus if not done already */
 
38
        if (bus == NULL) {
 
39
                error = NULL;
 
40
                bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 
41
                if (error) {
 
42
                        g_warning ("Couldn't get session bus: %s", error->message);
 
43
                        g_error_free (error);
 
44
                        return;
 
45
                }
 
46
        }
 
47
 
 
48
        status_proxy = dbus_g_proxy_new_for_name (bus, "com.ubuntuone.SyncDaemon",
 
49
                                                  "/status", "com.ubuntuone.SyncDaemon.Status");
 
50
        if (status_proxy == NULL) {
 
51
                g_warning ("Could not get proxy for com.ubuntuone.SyncDaemon.Status");
 
52
                dbus_g_connection_unref (bus);
 
53
        }
 
54
}
 
55
 
 
56
void
 
57
syncdaemon_finalize (void)
 
58
{
 
59
        g_object_unref (G_OBJECT (status_proxy));
 
60
        dbus_g_connection_unref (bus);
 
61
}
 
62
 
 
63
static void
27
64
free_syncdaemon_download (SyncdaemonDownload *item_data)
28
65
{
29
66
        g_free (item_data->path);
36
73
GHashTable *
37
74
syncdaemon_get_current_downloads (void)
38
75
{
39
 
        DBusGConnection *bus;
40
 
        DBusGProxy *status_proxy;
41
76
        GError *error;
42
77
        GPtrArray *return_value;
43
 
        GHashTable *downloads;
44
 
 
45
 
        /* Initialize connection to DBus if not done already */
46
 
        error = NULL;
47
 
        bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
48
 
        if (error) {
49
 
                g_warning ("Couldn't get session bus: %s", error->message);
50
 
                g_error_free (error);
51
 
                return NULL;
52
 
        }
53
 
 
54
 
        status_proxy = dbus_g_proxy_new_for_name (bus, "com.ubuntuone.SyncDaemon",
55
 
                                                  "/status", "com.ubuntuone.SyncDaemon.Status");
56
 
        if (status_proxy == NULL) {
57
 
                g_warning ("Could not get proxy for com.ubuntuone.SyncDaemon.Status");
58
 
                dbus_g_connection_unref (bus);
59
 
 
60
 
                return NULL;
61
 
        }
 
78
        GHashTable *downloads = NULL;
 
79
 
 
80
        syncdaemon_init ();
62
81
 
63
82
        /* Ask the syncdaemon about current downloads */
64
83
        error = NULL;
73
92
                downloads = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_syncdaemon_download);
74
93
                for (i = 0; i < return_value->len; i++) {
75
94
                        SyncdaemonDownload *item_data;
 
95
                        gchar *str;
76
96
                        GHashTable *values = (GHashTable *) g_ptr_array_index (return_value, i);
77
97
 
78
98
                        item_data = g_new0 (SyncdaemonDownload, 1);
79
99
                        item_data->path = g_strdup (g_hash_table_lookup (values, "path"));
80
100
                        item_data->share_id = g_strdup (g_hash_table_lookup (values, "share_id"));
81
101
                        item_data->node_id = g_strdup (g_hash_table_lookup (values, "node_id"));
82
 
                        item_data->n_bytes_read = atol (g_hash_table_lookup (values, "n_bytes_read"));
83
 
                        item_data->deflated_size = atol (g_hash_table_lookup (values, "deflated_size"));
 
102
 
 
103
                        str = g_hash_table_lookup (values, "n_bytes_read");
 
104
                        if (str != NULL)
 
105
                                item_data->n_bytes_read = atol (str);
 
106
                        else
 
107
                                item_data->n_bytes_read = 0;
 
108
 
 
109
                        str = g_hash_table_lookup (values, "deflated_size");
 
110
                        if (str != NULL)
 
111
                                item_data->deflated_size = atol (str);
 
112
                        else
 
113
                                item_data->deflated_size = 0;
84
114
 
85
115
                        g_hash_table_insert (downloads, g_strdup (item_data->path), item_data);
86
116
                }
91
121
                g_error_free (error);
92
122
        }
93
123
 
94
 
        g_object_unref (G_OBJECT (status_proxy));
95
 
        dbus_g_connection_unref (bus);
96
 
 
97
124
        return downloads;
98
125
}
99
126