[Xfce4-commits] <midori:master> Replace midori_browser_is_action by _assert_action

Christian Dywan noreply at xfce.org
Wed Mar 27 00:14:01 CET 2013


Updating branch refs/heads/master
         to fc06bd5a819712fb4fd5ba30fe0b8e10f7685519 (commit)
       from af33570146f722406a753d1cb735175e72dc0f50 (commit)

commit fc06bd5a819712fb4fd5ba30fe0b8e10f7685519
Author: Christian Dywan <christian at twotoasts.de>
Date:   Wed Mar 27 00:07:34 2013 +0100

    Replace midori_browser_is_action by _assert_action

 midori/midori-app.c      |   18 ++++----------
 midori/midori-browser.c  |   60 +++++++++++++++++++++++++++++++++-------------
 midori/midori-browser.h  |    4 +-
 midori/midori-frontend.c |   42 +++++++++++++++++---------------
 midori/midori-session.c  |   14 ++++-------
 5 files changed, 77 insertions(+), 61 deletions(-)

diff --git a/midori/midori-app.c b/midori/midori-app.c
index 6677ddf..1dfd95a 100644
--- a/midori/midori-app.c
+++ b/midori/midori-app.c
@@ -571,15 +571,11 @@ midori_app_command_received (MidoriApp*   app,
     }
     else if (g_str_equal (command, "command"))
     {
-        guint i = 0;
-
         if (!uris || !app->browser)
             return FALSE;
-        while (uris[i] != NULL)
-        {
+        gint i;
+        for (i = 0; uris && uris[i]; i++)
             midori_browser_activate_action (app->browser, uris[i]);
-            i++;
-        }
         return TRUE;
     }
 
@@ -1117,19 +1113,15 @@ midori_app_send_command (MidoriApp* app,
     g_return_val_if_fail (MIDORI_IS_APP (app), FALSE);
     g_return_val_if_fail (command != NULL, FALSE);
 
-    if (midori_app_instance_is_running (app))
+    if (!midori_app_instance_is_running (app))
     {
         MidoriBrowser* browser = midori_browser_new ();
         int i;
         for (i=0; command && command[i]; i++)
-        {
-            if (!midori_browser_is_action (browser, command[i]))
-                midori_error (_("Unexpected action '%s'."), command[i]);
-        }
+            midori_browser_assert_action (browser, command[i]);
         gtk_widget_destroy (GTK_WIDGET (browser));
-    }
-    else
         return midori_app_command_received (app, "command", command, NULL);
+    }
 
     #if HAVE_UNIQUE
     if (app->instance)
diff --git a/midori/midori-browser.c b/midori/midori-browser.c
index 48e74dc..96db253 100644
--- a/midori/midori-browser.c
+++ b/midori/midori-browser.c
@@ -529,34 +529,54 @@ midori_browser_update_history_title (MidoriBrowser* browser,
     midori_browser_update_history (item, "website", "access");
 }
 
-gboolean
-midori_browser_is_action (MidoriBrowser* browser,
-                          const gchar*   name)
+/**
+ * midori_browser_assert_action:
+ * @browser: a #MidoriBrowser
+ * @name: name of the action or setting=value expression
+ *
+ * Assert that @name is a valid action or setting expression,
+ * if it fails the program will terminate with an error.
+ * To be used with command line interfaces.
+ *
+ * Since: 0.5.0
+ **/
+void
+midori_browser_assert_action (MidoriBrowser* browser,
+                              const gchar*   name)
 {
-    g_return_val_if_fail (MIDORI_IS_BROWSER (browser), FALSE);
+    g_return_if_fail (MIDORI_IS_BROWSER (browser));
+    g_return_if_fail (name != NULL);
 
-    GtkAction* action = _action_by_name (browser, name);
-    if (action)
-        return TRUE;
-    else if (strchr (name, '='))
+    if (strchr (name, '='))
     {
         gchar** parts = g_strsplit (name, "=", 0);
         GObjectClass* class = G_OBJECT_GET_CLASS (browser->settings);
         GParamSpec* pspec = g_object_class_find_property (class, parts[0]);
+        GType type = pspec ? G_PARAM_SPEC_TYPE (pspec) : G_TYPE_INVALID;
+        if (!(
+            (type == G_TYPE_PARAM_BOOLEAN && (!strcmp (parts[1], "true") || !strcmp (parts[1], "false")))
+         || type == G_TYPE_PARAM_STRING
+         || type == G_TYPE_PARAM_INT
+         || type == G_TYPE_PARAM_FLOAT
+         || type == G_TYPE_PARAM_ENUM))
+            midori_error (_("Value '%s' is invalid for %s"), parts[1], parts[0]);
         g_strfreev (parts);
-        return pspec != NULL;
     }
-    return FALSE;
+    else
+    {
+        GtkAction* action = _action_by_name (browser, name);
+        if (!action)
+            midori_error (_("Unexpected action '%s'."), name);
+    }
 }
 
 static void
 _midori_browser_activate_action (MidoriBrowser* browser,
                                  const gchar*   name)
 {
-    GtkAction* action = _action_by_name (browser, name);
-    if (action)
-        gtk_action_activate (action);
-    else if (strchr (name, '='))
+    g_return_if_fail (name != NULL);
+
+    if (strchr (name, '='))
     {
         gchar** parts = g_strsplit (name, "=", 0);
         GObjectClass* class = G_OBJECT_GET_CLASS (browser->settings);
@@ -586,7 +606,13 @@ _midori_browser_activate_action (MidoriBrowser* browser,
         g_strfreev (parts);
     }
     else
-        g_warning (_("Unexpected action '%s'."), name);
+    {
+        GtkAction* action = _action_by_name (browser, name);
+        if (action)
+            gtk_action_activate (action);
+        else
+            g_warning (_("Unexpected action '%s'."), name);
+    }
 }
 
 static void
@@ -7397,9 +7423,9 @@ midori_browser_add_uri (MidoriBrowser* browser,
 /**
  * midori_browser_activate_action:
  * @browser: a #MidoriBrowser
- * @name: name of the action
+ * @name: name of the action or setting=value expression
  *
- * Activates the specified action.
+ * Activates the specified action. See also midori_browser_assert_action().
  **/
 void
 midori_browser_activate_action (MidoriBrowser* browser,
diff --git a/midori/midori-browser.h b/midori/midori-browser.h
index 8036678..c68540a 100644
--- a/midori/midori-browser.h
+++ b/midori/midori-browser.h
@@ -96,8 +96,8 @@ void
 midori_browser_activate_action        (MidoriBrowser*     browser,
                                        const gchar*       name);
 
-gboolean
-midori_browser_is_action              (MidoriBrowser*     browser,
+void
+midori_browser_assert_action          (MidoriBrowser*     browser,
                                        const gchar*       name);
 
 void
diff --git a/midori/midori-frontend.c b/midori/midori-frontend.c
index c8a8ea8..af28e4a 100644
--- a/midori/midori-frontend.c
+++ b/midori/midori-frontend.c
@@ -87,20 +87,21 @@ midori_web_app_new (const gchar* config,
         g_free (tmp_uri);
     }
 
-    if (open_uris != NULL)
-        for (i = 0; open_uris[i] != NULL; i++)
-        {
-            gchar* new_uri = sokoke_magic_uri (open_uris[i], FALSE, TRUE);
-            midori_browser_add_uri (browser, new_uri);
-            g_free (new_uri);
-        }
+    for (i = 0; open_uris && open_uris[i]; i++)
+    {
+        gchar* new_uri = sokoke_magic_uri (open_uris[i], FALSE, TRUE);
+        midori_browser_add_uri (browser, new_uri);
+        g_free (new_uri);
+    }
     if (midori_browser_get_n_pages (browser) == 0)
         midori_browser_add_uri (browser, "about:blank");
     gtk_widget_show (GTK_WIDGET (browser));
 
-    if (execute_commands != NULL)
-        for (i = 0; execute_commands[i] != NULL; i++)
-            midori_browser_activate_action (browser, execute_commands[i]);
+    for (i = 0; execute_commands && execute_commands[i]; i++)
+    {
+        midori_browser_assert_action (browser, execute_commands[i]);
+        midori_browser_activate_action (browser, execute_commands[i]);
+    }
     return browser;
 }
 
@@ -215,20 +216,21 @@ midori_private_app_new (const gchar* config,
         g_free (tmp_uri);
     }
 
-    if (open_uris != NULL)
-        for (i = 0; open_uris[i] != NULL; i++)
-        {
-            gchar* new_uri = sokoke_magic_uri (open_uris[i], FALSE, TRUE);
-            midori_browser_add_uri (browser, new_uri);
-            g_free (new_uri);
-        }
+    for (i = 0; open_uris && open_uris[i]; i++)
+    {
+        gchar* new_uri = sokoke_magic_uri (open_uris[i], FALSE, TRUE);
+        midori_browser_add_uri (browser, new_uri);
+        g_free (new_uri);
+    }
     if (midori_browser_get_n_pages (browser) == 0)
         midori_browser_add_uri (browser, "about:private");
     gtk_widget_show (GTK_WIDGET (browser));
 
-    if (execute_commands != NULL)
-        for (i = 0; execute_commands[i] != NULL; i++)
-            midori_browser_activate_action (browser, execute_commands[i]);
+    for (i = 0; execute_commands && execute_commands[i]; i++)
+    {
+        midori_browser_assert_action (browser, execute_commands[i]);
+        midori_browser_activate_action (browser, execute_commands[i]);
+    }
     return browser;
 }
 
diff --git a/midori/midori-session.c b/midori/midori-session.c
index 1e42371..12ddfc8 100644
--- a/midori/midori-session.c
+++ b/midori/midori-session.c
@@ -514,16 +514,12 @@ midori_load_session (gpointer data)
     if (midori_uri_is_blank (katze_item_get_uri (item)))
         midori_browser_activate_action (browser, "Location");
 
-    if (open_uris != NULL)
+    guint i = 0;
+    for (i = 0; open_uris && open_uris[i]; i++)
     {
-        guint i = 0;
-        while (open_uris[i])
-        {
-            gchar* uri = sokoke_magic_uri (open_uris[i], TRUE, TRUE);
-            midori_browser_add_uri (browser, uri);
-            g_free (uri);
-            i++;
-        }
+        gchar* uri = sokoke_magic_uri (open_uris[i], TRUE, TRUE);
+        midori_browser_add_uri (browser, uri);
+        g_free (uri);
     }
 
     g_object_unref (settings);


More information about the Xfce4-commits mailing list