summaryrefslogtreecommitdiff
path: root/src/gnome2/ipc.c
blob: cbca8058400e53e18f541f109a03fa93cc775be0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Xiphos Bible Study Tool
 * ipc.c - Interprocess Communication - dbus integration
 *
 * Copyright (C) 2000-2016 Xiphos Developer Team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:ipc
 * @short_description: Interprocess Communication - dbus integration
 *
 * An #IpcObject is a gobject that publishes signals and methods over
 * dbus. It allows external control of Xiphos via dbus.
 *
 * sample client programs are in xiphos/src/examples/ipc_client.c
 * and xiphos/src/examples/ipc_client.py
 */

#include <glib.h>
#include <dbus/dbus-glib.h>
#include <stdlib.h>
#include "gui/ipc.h"
#include "marshal.h"

G_DEFINE_TYPE(IpcObject, ipc_object, G_TYPE_OBJECT)

gboolean ipc_object_set_current_reference(IpcObject *obj,
					  gchar *reference,
					  GError **error);
gboolean ipc_object_get_current_reference(IpcObject *obj,
					  gchar **reference,
					  GError **error);
gboolean ipc_object_get_next_search_reference(IpcObject *obj,
					      gchar **reference,
					      GError **error);

#include "ipc-interface.h"
#include "main/url.hh"

static IpcObject *main_ipc_obj;

static void ipc_object_init(IpcObject *obj)
{
	g_assert(obj != NULL);
	obj->references = NULL;
	obj->current_ref = NULL;
}

static void ipc_object_class_init(IpcObjectClass *klass)
{

	/**
	 * IpcObject::search_performed_signal:
	 * @hits: the number of search results
	 *
	 * The search_performed_signal is emitted any time
	 * Xiphos performs a sidebar search; it is intended
	 * to be listened to and handled over dbus
	 *
	 * Since: 3.2
	 */
	klass->signals[SEARCH_PERFORMED] =
	    g_signal_new("search-performed-signal",
			 G_OBJECT_CLASS_TYPE(klass),
			 G_SIGNAL_RUN_LAST,
			 0,
			 NULL,
			 NULL,
			 ipc_marshal_VOID__STRING_INT,
			 G_TYPE_NONE, 1, G_TYPE_STRING);
	/**
	 * IpcObject::navigation_signal:
	 * @reference: the new reference
	 *
	 * The navigation_signal is emitted any time Xiphos
	 * navigates to a new Bible reference; it is intended
	 * to be listened to and handled over dbus
	 *
	 * Since: 3.2
	 */
	klass->signals[NAVIGATION] =
	    g_signal_new("navigation-signal",
			 G_OBJECT_CLASS_TYPE(klass),
			 G_SIGNAL_RUN_LAST,
			 0,
			 NULL,
			 NULL,
			 g_cclosure_marshal_VOID__STRING,
			 G_TYPE_NONE, 1, G_TYPE_STRING);

	dbus_g_object_type_install_info(IPC_TYPE_OBJECT,
					&dbus_glib_ipc_object_object_info);
}

/**
 * ipc_object_search_performed:
 * @obj: an #IpcObject
 * @search_term: the search term
 * @hits: number of search results
 * @error: GErorr
 *
 * makes the IpcObject emit a SEARCH_PERFORMED signal which is then
 * published over dbus
 *
 * Since: 3.2
 */
gboolean ipc_object_search_performed(IpcObject *obj,
				     const gchar *search_term,
				     const int *hits, GError **error)
{

	IpcObjectClass *klass = IPC_OBJECT_GET_CLASS(obj);

	GString *s = g_string_new("");
	g_string_printf(s, "%d", *hits);

	g_signal_emit(obj,
		      klass->signals[SEARCH_PERFORMED], 0, s->str, hits);
	return FALSE;
}

/**
 * ipc_object_navigation_signal:
 * @obj: an #IpcObject
 * @reference: a gchar with the new reference
 * @error: GError (pass NULL)
 *
 * makes the IpcObject emit a NAVIGATION signal which is then
 * published over dbus
 *
 * Since: 3.2
 */

gboolean ipc_object_navigation_signal(IpcObject *obj,
				      const gchar *reference,
				      GError **error)
{
	obj->current_ref = g_strdup(reference);

	IpcObjectClass *klass = IPC_OBJECT_GET_CLASS(obj);

	g_signal_emit(obj, klass->signals[NAVIGATION], 0, reference);
	return FALSE;
}

/**
 * ipc_object_get_next_search_reference:
 * @obj: an #IpcObject
 * @reference: the return reference
 * @error: GError
 *
 * to be called from dbus; returns all the current search
 * results; returns XIPHOS_SEARCH_END when all have been retrieved
 *
 * Since: 3.2
 */

gboolean ipc_object_get_next_search_reference(IpcObject *obj,
					      gchar **reference,
					      GError **error)
{
	if (obj->references)
		*reference = g_strdup((gchar *)obj->references->data);
	else
		*reference = g_strdup("XIPHOS_SEARCH_END");

	obj->references = g_list_next(obj->references);
	return TRUE;
}

/**
 * ipc_object_set_current_reference:
 * @obj: an #IpcObject
 * @reference: the reference to set
 * @error: GError
 *
 * to be called from dbus; sets the current reference in Xiphos;
 * calls main_url_handler to handle the navigation
 *
 * Since: 3.2
 */
gboolean ipc_object_set_current_reference(IpcObject *obj,
					  gchar *reference,
					  GError **error)
{
	//easy route
	main_url_handler((const gchar *)reference, TRUE);
	//it should be done like this, probably
	/* g_signal_emit(obj,
	   "navigate-requested",
	   reference); */

	return TRUE;
}

/**
 * ipc_object_get_current_reference:
 * @obj: an #IpcObject
 * @reference: the return reference
 * @error: GError
 *
 * to be called from dbus; gets the current reference in Xiphos;
 *
 * Since: 3.2
 */
gboolean ipc_object_get_current_reference(IpcObject *obj,
					  gchar **reference,
					  GError **error)
{
	*reference = g_strdup(obj->current_ref);
	return TRUE;
}

/**
 * ipc_init_dbus_connection
 * @obj: an #IpcObject
 *
 * creates a new IpcObject and initiates the d-bus connection;
 * sets the single static object to this new object
 *
 * Since: 3.2
 */
IpcObject *ipc_init_dbus_connection(IpcObject *obj)
{
	DBusGConnection *bus = NULL;
	DBusGProxy *busProxy = NULL;
	obj = NULL;
	guint result;
	GError *error = NULL;

	bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
	if (error != NULL)
		return FALSE;

	busProxy = dbus_g_proxy_new_for_name(bus,
					     "org.freedesktop.DBus",
					     "/org/freedesktop/DBus",
					     "org.freedesktop.DBus");
	if (busProxy == NULL)
		return NULL;

	if (!dbus_g_proxy_call(busProxy,
			       "RequestName",
			       &error,
			       G_TYPE_STRING,
			       "org.xiphos.remote",
			       G_TYPE_UINT,
			       0,
			       G_TYPE_INVALID,
			       G_TYPE_UINT, &result, G_TYPE_INVALID)) {
		return NULL;
	}

	if (result != 1)
		return NULL;

	obj = g_object_new(IPC_TYPE_OBJECT, NULL);

	dbus_g_connection_register_g_object(bus,
					    "/org/xiphos/remote/ipc",
					    G_OBJECT(obj));

	main_ipc_obj = obj;

	return obj;
}

/**
 * ipc_get_main_ipc:
 *
 * returns the single static IpcObject
 *
 * Since: 3.2
 */
IpcObject *ipc_get_main_ipc(void)
{
	return main_ipc_obj;
}