1 | /* Wombat personal information server - main file |
---|
2 | * |
---|
3 | * Author: Nat Friedman <nat@ximian.com> |
---|
4 | * |
---|
5 | * Copyright 2000, Ximian, Inc. |
---|
6 | */ |
---|
7 | |
---|
8 | #ifdef HAVE_CONFIG_H |
---|
9 | #include <config.h> |
---|
10 | #endif |
---|
11 | |
---|
12 | /* define this if you need/want to be able to send USR2 to wombat and |
---|
13 | get a list of the active backends */ |
---|
14 | /*#define DEBUG_BACKENDS*/ |
---|
15 | |
---|
16 | #include <stdlib.h> |
---|
17 | #ifdef DEBUG_BACKENDS |
---|
18 | #include <sys/signal.h> |
---|
19 | #endif |
---|
20 | #include <glib.h> |
---|
21 | #include <libgnome/gnome-defs.h> |
---|
22 | #include <libgnome/gnome-i18n.h> |
---|
23 | #include <libgnomeui/gnome-init.h> |
---|
24 | #include <liboaf/liboaf.h> |
---|
25 | #include <libgnomevfs/gnome-vfs-init.h> |
---|
26 | #include <bonobo/bonobo-main.h> |
---|
27 | |
---|
28 | #include "pas/pas-book-factory.h" |
---|
29 | #include "pas/pas-backend-file.h" |
---|
30 | |
---|
31 | #include "calendar/pcs/cal-factory.h" |
---|
32 | #include "calendar/pcs/cal-backend-file.h" |
---|
33 | |
---|
34 | #ifdef HAVE_LDAP |
---|
35 | #include "pas/pas-backend-ldap.h" |
---|
36 | #endif |
---|
37 | |
---|
38 | #include "wombat-moniker.h" |
---|
39 | #include "wombat-private-moniker.h" |
---|
40 | |
---|
41 | #define CAL_FACTORY_OAF_ID "OAFIID:GNOME_Evolution_Wombat_CalendarFactory" |
---|
42 | #define PAS_BOOK_FACTORY_OAF_ID "OAFIID:GNOME_Evolution_Wombat_ServerFactory" |
---|
43 | |
---|
44 | /* The and addressbook calendar factories */ |
---|
45 | |
---|
46 | static CalFactory *cal_factory; |
---|
47 | static PASBookFactory *pas_book_factory; |
---|
48 | |
---|
49 | /* Timeout interval in milliseconds for termination */ |
---|
50 | #define EXIT_TIMEOUT 5000 |
---|
51 | |
---|
52 | /* Timeout ID for termination handler */ |
---|
53 | static guint termination_handler_id; |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | /* Termination */ |
---|
58 | |
---|
59 | /* Termination handler. Checks if both factories have zero running backends, |
---|
60 | * and if so terminates the program. |
---|
61 | */ |
---|
62 | static gboolean |
---|
63 | termination_handler (gpointer data) |
---|
64 | { |
---|
65 | if (cal_factory_get_n_backends (cal_factory) == 0 |
---|
66 | && pas_book_factory_get_n_backends (pas_book_factory) == 0) { |
---|
67 | fprintf (stderr, "termination_handler(): Terminating the Wombat. Have a nice day.\n"); |
---|
68 | gtk_main_quit (); |
---|
69 | } |
---|
70 | |
---|
71 | termination_handler_id = 0; |
---|
72 | return FALSE; |
---|
73 | } |
---|
74 | |
---|
75 | /* Queues a timeout for handling termination of Wombat */ |
---|
76 | static void |
---|
77 | queue_termination (void) |
---|
78 | { |
---|
79 | if (termination_handler_id) |
---|
80 | return; |
---|
81 | |
---|
82 | termination_handler_id = g_timeout_add (EXIT_TIMEOUT, termination_handler, NULL); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | static void |
---|
88 | last_book_gone_cb (PASBookFactory *factory, gpointer data) |
---|
89 | { |
---|
90 | queue_termination (); |
---|
91 | } |
---|
92 | |
---|
93 | static gboolean |
---|
94 | setup_pas (int argc, char **argv) |
---|
95 | { |
---|
96 | pas_book_factory = pas_book_factory_new (); |
---|
97 | |
---|
98 | if (!pas_book_factory) |
---|
99 | return FALSE; |
---|
100 | |
---|
101 | pas_book_factory_register_backend ( |
---|
102 | pas_book_factory, "file", pas_backend_file_new); |
---|
103 | |
---|
104 | #ifdef HAVE_LDAP |
---|
105 | pas_book_factory_register_backend ( |
---|
106 | pas_book_factory, "ldap", pas_backend_ldap_new); |
---|
107 | #endif |
---|
108 | |
---|
109 | gtk_signal_connect (GTK_OBJECT (pas_book_factory), |
---|
110 | "last_book_gone", |
---|
111 | GTK_SIGNAL_FUNC (last_book_gone_cb), |
---|
112 | NULL); |
---|
113 | |
---|
114 | if (!pas_book_factory_activate (pas_book_factory, PAS_BOOK_FACTORY_OAF_ID)) { |
---|
115 | bonobo_object_unref (BONOBO_OBJECT (pas_book_factory)); |
---|
116 | pas_book_factory = NULL; |
---|
117 | return FALSE; |
---|
118 | } |
---|
119 | |
---|
120 | return TRUE; |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | /* Personal calendar server */ |
---|
126 | |
---|
127 | /* Callback used when the calendar factory has no more running backends */ |
---|
128 | static void |
---|
129 | last_calendar_gone_cb (CalFactory *factory, gpointer data) |
---|
130 | { |
---|
131 | queue_termination (); |
---|
132 | } |
---|
133 | |
---|
134 | /* Creates the calendar factory object and registers it */ |
---|
135 | static gboolean |
---|
136 | setup_pcs (int argc, char **argv) |
---|
137 | { |
---|
138 | cal_factory = cal_factory_new (); |
---|
139 | |
---|
140 | if (!cal_factory) { |
---|
141 | g_message ("setup_pcs(): Could not create the calendar factory"); |
---|
142 | return FALSE; |
---|
143 | } |
---|
144 | |
---|
145 | cal_factory_register_method (cal_factory, "file", CAL_BACKEND_FILE_TYPE); |
---|
146 | |
---|
147 | if (!cal_factory_oaf_register (cal_factory, CAL_FACTORY_OAF_ID)) { |
---|
148 | bonobo_object_unref (BONOBO_OBJECT (cal_factory)); |
---|
149 | cal_factory = NULL; |
---|
150 | return FALSE; |
---|
151 | } |
---|
152 | |
---|
153 | gtk_signal_connect (GTK_OBJECT (cal_factory), |
---|
154 | "last_calendar_gone", |
---|
155 | GTK_SIGNAL_FUNC (last_calendar_gone_cb), |
---|
156 | NULL); |
---|
157 | |
---|
158 | return TRUE; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | static gboolean |
---|
164 | setup_config (int argc, char **argv) |
---|
165 | { |
---|
166 | BonoboGenericFactory *factory; |
---|
167 | char *oafiid = "OAFIID:Bonobo_Moniker_wombat_Factory"; |
---|
168 | |
---|
169 | factory = bonobo_generic_factory_new_multi (oafiid, |
---|
170 | wombat_moniker_factory, |
---|
171 | NULL); |
---|
172 | |
---|
173 | // bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (factory)); |
---|
174 | |
---|
175 | |
---|
176 | return TRUE; |
---|
177 | } |
---|
178 | |
---|
179 | static gboolean |
---|
180 | setup_private (int argc, char **argv) |
---|
181 | { |
---|
182 | BonoboGenericFactory *factory; |
---|
183 | char *oafiid = "OAFIID:Bonobo_Moniker_wombat_private_Factory"; |
---|
184 | |
---|
185 | factory = bonobo_generic_factory_new_multi (oafiid, |
---|
186 | wombat_private_moniker_factory, |
---|
187 | NULL); |
---|
188 | |
---|
189 | // bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (factory)); |
---|
190 | |
---|
191 | |
---|
192 | return TRUE; |
---|
193 | } |
---|
194 | |
---|
195 | static void |
---|
196 | setup_vfs (int argc, char **argv) |
---|
197 | { |
---|
198 | if (!gnome_vfs_init ()) { |
---|
199 | g_message (_("setup_vfs(): could not initialize GNOME-VFS")); |
---|
200 | exit (EXIT_FAILURE); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | static void |
---|
207 | init_corba (int *argc, char **argv) |
---|
208 | { |
---|
209 | if (gnome_init_with_popt_table ("wombat", VERSION, |
---|
210 | *argc, argv, oaf_popt_options, 0, NULL) != 0) { |
---|
211 | g_message (_("init_corba(): could not initialize GNOME")); |
---|
212 | exit (EXIT_FAILURE); |
---|
213 | } |
---|
214 | |
---|
215 | oaf_init (*argc, argv); |
---|
216 | } |
---|
217 | |
---|
218 | static void |
---|
219 | init_bonobo (int *argc, char **argv) |
---|
220 | { |
---|
221 | init_corba (argc, argv); |
---|
222 | |
---|
223 | if (!bonobo_init (CORBA_OBJECT_NIL, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL)) { |
---|
224 | g_message (_("init_bonobo(): could not initialize Bonobo")); |
---|
225 | exit (EXIT_FAILURE); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | #ifdef DEBUG_BACKENDS |
---|
230 | static void |
---|
231 | dump_backends (int signal) |
---|
232 | { |
---|
233 | pas_book_factory_dump_active_backends (pas_book_factory); |
---|
234 | cal_factory_dump_active_backends (cal_factory); |
---|
235 | } |
---|
236 | #endif |
---|
237 | |
---|
238 | int |
---|
239 | main (int argc, char **argv) |
---|
240 | { |
---|
241 | gboolean did_pas=FALSE, did_pcs=FALSE, did_config=FALSE, did_private=FALSE; |
---|
242 | |
---|
243 | bindtextdomain (PACKAGE, EVOLUTION_LOCALEDIR); |
---|
244 | textdomain (PACKAGE); |
---|
245 | |
---|
246 | g_message ("Starting wombat"); |
---|
247 | |
---|
248 | #ifdef DEBUG_BACKENDS |
---|
249 | signal (SIGUSR2, dump_backends); |
---|
250 | #endif |
---|
251 | |
---|
252 | init_bonobo (&argc, argv); |
---|
253 | setup_vfs (argc, argv); |
---|
254 | |
---|
255 | /*g_log_set_always_fatal (G_LOG_LEVEL_ERROR | |
---|
256 | G_LOG_LEVEL_CRITICAL | |
---|
257 | G_LOG_LEVEL_WARNING);*/ |
---|
258 | |
---|
259 | if (!( (did_pas = setup_pas (argc, argv)) |
---|
260 | && (did_pcs = setup_pcs (argc, argv)) |
---|
261 | && (did_private = setup_private (argc, argv)) |
---|
262 | /* WARNING: Do not change the order here. `setup_config()' must |
---|
263 | come last, to work around an OAF race condition. */ |
---|
264 | && (did_config = setup_config (argc, argv)))) { |
---|
265 | |
---|
266 | const gchar *failed = NULL; |
---|
267 | |
---|
268 | if (!did_pas) |
---|
269 | failed = "PAS"; |
---|
270 | else if (!did_pcs) |
---|
271 | failed = "PCS"; |
---|
272 | else if (!did_config) |
---|
273 | failed = "Config"; |
---|
274 | else if (!did_private) |
---|
275 | failed = "Private Config"; |
---|
276 | |
---|
277 | g_message ("main(): could not initialize Wombat service \"%s\"; terminating", failed); |
---|
278 | |
---|
279 | if (pas_book_factory) { |
---|
280 | bonobo_object_unref (BONOBO_OBJECT (pas_book_factory)); |
---|
281 | pas_book_factory = NULL; |
---|
282 | } |
---|
283 | |
---|
284 | if (cal_factory) { |
---|
285 | bonobo_object_unref (BONOBO_OBJECT (cal_factory)); |
---|
286 | cal_factory = NULL; |
---|
287 | } |
---|
288 | |
---|
289 | exit (EXIT_FAILURE); |
---|
290 | } |
---|
291 | |
---|
292 | g_print ("Wombat up and running\n"); |
---|
293 | |
---|
294 | bonobo_main (); |
---|
295 | |
---|
296 | bonobo_object_unref (BONOBO_OBJECT (cal_factory)); |
---|
297 | cal_factory = NULL; |
---|
298 | |
---|
299 | bonobo_object_unref (BONOBO_OBJECT (pas_book_factory)); |
---|
300 | pas_book_factory = NULL; |
---|
301 | |
---|
302 | gnome_vfs_shutdown (); |
---|
303 | |
---|
304 | return 0; |
---|
305 | } |
---|