Fix relative project base path when creating a new project
[geany-mirror.git] / src / project.c
blob5ae97edde37a3d375254a4dbe551916167e9bf31
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"
47 #include "win32.h"
49 #include <string.h>
50 #include <unistd.h>
51 #include <errno.h>
54 ProjectPrefs project_prefs = { NULL, FALSE, FALSE };
57 static GeanyProjectPrivate priv;
58 static GeanyIndentPrefs indentation;
60 static GSList *stash_groups = NULL;
62 static struct
64 gchar *project_file_path; /* in UTF-8 */
65 } local_prefs = { NULL };
67 /* simple struct to keep references to the elements of the properties dialog */
68 typedef struct _PropertyDialogElements
70 GtkWidget *dialog;
71 GtkWidget *notebook;
72 GtkWidget *name;
73 GtkWidget *description;
74 GtkWidget *file_name;
75 GtkWidget *base_path;
76 GtkWidget *patterns;
77 BuildTableData build_properties;
78 gint build_page_num;
79 gboolean entries_modified;
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 = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, FALSE };
116 if (! project_ask_close())
117 return;
119 g_return_if_fail(app->project == NULL);
121 e.dialog = gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(main_widgets.window),
122 GTK_DIALOG_DESTROY_WITH_PARENT,
123 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
125 gtk_widget_set_name(e.dialog, "GeanyDialogProject");
126 button = ui_button_new_with_image(GTK_STOCK_NEW, _("C_reate"));
127 gtk_widget_set_can_default(button, TRUE);
128 gtk_window_set_default(GTK_WINDOW(e.dialog), button);
129 gtk_dialog_add_action_widget(GTK_DIALOG(e.dialog), button, GTK_RESPONSE_OK);
131 vbox = ui_dialog_vbox_new(GTK_DIALOG(e.dialog));
133 table = gtk_table_new(3, 2, FALSE);
134 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
135 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
137 label = gtk_label_new(_("Name:"));
138 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
140 e.name = gtk_entry_new();
141 gtk_entry_set_activates_default(GTK_ENTRY(e.name), TRUE);
142 ui_entry_add_clear_icon(GTK_ENTRY(e.name));
143 gtk_entry_set_max_length(GTK_ENTRY(e.name), MAX_NAME_LEN);
144 gtk_widget_set_tooltip_text(e.name, _("Project name"));
146 ui_table_add_row(GTK_TABLE(table), 0, label, e.name, NULL);
148 label = gtk_label_new(_("Filename:"));
149 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
151 e.file_name = gtk_entry_new();
152 gtk_entry_set_activates_default(GTK_ENTRY(e.file_name), TRUE);
153 ui_entry_add_clear_icon(GTK_ENTRY(e.file_name));
154 gtk_entry_set_width_chars(GTK_ENTRY(e.file_name), 30);
155 tooltip = g_strdup_printf(
156 _("Path of the file representing the project and storing its settings. "
157 "It should normally have the \"%s\" extension."), "."GEANY_PROJECT_EXT);
158 gtk_widget_set_tooltip_text(e.file_name, tooltip);
159 g_free(tooltip);
160 button = gtk_button_new();
161 g_signal_connect(button, "clicked", G_CALLBACK(on_file_save_button_clicked), &e);
162 image = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
163 gtk_container_add(GTK_CONTAINER(button), image);
164 bbox = gtk_hbox_new(FALSE, 6);
165 gtk_box_pack_start(GTK_BOX(bbox), e.file_name, TRUE, TRUE, 0);
166 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
168 ui_table_add_row(GTK_TABLE(table), 1, label, bbox, NULL);
170 label = gtk_label_new(_("Base path:"));
171 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
173 e.base_path = gtk_entry_new();
174 gtk_entry_set_activates_default(GTK_ENTRY(e.base_path), TRUE);
175 ui_entry_add_clear_icon(GTK_ENTRY(e.base_path));
176 gtk_widget_set_tooltip_text(e.base_path,
177 _("Base directory of all files that make up the project. "
178 "This can be a new path, or an existing directory tree. "
179 "You can use paths relative to the project filename."));
180 bbox = ui_path_box_new(_("Choose Project Base Path"),
181 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(e.base_path));
183 ui_table_add_row(GTK_TABLE(table), 2, label, bbox, NULL);
185 gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
187 /* signals */
188 g_signal_connect(e.name, "changed", G_CALLBACK(on_name_entry_changed), &e);
189 /* run the callback manually to initialise the base_path and file_name fields */
190 on_name_entry_changed(GTK_EDITABLE(e.name), &e);
192 g_signal_connect(e.file_name, "changed", G_CALLBACK(on_entries_changed), &e);
193 g_signal_connect(e.base_path, "changed", G_CALLBACK(on_entries_changed), &e);
195 gtk_widget_show_all(e.dialog);
197 while (gtk_dialog_run(GTK_DIALOG(e.dialog)) == GTK_RESPONSE_OK)
199 if (update_config(&e, TRUE))
201 if (!write_config(TRUE))
202 SHOW_ERR(_("Project file could not be written"));
203 else
205 ui_set_statusbar(TRUE, _("Project \"%s\" created."), app->project->name);
207 ui_add_recent_project_file(app->project->file_name);
208 break;
212 gtk_widget_destroy(e.dialog);
216 gboolean project_load_file_with_session(const gchar *locale_file_name)
218 if (project_load_file(locale_file_name))
220 if (project_prefs.project_session)
222 configuration_open_files();
223 /* open a new file if no other file was opened */
224 document_new_file_if_non_open();
225 ui_focus_current_document();
227 return TRUE;
229 return FALSE;
233 #ifndef G_OS_WIN32
234 static void run_open_dialog(GtkDialog *dialog)
236 while (gtk_dialog_run(dialog) == GTK_RESPONSE_ACCEPT)
238 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
240 /* try to load the config */
241 if (! project_load_file_with_session(filename))
243 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
245 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), utf8_filename);
246 gtk_widget_grab_focus(GTK_WIDGET(dialog));
247 g_free(utf8_filename);
248 g_free(filename);
249 continue;
251 g_free(filename);
252 break;
255 #endif
258 void project_open(void)
260 const gchar *dir = local_prefs.project_file_path;
261 #ifdef G_OS_WIN32
262 gchar *file;
263 #else
264 GtkWidget *dialog;
265 GtkFileFilter *filter;
266 gchar *locale_path;
267 #endif
268 if (! project_ask_close()) return;
270 #ifdef G_OS_WIN32
271 file = win32_show_project_open_dialog(main_widgets.window, _("Open Project"), dir, FALSE, TRUE);
272 if (file != NULL)
274 /* try to load the config */
275 if (! project_load_file_with_session(file))
277 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), file);
279 g_free(file);
281 #else
283 dialog = gtk_file_chooser_dialog_new(_("Open Project"), GTK_WINDOW(main_widgets.window),
284 GTK_FILE_CHOOSER_ACTION_OPEN,
285 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
286 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
287 gtk_widget_set_name(dialog, "GeanyDialogProject");
289 /* set default Open, so pressing enter can open multiple files */
290 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
291 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
292 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
293 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
294 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
295 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
297 /* add FileFilters */
298 filter = gtk_file_filter_new();
299 gtk_file_filter_set_name(filter, _("All files"));
300 gtk_file_filter_add_pattern(filter, "*");
301 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
302 filter = gtk_file_filter_new();
303 gtk_file_filter_set_name(filter, _("Project files"));
304 gtk_file_filter_add_pattern(filter, "*." GEANY_PROJECT_EXT);
305 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
306 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
308 locale_path = utils_get_locale_from_utf8(dir);
309 if (g_file_test(locale_path, G_FILE_TEST_EXISTS) &&
310 g_file_test(locale_path, G_FILE_TEST_IS_DIR))
312 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
314 g_free(locale_path);
316 gtk_widget_show_all(dialog);
317 run_open_dialog(GTK_DIALOG(dialog));
318 gtk_widget_destroy(GTK_WIDGET(dialog));
319 #endif
323 /* Called when creating, opening, closing and updating projects. */
324 static void update_ui(void)
326 if (main_status.quitting)
327 return;
329 ui_set_window_title(NULL);
330 build_menu_update(NULL);
331 sidebar_openfiles_update_all();
335 static void remove_foreach_project_filetype(gpointer data, gpointer user_data)
337 GeanyFiletype *ft = data;
338 if (ft != NULL)
340 SETPTR(ft->priv->projfilecmds, NULL);
341 SETPTR(ft->priv->projexeccmds, NULL);
342 SETPTR(ft->priv->projerror_regex_string, NULL);
343 ft->priv->project_list_entry = -1;
348 /* open_default will make function reload default session files on close */
349 void project_close(gboolean open_default)
351 GSList *node;
353 g_return_if_fail(app->project != NULL);
355 /* save project session files, etc */
356 if (!write_config(FALSE))
357 g_warning("Project file \"%s\" could not be written", app->project->file_name);
359 if (project_prefs.project_session)
361 /* close all existing tabs first */
362 if (!document_close_all())
363 return;
365 ui_set_statusbar(TRUE, _("Project \"%s\" closed."), app->project->name);
367 /* remove project filetypes build entries */
368 if (app->project->priv->build_filetypes_list != NULL)
370 g_ptr_array_foreach(app->project->priv->build_filetypes_list, remove_foreach_project_filetype, NULL);
371 g_ptr_array_free(app->project->priv->build_filetypes_list, FALSE);
374 /* remove project non filetype build menu items */
375 build_remove_menu_item(GEANY_BCS_PROJ, GEANY_GBG_NON_FT, -1);
376 build_remove_menu_item(GEANY_BCS_PROJ, GEANY_GBG_EXEC, -1);
378 g_free(app->project->name);
379 g_free(app->project->description);
380 g_free(app->project->file_name);
381 g_free(app->project->base_path);
383 g_free(app->project);
384 app->project = NULL;
386 foreach_slist(node, stash_groups)
387 stash_group_free(node->data);
389 g_slist_free(stash_groups);
390 stash_groups = NULL;
392 apply_editor_prefs(); /* ensure that global settings are restored */
394 if (project_prefs.project_session)
396 /* after closing all tabs let's open the tabs found in the default config */
397 if (open_default && cl_options.load_session)
399 configuration_reload_default_session();
400 configuration_open_files();
401 /* open a new file if no other file was opened */
402 document_new_file_if_non_open();
403 ui_focus_current_document();
406 g_signal_emit_by_name(geany_object, "project-close");
408 update_ui();
412 /* Shows the file chooser dialog when base path button is clicked
413 * FIXME: this should be connected in Glade but 3.8.1 has a bug
414 * where it won't pass any objects as user data (#588824). */
415 G_MODULE_EXPORT void
416 on_project_properties_base_path_button_clicked(GtkWidget *button,
417 GtkWidget *base_path_entry)
419 GtkWidget *dialog;
421 g_return_if_fail(base_path_entry != NULL);
422 g_return_if_fail(GTK_IS_WIDGET(base_path_entry));
424 dialog = gtk_file_chooser_dialog_new(_("Choose Project Base Path"),
425 NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
426 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
427 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
428 NULL);
430 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
432 gtk_entry_set_text(GTK_ENTRY(base_path_entry),
433 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
436 gtk_widget_destroy(dialog);
440 static void insert_build_page(PropertyDialogElements *e)
442 GtkWidget *build_table, *label;
443 GeanyDocument *doc = document_get_current();
444 GeanyFiletype *ft = NULL;
446 if (doc != NULL)
447 ft = doc->file_type;
449 build_table = build_commands_table(doc, GEANY_BCS_PROJ, &(e->build_properties), ft);
450 gtk_container_set_border_width(GTK_CONTAINER(build_table), 6);
451 label = gtk_label_new(_("Build"));
452 e->build_page_num = gtk_notebook_append_page(GTK_NOTEBOOK(e->notebook),
453 build_table, label);
457 static void create_properties_dialog(PropertyDialogElements *e)
459 GtkWidget *base_path_button;
460 static guint base_path_button_handler_id = 0;
461 static guint radio_long_line_handler_id = 0;
463 e->dialog = create_project_dialog();
464 e->notebook = ui_lookup_widget(e->dialog, "project_notebook");
465 e->file_name = ui_lookup_widget(e->dialog, "label_project_dialog_filename");
466 e->name = ui_lookup_widget(e->dialog, "entry_project_dialog_name");
467 e->description = ui_lookup_widget(e->dialog, "textview_project_dialog_description");
468 e->base_path = ui_lookup_widget(e->dialog, "entry_project_dialog_base_path");
469 e->patterns = ui_lookup_widget(e->dialog, "entry_project_dialog_file_patterns");
471 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
473 ui_entry_add_clear_icon(GTK_ENTRY(e->name));
474 ui_entry_add_clear_icon(GTK_ENTRY(e->base_path));
475 ui_entry_add_clear_icon(GTK_ENTRY(e->patterns));
477 /* Workaround for bug in Glade 3.8.1, see comment above signal handler */
478 if (base_path_button_handler_id == 0)
480 base_path_button = ui_lookup_widget(e->dialog, "button_project_dialog_base_path");
481 base_path_button_handler_id =
482 g_signal_connect(base_path_button, "clicked",
483 G_CALLBACK(on_project_properties_base_path_button_clicked),
484 e->base_path);
487 /* Same as above, should be in Glade but can't due to bug in 3.8.1 */
488 if (radio_long_line_handler_id == 0)
490 radio_long_line_handler_id =
491 g_signal_connect(ui_lookup_widget(e->dialog,
492 "radio_long_line_custom_project"), "toggled",
493 G_CALLBACK(on_radio_long_line_custom_toggled),
494 ui_lookup_widget(e->dialog, "spin_long_line_project"));
499 static void show_project_properties(gboolean show_build)
501 GeanyProject *p = app->project;
502 GtkWidget *widget = NULL;
503 GtkWidget *radio_long_line_custom;
504 static PropertyDialogElements e;
505 GSList *node;
507 g_return_if_fail(app->project != NULL);
509 if (e.dialog == NULL)
510 create_properties_dialog(&e);
512 insert_build_page(&e);
514 foreach_slist(node, stash_groups)
515 stash_group_display(node->data, e.dialog);
517 /* fill the elements with the appropriate data */
518 gtk_entry_set_text(GTK_ENTRY(e.name), p->name);
519 gtk_label_set_text(GTK_LABEL(e.file_name), p->file_name);
520 gtk_entry_set_text(GTK_ENTRY(e.base_path), p->base_path);
522 radio_long_line_custom = ui_lookup_widget(e.dialog, "radio_long_line_custom_project");
523 switch (p->priv->long_line_behaviour)
525 case 0: widget = ui_lookup_widget(e.dialog, "radio_long_line_disabled_project"); break;
526 case 1: widget = ui_lookup_widget(e.dialog, "radio_long_line_default_project"); break;
527 case 2: widget = radio_long_line_custom; break;
529 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
531 widget = ui_lookup_widget(e.dialog, "spin_long_line_project");
532 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), (gdouble)p->priv->long_line_column);
533 on_radio_long_line_custom_toggled(GTK_TOGGLE_BUTTON(radio_long_line_custom), widget);
535 if (p->description != NULL)
536 { /* set text */
537 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e.description));
538 gtk_text_buffer_set_text(buffer, p->description, -1);
541 if (p->file_patterns != NULL)
542 { /* set the file patterns */
543 gchar *str;
545 str = g_strjoinv(" ", p->file_patterns);
546 gtk_entry_set_text(GTK_ENTRY(e.patterns), str);
547 g_free(str);
550 g_signal_emit_by_name(geany_object, "project-dialog-open", e.notebook);
551 gtk_widget_show_all(e.dialog);
553 /* note: notebook page must be shown before setting current page */
554 if (show_build)
555 gtk_notebook_set_current_page(GTK_NOTEBOOK(e.notebook), e.build_page_num);
556 else
557 gtk_notebook_set_current_page(GTK_NOTEBOOK(e.notebook), 0);
559 while (gtk_dialog_run(GTK_DIALOG(e.dialog)) == GTK_RESPONSE_OK)
561 if (update_config(&e, FALSE))
563 g_signal_emit_by_name(geany_object, "project-dialog-confirmed", e.notebook);
564 if (!write_config(TRUE))
565 SHOW_ERR(_("Project file could not be written"));
566 else
568 ui_set_statusbar(TRUE, _("Project \"%s\" saved."), app->project->name);
569 break;
574 build_free_fields(e.build_properties);
575 g_signal_emit_by_name(geany_object, "project-dialog-close", e.notebook);
576 gtk_notebook_remove_page(GTK_NOTEBOOK(e.notebook), e.build_page_num);
577 gtk_widget_hide(e.dialog);
581 void project_properties(void)
583 show_project_properties(FALSE);
587 void project_build_properties(void)
589 show_project_properties(TRUE);
593 /* checks whether there is an already open project and asks the user if he wants to close it or
594 * abort the current action. Returns FALSE when the current action(the caller) should be cancelled
595 * and TRUE if we can go ahead */
596 gboolean project_ask_close(void)
598 if (app->project != NULL)
600 if (dialogs_show_question_full(NULL, GTK_STOCK_CLOSE, GTK_STOCK_CANCEL,
601 _("Do you want to close it before proceeding?"),
602 _("The '%s' project is open."), app->project->name))
604 project_close(FALSE);
605 return TRUE;
607 else
608 return FALSE;
610 else
611 return TRUE;
615 static GeanyProject *create_project(void)
617 GeanyProject *project = g_new0(GeanyProject, 1);
619 memset(&priv, 0, sizeof priv);
620 priv.indentation = &indentation;
621 project->priv = &priv;
623 init_stash_prefs();
625 project->file_patterns = NULL;
627 project->priv->long_line_behaviour = 1 /* use global settings */;
628 project->priv->long_line_column = editor_prefs.long_line_column;
630 app->project = project;
631 return project;
635 /* Verifies data for New & Properties dialogs.
636 * Returns: FALSE if the user needs to change any data. */
637 static gboolean update_config(const PropertyDialogElements *e, gboolean new_project)
639 const gchar *name, *file_name, *base_path;
640 gchar *locale_filename;
641 gsize name_len;
642 gint err_code = 0;
643 GeanyProject *p;
645 g_return_val_if_fail(e != NULL, TRUE);
647 name = gtk_entry_get_text(GTK_ENTRY(e->name));
648 name_len = strlen(name);
649 if (name_len == 0)
651 SHOW_ERR(_("The specified project name is too short."));
652 gtk_widget_grab_focus(e->name);
653 return FALSE;
655 else if (name_len > MAX_NAME_LEN)
657 SHOW_ERR1(_("The specified project name is too long (max. %d characters)."), MAX_NAME_LEN);
658 gtk_widget_grab_focus(e->name);
659 return FALSE;
662 if (new_project)
663 file_name = gtk_entry_get_text(GTK_ENTRY(e->file_name));
664 else
665 file_name = gtk_label_get_text(GTK_LABEL(e->file_name));
667 if (G_UNLIKELY(EMPTY(file_name)))
669 SHOW_ERR(_("You have specified an invalid project filename."));
670 gtk_widget_grab_focus(e->file_name);
671 return FALSE;
674 locale_filename = utils_get_locale_from_utf8(file_name);
675 base_path = gtk_entry_get_text(GTK_ENTRY(e->base_path));
676 if (!EMPTY(base_path))
677 { /* check whether the given directory actually exists */
678 gchar *locale_path = utils_get_locale_from_utf8(base_path);
680 if (! g_path_is_absolute(locale_path))
681 { /* relative base path, so add base dir of project file name */
682 gchar *dir = g_path_get_dirname(locale_filename);
683 SETPTR(locale_path, g_build_filename(dir, locale_path, NULL));
684 g_free(dir);
687 if (! g_file_test(locale_path, G_FILE_TEST_IS_DIR))
689 gboolean create_dir;
691 create_dir = dialogs_show_question_full(NULL, GTK_STOCK_OK, GTK_STOCK_CANCEL,
692 _("Create the project's base path directory?"),
693 _("The path \"%s\" does not exist."),
694 base_path);
696 if (create_dir)
697 err_code = utils_mkdir(locale_path, TRUE);
699 if (! create_dir || err_code != 0)
701 if (err_code != 0)
702 SHOW_ERR1(_("Project base directory could not be created (%s)."),
703 g_strerror(err_code));
704 gtk_widget_grab_focus(e->base_path);
705 utils_free_pointers(2, locale_path, locale_filename, NULL);
706 return FALSE;
709 g_free(locale_path);
711 /* finally test whether the given project file can be written */
712 if ((err_code = utils_is_file_writable(locale_filename)) != 0 ||
713 (err_code = g_file_test(locale_filename, G_FILE_TEST_IS_DIR) ? EISDIR : 0) != 0)
715 SHOW_ERR1(_("Project file could not be written (%s)."), g_strerror(err_code));
716 gtk_widget_grab_focus(e->file_name);
717 g_free(locale_filename);
718 return FALSE;
720 else if (new_project && g_file_test(locale_filename, G_FILE_TEST_EXISTS) &&
721 ! dialogs_show_question_full(NULL, _("_Replace"), GTK_STOCK_CANCEL,
722 NULL,
723 _("The file '%s' already exists. Do you want to overwrite it?"),
724 file_name))
726 gtk_widget_grab_focus(e->file_name);
727 g_free(locale_filename);
728 return FALSE;
730 g_free(locale_filename);
732 if (app->project == NULL)
734 create_project();
735 new_project = TRUE;
737 p = app->project;
739 SETPTR(p->name, g_strdup(name));
740 SETPTR(p->file_name, g_strdup(file_name));
741 /* use "." if base_path is empty */
742 SETPTR(p->base_path, g_strdup(!EMPTY(base_path) ? base_path : "./"));
744 if (! new_project) /* save properties specific fields */
746 GtkTextIter start, end;
747 GtkTextBuffer *buffer;
748 GeanyDocument *doc = document_get_current();
749 GeanyBuildCommand *oldvalue;
750 GeanyFiletype *ft = doc ? doc->file_type : NULL;
751 GtkWidget *widget;
752 gchar *tmp;
753 GString *str;
754 GSList *node;
756 /* get and set the project description */
757 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->description));
758 gtk_text_buffer_get_start_iter(buffer, &start);
759 gtk_text_buffer_get_end_iter(buffer, &end);
760 SETPTR(p->description, g_strdup(gtk_text_buffer_get_text(buffer, &start, &end, FALSE)));
762 foreach_slist(node, stash_groups)
763 stash_group_update(node->data, e->dialog);
765 /* read the project build menu */
766 oldvalue = ft ? ft->priv->projfilecmds : NULL;
767 build_read_project(ft, e->build_properties);
769 if (ft != NULL && ft->priv->projfilecmds != oldvalue && ft->priv->project_list_entry < 0)
771 if (p->priv->build_filetypes_list == NULL)
772 p->priv->build_filetypes_list = g_ptr_array_new();
773 ft->priv->project_list_entry = p->priv->build_filetypes_list->len;
774 g_ptr_array_add(p->priv->build_filetypes_list, ft);
776 build_menu_update(doc);
778 widget = ui_lookup_widget(e->dialog, "radio_long_line_disabled_project");
779 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
780 p->priv->long_line_behaviour = 0;
781 else
783 widget = ui_lookup_widget(e->dialog, "radio_long_line_default_project");
784 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
785 p->priv->long_line_behaviour = 1;
786 else
787 /* "Custom" radio button must be checked */
788 p->priv->long_line_behaviour = 2;
791 widget = ui_lookup_widget(e->dialog, "spin_long_line_project");
792 p->priv->long_line_column = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
793 apply_editor_prefs();
795 /* get and set the project file patterns */
796 tmp = g_strdup(gtk_entry_get_text(GTK_ENTRY(e->patterns)));
797 g_strfreev(p->file_patterns);
798 g_strstrip(tmp);
799 str = g_string_new(tmp);
800 do {} while (utils_string_replace_all(str, " ", " "));
801 p->file_patterns = g_strsplit(str->str, " ", -1);
802 g_string_free(str, TRUE);
803 g_free(tmp);
806 update_ui();
808 return TRUE;
812 #ifndef G_OS_WIN32
813 static void run_dialog(GtkWidget *dialog, GtkWidget *entry)
815 /* set filename in the file chooser dialog */
816 const gchar *utf8_filename = gtk_entry_get_text(GTK_ENTRY(entry));
817 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
819 if (g_path_is_absolute(locale_filename))
821 if (g_file_test(locale_filename, G_FILE_TEST_EXISTS))
823 /* if the current filename is a directory, we must use
824 * gtk_file_chooser_set_current_folder(which expects a locale filename) otherwise
825 * we end up in the parent directory */
826 if (g_file_test(locale_filename, G_FILE_TEST_IS_DIR))
827 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_filename);
828 else
829 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), utf8_filename);
831 else /* if the file doesn't yet exist, use at least the current directory */
833 gchar *locale_dir = g_path_get_dirname(locale_filename);
834 gchar *name = g_path_get_basename(utf8_filename);
836 if (g_file_test(locale_dir, G_FILE_TEST_EXISTS))
837 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dir);
838 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), name);
840 g_free(name);
841 g_free(locale_dir);
844 else if (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog)) != GTK_FILE_CHOOSER_ACTION_OPEN)
846 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), utf8_filename);
848 g_free(locale_filename);
850 /* run it */
851 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
853 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
854 gchar *tmp_utf8_filename = utils_get_utf8_from_locale(filename);
856 gtk_entry_set_text(GTK_ENTRY(entry), tmp_utf8_filename);
858 g_free(tmp_utf8_filename);
859 g_free(filename);
861 gtk_widget_destroy(dialog);
863 #endif
866 static void on_file_save_button_clicked(GtkButton *button, PropertyDialogElements *e)
868 #ifdef G_OS_WIN32
869 gchar *path = win32_show_project_open_dialog(e->dialog, _("Choose Project Filename"),
870 gtk_entry_get_text(GTK_ENTRY(e->file_name)), TRUE, TRUE);
871 if (path != NULL)
873 gtk_entry_set_text(GTK_ENTRY(e->file_name), path);
874 g_free(path);
876 #else
877 GtkWidget *dialog;
879 /* initialise the dialog */
880 dialog = gtk_file_chooser_dialog_new(_("Choose Project Filename"), NULL,
881 GTK_FILE_CHOOSER_ACTION_SAVE,
882 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
883 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
884 gtk_widget_set_name(dialog, "GeanyDialogProject");
885 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
886 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
887 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
888 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
890 run_dialog(dialog, e->file_name);
891 #endif
895 /* sets the project base path and the project file name according to the project name */
896 static void on_name_entry_changed(GtkEditable *editable, PropertyDialogElements *e)
898 gchar *base_path;
899 gchar *file_name;
900 gchar *name;
901 const gchar *project_dir = local_prefs.project_file_path;
903 if (e->entries_modified)
904 return;
906 name = gtk_editable_get_chars(editable, 0, -1);
907 if (!EMPTY(name))
909 base_path = g_strconcat(project_dir, G_DIR_SEPARATOR_S,
910 name, G_DIR_SEPARATOR_S, NULL);
911 if (project_prefs.project_file_in_basedir)
912 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S, name, G_DIR_SEPARATOR_S,
913 name, "." GEANY_PROJECT_EXT, NULL);
914 else
915 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S,
916 name, "." GEANY_PROJECT_EXT, NULL);
918 else
920 base_path = g_strconcat(project_dir, G_DIR_SEPARATOR_S, NULL);
921 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S, NULL);
923 g_free(name);
925 gtk_entry_set_text(GTK_ENTRY(e->base_path), base_path);
926 gtk_entry_set_text(GTK_ENTRY(e->file_name), file_name);
928 e->entries_modified = FALSE;
930 g_free(base_path);
931 g_free(file_name);
935 static void on_entries_changed(GtkEditable *editable, PropertyDialogElements *e)
937 e->entries_modified = TRUE;
941 static void on_radio_long_line_custom_toggled(GtkToggleButton *radio, GtkWidget *spin_long_line)
943 gtk_widget_set_sensitive(spin_long_line, gtk_toggle_button_get_active(radio));
947 gboolean project_load_file(const gchar *locale_file_name)
949 g_return_val_if_fail(locale_file_name != NULL, FALSE);
951 if (load_config(locale_file_name))
953 gchar *utf8_filename = utils_get_utf8_from_locale(locale_file_name);
955 ui_set_statusbar(TRUE, _("Project \"%s\" opened."), app->project->name);
957 ui_add_recent_project_file(utf8_filename);
958 g_free(utf8_filename);
959 return TRUE;
961 else
963 gchar *utf8_filename = utils_get_utf8_from_locale(locale_file_name);
965 ui_set_statusbar(TRUE, _("Project file \"%s\" could not be loaded."), utf8_filename);
966 g_free(utf8_filename);
968 return FALSE;
972 /* Reads the given filename and creates a new project with the data found in the file.
973 * At this point there should not be an already opened project in Geany otherwise it will just
974 * return.
975 * The filename is expected in the locale encoding. */
976 static gboolean load_config(const gchar *filename)
978 GKeyFile *config;
979 GeanyProject *p;
980 GSList *node;
982 /* there should not be an open project */
983 g_return_val_if_fail(app->project == NULL && filename != NULL, FALSE);
985 config = g_key_file_new();
986 if (! g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL))
988 g_key_file_free(config);
989 return FALSE;
992 p = create_project();
994 foreach_slist(node, stash_groups)
995 stash_group_load_from_key_file(node->data, config);
997 p->name = utils_get_setting_string(config, "project", "name", GEANY_STRING_UNTITLED);
998 p->description = utils_get_setting_string(config, "project", "description", "");
999 p->file_name = utils_get_utf8_from_locale(filename);
1000 p->base_path = utils_get_setting_string(config, "project", "base_path", "");
1001 p->file_patterns = g_key_file_get_string_list(config, "project", "file_patterns", NULL, NULL);
1003 p->priv->long_line_behaviour = utils_get_setting_integer(config, "long line marker",
1004 "long_line_behaviour", 1 /* follow global */);
1005 p->priv->long_line_column = utils_get_setting_integer(config, "long line marker",
1006 "long_line_column", editor_prefs.long_line_column);
1007 apply_editor_prefs();
1009 build_load_menu(config, GEANY_BCS_PROJ, (gpointer)p);
1010 if (project_prefs.project_session)
1012 /* save current (non-project) session (it could has been changed since program startup) */
1013 configuration_save_default_session();
1014 /* now close all open files */
1015 document_close_all();
1016 /* read session files so they can be opened with configuration_open_files() */
1017 configuration_load_session_files(config, FALSE);
1018 ui_focus_current_document();
1020 g_signal_emit_by_name(geany_object, "project-open", config);
1021 g_key_file_free(config);
1023 update_ui();
1024 return TRUE;
1028 static void apply_editor_prefs(void)
1030 guint i;
1032 foreach_document(i)
1033 editor_apply_update_prefs(documents[i]->editor);
1037 /* Write the project settings as well as the project session files into its configuration files.
1038 * emit_signal defines whether the project-save signal should be emitted. When write_config()
1039 * is called while closing a project, this is used to skip emitting the signal because
1040 * project-close will be emitted afterwards.
1041 * Returns: TRUE if project file was written successfully. */
1042 static gboolean write_config(gboolean emit_signal)
1044 GeanyProject *p;
1045 GKeyFile *config;
1046 gchar *filename;
1047 gchar *data;
1048 gboolean ret = FALSE;
1049 GSList *node;
1051 g_return_val_if_fail(app->project != NULL, FALSE);
1053 p = app->project;
1055 config = g_key_file_new();
1056 /* try to load an existing config to keep manually added comments */
1057 filename = utils_get_locale_from_utf8(p->file_name);
1058 g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL);
1060 foreach_slist(node, stash_groups)
1061 stash_group_save_to_key_file(node->data, config);
1063 g_key_file_set_string(config, "project", "name", p->name);
1064 g_key_file_set_string(config, "project", "base_path", p->base_path);
1066 if (p->description)
1067 g_key_file_set_string(config, "project", "description", p->description);
1068 if (p->file_patterns)
1069 g_key_file_set_string_list(config, "project", "file_patterns",
1070 (const gchar**) p->file_patterns, g_strv_length(p->file_patterns));
1072 g_key_file_set_integer(config, "long line marker", "long_line_behaviour", p->priv->long_line_behaviour);
1073 g_key_file_set_integer(config, "long line marker", "long_line_column", p->priv->long_line_column);
1075 /* store the session files into the project too */
1076 if (project_prefs.project_session)
1077 configuration_save_session_files(config);
1078 build_save_menu(config, (gpointer)p, GEANY_BCS_PROJ);
1079 if (emit_signal)
1081 g_signal_emit_by_name(geany_object, "project-save", config);
1083 /* write the file */
1084 data = g_key_file_to_data(config, NULL, NULL);
1085 ret = (utils_write_file(filename, data) == 0);
1087 g_free(data);
1088 g_free(filename);
1089 g_key_file_free(config);
1091 return ret;
1095 /* Constructs the project's base path which is used for "Make all" and "Execute".
1096 * The result is an absolute string in UTF-8 encoding which is either the same as
1097 * base path if it is absolute or it is built out of project file name's dir and base_path.
1098 * If there is no project or project's base_path is invalid, NULL will be returned.
1099 * The returned string should be freed when no longer needed. */
1100 gchar *project_get_base_path(void)
1102 GeanyProject *project = app->project;
1104 if (project && !EMPTY(project->base_path))
1106 if (g_path_is_absolute(project->base_path))
1107 return g_strdup(project->base_path);
1108 else
1109 { /* build base_path out of project file name's dir and base_path */
1110 gchar *path;
1111 gchar *dir = g_path_get_dirname(project->file_name);
1113 if (utils_str_equal(project->base_path, "./"))
1114 return dir;
1116 path = g_build_filename(dir, project->base_path, NULL);
1117 g_free(dir);
1118 return path;
1121 return NULL;
1125 /* This is to save project-related global settings, NOT project file settings. */
1126 void project_save_prefs(GKeyFile *config)
1128 GeanyProject *project = app->project;
1130 if (cl_options.load_session)
1132 const gchar *utf8_filename = (project == NULL) ? "" : project->file_name;
1134 g_key_file_set_string(config, "project", "session_file", utf8_filename);
1136 g_key_file_set_string(config, "project", "project_file_path",
1137 FALLBACK(local_prefs.project_file_path, ""));
1141 void project_load_prefs(GKeyFile *config)
1143 if (cl_options.load_session)
1145 g_return_if_fail(project_prefs.session_file == NULL);
1146 project_prefs.session_file = utils_get_setting_string(config, "project",
1147 "session_file", "");
1149 local_prefs.project_file_path = utils_get_setting_string(config, "project",
1150 "project_file_path", NULL);
1151 if (local_prefs.project_file_path == NULL)
1153 local_prefs.project_file_path = g_build_filename(g_get_home_dir(), PROJECT_DIR, NULL);
1158 /* Initialize project-related preferences in the Preferences dialog. */
1159 void project_setup_prefs(void)
1161 GtkWidget *path_entry = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_entry");
1162 GtkWidget *path_btn = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_button");
1163 static gboolean callback_setup = FALSE;
1165 g_return_if_fail(local_prefs.project_file_path != NULL);
1167 gtk_entry_set_text(GTK_ENTRY(path_entry), local_prefs.project_file_path);
1168 if (! callback_setup)
1169 { /* connect the callback only once */
1170 callback_setup = TRUE;
1171 ui_setup_open_button_callback(path_btn, NULL,
1172 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(path_entry));
1177 /* Update project-related preferences after using the Preferences dialog. */
1178 void project_apply_prefs(void)
1180 GtkWidget *path_entry = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_entry");
1181 const gchar *str;
1183 str = gtk_entry_get_text(GTK_ENTRY(path_entry));
1184 SETPTR(local_prefs.project_file_path, g_strdup(str));
1188 static void add_stash_group(StashGroup *group)
1190 stash_groups = g_slist_prepend(stash_groups, group);
1194 static void init_stash_prefs(void)
1196 StashGroup *group;
1197 GKeyFile *kf;
1199 group = stash_group_new("indentation");
1200 /* copy global defaults */
1201 indentation = *editor_get_indent_prefs(NULL);
1202 stash_group_set_use_defaults(group, FALSE);
1203 add_stash_group(group);
1205 stash_group_add_spin_button_integer(group, &indentation.width,
1206 "indent_width", 4, "spin_indent_width_project");
1207 stash_group_add_radio_buttons(group, (gint*)(gpointer)&indentation.type,
1208 "indent_type", GEANY_INDENT_TYPE_TABS,
1209 "radio_indent_spaces_project", GEANY_INDENT_TYPE_SPACES,
1210 "radio_indent_tabs_project", GEANY_INDENT_TYPE_TABS,
1211 "radio_indent_both_project", GEANY_INDENT_TYPE_BOTH,
1212 NULL);
1213 /* This is a 'hidden' pref for backwards-compatibility */
1214 stash_group_add_integer(group, &indentation.hard_tab_width,
1215 "indent_hard_tab_width", 8);
1216 stash_group_add_toggle_button(group, &indentation.detect_type,
1217 "detect_indent", FALSE, "check_detect_indent_type_project");
1218 stash_group_add_toggle_button(group, &indentation.detect_width,
1219 "detect_indent_width", FALSE, "check_detect_indent_width_project");
1220 stash_group_add_combo_box(group, (gint*)(gpointer)&indentation.auto_indent_mode,
1221 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode_project");
1223 group = stash_group_new("file_prefs");
1224 stash_group_add_toggle_button(group, &priv.final_new_line,
1225 "final_new_line", file_prefs.final_new_line, "check_new_line1");
1226 stash_group_add_toggle_button(group, &priv.ensure_convert_new_lines,
1227 "ensure_convert_new_lines", file_prefs.ensure_convert_new_lines, "check_ensure_convert_new_lines1");
1228 stash_group_add_toggle_button(group, &priv.strip_trailing_spaces,
1229 "strip_trailing_spaces", file_prefs.strip_trailing_spaces, "check_trailing_spaces1");
1230 stash_group_add_toggle_button(group, &priv.replace_tabs,
1231 "replace_tabs", file_prefs.replace_tabs, "check_replace_tabs1");
1232 add_stash_group(group);
1233 /* apply defaults */
1234 kf = g_key_file_new();
1235 stash_group_load_from_key_file(group, kf);
1236 g_key_file_free(kf);
1240 #define COPY_PREF(dest, prefname)\
1241 (dest.prefname = priv.prefname)
1243 const GeanyFilePrefs *project_get_file_prefs(void)
1245 static GeanyFilePrefs fp;
1247 if (!app->project)
1248 return &file_prefs;
1250 fp = file_prefs;
1251 COPY_PREF(fp, final_new_line);
1252 COPY_PREF(fp, ensure_convert_new_lines);
1253 COPY_PREF(fp, strip_trailing_spaces);
1254 COPY_PREF(fp, replace_tabs);
1255 return &fp;
1259 void project_init(void)
1264 void project_finalize(void)