configure: fix build of vala plugin
[anjuta.git] / plugins / project-wizard / header.c
bloba3849cfcb143f687a57bbeb782e598f68d14c940
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 header.c
4 Copyright (C) 2004 Sebastien Granjoux
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * header data read in .wiz file, used in the first page (project selection)
24 *---------------------------------------------------------------------------*/
26 #include <config.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29 #include <stdlib.h>
31 #include "header.h"
33 #include <glib.h>
34 #include <glib/gi18n.h>
36 #include <libanjuta/anjuta-utils.h>
38 /*---------------------------------------------------------------------------*/
40 struct _NPWHeader {
41 gchar* name;
42 gint name_lang;
43 gchar* description;
44 gint description_lang;
45 gchar* iconfile;
46 gchar* category;
47 guint order;
48 gchar* filename;
49 GList* required_programs;
50 GList* required_packages;
53 /* Header
54 *---------------------------------------------------------------------------*/
56 NPWHeader*
57 npw_header_new (void)
59 NPWHeader* self;
61 self = g_slice_new0(NPWHeader);
63 return self;
66 void
67 npw_header_free (NPWHeader* self)
69 if (self == NULL) return;
71 g_free (self->name);
72 g_free (self->description);
73 g_free (self->iconfile);
74 g_free (self->category);
75 g_free (self->filename);
76 g_list_free (self->required_programs);
77 g_list_free (self->required_packages);
78 g_slice_free (NPWHeader, self);
81 void
82 npw_header_set_name (NPWHeader* self, const gchar* name, gint lang)
84 if (lang >= self->name_lang)
86 g_free (self->name);
87 self->name = g_strdup (name);
88 self->name_lang = lang;
92 const gchar*
93 npw_header_get_name (const NPWHeader* self)
95 return self->name_lang == 0 ? _(self->name) : self->name;
98 void
99 npw_header_set_filename (NPWHeader* self, const gchar* filename)
101 g_free (self->filename);
102 self->filename = g_strdup (filename);
105 const gchar*
106 npw_header_get_filename (const NPWHeader* self)
108 return self->filename;
111 void
112 npw_header_set_category (NPWHeader* self, const gchar* category)
114 g_free (self->category);
115 self->category =g_strdup (category);
118 const gchar*
119 npw_header_get_category (const NPWHeader* self)
121 return self->category;
124 void
125 npw_header_set_description (NPWHeader* self, const gchar* description, gint lang)
127 if (lang >= self->description_lang)
129 g_free (self->description);
130 self->description = g_strdup (description);
131 self->description_lang = lang;
135 const gchar*
136 npw_header_get_description (const NPWHeader* self)
138 return self->description_lang == 0 ? _(self->description) : self->description;
141 void
142 npw_header_set_iconfile (NPWHeader* self, const gchar* iconfile)
144 g_free (self->iconfile);
145 self->iconfile = g_strdup (iconfile);
148 const gchar*
149 npw_header_get_iconfile (const NPWHeader* self)
151 return self->iconfile;
154 void
155 npw_header_set_order (NPWHeader* self, const gchar* order)
157 self->order = strtoul (order, NULL, 10);
160 const guint
161 npw_header_get_order (const NPWHeader* self)
163 return self->order;
166 void
167 npw_header_add_required_program (NPWHeader* self, const gchar* program)
169 self->required_programs =
170 g_list_prepend (self->required_programs,
171 g_strdup (program));
175 void
176 npw_header_add_required_package (NPWHeader* self, const gchar* package)
178 self->required_packages =
179 g_list_prepend (self->required_packages,
180 g_strdup(package));
183 GList*
184 npw_header_check_required_programs (NPWHeader* self)
186 GList *node = NULL;
187 GList *failed_programs = NULL;
188 for (node = self->required_programs; node; node = g_list_next (node))
190 if (!anjuta_util_prog_is_installed (node->data, FALSE))
192 const gchar *const prog = (const gchar *) node->data;
193 failed_programs = g_list_prepend (failed_programs,
194 g_strdup (prog));
197 return failed_programs;
200 GList*
201 npw_header_check_required_packages (NPWHeader* self)
203 GList *node = NULL;
204 GList *failed_packages = NULL;
205 for (node = self->required_packages; node; node = g_list_next (node))
207 if (!anjuta_util_package_is_installed (node->data, FALSE))
209 const gchar *const pkg = (const gchar *) node->data;
210 failed_packages = g_list_prepend (failed_packages,
211 g_strdup (pkg));
214 return failed_packages;
217 /* Header list
218 *---------------------------------------------------------------------------*/
220 GList*
221 npw_header_list_new (void)
223 GList* list;
225 list = NULL;
227 return list;
230 static void
231 remove_category (GList *list)
233 g_list_foreach (list, (GFunc)npw_header_free, NULL);
234 g_list_free (list);
237 void
238 npw_header_list_free (GList* list)
240 g_list_foreach (list, (GFunc)remove_category, NULL);
241 g_list_free (list);
244 static gint
245 compare_header_name (NPWHeader *a, NPWHeader *b)
247 if (npw_header_get_order (a) == npw_header_get_order (b))
249 return g_utf8_collate (npw_header_get_name (a), npw_header_get_name (b));
251 else if (npw_header_get_order (a) == 0)
253 return 1;
255 else if (npw_header_get_order (b) == 0)
257 return -1;
259 else
261 return npw_header_get_order (a) - npw_header_get_order (b);
265 GList *
266 npw_header_list_insert_header (GList *list, NPWHeader *header)
268 GList *node;
269 GList *template_list;
271 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
273 NPWHeader* first;
274 gint res;
276 template_list = (GList *)node->data;
277 first = (NPWHeader *)template_list->data;
278 res = g_ascii_strcasecmp (npw_header_get_category (first), npw_header_get_category (header));
279 if (res == 0)
281 node->data = g_list_insert_sorted (template_list, header, (GCompareFunc) compare_header_name);
282 return list;
284 else if (res > 0)
286 break;
290 template_list = g_list_prepend (NULL, header);
292 return g_list_insert_before (list, node, template_list);
295 NPWHeader*
296 npw_header_list_find_header (GList *list, NPWHeader *header)
298 GList *node;
299 GList *template_list;
301 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
303 NPWHeader* first;
304 gint res;
306 template_list = (GList *)node->data;
307 first = (NPWHeader *)template_list->data;
308 res = g_ascii_strcasecmp (npw_header_get_category (first), npw_header_get_category (header));
309 if (res == 0)
311 GList *find;
313 find = g_list_find_custom (template_list, header, (GCompareFunc) compare_header_name);
314 if (find != NULL)
316 return (NPWHeader *)find->data;
319 break;
321 else if (res > 0)
323 break;
327 return NULL;