Skip to content

Commit

Permalink
Registrar: Add registrar dbus interface for reference.
Browse files Browse the repository at this point in the history
  • Loading branch information
rilian-la-te committed Feb 16, 2018
1 parent 934a0de commit 7628da6
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 30 deletions.
35 changes: 35 additions & 0 deletions lib/menu-widget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,46 @@ namespace Appmenu
this.add(scroller);
scroller.add(mwidget);
this.show_all();
try
{
var con = Bus.get_sync(BusType.SESSION);
con.call.begin(
"org.valapanel.AppMenu.Registrar",
"/Registrar",
"org.valapanel.AppMenu.Registrar",
"Reference",
null,null,
DBusCallFlags.NONE, -1);

}
catch(Error e)
{
stderr.printf("%s\n",e.message);
}
}
public MenuWidget()
{
Object();
}
protected override void destroy()
{
try
{
var con = Bus.get_sync(BusType.SESSION,null);
con.call.begin(
"org.valapanel.AppMenu.Registrar",
"/Registrar",
"org.valapanel.AppMenu.Registrar",
"UnReference",
null,null,
DBusCallFlags.NO_AUTO_START, -1);

}
catch(Error e)
{
stderr.printf("%s\n",e.message);
}
}
private void restock()
{
unowned Gtk.StyleContext mcontext = mwidget.get_style_context();
Expand Down
14 changes: 14 additions & 0 deletions lib/registrar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ namespace Appmenu
construct
{
have_registrar = false;
try{
var con = Bus.get_sync(BusType.SESSION);
con.call.begin(
DBUS_NAME,
REG_OBJECT,
REG_IFACE,
"GetMenus",
null,null,
DBusCallFlags.NONE, -1);
}
catch(Error e)
{
stderr.printf("%s\n",e.message);
}
create_outer_registrar();
}
public void get_menu_for_window(uint window, out string name, out ObjectPath path)
Expand Down
2 changes: 2 additions & 0 deletions subprojects/registrar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ include(GLibProgramHandlers)
add_glib_marshal(MARSHALC MARSHALH "registrar-marshal" g_cclosure_user_marshal)
file(STRINGS data/com.canonical.AppMenu.Registrar.xml XML_RAW)
string(CONCAT XML_CONTENTS ${XML_RAW})
file(STRINGS data/org.valapanel.AppMenu.Registrar.xml PRIVATE_RAW)
string(CONCAT PRIVATE_CONTENTS ${PRIVATE_RAW})
configure_file(registrar-xml.c.in registrar-xml.c ESCAPE_QUOTES)

add_executable (appmenu-registrar
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<node>
<interface name="org.valapanel.AppMenu.Registrar">
<method name="Reference">
</method>
<method name="UnReference">
</method>
</interface>
</node>
25 changes: 13 additions & 12 deletions subprojects/registrar/registrar-dbusmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct _RegistrarDBusMenu
{
GObject parent;
GHashTable *menus;
uint registered_object;
};

G_DEFINE_TYPE(RegistrarDBusMenu, registrar_dbus_menu, G_TYPE_OBJECT)
Expand Down Expand Up @@ -374,34 +375,34 @@ static void _dbus_registrar_dbus_menu_window_unregistered(GObject *_sender, uint
static const GDBusInterfaceVTable _interface_vtable =
{ registrar_dbus_menu_dbus_interface_method_call, NULL, NULL };

static void _registrar_dbus_menu_unregister_object(gpointer user_data)
void registrar_dbus_menu_unregister(RegistrarDBusMenu *data, GDBusConnection *con)
{
GObject *data = G_OBJECT(user_data);
g_dbus_connection_unregister_object(con, data->registered_object);
g_signal_handlers_disconnect_by_func(data,
_dbus_registrar_dbus_menu_window_registered,
data);
con);
g_signal_handlers_disconnect_by_func(data,
_dbus_registrar_dbus_menu_window_unregistered,
data);
con);
g_object_unref(data);
}

uint registrar_dbus_menu_register(RegistrarDBusMenu *object, GDBusConnection *connection,
GError **error)
{
uint result;
GDBusNodeInfo *info = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
result = g_dbus_connection_register_object(connection,
DBUSMENU_REG_OBJECT,
(GDBusInterfaceInfo *)info->interfaces[0],
&_interface_vtable,
object,
_registrar_dbus_menu_unregister_object,
error);
uint result = g_dbus_connection_register_object(connection,
DBUSMENU_REG_OBJECT,
(GDBusInterfaceInfo *)info->interfaces[0],
&_interface_vtable,
object,
NULL,
error);
if (!result)
{
return 0;
}
object->registered_object = result;
g_signal_connect(object,
"window-registered",
(GCallback)_dbus_registrar_dbus_menu_window_registered,
Expand Down
1 change: 1 addition & 0 deletions subprojects/registrar/registrar-dbusmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE(RegistrarDBusMenu, registrar_dbus_menu, REGISTRAR, DBUS_MENU, GObject)
uint registrar_dbus_menu_register(RegistrarDBusMenu *object, GDBusConnection *connection,
GError **error);
void registrar_dbus_menu_unregister(RegistrarDBusMenu *data, GDBusConnection *con);

G_END_DECLS

Expand Down
70 changes: 52 additions & 18 deletions subprojects/registrar/registrar-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@
#include <stdbool.h>
#include <stdint.h>

#define MAIN_OBJECT_PATH "/Registrar"

struct _RegistrarApplication
{
GApplication parent;
RegistrarDBusMenu *registrar;
u_int32_t dbusmenu_binding;
u_int32_t private_binding;
};

extern const char *private_xml;

G_DEFINE_TYPE(RegistrarApplication, registrar_application, G_TYPE_APPLICATION)

static const GOptionEntry options[4] =
Expand All @@ -53,20 +58,18 @@ static const GOptionEntry options[4] =

RegistrarApplication *registrar_application_new()
{
return REGISTRAR_APPLICATION(g_object_new(registrar_application_get_type(),
"application-id",
"org.valapanel.AppMenu.Registrar",
"flags",
G_APPLICATION_HANDLES_COMMAND_LINE,
"resource-base-path",
"/org/valapanel/registrar",
NULL));
return REGISTRAR_APPLICATION(
g_object_new(registrar_application_get_type(),
"application-id",
"org.valapanel.AppMenu.Registrar",
"flags",
G_APPLICATION_HANDLES_COMMAND_LINE | G_APPLICATION_IS_SERVICE,
"resource-base-path",
"/org/valapanel/registrar",
NULL));
}
static void registrar_application_activate(GApplication *base)
{
RegistrarApplication *self = REGISTRAR_APPLICATION(base);
if (self->dbusmenu_binding > 0)
g_application_hold(base);
}
static int registrar_application_handle_local_options(GApplication *application,
GVariantDict *options)
Expand Down Expand Up @@ -94,9 +97,7 @@ static void registrar_application_on_dbus_name_aquired(GDBusConnection *connecti
RegistrarApplication *self = REGISTRAR_APPLICATION(user_data);
g_autoptr(GError) err = NULL;
registrar_dbus_menu_register(self->registrar, connection, &err);
if (!err)
g_application_hold(G_APPLICATION(user_data));
else
if (err)
{
g_print("%s\n", err->message);
}
Expand All @@ -105,8 +106,31 @@ static void registrar_application_on_dbus_name_lost(GDBusConnection *connection,
gpointer user_data)
{
GApplication *app = G_APPLICATION(user_data);
g_application_release(app);
}

static void registrar_application_method_call(GDBusConnection *connection, const char *sender,
const char *object_path, const char *interface_name,
const char *method_name, GVariant *parameters,
GDBusMethodInvocation *invocation, gpointer user_data)
{
GApplication *app = G_APPLICATION(user_data);
if (g_strcmp0(method_name, "Reference") == 0)
{
g_application_hold(app);
}
else if (g_strcmp0(method_name, "UnReference") == 0)
{
g_application_release(app);
}
else
{
g_object_unref(invocation);
}
}
static const GDBusInterfaceVTable _interface_vtable = { registrar_application_method_call,
NULL,
NULL };

static bool registrar_application_dbus_register(GApplication *base, GDBusConnection *connection,
const char *object_path, GError **error)
{
Expand All @@ -125,7 +149,17 @@ static bool registrar_application_dbus_register(GApplication *base, GDBusConnect
registrar_application_on_dbus_name_lost,
self,
NULL);
return ret && self->dbusmenu_binding;
GDBusNodeInfo *info = g_dbus_node_info_new_for_xml(private_xml, NULL);
self->private_binding =
g_dbus_connection_register_object(connection,
MAIN_OBJECT_PATH,
(GDBusInterfaceInfo *)info->interfaces[0],
&_interface_vtable,
self,
NULL,
error);

return ret && self->dbusmenu_binding && self->private_binding;
}
static void registrar_application_dbus_unregister(GApplication *base, GDBusConnection *connection,
const char *object_path)
Expand All @@ -134,8 +168,9 @@ static void registrar_application_dbus_unregister(GApplication *base, GDBusConne
g_return_if_fail(connection != NULL);
g_return_if_fail(object_path != NULL);
g_bus_unown_name(self->dbusmenu_binding);
registrar_dbus_menu_unregister(self->registrar, connection);
g_dbus_connection_unregister_object(connection, self->private_binding);
self->dbusmenu_binding = 0;
g_application_release(base);
G_APPLICATION_CLASS(registrar_application_parent_class)
->dbus_unregister(base, connection, object_path);
}
Expand All @@ -153,7 +188,6 @@ static void registrar_application_init(RegistrarApplication *application)
application->registrar =
REGISTRAR_DBUS_MENU(g_object_new(registrar_dbus_menu_get_type(), NULL));
g_application_add_main_option_entries(G_APPLICATION(application), options);
g_application_hold(application);
}

static void registrar_application_class_init(RegistrarApplicationClass *klass)
Expand Down
1 change: 1 addition & 0 deletions subprojects/registrar/registrar-xml.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
*/

const char* introspection_xml = "@XML_CONTENTS@";
const char* private_xml = "@PRIVATE_CONTENTS@";

0 comments on commit 7628da6

Please sign in to comment.