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 <glib/gi18n-lib.h>
22 #include <libgda/libgda.h>
23 #include <sql-parser/gda-sql-parser.h>
24 #include "gda-tree-mgr-label.h"
25 #include "gda-tree-node.h"
26 
27 struct _GdaTreeMgrLabelPriv {
28 	gchar         *label; /* imposed upon construction */
29 };
30 
31 static void gda_tree_mgr_label_class_init (GdaTreeMgrLabelClass *klass);
32 static void gda_tree_mgr_label_init       (GdaTreeMgrLabel *tmgr1, GdaTreeMgrLabelClass *klass);
33 static void gda_tree_mgr_label_dispose    (GObject *object);
34 static void gda_tree_mgr_label_set_property (GObject *object,
35 					      guint param_id,
36 					      const GValue *value,
37 					      GParamSpec *pspec);
38 static void gda_tree_mgr_label_get_property (GObject *object,
39 					      guint param_id,
40 					      GValue *value,
41 					      GParamSpec *pspec);
42 
43 /* virtual methods */
44 static GSList *gda_tree_mgr_label_update_children (GdaTreeManager *manager, GdaTreeNode *node, const GSList *children_nodes,
45 						   gboolean *out_error, GError **error);
46 
47 static GObjectClass *parent_class = NULL;
48 
49 /* properties */
50 enum {
51         PROP_0,
52 	PROP_LABEL
53 };
54 
55 /*
56  * GdaTreeMgrLabel class implementation
57  * @klass:
58  */
59 static void
gda_tree_mgr_label_class_init(GdaTreeMgrLabelClass * klass)60 gda_tree_mgr_label_class_init (GdaTreeMgrLabelClass *klass)
61 {
62 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
63 
64 	parent_class = g_type_class_peek_parent (klass);
65 
66 	/* virtual methods */
67 	((GdaTreeManagerClass*) klass)->update_children = gda_tree_mgr_label_update_children;
68 
69 	/* Properties */
70         object_class->set_property = gda_tree_mgr_label_set_property;
71         object_class->get_property = gda_tree_mgr_label_get_property;
72 
73         g_object_class_install_property (object_class, PROP_LABEL,
74                                          g_param_spec_string ("label", NULL,
75                                                               "Label for the node",
76                                                               NULL, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
77 
78 	object_class->dispose = gda_tree_mgr_label_dispose;
79 }
80 
81 static void
gda_tree_mgr_label_init(GdaTreeMgrLabel * mgr,G_GNUC_UNUSED GdaTreeMgrLabelClass * klass)82 gda_tree_mgr_label_init (GdaTreeMgrLabel *mgr, G_GNUC_UNUSED GdaTreeMgrLabelClass *klass)
83 {
84 	g_return_if_fail (GDA_IS_TREE_MGR_LABEL (mgr));
85 	mgr->priv = g_new0 (GdaTreeMgrLabelPriv, 1);
86 }
87 
88 static void
gda_tree_mgr_label_dispose(GObject * object)89 gda_tree_mgr_label_dispose (GObject *object)
90 {
91 	GdaTreeMgrLabel *mgr = (GdaTreeMgrLabel *) object;
92 
93 	g_return_if_fail (GDA_IS_TREE_MGR_LABEL (mgr));
94 
95 	if (mgr->priv) {
96 		g_free (mgr->priv->label);
97 		g_free (mgr->priv);
98 		mgr->priv = NULL;
99 	}
100 
101 	/* chain to parent class */
102 	parent_class->dispose (object);
103 }
104 
105 /**
106  * gda_tree_mgr_label_get_type:
107  *
108  * Since: 4.2
109  */
110 GType
gda_tree_mgr_label_get_type(void)111 gda_tree_mgr_label_get_type (void)
112 {
113         static GType type = 0;
114 
115         if (G_UNLIKELY (type == 0)) {
116                 static GMutex registering;
117                 static const GTypeInfo info = {
118                         sizeof (GdaTreeMgrLabelClass),
119                         (GBaseInitFunc) NULL,
120                         (GBaseFinalizeFunc) NULL,
121                         (GClassInitFunc) gda_tree_mgr_label_class_init,
122                         NULL,
123                         NULL,
124                         sizeof (GdaTreeMgrLabel),
125                         0,
126                         (GInstanceInitFunc) gda_tree_mgr_label_init,
127 			0
128                 };
129 
130                 g_mutex_lock (&registering);
131                 if (type == 0)
132                         type = g_type_register_static (GDA_TYPE_TREE_MANAGER, "GdaTreeMgrLabel", &info, 0);
133                 g_mutex_unlock (&registering);
134         }
135         return type;
136 }
137 
138 static void
gda_tree_mgr_label_set_property(GObject * object,guint param_id,const GValue * value,GParamSpec * pspec)139 gda_tree_mgr_label_set_property (GObject *object,
140 				   guint param_id,
141 				   const GValue *value,
142 				   GParamSpec *pspec)
143 {
144         GdaTreeMgrLabel *mgr;
145 
146         mgr = GDA_TREE_MGR_LABEL (object);
147         if (mgr->priv) {
148                 switch (param_id) {
149 		case PROP_LABEL:
150 			mgr->priv->label = g_value_dup_string (value);
151 			break;
152 		default:
153 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
154 			break;
155                 }
156         }
157 }
158 
159 static void
gda_tree_mgr_label_get_property(GObject * object,guint param_id,GValue * value,GParamSpec * pspec)160 gda_tree_mgr_label_get_property (GObject *object,
161 				   guint param_id,
162 				   GValue *value,
163 				   GParamSpec *pspec)
164 {
165         GdaTreeMgrLabel *mgr;
166 
167         mgr = GDA_TREE_MGR_LABEL (object);
168         if (mgr->priv) {
169                 switch (param_id) {
170 		case PROP_LABEL:
171 			g_value_set_string (value, mgr->priv->label);
172 			break;
173 		default:
174 			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
175 			break;
176                 }
177         }
178 }
179 
180 /**
181  * gda_tree_mgr_label_new:
182  * @label: a label string
183  *
184  * Creates a new #GdaTreeManager object which will add one tree node labelled @label
185  *
186  * Returns: (transfer full): a new #GdaTreeManager object
187  *
188  * Since: 4.2
189  */
190 GdaTreeManager*
gda_tree_mgr_label_new(const gchar * label)191 gda_tree_mgr_label_new (const gchar *label)
192 {
193 	GdaTreeMgrLabel *mgr;
194 	mgr = (GdaTreeMgrLabel*) g_object_new (GDA_TYPE_TREE_MGR_LABEL,
195 					       "label", label, NULL);
196 	return (GdaTreeManager*) mgr;
197 }
198 
199 static GSList *
gda_tree_mgr_label_update_children(GdaTreeManager * manager,GdaTreeNode * node,const GSList * children_nodes,G_GNUC_UNUSED gboolean * out_error,G_GNUC_UNUSED GError ** error)200 gda_tree_mgr_label_update_children (GdaTreeManager *manager, GdaTreeNode *node, const GSList *children_nodes,
201 				    G_GNUC_UNUSED gboolean *out_error, G_GNUC_UNUSED GError **error)
202 {
203 	if (children_nodes) {
204 		GSList *list = g_slist_copy ((GSList*) children_nodes);
205 		g_slist_foreach (list, (GFunc) g_object_ref, NULL);
206 		return list;
207 	}
208 
209 	GdaTreeMgrLabel *mgr = GDA_TREE_MGR_LABEL (manager);
210 	GdaTreeNode *snode;
211 
212 	snode = gda_tree_manager_create_node (manager, node, mgr->priv->label ? mgr->priv->label : _("No name"));
213 	return g_slist_prepend (NULL, snode);
214 }
215