2 #include <dbus/dbus-glib.h>
3 #include "dbus-init.h"
5 #include "application-interface.h"
6 #include "application-server-glue.h"
8 #include "document-interface.h"
9 #include "document-server-glue.h"
11 #include "inkscape.h"
12 #include "document.h"
13 #include "desktop.h"
14 #include "file.h"
15 #include "verbs.h"
16 #include "helper/action.h"
18 #include <algorithm>
19 #include <iostream>
20 #include <sstream>
25 namespace Inkscape {
26 namespace Extension {
27 namespace Dbus {
29 /* PRIVATE get a connection to the session bus */
30 DBusGConnection *
31 dbus_get_connection() {
32 GError *error = NULL;
33 DBusGConnection *connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
34 if (error) {
35 fprintf(stderr, "Failed to get connection");
36 return NULL;
37 }
38 else
39 return connection;
40 }
42 /* PRIVATE create a proxy object for a bus.*/
43 DBusGProxy *
44 dbus_get_proxy(DBusGConnection *connection) {
45 return dbus_g_proxy_new_for_name (connection,
46 DBUS_SERVICE_DBUS,
47 DBUS_PATH_DBUS,
48 DBUS_INTERFACE_DBUS);
49 }
51 /* PRIVATE register an object on a bus */
52 static gpointer
53 dbus_register_object (DBusGConnection *connection,
54 DBusGProxy *proxy,
55 GType object_type,
56 const DBusGObjectInfo *info,
57 const gchar *path)
58 {
59 GObject *object = (GObject*)g_object_new (object_type, NULL);
60 dbus_g_object_type_install_info (object_type, info);
61 dbus_g_connection_register_g_object (connection, path, object);
62 return object;
63 }
65 /* Initialize a Dbus service */
66 void
67 init (void)
68 {
69 guint result;
70 GError *error = NULL;
71 DBusGConnection *connection;
72 DBusGProxy *proxy;
73 DocumentInterface *obj;
74 connection = dbus_get_connection();
75 proxy = dbus_get_proxy(connection);
76 org_freedesktop_DBus_request_name (proxy,
77 "org.inkscape",
78 DBUS_NAME_FLAG_DO_NOT_QUEUE, &result, &error);
79 //create interface for application
80 dbus_register_object (connection,
81 proxy,
82 TYPE_APPLICATION_INTERFACE,
83 &dbus_glib_application_interface_object_info,
84 DBUS_APPLICATION_INTERFACE_PATH);
85 } //init
87 gchar *
88 init_document (void) {
89 guint result;
90 GError *error = NULL;
91 DBusGConnection *connection;
92 DBusGProxy *proxy;
93 SPDocument *doc;
95 doc = sp_document_new(NULL, 1, TRUE);
97 std::string name("/org/inkscape/");
98 name.append(doc->name);
99 std::replace(name.begin(), name.end(), ' ', '_');
101 connection = dbus_get_connection();
102 proxy = dbus_get_proxy(connection);
104 dbus_register_object (connection,
105 proxy,
106 TYPE_DOCUMENT_INTERFACE,
107 &dbus_glib_document_interface_object_info,
108 name.c_str());
109 return strdup(name.c_str());
110 } //init_document
112 gchar *
113 dbus_init_desktop_interface (SPDesktop * dt)
114 {
115 DBusGConnection *connection;
116 DBusGProxy *proxy;
117 DocumentInterface *obj;
119 std::string name("/org/inkscape/desktop_");
120 std::stringstream out;
121 out << dt->dkey;
122 name.append(out.str());
124 //printf("DKEY: %d\n, NUMBER %d\n NAME: %s\n", dt->dkey, dt->number, name.c_str());
126 connection = dbus_get_connection();
127 proxy = dbus_get_proxy(connection);
129 obj = (DocumentInterface*) dbus_register_object (connection,
130 proxy, TYPE_DOCUMENT_INTERFACE,
131 &dbus_glib_document_interface_object_info, name.c_str());
132 obj->desk = dt;
133 obj->updates = TRUE;
135 return strdup(name.c_str());
136 }
138 gchar *
139 init_desktop (void) {
140 //this function will create a new desktop and call
141 //dbus_init_desktop_interface.
142 SPDesktop * dt = sp_file_new_default();
144 std::string name("/org/inkscape/desktop_");
145 std::stringstream out;
146 out << dt->dkey;
147 name.append(out.str());
148 return strdup(name.c_str());
149 } //init_desktop
153 } } } /* namespace Inkscape::Extension::Dbus */