1 /*
2  * Copyright (C) 2009 - 2012 Vivien Malerba <malerba@gnome-db.org>
3  * Copyright (C) 2010 David King <davidk@openismus.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301, USA.
19  */
20 
21 #include <libgda/libgda.h>
22 #include <glib/gi18n-lib.h>
23 #include <sql-parser/gda-sql-parser.h>
24 #include <libgda-ui/libgda-ui.h>
25 #include <libgda-ui/gdaui-plugin.h>
26 #include <libgda/binreloc/gda-binreloc.h>
27 
28 #include "gdaui-entry-filesel.h"
29 #include "gdaui-entry-cidr.h"
30 #include "gdaui-entry-text.h"
31 #include "gdaui-entry-pict.h"
32 #include "gdaui-entry-rt.h"
33 #include "gdaui-data-cell-renderer-pict.h"
34 #include "gdaui-entry-format.h"
35 
36 #ifdef HAVE_LIBGCRYPT
37 #include "gdaui-entry-password.h"
38 #include "gdaui-data-cell-renderer-password.h"
39 #endif
40 
41 #ifdef HAVE_GTKSOURCEVIEW
42   #ifdef GTK_DISABLE_SINGLE_INCLUDES
43     #undef GTK_DISABLE_SINGLE_INCLUDES
44   #endif
45 
46   #include <gtksourceview/gtksourceview.h>
47   #include <gtksourceview/gtksourcelanguagemanager.h>
48   #include <gtksourceview/gtksourcebuffer.h>
49   #include <gtksourceview/gtksourcestyleschememanager.h>
50   #include <gtksourceview/gtksourcestylescheme.h>
51 #endif
52 
53 static GdauiDataEntry *plugin_entry_filesel_create_func (GdaDataHandler *handler, GType type, const gchar *options);
54 static GdauiDataEntry *plugin_entry_cidr_create_func (GdaDataHandler *handler, GType type, const gchar *options);
55 static GdauiDataEntry *plugin_entry_format_create_func (GdaDataHandler *handler, GType type, const gchar *options);
56 static GdauiDataEntry *plugin_entry_text_create_func (GdaDataHandler *handler, GType type, const gchar *options);
57 static GdauiDataEntry *plugin_entry_rt_create_func (GdaDataHandler *handler, GType type, const gchar *options);
58 static GdauiDataEntry *plugin_entry_pict_create_func (GdaDataHandler *handler, GType type, const gchar *options);
59 static GtkCellRenderer  *plugin_cell_renderer_pict_create_func (GdaDataHandler *handler, GType type, const gchar *options);
60 
61 #ifdef HAVE_LIBGCRYPT
62 static GdauiDataEntry *plugin_entry_password_create_func (GdaDataHandler *handler, GType type, const gchar *options);
63 static GtkCellRenderer  *plugin_cell_renderer_password_create_func (GdaDataHandler *handler, GType type, const gchar *options);
64 #endif
65 
66 GSList *
plugin_init(GError ** error)67 plugin_init (GError **error)
68 {
69 	GdauiPlugin *plugin;
70 	GSList *retlist = NULL;
71 	gchar *file;
72 
73 	/* file selector */
74 	plugin = g_new0 (GdauiPlugin, 1);
75 	plugin->plugin_name = "filesel";
76 	plugin->plugin_descr = "File selection entry";
77 	plugin->plugin_file = NULL; /* always leave NULL */
78 	plugin->nb_g_types = 1;
79 	plugin->valid_g_types = g_new (GType, plugin->nb_g_types);
80 	plugin->valid_g_types [0] = G_TYPE_STRING;
81 	plugin->options_xml_spec = NULL;
82 	plugin->entry_create_func = plugin_entry_filesel_create_func;
83 	plugin->cell_create_func = NULL;
84 	retlist = g_slist_append (retlist, plugin);
85 	file = gda_gbr_get_file_path (GDA_LIB_DIR, LIBGDA_ABI_NAME, "plugins", "gdaui-entry-filesel-spec.xml", NULL);
86 	if (! g_file_test (file, G_FILE_TEST_EXISTS)) {
87 		if (error && !*error)
88 			g_set_error (error, GDAUI_DATA_ENTRY_ERROR, GDAUI_DATA_ENTRY_FILE_NOT_FOUND_ERROR,
89 				     _("Missing spec. file '%s'"), file);
90         }
91 	else {
92 		gsize len;
93 		g_file_get_contents (file, &(plugin->options_xml_spec), &len, error);
94 	}
95 	g_free (file);
96 
97 	/* CIDR */
98 	plugin = g_new0 (GdauiPlugin, 1);
99 	plugin->plugin_name = "cird";
100 	plugin->plugin_descr = "Entry to hold an IPv4 network specification";
101 	plugin->plugin_file = NULL; /* always leave NULL */
102 	plugin->nb_g_types = 1;
103 	plugin->valid_g_types = g_new (GType, plugin->nb_g_types);
104 	plugin->valid_g_types [0] = G_TYPE_STRING;
105 	plugin->options_xml_spec = NULL;
106 	plugin->entry_create_func = plugin_entry_cidr_create_func;
107 	plugin->cell_create_func = NULL;
108 	retlist = g_slist_append (retlist, plugin);
109 
110 	/* FORMAT */
111 	plugin = g_new0 (GdauiPlugin, 1);
112 	plugin->plugin_name = "format";
113 	plugin->plugin_descr = "Text entry with specific format";
114 	plugin->plugin_file = NULL; /* always leave NULL */
115 	plugin->nb_g_types = 1;
116 	plugin->valid_g_types = g_new (GType, plugin->nb_g_types);
117 	plugin->valid_g_types [0] = G_TYPE_STRING;
118 	plugin->options_xml_spec = NULL;
119 	plugin->entry_create_func = plugin_entry_format_create_func;
120 	plugin->cell_create_func = NULL;
121 	retlist = g_slist_append (retlist, plugin);
122 	file = gda_gbr_get_file_path (GDA_LIB_DIR, LIBGDA_ABI_NAME, "plugins", "gdaui-entry-format-spec.xml", NULL);
123 	if (! g_file_test (file, G_FILE_TEST_EXISTS)) {
124 		if (error && !*error)
125 			g_set_error (error, GDAUI_DATA_ENTRY_ERROR, GDAUI_DATA_ENTRY_FILE_NOT_FOUND_ERROR,
126 				     _("Missing spec. file '%s'"), file);
127         }
128 	else {
129 		gsize len;
130 		g_file_get_contents (file, &(plugin->options_xml_spec), &len, error);
131 	}
132 	g_free (file);
133 
134 #ifdef HAVE_LIBGCRYPT
135 	/* PASSWORD */
136 	plugin = g_new0 (GdauiPlugin, 1);
137 	plugin->plugin_name = "password";
138 	plugin->plugin_descr = "password entry";
139 	plugin->plugin_file = NULL; /* always leave NULL */
140 	plugin->nb_g_types = 1;
141 	plugin->valid_g_types = g_new (GType, plugin->nb_g_types);
142 	plugin->valid_g_types [0] = G_TYPE_STRING;
143 	plugin->options_xml_spec = NULL;
144 	plugin->entry_create_func = plugin_entry_password_create_func;
145 	plugin->cell_create_func = plugin_cell_renderer_password_create_func;
146 	retlist = g_slist_append (retlist, plugin);
147 
148 	file = gda_gbr_get_file_path (GDA_LIB_DIR, LIBGDA_ABI_NAME, "plugins", "gdaui-entry-password.xml", NULL);
149 	if (! g_file_test (file, G_FILE_TEST_EXISTS)) {
150 		if (error && !*error)
151 			g_set_error (error,  GDAUI_DATA_ENTRY_ERROR, GDAUI_DATA_ENTRY_FILE_NOT_FOUND_ERROR,
152 				     _("Missing spec. file '%s'"), file);
153         }
154 	else {
155 		gsize len;
156 		g_file_get_contents (file, &(plugin->options_xml_spec), &len, error);
157 	}
158 	g_free (file);
159 #endif
160 
161 	/* TEXT */
162 	plugin = g_new0 (GdauiPlugin, 1);
163 	plugin->plugin_name = "text";
164 	plugin->plugin_descr = "Multiline text entry";
165 	plugin->plugin_file = NULL; /* always leave NULL */
166 	plugin->nb_g_types = 3;
167 	plugin->valid_g_types = g_new (GType, plugin->nb_g_types);
168 	plugin->valid_g_types [0] = G_TYPE_STRING;
169 	plugin->valid_g_types [1] = GDA_TYPE_BLOB;
170 	plugin->valid_g_types [2] = GDA_TYPE_BINARY;
171 	plugin->options_xml_spec = NULL;
172 	plugin->entry_create_func = plugin_entry_text_create_func;
173 	plugin->cell_create_func = NULL;
174 	retlist = g_slist_append (retlist, plugin);
175 
176 #ifdef HAVE_GTKSOURCEVIEW
177 	file = gda_gbr_get_file_path (GDA_LIB_DIR, LIBGDA_ABI_NAME, "plugins", "gdaui-entry-text-spec.xml", NULL);
178 	if (! g_file_test (file, G_FILE_TEST_EXISTS)) {
179 		if (error && !*error)
180 			g_set_error (error,  GDAUI_DATA_ENTRY_ERROR, GDAUI_DATA_ENTRY_FILE_NOT_FOUND_ERROR,
181 				     _("Missing spec. file '%s'"), file);
182         }
183 	else {
184 		xmlDocPtr doc;
185 		doc = xmlParseFile (file);
186 		if (!doc) {
187 			if (error && !*error)
188 				g_set_error (error,  GDAUI_DATA_ENTRY_ERROR, GDAUI_DATA_ENTRY_FILE_NOT_FOUND_ERROR,
189 					     _("Missing spec. file '%s'"), file);
190 		}
191 		else {
192 			xmlNodePtr node;
193 			node = xmlDocGetRootElement (doc);
194 			for (node = node->children; node; node = node->next) {
195 				if (!strcmp ((gchar*) node->name, "sources")) {
196 					for (node = node->children; node; node = node->next) {
197 						if (!strcmp ((gchar*) node->name, "gda_array")) {
198 							for (node = node->children; node; node = node->next) {
199 								if (!strcmp ((gchar*) node->name,
200 									     "gda_array_data")) {
201 									break;
202 								}
203 							}
204 
205 							break;
206 						}
207 					}
208 					break;
209 				}
210 			}
211 			GtkSourceLanguageManager *lm;
212 			const gchar * const * langs;
213 			lm = gtk_source_language_manager_get_default ();
214 			langs = gtk_source_language_manager_get_language_ids (lm);
215 			if (langs) {
216 				gint i;
217 				for (i = 0; ; i++) {
218 					const gchar *tmp;
219 					tmp = langs [i];
220 					if (!tmp)
221 						break;
222 					if (node) {
223 						xmlNodePtr row;
224 						row = xmlNewChild (node, NULL, BAD_CAST "gda_array_row", NULL);
225 						xmlNewChild (row, NULL, BAD_CAST "gda_value", BAD_CAST tmp);
226 
227 						GtkSourceLanguage *sl;
228 						sl = gtk_source_language_manager_get_language (lm, tmp);
229 						if (sl)
230 							xmlNewChild (row, NULL, BAD_CAST "gda_value",
231 								     BAD_CAST gtk_source_language_get_name (sl));
232 						else
233 							xmlNewChild (row, NULL, BAD_CAST "gda_value", BAD_CAST tmp);
234 					}
235 				}
236 			}
237 
238 			xmlChar *out;
239 			int size;
240 			xmlDocDumpFormatMemory (doc, &out, &size, 0);
241 			xmlFreeDoc (doc);
242 			plugin->options_xml_spec = g_strdup ((gchar*) out);
243 			xmlFree (out);
244 		}
245 	}
246 	g_free (file);
247 #endif
248 
249 	/* TEXT */
250 	plugin = g_new0 (GdauiPlugin, 1);
251 	plugin->plugin_name = "rtext";
252 	plugin->plugin_descr = "Rich text editor entry";
253 	plugin->plugin_file = NULL; /* always leave NULL */
254 	plugin->nb_g_types = 3;
255 	plugin->valid_g_types = g_new (GType, plugin->nb_g_types);
256 	plugin->valid_g_types [0] = G_TYPE_STRING;
257 	plugin->valid_g_types [1] = GDA_TYPE_BLOB;
258 	plugin->valid_g_types [2] = GDA_TYPE_BINARY;
259 	plugin->options_xml_spec = NULL;
260 	plugin->entry_create_func = plugin_entry_rt_create_func;
261 	plugin->cell_create_func = NULL;
262 	retlist = g_slist_append (retlist, plugin);
263 
264 	/* Picture - binary */
265 	plugin = g_new0 (GdauiPlugin, 1);
266 	plugin->plugin_name = "picture";
267 	plugin->plugin_descr = "Picture entry";
268 	plugin->plugin_file = NULL; /* always leave NULL */
269 	plugin->nb_g_types = 2;
270 	plugin->valid_g_types = g_new (GType, plugin->nb_g_types);
271 	plugin->valid_g_types [0] = GDA_TYPE_BINARY;
272 	plugin->valid_g_types [1] = GDA_TYPE_BLOB;
273 	plugin->options_xml_spec = NULL;
274 	plugin->entry_create_func = plugin_entry_pict_create_func;
275 	plugin->cell_create_func = plugin_cell_renderer_pict_create_func;
276 	retlist = g_slist_append (retlist, plugin);
277 
278 	file = gda_gbr_get_file_path (GDA_LIB_DIR, LIBGDA_ABI_NAME, "plugins", "gdaui-entry-pict-spec.xml", NULL);
279 	if (! g_file_test (file, G_FILE_TEST_EXISTS)) {
280 		if (error && !*error)
281 			g_set_error (error, GDAUI_DATA_ENTRY_ERROR, GDAUI_DATA_ENTRY_FILE_NOT_FOUND_ERROR,
282 				     _("Missing spec. file '%s'"), file);
283         }
284 	else {
285 		gsize len;
286 		g_file_get_contents (file, &(plugin->options_xml_spec), &len, error);
287 	}
288 	g_free (file);
289 
290 	/* picture - string encoded */
291 	plugin = g_new0 (GdauiPlugin, 1);
292 	plugin->plugin_name = "picture_as_string";
293 	plugin->plugin_descr = "Picture entry for data stored as a string";
294 	plugin->plugin_file = NULL; /* always leave NULL */
295 	plugin->nb_g_types = 1;
296 	plugin->valid_g_types = g_new (GType, plugin->nb_g_types);
297 	plugin->valid_g_types [0] = G_TYPE_STRING;
298 	plugin->options_xml_spec = NULL;
299 	plugin->entry_create_func = plugin_entry_pict_create_func;
300 	plugin->cell_create_func = plugin_cell_renderer_pict_create_func;
301 	retlist = g_slist_append (retlist, plugin);
302 
303 	file = gda_gbr_get_file_path (GDA_LIB_DIR, LIBGDA_ABI_NAME, "plugins", "gdaui-entry-pict-spec_string.xml", NULL);
304 	if (! g_file_test (file, G_FILE_TEST_EXISTS)) {
305 		if (error && !*error)
306 			g_set_error (error,  GDAUI_DATA_ENTRY_ERROR, GDAUI_DATA_ENTRY_FILE_NOT_FOUND_ERROR,
307 				     _("Missing spec. file '%s'"), file);
308         }
309 	else {
310 		gsize len;
311 		g_file_get_contents (file, &(plugin->options_xml_spec), &len, error);
312 	}
313 	g_free (file);
314 
315 	return retlist;
316 }
317 
318 static GdauiDataEntry *
plugin_entry_filesel_create_func(GdaDataHandler * handler,GType type,const gchar * options)319 plugin_entry_filesel_create_func (GdaDataHandler *handler, GType type, const gchar *options)
320 {
321 	return (GdauiDataEntry *) gdaui_entry_filesel_new (handler, type, options);
322 }
323 
324 static GdauiDataEntry *
plugin_entry_cidr_create_func(GdaDataHandler * handler,GType type,G_GNUC_UNUSED const gchar * options)325 plugin_entry_cidr_create_func (GdaDataHandler *handler, GType type, G_GNUC_UNUSED const gchar *options)
326 {
327 	return (GdauiDataEntry *) gdaui_entry_cidr_new (handler, type);
328 }
329 
330 static GdauiDataEntry *
plugin_entry_format_create_func(GdaDataHandler * handler,GType type,const gchar * options)331 plugin_entry_format_create_func (GdaDataHandler *handler, GType type, const gchar *options)
332 {
333 	return (GdauiDataEntry *) gdaui_entry_format_new (handler, type, options);
334 }
335 
336 static GdauiDataEntry *
plugin_entry_text_create_func(GdaDataHandler * handler,GType type,const gchar * options)337 plugin_entry_text_create_func (GdaDataHandler *handler, GType type, const gchar *options)
338 {
339 	return (GdauiDataEntry *) gdaui_entry_text_new (handler, type, options);
340 }
341 
342 static GdauiDataEntry *
plugin_entry_rt_create_func(GdaDataHandler * handler,GType type,const gchar * options)343 plugin_entry_rt_create_func (GdaDataHandler *handler, GType type, const gchar *options)
344 {
345 	return (GdauiDataEntry *) gdaui_entry_rt_new (handler, type, options);
346 }
347 
348 static GdauiDataEntry *
plugin_entry_pict_create_func(GdaDataHandler * handler,GType type,const gchar * options)349 plugin_entry_pict_create_func (GdaDataHandler *handler, GType type, const gchar *options)
350 {
351 	return (GdauiDataEntry *) gdaui_entry_pict_new (handler, type, options);
352 }
353 
354 static GtkCellRenderer *
plugin_cell_renderer_pict_create_func(GdaDataHandler * handler,GType type,const gchar * options)355 plugin_cell_renderer_pict_create_func (GdaDataHandler *handler, GType type, const gchar *options)
356 {
357 	return gdaui_data_cell_renderer_pict_new (handler, type, options);
358 }
359 
360 #ifdef HAVE_LIBGCRYPT
361 static GdauiDataEntry *
plugin_entry_password_create_func(GdaDataHandler * handler,GType type,const gchar * options)362 plugin_entry_password_create_func (GdaDataHandler *handler, GType type, const gchar *options)
363 {
364         return (GdauiDataEntry *) gdaui_entry_password_new (handler, type, options);
365 }
366 
367 static GtkCellRenderer *
plugin_cell_renderer_password_create_func(GdaDataHandler * handler,GType type,const gchar * options)368 plugin_cell_renderer_password_create_func (GdaDataHandler *handler, GType type, const gchar *options)
369 {
370 	return gdaui_data_cell_renderer_password_new (handler, type, options);
371 }
372 #endif
373