Added byline
[anjuta.git] / plugins / am-project / amp-package.c
blob5de6ef032afe260a0b014eff9a455bbbcbf7ac5f
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4; coding: utf-8 -*- */
2 /* am-package.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., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include "amp-package.h"
29 #include "amp-node.h"
30 #include "am-scanner.h"
31 #include "am-properties.h"
32 #include "ac-writer.h"
35 #include <libanjuta/interfaces/ianjuta-project.h>
36 #include <libanjuta/anjuta-utils.h>
37 #include <libanjuta/anjuta-pkg-config.h>
39 #include <libanjuta/anjuta-debug.h>
41 #include <glib/gi18n.h>
43 #include <memory.h>
44 #include <string.h>
45 #include <ctype.h>
47 /* Types
48 *---------------------------------------------------------------------------*/
50 struct _AmpPackageNode {
51 AnjutaProjectNode base;
52 gchar *version;
53 AnjutaToken *token;
56 typedef struct _AmpPackageNodeClass AmpPackageNodeClass;
58 struct _AmpPackageNodeClass {
59 AmpNodeClass parent_class;
62 G_DEFINE_DYNAMIC_TYPE (AmpPackageNode, amp_package_node, AMP_TYPE_NODE);
65 /* Package objects
66 *---------------------------------------------------------------------------*/
68 AmpPackageNode*
69 amp_package_node_new (const gchar *name)
71 AmpPackageNode *node = NULL;
73 node = g_object_new (AMP_TYPE_PACKAGE_NODE, NULL);
74 node->base.name = g_strdup (name);
76 return node;
79 AmpPackageNode*
80 amp_package_node_new_valid (const gchar *name, GError **error)
82 return amp_package_node_new (name);
85 void
86 amp_package_node_free (AmpPackageNode *node)
88 g_object_unref (G_OBJECT (node));
91 void
92 amp_package_node_set_version (AmpPackageNode *node, const gchar *compare, const gchar *version)
94 g_return_if_fail (node != NULL);
95 g_return_if_fail ((version == NULL) || (compare != NULL));
97 g_free (node->version);
98 node->version = version != NULL ? g_strconcat (compare, version, NULL) : NULL;
101 AnjutaToken *
102 amp_package_node_get_token (AmpPackageNode *node)
104 return node->token;
107 void
108 amp_package_node_add_token (AmpPackageNode *node, AnjutaToken *token)
110 node->token = token;
113 void
114 amp_package_node_update_node (AmpPackageNode *node, AmpPackageNode *new_node)
116 g_return_if_fail (new_node != NULL);
118 node->token = new_node->token;
119 g_free (node->version);
120 node->version = new_node->version;
121 new_node->version = NULL;
124 /* AmpNode implementation
125 *---------------------------------------------------------------------------*/
127 static gboolean
128 amp_package_node_load (AmpNode *node, AmpNode *parent, AmpProject *project, GError **error)
130 GList* deps;
131 GList* dep;
132 GList* include_dirs = NULL;
134 deps = anjuta_pkg_config_list_dependencies (anjuta_project_node_get_name (ANJUTA_PROJECT_NODE (node)),
135 error);
136 for (dep = deps; dep != NULL; dep = g_list_next (dep))
138 /* Create a package node for the depedencies */
139 AnjutaProjectNode *pkg;
141 pkg = ANJUTA_PROJECT_NODE (amp_package_node_new (dep->data));
142 anjuta_project_node_append (ANJUTA_PROJECT_NODE (node), pkg);
144 anjuta_util_glist_strings_free (deps);
146 if (*error != NULL)
148 g_warning ("Error getting dependencies: %s", (*error)->message);
149 g_error_free (*error);
150 *error = NULL;
153 if ((include_dirs = anjuta_pkg_config_get_directories (anjuta_project_node_get_name (ANJUTA_PROJECT_NODE (node)),
154 TRUE, error)))
156 GList* include_dir;
158 for (include_dir = include_dirs; include_dir != NULL; include_dir = g_list_next (include_dir))
160 GList* children = NULL;
161 GList* file = NULL;
162 GFile* dir = g_file_new_for_path (include_dir->data);
164 anjuta_util_list_all_dir_children (&children, dir);
165 for (file = g_list_first (children); file != NULL; file = g_list_next (file))
167 /* Create a source for files */
168 AnjutaProjectNode *source;
170 source = amp_node_new_valid (ANJUTA_PROJECT_NODE (parent), ANJUTA_PROJECT_SOURCE, (GFile *)file->data, NULL, NULL);
171 anjuta_project_node_append (ANJUTA_PROJECT_NODE (node), source);
172 g_object_unref ((GObject *)file->data);
174 g_list_free (children);
175 g_object_unref (dir);
178 anjuta_util_glist_strings_free (include_dirs);
180 return TRUE;
183 static gboolean
184 amp_package_node_update (AmpNode *node, AmpNode *new_node)
186 amp_package_node_update_node (AMP_PACKAGE_NODE (node), AMP_PACKAGE_NODE (new_node));
188 return TRUE;
191 static AmpNode *
192 amp_package_node_copy (AmpNode *old_node)
194 AmpNode *new_node;
196 new_node = AMP_NODE_CLASS (amp_package_node_parent_class)->copy (old_node);
197 // FIXME: We should probably copy the version number too because it will not
198 // be updated when the node is reloaded. So later when updating the old_node,
199 // the value will be overwritten with the new node empty value.
200 amp_package_node_add_token (AMP_PACKAGE_NODE (new_node), amp_package_node_get_token (AMP_PACKAGE_NODE (old_node)));
202 return new_node;
205 static gboolean
206 amp_package_node_write (AmpNode *node, AmpNode *parent, AmpProject *project, GError **error)
208 return amp_package_node_create_token (project, AMP_PACKAGE_NODE (node), error);
211 static gboolean
212 amp_package_node_erase (AmpNode *node, AmpNode *parent, AmpProject *project, GError **error)
214 return amp_package_node_delete_token (project, AMP_PACKAGE_NODE (node), error);
218 /* GObjet implementation
219 *---------------------------------------------------------------------------*/
221 static void
222 amp_package_node_init (AmpPackageNode *node)
224 node->base.type = ANJUTA_PROJECT_PACKAGE;
225 node->base.properties_info = amp_get_package_property_list();
226 node->base.state = ANJUTA_PROJECT_CAN_REMOVE;
227 node->version = NULL;
230 static void
231 amp_package_node_finalize (GObject *object)
233 AmpPackageNode *node = AMP_PACKAGE_NODE (object);
235 g_list_foreach (node->base.properties, (GFunc)amp_property_free, NULL);
237 G_OBJECT_CLASS (amp_package_node_parent_class)->finalize (object);
240 static void
241 amp_package_node_class_init (AmpPackageNodeClass *klass)
243 GObjectClass* object_class = G_OBJECT_CLASS (klass);
244 AmpNodeClass* node_class;
246 object_class->finalize = amp_package_node_finalize;
248 node_class = AMP_NODE_CLASS (klass);
249 node_class->load = amp_package_node_load;
250 node_class->update = amp_package_node_update;
251 node_class->copy = amp_package_node_copy;
252 node_class->erase = amp_package_node_erase;
253 node_class->write = amp_package_node_write;
256 static void
257 amp_package_node_class_finalize (AmpPackageNodeClass *klass)
261 void
262 amp_package_node_register (GTypeModule *module)
264 amp_package_node_register_type (module);