summaryrefslogtreecommitdiff
path: root/src/yelp-base.c
blob: a755eb81741c444cca06951f92d08b280a9b3927 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001-2002 Mikael Hallendal <micke@imendio.com>
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Mikael Hallendal <micke@imendio.com>
 */

#include <config.h>

#include <dbus/dbus-glib-bindings.h>
#include <libgnomevfs/gnome-vfs.h>
#include <gdk/gdkx.h>
#define SN_API_NOT_YET_FROZEN
#include <libsn/sn-launchee.h>

#include <string.h>

#include "yelp-window.h"
#include "yelp-settings.h"
//#include "yelp-pager.h"
/*#include "yelp-toc-pager.h"*/
#include "yelp-base.h"
#include "yelp-bookmarks.h"
#include "server-bindings.h"

gboolean main_running;

#define YELP_BASE_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), YELP_TYPE_BASE, YelpBasePriv))

struct _YelpBasePriv {
	GNode  *toc_tree;
	gboolean private_session;

	GList  *index;
	GSList *windows;
};

static void           yelp_base_init                (YelpBase       *base);
static void           yelp_base_class_init          (YelpBaseClass  *klass);
static void           yelp_base_register_dbus       (YelpBase       *base);
static void           yelp_base_new_window_cb       (YelpWindow     *window,
						     const gchar    *uri,
						     YelpBase       *base);
static void           yelp_base_window_finalized_cb (YelpBase       *base,
						     YelpWindow     *window);


#define PARENT_TYPE G_TYPE_OBJECT
static GObjectClass *parent_class;

GType
yelp_base_get_type (void)
{
    static GType base_type = 0;

    if (!base_type) {
        static const GTypeInfo base_info = {
            sizeof (YelpBaseClass),
            NULL,
            NULL,
            (GClassInitFunc) yelp_base_class_init,
            NULL,
            NULL,
            sizeof (YelpBase),
            0,
            (GInstanceInitFunc) yelp_base_init,
        };

        base_type = g_type_register_static (G_TYPE_OBJECT,
                                              "YelpBase",
                                              &base_info, 0);
    }

    return base_type;
}

static void
yelp_base_init (YelpBase *base)
{
        YelpBasePriv *priv;

        base->priv = priv = YELP_BASE_GET_PRIVATE (base);
	priv->toc_tree = g_node_new (NULL);
	priv->index    = NULL;
	priv->windows  = NULL;

	yelp_bookmarks_init ();
	yelp_settings_init ();
}

static void
yelp_base_class_init (YelpBaseClass *klass)
{
	parent_class = gtk_type_class (PARENT_TYPE);

	main_running = TRUE;

	g_type_class_add_private (klass, sizeof (YelpBasePriv));
}

gboolean
server_new_window (YelpBase *base, gchar *url, gchar *timestamp, 
			GError **error)
{
	GtkWidget *new_window;

	new_window = yelp_base_new_window (base, url, timestamp);
	gtk_widget_show (new_window);
	return TRUE;
}

gboolean
server_get_url_list (YelpBase *server, gchar **urls, GError **error)
{
#if 0
	gint len,  i;
	GSList *node;
	YelpDocInfo *doc_info;
	gchar *uri;
	YelpBasePriv *priv;

	priv = server->priv;

	len  = g_slist_length (priv->windows);

	node = priv->windows;

	doc_info = yelp_window_get_doc_info (YELP_WINDOW (node->data));
	uri = yelp_doc_info_get_uri (doc_info, NULL, 
				     YELP_URI_TYPE_ANY);
	*urls = g_strdup (uri);
	g_free (uri);
	node = node->next;

	for (i = 0; node; node = node->next, i++) {
		gchar *list;
		doc_info = yelp_window_get_doc_info (YELP_WINDOW (node->data));
		uri = yelp_doc_info_get_uri (doc_info, NULL, 
					     YELP_URI_TYPE_ANY);
		list = g_strconcat (*urls, ";", uri, NULL);
		g_free (*urls);
		*urls = g_strdup (list);
		g_free (list);
		g_free (uri);
	}
	return TRUE;
#endif
}

static void
yelp_base_new_window_cb (YelpWindow *window, const gchar *uri, 
			 YelpBase *base)
{
	GtkWidget *new_window;
	
	g_return_if_fail (YELP_IS_WINDOW (window));
	g_return_if_fail (YELP_IS_BASE (base));
	
	new_window = yelp_base_new_window (base, uri, NULL);

	gtk_widget_show (new_window);
}

static void
yelp_base_window_finalized_cb (YelpBase *base, YelpWindow *window)
{
	YelpBasePriv *priv;
	
	g_return_if_fail (YELP_IS_BASE (base));

	priv = base->priv;
	
	priv->windows = g_slist_remove (priv->windows, window);

	if (g_slist_length (priv->windows) == 0) {
		main_running = FALSE;
		gtk_main_quit ();
	}
}

YelpBase *
yelp_base_new (gboolean priv)
{
        YelpBase     *base;
	
        base = g_object_new (YELP_TYPE_BASE, NULL);
	if (!priv)
		yelp_base_register_dbus (base);
	base->priv->private_session = priv;
	//yelp_toc_pager_init ();

        return base;
}

static void
sn_error_trap_push (SnDisplay *display,
		    Display *xdisplay)
{
	gdk_error_trap_push ();
}

static void
sn_error_trap_pop (SnDisplay *display,
		   Display *xdisplay)
{
	gdk_error_trap_pop ();
}

GtkWidget *
yelp_base_new_window (YelpBase *base, const gchar *uri, const gchar *startup_id)
{
	YelpBasePriv *priv;
	GtkWidget    *window;
	SnDisplay *sn_display = NULL;
	GdkScreen *screen = NULL;
	GdkDisplay *display = NULL;
	SnLauncheeContext *context = NULL;

        g_return_val_if_fail (YELP_IS_BASE (base), NULL);

	priv = base->priv;
        
        window = yelp_window_new (priv->toc_tree, priv->index);
	gtk_widget_realize (GTK_WIDGET (window));

	if (startup_id) {
		screen = gtk_window_get_screen (GTK_WINDOW (window));
		display = gdk_screen_get_display (screen);
		
		sn_display = 
			sn_display_new (gdk_x11_display_get_xdisplay (display),
					sn_error_trap_push, sn_error_trap_pop);
		context = sn_launchee_context_new (sn_display,
					   gdk_screen_get_number (screen),
						   startup_id);
		if (strncmp (sn_launchee_context_get_startup_id (context), 
			     "_TIME", 5) != 0)
			sn_launchee_context_setup_window (context,
					 GDK_WINDOW_XWINDOW (window->window));

		if (sn_launchee_context_get_id_has_timestamp (context)) {
			gulong time;

			time = sn_launchee_context_get_timestamp (context);
			gdk_x11_window_set_user_time (window->window, time);
		}		
	}
	        
	priv->windows = g_slist_prepend (priv->windows, window);

	g_object_weak_ref (G_OBJECT (window),
			   (GWeakNotify) yelp_base_window_finalized_cb,
			   base);
	
	g_signal_connect (window, "new_window_requested",
			  G_CALLBACK (yelp_base_new_window_cb),
			  base);

	gtk_widget_show (window);

	if (uri && uri[0] != '\0')
		yelp_window_load (YELP_WINDOW (window), uri);
	else
		yelp_window_load (YELP_WINDOW (window), "x-yelp-toc:");

	if (context) {
		sn_launchee_context_complete (context);
		sn_launchee_context_unref (context);
		sn_display_unref (sn_display);		
	}

	return window;
}

static void
yelp_base_register_dbus (YelpBase *base)
{
	GError *error = NULL;
        DBusGProxy *driver_proxy;
	YelpBaseClass *klass = YELP_BASE_GET_CLASS (base);
        guint request_ret;
	YelpBasePriv *priv;

	priv = base->priv;
	
        klass->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
        if (klass->connection == NULL) {
		g_warning("Unable to connect to dbus: %s", error->message);
		g_error_free (error);
		return;
	}

        dbus_g_object_type_install_info (YELP_TYPE_BASE,
                                         &dbus_glib_server_object_object_info);
	
	dbus_g_connection_register_g_object (klass->connection,
                                             "/org/gnome/YelpService",
                                             G_OBJECT (base));

        driver_proxy = dbus_g_proxy_new_for_name (klass->connection,
                                                  DBUS_SERVICE_DBUS,
                                                  DBUS_PATH_DBUS,
                                                  DBUS_INTERFACE_DBUS);
	
        if(!org_freedesktop_DBus_request_name (driver_proxy,
                                               "org.gnome.YelpService",
                                               0, &request_ret,
                                               &error)) {
		g_warning("Unable to register service: %s", error->message);
		g_error_free (error);
	}
        g_object_unref (driver_proxy);

}