File: dbus.c

package info (click to toggle)
evolution-rss 0.3.95~20140414-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,244 kB
  • ctags: 1,105
  • sloc: ansic: 16,443; makefile: 314; cpp: 224; xml: 133; sh: 50
file content (236 lines) | stat: -rw-r--r-- 5,868 bytes parent folder | download | duplicates (5)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*  Evoution RSS Reader Plugin
 *  Copyright (C) 2007-2011  Lucian Langa <cooly@gnome.eu.org>
 *
 *  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 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 Street, Fifth Floor, Boston, MA
 *  02110-1301 USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif


#include <string.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include <gio/gio.h>

extern int rss_verbose_debug;

#include "rss.h"
#include "rss-config-factory.h"
#include "misc.h"
#include "notification.h"
#include "dbus.h"

#define RSS_DBUS_PATH "/org/gnome/feed/Reader"
#define RSS_DBUS_SERVICE "org.gnome.feed.Reader"
#define RSS_DBUS_INTERFACE "org.gnome.feed.Reader"

gboolean subscribe_method(gchar *url);

static void
method_call_cb (GDBusConnection       *connection,
		const gchar           *sender,
		const gchar           *object_path,
		const gchar           *interface_name,
		const gchar           *method_name,
		GVariant              *parameters,
		GDBusMethodInvocation *invocation,
		gpointer               user_data);

static GDBusConnection *connection = NULL;

static GDBusNodeInfo *nodeinfo = NULL;

static const gchar introspection_xml[] =
	"<node name='/org/gnome/feed/Reader'>"
		"<interface name='org.gnome.feed.Reader'>"
			"<method name='Ping'>"
				"<arg name='result' type='b' direction='out'/>"
			"</method>"
			"<method name='Subscribe'>"
				"<arg name='url' type='s'/>"
				"<arg name='result' type='b' direction='out'/>"
			"</method>"
		"</interface>"
	"</node>";

static const GDBusInterfaceVTable vtable =
{
	method_call_cb,
	NULL,
	NULL
};

static gboolean enabled = FALSE;

extern rssfeed *rf;

static gboolean
reinit_dbus (gpointer user_data)
{
	if (!enabled || init_gdbus ())
		return FALSE;

	/* keep trying to re-establish dbus connection */

	return TRUE;
}

gboolean
subscribe_method(gchar *url)
{
	add_feed *feed = g_new0(add_feed, 1);
	feed->feed_url = url;
	feed->add=1;
	feed->enabled=feed->validate=1;
	feed->fetch_html = 0;
	if (feed->feed_url && strlen(feed->feed_url)) {
		g_print("New Feed received: %s\n", url);
		feed->feed_url = sanitize_url(feed->feed_url);
		d("sanitized feed URL: %s\n", feed->feed_url);
		if (g_hash_table_find(rf->hr, check_if_match, feed->feed_url)) {
			rss_error(NULL, NULL, _("Error adding feed."),
				_("Feed already exists!"));
				//return FALSE;
				/* we return true here since org.gnome.feed.Reader
				 * doesn't support status */
				return TRUE;
		}
		if (setup_feed(feed)) {
			gchar *msg = g_strdup_printf(_("Importing URL: %s"),
					feed->feed_url);
			taskbar_push_message(msg);
			g_free(msg);
		}
		if (rf->treeview)
			store_redraw(GTK_TREE_VIEW(rf->treeview));
		save_gconf_feed();
#if (DATASERVER_VERSION >= 2033001)
		camel_operation_pop_message (NULL);
#else
	camel_operation_end(NULL);
#endif
	}
	g_free(url);
	return TRUE;
}

static void
connection_closed_cb (GDBusConnection *pconnection,
	gboolean remote_peer_vanished, GError *error,
	gpointer user_data)
{
	g_return_if_fail (connection != pconnection);
	g_object_unref (connection);
	connection = NULL;

	g_timeout_add (3000, reinit_dbus, NULL);
}

static void
method_call_cb (GDBusConnection       *connection,
		const gchar           *sender,
		const gchar           *object_path,
		const gchar           *interface_name,
		const gchar           *method_name,
		GVariant              *parameters,
		GDBusMethodInvocation *invocation,
		gpointer               user_data)
{
	gchar *url;
	gboolean res = FALSE;
	d("method:%s\n", method_name);
	if (!g_strcmp0 (method_name, "Subscribe")) {
		g_variant_get (parameters, "(s)", &url);
		res = subscribe_method(url);
		g_dbus_method_invocation_return_value (invocation,
			g_variant_new ("(b)", res));
	}
	if (!g_strcmp0 (method_name, "Ping")) {
		g_dbus_method_invocation_return_value (invocation,
			g_variant_new ("(b)", TRUE));
	}
}

static void
on_bus_acquired (GDBusConnection *connection,
		const gchar     *name,
		gpointer         user_data)
{
	guint reg_id;
	GError     *error = NULL;
	nodeinfo = g_dbus_node_info_new_for_xml (introspection_xml,
				NULL);

	reg_id = g_dbus_connection_register_object (connection,
		RSS_DBUS_PATH,
		nodeinfo->interfaces[0],
		&vtable,
		NULL, NULL,
		&error);
	if (!reg_id) {
		g_printerr ("Failed to register bus object: %s\n", error->message);
		g_error_free (error);
	}
}

static void
on_name_acquired (GDBusConnection *connection,
		const gchar     *name,
		gpointer         user_data)
{
	d("Name aquired.\n");
}

static void
on_name_lost (GDBusConnection *connection,
		const gchar     *name,
		gpointer         user_data)
{
	d("Name lost.\n");
}

gboolean
init_gdbus (void)
{
	GError *error = NULL;

	connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
	if (error) {
		g_warning ("could not get system bus: %s\n", error->message);
		g_error_free (error);
		return FALSE;
	}

	g_dbus_connection_set_exit_on_close (connection, FALSE);
	g_signal_connect (connection, "closed",
		G_CALLBACK (connection_closed_cb), NULL);

	g_bus_own_name (G_BUS_TYPE_SESSION,
		RSS_DBUS_SERVICE,
		G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
		on_bus_acquired,
		on_name_acquired,
		on_name_lost,
		NULL,
		NULL);

	return FALSE;
}