"Fossies" - the Fresh Open Source Software Archive

Member "etherape-0.9.18/src/ui_utils.c" (14 Apr 2018, 4572 Bytes) of package /linux/privat/etherape-0.9.18.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ui_utils.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.9.17_vs_0.9.18.

    1 /* EtherApe
    2  * Copyright (C) 2001 Juan Toledo, Riccardo Ghetta
    3  *
    4  * This program is free software; you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation; either version 2 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
   16  */
   17 
   18 #ifdef HAVE_CONFIG_H
   19 #include <config.h>
   20 #endif
   21 #include <pwd.h>
   22 
   23 #include "appdata.h"
   24 #include "ui_utils.h"
   25 #include <pcap.h> /*JTC*/
   26 
   27 /* 
   28   
   29   Helper functions 
   30 
   31 */
   32 
   33 /* registers the named glade widget on the specified object */
   34 void register_glade_widget(GtkBuilder *bldr, GtkWidget *tgt, const gchar *widgetName)
   35 {
   36   GtkWidget *widget;
   37   widget = GTK_WIDGET(gtk_builder_get_object(bldr, widgetName));
   38   g_assert(widget);
   39   g_object_set_data(G_OBJECT(tgt), widgetName, widget);
   40 }
   41 
   42 void update_gtklabel(GtkWidget *window, const gchar *lblname, const gchar *value)
   43 {
   44   GtkLabel *lbl = GTK_LABEL (g_object_get_data (G_OBJECT (window), lblname));
   45   gint start, end;
   46   gboolean sel = gtk_label_get_selection_bounds (lbl, &start, &end);
   47   gtk_label_set_text (lbl, value);
   48   if (sel)
   49     gtk_label_select_region (lbl, start, end);
   50 }
   51 
   52 
   53 void show_widget(GtkWidget *window, const gchar *lblname)
   54 {
   55   GtkWidget *widget = g_object_get_data (G_OBJECT (window), lblname);
   56   gtk_widget_show(widget);
   57 }
   58 void hide_widget(GtkWidget *window, const gchar *lblname)
   59 {
   60   GtkWidget *widget = g_object_get_data (G_OBJECT (window), lblname);
   61   gtk_widget_hide(widget);
   62 }
   63 
   64 
   65 /* creates a new text column with a specific title, column number colno and
   66  * adds it to treeview gv.  If r_just true the column is right justified */
   67 void create_add_text_column(GtkTreeView *gv, const gchar *title, int colno, 
   68                             gboolean r_just)
   69 {
   70   GtkTreeViewColumn *gc;
   71   GtkCellRenderer *gr;
   72 
   73   gr = gtk_cell_renderer_text_new ();
   74   if (r_just)
   75     g_object_set (G_OBJECT (gr), "xalign", 1.0, NULL);
   76   
   77   gc = gtk_tree_view_column_new_with_attributes(title, gr, "text", colno, NULL);
   78   g_object_set (G_OBJECT (gc), "resizable", TRUE, 
   79                                "reorderable", TRUE, 
   80                                NULL);
   81   gtk_tree_view_column_set_sort_column_id(gc, colno);
   82   gtk_tree_view_append_column (gv, gc);
   83 }
   84 
   85 /* returns a newly allocated string with a formatted traffic  */
   86 gchar *traffic_to_str (gdouble traffic, gboolean is_speed)
   87 {
   88   gchar *str;
   89   if (is_speed)
   90     {
   91       if (traffic > 1000000000.0)
   92         str = g_strdup_printf ("%.2f Gbps", traffic / 1000000000.0);
   93       else if (traffic > 1000000)
   94         str = g_strdup_printf ("%.2f Mbps", traffic / 1000000);
   95       else if (traffic > 1000)
   96     str = g_strdup_printf ("%.2f Kbps", traffic / 1000);
   97       else
   98     str = g_strdup_printf ("%.0f bps", traffic);
   99     }
  100   else
  101     {
  102       /* Debug code for sanity check */
  103       if (traffic && traffic < 1)
  104     g_warning ("Ill traffic value in traffic_to_str");
  105 
  106       if (traffic > 1024.0 * 1024.0 * 1024.0)
  107         str = g_strdup_printf ("%.2f Gbytes", traffic / (1024.0*1024.0*1024.0));
  108       else if (traffic > 1024 * 1024)
  109         str = g_strdup_printf ("%.2f Mbytes", traffic / 1024 / 1024);
  110       else if (traffic > 1024)
  111     str = g_strdup_printf ("%.2f Kbytes", traffic / 1024);
  112       else
  113     str = g_strdup_printf ("%.0f bytes", traffic);
  114     }
  115 
  116   return str;
  117 }               /* traffic_to_str */
  118 
  119 /* register/get a treeview to/from a window */
  120 void register_treeview(GtkWidget *window, GtkTreeView *gv)
  121 {
  122   g_assert(window);
  123   g_object_set_data ( G_OBJECT(window), "EA_gv", gv);
  124 }
  125 GtkTreeView *retrieve_treeview(GtkWidget *window)
  126 {
  127   if (!window)
  128     return NULL;
  129   return GTK_TREE_VIEW(g_object_get_data ( G_OBJECT(window), "EA_gv"));
  130 }
  131 
  132 const char *get_home_dir(void)
  133 {
  134   char *env_value;
  135   static const char *home = NULL;
  136   struct passwd *pwd;
  137 
  138   /* Return the cached value, if available */
  139   if (home)
  140     return home;
  141 
  142   env_value = getenv ("HOME");
  143 
  144   if (env_value)
  145     {
  146       home = env_value;
  147     }
  148   else
  149     {
  150       pwd = getpwuid(getuid ());
  151       if (pwd != NULL)
  152     {
  153       /* This is cached, so we don't need to worry
  154          about allocating multiple ones of them. */
  155       home = g_strdup (pwd->pw_dir);
  156     }
  157       else
  158     home = "/tmp";
  159     }
  160 
  161   return home;
  162 }