~ubuntu-branches/ubuntu/utopic/geany/utopic

« back to all changes in this revision

Viewing changes to .pc/20_use_sensible_browser.patch/src/keyfile.c

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2011-12-10 07:43:26 UTC
  • mfrom: (3.3.7 sid)
  • Revision ID: package-import@ubuntu.com-20111210074326-s8yqbew5i20h33tf
Tags: 0.21-1ubuntu1
* Merge from Debian Unstable, remaining changes:
  - debian/patches/20_use_evince_viewer.patch:
     + use evince as viewer for pdf and dvi files
  - debian/patches/20_use_x_terminal_emulator.patch:
     + use x-terminal-emulator as terminal
  - debian/control
     + Add breaks on geany-plugins-common << 0.20
* Also fixes bugs:
  - Filter for MATLAB/Octave files filters everythign (LP: 885505)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      keyfile.c - this file is part of Geany, a fast and lightweight IDE
 
3
 *
 
4
 *      Copyright 2005-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
 
5
 *      Copyright 2006-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 *
 
21
 * $Id: keyfile.c 5947 2011-09-22 15:55:10Z ntrel $
 
22
 */
 
23
 
 
24
/*
 
25
 * geany.conf preferences file loading and saving.
 
26
 */
 
27
 
 
28
/*
 
29
 * Session file format:
 
30
 * filename_xx=pos;filetype UID;read only;encoding idx;use_tabs;auto_indent;line_wrapping;filename
 
31
 */
 
32
 
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
 
 
36
#include "geany.h"
 
37
 
 
38
#ifdef HAVE_VTE
 
39
#include <pwd.h>
 
40
#include <sys/types.h>
 
41
#include <unistd.h>
 
42
#endif
 
43
 
 
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"
 
63
 
 
64
 
 
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
 
86
#define GEANY_MAX_SYMBOLS_UPDATE_FREQ   250
 
87
 
 
88
 
 
89
static gchar *scribble_text = NULL;
 
90
static gint scribble_pos = -1;
 
91
static GPtrArray *session_files = NULL;
 
92
static gint session_notebook_page;
 
93
static gint hpan_position;
 
94
static gint vpan_position;
 
95
static const gchar atomic_file_saving_key[] = "use_atomic_file_saving";
 
96
 
 
97
static GPtrArray *keyfile_groups = NULL;
 
98
 
 
99
GPtrArray *pref_groups = NULL;
 
100
 
 
101
static struct
 
102
{
 
103
        int number_ft_menu_items;
 
104
        int number_non_ft_menu_items;
 
105
        int number_exec_menu_items;
 
106
}
 
107
build_menu_prefs;
 
108
 
 
109
 
 
110
/* The group will be free'd on quitting.
 
111
 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
 
112
void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
 
113
{
 
114
        g_ptr_array_add(keyfile_groups, group);
 
115
 
 
116
        if (for_prefs_dialog)
 
117
                g_ptr_array_add(pref_groups, group);
 
118
}
 
119
 
 
120
 
 
121
/* The group will be free'd on quitting. */
 
122
void configuration_add_various_pref_group(struct StashGroup *group)
 
123
{
 
124
        configuration_add_pref_group(group, TRUE);
 
125
        stash_group_set_various(group, TRUE);
 
126
}
 
127
 
 
128
 
 
129
static void init_pref_groups(void)
 
130
{
 
131
        StashGroup *group;
 
132
 
 
133
        group = stash_group_new(PACKAGE);
 
134
        configuration_add_pref_group(group, TRUE);
 
135
        stash_group_add_entry(group, &prefs.default_open_path,
 
136
                "default_open_path", "", "startup_path_entry");
 
137
 
 
138
        stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
 
139
                "cmdline_new_files", TRUE, "check_cmdline_new_files");
 
140
 
 
141
        stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
 
142
                "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
 
143
        stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
 
144
        stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
 
145
                "sidebar_pos", GTK_POS_LEFT,
 
146
                "radio_sidebar_left", GTK_POS_LEFT,
 
147
                "radio_sidebar_right", GTK_POS_RIGHT,
 
148
                NULL);
 
149
 
 
150
        /* editor display */
 
151
        stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
 
152
                "highlighting_invert_all", FALSE, "check_highlighting_invert");
 
153
 
 
154
        stash_group_add_toggle_button(group, &search_prefs.suppress_dialogs,
 
155
                "pref_main_suppress_search_dialogs", FALSE, "check_ask_suppress_search_dialogs");
 
156
        stash_group_add_toggle_button(group, &search_prefs.use_current_word,
 
157
                "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
 
158
 
 
159
        /* editor */
 
160
        stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
 
161
                "check_detect_indent", FALSE, "check_detect_indent_type");
 
162
        stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_width,
 
163
                "detect_indent_width", FALSE, "check_detect_indent_width");
 
164
        stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
 
165
                "use_tab_to_indent", TRUE, "check_tab_key_indents");
 
166
        stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
 
167
                "pref_editor_tab_width", 4, "spin_indent_width");
 
168
        stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
 
169
                "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
 
170
        stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
 
171
                "indent_type", GEANY_INDENT_TYPE_TABS,
 
172
                "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
 
173
                "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
 
174
                "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
 
175
                NULL);
 
176
        stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
 
177
                "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
 
178
                "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
 
179
                "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
 
180
                "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
 
181
                NULL);
 
182
        stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
 
183
                "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
 
184
        stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
 
185
                "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
 
186
        stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
 
187
                "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
 
188
                "spin_autocompletion_max_entries");
 
189
        stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_update_freq,
 
190
                "autocompletion_update_freq", GEANY_MAX_SYMBOLS_UPDATE_FREQ, "spin_symbol_update_freq");
 
191
        stash_group_add_string(group, &editor_prefs.color_scheme,
 
192
                "color_scheme", NULL);
 
193
 
 
194
        /* files */
 
195
        stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
 
196
                "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
 
197
        stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
 
198
                "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
 
199
 
 
200
        /* various geany prefs */
 
201
        group = stash_group_new(PACKAGE);
 
202
        configuration_add_various_pref_group(group);
 
203
 
 
204
        stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
 
205
                "show_editor_scrollbars", TRUE);
 
206
        stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
 
207
                "brace_match_ltgt", FALSE);
 
208
        stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
 
209
                "use_gtk_word_boundaries", TRUE);
 
210
        stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
 
211
                "complete_snippets_whilst_editing", FALSE);
 
212
        stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
 
213
                atomic_file_saving_key, FALSE);
 
214
        stash_group_add_boolean(group, &file_prefs.gio_unsafe_save_backup,
 
215
                "gio_unsafe_save_backup", FALSE);
 
216
        stash_group_add_boolean(group, &file_prefs.use_gio_unsafe_file_saving,
 
217
                "use_gio_unsafe_file_saving", TRUE);
 
218
        /* for backwards-compatibility */
 
219
        stash_group_add_integer(group, &editor_prefs.indentation->hard_tab_width,
 
220
                "indent_hard_tab_width", 8);
 
221
        stash_group_add_integer(group, (gint*)&search_prefs.find_selection_type,
 
222
                "find_selection_type", GEANY_FIND_SEL_CURRENT_WORD);
 
223
 
 
224
        /* Note: Interface-related various prefs are in ui_init_prefs() */
 
225
 
 
226
        /* various build-menu prefs */
 
227
        group = stash_group_new("build-menu");
 
228
        configuration_add_various_pref_group(group);
 
229
 
 
230
        stash_group_add_integer(group, &build_menu_prefs.number_ft_menu_items,
 
231
                "number_ft_menu_items", 0);
 
232
        stash_group_add_integer(group, &build_menu_prefs.number_non_ft_menu_items,
 
233
                "number_non_ft_menu_items", 0);
 
234
        stash_group_add_integer(group, &build_menu_prefs.number_exec_menu_items,
 
235
                "number_exec_menu_items", 0);
 
236
}
 
237
 
 
238
 
 
239
typedef enum SettingAction
 
240
{
 
241
        SETTING_READ,
 
242
        SETTING_WRITE
 
243
}
 
244
SettingAction;
 
245
 
 
246
static void settings_action(GKeyFile *config, SettingAction action)
 
247
{
 
248
        guint i;
 
249
        StashGroup *group;
 
250
 
 
251
        foreach_ptr_array(group, i, keyfile_groups)
 
252
        {
 
253
                switch (action)
 
254
                {
 
255
                        case SETTING_READ:
 
256
                                stash_group_load_from_key_file(group, config); break;
 
257
                        case SETTING_WRITE:
 
258
                                stash_group_save_to_key_file(group, config); break;
 
259
                }
 
260
        }
 
261
}
 
262
 
 
263
 
 
264
static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
 
265
{
 
266
        gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
 
267
        guint i;
 
268
 
 
269
        for (i = 0; i < file_prefs.mru_length; i++)
 
270
        {
 
271
                if (! g_queue_is_empty(queue))
 
272
                {
 
273
                        /* copy the values, this is necessary when this function is called from the
 
274
                         * preferences dialog or when quitting is canceled to keep the queue intact */
 
275
                        recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
 
276
                }
 
277
                else
 
278
                {
 
279
                        recent_files[i] = NULL;
 
280
                        break;
 
281
                }
 
282
        }
 
283
        /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
 
284
        recent_files[file_prefs.mru_length] = NULL;
 
285
        g_key_file_set_string_list(config, "files", key,
 
286
                                (const gchar**)recent_files, file_prefs.mru_length);
 
287
        g_strfreev(recent_files);
 
288
}
 
289
 
 
290
 
 
291
static gchar *get_session_file_string(GeanyDocument *doc)
 
292
{
 
293
        gchar *fname;
 
294
        gchar *locale_filename;
 
295
        GeanyFiletype *ft = doc->file_type;
 
296
 
 
297
        if (ft == NULL) /* can happen when saving a new file when quitting */
 
298
                ft = filetypes[GEANY_FILETYPES_NONE];
 
299
 
 
300
        locale_filename = utils_get_locale_from_utf8(doc->file_name);
 
301
        /* If the filename contains any ';' (semi-colons) we need to escape them otherwise
 
302
         * g_key_file_get_string_list() would fail reading them, so we replace them before
 
303
         * writing with usual colons which must never appear in a filename and replace them
 
304
         * back when we read the file again from the file.
 
305
         * (g_path_skip_root() to skip C:\... on Windows) */
 
306
        g_strdelimit((gchar*) utils_path_skip_root(locale_filename), ";", ':');
 
307
 
 
308
        fname = g_strdup_printf("%d;%s;%d;%d;%d;%d;%d;%s;%d;%d",
 
309
                sci_get_current_position(doc->editor->sci),
 
310
                ft->name,
 
311
                doc->readonly,
 
312
                encodings_get_idx_from_charset(doc->encoding),
 
313
                doc->editor->indent_type,
 
314
                doc->editor->auto_indent,
 
315
                doc->editor->line_wrapping,
 
316
                locale_filename,
 
317
                doc->editor->line_breaking,
 
318
                doc->editor->indent_width);
 
319
        g_free(locale_filename);
 
320
        return fname;
 
321
}
 
322
 
 
323
 
 
324
void configuration_save_session_files(GKeyFile *config)
 
325
{
 
326
        gint npage;
 
327
        gchar *tmp;
 
328
        gchar entry[16];
 
329
        guint i = 0, j = 0, max;
 
330
        GeanyDocument *doc;
 
331
 
 
332
        npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
 
333
        g_key_file_set_integer(config, "files", "current_page", npage);
 
334
 
 
335
        /* store the filenames to reopen them the next time */
 
336
        max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
 
337
        for (i = 0; i < max; i++)
 
338
        {
 
339
                doc = document_get_from_page(i);
 
340
                if (doc != NULL && doc->real_path != NULL)
 
341
                {
 
342
                        gchar *fname;
 
343
 
 
344
                        g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
 
345
                        fname = get_session_file_string(doc);
 
346
                        g_key_file_set_string(config, "files", entry, fname);
 
347
                        g_free(fname);
 
348
                        j++;
 
349
                }
 
350
        }
 
351
        /* if open filenames less than saved session files, delete existing entries in the list */
 
352
        i = j;
 
353
        while (TRUE)
 
354
        {
 
355
                g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
 
356
                tmp = g_key_file_get_string(config, "files", entry, NULL);
 
357
                if (G_UNLIKELY(tmp == NULL))
 
358
                {
 
359
                        break;
 
360
                }
 
361
                else
 
362
                {
 
363
                        g_key_file_remove_key(config, "files", entry, NULL);
 
364
                        g_free(tmp);
 
365
                        i++;
 
366
                }
 
367
        }
 
368
 
 
369
#ifdef HAVE_VTE
 
370
        if (vte_info.have_vte)
 
371
        {
 
372
                vte_get_working_directory();    /* refresh vte_info.dir */
 
373
                g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
 
374
        }
 
375
#endif
 
376
}
 
377
 
 
378
 
 
379
static void save_dialog_prefs(GKeyFile *config)
 
380
{
 
381
        /* new settings should be added in init_pref_groups() */
 
382
        settings_action(config, SETTING_WRITE);
 
383
 
 
384
        /* Some of the key names are not consistent, but this is for backwards compatibility */
 
385
 
 
386
        /* general */
 
387
        g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
 
388
        g_key_file_set_boolean(config, PACKAGE, "pref_main_project_session", project_prefs.project_session);
 
389
        g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
 
390
        g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
 
391
        g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
 
392
        g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
 
393
        g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
 
394
        g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
 
395
        g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
 
396
 
 
397
        /* interface */
 
398
        g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
 
399
        g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
 
400
        g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
 
401
        g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
 
402
        g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
 
403
        g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
 
404
        g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
 
405
        g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
 
406
        g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
 
407
        g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
 
408
        g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
 
409
        g_key_file_set_boolean(config, PACKAGE, "use_native_windows_dialogs", interface_prefs.use_native_windows_dialogs);
 
410
 
 
411
        /* display */
 
412
        g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
 
413
        g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
 
414
        g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
 
415
        g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
 
416
        g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
 
417
        g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_enabled);
 
418
        g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_type);
 
419
        g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_column);
 
420
        g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
 
421
 
 
422
        /* editor */
 
423
        g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
 
424
        g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
 
425
        g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
 
426
        g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
 
427
        g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
 
428
        g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
 
429
        g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
 
430
        g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
 
431
        g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
 
432
        g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
 
433
        g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
 
434
        g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
 
435
        g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
 
436
        g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
 
437
        g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
 
438
        g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
 
439
        g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
 
440
 
 
441
        /* files */
 
442
        g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
 
443
        if (file_prefs.default_open_encoding == -1)
 
444
                g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
 
445
        else
 
446
                g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
 
447
        g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
 
448
        g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
 
449
        g_key_file_set_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", file_prefs.ensure_convert_new_lines);
 
450
        g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
 
451
        g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
 
452
 
 
453
        /* toolbar */
 
454
        g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
 
455
        g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
 
456
        g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
 
457
        g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
 
458
        g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
 
459
        g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
 
460
 
 
461
        /* templates */
 
462
        g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
 
463
        g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
 
464
        g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
 
465
        g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
 
466
        g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
 
467
        g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
 
468
        g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
 
469
        g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
 
470
 
 
471
        /* tools settings */
 
472
        g_key_file_set_string(config, "tools", "term_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
 
473
        g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
 
474
        g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
 
475
        g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
 
476
 
 
477
        /* build menu */
 
478
        build_save_menu(config, NULL, GEANY_BCS_PREF);
 
479
 
 
480
        /* printing */
 
481
        g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
 
482
        g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
 
483
        g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
 
484
        g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
 
485
        g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
 
486
        g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
 
487
        g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
 
488
 
 
489
        /* VTE */
 
490
#ifdef HAVE_VTE
 
491
        g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
 
492
        if (vte_info.have_vte)
 
493
        {
 
494
                gchar *tmp_string;
 
495
 
 
496
                if (!g_key_file_has_key(config, "VTE", "emulation", NULL))      /* hidden */
 
497
                        g_key_file_set_string(config, "VTE", "emulation", vc->emulation);
 
498
                if (!g_key_file_has_key(config, "VTE", "send_selection_unsafe", NULL))  /* hidden */
 
499
                        g_key_file_set_boolean(config, "VTE", "send_selection_unsafe",
 
500
                                vc->send_selection_unsafe);
 
501
                g_key_file_set_string(config, "VTE", "font", vc->font);
 
502
                g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
 
503
                g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
 
504
                g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
 
505
                g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
 
506
                g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
 
507
                g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
 
508
                g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
 
509
                g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
 
510
                g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
 
511
                g_key_file_set_string(config, "VTE", "font", vc->font);
 
512
                g_key_file_set_string(config, "VTE", "shell", vc->shell);
 
513
                tmp_string = utils_get_hex_from_color(vc->colour_fore);
 
514
                g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
 
515
                g_free(tmp_string);
 
516
                tmp_string = utils_get_hex_from_color(vc->colour_back);
 
517
                g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
 
518
                g_free(tmp_string);
 
519
        }
 
520
#endif
 
521
}
 
522
 
 
523
 
 
524
static void save_ui_prefs(GKeyFile *config)
 
525
{
 
526
        g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
 
527
        g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
 
528
        g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
 
529
        g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
 
530
 
 
531
        /* get the text from the scribble textview */
 
532
        {
 
533
                GtkTextBuffer *buffer;
 
534
                GtkTextIter start, end, iter;
 
535
                GtkTextMark *mark;
 
536
 
 
537
                buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
 
538
                gtk_text_buffer_get_bounds(buffer, &start, &end);
 
539
                scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
 
540
                g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
 
541
                g_free(scribble_text);
 
542
 
 
543
                mark = gtk_text_buffer_get_insert(buffer);
 
544
                gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
 
545
                scribble_pos = gtk_text_iter_get_offset(&iter);
 
546
                g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
 
547
        }
 
548
 
 
549
        if (prefs.save_winpos)
 
550
        {
 
551
                g_key_file_set_integer(config, PACKAGE, "treeview_position",
 
552
                                gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
 
553
                g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
 
554
                                gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
 
555
 
 
556
                gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
 
557
                gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
 
558
                if (gdk_window_get_state(main_widgets.window->window) & GDK_WINDOW_STATE_MAXIMIZED)
 
559
                        ui_prefs.geometry[4] = 1;
 
560
                else
 
561
                        ui_prefs.geometry[4] = 0;
 
562
 
 
563
                g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
 
564
        }
 
565
 
 
566
        g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
 
567
        if (ui_prefs.custom_commands != NULL)
 
568
        {
 
569
                g_key_file_set_string_list(config, PACKAGE, "custom_commands",
 
570
                                (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
 
571
        }
 
572
}
 
573
 
 
574
 
 
575
void configuration_save(void)
 
576
{
 
577
        GKeyFile *config = g_key_file_new();
 
578
        gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
 
579
        gchar *data;
 
580
 
 
581
        g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
 
582
 
 
583
        /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
 
584
        g_signal_emit_by_name(geany_object, "save-settings", config);
 
585
 
 
586
        save_dialog_prefs(config);
 
587
        save_ui_prefs(config);
 
588
        project_save_prefs(config);     /* save project filename, etc. */
 
589
        save_recent_files(config, ui_prefs.recent_queue, "recent_files");
 
590
        save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
 
591
 
 
592
        if (cl_options.load_session)
 
593
                configuration_save_session_files(config);
 
594
#ifdef HAVE_VTE
 
595
        else if (vte_info.have_vte)
 
596
        {
 
597
                vte_get_working_directory();    /* refresh vte_info.dir */
 
598
                g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
 
599
        }
 
600
#endif
 
601
 
 
602
        /* write the file */
 
603
        data = g_key_file_to_data(config, NULL, NULL);
 
604
        utils_write_file(configfile, data);
 
605
        g_free(data);
 
606
 
 
607
        g_key_file_free(config);
 
608
        g_free(configfile);
 
609
}
 
610
 
 
611
 
 
612
static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
 
613
{
 
614
        gchar **recent_files;
 
615
        gsize i, len = 0;
 
616
 
 
617
        recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
 
618
        if (recent_files != NULL)
 
619
        {
 
620
                for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
 
621
                {
 
622
                        gchar *filename = g_strdup(recent_files[i]);
 
623
                        g_queue_push_tail(queue, filename);
 
624
                }
 
625
                g_strfreev(recent_files);
 
626
        }
 
627
}
 
628
 
 
629
 
 
630
/*
 
631
 * Load session list from the given keyfile, and store it in the global
 
632
 * session_files variable for later file loading
 
633
 * */
 
634
void configuration_load_session_files(GKeyFile *config, gboolean read_recent_files)
 
635
{
 
636
        guint i;
 
637
        gboolean have_session_files;
 
638
        gchar entry[16];
 
639
        gchar **tmp_array;
 
640
        GError *error = NULL;
 
641
 
 
642
        session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
 
643
 
 
644
        if (read_recent_files)
 
645
        {
 
646
                load_recent_files(config, ui_prefs.recent_queue, "recent_files");
 
647
                load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
 
648
        }
 
649
 
 
650
        /* the project may load another list than the main setting */
 
651
        if (session_files != NULL)
 
652
                g_ptr_array_free(session_files, TRUE);
 
653
 
 
654
        session_files = g_ptr_array_new();
 
655
        have_session_files = TRUE;
 
656
        i = 0;
 
657
        while (have_session_files)
 
658
        {
 
659
                g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
 
660
                tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
 
661
                if (! tmp_array || error)
 
662
                {
 
663
                        g_error_free(error);
 
664
                        error = NULL;
 
665
                        have_session_files = FALSE;
 
666
                }
 
667
                g_ptr_array_add(session_files, tmp_array);
 
668
                i++;
 
669
        }
 
670
 
 
671
#ifdef HAVE_VTE
 
672
        /* BUG: after loading project at startup, closing project doesn't restore old VTE path */
 
673
        if (vte_info.have_vte)
 
674
        {
 
675
                gchar *tmp_string = utils_get_setting_string(config, "VTE", "last_dir", NULL);
 
676
                vte_cwd(tmp_string,TRUE);
 
677
                g_free(tmp_string);
 
678
        }
 
679
#endif
 
680
}
 
681
 
 
682
 
 
683
/* note: new settings should be added in init_pref_groups() */
 
684
static void load_dialog_prefs(GKeyFile *config)
 
685
{
 
686
        gchar *tmp_string, *tmp_string2;
 
687
        const gchar *default_charset = NULL;
 
688
 
 
689
        /* compatibility with Geany 0.20 */
 
690
        if (!g_key_file_has_key(config, PACKAGE, atomic_file_saving_key, NULL))
 
691
        {
 
692
                g_key_file_set_boolean(config, PACKAGE, atomic_file_saving_key,
 
693
                        utils_get_setting_boolean(config, PACKAGE, "use_safe_file_saving", FALSE));
 
694
        }
 
695
        
 
696
        /* read stash prefs */
 
697
        settings_action(config, SETTING_READ);
 
698
 
 
699
        /* general */
 
700
        prefs.confirm_exit = utils_get_setting_boolean(config, PACKAGE, "pref_main_confirm_exit", FALSE);
 
701
        prefs.suppress_status_messages = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_status_messages", FALSE);
 
702
        prefs.load_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_load_session", TRUE);
 
703
        project_prefs.project_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_session", TRUE);
 
704
        project_prefs.project_file_in_basedir = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", FALSE);
 
705
        prefs.save_winpos = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_winpos", TRUE);
 
706
        prefs.beep_on_errors = utils_get_setting_boolean(config, PACKAGE, "beep_on_errors", TRUE);
 
707
        prefs.switch_to_status = utils_get_setting_boolean(config, PACKAGE, "switch_msgwin_pages", FALSE);
 
708
        prefs.auto_focus = utils_get_setting_boolean(config, PACKAGE, "auto_focus", FALSE);
 
709
 
 
710
        /* interface */
 
711
        interface_prefs.tab_pos_editor = utils_get_setting_integer(config, PACKAGE, "tab_pos_editor", GTK_POS_TOP);
 
712
        interface_prefs.tab_pos_msgwin = utils_get_setting_integer(config, PACKAGE, "tab_pos_msgwin",GTK_POS_LEFT);
 
713
        interface_prefs.sidebar_symbol_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_symbol_visible", TRUE);
 
714
        interface_prefs.sidebar_openfiles_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_openfiles_visible", TRUE);
 
715
        interface_prefs.statusbar_visible = utils_get_setting_boolean(config, PACKAGE, "statusbar_visible", TRUE);
 
716
        file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
 
717
        file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
 
718
        interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
 
719
        file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
 
720
        interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
 
721
        interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
 
722
        interface_prefs.msgwin_font = utils_get_setting_string(config, PACKAGE, "msgwin_font", GEANY_DEFAULT_FONT_MSG_WINDOW);
 
723
        interface_prefs.use_native_windows_dialogs = utils_get_setting_boolean(config, PACKAGE, "use_native_windows_dialogs", FALSE);
 
724
 
 
725
        /* display, editor */
 
726
        editor_prefs.long_line_enabled = utils_get_setting_boolean(config, PACKAGE, "long_line_enabled", TRUE);
 
727
        editor_prefs.long_line_type = utils_get_setting_integer(config, PACKAGE, "long_line_type", 0);
 
728
        if (editor_prefs.long_line_type == 2) /* backward compatibility */
 
729
        {
 
730
                editor_prefs.long_line_type = 0;
 
731
                editor_prefs.long_line_enabled = FALSE;
 
732
        }
 
733
        editor_prefs.long_line_color = utils_get_setting_string(config, PACKAGE, "long_line_color", "#C2EBC2");
 
734
        editor_prefs.long_line_column = utils_get_setting_integer(config, PACKAGE, "long_line_column", 72);
 
735
        editor_prefs.symbolcompletion_min_chars = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_min_chars", GEANY_MIN_SYMBOLLIST_CHARS);
 
736
        editor_prefs.symbolcompletion_max_height = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_max_height", GEANY_MAX_SYMBOLLIST_HEIGHT);
 
737
        editor_prefs.line_wrapping = utils_get_setting_boolean(config, PACKAGE, "line_wrapping", FALSE); /* default is off for better performance */
 
738
        editor_prefs.use_indicators = utils_get_setting_boolean(config, PACKAGE, "use_indicators", TRUE);
 
739
        editor_prefs.show_indent_guide = utils_get_setting_boolean(config, PACKAGE, "show_indent_guide", FALSE);
 
740
        editor_prefs.show_white_space = utils_get_setting_boolean(config, PACKAGE, "show_white_space", FALSE);
 
741
        editor_prefs.show_line_endings = utils_get_setting_boolean(config, PACKAGE, "show_line_endings", FALSE);
 
742
        editor_prefs.scroll_stop_at_last_line = utils_get_setting_boolean(config, PACKAGE, "scroll_stop_at_last_line", TRUE);
 
743
        editor_prefs.auto_close_xml_tags = utils_get_setting_boolean(config, PACKAGE, "auto_close_xml_tags", TRUE);
 
744
        editor_prefs.complete_snippets = utils_get_setting_boolean(config, PACKAGE, "complete_snippets", TRUE);
 
745
        editor_prefs.auto_complete_symbols = utils_get_setting_boolean(config, PACKAGE, "auto_complete_symbols", TRUE);
 
746
        editor_prefs.folding = utils_get_setting_boolean(config, PACKAGE, "use_folding", TRUE);
 
747
        editor_prefs.unfold_all_children = utils_get_setting_boolean(config, PACKAGE, "unfold_all_children", FALSE);
 
748
        editor_prefs.show_markers_margin = utils_get_setting_boolean(config, PACKAGE, "show_markers_margin", TRUE);
 
749
        editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
 
750
        editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
 
751
        editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
 
752
        editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
 
753
        editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
 
754
        editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
 
755
        editor_prefs.comment_toggle_mark = utils_get_setting_string(config, PACKAGE, "comment_toggle_mark", GEANY_TOGGLE_MARK);
 
756
        editor_prefs.autoclose_chars = utils_get_setting_integer(config, PACKAGE, "autoclose_chars", 0);
 
757
 
 
758
        /* Files
 
759
         * use current locale encoding as default for new files (should be UTF-8 in most cases) */
 
760
        g_get_charset(&default_charset);
 
761
        tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_new_encoding",
 
762
                default_charset);
 
763
        if (tmp_string)
 
764
        {
 
765
                const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
 
766
                if (enc != NULL)
 
767
                        file_prefs.default_new_encoding = enc->idx;
 
768
                else
 
769
                        file_prefs.default_new_encoding = GEANY_ENCODING_UTF_8;
 
770
 
 
771
                g_free(tmp_string);
 
772
        }
 
773
        tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_open_encoding",
 
774
                "none");
 
775
        if (tmp_string)
 
776
        {
 
777
                const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
 
778
                if (enc != NULL)
 
779
                        file_prefs.default_open_encoding = enc->idx;
 
780
                else
 
781
                        file_prefs.default_open_encoding = -1;
 
782
 
 
783
                g_free(tmp_string);
 
784
        }
 
785
        file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
 
786
        file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
 
787
        file_prefs.ensure_convert_new_lines = utils_get_setting_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", FALSE);
 
788
        file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
 
789
        file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
 
790
 
 
791
        /* toolbar */
 
792
        toolbar_prefs.visible = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_show", TRUE);
 
793
        toolbar_prefs.append_to_menu = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", FALSE);
 
794
        {
 
795
                toolbar_prefs.use_gtk_default_style = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", TRUE);
 
796
                if (! toolbar_prefs.use_gtk_default_style)
 
797
                        toolbar_prefs.icon_style = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_style", GTK_TOOLBAR_ICONS);
 
798
 
 
799
                toolbar_prefs.use_gtk_default_icon = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", TRUE);
 
800
                if (! toolbar_prefs.use_gtk_default_icon)
 
801
                        toolbar_prefs.icon_size = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_size", GTK_ICON_SIZE_LARGE_TOOLBAR);
 
802
        }
 
803
 
 
804
        /* VTE */
 
805
#ifdef HAVE_VTE
 
806
        vte_info.load_vte = utils_get_setting_boolean(config, "VTE", "load_vte", TRUE);
 
807
        if (vte_info.load_vte)
 
808
        {
 
809
                struct passwd *pw = getpwuid(getuid());
 
810
                const gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
 
811
 
 
812
                vc = g_new0(VteConfig, 1);
 
813
                vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
 
814
                if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
 
815
                        /* last dir is not set, fallback to user's home directory */
 
816
                        vte_info.dir = g_strdup(pw->pw_dir);
 
817
                else if (vte_info.dir == NULL && pw == NULL)
 
818
                        /* fallback to root */
 
819
                        vte_info.dir = g_strdup("/");
 
820
 
 
821
                vc->emulation = utils_get_setting_string(config, "VTE", "emulation", "xterm");
 
822
                vc->send_selection_unsafe = utils_get_setting_boolean(config, "VTE",
 
823
                        "send_selection_unsafe", FALSE);
 
824
                vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
 
825
                vc->font = utils_get_setting_string(config, "VTE", "font", "Monospace 10");
 
826
                vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
 
827
                vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
 
828
                vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", TRUE);
 
829
                vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
 
830
                vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
 
831
                vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
 
832
                vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
 
833
                vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
 
834
                vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
 
835
                vc->colour_fore = g_new0(GdkColor, 1);
 
836
                vc->colour_back = g_new0(GdkColor, 1);
 
837
                tmp_string = utils_get_setting_string(config, "VTE", "colour_fore", "#ffffff");
 
838
                gdk_color_parse(tmp_string, vc->colour_fore);
 
839
                g_free(tmp_string);
 
840
                tmp_string = utils_get_setting_string(config, "VTE", "colour_back", "#000000");
 
841
                gdk_color_parse(tmp_string, vc->colour_back);
 
842
                g_free(tmp_string);
 
843
        }
 
844
#endif
 
845
        /* templates */
 
846
        template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
 
847
        template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
 
848
        tmp_string = utils_get_initials(template_prefs.developer);
 
849
        template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
 
850
        g_free(tmp_string);
 
851
 
 
852
        template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
 
853
 
 
854
        tmp_string2 = utils_get_hostname();
 
855
        tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), tmp_string2);
 
856
        template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
 
857
        g_free(tmp_string);
 
858
        g_free(tmp_string2);
 
859
        template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", "%Y");
 
860
        template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", "%Y-%m-%d");
 
861
        template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", "%d.%m.%Y %H:%M:%S %Z");
 
862
 
 
863
        /* tools */
 
864
        tool_prefs.term_cmd = utils_get_setting_string(config, "tools", "term_cmd", GEANY_DEFAULT_TOOLS_TERMINAL);
 
865
        tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
 
866
        tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
 
867
 
 
868
        tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
 
869
 
 
870
        /* build menu */
 
871
        build_set_group_count(GEANY_GBG_FT, build_menu_prefs.number_ft_menu_items);
 
872
        build_set_group_count(GEANY_GBG_NON_FT, build_menu_prefs.number_non_ft_menu_items);
 
873
        build_set_group_count(GEANY_GBG_EXEC, build_menu_prefs.number_exec_menu_items);
 
874
        build_load_menu(config, GEANY_BCS_PREF, NULL);
 
875
 
 
876
        /* printing */
 
877
        tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
 
878
#ifdef G_OS_WIN32
 
879
        /* single quote paths on Win32 for g_spawn_command_line_async */
 
880
        tmp_string = g_strconcat("'", tmp_string2, "' '%f'", NULL);
 
881
#else
 
882
        tmp_string = g_strconcat(tmp_string2, " %f", NULL);
 
883
#endif
 
884
        printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
 
885
        g_free(tmp_string);
 
886
        g_free(tmp_string2);
 
887
 
 
888
        printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
 
889
        printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
 
890
        printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
 
891
        printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
 
892
        printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
 
893
        printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
 
894
}
 
895
 
 
896
 
 
897
static void load_ui_prefs(GKeyFile *config)
 
898
{
 
899
        gint *geo;
 
900
        GError *error = NULL;
 
901
 
 
902
        ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
 
903
        ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
 
904
        ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
 
905
        ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
 
906
        ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
 
907
 
 
908
        scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
 
909
                                _("Type here what you want, use it as a notice/scratch board"));
 
910
        scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
 
911
 
 
912
        geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", NULL, &error);
 
913
        if (error)
 
914
        {
 
915
                ui_prefs.geometry[0] = -1;
 
916
                g_error_free(error);
 
917
                error = NULL;
 
918
        }
 
919
        else
 
920
        {
 
921
                gint i;
 
922
 
 
923
                ui_prefs.geometry[0] = geo[0];
 
924
                ui_prefs.geometry[1] = geo[1];
 
925
                ui_prefs.geometry[2] = geo[2];
 
926
                ui_prefs.geometry[3] = geo[3];
 
927
                ui_prefs.geometry[4] = geo[4];
 
928
 
 
929
                /* don't use insane values but when main windows was maximized last time, pos might be
 
930
                 * negative (due to differences in root window and window decorations) */
 
931
                if (ui_prefs.geometry[4] != 1)
 
932
                {
 
933
                        for (i = 2; i < 4; i++)
 
934
                        {
 
935
                                if (ui_prefs.geometry[i] < -1)
 
936
                                        ui_prefs.geometry[i] = -1;
 
937
                        }
 
938
                }
 
939
        }
 
940
        hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
 
941
        vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
 
942
                                (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
 
943
                                (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
 
944
 
 
945
        g_free(geo);
 
946
}
 
947
 
 
948
 
 
949
/*
 
950
 * Save current session in default configuration file
 
951
 */
 
952
void configuration_save_default_session(void)
 
953
{
 
954
        gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
 
955
        gchar *data;
 
956
        GKeyFile *config = g_key_file_new();
 
957
 
 
958
        g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
 
959
 
 
960
        if (cl_options.load_session)
 
961
                configuration_save_session_files(config);
 
962
 
 
963
        /* write the file */
 
964
        data = g_key_file_to_data(config, NULL, NULL);
 
965
        utils_write_file(configfile, data);
 
966
        g_free(data);
 
967
 
 
968
        g_key_file_free(config);
 
969
        g_free(configfile);
 
970
}
 
971
 
 
972
 
 
973
/*
 
974
 * Only reload the session part of the default configuration
 
975
 */
 
976
void configuration_reload_default_session(void)
 
977
{
 
978
        const gchar *configfile = utils_build_path(app->configdir, "geany.conf", NULL);
 
979
        GKeyFile *config = g_key_file_new();
 
980
 
 
981
        g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
 
982
 
 
983
        configuration_load_session_files(config, FALSE);
 
984
 
 
985
        g_key_file_free(config);
 
986
}
 
987
 
 
988
 
 
989
gboolean configuration_load(void)
 
990
{
 
991
        gchar *configfile = utils_build_path(app->configdir, "geany.conf", NULL);
 
992
        GKeyFile *config = g_key_file_new();
 
993
 
 
994
        if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
 
995
        {       /* config file does not (yet) exist, so try to load a global config file which may be */
 
996
                /* created by distributors */
 
997
                geany_debug("No user config file found, trying to use global configuration.");
 
998
                setptr(configfile, utils_build_path(app->datadir, "geany.conf", NULL));
 
999
        }
 
1000
        g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
 
1001
        g_free(configfile);
 
1002
 
 
1003
        load_dialog_prefs(config);
 
1004
        load_ui_prefs(config);
 
1005
        project_load_prefs(config);
 
1006
        configuration_load_session_files(config, TRUE);
 
1007
 
 
1008
        /* this signal can be used e.g. to delay building UI elements until settings have been read */
 
1009
        g_signal_emit_by_name(geany_object, "load-settings", config);
 
1010
 
 
1011
        g_key_file_free(config);
 
1012
        return TRUE;
 
1013
}
 
1014
 
 
1015
 
 
1016
static gboolean open_session_file(gchar **tmp, guint len)
 
1017
{
 
1018
        guint pos;
 
1019
        const gchar *ft_name;
 
1020
        gchar *locale_filename;
 
1021
        gint enc_idx, indent_type;
 
1022
        gboolean ro, auto_indent, line_wrapping;
 
1023
        /** TODO when we have a global pref for line breaking, use its value */
 
1024
        gboolean line_breaking = FALSE;
 
1025
        gboolean ret = FALSE;
 
1026
 
 
1027
        pos = atoi(tmp[0]);
 
1028
        ft_name = tmp[1];
 
1029
        ro = atoi(tmp[2]);
 
1030
        enc_idx = atoi(tmp[3]);
 
1031
        indent_type = atoi(tmp[4]);
 
1032
        auto_indent = atoi(tmp[5]);
 
1033
        line_wrapping = atoi(tmp[6]);
 
1034
        /* try to get the locale equivalent for the filename */
 
1035
        locale_filename = utils_get_locale_from_utf8(tmp[7]);
 
1036
        /* replace ':' back with ';' (see get_session_file_string for details) */
 
1037
        g_strdelimit((gchar*) utils_path_skip_root(locale_filename), ":", ';');
 
1038
 
 
1039
        if (len > 8)
 
1040
                line_breaking = atoi(tmp[8]);
 
1041
 
 
1042
        if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR))
 
1043
        {
 
1044
                GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
 
1045
                GeanyDocument *doc = document_open_file_full(
 
1046
                        NULL, locale_filename, pos, ro, ft,
 
1047
                        (enc_idx >= 0 && enc_idx < GEANY_ENCODINGS_MAX) ?
 
1048
                                encodings[enc_idx].charset : NULL);
 
1049
 
 
1050
                if (doc)
 
1051
                {
 
1052
                        gint indent_width = doc->editor->indent_width;
 
1053
 
 
1054
                        if (len > 9)
 
1055
                                indent_width = atoi(tmp[9]);
 
1056
                        editor_set_indent(doc->editor, indent_type, indent_width);
 
1057
                        editor_set_line_wrapping(doc->editor, line_wrapping);
 
1058
                        doc->editor->line_breaking = line_breaking;
 
1059
                        doc->editor->auto_indent = auto_indent;
 
1060
                        ret = TRUE;
 
1061
                }
 
1062
        }
 
1063
        else
 
1064
        {
 
1065
                geany_debug("Could not find file '%s'.", tmp[7]);
 
1066
        }
 
1067
 
 
1068
        g_free(locale_filename);
 
1069
        return ret;
 
1070
}
 
1071
 
 
1072
 
 
1073
/* Open session files
 
1074
 * Note: notebook page switch handler and adding to recent files list is always disabled
 
1075
 * for all files opened within this function */
 
1076
void configuration_open_files(void)
 
1077
{
 
1078
        gint i;
 
1079
        gboolean failure = FALSE;
 
1080
 
 
1081
        /* necessary to set it to TRUE for project session support */
 
1082
        main_status.opening_session_files = TRUE;
 
1083
 
 
1084
        i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
 
1085
        while (TRUE)
 
1086
        {
 
1087
                gchar **tmp = g_ptr_array_index(session_files, i);
 
1088
                guint len;
 
1089
 
 
1090
                if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
 
1091
                {
 
1092
                        if (! open_session_file(tmp, len))
 
1093
                                failure = TRUE;
 
1094
                }
 
1095
                g_strfreev(tmp);
 
1096
 
 
1097
                if (file_prefs.tab_order_ltr)
 
1098
                {
 
1099
                        i++;
 
1100
                        if (i >= (gint)session_files->len)
 
1101
                                break;
 
1102
                }
 
1103
                else
 
1104
                {
 
1105
                        i--;
 
1106
                        if (i < 0)
 
1107
                                break;
 
1108
                }
 
1109
        }
 
1110
 
 
1111
        g_ptr_array_free(session_files, TRUE);
 
1112
        session_files = NULL;
 
1113
 
 
1114
        if (failure)
 
1115
                ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
 
1116
        else if (session_notebook_page >= 0)
 
1117
        {
 
1118
                /* explicitly allow notebook switch page callback to be called for window title,
 
1119
                 * encoding settings and so other things */
 
1120
                main_status.opening_session_files = FALSE;
 
1121
                /** TODO if session_notebook_page is equal to the current notebook tab(the last opened)
 
1122
                 ** the notebook switch page callback isn't triggered and e.g. menu items are not updated */
 
1123
                gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), session_notebook_page);
 
1124
        }
 
1125
        main_status.opening_session_files = FALSE;
 
1126
}
 
1127
 
 
1128
 
 
1129
/* set some settings which are already read from the config file, but need other things, like the
 
1130
 * realisation of the main window */
 
1131
void configuration_apply_settings(void)
 
1132
{
 
1133
        if (scribble_text)
 
1134
        {       /* update the scribble widget, because now it's realized */
 
1135
                GtkTextIter iter;
 
1136
                GtkTextBuffer *buffer =
 
1137
                        gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
 
1138
 
 
1139
                gtk_text_buffer_set_text(buffer, scribble_text, -1);
 
1140
                gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
 
1141
                gtk_text_buffer_place_cursor(buffer, &iter);
 
1142
        }
 
1143
        g_free(scribble_text);
 
1144
 
 
1145
        /* set the position of the hpaned and vpaned */
 
1146
        if (prefs.save_winpos)
 
1147
        {
 
1148
                gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
 
1149
                gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
 
1150
        }
 
1151
 
 
1152
        /* set fullscreen after initial draw so that returning to normal view is the right size.
 
1153
         * fullscreen mode is disabled by default, so act only if it is true */
 
1154
        if (ui_prefs.fullscreen)
 
1155
        {
 
1156
                gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
 
1157
                ui_prefs.fullscreen = TRUE;
 
1158
                ui_set_fullscreen();
 
1159
        }
 
1160
 
 
1161
        msgwin_show_hide_tabs();
 
1162
}
 
1163
 
 
1164
 
 
1165
void configuration_init(void)
 
1166
{
 
1167
        keyfile_groups = g_ptr_array_new();
 
1168
        pref_groups = g_ptr_array_new();
 
1169
        init_pref_groups();
 
1170
}
 
1171
 
 
1172
 
 
1173
void configuration_finalize(void)
 
1174
{
 
1175
        guint i;
 
1176
        StashGroup *group;
 
1177
 
 
1178
        foreach_ptr_array(group, i, keyfile_groups)
 
1179
                stash_group_free(group);
 
1180
 
 
1181
        g_ptr_array_free(keyfile_groups, TRUE);
 
1182
        g_ptr_array_free(pref_groups, TRUE);
 
1183
}