[glib/wip/gapplication] application: Make try_new() use varargs



commit b7c1974accfda862c8b24c8965babac7bfce7e04
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Tue May 18 13:45:02 2010 +0100

    application: Make try_new() use varargs
    
    Since using GParameters in the API is (rightfully) frowned upon, let's
    make try_new() use variadic arguments, and rename try_new() to
    try_newv(), to match the GObject conventions.

 gio/gapplication.c |  152 ++++++++++++++++++++++++++++++++++++++++++++--------
 gio/gapplication.h |    6 ++
 gio/gio.symbols    |    1 +
 3 files changed, 137 insertions(+), 22 deletions(-)
---
diff --git a/gio/gapplication.c b/gio/gapplication.c
index a03da0f..e41d53d 100644
--- a/gio/gapplication.c
+++ b/gio/gapplication.c
@@ -23,6 +23,8 @@
 
 #include "config.h"
 
+#include <gobject/gvaluecollector.h>
+
 #include "gapplication.h"
 #include "gio-marshal.h"
 #include "glibintl.h"
@@ -145,6 +147,58 @@ g_application_action_free (gpointer data)
 }
 
 /**
+ * g_application_try_newv:
+ * @flags: Application flags
+ * @type: Actual GObject class type to instantiate
+ * @on_other_process_exists: (scope call) (allow-none): Called if this
+ *   application is already running
+ * @user_data: (closure): User data
+ * @n_parameters: Number of object construction parameters
+ * @args: (array length=n_parameters): Object construction parameters
+ *
+ * Attempt to create an application instance.  If the application is
+ * already running in another process, then call @on_other_process_exists.
+ * The default if @on_other_process_exists is %NULL will terminate the
+ * current process.
+ *
+ * This function is defined to call g_type_init() as its very first
+ * action.
+ *
+ * If called multiple times, this function will return the extant
+ * application instance.
+ *
+ * Returns: (transfer full): The application instance
+ *
+ * Since: 2.26
+ */
+GApplication *
+g_application_try_newv (GApplicationFlags   flags,
+                        GType               class_type,
+                        GApplicationExistsCallback on_other_process_exists,
+                        gpointer            user_data,
+                        guint               n_parameters,
+                        GParameter         *parameters)
+{
+  if (application_instance != NULL)
+    return g_object_ref (application_instance);
+
+  g_type_init ();
+
+  if (!(flags & G_APPLICATION_DISABLE_SINGLE_INSTANCE))
+    {
+      if (!_g_application_platform_acquire_single_instance (g_value_get_string (&parameters[0].value), NULL))
+        {
+          if (on_other_process_exists)
+            on_other_process_exists (user_data);
+          else
+            _g_application_platform_default_exit ();
+        }
+    }
+
+  return G_APPLICATION (g_object_newv (class_type, n_parameters, parameters));
+}
+
+/**
  * g_application_new:
  * @appid: (allow-none): System-dependent application identifier
  * @flags: Initialization flags
@@ -177,10 +231,10 @@ g_application_new (const char        *appid,
   g_value_init (&parameters.value, G_TYPE_STRING);
   g_value_set_string (&parameters.value, appid);
 
-  app = g_application_try_new (flags, G_TYPE_APPLICATION,
-                               NULL, NULL,
-                               1,
-                               &parameters);
+  app = g_application_try_newv (flags, G_TYPE_APPLICATION,
+                                NULL, NULL,
+                                1,
+                                &parameters);
 
   g_value_unset (&parameters.value);
 
@@ -194,8 +248,9 @@ g_application_new (const char        *appid,
  * @on_other_process_exists: (scope call) (allow-none): Called if this
  *   application is already running
  * @user_data: (closure): User data
- * @n_parameters: Number of object construction parameters
- * @args: (array length=8): Object construction parameters
+ * @first_property_name: the name of the property
+ * @var_args: the value for the property name, followed by a list of
+ *   pairs of property names and values, terminated by %NULL
  *
  * Attempt to create an application instance.  If the application is
  * already running in another process, then call @on_other_process_exists.
@@ -208,33 +263,86 @@ g_application_new (const char        *appid,
  * If called multiple times, this function will return the extant
  * application instance.
  *
+ * This function is the variadic arguments version of g_application_try_newv()
+ * and it's meant as a convenience function for the C API. Bindings should
+ * use the array-based function instead.
+ *
  * Returns: (transfer full): The application instance
+ *
+ * Since: 2.26
  */
 GApplication *
-g_application_try_new (GApplicationFlags   flags,
-                       GType               class_type,
+g_application_try_new (GApplicationFlags  flags,
+                       GType              class_type,
                        GApplicationExistsCallback on_other_process_exists,
-                       gpointer            user_data,
-                       guint               n_parameters,
-                       GParameter         *parameters)
+                       gpointer           user_data,
+                       const char        *first_property_name,
+                       ...)
 {
-  if (application_instance != NULL)
-    return g_object_ref (application_instance);
+  GApplication *retval;
+  GObjectClass *class;
+  GParameter *params;
+  const gchar *name;
+  va_list args;
+  gint n_alloced_params = 16, n_params;
 
-  g_type_init ();
-  
-  if (!(flags & G_APPLICATION_DISABLE_SINGLE_INSTANCE))
+  va_start (args, first_property_name);
+
+  class = g_type_class_ref (class_type);
+
+  params = g_new0 (GParameter, n_alloced_params);
+  name = first_property_name;
+  while (name != NULL)
     {
-      if (!_g_application_platform_acquire_single_instance (g_value_get_string (&parameters[0].value), NULL))
+      gchar *error = NULL;
+      GParamSpec *pspec = g_object_class_find_property (class, name);
+      if (pspec == NULL)
         {
-          if (on_other_process_exists)
-            on_other_process_exists (user_data);
-          else
-            _g_application_platform_default_exit ();
+          g_warning ("%s: object class `%s' has no property named `%s'",
+                     G_STRFUNC,
+                     g_type_name (class_type),
+                     name);
+          break;
+        }
+
+      if (n_params >= n_alloced_params)
+        {
+          n_alloced_params += 16;
+          params = g_renew (GParameter, params, n_alloced_params);
+        }
+
+      params[n_params].name = name;
+      G_VALUE_COLLECT_INIT (&params[n_params].value, pspec->value_type,
+                            args,
+                            0,
+                            &error);
+      if (error)
+        {
+          g_warning ("%s: %s", G_STRFUNC, error);
+          g_free (error);
+          g_value_unset (&params[n_params].value);
+          break;
         }
+
+      n_params += 1;
+
+      name = va_arg (args, gchar*);
     }
 
-  return G_APPLICATION (g_object_newv (class_type, n_parameters, parameters));
+  retval = g_application_try_newv (flags, class_type,
+                                   on_other_process_exists, user_data,
+                                   n_params, params);
+
+  while (n_params--)
+    g_value_unset (&params[n_params].value);
+
+  g_free (params);
+
+  g_type_class_unref (class);
+
+  va_end (args);
+
+  return retval;
 }
 
 /**
diff --git a/gio/gapplication.h b/gio/gapplication.h
index 005ce32..aa571fc 100644
--- a/gio/gapplication.h
+++ b/gio/gapplication.h
@@ -131,6 +131,12 @@ GApplication *          g_application_try_new                   (GApplicationFla
                                                                  GType              class_type,
                                                                  GApplicationExistsCallback on_other_process_exists,
                                                                  gpointer           user_data,
+                                                                 const char        *first_property_name,
+                                                                 ...) G_GNUC_NULL_TERMINATED;
+GApplication *          g_application_try_newv                  (GApplicationFlags  flags,
+                                                                 GType              class_type,
+                                                                 GApplicationExistsCallback on_other_process_exists,
+                                                                 gpointer           user_data,
                                                                  guint              n_parameters,
                                                                  GParameter        *parameters);
 
diff --git a/gio/gio.symbols b/gio/gio.symbols
index ae78735..4680d86 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -31,6 +31,7 @@ g_vfs_get_local
 g_application_get_type G_GNUC_CONST
 g_application_new
 g_application_try_new
+g_application_try_newv
 g_application_get_instance
 g_application_get_id
 g_application_set_action_enabled



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