]> saetta.ns0.it Git - libgtkform/commitdiff
Added command line debug arguments.
authorAndrea Zagli <azagli@libero.it>
Sat, 2 Apr 2011 09:39:22 +0000 (11:39 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 2 Apr 2011 09:39:22 +0000 (11:39 +0200)
src/form.c
src/form.h
test/from_xml_with_db.c

index 4fe5056cf8564f89f86b19a2fd21876ae726e9f4..368d5cca974898ce7817e8372eceee753d3d5441 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 #include "widgettextview.h"
 
 
+static guint debug;
+static gchar *log_file;
+
+static GOptionEntry entries[] = 
+{
+       { "gtkform-debug-level", 0, 0, G_OPTION_ARG_INT, &debug, "Sets the debug level", NULL },
+       { "gtkform-log-file", 0, 0, G_OPTION_ARG_FILENAME, &log_file, "Path to file where to write debug info (or stdout or stderr)", NULL },
+       { NULL }
+};
+
 enum
 {
        PROP_0,
@@ -87,6 +97,9 @@ struct _GtkFormPrivate
                GdaEx *gdaex;
 
                GSList *groups;
+
+               guint debug;
+               GFileOutputStream *log_file;
        };
 
 G_DEFINE_TYPE (GtkForm, gtk_form, G_TYPE_OBJECT)
@@ -207,6 +220,93 @@ GtkForm
        return form;
 }
 
+static void
+gtkform_log_handler (const gchar *log_domain,
+                   GLogLevelFlags log_level,
+                   const gchar *message,
+                   gpointer user_data)
+{
+       GError *error;
+
+       gchar *msg;
+
+       GtkFormPrivate *priv = GTK_FORM_GET_PRIVATE ((GtkForm *)user_data);
+
+       msg = g_strdup_printf ("%s **: %s\n\n", log_domain, message);
+
+       if (g_output_stream_write (G_OUTPUT_STREAM (priv->log_file),
+           msg, strlen (msg), NULL, &error) < 0)
+               {
+                       g_warning ("Error on writing on log file: %s",
+                                  error != NULL && error->message != NULL ? error->message : "no details.");
+               }
+}
+
+static gboolean
+gtkform_post_parse_options (GOptionContext *context,
+                          GOptionGroup *group,
+                          gpointer user_data,
+                          GError **error)
+{
+       GtkFormPrivate *priv = GTK_FORM_GET_PRIVATE ((GtkForm *)user_data);
+
+       GError *my_error;
+
+       priv->debug = debug;
+       if (log_file == NULL)
+               {
+                       priv->log_file = 0;
+               }
+       else if (priv->debug > 0)
+               {
+                       gchar *filename = g_strstrip (g_strdup (log_file));
+                       if (g_ascii_strncasecmp (filename, "stdout", 6) == 0
+                           || g_ascii_strncasecmp (filename, "stderr", 6) == 0)
+                               {
+                               }
+                       else
+                               {
+                                       my_error = NULL;
+                                       priv->log_file = g_file_replace (g_file_new_for_path (filename),
+                                                                        NULL, FALSE, G_FILE_CREATE_NONE, NULL, &my_error);
+                                       if (priv->log_file == NULL)
+                                               {
+                                                       g_warning ("Error on opening log file: %s.",
+                                                                  my_error != NULL && my_error->message != NULL ? my_error->message : "no details.");
+                                               }
+                                       else
+                                               {
+                                                       /* set handler */
+                                                       g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
+                                                                          | G_LOG_FLAG_RECURSION, gtkform_log_handler, user_data);
+                                               }
+                               }       
+               }
+
+       return TRUE;
+}
+
+/**
+ * gtk_form_get_option_group:
+ * #gtkform: a #GtkForm object.
+ *
+ * Returns: the #GOptionGroup.
+ */
+GOptionGroup
+*gtk_form_get_option_group (GtkForm *gtkform)
+{
+       GOptionGroup *ret;
+
+       ret = g_option_group_new ("gtkform", "GtkForm", "GtkForm", (gpointer)gtkform, g_free);
+       if (ret != NULL)
+               {
+                       g_option_group_add_entries (ret, entries);
+                       g_option_group_set_parse_hooks (ret, NULL, gtkform_post_parse_options);
+               }
+
+       return ret;
+}
+
 static void
 parse_widget_on_change (GtkForm *form, xmlNodePtr xnode, GtkFormWidget *fwidget)
 {
@@ -1292,7 +1392,7 @@ gtk_form_check (GtkForm *form, gboolean with_key, GtkFormWidget **form_widget,
                                        widget = gtk_form_widget_get_widget (fwidget);
 
                                        if (form_widget != NULL && *form_widget == NULL)
-                                               {                                               
+                                               {
                                                        *form_widget = fwidget;
                                                }
 
@@ -1338,7 +1438,7 @@ gtk_form_check (GtkForm *form, gboolean with_key, GtkFormWidget **form_widget,
 
 /**
  * gtk_form_is_changed:
- * @form:
+ * @form: a #GtkForm object.
  *
  * Returns: TRUE if the values in the form are changed since last calling to
  * gtk_form_clear(), gtk_form_fill_from_datamodel() or gtk_form_set_as_origin().
@@ -1367,7 +1467,14 @@ gtk_form_is_changed (GtkForm *form)
                        if (gtk_form_field_is_to_save (field))
                                {
                                        ret = gtk_form_field_is_changed (field);
-                                       if (ret) break;
+                                       if (ret)
+                                               {
+                                                       if (priv->debug > 0)
+                                                               {
+                                                                       g_message ("Field «%s» is changed.", gtk_form_field_get_field_name (field));
+                                                               }
+                                                       break;
+                                               }
                                }
 
                        fields = g_slist_next (fields);
@@ -1378,7 +1485,7 @@ gtk_form_is_changed (GtkForm *form)
 
 /**
  * gtk_form_set_as_origin:
- * @form:  a #GtkForm object.
+ * @form: a #GtkForm object.
  *
  * Sets the original values of every #GtkForm #GtkFormField's to the currents
  * values.
index a7cbf7b808ac0a5e9fa509fe7a236dc48fe990b8..931f5a43d0959ac17d9ac1940155880d3fdf6b47 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2011 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -59,6 +59,8 @@ GtkForm *gtk_form_new (void);
 GtkForm *gtk_form_new_from_xml (xmlDoc *xmldoc, GtkBuilder *gtkbuilder);
 GtkForm *gtk_form_new_from_file (const gchar *filename, GtkBuilder *gtkbuilder);
 
+GOptionGroup *gtk_form_get_option_group (GtkForm *gtkform);
+
 gboolean gtk_form_load_from_xml (GtkForm *form, xmlDoc *xmldoc, GtkBuilder *gtkbuilder);
 gboolean gtk_form_load_from_file (GtkForm *form, const gchar *filename, GtkBuilder *gtkbuilder);
 
index 34d99966f814057de78cfa226b78ee1d16086128..bba0d93fbe09a05b1953faa60c9e283f75eefa75 100644 (file)
@@ -213,6 +213,8 @@ fill_cb_customers (void)
 int
 main (int argc, char **argv)
 {
+       GError *error;
+       GOptionContext *context;
        GtkBuilder *gtkbuilder;
 
        gtk_init (&argc, &argv);
@@ -228,6 +230,16 @@ main (int argc, char **argv)
 
        gtkbuilder = gtk_form_get_gtkbuilder (form);
 
+       error = NULL;
+       context = g_option_context_new ("test");
+       g_option_context_add_group (context, gdaex_get_option_group (gdaex));
+       g_option_context_add_group (context, gtk_form_get_option_group (form));
+       g_option_context_parse (context, &argc, &argv, &error);
+       if (error != NULL)
+               {
+                       g_warning ("Error on command line parsing: %s", error->message);
+               }
+
        w = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "wMain"));
 
        lbl_id = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "lbl_id"));