Commit a7923a4a authored by Allison Karlitskaya's avatar Allison Karlitskaya

new GApplication implementation

parent 9d80c361
......@@ -142,10 +142,11 @@ application_headers = \
gapplication.h
application_sources = \
gactiongroup.c \
gsimpleactiongroup.c \
gaction.c \
gsimpleaction.c \
gactiongroup.c \
gsimpleactiongroup.c \
gaction.c \
gsimpleaction.c \
gapplicationimpl-dbus.c \
gapplication.c
local_sources = \
......
This diff is collapsed.
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright © 2010 Red Hat, Inc
/*
* Copyright © 2010 Codethink 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
......@@ -17,8 +16,7 @@
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Colin Walters <walters@verbum.org>
* Emmanuele Bassi <ebassi@linux.intel.com>
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
......@@ -28,21 +26,22 @@
#ifndef __G_APPLICATION_H__
#define __G_APPLICATION_H__
#include <glib-object.h>
#include <gio/giotypes.h>
G_BEGIN_DECLS
#define G_TYPE_APPLICATION (g_application_get_type ())
#define G_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_APPLICATION, GApplication))
#define G_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), G_TYPE_APPLICATION, GApplicationClass))
#define G_IS_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_TYPE_APPLICATION))
#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))
#define G_TYPE_APPLICATION (g_application_get_type ())
#define G_APPLICATION(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
G_TYPE_APPLICATION, GApplication))
#define G_APPLICATION_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), \
G_TYPE_APPLICATION, GApplicationClass))
#define G_IS_APPLICATION(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), G_TYPE_APPLICATION))
#define G_IS_APPLICATION_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_APPLICATION))
#define G_APPLICATION_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), \
G_TYPE_APPLICATION, GApplicationClass))
typedef struct _GApplication GApplication;
typedef struct _GApplicationPrivate GApplicationPrivate;
typedef struct _GApplicationClass GApplicationClass;
typedef struct _GApplicationPrivate GApplicationPrivate;
typedef struct _GApplicationClass GApplicationClass;
/**
* GApplication:
......@@ -62,13 +61,19 @@ struct _GApplication
/**
* GApplicationClass:
* @action_with_data: class handler for the #GApplication::action-with-data signal
* @quit_with_data: class handler for the #GApplication::quit-with-data signal
* @prepare_activation: class handler for the #GApplication::prepare-activation signal
* @run: virtual function, called by g_application_run()
*
* The <structname>GApplicationClass</structname> structure contains
* private data only
* @startup: invoked on the primary instance immediately after registration
* @activate: invoked on the primary instance when an activation occurs
* @open: invoked on the primary instance when there are files to open
* @local_command_line: invoked (locally) when the process has been invoked via commandline execution. The
* virtual function has the chance to inspect (and possibly replace) the list of command line arguments. See
* g_application_run() for more information.
* @before_emit: invoked on the primary instance before 'activate', 'open' or any action invocation
* @after_emit: invoked on the primary instance after 'activate', 'open' or any action invocation
* @add_platform_data: invoked (locally) to add 'platform data' to be sent to the primary instance when
* activating, opening or invoking actions
* @quit_mainloop: invoked on the primary instance when the use count of the application drops to zero (and
* after any inactivity timeout, if requested)
* @run_mainloop: invoked on the primary instance from g_application_run() if the use-count is non-zero
*
* Since: 2.26
*/
......@@ -79,70 +84,78 @@ struct _GApplicationClass
/*< public >*/
/* signals */
void (* action_with_data) (GApplication *application,
const gchar *action_name,
GVariant *platform_data);
gboolean (* quit_with_data) (GApplication *application,
GVariant *platform_data);
void (* prepare_activation) (GApplication *application,
GVariant *arguments,
GVariant *platform_data);
void (* startup) (GApplication *application);
void (* activate) (GApplication *application);
void (* open) (GApplication *application,
GFile **files,
gint n_files,
const gchar *hint);
gpointer _reserved_1;
/* vfuncs */
void (* run) (GApplication *application);
gboolean (* local_command_line) (GApplication *application,
GVariant **arguments,
int *exit_status);
gpointer _reserved_2;
void (* before_emit) (GApplication *application,
GVariant *platform_data);
void (* after_emit) (GApplication *application,
GVariant *platform_data);
void (* add_platform_data) (GApplication *application,
GVariantBuilder *builder);
void (* quit_mainloop) (GApplication *application);
void (* run_mainloop) (GApplication *application);
/*< private >*/
/* Padding for future expansion */
void (*_g_reserved1) (void);
void (*_g_reserved2) (void);
void (*_g_reserved3) (void);
void (*_g_reserved4) (void);
void (*_g_reserved5) (void);
void (*_g_reserved6) (void);
gpointer padding[12];
};
GType g_application_get_type (void) G_GNUC_CONST;
GApplication * g_application_new (const gchar *appid,
int argc,
char **argv);
GApplication * g_application_try_new (const gchar *appid,
int argc,
char **argv,
GError **error);
GApplication * g_application_unregistered_try_new (const gchar *appid,
int argc,
char **argv,
GError **error);
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,
GVariant *platform_data);
void g_application_run (GApplication *application);
gboolean g_application_quit_with_data (GApplication *application,
GVariant *platform_data);
gboolean g_application_is_remote (GApplication *application);
GType g_application_get_type (void) G_GNUC_CONST;
gboolean g_application_id_is_valid (const gchar *application_id);
GApplication * g_application_new (const gchar *application_id,
GApplicationFlags flags);
const gchar * g_application_get_application_id (GApplication *application);
void g_application_set_application_id (GApplication *application,
const gchar *application_id);
guint g_application_get_inactivity_timeout (GApplication *application);
void g_application_set_inactivity_timeout (GApplication *application,
guint inactivity_timeout);
GApplicationFlags g_application_get_flags (GApplication *application);
void g_application_set_flags (GApplication *application,
GApplicationFlags flags);
gboolean g_application_get_is_registered (GApplication *application);
gboolean g_application_get_is_remote (GApplication *application);
gboolean g_application_register (GApplication *application,
GCancellable *cancellable,
GError **error);
void g_application_hold (GApplication *application);
void g_application_release (GApplication *application);
void g_application_activate (GApplication *application);
void g_application_open (GApplication *application,
GFile **file,
gint n_files,
const gchar *hint);
int g_application_run (GApplication *application,
int argc,
char **argv);
int g_application_run_with_arguments (GApplication *application,
GVariant *arguments);
G_END_DECLS
......
/*
* Copyright © 2010 Codethink Limited
*
* This library 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.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
static const GDBusArgInfo platform_data_arg = { -1, (gchar *) "platform_data", (gchar *) "a{sv}" };
static const GDBusArgInfo open_uris_arg = { -1, (gchar *) "uris", (gchar *) "as" };
static const GDBusArgInfo open_hint_arg = { -1, (gchar *) "hint", (gchar *) "s" };
static const GDBusArgInfo invoke_action_name_arg = { -1, (gchar *) "name", (gchar *) "s" };
static const GDBusArgInfo invoke_action_args_arg = { -1, (gchar *) "args", (gchar *) "v" };
static const GDBusArgInfo cmdline_path_arg = { -1, (gchar *) "path", (gchar *) "o" };
static const GDBusArgInfo cmdline_arguments_arg = { -1, (gchar *) "arguments", (gchar *) "aay" };
static const GDBusArgInfo cmdline_exit_status_arg = { -1, (gchar *) "exit_status", (gchar *) "i" };
static const GDBusArgInfo *activate_in[] = { &platform_data_arg, NULL };
static const GDBusArgInfo *activate_out[] = { NULL };
static const GDBusArgInfo *open_in[] = { &open_uris_arg, &open_hint_arg, &platform_data_arg, NULL };
static const GDBusArgInfo *open_out[] = { NULL };
static const GDBusMethodInfo activate_method = {
-1, (gchar *) "Activate",
(GDBusArgInfo **) activate_in,
(GDBusArgInfo **) activate_out
};
static const GDBusMethodInfo open_method = {
-1, (gchar *) "Open",
(GDBusArgInfo **) open_in,
(GDBusArgInfo **) open_out
};
static const GDBusMethodInfo *application_methods[] = {
&activate_method, &open_method, NULL
};
const GDBusInterfaceInfo org_gtk_Application = {
-1, (gchar *) "org.gtk.Application",
(GDBusMethodInfo **) application_methods
};
/*
* Copyright © 2010 Codethink Limited
*
* This library 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 License, 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 "gapplicationimpl.h"
#include "gapplication.h"
#include "gfile.h"
#include "gdbusconnection.h"
#include "gdbusintrospection.h"
#include "gdbuserror.h"
#include <string.h>
#include "gapplicationimpl-dbus-interface.c"
struct _GApplicationImpl
{
GDBusConnection *session_bus;
const gchar *bus_name;
gchar *object_path;
guint object_id;
gpointer app;
GMainLoop *cmdline_mainloop;
};
static void
g_application_impl_method_call (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
GApplicationImpl *impl = user_data;
GApplicationClass *class;
class = G_APPLICATION_GET_CLASS (impl->app);
if (strcmp (method_name, "Activate") == 0)
{
GVariant *platform_data;
g_variant_get (parameters, "(@a{sv})", &platform_data);
class->before_emit (impl->app, platform_data);
g_signal_emit_by_name (impl->app, "activate");
class->after_emit (impl->app, platform_data);
g_variant_unref (platform_data);
}
else if (strcmp (method_name, "Open") == 0)
{
GVariant *platform_data;
const gchar *hint;
GVariant *array;
GFile **files;
gint n, i;
g_variant_get (parameters, "(@ass@a{sv})",
&array, &hint, &platform_data);
n = g_variant_n_children (array);
files = g_new (GFile *, n + 1);
for (i = 0; i < n; i++)
{
const gchar *uri;
g_variant_get_child (array, i, "&s", &uri);
files[i] = g_file_new_for_uri (uri);
}
g_variant_unref (array);
files[n] = NULL;
class->before_emit (impl->app, platform_data);
g_signal_emit_by_name (impl->app, "open", files, n, hint);
class->after_emit (impl->app, platform_data);
g_variant_unref (platform_data);
for (i = 0; i < n; i++)
g_object_unref (files[i]);
g_free (files);
}
else
g_assert_not_reached ();
}
static gchar *
application_path_from_appid (const gchar *appid)
{
gchar *appid_path, *iter;
appid_path = g_strconcat ("/", appid, NULL);
for (iter = appid_path; *iter; iter++)
{
if (*iter == '.')
*iter = '/';
}
return appid_path;
}
void
g_application_impl_destroy (GApplicationImpl *impl)
{
if (impl->session_bus)
{
if (impl->object_id)
g_dbus_connection_unregister_object (impl->session_bus,
impl->object_id);
g_object_unref (impl->session_bus);
g_free (impl->object_path);
}
else
{
g_assert (impl->object_path == NULL);
g_assert (impl->object_id == 0);
}
g_slice_free (GApplicationImpl, impl);
}
GApplicationImpl *
g_application_impl_register (GApplication *application,
const gchar *appid,
GApplicationFlags flags,
gboolean *is_remote,
GCancellable *cancellable,
GError **error)
{
const static GDBusInterfaceVTable vtable = {
g_application_impl_method_call
};
GApplicationImpl *impl;
GVariant *reply;
guint32 rval;
impl = g_slice_new (GApplicationImpl);
impl->app = application;
impl->bus_name = appid;
impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION,
cancellable, error);
if (impl->session_bus == NULL)
{
g_slice_free (GApplicationImpl, impl);
return NULL;
}
impl->object_path = application_path_from_appid (appid);
if (flags & G_APPLICATION_FLAGS_IS_LAUNCHER)
{
impl->object_id = 0;
*is_remote = TRUE;
return impl;
}
impl->object_id = g_dbus_connection_register_object (impl->session_bus,
impl->object_path,
(GDBusInterfaceInfo *)
&org_gtk_Application,
&vtable,
impl, NULL,
error);
if (impl->object_id == 0)
{
g_object_unref (impl->session_bus);
g_free (impl->object_path);
impl->session_bus = NULL;
impl->object_path = NULL;
g_slice_free (GApplicationImpl, impl);
return NULL;
}
reply = g_dbus_connection_call_sync (impl->session_bus,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"RequestName",
g_variant_new ("(su)",
/* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
impl->bus_name, 0x4),
G_VARIANT_TYPE ("(u)"),
0, -1, cancellable, error);
if (reply == NULL)
{
g_dbus_connection_unregister_object (impl->session_bus,
impl->object_id);
impl->object_id = 0;
g_object_unref (impl->session_bus);
g_free (impl->object_path);
impl->session_bus = NULL;
impl->object_path = NULL;
g_slice_free (GApplicationImpl, impl);
return NULL;
}
g_variant_get (reply, "(u)", &rval);
g_variant_unref (reply);
/* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
if ((*is_remote = (rval == 3)))
{
g_dbus_connection_unregister_object (impl->session_bus,
impl->object_id);
impl->object_id = 0;
if (flags & G_APPLICATION_FLAGS_IS_SERVICE)
{
g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
"Unable to acquire bus name `%s'", appid);
g_object_unref (impl->session_bus);
g_free (impl->object_path);
g_slice_free (GApplicationImpl, impl);
impl = NULL;
}
}
return impl;
}
void
g_application_impl_activate (GApplicationImpl *impl,
GVariant *platform_data)
{
g_dbus_connection_call (impl->session_bus,
impl->bus_name,
impl->object_path,
"org.gtk.Application",
"Activate",
g_variant_new ("(@a{sv})", platform_data),
NULL, 0, -1, NULL, NULL, NULL);
}
void
g_application_impl_open (GApplicationImpl *impl,
GFile **files,
gint n_files,
const gchar *hint,
GVariant *platform_data)
{
GVariantBuilder builder;
gint i;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
for (i = 0; i < n_files; i++)
{
gchar *uri = g_file_get_uri (files[i]);
g_variant_builder_add (&builder, "s", uri);
g_free (uri);
}
g_variant_builder_close (&builder);
g_variant_builder_add (&builder, "s", hint);
g_variant_builder_add_value (&builder, platform_data);
g_dbus_connection_call (impl->session_bus,
impl->bus_name,
impl->object_path,
"org.gtk.Application",
"Open",
g_variant_builder_end (&builder),
NULL, 0, -1, NULL, NULL, NULL);
}
void
g_application_impl_flush (GApplicationImpl *impl)
{
g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
}
#include "giotypes.h"
typedef struct _GApplicationImpl GApplicationImpl;
G_GNUC_INTERNAL
void g_application_impl_destroy (GApplicationImpl *impl);
G_GNUC_INTERNAL
GApplicationImpl * g_application_impl_register (GApplication *application,
const gchar *appid,
GApplicationFlags flags,
gboolean *is_remote,
GCancellable *cancellable,
GError **error);
G_GNUC_INTERNAL
void g_application_impl_activate (GApplicationImpl *impl,
GVariant *platform_data);
G_GNUC_INTERNAL
void g_application_impl_open (GApplicationImpl *impl,
GFile **files,
gint n_files,
const gchar *hint,
GVariant *platform_data);
G_GNUC_INTERNAL
void g_application_impl_flush (GApplicationImpl *impl);
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright © 2010 Red Hat, Inc
*
* This library 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 License, 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: Colin Walters <walters@verbum.org>
*/
#define G_APPLICATION_IFACE "org.gtk.Application"
static void
application_dbus_method_call (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
GApplication *app = G_APPLICATION (user_data);
if (method_name == NULL && *method_name == '\0')
return;
if (strcmp (method_name, "Quit") == 0)
{
GVariant *platform_data;
g_variant_get (parameters, "(@a{sv})", &platform_data);
g_dbus_method_invocation_return_value (invocation, NULL);
g_application_quit_with_data (app, platform_data);
g_variant_unref (platform_data);
}
else if (strcmp (method_name, "ListActions") == 0)
{
GHashTableIter iter;
GApplicationAction *value;
GVariantBuilder builder;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("(a{s(sb)})"));
g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{s(sb)}"));
g_hash_table_iter_init (&iter, app->priv->actions);
while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&value))
g_variant_builder_add (&builder, "{s(sb)}",
value->name,
value->description ? value->description : "",
value->enabled);
g_variant_builder_close (&builder);
g_dbus_method_invocation_return_value (invocation,
g_variant_builder_end (&builder));
}
else if (strcmp (method_name, "InvokeAction") == 0)
{
const char *action_name;
GVariant *platform_data;
GApplicationAction *action;
g_variant_get (parameters, "(&s@a{sv})", &action_name, &platform_data);
action = g_hash_table_lookup (app->priv->actions, action_name);
if (!action)
{
char *errmsg = g_strdup_printf ("Invalid action: %s", action_name);
g_dbus_method_invocation_return_dbus_error (invocation, G_APPLICATION_IFACE ".InvalidAction", errmsg);
g_free (errmsg);
g_variant_unref (platform_data);
return;
}
g_signal_emit (app, application_signals[ACTION_WITH_DATA],
g_quark_from_string (action_name), action_name, platform_data);
g_dbus_method_invocation_return_value (invocation, NULL);
g_variant_unref (platform_data);
}
else if (strcmp (method_name, "Activate") == 0)
{
GVariant *args;
GVariant *platform_data;
g_variant_get (parameters, "(@aay@a{sv})", &args, &platform_data);
g_signal_emit (app, application_signals[PREPARE_ACTIVATION], 0, args, platform_data);
g_variant_unref (args);
g_variant_unref (platform_data);