1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2001-2002 Mikael Hallendal <micke@codefactory.se> |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or |
---|
6 | * modify it under the terms of the GNU General Public License as |
---|
7 | * published by the Free Software Foundation; either version 2 of the |
---|
8 | * License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | * General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public |
---|
16 | * License along with this program; if not, write to the |
---|
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
18 | * Boston, MA 02111-1307, USA. |
---|
19 | * |
---|
20 | * Author: Mikael Hallendal <micke@codefactory.se> |
---|
21 | */ |
---|
22 | |
---|
23 | #ifdef HAVE_CONFIG_H |
---|
24 | #include <config.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | #include <gtk/gtkmain.h> |
---|
28 | #include <gtk/gtkwidget.h> |
---|
29 | #include <bonobo/bonobo-context.h> |
---|
30 | #include <bonobo/bonobo-exception.h> |
---|
31 | #include <bonobo/bonobo-generic-factory.h> |
---|
32 | #include <bonobo/bonobo-main.h> |
---|
33 | #include <libgnome/gnome-i18n.h> |
---|
34 | #include <libgnome/gnome-program.h> |
---|
35 | #include <libgnomeui/gnome-ui-init.h> |
---|
36 | #include <libgnomevfs/gnome-vfs.h> |
---|
37 | #include <libgnomeui/gnome-client.h> |
---|
38 | #include <stdlib.h> |
---|
39 | |
---|
40 | #include "GNOME_Yelp.h" |
---|
41 | #include "yelp-window.h" |
---|
42 | #include "yelp-base.h" |
---|
43 | |
---|
44 | #define YELP_FACTORY_OAFIID "OAFIID:GNOME_Yelp_Factory" |
---|
45 | |
---|
46 | poptContext poptCon; |
---|
47 | gint next_opt; |
---|
48 | |
---|
49 | /*structure defining command line option.*/ |
---|
50 | |
---|
51 | struct poptOption options[] = { |
---|
52 | { |
---|
53 | "url", |
---|
54 | 'u', |
---|
55 | POPT_ARG_STRING, |
---|
56 | NULL, |
---|
57 | 1, |
---|
58 | NULL, |
---|
59 | NULL, |
---|
60 | }, |
---|
61 | NULL |
---|
62 | }; |
---|
63 | |
---|
64 | static BonoboObject * main_base_factory (BonoboGenericFactory *factory, |
---|
65 | const gchar *iid, |
---|
66 | gpointer closure); |
---|
67 | static CORBA_Object main_activate_base (void); |
---|
68 | static void main_open_new_window (CORBA_Object yelp_base, |
---|
69 | const gchar *url); |
---|
70 | static void main_start (gchar *url); |
---|
71 | static gboolean main_idle_start (gchar *url); |
---|
72 | static int main_save_session (GnomeClient *client, |
---|
73 | gint phase, |
---|
74 | GnomeRestartStyle rstyle, |
---|
75 | gint shutdown, |
---|
76 | GnomeInteractStyle istyle, |
---|
77 | gint fast, |
---|
78 | gpointer cdata); |
---|
79 | |
---|
80 | static void main_client_die (GnomeClient *client, |
---|
81 | gpointer cdata); |
---|
82 | |
---|
83 | static gboolean main_restore_session (void); |
---|
84 | |
---|
85 | |
---|
86 | static BonoboObject * |
---|
87 | main_base_factory (BonoboGenericFactory *factory, |
---|
88 | const gchar *iid, |
---|
89 | gpointer closure) |
---|
90 | { |
---|
91 | static YelpBase *yelp_base = NULL; |
---|
92 | |
---|
93 | if (!yelp_base) { |
---|
94 | yelp_base = yelp_base_new (); |
---|
95 | } else { |
---|
96 | bonobo_object_ref (BONOBO_OBJECT (yelp_base)); |
---|
97 | } |
---|
98 | |
---|
99 | return BONOBO_OBJECT (yelp_base); |
---|
100 | } |
---|
101 | |
---|
102 | static CORBA_Object |
---|
103 | main_activate_base () |
---|
104 | { |
---|
105 | CORBA_Environment ev; |
---|
106 | CORBA_Object yelp_base; |
---|
107 | |
---|
108 | CORBA_exception_init (&ev); |
---|
109 | |
---|
110 | yelp_base = bonobo_activation_activate_from_id ("OAFIID:GNOME_Yelp", |
---|
111 | 0, NULL, &ev); |
---|
112 | |
---|
113 | if (BONOBO_EX (&ev) || yelp_base == CORBA_OBJECT_NIL) { |
---|
114 | g_error (_("Could not activate Yelp: '%s'"), |
---|
115 | bonobo_exception_get_text (&ev)); |
---|
116 | } |
---|
117 | |
---|
118 | CORBA_exception_free (&ev); |
---|
119 | |
---|
120 | return yelp_base; |
---|
121 | } |
---|
122 | |
---|
123 | static void |
---|
124 | main_open_new_window (CORBA_Object yelp_base, const gchar *url) |
---|
125 | { |
---|
126 | CORBA_Environment ev; |
---|
127 | |
---|
128 | CORBA_exception_init (&ev); |
---|
129 | |
---|
130 | GNOME_Yelp_newWindow (yelp_base, url, &ev); |
---|
131 | |
---|
132 | if (BONOBO_EX (&ev)) { |
---|
133 | g_error (_("Could not open new window.")); |
---|
134 | } |
---|
135 | |
---|
136 | CORBA_exception_free (&ev); |
---|
137 | } |
---|
138 | |
---|
139 | static void |
---|
140 | main_start (gchar *url) |
---|
141 | { |
---|
142 | CORBA_Object yelp_base; |
---|
143 | |
---|
144 | yelp_base = main_activate_base (); |
---|
145 | |
---|
146 | if (!yelp_base) { |
---|
147 | g_error ("Couldn't activate YelpBase"); |
---|
148 | } |
---|
149 | |
---|
150 | main_open_new_window (yelp_base, url); |
---|
151 | |
---|
152 | bonobo_object_release_unref (yelp_base, NULL); |
---|
153 | |
---|
154 | if (url) { |
---|
155 | g_free (url); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | static gboolean |
---|
160 | main_idle_start (gchar *url) |
---|
161 | { |
---|
162 | CORBA_Object yelp_base; |
---|
163 | |
---|
164 | yelp_base = main_activate_base (); |
---|
165 | |
---|
166 | if (!yelp_base) { |
---|
167 | g_error ("Couldn't activate YelpBase"); |
---|
168 | } |
---|
169 | |
---|
170 | main_open_new_window (yelp_base, url); |
---|
171 | |
---|
172 | if (url) { |
---|
173 | g_free (url); |
---|
174 | } |
---|
175 | |
---|
176 | return FALSE; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | static gint |
---|
181 | main_save_session (GnomeClient *client, |
---|
182 | gint phase, |
---|
183 | GnomeRestartStyle rstyle, |
---|
184 | gint shutdown, |
---|
185 | GnomeInteractStyle istyle, |
---|
186 | gint fast, |
---|
187 | gpointer cdata) |
---|
188 | { |
---|
189 | |
---|
190 | GNOME_Yelp_WindowList *list; |
---|
191 | CORBA_Environment ev; |
---|
192 | CORBA_Object yelp_base; |
---|
193 | gchar **argv; |
---|
194 | gint i=1; |
---|
195 | gint temp; |
---|
196 | |
---|
197 | CORBA_exception_init (&ev); |
---|
198 | |
---|
199 | yelp_base = main_activate_base (); |
---|
200 | |
---|
201 | list = GNOME_Yelp_getWindows (yelp_base, &ev); |
---|
202 | |
---|
203 | bonobo_object_release_unref (yelp_base, NULL); |
---|
204 | |
---|
205 | temp = list->_length; |
---|
206 | |
---|
207 | temp = temp + 1; |
---|
208 | |
---|
209 | argv = g_malloc0 (sizeof (gchar *) * temp); |
---|
210 | |
---|
211 | argv[0] = (gchar*) cdata; |
---|
212 | |
---|
213 | /* Get the URI of each window */ |
---|
214 | |
---|
215 | for (i=0 ;i < list->_length; i++) { |
---|
216 | argv[i+1] = g_strconcat ("--url=", list->_buffer[i], NULL); |
---|
217 | } |
---|
218 | |
---|
219 | gnome_client_set_clone_command (client, temp, argv); |
---|
220 | gnome_client_set_restart_command (client, temp, argv); |
---|
221 | |
---|
222 | g_free (argv); |
---|
223 | |
---|
224 | return TRUE; |
---|
225 | } |
---|
226 | |
---|
227 | static void |
---|
228 | main_client_die (GnomeClient *client, |
---|
229 | gpointer cdata) |
---|
230 | { |
---|
231 | bonobo_main_quit (); |
---|
232 | } |
---|
233 | |
---|
234 | static gboolean |
---|
235 | main_restore_session (void) |
---|
236 | { |
---|
237 | CORBA_Object yelp_base; |
---|
238 | |
---|
239 | yelp_base = main_activate_base (); |
---|
240 | |
---|
241 | if (!yelp_base) { |
---|
242 | g_error ("Couldn't activate YelpBase"); |
---|
243 | } |
---|
244 | |
---|
245 | /*Get the argument of commandline option*/ |
---|
246 | |
---|
247 | while( (next_opt = poptGetNextOpt (poptCon)) > 0) { |
---|
248 | if ( next_opt == 1) { |
---|
249 | gchar *url = (gchar *) poptGetOptArg (poptCon); |
---|
250 | main_open_new_window (yelp_base, url); |
---|
251 | if (url) { |
---|
252 | g_free (url); |
---|
253 | } |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | return TRUE; |
---|
258 | } |
---|
259 | |
---|
260 | int |
---|
261 | main (int argc, char **argv) |
---|
262 | { |
---|
263 | GnomeProgram *program; |
---|
264 | CORBA_Object factory; |
---|
265 | gchar *url = NULL; |
---|
266 | GnomeClient *client; |
---|
267 | gboolean flag = FALSE; |
---|
268 | |
---|
269 | bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR); |
---|
270 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
---|
271 | textdomain(GETTEXT_PACKAGE); |
---|
272 | g_thread_init (NULL); |
---|
273 | |
---|
274 | if (argc >= 2) { |
---|
275 | url = g_strdup (argv[1]); |
---|
276 | } else { |
---|
277 | url = g_strdup (""); |
---|
278 | } |
---|
279 | |
---|
280 | program = gnome_program_init (PACKAGE, VERSION, |
---|
281 | LIBGNOMEUI_MODULE, |
---|
282 | argc, argv, |
---|
283 | GNOME_PROGRAM_STANDARD_PROPERTIES, |
---|
284 | NULL); |
---|
285 | |
---|
286 | gnome_vfs_init (); |
---|
287 | |
---|
288 | /*Commandline parsing is done here*/ |
---|
289 | |
---|
290 | poptCon = poptGetContext (PACKAGE, argc, (const gchar **) argv, options, 0); |
---|
291 | |
---|
292 | client = gnome_master_client (); |
---|
293 | g_signal_connect (client, "save_yourself", |
---|
294 | G_CALLBACK (main_save_session), (gpointer) argv[0]); |
---|
295 | g_signal_connect (client, "die", |
---|
296 | G_CALLBACK (main_client_die), NULL); |
---|
297 | |
---|
298 | factory = bonobo_activation_activate_from_id (YELP_FACTORY_OAFIID, |
---|
299 | Bonobo_ACTIVATION_FLAG_EXISTING_ONLY, |
---|
300 | NULL, NULL); |
---|
301 | |
---|
302 | /*Check for previous session to restore.*/ |
---|
303 | |
---|
304 | if(gnome_client_get_flags (client) & GNOME_CLIENT_RESTORED) { |
---|
305 | flag = TRUE; |
---|
306 | } |
---|
307 | |
---|
308 | if (!factory) { |
---|
309 | BonoboGenericFactory *factory; |
---|
310 | /* Not started, start now */ |
---|
311 | |
---|
312 | factory = bonobo_generic_factory_new (YELP_FACTORY_OAFIID, |
---|
313 | main_base_factory, |
---|
314 | NULL); |
---|
315 | |
---|
316 | bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (factory)); |
---|
317 | |
---|
318 | /*Depending on the flag, restore the session*/ |
---|
319 | |
---|
320 | if (flag) { |
---|
321 | g_idle_add ((GSourceFunc) main_restore_session, NULL); |
---|
322 | } else { |
---|
323 | g_idle_add ((GSourceFunc) main_idle_start, url); |
---|
324 | } |
---|
325 | |
---|
326 | bonobo_main (); |
---|
327 | } else { |
---|
328 | main_start (url); |
---|
329 | } |
---|
330 | |
---|
331 | return 0; |
---|
332 | } |
---|