dbus-init.cpp revision f71a1d814d75fc08a50d7225270b881064e2af30
418N/A/*
418N/A * This is where Inkscape connects to the DBus when it starts and
605N/A * registers the main interface.
418N/A *
605N/A * Also where new interfaces are registered when a new document is created.
418N/A * (Not called directly by application-interface but called indirectly.)
418N/A *
418N/A * Authors:
418N/A * Soren Berg <Glimmer07@gmail.com>
418N/A *
418N/A * Copyright (C) 2009 Soren Berg
418N/A *
418N/A * Released under GNU GPL, read the file 'COPYING' for more information
418N/A */
418N/A
418N/A#include <dbus/dbus-glib.h>
418N/A#include "dbus-init.h"
418N/A
418N/A#include "application-interface.h"
418N/A#include "application-server-glue.h"
418N/A
418N/A#include "document-interface.h"
418N/A#include "document-server-glue.h"
418N/A
418N/A#include "inkscape.h"
418N/A#include "document.h"
418N/A#include "desktop.h"
418N/A#include "file.h"
418N/A#include "verbs.h"
418N/A#include "helper/action.h"
418N/A
418N/A#include <algorithm>
418N/A#include <iostream>
418N/A#include <sstream>
418N/A
493N/A
418N/A
418N/A
605N/Anamespace Inkscape {
418N/Anamespace Extension {
418N/Anamespace Dbus {
553N/A
418N/A/* PRIVATE get a connection to the session bus */
553N/ADBusGConnection *
553N/Adbus_get_connection() {
553N/A GError *error = NULL;
553N/A DBusGConnection *connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
418N/A if (error) {
418N/A fprintf(stderr, "Failed to get connection");
418N/A return NULL;
418N/A }
418N/A else
418N/A return connection;
418N/A}
418N/A
493N/A/* PRIVATE create a proxy object for a bus.*/
493N/ADBusGProxy *
418N/Adbus_get_proxy(DBusGConnection *connection) {
418N/A return dbus_g_proxy_new_for_name (connection,
418N/A DBUS_SERVICE_DBUS,
418N/A DBUS_PATH_DBUS,
418N/A DBUS_INTERFACE_DBUS);
418N/A}
418N/A
418N/A/* PRIVATE register an object on a bus */
418N/Astatic gpointer
418N/Adbus_register_object (DBusGConnection *connection,
493N/A DBusGProxy *proxy,
418N/A GType object_type,
418N/A const DBusGObjectInfo *info,
418N/A const gchar *path)
418N/A{
418N/A GObject *object = (GObject*)g_object_new (object_type, NULL);
418N/A dbus_g_object_type_install_info (object_type, info);
493N/A dbus_g_connection_register_g_object (connection, path, object);
418N/A return object;
418N/A}
/* Initialize a Dbus service */
void
init (void)
{
guint result;
GError *error = NULL;
DBusGConnection *connection;
DBusGProxy *proxy;
DocumentInterface *obj;
connection = dbus_get_connection();
proxy = dbus_get_proxy(connection);
org_freedesktop_DBus_request_name (proxy,
"org.inkscape",
DBUS_NAME_FLAG_DO_NOT_QUEUE, &result, &error);
//create interface for application
dbus_register_object (connection,
proxy,
TYPE_APPLICATION_INTERFACE,
&dbus_glib_application_interface_object_info,
DBUS_APPLICATION_INTERFACE_PATH);
} //init
gchar *
init_document (void) {
guint result;
GError *error = NULL;
DBusGConnection *connection;
DBusGProxy *proxy;
SPDocument *doc;
doc = sp_document_new(NULL, 1, TRUE);
std::string name("/org/inkscape/");
name.append(doc->name);
std::replace(name.begin(), name.end(), ' ', '_');
connection = dbus_get_connection();
proxy = dbus_get_proxy(connection);
dbus_register_object (connection,
proxy,
TYPE_DOCUMENT_INTERFACE,
&dbus_glib_document_interface_object_info,
name.c_str());
return strdup(name.c_str());
} //init_document
gchar *
dbus_init_desktop_interface (SPDesktop * dt)
{
DBusGConnection *connection;
DBusGProxy *proxy;
DocumentInterface *obj;
dbus_g_error_domain_register (INKSCAPE_ERROR,
NULL,
INKSCAPE_TYPE_ERROR);
std::string name("/org/inkscape/desktop_");
std::stringstream out;
out << dt->dkey;
name.append(out.str());
//printf("DKEY: %d\n, NUMBER %d\n NAME: %s\n", dt->dkey, dt->number, name.c_str());
connection = dbus_get_connection();
proxy = dbus_get_proxy(connection);
obj = (DocumentInterface*) dbus_register_object (connection,
proxy, TYPE_DOCUMENT_INTERFACE,
&dbus_glib_document_interface_object_info, name.c_str());
obj->desk = dt;
obj->updates = TRUE;
return strdup(name.c_str());
}
gchar *
init_desktop (void) {
//this function will create a new desktop and call
//dbus_init_desktop_interface.
SPDesktop * dt = sp_file_new_default();
std::string name("/org/inkscape/desktop_");
std::stringstream out;
out << dt->dkey;
name.append(out.str());
return strdup(name.c_str());
} //init_desktop
} } } /* namespace Inkscape::Extension::Dbus */