1 /*
2 * This is where Inkscape connects to the DBus when it starts and
3 * registers the main interface.
4 *
5 * Also where new interfaces are registered when a new document is created.
6 * (Not called directly by application-interface but called indirectly.)
7 *
8 * Authors:
9 * Soren Berg <Glimmer07@gmail.com>
10 *
11 * Copyright (C) 2009 Soren Berg
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include <dbus/dbus-glib.h>
17 #include "dbus-init.h"
19 #include "application-interface.h"
20 #include "application-server-glue.h"
22 #include "document-interface.h"
23 #include "document-server-glue.h"
25 #include "inkscape.h"
26 #include "document.h"
27 #include "desktop.h"
28 #include "file.h"
29 #include "verbs.h"
30 #include "helper/action.h"
32 #include <algorithm>
33 #include <iostream>
34 #include <sstream>
39 namespace Inkscape {
40 namespace Extension {
41 namespace Dbus {
43 /* PRIVATE get a connection to the session bus */
44 DBusGConnection *
45 dbus_get_connection() {
46 GError *error = NULL;
47 DBusGConnection *connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
48 if (error) {
49 fprintf(stderr, "Failed to get connection");
50 return NULL;
51 }
52 else
53 return connection;
54 }
56 /* PRIVATE create a proxy object for a bus.*/
57 DBusGProxy *
58 dbus_get_proxy(DBusGConnection *connection) {
59 return dbus_g_proxy_new_for_name (connection,
60 DBUS_SERVICE_DBUS,
61 DBUS_PATH_DBUS,
62 DBUS_INTERFACE_DBUS);
63 }
65 /* PRIVATE register an object on a bus */
66 static gpointer
67 dbus_register_object (DBusGConnection *connection,
68 DBusGProxy *proxy,
69 GType object_type,
70 const DBusGObjectInfo *info,
71 const gchar *path)
72 {
73 GObject *object = (GObject*)g_object_new (object_type, NULL);
74 dbus_g_object_type_install_info (object_type, info);
75 dbus_g_connection_register_g_object (connection, path, object);
76 return object;
77 }
79 /* Initialize a Dbus service */
80 void
81 init (void)
82 {
83 guint result;
84 GError *error = NULL;
85 DBusGConnection *connection;
86 DBusGProxy *proxy;
87 DocumentInterface *obj;
88 connection = dbus_get_connection();
89 proxy = dbus_get_proxy(connection);
90 org_freedesktop_DBus_request_name (proxy,
91 "org.inkscape",
92 DBUS_NAME_FLAG_DO_NOT_QUEUE, &result, &error);
93 //create interface for application
94 dbus_register_object (connection,
95 proxy,
96 TYPE_APPLICATION_INTERFACE,
97 &dbus_glib_application_interface_object_info,
98 DBUS_APPLICATION_INTERFACE_PATH);
99 } //init
101 gchar *
102 init_document (void) {
103 guint result;
104 GError *error = NULL;
105 DBusGConnection *connection;
106 DBusGProxy *proxy;
107 SPDocument *doc;
109 doc = sp_document_new(NULL, 1, TRUE);
111 std::string name("/org/inkscape/");
112 name.append(doc->name);
113 std::replace(name.begin(), name.end(), ' ', '_');
115 connection = dbus_get_connection();
116 proxy = dbus_get_proxy(connection);
118 dbus_register_object (connection,
119 proxy,
120 TYPE_DOCUMENT_INTERFACE,
121 &dbus_glib_document_interface_object_info,
122 name.c_str());
123 return strdup(name.c_str());
124 } //init_document
126 gchar *
127 dbus_init_desktop_interface (SPDesktop * dt)
128 {
129 DBusGConnection *connection;
130 DBusGProxy *proxy;
131 DocumentInterface *obj;
132 dbus_g_error_domain_register (INKSCAPE_ERROR,
133 NULL,
134 INKSCAPE_TYPE_ERROR);
136 std::string name("/org/inkscape/desktop_");
137 std::stringstream out;
138 out << dt->dkey;
139 name.append(out.str());
141 //printf("DKEY: %d\n, NUMBER %d\n NAME: %s\n", dt->dkey, dt->number, name.c_str());
143 connection = dbus_get_connection();
144 proxy = dbus_get_proxy(connection);
146 obj = (DocumentInterface*) dbus_register_object (connection,
147 proxy, TYPE_DOCUMENT_INTERFACE,
148 &dbus_glib_document_interface_object_info, name.c_str());
149 obj->desk = dt;
150 obj->updates = TRUE;
152 return strdup(name.c_str());
153 }
155 gchar *
156 init_desktop (void) {
157 //this function will create a new desktop and call
158 //dbus_init_desktop_interface.
159 SPDesktop * dt = sp_file_new_default();
161 std::string name("/org/inkscape/desktop_");
162 std::stringstream out;
163 out << dt->dkey;
164 name.append(out.str());
165 return strdup(name.c_str());
166 } //init_desktop
170 } } } /* namespace Inkscape::Extension::Dbus */