File: mg-data-handler.c

package info (click to toggle)
mergeant 0.52-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 12,872 kB
  • ctags: 6,584
  • sloc: ansic: 63,372; xml: 23,218; sh: 10,895; makefile: 612; sql: 237
file content (453 lines) | stat: -rw-r--r-- 13,243 bytes parent folder | download | duplicates (2)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* mg-data-handler.c
 *
 * Copyright (C) 2003 Vivien Malerba
 *
 * 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
 */

#include "mg-data-handler.h"
#include "handlers/mg-data-cell-renderer-textual.h"
#include "marshal.h"


/* signals */
enum
{
	DUMMY,
	LAST_SIGNAL
};

static gint mg_data_handler_signals[LAST_SIGNAL] = { 0 };
static void mg_data_handler_iface_init (gpointer g_class);

guint
mg_data_handler_get_type (void)
{
	static GType type = 0;

	if (!type) {
		static const GTypeInfo info = {
			sizeof (MgDataHandlerIface),
			(GBaseInitFunc) mg_data_handler_iface_init,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) NULL,
			NULL,
			NULL,
			0,
			0,
			(GInstanceInitFunc) NULL
		};
		
		type = g_type_register_static (G_TYPE_INTERFACE, "MgDataHandler", &info, 0);
	}
	return type;
}


static void
mg_data_handler_iface_init (gpointer g_class)
{
	static gboolean initialized = FALSE;

	if (! initialized) {
		initialized = TRUE;
	}
}

/**
 * mg_data_handler_get_entry_from_value
 * @dh: an object which implements the #MgDataHandler interface
 * @value: the original value to display or %NULL
 * @type: the requested data type (if @value is not %NULL or of type NULL, then this parameter is ignored)
 *
 * Create a new MgDataEntry widget to edit the given value. If the value is NULL or of
 * type GDA_VALUE_TYPE_NULL, then the type argument is used and determines the real requested
 * type (it is otherwise ignored).
 *
 * Also, if the value is NULL or of type GDA_VALUE_TYPE_NULL, then the initial edited value in the
 * widget will be the sane initial value provided by the MgDataHandler object.
 *
 * Returns: the new widget
 */
MgDataEntry *
mg_data_handler_get_entry_from_value (MgDataHandler *dh, const GdaValue *value, GdaValueType type)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);

	if (!value || gda_value_is_null (value))
		g_return_val_if_fail (mg_data_handler_accepts_gda_type (MG_DATA_HANDLER (dh), type), NULL);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_entry_from_value)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_entry_from_value) (dh, value, type);
	
	return NULL;
}

/**
 * mg_data_handler_get_cell_renderer
 * @dh: an object which implements the #MgDataHandler interface
 * @type: the requested data type
 *
 * Creates a new #GtkCellRenderer object to handle @type of data.
 *
 * Returns: the new cell renderer
 */
GtkCellRenderer *
mg_data_handler_get_cell_renderer (MgDataHandler *dh, GdaValueType type)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_cell_renderer)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_cell_renderer) (dh, type);
	else 
		return mg_data_cell_renderer_textual_new (dh, type);
}

/**
 * mg_data_handler_get_sql_from_value
 * @dh: an object which implements the #MgDataHandler interface
 * @value: the value to be converted to a string
 *
 * Creates a new string which is an SQL representation of the given value. If the value is NULL or
 * is of type GDA_VALUE_TYPE_NULL, the returned string is NULL.
 *
 * Returns: the new string.
 */
gchar *
mg_data_handler_get_sql_from_value (MgDataHandler *dh, const GdaValue *value)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);
	
	if (! value || gda_value_is_null (value))
		return NULL;

	/* Calling the real function with value != NULL and not of type GDA_VALUE_TYPE_NULL */
	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_sql_from_value)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_sql_from_value) (dh, value);
	
	return NULL;
}

/**
 * mg_data_handler_get_str_from_value
 * @dh: an object which implements the #MgDataHandler interface
 * @value: the value to be converted to a string
 *
 * Creates a new string which is a "user friendly" representation of the given value (usually
 * it will be in the users's locale, specially for the dates). If the value is 
 * NULL or is of type GDA_VALUE_TYPE_NULL, the returned string is a copy of "" (empty string).
 *
 * Returns: the new string.
 */
gchar *
mg_data_handler_get_str_from_value (MgDataHandler *dh, const GdaValue *value)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);

	if (! value || gda_value_is_null (value))
		return g_strdup ("");

	/* Calling the real function with value != NULL and not of type GDA_VALUE_TYPE_NULL */
	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_str_from_value)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_str_from_value) (dh, value);
	
	return NULL;
}

/**
 * mg_data_handler_get_value_from_sql
 * @dh: an object which implements the #MgDataHandler interface
 * @sql:
 * @type: 
 *
 * Creates a new GdaValue which represents the SQL value given as argument. This is
 * the opposite of the function mg_data_handler_get_sql_from_value(). The type argument
 * is used to determine the real data type requested for the returned value.
 *
 * If the sql string is NULL, then the returned GdaValue is of type GDA_VALUE_TYPE_NULL;
 * if the sql string does not correspond to a valid SQL string for the requested type, then
 * NULL is returned.
 *
 * Returns: the new GdaValue or NULL on error
 */
GdaValue *
mg_data_handler_get_value_from_sql (MgDataHandler *dh, const gchar *sql, GdaValueType type)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);
	g_return_val_if_fail (mg_data_handler_accepts_gda_type (MG_DATA_HANDLER (dh), type), NULL);

	if (!sql)
		return gda_value_new_null ();

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_value_from_sql)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_value_from_sql) (dh, sql, type);
	
	return NULL;
}


/**
 * mg_data_handler_get_value_from_str
 * @dh: an object which implements the #MgDataHandler interface
 * @str:
 * @type: 
 *
 * Creates a new GdaValue which represents the STR value given as argument. This is
 * the opposite of the function mg_data_handler_get_str_from_value(). The type argument
 * is used to determine the real data type requested for the returned value.
 *
 * If the str string is NULL, then the returned GdaValue is of type GDA_VALUE_TYPE_NULL;
 * if the str string does not correspond to a valid STR string for the requested type, then
 * NULL is returned.
 *
 * Returns: the new GdaValue or NULL on error
 */
GdaValue *
mg_data_handler_get_value_from_str (MgDataHandler *dh, const gchar *str, GdaValueType type)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);
	g_return_val_if_fail (mg_data_handler_accepts_gda_type (MG_DATA_HANDLER (dh), type), NULL);

	if (!str)
		return gda_value_new_null ();

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_value_from_str)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_value_from_str) (dh, str, type);
	else {
		/* if the get_value_from_str() method is not implemented, then we try the
		   get_value_from_sql() method */
		if (MG_DATA_HANDLER_GET_IFACE (dh)->get_value_from_sql)
			return (MG_DATA_HANDLER_GET_IFACE (dh)->get_value_from_sql) (dh, str, type);
	}
	
	return NULL;
}


/**
 * mg_data_handler_get_sane_init_value
 * @dh: an object which implements the #MgDataHandler interface
 * @type: 
 *
 * Creates a new GdaValue which holds a sane initial value to be used if no value is specifically
 * provided. For example for a simple string, this would return gda_value_new_string ("").
 *
 * Returns: the new GdaValue.
 */
GdaValue *
mg_data_handler_get_sane_init_value (MgDataHandler *dh, GdaValueType type)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);
	g_return_val_if_fail (mg_data_handler_accepts_gda_type (MG_DATA_HANDLER (dh), type), NULL);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_sane_init_value)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_sane_init_value) (dh, type);
	
	return NULL;
}

/**
 * mg_data_handler_get_nb_gda_types
 * @dh: an object which implements the #MgDataHandler interface
 *
 * Get the number of GdaValueType types the MgDataHandler can handle correctly
 *
 * Returns: the number.
 */
guint
mg_data_handler_get_nb_gda_types (MgDataHandler *dh)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), 0);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_nb_gda_types)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_nb_gda_types) (dh);
	
	return 0;
}

/**
 * mg_data_handler_accepts_gda_type
 * @dh: an object which implements the #MgDataHandler interface
 * @type:
 *
 * Checks wether the MgDataHandler is able to handle the gda type given as argument.
 *
 * Returns: TRUE if the gda type can be handled
 */
gboolean
mg_data_handler_accepts_gda_type (MgDataHandler *dh, GdaValueType type)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), FALSE);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->accepts_gda_type)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->accepts_gda_type) (dh, type);
	
	return FALSE;
}

/**
 * mg_data_handler_get_gda_type_index
 * @dh: an object which implements the #MgDataHandler interface
 * @index: 
 *
 * Get the GdaValueType handled by the MgDataHandler, at the given position (starting at zero).
 *
 * Returns: the GdaValueType
 */
GdaValueType
mg_data_handler_get_gda_type_index (MgDataHandler *dh, guint index)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), GDA_VALUE_TYPE_UNKNOWN);
	g_return_val_if_fail (index < mg_data_handler_get_nb_gda_types (dh),
			      GDA_VALUE_TYPE_UNKNOWN);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_gda_type_index)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_gda_type_index) (dh, index);
	
	return GDA_VALUE_TYPE_UNKNOWN;
}

/**
 * mg_data_handler_get_descr
 * @dh: an object which implements the #MgDataHandler interface
 *
 * Get a short description of the MgDataHandler
 *
 * Returns: the description
 */
const gchar *
mg_data_handler_get_descr (MgDataHandler *dh)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_descr)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_descr) (dh);
	
	return NULL;
}

/**
 * mg_data_handler_get_descr_detail
 * @dh: an object which implements the #MgDataHandler interface
 *
 * Get a detailled description of the MgDataHandler
 *
 * Returns: the description
 */
const gchar *
mg_data_handler_get_descr_detail (MgDataHandler *dh)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_descr_detail)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_descr_detail) (dh);
	
	return NULL;
}

/**
 * mg_data_handler_get_version
 * @dh: an object which implements the #MgDataHandler interface
 *
 * Get the MgDataHandler's version
 *
 * Returns: the version
 */
const gchar *
mg_data_handler_get_version (MgDataHandler *dh)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_version)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_version) (dh);
	
	return NULL;
}

/**
 * mg_data_handler_is_plugin
 * @dh: an object which implements the #MgDataHandler interface
 *
 * Get wether the MgDataHandler is a plugin or not
 *
 * Returns: TRUE if the MgDataHandler is a plugin
 */
gboolean
mg_data_handler_is_plugin (MgDataHandler *dh)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), FALSE);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->is_plugin)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->is_plugin) (dh);
	
	return FALSE;
}

/**
 * mg_data_handler_get_plugin_name
 * @dh: an object which implements the #MgDataHandler interface
 *
 * Get the name of the MgDataHandler if it is a plugin
 *
 * Returns: the name of the MgDataHandler if it is a plugin or NULL
 */
const gchar *mg_data_handler_get_plugin_name (MgDataHandler *dh)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_plugin_name)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_plugin_name) (dh);
	
	return NULL;
}

/**
 * mg_data_handler_get_plugin_file
 * @dh: an object which implements the #MgDataHandler interface
 *
 * Get the file name (.so on Unix) of the MgDataHandler if it is a plugin
 *
 * Returns: the file name of the MgDataHandler if it is a plugin or NULL
 */
const gchar *
mg_data_handler_get_plugin_file (MgDataHandler *dh)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_plugin_file)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_plugin_file) (dh);
	
	return NULL;
}

/**
 * mg_data_handler_get_key
 * @dh: an object which implements the #MgDataHandler interface
 *
 * Get a unique identifier for the MgDataHandler
 *
 * Returns: the identifier
 */
gchar *
mg_data_handler_get_key (MgDataHandler *dh)
{
	g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);

	if (MG_DATA_HANDLER_GET_IFACE (dh)->get_key)
		return (MG_DATA_HANDLER_GET_IFACE (dh)->get_key) (dh);
	
	return NULL;
}