Updated Spanish translation
[anjuta.git] / plugins / project-manager / tree-data.c
blobd7769aafe18e9fb7a770e84dea03622dacbb573b
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /* tree-data.c
4 * Copyright (C) 2010 Sébastien Granjoux
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <gio/gio.h>
27 #include "tree-data.h"
29 #include <string.h>
31 /**
32 * The GbfTreeData object store all data needed by each element of the project
33 * tree view. It has the same role than AnjutaProjectNode in the project
34 * backend and normally there is one GbfTreeData for each AnjutaProjectNode.
36 * The GbfTreeData objects do not have a pointer to their corresponding
37 * AnjutaProjectNode because they do not have the same life time. By example
38 * when a project is reloaded all AnjutaProjectNode are destroyed but the
39 * GbfTreeData stay alive. They contain enough information to be able to
40 * find the corresponding AnjutaProjectNode though, but it is a search so
41 * it takes more time.
43 * Several GbfTreeData can correspond to the same AnjutaProjectNode because
44 * the tree view can contain shortcuts on alreay existing project node. Each
45 * shortcut has its own GbfTreeData object containing the same data than the
46 * root object.
48 * The GbfTreeData objects are not owned by the tree view which keeps only
49 * pointer on them. When removing a element in the tree view, it is the
50 * responsability of the routine to free all GbfTreeData objects before. On
51 * the other hand, it must not be freed after getting one pointer from
52 * the tree view.
55 gchar *
56 gbf_tree_data_get_uri (GbfTreeData *data)
58 return data->node ? g_file_get_uri (anjuta_project_node_get_file (data->node)) : NULL;
61 GFile *
62 gbf_tree_data_get_file (GbfTreeData *data)
64 if (data->source != NULL)
66 return g_object_ref (g_file_get_uri (data->source));
68 else if (data->target != NULL)
70 GFile *target;
72 target = g_file_get_child (data->group, data->target);
74 return target;
76 else if (data->group != NULL)
78 return g_object_ref (g_file_get_uri (data->group));
81 return NULL;
84 gchar *
85 gbf_tree_data_get_path (GbfTreeData *data)
87 return data->node ? g_file_get_path (anjuta_project_node_get_file (data->node)) : NULL;
90 const gchar *
91 gbf_tree_data_get_name (GbfTreeData *data)
93 return data->node != NULL ? anjuta_project_node_get_name (data->node) : data->name;
96 AnjutaProjectNode *
97 gbf_tree_data_get_node (GbfTreeData *data)
99 return data->node;
102 gboolean
103 gbf_tree_data_equal (GbfTreeData *data_a, GbfTreeData *data_b)
105 gboolean equal;
107 equal = data_a == data_b;
108 if (!equal && (data_a != NULL) && (data_b != NULL))
110 equal = data_a->type == data_b->type;
111 if (equal)
113 if ((data_a->group != NULL) && (data_b->group != NULL))
115 equal = g_file_equal (data_a->group, data_b->group);
118 if (equal)
120 if ((data_a->target != NULL) && (data_b->target != NULL))
122 equal = strcmp (data_a->target, data_b->target) == 0;
125 if (equal)
127 if ((data_a->source != NULL) && (data_b->source != NULL))
129 equal = g_file_equal (data_a->source, data_b->source);
134 else if ((data_a->type == GBF_TREE_NODE_UNKNOWN) || (data_b->type == GBF_TREE_NODE_UNKNOWN))
136 equal = strcmp (data_b->name, data_a->name);
140 return equal;
143 gboolean
144 gbf_tree_data_equal_file (GbfTreeData *data, GbfTreeNodeType type, GFile *file)
146 gboolean equal = FALSE;
148 if (data != NULL)
150 AnjutaProjectNode *node = gbf_tree_data_get_node (data);
152 if (node != NULL)
154 if ((type == GBF_TREE_NODE_UNKNOWN) || (type == data->type))
156 GFile* node_file = anjuta_project_node_get_file (node);
157 if (node_file && g_file_equal (node_file, file))
159 equal = TRUE;
165 return equal;
168 gboolean
169 gbf_tree_data_equal_name (GbfTreeData *data, const gchar *name)
171 return g_strcmp0 (data->name, name) == 0;
174 GbfTreeNodeType
175 gbf_tree_node_type_from_project (AnjutaProjectNodeType type)
177 GbfTreeNodeType tree_type;
179 switch (type & ANJUTA_PROJECT_TYPE_MASK)
181 case ANJUTA_PROJECT_ROOT:
182 tree_type = GBF_TREE_NODE_ROOT;
183 break;
184 case ANJUTA_PROJECT_GROUP:
185 tree_type = GBF_TREE_NODE_GROUP;
186 break;
187 case ANJUTA_PROJECT_TARGET:
188 tree_type = GBF_TREE_NODE_TARGET;
189 break;
190 case ANJUTA_PROJECT_SOURCE:
191 tree_type = GBF_TREE_NODE_SOURCE;
192 break;
193 case ANJUTA_PROJECT_MODULE:
194 tree_type = GBF_TREE_NODE_MODULE;
195 break;
196 case ANJUTA_PROJECT_PACKAGE:
197 tree_type = GBF_TREE_NODE_PACKAGE;
198 break;
199 default:
200 tree_type = GBF_TREE_NODE_UNKNOWN;
201 break;
204 return tree_type;
207 void
208 gbf_tree_data_invalidate (GbfTreeData *data)
210 data->type = GBF_TREE_NODE_INVALID;
213 GbfTreeData *
214 gbf_tree_data_new_string (const gchar *string)
216 GbfTreeData *data = g_slice_new0 (GbfTreeData);
218 data->type = GBF_TREE_NODE_STRING;
219 data->name = g_strdup (string);
221 return data;
224 GbfTreeData *
225 gbf_tree_data_new_shortcut (GbfTreeData *src)
227 GbfTreeData *data = g_slice_new0 (GbfTreeData);
229 data->type = GBF_TREE_NODE_SHORTCUT;
230 data->node = src->node;
231 data->name = g_strdup (src->name);
232 data->group = src->group == NULL ? NULL : g_object_ref (src->group);
233 data->target = g_strdup (src->target);
234 data->source = src->source == NULL ? NULL : g_object_ref (src->source);
235 data->is_shortcut = TRUE;
236 data->shortcut = src;
237 //src->shortcut = data;
239 return data;
242 GbfTreeData *
243 gbf_tree_data_new_proxy (const char *name, gboolean expanded)
245 GbfTreeData *data = g_slice_new0 (GbfTreeData);
247 data->type = GBF_TREE_NODE_UNKNOWN;
248 data->node = NULL;
249 data->name = g_strdup (name);
250 data->expanded = expanded;
252 return data;
255 GbfTreeData *
256 gbf_tree_data_new_group (AnjutaProjectNode *group)
258 GbfTreeData *data = g_slice_new0 (GbfTreeData);
260 data->type = anjuta_project_node_parent(group) == NULL ? GBF_TREE_NODE_ROOT : GBF_TREE_NODE_GROUP;
261 data->node = group;
263 data->name = g_strdup (anjuta_project_node_get_name (group));
265 data->group = g_object_ref (anjuta_project_node_get_file (group));
267 return data;
270 GbfTreeData *
271 gbf_tree_data_new_target (AnjutaProjectNode *target)
273 GbfTreeData *data = g_slice_new0 (GbfTreeData);
274 AnjutaProjectNode *group;
276 data->type = GBF_TREE_NODE_TARGET;
277 data->node = target;
278 data->name = g_strdup (anjuta_project_node_get_name (target));
280 group = anjuta_project_node_parent (target);
281 data->group = g_object_ref (anjuta_project_node_get_file (group));
282 data->target = g_strdup (anjuta_project_node_get_name (target));
284 return data;
287 GbfTreeData *
288 gbf_tree_data_new_object (AnjutaProjectNode *node)
290 GbfTreeData *data = g_slice_new0 (GbfTreeData);
291 GFileInfo *ginfo;
292 AnjutaProjectNode *parent;
294 data->type = GBF_TREE_NODE_OBJECT;
295 data->node = node;
297 data->source = g_object_ref (anjuta_project_node_get_file (node));
299 ginfo = g_file_query_info (data->source,
300 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
301 G_FILE_QUERY_INFO_NONE,
302 NULL, NULL);
303 if (ginfo)
305 data->name = g_strdup (g_file_info_get_display_name (ginfo));
306 g_object_unref(ginfo);
308 else
310 data->name = g_file_get_basename (data->source);
313 parent = anjuta_project_node_parent (node);
314 if (anjuta_project_node_get_node_type (parent) == ANJUTA_PROJECT_GROUP)
316 data->group = g_object_ref (anjuta_project_node_get_file (parent));
318 else if (anjuta_project_node_get_node_type (parent) == ANJUTA_PROJECT_TARGET)
320 AnjutaProjectNode *group;
322 group = anjuta_project_node_parent (parent);
323 data->group = g_object_ref (anjuta_project_node_get_file (group));
324 data->target = g_strdup (anjuta_project_node_get_name (parent));
327 return data;
330 GbfTreeData *
331 gbf_tree_data_new_source (AnjutaProjectNode *source)
333 GbfTreeData *data = g_slice_new0 (GbfTreeData);
334 GFileInfo *ginfo;
335 AnjutaProjectNode *parent;
337 data->type = GBF_TREE_NODE_SOURCE;
338 data->node = source;
340 data->source = g_object_ref (anjuta_project_node_get_file (source));
342 ginfo = g_file_query_info (data->source,
343 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
344 G_FILE_QUERY_INFO_NONE,
345 NULL, NULL);
346 if (ginfo)
348 data->name = g_strdup (g_file_info_get_display_name (ginfo));
349 g_object_unref(ginfo);
351 else
353 data->name = g_file_get_basename (data->source);
356 parent = anjuta_project_node_parent (source);
357 if (anjuta_project_node_get_node_type (parent) == ANJUTA_PROJECT_GROUP)
359 data->group = g_object_ref (anjuta_project_node_get_file (parent));
361 else if (anjuta_project_node_get_node_type (parent) == ANJUTA_PROJECT_TARGET)
363 AnjutaProjectNode *group;
365 group = anjuta_project_node_parent (parent);
366 data->group = g_object_ref (anjuta_project_node_get_file (group));
367 data->target = g_strdup (anjuta_project_node_get_name (parent));
370 return data;
373 GbfTreeData *
374 gbf_tree_data_new_root (AnjutaProjectNode *root)
376 GbfTreeData *data = g_slice_new0 (GbfTreeData);
378 data->type = GBF_TREE_NODE_ROOT;
379 data->node = root;
380 data->name = g_strdup (anjuta_project_node_get_name (root));
382 return data;
385 GbfTreeData *
386 gbf_tree_data_new_module (AnjutaProjectNode *module)
388 GbfTreeData *data = g_slice_new0 (GbfTreeData);
390 data->type = GBF_TREE_NODE_MODULE;
391 data->node = module;
392 data->name = g_strdup (anjuta_project_node_get_name (module));
394 return data;
397 GbfTreeData *
398 gbf_tree_data_new_package (AnjutaProjectNode *package)
400 GbfTreeData *data = g_slice_new0 (GbfTreeData);
402 data->type = GBF_TREE_NODE_PACKAGE;
403 data->node = package;
404 data->name = g_strdup (anjuta_project_node_get_name (package));
406 return data;
409 GbfTreeData *
410 gbf_tree_data_new_node (AnjutaProjectNode *node)
412 GbfTreeData *data = NULL;
414 switch (anjuta_project_node_get_node_type (node))
416 case ANJUTA_PROJECT_GROUP:
417 data = gbf_tree_data_new_group (node);
418 break;
419 case ANJUTA_PROJECT_TARGET:
420 data = gbf_tree_data_new_target(node);
421 break;
422 case ANJUTA_PROJECT_OBJECT:
423 data = gbf_tree_data_new_object (node);
424 break;
425 case ANJUTA_PROJECT_SOURCE:
426 data = gbf_tree_data_new_source (node);
427 break;
428 case ANJUTA_PROJECT_MODULE:
429 data = gbf_tree_data_new_module (node);
430 break;
431 case ANJUTA_PROJECT_PACKAGE:
432 data = gbf_tree_data_new_package (node);
433 break;
434 case ANJUTA_PROJECT_ROOT:
435 data = gbf_tree_data_new_root (node);
436 break;
437 default:
438 break;
441 return data;
445 void
446 gbf_tree_data_free (GbfTreeData *data)
448 if (data)
450 g_free (data->name);
451 if (data->group != NULL) g_object_unref (data->group);
452 g_free (data->target);
453 if (data->source != NULL) g_object_unref (data->source);
454 //if (data->shortcut) data->shortcut->shortcut = NULL;
455 if (data->properties_dialog) gtk_widget_destroy (data->properties_dialog);
456 g_slice_free (GbfTreeData, data);