1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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.
27 #include "tree-data.h"
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
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
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
56 gbf_tree_data_get_uri (GbfTreeData
*data
)
58 return data
->node
? g_file_get_uri (anjuta_project_node_get_file (data
->node
)) : NULL
;
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
)
72 target
= g_file_get_child (data
->group
, data
->target
);
76 else if (data
->group
!= NULL
)
78 return g_object_ref (g_file_get_uri (data
->group
));
85 gbf_tree_data_get_path (GbfTreeData
*data
)
87 return data
->node
? g_file_get_path (anjuta_project_node_get_file (data
->node
)) : NULL
;
91 gbf_tree_data_get_name (GbfTreeData
*data
)
93 return data
->node
!= NULL
? anjuta_project_node_get_name (data
->node
) : data
->name
;
97 gbf_tree_data_get_node (GbfTreeData
*data
)
103 gbf_tree_data_equal (GbfTreeData
*data_a
, GbfTreeData
*data_b
)
107 equal
= data_a
== data_b
;
108 if (!equal
&& (data_a
!= NULL
) && (data_b
!= NULL
))
110 equal
= data_a
->type
== data_b
->type
;
113 if ((data_a
->group
!= NULL
) && (data_b
->group
!= NULL
))
115 equal
= g_file_equal (data_a
->group
, data_b
->group
);
120 if ((data_a
->target
!= NULL
) && (data_b
->target
!= NULL
))
122 equal
= strcmp (data_a
->target
, data_b
->target
) == 0;
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
);
144 gbf_tree_data_equal_file (GbfTreeData
*data
, GbfTreeNodeType type
, GFile
*file
)
146 gboolean equal
= FALSE
;
150 AnjutaProjectNode
*node
= gbf_tree_data_get_node (data
);
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
))
169 gbf_tree_data_equal_name (GbfTreeData
*data
, const gchar
*name
)
171 return g_strcmp0 (data
->name
, name
) == 0;
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
;
184 case ANJUTA_PROJECT_GROUP
:
185 tree_type
= GBF_TREE_NODE_GROUP
;
187 case ANJUTA_PROJECT_TARGET
:
188 tree_type
= GBF_TREE_NODE_TARGET
;
190 case ANJUTA_PROJECT_SOURCE
:
191 tree_type
= GBF_TREE_NODE_SOURCE
;
193 case ANJUTA_PROJECT_MODULE
:
194 tree_type
= GBF_TREE_NODE_MODULE
;
196 case ANJUTA_PROJECT_PACKAGE
:
197 tree_type
= GBF_TREE_NODE_PACKAGE
;
200 tree_type
= GBF_TREE_NODE_UNKNOWN
;
208 gbf_tree_data_invalidate (GbfTreeData
*data
)
210 data
->type
= GBF_TREE_NODE_INVALID
;
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
);
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;
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
;
249 data
->name
= g_strdup (name
);
250 data
->expanded
= expanded
;
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
;
263 data
->name
= g_strdup (anjuta_project_node_get_name (group
));
265 data
->group
= g_object_ref (anjuta_project_node_get_file (group
));
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
;
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
));
288 gbf_tree_data_new_object (AnjutaProjectNode
*node
)
290 GbfTreeData
*data
= g_slice_new0 (GbfTreeData
);
292 AnjutaProjectNode
*parent
;
294 data
->type
= GBF_TREE_NODE_OBJECT
;
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
,
305 data
->name
= g_strdup (g_file_info_get_display_name (ginfo
));
306 g_object_unref(ginfo
);
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
));
331 gbf_tree_data_new_source (AnjutaProjectNode
*source
)
333 GbfTreeData
*data
= g_slice_new0 (GbfTreeData
);
335 AnjutaProjectNode
*parent
;
337 data
->type
= GBF_TREE_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
,
348 data
->name
= g_strdup (g_file_info_get_display_name (ginfo
));
349 g_object_unref(ginfo
);
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
));
374 gbf_tree_data_new_root (AnjutaProjectNode
*root
)
376 GbfTreeData
*data
= g_slice_new0 (GbfTreeData
);
378 data
->type
= GBF_TREE_NODE_ROOT
;
380 data
->name
= g_strdup (anjuta_project_node_get_name (root
));
386 gbf_tree_data_new_module (AnjutaProjectNode
*module
)
388 GbfTreeData
*data
= g_slice_new0 (GbfTreeData
);
390 data
->type
= GBF_TREE_NODE_MODULE
;
392 data
->name
= g_strdup (anjuta_project_node_get_name (module
));
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
));
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
);
419 case ANJUTA_PROJECT_TARGET
:
420 data
= gbf_tree_data_new_target(node
);
422 case ANJUTA_PROJECT_OBJECT
:
423 data
= gbf_tree_data_new_object (node
);
425 case ANJUTA_PROJECT_SOURCE
:
426 data
= gbf_tree_data_new_source (node
);
428 case ANJUTA_PROJECT_MODULE
:
429 data
= gbf_tree_data_new_module (node
);
431 case ANJUTA_PROJECT_PACKAGE
:
432 data
= gbf_tree_data_new_package (node
);
434 case ANJUTA_PROJECT_ROOT
:
435 data
= gbf_tree_data_new_root (node
);
446 gbf_tree_data_free (GbfTreeData
*data
)
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
);