~ubuntu-branches/debian/squeeze/gnome-main-menu/squeeze

« back to all changes in this revision

Viewing changes to main-menu/src/hard-drive-status-tile.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2009-05-07 17:08:50 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090507170850-2cpq07j27tedp3fp
Tags: 0.9.12-2~squeeze1
* Rebuild for squeeze against NM 0.7.
* Upload coordinated with the release team.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include "hard-drive-status-tile.h"
22
22
 
23
23
#include <string.h>
24
 
#include <dbus/dbus.h>
25
 
#include <dbus/dbus-glib.h>
26
 
#include <dbus/dbus-glib-lowlevel.h>
 
24
#include <sys/statvfs.h>
27
25
#include <glib/gi18n.h>
28
 
#include <glibtop/fsusage.h>
29
 
#include <libhal.h>
30
 
#include <libhal-storage.h>
31
26
#include <gconf/gconf-client.h>
32
27
 
33
28
#include "slab-gnome-util.h"
40
35
#define TIMEOUT_KEY_DIR "/apps/procman"
41
36
#define TIMEOUT_KEY     "/apps/procman/disks_interval"
42
37
 
43
 
#define FILE_BROWSER_GCONF_KEY "/desktop/gnome/applications/main-menu/file_browser"
 
38
#define SYSTEM_MONITOR_GCONF_KEY "/desktop/gnome/applications/main-menu/system_monitor"
44
39
 
45
40
G_DEFINE_TYPE (HardDriveStatusTile, hard_drive_status_tile, NAMEPLATE_TILE_TYPE)
46
41
 
59
54
 
60
55
typedef struct
61
56
{
62
 
        LibHalContext *hal_context;
63
 
        DBusConnection *dbus_connection;
64
 
        
65
57
        GConfClient *gconf;
66
58
        
67
 
        guint64 capacity_bytes;
68
 
        guint64 available_bytes;
69
 
        
 
59
        gdouble capacity_bytes;
 
60
        gdouble available_bytes;        
 
61
 
70
62
        guint timeout_notify;
71
63
        
72
64
        guint update_timeout;
105
97
        char *name;
106
98
 
107
99
        image = gtk_image_new ();
108
 
        slab_load_image (GTK_IMAGE (image), GTK_ICON_SIZE_BUTTON, "gnome-dev-harddisk");
 
100
        slab_load_image (GTK_IMAGE (image), GTK_ICON_SIZE_BUTTON, "utilities-system-monitor");
109
101
 
110
 
        name = g_strdup (_("Hard _Drive"));
 
102
        name = g_strdup (_("_System Monitor"));
111
103
 
112
104
        header = gtk_label_new (name);
113
105
        gtk_label_set_use_underline (GTK_LABEL (header), TRUE);
148
140
hard_drive_status_tile_init (HardDriveStatusTile * tile)
149
141
{
150
142
        HardDriveStatusTilePrivate *priv = HARD_DRIVE_STATUS_TILE_GET_PRIVATE (tile);
151
 
        DBusError error;
152
 
 
153
 
        priv->hal_context = libhal_ctx_new ();
154
 
 
155
 
        if (!priv->hal_context)
156
 
                return;
157
 
 
158
 
        dbus_error_init (&error);
159
 
        priv->dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
160
 
 
161
 
        dbus_connection_set_exit_on_disconnect (priv->dbus_connection, FALSE);
162
 
 
163
 
        if (dbus_error_is_set (&error))
164
 
        {
165
 
                g_warning ("error (%s): [%s]\n", error.name, error.message);
166
 
 
167
 
                dbus_error_free (&error);
168
 
 
169
 
                priv->hal_context = NULL;
170
 
                priv->dbus_connection = NULL;
171
 
 
172
 
                return;
173
 
        }
174
 
 
175
 
        dbus_error_free (&error);
176
 
 
177
 
        dbus_connection_setup_with_g_main (priv->dbus_connection, g_main_context_default ());
178
 
 
179
 
        libhal_ctx_set_dbus_connection (priv->hal_context, priv->dbus_connection);
180
 
 
181
 
        dbus_error_init (&error);
182
 
        libhal_ctx_init (priv->hal_context, &error);
183
 
 
184
 
        if (dbus_error_is_set (&error))
185
 
        {
186
 
                g_warning ("error (%s): [%s]\n", error.name, error.message);
187
 
 
188
 
                dbus_error_free (&error);
189
 
 
190
 
                priv->hal_context = NULL;
191
 
 
192
 
                return;
193
 
        }
194
 
 
195
 
        dbus_error_free (&error);
196
143
 
197
144
        priv->gconf = gconf_client_get_default ();
198
145
        gconf_client_add_dir (priv->gconf, TIMEOUT_KEY_DIR, GCONF_CLIENT_PRELOAD_NONE, NULL);
201
148
static void
202
149
hard_drive_status_tile_finalize (GObject * g_object)
203
150
{
204
 
/*      if (data->hal_context)
205
 
                libhal_ctx_free (data->hal_context); */
206
151
        /* FIXME */
207
152
        (*G_OBJECT_CLASS (hard_drive_status_tile_parent_class)->finalize) (g_object);
208
153
}
242
187
                tile_trigger_action_with_time (tile, tile->default_action, event->time);
243
188
}
244
189
 
245
 
static GList *
246
 
get_pertinent_devices (LibHalContext * hal_context)
247
 
{
248
 
        GList *devices;
249
 
 
250
 
        gchar **udis;
251
 
        gint n_udis;
252
 
 
253
 
        LibHalVolume *vol;
254
 
        gchar *mount_point;
255
 
 
256
 
        DBusError error;
257
 
 
258
 
        gint i;
259
 
 
260
 
        if (!hal_context)
261
 
                return NULL;
262
 
 
263
 
        dbus_error_init (&error);
264
 
        udis = libhal_find_device_by_capability (hal_context, "volume", &n_udis, &error);
265
 
 
266
 
        if (dbus_error_is_set (&error))
267
 
        {
268
 
                g_warning ("error (%s): [%s]\n", error.name, error.message);
269
 
 
270
 
                dbus_error_free (&error);
271
 
 
272
 
                return NULL;
273
 
        }
274
 
 
275
 
        dbus_error_free (&error);
276
 
 
277
 
        devices = NULL;
278
 
 
279
 
        for (i = 0; i < n_udis; ++i)
280
 
        {
281
 
                vol = libhal_volume_from_udi (hal_context, udis[i]);
282
 
 
283
 
                if (libhal_volume_is_mounted (vol) && !libhal_volume_is_disc (vol))
284
 
                {
285
 
                        mount_point = g_strdup (libhal_volume_get_mount_point (vol));
286
 
                        devices = g_list_append (devices, mount_point);
287
 
                }
288
 
 
289
 
                libhal_volume_free (vol);
290
 
        }
291
 
 
292
 
        libhal_free_string_array (udis);
293
 
 
294
 
        return devices;
295
 
}
296
 
 
297
190
static void
298
191
compute_usage (HardDriveStatusTile * tile)
299
192
{
300
193
        HardDriveStatusTilePrivate *priv = HARD_DRIVE_STATUS_TILE_GET_PRIVATE (tile);
301
 
 
302
 
        GList *devices;
303
 
        glibtop_fsusage fs_usage;
304
 
 
305
 
        GList *node;
306
 
 
307
 
        if (!priv->hal_context)
 
194
        struct statvfs s;
 
195
 
 
196
        if (statvfs (g_get_home_dir (), &s) != 0) {
 
197
                priv->available_bytes = 0;
 
198
                priv->capacity_bytes = 0;
308
199
                return;
309
 
 
310
 
        devices = get_pertinent_devices (priv->hal_context);
311
 
 
312
 
        priv->capacity_bytes = 0;
313
 
        priv->available_bytes = 0;
314
 
 
315
 
        for (node = devices; node; node = node->next)
316
 
        {
317
 
                if (node->data)
318
 
                {
319
 
                        glibtop_get_fsusage (&fs_usage, (gchar *) node->data);
320
 
 
321
 
                        priv->available_bytes += fs_usage.bfree * fs_usage.block_size;
322
 
                        priv->capacity_bytes += fs_usage.blocks * fs_usage.block_size;
323
 
 
324
 
                        g_free (node->data);
325
 
                }
326
200
        }
327
201
 
328
 
        g_list_free (devices);
 
202
        priv->available_bytes = (gdouble)s.f_frsize * s.f_bavail;
 
203
        priv->capacity_bytes = (gdouble)s.f_frsize * s.f_blocks;
329
204
}
330
205
 
331
206
static gchar *
332
 
size_bytes_to_string (guint64 size)
 
207
size_bytes_to_string (gdouble size)
333
208
{
334
209
        gchar *size_string;
335
 
        unsigned long long size_bytes;
336
 
 
337
 
/* FIXME:  this is just a work around for gcc warnings about %lu not big enough
338
 
 * to hold guint64 on 32bit machines.  on 64bit machines, however, gcc warns
339
 
 * that %llu is too big for guint64.  ho-hum.
340
 
 */
341
 
 
342
 
        size_bytes = (unsigned long long) size;
343
 
 
344
 
        if (size_bytes > GIGA)
345
 
                size_string = g_strdup_printf (_("%lluG"), size_bytes / GIGA);
346
 
        else if (size_bytes > MEGA)
347
 
                size_string = g_strdup_printf (_("%lluM"), size_bytes / MEGA);
348
 
        else if (size_bytes > KILO)
349
 
                size_string = g_strdup_printf (_("%lluK"), size_bytes / KILO);
 
210
 
 
211
        if (size > GIGA)
 
212
                size_string = g_strdup_printf (_("%.1fG"), size / GIGA);
 
213
        else if (size > MEGA)
 
214
                size_string = g_strdup_printf (_("%.1fM"), size / MEGA);
 
215
        else if (size > KILO)
 
216
                size_string = g_strdup_printf (_("%.1fK"), size / KILO);
350
217
        else
351
 
                size_string = g_strdup_printf (_("%llub"), size_bytes);
 
218
                size_string = g_strdup_printf (_("%.1fb"), size);
352
219
 
353
220
        return size_string;
354
221
}
369
236
        available = size_bytes_to_string (priv->available_bytes);
370
237
        capacity = size_bytes_to_string (priv->capacity_bytes);
371
238
 
372
 
        markup = g_strdup_printf (_("%s Free / %s Total"), available, capacity);
 
239
        markup = g_strdup_printf (_("Home: %s Free / %s"), available, capacity);
373
240
 
374
241
        gtk_label_set_text (GTK_LABEL (NAMEPLATE_TILE (tile)->subheader), markup);
375
242
 
459
326
        gchar *fb_ditem_id;
460
327
        
461
328
 
462
 
        fb_ditem_id = (gchar *) libslab_get_gconf_value (FILE_BROWSER_GCONF_KEY);
 
329
        fb_ditem_id = (gchar *) libslab_get_gconf_value (SYSTEM_MONITOR_GCONF_KEY);
463
330
 
464
331
        if (! fb_ditem_id)
465
 
                fb_ditem_id = g_strdup ("nautilus.desktop");
 
332
                fb_ditem_id = g_strdup ("gnome-system-monitor.desktop");
466
333
 
467
334
        ditem = libslab_gnome_desktop_item_new_from_unknown_id (fb_ditem_id);
468
335