Also list the space available on each volume

This commit is contained in:
Pierre Pronchery 2013-11-27 22:05:50 +01:00
parent a90fdd39fd
commit 8fc65bb5e6

View File

@ -1,5 +1,5 @@
/* $Id$ */ /* $Id$ */
/* Copyright (c) 2011-2012 Pierre Pronchery <khorben@defora.org> */ /* Copyright (c) 2011-2013 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS Desktop Browser */ /* This file is part of DeforaOS Desktop Browser */
/* This program is free software: you can redistribute it and/or modify /* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -34,9 +34,11 @@ enum _VolumesColumn
{ {
DC_PIXBUF = 0, DC_PIXBUF = 0,
DC_NAME, DC_NAME,
DC_MOUNTPOINT DC_MOUNTPOINT,
DC_FREE,
DC_FREE_DISPLAY
}; };
#define DC_LAST DC_MOUNTPOINT #define DC_LAST DC_FREE_DISPLAY
#define DC_COUNT (DC_LAST + 1) #define DC_COUNT (DC_LAST + 1)
enum _VolumesPixbuf enum _VolumesPixbuf
@ -108,18 +110,27 @@ static Volumes * _volumes_init(BrowserPluginHelper * helper)
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(volumes->window), gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(volumes->window),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
volumes->store = gtk_list_store_new(DC_COUNT, GDK_TYPE_PIXBUF, volumes->store = gtk_list_store_new(DC_COUNT, GDK_TYPE_PIXBUF,
G_TYPE_STRING, G_TYPE_STRING); G_TYPE_STRING, G_TYPE_STRING, G_TYPE_DOUBLE,
G_TYPE_STRING);
volumes->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL( volumes->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(
volumes->store)); volumes->store));
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(volumes->view), FALSE); gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(volumes->view), FALSE);
/* icon */
renderer = gtk_cell_renderer_pixbuf_new(); renderer = gtk_cell_renderer_pixbuf_new();
column = gtk_tree_view_column_new_with_attributes(NULL, renderer, column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
"pixbuf", DC_PIXBUF, NULL); "pixbuf", DC_PIXBUF, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(volumes->view), column); gtk_tree_view_append_column(GTK_TREE_VIEW(volumes->view), column);
/* volume name */
renderer = gtk_cell_renderer_text_new(); renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(NULL, renderer, column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
"text", DC_NAME, NULL); "text", DC_NAME, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(volumes->view), column); gtk_tree_view_append_column(GTK_TREE_VIEW(volumes->view), column);
/* free space */
renderer = gtk_cell_renderer_progress_new();
/* FIXME the "value" field seems to be ignored */
column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
"text", DC_FREE_DISPLAY, "value", DC_FREE, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(volumes->view), column);
treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(volumes->view)); treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(volumes->view));
gtk_tree_selection_set_mode(treesel, GTK_SELECTION_SINGLE); gtk_tree_selection_set_mode(treesel, GTK_SELECTION_SINGLE);
g_signal_connect_swapped(G_OBJECT(treesel), "changed", G_CALLBACK( g_signal_connect_swapped(G_OBJECT(treesel), "changed", G_CALLBACK(
@ -155,7 +166,7 @@ static GtkWidget * _volumes_get_widget(Volumes * volumes)
/* volumes_refresh */ /* volumes_refresh */
static void _refresh_add(Volumes * volumes, char const * name, static void _refresh_add(Volumes * volumes, char const * name,
char const * device, char const * mountpoint, char const * device, char const * mountpoint,
char const * filesystem); char const * filesystem, fsblkcnt_t free, fsblkcnt_t total);
static void _volumes_refresh(Volumes * volumes, GList * selection) static void _volumes_refresh(Volumes * volumes, GList * selection)
{ {
@ -183,9 +194,10 @@ static void _volumes_refresh(Volumes * volumes, GList * selection)
return; return;
for(i = 0; i < res; i++) for(i = 0; i < res; i++)
_refresh_add(volumes, NULL, mnt[i].f_mntfromname, _refresh_add(volumes, NULL, mnt[i].f_mntfromname,
mnt[i].f_mntonname, mnt[i].f_fstypename); mnt[i].f_mntonname, mnt[i].f_fstypename,
mnt[i].f_bavail, mnt[i].f_blocks);
#else #else
_refresh_add(volumes, NULL, NULL, "/", NULL); _refresh_add(volumes, NULL, NULL, "/", NULL, 0, 0);
#endif #endif
if(volumes->source == 0) if(volumes->source == 0)
volumes->source = g_timeout_add(1000, _volumes_on_timeout, volumes->source = g_timeout_add(1000, _volumes_on_timeout,
@ -194,7 +206,7 @@ static void _volumes_refresh(Volumes * volumes, GList * selection)
static void _refresh_add(Volumes * volumes, char const * name, static void _refresh_add(Volumes * volumes, char const * name,
char const * device, char const * mountpoint, char const * device, char const * mountpoint,
char const * filesystem) char const * filesystem, fsblkcnt_t free, fsblkcnt_t total)
{ {
GtkTreeIter iter; GtkTreeIter iter;
GdkPixbuf * pixbuf = volumes->icons[0]; GdkPixbuf * pixbuf = volumes->icons[0];
@ -202,6 +214,8 @@ static void _refresh_add(Volumes * volumes, char const * name,
char const * cdrom[] = { "/dev/cd" }; char const * cdrom[] = { "/dev/cd" };
char const * removable[] = { "/dev/sd" }; char const * removable[] = { "/dev/sd" };
size_t i; size_t i;
double fraction = 0.0;
char buf[16] = "";
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "DEBUG: %s(\"%s\", \"%s\", \"%s\", \"%s\")\n", __func__, fprintf(stderr, "DEBUG: %s(\"%s\", \"%s\", \"%s\", \"%s\")\n", __func__,
@ -229,9 +243,16 @@ static void _refresh_add(Volumes * volumes, char const * name,
else else
name = mountpoint; name = mountpoint;
} }
if(total != 0 && total >= free)
{
fraction = total - free;
fraction = fraction / total;
snprintf(buf, sizeof(buf), "%.1lf%%", fraction * 100.0);
}
gtk_list_store_append(volumes->store, &iter); gtk_list_store_append(volumes->store, &iter);
gtk_list_store_set(volumes->store, &iter, DC_PIXBUF, pixbuf, gtk_list_store_set(volumes->store, &iter, DC_PIXBUF, pixbuf,
DC_NAME, name, DC_MOUNTPOINT, mountpoint, -1); DC_NAME, name, DC_MOUNTPOINT, mountpoint,
DC_FREE, fraction, DC_FREE_DISPLAY, buf, -1);
} }