[glib/wip/menus-rebase2] add GActionObserver/Observable interface



commit d642140446aab13fa043f90146036e185376824b
Author: Ryan Lortie <desrt desrt ca>
Date:   Wed Nov 23 11:34:06 2011 -0500

    add GActionObserver/Observable interface

 docs/reference/gio/gio-docs.xml     |    1 +
 docs/reference/gio/gio-sections.txt |   12 +++
 gio/Makefile.am                     |    4 +
 gio/gactionobservable.c             |   86 ++++++++++++++++++
 gio/gactionobservable.h             |   65 +++++++++++++
 gio/gactionobserver.c               |  171 +++++++++++++++++++++++++++++++++++
 gio/gactionobserver.h               |   87 ++++++++++++++++++
 gio/gio.h                           |    2 +
 gio/gio.symbols                     |    5 +
 gio/giotypes.h                      |    2 +
 10 files changed, 435 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gio/gio-docs.xml b/docs/reference/gio/gio-docs.xml
index da2c259..bb741e4 100644
--- a/docs/reference/gio/gio-docs.xml
+++ b/docs/reference/gio/gio-docs.xml
@@ -193,6 +193,7 @@
     <chapter id="application">
         <title>Application support</title>
         <xi:include href="xml/gactiongroup.xml"/>
+        <xi:include href="xml/gactionobserver.xml"/>
         <xi:include href="xml/gsimpleactiongroup.xml"/>
         <xi:include href="xml/gaction.xml"/>
         <xi:include href="xml/gsimpleaction.xml"/>
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index fe89c12..9209364 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -2914,6 +2914,18 @@ g_action_group_exporter_query
 </SECTION>
 
 <SECTION>
+<FILE>gactionobserver</FILE>
+<TITLE>GActionObserver</TITLE>
+g_action_observer_action_added
+g_action_observer_action_enabled_changed
+g_action_observer_action_state_changed
+g_action_observer_action_removed
+
+<SUBSECTION Private>
+g_action_observer_get_type
+</SECTION>
+
+<SECTION>
 <FILE>gdbusactiongroup</FILE>
 GDBusActionGroup
 g_dbus_action_group_new
diff --git a/gio/Makefile.am b/gio/Makefile.am
index dbceb93..1fca66d 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -126,6 +126,8 @@ endif
 
 application_headers = \
 	gactiongroup.h			\
+	gactionobservable.h		\
+	gactionobserver.h		\
 	gsimpleactiongroup.h		\
 	gaction.h			\
 	gsimpleaction.h			\
@@ -141,6 +143,8 @@ application_headers = \
 
 application_sources = \
 	gactiongroup.c				\
+	gactionobservable.c			\
+	gactionobserver.c			\
 	gsimpleactiongroup.c			\
 	gaction.c				\
 	gsimpleaction.c				\
diff --git a/gio/gactionobservable.c b/gio/gactionobservable.c
new file mode 100644
index 0000000..11ed63b
--- /dev/null
+++ b/gio/gactionobservable.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright  2011 Canonical Limited
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * licence or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
+ * USA.
+ *
+ * Authors: Ryan Lortie <desrt desrt ca>
+ */
+
+#include "config.h"
+
+#include "gactionobservable.h"
+
+G_DEFINE_INTERFACE (GActionObservable, g_action_observable, G_TYPE_OBJECT)
+
+/**
+ * SECTION:gactionobserable
+ * @short_description: an interface implemented by objects that report
+ *                     changes to actions
+ *
+ * Since: 2.32
+ */
+
+void
+g_action_observable_default_init (GActionObservableInterface *iface)
+{
+}
+
+/**
+ * g_action_observable_register_observer:
+ * @observable: a #GActionObservable
+ * @action_name: the name of the action
+ * @observer: the #GActionObserver to which the events will be reported
+ *
+ * Registers @observer as being interested in changes to @action_name on
+ * @observable.
+ *
+ * Since: 2.32
+ **/
+void
+g_action_observable_register_observer (GActionObservable *observable,
+                                       const gchar       *action_name,
+                                       GActionObserver   *observer)
+{
+  g_return_if_fail (G_IS_ACTION_OBSERVABLE (observable));
+
+  G_ACTION_OBSERVABLE_GET_IFACE (observable)
+    ->register_observer (observable, action_name, observer);
+}
+
+/**
+ * g_action_observable_unregister_observer:
+ * @observable: a #GActionObservable
+ * @action_name: the name of the action
+ * @observer: the #GActionObserver to which the events will be reported
+ *
+ * Removes the registration of @observer as being interested in changes
+ * to @action_name on @observable.
+ *
+ * If the observer was registered multiple times, it must be
+ * unregistered an equal number of times.
+ *
+ * Since: 2.32
+ **/
+void
+g_action_observable_unregister_observer (GActionObservable *observable,
+                                         const gchar       *action_name,
+                                         GActionObserver   *observer)
+{
+  g_return_if_fail (G_IS_ACTION_OBSERVABLE (observable));
+
+  G_ACTION_OBSERVABLE_GET_IFACE (observable)
+    ->unregister_observer (observable, action_name, observer);
+}
diff --git a/gio/gactionobservable.h b/gio/gactionobservable.h
new file mode 100644
index 0000000..b1f7357
--- /dev/null
+++ b/gio/gactionobservable.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright  2011 Canonical Limited
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * licence or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
+ * USA.
+ *
+ * Authors: Ryan Lortie <desrt desrt ca>
+ */
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#ifndef __G_ACTION_OBSERVABLE_H__
+#define __G_ACTION_OBSERVABLE_H__
+
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_ACTION_OBSERVABLE                            (g_action_observable_get_type ())
+#define G_ACTION_OBSERVABLE(inst)                           (G_TYPE_CHECK_INSTANCE_CAST ((inst),                     \
+                                                             G_TYPE_ACTION_OBSERVABLE, GActionObservable))
+#define G_IS_ACTION_OBSERVABLE(inst)                        (G_TYPE_CHECK_INSTANCE_TYPE ((inst),                     \
+                                                             G_TYPE_ACTION_OBSERVABLE))
+#define G_ACTION_OBSERVABLE_GET_IFACE(inst)                 (G_TYPE_INSTANCE_GET_INTERFACE ((inst),                  \
+                                                             G_TYPE_ACTION_OBSERVABLE, GActionObservableInterface))
+
+typedef struct _GActionObservableInterface                  GActionObservableInterface;
+
+struct _GActionObservableInterface
+{
+  GTypeInterface g_iface;
+
+  void (* register_observer)   (GActionObservable *observable,
+                                const gchar       *action_name,
+                                GActionObserver   *observer);
+  void (* unregister_observer) (GActionObservable *observable,
+                                const gchar       *action_name,
+                                GActionObserver   *observer);
+};
+
+GType                   g_action_observable_get_type                    (void);
+void                    g_action_observable_register_observer           (GActionObservable  *observable,
+                                                                         const gchar        *action_name,
+                                                                         GActionObserver    *observer);
+void                    g_action_observable_unregister_observer         (GActionObservable  *observable,
+                                                                         const gchar        *action_name,
+                                                                         GActionObserver    *observer);
+
+G_END_DECLS
+
+#endif /* __G_ACTION_OBSERVABLE_H__ */
diff --git a/gio/gactionobserver.c b/gio/gactionobserver.c
new file mode 100644
index 0000000..c9d4176
--- /dev/null
+++ b/gio/gactionobserver.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright  2011 Canonical Limited
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * licence or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
+ * USA.
+ *
+ * Authors: Ryan Lortie <desrt desrt ca>
+ */
+
+#include "config.h"
+
+#include "gactionobserver.h"
+
+G_DEFINE_INTERFACE (GActionObserver, g_action_observer, G_TYPE_OBJECT)
+
+/**
+ * SECTION:gactionobserver
+ * @short_description: an interface implemented by objects that are
+ *                     interested in monitoring actions for changes
+ *
+ * GActionObserver is a simple interface allowing objects that wish to
+ * be notified of changes to actions to be notified of those changes.
+ *
+ * It is also possible to monitor changes to action groups using
+ * #GObject signals, but there are a number of reasons that this
+ * approach could become problematic:
+ *
+ *  - there are four separate signals that must be manually connected
+ *    and disconnected
+ *
+ *  - when a large number of different observers wish to monitor a
+ *    (usually disjoint) set of actions within the same action group,
+ *    there is only one way to avoid having all notifications delivered
+ *    to all observers: signal detail.  In order to use signal detail,
+ *    each action name must be quarked, which is not always practical.
+ *
+ *  - even if quarking is acceptable, #GObject signal details are
+ *    implemented by scanning a linked list, so there is no real
+ *    decrease in complexity
+ *
+ * Since: 2.32
+ */
+
+void
+g_action_observer_default_init (GActionObserverInterface *class)
+{
+}
+
+/**
+ * g_action_observer_action_added:
+ * @observer: a #GActionObserver
+ * @observable: the source of the event
+ * @action_name: the name of the action
+ * @enabled: %TRUE if the action is now enabled
+ * @parameter_type: the parameter type for action invocations, or %NULL
+ *                  if no parameter is required
+ * @state: the current state of the action, or %NULL if the action is
+ *         stateless
+ *
+ * This function is called when an action that the observer is
+ * registered to receive events for is added.
+ *
+ * This function should only be called by objects with which the
+ * observer has explicitly registered itself to receive events.
+ *
+ * Since: 2.32
+ **/
+void
+g_action_observer_action_added (GActionObserver    *observer,
+                                GActionObservable  *observable,
+                                const gchar        *action_name,
+                                const GVariantType *parameter_type,
+                                gboolean            enabled,
+                                GVariant           *state)
+{
+  g_return_if_fail (G_IS_ACTION_OBSERVER (observer));
+
+  G_ACTION_OBSERVER_GET_IFACE (observer)
+    ->action_added (observer, observable, action_name, parameter_type, enabled, state);
+}
+
+/**
+ * g_action_observer_action_enabled_changed:
+ * @observer: a #GActionObserver
+ * @observable: the source of the event
+ * @action_name: the name of the action
+ * @enabled: %TRUE if the action is now enabled
+ *
+ * This function is called when an action that the observer is
+ * registered to receive events for becomes enabled or disabled.
+ *
+ * This function should only be called by objects with which the
+ * observer has explicitly registered itself to receive events.
+ *
+ * Since: 2.32
+ **/
+void
+g_action_observer_action_enabled_changed (GActionObserver   *observer,
+                                          GActionObservable *observable,
+                                          const gchar       *action_name,
+                                          gboolean           enabled)
+{
+  g_return_if_fail (G_IS_ACTION_OBSERVER (observer));
+
+  G_ACTION_OBSERVER_GET_IFACE (observer)
+    ->action_enabled_changed (observer, observable, action_name, enabled);
+}
+
+/**
+ * g_action_observer_action_state_changed:
+ * @observer: a #GActionObserver
+ * @observable: the source of the event
+ * @action_name: the name of the action
+ * @state: the new state of the action
+ *
+ * This function is called when an action that the observer is
+ * registered to receive events for changes its state.
+ *
+ * This function should only be called by objects with which the
+ * observer has explicitly registered itself to receive events.
+ *
+ * Since: 2.32
+ **/
+void
+g_action_observer_action_state_changed (GActionObserver   *observer,
+                                        GActionObservable *observable,
+                                        const gchar       *action_name,
+                                        GVariant          *state)
+{
+  g_return_if_fail (G_IS_ACTION_OBSERVER (observer));
+
+  G_ACTION_OBSERVER_GET_IFACE (observer)
+    ->action_state_changed (observer, observable, action_name, state);
+}
+
+/**
+ * g_action_observer_action_removed:
+ * @observer: a #GActionObserver
+ * @observable: the source of the event
+ * @action_name: the name of the action
+ *
+ * This function is called when an action that the observer is
+ * registered to receive events for is removed.
+ *
+ * This function should only be called by objects with which the
+ * observer has explicitly registered itself to receive events.
+ *
+ * Since: 2.32
+ **/
+void
+g_action_observer_action_removed (GActionObserver   *observer,
+                                  GActionObservable *observable,
+                                  const gchar       *action_name)
+{
+  g_return_if_fail (G_IS_ACTION_OBSERVER (observer));
+
+  G_ACTION_OBSERVER_GET_IFACE (observer)
+    ->action_removed (observer, observable, action_name);
+}
diff --git a/gio/gactionobserver.h b/gio/gactionobserver.h
new file mode 100644
index 0000000..6c7949c
--- /dev/null
+++ b/gio/gactionobserver.h
@@ -0,0 +1,87 @@
+/*
+ * Copyright  2011 Canonical Limited
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * licence or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
+ * USA.
+ *
+ * Authors: Ryan Lortie <desrt desrt ca>
+ */
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#ifndef __G_ACTION_OBSERVER_H__
+#define __G_ACTION_OBSERVER_H__
+
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_ACTION_OBSERVER                              (g_action_observer_get_type ())
+#define G_ACTION_OBSERVER(inst)                             (G_TYPE_CHECK_INSTANCE_CAST ((inst),                     \
+                                                             G_TYPE_ACTION_OBSERVER, GActionObserver))
+#define G_IS_ACTION_OBSERVER(inst)                          (G_TYPE_CHECK_INSTANCE_TYPE ((inst),                     \
+                                                             G_TYPE_ACTION_OBSERVER))
+#define G_ACTION_OBSERVER_GET_IFACE(inst)                   (G_TYPE_INSTANCE_GET_INTERFACE ((inst),                  \
+                                                             G_TYPE_ACTION_OBSERVER, GActionObserverInterface))
+
+typedef struct _GActionObserverInterface                    GActionObserverInterface;
+
+struct _GActionObserverInterface
+{
+  GTypeInterface g_iface;
+
+  void (* action_added)           (GActionObserver    *observer,
+                                   GActionObservable  *observable,
+                                   const gchar        *action_name,
+                                   const GVariantType *parameter_type,
+                                   gboolean            enabled,
+                                   GVariant           *state);
+  void (* action_enabled_changed) (GActionObserver    *observer,
+                                   GActionObservable  *observable,
+                                   const gchar        *action_name,
+                                   gboolean            enabled);
+  void (* action_state_changed)   (GActionObserver    *observer,
+                                   GActionObservable  *observable,
+                                   const gchar        *action_name,
+                                   GVariant           *state);
+  void (* action_removed)         (GActionObserver    *observer,
+                                   GActionObservable  *observable,
+                                   const gchar        *action_name);
+};
+
+GType                   g_action_observer_get_type                      (void);
+void                    g_action_observer_action_added                  (GActionObserver    *observer,
+                                                                         GActionObservable  *observable,
+                                                                         const gchar        *action_name,
+                                                                         const GVariantType *parameter_type,
+                                                                         gboolean            enabled,
+                                                                         GVariant           *state);
+void                    g_action_observer_action_enabled_changed        (GActionObserver    *observer,
+                                                                         GActionObservable  *observable,
+                                                                         const gchar        *action_name,
+                                                                         gboolean            enabled);
+void                    g_action_observer_action_state_changed          (GActionObserver    *observer,
+                                                                         GActionObservable  *observable,
+                                                                         const gchar        *action_name,
+                                                                         GVariant           *state);
+void                    g_action_observer_action_removed                (GActionObserver    *observer,
+                                                                         GActionObservable  *observable,
+                                                                         const gchar        *action_name);
+
+G_END_DECLS
+
+#endif /* __G_ACTION_OBSERVER_H__ */
diff --git a/gio/gio.h b/gio/gio.h
index 097afe6..8b6c145 100644
--- a/gio/gio.h
+++ b/gio/gio.h
@@ -151,6 +151,8 @@
 #include <gio/gmenuexporter.h>
 #include <gio/gmenumarkup.h>
 #include <gio/gmenuproxy.h>
+#include <gio/gactionobservable.h>
+#include <gio/gactionobserver.h>
 
 #undef __GIO_GIO_H_INSIDE__
 
diff --git a/gio/gio.symbols b/gio/gio.symbols
index 5afbb7f..7aea07a 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -1415,6 +1415,11 @@ g_action_group_change_action_state
 g_action_group_exporter_export
 g_action_group_exporter_query
 g_action_group_exporter_stop
+g_action_observer_action_added
+g_action_observer_action_enabled_changed
+g_action_observer_action_removed
+g_action_observer_action_state_changed
+g_action_observer_get_type
 g_action_activate
 g_action_get_enabled
 g_action_get_name
diff --git a/gio/giotypes.h b/gio/giotypes.h
index c417ea8..328af3c 100644
--- a/gio/giotypes.h
+++ b/gio/giotypes.h
@@ -50,6 +50,8 @@ typedef struct _GZlibDecompressor             GZlibDecompressor;
 typedef struct _GSimpleActionGroup            GSimpleActionGroup;
 typedef struct _GDBusActionGroup              GDBusActionGroup;
 typedef struct _GActionGroup                  GActionGroup;
+typedef struct _GActionObserver               GActionObserver;
+typedef struct _GActionObservable             GActionObservable;
 typedef struct _GSimpleAction                 GSimpleAction;
 typedef struct _GAction                       GAction;
 typedef struct _GApplication                  GApplication;



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