[glib/application: 2/2] more stuff...



commit 97c14ac87e73c684799ebe1d11dedd0c04c14b0f
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Jul 22 09:52:17 2010 -0400

    more stuff...

 gio/gapplication.c           |  175 ++++++++++++++++++++++++++++++++++++++++-
 gio/gapplication.h           |  116 ++++++++++++++++++----------
 gio/gapplicationinvocation.h |    3 +-
 gio/giotypes.h               |    2 +
 4 files changed, 249 insertions(+), 47 deletions(-)
---
diff --git a/gio/gapplication.c b/gio/gapplication.c
index 6c5e45b..622bbc9 100644
--- a/gio/gapplication.c
+++ b/gio/gapplication.c
@@ -1157,9 +1157,33 @@ g_application_class_init (GApplicationClass *klass)
 }
 
 
+/**
+ * g_application_open:
+ * @application: a #GApplication
+ * @file: a #GFile, the file to open
+ * @hint: the hint
+ *
+ * Request that a file be opened by the application.
+ *
+ * The application is registered (if it is not already).
+ *
+ * If this process is the first instance of the application that was
+ * invoked then the 'open' signal is emitted.
+ *
+
+/**
+ * g_application_invoke_variant:
+ * @application: a #GApplication
+ * @arguments: a bytestring array #GVariant
+ * @returns: a status code
+ *
+ * Invokes the application with the given commandline arguments.
+ *
+ * This is a bindings-friendly version of g_application_invoke().
+ **/
 int
-g_application_run_variant (GApplication *application,
-                           GVariant     *arguments)
+g_application_invoke_variant (GApplication *application,
+                              GVariant     *arguments)
 {
   GApplicationClass *class = G_APPLICATION_GET_CLASS (application);
   int exit_status;
@@ -1204,12 +1228,153 @@ g_application_run_variant (GApplication *application,
   return exit_status;
 }
 
+/**
+ * g_application_invoke:
+ * @application: a #GApplication
+ * @argc: the argc from the main function
+ * @argv: the argv from the main function
+ * @returns: a status code
+ *
+ * Invokes the application with the given commandline arguments.
+ *
+ * The application is registered (if it is not already).
+ *
+ * If this process is the first instance of the application that was
+ * invoked then the 'invoke' signal is emitted.
+ *
+ * If the use count of the application is zero after 'invoke' then this
+ * function immediately returns the same value as was returned by
+ * 'invoke'.
+ *
+ * If the use count of the application is non-zero after 'invoke' then
+ * the application continues to run until the use count drops to zero,
+ * at which point this function returns 0.
+ *
+ * If the application is already running then the 'invoke' method will
+ * be called in the existing application.  If the application is not
+ * already running and the disposition of @application is
+ * %G_APPLICATION_DISPOSITION_LAUNCHER then the application will be
+ * activated.  In either case, this function will return when the
+ * invocation object is no longer in use (which can be at the end of the
+ * 'invoke' call, or later if the object is held).  The return value of
+ * this function will be the exit status of the invocation.
+ *
+ * It is an error to use this function if the application has the
+ * %G_APPLICATION_DISPOSITION_SERVICE disposition.
+ *
+ * The return value of this function is intended to be used as the exit
+ * status of the process calling it.
+ **/
 int
-g_application_run (GApplication *application,
-                   int argc, char **argv)
+g_application_invoke (GApplication *application,
+                      int argc, char **argv)
 {
   GVariant *args;
 
   args = g_variant_bytestring_array_new ((const gchar **) argv, argc);
-  return g_application_run_variant (args);
+  return g_application_invoke_with_arguments (args);
+}
+
+/**
+ * g_application_hold:
+ * @application: a #GApplication
+ *
+ * Increases the use count of @application.
+ *
+ * Use this function to indicate that the application has a reason to
+ * continue to run.  For example, g_application_hold() is called by Gtk
+ * when a toplevel window is on the screen.
+ *
+ * To cancel the hold, call g_application_release().
+ **/
+void
+g_application_hold (GApplication *application)
+{
+  if (application->priv->inactivity_timeout)
+    {
+      g_source_remove (application->priv->inactivity_timeout);
+      application->priv->inactivity_timeout;
+    }
+
+  application->priv->use_count++;
+}
+
+/**
+ * g_application_release:
+ * @application: a #GApplication
+ *
+ * Decrease the use count of @application.
+ *
+ * When the use count reaches zero, the application will stop running.
+ *
+ * Never call this function except to cancel the effect of a previous
+ * call to g_application_hold().
+ **/
+void
+g_application_release (GApplication *application)
+{
+  application->priv->use_count--;
+
+  if (application->priv->use_count == 0)
+    g_application_quit (application);
+}
+
+static gboolean
+g_application_inactivity_timeout (gpointer data)
+{
+  GApplication *application = data;
+
+  g_message ("exiting application due to inactivity");
+  application->priv->inactivity_timeout = 0;
+  g_application_quit (application);
+
+  return FALSE;
+}
+
+/**
+ * g_application_run_service:
+ * @application: a #GApplication
+ * @returns: a status code
+ *
+ * Runs the application as a service.
+ *
+ * This call will switch the disposition of the application to
+ * %G_APPLICATION_DISPOSITION_SERVICE, register the application (if not
+ * yet registered), invoke the 'startup' virtual function and run the
+ * mainloop.
+ *
+ * Because it is not possible to change the disposition of an
+ * application after it has been registered, the application must either
+ * be unregistered or already have had its disposition set to
+ * %%G_APPLICATION_DISPOSITION_SERVICE.
+ *
+ * The initial use count of the application will be zero.  The
+ * application will exit after 10 seconds if the use count remains at
+ * zero.
+ *
+ * This call never results in activation of another application process
+ * or any communication with an existing application process.  If an
+ * instance of the application is already running then this call will
+ * simply fail, writing a diagnostic to stderr and returning a non-zero
+ * value, with no additional side effects.
+ *
+ * The return value of this function is intended to be used as the exit
+ * status of the process calling it.
+ **/
+int
+g_application_run_service (GApplication *application)
+{
+  g_return_val_if_fail (G_IS_APPLICATION (application), 1);
+  g_return_val_if_fail (application->priv->disposition ==
+                          G_APPLICATION_DISPOSITION_SERVICE ||
+                        !application->priv->registered, 1);
+
+  g_application_set_disposition (application,
+                                 G_APPLICATION_DISPOSITION_SERVICE);
+  g_application_register (application);
+
+  application->priv->inactivity_timeout =
+    g_timeout_add (10000, g_application_inactivity_timeout, application);
+
+  g_application_run_mainloop (application);
 }
diff --git a/gio/gapplication.h b/gio/gapplication.h
index e31aa4d..f74bb25 100644
--- a/gio/gapplication.h
+++ b/gio/gapplication.h
@@ -28,7 +28,6 @@
 #ifndef __G_APPLICATION_H__
 #define __G_APPLICATION_H__
 
-#include <glib-object.h>
 #include <gio/giotypes.h>
 
 G_BEGIN_DECLS
@@ -40,7 +39,6 @@ G_BEGIN_DECLS
 #define G_IS_APPLICATION_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), G_TYPE_APPLICATION))
 #define G_APPLICATION_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_APPLICATION, GApplicationClass))
 
-typedef struct _GApplication            GApplication;
 typedef struct _GApplicationPrivate     GApplicationPrivate;
 typedef struct _GApplicationClass       GApplicationClass;
 
@@ -82,7 +80,8 @@ struct _GApplicationClass
   void                     (* startup)            (GApplication            *application);
 
   void                     (* open)               (GApplication            *application,
-                                                   GFile                   *file);
+                                                   GFile                   *file,
+                                                   const gchar             *hint);
 
   void                     (* activate)           (GApplication            *application);
 
@@ -96,7 +95,7 @@ struct _GApplicationClass
   /* vfuncs */
   void                     (* add_platform_data)  (GApplication            *application,
                                                    GVariantBuilder         *builder);
-  gboolean                 (* preprocess_args)    (GApplication            *application,
+  gboolean                 (* process_arguments)  (GApplication            *application,
                                                    GVariant               **arguments,
                                                    gint                    *exit_status);
 
@@ -121,42 +120,79 @@ struct _GApplicationClass
   /*< private >*/
   gpointer padding[12];
 };
-GType                   g_application_get_type                  (void) G_GNUC_CONST;
-
-gboolean                g_application_is_supported              (void);
-
-GApplication *          g_application_new                       (const gchar      *appid);
-
-gboolean                g_application_register                  (GApplication      *application);
-
-GApplication *          g_application_get_instance              (void);
-G_CONST_RETURN gchar *  g_application_get_id                    (GApplication      *application);
-
-void                    g_application_add_action                (GApplication      *application,
-                                                                 const gchar       *name,
-                                                                 const gchar       *description);
-void                    g_application_remove_action             (GApplication      *application,
-                                                                 const gchar       *name);
-gchar **                g_application_list_actions              (GApplication      *application);
-void                    g_application_set_action_enabled        (GApplication      *application,
-                                                                 const gchar       *name,
-                                                                 gboolean           enabled);
-gboolean                g_application_get_action_enabled        (GApplication      *application,
-                                                                 const gchar       *name);
-G_CONST_RETURN gchar *  g_application_get_action_description    (GApplication      *application,
-                                                                 const gchar       *name);
-void                    g_application_invoke_action             (GApplication      *application,
-                                                                 const gchar       *name);
-
-int                     g_application_invoke_variant            (GApplication      *application,
-                                                                 GVariant          *arguments);
-int                     g_application_invoke                    (GApplication      *application,
-                                                                 int                argc,
-                                                                 char             **argv);
-
-void                    g_application_run                       (GApplication      *application);
-
-gboolean                g_application_is_remote                 (GApplication      *application);
+
+/**
+ * GApplicationDisposition:
+ * @G_APPLICATION_DISPOSITION_UNIQUE:     the application is a unique application contained within and launched
+ *                                        by this executable.  The application will stop running when its use
+ *                                        count drops to zero.  This is the default.
+ * @G_APPLICATION_DISPOSITION_NON_UNIQUE: the application is a non-unique application contained within and
+ *                                        launched by this executable.  This is useful if you want the lifecycle
+ *                                        management provided by #GApplication but not the unique application
+ *                                        support.  The application will stop running when its use count drops
+ *                                        to zero.
+ * @G_APPLICATION_DISPOSITION_SERVICE:    this executable contains a service that provides the application; the
+ *                                        application is not usually launched by running this executable.  The
+ *                                        application will stop running when its use count drops to zero.
+ * @G_APPLICATION_DISPOSITION_LAUNCHER:   this executable is used only for launching the application.  The
+ *                                        application service will be started if it is not already running.
+ *                                        This process will stop running when the application service is done
+ *                                        handling the request dispatched by this process.
+ **/
+typedef enum
+{
+  G_APPLICATION_DISPOSITION_UNIQUE,
+  G_APPLICATION_DISPOSITION_NON_UNIQUE,
+  G_APPLICATION_DISPOSITION_SERVICE,
+  G_APPLICATION_DISPOSITION_LAUNCHER
+} GApplicationDisposition;
+
+GType                   g_application_get_type                          (void) G_GNUC_CONST;
+
+GApplication *          g_application_get_instance                      (void);
+gboolean                g_application_is_supported                      (void);
+
+GApplication *          g_application_new                               (const gchar              *appid);
+
+gboolean                g_application_register                          (GApplication             *application);
+gboolean                g_application_is_remote                         (GApplication             *application);
+void                    g_application_set_disposition                   (GApplication             *application,
+                                                                         GApplicationDisposition   disposition);
+
+const gchar *           g_application_get_id                            (GApplication             *application);
+
+void                    g_application_add_action                        (GApplication             *application,
+                                                                         const gchar              *name,
+                                                                         const gchar              *description);
+void                    g_application_remove_action                     (GApplication             *application,
+                                                                         const gchar              *name);
+gchar **                g_application_list_actions                      (GApplication             *application);
+void                    g_application_set_action_enabled                (GApplication             *application,
+                                                                         const gchar              *name,
+                                                                         gboolean                  enabled);
+gboolean                g_application_get_action_enabled                (GApplication             *application,
+                                                                         const gchar              *name);
+const gchar *           g_application_get_action_description            (GApplication             *application,
+                                                                         const gchar              *name);
+void                    g_application_invoke_action                     (GApplication             *application,
+                                                                         const gchar              *name);
+
+int                     g_application_invoke_with_arguments             (GApplication             *application,
+                                                                         GVariant                 *arguments);
+int                     g_application_invoke                            (GApplication             *application,
+                                                                         int                       argc,
+                                                                         char                    **argv);
+
+void                    g_application_action                            (GApplication             *application,
+                                                                         const gchar              *action);
+
+void                    g_application_activate                          (GApplication             *application);
+
+void                    g_application_open                              (GApplication             *application,
+                                                                         GFile                    *file,
+                                                                         const gchar              *hint);
+
+int                     g_application_run_service                       (GApplication             *application);
 
 G_END_DECLS
 
diff --git a/gio/gapplicationinvocation.h b/gio/gapplicationinvocation.h
index f918984..79cd2b5 100644
--- a/gio/gapplicationinvocation.h
+++ b/gio/gapplicationinvocation.h
@@ -27,7 +27,7 @@
 #ifndef __G_APPLICATION_INVOCATION_H__
 #define __G_APPLICATION_INVOCATION_H__
 
-#include <glib-object.h>
+#include <gio/giotypes.h>
 
 G_BEGIN_DECLS
 
@@ -46,7 +46,6 @@ G_BEGIN_DECLS
                                                              G_TYPE_APPLICATION_INVOCATION,                          \
                                                              GApplicationInvocationClass))
 
-typedef struct _GApplicationInvocation                      GApplicationInvocation;
 typedef struct _GApplicationInvocationPrivate               GApplicationInvocationPrivate;
 typedef struct _GApplicationInvocationClass                 GApplicationInvocationClass;
 
diff --git a/gio/giotypes.h b/gio/giotypes.h
index c6b0806..482aa71 100644
--- a/gio/giotypes.h
+++ b/gio/giotypes.h
@@ -47,6 +47,8 @@ typedef struct _GSimplePermission             GSimplePermission;
 typedef struct _GZlibCompressor               GZlibCompressor;
 typedef struct _GZlibDecompressor             GZlibDecompressor;
 
+typedef struct _GApplication                  GApplication;
+typedef struct _GApplicationInvocation        GApplicationInvocation;
 typedef struct _GSettingsBackend              GSettingsBackend;
 typedef struct _GSettings                     GSettings;
 typedef struct _GPermission                   GPermission;



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