Update C++ compiler check to require C++17 (#2862)
[geany-mirror.git] / plugins / saveactions.c
blobe7d236ecc05436d63d7ee1627a2d291463c01995
1 /*
2 * saveactions.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include "geanyplugin.h"
27 #include "gtkcompat.h"
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <glib/gstdio.h>
36 GeanyPlugin *geany_plugin;
37 GeanyData *geany_data;
40 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
42 PLUGIN_SET_INFO(_("Save Actions"), _("This plugin provides different actions related to saving of files."),
43 VERSION, _("The Geany developer team"))
46 enum
48 NOTEBOOK_PAGE_AUTOSAVE = 0,
49 NOTEBOOK_PAGE_INSTANTSAVE,
50 NOTEBOOK_PAGE_BACKUPCOPY
53 static struct
55 GtkWidget *checkbox_enable_autosave;
56 GtkWidget *checkbox_enable_autosave_losing_focus;
57 GtkWidget *checkbox_enable_instantsave;
58 GtkWidget *checkbox_enable_backupcopy;
60 GtkWidget *autosave_interval_spin;
61 GtkWidget *autosave_print_msg_checkbox;
62 GtkWidget *autosave_save_all_radio1;
63 GtkWidget *autosave_save_all_radio2;
65 GtkWidget *instantsave_ft_combo;
67 GtkWidget *backupcopy_entry_dir;
68 GtkWidget *backupcopy_entry_time;
69 GtkWidget *backupcopy_spin_dir_levels;
71 pref_widgets;
74 static gboolean enable_autosave;
75 static gboolean enable_autosave_losing_focus;
76 static gboolean enable_instantsave;
77 static gboolean enable_backupcopy;
79 static gint autosave_interval;
80 static gboolean autosave_print_msg;
81 static gboolean autosave_save_all;
82 static guint autosave_src_id = 0;
84 static gchar *instantsave_default_ft;
86 static gchar *backupcopy_backup_dir; /* path to an existing directory in locale encoding */
87 static gchar *backupcopy_time_fmt;
88 static gint backupcopy_dir_levels;
90 static gchar *config_file;
93 /* Ensures utf8_dir exists and is writable and
94 * set backup_dir to the locale encoded form of utf8_dir */
95 static gboolean backupcopy_set_backup_dir(const gchar *utf8_dir)
97 gchar *tmp;
99 if (G_UNLIKELY(EMPTY(utf8_dir)))
100 return FALSE;
102 tmp = utils_get_locale_from_utf8(utf8_dir);
104 if (! g_path_is_absolute(tmp) ||
105 ! g_file_test(tmp, G_FILE_TEST_EXISTS) ||
106 ! g_file_test(tmp, G_FILE_TEST_IS_DIR))
108 g_free(tmp);
109 return FALSE;
111 /** TODO add utils_is_file_writeable() to the plugin API and make use of it **/
113 SETPTR(backupcopy_backup_dir, tmp);
115 return TRUE;
119 static gchar *backupcopy_skip_root(gchar *filename)
121 /* first skip the root (e.g. c:\ on windows) */
122 const gchar *dir = g_path_skip_root(filename);
124 /* if this has failed, use the filename again */
125 if (dir == NULL)
126 dir = filename;
127 /* check again for leading / or \ */
128 while (*dir == G_DIR_SEPARATOR)
129 dir++;
131 return (gchar *) dir;
135 static gchar *backupcopy_create_dir_parts(const gchar *filename)
137 gint cnt_dir_parts = 0;
138 gchar *cp;
139 gchar *dirname;
140 gchar last_char = 0;
141 gint error;
142 gchar *result;
143 gchar *target_dir;
145 if (backupcopy_dir_levels == 0)
146 return g_strdup("");
148 dirname = g_path_get_dirname(filename);
150 cp = dirname;
151 /* walk to the end of the string */
152 while (*cp != '\0')
153 cp++;
155 /* walk backwards to find directory parts */
156 while (cp > dirname)
158 if (*cp == G_DIR_SEPARATOR && last_char != G_DIR_SEPARATOR)
159 cnt_dir_parts++;
161 if (cnt_dir_parts == backupcopy_dir_levels)
162 break;
164 last_char = *cp;
165 cp--;
168 result = backupcopy_skip_root(cp); /* skip leading slash/backslash and c:\ */
169 target_dir = g_build_filename(backupcopy_backup_dir, result, NULL);
171 error = utils_mkdir(target_dir, TRUE);
172 if (error != 0)
174 ui_set_statusbar(FALSE, _("Backup Copy: Directory could not be created (%s)."),
175 g_strerror(error));
177 result = g_strdup(""); /* return an empty string in case of an error */
179 else
180 result = g_strdup(result);
182 g_free(dirname);
183 g_free(target_dir);
185 return result;
189 static void backupcopy_document_save_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
191 FILE *src, *dst;
192 gchar *locale_filename_src;
193 gchar *locale_filename_dst;
194 gchar *basename_src;
195 gchar *dir_parts_src;
196 gchar *stamp;
197 gchar buf[512];
198 gint fd_dst = -1;
200 if (! enable_backupcopy)
201 return;
203 locale_filename_src = utils_get_locale_from_utf8(doc->file_name);
205 if ((src = g_fopen(locale_filename_src, "r")) == NULL)
207 /* it's unlikely that this happens */
208 ui_set_statusbar(FALSE, _("Backup Copy: File could not be read (%s)."),
209 g_strerror(errno));
210 g_free(locale_filename_src);
211 return;
214 stamp = utils_get_date_time(backupcopy_time_fmt, NULL);
215 basename_src = g_path_get_basename(locale_filename_src);
216 dir_parts_src = backupcopy_create_dir_parts(locale_filename_src);
217 locale_filename_dst = g_strconcat(
218 backupcopy_backup_dir, G_DIR_SEPARATOR_S,
219 dir_parts_src, G_DIR_SEPARATOR_S,
220 basename_src, ".", stamp, NULL);
221 g_free(basename_src);
222 g_free(dir_parts_src);
224 #ifdef G_OS_WIN32
225 if ((dst = g_fopen(locale_filename_dst, "wb")) == NULL)
226 #else
227 /* Use g_open() on non-Windows to set file permissions to 600 atomically.
228 * On Windows, seting file permissions would require specific Windows API. */
229 fd_dst = g_open(locale_filename_dst, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR);
230 if (fd_dst == -1 || (dst = fdopen(fd_dst, "w")) == NULL)
231 #endif
233 ui_set_statusbar(FALSE, _("Backup Copy: File could not be saved (%s)."),
234 g_strerror(errno));
235 g_free(locale_filename_src);
236 g_free(locale_filename_dst);
237 g_free(stamp);
238 fclose(src);
239 if (fd_dst != -1)
240 close(fd_dst);
241 return;
244 while (fgets(buf, sizeof(buf), src) != NULL)
246 fputs(buf, dst);
249 fclose(src);
250 fclose(dst);
251 if (fd_dst != -1)
252 close(fd_dst);
253 g_free(locale_filename_src);
254 g_free(locale_filename_dst);
255 g_free(stamp);
259 static void instantsave_document_new_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
261 if (enable_instantsave && doc->file_name == NULL)
263 gchar *new_filename;
264 gint fd;
265 GeanyFiletype *ft = doc->file_type;
267 fd = g_file_open_tmp("gis_XXXXXX", &new_filename, NULL);
268 if (fd != -1)
269 close(fd); /* close the returned file descriptor as we only need the filename */
271 if (ft == NULL || ft->id == GEANY_FILETYPES_NONE)
272 /* ft is NULL when a new file without template was opened, so use the
273 * configured default file type */
274 ft = filetypes_lookup_by_name(instantsave_default_ft);
276 if (ft != NULL)
277 /* add the filetype's default extension to the new filename */
278 SETPTR(new_filename, g_strconcat(new_filename, ".", ft->extension, NULL));
280 doc->file_name = new_filename;
282 if (doc->file_type->id == GEANY_FILETYPES_NONE)
283 document_set_filetype(doc, filetypes_lookup_by_name(instantsave_default_ft));
285 /* force saving the file to enable all the related actions(tab name, filetype, etc.) */
286 document_save_file(doc, TRUE);
291 /* Save when focus out
293 * @param pointer ref to the current doc (struct GeanyDocument *)
295 * @return always FALSE = Just a one shot execution
298 static gboolean save_on_focus_out_idle(gpointer p_cur_doc)
300 GeanyDocument *cur_doc = p_cur_doc;
302 if (DOC_VALID(cur_doc) && (cur_doc->file_name != NULL))
303 document_save_file(cur_doc, FALSE);
305 return FALSE;
309 /* Autosave the current file when the focus out of the _editor_
311 * Get the SCN_FOCUSOUT signal, and then ask plugin_idle_add()
312 * to save the current doc when idle
314 * @return always FALSE = Non block signals
317 static gboolean on_document_focus_out(GObject *object, GeanyEditor *editor,
318 SCNotification *nt, gpointer data)
320 if (nt->nmhdr.code == SCN_FOCUSOUT
321 && enable_autosave_losing_focus
322 && editor->document->file_name != NULL)
324 plugin_idle_add(geany_plugin, save_on_focus_out_idle, editor->document);
327 return FALSE;
331 PluginCallback plugin_callbacks[] =
333 { "document-new", (GCallback) &instantsave_document_new_cb, FALSE, NULL },
334 { "document-save", (GCallback) &backupcopy_document_save_cb, FALSE, NULL },
335 { "editor-notify", (GCallback) &on_document_focus_out, FALSE, NULL },
336 { NULL, NULL, FALSE, NULL }
340 static gboolean auto_save(gpointer data)
342 GeanyDocument *doc;
343 GeanyDocument *cur_doc = document_get_current();
344 gint i, max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(geany->main_widgets->notebook));
345 gint saved_files = 0;
347 if (cur_doc == NULL)
348 return TRUE;
350 if (autosave_save_all)
352 for (i = 0; i < max; i++)
354 doc = document_get_from_page(i);
356 /* skip current file (save it last), skip files without name */
357 if (doc != cur_doc && doc->file_name != NULL)
358 if (document_save_file(doc, FALSE))
359 saved_files++;
362 /* finally save current file, do it after all other files to get correct window title and
363 * symbol list */
364 if (cur_doc->file_name != NULL)
365 if (document_save_file(cur_doc, FALSE))
366 saved_files++;
368 if (saved_files > 0 && autosave_print_msg)
369 ui_set_statusbar(FALSE, ngettext(
370 "Autosave: Saved %d file automatically.",
371 "Autosave: Saved %d files automatically.", saved_files),
372 saved_files);
374 return TRUE;
378 static void autosave_set_timeout(void)
380 if (! enable_autosave)
381 return;
383 if (autosave_src_id != 0)
384 g_source_remove(autosave_src_id);
385 autosave_src_id = g_timeout_add(autosave_interval * 1000, (GSourceFunc) auto_save, NULL);
389 void plugin_init(GeanyData *data)
391 GKeyFile *config = g_key_file_new();
392 gchar *tmp;
394 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S, "plugins",
395 G_DIR_SEPARATOR_S, "saveactions", G_DIR_SEPARATOR_S, "saveactions.conf", NULL);
397 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
399 enable_autosave = utils_get_setting_boolean(
400 config, "saveactions", "enable_autosave", FALSE);
401 enable_autosave_losing_focus = utils_get_setting_boolean(
402 config, "saveactions", "enable_autosave_losing_focus", FALSE);
403 enable_instantsave = utils_get_setting_boolean(
404 config, "saveactions", "enable_instantsave", FALSE);
405 enable_backupcopy = utils_get_setting_boolean(
406 config, "saveactions", "enable_backupcopy", FALSE);
408 instantsave_default_ft = utils_get_setting_string(config, "instantsave", "default_ft",
409 filetypes[GEANY_FILETYPES_NONE]->name);
411 autosave_src_id = 0; /* mark as invalid */
412 autosave_interval = utils_get_setting_integer(config, "autosave", "interval", 300);
413 autosave_print_msg = utils_get_setting_boolean(config, "autosave", "print_messages", FALSE);
414 autosave_save_all = utils_get_setting_boolean(config, "autosave", "save_all", FALSE);
415 if (enable_autosave)
416 autosave_set_timeout();
418 backupcopy_dir_levels = utils_get_setting_integer(config, "backupcopy", "dir_levels", 0);
419 backupcopy_time_fmt = utils_get_setting_string(
420 config, "backupcopy", "time_fmt", "%Y-%m-%d-%H-%M-%S");
421 tmp = utils_get_setting_string(config, "backupcopy", "backup_dir", g_get_tmp_dir());
422 backupcopy_set_backup_dir(tmp);
424 g_key_file_free(config);
425 g_free(tmp);
429 static void backupcopy_dir_button_clicked_cb(GtkButton *button, gpointer item)
431 /** TODO add win32_show_pref_file_dialog to the plugin API and use it **/
433 #ifdef G_OS_WIN32
434 win32_show_pref_file_dialog(item);
435 #else
437 GtkWidget *dialog;
438 gchar *text;
440 /* initialize the dialog */
441 dialog = gtk_file_chooser_dialog_new(_("Select Directory"), NULL,
442 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
443 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
444 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
446 text = utils_get_locale_from_utf8(gtk_entry_get_text(GTK_ENTRY(item)));
447 if (!EMPTY(text))
448 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), text);
450 /* run it */
451 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
453 gchar *utf8_filename, *tmp;
455 tmp = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
456 utf8_filename = utils_get_utf8_from_locale(tmp);
458 gtk_entry_set_text(GTK_ENTRY(item), utf8_filename);
460 g_free(utf8_filename);
461 g_free(tmp);
464 gtk_widget_destroy(dialog);
468 static void configure_response_cb(GtkDialog *dialog, gint response, G_GNUC_UNUSED gpointer data)
470 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
472 GKeyFile *config = g_key_file_new();
473 gchar *str;
474 const gchar *text_dir, *text_time;
475 gchar *config_dir = g_path_get_dirname(config_file);
477 enable_autosave = gtk_toggle_button_get_active(
478 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_autosave));
479 enable_autosave_losing_focus = gtk_toggle_button_get_active(
480 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_autosave_losing_focus));
481 enable_instantsave = gtk_toggle_button_get_active(
482 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_instantsave));
483 enable_backupcopy = gtk_toggle_button_get_active(
484 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_backupcopy));
486 autosave_interval = gtk_spin_button_get_value_as_int(
487 GTK_SPIN_BUTTON(pref_widgets.autosave_interval_spin));
488 autosave_print_msg = gtk_toggle_button_get_active(
489 GTK_TOGGLE_BUTTON(pref_widgets.autosave_print_msg_checkbox));
490 autosave_save_all = gtk_toggle_button_get_active(
491 GTK_TOGGLE_BUTTON(pref_widgets.autosave_save_all_radio2));
493 g_free(instantsave_default_ft);
494 instantsave_default_ft = gtk_combo_box_text_get_active_text(
495 GTK_COMBO_BOX_TEXT(pref_widgets.instantsave_ft_combo));
497 text_dir = gtk_entry_get_text(GTK_ENTRY(pref_widgets.backupcopy_entry_dir));
498 text_time = gtk_entry_get_text(GTK_ENTRY(pref_widgets.backupcopy_entry_time));
499 backupcopy_dir_levels = gtk_spin_button_get_value_as_int(
500 GTK_SPIN_BUTTON(pref_widgets.backupcopy_spin_dir_levels));
503 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
505 g_key_file_set_boolean(config, "saveactions", "enable_autosave", enable_autosave);
506 g_key_file_set_boolean(config, "saveactions", "enable_autosave_losing_focus", enable_autosave_losing_focus);
507 g_key_file_set_boolean(config, "saveactions", "enable_instantsave", enable_instantsave);
508 g_key_file_set_boolean(config, "saveactions", "enable_backupcopy", enable_backupcopy);
510 g_key_file_set_boolean(config, "autosave", "print_messages", autosave_print_msg);
511 g_key_file_set_boolean(config, "autosave", "save_all", autosave_save_all);
512 g_key_file_set_integer(config, "autosave", "interval", autosave_interval);
514 if (instantsave_default_ft != NULL)
515 g_key_file_set_string(config, "instantsave", "default_ft", instantsave_default_ft);
517 g_key_file_set_integer(config, "backupcopy", "dir_levels", backupcopy_dir_levels);
518 g_key_file_set_string(config, "backupcopy", "time_fmt", text_time);
519 SETPTR(backupcopy_time_fmt, g_strdup(text_time));
520 if (enable_backupcopy)
522 if (!EMPTY(text_dir) && backupcopy_set_backup_dir(text_dir))
524 g_key_file_set_string(config, "backupcopy", "backup_dir", text_dir);
526 else
528 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
529 _("Backup directory does not exist or is not writable."));
534 if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
536 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
537 _("Plugin configuration directory could not be created."));
539 else
541 /* write config to file */
542 str = g_key_file_to_data(config, NULL, NULL);
543 utils_write_file(config_file, str);
544 g_free(str);
547 if (enable_autosave)
548 autosave_set_timeout(); /* apply the changes */
550 g_free(config_dir);
551 g_key_file_free(config);
556 static void checkbox_toggled_cb(GtkToggleButton *tb, gpointer data)
558 gboolean enable = gtk_toggle_button_get_active(tb);
560 switch (GPOINTER_TO_INT(data))
562 case NOTEBOOK_PAGE_AUTOSAVE:
564 gtk_widget_set_sensitive(pref_widgets.autosave_interval_spin, enable);
565 gtk_widget_set_sensitive(pref_widgets.autosave_print_msg_checkbox, enable);
566 gtk_widget_set_sensitive(pref_widgets.autosave_save_all_radio1, enable);
567 gtk_widget_set_sensitive(pref_widgets.autosave_save_all_radio2, enable);
568 break;
570 case NOTEBOOK_PAGE_INSTANTSAVE:
572 gtk_widget_set_sensitive(pref_widgets.instantsave_ft_combo, enable);
573 break;
575 case NOTEBOOK_PAGE_BACKUPCOPY:
577 gtk_widget_set_sensitive(pref_widgets.backupcopy_entry_dir, enable);
578 gtk_widget_set_sensitive(pref_widgets.backupcopy_entry_time, enable);
579 gtk_widget_set_sensitive(pref_widgets.backupcopy_spin_dir_levels, enable);
580 break;
587 GtkWidget *plugin_configure(GtkDialog *dialog)
589 GtkWidget *vbox, *label, *notebook_vbox, *checkbox_enable;
590 GtkWidget *notebook, *inner_vbox;
592 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
594 notebook = gtk_notebook_new();
595 gtk_widget_set_can_focus(notebook, FALSE);
596 gtk_container_set_border_width(GTK_CONTAINER(notebook), 5);
597 gtk_box_pack_start(GTK_BOX(vbox), notebook, FALSE, TRUE, 0);
600 * Auto Save
603 GtkWidget *spin, *hbox, *checkbox, *checkbox_enable_as_lf, *radio1, *radio2;
605 notebook_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
606 inner_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
607 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox), 5);
608 gtk_box_pack_start(GTK_BOX(notebook_vbox), inner_vbox, TRUE, TRUE, 5);
609 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook),
610 notebook_vbox, gtk_label_new(_("Auto Save")), NOTEBOOK_PAGE_AUTOSAVE);
612 checkbox_enable_as_lf = gtk_check_button_new_with_mnemonic(_("Enable save when losing _focus"));
613 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable_as_lf), FALSE);
614 pref_widgets.checkbox_enable_autosave_losing_focus = checkbox_enable_as_lf;
615 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable_as_lf, FALSE, FALSE, 6);
616 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable_as_lf), enable_autosave_losing_focus);
618 checkbox_enable = gtk_check_button_new_with_mnemonic(_("_Enable"));
619 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable), FALSE);
620 pref_widgets.checkbox_enable_autosave = checkbox_enable;
621 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable, FALSE, FALSE, 6);
622 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable), enable_autosave);
623 g_signal_connect(checkbox_enable, "toggled",
624 G_CALLBACK(checkbox_toggled_cb), GINT_TO_POINTER(NOTEBOOK_PAGE_AUTOSAVE));
626 label = gtk_label_new_with_mnemonic(_("Auto save _interval:"));
627 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
628 gtk_box_pack_start(GTK_BOX(inner_vbox), label, TRUE, TRUE, 0);
630 pref_widgets.autosave_interval_spin = spin = gtk_spin_button_new_with_range(1, 1800, 1);
631 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), autosave_interval);
632 gtk_label_set_mnemonic_widget(GTK_LABEL(label), spin);
634 label = gtk_label_new(_("seconds"));
636 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
637 gtk_box_pack_start(GTK_BOX(hbox), spin, TRUE, TRUE, 0);
638 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
640 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 5);
642 checkbox = gtk_check_button_new_with_mnemonic(
643 _("_Print status message if files have been automatically saved"));
644 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox), FALSE);
645 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), autosave_print_msg);
646 gtk_label_set_mnemonic_widget(GTK_LABEL(label), checkbox);
647 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox, FALSE, FALSE, 5);
648 pref_widgets.autosave_print_msg_checkbox = checkbox;
650 radio1 = gtk_radio_button_new_with_mnemonic(NULL,
651 _("Save only current open _file"));
652 pref_widgets.autosave_save_all_radio1 = radio1;
653 gtk_label_set_mnemonic_widget(GTK_LABEL(label), radio1);
654 gtk_button_set_focus_on_click(GTK_BUTTON(radio1), FALSE);
655 gtk_container_add(GTK_CONTAINER(inner_vbox), radio1);
657 radio2 = gtk_radio_button_new_with_mnemonic_from_widget(
658 GTK_RADIO_BUTTON(radio1), _("Sa_ve all open files"));
659 pref_widgets.autosave_save_all_radio2 = radio2;
660 gtk_label_set_mnemonic_widget(GTK_LABEL(label), radio2);
661 gtk_button_set_focus_on_click(GTK_BUTTON(radio2), FALSE);
662 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio2), autosave_save_all);
663 gtk_container_add(GTK_CONTAINER(inner_vbox), radio2);
666 * Instant Save
669 GtkWidget *combo;
670 guint i;
671 const GSList *node;
673 notebook_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
674 inner_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
675 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox), 5);
676 gtk_box_pack_start(GTK_BOX(notebook_vbox), inner_vbox, TRUE, TRUE, 5);
677 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook),
678 notebook_vbox, gtk_label_new(_("Instant Save")), NOTEBOOK_PAGE_INSTANTSAVE);
680 checkbox_enable = gtk_check_button_new_with_mnemonic(_("_Enable"));
681 pref_widgets.checkbox_enable_instantsave = checkbox_enable;
682 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable), FALSE);
683 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable), enable_instantsave);
684 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable, FALSE, FALSE, 6);
685 g_signal_connect(checkbox_enable, "toggled",
686 G_CALLBACK(checkbox_toggled_cb), GINT_TO_POINTER(NOTEBOOK_PAGE_INSTANTSAVE));
688 label = gtk_label_new_with_mnemonic(_("Default _filetype to use for new files:"));
689 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
690 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 0);
692 pref_widgets.instantsave_ft_combo = combo = gtk_combo_box_text_new();
693 i = 0;
694 foreach_slist(node, filetypes_get_sorted_by_name())
696 GeanyFiletype *ft = node->data;
698 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), ft->name);
700 if (utils_str_equal(ft->name, instantsave_default_ft))
701 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), i);
702 i++;
704 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(combo), 3);
705 gtk_label_set_mnemonic_widget(GTK_LABEL(label), combo);
706 gtk_box_pack_start(GTK_BOX(inner_vbox), combo, FALSE, FALSE, 0);
709 * Backup Copy
712 GtkWidget *hbox, *entry_dir, *entry_time, *button, *image, *spin_dir_levels;
714 notebook_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
715 inner_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
716 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox), 5);
717 gtk_box_pack_start(GTK_BOX(notebook_vbox), inner_vbox, TRUE, TRUE, 5);
718 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook),
719 notebook_vbox, gtk_label_new(_("Backup Copy")), NOTEBOOK_PAGE_BACKUPCOPY);
721 checkbox_enable = gtk_check_button_new_with_mnemonic(_("_Enable"));
722 pref_widgets.checkbox_enable_backupcopy = checkbox_enable;
723 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable), FALSE);
724 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable), enable_backupcopy);
725 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable, FALSE, FALSE, 6);
726 g_signal_connect(checkbox_enable, "toggled",
727 G_CALLBACK(checkbox_toggled_cb), GINT_TO_POINTER(NOTEBOOK_PAGE_BACKUPCOPY));
729 label = gtk_label_new_with_mnemonic(_("_Directory to save backup files in:"));
730 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
731 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 0);
733 pref_widgets.backupcopy_entry_dir = entry_dir = gtk_entry_new();
734 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry_dir);
735 if (!EMPTY(backupcopy_backup_dir))
736 gtk_entry_set_text(GTK_ENTRY(entry_dir), backupcopy_backup_dir);
738 button = gtk_button_new();
739 g_signal_connect(button, "clicked",
740 G_CALLBACK(backupcopy_dir_button_clicked_cb), entry_dir);
742 image = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
743 gtk_container_add(GTK_CONTAINER(button), image);
745 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
746 gtk_box_pack_start(GTK_BOX(hbox), entry_dir, TRUE, TRUE, 0);
747 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
749 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 0);
751 label = gtk_label_new_with_mnemonic(
752 _("Date/_Time format for backup files (\"man strftime\" for details):"));
753 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
754 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 7);
756 pref_widgets.backupcopy_entry_time = entry_time = gtk_entry_new();
757 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry_time);
758 if (!EMPTY(backupcopy_time_fmt))
759 gtk_entry_set_text(GTK_ENTRY(entry_time), backupcopy_time_fmt);
760 gtk_box_pack_start(GTK_BOX(inner_vbox), entry_time, FALSE, FALSE, 0);
762 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
764 label = gtk_label_new_with_mnemonic(
765 _("Directory _levels to include in the backup destination:"));
766 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
767 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
769 spin_dir_levels = gtk_spin_button_new_with_range(0, 20, 1);
770 pref_widgets.backupcopy_spin_dir_levels = spin_dir_levels;
771 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin_dir_levels), backupcopy_dir_levels);
772 gtk_label_set_mnemonic_widget(GTK_LABEL(label), spin_dir_levels);
773 gtk_box_pack_start(GTK_BOX(hbox), spin_dir_levels, FALSE, FALSE, 0);
775 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 7);
778 /* manually emit the toggled signal of the enable checkboxes to update the widget sensitivity */
779 g_signal_emit_by_name(pref_widgets.checkbox_enable_autosave, "toggled");
780 g_signal_emit_by_name(pref_widgets.checkbox_enable_instantsave, "toggled");
781 g_signal_emit_by_name(pref_widgets.checkbox_enable_backupcopy, "toggled");
783 gtk_widget_show_all(vbox);
784 g_signal_connect(dialog, "response", G_CALLBACK(configure_response_cb), NULL);
786 return vbox;
790 void plugin_cleanup(void)
792 if (autosave_src_id != 0)
793 g_source_remove(autosave_src_id);
795 g_free(instantsave_default_ft);
797 g_free(backupcopy_backup_dir);
798 g_free(backupcopy_time_fmt);
800 g_free(config_file);