Bump API version for new plugin entry points (oops)
[geany-mirror.git] / plugins / saveactions.c
blobab51a29b216842df9be06e7a9de0bece41917242
1 /*
2 * saveactions.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include "geanyplugin.h"
28 #include "gtkcompat.h"
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <glib/gstdio.h>
37 GeanyPlugin *geany_plugin;
38 GeanyData *geany_data;
41 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
43 PLUGIN_SET_INFO(_("Save Actions"), _("This plugin provides different actions related to saving of files."),
44 VERSION, _("The Geany developer team"))
47 enum
49 NOTEBOOK_PAGE_AUTOSAVE = 0,
50 NOTEBOOK_PAGE_INSTANTSAVE,
51 NOTEBOOK_PAGE_BACKUPCOPY
54 static struct
56 GtkWidget *checkbox_enable_autosave;
57 GtkWidget *checkbox_enable_autosave_losing_focus;
58 GtkWidget *checkbox_enable_instantsave;
59 GtkWidget *checkbox_enable_backupcopy;
61 GtkWidget *autosave_interval_spin;
62 GtkWidget *autosave_print_msg_checkbox;
63 GtkWidget *autosave_save_all_radio1;
64 GtkWidget *autosave_save_all_radio2;
66 GtkWidget *instantsave_ft_combo;
68 GtkWidget *backupcopy_entry_dir;
69 GtkWidget *backupcopy_entry_time;
70 GtkWidget *backupcopy_spin_dir_levels;
72 pref_widgets;
75 static gboolean enable_autosave;
76 static gboolean enable_autosave_losing_focus;
77 static gboolean enable_instantsave;
78 static gboolean enable_backupcopy;
80 static gint autosave_interval;
81 static gboolean autosave_print_msg;
82 static gboolean autosave_save_all;
83 static guint autosave_src_id = 0;
85 static gchar *instantsave_default_ft;
87 static gchar *backupcopy_backup_dir; /* path to an existing directory in locale encoding */
88 static gchar *backupcopy_time_fmt;
89 static gint backupcopy_dir_levels;
91 static gchar *config_file;
94 /* Ensures utf8_dir exists and is writable and
95 * set backup_dir to the locale encoded form of utf8_dir */
96 static gboolean backupcopy_set_backup_dir(const gchar *utf8_dir)
98 gchar *tmp;
100 if (G_UNLIKELY(EMPTY(utf8_dir)))
101 return FALSE;
103 tmp = utils_get_locale_from_utf8(utf8_dir);
105 if (! g_path_is_absolute(tmp) ||
106 ! g_file_test(tmp, G_FILE_TEST_EXISTS) ||
107 ! g_file_test(tmp, G_FILE_TEST_IS_DIR))
109 g_free(tmp);
110 return FALSE;
112 /** TODO add utils_is_file_writeable() to the plugin API and make use of it **/
114 SETPTR(backupcopy_backup_dir, tmp);
116 return TRUE;
120 static gchar *backupcopy_skip_root(gchar *filename)
122 /* first skip the root (e.g. c:\ on windows) */
123 const gchar *dir = g_path_skip_root(filename);
125 /* if this has failed, use the filename again */
126 if (dir == NULL)
127 dir = filename;
128 /* check again for leading / or \ */
129 while (*dir == G_DIR_SEPARATOR)
130 dir++;
132 return (gchar *) dir;
136 static gchar *backupcopy_create_dir_parts(const gchar *filename)
138 gint cnt_dir_parts = 0;
139 gchar *cp;
140 gchar *dirname;
141 gchar last_char = 0;
142 gint error;
143 gchar *result;
144 gchar *target_dir;
146 if (backupcopy_dir_levels == 0)
147 return g_strdup("");
149 dirname = g_path_get_dirname(filename);
151 cp = dirname;
152 /* walk to the end of the string */
153 while (*cp != '\0')
154 cp++;
156 /* walk backwards to find directory parts */
157 while (cp > dirname)
159 if (*cp == G_DIR_SEPARATOR && last_char != G_DIR_SEPARATOR)
160 cnt_dir_parts++;
162 if (cnt_dir_parts == backupcopy_dir_levels)
163 break;
165 last_char = *cp;
166 cp--;
169 result = backupcopy_skip_root(cp); /* skip leading slash/backslash and c:\ */
170 target_dir = g_build_filename(backupcopy_backup_dir, result, NULL);
172 error = utils_mkdir(target_dir, TRUE);
173 if (error != 0)
175 ui_set_statusbar(FALSE, _("Backup Copy: Directory could not be created (%s)."),
176 g_strerror(error));
178 result = g_strdup(""); /* return an empty string in case of an error */
180 else
181 result = g_strdup(result);
183 g_free(dirname);
184 g_free(target_dir);
186 return result;
190 static void backupcopy_document_save_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
192 FILE *src, *dst;
193 gchar *locale_filename_src;
194 gchar *locale_filename_dst;
195 gchar *basename_src;
196 gchar *dir_parts_src;
197 gchar *stamp;
198 gchar buf[512];
199 gint fd_dst = -1;
201 if (! enable_backupcopy)
202 return;
204 locale_filename_src = utils_get_locale_from_utf8(doc->file_name);
206 if ((src = g_fopen(locale_filename_src, "r")) == NULL)
208 /* it's unlikely that this happens */
209 ui_set_statusbar(FALSE, _("Backup Copy: File could not be read (%s)."),
210 g_strerror(errno));
211 g_free(locale_filename_src);
212 return;
215 stamp = utils_get_date_time(backupcopy_time_fmt, NULL);
216 basename_src = g_path_get_basename(locale_filename_src);
217 dir_parts_src = backupcopy_create_dir_parts(locale_filename_src);
218 locale_filename_dst = g_strconcat(
219 backupcopy_backup_dir, G_DIR_SEPARATOR_S,
220 dir_parts_src, G_DIR_SEPARATOR_S,
221 basename_src, ".", stamp, NULL);
222 g_free(basename_src);
223 g_free(dir_parts_src);
225 #ifdef G_OS_WIN32
226 if ((dst = g_fopen(locale_filename_dst, "wb")) == NULL)
227 #else
228 /* Use g_open() on non-Windows to set file permissions to 600 atomically.
229 * On Windows, seting file permissions would require specific Windows API. */
230 fd_dst = g_open(locale_filename_dst, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR);
231 if (fd_dst == -1 || (dst = fdopen(fd_dst, "w")) == NULL)
232 #endif
234 ui_set_statusbar(FALSE, _("Backup Copy: File could not be saved (%s)."),
235 g_strerror(errno));
236 g_free(locale_filename_src);
237 g_free(locale_filename_dst);
238 g_free(stamp);
239 fclose(src);
240 if (fd_dst != -1)
241 close(fd_dst);
242 return;
245 while (fgets(buf, sizeof(buf), src) != NULL)
247 fputs(buf, dst);
250 fclose(src);
251 fclose(dst);
252 if (fd_dst != -1)
253 close(fd_dst);
254 g_free(locale_filename_src);
255 g_free(locale_filename_dst);
256 g_free(stamp);
260 static void instantsave_document_new_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
262 if (enable_instantsave && doc->file_name == NULL)
264 gchar *new_filename;
265 gint fd;
266 GeanyFiletype *ft = doc->file_type;
268 fd = g_file_open_tmp("gis_XXXXXX", &new_filename, NULL);
269 if (fd != -1)
270 close(fd); /* close the returned file descriptor as we only need the filename */
272 if (ft == NULL || ft->id == GEANY_FILETYPES_NONE)
273 /* ft is NULL when a new file without template was opened, so use the
274 * configured default file type */
275 ft = filetypes_lookup_by_name(instantsave_default_ft);
277 if (ft != NULL)
278 /* add the filetype's default extension to the new filename */
279 SETPTR(new_filename, g_strconcat(new_filename, ".", ft->extension, NULL));
281 doc->file_name = new_filename;
283 if (doc->file_type->id == GEANY_FILETYPES_NONE)
284 document_set_filetype(doc, filetypes_lookup_by_name(instantsave_default_ft));
286 /* force saving the file to enable all the related actions(tab name, filetype, etc.) */
287 document_save_file(doc, TRUE);
292 /* Save when focus out
294 * @param pointer ref to the current doc (struct GeanyDocument *)
296 * @return always FALSE = Just a one shot execution
299 static gboolean save_on_focus_out_idle(gpointer p_cur_doc)
301 GeanyDocument *cur_doc = p_cur_doc;
303 if (DOC_VALID(cur_doc) && (cur_doc->file_name != NULL))
304 document_save_file(cur_doc, FALSE);
306 return FALSE;
310 /* Autosave the current file when the focus out of the _editor_
312 * Get the SCN_FOCUSOUT signal, and then ask plugin_idle_add()
313 * to save the current doc when idle
315 * @return always FALSE = Non block signals
318 static gboolean on_document_focus_out(GObject *object, GeanyEditor *editor,
319 SCNotification *nt, gpointer data)
321 if (nt->nmhdr.code == SCN_FOCUSOUT
322 && enable_autosave_losing_focus
323 && editor->document->file_name != NULL)
325 plugin_idle_add(geany_plugin, save_on_focus_out_idle, editor->document);
328 return FALSE;
332 PluginCallback plugin_callbacks[] =
334 { "document-new", (GCallback) &instantsave_document_new_cb, FALSE, NULL },
335 { "document-save", (GCallback) &backupcopy_document_save_cb, FALSE, NULL },
336 { "editor-notify", (GCallback) &on_document_focus_out, FALSE, NULL },
337 { NULL, NULL, FALSE, NULL }
341 static gboolean auto_save(gpointer data)
343 GeanyDocument *doc;
344 GeanyDocument *cur_doc = document_get_current();
345 gint i, max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(geany->main_widgets->notebook));
346 gint saved_files = 0;
348 if (cur_doc == NULL)
349 return TRUE;
351 if (autosave_save_all)
353 for (i = 0; i < max; i++)
355 doc = document_get_from_page(i);
357 /* skip current file (save it last), skip files without name */
358 if (doc != cur_doc && doc->file_name != NULL)
359 if (document_save_file(doc, FALSE))
360 saved_files++;
363 /* finally save current file, do it after all other files to get correct window title and
364 * symbol list */
365 if (cur_doc->file_name != NULL)
366 if (document_save_file(cur_doc, FALSE))
367 saved_files++;
369 if (saved_files > 0 && autosave_print_msg)
370 ui_set_statusbar(FALSE, ngettext(
371 "Autosave: Saved %d file automatically.",
372 "Autosave: Saved %d files automatically.", saved_files),
373 saved_files);
375 return TRUE;
379 static void autosave_set_timeout(void)
381 if (! enable_autosave)
382 return;
384 if (autosave_src_id != 0)
385 g_source_remove(autosave_src_id);
386 autosave_src_id = g_timeout_add(autosave_interval * 1000, (GSourceFunc) auto_save, NULL);
390 void plugin_init(GeanyData *data)
392 GKeyFile *config = g_key_file_new();
393 gchar *tmp;
395 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S, "plugins",
396 G_DIR_SEPARATOR_S, "saveactions", G_DIR_SEPARATOR_S, "saveactions.conf", NULL);
398 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
400 enable_autosave = utils_get_setting_boolean(
401 config, "saveactions", "enable_autosave", FALSE);
402 enable_autosave_losing_focus = utils_get_setting_boolean(
403 config, "saveactions", "enable_autosave_losing_focus", FALSE);
404 enable_instantsave = utils_get_setting_boolean(
405 config, "saveactions", "enable_instantsave", FALSE);
406 enable_backupcopy = utils_get_setting_boolean(
407 config, "saveactions", "enable_backupcopy", FALSE);
409 instantsave_default_ft = utils_get_setting_string(config, "instantsave", "default_ft",
410 filetypes[GEANY_FILETYPES_NONE]->name);
412 autosave_src_id = 0; /* mark as invalid */
413 autosave_interval = utils_get_setting_integer(config, "autosave", "interval", 300);
414 autosave_print_msg = utils_get_setting_boolean(config, "autosave", "print_messages", FALSE);
415 autosave_save_all = utils_get_setting_boolean(config, "autosave", "save_all", FALSE);
416 if (enable_autosave)
417 autosave_set_timeout();
419 backupcopy_dir_levels = utils_get_setting_integer(config, "backupcopy", "dir_levels", 0);
420 backupcopy_time_fmt = utils_get_setting_string(
421 config, "backupcopy", "time_fmt", "%Y-%m-%d-%H-%M-%S");
422 tmp = utils_get_setting_string(config, "backupcopy", "backup_dir", g_get_tmp_dir());
423 backupcopy_set_backup_dir(tmp);
425 g_key_file_free(config);
426 g_free(tmp);
430 static void backupcopy_dir_button_clicked_cb(GtkButton *button, gpointer item)
432 /** TODO add win32_show_pref_file_dialog to the plugin API and use it **/
434 #ifdef G_OS_WIN32
435 win32_show_pref_file_dialog(item);
436 #else
438 GtkWidget *dialog;
439 gchar *text;
441 /* initialize the dialog */
442 dialog = gtk_file_chooser_dialog_new(_("Select Directory"), NULL,
443 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
444 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
445 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
447 text = utils_get_locale_from_utf8(gtk_entry_get_text(GTK_ENTRY(item)));
448 if (!EMPTY(text))
449 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), text);
451 /* run it */
452 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
454 gchar *utf8_filename, *tmp;
456 tmp = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
457 utf8_filename = utils_get_utf8_from_locale(tmp);
459 gtk_entry_set_text(GTK_ENTRY(item), utf8_filename);
461 g_free(utf8_filename);
462 g_free(tmp);
465 gtk_widget_destroy(dialog);
469 static void configure_response_cb(GtkDialog *dialog, gint response, G_GNUC_UNUSED gpointer data)
471 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
473 GKeyFile *config = g_key_file_new();
474 gchar *str;
475 const gchar *text_dir, *text_time;
476 gchar *config_dir = g_path_get_dirname(config_file);
478 enable_autosave = gtk_toggle_button_get_active(
479 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_autosave));
480 enable_autosave_losing_focus = gtk_toggle_button_get_active(
481 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_autosave_losing_focus));
482 enable_instantsave = gtk_toggle_button_get_active(
483 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_instantsave));
484 enable_backupcopy = gtk_toggle_button_get_active(
485 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_backupcopy));
487 autosave_interval = gtk_spin_button_get_value_as_int(
488 GTK_SPIN_BUTTON(pref_widgets.autosave_interval_spin));
489 autosave_print_msg = gtk_toggle_button_get_active(
490 GTK_TOGGLE_BUTTON(pref_widgets.autosave_print_msg_checkbox));
491 autosave_save_all = gtk_toggle_button_get_active(
492 GTK_TOGGLE_BUTTON(pref_widgets.autosave_save_all_radio2));
494 g_free(instantsave_default_ft);
495 instantsave_default_ft = gtk_combo_box_text_get_active_text(
496 GTK_COMBO_BOX_TEXT(pref_widgets.instantsave_ft_combo));
498 text_dir = gtk_entry_get_text(GTK_ENTRY(pref_widgets.backupcopy_entry_dir));
499 text_time = gtk_entry_get_text(GTK_ENTRY(pref_widgets.backupcopy_entry_time));
500 backupcopy_dir_levels = gtk_spin_button_get_value_as_int(
501 GTK_SPIN_BUTTON(pref_widgets.backupcopy_spin_dir_levels));
504 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
506 g_key_file_set_boolean(config, "saveactions", "enable_autosave", enable_autosave);
507 g_key_file_set_boolean(config, "saveactions", "enable_autosave_losing_focus", enable_autosave_losing_focus);
508 g_key_file_set_boolean(config, "saveactions", "enable_instantsave", enable_instantsave);
509 g_key_file_set_boolean(config, "saveactions", "enable_backupcopy", enable_backupcopy);
511 g_key_file_set_boolean(config, "autosave", "print_messages", autosave_print_msg);
512 g_key_file_set_boolean(config, "autosave", "save_all", autosave_save_all);
513 g_key_file_set_integer(config, "autosave", "interval", autosave_interval);
515 if (instantsave_default_ft != NULL)
516 g_key_file_set_string(config, "instantsave", "default_ft", instantsave_default_ft);
518 g_key_file_set_integer(config, "backupcopy", "dir_levels", backupcopy_dir_levels);
519 g_key_file_set_string(config, "backupcopy", "time_fmt", text_time);
520 SETPTR(backupcopy_time_fmt, g_strdup(text_time));
521 if (enable_backupcopy)
523 if (!EMPTY(text_dir) && backupcopy_set_backup_dir(text_dir))
525 g_key_file_set_string(config, "backupcopy", "backup_dir", text_dir);
527 else
529 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
530 _("Backup directory does not exist or is not writable."));
535 if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
537 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
538 _("Plugin configuration directory could not be created."));
540 else
542 /* write config to file */
543 str = g_key_file_to_data(config, NULL, NULL);
544 utils_write_file(config_file, str);
545 g_free(str);
548 if (enable_autosave)
549 autosave_set_timeout(); /* apply the changes */
551 g_free(config_dir);
552 g_key_file_free(config);
557 static void checkbox_toggled_cb(GtkToggleButton *tb, gpointer data)
559 gboolean enable = gtk_toggle_button_get_active(tb);
561 switch (GPOINTER_TO_INT(data))
563 case NOTEBOOK_PAGE_AUTOSAVE:
565 gtk_widget_set_sensitive(pref_widgets.autosave_interval_spin, enable);
566 gtk_widget_set_sensitive(pref_widgets.autosave_print_msg_checkbox, enable);
567 gtk_widget_set_sensitive(pref_widgets.autosave_save_all_radio1, enable);
568 gtk_widget_set_sensitive(pref_widgets.autosave_save_all_radio2, enable);
569 break;
571 case NOTEBOOK_PAGE_INSTANTSAVE:
573 gtk_widget_set_sensitive(pref_widgets.instantsave_ft_combo, enable);
574 break;
576 case NOTEBOOK_PAGE_BACKUPCOPY:
578 gtk_widget_set_sensitive(pref_widgets.backupcopy_entry_dir, enable);
579 gtk_widget_set_sensitive(pref_widgets.backupcopy_entry_time, enable);
580 gtk_widget_set_sensitive(pref_widgets.backupcopy_spin_dir_levels, enable);
581 break;
588 GtkWidget *plugin_configure(GtkDialog *dialog)
590 GtkWidget *vbox, *label, *notebook_vbox, *checkbox_enable;
591 GtkWidget *notebook, *inner_vbox;
593 vbox = gtk_vbox_new(FALSE, 6);
595 notebook = gtk_notebook_new();
596 gtk_widget_set_can_focus(notebook, FALSE);
597 gtk_container_set_border_width(GTK_CONTAINER(notebook), 5);
598 gtk_box_pack_start(GTK_BOX(vbox), notebook, FALSE, TRUE, 0);
601 * Auto Save
604 GtkWidget *spin, *hbox, *checkbox, *checkbox_enable_as_lf, *radio1, *radio2;
606 notebook_vbox = gtk_vbox_new(FALSE, 2);
607 inner_vbox = gtk_vbox_new(FALSE, 5);
608 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox), 5);
609 gtk_box_pack_start(GTK_BOX(notebook_vbox), inner_vbox, TRUE, TRUE, 5);
610 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook),
611 notebook_vbox, gtk_label_new(_("Auto Save")), NOTEBOOK_PAGE_AUTOSAVE);
613 checkbox_enable_as_lf = gtk_check_button_new_with_mnemonic(_("Enable save when losing _focus"));
614 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable_as_lf), FALSE);
615 pref_widgets.checkbox_enable_autosave_losing_focus = checkbox_enable_as_lf;
616 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable_as_lf, FALSE, FALSE, 6);
617 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable_as_lf), enable_autosave_losing_focus);
619 checkbox_enable = gtk_check_button_new_with_mnemonic(_("_Enable"));
620 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable), FALSE);
621 pref_widgets.checkbox_enable_autosave = checkbox_enable;
622 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable, FALSE, FALSE, 6);
623 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable), enable_autosave);
624 g_signal_connect(checkbox_enable, "toggled",
625 G_CALLBACK(checkbox_toggled_cb), GINT_TO_POINTER(NOTEBOOK_PAGE_AUTOSAVE));
627 label = gtk_label_new_with_mnemonic(_("Auto save _interval:"));
628 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
629 gtk_box_pack_start(GTK_BOX(inner_vbox), label, TRUE, TRUE, 0);
631 pref_widgets.autosave_interval_spin = spin = gtk_spin_button_new_with_range(1, 1800, 1);
632 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), autosave_interval);
633 gtk_label_set_mnemonic_widget(GTK_LABEL(label), spin);
635 label = gtk_label_new(_("seconds"));
637 hbox = gtk_hbox_new(FALSE, 5);
638 gtk_box_pack_start(GTK_BOX(hbox), spin, TRUE, TRUE, 0);
639 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
641 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 5);
643 checkbox = gtk_check_button_new_with_mnemonic(
644 _("_Print status message if files have been automatically saved"));
645 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox), FALSE);
646 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), autosave_print_msg);
647 gtk_label_set_mnemonic_widget(GTK_LABEL(label), checkbox);
648 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox, FALSE, FALSE, 5);
649 pref_widgets.autosave_print_msg_checkbox = checkbox;
651 radio1 = gtk_radio_button_new_with_mnemonic(NULL,
652 _("Save only current open _file"));
653 pref_widgets.autosave_save_all_radio1 = radio1;
654 gtk_label_set_mnemonic_widget(GTK_LABEL(label), radio1);
655 gtk_button_set_focus_on_click(GTK_BUTTON(radio1), FALSE);
656 gtk_container_add(GTK_CONTAINER(inner_vbox), radio1);
658 radio2 = gtk_radio_button_new_with_mnemonic_from_widget(
659 GTK_RADIO_BUTTON(radio1), _("Sa_ve all open files"));
660 pref_widgets.autosave_save_all_radio2 = radio2;
661 gtk_label_set_mnemonic_widget(GTK_LABEL(label), radio2);
662 gtk_button_set_focus_on_click(GTK_BUTTON(radio2), FALSE);
663 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio2), autosave_save_all);
664 gtk_container_add(GTK_CONTAINER(inner_vbox), radio2);
667 * Instant Save
670 GtkWidget *combo;
671 guint i;
672 const GSList *node;
674 notebook_vbox = gtk_vbox_new(FALSE, 2);
675 inner_vbox = gtk_vbox_new(FALSE, 5);
676 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox), 5);
677 gtk_box_pack_start(GTK_BOX(notebook_vbox), inner_vbox, TRUE, TRUE, 5);
678 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook),
679 notebook_vbox, gtk_label_new(_("Instant Save")), NOTEBOOK_PAGE_INSTANTSAVE);
681 checkbox_enable = gtk_check_button_new_with_mnemonic(_("_Enable"));
682 pref_widgets.checkbox_enable_instantsave = checkbox_enable;
683 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable), FALSE);
684 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable), enable_instantsave);
685 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable, FALSE, FALSE, 6);
686 g_signal_connect(checkbox_enable, "toggled",
687 G_CALLBACK(checkbox_toggled_cb), GINT_TO_POINTER(NOTEBOOK_PAGE_INSTANTSAVE));
689 label = gtk_label_new_with_mnemonic(_("_Filetype to use for newly opened files:"));
690 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
691 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 0);
693 pref_widgets.instantsave_ft_combo = combo = gtk_combo_box_text_new();
694 i = 0;
695 foreach_slist(node, filetypes_get_sorted_by_name())
697 GeanyFiletype *ft = node->data;
699 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), ft->name);
701 if (utils_str_equal(ft->name, instantsave_default_ft))
702 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), i);
703 i++;
705 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(combo), 3);
706 gtk_label_set_mnemonic_widget(GTK_LABEL(label), combo);
707 gtk_box_pack_start(GTK_BOX(inner_vbox), combo, FALSE, FALSE, 0);
710 * Backup Copy
713 GtkWidget *hbox, *entry_dir, *entry_time, *button, *image, *spin_dir_levels;
715 notebook_vbox = gtk_vbox_new(FALSE, 2);
716 inner_vbox = gtk_vbox_new(FALSE, 5);
717 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox), 5);
718 gtk_box_pack_start(GTK_BOX(notebook_vbox), inner_vbox, TRUE, TRUE, 5);
719 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook),
720 notebook_vbox, gtk_label_new(_("Backup Copy")), NOTEBOOK_PAGE_BACKUPCOPY);
722 checkbox_enable = gtk_check_button_new_with_mnemonic(_("_Enable"));
723 pref_widgets.checkbox_enable_backupcopy = checkbox_enable;
724 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable), FALSE);
725 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable), enable_backupcopy);
726 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable, FALSE, FALSE, 6);
727 g_signal_connect(checkbox_enable, "toggled",
728 G_CALLBACK(checkbox_toggled_cb), GINT_TO_POINTER(NOTEBOOK_PAGE_BACKUPCOPY));
730 label = gtk_label_new_with_mnemonic(_("_Directory to save backup files in:"));
731 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
732 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 0);
734 pref_widgets.backupcopy_entry_dir = entry_dir = gtk_entry_new();
735 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry_dir);
736 if (!EMPTY(backupcopy_backup_dir))
737 gtk_entry_set_text(GTK_ENTRY(entry_dir), backupcopy_backup_dir);
739 button = gtk_button_new();
740 g_signal_connect(button, "clicked",
741 G_CALLBACK(backupcopy_dir_button_clicked_cb), entry_dir);
743 image = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
744 gtk_container_add(GTK_CONTAINER(button), image);
746 hbox = gtk_hbox_new(FALSE, 6);
747 gtk_box_pack_start(GTK_BOX(hbox), entry_dir, TRUE, TRUE, 0);
748 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
750 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 0);
752 label = gtk_label_new_with_mnemonic(
753 _("Date/_Time format for backup files (\"man strftime\" for details):"));
754 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
755 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 7);
757 pref_widgets.backupcopy_entry_time = entry_time = gtk_entry_new();
758 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry_time);
759 if (!EMPTY(backupcopy_time_fmt))
760 gtk_entry_set_text(GTK_ENTRY(entry_time), backupcopy_time_fmt);
761 gtk_box_pack_start(GTK_BOX(inner_vbox), entry_time, FALSE, FALSE, 0);
763 hbox = gtk_hbox_new(FALSE, 6);
765 label = gtk_label_new_with_mnemonic(
766 _("Directory _levels to include in the backup destination:"));
767 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
768 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
770 spin_dir_levels = gtk_spin_button_new_with_range(0, 20, 1);
771 pref_widgets.backupcopy_spin_dir_levels = spin_dir_levels;
772 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin_dir_levels), backupcopy_dir_levels);
773 gtk_label_set_mnemonic_widget(GTK_LABEL(label), spin_dir_levels);
774 gtk_box_pack_start(GTK_BOX(hbox), spin_dir_levels, FALSE, FALSE, 0);
776 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 7);
779 /* manually emit the toggled signal of the enable checkboxes to update the widget sensitivity */
780 g_signal_emit_by_name(pref_widgets.checkbox_enable_autosave, "toggled");
781 g_signal_emit_by_name(pref_widgets.checkbox_enable_instantsave, "toggled");
782 g_signal_emit_by_name(pref_widgets.checkbox_enable_backupcopy, "toggled");
784 gtk_widget_show_all(vbox);
785 g_signal_connect(dialog, "response", G_CALLBACK(configure_response_cb), NULL);
787 return vbox;
791 void plugin_cleanup(void)
793 if (autosave_src_id != 0)
794 g_source_remove(autosave_src_id);
796 g_free(instantsave_default_ft);
798 g_free(backupcopy_backup_dir);
799 g_free(backupcopy_time_fmt);
801 g_free(config_file);