flightmode.c

Go to the documentation of this file.
00001 /*
00002  * File Name: flightmode.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 #include <string.h>
00033 #include <stdlib.h>
00034 #include <unistd.h>
00035 #include <stdio.h>
00036 
00037 #include <gtk/gtk.h>
00038 #include <gdk/gdk.h>
00039 #include <glib.h>
00040 
00041 #include <libergtk/ergtk.h>
00042 
00043 #include "i18n.h"
00044 #include "log.h"
00045 #include "ipc.h"
00046 #include "settings.h"
00047 #include "settings_utils.h"
00048 #include "settings_style.h"
00049 #include "flightmode.h"
00050 
00051 
00052 //----------------------------------------------------------------------------
00053 // Type Declarations
00054 //----------------------------------------------------------------------------
00055 
00056 typedef struct
00057 {
00058     int flightmode_mode;
00059 } FlightModeSettings;
00060 
00061 enum
00062 {
00063     FLIGHTMODE_MODE_OFF = 0,
00064     FLIGHTMODE_MODE_ON,
00065     NUM_FLIGHTMODE_MODES,
00066 } FlightModeModes_e;
00067 
00068 
00069 //----------------------------------------------------------------------------
00070 // Global Constants
00071 //----------------------------------------------------------------------------
00072 
00073 
00074 //----------------------------------------------------------------------------
00075 // Static Variables
00076 //----------------------------------------------------------------------------
00077 
00078 static gboolean   g_orig_is_flightmode_enabled = FALSE;
00079 static gboolean   g_cur_is_flightmode_enabled = FALSE;
00080 
00081 static GtkWidget* g_flightmode_radios[NUM_FLIGHTMODE_MODES];
00082 
00083 //============================================================================
00084 // Local Function Definitions
00085 //============================================================================
00086 
00087 static GtkWidget* create_flightmode_widgets         (GtkBox* parent);
00088 
00089 static void on_flightmode_changed                   (GtkWidget *widget, gpointer data);
00090 static void init_widgets_with_settings          (void);
00091 static void on_listview_row_activated ( GtkTreeView       *view,
00092                                         GtkTreePath       *path,
00093                                         GtkTreeViewColumn *column,
00094                                         gpointer          user_data );
00095 
00096 
00097 static void on_listview_navigate_cursor ( erGtkListView         *er_listview,
00098         erGtkListViewKeyPress keycode,
00099         gpointer              user_data );
00100 
00101 static gboolean on_focus_in(GtkWidget* widget, gpointer data );
00102 static gboolean on_focus_out(GtkWidget* widget, gpointer data );
00103 
00104 //============================================================================
00105 // Functions Implementation
00106 //============================================================================
00107 
00108 GtkWidget* create_flightmode_window(GtkWidget* parent)
00109 {
00110     // create top window
00111     GtkWidget* top_window = parent;
00112     gtk_window_maximize(GTK_WINDOW(top_window));
00113     gtk_window_set_resizable(GTK_WINDOW(top_window), FALSE);
00114     gtk_container_set_border_width(GTK_CONTAINER(top_window),WINDOW_BORDER_PADDING  );
00115     gtk_window_set_modal(GTK_WINDOW(top_window), TRUE);
00116 
00117     // top level vbox (vboxtop)
00118     GtkWidget* vboxtop = gtk_vbox_new(FALSE, 3);
00119     gtk_container_add(GTK_CONTAINER(top_window), vboxtop);
00120 
00121     // add header container the title and subtitle of this settings page
00122     create_title(GTK_VBOX(vboxtop), _("Settings"), _("Airplane Mode"));
00123     
00124     // add the back/exit bar below the title 
00125     GtkWidget* view = create_settingsview();
00126     gtk_box_pack_start(GTK_BOX(vboxtop), view, FALSE, FALSE,0 ); 
00127     g_signal_connect(view, "row-activated", G_CALLBACK(on_listview_row_activated), NULL ) ;
00128     g_signal_connect(view, "navigate-cursor", G_CALLBACK(on_listview_navigate_cursor), NULL ) ;
00129     g_signal_connect(view, "focus-in-event", G_CALLBACK(on_focus_in), NULL );
00130     g_signal_connect(view, "focus-out-event", G_CALLBACK(on_focus_out), NULL );
00131 
00132     // add an alignment below the exit bar
00133     GtkWidget* alignment = gtk_alignment_new(0, 0, 1.0, 0.0);
00134     gtk_container_add(GTK_CONTAINER(vboxtop), alignment);
00135 
00136     // introdude a frame in the alignment
00137     GtkWidget* frame = gtk_frame_new(NULL);
00138     gtk_widget_set_name(frame, "irex-margins-frame");
00139     gtk_container_add(GTK_CONTAINER(alignment), frame);
00140     gtk_container_set_border_width(GTK_CONTAINER(frame), 3);
00141 
00142     // alignment in frame 
00143     GtkWidget* alignment2 = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
00144     gtk_container_add(GTK_CONTAINER(frame), alignment2);
00145     gtk_alignment_set_padding(GTK_ALIGNMENT(alignment2),
00146                               WINDOW_TOP_ALT_PADDING,
00147                               WINDOW_BOTTOM_PADDING,
00148                               WINDOW_H_PADDING,
00149                               WINDOW_H_PADDING);
00150    
00151     // vbox in alignment2
00152     GtkWidget* innervbox = gtk_vbox_new(FALSE, SMALL_SPACING);
00153     gtk_container_add(GTK_CONTAINER(alignment2), innervbox);
00154     
00155     // The margin settings section.
00156     create_flightmode_widgets(GTK_BOX(innervbox));
00157 
00158     // Update widget with current settings.
00159     init_widgets_with_settings();
00160 
00161     // back button has initial focus
00162     gtk_widget_grab_focus(view);
00163 
00164     gtk_widget_show_all(top_window);
00165     return top_window;
00166 }
00167 
00168 //============================================================================
00169 // Local Functions Implementation
00170 //============================================================================
00171 
00172 static void on_listview_row_activated ( GtkTreeView       *view,
00173                                         GtkTreePath       *path,
00174                                         GtkTreeViewColumn *column,
00175                                         gpointer          user_data )
00176 {
00177 
00178     // save flightmode if changed
00179     save_flightmode_settings();
00180     main_quit();
00181     return;
00182 }
00183 
00184 static void on_listview_navigate_cursor ( erGtkListView         *er_listview,
00185         erGtkListViewKeyPress keycode,
00186         gpointer              user_data )
00187 {
00188    GtkTreeSelection* my_selection = gtk_tree_view_get_selection((GtkTreeView*) er_listview);
00189   g_assert( my_selection != NULL ) ;
00190 
00191     // determine new cursor position
00192     switch (keycode)
00193     {
00194         case ERGTK_LIST_VIEW_PRESS_SHORT_DOWN:
00195         case ERGTK_LIST_VIEW_PRESS_LONG_DOWN:
00196             gtk_widget_grab_focus(GTK_WIDGET(g_flightmode_radios[0]));
00197             gtk_tree_selection_unselect_all(my_selection);
00198             break;
00199         default:
00200             LOGPRINTF("illegal erGtkListViewKeyPress [%d]", keycode);
00201             ;  // ignore
00202             break;
00203     }
00204 
00205 }
00206 
00207 static gboolean on_focus_in(GtkWidget* widget, gpointer data )
00208 {
00209     g_assert(widget != NULL ) ;
00210     ergtk_list_view_set_cursor( ERGTK_LIST_VIEW(widget), 0); // row = 0;
00211     return FALSE;
00212 }
00213 
00214 static gboolean on_focus_out(GtkWidget* widget, gpointer data )
00215 {
00216     g_assert(widget != NULL ) ;
00217     GtkTreeSelection* my_selection = gtk_tree_view_get_selection((GtkTreeView*) widget);
00218     g_assert( my_selection != NULL ) ;
00219     gtk_tree_selection_unselect_all(my_selection);
00220     return FALSE;
00221 }
00222 
00223 //============================================================================
00224 // Functions Implementation
00225 //============================================================================
00226 
00227 
00228 void load_flightmode_settings()
00229 {
00230     g_orig_is_flightmode_enabled = get_value_bool (GCONF_FLIGHTMODE_MODE);
00231 
00232     g_cur_is_flightmode_enabled = g_orig_is_flightmode_enabled;
00233 }
00234 
00235 
00236 void save_flightmode_settings()
00237 {
00238     if (g_cur_is_flightmode_enabled  != g_orig_is_flightmode_enabled )
00239     {
00240         set_value_bool(GCONF_FLIGHTMODE_MODE, g_cur_is_flightmode_enabled);
00241         g_orig_is_flightmode_enabled = g_cur_is_flightmode_enabled;
00242     }
00243 
00244     LOGPRINTF("Saving flightmode settings, done.");
00245 }
00246 
00247 
00248 //============================================================================
00249 // Local Functions Implementation
00250 //============================================================================
00251 
00252 
00253 static GtkWidget* create_radio_button_item(GtkWidget* group, gchar* title, gchar* subtitle)
00254 {
00255     GtkWidget* radio_button = NULL ;
00256     if ( group == NULL ) 
00257     {
00258         radio_button = gtk_radio_button_new(NULL); 
00259     }
00260     else
00261     {
00262         radio_button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(group)); 
00263 
00264     }
00265     gtk_widget_set_name(radio_button, "irex-settings-checkbutton");
00266 
00267     // alignment in radio button for some padding
00268     GtkWidget* align = gtk_alignment_new(0, 0.0, 1.0, 1.0);
00269     gtk_container_add(GTK_CONTAINER(radio_button), align);
00270     gtk_alignment_set_padding(GTK_ALIGNMENT(align)
00271                                 , 6 // top
00272                                 , 6 // bottom
00273                                 , 6 // left
00274                                 , 6 // right
00275             );
00276  
00277     // vbox in radio button
00278     GtkWidget* radiovbox = gtk_vbox_new( FALSE, 0);
00279     gtk_container_add(GTK_CONTAINER(align), radiovbox);
00280 
00281     // top level label for radio button ( no margins )
00282     GtkWidget* label = gtk_label_new( title );
00283     gtk_widget_set_name(label, "irex-radio-title");
00284     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
00285     gtk_box_pack_start(GTK_BOX(radiovbox), label, FALSE, FALSE, 0);
00286 
00287     // second level label for radio button ( descriptive test )
00288     label = gtk_label_new( subtitle  );
00289     gtk_widget_set_name(label, "irex-radio-subtitle");
00290     gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
00291     gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
00292     gtk_widget_set_size_request(label, SETTINGS_LABEL_MAX_WIDTH - 40 , -1);
00293     gtk_box_pack_start(GTK_BOX(radiovbox), label, FALSE, FALSE, 0);
00294 
00295     return radio_button;
00296 }
00297 
00298 
00299 
00300 // Widget Hierarchy
00301 // |--Top Level vbox
00302 //    |--label ("UDS settings")
00303 //    |--label ("Choose the page margin")
00304 //    |--vbox
00305 //       |--radio
00306 //       |--radio
00307 //       |--radio
00308 //       |--radio
00309 static GtkWidget* create_flightmode_widgets(GtkBox* parent)
00310 {
00311 
00312     // Top level vbox.
00313     GtkWidget* top_level_vbox = gtk_vbox_new(FALSE, ITEM_SPACING);
00314     gtk_box_pack_start(parent, top_level_vbox, FALSE, FALSE, 0);
00315 
00316     // The vbox containing radio buttons.
00317     GtkWidget* vbox = gtk_vbox_new(TRUE, 10);
00318     gtk_box_pack_start(GTK_BOX(top_level_vbox), vbox, FALSE, FALSE, 0);
00319 
00320     int i = 0 ;
00321     for (i=0; i<NUM_FLIGHTMODE_MODES; i++)
00322     {
00323         switch (i)
00324         {
00325             case 0:
00326             {
00327                 // The radio button "Normal margins".
00328                 g_flightmode_radios[i] = create_radio_button_item( NULL,
00329                                            _("Airplane Mode OFF"),
00330                                            _("Wireless is enabled and the device can connect to networks.") );
00331                 
00332                 break;
00333             }
00334             case 1:
00335             {
00336                 // the radio button "No Margins"
00337                 g_flightmode_radios[i] = create_radio_button_item( g_flightmode_radios[0],
00338                                            _("Airplane Mode ON"),
00339                                            _("Wireless is disabled and the device cannot connect to networks.") );
00340                 
00341                 break;
00342             }
00343             default:
00344             {
00345                 g_assert_not_reached();
00346             }
00347         }
00348 
00349         // Add signal handler.
00350         g_signal_connect_after(G_OBJECT(g_flightmode_radios[i]),
00351             "toggled",
00352             G_CALLBACK(on_flightmode_changed),
00353             (gpointer)i);
00354 
00355         gtk_box_pack_start(GTK_BOX(vbox), g_flightmode_radios[i], FALSE, FALSE, 0);
00356     }
00357 
00358     return top_level_vbox;
00359 }
00360 
00361 
00362 static void on_flightmode_changed(GtkWidget *widget, gpointer data)
00363 {
00364     int index = (int)data;
00365 
00366     g_assert( 0 <= index );
00367     g_assert( index < NUM_FLIGHTMODE_MODES );
00368 
00369     gboolean is_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
00370 
00371     if (is_active == TRUE )
00372     {
00373         switch (index)
00374         {
00375             case 0:
00376                 g_cur_is_flightmode_enabled = FALSE;
00377                 break;
00378             case 1:
00379                 g_cur_is_flightmode_enabled = TRUE;
00380                 break;
00381             default:
00382                 g_assert_not_reached();
00383         }
00384     }
00385 }
00386 
00387 static void init_widgets_with_settings()
00388 {
00389     int i;
00390     int item_to_activate = g_cur_is_flightmode_enabled ? 1 : 0;
00391 
00392     for (i = 0; i < NUM_FLIGHTMODE_MODES; i++)
00393     {
00394         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(g_flightmode_radios[i]),
00395                                       (i == item_to_activate) ? TRUE : FALSE );
00396     }
00397 }
00398 
00399 
Generated by  doxygen 1.6.2-20100208