summaryrefslogtreecommitdiff
path: root/src/yelp-base.c
blob: 9bb9de6e90a2183a82988a5ec787d8aa16732851 (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
/* -*- 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 <bonobo/bonobo-main.h>

#include <string.h>

#include "yelp-cache.h"
#include "yelp-window.h"
#include "yelp-section.h"
#include "yelp-scrollkeeper.h"
#include "yelp-man.h"
#include "yelp-info.h"
#include "yelp-base.h"

struct _YelpBasePriv {
	GNode  *toc_tree;

	GList  *index;
	GSList *windows;
};

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


#define PARENT_TYPE BONOBO_OBJECT_TYPE
static BonoboObjectClass *parent_class;

static void
impl_Yelp_newWindow (PortableServer_Servant  servant,
		     const CORBA_char       *url,
		     CORBA_Environment      *ev)
{
	YelpBase  *yelp_base;
	
	yelp_base = YELP_BASE (bonobo_object (servant));

	yelp_base_new_window (yelp_base, url);
}

static GNOME_Yelp_WindowList *
impl_Yelp_getWindows (PortableServer_Servant  servant,
		      CORBA_Environment      *ev)
{
	YelpBase              *base;
	YelpBasePriv          *priv;
	GNOME_Yelp_WindowList *list;
	gint                   len, i;
	GSList                *node;
	YelpURI               *uri;
	
	base = YELP_BASE (bonobo_object (servant));
	priv = base->priv;

	len  = g_slist_length (priv->windows);
	
	list = GNOME_Yelp_WindowList__alloc ();
	list->_buffer = CORBA_sequence_CORBA_string_allocbuf (len);
	list->_length = len;
	list->_maximum = len;
	CORBA_sequence_set_release (list, CORBA_TRUE);
	
	for (node = priv->windows, i = 0; node; node = node->next, i++) {
		gchar *str_uri;

		uri = yelp_window_get_current_uri (YELP_WINDOW (node->data));
		str_uri = yelp_uri_to_string (uri);
		list->_buffer[i] = CORBA_string_dup (str_uri);
		g_free (str_uri);
	}
	
	return list;
}

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

        priv = g_new0 (YelpBasePriv, 1);
        
	priv->toc_tree = g_node_new (NULL);
	priv->index    = NULL;
	priv->windows  = NULL;
        base->priv     = priv;

	yelp_cache_init ();
}

static void
yelp_base_class_init (YelpBaseClass *klass)
{
	POA_GNOME_Yelp__epv *epv = &klass->epv;
	
	parent_class = gtk_type_class (PARENT_TYPE);

	epv->newWindow  = impl_Yelp_newWindow;
	epv->getWindows = impl_Yelp_getWindows;
}

static void
yelp_base_new_window_cb (YelpWindow *window, 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, NULL);
	
	gtk_widget_show_all (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) {
		bonobo_main_quit ();
	}
}

YelpBase *
yelp_base_new (void)
{
        YelpBase     *base;
	YelpBasePriv *priv;
	
        base = g_object_new (YELP_TYPE_BASE, NULL);
	priv = base->priv;
	
	yelp_scrollkeeper_init (priv->toc_tree, &priv->index);
	yelp_man_init (base->priv->toc_tree, &priv->index);
	yelp_info_init (base->priv->toc_tree, &priv->index);
	
	priv->index = g_list_sort (priv->index, yelp_section_compare);

        return base;
}

GtkWidget *
yelp_base_new_window (YelpBase *base, const gchar *str_uri)
{
	YelpBasePriv *priv;
	YelpURI      *uri;
	GtkWidget    *window;
        
        g_return_val_if_fail (YELP_IS_BASE (base), NULL);

	priv = base->priv;
        
        window = yelp_window_new (priv->toc_tree, priv->index);
        
	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_all (window);

	if (str_uri && strcmp (str_uri, ""))
		uri = yelp_uri_new (str_uri);
	else
		uri = yelp_uri_new ("toc:");

	yelp_window_open_uri (YELP_WINDOW (window), uri);

	return window;
}

BONOBO_TYPE_FUNC_FULL (YelpBase, GNOME_Yelp, PARENT_TYPE, yelp_base);