Add tooltips to explain the purpose of the New Project dialog fields
[geany-mirror.git] / src / project.c
blob42d3d3e6ba37ac21ea2f53684731c5ff63bcdaea
1 /*
2 * project.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2012 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /** @file project.h
23 * Project Management.
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "project.h"
32 #include "app.h"
33 #include "build.h"
34 #include "dialogs.h"
35 #include "document.h"
36 #include "editor.h"
37 #include "filetypesprivate.h"
38 #include "geanyobject.h"
39 #include "keyfile.h"
40 #include "main.h"
41 #include "projectprivate.h"
42 #include "sidebar.h"
43 #include "stash.h"
44 #include "support.h"
45 #include "ui_utils.h"
46 #include "utils.h"
48 #include <string.h>
49 #include <unistd.h>
50 #include <errno.h>
53 ProjectPrefs project_prefs = { NULL, FALSE, FALSE };
56 static GeanyProjectPrivate priv;
57 static GeanyIndentPrefs indentation;
59 static GSList *stash_groups = NULL;
61 static struct
63 gchar *project_file_path; /* in UTF-8 */
64 } 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 gint build_page_num;
80 } PropertyDialogElements;
83 static gboolean update_config(const PropertyDialogElements *e, gboolean new_project);
84 static void on_file_save_button_clicked(GtkButton *button, PropertyDialogElements *e);
85 static gboolean load_config(const gchar *filename);
86 static gboolean write_config(gboolean emit_signal);
87 static void on_name_entry_changed(GtkEditable *editable, PropertyDialogElements *e);
88 static void on_entries_changed(GtkEditable *editable, PropertyDialogElements *e);
89 static void on_radio_long_line_custom_toggled(GtkToggleButton *radio, GtkWidget *spin_long_line);
90 static void apply_editor_prefs(void);
91 static void init_stash_prefs(void);
94 #define SHOW_ERR(args) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args)
95 #define SHOW_ERR1(args, more) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args, more)
96 #define MAX_NAME_LEN 50
97 /* "projects" is part of the default project base path so be careful when translating
98 * please avoid special characters and spaces, look at the source for details or ask Frank */
99 #define PROJECT_DIR _("projects")
102 /* TODO: this should be ported to Glade like the project preferences dialog,
103 * then we can get rid of the PropertyDialogElements struct altogether as
104 * widgets pointers can be accessed through ui_lookup_widget(). */
105 void project_new(void)
107 GtkWidget *vbox;
108 GtkWidget *table;
109 GtkWidget *image;
110 GtkWidget *button;
111 GtkWidget *bbox;
112 GtkWidget *label;
113 gchar *tooltip;
114 PropertyDialogElements *e;
116 if (! project_ask_close())
117 return;
119 g_return_if_fail(app->project == NULL);
121 e = g_new0(PropertyDialogElements, 1);
122 e->dialog = gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(main_widgets.window),
123 GTK_DIALOG_DESTROY_WITH_PARENT,
124 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
126 gtk_widget_set_name(e->dialog, "GeanyDialogProject");
127 button = ui_button_new_with_image(GTK_STOCK_NEW, _("C_reate"));
128 gtk_widget_set_can_default(button, TRUE);
129 gtk_window_set_default(GTK_WINDOW(e->dialog), button);
130 gtk_dialog_add_action_widget(GTK_DIALOG(e->dialog), button, GTK_RESPONSE_OK);
132 vbox = ui_dialog_vbox_new(GTK_DIALOG(e->dialog));
134 entries_modified = FALSE;
136 table = gtk_table_new(3, 2, FALSE);
137 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
138 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
140 label = gtk_label_new(_("Name:"));
141 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
143 e->name = gtk_entry_new();
144 gtk_entry_set_activates_default(GTK_ENTRY(e->name), TRUE);
145 ui_entry_add_clear_icon(GTK_ENTRY(e->name));
146 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
147 gtk_widget_set_tooltip_text(e->name, _("Project name"));
149 ui_table_add_row(GTK_TABLE(table), 0, label, e->name, NULL);
151 label = gtk_label_new(_("Filename:"));
152 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
154 e->file_name = gtk_entry_new();
155 gtk_entry_set_activates_default(GTK_ENTRY(e->file_name), TRUE);
156 ui_entry_add_clear_icon(GTK_ENTRY(e->file_name));
157 gtk_entry_set_width_chars(GTK_ENTRY(e->file_name), 30);
158 tooltip = g_strdup_printf(
159 _("Path of the file representing the project and storing its settings. "
160 "It should normally have the \"%s\" extension."), "."GEANY_PROJECT_EXT);
161 gtk_widget_set_tooltip_text(e->file_name, tooltip);
162 g_free(tooltip);
163 button = gtk_button_new();
164 g_signal_connect(button, "clicked", G_CALLBACK(on_file_save_button_clicked), e);
165 image = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
166 gtk_container_add(GTK_CONTAINER(button), image);
167 bbox = gtk_hbox_new(FALSE, 6);
168 gtk_box_pack_start(GTK_BOX(bbox), e->file_name, TRUE, TRUE, 0);
169 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
171 ui_table_add_row(GTK_TABLE(table), 1, label, bbox, NULL);
173 label = gtk_label_new(_("Base path:"));
174 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
176 e->base_path = gtk_entry_new();
177 gtk_entry_set_activates_default(GTK_ENTRY(e->base_path), TRUE);
178 ui_entry_add_clear_icon(GTK_ENTRY(e->base_path));
179 gtk_widget_set_tooltip_text(e->base_path,
180 _("Base directory of all files that make up the project. "
181 "This can be a new path, or an existing directory tree. "
182 "You can use paths relative to the project filename."));
183 bbox = ui_path_box_new(_("Choose Project Base Path"),
184 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(e->base_path));
186 ui_table_add_row(GTK_TABLE(table), 2, label, bbox, NULL);
188 gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
190 /* signals */
191 g_signal_connect(e->name, "changed", G_CALLBACK(on_name_entry_changed), e);
192 /* run the callback manually to initialise the base_path and file_name fields */
193 on_name_entry_changed(GTK_EDITABLE(e->name), e);
195 g_signal_connect(e->file_name, "changed", G_CALLBACK(on_entries_changed), e);
196 g_signal_connect(e->base_path, "changed", G_CALLBACK(on_entries_changed), e);
198 gtk_widget_show_all(e->dialog);
200 while (gtk_dialog_run(GTK_DIALOG(e->dialog)) == GTK_RESPONSE_OK)
202 if (update_config(e, TRUE))
204 if (!write_config(TRUE))
205 SHOW_ERR(_("Project file could not be written"));
206 else
208 ui_set_statusbar(TRUE, _("Project \"%s\" created."), app->project->name);
210 ui_add_recent_project_file(app->project->file_name);
211 break;
215 gtk_widget_destroy(e->dialog);
216 g_free(e);
220 gboolean project_load_file_with_session(const gchar *locale_file_name)
222 if (project_load_file(locale_file_name))
224 if (project_prefs.project_session)
226 configuration_open_files();
227 /* open a new file if no other file was opened */
228 document_new_file_if_non_open();
229 ui_focus_current_document();
231 return TRUE;
233 return FALSE;
237 #ifndef G_OS_WIN32
238 static void run_open_dialog(GtkDialog *dialog)
240 while (gtk_dialog_run(dialog) == GTK_RESPONSE_ACCEPT)
242 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
244 /* try to load the config */
245 if (! project_load_file_with_session(filename))
247 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
249 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), utf8_filename);
250 gtk_widget_grab_focus(GTK_WIDGET(dialog));
251 g_free(utf8_filename);
252 g_free(filename);
253 continue;
255 g_free(filename);
256 break;
259 #endif
262 void project_open(void)
264 const gchar *dir = local_prefs.project_file_path;
265 #ifdef G_OS_WIN32
266 gchar *file;
267 #else
268 GtkWidget *dialog;
269 GtkFileFilter *filter;
270 gchar *locale_path;
271 #endif
272 if (! project_ask_close()) return;
274 #ifdef G_OS_WIN32
275 file = win32_show_project_open_dialog(main_widgets.window, _("Open Project"), dir, FALSE, TRUE);
276 if (file != NULL)
278 /* try to load the config */
279 if (! project_load_file_with_session(file))
281 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), file);
283 g_free(file);
285 #else
287 dialog = gtk_file_chooser_dialog_new(_("Open Project"), GTK_WINDOW(main_widgets.window),
288 GTK_FILE_CHOOSER_ACTION_OPEN,
289 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
290 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
291 gtk_widget_set_name(dialog, "GeanyDialogProject");
293 /* set default Open, so pressing enter can open multiple files */
294 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
295 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
296 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
297 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
298 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
299 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
301 /* add FileFilters */
302 filter = gtk_file_filter_new();
303 gtk_file_filter_set_name(filter, _("All files"));
304 gtk_file_filter_add_pattern(filter, "*");
305 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
306 filter = gtk_file_filter_new();
307 gtk_file_filter_set_name(filter, _("Project files"));
308 gtk_file_filter_add_pattern(filter, "*." GEANY_PROJECT_EXT);
309 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
310 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
312 locale_path = utils_get_locale_from_utf8(dir);
313 if (g_file_test(locale_path, G_FILE_TEST_EXISTS) &&
314 g_file_test(locale_path, G_FILE_TEST_IS_DIR))
316 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
318 g_free(locale_path);
320 gtk_widget_show_all(dialog);
321 run_open_dialog(GTK_DIALOG(dialog));
322 gtk_widget_destroy(GTK_WIDGET(dialog));
323 #endif
327 /* Called when creating, opening, closing and updating projects. */
328 static void update_ui(void)
330 if (main_status.quitting)
331 return;
333 ui_set_window_title(NULL);
334 build_menu_update(NULL);
335 sidebar_openfiles_update_all();
339 static void remove_foreach_project_filetype(gpointer data, gpointer user_data)
341 GeanyFiletype *ft = data;
342 if (ft != NULL)
344 SETPTR(ft->priv->projfilecmds, NULL);
345 SETPTR(ft->priv->projexeccmds, NULL);
346 SETPTR(ft->priv->projerror_regex_string, NULL);
347 ft->priv->project_list_entry = -1;
352 /* open_default will make function reload default session files on close */
353 void project_close(gboolean open_default)
355 GSList *node;
357 g_return_if_fail(app->project != NULL);
359 /* save project session files, etc */
360 if (!write_config(FALSE))
361 g_warning("Project file \"%s\" could not be written", app->project->file_name);
363 if (project_prefs.project_session)
365 /* close all existing tabs first */
366 if (!document_close_all())
367 return;
369 ui_set_statusbar(TRUE, _("Project \"%s\" closed."), app->project->name);
371 /* remove project filetypes build entries */
372 if (app->project->priv->build_filetypes_list != NULL)
374 g_ptr_array_foreach(app->project->priv->build_filetypes_list, remove_foreach_project_filetype, NULL);
375 g_ptr_array_free(app->project->priv->build_filetypes_list, FALSE);
378 /* remove project non filetype build menu items */
379 build_remove_menu_item(GEANY_BCS_PROJ, GEANY_GBG_NON_FT, -1);
380 build_remove_menu_item(GEANY_BCS_PROJ, GEANY_GBG_EXEC, -1);
382 g_free(app->project->name);
383 g_free(app->project->description);
384 g_free(app->project->file_name);
385 g_free(app->project->base_path);
387 g_free(app->project);
388 app->project = NULL;
390 foreach_slist(node, stash_groups)
391 stash_group_free(node->data);
393 g_slist_free(stash_groups);
394 stash_groups = NULL;
396 apply_editor_prefs(); /* ensure that global settings are restored */
398 if (project_prefs.project_session)
400 /* after closing all tabs let's open the tabs found in the default config */
401 if (open_default && cl_options.load_session)
403 configuration_reload_default_session();
404 configuration_open_files();
405 /* open a new file if no other file was opened */
406 document_new_file_if_non_open();
407 ui_focus_current_document();
410 g_signal_emit_by_name(geany_object, "project-close");
412 update_ui();
416 /* Shows the file chooser dialog when base path button is clicked
417 * FIXME: this should be connected in Glade but 3.8.1 has a bug
418 * where it won't pass any objects as user data (#588824). */
419 G_MODULE_EXPORT void
420 on_project_properties_base_path_button_clicked(GtkWidget *button,
421 GtkWidget *base_path_entry)
423 GtkWidget *dialog;
425 g_return_if_fail(base_path_entry != NULL);
426 g_return_if_fail(GTK_IS_WIDGET(base_path_entry));
428 dialog = gtk_file_chooser_dialog_new(_("Choose Project Base Path"),
429 NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
430 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
431 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
432 NULL);
434 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
436 gtk_entry_set_text(GTK_ENTRY(base_path_entry),
437 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
440 gtk_widget_destroy(dialog);
444 static void insert_build_page(PropertyDialogElements *e)
446 GtkWidget *build_table, *label;
447 GeanyDocument *doc = document_get_current();
448 GeanyFiletype *ft = NULL;
450 if (doc != NULL)
451 ft = doc->file_type;
453 build_table = build_commands_table(doc, GEANY_BCS_PROJ, &(e->build_properties), ft);
454 gtk_container_set_border_width(GTK_CONTAINER(build_table), 6);
455 label = gtk_label_new(_("Build"));
456 e->build_page_num = gtk_notebook_append_page(GTK_NOTEBOOK(e->notebook),
457 build_table, label);
461 static void create_properties_dialog(PropertyDialogElements *e)
463 GtkWidget *base_path_button;
464 static guint base_path_button_handler_id = 0;
465 static guint radio_long_line_handler_id = 0;
467 e->dialog = create_project_dialog();
468 e->notebook = ui_lookup_widget(e->dialog, "project_notebook");
469 e->file_name = ui_lookup_widget(e->dialog, "label_project_dialog_filename");
470 e->name = ui_lookup_widget(e->dialog, "entry_project_dialog_name");
471 e->description = ui_lookup_widget(e->dialog, "textview_project_dialog_description");
472 e->base_path = ui_lookup_widget(e->dialog, "entry_project_dialog_base_path");
473 e->patterns = ui_lookup_widget(e->dialog, "entry_project_dialog_file_patterns");
475 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
477 ui_entry_add_clear_icon(GTK_ENTRY(e->name));
478 ui_entry_add_clear_icon(GTK_ENTRY(e->base_path));
479 ui_entry_add_clear_icon(GTK_ENTRY(e->patterns));
481 /* Workaround for bug in Glade 3.8.1, see comment above signal handler */
482 if (base_path_button_handler_id == 0)
484 base_path_button = ui_lookup_widget(e->dialog, "button_project_dialog_base_path");
485 base_path_button_handler_id =
486 g_signal_connect(base_path_button, "clicked",
487 G_CALLBACK(on_project_properties_base_path_button_clicked),
488 e->base_path);
491 /* Same as above, should be in Glade but can't due to bug in 3.8.1 */
492 if (radio_long_line_handler_id == 0)
494 radio_long_line_handler_id =
495 g_signal_connect(ui_lookup_widget(e->dialog,
496 "radio_long_line_custom_project"), "toggled",
497 G_CALLBACK(on_radio_long_line_custom_toggled),
498 ui_lookup_widget(e->dialog, "spin_long_line_project"));
503 static void show_project_properties(gboolean show_build)
505 GeanyProject *p = app->project;
506 GtkWidget *widget = NULL;
507 GtkWidget *radio_long_line_custom;
508 static PropertyDialogElements e;
509 GSList *node;
511 g_return_if_fail(app->project != NULL);
513 entries_modified = FALSE;
515 if (e.dialog == NULL)
516 create_properties_dialog(&e);
518 insert_build_page(&e);
520 foreach_slist(node, stash_groups)
521 stash_group_display(node->data, e.dialog);
523 /* fill the elements with the appropriate data */
524 gtk_entry_set_text(GTK_ENTRY(e.name), p->name);
525 gtk_label_set_text(GTK_LABEL(e.file_name), p->file_name);
526 gtk_entry_set_text(GTK_ENTRY(e.base_path), p->base_path);
528 radio_long_line_custom = ui_lookup_widget(e.dialog, "radio_long_line_custom_project");
529 switch (p->priv->long_line_behaviour)
531 case 0: widget = ui_lookup_widget(e.dialog, "radio_long_line_disabled_project"); break;
532 case 1: widget = ui_lookup_widget(e.dialog, "radio_long_line_default_project"); break;
533 case 2: widget = radio_long_line_custom; break;
535 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
537 widget = ui_lookup_widget(e.dialog, "spin_long_line_project");
538 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), (gdouble)p->priv->long_line_column);
539 on_radio_long_line_custom_toggled(GTK_TOGGLE_BUTTON(radio_long_line_custom), widget);
541 if (p->description != NULL)
542 { /* set text */
543 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e.description));
544 gtk_text_buffer_set_text(buffer, p->description, -1);
547 if (p->file_patterns != NULL)
548 { /* set the file patterns */
549 gchar *str;
551 str = g_strjoinv(" ", p->file_patterns);
552 gtk_entry_set_text(GTK_ENTRY(e.patterns), str);
553 g_free(str);
556 g_signal_emit_by_name(geany_object, "project-dialog-open", e.notebook);
557 gtk_widget_show_all(e.dialog);
559 /* note: notebook page must be shown before setting current page */
560 if (show_build)
561 gtk_notebook_set_current_page(GTK_NOTEBOOK(e.notebook), e.build_page_num);
562 else
563 gtk_notebook_set_current_page(GTK_NOTEBOOK(e.notebook), 0);
565 while (gtk_dialog_run(GTK_DIALOG(e.dialog)) == GTK_RESPONSE_OK)
567 if (update_config(&e, FALSE))
569 g_signal_emit_by_name(geany_object, "project-dialog-confirmed", e.notebook);
570 if (!write_config(TRUE))
571 SHOW_ERR(_("Project file could not be written"));
572 else
574 ui_set_statusbar(TRUE, _("Project \"%s\" saved."), app->project->name);
575 break;
580 build_free_fields(e.build_properties);
581 g_signal_emit_by_name(geany_object, "project-dialog-close", e.notebook);
582 gtk_notebook_remove_page(GTK_NOTEBOOK(e.notebook), e.build_page_num);
583 gtk_widget_hide(e.dialog);
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 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 priv.indentation = &indentation;
627 project->priv = &priv;
629 init_stash_prefs();
631 project->file_patterns = NULL;
633 project->priv->long_line_behaviour = 1 /* use global settings */;
634 project->priv->long_line_column = editor_prefs.long_line_column;
636 app->project = project;
637 return project;
641 /* Verifies data for New & Properties dialogs.
642 * Returns: FALSE if the user needs to change any data. */
643 static gboolean update_config(const PropertyDialogElements *e, gboolean new_project)
645 const gchar *name, *file_name, *base_path;
646 gchar *locale_filename;
647 gsize name_len;
648 gint err_code = 0;
649 GeanyProject *p;
651 g_return_val_if_fail(e != NULL, TRUE);
653 name = gtk_entry_get_text(GTK_ENTRY(e->name));
654 name_len = strlen(name);
655 if (name_len == 0)
657 SHOW_ERR(_("The specified project name is too short."));
658 gtk_widget_grab_focus(e->name);
659 return FALSE;
661 else if (name_len > MAX_NAME_LEN)
663 SHOW_ERR1(_("The specified project name is too long (max. %d characters)."), MAX_NAME_LEN);
664 gtk_widget_grab_focus(e->name);
665 return FALSE;
668 if (new_project)
669 file_name = gtk_entry_get_text(GTK_ENTRY(e->file_name));
670 else
671 file_name = gtk_label_get_text(GTK_LABEL(e->file_name));
673 if (G_UNLIKELY(EMPTY(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 (!EMPTY(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, 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_writable(locale_filename)) != 0 ||
719 (err_code = g_file_test(locale_filename, G_FILE_TEST_IS_DIR) ? EISDIR : 0) != 0)
721 SHOW_ERR1(_("Project file could not be written (%s)."), g_strerror(err_code));
722 gtk_widget_grab_focus(e->file_name);
723 g_free(locale_filename);
724 return FALSE;
726 g_free(locale_filename);
728 if (app->project == NULL)
730 create_project();
731 new_project = TRUE;
733 p = app->project;
735 SETPTR(p->name, g_strdup(name));
736 SETPTR(p->file_name, g_strdup(file_name));
737 /* use "." if base_path is empty */
738 SETPTR(p->base_path, g_strdup(!EMPTY(base_path) ? base_path : "./"));
740 if (! new_project) /* save properties specific fields */
742 GtkTextIter start, end;
743 GtkTextBuffer *buffer;
744 GeanyDocument *doc = document_get_current();
745 GeanyBuildCommand *oldvalue;
746 GeanyFiletype *ft = doc ? doc->file_type : NULL;
747 GtkWidget *widget;
748 gchar *tmp;
749 GString *str;
750 GSList *node;
752 /* get and set the project description */
753 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->description));
754 gtk_text_buffer_get_start_iter(buffer, &start);
755 gtk_text_buffer_get_end_iter(buffer, &end);
756 SETPTR(p->description, g_strdup(gtk_text_buffer_get_text(buffer, &start, &end, FALSE)));
758 foreach_slist(node, stash_groups)
759 stash_group_update(node->data, e->dialog);
761 /* read the project build menu */
762 oldvalue = ft ? ft->priv->projfilecmds : NULL;
763 build_read_project(ft, e->build_properties);
765 if (ft != NULL && ft->priv->projfilecmds != oldvalue && ft->priv->project_list_entry < 0)
767 if (p->priv->build_filetypes_list == NULL)
768 p->priv->build_filetypes_list = g_ptr_array_new();
769 ft->priv->project_list_entry = p->priv->build_filetypes_list->len;
770 g_ptr_array_add(p->priv->build_filetypes_list, ft);
772 build_menu_update(doc);
774 widget = ui_lookup_widget(e->dialog, "radio_long_line_disabled_project");
775 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
776 p->priv->long_line_behaviour = 0;
777 else
779 widget = ui_lookup_widget(e->dialog, "radio_long_line_default_project");
780 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
781 p->priv->long_line_behaviour = 1;
782 else
783 /* "Custom" radio button must be checked */
784 p->priv->long_line_behaviour = 2;
787 widget = ui_lookup_widget(e->dialog, "spin_long_line_project");
788 p->priv->long_line_column = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
789 apply_editor_prefs();
791 /* get and set the project file patterns */
792 tmp = g_strdup(gtk_entry_get_text(GTK_ENTRY(e->patterns)));
793 g_strfreev(p->file_patterns);
794 g_strstrip(tmp);
795 str = g_string_new(tmp);
796 do {} while (utils_string_replace_all(str, " ", " "));
797 p->file_patterns = g_strsplit(str->str, " ", -1);
798 g_string_free(str, TRUE);
799 g_free(tmp);
802 update_ui();
804 return TRUE;
808 #ifndef G_OS_WIN32
809 static void run_dialog(GtkWidget *dialog, GtkWidget *entry)
811 /* set filename in the file chooser dialog */
812 const gchar *utf8_filename = gtk_entry_get_text(GTK_ENTRY(entry));
813 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
815 if (g_path_is_absolute(locale_filename))
817 if (g_file_test(locale_filename, G_FILE_TEST_EXISTS))
819 /* if the current filename is a directory, we must use
820 * gtk_file_chooser_set_current_folder(which expects a locale filename) otherwise
821 * we end up in the parent directory */
822 if (g_file_test(locale_filename, G_FILE_TEST_IS_DIR))
823 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_filename);
824 else
825 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), utf8_filename);
827 else /* if the file doesn't yet exist, use at least the current directory */
829 gchar *locale_dir = g_path_get_dirname(locale_filename);
830 gchar *name = g_path_get_basename(utf8_filename);
832 if (g_file_test(locale_dir, G_FILE_TEST_EXISTS))
833 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dir);
834 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), name);
836 g_free(name);
837 g_free(locale_dir);
840 else if (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog)) != GTK_FILE_CHOOSER_ACTION_OPEN)
842 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), utf8_filename);
844 g_free(locale_filename);
846 /* run it */
847 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
849 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
850 gchar *tmp_utf8_filename = utils_get_utf8_from_locale(filename);
852 gtk_entry_set_text(GTK_ENTRY(entry), tmp_utf8_filename);
854 g_free(tmp_utf8_filename);
855 g_free(filename);
857 gtk_widget_destroy(dialog);
859 #endif
862 static void on_file_save_button_clicked(GtkButton *button, PropertyDialogElements *e)
864 #ifdef G_OS_WIN32
865 gchar *path = win32_show_project_open_dialog(e->dialog, _("Choose Project Filename"),
866 gtk_entry_get_text(GTK_ENTRY(e->file_name)), TRUE, TRUE);
867 if (path != NULL)
869 gtk_entry_set_text(GTK_ENTRY(e->file_name), path);
870 g_free(path);
872 #else
873 GtkWidget *dialog;
875 /* initialise the dialog */
876 dialog = gtk_file_chooser_dialog_new(_("Choose Project Filename"), NULL,
877 GTK_FILE_CHOOSER_ACTION_SAVE,
878 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
879 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
880 gtk_widget_set_name(dialog, "GeanyDialogProject");
881 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
882 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
883 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
884 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
886 run_dialog(dialog, e->file_name);
887 #endif
891 /* sets the project base path and the project file name according to the project name */
892 static void on_name_entry_changed(GtkEditable *editable, PropertyDialogElements *e)
894 gchar *base_path;
895 gchar *file_name;
896 gchar *name;
897 const gchar *project_dir = local_prefs.project_file_path;
899 if (entries_modified)
900 return;
902 name = gtk_editable_get_chars(editable, 0, -1);
903 if (!EMPTY(name))
905 base_path = g_strconcat(project_dir, G_DIR_SEPARATOR_S,
906 name, G_DIR_SEPARATOR_S, NULL);
907 if (project_prefs.project_file_in_basedir)
908 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S, name, G_DIR_SEPARATOR_S,
909 name, "." GEANY_PROJECT_EXT, NULL);
910 else
911 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S,
912 name, "." GEANY_PROJECT_EXT, NULL);
914 else
916 base_path = g_strconcat(project_dir, G_DIR_SEPARATOR_S, NULL);
917 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S, NULL);
919 g_free(name);
921 gtk_entry_set_text(GTK_ENTRY(e->base_path), base_path);
922 gtk_entry_set_text(GTK_ENTRY(e->file_name), file_name);
924 entries_modified = FALSE;
926 g_free(base_path);
927 g_free(file_name);
931 static void on_entries_changed(GtkEditable *editable, PropertyDialogElements *e)
933 entries_modified = TRUE;
937 static void on_radio_long_line_custom_toggled(GtkToggleButton *radio, GtkWidget *spin_long_line)
939 gtk_widget_set_sensitive(spin_long_line, gtk_toggle_button_get_active(radio));
943 gboolean project_load_file(const gchar *locale_file_name)
945 g_return_val_if_fail(locale_file_name != NULL, FALSE);
947 if (load_config(locale_file_name))
949 gchar *utf8_filename = utils_get_utf8_from_locale(locale_file_name);
951 ui_set_statusbar(TRUE, _("Project \"%s\" opened."), app->project->name);
953 ui_add_recent_project_file(utf8_filename);
954 g_free(utf8_filename);
955 return TRUE;
957 else
959 gchar *utf8_filename = utils_get_utf8_from_locale(locale_file_name);
961 ui_set_statusbar(TRUE, _("Project file \"%s\" could not be loaded."), utf8_filename);
962 g_free(utf8_filename);
964 return FALSE;
968 /* Reads the given filename and creates a new project with the data found in the file.
969 * At this point there should not be an already opened project in Geany otherwise it will just
970 * return.
971 * The filename is expected in the locale encoding. */
972 static gboolean load_config(const gchar *filename)
974 GKeyFile *config;
975 GeanyProject *p;
976 GSList *node;
978 /* there should not be an open project */
979 g_return_val_if_fail(app->project == NULL && filename != NULL, FALSE);
981 config = g_key_file_new();
982 if (! g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL))
984 g_key_file_free(config);
985 return FALSE;
988 p = create_project();
990 foreach_slist(node, stash_groups)
991 stash_group_load_from_key_file(node->data, config);
993 p->name = utils_get_setting_string(config, "project", "name", GEANY_STRING_UNTITLED);
994 p->description = utils_get_setting_string(config, "project", "description", "");
995 p->file_name = utils_get_utf8_from_locale(filename);
996 p->base_path = utils_get_setting_string(config, "project", "base_path", "");
997 p->file_patterns = g_key_file_get_string_list(config, "project", "file_patterns", NULL, NULL);
999 p->priv->long_line_behaviour = utils_get_setting_integer(config, "long line marker",
1000 "long_line_behaviour", 1 /* follow global */);
1001 p->priv->long_line_column = utils_get_setting_integer(config, "long line marker",
1002 "long_line_column", editor_prefs.long_line_column);
1003 apply_editor_prefs();
1005 build_load_menu(config, GEANY_BCS_PROJ, (gpointer)p);
1006 if (project_prefs.project_session)
1008 /* save current (non-project) session (it could has been changed since program startup) */
1009 configuration_save_default_session();
1010 /* now close all open files */
1011 document_close_all();
1012 /* read session files so they can be opened with configuration_open_files() */
1013 configuration_load_session_files(config, FALSE);
1014 ui_focus_current_document();
1016 g_signal_emit_by_name(geany_object, "project-open", config);
1017 g_key_file_free(config);
1019 update_ui();
1020 return TRUE;
1024 static void apply_editor_prefs(void)
1026 guint i;
1028 foreach_document(i)
1029 editor_apply_update_prefs(documents[i]->editor);
1033 /* Write the project settings as well as the project session files into its configuration files.
1034 * emit_signal defines whether the project-save signal should be emitted. When write_config()
1035 * is called while closing a project, this is used to skip emitting the signal because
1036 * project-close will be emitted afterwards.
1037 * Returns: TRUE if project file was written successfully. */
1038 static gboolean write_config(gboolean emit_signal)
1040 GeanyProject *p;
1041 GKeyFile *config;
1042 gchar *filename;
1043 gchar *data;
1044 gboolean ret = FALSE;
1045 GSList *node;
1047 g_return_val_if_fail(app->project != NULL, FALSE);
1049 p = app->project;
1051 config = g_key_file_new();
1052 /* try to load an existing config to keep manually added comments */
1053 filename = utils_get_locale_from_utf8(p->file_name);
1054 g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL);
1056 foreach_slist(node, stash_groups)
1057 stash_group_save_to_key_file(node->data, config);
1059 g_key_file_set_string(config, "project", "name", p->name);
1060 g_key_file_set_string(config, "project", "base_path", p->base_path);
1062 if (p->description)
1063 g_key_file_set_string(config, "project", "description", p->description);
1064 if (p->file_patterns)
1065 g_key_file_set_string_list(config, "project", "file_patterns",
1066 (const gchar**) p->file_patterns, g_strv_length(p->file_patterns));
1068 g_key_file_set_integer(config, "long line marker", "long_line_behaviour", p->priv->long_line_behaviour);
1069 g_key_file_set_integer(config, "long line marker", "long_line_column", p->priv->long_line_column);
1071 /* store the session files into the project too */
1072 if (project_prefs.project_session)
1073 configuration_save_session_files(config);
1074 build_save_menu(config, (gpointer)p, GEANY_BCS_PROJ);
1075 if (emit_signal)
1077 g_signal_emit_by_name(geany_object, "project-save", config);
1079 /* write the file */
1080 data = g_key_file_to_data(config, NULL, NULL);
1081 ret = (utils_write_file(filename, data) == 0);
1083 g_free(data);
1084 g_free(filename);
1085 g_key_file_free(config);
1087 return ret;
1091 /* Constructs the project's base path which is used for "Make all" and "Execute".
1092 * The result is an absolute string in UTF-8 encoding which is either the same as
1093 * base path if it is absolute or it is built out of project file name's dir and base_path.
1094 * If there is no project or project's base_path is invalid, NULL will be returned.
1095 * The returned string should be freed when no longer needed. */
1096 gchar *project_get_base_path(void)
1098 GeanyProject *project = app->project;
1100 if (project && !EMPTY(project->base_path))
1102 if (g_path_is_absolute(project->base_path))
1103 return g_strdup(project->base_path);
1104 else
1105 { /* build base_path out of project file name's dir and base_path */
1106 gchar *path;
1107 gchar *dir = g_path_get_dirname(project->file_name);
1109 if (utils_str_equal(project->base_path, "./"))
1110 return dir;
1112 path = g_build_filename(dir, project->base_path, NULL);
1113 g_free(dir);
1114 return path;
1117 return NULL;
1121 /* This is to save project-related global settings, NOT project file settings. */
1122 void project_save_prefs(GKeyFile *config)
1124 GeanyProject *project = app->project;
1126 if (cl_options.load_session)
1128 const gchar *utf8_filename = (project == NULL) ? "" : project->file_name;
1130 g_key_file_set_string(config, "project", "session_file", utf8_filename);
1132 g_key_file_set_string(config, "project", "project_file_path",
1133 FALLBACK(local_prefs.project_file_path, ""));
1137 void project_load_prefs(GKeyFile *config)
1139 if (cl_options.load_session)
1141 g_return_if_fail(project_prefs.session_file == NULL);
1142 project_prefs.session_file = utils_get_setting_string(config, "project",
1143 "session_file", "");
1145 local_prefs.project_file_path = utils_get_setting_string(config, "project",
1146 "project_file_path", NULL);
1147 if (local_prefs.project_file_path == NULL)
1149 local_prefs.project_file_path = g_build_filename(g_get_home_dir(), PROJECT_DIR, NULL);
1154 /* Initialize project-related preferences in the Preferences dialog. */
1155 void project_setup_prefs(void)
1157 GtkWidget *path_entry = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_entry");
1158 GtkWidget *path_btn = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_button");
1159 static gboolean callback_setup = FALSE;
1161 g_return_if_fail(local_prefs.project_file_path != NULL);
1163 gtk_entry_set_text(GTK_ENTRY(path_entry), local_prefs.project_file_path);
1164 if (! callback_setup)
1165 { /* connect the callback only once */
1166 callback_setup = TRUE;
1167 ui_setup_open_button_callback(path_btn, NULL,
1168 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(path_entry));
1173 /* Update project-related preferences after using the Preferences dialog. */
1174 void project_apply_prefs(void)
1176 GtkWidget *path_entry = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_entry");
1177 const gchar *str;
1179 str = gtk_entry_get_text(GTK_ENTRY(path_entry));
1180 SETPTR(local_prefs.project_file_path, g_strdup(str));
1184 static void add_stash_group(StashGroup *group)
1186 stash_groups = g_slist_prepend(stash_groups, group);
1190 static void init_stash_prefs(void)
1192 StashGroup *group;
1193 GKeyFile *kf;
1195 group = stash_group_new("indentation");
1196 /* copy global defaults */
1197 indentation = *editor_get_indent_prefs(NULL);
1198 stash_group_set_use_defaults(group, FALSE);
1199 add_stash_group(group);
1201 stash_group_add_spin_button_integer(group, &indentation.width,
1202 "indent_width", 4, "spin_indent_width_project");
1203 stash_group_add_radio_buttons(group, (gint*)(gpointer)&indentation.type,
1204 "indent_type", GEANY_INDENT_TYPE_TABS,
1205 "radio_indent_spaces_project", GEANY_INDENT_TYPE_SPACES,
1206 "radio_indent_tabs_project", GEANY_INDENT_TYPE_TABS,
1207 "radio_indent_both_project", GEANY_INDENT_TYPE_BOTH,
1208 NULL);
1209 /* This is a 'hidden' pref for backwards-compatibility */
1210 stash_group_add_integer(group, &indentation.hard_tab_width,
1211 "indent_hard_tab_width", 8);
1212 stash_group_add_toggle_button(group, &indentation.detect_type,
1213 "detect_indent", FALSE, "check_detect_indent_type_project");
1214 stash_group_add_toggle_button(group, &indentation.detect_width,
1215 "detect_indent_width", FALSE, "check_detect_indent_width_project");
1216 stash_group_add_combo_box(group, (gint*)(gpointer)&indentation.auto_indent_mode,
1217 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode_project");
1219 group = stash_group_new("file_prefs");
1220 stash_group_add_toggle_button(group, &priv.final_new_line,
1221 "final_new_line", file_prefs.final_new_line, "check_new_line1");
1222 stash_group_add_toggle_button(group, &priv.ensure_convert_new_lines,
1223 "ensure_convert_new_lines", file_prefs.ensure_convert_new_lines, "check_ensure_convert_new_lines1");
1224 stash_group_add_toggle_button(group, &priv.strip_trailing_spaces,
1225 "strip_trailing_spaces", file_prefs.strip_trailing_spaces, "check_trailing_spaces1");
1226 stash_group_add_toggle_button(group, &priv.replace_tabs,
1227 "replace_tabs", file_prefs.replace_tabs, "check_replace_tabs1");
1228 add_stash_group(group);
1229 /* apply defaults */
1230 kf = g_key_file_new();
1231 stash_group_load_from_key_file(group, kf);
1232 g_key_file_free(kf);
1236 #define COPY_PREF(dest, prefname)\
1237 (dest.prefname = priv.prefname)
1239 const GeanyFilePrefs *project_get_file_prefs(void)
1241 static GeanyFilePrefs fp;
1243 if (!app->project)
1244 return &file_prefs;
1246 fp = file_prefs;
1247 COPY_PREF(fp, final_new_line);
1248 COPY_PREF(fp, ensure_convert_new_lines);
1249 COPY_PREF(fp, strip_trailing_spaces);
1250 COPY_PREF(fp, replace_tabs);
1251 return &fp;
1255 void project_init(void)
1260 void project_finalize(void)