Cache G_TYPE_INSTANCE_GET_PRIVATE() result when initializing an
[geany-mirror.git] / src / project.c
bloba9efe0b4eefca9e8e55577b7f3ba3ca9b1d269c3
1 /*
2 * project.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2010 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.
21 * $Id$
24 /** @file project.h
25 * Project Management.
28 #include "geany.h"
30 #include <string.h>
31 #include <unistd.h>
33 #include "project.h"
34 #include "projectprivate.h"
36 #include "dialogs.h"
37 #include "support.h"
38 #include "utils.h"
39 #include "ui_utils.h"
40 #include "document.h"
41 #include "msgwindow.h"
42 #include "main.h"
43 #include "keyfile.h"
44 #ifdef G_OS_WIN32
45 # include "win32.h"
46 #endif
47 #include "build.h"
48 #include "interface.h"
49 #include "editor.h"
50 #include "stash.h"
51 #include "sidebar.h"
52 #include "filetypes.h"
55 ProjectPrefs project_prefs = { NULL, FALSE, FALSE };
58 static GeanyProjectPrivate priv;
59 static GeanyIndentPrefs indentation;
61 static StashGroup *indent_group = NULL;
63 static struct
65 gchar *project_file_path; /* in UTF-8 */
66 } local_prefs = { NULL };
69 static gboolean entries_modified;
71 /* simple struct to keep references to the elements of the properties dialog */
72 typedef struct _PropertyDialogElements
74 GtkWidget *dialog;
75 GtkWidget *name;
76 GtkWidget *description;
77 GtkWidget *file_name;
78 GtkWidget *base_path;
79 GtkWidget *patterns;
80 TableData build_properties;
81 } PropertyDialogElements;
85 static gboolean update_config(const PropertyDialogElements *e);
86 static void on_file_save_button_clicked(GtkButton *button, PropertyDialogElements *e);
87 static gboolean load_config(const gchar *filename);
88 static gboolean write_config(gboolean emit_signal);
89 static void on_name_entry_changed(GtkEditable *editable, PropertyDialogElements *e);
90 static void on_entries_changed(GtkEditable *editable, PropertyDialogElements *e);
91 static void on_radio_long_line_custom_toggled(GtkToggleButton *radio, GtkWidget *spin_long_line);
92 static void apply_editor_prefs();
95 #define SHOW_ERR(args) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args)
96 #define SHOW_ERR1(args, more) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args, more)
97 #define MAX_NAME_LEN 50
98 /* "projects" is part of the default project base path so be careful when translating
99 * please avoid special characters and spaces, look at the source for details or ask Frank */
100 #define PROJECT_DIR _("projects")
103 void project_new(void)
105 GtkWidget *vbox;
106 GtkWidget *table;
107 GtkWidget *image;
108 GtkWidget *button;
109 GtkWidget *bbox;
110 GtkWidget *label;
111 PropertyDialogElements *e;
113 if (! project_ask_close())
114 return;
116 g_return_if_fail(app->project == NULL);
118 e = g_new0(PropertyDialogElements, 1);
119 e->dialog = gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(main_widgets.window),
120 GTK_DIALOG_DESTROY_WITH_PARENT,
121 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
123 gtk_widget_set_name(e->dialog, "GeanyDialogProject");
124 bbox = gtk_hbox_new(FALSE, 0);
125 button = gtk_button_new();
126 image = gtk_image_new_from_stock("gtk-new", GTK_ICON_SIZE_BUTTON);
127 label = gtk_label_new_with_mnemonic(_("C_reate"));
128 gtk_box_pack_start(GTK_BOX(bbox), image, FALSE, FALSE, 3);
129 gtk_box_pack_start(GTK_BOX(bbox), label, FALSE, FALSE, 3);
130 gtk_container_add(GTK_CONTAINER(button), bbox);
131 gtk_dialog_add_action_widget(GTK_DIALOG(e->dialog), button, GTK_RESPONSE_OK);
133 vbox = ui_dialog_vbox_new(GTK_DIALOG(e->dialog));
135 entries_modified = FALSE;
137 table = gtk_table_new(3, 2, FALSE);
138 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
139 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
141 label = gtk_label_new(_("Name:"));
142 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
144 e->name = gtk_entry_new();
145 ui_entry_add_clear_icon(GTK_ENTRY(e->name));
146 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
148 ui_table_add_row(GTK_TABLE(table), 0, label, e->name, NULL);
150 label = gtk_label_new(_("Filename:"));
151 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
153 e->file_name = gtk_entry_new();
154 ui_entry_add_clear_icon(GTK_ENTRY(e->file_name));
155 gtk_entry_set_width_chars(GTK_ENTRY(e->file_name), 30);
156 button = gtk_button_new();
157 g_signal_connect(button, "clicked", G_CALLBACK(on_file_save_button_clicked), e);
158 image = gtk_image_new_from_stock("gtk-open", GTK_ICON_SIZE_BUTTON);
159 gtk_container_add(GTK_CONTAINER(button), image);
160 bbox = gtk_hbox_new(FALSE, 6);
161 gtk_box_pack_start_defaults(GTK_BOX(bbox), e->file_name);
162 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
164 ui_table_add_row(GTK_TABLE(table), 1, label, bbox, NULL);
166 label = gtk_label_new(_("Base path:"));
167 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
169 e->base_path = gtk_entry_new();
170 ui_entry_add_clear_icon(GTK_ENTRY(e->base_path));
171 ui_widget_set_tooltip_text(e->base_path,
172 _("Base directory of all files that make up the project. "
173 "This can be a new path, or an existing directory tree. "
174 "You can use paths relative to the project filename."));
175 bbox = ui_path_box_new(_("Choose Project Base Path"),
176 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(e->base_path));
178 ui_table_add_row(GTK_TABLE(table), 2, label, bbox, NULL);
180 gtk_container_add(GTK_CONTAINER(vbox), table);
182 /* signals */
183 g_signal_connect(e->name, "changed", G_CALLBACK(on_name_entry_changed), e);
184 /* run the callback manually to initialise the base_path and file_name fields */
185 on_name_entry_changed(GTK_EDITABLE(e->name), e);
187 g_signal_connect(e->file_name, "changed", G_CALLBACK(on_entries_changed), e);
188 g_signal_connect(e->base_path, "changed", G_CALLBACK(on_entries_changed), e);
190 gtk_widget_show_all(e->dialog);
192 while (gtk_dialog_run(GTK_DIALOG(e->dialog)) == GTK_RESPONSE_OK)
194 if (update_config(e))
196 ui_add_recent_project_file(app->project->file_name);
197 break;
200 gtk_widget_destroy(e->dialog);
201 g_free(e);
205 gboolean project_load_file_with_session(const gchar *locale_file_name)
207 if (project_load_file(locale_file_name))
209 if (project_prefs.project_session)
211 configuration_open_files();
212 /* open a new file if no other file was opened */
213 document_new_file_if_non_open();
215 return TRUE;
217 return FALSE;
221 #ifndef G_OS_WIN32
222 static void run_open_dialog(GtkDialog *dialog)
224 while (gtk_dialog_run(dialog) == GTK_RESPONSE_ACCEPT)
226 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
228 /* try to load the config */
229 if (! project_load_file_with_session(filename))
231 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
233 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), utf8_filename);
234 gtk_widget_grab_focus(GTK_WIDGET(dialog));
235 g_free(utf8_filename);
236 g_free(filename);
237 continue;
239 g_free(filename);
240 break;
243 #endif
246 void project_open(void)
248 const gchar *dir = local_prefs.project_file_path;
249 #ifdef G_OS_WIN32
250 gchar *file;
251 #else
252 GtkWidget *dialog;
253 GtkFileFilter *filter;
254 gchar *locale_path;
255 #endif
256 if (! project_ask_close()) return;
258 #ifdef G_OS_WIN32
259 file = win32_show_project_open_dialog(main_widgets.window, _("Open Project"), dir, FALSE, TRUE);
260 if (file != NULL)
262 /* try to load the config */
263 if (! project_load_file_with_session(file))
265 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), file);
267 g_free(file);
269 #else
271 dialog = gtk_file_chooser_dialog_new(_("Open Project"), GTK_WINDOW(main_widgets.window),
272 GTK_FILE_CHOOSER_ACTION_OPEN,
273 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
274 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
275 gtk_widget_set_name(dialog, "GeanyDialogProject");
277 /* set default Open, so pressing enter can open multiple files */
278 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
279 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
280 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
281 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
282 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
283 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
285 /* add FileFilters */
286 filter = gtk_file_filter_new();
287 gtk_file_filter_set_name(filter, _("All files"));
288 gtk_file_filter_add_pattern(filter, "*");
289 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
290 filter = gtk_file_filter_new();
291 gtk_file_filter_set_name(filter, _("Project files"));
292 gtk_file_filter_add_pattern(filter, "*." GEANY_PROJECT_EXT);
293 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
294 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
296 locale_path = utils_get_locale_from_utf8(dir);
297 if (g_file_test(locale_path, G_FILE_TEST_EXISTS) &&
298 g_file_test(locale_path, G_FILE_TEST_IS_DIR))
300 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
302 g_free(locale_path);
304 gtk_widget_show_all(dialog);
305 run_open_dialog(GTK_DIALOG(dialog));
306 gtk_widget_destroy(GTK_WIDGET(dialog));
307 #endif
311 /* Called when creating, opening, closing and updating projects. */
312 static void update_ui(void)
314 ui_set_window_title(NULL);
315 build_menu_update(NULL);
316 sidebar_openfiles_update_all();
320 static void remove_foreach_project_filetype(gpointer data, gpointer user_data)
322 GeanyFiletype *ft = (GeanyFiletype*)data;
323 if (ft != NULL)
325 setptr(ft->projfilecmds, NULL);
326 setptr(ft->projexeccmds, NULL);
327 setptr(ft->projerror_regex_string, NULL);
328 ft->project_list_entry = -1;
333 /* open_default will make function reload default session files on close */
334 void project_close(gboolean open_default)
336 g_return_if_fail(app->project != NULL);
338 ui_set_statusbar(TRUE, _("Project \"%s\" closed."), app->project->name);
340 /* use write_config() to save project session files */
341 write_config(FALSE);
343 /* remove project filetypes build entries */
344 if (app->project->build_filetypes_list != NULL)
346 g_ptr_array_foreach(app->project->build_filetypes_list, remove_foreach_project_filetype, NULL);
347 g_ptr_array_free(app->project->build_filetypes_list, FALSE);
350 /* remove project non filetype build menu items */
351 build_remove_menu_item(GEANY_BCS_PROJ, GEANY_GBG_NON_FT, -1);
352 build_remove_menu_item(GEANY_BCS_PROJ, GEANY_GBG_EXEC, -1);
354 /* remove project regexen */
355 setptr(regex_proj, NULL);
357 g_free(app->project->name);
358 g_free(app->project->description);
359 g_free(app->project->file_name);
360 g_free(app->project->base_path);
362 g_free(app->project);
363 app->project = NULL;
365 apply_editor_prefs(); /* ensure that global settings are restored */
367 if (project_prefs.project_session)
369 /* close all existing tabs first */
370 document_close_all();
372 /* after closing all tabs let's open the tabs found in the default config */
373 if (open_default && cl_options.load_session)
375 configuration_reload_default_session();
376 configuration_open_files();
377 /* open a new file if no other file was opened */
378 document_new_file_if_non_open();
381 g_signal_emit_by_name(geany_object, "project-close");
383 update_ui();
387 static void on_set_use_base_path_clicked(GtkWidget *unused1, gpointer user_data)
389 build_set_non_ft_wd_to_proj((TableData)user_data);
393 static void create_properties_dialog(PropertyDialogElements *e)
395 GtkWidget *table, *notebook, *build_table;
396 GtkWidget *button;
397 GtkWidget *bbox;
398 GtkWidget *label;
399 GtkWidget *swin;
400 GeanyDocument *doc = document_get_current();
401 GeanyFiletype *ft = NULL;
403 e->dialog = create_project_dialog();
404 gtk_window_set_transient_for(GTK_WINDOW(e->dialog), GTK_WINDOW(main_widgets.window));
405 gtk_window_set_destroy_with_parent(GTK_WINDOW(e->dialog), TRUE);
406 gtk_widget_set_name(e->dialog, "GeanyDialogProject");
408 ui_entry_add_clear_icon(GTK_ENTRY(ui_lookup_widget(e->dialog, "spin_indent_width")));
409 ui_entry_add_clear_icon(GTK_ENTRY(ui_lookup_widget(e->dialog, "spin_tab_width")));
411 table = gtk_table_new(6, 2, FALSE);
412 gtk_container_set_border_width(GTK_CONTAINER(table), 6);
413 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
414 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
416 label = gtk_label_new(_("Name:"));
417 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
418 (GtkAttachOptions) (GTK_FILL),
419 (GtkAttachOptions) (0), 0, 0);
420 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
422 e->name = gtk_entry_new();
423 ui_entry_add_clear_icon(GTK_ENTRY(e->name));
424 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
425 gtk_table_attach(GTK_TABLE(table), e->name, 1, 2, 0, 1,
426 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
427 (GtkAttachOptions) (0), 0, 0);
429 label = gtk_label_new(_("Filename:"));
430 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
431 (GtkAttachOptions) (GTK_FILL),
432 (GtkAttachOptions) (0), 0, 0);
433 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
435 e->file_name = gtk_entry_new();
436 ui_entry_add_clear_icon(GTK_ENTRY(e->file_name));
437 gtk_editable_set_editable(GTK_EDITABLE(e->file_name), FALSE); /* read-only */
438 gtk_table_attach(GTK_TABLE(table), e->file_name, 1, 2, 1, 2,
439 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
440 (GtkAttachOptions) (0), 0, 0);
442 label = gtk_label_new(_("Description:"));
443 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
444 (GtkAttachOptions) (GTK_FILL),
445 (GtkAttachOptions) (GTK_FILL), 0, 0);
446 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
448 e->description = gtk_text_view_new();
449 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(e->description), GTK_WRAP_WORD);
450 swin = gtk_scrolled_window_new(NULL, NULL);
451 gtk_widget_set_size_request(swin, 250, 80);
452 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
453 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
454 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(swin), GTK_WIDGET(e->description));
455 gtk_table_attach(GTK_TABLE(table), swin, 1, 2, 2, 3,
456 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
457 (GtkAttachOptions) (0), 0, 0);
459 label = gtk_label_new(_("Base path:"));
460 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
461 (GtkAttachOptions) (GTK_FILL),
462 (GtkAttachOptions) (0), 0, 0);
463 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
465 e->base_path = gtk_entry_new();
466 ui_entry_add_clear_icon(GTK_ENTRY(e->base_path));
467 ui_widget_set_tooltip_text(e->base_path,
468 _("Base directory of all files that make up the project. "
469 "This can be a new path, or an existing directory tree. "
470 "You can use paths relative to the project filename."));
471 bbox = ui_path_box_new(_("Choose Project Base Path"),
472 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(e->base_path));
473 gtk_table_attach(GTK_TABLE(table), bbox, 1, 2, 3, 4,
474 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
475 (GtkAttachOptions) (0), 0, 0);
477 if (doc != NULL) ft = doc->file_type;
478 build_table = build_commands_table(doc, GEANY_BCS_PROJ, &(e->build_properties), ft);
479 gtk_container_set_border_width(GTK_CONTAINER(build_table), 6);
480 label = gtk_label_new(_("Build"));
481 notebook = ui_lookup_widget(e->dialog, "project_notebook");
482 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook), build_table, label, 2);
484 label = gtk_label_new(_("Set the Build non-filetype working directories to use base path:"));
485 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
487 button = gtk_button_new_with_label(_("Set"));
488 ui_widget_set_tooltip_text(button,
489 _("Set the working directories (on the Build tab) "
490 "for the non-filetype build commands to use the base path"));
491 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
492 g_signal_connect(button, "clicked", G_CALLBACK(on_set_use_base_path_clicked), e->build_properties);
493 bbox = gtk_hbox_new(FALSE, 6);
494 gtk_box_pack_start(GTK_BOX(bbox), label, TRUE, TRUE, 0);
495 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
496 gtk_table_attach(GTK_TABLE(table), bbox, 0, 2, 4, 5,
497 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
498 (GtkAttachOptions) (GTK_FILL), 0, 0);
500 g_signal_connect(ui_lookup_widget(e->dialog, "radio_long_line_custom"), "toggled",
501 G_CALLBACK(on_radio_long_line_custom_toggled), ui_lookup_widget(e->dialog, "spin_long_line"));
503 #if 0
504 label = gtk_label_new(_("File patterns:"));
505 /* <small>Separate multiple patterns by a new line</small> */
506 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 6, 7,
507 (GtkAttachOptions) (GTK_FILL),
508 (GtkAttachOptions) (GTK_FILL), 0, 0);
509 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
511 e->patterns = gtk_text_view_new();
512 swin = gtk_scrolled_window_new(NULL, NULL);
513 gtk_widget_set_size_request(swin, -1, 80);
514 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
515 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
516 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(swin), GTK_WIDGET(e->patterns));
517 gtk_table_attach(GTK_TABLE(table), swin, 1, 2, 6, 7,
518 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
519 (GtkAttachOptions) (0), 0, 0);
520 #endif
522 label = gtk_label_new(_("Project"));
523 gtk_widget_show(table); /* needed to switch current page */
524 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook), table, label, 0);
525 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 0);
529 void project_properties(void)
531 PropertyDialogElements *e = g_new(PropertyDialogElements, 1);
532 GeanyProject *p = app->project;
533 GtkWidget *widget = NULL;
534 GtkWidget *radio_long_line_custom;
536 g_return_if_fail(app->project != NULL);
538 entries_modified = FALSE;
540 create_properties_dialog(e);
542 stash_group_display(indent_group, e->dialog);
544 /* fill the elements with the appropriate data */
545 gtk_entry_set_text(GTK_ENTRY(e->name), p->name);
546 gtk_entry_set_text(GTK_ENTRY(e->file_name), p->file_name);
547 gtk_entry_set_text(GTK_ENTRY(e->base_path), p->base_path);
549 radio_long_line_custom = ui_lookup_widget(e->dialog, "radio_long_line_custom");
550 switch (p->long_line_behaviour)
552 case 0: widget = ui_lookup_widget(e->dialog, "radio_long_line_disabled"); break;
553 case 1: widget = ui_lookup_widget(e->dialog, "radio_long_line_default"); break;
554 case 2: widget = radio_long_line_custom; break;
556 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
558 widget = ui_lookup_widget(e->dialog, "spin_long_line");
559 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), (gdouble)p->long_line_column);
560 on_radio_long_line_custom_toggled(GTK_TOGGLE_BUTTON(radio_long_line_custom), widget);
562 if (p->description != NULL)
563 { /* set text */
564 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->description));
565 gtk_text_buffer_set_text(buffer, p->description, -1);
568 #if 0
569 if (p->file_patterns != NULL)
570 { /* set the file patterns */
571 gint i;
572 gint len = g_strv_length(p->file_patterns);
573 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->patterns));
574 GString *str = g_string_sized_new(len * 4);
576 for (i = 0; i < len; i++)
578 if (p->file_patterns[i] != NULL)
580 g_string_append(str, p->file_patterns[i]);
581 g_string_append_c(str, '\n');
584 gtk_text_buffer_set_text(buffer, str->str, -1);
585 g_string_free(str, TRUE);
587 #endif
589 gtk_widget_show_all(e->dialog);
591 while (gtk_dialog_run(GTK_DIALOG(e->dialog)) == GTK_RESPONSE_OK)
593 if (update_config(e))
595 stash_group_update(indent_group, e->dialog);
596 break;
599 build_free_fields(e->build_properties);
600 gtk_widget_destroy(e->dialog);
601 g_free(e);
605 /* checks whether there is an already open project and asks the user if he wants to close it or
606 * abort the current action. Returns FALSE when the current action(the caller) should be cancelled
607 * and TRUE if we can go ahead */
608 gboolean project_ask_close(void)
610 if (app->project != NULL)
612 if (dialogs_show_question_full(NULL, GTK_STOCK_CLOSE, GTK_STOCK_CANCEL,
613 _("Do you want to close it before proceeding?"),
614 _("The '%s' project is already open."), app->project->name))
616 project_close(FALSE);
617 return TRUE;
619 else
620 return FALSE;
622 else
623 return TRUE;
627 static GeanyProject *create_project(void)
629 GeanyProject *project = g_new0(GeanyProject, 1);
631 memset(&priv, 0, sizeof priv);
632 indentation = *editor_get_indent_prefs(NULL);
633 priv.indentation = &indentation;
634 project->priv = &priv;
636 project->long_line_behaviour = 1 /* use global settings */;
637 project->long_line_column = editor_prefs.long_line_global_column;
639 app->project = project;
640 return project;
644 /* Verifies data for New & Properties dialogs.
645 * Returns: FALSE if the user needs to change any data. */
646 static gboolean update_config(const PropertyDialogElements *e)
648 const gchar *name, *file_name, *base_path;
649 gchar *locale_filename;
650 gint name_len;
651 gint err_code = 0;
652 gboolean new_project = FALSE;
653 GeanyProject *p;
655 g_return_val_if_fail(e != NULL, TRUE);
657 name = gtk_entry_get_text(GTK_ENTRY(e->name));
658 name_len = strlen(name);
659 if (name_len == 0)
661 SHOW_ERR(_("The specified project name is too short."));
662 gtk_widget_grab_focus(e->name);
663 return FALSE;
665 else if (name_len > MAX_NAME_LEN)
667 SHOW_ERR1(_("The specified project name is too long (max. %d characters)."), MAX_NAME_LEN);
668 gtk_widget_grab_focus(e->name);
669 return FALSE;
672 file_name = gtk_entry_get_text(GTK_ENTRY(e->file_name));
673 if (! NZV(file_name))
675 SHOW_ERR(_("You have specified an invalid project filename."));
676 gtk_widget_grab_focus(e->file_name);
677 return FALSE;
680 locale_filename = utils_get_locale_from_utf8(file_name);
681 base_path = gtk_entry_get_text(GTK_ENTRY(e->base_path));
682 if (NZV(base_path))
683 { /* check whether the given directory actually exists */
684 gchar *locale_path = utils_get_locale_from_utf8(base_path);
686 if (! g_path_is_absolute(locale_path))
687 { /* relative base path, so add base dir of project file name */
688 gchar *dir = g_path_get_dirname(locale_filename);
689 setptr(locale_path, g_strconcat(dir, G_DIR_SEPARATOR_S, locale_path, NULL));
690 g_free(dir);
693 if (! g_file_test(locale_path, G_FILE_TEST_IS_DIR))
695 gboolean create_dir;
697 create_dir = dialogs_show_question_full(NULL, GTK_STOCK_OK, GTK_STOCK_CANCEL,
698 _("Create the project's base path directory?"),
699 _("The path \"%s\" does not exist."),
700 base_path);
702 if (create_dir)
703 err_code = utils_mkdir(locale_path, TRUE);
705 if (! create_dir || err_code != 0)
707 if (err_code != 0)
708 SHOW_ERR1(_("Project base directory could not be created (%s)."),
709 g_strerror(err_code));
710 gtk_widget_grab_focus(e->base_path);
711 utils_free_pointers(2, locale_path, locale_filename, NULL);
712 return FALSE;
715 g_free(locale_path);
717 /* finally test whether the given project file can be written */
718 if ((err_code = utils_is_file_writeable(locale_filename)) != 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 BuildDestination menu_dst;
745 GeanyBuildCommand *oldvalue;
746 GeanyFiletype *ft = NULL;
747 GtkWidget *widget;
749 /* get and set the project description */
750 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->description));
751 gtk_text_buffer_get_start_iter(buffer, &start);
752 gtk_text_buffer_get_end_iter(buffer, &end);
753 setptr(p->description, g_strdup(gtk_text_buffer_get_text(buffer, &start, &end, FALSE)));
755 /* read the project build menu */
756 if (doc != NULL)
757 ft = doc->file_type;
758 if (ft != NULL)
760 menu_dst.dst[GEANY_GBG_FT] = &(ft->projfilecmds);
761 oldvalue = ft->projfilecmds;
762 menu_dst.fileregexstr = &(ft->projerror_regex_string);
764 else
766 menu_dst.dst[GEANY_GBG_FT] = NULL;
767 oldvalue = NULL;
768 menu_dst.fileregexstr = NULL;
770 menu_dst.dst[GEANY_GBG_NON_FT] = &non_ft_proj;
771 menu_dst.dst[GEANY_GBG_EXEC] = &exec_proj;
772 menu_dst.nonfileregexstr = &regex_proj;
773 build_read_commands(&menu_dst, e->build_properties, GTK_RESPONSE_ACCEPT);
774 if (ft != NULL && ft->projfilecmds != oldvalue && ft->project_list_entry < 0)
776 if (p->build_filetypes_list == NULL)
777 p->build_filetypes_list = g_ptr_array_new();
778 ft->project_list_entry = p->build_filetypes_list->len;
779 g_ptr_array_add(p->build_filetypes_list, ft);
781 build_menu_update(doc);
783 widget = ui_lookup_widget(e->dialog, "radio_long_line_disabled");
784 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
785 p->long_line_behaviour = 0;
786 else
788 widget = ui_lookup_widget(e->dialog, "radio_long_line_default");
789 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
790 p->long_line_behaviour = 1;
791 else
792 /* "Custom" radio button must be checked */
793 p->long_line_behaviour = 2;
796 widget = ui_lookup_widget(e->dialog, "spin_long_line");
797 p->long_line_column = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
798 apply_editor_prefs();
800 #if 0
801 /* get and set the project file patterns */
802 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->patterns));
803 gtk_text_buffer_get_start_iter(buffer, &start);
804 gtk_text_buffer_get_end_iter(buffer, &end);
805 tmp = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
806 g_strfreev(p->file_patterns);
807 p->file_patterns = g_strsplit(tmp, "\n", -1);
808 g_free(tmp);
809 #endif
811 write_config(TRUE);
812 if (new_project)
813 ui_set_statusbar(TRUE, _("Project \"%s\" created."), p->name);
814 else
815 ui_set_statusbar(TRUE, _("Project \"%s\" saved."), p->name);
817 update_ui();
819 return TRUE;
823 #ifndef G_OS_WIN32
824 static void run_dialog(GtkWidget *dialog, GtkWidget *entry)
826 /* set filename in the file chooser dialog */
827 const gchar *utf8_filename = gtk_entry_get_text(GTK_ENTRY(entry));
828 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
830 if (g_path_is_absolute(locale_filename))
832 if (g_file_test(locale_filename, G_FILE_TEST_EXISTS))
834 /* if the current filename is a directory, we must use
835 * gtk_file_chooser_set_current_folder(which expects a locale filename) otherwise
836 * we end up in the parent directory */
837 if (g_file_test(locale_filename, G_FILE_TEST_IS_DIR))
838 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_filename);
839 else
840 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), utf8_filename);
842 else /* if the file doesn't yet exist, use at least the current directory */
844 gchar *locale_dir = g_path_get_dirname(locale_filename);
845 gchar *name = g_path_get_basename(utf8_filename);
847 if (g_file_test(locale_dir, G_FILE_TEST_EXISTS))
848 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dir);
849 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), name);
851 g_free(name);
852 g_free(locale_dir);
855 else if (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog)) != GTK_FILE_CHOOSER_ACTION_OPEN)
857 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), utf8_filename);
859 g_free(locale_filename);
861 /* run it */
862 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
864 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
865 gchar *tmp_utf8_filename = utils_get_utf8_from_locale(filename);
867 gtk_entry_set_text(GTK_ENTRY(entry), tmp_utf8_filename);
869 g_free(tmp_utf8_filename);
870 g_free(filename);
872 gtk_widget_destroy(dialog);
874 #endif
877 static void on_file_save_button_clicked(GtkButton *button, PropertyDialogElements *e)
879 #ifdef G_OS_WIN32
880 gchar *path = win32_show_project_open_dialog(e->dialog, _("Choose Project Filename"),
881 gtk_entry_get_text(GTK_ENTRY(e->file_name)), TRUE, TRUE);
882 if (path != NULL)
884 gtk_entry_set_text(GTK_ENTRY(e->file_name), path);
885 g_free(path);
887 #else
888 GtkWidget *dialog;
890 /* initialise the dialog */
891 dialog = gtk_file_chooser_dialog_new(_("Choose Project Filename"), NULL,
892 GTK_FILE_CHOOSER_ACTION_SAVE,
893 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
894 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
895 gtk_widget_set_name(dialog, "GeanyDialogProject");
896 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
897 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
898 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
899 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
901 run_dialog(dialog, e->file_name);
902 #endif
906 /* sets the project base path and the project file name according to the project name */
907 static void on_name_entry_changed(GtkEditable *editable, PropertyDialogElements *e)
909 gchar *base_path;
910 gchar *file_name;
911 gchar *name;
912 const gchar *project_dir = local_prefs.project_file_path;
914 if (entries_modified)
915 return;
917 name = gtk_editable_get_chars(editable, 0, -1);
918 if (NZV(name))
920 base_path = g_strconcat(project_dir, G_DIR_SEPARATOR_S,
921 name, G_DIR_SEPARATOR_S, NULL);
922 if (project_prefs.project_file_in_basedir)
923 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S, name, G_DIR_SEPARATOR_S,
924 name, "." GEANY_PROJECT_EXT, NULL);
925 else
926 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S,
927 name, "." GEANY_PROJECT_EXT, NULL);
929 else
931 base_path = g_strconcat(project_dir, G_DIR_SEPARATOR_S, NULL);
932 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S, NULL);
934 g_free(name);
936 gtk_entry_set_text(GTK_ENTRY(e->base_path), base_path);
937 gtk_entry_set_text(GTK_ENTRY(e->file_name), file_name);
939 entries_modified = FALSE;
941 g_free(base_path);
942 g_free(file_name);
946 static void on_entries_changed(GtkEditable *editable, PropertyDialogElements *e)
948 entries_modified = TRUE;
952 static void on_radio_long_line_custom_toggled(GtkToggleButton *radio, GtkWidget *spin_long_line)
954 gtk_widget_set_sensitive(spin_long_line, gtk_toggle_button_get_active(radio));
958 gboolean project_load_file(const gchar *locale_file_name)
960 g_return_val_if_fail(locale_file_name != NULL, FALSE);
962 if (load_config(locale_file_name))
964 gchar *utf8_filename = utils_get_utf8_from_locale(locale_file_name);
966 ui_set_statusbar(TRUE, _("Project \"%s\" opened."), app->project->name);
968 ui_add_recent_project_file(utf8_filename);
969 g_free(utf8_filename);
970 return TRUE;
972 else
974 gchar *utf8_filename = utils_get_utf8_from_locale(locale_file_name);
976 ui_set_statusbar(TRUE, _("Project file \"%s\" could not be loaded."), utf8_filename);
977 g_free(utf8_filename);
979 return FALSE;
983 /* Reads the given filename and creates a new project with the data found in the file.
984 * At this point there should not be an already opened project in Geany otherwise it will just
985 * return.
986 * The filename is expected in the locale encoding. */
987 static gboolean load_config(const gchar *filename)
989 GKeyFile *config;
990 GeanyProject *p;
992 /* there should not be an open project */
993 g_return_val_if_fail(app->project == NULL && filename != NULL, FALSE);
995 config = g_key_file_new();
996 if (! g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL))
998 g_key_file_free(config);
999 return FALSE;
1002 p = create_project();
1004 stash_group_load_from_key_file(indent_group, config);
1006 p->name = utils_get_setting_string(config, "project", "name", GEANY_STRING_UNTITLED);
1007 p->description = utils_get_setting_string(config, "project", "description", "");
1008 p->file_name = utils_get_utf8_from_locale(filename);
1009 p->base_path = utils_get_setting_string(config, "project", "base_path", "");
1010 p->file_patterns = g_key_file_get_string_list(config, "project", "file_patterns", NULL, NULL);
1012 p->long_line_behaviour = utils_get_setting_integer(config, "long line marker",
1013 "long_line_behaviour", 1 /* follow global */);
1014 p->long_line_column = utils_get_setting_integer(config, "long line marker",
1015 "long_line_column", editor_prefs.long_line_global_column);
1016 apply_editor_prefs();
1018 build_load_menu(config, GEANY_BCS_PROJ, (gpointer)p);
1019 if (project_prefs.project_session)
1021 /* save current (non-project) session (it could has been changed since program startup) */
1022 configuration_save_default_session();
1023 /* now close all open files */
1024 document_close_all();
1025 /* read session files so they can be opened with configuration_open_files() */
1026 configuration_load_session_files(config, FALSE);
1028 g_signal_emit_by_name(geany_object, "project-open", config);
1029 g_key_file_free(config);
1031 update_ui();
1032 return TRUE;
1036 static void apply_editor_prefs()
1038 guint i;
1040 foreach_document(i)
1041 editor_apply_update_prefs(documents[i]->editor);
1045 /* Write the project settings as well as the project session files into its configuration files.
1046 * emit_signal defines whether the project-save signal should be emitted. When write_config()
1047 * is called while closing a project, this is used to skip emitting the signal because
1048 * project-close will be emitted afterwards.
1049 * Returns: TRUE if project file was written successfully. */
1050 static gboolean write_config(gboolean emit_signal)
1052 GeanyProject *p;
1053 GKeyFile *config;
1054 gchar *filename;
1055 gchar *data;
1056 gboolean ret = FALSE;
1058 g_return_val_if_fail(app->project != NULL, FALSE);
1060 p = app->project;
1062 config = g_key_file_new();
1063 /* try to load an existing config to keep manually added comments */
1064 filename = utils_get_locale_from_utf8(p->file_name);
1065 g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL);
1067 stash_group_save_to_key_file(indent_group, config);
1069 g_key_file_set_string(config, "project", "name", p->name);
1070 g_key_file_set_string(config, "project", "base_path", p->base_path);
1072 if (p->description)
1073 g_key_file_set_string(config, "project", "description", p->description);
1074 if (p->file_patterns)
1075 g_key_file_set_string_list(config, "project", "file_patterns",
1076 (const gchar**) p->file_patterns, g_strv_length(p->file_patterns));
1078 g_key_file_set_integer(config, "long line marker", "long_line_behaviour", p->long_line_behaviour);
1079 g_key_file_set_integer(config, "long line marker", "long_line_column", p->long_line_column);
1081 /* store the session files into the project too */
1082 if (project_prefs.project_session)
1083 configuration_save_session_files(config);
1084 build_save_menu(config, (gpointer)p, GEANY_BCS_PROJ);
1085 if (emit_signal)
1087 g_signal_emit_by_name(geany_object, "project-save", config);
1089 /* write the file */
1090 data = g_key_file_to_data(config, NULL, NULL);
1091 ret = (utils_write_file(filename, data) == 0);
1093 g_free(data);
1094 g_free(filename);
1095 g_key_file_free(config);
1097 return ret;
1101 /* Constructs the project's base path which is used for "Make all" and "Execute".
1102 * The result is an absolute string in UTF-8 encoding which is either the same as
1103 * base path if it is absolute or it is built out of project file name's dir and base_path.
1104 * If there is no project or project's base_path is invalid, NULL will be returned.
1105 * The returned string should be freed when no longer needed. */
1106 gchar *project_get_base_path(void)
1108 GeanyProject *project = app->project;
1110 if (project && NZV(project->base_path))
1112 if (g_path_is_absolute(project->base_path))
1113 return g_strdup(project->base_path);
1114 else
1115 { /* build base_path out of project file name's dir and base_path */
1116 gchar *path;
1117 gchar *dir = g_path_get_dirname(project->file_name);
1119 if (utils_str_equal(project->base_path, "./"))
1120 return dir;
1121 else
1122 path = g_strconcat(dir, G_DIR_SEPARATOR_S, project->base_path, NULL);
1123 g_free(dir);
1124 return path;
1127 return NULL;
1131 /* This is to save project-related global settings, NOT project file settings. */
1132 void project_save_prefs(GKeyFile *config)
1134 GeanyProject *project = app->project;
1136 if (cl_options.load_session)
1138 gchar *utf8_filename = (project == NULL) ? "" : project->file_name;
1140 g_key_file_set_string(config, "project", "session_file", utf8_filename);
1142 g_key_file_set_string(config, "project", "project_file_path",
1143 NVL(local_prefs.project_file_path, ""));
1147 void project_load_prefs(GKeyFile *config)
1149 if (cl_options.load_session)
1151 g_return_if_fail(project_prefs.session_file == NULL);
1152 project_prefs.session_file = utils_get_setting_string(config, "project",
1153 "session_file", "");
1155 local_prefs.project_file_path = utils_get_setting_string(config, "project",
1156 "project_file_path", NULL);
1157 if (local_prefs.project_file_path == NULL)
1159 local_prefs.project_file_path = g_strconcat(g_get_home_dir(),
1160 G_DIR_SEPARATOR_S, PROJECT_DIR, NULL);
1165 /* Initialize project-related preferences in the Preferences dialog. */
1166 void project_setup_prefs(void)
1168 GtkWidget *path_entry = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_entry");
1169 GtkWidget *path_btn = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_button");
1170 static gboolean callback_setup = FALSE;
1172 g_return_if_fail(local_prefs.project_file_path != NULL);
1174 gtk_entry_set_text(GTK_ENTRY(path_entry), local_prefs.project_file_path);
1175 if (! callback_setup)
1176 { /* connect the callback only once */
1177 callback_setup = TRUE;
1178 ui_setup_open_button_callback(path_btn, NULL,
1179 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(path_entry));
1184 /* Update project-related preferences after using the Preferences dialog. */
1185 void project_apply_prefs(void)
1187 GtkWidget *path_entry = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_entry");
1188 const gchar *str;
1190 str = gtk_entry_get_text(GTK_ENTRY(path_entry));
1191 setptr(local_prefs.project_file_path, g_strdup(str));
1195 void project_init(void)
1197 StashGroup *group;
1199 group = stash_group_new("indentation");
1200 /* defaults are copied from editor indent prefs */
1201 stash_group_set_use_defaults(group, FALSE);
1202 indent_group = group;
1204 stash_group_add_spin_button_integer(group, &indentation.width,
1205 "indent_width", 4, "spin_indent_width");
1206 stash_group_add_radio_buttons(group, (gint*)(gpointer)&indentation.type,
1207 "indent_type", GEANY_INDENT_TYPE_TABS,
1208 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
1209 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
1210 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
1211 NULL);
1212 stash_group_add_spin_button_integer(group, &indentation.hard_tab_width,
1213 "indent_hard_tab_width", 8, "spin_tab_width");
1214 stash_group_add_toggle_button(group, &indentation.detect_type,
1215 "detect_indent", FALSE, "check_detect_indent");
1216 stash_group_add_combo_box(group, (gint*)(gpointer)&indentation.auto_indent_mode,
1217 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
1221 void project_finalize(void)
1223 stash_group_free(indent_group);