sd_capacity.c

Go to the documentation of this file.
00001 /*
00002  * File Name: sd_capacity.c
00003  */
00004 
00005 /*
00006  * This file is part of settings.
00007  *
00008  * settings is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * settings is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 // system include files, between < >
00032 #ifndef WIN32
00033 #include <sys/stat.h>
00034 #include <sys/statfs.h>
00035 #include <sys/types.h>
00036 #include <sys/wait.h>
00037 #endif
00038 
00039 #include <string.h>
00040 #include <stdlib.h>
00041 #include <unistd.h>
00042 #include <stdio.h>
00043 #include <sched.h>
00044 
00045 #include <gtk/gtk.h>
00046 #include <gdk/gdk.h>
00047 #include <glib.h>
00048 
00049 #include <libergtk/ergtk.h>
00050 
00051 #include "i18n.h"
00052 #include "log.h"
00053 #include "ipc.h"
00054 #include "settings.h"
00055 #include "settings_utils.h"
00056 #include "settings_style.h"
00057 #include "sd_capacity.h"
00058 
00059 
00060 //----------------------------------------------------------------------------
00061 // Type Declarations
00062 //----------------------------------------------------------------------------
00063 
00064 //----------------------------------------------------------------------------
00065 // Global Constants
00066 //----------------------------------------------------------------------------
00067 
00068 //----------------------------------------------------------------------------
00069 // Static Variables
00070 //----------------------------------------------------------------------------
00071 static gboolean g_sd_card_mounted = FALSE;  // SD card currently mounted or not
00072 
00073 static GtkWidget* g_sdcapacity_window = NULL ; 
00074 static GtkWidget* g_capacity_bar = NULL ; 
00075 static GtkWidget* g_used_space_label = NULL ; 
00076 static GtkWidget* g_free_space_label = NULL ;
00077 
00078 static volatile gboolean g_is_updating = FALSE;
00079 
00080 //============================================================================
00081 // Local Function Definitions
00082 //============================================================================
00083 
00084 static GtkWidget* create_sdcapacity_widgets         (GtkBox* parent);
00085 static void update_sd_widgets();
00086 
00087 static void init_widgets_with_settings          (void);
00088 static void on_listview_row_activated ( GtkTreeView       *view,
00089                                         GtkTreePath       *path,
00090                                         GtkTreeViewColumn *column,
00091                                         gpointer          user_data );
00092 
00093 static void on_listview_navigate_cursor ( erGtkListView         *er_listview,
00094         erGtkListViewKeyPress keycode,
00095         gpointer              user_data );
00096 
00097 static gboolean on_focus_in(GtkWidget* widget, gpointer data );
00098 static gboolean on_focus_out(GtkWidget* widget, gpointer data );
00099 
00100 //============================================================================
00101 // Functions Implementation
00102 //============================================================================
00103 
00104 GtkWidget* create_sdcapacity_window(GtkWidget* parent)
00105 {
00106     // create top window
00107     GtkWidget* top_window = parent;
00108     gtk_window_maximize(GTK_WINDOW(top_window));
00109     gtk_window_set_resizable(GTK_WINDOW(top_window), FALSE);
00110     gtk_container_set_border_width(GTK_CONTAINER(top_window),WINDOW_BORDER_PADDING  );
00111     gtk_window_set_modal(GTK_WINDOW(top_window), TRUE);
00112 
00113     // top level vbox (vboxtop)
00114     GtkWidget* vboxtop = gtk_vbox_new(FALSE, 0);
00115     gtk_container_add(GTK_CONTAINER(top_window), vboxtop);
00116 
00117     // add header container the title and subtitle of this settings page
00118     create_title(GTK_VBOX(vboxtop), _("Settings"), _("SD Card Information"));
00119     
00120     // add the back/exit bar below the title 
00121     GtkWidget* view = create_settingsview();
00122     gtk_box_pack_start(GTK_BOX(vboxtop), view, FALSE, FALSE,0 ); 
00123     g_signal_connect(view, "row-activated", G_CALLBACK(on_listview_row_activated), NULL ) ;
00124     g_signal_connect(view, "navigate-cursor", G_CALLBACK(on_listview_navigate_cursor), NULL ) ;
00125     g_signal_connect(view, "focus-in-event", G_CALLBACK(on_focus_in), NULL );
00126     g_signal_connect(view, "focus-out-event", G_CALLBACK(on_focus_out), NULL );
00127 
00128     // add an alignment below the exit bar
00129     GtkWidget* alignment = gtk_alignment_new(0, 0, 1.0, 0.0);
00130     gtk_container_add(GTK_CONTAINER(vboxtop), alignment);
00131 
00132     // introdude a frame in the alignment
00133     GtkWidget* frame = gtk_frame_new(NULL);
00134     gtk_widget_set_name(frame, "irex-margins-frame");
00135     gtk_container_add(GTK_CONTAINER(alignment), frame);
00136     gtk_container_set_border_width(GTK_CONTAINER(frame), 3);
00137 
00138     // alignment in frame 
00139     GtkWidget* alignment2 = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
00140     gtk_container_add(GTK_CONTAINER(frame), alignment2);
00141     gtk_alignment_set_padding(GTK_ALIGNMENT(alignment2),
00142                               WINDOW_TOP_ALT_PADDING,
00143                               WINDOW_BOTTOM_PADDING,
00144                               WINDOW_H_PADDING,
00145                               WINDOW_H_PADDING);
00146    
00147     // vbox in alignment2
00148     GtkWidget* innervbox = gtk_vbox_new(FALSE, SMALL_SPACING);
00149     gtk_container_add(GTK_CONTAINER(alignment2), innervbox);
00150     
00151     // The margin settings section.
00152     create_sdcapacity_widgets(GTK_BOX(innervbox));
00153 
00154     // Update widget with current settings.
00155     init_widgets_with_settings();
00156 
00157     gtk_widget_grab_focus(view);
00158     
00159     gtk_widget_show_all(top_window);
00160 
00161     g_sdcapacity_window = top_window;
00162 
00163     return top_window;
00164 }
00165 
00166 
00167 void load_sdcapacity_settings()
00168 {
00169     ;
00170 }
00171 
00172 
00173 void save_sdcapacity_settings()
00174 {
00175     ;
00176 }
00177 
00178 
00179 void sd_card_mounted(gboolean is_mounted)
00180 {
00181     LOGPRINTF("entry: is_mounted [%d]", is_mounted);
00182     
00183     while (g_is_updating == TRUE) {
00184         sched_yield();
00185     }
00186 
00187     g_is_updating = TRUE;
00188     
00189     // remember card state
00190     g_sd_card_mounted = is_mounted;
00191 
00192     // show free space
00193 
00194     if (g_sdcapacity_window != NULL)
00195     {
00196         update_sd_widgets();
00197     }
00198     g_is_updating = FALSE;
00199 }
00200 
00201 //============================================================================
00202 // Local Functions Implementation
00203 //============================================================================
00204 
00205 static void on_listview_row_activated ( GtkTreeView       *view,
00206                                         GtkTreePath       *path,
00207                                         GtkTreeViewColumn *column,
00208                                         gpointer          user_data )
00209 {
00210     main_quit();
00211     return;
00212 }
00213 
00214 static void on_listview_navigate_cursor ( erGtkListView         *er_listview,
00215                                           erGtkListViewKeyPress keycode,
00216                                           gpointer              user_data )
00217 {
00218     GtkTreeSelection* my_selection = gtk_tree_view_get_selection((GtkTreeView*) er_listview);
00219     g_assert( my_selection != NULL ) ;
00220 
00221     // determine new cursor position
00222     switch (keycode)
00223     {
00224         case ERGTK_LIST_VIEW_PRESS_SHORT_DOWN:
00225         case ERGTK_LIST_VIEW_PRESS_LONG_DOWN:
00226             //gtk_widget_grab_focus(GTK_WIDGET(g_calibrate_button));
00227             gtk_tree_selection_unselect_all(my_selection);
00228             break;
00229         default:
00230             LOGPRINTF("illegal erGtkListViewKeyPress [%d]", keycode);
00231             ;  // ignore
00232             break;
00233     }
00234 
00235 }
00236 
00237 static GtkWidget* create_sdcapacity_widgets(GtkBox* parent)
00238 {
00239     // Top level vbox.
00240     GtkWidget* top_level_vbox = gtk_vbox_new(FALSE, 3);
00241     gtk_box_pack_start(parent, top_level_vbox, FALSE, FALSE, 0);
00242 
00243     // The label "Card Storage Capacity".
00244     GtkWidget* label = gtk_label_new(_("SD Card Memory"));
00245     gtk_widget_set_name(label, "irex-normal-text");
00246     gtk_widget_set_size_request(label, -1, -1);
00247     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
00248     gtk_box_pack_start(GTK_BOX(top_level_vbox), label, FALSE, FALSE, 6);
00249 
00250     // The GtkProgressBar.
00251     g_capacity_bar = gtk_progress_bar_new();
00252     // gtk_widget_set_name(g_capacity_bar, "capacity");
00253     gtk_box_pack_start(GTK_BOX(top_level_vbox), g_capacity_bar, FALSE, FALSE, 6);
00254 
00255     // The label "Used Space:"
00256     g_used_space_label = gtk_label_new(NULL);
00257     gtk_widget_set_name(g_used_space_label, "irex-normal-text");
00258     gtk_widget_set_size_request(g_used_space_label, -1, -1);
00259     gtk_misc_set_alignment(GTK_MISC(g_used_space_label), 0.0, 0.0);
00260     gtk_box_pack_start(GTK_BOX(top_level_vbox), g_used_space_label, FALSE, FALSE, 6);
00261 
00262     // The label "Free Space:"
00263     g_free_space_label = gtk_label_new(NULL);
00264     gtk_widget_set_name(g_free_space_label, "irex-normal-text");
00265     gtk_widget_set_size_request(g_free_space_label, -1, -1);
00266     gtk_misc_set_alignment(GTK_MISC(g_free_space_label), 0.0, 0.0);
00267     gtk_box_pack_start(GTK_BOX(top_level_vbox), g_free_space_label, FALSE, FALSE, 6);
00268 
00269 
00270     return top_level_vbox;
00271 }
00272 
00273 static void init_widgets_with_settings()
00274 {
00275    
00276     if ( g_is_updating == FALSE )
00277     {
00278         g_is_updating = TRUE;
00279         update_sd_widgets();
00280         g_is_updating = FALSE; 
00281     }
00282 
00283 }
00284 
00285 static gboolean on_focus_in(GtkWidget* widget, gpointer data )
00286 {
00287     g_assert(widget != NULL ) ;
00288     ergtk_list_view_set_cursor( ERGTK_LIST_VIEW(widget), 0); // row = 0;
00289     return FALSE;
00290 }
00291 
00292 static gboolean on_focus_out(GtkWidget* widget, gpointer data )
00293 {
00294     g_assert(widget != NULL ) ;
00295     GtkTreeSelection* my_selection = gtk_tree_view_get_selection((GtkTreeView*) widget);
00296     g_assert( my_selection != NULL ) ;
00297     gtk_tree_selection_unselect_all(my_selection);
00298     return FALSE;
00299 }
00300 
00301 static void update_sd_widgets()
00302 {
00303 #ifndef WIN32
00304     int           rc;
00305     struct statfs stat;
00306     long          used_blocks;
00307     gdouble       usage;
00308     gchar*        message = NULL ; 
00309 
00310     LOGPRINTF("entry");
00311 
00312     rc = statfs(SD_CARD_MOUNTPOINT, &stat);
00313 
00314     if ( rc == 0  &&  g_sd_card_mounted )
00315     {
00316         // update filesystem usage
00317         used_blocks = stat.f_blocks - stat.f_bfree;
00318         usage       = (gdouble)used_blocks / (gdouble)stat.f_blocks;
00319         /* TRANSLATORS: '%ld' is the percentage of remaining free space on the SD card 
00320          *              Example: "95% remaining"
00321          *              Note: '%%' creates literally a %-sign
00322          */
00323         gchar* progress_text  = g_strdup_printf(_("%ld%% remaining"), (long)(100-(100*usage)));
00324         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(g_capacity_bar), usage);
00325         gtk_widget_show(g_capacity_bar);
00326         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(g_capacity_bar), progress_text);
00327         g_free(progress_text);
00328 
00329         /* TRANSLATORS: '%ld' is the total number of Megabytes
00330          *              Example: "Total: 1024 MB"
00331          */
00332          message = g_strdup_printf(_("Total: %ld MB"), stat.f_blocks / (1048576 / stat.f_bsize));
00333         gtk_label_set_label(GTK_LABEL(g_used_space_label), message);
00334         gtk_widget_show(g_used_space_label);
00335         g_free(message);
00336 
00337         /* TRANSLATORS: '%ld' is the number of Megabytes available
00338          *              Example: "Available: 560 MB"
00339          */
00340          message = g_strdup_printf(_("Available: %ld MB"), stat.f_bfree / (1048576 / stat.f_bsize));
00341         gtk_label_set_label(GTK_LABEL(g_free_space_label), message);
00342         gtk_widget_show(g_free_space_label);
00343         g_free(message);
00344     }
00345     else
00346     {
00347         /* TRANSLATORS: N/A stands for not applicable
00348          */
00349          gtk_label_set_label(GTK_LABEL(g_used_space_label), _("Total: N/A"));
00350         gtk_widget_show(g_capacity_bar);
00351         gtk_label_set_label(GTK_LABEL(g_free_space_label), _("Available: N/A"));
00352         gtk_widget_show(g_free_space_label);
00353 
00354         gchar* progress_text  = g_strdup_printf(_("Unknown remaining"));
00355         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(g_capacity_bar), 0.0);
00356         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(g_capacity_bar), progress_text);
00357         g_free(progress_text);
00358 
00359         gtk_widget_show(g_capacity_bar);
00360     }
00361 
00362 #endif
00363 }
Generated by  doxygen 1.6.2-20100208