2 * geanyprj - Alternative project support for geany light IDE.
4 * Copyright 2007 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
5 * Copyright 2007 Enrico Tröger <enrico.troeger@uvena.de>
6 * Copyright 2007 Nick Treleaven <nick.treleaven@btinternet.com>
7 * Copyright 2007,2008 Yura Siamashka <yurand2@gmail.com>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "plugindata.h"
30 #include "filetypes.h"
32 #include "pluginmacros.h"
38 extern GeanyData
*geany_data
;
40 const gchar
*project_type_string
[NEW_PROJECT_TYPE_SIZE
] = {
49 project_filter_c_cpp(const gchar
* file
)
51 if (geany_data
->filetype
->detect_from_filename(file
)->id
== GEANY_FILETYPES_C
||
52 geany_data
->filetype
->detect_from_filename(file
)->id
== GEANY_FILETYPES_CPP
)
58 project_filter_c(const gchar
* file
)
60 if (geany_data
->filetype
->detect_from_filename(file
)->id
== GEANY_FILETYPES_C
)
66 project_filter_python(const gchar
* file
)
68 if (geany_data
->filetype
->detect_from_filename(file
)->id
== GEANY_FILETYPES_PYTHON
)
74 project_filter_all(const gchar
* file
)
76 if (geany_data
->filetype
->detect_from_filename(file
)->id
!= GEANY_FILETYPES_ALL
)
82 project_filter_none(UNUSED(const gchar
* file
))
88 void *project_type_filter
[NEW_PROJECT_TYPE_SIZE
] = {
92 project_filter_python
,
98 free_tag_object(gpointer obj
)
100 p_tm
->workspace_remove_object((TMWorkObject
*) obj
, TRUE
, FALSE
);
103 struct GeanyProject
*
106 struct GeanyProject
*ret
;
108 ret
= (struct GeanyProject
*) g_new0(struct GeanyProject
, 1);
109 ret
->tags
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, free_tag_object
);
114 struct GeanyProject
*
115 geany_project_load(const gchar
* path
)
117 struct GeanyProject
*ret
;
118 TMWorkObject
*tm_obj
= NULL
;
122 gchar
*filename
, *locale_filename
;
126 debug("%s path=%s\n", __FUNCTION__
, path
);
131 config
= g_key_file_new();
132 if (!g_key_file_load_from_file(config
, path
, G_KEY_FILE_NONE
, NULL
))
134 g_key_file_free(config
);
139 ret
= geany_project_new();
140 geany_project_set_path(ret
, path
);
142 tmp
= p_utils
->get_setting_string(config
, "project", "name", GEANY_STRING_UNTITLED
);
143 geany_project_set_name(ret
, tmp
);
146 tmp
= p_utils
->get_setting_string(config
, "project", "description", "");
147 geany_project_set_description(ret
, tmp
);
150 tmp
= p_utils
->get_setting_string(config
, "project", "base_path", "");
151 geany_project_set_base_path(ret
, tmp
);
154 tmp
= p_utils
->get_setting_string(config
, "project", "run_cmd", "");
155 geany_project_set_run_cmd(ret
, tmp
);
158 geany_project_set_type_string(ret
,
159 p_utils
->get_setting_string(config
, "project", "type",
160 project_type_string
[0]));
161 geany_project_set_regenerate(ret
,
162 g_key_file_get_boolean(config
, "project", "regenerate", NULL
));
166 geany_project_regenerate_file_list(ret
);
171 key
= g_strdup_printf("file%d", i
);
172 while ((file
= g_key_file_get_string(config
, "files", key
, NULL
)))
174 filename
= get_full_path(path
, file
);
176 locale_filename
= p_utils
->get_locale_from_utf8(filename
);
177 tm_obj
= p_tm
->source_file_new(locale_filename
, FALSE
,
178 geany_data
->filetype
->
179 detect_from_filename(filename
)->name
);
180 g_free(locale_filename
);
183 g_hash_table_insert(ret
->tags
, filename
, tm_obj
);
184 p_tm
->source_file_update(tm_obj
, TRUE
, FALSE
, TRUE
);
191 key
= g_strdup_printf("file%d", i
);
195 g_key_file_free(config
);
200 geany_project_regenerate_file_list(struct GeanyProject
*prj
)
204 debug("%s path=%s\n", __FUNCTION__
, prj
->base_path
);
205 g_hash_table_remove_all(prj
->tags
);
207 lst
= get_file_list(prj
->base_path
, NULL
, project_type_filter
[prj
->type
], NULL
);
208 geany_project_set_tags_from_list(prj
, lst
);
210 g_slist_foreach(lst
, (GFunc
) g_free
, NULL
);
215 geany_project_set_path(struct GeanyProject
*prj
, const gchar
* path
)
217 gchar
*norm_path
= normpath(path
);
220 if (strcmp(prj
->path
, norm_path
) == 0)
226 prj
->path
= norm_path
;
230 geany_project_set_name(struct GeanyProject
*prj
, const gchar
* name
)
234 prj
->name
= g_strdup(name
);
236 if (prj
== g_current_project
)
241 g_free(project
->name
);
242 project
->name
= g_strdup(name
);
248 geany_project_set_type_int(struct GeanyProject
*prj
, gint val
)
254 geany_project_set_type_string(struct GeanyProject
*prj
, const gchar
* val
)
258 for (i
= 0; i
< sizeof(project_type_string
) / sizeof(project_type_string
[0]); i
++)
260 if (strcmp(val
, project_type_string
[i
]) == 0)
261 return geany_project_set_type_int(prj
, i
);
266 geany_project_set_regenerate(struct GeanyProject
*prj
, gboolean val
)
268 prj
->regenerate
= val
;
272 geany_project_set_description(struct GeanyProject
*prj
, const gchar
* description
)
274 if (prj
->description
)
275 g_free(prj
->description
);
276 prj
->description
= g_strdup(description
);
278 if (prj
== g_current_project
)
282 if (project
->description
)
283 g_free(project
->description
);
284 project
->description
= g_strdup(description
);
290 geany_project_set_base_path(struct GeanyProject
*prj
, const gchar
* base_path
)
293 g_free(prj
->base_path
);
295 if (g_path_is_absolute(base_path
))
297 prj
->base_path
= g_strdup(base_path
);
301 prj
->base_path
= get_full_path(prj
->path
, base_path
);
304 if (prj
== g_current_project
)
308 if (project
->base_path
)
309 g_free(project
->base_path
);
310 project
->base_path
= g_strdup(base_path
);
316 geany_project_set_run_cmd(struct GeanyProject
*prj
, const gchar
* run_cmd
)
319 g_free(prj
->run_cmd
);
320 prj
->run_cmd
= g_strdup(run_cmd
);
322 if (prj
== g_current_project
)
326 if (project
->run_cmd
)
327 g_free(project
->run_cmd
);
328 project
->run_cmd
= g_strdup(run_cmd
);
336 geany_project_set_tags_from_list(struct GeanyProject
*prj
, GSList
* files
)
339 gchar
*locale_filename
;
340 TMWorkObject
*tm_obj
= NULL
;
343 g_hash_table_destroy(prj
->tags
);
344 prj
->tags
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, free_tag_object
);
346 for (tmp
= files
; tmp
!= NULL
; tmp
= g_slist_next(tmp
))
348 locale_filename
= p_utils
->get_locale_from_utf8(tmp
->data
);
349 tm_obj
= p_tm
->source_file_new(locale_filename
, FALSE
,
350 geany_data
->filetype
->
351 detect_from_filename(tmp
->data
)->name
);
352 g_free(locale_filename
);
355 g_hash_table_insert(prj
->tags
, g_strdup(tmp
->data
), tm_obj
);
356 p_tm
->source_file_update(tm_obj
, TRUE
, FALSE
, TRUE
);
363 geany_project_free(struct GeanyProject
*prj
)
365 debug("%s prj=%p\n", __FUNCTION__
, prj
);
366 g_return_if_fail(prj
);
372 if (prj
->description
)
373 g_free(prj
->description
);
375 g_free(prj
->base_path
);
377 g_free(prj
->run_cmd
);
379 g_hash_table_destroy(prj
->tags
);
385 geany_project_add_file(struct GeanyProject
*prj
, const gchar
* path
)
388 TMWorkObject
*tm_obj
= NULL
;
392 config
= g_key_file_new();
393 if (!g_key_file_load_from_file(config
, prj
->path
, G_KEY_FILE_NONE
, NULL
))
395 g_key_file_free(config
);
399 if (g_hash_table_lookup(prj
->tags
, path
))
401 g_key_file_free(config
);
405 filename
= p_utils
->get_locale_from_utf8(path
);
406 tm_obj
= p_tm
->source_file_new(filename
, FALSE
,
407 geany_data
->filetype
->detect_from_filename(path
)->name
);
411 g_hash_table_insert(prj
->tags
, g_strdup(path
), tm_obj
);
412 p_tm
->source_file_update(tm_obj
, TRUE
, FALSE
, TRUE
);
414 geany_project_save(prj
);
420 struct GeanyProject
*prj
;
426 geany_project_save_files(gpointer key
, UNUSED(gpointer value
), gpointer user_data
)
430 struct CFGData
*data
= (struct CFGData
*) user_data
;
432 filename
= get_relative_path(data
->prj
->path
, (const gchar
*) key
);
435 fkey
= g_strdup_printf("file%d", data
->i
);
436 g_key_file_set_string(data
->config
, "files", fkey
, filename
);
444 geany_project_remove_file(struct GeanyProject
*prj
, const gchar
* path
)
446 if (!g_hash_table_remove(prj
->tags
, path
))
451 geany_project_save(prj
);
456 geany_project_save(struct GeanyProject
*prj
)
462 base_path
= get_relative_path(prj
->path
, prj
->base_path
);
464 config
= g_key_file_new();
465 g_key_file_load_from_file(config
, prj
->path
, G_KEY_FILE_NONE
, NULL
);
467 g_key_file_set_string(config
, "project", "name", prj
->name
);
468 g_key_file_set_string(config
, "project", "description", prj
->description
);
469 g_key_file_set_string(config
, "project", "base_path", base_path
);
470 g_key_file_set_string(config
, "project", "run_cmd", prj
->run_cmd
);
471 g_key_file_set_boolean(config
, "project", "regenerate", prj
->regenerate
);
472 g_key_file_set_string(config
, "project", "type", project_type_string
[prj
->type
]);
475 data
.config
= config
;
478 g_key_file_remove_group(config
, "files", NULL
);
479 if (!prj
->regenerate
)
481 g_hash_table_foreach(prj
->tags
, geany_project_save_files
, &data
);
483 save_config(config
, prj
->path
);