summaryrefslogtreecommitdiff
path: root/components/services/trilobite/libtrilobite/trilobite-root-client.c
blob: 361d0155cd047450b8df36f089ee92a0706a093e (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
330
331
332
333
334
335
336
337
338
339
340
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* 
 * TrilobiteRootClient is a GtkObject wrapper for the corba object
 * PasswordQueryClient.
 *
 * Copyright (C) 2000 Eazel, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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 Library 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.
 *
 * Authors: Robey Pointer <robey@eazel.com>
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <liboaf/liboaf.h>
#include <bonobo.h>
#include "libtrilobite.h"
#include "trilobite-service.h"		/* autogenerated */
#include "trilobite-root-client-public.h"
#include "trilobite-root-client-private.h"


static GtkObject *parent_class;

/* signals that a TrilobiteRootClient can emit */
/* LAST_SIGNAL is just a sentinel marking the end of the list */
enum {
	NEED_PASSWORD,
	TRY_AGAIN,
	LAST_SIGNAL
};
static guint root_client_signals[LAST_SIGNAL] = { 0 };


/**********   corba stuff   **********/

static PortableServer_ServantBase__epv base_epv = { NULL, NULL, NULL };

typedef struct {
	POA_Trilobite_PasswordQueryClient servant;
	BonoboObject *bonobo_object;
} impl_POA_Trilobite_PasswordQueryClient;

/* emit the signal to get the password */
static CORBA_char *
impl_Trilobite_PasswordQueryClient_get_password (impl_POA_Trilobite_PasswordQueryClient *trilobite,
						 CORBA_char *prompt,
						 CORBA_Environment *ev)
{
	char *password;
	char *result;

	/* now emit a signal and get the password */
	password = NULL;
	gtk_signal_emit (GTK_OBJECT (trilobite->bonobo_object), root_client_signals[NEED_PASSWORD], prompt, &password);
	if (password == NULL) {
		/* bummer.  nobody caught the signal. */
		password = g_strdup ("");
	}

	/* make it be owned by corba */
	result = CORBA_string_dup (password);
	g_free (password);

	return result;
};

static CORBA_boolean
impl_Trilobite_PasswordQueryClient_try_again (impl_POA_Trilobite_PasswordQueryClient *trilobite,
					      CORBA_Environment *ev)
{
	gboolean result;

	result = FALSE;
	gtk_signal_emit (GTK_OBJECT (trilobite->bonobo_object), root_client_signals[TRY_AGAIN], &result);
	return (CORBA_boolean)result;
}

POA_Trilobite_PasswordQueryClient__epv *
trilobite_root_client_get_epv(void) 
{
	POA_Trilobite_PasswordQueryClient__epv *epv;

	epv = g_new0 (POA_Trilobite_PasswordQueryClient__epv, 1);
	epv->get_password = (gpointer)&impl_Trilobite_PasswordQueryClient_get_password;
	epv->try_again = (gpointer)&impl_Trilobite_PasswordQueryClient_try_again;

	return epv;
};

static Trilobite_PasswordQueryClient
trilobite_root_client_create_corba_object (BonoboObject *trilobite)
{
	impl_POA_Trilobite_PasswordQueryClient *servant;
	CORBA_Environment ev;

	g_assert (trilobite != NULL);

	CORBA_exception_init (&ev);
	
	servant = g_new0 (impl_POA_Trilobite_PasswordQueryClient, 1);
	((POA_Trilobite_PasswordQueryClient *)servant)->vepv =
		TRILOBITE_ROOT_CLIENT_CLASS (GTK_OBJECT (trilobite)->klass)->servant_vepv;
	servant->bonobo_object = trilobite;

	POA_Trilobite_PasswordQueryClient__init (servant, &ev);
	ORBIT_OBJECT_KEY (((POA_Trilobite_PasswordQueryClient *)servant)->_private)->object = NULL;	

	if (ev._major != CORBA_NO_EXCEPTION) {
		g_warning ("Cannot instantiate Trilobite_PasswordQueryClient corba object"); 
		g_free (servant);
		CORBA_exception_free (&ev);		
		return CORBA_OBJECT_NIL;
	}

	CORBA_exception_free (&ev);

	return (Trilobite_PasswordQueryClient) bonobo_object_activate_servant (trilobite, servant);
}


/**********   gtk object stuff   **********/

/* have to make my own signal marshaller for STRING__STRING (grumble) */
typedef gchar *(*GtkSignal_STRING__STRING) (GtkObject *object, gchar *arg1, gpointer user_data);
static void
gtk_marshal_STRING__STRING (GtkObject *object, GtkSignalFunc func, gpointer func_data, GtkArg *args)
{
	GtkSignal_STRING__STRING rfunc;
	gchar **return_value;

	return_value = GTK_RETLOC_STRING (args[1]);
	rfunc = (GtkSignal_STRING__STRING)func;
	*return_value = (*rfunc) (object, GTK_VALUE_STRING (args[0]), func_data);
}


/* destroy callback, and accessible from the outside world */
void
trilobite_root_client_unref (GtkObject *object)
{
	TrilobiteRootClient *root_client;

	g_return_if_fail (object != NULL);
	g_return_if_fail (TRILOBITE_IS_ROOT_CLIENT (object));

	root_client = TRILOBITE_ROOT_CLIENT (object);

	bonobo_object_unref (BONOBO_OBJECT (object));
}

static void
trilobite_root_client_finalize (GtkObject *object)
{
	TrilobiteRootClient *root_client;

	g_return_if_fail (object != NULL);
	g_return_if_fail (TRILOBITE_IS_ROOT_CLIENT (object));

	root_client = TRILOBITE_ROOT_CLIENT (object);

	g_free (root_client->private);
	root_client->private = NULL;

	/* call parent destructor */
	if (GTK_OBJECT_CLASS (parent_class)->finalize) {
		GTK_OBJECT_CLASS (parent_class)->finalize (object);
	}
}

/* class initializer */
static void
trilobite_root_client_class_initialize (TrilobiteRootClientClass *klass)
{
	GtkObjectClass *object_class;

	parent_class = gtk_type_class (GTK_TYPE_OBJECT);

	object_class = (GtkObjectClass *)klass;
	object_class->finalize = trilobite_root_client_finalize;

	klass->servant_init = POA_Trilobite_PasswordQueryClient__init;
	klass->servant_fini = POA_Trilobite_PasswordQueryClient__fini;
	klass->servant_vepv = g_new0 (POA_Trilobite_PasswordQueryClient__vepv, 1);

	((POA_Trilobite_PasswordQueryClient__vepv*)klass->servant_vepv)->_base_epv = &base_epv; 
	((POA_Trilobite_PasswordQueryClient__vepv*)klass->servant_vepv)->Bonobo_Unknown_epv = bonobo_object_get_epv ();
	((POA_Trilobite_PasswordQueryClient__vepv*)klass->servant_vepv)->Trilobite_PasswordQueryClient_epv = 
		trilobite_root_client_get_epv ();

	root_client_signals[NEED_PASSWORD] =
		gtk_signal_new ("need_password", 0, object_class->type, 0,
				gtk_marshal_STRING__STRING, GTK_TYPE_STRING, 1, GTK_TYPE_STRING);
	root_client_signals[TRY_AGAIN] =
		gtk_signal_new ("try_again", 0, object_class->type, 0,
				gtk_marshal_BOOL__NONE, GTK_TYPE_BOOL, 0);
	gtk_object_class_add_signals (object_class, root_client_signals, LAST_SIGNAL);
}

gboolean
trilobite_root_client_construct (TrilobiteRootClient *root_client,
				 Trilobite_PasswordQueryClient corba_trilobite)
{
	g_assert (root_client != NULL);
	g_assert (TRILOBITE_IS_ROOT_CLIENT (root_client));
	g_return_val_if_fail (corba_trilobite != CORBA_OBJECT_NIL, FALSE);

	if (!bonobo_object_construct (BONOBO_OBJECT (root_client), (CORBA_Object) corba_trilobite)) {
		return FALSE;
	}

	return TRUE;
}

/* object initializer */
static void
trilobite_root_client_initialize (TrilobiteRootClient *object)
{
	TrilobiteRootClient *root_client;
	Trilobite_PasswordQueryClient corba_trilobite;

	g_assert (object != NULL);
	g_assert (TRILOBITE_IS_ROOT_CLIENT (object));

	root_client = TRILOBITE_ROOT_CLIENT (object);

	corba_trilobite = trilobite_root_client_create_corba_object (BONOBO_OBJECT (object));
	if (trilobite_root_client_construct (object, corba_trilobite) == FALSE) {
		/* no good way to bail out now :( */
		corba_trilobite = CORBA_OBJECT_NIL;
	}

	object->private = g_new0 (TrilobiteRootClientPrivate, 1);
	object->private->pq_client = corba_trilobite;
}

/* generate the GtkType for TrilobiteRootClient */
GtkType
trilobite_root_client_get_type (void)
{
	static GtkType trilobite_root_client_type = 0;

	/* First time it's called ? */
	if (! trilobite_root_client_type) {
		static const GtkTypeInfo root_client_info = {
			"TrilobiteRootClient",
			sizeof (TrilobiteRootClient),
			sizeof (TrilobiteRootClientClass),
			(GtkClassInitFunc) trilobite_root_client_class_initialize,
			(GtkObjectInitFunc) trilobite_root_client_initialize,
			/* reserved_1 */ NULL,
			/* reserved_2 */ NULL,
			(GtkClassInitFunc) NULL,
		};

		/* Get a unique GtkType */
		trilobite_root_client_type = gtk_type_unique (bonobo_object_get_type(), &root_client_info);
	}

	return trilobite_root_client_type;
}

TrilobiteRootClient *
trilobite_root_client_new (void)
{
	return TRILOBITE_ROOT_CLIENT (gtk_object_new (TRILOBITE_TYPE_ROOT_CLIENT, NULL));
}


/**********   functions that actually implement the root client   **********/

/* returns the actual corba object for the PasswordQueryClient.
 * only needed if you feel like writing your own _attach function instead of using the one below.
 */
Trilobite_PasswordQueryClient
trilobite_root_client_get_passwordqueryclient (TrilobiteRootClient *root_client)
{
	g_return_val_if_fail (root_client != NULL, CORBA_OBJECT_NIL);
	g_return_val_if_fail (TRILOBITE_IS_ROOT_CLIENT (root_client), CORBA_OBJECT_NIL);

	return root_client->private->pq_client;
}

/* if 'service' implements PasswordQuery, this function attaches the TrilobiteRootClient to that
 * PasswordQuery as the callback mechanism.
 * --- this is a necessary step, if you want a working TrilobiteRootClient.
 * returns TRUE on success
 */
gboolean
trilobite_root_client_attach (TrilobiteRootClient *root_client, BonoboObjectClient *service)
{
	Trilobite_PasswordQuery trilobite_password;
	CORBA_Environment ev;

	g_return_val_if_fail (root_client != NULL, FALSE);
	g_return_val_if_fail (TRILOBITE_IS_ROOT_CLIENT (root_client), FALSE);
	g_return_val_if_fail (service != NULL, FALSE);
	g_return_val_if_fail (BONOBO_IS_OBJECT_CLIENT (service), FALSE);
	g_assert (root_client->private->pq_client != CORBA_OBJECT_NIL);

	CORBA_exception_init (&ev);
	if (! bonobo_object_client_has_interface (service, "IDL:Trilobite/PasswordQuery:1.0", &ev)) {
		goto fail;
	}

	trilobite_password = bonobo_object_query_interface (BONOBO_OBJECT (service),
							    "IDL:Trilobite/PasswordQuery:1.0");
	if (trilobite_password == CORBA_OBJECT_NIL) {
		g_warning ("somehow query-interface(PasswordQuery) returned nil");
		goto fail;
	}

	Trilobite_PasswordQuery_set_query_client (trilobite_password, root_client->private->pq_client, &ev);
	if (ev._major != CORBA_NO_EXCEPTION) {
		g_warning ("set-query-client got exception :(");
	}
	Trilobite_PasswordQuery_unref (trilobite_password, &ev);
	CORBA_Object_release (trilobite_password, &ev);

	CORBA_exception_free (&ev);
	return TRUE;

fail:
	CORBA_exception_free (&ev);
	return FALSE;
}