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.
27 #include "geanyplugin.h"
28 #include "gtkcompat.h"
34 #include <glib/gstdio.h>
37 GeanyPlugin
*geany_plugin
;
38 GeanyData
*geany_data
;
39 GeanyFunctions
*geany_functions
;
42 PLUGIN_VERSION_CHECK(GEANY_API_VERSION
)
44 PLUGIN_SET_INFO(_("Save Actions"), _("This plugin provides different actions related to saving of files."),
45 VERSION
, _("The Geany developer team"))
50 NOTEBOOK_PAGE_AUTOSAVE
= 0,
51 NOTEBOOK_PAGE_INSTANTSAVE
,
52 NOTEBOOK_PAGE_BACKUPCOPY
57 GtkWidget
*checkbox_enable_autosave
;
58 GtkWidget
*checkbox_enable_autosave_losing_focus
;
59 GtkWidget
*checkbox_enable_instantsave
;
60 GtkWidget
*checkbox_enable_backupcopy
;
62 GtkWidget
*autosave_interval_spin
;
63 GtkWidget
*autosave_print_msg_checkbox
;
64 GtkWidget
*autosave_save_all_radio1
;
65 GtkWidget
*autosave_save_all_radio2
;
67 GtkWidget
*instantsave_ft_combo
;
69 GtkWidget
*backupcopy_entry_dir
;
70 GtkWidget
*backupcopy_entry_time
;
71 GtkWidget
*backupcopy_spin_dir_levels
;
76 static gboolean enable_autosave
;
77 static gboolean enable_autosave_losing_focus
;
78 static gboolean enable_instantsave
;
79 static gboolean enable_backupcopy
;
81 static gint autosave_interval
;
82 static gboolean autosave_print_msg
;
83 static gboolean autosave_save_all
;
84 static guint autosave_src_id
= 0;
86 static gchar
*instantsave_default_ft
;
88 static gchar
*backupcopy_backup_dir
; /* path to an existing directory in locale encoding */
89 static gchar
*backupcopy_time_fmt
;
90 static gint backupcopy_dir_levels
;
92 static gchar
*config_file
;
95 /* Ensures utf8_dir exists and is writable and
96 * set backup_dir to the locale encoded form of utf8_dir */
97 static gboolean
backupcopy_set_backup_dir(const gchar
*utf8_dir
)
101 if (G_UNLIKELY(EMPTY(utf8_dir
)))
104 tmp
= utils_get_locale_from_utf8(utf8_dir
);
106 if (! g_path_is_absolute(tmp
) ||
107 ! g_file_test(tmp
, G_FILE_TEST_EXISTS
) ||
108 ! g_file_test(tmp
, G_FILE_TEST_IS_DIR
))
113 /** TODO add utils_is_file_writeable() to the plugin API and make use of it **/
115 SETPTR(backupcopy_backup_dir
, tmp
);
121 static gchar
*backupcopy_skip_root(gchar
*filename
)
123 /* first skip the root (e.g. c:\ on windows) */
124 const gchar
*dir
= g_path_skip_root(filename
);
126 /* if this has failed, use the filename again */
129 /* check again for leading / or \ */
130 while (*dir
== G_DIR_SEPARATOR
)
133 return (gchar
*) dir
;
137 static gchar
*backupcopy_create_dir_parts(const gchar
*filename
)
139 gint cnt_dir_parts
= 0;
147 if (backupcopy_dir_levels
== 0)
150 dirname
= g_path_get_dirname(filename
);
153 /* walk to the end of the string */
157 /* walk backwards to find directory parts */
160 if (*cp
== G_DIR_SEPARATOR
&& last_char
!= G_DIR_SEPARATOR
)
163 if (cnt_dir_parts
== backupcopy_dir_levels
)
170 result
= backupcopy_skip_root(cp
); /* skip leading slash/backslash and c:\ */
171 target_dir
= g_build_filename(backupcopy_backup_dir
, result
, NULL
);
173 error
= utils_mkdir(target_dir
, TRUE
);
176 ui_set_statusbar(FALSE
, _("Backup Copy: Directory could not be created (%s)."),
179 result
= g_strdup(""); /* return an empty string in case of an error */
182 result
= g_strdup(result
);
191 static void backupcopy_document_save_cb(GObject
*obj
, GeanyDocument
*doc
, gpointer user_data
)
194 gchar
*locale_filename_src
;
195 gchar
*locale_filename_dst
;
197 gchar
*dir_parts_src
;
202 if (! enable_backupcopy
)
205 locale_filename_src
= utils_get_locale_from_utf8(doc
->file_name
);
207 if ((src
= g_fopen(locale_filename_src
, "r")) == NULL
)
209 /* it's unlikely that this happens */
210 ui_set_statusbar(FALSE
, _("Backup Copy: File could not be read (%s)."),
212 g_free(locale_filename_src
);
216 stamp
= utils_get_date_time(backupcopy_time_fmt
, NULL
);
217 basename_src
= g_path_get_basename(locale_filename_src
);
218 dir_parts_src
= backupcopy_create_dir_parts(locale_filename_src
);
219 locale_filename_dst
= g_strconcat(
220 backupcopy_backup_dir
, G_DIR_SEPARATOR_S
,
221 dir_parts_src
, G_DIR_SEPARATOR_S
,
222 basename_src
, ".", stamp
, NULL
);
223 g_free(basename_src
);
224 g_free(dir_parts_src
);
227 if ((dst
= g_fopen(locale_filename_dst
, "wb")) == NULL
)
229 /* Use g_open() on non-Windows to set file permissions to 600 atomically.
230 * On Windows, seting file permissions would require specific Windows API. */
231 fd_dst
= g_open(locale_filename_dst
, O_CREAT
| O_WRONLY
, S_IWUSR
| S_IRUSR
);
232 if (fd_dst
== -1 || (dst
= fdopen(fd_dst
, "w")) == NULL
)
235 ui_set_statusbar(FALSE
, _("Backup Copy: File could not be saved (%s)."),
237 g_free(locale_filename_src
);
238 g_free(locale_filename_dst
);
246 while (fgets(buf
, sizeof(buf
), src
) != NULL
)
255 g_free(locale_filename_src
);
256 g_free(locale_filename_dst
);
261 static void instantsave_document_new_cb(GObject
*obj
, GeanyDocument
*doc
, gpointer user_data
)
263 if (enable_instantsave
&& doc
->file_name
== NULL
)
267 GeanyFiletype
*ft
= doc
->file_type
;
269 fd
= g_file_open_tmp("gis_XXXXXX", &new_filename
, NULL
);
271 close(fd
); /* close the returned file descriptor as we only need the filename */
273 if (ft
== NULL
|| ft
->id
== GEANY_FILETYPES_NONE
)
274 /* ft is NULL when a new file without template was opened, so use the
275 * configured default file type */
276 ft
= filetypes_lookup_by_name(instantsave_default_ft
);
279 /* add the filetype's default extension to the new filename */
280 SETPTR(new_filename
, g_strconcat(new_filename
, ".", ft
->extension
, NULL
));
282 doc
->file_name
= new_filename
;
284 if (doc
->file_type
->id
== GEANY_FILETYPES_NONE
)
285 document_set_filetype(doc
, filetypes_lookup_by_name(instantsave_default_ft
));
287 /* force saving the file to enable all the related actions(tab name, filetype, etc.) */
288 document_save_file(doc
, TRUE
);
293 /* Save when focus out
295 * @param pointer ref to the current doc (struct GeanyDocument *)
297 * @return always FALSE = Just a one shot execution
300 static gboolean
save_on_focus_out_idle(gpointer p_cur_doc
)
302 GeanyDocument
*cur_doc
= p_cur_doc
;
304 if (DOC_VALID(cur_doc
) && (cur_doc
->file_name
!= NULL
))
305 document_save_file(cur_doc
, FALSE
);
311 /* Autosave the current file when the focus out of the _editor_
313 * Get the SCN_FOCUSOUT signal, and then ask plugin_idle_add()
314 * to save the current doc when idle
316 * @return always FALSE = Non block signals
319 static gboolean
on_document_focus_out(GObject
*object
, GeanyEditor
*editor
,
320 SCNotification
*nt
, gpointer data
)
322 if (nt
->nmhdr
.code
== SCN_FOCUSOUT
323 && enable_autosave_losing_focus
324 && editor
->document
->file_name
!= NULL
)
326 plugin_idle_add(geany_plugin
, save_on_focus_out_idle
, editor
->document
);
333 PluginCallback plugin_callbacks
[] =
335 { "document-new", (GCallback
) &instantsave_document_new_cb
, FALSE
, NULL
},
336 { "document-save", (GCallback
) &backupcopy_document_save_cb
, FALSE
, NULL
},
337 { "editor-notify", (GCallback
) &on_document_focus_out
, FALSE
, NULL
},
338 { NULL
, NULL
, FALSE
, NULL
}
342 static gboolean
auto_save(gpointer data
)
345 GeanyDocument
*cur_doc
= document_get_current();
346 gint i
, max
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(geany
->main_widgets
->notebook
));
347 gint saved_files
= 0;
352 if (autosave_save_all
)
354 for (i
= 0; i
< max
; i
++)
356 doc
= document_get_from_page(i
);
358 /* skip current file (save it last), skip files without name */
359 if (doc
!= cur_doc
&& doc
->file_name
!= NULL
)
360 if (document_save_file(doc
, FALSE
))
364 /* finally save current file, do it after all other files to get correct window title and
366 if (cur_doc
->file_name
!= NULL
)
367 if (document_save_file(cur_doc
, FALSE
))
370 if (saved_files
> 0 && autosave_print_msg
)
371 ui_set_statusbar(FALSE
, ngettext(
372 "Autosave: Saved %d file automatically.",
373 "Autosave: Saved %d files automatically.", saved_files
),
380 static void autosave_set_timeout(void)
382 if (! enable_autosave
)
385 if (autosave_src_id
!= 0)
386 g_source_remove(autosave_src_id
);
387 autosave_src_id
= g_timeout_add(autosave_interval
* 1000, (GSourceFunc
) auto_save
, NULL
);
391 void plugin_init(GeanyData
*data
)
393 GKeyFile
*config
= g_key_file_new();
396 config_file
= g_strconcat(geany
->app
->configdir
, G_DIR_SEPARATOR_S
, "plugins",
397 G_DIR_SEPARATOR_S
, "saveactions", G_DIR_SEPARATOR_S
, "saveactions.conf", NULL
);
399 g_key_file_load_from_file(config
, config_file
, G_KEY_FILE_NONE
, NULL
);
401 enable_autosave
= utils_get_setting_boolean(
402 config
, "saveactions", "enable_autosave", FALSE
);
403 enable_autosave_losing_focus
= utils_get_setting_boolean(
404 config
, "saveactions", "enable_autosave_losing_focus", FALSE
);
405 enable_instantsave
= utils_get_setting_boolean(
406 config
, "saveactions", "enable_instantsave", FALSE
);
407 enable_backupcopy
= utils_get_setting_boolean(
408 config
, "saveactions", "enable_backupcopy", FALSE
);
410 instantsave_default_ft
= utils_get_setting_string(config
, "instantsave", "default_ft",
411 filetypes
[GEANY_FILETYPES_NONE
]->name
);
413 autosave_src_id
= 0; /* mark as invalid */
414 autosave_interval
= utils_get_setting_integer(config
, "autosave", "interval", 300);
415 autosave_print_msg
= utils_get_setting_boolean(config
, "autosave", "print_messages", FALSE
);
416 autosave_save_all
= utils_get_setting_boolean(config
, "autosave", "save_all", FALSE
);
418 autosave_set_timeout();
420 backupcopy_dir_levels
= utils_get_setting_integer(config
, "backupcopy", "dir_levels", 0);
421 backupcopy_time_fmt
= utils_get_setting_string(
422 config
, "backupcopy", "time_fmt", "%Y-%m-%d-%H-%M-%S");
423 tmp
= utils_get_setting_string(config
, "backupcopy", "backup_dir", g_get_tmp_dir());
424 backupcopy_set_backup_dir(tmp
);
426 g_key_file_free(config
);
431 static void backupcopy_dir_button_clicked_cb(GtkButton
*button
, gpointer item
)
433 /** TODO add win32_show_pref_file_dialog to the plugin API and use it **/
436 win32_show_pref_file_dialog(item);
442 /* initialize the dialog */
443 dialog
= gtk_file_chooser_dialog_new(_("Select Directory"), NULL
,
444 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
,
445 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
446 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
, NULL
);
448 text
= utils_get_locale_from_utf8(gtk_entry_get_text(GTK_ENTRY(item
)));
450 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), text
);
453 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
455 gchar
*utf8_filename
, *tmp
;
457 tmp
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
458 utf8_filename
= utils_get_utf8_from_locale(tmp
);
460 gtk_entry_set_text(GTK_ENTRY(item
), utf8_filename
);
462 g_free(utf8_filename
);
466 gtk_widget_destroy(dialog
);
470 static void configure_response_cb(GtkDialog
*dialog
, gint response
, G_GNUC_UNUSED gpointer data
)
472 if (response
== GTK_RESPONSE_OK
|| response
== GTK_RESPONSE_APPLY
)
474 GKeyFile
*config
= g_key_file_new();
476 const gchar
*text_dir
, *text_time
;
477 gchar
*config_dir
= g_path_get_dirname(config_file
);
479 enable_autosave
= gtk_toggle_button_get_active(
480 GTK_TOGGLE_BUTTON(pref_widgets
.checkbox_enable_autosave
));
481 enable_autosave_losing_focus
= gtk_toggle_button_get_active(
482 GTK_TOGGLE_BUTTON(pref_widgets
.checkbox_enable_autosave_losing_focus
));
483 enable_instantsave
= gtk_toggle_button_get_active(
484 GTK_TOGGLE_BUTTON(pref_widgets
.checkbox_enable_instantsave
));
485 enable_backupcopy
= gtk_toggle_button_get_active(
486 GTK_TOGGLE_BUTTON(pref_widgets
.checkbox_enable_backupcopy
));
488 autosave_interval
= gtk_spin_button_get_value_as_int(
489 GTK_SPIN_BUTTON(pref_widgets
.autosave_interval_spin
));
490 autosave_print_msg
= gtk_toggle_button_get_active(
491 GTK_TOGGLE_BUTTON(pref_widgets
.autosave_print_msg_checkbox
));
492 autosave_save_all
= gtk_toggle_button_get_active(
493 GTK_TOGGLE_BUTTON(pref_widgets
.autosave_save_all_radio2
));
495 g_free(instantsave_default_ft
);
496 instantsave_default_ft
= gtk_combo_box_text_get_active_text(
497 GTK_COMBO_BOX_TEXT(pref_widgets
.instantsave_ft_combo
));
499 text_dir
= gtk_entry_get_text(GTK_ENTRY(pref_widgets
.backupcopy_entry_dir
));
500 text_time
= gtk_entry_get_text(GTK_ENTRY(pref_widgets
.backupcopy_entry_time
));
501 backupcopy_dir_levels
= gtk_spin_button_get_value_as_int(
502 GTK_SPIN_BUTTON(pref_widgets
.backupcopy_spin_dir_levels
));
505 g_key_file_load_from_file(config
, config_file
, G_KEY_FILE_NONE
, NULL
);
507 g_key_file_set_boolean(config
, "saveactions", "enable_autosave", enable_autosave
);
508 g_key_file_set_boolean(config
, "saveactions", "enable_autosave_losing_focus", enable_autosave_losing_focus
);
509 g_key_file_set_boolean(config
, "saveactions", "enable_instantsave", enable_instantsave
);
510 g_key_file_set_boolean(config
, "saveactions", "enable_backupcopy", enable_backupcopy
);
512 g_key_file_set_boolean(config
, "autosave", "print_messages", autosave_print_msg
);
513 g_key_file_set_boolean(config
, "autosave", "save_all", autosave_save_all
);
514 g_key_file_set_integer(config
, "autosave", "interval", autosave_interval
);
516 if (instantsave_default_ft
!= NULL
)
517 g_key_file_set_string(config
, "instantsave", "default_ft", instantsave_default_ft
);
519 g_key_file_set_integer(config
, "backupcopy", "dir_levels", backupcopy_dir_levels
);
520 g_key_file_set_string(config
, "backupcopy", "time_fmt", text_time
);
521 SETPTR(backupcopy_time_fmt
, g_strdup(text_time
));
522 if (enable_backupcopy
)
524 if (!EMPTY(text_dir
) && backupcopy_set_backup_dir(text_dir
))
526 g_key_file_set_string(config
, "backupcopy", "backup_dir", text_dir
);
530 dialogs_show_msgbox(GTK_MESSAGE_ERROR
,
531 _("Backup directory does not exist or is not writable."));
536 if (! g_file_test(config_dir
, G_FILE_TEST_IS_DIR
) && utils_mkdir(config_dir
, TRUE
) != 0)
538 dialogs_show_msgbox(GTK_MESSAGE_ERROR
,
539 _("Plugin configuration directory could not be created."));
543 /* write config to file */
544 str
= g_key_file_to_data(config
, NULL
, NULL
);
545 utils_write_file(config_file
, str
);
550 autosave_set_timeout(); /* apply the changes */
553 g_key_file_free(config
);
558 static void checkbox_toggled_cb(GtkToggleButton
*tb
, gpointer data
)
560 gboolean enable
= gtk_toggle_button_get_active(tb
);
562 switch (GPOINTER_TO_INT(data
))
564 case NOTEBOOK_PAGE_AUTOSAVE
:
566 gtk_widget_set_sensitive(pref_widgets
.autosave_interval_spin
, enable
);
567 gtk_widget_set_sensitive(pref_widgets
.autosave_print_msg_checkbox
, enable
);
568 gtk_widget_set_sensitive(pref_widgets
.autosave_save_all_radio1
, enable
);
569 gtk_widget_set_sensitive(pref_widgets
.autosave_save_all_radio2
, enable
);
572 case NOTEBOOK_PAGE_INSTANTSAVE
:
574 gtk_widget_set_sensitive(pref_widgets
.instantsave_ft_combo
, enable
);
577 case NOTEBOOK_PAGE_BACKUPCOPY
:
579 gtk_widget_set_sensitive(pref_widgets
.backupcopy_entry_dir
, enable
);
580 gtk_widget_set_sensitive(pref_widgets
.backupcopy_entry_time
, enable
);
581 gtk_widget_set_sensitive(pref_widgets
.backupcopy_spin_dir_levels
, enable
);
589 GtkWidget
*plugin_configure(GtkDialog
*dialog
)
591 GtkWidget
*vbox
, *label
, *notebook_vbox
, *checkbox_enable
;
592 GtkWidget
*notebook
, *inner_vbox
;
594 vbox
= gtk_vbox_new(FALSE
, 6);
596 notebook
= gtk_notebook_new();
597 gtk_widget_set_can_focus(notebook
, FALSE
);
598 gtk_container_set_border_width(GTK_CONTAINER(notebook
), 5);
599 gtk_box_pack_start(GTK_BOX(vbox
), notebook
, FALSE
, TRUE
, 0);
605 GtkWidget
*spin
, *hbox
, *checkbox
, *checkbox_enable_as_lf
, *radio1
, *radio2
;
607 notebook_vbox
= gtk_vbox_new(FALSE
, 2);
608 inner_vbox
= gtk_vbox_new(FALSE
, 5);
609 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox
), 5);
610 gtk_box_pack_start(GTK_BOX(notebook_vbox
), inner_vbox
, TRUE
, TRUE
, 5);
611 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook
),
612 notebook_vbox
, gtk_label_new(_("Auto Save")), NOTEBOOK_PAGE_AUTOSAVE
);
614 checkbox_enable_as_lf
= gtk_check_button_new_with_mnemonic(_("Enable save when losing _focus"));
615 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable_as_lf
), FALSE
);
616 pref_widgets
.checkbox_enable_autosave_losing_focus
= checkbox_enable_as_lf
;
617 gtk_box_pack_start(GTK_BOX(inner_vbox
), checkbox_enable_as_lf
, FALSE
, FALSE
, 6);
618 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable_as_lf
), enable_autosave_losing_focus
);
620 checkbox_enable
= gtk_check_button_new_with_mnemonic(_("_Enable"));
621 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable
), FALSE
);
622 pref_widgets
.checkbox_enable_autosave
= checkbox_enable
;
623 gtk_box_pack_start(GTK_BOX(inner_vbox
), checkbox_enable
, FALSE
, FALSE
, 6);
624 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable
), enable_autosave
);
625 g_signal_connect(checkbox_enable
, "toggled",
626 G_CALLBACK(checkbox_toggled_cb
), GINT_TO_POINTER(NOTEBOOK_PAGE_AUTOSAVE
));
628 label
= gtk_label_new_with_mnemonic(_("Auto save _interval:"));
629 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
630 gtk_box_pack_start(GTK_BOX(inner_vbox
), label
, TRUE
, TRUE
, 0);
632 pref_widgets
.autosave_interval_spin
= spin
= gtk_spin_button_new_with_range(1, 1800, 1);
633 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin
), autosave_interval
);
634 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), spin
);
636 label
= gtk_label_new(_("seconds"));
638 hbox
= gtk_hbox_new(FALSE
, 5);
639 gtk_box_pack_start(GTK_BOX(hbox
), spin
, TRUE
, TRUE
, 0);
640 gtk_box_pack_start(GTK_BOX(hbox
), label
, FALSE
, FALSE
, 0);
642 gtk_box_pack_start(GTK_BOX(inner_vbox
), hbox
, FALSE
, FALSE
, 5);
644 checkbox
= gtk_check_button_new_with_mnemonic(
645 _("_Print status message if files have been automatically saved"));
646 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox
), FALSE
);
647 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox
), autosave_print_msg
);
648 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), checkbox
);
649 gtk_box_pack_start(GTK_BOX(inner_vbox
), checkbox
, FALSE
, FALSE
, 5);
650 pref_widgets
.autosave_print_msg_checkbox
= checkbox
;
652 radio1
= gtk_radio_button_new_with_mnemonic(NULL
,
653 _("Save only current open _file"));
654 pref_widgets
.autosave_save_all_radio1
= radio1
;
655 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), radio1
);
656 gtk_button_set_focus_on_click(GTK_BUTTON(radio1
), FALSE
);
657 gtk_container_add(GTK_CONTAINER(inner_vbox
), radio1
);
659 radio2
= gtk_radio_button_new_with_mnemonic_from_widget(
660 GTK_RADIO_BUTTON(radio1
), _("Sa_ve all open files"));
661 pref_widgets
.autosave_save_all_radio2
= radio2
;
662 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), radio2
);
663 gtk_button_set_focus_on_click(GTK_BUTTON(radio2
), FALSE
);
664 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio2
), autosave_save_all
);
665 gtk_container_add(GTK_CONTAINER(inner_vbox
), radio2
);
675 notebook_vbox
= gtk_vbox_new(FALSE
, 2);
676 inner_vbox
= gtk_vbox_new(FALSE
, 5);
677 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox
), 5);
678 gtk_box_pack_start(GTK_BOX(notebook_vbox
), inner_vbox
, TRUE
, TRUE
, 5);
679 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook
),
680 notebook_vbox
, gtk_label_new(_("Instant Save")), NOTEBOOK_PAGE_INSTANTSAVE
);
682 checkbox_enable
= gtk_check_button_new_with_mnemonic(_("_Enable"));
683 pref_widgets
.checkbox_enable_instantsave
= checkbox_enable
;
684 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable
), FALSE
);
685 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable
), enable_instantsave
);
686 gtk_box_pack_start(GTK_BOX(inner_vbox
), checkbox_enable
, FALSE
, FALSE
, 6);
687 g_signal_connect(checkbox_enable
, "toggled",
688 G_CALLBACK(checkbox_toggled_cb
), GINT_TO_POINTER(NOTEBOOK_PAGE_INSTANTSAVE
));
690 label
= gtk_label_new_with_mnemonic(_("_Filetype to use for newly opened files:"));
691 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
692 gtk_box_pack_start(GTK_BOX(inner_vbox
), label
, FALSE
, FALSE
, 0);
694 pref_widgets
.instantsave_ft_combo
= combo
= gtk_combo_box_text_new();
696 foreach_slist(node
, filetypes_get_sorted_by_name())
698 GeanyFiletype
*ft
= node
->data
;
700 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo
), ft
->name
);
702 if (utils_str_equal(ft
->name
, instantsave_default_ft
))
703 gtk_combo_box_set_active(GTK_COMBO_BOX(combo
), i
);
706 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(combo
), 3);
707 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), combo
);
708 gtk_box_pack_start(GTK_BOX(inner_vbox
), combo
, FALSE
, FALSE
, 0);
714 GtkWidget
*hbox
, *entry_dir
, *entry_time
, *button
, *image
, *spin_dir_levels
;
716 notebook_vbox
= gtk_vbox_new(FALSE
, 2);
717 inner_vbox
= gtk_vbox_new(FALSE
, 5);
718 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox
), 5);
719 gtk_box_pack_start(GTK_BOX(notebook_vbox
), inner_vbox
, TRUE
, TRUE
, 5);
720 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook
),
721 notebook_vbox
, gtk_label_new(_("Backup Copy")), NOTEBOOK_PAGE_BACKUPCOPY
);
723 checkbox_enable
= gtk_check_button_new_with_mnemonic(_("_Enable"));
724 pref_widgets
.checkbox_enable_backupcopy
= checkbox_enable
;
725 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable
), FALSE
);
726 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable
), enable_backupcopy
);
727 gtk_box_pack_start(GTK_BOX(inner_vbox
), checkbox_enable
, FALSE
, FALSE
, 6);
728 g_signal_connect(checkbox_enable
, "toggled",
729 G_CALLBACK(checkbox_toggled_cb
), GINT_TO_POINTER(NOTEBOOK_PAGE_BACKUPCOPY
));
731 label
= gtk_label_new_with_mnemonic(_("_Directory to save backup files in:"));
732 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
733 gtk_box_pack_start(GTK_BOX(inner_vbox
), label
, FALSE
, FALSE
, 0);
735 pref_widgets
.backupcopy_entry_dir
= entry_dir
= gtk_entry_new();
736 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), entry_dir
);
737 if (!EMPTY(backupcopy_backup_dir
))
738 gtk_entry_set_text(GTK_ENTRY(entry_dir
), backupcopy_backup_dir
);
740 button
= gtk_button_new();
741 g_signal_connect(button
, "clicked",
742 G_CALLBACK(backupcopy_dir_button_clicked_cb
), entry_dir
);
744 image
= gtk_image_new_from_stock(GTK_STOCK_OPEN
, GTK_ICON_SIZE_BUTTON
);
745 gtk_container_add(GTK_CONTAINER(button
), image
);
747 hbox
= gtk_hbox_new(FALSE
, 6);
748 gtk_box_pack_start(GTK_BOX(hbox
), entry_dir
, TRUE
, TRUE
, 0);
749 gtk_box_pack_start(GTK_BOX(hbox
), button
, FALSE
, FALSE
, 0);
751 gtk_box_pack_start(GTK_BOX(inner_vbox
), hbox
, FALSE
, FALSE
, 0);
753 label
= gtk_label_new_with_mnemonic(
754 _("Date/_Time format for backup files (\"man strftime\" for details):"));
755 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
756 gtk_box_pack_start(GTK_BOX(inner_vbox
), label
, FALSE
, FALSE
, 7);
758 pref_widgets
.backupcopy_entry_time
= entry_time
= gtk_entry_new();
759 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), entry_time
);
760 if (!EMPTY(backupcopy_time_fmt
))
761 gtk_entry_set_text(GTK_ENTRY(entry_time
), backupcopy_time_fmt
);
762 gtk_box_pack_start(GTK_BOX(inner_vbox
), entry_time
, FALSE
, FALSE
, 0);
764 hbox
= gtk_hbox_new(FALSE
, 6);
766 label
= gtk_label_new_with_mnemonic(
767 _("Directory _levels to include in the backup destination:"));
768 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
769 gtk_box_pack_start(GTK_BOX(hbox
), label
, FALSE
, FALSE
, 0);
771 spin_dir_levels
= gtk_spin_button_new_with_range(0, 20, 1);
772 pref_widgets
.backupcopy_spin_dir_levels
= spin_dir_levels
;
773 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin_dir_levels
), backupcopy_dir_levels
);
774 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), spin_dir_levels
);
775 gtk_box_pack_start(GTK_BOX(hbox
), spin_dir_levels
, FALSE
, FALSE
, 0);
777 gtk_box_pack_start(GTK_BOX(inner_vbox
), hbox
, FALSE
, FALSE
, 7);
780 /* manually emit the toggled signal of the enable checkboxes to update the widget sensitivity */
781 g_signal_emit_by_name(pref_widgets
.checkbox_enable_autosave
, "toggled");
782 g_signal_emit_by_name(pref_widgets
.checkbox_enable_instantsave
, "toggled");
783 g_signal_emit_by_name(pref_widgets
.checkbox_enable_backupcopy
, "toggled");
785 gtk_widget_show_all(vbox
);
786 g_signal_connect(dialog
, "response", G_CALLBACK(configure_response_cb
), NULL
);
792 void plugin_cleanup(void)
794 if (autosave_src_id
!= 0)
795 g_source_remove(autosave_src_id
);
797 g_free(instantsave_default_ft
);
799 g_free(backupcopy_backup_dir
);
800 g_free(backupcopy_time_fmt
);