summaryrefslogtreecommitdiffstats
path: root/src/extension/dbus/dbus-init.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/extension/dbus/dbus-init.cpp')
-rw-r--r--src/extension/dbus/dbus-init.cpp118
1 files changed, 90 insertions, 28 deletions
diff --git a/src/extension/dbus/dbus-init.cpp b/src/extension/dbus/dbus-init.cpp
index 9ff6897e5..19b48e10d 100644
--- a/src/extension/dbus/dbus-init.cpp
+++ b/src/extension/dbus/dbus-init.cpp
@@ -36,7 +36,14 @@
#include <sstream>
-
+namespace
+{
+ // This stores the bus name to use for this app instance. By default, it
+ // will be set to org.inkscape. However, users may provide other names by
+ // setting command-line parameters when starting Inkscape, so that more
+ // than one instance of Inkscape may be used by external scripts.
+ gchar *instance_bus_name = NULL;
+}
namespace Inkscape {
namespace Extension {
@@ -78,10 +85,53 @@ dbus_register_object (DBusGConnection *connection,
return object;
}
+/*
+ * PRIVATE register a document interface for the document in the given ActionContext, if none exists.
+ * Return the DBus path to the interface (something like /org/inkscape/document_0).
+ * Note that while a DocumentInterface could be used either for a document with no desktop, or a
+ * document with a desktop, this function is only used for creating interfaces in the former case.
+ * Desktop-associated DocumentInterfaces are named /org/inkscape/desktop_0, etc.
+ * FIXME: This state of affairs probably needs tidying up at some point in the future.
+ */
+static gchar *
+dbus_register_document(Inkscape::ActionContext const & target)
+{
+ SPDocument *doc = target.getDocument();
+ g_assert(doc != NULL);
+
+ // Document name is not suitable for DBus name, as it might contain invalid chars
+ std::string name("/org/inkscape/document_");
+ std::stringstream ss;
+ ss << doc->serial();
+ name.append(ss.str());
+
+ DBusGConnection *connection = dbus_get_connection();
+ DBusGProxy *proxy = dbus_get_proxy(connection);
+
+ // Has the document already been registered?
+ if (!dbus_g_connection_lookup_g_object(connection, name.c_str())) {
+ // No - register it
+ DocumentInterface *doc_interface = (DocumentInterface*) dbus_register_object (connection,
+ proxy,
+ TYPE_DOCUMENT_INTERFACE,
+ &dbus_glib_document_interface_object_info,
+ name.c_str());
+
+ // Set the document info for this interface
+ doc_interface->target = target;
+ }
+ return strdup(name.c_str());
+}
+
/* Initialize a Dbus service */
void
init (void)
{
+ if (instance_bus_name == NULL) {
+ // Set the bus name to the default
+ instance_bus_name = strdup("org.inkscape");
+ }
+
guint result;
GError *error = NULL;
DBusGConnection *connection;
@@ -89,7 +139,7 @@ init (void)
connection = dbus_get_connection();
proxy = dbus_get_proxy(connection);
org_freedesktop_DBus_request_name (proxy,
- "org.inkscape",
+ instance_bus_name,
DBUS_NAME_FLAG_DO_NOT_QUEUE, &result, &error);
//create interface for application
dbus_register_object (connection,
@@ -97,37 +147,36 @@ init (void)
TYPE_APPLICATION_INTERFACE,
&dbus_glib_application_interface_object_info,
DBUS_APPLICATION_INTERFACE_PATH);
-} //init
+}
gchar *
-init_document (void) {
- DBusGConnection *connection;
- DBusGProxy *proxy;
- SPDocument *doc;
-
- doc = SPDocument::createNewDoc(NULL, 1, TRUE);
-
- std::string name("/org/inkscape/");
- name.append(doc->getName());
- std::replace(name.begin(), name.end(), ' ', '_');
+init_document (void)
+{
+ // This is for command-line use only
+ g_assert(!inkscape_use_gui());
- connection = dbus_get_connection();
- proxy = dbus_get_proxy(connection);
+ // Create a blank document and get its selection model etc in an ActionContext
+ SPDocument *doc = SPDocument::createNewDoc(NULL, 1, TRUE);
+ inkscape_add_document(doc);
+ return dbus_register_document(inkscape_action_context_for_document(doc));
+}
- 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 *
+init_active_document()
+{
+ SPDocument *doc = inkscape_active_document();
+ if (!doc) {
+ return NULL;
+ }
+
+ return dbus_register_document(inkscape_active_action_context());
+}
gchar *
dbus_init_desktop_interface (SPDesktop * dt)
{
DBusGConnection *connection;
DBusGProxy *proxy;
- DocumentInterface *obj;
std::string name("/org/inkscape/desktop_");
std::stringstream out;
@@ -139,12 +188,12 @@ dbus_init_desktop_interface (SPDesktop * dt)
connection = dbus_get_connection();
proxy = dbus_get_proxy(connection);
- obj = (DocumentInterface*) dbus_register_object (connection,
+ DocumentInterface *doc_interface = (DocumentInterface*) dbus_register_object (connection,
proxy, TYPE_DOCUMENT_INTERFACE,
&dbus_glib_document_interface_object_info, name.c_str());
- obj->desk = dt;
- obj->updates = TRUE;
- dt->dbus_document_interface=obj;
+ doc_interface->target = Inkscape::ActionContext(dt);
+ doc_interface->updates = TRUE;
+ dt->dbus_document_interface=doc_interface;
return strdup(name.c_str());
}
@@ -159,8 +208,21 @@ init_desktop (void) {
out << dt->dkey;
name.append(out.str());
return strdup(name.c_str());
-} //init_desktop
+}
+void
+dbus_set_bus_name(gchar * bus_name)
+{
+ g_assert(bus_name != NULL);
+ g_assert(instance_bus_name == NULL);
+ instance_bus_name = strdup(bus_name);
+}
+gchar *
+dbus_get_bus_name()
+{
+ g_assert(instance_bus_name != NULL);
+ return instance_bus_name;
+}
} } } /* namespace Inkscape::Extension::Dbus */