search: Use G_REGEX_MULTILINE for find in files.
[anjuta.git] / plugins / am-project / amp-node.c
blobcf64fce7cea295f0a24170a92f10ee294942f208
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4; coding: utf-8 -*- */
2 /* amp-node.c
4 * Copyright (C) 2009 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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include "amp-node.h"
29 #include "am-properties.h"
31 #include "amp-root.h"
32 #include "amp-module.h"
33 #include "amp-package.h"
34 #include "amp-group.h"
35 #include "amp-target.h"
36 #include "amp-object.h"
37 #include "amp-source.h"
39 #include <libanjuta/anjuta-debug.h>
41 #include <glib/gi18n.h>
43 #include <memory.h>
44 #include <string.h>
45 #include <ctype.h>
48 /* Public functions
49 *---------------------------------------------------------------------------*/
51 AnjutaProjectNode *
52 amp_node_new_valid(AnjutaProjectNode *parent, AnjutaProjectNodeType type, GFile *file, const gchar *name, GError **error)
54 AnjutaProjectNode *node = NULL;
55 AnjutaProjectNode *group = NULL;
56 GFile *new_file = NULL;
58 switch (type & ANJUTA_PROJECT_TYPE_MASK) {
59 case ANJUTA_PROJECT_GROUP:
60 if ((file == NULL) && (name != NULL))
62 if (g_path_is_absolute (name))
64 new_file = g_file_new_for_path (name);
66 else
68 new_file = g_file_get_child (anjuta_project_node_get_file (parent), name);
70 file = new_file;
72 node = ANJUTA_PROJECT_NODE (amp_group_node_new_valid (file, FALSE, error));
73 break;
74 case ANJUTA_PROJECT_TARGET:
75 node = ANJUTA_PROJECT_NODE (amp_target_node_new_valid (name, type, NULL, 0, error));
76 break;
77 case ANJUTA_PROJECT_OBJECT:
78 node = ANJUTA_PROJECT_NODE (amp_object_node_new_valid (file, type, error));
79 break;
80 case ANJUTA_PROJECT_SOURCE:
81 /* Look for parent */
82 group = anjuta_project_node_parent_type (parent, ANJUTA_PROJECT_GROUP);
84 if ((file == NULL) && (name != NULL))
86 /* Find file from name */
87 if (anjuta_project_node_get_node_type (group) == ANJUTA_PROJECT_GROUP)
89 if (g_path_is_absolute (name))
91 new_file = g_file_new_for_path (name);
93 else
95 new_file = g_file_get_child (anjuta_project_node_get_file (group), name);
98 else
100 new_file = g_file_new_for_commandline_arg (name);
102 file = new_file;
105 /* Check that source file is inside the project if it is not belonging to a package */
106 if ((anjuta_project_node_get_node_type (group) == ANJUTA_PROJECT_GROUP) && (anjuta_project_node_get_node_type (parent) != ANJUTA_PROJECT_MODULE))
108 AnjutaProjectNode *root;
109 gchar *relative;
111 root = anjuta_project_node_root (group);
112 relative = g_file_get_relative_path (anjuta_project_node_get_file (root), file);
113 g_free (relative);
114 if (relative == NULL)
116 /* Source outside the project directory, so copy the file */
117 GFile *dest;
118 gchar *basename;
120 basename = g_file_get_basename (file);
121 dest = g_file_get_child (anjuta_project_node_get_file (group), basename);
122 g_free (basename);
124 g_file_copy_async (file, dest, G_FILE_COPY_BACKUP, G_PRIORITY_DEFAULT, NULL, NULL, NULL, NULL, NULL);
125 if (new_file != NULL) g_object_unref (new_file);
126 new_file = dest;
127 file = dest;
131 node = ANJUTA_PROJECT_NODE (amp_source_node_new_valid (file, type, error));
132 break;
133 case ANJUTA_PROJECT_MODULE:
134 node = ANJUTA_PROJECT_NODE (amp_module_node_new_valid (name, error));
135 break;
136 case ANJUTA_PROJECT_PACKAGE:
137 node = ANJUTA_PROJECT_NODE (amp_package_node_new_valid (name, error));
138 break;
139 default:
140 g_assert_not_reached ();
141 break;
143 if (node != NULL) node->type = type;
144 if (new_file != NULL) g_object_unref (new_file);
146 return node;
149 gboolean
150 amp_node_load (AmpNode *node,
151 AmpNode *parent,
152 AmpProject *project,
153 GError **error)
155 g_return_val_if_fail (AMP_IS_NODE (node), FALSE);
157 return AMP_NODE_GET_CLASS (node)->load (node, parent, project, error);
160 gboolean
161 amp_node_save (AmpNode *node,
162 AmpNode *parent,
163 AmpProject *project,
164 GError **error)
166 g_return_val_if_fail (AMP_IS_NODE (node), FALSE);
168 return AMP_NODE_GET_CLASS (node)->save (node, parent, project, error);
171 gboolean
172 amp_node_update (AmpNode *node,
173 AmpNode *new_node)
175 g_return_val_if_fail (AMP_IS_NODE (node), FALSE);
177 return AMP_NODE_GET_CLASS (node)->update (node, new_node);
180 gboolean
181 amp_node_write (AmpNode *node,
182 AmpNode *parent,
183 AmpProject *project,
184 GError **error)
186 g_return_val_if_fail (AMP_IS_NODE (node), FALSE);
188 return AMP_NODE_GET_CLASS (node)->write (node, parent, project, error);
191 gboolean
192 amp_node_erase (AmpNode *node,
193 AmpNode *parent,
194 AmpProject *project,
195 GError **error)
197 g_return_val_if_fail (AMP_IS_NODE (node), FALSE);
199 return AMP_NODE_GET_CLASS (node)->erase (node, parent, project, error);
202 /* AmpNode implementation
203 *---------------------------------------------------------------------------*/
205 static gboolean
206 amp_node_real_load (AmpNode *node,
207 AmpNode *parent,
208 AmpProject *project,
209 GError **error)
211 return FALSE;
214 static gboolean
215 amp_node_real_save (AmpNode *node,
216 AmpNode *parent,
217 AmpProject *project,
218 GError **error)
220 /* Save nothing by default */
221 return TRUE;
224 static gboolean
225 amp_node_real_update (AmpNode *node,
226 AmpNode *new_node)
228 return FALSE;
231 static gboolean
232 amp_node_real_write (AmpNode *node,
233 AmpNode *parent,
234 AmpProject *project,
235 GError **error)
237 return FALSE;
240 static gboolean
241 amp_node_real_erase (AmpNode *node,
242 AmpNode *parent,
243 AmpProject *project,
244 GError **error)
246 return FALSE;
250 /* GObjet implementation
251 *---------------------------------------------------------------------------*/
253 G_DEFINE_DYNAMIC_TYPE (AmpNode, amp_node, ANJUTA_TYPE_PROJECT_NODE);
255 static void
256 amp_node_init (AmpNode *node)
260 static void
261 amp_node_finalize (GObject *object)
263 AmpNode *node = AMP_NODE (object);
265 /* Subclasses set this directly and it should not be freed. */
266 node->parent.properties_info = NULL;
268 g_list_foreach (node->parent.properties, (GFunc)amp_property_free, NULL);
269 g_list_free (node->parent.properties);
270 node->parent.properties = NULL;
272 G_OBJECT_CLASS (amp_node_parent_class)->finalize (object);
275 static void
276 amp_node_class_init (AmpNodeClass *klass)
278 GObjectClass* object_class = G_OBJECT_CLASS (klass);
280 object_class->finalize = amp_node_finalize;
282 klass->load = amp_node_real_load;
283 klass->save = amp_node_real_save;
284 klass->update = amp_node_real_update;
285 klass->write = amp_node_real_write;
286 klass->erase = amp_node_real_erase;
289 static void
290 amp_node_class_finalize (AmpNodeClass *klass)
296 /* Register function
297 *---------------------------------------------------------------------------*/
299 void
300 amp_node_register (GTypeModule *module)
302 amp_node_register_type (module);
303 amp_group_node_register (module);
304 amp_root_node_register (module);
305 amp_module_node_register (module);
306 amp_package_node_register (module);
307 amp_target_node_register (module);
308 amp_object_node_register (module);
309 amp_source_node_register (module);