[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r22302 - gnunet-gtk/src/setup
From: |
gnunet |
Subject: |
[GNUnet-SVN] r22302 - gnunet-gtk/src/setup |
Date: |
Tue, 26 Jun 2012 16:47:40 +0200 |
Author: grothoff
Date: 2012-06-26 16:47:40 +0200 (Tue, 26 Jun 2012)
New Revision: 22302
Modified:
gnunet-gtk/src/setup/gnunet-setup-gns.c
Log:
-code cleanup
Modified: gnunet-gtk/src/setup/gnunet-setup-gns.c
===================================================================
--- gnunet-gtk/src/setup/gnunet-setup-gns.c 2012-06-26 14:45:47 UTC (rev
22301)
+++ gnunet-gtk/src/setup/gnunet-setup-gns.c 2012-06-26 14:47:40 UTC (rev
22302)
@@ -167,29 +167,6 @@
/**
- *
- */
-struct UpdateContext
-{
-
- /**
- *
- */
- struct GNUNET_NAMESTORE_RecordData *rd;
-
- /**
- *
- */
- char * name;
-
- /**
- *
- */
- unsigned int rd_count;
-};
-
-
-/**
* Name of our zone as a string.
*/
static char *zone_as_string;
@@ -247,88 +224,144 @@
/**
+ * Context we use for making changes to the namestore.
+ * (closure for 'add_new_records_after_removing_old_records').
+ */
+struct UpdateContext
+{
+
+ /**
+ * Array of records to add.
+ */
+ struct GNUNET_NAMESTORE_RecordData *rd;
+
+ /**
+ * Name under which we should add the records.
+ */
+ char * name;
+
+ /**
+ * Size of the 'rd' array.
+ */
+ unsigned int rd_count;
+};
+
+
+/**
+ * Display an error message for the user.
*
+ * @param title title of the error message
+ * @param emsg error message to show
*/
static void
-check_name_validity_and_commit_remove_proc (void *cls,
+show_error_message (const char *title,
+ const char *emsg)
+{
+ GtkWindow *main_window;
+ GtkDialog *dialog;
+
+ /* FIXME: consider replacing with widget in the main window */
+ main_window = GTK_WINDOW (GNUNET_SETUP_get_object ("GNUNET_setup_dialog"));
+ dialog = GTK_DIALOG(gtk_message_dialog_new (main_window,
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_CLOSE,
+ _("%s\n%s\n"),
+ title,
+ emsg));
+ g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
+ gtk_widget_show_all (GTK_WIDGET(dialog));
+}
+
+
+/**
+ * Function called after we removed the old record. If the
+ * removal was successful, add the new records from the
+ * update context.
+ *
+ * @param cls the 'struct UpdateContext'
+ * @param success GNUNET_SYSERR on failure (including timeout/queue
drop/failure to validate)
+ * GNUNET_NO if content was already there or not found
+ * GNUNET_YES (or other positive value) on success
+ * @param emsg NULL on success, otherwise an error message
+ */
+static void
+add_new_records_after_removing_old_records (void *cls,
int32_t success,
const char *emsg)
{
struct UpdateContext * uc = cls;
unsigned int c;
- if ((GNUNET_OK == success) || (GNUNET_NO == success))
+ switch (success)
{
- for (c = 0; c < uc->rd_count; c++)
- {
- GNUNET_NAMESTORE_record_create(namestore, pkey,
- uc->name, &uc->rd[c],NULL, NULL);
- GNUNET_free ((void *) uc->rd[c].data);
- }
- GNUNET_free (uc->rd);
- GNUNET_free (uc->name);
- GNUNET_free (uc);
- }
- else if (GNUNET_SYSERR == success)
- {
- for (c = 0; c < uc->rd_count; c++)
- GNUNET_free ((void *) uc->rd[c].data);
- GNUNET_free (uc->rd);
- GNUNET_free (uc->name);
- GNUNET_free (uc);
- }
- else
- {
+ case GNUNET_OK:
+ case GNUNET_NO:
+ for (c = 0; c < uc->rd_count; c++)
+ GNUNET_NAMESTORE_record_create (namestore, pkey,
+ uc->name, &uc->rd[c],
+ NULL, NULL);
+ break;
+ case GNUNET_SYSERR:
+ show_error_message (_("Failed to remove record"),
+ emsg);
+ break;
+ default:
GNUNET_break (0);
- GNUNET_free (uc);
+ break;
}
+ for (c = 0; c < uc->rd_count; c++)
+ GNUNET_free ((void *) uc->rd[c].data);
+ GNUNET_free (uc->rd);
+ GNUNET_free (uc->name);
+ GNUNET_free (uc);
}
/**
+ * Check that the data at the given 'path' (see
gtk_tree_model_get_iter_from_string)
+ * is valid and if so commit it after removing the old data.
*
+ * @param path path identifying the new record (FIXME: use GtkTreeIter
instead!?)
+ * @param oldname name of the old record, NULL if this is a fresh name
*/
static void
-check_name_validity_and_commit (gchar *path, char * oldname)
+check_name_validity_and_commit (const gchar *path, const char * oldname)
{
GtkTreeIter it;
- GtkTreeIter parent;
- int records;
- int children;
- int append_pseu;
+ GtkTreeIter parent; /* parent record with the 'name' */
+ char * name; /* name of the records */
+ struct GNUNET_NAMESTORE_RecordData *rd;
+ unsigned int records; /* number of records in 'rd' */
+ int children; /* number of records below 'parent' */
+ int append_pseu; /* do we need to create a '+' PSEU record? */
+ struct UpdateContext * uc;
int c;
- int valid = GNUNET_YES;
- char * name;
- void * data;
- size_t data_size;
- const gchar * pseu;
- char *n_name;
- int n_type;
- gboolean n_public;
- char *n_exp_color;
- guint64 n_exp_time;
- char *n_exp_str;
- gboolean n_is_relative;
- char *n_value;
- char *n_value_color;
gtk_tree_model_get_iter_from_string(tm, &it, path);
-
- if (FALSE == gtk_tree_model_iter_parent (tm, &parent, &it))
+ if (! gtk_tree_model_iter_parent (tm, &parent, &it))
+ {
+ if (NULL != oldname)
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Name of existing record `%s' was changed, moving associated
records\n",
+ oldname);
parent = it;
-
+ }
+ gtk_tree_model_get (tm, &parent,
+ GNS_TREESTORE_COL_NAME, &name,
+ -1);
children = gtk_tree_model_iter_n_children (tm, &parent);
if (children < 1)
{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Changed name `%s' has no associated records, not committing to
namestore\n",
+ name);
+ g_free (name);
return;
}
-
- gtk_tree_model_get(tm, &parent,
- GNS_TREESTORE_COL_NAME, &name,
- -1);
-
if (0 == strcmp (name, ROOT_STR))
{
+ /* FIXME: only if this is our 'primary' zone (not for private/shorten
zones!) */
/* We have to append PSEU RECORD */
append_pseu = GNUNET_YES;
records = children + 1;
@@ -339,51 +372,42 @@
records = children;
}
- struct GNUNET_NAMESTORE_RecordData *rd = GNUNET_malloc (records * sizeof
(struct GNUNET_NAMESTORE_RecordData));
+ rd = GNUNET_malloc (records * sizeof (struct GNUNET_NAMESTORE_RecordData));
+ GNUNET_assert (gtk_tree_model_iter_children (tm, &it, &parent));
- if (FALSE == gtk_tree_model_iter_children (tm, &it, &parent))
- return;
-
for (c = 0; c < children; c++)
{
- gtk_tree_model_get(tm, &it,
- GNS_TREESTORE_COL_NAME, &n_name,
- GNS_TREESTORE_COL_RECORD_TYPE, &n_type,
- GNS_TREESTORE_COL_IS_PUBLIC, &n_public,
- GNS_TREESTORE_COL_EXP_TIME_COLOR, &n_exp_color,
- GNS_TREESTORE_COL_EXP_TIME, &n_exp_time,
- GNS_TREESTORE_COL_EXP_TIME_IS_REL, &n_is_relative,
- GNS_TREESTORE_COL_EXP_TIME_AS_STR, &n_exp_str,
- GNS_TREESTORE_COL_VAL_AS_STR, &n_value,
- GNS_TREESTORE_COL_VAL_COLOR, &n_value_color,
- -1);
- /* valid name */
- if (NULL == n_name)
- valid = GNUNET_NO;
- else
- {
- if (GNUNET_SYSERR == GNUNET_NAMESTORE_check_name (n_name))
- valid = GNUNET_NO;
- }
+ gchar *n_name;
+ gint n_type;
+ gboolean n_public;
+ guint64 n_exp_time;
+ gboolean n_is_relative;
+ gchar *n_value;
+ void * data;
+ size_t data_size;
- /* valid record type */
- if (0 == n_type)
- valid = GNUNET_NO;
-
- /* valid expiration */
- if ((n_exp_color != NULL) || (NULL == n_exp_str) || (0 == n_exp_time))
- valid = GNUNET_NO;
-
- /* valid value */
- if ((n_value_color != NULL) || (NULL == n_value))
- valid = GNUNET_NO;
- if (NULL != n_value)
+ gtk_tree_model_get (tm, &it,
+ GNS_TREESTORE_COL_NAME, &n_name,
+ GNS_TREESTORE_COL_RECORD_TYPE, &n_type,
+ GNS_TREESTORE_COL_IS_PUBLIC, &n_public,
+ GNS_TREESTORE_COL_EXP_TIME, &n_exp_time,
+ GNS_TREESTORE_COL_EXP_TIME_IS_REL, &n_is_relative,
+ GNS_TREESTORE_COL_VAL_AS_STR, &n_value,
+ -1);
+ if ( (NULL == n_name) ||
+ (GNUNET_SYSERR == GNUNET_NAMESTORE_check_name (n_name)) ||
+ (0 == n_type) ||
+ (0 == n_exp_time) ||
+ (NULL == n_value) ||
+ (GNUNET_OK != GNUNET_NAMESTORE_string_to_value(n_type, n_value, &data,
&data_size)) )
{
- if (GNUNET_OK != GNUNET_NAMESTORE_string_to_value(n_type, n_value,
&data, &data_size))
- valid = GNUNET_NO;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Invalid record, skipping\n");
+ records--;
+ children--;
+ c--;
}
-
- if (GNUNET_YES == valid)
+ else
{
if (FALSE == n_public)
rd[c].flags = GNUNET_NAMESTORE_RF_AUTHORITY |
GNUNET_NAMESTORE_RF_PRIVATE;
@@ -396,113 +420,114 @@
memcpy ((void *) rd[c].data, data, data_size);
}
g_free (n_name);
- g_free (n_exp_color);
- g_free (n_exp_str);
g_free (n_value);
- g_free (n_value_color);
-
- if (FALSE == gtk_tree_model_iter_next (tm, &it))
- break;
+ GNUNET_assert (gtk_tree_model_iter_next (tm, &it));
}
- if (GNUNET_NO == valid)
+ if (GNUNET_YES == append_pseu)
{
- for (c = 0; c < children; c++)
- GNUNET_free_non_null ((void *) rd[c].data);
- GNUNET_free_non_null (rd);
- }
- else
- {
+ GtkEntry * entry;
+ const gchar * pseu;
- if (GNUNET_YES == append_pseu)
+ /* Append PSEU record */
+ GNUNET_assert (children == (records -1));
+ entry = GTK_ENTRY (GNUNET_SETUP_get_object
("GNUNET_SETUP_gns_pseu_entry"));
+ pseu = gtk_entry_get_text (GTK_ENTRY(entry));
+ if ( (NULL == pseu) ||
+ (0 == strcmp (PSEU_EMPTY_STR, pseu)) ||
+ (0 == strcmp ("", pseu)) )
{
- GNUNET_assert (children == (records -1));
-
- /* Append PSEU record */
- GtkEntry * entry = GTK_ENTRY (GNUNET_SETUP_get_object
("GNUNET_SETUP_gns_pseu_entry"));
- pseu = gtk_entry_get_text (GTK_ENTRY(entry));
- if ((NULL != pseu) && (0 != strcmp (PSEU_EMPTY_STR, pseu)) && (0 !=
strcmp ("", pseu)))
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "No zone pseudonym given, not creating PSEU record\n");
+ records--;
+ }
+ else
+ {
+ if (GNUNET_OK != GNUNET_NAMESTORE_string_to_value
(GNUNET_NAMESTORE_TYPE_PSEU,
+ pseu,
+ (void **) &rd[records
- 1].data,
+ &rd[records -
1].data_size))
{
- if (GNUNET_OK !=
GNUNET_NAMESTORE_string_to_value(GNUNET_NAMESTORE_TYPE_PSEU,
- pseu,
- (void **) &rd[records - 1].data,
- &rd[records - 1].data_size))
- {
- GNUNET_break (0);
- for (c = 0; c < records; c++)
- GNUNET_free_non_null ((void *) rd[c].data);
- GNUNET_free_non_null (rd);
- }
+ show_error_message (_("Invalid pseudonym specified for zone"),
+ pseu);
+ records--;
+ }
+ else
+ {
rd[records - 1].record_type = GNUNET_NAMESTORE_TYPE_PSEU;
rd[records - 1].expiration_time = UINT64_MAX;
rd[records - 1].flags = GNUNET_NAMESTORE_RF_AUTHORITY |
GNUNET_NAMESTORE_RF_NONE;
}
- else
- {
- GNUNET_break (0);
- }
}
+ }
+ if (0 == records)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "No valid records remaining, not storing to namestore\n");
+ GNUNET_free (rd);
+ return;
+ }
- /* Remove old entries */
- struct UpdateContext * uc = GNUNET_malloc (sizeof (struct UpdateContext));
- uc->rd = rd;
- uc->rd_count = records;
- uc->name = strdup (name);
- if (oldname != NULL)
- GNUNET_NAMESTORE_record_remove (namestore, pkey, oldname, NULL,
&check_name_validity_and_commit_remove_proc, uc);
- else
- GNUNET_NAMESTORE_record_remove (namestore, pkey, name, NULL,
&check_name_validity_and_commit_remove_proc, uc);
- g_free (name);
- }
+ /* Store update information in context and remove old entries */
+ uc = GNUNET_malloc (sizeof (struct UpdateContext));
+ uc->rd = rd;
+ uc->rd_count = records;
+ uc->name = name;
+ GNUNET_NAMESTORE_record_remove (namestore,
+ pkey,
+ (NULL != oldname) ? oldname : name,
+ NULL,
+ &add_new_records_after_removing_old_records,
uc);
}
/**
- *
+ * Closure for 'check_name_validity_and_remove_proc'.
*/
-struct Remove_Context
+struct RemoveContext
{
/**
- *
+ * Path for 'gtk_tree_model_get_iter_from_string' for removing
+ * the record from the tree view IF the operation was successful.
+ * FIXME: replace with a 'GtkTreeIter'?
*/
char *path;
};
/**
+ * We tried to remove a record from the namestore, if we were
+ * successful, also remove it from the model.
*
+ * @param cls the 'struct RemoveContext'
+ * @param success GNUNET_SYSERR on failure (including timeout/queue
drop/failure to validate)
+ * GNUNET_NO if content was already there or not found
+ * GNUNET_YES (or other positive value) on success
+ * @param emsg NULL on success, otherwise an error message
*/
static void
-check_name_validity_and_remove_proc (void *cls,
+update_treemodel_after_remove (void *cls,
int32_t success,
const char *emsg)
{
- struct Remove_Context *rcc = cls;
- GtkDialog *dialog;
+ struct RemoveContext *rcc = cls;
GtkTreeIter it;
- if (GNUNET_SYSERR == success)
+ switch (success)
{
- const char * message = _("Record could not be deleted:");
- GtkWindow *main_window;
-
- main_window = GTK_WINDOW (GNUNET_SETUP_get_object ("GNUNET_setup_dialog"));
- dialog = GTK_DIALOG(gtk_message_dialog_new (main_window,
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_ERROR,
- GTK_BUTTONS_CLOSE,
- _("%s\n%s\n"),
- message,
- emsg));
-
- g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy),
NULL);
- gtk_widget_show_all (GTK_WIDGET(dialog));
- }
- else
- {
+ case GNUNET_YES:
+ case GNUNET_NO:
gtk_tree_model_get_iter_from_string(tm, &it, rcc->path);
gtk_tree_store_remove (ts, &it);
+ break;
+ case GNUNET_SYSERR:
+ show_error_message (_("Failed to remove record"),
+ emsg);
+ break;
+ default:
+ GNUNET_break (0);
+ break;
}
GNUNET_free (rcc->path);
GNUNET_free (rcc);
@@ -510,7 +535,9 @@
/**
- *
+ * FIXME...
+ *
+ * @param path
*/
static void
check_name_validity_and_remove (gchar *path)
@@ -520,7 +547,7 @@
char *name;
int valid = GNUNET_YES;
struct GNUNET_NAMESTORE_RecordData rd;
- struct Remove_Context *rcc;
+ struct RemoveContext *rcc;
char *n_name;
int n_type;
@@ -583,9 +610,9 @@
rd.data = GNUNET_malloc(rd.data_size);
memcpy ((void *) rd.data, n_value, rd.data_size);
- rcc = GNUNET_malloc(sizeof (struct Remove_Context));
+ rcc = GNUNET_malloc(sizeof (struct RemoveContext));
rcc->path = strdup (path);
- GNUNET_NAMESTORE_record_remove (namestore, pkey, name, &rd,
&check_name_validity_and_remove_proc, rcc);
+ GNUNET_NAMESTORE_record_remove (namestore, pkey, name, &rd,
&update_treemodel_after_remove, rcc);
GNUNET_free ((void *) rd.data);
}
else
@@ -602,9 +629,9 @@
else if (0 != strcmp (name, ROOT_STR))
{
/* Removing the whole name record */
- rcc = GNUNET_malloc(sizeof (struct Remove_Context));
+ rcc = GNUNET_malloc(sizeof (struct RemoveContext));
rcc->path = strdup (path);
- GNUNET_NAMESTORE_record_remove (namestore, pkey, name, NULL,
&check_name_validity_and_remove_proc, rcc);
+ GNUNET_NAMESTORE_record_remove (namestore, pkey, name, NULL,
&update_treemodel_after_remove, rcc);
}
g_free (name);
}
@@ -762,7 +789,9 @@
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;
@@ -802,13 +831,19 @@
if (GNUNET_SYSERR == check_time(text))
{
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Invalid time `%s'\n"), text);
GNUNET_break (0);
return GNUNET_TIME_UNIT_ZERO_ABS;
}
int count = SSCANF (text, "%02d/%02d/%04d %02d:%02d", &t_mon, &t_day,
&t_year, &t_hrs, &t_min);
if ((EOF == count) || (5 != count))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Invalid time `%s', returning 0\n"), text);
return GNUNET_TIME_UNIT_ZERO_ABS;
+ }
time.tm_mon = (t_mon - 1);
time.tm_mday = t_day;
@@ -847,6 +882,9 @@
if (NULL == new_text)
return; /* can this happen? */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "New expiration time: `%s'\n",
+ new_text);
gtk_tree_model_get_iter_from_string(tm, &it, path);
gtk_tree_model_get(tm, &it,
GNS_TREESTORE_COL_EXP_TIME_AS_STR, &old_text,
@@ -1050,14 +1088,24 @@
GtkTreeIter it;
GtkMenu *popup;
GtkTreeSelection * ts;
- int not_dummy;
+ gboolean not_dummy;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Considering creating popup menu...\n");
ts = gtk_tree_view_get_selection(tv);
if (! gtk_tree_selection_get_selected (ts, &tm, &it))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "No row selected\n");
return FALSE;
+ }
gtk_tree_model_get(tm, &it, GNS_TREESTORE_COL_NOT_DUMMY_ROW, ¬_dummy, -1);
- if (GNUNET_NO == not_dummy)
+ if (! not_dummy)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Dummy row selected\n");
return FALSE;
+ }
popup = GTK_MENU (GNUNET_SETUP_get_object
("GNUNET_setup_gns_delete_popup_menu"));
gtk_widget_show_all (GTK_WIDGET(popup));
gtk_menu_popup(popup, NULL, NULL, NULL, NULL, 0, 0);
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r22302 - gnunet-gtk/src/setup,
gnunet <=