gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r20677 - in gnunet-gtk: contrib src/gns


From: gnunet
Subject: [GNUnet-SVN] r20677 - in gnunet-gtk: contrib src/gns
Date: Thu, 22 Mar 2012 17:27:34 +0100

Author: wachs
Date: 2012-03-22 17:27:34 +0100 (Thu, 22 Mar 2012)
New Revision: 20677

Modified:
   gnunet-gtk/contrib/gnunet_gns_gtk_main_window.glade
   gnunet-gtk/src/gns/gnunet-gns-gtk.c
   gnunet-gtk/src/gns/gnunet-gns-gtk_zone.c
Log:
- rewritten time management


Modified: gnunet-gtk/contrib/gnunet_gns_gtk_main_window.glade
===================================================================
--- gnunet-gtk/contrib/gnunet_gns_gtk_main_window.glade 2012-03-22 13:15:22 UTC 
(rev 20676)
+++ gnunet-gtk/contrib/gnunet_gns_gtk_main_window.glade 2012-03-22 16:27:34 UTC 
(rev 20677)
@@ -26,7 +26,7 @@
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="use_action_appearance">False</property>
-        <property name="label" translatable="yes">Set expiration 1 
day</property>
+        <property name="label" translatable="yes">Record expires in 1 
day</property>
         <property name="use_underline">True</property>
         <signal name="activate" 
handler="GNUNET_GNS_GTK_main_treeview_popup_menu_exp1d_cb" swapped="no"/>
       </object>
@@ -36,7 +36,7 @@
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="use_action_appearance">False</property>
-        <property name="label" translatable="yes">Set expiration 1 
week</property>
+        <property name="label" translatable="yes">Record expires in 1 
week</property>
         <property name="use_underline">True</property>
         <signal name="activate" 
handler="GNUNET_GNS_GTK_main_treeview_popup_menu_exp1w_cb" swapped="no"/>
       </object>
@@ -46,7 +46,7 @@
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="use_action_appearance">False</property>
-        <property name="label" translatable="yes">Set expiration 1 
year</property>
+        <property name="label" translatable="yes">Record expires in 1 
year</property>
         <property name="use_underline">True</property>
         <signal name="activate" 
handler="GNUNET_GNS_GTK_main_treeview_popup_menu_exp1y_cb" swapped="no"/>
       </object>
@@ -56,7 +56,7 @@
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="use_action_appearance">False</property>
-        <property name="label" translatable="yes">Set expiration end of 
time</property>
+        <property name="label" translatable="yes">Record never 
expires</property>
         <property name="use_underline">True</property>
         <signal name="activate" 
handler="GNUNET_GNS_GTK_main_treeview_popup_menu_expinf_cb" swapped="no"/>
       </object>

Modified: gnunet-gtk/src/gns/gnunet-gns-gtk.c
===================================================================
--- gnunet-gtk/src/gns/gnunet-gns-gtk.c 2012-03-22 13:15:22 UTC (rev 20676)
+++ gnunet-gtk/src/gns/gnunet-gns-gtk.c 2012-03-22 16:27:34 UTC (rev 20677)
@@ -247,8 +247,6 @@
   /* FIXME: move to new zone 'filename' */
   fprintf (stderr, "Got zone `%s'\n", filename);
 
-  fork();
-
   GNUNET_free (filename);  
 }
 

Modified: gnunet-gtk/src/gns/gnunet-gns-gtk_zone.c
===================================================================
--- gnunet-gtk/src/gns/gnunet-gns-gtk_zone.c    2012-03-22 13:15:22 UTC (rev 
20676)
+++ gnunet-gtk/src/gns/gnunet-gns-gtk_zone.c    2012-03-22 16:27:34 UTC (rev 
20677)
@@ -28,6 +28,8 @@
 
 #define NEW_NAME_STR "<new name>"
 #define NEW_RECORD_STR "<new record>"
+#define EXPIRE_NEVER_STRING "never"
+#define EXPIRE_INVALID_STRING "invalid"
 
 enum TREESTORE_COLUMNS
 {
@@ -459,7 +461,96 @@
   check_name_validity_and_commit (gns, path);
 }
 
+static
+char * convert_time_to_string (struct GNUNET_TIME_Absolute t)
+{
+  time_t tt;
+  struct tm *time;
+  char *ret;
 
+  if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
+      return GNUNET_strdup (_(EXPIRE_NEVER_STRING));
+  if (t.abs_value == GNUNET_TIME_UNIT_ZERO_ABS.abs_value)
+      return GNUNET_strdup (_(EXPIRE_INVALID_STRING));
+
+  tt = t.abs_value / 1000;
+  time = localtime (&tt);
+
+  GNUNET_asprintf(&ret, "%02u/%02u/%04u %02u:%02u",time->tm_mon, 
time->tm_mday, 1900 + time->tm_year, time->tm_hour, time->tm_min);
+  return ret;
+}
+
+static
+int check_time (const char * text)
+{
+  unsigned int t_mon;
+  unsigned int t_day;
+  unsigned int t_year;
+  unsigned int t_hrs;
+  unsigned int t_min;
+
+  int count = SSCANF (text, "%02u/%02u/%04u %02u:%02u", &t_mon, &t_day, 
&t_year, &t_hrs, &t_min);
+  if ((EOF == count) || (5 != count))
+    return GNUNET_SYSERR;
+
+  if (t_mon > 12)
+    return GNUNET_SYSERR;
+  if (t_day > 31)
+    return GNUNET_SYSERR;
+  if (t_hrs > 24)
+    return GNUNET_SYSERR;
+  if (t_min > 59)
+      return GNUNET_SYSERR;
+
+  return GNUNET_OK;
+}
+
+static
+const struct GNUNET_TIME_Absolute convert_string_to_abs_time (const char * 
text)
+{
+  static struct GNUNET_TIME_Absolute abs_t;
+  struct tm time;
+  time_t t;
+
+  int t_mon;
+  int t_day;
+  int t_year;
+  int t_hrs;
+  int t_min;
+
+  GNUNET_assert (NULL != text);
+
+  if (0 == strcmp(text, EXPIRE_NEVER_STRING))
+    return GNUNET_TIME_absolute_get_forever();
+
+  memset (&time, '\0', sizeof (struct tm));
+
+  if (GNUNET_SYSERR == check_time(text))
+  {
+    GNUNET_break (0);
+    return GNUNET_TIME_absolute_get_zero();
+  }
+
+  int count = SSCANF (text, "%02d/%02d/%04d %02d:%02d", &t_mon, &t_day, 
&t_year, &t_hrs, &t_min);
+  if ((EOF == count) || (5 != count))
+    return GNUNET_TIME_absolute_get_zero();
+
+  time.tm_mon = (t_mon - 1);
+  time.tm_mday = t_day;
+  time.tm_year = t_year - 1900;
+  time.tm_hour = (t_hrs);
+  time.tm_min = t_min;
+
+  t = mktime (&time);
+  if (-1 == t)
+    return GNUNET_TIME_absolute_get_zero();
+
+  abs_t.abs_value = t * 1000;
+
+  return abs_t;
+}
+
+
 /**
  * The user has edited a 'expiration' cell.  Update the model.
  *
@@ -476,11 +567,10 @@
 {
   struct GNUNET_GNS_Context * gns = user_data;
   GtkTreeIter it;
-  struct GNUNET_TIME_Relative reltime;
   struct GNUNET_TIME_Absolute abstime;
   gboolean is_rel;
-  char * time = new_text;
-  char * old_text;
+  char *time = new_text;
+  char *old_text;
 
   if ((NULL != new_text))
   {
@@ -491,14 +581,15 @@
                        -1);
     if (0 == strcmp(new_text, old_text))
       return;
-    if ((0 == strcmp(new_text,"")) || (0 == strcmp(new_text,"end of time")))
+
+    if ((0 == strcmp(new_text,"")) || (0 == 
strcmp(new_text,EXPIRE_NEVER_STRING)))
     {
-      time = "end of time";
+      time = EXPIRE_NEVER_STRING;
       abstime = GNUNET_TIME_absolute_get_forever();
     }
     else
     {
-      if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative (time, 
&reltime))
+      if (GNUNET_SYSERR == check_time(new_text))
       {
         gtk_tree_store_set (gns->ts, &it,
                             TREE_COL_EXP_TIME_AS_STR, new_text,
@@ -511,17 +602,15 @@
       /* TODO: fix this when we have relative time */
       if (TRUE == is_rel)
       {
-        abstime = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), 
reltime);
+        abstime = convert_string_to_abs_time(new_text);
       }
       else
       {
-        abstime = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), 
reltime);
+        abstime = convert_string_to_abs_time(new_text);
       }
     }
-
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New text for `%s' is `%s'\n", path, 
GNUNET_STRINGS_absolute_time_to_string(abstime));
     gtk_tree_store_set (gns->ts, &it,
-                        TREE_COL_EXP_TIME_AS_STR, 
GNUNET_STRINGS_absolute_time_to_string(abstime),
+                        TREE_COL_EXP_TIME_AS_STR, new_text,
                         TREE_COL_EXP_TIME, abstime.abs_value,
                         TREE_COL_EXP_TIME_COLOR, NULL,
                         -1);
@@ -595,7 +684,6 @@
   GtkTreeIter child;
   GtkTreeModel *tm = GTK_TREE_MODEL(gns->ts);
   int not_dummy;
-  int children;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New text for `%s' is `%s'\n", path, 
new_text);
   if ((0 == strcmp (new_text, NEW_NAME_STR)) || (0 == strcmp (new_text, "")))
@@ -696,13 +784,14 @@
   return TRUE;
 }
 
-void set_exp (struct GNUNET_GNS_Context *gns, char * exp)
+void set_relative_expiration_time (struct GNUNET_GNS_Context *gns, struct 
GNUNET_TIME_Relative reltime)
 {
   GtkTreeIter it;
   GtkTreeIter parent;
   int not_dummy;
   gboolean has_parent;
   GtkCellRendererText *renderer;
+  struct GNUNET_TIME_Absolute abstime;
 
   char *path;
 
@@ -718,16 +807,19 @@
   /* Has parent? */
   has_parent = gtk_tree_model_iter_parent (tm, &parent, &it);
 
-  if (TRUE == has_parent)
-  {
-    /* this is a single record */
-    renderer = GTK_CELL_RENDERER_TEXT((gtk_builder_get_object (gns->builder, 
"GNUNET_GNS_GTK_name_cellrenderertext")));
-    path = gtk_tree_model_get_string_from_iter (tm, &it);
-    GNUNET_GNS_GTK_expiration_cellrenderertext_edited_cb (renderer,
-                                                          path,
-                                                          exp,
-                                                          gns);
-  }
+  if (FALSE == has_parent)
+    return;
+
+  abstime = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), reltime);
+
+  /* this is a single record */
+  renderer = GTK_CELL_RENDERER_TEXT((gtk_builder_get_object (gns->builder, 
"GNUNET_GNS_GTK_name_cellrenderertext")));
+  path = gtk_tree_model_get_string_from_iter (tm, &it);
+  GNUNET_GNS_GTK_expiration_cellrenderertext_edited_cb (renderer,
+                                                        path,
+                                                        convert_time_to_string 
(abstime),
+                                                        gns);
+
 }
 
 
@@ -735,7 +827,7 @@
 GNUNET_GNS_GTK_main_treeview_popup_menu_exp1d_cb (GtkWidget *widget,
                                             gpointer user_data)
 {
-  set_exp (user_data, "1 d");
+  set_relative_expiration_time (user_data, GNUNET_TIME_UNIT_DAYS);
   return TRUE;
 }
 
@@ -744,7 +836,7 @@
 GNUNET_GNS_GTK_main_treeview_popup_menu_exp1w_cb (GtkWidget *widget,
                                             gpointer user_data)
 {
-  set_exp (user_data, "7 d");
+  set_relative_expiration_time (user_data, GNUNET_TIME_UNIT_WEEKS);
   return TRUE;
 }
 
@@ -752,7 +844,7 @@
 GNUNET_GNS_GTK_main_treeview_popup_menu_exp1y_cb (GtkWidget *widget,
                                             gpointer user_data)
 {
-  set_exp (user_data, "1 a");
+  set_relative_expiration_time (user_data, GNUNET_TIME_UNIT_YEARS);
   return TRUE;
 }
 
@@ -760,7 +852,7 @@
 GNUNET_GNS_GTK_main_treeview_popup_menu_expinf_cb (GtkWidget *widget,
                                             gpointer user_data)
 {
-  set_exp (user_data, "end of time");
+  set_relative_expiration_time (user_data, GNUNET_TIME_UNIT_FOREVER_REL);
   return TRUE;
 }
 
@@ -928,13 +1020,13 @@
       struct GNUNET_TIME_Absolute exp_abs;
       exp_abs = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), rel_time);
       exp_t = exp_abs.abs_value;
-      exp = GNUNET_STRINGS_absolute_time_to_string (exp_abs);
+      exp = convert_time_to_string (exp_abs);
     }
     else
     {
       struct GNUNET_TIME_Absolute exp_abs = rd[c].expiration;
       exp_t = exp_abs.abs_value;
-      exp = GNUNET_STRINGS_absolute_time_to_string (exp_abs);
+      exp = convert_time_to_string (exp_abs);
     }
     /* value */
     val = GNUNET_NAMESTORE_value_to_string (rd[c].record_type,




reply via email to

[Prev in Thread] Current Thread [Next in Thread]