Fixup various filedefs mappings
[geany-mirror.git] / src / project.c
blob6cb87866c306c69e6d10b11028f192501204429b
1 /*
2 * project.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /** @file project.h
23 * Project Management.
26 #include "geany.h"
28 #include <string.h>
29 #include <unistd.h>
30 #include <errno.h>
32 #include "project.h"
33 #include "projectprivate.h"
35 #include "dialogs.h"
36 #include "support.h"
37 #include "utils.h"
38 #include "ui_utils.h"
39 #include "document.h"
40 #include "msgwindow.h"
41 #include "main.h"
42 #include "keyfile.h"
43 #include "win32.h"
44 #include "build.h"
45 #include "interface.h"
46 #include "editor.h"
47 #include "stash.h"
48 #include "sidebar.h"
49 #include "filetypes.h"
52 ProjectPrefs project_prefs = { NULL, FALSE, FALSE };
55 static GeanyProjectPrivate priv;
56 static GeanyIndentPrefs indentation;
58 static StashGroup *indent_group = NULL;
60 static struct
62 gchar *project_file_path; /* in UTF-8 */
63 } local_prefs = { NULL };
66 static gboolean entries_modified;
68 /* simple struct to keep references to the elements of the properties dialog */
69 typedef struct _PropertyDialogElements
71 GtkWidget *dialog;
72 GtkWidget *notebook;
73 GtkWidget *name;
74 GtkWidget *description;
75 GtkWidget *file_name;
76 GtkWidget *base_path;
77 GtkWidget *patterns;
78 BuildTableData build_properties;
79 } PropertyDialogElements;
82 static gboolean update_config(const PropertyDialogElements *e, gboolean new_project);
83 static void on_file_save_button_clicked(GtkButton *button, PropertyDialogElements *e);
84 static gboolean load_config(const gchar *filename);
85 static gboolean write_config(gboolean emit_signal);
86 static void on_name_entry_changed(GtkEditable *editable, PropertyDialogElements *e);
87 static void on_entries_changed(GtkEditable *editable, PropertyDialogElements *e);
88 static void on_radio_long_line_custom_toggled(GtkToggleButton *radio, GtkWidget *spin_long_line);
89 static void apply_editor_prefs(void);
92 #define SHOW_ERR(args) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args)
93 #define SHOW_ERR1(args, more) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args, more)
94 #define MAX_NAME_LEN 50
95 /* "projects" is part of the default project base path so be careful when translating
96 * please avoid special characters and spaces, look at the source for details or ask Frank */
97 #define PROJECT_DIR _("projects")
100 void project_new(void)
102 GtkWidget *vbox;
103 GtkWidget *table;
104 GtkWidget *image;
105 GtkWidget *button;
106 GtkWidget *bbox;
107 GtkWidget *label;
108 PropertyDialogElements *e;
110 if (! project_ask_close())
111 return;
113 g_return_if_fail(app->project == NULL);
115 e = g_new0(PropertyDialogElements, 1);
116 e->dialog = gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(main_widgets.window),
117 GTK_DIALOG_DESTROY_WITH_PARENT,
118 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
120 gtk_widget_set_name(e->dialog, "GeanyDialogProject");
121 bbox = gtk_hbox_new(FALSE, 0);
122 button = gtk_button_new();
123 image = gtk_image_new_from_stock("gtk-new", GTK_ICON_SIZE_BUTTON);
124 label = gtk_label_new_with_mnemonic(_("C_reate"));
125 gtk_box_pack_start(GTK_BOX(bbox), image, FALSE, FALSE, 3);
126 gtk_box_pack_start(GTK_BOX(bbox), label, FALSE, FALSE, 3);
127 gtk_container_add(GTK_CONTAINER(button), bbox);
128 gtk_dialog_add_action_widget(GTK_DIALOG(e->dialog), button, GTK_RESPONSE_OK);
130 vbox = ui_dialog_vbox_new(GTK_DIALOG(e->dialog));
132 entries_modified = FALSE;
134 table = gtk_table_new(3, 2, FALSE);
135 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
136 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
138 label = gtk_label_new(_("Name:"));
139 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
141 e->name = gtk_entry_new();
142 ui_entry_add_clear_icon(GTK_ENTRY(e->name));
143 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
145 ui_table_add_row(GTK_TABLE(table), 0, label, e->name, NULL);
147 label = gtk_label_new(_("Filename:"));
148 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
150 e->file_name = gtk_entry_new();
151 ui_entry_add_clear_icon(GTK_ENTRY(e->file_name));
152 gtk_entry_set_width_chars(GTK_ENTRY(e->file_name), 30);
153 button = gtk_button_new();
154 g_signal_connect(button, "clicked", G_CALLBACK(on_file_save_button_clicked), e);
155 image = gtk_image_new_from_stock("gtk-open", GTK_ICON_SIZE_BUTTON);
156 gtk_container_add(GTK_CONTAINER(button), image);
157 bbox = gtk_hbox_new(FALSE, 6);
158 gtk_box_pack_start_defaults(GTK_BOX(bbox), e->file_name);
159 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
161 ui_table_add_row(GTK_TABLE(table), 1, label, bbox, NULL);
163 label = gtk_label_new(_("Base path:"));
164 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
166 e->base_path = gtk_entry_new();
167 ui_entry_add_clear_icon(GTK_ENTRY(e->base_path));
168 gtk_widget_set_tooltip_text(e->base_path,
169 _("Base directory of all files that make up the project. "
170 "This can be a new path, or an existing directory tree. "
171 "You can use paths relative to the project filename."));
172 bbox = ui_path_box_new(_("Choose Project Base Path"),
173 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(e->base_path));
175 ui_table_add_row(GTK_TABLE(table), 2, label, bbox, NULL);
177 gtk_container_add(GTK_CONTAINER(vbox), table);
179 /* signals */
180 g_signal_connect(e->name, "changed", G_CALLBACK(on_name_entry_changed), e);
181 /* run the callback manually to initialise the base_path and file_name fields */
182 on_name_entry_changed(GTK_EDITABLE(e->name), e);
184 g_signal_connect(e->file_name, "changed", G_CALLBACK(on_entries_changed), e);
185 g_signal_connect(e->base_path, "changed", G_CALLBACK(on_entries_changed), e);
187 gtk_widget_show_all(e->dialog);
189 while (gtk_dialog_run(GTK_DIALOG(e->dialog)) == GTK_RESPONSE_OK)
191 if (update_config(e, TRUE))
193 if (!write_config(TRUE))
194 SHOW_ERR(_("Project file could not be written"));
195 else
197 ui_set_statusbar(TRUE, _("Project \"%s\" created."), app->project->name);
199 ui_add_recent_project_file(app->project->file_name);
200 break;
204 gtk_widget_destroy(e->dialog);
205 g_free(e);
209 gboolean project_load_file_with_session(const gchar *locale_file_name)
211 if (project_load_file(locale_file_name))
213 if (project_prefs.project_session)
215 configuration_open_files();
216 /* open a new file if no other file was opened */
217 document_new_file_if_non_open();
218 ui_focus_current_document();
220 return TRUE;
222 return FALSE;
226 #ifndef G_OS_WIN32
227 static void run_open_dialog(GtkDialog *dialog)
229 while (gtk_dialog_run(dialog) == GTK_RESPONSE_ACCEPT)
231 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
233 /* try to load the config */
234 if (! project_load_file_with_session(filename))
236 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
238 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), utf8_filename);
239 gtk_widget_grab_focus(GTK_WIDGET(dialog));
240 g_free(utf8_filename);
241 g_free(filename);
242 continue;
244 g_free(filename);
245 break;
248 #endif
251 void project_open(void)
253 const gchar *dir = local_prefs.project_file_path;
254 #ifdef G_OS_WIN32
255 gchar *file;
256 #else
257 GtkWidget *dialog;
258 GtkFileFilter *filter;
259 gchar *locale_path;
260 #endif
261 if (! project_ask_close()) return;
263 #ifdef G_OS_WIN32
264 file = win32_show_project_open_dialog(main_widgets.window, _("Open Project"), dir, FALSE, TRUE);
265 if (file != NULL)
267 /* try to load the config */
268 if (! project_load_file_with_session(file))
270 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), file);
272 g_free(file);
274 #else
276 dialog = gtk_file_chooser_dialog_new(_("Open Project"), GTK_WINDOW(main_widgets.window),
277 GTK_FILE_CHOOSER_ACTION_OPEN,
278 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
279 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
280 gtk_widget_set_name(dialog, "GeanyDialogProject");
282 /* set default Open, so pressing enter can open multiple files */
283 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
284 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
285 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
286 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
287 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
288 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
290 /* add FileFilters */
291 filter = gtk_file_filter_new();
292 gtk_file_filter_set_name(filter, _("All files"));
293 gtk_file_filter_add_pattern(filter, "*");
294 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
295 filter = gtk_file_filter_new();
296 gtk_file_filter_set_name(filter, _("Project files"));
297 gtk_file_filter_add_pattern(filter, "*." GEANY_PROJECT_EXT);
298 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
299 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
301 locale_path = utils_get_locale_from_utf8(dir);
302 if (g_file_test(locale_path, G_FILE_TEST_EXISTS) &&
303 g_file_test(locale_path, G_FILE_TEST_IS_DIR))
305 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
307 g_free(locale_path);
309 gtk_widget_show_all(dialog);
310 run_open_dialog(GTK_DIALOG(dialog));
311 gtk_widget_destroy(GTK_WIDGET(dialog));
312 #endif
316 /* Called when creating, opening, closing and updating projects. */
317 static void update_ui(void)
319 if (main_status.quitting)
320 return;
322 ui_set_window_title(NULL);
323 build_menu_update(NULL);
324 sidebar_openfiles_update_all();
328 static void remove_foreach_project_filetype(gpointer data, gpointer user_data)
330 GeanyFiletype *ft = data;
331 if (ft != NULL)
333 setptr(ft->projfilecmds, NULL);
334 setptr(ft->projexeccmds, NULL);
335 setptr(ft->projerror_regex_string, NULL);
336 ft->project_list_entry = -1;
341 /* open_default will make function reload default session files on close */
342 void project_close(gboolean open_default)
344 g_return_if_fail(app->project != NULL);
346 ui_set_statusbar(TRUE, _("Project \"%s\" closed."), app->project->name);
348 /* use write_config() to save project session files */
349 if (!write_config(FALSE))
350 g_warning("Project file \"%s\" could not be written", app->project->file_name);
352 /* remove project filetypes build entries */
353 if (app->project->build_filetypes_list != NULL)
355 g_ptr_array_foreach(app->project->build_filetypes_list, remove_foreach_project_filetype, NULL);
356 g_ptr_array_free(app->project->build_filetypes_list, FALSE);
359 /* remove project non filetype build menu items */
360 build_remove_menu_item(GEANY_BCS_PROJ, GEANY_GBG_NON_FT, -1);
361 build_remove_menu_item(GEANY_BCS_PROJ, GEANY_GBG_EXEC, -1);
363 g_free(app->project->name);
364 g_free(app->project->description);
365 g_free(app->project->file_name);
366 g_free(app->project->base_path);
368 g_free(app->project);
369 app->project = NULL;
371 apply_editor_prefs(); /* ensure that global settings are restored */
373 if (project_prefs.project_session)
375 /* close all existing tabs first */
376 document_close_all();
378 /* after closing all tabs let's open the tabs found in the default config */
379 if (open_default && cl_options.load_session)
381 configuration_reload_default_session();
382 configuration_open_files();
383 /* open a new file if no other file was opened */
384 document_new_file_if_non_open();
385 ui_focus_current_document();
388 g_signal_emit_by_name(geany_object, "project-close");
390 update_ui();
394 static gint build_page_num = 0;
397 static void create_properties_dialog(PropertyDialogElements *e)
399 GtkWidget *table, *notebook, *build_table;
400 GtkWidget *bbox;
401 GtkWidget *label;
402 GtkWidget *swin;
403 GeanyDocument *doc = document_get_current();
404 GeanyFiletype *ft = NULL;
406 e->dialog = create_project_dialog();
407 gtk_window_set_transient_for(GTK_WINDOW(e->dialog), GTK_WINDOW(main_widgets.window));
408 gtk_window_set_destroy_with_parent(GTK_WINDOW(e->dialog), TRUE);
409 gtk_widget_set_name(e->dialog, "GeanyDialogProject");
411 ui_entry_add_clear_icon(GTK_ENTRY(ui_lookup_widget(e->dialog, "spin_indent_width")));
413 table = gtk_table_new(5, 2, FALSE);
414 gtk_container_set_border_width(GTK_CONTAINER(table), 6);
415 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
416 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
418 label = gtk_label_new(_("Filename:"));
419 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
420 (GtkAttachOptions) (GTK_FILL),
421 (GtkAttachOptions) (0), 0, 0);
422 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
424 e->file_name = gtk_label_new("");
425 gtk_label_set_selectable(GTK_LABEL(e->file_name), TRUE);
426 gtk_table_attach(GTK_TABLE(table), e->file_name, 1, 2, 0, 1,
427 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
428 (GtkAttachOptions) (0), 0, 0);
429 gtk_misc_set_alignment(GTK_MISC(e->file_name), 0, 0);
431 label = gtk_label_new(_("Name:"));
432 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
433 (GtkAttachOptions) (GTK_FILL),
434 (GtkAttachOptions) (0), 0, 0);
435 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
437 e->name = gtk_entry_new();
438 ui_entry_add_clear_icon(GTK_ENTRY(e->name));
439 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
440 gtk_table_attach(GTK_TABLE(table), e->name, 1, 2, 1, 2,
441 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
442 (GtkAttachOptions) (0), 0, 0);
444 label = gtk_label_new(_("Description:"));
445 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
446 (GtkAttachOptions) (GTK_FILL),
447 (GtkAttachOptions) (GTK_FILL), 0, 0);
448 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
450 e->description = gtk_text_view_new();
451 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(e->description), GTK_WRAP_WORD);
452 swin = gtk_scrolled_window_new(NULL, NULL);
453 gtk_widget_set_size_request(swin, 250, 80);
454 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
455 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
456 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(swin), GTK_WIDGET(e->description));
457 gtk_table_attach(GTK_TABLE(table), swin, 1, 2, 2, 3,
458 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
459 (GtkAttachOptions) (0), 0, 0);
461 label = gtk_label_new(_("Base path:"));
462 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
463 (GtkAttachOptions) (GTK_FILL),
464 (GtkAttachOptions) (0), 0, 0);
465 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
467 e->base_path = gtk_entry_new();
468 ui_entry_add_clear_icon(GTK_ENTRY(e->base_path));
469 gtk_widget_set_tooltip_text(e->base_path,
470 _("Base directory of all files that make up the project. "
471 "This can be a new path, or an existing directory tree. "
472 "You can use paths relative to the project filename."));
473 bbox = ui_path_box_new(_("Choose Project Base Path"),
474 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(e->base_path));
475 gtk_table_attach(GTK_TABLE(table), bbox, 1, 2, 3, 4,
476 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
477 (GtkAttachOptions) (0), 0, 0);
479 if (doc != NULL) ft = doc->file_type;
480 build_table = build_commands_table(doc, GEANY_BCS_PROJ, &(e->build_properties), ft);
481 gtk_container_set_border_width(GTK_CONTAINER(build_table), 6);
482 label = gtk_label_new(_("Build"));
483 notebook = ui_lookup_widget(e->dialog, "project_notebook");
484 build_page_num = gtk_notebook_insert_page(GTK_NOTEBOOK(notebook), build_table, label, 2);
485 e->notebook = notebook;
487 g_signal_connect(ui_lookup_widget(e->dialog, "radio_long_line_custom"), "toggled",
488 G_CALLBACK(on_radio_long_line_custom_toggled), ui_lookup_widget(e->dialog, "spin_long_line"));
490 label = gtk_label_new(_("File patterns:"));
491 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 4, 5,
492 (GtkAttachOptions) (GTK_FILL),
493 (GtkAttachOptions) (0), 0, 0);
494 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
496 e->patterns = gtk_entry_new();
497 gtk_widget_set_tooltip_text(e->patterns,
498 _("Space separated list of file patterns used for the find in files dialog "
499 "(e.g. *.c *.h)"));
500 gtk_table_attach(GTK_TABLE(table), e->patterns, 1, 2, 4, 5,
501 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
502 (GtkAttachOptions) (0), 0, 0);
504 label = gtk_label_new(_("Project"));
505 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook), table, label, 0);
506 build_page_num++;
510 static void show_project_properties(gboolean show_build)
512 PropertyDialogElements *e = g_new(PropertyDialogElements, 1);
513 GeanyProject *p = app->project;
514 GtkWidget *widget = NULL;
515 GtkWidget *radio_long_line_custom;
517 g_return_if_fail(app->project != NULL);
519 entries_modified = FALSE;
521 create_properties_dialog(e);
523 stash_group_display(indent_group, e->dialog);
525 /* fill the elements with the appropriate data */
526 gtk_entry_set_text(GTK_ENTRY(e->name), p->name);
527 gtk_label_set_text(GTK_LABEL(e->file_name), p->file_name);
528 gtk_entry_set_text(GTK_ENTRY(e->base_path), p->base_path);
530 radio_long_line_custom = ui_lookup_widget(e->dialog, "radio_long_line_custom");
531 switch (p->long_line_behaviour)
533 case 0: widget = ui_lookup_widget(e->dialog, "radio_long_line_disabled"); break;
534 case 1: widget = ui_lookup_widget(e->dialog, "radio_long_line_default"); break;
535 case 2: widget = radio_long_line_custom; break;
537 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
539 widget = ui_lookup_widget(e->dialog, "spin_long_line");
540 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), (gdouble)p->long_line_column);
541 on_radio_long_line_custom_toggled(GTK_TOGGLE_BUTTON(radio_long_line_custom), widget);
543 if (p->description != NULL)
544 { /* set text */
545 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->description));
546 gtk_text_buffer_set_text(buffer, p->description, -1);
549 if (p->file_patterns != NULL)
550 { /* set the file patterns */
551 gchar *str;
553 str = g_strjoinv(" ", p->file_patterns);
554 gtk_entry_set_text(GTK_ENTRY(e->patterns), str);
555 g_free(str);
558 g_signal_emit_by_name(geany_object, "project-dialog-create", e->notebook);
559 gtk_widget_show_all(e->dialog);
561 /* note: notebook page must be shown before setting current page */
562 if (show_build)
563 gtk_notebook_set_current_page(GTK_NOTEBOOK(e->notebook), build_page_num);
564 else
565 gtk_notebook_set_current_page(GTK_NOTEBOOK(e->notebook), 0);
567 while (gtk_dialog_run(GTK_DIALOG(e->dialog)) == GTK_RESPONSE_OK)
569 if (update_config(e, FALSE))
571 g_signal_emit_by_name(geany_object, "project-dialog-confirmed", e->notebook);
572 if (!write_config(TRUE))
573 SHOW_ERR(_("Project file could not be written"));
574 else
576 ui_set_statusbar(TRUE, _("Project \"%s\" saved."), app->project->name);
577 break;
581 build_free_fields(e->build_properties);
582 gtk_widget_destroy(e->dialog);
583 g_free(e);
587 void project_properties(void)
589 show_project_properties(FALSE);
593 void project_build_properties(void)
595 show_project_properties(TRUE);
599 /* checks whether there is an already open project and asks the user if he wants to close it or
600 * abort the current action. Returns FALSE when the current action(the caller) should be cancelled
601 * and TRUE if we can go ahead */
602 gboolean project_ask_close(void)
604 if (app->project != NULL)
606 if (dialogs_show_question_full(NULL, GTK_STOCK_CLOSE, GTK_STOCK_CANCEL,
607 _("Do you want to close it before proceeding?"),
608 _("The '%s' project is already open."), app->project->name))
610 project_close(FALSE);
611 return TRUE;
613 else
614 return FALSE;
616 else
617 return TRUE;
621 static GeanyProject *create_project(void)
623 GeanyProject *project = g_new0(GeanyProject, 1);
625 memset(&priv, 0, sizeof priv);
626 indentation = *editor_get_indent_prefs(NULL);
627 priv.indentation = &indentation;
628 project->priv = &priv;
630 project->file_patterns = NULL;
632 project->long_line_behaviour = 1 /* use global settings */;
633 project->long_line_column = editor_prefs.long_line_column;
635 app->project = project;
636 return project;
640 /* Verifies data for New & Properties dialogs.
641 * Returns: FALSE if the user needs to change any data. */
642 static gboolean update_config(const PropertyDialogElements *e, gboolean new_project)
644 const gchar *name, *file_name, *base_path;
645 gchar *locale_filename;
646 gint name_len;
647 gint err_code = 0;
648 GeanyProject *p;
650 g_return_val_if_fail(e != NULL, TRUE);
652 name = gtk_entry_get_text(GTK_ENTRY(e->name));
653 name_len = strlen(name);
654 if (name_len == 0)
656 SHOW_ERR(_("The specified project name is too short."));
657 gtk_widget_grab_focus(e->name);
658 return FALSE;
660 else if (name_len > MAX_NAME_LEN)
662 SHOW_ERR1(_("The specified project name is too long (max. %d characters)."), MAX_NAME_LEN);
663 gtk_widget_grab_focus(e->name);
664 return FALSE;
667 if (new_project)
668 file_name = gtk_entry_get_text(GTK_ENTRY(e->file_name));
669 else
670 file_name = gtk_label_get_text(GTK_LABEL(e->file_name));
672 if (G_UNLIKELY(! NZV(file_name)))
674 SHOW_ERR(_("You have specified an invalid project filename."));
675 gtk_widget_grab_focus(e->file_name);
676 return FALSE;
679 locale_filename = utils_get_locale_from_utf8(file_name);
680 base_path = gtk_entry_get_text(GTK_ENTRY(e->base_path));
681 if (NZV(base_path))
682 { /* check whether the given directory actually exists */
683 gchar *locale_path = utils_get_locale_from_utf8(base_path);
685 if (! g_path_is_absolute(locale_path))
686 { /* relative base path, so add base dir of project file name */
687 gchar *dir = g_path_get_dirname(locale_filename);
688 setptr(locale_path, g_strconcat(dir, G_DIR_SEPARATOR_S, locale_path, NULL));
689 g_free(dir);
692 if (! g_file_test(locale_path, G_FILE_TEST_IS_DIR))
694 gboolean create_dir;
696 create_dir = dialogs_show_question_full(NULL, GTK_STOCK_OK, GTK_STOCK_CANCEL,
697 _("Create the project's base path directory?"),
698 _("The path \"%s\" does not exist."),
699 base_path);
701 if (create_dir)
702 err_code = utils_mkdir(locale_path, TRUE);
704 if (! create_dir || err_code != 0)
706 if (err_code != 0)
707 SHOW_ERR1(_("Project base directory could not be created (%s)."),
708 g_strerror(err_code));
709 gtk_widget_grab_focus(e->base_path);
710 utils_free_pointers(2, locale_path, locale_filename, NULL);
711 return FALSE;
714 g_free(locale_path);
716 /* finally test whether the given project file can be written */
717 if ((err_code = utils_is_file_writable(locale_filename)) != 0 ||
718 (err_code = g_file_test(locale_filename, G_FILE_TEST_IS_DIR) ? EISDIR : 0) != 0)
720 SHOW_ERR1(_("Project file could not be written (%s)."), g_strerror(err_code));
721 gtk_widget_grab_focus(e->file_name);
722 g_free(locale_filename);
723 return FALSE;
725 g_free(locale_filename);
727 if (app->project == NULL)
729 create_project();
730 new_project = TRUE;
732 p = app->project;
734 setptr(p->name, g_strdup(name));
735 setptr(p->file_name, g_strdup(file_name));
736 /* use "." if base_path is empty */
737 setptr(p->base_path, g_strdup(NZV(base_path) ? base_path : "./"));
739 if (! new_project) /* save properties specific fields */
741 GtkTextIter start, end;
742 GtkTextBuffer *buffer;
743 GeanyDocument *doc = document_get_current();
744 GeanyBuildCommand *oldvalue;
745 GeanyFiletype *ft = doc ? doc->file_type : NULL;
746 GtkWidget *widget;
747 gchar *tmp;
748 GString *str;
750 /* get and set the project description */
751 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->description));
752 gtk_text_buffer_get_start_iter(buffer, &start);
753 gtk_text_buffer_get_end_iter(buffer, &end);
754 setptr(p->description, g_strdup(gtk_text_buffer_get_text(buffer, &start, &end, FALSE)));
756 stash_group_update(indent_group, e->dialog);
758 /* read the project build menu */
759 oldvalue = ft ? ft->projfilecmds : NULL;
760 build_read_project(ft, e->build_properties);
762 if (ft != NULL && ft->projfilecmds != oldvalue && ft->project_list_entry < 0)
764 if (p->build_filetypes_list == NULL)
765 p->build_filetypes_list = g_ptr_array_new();
766 ft->project_list_entry = p->build_filetypes_list->len;
767 g_ptr_array_add(p->build_filetypes_list, ft);
769 build_menu_update(doc);
771 widget = ui_lookup_widget(e->dialog, "radio_long_line_disabled");
772 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
773 p->long_line_behaviour = 0;
774 else
776 widget = ui_lookup_widget(e->dialog, "radio_long_line_default");
777 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
778 p->long_line_behaviour = 1;
779 else
780 /* "Custom" radio button must be checked */
781 p->long_line_behaviour = 2;
784 widget = ui_lookup_widget(e->dialog, "spin_long_line");
785 p->long_line_column = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
786 apply_editor_prefs();
788 /* get and set the project file patterns */
789 tmp = g_strdup(gtk_entry_get_text(GTK_ENTRY(e->patterns)));
790 g_strfreev(p->file_patterns);
791 g_strstrip(tmp);
792 str = g_string_new(tmp);
793 do {} while (utils_string_replace_all(str, " ", " "));
794 p->file_patterns = g_strsplit(str->str, " ", -1);
795 g_string_free(str, TRUE);
796 g_free(tmp);
799 update_ui();
801 return TRUE;
805 #ifndef G_OS_WIN32
806 static void run_dialog(GtkWidget *dialog, GtkWidget *entry)
808 /* set filename in the file chooser dialog */
809 const gchar *utf8_filename = gtk_entry_get_text(GTK_ENTRY(entry));
810 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
812 if (g_path_is_absolute(locale_filename))
814 if (g_file_test(locale_filename, G_FILE_TEST_EXISTS))
816 /* if the current filename is a directory, we must use
817 * gtk_file_chooser_set_current_folder(which expects a locale filename) otherwise
818 * we end up in the parent directory */
819 if (g_file_test(locale_filename, G_FILE_TEST_IS_DIR))
820 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_filename);
821 else
822 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), utf8_filename);
824 else /* if the file doesn't yet exist, use at least the current directory */
826 gchar *locale_dir = g_path_get_dirname(locale_filename);
827 gchar *name = g_path_get_basename(utf8_filename);
829 if (g_file_test(locale_dir, G_FILE_TEST_EXISTS))
830 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dir);
831 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), name);
833 g_free(name);
834 g_free(locale_dir);
837 else if (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog)) != GTK_FILE_CHOOSER_ACTION_OPEN)
839 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), utf8_filename);
841 g_free(locale_filename);
843 /* run it */
844 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
846 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
847 gchar *tmp_utf8_filename = utils_get_utf8_from_locale(filename);
849 gtk_entry_set_text(GTK_ENTRY(entry), tmp_utf8_filename);
851 g_free(tmp_utf8_filename);
852 g_free(filename);
854 gtk_widget_destroy(dialog);
856 #endif
859 static void on_file_save_button_clicked(GtkButton *button, PropertyDialogElements *e)
861 #ifdef G_OS_WIN32
862 gchar *path = win32_show_project_open_dialog(e->dialog, _("Choose Project Filename"),
863 gtk_entry_get_text(GTK_ENTRY(e->file_name)), TRUE, TRUE);
864 if (path != NULL)
866 gtk_entry_set_text(GTK_ENTRY(e->file_name), path);
867 g_free(path);
869 #else
870 GtkWidget *dialog;
872 /* initialise the dialog */
873 dialog = gtk_file_chooser_dialog_new(_("Choose Project Filename"), NULL,
874 GTK_FILE_CHOOSER_ACTION_SAVE,
875 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
876 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
877 gtk_widget_set_name(dialog, "GeanyDialogProject");
878 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
879 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
880 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
881 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
883 run_dialog(dialog, e->file_name);
884 #endif
888 /* sets the project base path and the project file name according to the project name */
889 static void on_name_entry_changed(GtkEditable *editable, PropertyDialogElements *e)
891 gchar *base_path;
892 gchar *file_name;
893 gchar *name;
894 const gchar *project_dir = local_prefs.project_file_path;
896 if (entries_modified)
897 return;
899 name = gtk_editable_get_chars(editable, 0, -1);
900 if (NZV(name))
902 base_path = g_strconcat(project_dir, G_DIR_SEPARATOR_S,
903 name, G_DIR_SEPARATOR_S, NULL);
904 if (project_prefs.project_file_in_basedir)
905 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S, name, G_DIR_SEPARATOR_S,
906 name, "." GEANY_PROJECT_EXT, NULL);
907 else
908 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S,
909 name, "." GEANY_PROJECT_EXT, NULL);
911 else
913 base_path = g_strconcat(project_dir, G_DIR_SEPARATOR_S, NULL);
914 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S, NULL);
916 g_free(name);
918 gtk_entry_set_text(GTK_ENTRY(e->base_path), base_path);
919 gtk_entry_set_text(GTK_ENTRY(e->file_name), file_name);
921 entries_modified = FALSE;
923 g_free(base_path);
924 g_free(file_name);
928 static void on_entries_changed(GtkEditable *editable, PropertyDialogElements *e)
930 entries_modified = TRUE;
934 static void on_radio_long_line_custom_toggled(GtkToggleButton *radio, GtkWidget *spin_long_line)
936 gtk_widget_set_sensitive(spin_long_line, gtk_toggle_button_get_active(radio));
940 gboolean project_load_file(const gchar *locale_file_name)
942 g_return_val_if_fail(locale_file_name != NULL, FALSE);
944 if (load_config(locale_file_name))
946 gchar *utf8_filename = utils_get_utf8_from_locale(locale_file_name);
948 ui_set_statusbar(TRUE, _("Project \"%s\" opened."), app->project->name);
950 ui_add_recent_project_file(utf8_filename);
951 g_free(utf8_filename);
952 return TRUE;
954 else
956 gchar *utf8_filename = utils_get_utf8_from_locale(locale_file_name);
958 ui_set_statusbar(TRUE, _("Project file \"%s\" could not be loaded."), utf8_filename);
959 g_free(utf8_filename);
961 return FALSE;
965 /* Reads the given filename and creates a new project with the data found in the file.
966 * At this point there should not be an already opened project in Geany otherwise it will just
967 * return.
968 * The filename is expected in the locale encoding. */
969 static gboolean load_config(const gchar *filename)
971 GKeyFile *config;
972 GeanyProject *p;
974 /* there should not be an open project */
975 g_return_val_if_fail(app->project == NULL && filename != NULL, FALSE);
977 config = g_key_file_new();
978 if (! g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL))
980 g_key_file_free(config);
981 return FALSE;
984 p = create_project();
986 stash_group_load_from_key_file(indent_group, config);
988 p->name = utils_get_setting_string(config, "project", "name", GEANY_STRING_UNTITLED);
989 p->description = utils_get_setting_string(config, "project", "description", "");
990 p->file_name = utils_get_utf8_from_locale(filename);
991 p->base_path = utils_get_setting_string(config, "project", "base_path", "");
992 p->file_patterns = g_key_file_get_string_list(config, "project", "file_patterns", NULL, NULL);
994 p->long_line_behaviour = utils_get_setting_integer(config, "long line marker",
995 "long_line_behaviour", 1 /* follow global */);
996 p->long_line_column = utils_get_setting_integer(config, "long line marker",
997 "long_line_column", editor_prefs.long_line_column);
998 apply_editor_prefs();
1000 build_load_menu(config, GEANY_BCS_PROJ, (gpointer)p);
1001 if (project_prefs.project_session)
1003 /* save current (non-project) session (it could has been changed since program startup) */
1004 configuration_save_default_session();
1005 /* now close all open files */
1006 document_close_all();
1007 /* read session files so they can be opened with configuration_open_files() */
1008 configuration_load_session_files(config, FALSE);
1009 ui_focus_current_document();
1011 g_signal_emit_by_name(geany_object, "project-open", config);
1012 g_key_file_free(config);
1014 update_ui();
1015 return TRUE;
1019 static void apply_editor_prefs(void)
1021 guint i;
1023 foreach_document(i)
1024 editor_apply_update_prefs(documents[i]->editor);
1028 /* Write the project settings as well as the project session files into its configuration files.
1029 * emit_signal defines whether the project-save signal should be emitted. When write_config()
1030 * is called while closing a project, this is used to skip emitting the signal because
1031 * project-close will be emitted afterwards.
1032 * Returns: TRUE if project file was written successfully. */
1033 static gboolean write_config(gboolean emit_signal)
1035 GeanyProject *p;
1036 GKeyFile *config;
1037 gchar *filename;
1038 gchar *data;
1039 gboolean ret = FALSE;
1041 g_return_val_if_fail(app->project != NULL, FALSE);
1043 p = app->project;
1045 config = g_key_file_new();
1046 /* try to load an existing config to keep manually added comments */
1047 filename = utils_get_locale_from_utf8(p->file_name);
1048 g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL);
1050 stash_group_save_to_key_file(indent_group, config);
1052 g_key_file_set_string(config, "project", "name", p->name);
1053 g_key_file_set_string(config, "project", "base_path", p->base_path);
1055 if (p->description)
1056 g_key_file_set_string(config, "project", "description", p->description);
1057 if (p->file_patterns)
1058 g_key_file_set_string_list(config, "project", "file_patterns",
1059 (const gchar**) p->file_patterns, g_strv_length(p->file_patterns));
1061 g_key_file_set_integer(config, "long line marker", "long_line_behaviour", p->long_line_behaviour);
1062 g_key_file_set_integer(config, "long line marker", "long_line_column", p->long_line_column);
1064 /* store the session files into the project too */
1065 if (project_prefs.project_session)
1066 configuration_save_session_files(config);
1067 build_save_menu(config, (gpointer)p, GEANY_BCS_PROJ);
1068 if (emit_signal)
1070 g_signal_emit_by_name(geany_object, "project-save", config);
1072 /* write the file */
1073 data = g_key_file_to_data(config, NULL, NULL);
1074 ret = (utils_write_file(filename, data) == 0);
1076 g_free(data);
1077 g_free(filename);
1078 g_key_file_free(config);
1080 return ret;
1084 /* Constructs the project's base path which is used for "Make all" and "Execute".
1085 * The result is an absolute string in UTF-8 encoding which is either the same as
1086 * base path if it is absolute or it is built out of project file name's dir and base_path.
1087 * If there is no project or project's base_path is invalid, NULL will be returned.
1088 * The returned string should be freed when no longer needed. */
1089 gchar *project_get_base_path(void)
1091 GeanyProject *project = app->project;
1093 if (project && NZV(project->base_path))
1095 if (g_path_is_absolute(project->base_path))
1096 return g_strdup(project->base_path);
1097 else
1098 { /* build base_path out of project file name's dir and base_path */
1099 gchar *path;
1100 gchar *dir = g_path_get_dirname(project->file_name);
1102 if (utils_str_equal(project->base_path, "./"))
1103 return dir;
1104 else
1105 path = g_strconcat(dir, G_DIR_SEPARATOR_S, project->base_path, NULL);
1106 g_free(dir);
1107 return path;
1110 return NULL;
1114 /* This is to save project-related global settings, NOT project file settings. */
1115 void project_save_prefs(GKeyFile *config)
1117 GeanyProject *project = app->project;
1119 if (cl_options.load_session)
1121 const gchar *utf8_filename = (project == NULL) ? "" : project->file_name;
1123 g_key_file_set_string(config, "project", "session_file", utf8_filename);
1125 g_key_file_set_string(config, "project", "project_file_path",
1126 NVL(local_prefs.project_file_path, ""));
1130 void project_load_prefs(GKeyFile *config)
1132 if (cl_options.load_session)
1134 g_return_if_fail(project_prefs.session_file == NULL);
1135 project_prefs.session_file = utils_get_setting_string(config, "project",
1136 "session_file", "");
1138 local_prefs.project_file_path = utils_get_setting_string(config, "project",
1139 "project_file_path", NULL);
1140 if (local_prefs.project_file_path == NULL)
1142 local_prefs.project_file_path = g_strconcat(g_get_home_dir(),
1143 G_DIR_SEPARATOR_S, PROJECT_DIR, NULL);
1148 /* Initialize project-related preferences in the Preferences dialog. */
1149 void project_setup_prefs(void)
1151 GtkWidget *path_entry = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_entry");
1152 GtkWidget *path_btn = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_button");
1153 static gboolean callback_setup = FALSE;
1155 g_return_if_fail(local_prefs.project_file_path != NULL);
1157 gtk_entry_set_text(GTK_ENTRY(path_entry), local_prefs.project_file_path);
1158 if (! callback_setup)
1159 { /* connect the callback only once */
1160 callback_setup = TRUE;
1161 ui_setup_open_button_callback(path_btn, NULL,
1162 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(path_entry));
1167 /* Update project-related preferences after using the Preferences dialog. */
1168 void project_apply_prefs(void)
1170 GtkWidget *path_entry = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_entry");
1171 const gchar *str;
1173 str = gtk_entry_get_text(GTK_ENTRY(path_entry));
1174 setptr(local_prefs.project_file_path, g_strdup(str));
1178 void project_init(void)
1180 StashGroup *group;
1182 group = stash_group_new("indentation");
1183 /* defaults are copied from editor indent prefs */
1184 stash_group_set_use_defaults(group, FALSE);
1185 indent_group = group;
1187 stash_group_add_spin_button_integer(group, &indentation.width,
1188 "indent_width", 4, "spin_indent_width");
1189 stash_group_add_radio_buttons(group, (gint*)(gpointer)&indentation.type,
1190 "indent_type", GEANY_INDENT_TYPE_TABS,
1191 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
1192 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
1193 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
1194 NULL);
1195 /* This is a 'hidden' pref for backwards-compatibility */
1196 stash_group_add_integer(group, &indentation.hard_tab_width,
1197 "indent_hard_tab_width", 8);
1198 stash_group_add_toggle_button(group, &indentation.detect_type,
1199 "detect_indent", FALSE, "check_detect_indent_type");
1200 stash_group_add_toggle_button(group, &indentation.detect_width,
1201 "detect_indent_width", FALSE, "check_detect_indent_width");
1202 stash_group_add_combo_box(group, (gint*)(gpointer)&indentation.auto_indent_mode,
1203 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
1207 void project_finalize(void)
1209 stash_group_free(indent_group);