1 /*
2  * Copyright (C) 2009 - 2011 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 <string.h>
22 #include <libgda/gda-config.h>
23 #include "gdaui-dsn-selector.h"
24 #include <gtk/gtk.h>
25 
26 struct _GdauiDsnSelectorPrivate {
27 	gchar dummy;
28 };
29 
30 static void gdaui_dsn_selector_class_init (GdauiDsnSelectorClass *klass);
31 static void gdaui_dsn_selector_init       (GdauiDsnSelector *selector,
32 						      GdauiDsnSelectorClass *klass);
33 static void gdaui_dsn_selector_finalize   (GObject *object);
34 
35 static void gdaui_dsn_selector_set_property(GObject *object,
36                                                        guint param_id,
37                                                        const GValue *value,
38                                                        GParamSpec *pspec);
39 static void gdaui_dsn_selector_get_property(GObject *object,
40                                                        guint param_id,
41                                                        GValue *value,
42                                                        GParamSpec *pspec);
43 
44 enum {
45 	PROP_0,
46 
47 	PROP_SOURCE_NAME
48 };
49 
50 /* get a pointer to the parents to be able to call their destructor */
51 static GObjectClass *parent_class = NULL;
52 
53 /*
54  * GdauiDsnSelector class implementation
55  */
56 
57 static void
gdaui_dsn_selector_class_init(GdauiDsnSelectorClass * klass)58 gdaui_dsn_selector_class_init (GdauiDsnSelectorClass *klass)
59 {
60 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
61 
62 	parent_class = g_type_class_peek_parent (klass);
63 
64 	object_class->finalize = gdaui_dsn_selector_finalize;
65 	object_class->set_property = gdaui_dsn_selector_set_property;
66 	object_class->get_property = gdaui_dsn_selector_get_property;
67 
68 	g_object_class_install_property (object_class, PROP_SOURCE_NAME,
69 	                                 g_param_spec_string ("source-name", NULL, NULL, NULL,
70 	                                                      G_PARAM_WRITABLE | G_PARAM_READABLE));
71 }
72 
73 
74 static void
gdaui_dsn_selector_init(GdauiDsnSelector * selector,G_GNUC_UNUSED GdauiDsnSelectorClass * klass)75 gdaui_dsn_selector_init (GdauiDsnSelector *selector,
76 				    G_GNUC_UNUSED GdauiDsnSelectorClass *klass)
77 {
78 	GdaDataModel *model;
79 	gint cols_index[] = {0};
80 
81 	g_return_if_fail (GDAUI_IS_DSN_SELECTOR (selector));
82 
83 	selector->priv = g_new0 (GdauiDsnSelectorPrivate, 1);
84 
85 	model = gda_config_list_dsn ();
86 	gdaui_combo_set_model (GDAUI_COMBO (selector), model, 1, cols_index);
87 	g_object_unref (model);
88 }
89 
90 static void
gdaui_dsn_selector_finalize(GObject * object)91 gdaui_dsn_selector_finalize (GObject *object)
92 {
93 	GdauiDsnSelector *selector = (GdauiDsnSelector *) object;
94 
95 	g_return_if_fail (GDAUI_IS_DSN_SELECTOR (selector));
96 
97 
98 	g_free (selector->priv);
99 	selector->priv = NULL;
100 
101 	parent_class->finalize (object);
102 }
103 
104 static void
gdaui_dsn_selector_set_property(GObject * object,guint param_id,const GValue * value,GParamSpec * pspec)105 gdaui_dsn_selector_set_property (GObject *object,
106 				 guint param_id,
107 				 const GValue *value,
108 				 GParamSpec *pspec)
109 {
110 	GdauiDsnSelector *selector;
111 	GSList *list;
112 	gint cols_index[] = {0};
113 	selector = GDAUI_DSN_SELECTOR (object);
114 
115 	switch (param_id) {
116 	case PROP_SOURCE_NAME:
117 		list = g_slist_append (NULL, (gpointer) value);
118 		_gdaui_combo_set_selected_ext (GDAUI_COMBO (selector), list, cols_index);
119 		g_slist_free (list);
120 		break;
121 	default:
122 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
123 		break;
124 	}
125 }
126 
127 static void
gdaui_dsn_selector_get_property(GObject * object,guint param_id,GValue * value,GParamSpec * pspec)128 gdaui_dsn_selector_get_property (GObject *object,
129 				 guint param_id,
130 				 GValue *value,
131 				 GParamSpec *pspec)
132 {
133 	GdauiDsnSelector *selector;
134 	GSList *list;
135 	gint cols_index[] = {0};
136 	selector = GDAUI_DSN_SELECTOR (object);
137 
138 	switch (param_id) {
139 	case PROP_SOURCE_NAME:
140 		list = _gdaui_combo_get_selected_ext (GDAUI_COMBO (selector), 1, cols_index);
141 		if (list && list->data) {
142 			g_value_set_string (value, g_value_get_string ((GValue*) list->data));
143 			g_slist_free (list);
144 		}
145 		else
146 			g_value_set_string (value, NULL);
147 		break;
148 	default:
149 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
150 		break;
151 	}
152 }
153 
154 GType
_gdaui_dsn_selector_get_type(void)155 _gdaui_dsn_selector_get_type (void)
156 {
157 	static GType type = 0;
158 
159 	if (G_UNLIKELY (type == 0)) {
160 		static const GTypeInfo info = {
161 			sizeof (GdauiDsnSelectorClass),
162 			(GBaseInitFunc) NULL,
163 			(GBaseFinalizeFunc) NULL,
164 			(GClassInitFunc) gdaui_dsn_selector_class_init,
165 			NULL,
166 			NULL,
167 			sizeof (GdauiDsnSelector),
168 			0,
169 			(GInstanceInitFunc) gdaui_dsn_selector_init,
170 			0
171 		};
172 		type = g_type_from_name ("GdauiDsnSelector");
173 		if (type == 0)
174 			type = g_type_register_static (GDAUI_TYPE_COMBO,
175 						       "GdauiDsnSelector",
176 						       &info, 0);
177 	}
178 	return type;
179 }
180 
181 /**
182  * _gdaui_dsn_selector_new
183  *
184  * Create a new #GdauiDsnSelector, which is just a #GtkComboBox
185  * which displays, as its items, all the data sources currently
186  * configured in the system. It is useful for connection and configuration
187  * screens, where the user has to choose a data source to work with.
188  *
189  * Returns: the newly created widget.
190  */
191 GtkWidget *
_gdaui_dsn_selector_new(void)192 _gdaui_dsn_selector_new (void)
193 {
194 	return (GtkWidget*) g_object_new (GDAUI_TYPE_DSN_SELECTOR, NULL);
195 }
196 
197 /**
198  * _gdaui_dsn_selector_get_dsn
199  * @name: name of data source to display.
200  *
201  * Get the Data Source Name (DSN) actualy selected in the #GdauiDsnSelector.
202  *
203  * Returns: the DSN name actualy selected as a new string.
204  */
205 gchar *
_gdaui_dsn_selector_get_dsn(GdauiDsnSelector * selector)206 _gdaui_dsn_selector_get_dsn (GdauiDsnSelector *selector)
207 {
208 	gchar *dsn;
209 
210 	g_object_get (G_OBJECT (selector), "source-name", &dsn, NULL);
211 
212 	return dsn;
213 }
214 
215 /**
216  * _gdaui_dsn_selector_set_dsn
217  * @name: name of data source to display.
218  *
219  * Set the selected Data Source Name (DSN) in the #GdauiDsnSelector.
220  *
221  */
222 void
_gdaui_dsn_selector_set_dsn(GdauiDsnSelector * selector,const gchar * dsn)223 _gdaui_dsn_selector_set_dsn (GdauiDsnSelector *selector, const gchar *dsn)
224 {
225 	g_object_set (G_OBJECT (selector), "source-name", dsn, NULL);
226 }
227