Constify some more string pointers in the API (patch by
[geany-mirror.git] / src / keyfile.c
blob3a83baa284b049b9baef3276eb6542aeb7cca8f9
1 /*
2 * keyfile.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-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$
25 * geany.conf preferences file loading and saving.
29 * Session file format:
30 * filename_xx=pos;filetype UID;read only;encoding idx;use_tabs;auto_indent;line_wrapping;filename
33 #include <stdlib.h>
34 #include <string.h>
36 #include "geany.h"
38 #ifdef HAVE_VTE
39 #include <pwd.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42 #endif
44 #include "support.h"
45 #include "keyfile.h"
46 #include "prefs.h"
47 #include "ui_utils.h"
48 #include "utils.h"
49 #include "document.h"
50 #include "filetypes.h"
51 #include "sciwrappers.h"
52 #include "encodings.h"
53 #include "vte.h"
54 #include "main.h"
55 #include "msgwindow.h"
56 #include "search.h"
57 #include "project.h"
58 #include "editor.h"
59 #include "printing.h"
60 #include "templates.h"
61 #include "toolbar.h"
62 #include "stash.h"
65 /* some default settings which are used at the very first start of Geany to fill
66 * the configuration file */
67 #define GEANY_MAX_SYMBOLLIST_HEIGHT 10
68 #define GEANY_MIN_SYMBOLLIST_CHARS 4
69 #define GEANY_MSGWIN_HEIGHT 208
70 #define GEANY_DISK_CHECK_TIMEOUT 30
71 #define GEANY_DEFAULT_TOOLS_MAKE "make"
72 #ifdef G_OS_WIN32
73 #define GEANY_DEFAULT_TOOLS_TERMINAL "cmd.exe"
74 #else
75 #define GEANY_DEFAULT_TOOLS_TERMINAL "xterm"
76 #endif
77 #define GEANY_DEFAULT_TOOLS_BROWSER "firefox"
78 #define GEANY_DEFAULT_TOOLS_PRINTCMD "lpr"
79 #define GEANY_DEFAULT_TOOLS_GREP "grep"
80 #define GEANY_DEFAULT_MRU_LENGTH 10
81 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Sans 9"
82 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Sans 9"
83 #define GEANY_DEFAULT_FONT_EDITOR "Monospace 10"
84 #define GEANY_TOGGLE_MARK "~ "
85 #define GEANY_MAX_AUTOCOMPLETE_WORDS 30
88 static gchar *scribble_text = NULL;
89 static gint scribble_pos = -1;
90 static GPtrArray *session_files = NULL;
91 static gint session_notebook_page;
92 static gint hpan_position;
93 static gint vpan_position;
95 static GPtrArray *keyfile_groups = NULL;
96 GPtrArray *pref_groups = NULL;
99 /* The group will be free'd on quitting.
100 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
101 void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
103 g_ptr_array_add(keyfile_groups, group);
105 if (for_prefs_dialog)
106 g_ptr_array_add(pref_groups, group);
110 static void init_pref_groups(void)
112 StashGroup *group;
114 group = stash_group_new(PACKAGE);
115 configuration_add_pref_group(group, TRUE);
116 stash_group_add_entry(group, &prefs.default_open_path,
117 "default_open_path", "", "startup_path_entry");
119 stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
120 "cmdline_new_files", TRUE, "check_cmdline_new_files");
122 stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
123 "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
124 stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
125 stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
126 "sidebar_pos", GTK_POS_LEFT,
127 "radio_sidebar_left", GTK_POS_LEFT,
128 "radio_sidebar_right", GTK_POS_RIGHT,
129 NULL);
131 /* editor display */
132 stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
133 "highlighting_invert_all", FALSE, "check_highlighting_invert");
135 stash_group_add_toggle_button(group, &search_prefs.suppress_dialogs,
136 "pref_main_suppress_search_dialogs", FALSE, "check_ask_suppress_search_dialogs");
137 stash_group_add_toggle_button(group, &search_prefs.use_current_word,
138 "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
140 /* editor */
141 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
142 "check_detect_indent", FALSE, "check_detect_indent");
143 stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
144 "use_tab_to_indent", TRUE, "check_tab_key_indents");
145 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
146 "pref_editor_tab_width", 4, "spin_indent_width");
147 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->hard_tab_width,
148 "indent_hard_tab_width", 8, "spin_tab_width");
149 stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
150 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
151 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
152 "indent_type", GEANY_INDENT_TYPE_TABS,
153 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
154 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
155 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
156 NULL);
157 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
158 "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
159 "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
160 "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
161 "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
162 NULL);
163 stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
164 "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
165 stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
166 "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
167 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
168 "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
169 "spin_autocompletion_max_entries");
170 stash_group_add_string(group, &editor_prefs.color_scheme,
171 "color_scheme", NULL);
173 /* files */
174 stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
175 "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
176 stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
177 "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
179 /* hidden prefs (don't overwrite them so users can edit them manually) */
180 group = stash_group_new(PACKAGE);
181 configuration_add_pref_group(group, FALSE);
182 stash_group_set_write_once(group, TRUE);
183 stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
184 "show_editor_scrollbars", TRUE);
185 stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
186 "brace_match_ltgt", FALSE);
187 stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
188 "use_gtk_word_boundaries", TRUE);
189 stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
190 "complete_snippets_whilst_editing", FALSE);
191 stash_group_add_boolean(group, &interface_prefs.show_symbol_list_expanders,
192 "show_symbol_list_expanders", TRUE);
193 stash_group_add_boolean(group, &ui_prefs.allow_always_save,
194 "allow_always_save", FALSE);
195 stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
196 "use_safe_file_saving", FALSE);
200 typedef enum SettingAction
202 SETTING_READ,
203 SETTING_WRITE
205 SettingAction;
207 static void settings_action(GKeyFile *config, SettingAction action)
209 guint i;
210 StashGroup *group;
212 foreach_ptr_array(group, i, keyfile_groups)
214 switch (action)
216 case SETTING_READ:
217 stash_group_load_from_key_file(group, config); break;
218 case SETTING_WRITE:
219 stash_group_save_to_key_file(group, config); break;
225 static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
227 gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
228 guint i;
230 for (i = 0; i < file_prefs.mru_length; i++)
232 if (! g_queue_is_empty(queue))
234 /* copy the values, this is necessary when this function is called from the
235 * preferences dialog or when quitting is canceled to keep the queue intact */
236 recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
238 else
240 recent_files[i] = NULL;
241 break;
244 /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
245 recent_files[file_prefs.mru_length] = NULL;
246 g_key_file_set_string_list(config, "files", key,
247 (const gchar**)recent_files, file_prefs.mru_length);
248 g_strfreev(recent_files);
252 static gchar *get_session_file_string(GeanyDocument *doc)
254 gchar *fname;
255 gchar *locale_filename;
256 GeanyFiletype *ft = doc->file_type;
258 if (ft == NULL) /* can happen when saving a new file when quitting */
259 ft = filetypes[GEANY_FILETYPES_NONE];
261 locale_filename = utils_get_locale_from_utf8(doc->file_name);
262 /* If the filename contains any ';' (semi-colons) we need to escape them otherwise
263 * g_key_file_get_string_list() would fail reading them, so we replace them before
264 * writing with usual colons which must never appear in a filename and replace them
265 * back when we read the file again from the file.
266 * (g_path_skip_root() to skip C:\... on Windows) */
267 g_strdelimit((gchar*) utils_path_skip_root(locale_filename), ";", ':');
269 fname = g_strdup_printf("%d;%s;%d;%d;%d;%d;%d;%s;%d",
270 sci_get_current_position(doc->editor->sci),
271 ft->name,
272 doc->readonly,
273 encodings_get_idx_from_charset(doc->encoding),
274 doc->editor->indent_type,
275 doc->editor->auto_indent,
276 doc->editor->line_wrapping,
277 locale_filename,
278 doc->editor->line_breaking);
279 g_free(locale_filename);
280 return fname;
284 void configuration_save_session_files(GKeyFile *config)
286 gint npage;
287 gchar *tmp;
288 gchar entry[16];
289 guint i = 0, j = 0, max;
290 GeanyDocument *doc;
292 npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
293 g_key_file_set_integer(config, "files", "current_page", npage);
295 /* store the filenames to reopen them the next time */
296 max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
297 for (i = 0; i < max; i++)
299 doc = document_get_from_page(i);
300 if (doc != NULL && doc->real_path != NULL)
302 gchar *fname;
304 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
305 fname = get_session_file_string(doc);
306 g_key_file_set_string(config, "files", entry, fname);
307 g_free(fname);
308 j++;
311 /* if open filenames less than saved session files, delete existing entries in the list */
312 i = j;
313 while (TRUE)
315 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
316 tmp = g_key_file_get_string(config, "files", entry, NULL);
317 if (G_UNLIKELY(tmp == NULL))
319 break;
321 else
323 g_key_file_remove_key(config, "files", entry, NULL);
324 g_free(tmp);
325 i++;
331 static void save_dialog_prefs(GKeyFile *config)
333 /* new settings should be added in init_pref_groups() */
334 settings_action(config, SETTING_WRITE);
336 /* Some of the key names are not consistent, but this is for backwards compatibility */
338 /* general */
339 g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
340 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_session", project_prefs.project_session);
341 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
342 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
343 g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
344 g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
345 g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
346 g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
347 g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
349 /* interface */
350 g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
351 g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
352 g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
353 g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
354 g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
355 g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
356 g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
357 g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
358 g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
359 g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
360 g_key_file_set_boolean(config, PACKAGE, "msgwin_status_visible", interface_prefs.msgwin_status_visible);
361 g_key_file_set_boolean(config, PACKAGE, "msgwin_compiler_visible", interface_prefs.msgwin_compiler_visible);
362 g_key_file_set_boolean(config, PACKAGE, "msgwin_messages_visible", interface_prefs.msgwin_messages_visible);
363 g_key_file_set_boolean(config, PACKAGE, "msgwin_scribble_visible", interface_prefs.msgwin_scribble_visible);
365 /* display */
366 g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
367 g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
368 g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
369 g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
370 g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
371 g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_global_enabled);
372 g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_global_type);
373 g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_global_column);
374 g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
376 /* editor */
377 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
378 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
379 g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
380 g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
381 g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
382 g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
383 g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
384 g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
385 g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
386 g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
387 g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
388 g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
389 g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
390 g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
391 g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
392 g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
393 g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
395 /* files */
396 g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
397 if (file_prefs.default_open_encoding == -1)
398 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
399 else
400 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
401 g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
402 g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
403 g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
404 g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
406 /* toolbar */
407 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
408 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
409 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
410 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
411 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
412 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
414 /* templates */
415 g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
416 g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
417 g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
418 g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
419 g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
420 g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
421 g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
422 g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
424 /* tools settings */
425 g_key_file_set_string(config, "tools", "term_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
426 g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
427 g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
428 g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
430 /* build menu */
431 build_save_menu(config, NULL, GEANY_BCS_PREF);
433 /* printing */
434 g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
435 g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
436 g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
437 g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
438 g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
439 g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
440 g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
442 /* VTE */
443 #ifdef HAVE_VTE
444 g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
445 if (vte_info.load_vte && vc != NULL)
447 gchar *tmp_string;
449 if (!g_key_file_has_key(config, "VTE", "emulation", NULL)) /* hidden */
450 g_key_file_set_string(config, "VTE", "emulation", vc->emulation);
451 if (!g_key_file_has_key(config, "VTE", "send_selection_unsafe", NULL)) /* hidden */
452 g_key_file_set_boolean(config, "VTE", "send_selection_unsafe",
453 vc->send_selection_unsafe);
454 g_key_file_set_string(config, "VTE", "font", vc->font);
455 g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
456 g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
457 g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
458 g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
459 g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
460 g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
461 g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
462 g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
463 g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
464 g_key_file_set_string(config, "VTE", "font", vc->font);
465 g_key_file_set_string(config, "VTE", "shell", vc->shell);
466 tmp_string = utils_get_hex_from_color(vc->colour_fore);
467 g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
468 g_free(tmp_string);
469 tmp_string = utils_get_hex_from_color(vc->colour_back);
470 g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
471 g_free(tmp_string);
472 vte_get_working_directory(); /* refresh vte_info.dir */
473 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
475 #endif
479 static void save_ui_prefs(GKeyFile *config)
481 g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
482 g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
483 g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
484 g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
486 /* get the text from the scribble textview */
488 GtkTextBuffer *buffer;
489 GtkTextIter start, end, iter;
490 GtkTextMark *mark;
492 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
493 gtk_text_buffer_get_bounds(buffer, &start, &end);
494 scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
495 g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
496 g_free(scribble_text);
498 mark = gtk_text_buffer_get_insert(buffer);
499 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
500 scribble_pos = gtk_text_iter_get_offset(&iter);
501 g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
504 if (prefs.save_winpos)
506 g_key_file_set_integer(config, PACKAGE, "treeview_position",
507 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
508 g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
509 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
511 gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
512 gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
513 if (gdk_window_get_state(main_widgets.window->window) & GDK_WINDOW_STATE_MAXIMIZED)
514 ui_prefs.geometry[4] = 1;
515 else
516 ui_prefs.geometry[4] = 0;
518 g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
521 g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
522 if (ui_prefs.custom_commands != NULL)
524 g_key_file_set_string_list(config, PACKAGE, "custom_commands",
525 (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
530 void configuration_save(void)
532 GKeyFile *config = g_key_file_new();
533 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
534 gchar *data;
536 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
538 /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
539 g_signal_emit_by_name(geany_object, "save-settings", config);
541 save_dialog_prefs(config);
542 save_ui_prefs(config);
543 project_save_prefs(config); /* save project filename, etc. */
544 save_recent_files(config, ui_prefs.recent_queue, "recent_files");
545 save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
547 if (cl_options.load_session)
548 configuration_save_session_files(config);
550 /* write the file */
551 data = g_key_file_to_data(config, NULL, NULL);
552 utils_write_file(configfile, data);
553 g_free(data);
555 g_key_file_free(config);
556 g_free(configfile);
560 static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
562 gchar **recent_files;
563 gsize i, len = 0;
565 recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
566 if (recent_files != NULL)
568 for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
570 gchar *filename = g_strdup(recent_files[i]);
571 g_queue_push_tail(queue, filename);
573 g_strfreev(recent_files);
579 * Load session list from the given keyfile, and store it in the global
580 * session_files variable for later file loading
581 * */
582 void configuration_load_session_files(GKeyFile *config, gboolean read_recent_files)
584 guint i;
585 gboolean have_session_files;
586 gchar entry[16];
587 gchar **tmp_array;
588 GError *error = NULL;
590 session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
592 if (read_recent_files)
594 load_recent_files(config, ui_prefs.recent_queue, "recent_files");
595 load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
598 /* the project may load another list than the main setting */
599 if (session_files != NULL)
600 g_ptr_array_free(session_files, TRUE);
602 session_files = g_ptr_array_new();
603 have_session_files = TRUE;
604 i = 0;
605 while (have_session_files)
607 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
608 tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
609 if (! tmp_array || error)
611 g_error_free(error);
612 error = NULL;
613 have_session_files = FALSE;
615 g_ptr_array_add(session_files, tmp_array);
616 i++;
621 static void load_dialog_prefs(GKeyFile *config)
623 gchar *tmp_string, *tmp_string2;
624 const gchar *default_charset = NULL;
626 /* new settings should be added in init_pref_groups() */
627 settings_action(config, SETTING_READ);
629 /* general */
630 prefs.confirm_exit = utils_get_setting_boolean(config, PACKAGE, "pref_main_confirm_exit", FALSE);
631 prefs.suppress_status_messages = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_status_messages", FALSE);
632 prefs.load_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_load_session", TRUE);
633 project_prefs.project_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_session", TRUE);
634 project_prefs.project_file_in_basedir = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", FALSE);
635 prefs.save_winpos = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_winpos", TRUE);
636 prefs.beep_on_errors = utils_get_setting_boolean(config, PACKAGE, "beep_on_errors", TRUE);
637 prefs.switch_to_status = utils_get_setting_boolean(config, PACKAGE, "switch_msgwin_pages", FALSE);
638 prefs.auto_focus = utils_get_setting_boolean(config, PACKAGE, "auto_focus", FALSE);
640 /* interface */
641 interface_prefs.tab_pos_editor = utils_get_setting_integer(config, PACKAGE, "tab_pos_editor", GTK_POS_TOP);
642 interface_prefs.tab_pos_msgwin = utils_get_setting_integer(config, PACKAGE, "tab_pos_msgwin",GTK_POS_LEFT);
643 interface_prefs.sidebar_symbol_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_symbol_visible", TRUE);
644 interface_prefs.sidebar_openfiles_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_openfiles_visible", TRUE);
645 interface_prefs.statusbar_visible = utils_get_setting_boolean(config, PACKAGE, "statusbar_visible", TRUE);
646 file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
647 interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
648 file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
649 interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
650 interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
651 interface_prefs.msgwin_font = utils_get_setting_string(config, PACKAGE, "msgwin_font", GEANY_DEFAULT_FONT_MSG_WINDOW);
652 interface_prefs.msgwin_status_visible = utils_get_setting_boolean(config, PACKAGE, "msgwin_status_visible", TRUE);
653 interface_prefs.msgwin_compiler_visible = utils_get_setting_boolean(config, PACKAGE, "msgwin_compiler_visible", TRUE);
654 interface_prefs.msgwin_messages_visible = utils_get_setting_boolean(config, PACKAGE, "msgwin_messages_visible", TRUE);
655 interface_prefs.msgwin_scribble_visible = utils_get_setting_boolean(config, PACKAGE, "msgwin_scribble_visible", TRUE);
657 /* display, editor */
658 editor_prefs.long_line_global_enabled = utils_get_setting_boolean(config, PACKAGE, "long_line_enabled", TRUE);
659 editor_prefs.long_line_global_type = utils_get_setting_integer(config, PACKAGE, "long_line_type", 0);
660 if (editor_prefs.long_line_global_type == 2) /* backward compatibility */
662 editor_prefs.long_line_global_type = 0;
663 editor_prefs.long_line_global_enabled = FALSE;
665 editor_prefs.long_line_color = utils_get_setting_string(config, PACKAGE, "long_line_color", "#C2EBC2");
666 editor_prefs.long_line_global_column = utils_get_setting_integer(config, PACKAGE, "long_line_column", 72);
667 editor_prefs.symbolcompletion_min_chars = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_min_chars", GEANY_MIN_SYMBOLLIST_CHARS);
668 editor_prefs.symbolcompletion_max_height = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_max_height", GEANY_MAX_SYMBOLLIST_HEIGHT);
669 editor_prefs.line_wrapping = utils_get_setting_boolean(config, PACKAGE, "line_wrapping", FALSE); /* default is off for better performance */
670 editor_prefs.use_indicators = utils_get_setting_boolean(config, PACKAGE, "use_indicators", TRUE);
671 editor_prefs.show_indent_guide = utils_get_setting_boolean(config, PACKAGE, "show_indent_guide", FALSE);
672 editor_prefs.show_white_space = utils_get_setting_boolean(config, PACKAGE, "show_white_space", FALSE);
673 editor_prefs.show_line_endings = utils_get_setting_boolean(config, PACKAGE, "show_line_endings", FALSE);
674 editor_prefs.scroll_stop_at_last_line = utils_get_setting_boolean(config, PACKAGE, "scroll_stop_at_last_line", TRUE);
675 editor_prefs.auto_close_xml_tags = utils_get_setting_boolean(config, PACKAGE, "auto_close_xml_tags", TRUE);
676 editor_prefs.complete_snippets = utils_get_setting_boolean(config, PACKAGE, "complete_snippets", TRUE);
677 editor_prefs.auto_complete_symbols = utils_get_setting_boolean(config, PACKAGE, "auto_complete_symbols", TRUE);
678 editor_prefs.folding = utils_get_setting_boolean(config, PACKAGE, "use_folding", TRUE);
679 editor_prefs.unfold_all_children = utils_get_setting_boolean(config, PACKAGE, "unfold_all_children", FALSE);
680 editor_prefs.show_markers_margin = utils_get_setting_boolean(config, PACKAGE, "show_markers_margin", TRUE);
681 editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
682 editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
683 editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
684 editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
685 editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
686 editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
687 editor_prefs.comment_toggle_mark = utils_get_setting_string(config, PACKAGE, "comment_toggle_mark", GEANY_TOGGLE_MARK);
688 editor_prefs.autoclose_chars = utils_get_setting_integer(config, PACKAGE, "autoclose_chars", 0);
690 /* Files
691 * use current locale encoding as default for new files (should be UTF-8 in most cases) */
692 g_get_charset(&default_charset);
693 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_new_encoding",
694 default_charset);
695 if (tmp_string)
697 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
698 if (enc != NULL)
699 file_prefs.default_new_encoding = enc->idx;
700 else
701 file_prefs.default_new_encoding = GEANY_ENCODING_UTF_8;
703 g_free(tmp_string);
705 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_open_encoding",
706 "none");
707 if (tmp_string)
709 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
710 if (enc != NULL)
711 file_prefs.default_open_encoding = enc->idx;
712 else
713 file_prefs.default_open_encoding = -1;
715 g_free(tmp_string);
717 file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
718 file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
719 file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
720 file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
722 /* toolbar */
723 toolbar_prefs.visible = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_show", TRUE);
724 toolbar_prefs.append_to_menu = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", FALSE);
726 toolbar_prefs.use_gtk_default_style = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", TRUE);
727 if (! toolbar_prefs.use_gtk_default_style)
728 toolbar_prefs.icon_style = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_style", GTK_TOOLBAR_ICONS);
730 toolbar_prefs.use_gtk_default_icon = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", TRUE);
731 if (! toolbar_prefs.use_gtk_default_icon)
732 toolbar_prefs.icon_size = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_size", GTK_ICON_SIZE_LARGE_TOOLBAR);
735 /* VTE */
736 #ifdef HAVE_VTE
737 vte_info.load_vte = utils_get_setting_boolean(config, "VTE", "load_vte", TRUE);
738 if (vte_info.load_vte)
740 struct passwd *pw = getpwuid(getuid());
741 gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
743 vc = g_new0(VteConfig, 1);
744 vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
745 if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
746 /* last dir is not set, fallback to user's home directory */
747 vte_info.dir = g_strdup(pw->pw_dir);
748 else if (vte_info.dir == NULL && pw == NULL)
749 /* fallback to root */
750 vte_info.dir = g_strdup("/");
752 vc->emulation = utils_get_setting_string(config, "VTE", "emulation", "xterm");
753 vc->send_selection_unsafe = utils_get_setting_boolean(config, "VTE",
754 "send_selection_unsafe", FALSE);
755 vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
756 vc->font = utils_get_setting_string(config, "VTE", "font", "Monospace 10");
757 vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
758 vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
759 vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", FALSE);
760 vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
761 vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
762 vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
763 vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
764 vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
765 vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
766 vc->colour_fore = g_new0(GdkColor, 1);
767 vc->colour_back = g_new0(GdkColor, 1);
768 tmp_string = utils_get_setting_string(config, "VTE", "colour_fore", "#ffffff");
769 gdk_color_parse(tmp_string, vc->colour_fore);
770 g_free(tmp_string);
771 tmp_string = utils_get_setting_string(config, "VTE", "colour_back", "#000000");
772 gdk_color_parse(tmp_string, vc->colour_back);
773 g_free(tmp_string);
775 #endif
776 /* templates */
777 template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
778 template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
779 tmp_string = utils_get_initials(template_prefs.developer);
780 template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
781 g_free(tmp_string);
783 template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
785 tmp_string2 = utils_get_hostname();
786 tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), tmp_string2);
787 template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
788 g_free(tmp_string);
789 g_free(tmp_string2);
790 template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", "%Y");
791 template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", "%Y-%m-%d");
792 template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", "%d.%m.%Y %H:%M:%S %Z");
794 /* tools */
795 tool_prefs.term_cmd = utils_get_setting_string(config, "tools", "term_cmd", GEANY_DEFAULT_TOOLS_TERMINAL);
796 tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
797 tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
799 tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
801 /* build menu */
802 build_set_group_count( GEANY_GBG_FT, utils_get_setting_integer( config, "build-menu", "number_ft_menu_items", 0 ));
803 build_set_group_count( GEANY_GBG_NON_FT, utils_get_setting_integer( config, "build-menu", "number_non_ft_menu_items", 0 ));
804 build_set_group_count( GEANY_GBG_EXEC, utils_get_setting_integer( config, "build-menu", "number_exec_menu_items", 0 ));
805 build_load_menu(config, GEANY_BCS_PREF, NULL);
807 /* printing */
808 tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
809 #ifdef G_OS_WIN32
810 /* single quote paths on Win32 for g_spawn_command_line_async */
811 tmp_string = g_strconcat("'", tmp_string2, "' '%f'", NULL);
812 #else
813 tmp_string = g_strconcat(tmp_string2, " %f", NULL);
814 #endif
815 printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
816 g_free(tmp_string);
817 g_free(tmp_string2);
819 #if GTK_CHECK_VERSION(2, 10, 0)
820 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
821 #else
822 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", FALSE);
823 #endif
824 printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
825 printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
826 printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
827 printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
828 printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
832 static void load_ui_prefs(GKeyFile *config)
834 gint *geo;
835 GError *error = NULL;
837 ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
838 ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
839 ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
840 ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
841 ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
843 scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
844 _("Type here what you want, use it as a notice/scratch board"));
845 scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
847 geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", NULL, &error);
848 if (error)
850 ui_prefs.geometry[0] = -1;
851 g_error_free(error);
852 error = NULL;
854 else
856 gint i;
858 ui_prefs.geometry[0] = geo[0];
859 ui_prefs.geometry[1] = geo[1];
860 ui_prefs.geometry[2] = geo[2];
861 ui_prefs.geometry[3] = geo[3];
862 ui_prefs.geometry[4] = geo[4];
864 /* don't use insane values but when main windows was maximized last time, pos might be
865 * negative (due to differences in root window and window decorations) */
866 if (ui_prefs.geometry[4] != 1)
868 for (i = 2; i < 4; i++)
870 if (ui_prefs.geometry[i] < -1)
871 ui_prefs.geometry[i] = -1;
875 hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
876 vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
877 (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
878 (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
880 g_free(geo);
885 * Save current session in default configuration file
887 void configuration_save_default_session(void)
889 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
890 gchar *data;
891 GKeyFile *config = g_key_file_new();
893 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
895 if (cl_options.load_session)
896 configuration_save_session_files(config);
898 /* write the file */
899 data = g_key_file_to_data(config, NULL, NULL);
900 utils_write_file(configfile, data);
901 g_free(data);
903 g_key_file_free(config);
904 g_free(configfile);
909 * Only reload the session part of the default configuration
911 void configuration_reload_default_session(void)
913 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
914 GKeyFile *config = g_key_file_new();
916 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
918 configuration_load_session_files(config, FALSE);
920 g_key_file_free(config);
921 g_free(configfile);
925 gboolean configuration_load(void)
927 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
928 GKeyFile *config = g_key_file_new();
930 if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
931 { /* config file does not (yet) exist, so try to load a global config file which may be */
932 /* created by distributors */
933 geany_debug("No config file found, try to use global configuration.");
934 setptr(configfile, g_strconcat(app->datadir, G_DIR_SEPARATOR_S "geany.conf", NULL));
937 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
939 load_dialog_prefs(config);
940 load_ui_prefs(config);
941 project_load_prefs(config);
942 configuration_load_session_files(config, TRUE);
944 /* this signal can be used e.g. to delay building UI elements until settings have been read */
945 g_signal_emit_by_name(geany_object, "load-settings", config);
947 g_key_file_free(config);
948 g_free(configfile);
949 return TRUE;
953 static gboolean open_session_file(gchar **tmp, guint len)
955 guint pos;
956 const gchar *ft_name;
957 gchar *locale_filename;
958 gint enc_idx, indent_type;
959 gboolean ro, auto_indent, line_wrapping;
960 /** TODO when we have a global pref for line breaking, use its value */
961 gboolean line_breaking = FALSE;
962 gboolean ret = FALSE;
964 pos = atoi(tmp[0]);
965 ft_name = tmp[1];
966 ro = atoi(tmp[2]);
967 enc_idx = atoi(tmp[3]);
968 indent_type = atoi(tmp[4]);
969 auto_indent = atoi(tmp[5]);
970 line_wrapping = atoi(tmp[6]);
971 /* try to get the locale equivalent for the filename */
972 locale_filename = utils_get_locale_from_utf8(tmp[7]);
973 /* replace ':' back with ';' (see get_session_file_string for details) */
974 g_strdelimit((gchar*) utils_path_skip_root(locale_filename), ":", ';');
976 if (len > 8)
977 line_breaking = atoi(tmp[8]);
979 if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
981 GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
982 GeanyDocument *doc = document_open_file_full(
983 NULL, locale_filename, pos, ro, ft,
984 (enc_idx >= 0 && enc_idx < GEANY_ENCODINGS_MAX) ?
985 encodings[enc_idx].charset : NULL);
987 if (doc)
989 editor_set_indent_type(doc->editor, indent_type);
990 editor_set_line_wrapping(doc->editor, line_wrapping);
991 doc->editor->line_breaking = line_breaking;
992 doc->editor->auto_indent = auto_indent;
993 ret = TRUE;
996 else
998 geany_debug("Could not find file '%s'.", tmp[7]);
1001 g_free(locale_filename);
1002 return ret;
1006 /* Open session files
1007 * Note: notebook page switch handler and adding to recent files list is always disabled
1008 * for all files opened within this function */
1009 void configuration_open_files(void)
1011 gint i;
1012 gboolean failure = FALSE;
1014 /* necessary to set it to TRUE for project session support */
1015 main_status.opening_session_files = TRUE;
1017 i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
1018 while (TRUE)
1020 gchar **tmp = g_ptr_array_index(session_files, i);
1021 guint len;
1023 if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
1025 if (! open_session_file(tmp, len))
1026 failure = TRUE;
1028 g_strfreev(tmp);
1030 if (file_prefs.tab_order_ltr)
1032 i++;
1033 if (i >= (gint)session_files->len)
1034 break;
1036 else
1038 i--;
1039 if (i < 0)
1040 break;
1044 g_ptr_array_free(session_files, TRUE);
1045 session_files = NULL;
1047 if (failure)
1048 ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
1049 else if (session_notebook_page >= 0)
1051 /* explicitly allow notebook switch page callback to be called for window title,
1052 * encoding settings and so other things */
1053 main_status.opening_session_files = FALSE;
1054 /** TODO if session_notebook_page is equal to the current notebook tab(the last opened)
1055 ** the notebook switch page callback isn't triggered and e.g. menu items are not updated */
1056 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), session_notebook_page);
1058 main_status.opening_session_files = FALSE;
1062 /* set some settings which are already read from the config file, but need other things, like the
1063 * realisation of the main window */
1064 void configuration_apply_settings(void)
1066 if (scribble_text)
1067 { /* update the scribble widget, because now it's realized */
1068 GtkTextIter iter;
1069 GtkTextBuffer *buffer =
1070 gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
1072 gtk_text_buffer_set_text(buffer, scribble_text, -1);
1073 gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
1074 gtk_text_buffer_place_cursor(buffer, &iter);
1076 g_free(scribble_text);
1078 /* set the position of the hpaned and vpaned */
1079 if (prefs.save_winpos)
1081 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
1082 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
1085 /* set fullscreen after initial draw so that returning to normal view is the right size.
1086 * fullscreen mode is disabled by default, so act only if it is true */
1087 if (ui_prefs.fullscreen)
1089 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
1090 ui_prefs.fullscreen = TRUE;
1091 ui_set_fullscreen();
1094 msgwin_show_hide_tabs();
1098 void configuration_init(void)
1100 keyfile_groups = g_ptr_array_new();
1101 pref_groups = g_ptr_array_new();
1102 init_pref_groups();
1106 void configuration_finalize(void)
1108 guint i;
1109 StashGroup *group;
1111 foreach_ptr_array(group, i, keyfile_groups)
1112 stash_group_free(group);
1114 g_ptr_array_free(keyfile_groups, TRUE);
1115 g_ptr_array_free(pref_groups, TRUE);