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"
32 #include <glib/gstdio.h>
35 GeanyPlugin
*geany_plugin
;
36 GeanyData
*geany_data
;
37 GeanyFunctions
*geany_functions
;
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"))
48 NOTEBOOK_PAGE_AUTOSAVE
= 0,
49 NOTEBOOK_PAGE_INSTANTSAVE
,
50 NOTEBOOK_PAGE_BACKUPCOPY
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
;
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
)
99 if (G_UNLIKELY(EMPTY(utf8_dir
)))
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
))
111 /** TODO add utils_is_file_writeable() to the plugin API and make use of it **/
113 SETPTR(backupcopy_backup_dir
, tmp
);
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 */
127 /* check again for leading / or \ */
128 while (*dir
== G_DIR_SEPARATOR
)
131 return (gchar
*) dir
;
135 static gchar
*backupcopy_create_dir_parts(const gchar
*filename
)
137 gint cnt_dir_parts
= 0;
145 if (backupcopy_dir_levels
== 0)
148 dirname
= g_path_get_dirname(filename
);
151 /* walk to the end of the string */
155 /* walk backwards to find directory parts */
158 if (*cp
== G_DIR_SEPARATOR
&& last_char
!= G_DIR_SEPARATOR
)
161 if (cnt_dir_parts
== backupcopy_dir_levels
)
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
);
174 ui_set_statusbar(FALSE
, _("Backup Copy: Directory could not be created (%s)."),
177 result
= g_strdup(""); /* return an empty string in case of an error */
180 result
= g_strdup(result
);
189 static void backupcopy_document_save_cb(GObject
*obj
, GeanyDocument
*doc
, gpointer user_data
)
192 gchar
*locale_filename_src
;
193 gchar
*locale_filename_dst
;
195 gchar
*dir_parts_src
;
199 if (! enable_backupcopy
)
202 locale_filename_src
= utils_get_locale_from_utf8(doc
->file_name
);
204 if ((src
= g_fopen(locale_filename_src
, "r")) == NULL
)
206 /* it's unlikely that this happens */
207 ui_set_statusbar(FALSE
, _("Backup Copy: File could not be read (%s)."),
209 g_free(locale_filename_src
);
213 stamp
= utils_get_date_time(backupcopy_time_fmt
, NULL
);
214 basename_src
= g_path_get_basename(locale_filename_src
);
215 dir_parts_src
= backupcopy_create_dir_parts(locale_filename_src
);
216 locale_filename_dst
= g_strconcat(
217 backupcopy_backup_dir
, G_DIR_SEPARATOR_S
,
218 dir_parts_src
, G_DIR_SEPARATOR_S
,
219 basename_src
, ".", stamp
, NULL
);
220 g_free(basename_src
);
221 g_free(dir_parts_src
);
223 if ((dst
= g_fopen(locale_filename_dst
, "wb")) == NULL
)
225 ui_set_statusbar(FALSE
, _("Backup Copy: File could not be saved (%s)."),
227 g_free(locale_filename_src
);
228 g_free(locale_filename_dst
);
234 while (fgets(buf
, sizeof(buf
), src
) != NULL
)
241 g_free(locale_filename_src
);
242 g_free(locale_filename_dst
);
247 static void instantsave_document_new_cb(GObject
*obj
, GeanyDocument
*doc
, gpointer user_data
)
249 if (enable_instantsave
&& doc
->file_name
== NULL
)
253 GeanyFiletype
*ft
= doc
->file_type
;
255 fd
= g_file_open_tmp("gis_XXXXXX", &new_filename
, NULL
);
257 close(fd
); /* close the returned file descriptor as we only need the filename */
259 if (ft
== NULL
|| ft
->id
== GEANY_FILETYPES_NONE
)
260 /* ft is NULL when a new file without template was opened, so use the
261 * configured default file type */
262 ft
= filetypes_lookup_by_name(instantsave_default_ft
);
265 /* add the filetype's default extension to the new filename */
266 SETPTR(new_filename
, g_strconcat(new_filename
, ".", ft
->extension
, NULL
));
268 doc
->file_name
= new_filename
;
270 if (doc
->file_type
->id
== GEANY_FILETYPES_NONE
)
271 document_set_filetype(doc
, filetypes_lookup_by_name(instantsave_default_ft
));
273 /* force saving the file to enable all the related actions(tab name, filetype, etc.) */
274 document_save_file(doc
, TRUE
);
279 /* Save when focus out
281 * @param pointer ref to the current doc (struct GeanyDocument *)
283 * @return always FALSE = Just a one shot execution
286 static gboolean
save_on_focus_out_idle(gpointer p_cur_doc
)
288 GeanyDocument
*cur_doc
= p_cur_doc
;
290 if (DOC_VALID(cur_doc
) && (cur_doc
->file_name
!= NULL
))
291 document_save_file(cur_doc
, FALSE
);
297 /* Autosave the current file when the focus out of the _editor_
299 * Get the SCN_FOCUSOUT signal, and then ask plugin_idle_add()
300 * to save the current doc when idle
302 * @return always FALSE = Non block signals
305 static gboolean
on_document_focus_out(GObject
*object
, GeanyEditor
*editor
,
306 SCNotification
*nt
, gpointer data
)
308 if (nt
->nmhdr
.code
== SCN_FOCUSOUT
309 && enable_autosave_losing_focus
310 && editor
->document
->file_name
!= NULL
)
312 plugin_idle_add(geany_plugin
, save_on_focus_out_idle
, editor
->document
);
319 PluginCallback plugin_callbacks
[] =
321 { "document-new", (GCallback
) &instantsave_document_new_cb
, FALSE
, NULL
},
322 { "document-save", (GCallback
) &backupcopy_document_save_cb
, FALSE
, NULL
},
323 { "editor-notify", (GCallback
) &on_document_focus_out
, FALSE
, NULL
},
324 { NULL
, NULL
, FALSE
, NULL
}
328 static gboolean
auto_save(gpointer data
)
331 GeanyDocument
*cur_doc
= document_get_current();
332 gint i
, max
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(geany
->main_widgets
->notebook
));
333 gint saved_files
= 0;
338 if (autosave_save_all
)
340 for (i
= 0; i
< max
; i
++)
342 doc
= document_get_from_page(i
);
344 /* skip current file (save it last), skip files without name */
345 if (doc
!= cur_doc
&& doc
->file_name
!= NULL
)
346 if (document_save_file(doc
, FALSE
))
350 /* finally save current file, do it after all other files to get correct window title and
352 if (cur_doc
->file_name
!= NULL
)
353 if (document_save_file(cur_doc
, FALSE
))
356 if (saved_files
> 0 && autosave_print_msg
)
357 ui_set_statusbar(FALSE
, ngettext(
358 "Autosave: Saved %d file automatically.",
359 "Autosave: Saved %d files automatically.", saved_files
),
366 static void autosave_set_timeout(void)
368 if (! enable_autosave
)
371 if (autosave_src_id
!= 0)
372 g_source_remove(autosave_src_id
);
373 autosave_src_id
= g_timeout_add(autosave_interval
* 1000, (GSourceFunc
) auto_save
, NULL
);
377 void plugin_init(GeanyData
*data
)
379 GKeyFile
*config
= g_key_file_new();
382 config_file
= g_strconcat(geany
->app
->configdir
, G_DIR_SEPARATOR_S
, "plugins",
383 G_DIR_SEPARATOR_S
, "saveactions", G_DIR_SEPARATOR_S
, "saveactions.conf", NULL
);
385 g_key_file_load_from_file(config
, config_file
, G_KEY_FILE_NONE
, NULL
);
387 enable_autosave
= utils_get_setting_boolean(
388 config
, "saveactions", "enable_autosave", FALSE
);
389 enable_autosave_losing_focus
= utils_get_setting_boolean(
390 config
, "saveactions", "enable_autosave_losing_focus", FALSE
);
391 enable_instantsave
= utils_get_setting_boolean(
392 config
, "saveactions", "enable_instantsave", FALSE
);
393 enable_backupcopy
= utils_get_setting_boolean(
394 config
, "saveactions", "enable_backupcopy", FALSE
);
396 instantsave_default_ft
= utils_get_setting_string(config
, "instantsave", "default_ft",
397 filetypes
[GEANY_FILETYPES_NONE
]->name
);
399 autosave_src_id
= 0; /* mark as invalid */
400 autosave_interval
= utils_get_setting_integer(config
, "autosave", "interval", 300);
401 autosave_print_msg
= utils_get_setting_boolean(config
, "autosave", "print_messages", FALSE
);
402 autosave_save_all
= utils_get_setting_boolean(config
, "autosave", "save_all", FALSE
);
404 autosave_set_timeout();
406 backupcopy_dir_levels
= utils_get_setting_integer(config
, "backupcopy", "dir_levels", 0);
407 backupcopy_time_fmt
= utils_get_setting_string(
408 config
, "backupcopy", "time_fmt", "%Y-%m-%d-%H-%M-%S");
409 tmp
= utils_get_setting_string(config
, "backupcopy", "backup_dir", g_get_tmp_dir());
410 backupcopy_set_backup_dir(tmp
);
412 g_key_file_free(config
);
417 static void backupcopy_dir_button_clicked_cb(GtkButton
*button
, gpointer item
)
419 /** TODO add win32_show_pref_file_dialog to the plugin API and use it **/
422 win32_show_pref_file_dialog(item);
428 /* initialize the dialog */
429 dialog
= gtk_file_chooser_dialog_new(_("Select Directory"), NULL
,
430 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
,
431 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
432 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
, NULL
);
434 text
= utils_get_locale_from_utf8(gtk_entry_get_text(GTK_ENTRY(item
)));
436 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), text
);
439 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
441 gchar
*utf8_filename
, *tmp
;
443 tmp
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
444 utf8_filename
= utils_get_utf8_from_locale(tmp
);
446 gtk_entry_set_text(GTK_ENTRY(item
), utf8_filename
);
448 g_free(utf8_filename
);
452 gtk_widget_destroy(dialog
);
456 static void configure_response_cb(GtkDialog
*dialog
, gint response
, G_GNUC_UNUSED gpointer data
)
458 if (response
== GTK_RESPONSE_OK
|| response
== GTK_RESPONSE_APPLY
)
460 GKeyFile
*config
= g_key_file_new();
462 const gchar
*text_dir
, *text_time
;
463 gchar
*config_dir
= g_path_get_dirname(config_file
);
465 enable_autosave
= gtk_toggle_button_get_active(
466 GTK_TOGGLE_BUTTON(pref_widgets
.checkbox_enable_autosave
));
467 enable_autosave_losing_focus
= gtk_toggle_button_get_active(
468 GTK_TOGGLE_BUTTON(pref_widgets
.checkbox_enable_autosave_losing_focus
));
469 enable_instantsave
= gtk_toggle_button_get_active(
470 GTK_TOGGLE_BUTTON(pref_widgets
.checkbox_enable_instantsave
));
471 enable_backupcopy
= gtk_toggle_button_get_active(
472 GTK_TOGGLE_BUTTON(pref_widgets
.checkbox_enable_backupcopy
));
474 autosave_interval
= gtk_spin_button_get_value_as_int(
475 GTK_SPIN_BUTTON(pref_widgets
.autosave_interval_spin
));
476 autosave_print_msg
= gtk_toggle_button_get_active(
477 GTK_TOGGLE_BUTTON(pref_widgets
.autosave_print_msg_checkbox
));
478 autosave_save_all
= gtk_toggle_button_get_active(
479 GTK_TOGGLE_BUTTON(pref_widgets
.autosave_save_all_radio2
));
481 g_free(instantsave_default_ft
);
482 instantsave_default_ft
= gtk_combo_box_text_get_active_text(
483 GTK_COMBO_BOX_TEXT(pref_widgets
.instantsave_ft_combo
));
485 text_dir
= gtk_entry_get_text(GTK_ENTRY(pref_widgets
.backupcopy_entry_dir
));
486 text_time
= gtk_entry_get_text(GTK_ENTRY(pref_widgets
.backupcopy_entry_time
));
487 backupcopy_dir_levels
= gtk_spin_button_get_value_as_int(
488 GTK_SPIN_BUTTON(pref_widgets
.backupcopy_spin_dir_levels
));
491 g_key_file_load_from_file(config
, config_file
, G_KEY_FILE_NONE
, NULL
);
493 g_key_file_set_boolean(config
, "saveactions", "enable_autosave", enable_autosave
);
494 g_key_file_set_boolean(config
, "saveactions", "enable_autosave_losing_focus", enable_autosave_losing_focus
);
495 g_key_file_set_boolean(config
, "saveactions", "enable_instantsave", enable_instantsave
);
496 g_key_file_set_boolean(config
, "saveactions", "enable_backupcopy", enable_backupcopy
);
498 g_key_file_set_boolean(config
, "autosave", "print_messages", autosave_print_msg
);
499 g_key_file_set_boolean(config
, "autosave", "save_all", autosave_save_all
);
500 g_key_file_set_integer(config
, "autosave", "interval", autosave_interval
);
502 if (instantsave_default_ft
!= NULL
)
503 g_key_file_set_string(config
, "instantsave", "default_ft", instantsave_default_ft
);
505 g_key_file_set_integer(config
, "backupcopy", "dir_levels", backupcopy_dir_levels
);
506 g_key_file_set_string(config
, "backupcopy", "time_fmt", text_time
);
507 SETPTR(backupcopy_time_fmt
, g_strdup(text_time
));
508 if (enable_backupcopy
)
510 if (!EMPTY(text_dir
) && backupcopy_set_backup_dir(text_dir
))
512 g_key_file_set_string(config
, "backupcopy", "backup_dir", text_dir
);
516 dialogs_show_msgbox(GTK_MESSAGE_ERROR
,
517 _("Backup directory does not exist or is not writable."));
522 if (! g_file_test(config_dir
, G_FILE_TEST_IS_DIR
) && utils_mkdir(config_dir
, TRUE
) != 0)
524 dialogs_show_msgbox(GTK_MESSAGE_ERROR
,
525 _("Plugin configuration directory could not be created."));
529 /* write config to file */
530 str
= g_key_file_to_data(config
, NULL
, NULL
);
531 utils_write_file(config_file
, str
);
536 autosave_set_timeout(); /* apply the changes */
539 g_key_file_free(config
);
544 static void checkbox_toggled_cb(GtkToggleButton
*tb
, gpointer data
)
546 gboolean enable
= gtk_toggle_button_get_active(tb
);
548 switch (GPOINTER_TO_INT(data
))
550 case NOTEBOOK_PAGE_AUTOSAVE
:
552 gtk_widget_set_sensitive(pref_widgets
.autosave_interval_spin
, enable
);
553 gtk_widget_set_sensitive(pref_widgets
.autosave_print_msg_checkbox
, enable
);
554 gtk_widget_set_sensitive(pref_widgets
.autosave_save_all_radio1
, enable
);
555 gtk_widget_set_sensitive(pref_widgets
.autosave_save_all_radio2
, enable
);
558 case NOTEBOOK_PAGE_INSTANTSAVE
:
560 gtk_widget_set_sensitive(pref_widgets
.instantsave_ft_combo
, enable
);
563 case NOTEBOOK_PAGE_BACKUPCOPY
:
565 gtk_widget_set_sensitive(pref_widgets
.backupcopy_entry_dir
, enable
);
566 gtk_widget_set_sensitive(pref_widgets
.backupcopy_entry_time
, enable
);
567 gtk_widget_set_sensitive(pref_widgets
.backupcopy_spin_dir_levels
, enable
);
575 GtkWidget
*plugin_configure(GtkDialog
*dialog
)
577 GtkWidget
*vbox
, *label
, *notebook_vbox
, *checkbox_enable
;
578 GtkWidget
*notebook
, *inner_vbox
;
580 vbox
= gtk_vbox_new(FALSE
, 6);
582 notebook
= gtk_notebook_new();
583 gtk_widget_set_can_focus(notebook
, FALSE
);
584 gtk_container_set_border_width(GTK_CONTAINER(notebook
), 5);
585 gtk_box_pack_start(GTK_BOX(vbox
), notebook
, FALSE
, TRUE
, 0);
591 GtkWidget
*spin
, *hbox
, *checkbox
, *checkbox_enable_as_lf
, *radio1
, *radio2
;
593 notebook_vbox
= gtk_vbox_new(FALSE
, 2);
594 inner_vbox
= gtk_vbox_new(FALSE
, 5);
595 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox
), 5);
596 gtk_box_pack_start(GTK_BOX(notebook_vbox
), inner_vbox
, TRUE
, TRUE
, 5);
597 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook
),
598 notebook_vbox
, gtk_label_new(_("Auto Save")), NOTEBOOK_PAGE_AUTOSAVE
);
600 checkbox_enable_as_lf
= gtk_check_button_new_with_mnemonic(_("Enable save when losing _focus"));
601 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable_as_lf
), FALSE
);
602 pref_widgets
.checkbox_enable_autosave_losing_focus
= checkbox_enable_as_lf
;
603 gtk_box_pack_start(GTK_BOX(inner_vbox
), checkbox_enable_as_lf
, FALSE
, FALSE
, 6);
604 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable_as_lf
), enable_autosave_losing_focus
);
606 checkbox_enable
= gtk_check_button_new_with_mnemonic(_("_Enable"));
607 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable
), FALSE
);
608 pref_widgets
.checkbox_enable_autosave
= checkbox_enable
;
609 gtk_box_pack_start(GTK_BOX(inner_vbox
), checkbox_enable
, FALSE
, FALSE
, 6);
610 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable
), enable_autosave
);
611 g_signal_connect(checkbox_enable
, "toggled",
612 G_CALLBACK(checkbox_toggled_cb
), GINT_TO_POINTER(NOTEBOOK_PAGE_AUTOSAVE
));
614 label
= gtk_label_new_with_mnemonic(_("Auto save _interval:"));
615 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
616 gtk_box_pack_start(GTK_BOX(inner_vbox
), label
, TRUE
, TRUE
, 0);
618 pref_widgets
.autosave_interval_spin
= spin
= gtk_spin_button_new_with_range(1, 1800, 1);
619 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin
), autosave_interval
);
620 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), spin
);
622 label
= gtk_label_new(_("seconds"));
624 hbox
= gtk_hbox_new(FALSE
, 5);
625 gtk_box_pack_start(GTK_BOX(hbox
), spin
, TRUE
, TRUE
, 0);
626 gtk_box_pack_start(GTK_BOX(hbox
), label
, FALSE
, FALSE
, 0);
628 gtk_box_pack_start(GTK_BOX(inner_vbox
), hbox
, FALSE
, FALSE
, 5);
630 checkbox
= gtk_check_button_new_with_mnemonic(
631 _("_Print status message if files have been automatically saved"));
632 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox
), FALSE
);
633 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox
), autosave_print_msg
);
634 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), checkbox
);
635 gtk_box_pack_start(GTK_BOX(inner_vbox
), checkbox
, FALSE
, FALSE
, 5);
636 pref_widgets
.autosave_print_msg_checkbox
= checkbox
;
638 radio1
= gtk_radio_button_new_with_mnemonic(NULL
,
639 _("Save only current open _file"));
640 pref_widgets
.autosave_save_all_radio1
= radio1
;
641 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), radio1
);
642 gtk_button_set_focus_on_click(GTK_BUTTON(radio1
), FALSE
);
643 gtk_container_add(GTK_CONTAINER(inner_vbox
), radio1
);
645 radio2
= gtk_radio_button_new_with_mnemonic_from_widget(
646 GTK_RADIO_BUTTON(radio1
), _("Sa_ve all open files"));
647 pref_widgets
.autosave_save_all_radio2
= radio2
;
648 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), radio2
);
649 gtk_button_set_focus_on_click(GTK_BUTTON(radio2
), FALSE
);
650 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio2
), autosave_save_all
);
651 gtk_container_add(GTK_CONTAINER(inner_vbox
), radio2
);
661 notebook_vbox
= gtk_vbox_new(FALSE
, 2);
662 inner_vbox
= gtk_vbox_new(FALSE
, 5);
663 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox
), 5);
664 gtk_box_pack_start(GTK_BOX(notebook_vbox
), inner_vbox
, TRUE
, TRUE
, 5);
665 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook
),
666 notebook_vbox
, gtk_label_new(_("Instant Save")), NOTEBOOK_PAGE_INSTANTSAVE
);
668 checkbox_enable
= gtk_check_button_new_with_mnemonic(_("_Enable"));
669 pref_widgets
.checkbox_enable_instantsave
= checkbox_enable
;
670 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable
), FALSE
);
671 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable
), enable_instantsave
);
672 gtk_box_pack_start(GTK_BOX(inner_vbox
), checkbox_enable
, FALSE
, FALSE
, 6);
673 g_signal_connect(checkbox_enable
, "toggled",
674 G_CALLBACK(checkbox_toggled_cb
), GINT_TO_POINTER(NOTEBOOK_PAGE_INSTANTSAVE
));
676 label
= gtk_label_new_with_mnemonic(_("_Filetype to use for newly opened files:"));
677 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
678 gtk_box_pack_start(GTK_BOX(inner_vbox
), label
, FALSE
, FALSE
, 0);
680 pref_widgets
.instantsave_ft_combo
= combo
= gtk_combo_box_text_new();
682 foreach_slist(node
, filetypes_get_sorted_by_name())
684 GeanyFiletype
*ft
= node
->data
;
686 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo
), ft
->name
);
688 if (utils_str_equal(ft
->name
, instantsave_default_ft
))
689 gtk_combo_box_set_active(GTK_COMBO_BOX(combo
), i
);
692 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(combo
), 3);
693 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), combo
);
694 gtk_box_pack_start(GTK_BOX(inner_vbox
), combo
, FALSE
, FALSE
, 0);
700 GtkWidget
*hbox
, *entry_dir
, *entry_time
, *button
, *image
, *spin_dir_levels
;
702 notebook_vbox
= gtk_vbox_new(FALSE
, 2);
703 inner_vbox
= gtk_vbox_new(FALSE
, 5);
704 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox
), 5);
705 gtk_box_pack_start(GTK_BOX(notebook_vbox
), inner_vbox
, TRUE
, TRUE
, 5);
706 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook
),
707 notebook_vbox
, gtk_label_new(_("Backup Copy")), NOTEBOOK_PAGE_BACKUPCOPY
);
709 checkbox_enable
= gtk_check_button_new_with_mnemonic(_("_Enable"));
710 pref_widgets
.checkbox_enable_backupcopy
= checkbox_enable
;
711 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable
), FALSE
);
712 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable
), enable_backupcopy
);
713 gtk_box_pack_start(GTK_BOX(inner_vbox
), checkbox_enable
, FALSE
, FALSE
, 6);
714 g_signal_connect(checkbox_enable
, "toggled",
715 G_CALLBACK(checkbox_toggled_cb
), GINT_TO_POINTER(NOTEBOOK_PAGE_BACKUPCOPY
));
717 label
= gtk_label_new_with_mnemonic(_("_Directory to save backup files in:"));
718 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
719 gtk_box_pack_start(GTK_BOX(inner_vbox
), label
, FALSE
, FALSE
, 0);
721 pref_widgets
.backupcopy_entry_dir
= entry_dir
= gtk_entry_new();
722 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), entry_dir
);
723 if (!EMPTY(backupcopy_backup_dir
))
724 gtk_entry_set_text(GTK_ENTRY(entry_dir
), backupcopy_backup_dir
);
726 button
= gtk_button_new();
727 g_signal_connect(button
, "clicked",
728 G_CALLBACK(backupcopy_dir_button_clicked_cb
), entry_dir
);
730 image
= gtk_image_new_from_stock(GTK_STOCK_OPEN
, GTK_ICON_SIZE_BUTTON
);
731 gtk_container_add(GTK_CONTAINER(button
), image
);
733 hbox
= gtk_hbox_new(FALSE
, 6);
734 gtk_box_pack_start(GTK_BOX(hbox
), entry_dir
, TRUE
, TRUE
, 0);
735 gtk_box_pack_start(GTK_BOX(hbox
), button
, FALSE
, FALSE
, 0);
737 gtk_box_pack_start(GTK_BOX(inner_vbox
), hbox
, FALSE
, FALSE
, 0);
739 label
= gtk_label_new_with_mnemonic(
740 _("Date/_Time format for backup files (\"man strftime\" for details):"));
741 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
742 gtk_box_pack_start(GTK_BOX(inner_vbox
), label
, FALSE
, FALSE
, 7);
744 pref_widgets
.backupcopy_entry_time
= entry_time
= gtk_entry_new();
745 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), entry_time
);
746 if (!EMPTY(backupcopy_time_fmt
))
747 gtk_entry_set_text(GTK_ENTRY(entry_time
), backupcopy_time_fmt
);
748 gtk_box_pack_start(GTK_BOX(inner_vbox
), entry_time
, FALSE
, FALSE
, 0);
750 hbox
= gtk_hbox_new(FALSE
, 6);
752 label
= gtk_label_new_with_mnemonic(
753 _("Directory _levels to include in the backup destination:"));
754 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
755 gtk_box_pack_start(GTK_BOX(hbox
), label
, FALSE
, FALSE
, 0);
757 spin_dir_levels
= gtk_spin_button_new_with_range(0, 20, 1);
758 pref_widgets
.backupcopy_spin_dir_levels
= spin_dir_levels
;
759 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin_dir_levels
), backupcopy_dir_levels
);
760 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), spin_dir_levels
);
761 gtk_box_pack_start(GTK_BOX(hbox
), spin_dir_levels
, FALSE
, FALSE
, 0);
763 gtk_box_pack_start(GTK_BOX(inner_vbox
), hbox
, FALSE
, FALSE
, 7);
766 /* manually emit the toggled signal of the enable checkboxes to update the widget sensitivity */
767 g_signal_emit_by_name(pref_widgets
.checkbox_enable_autosave
, "toggled");
768 g_signal_emit_by_name(pref_widgets
.checkbox_enable_instantsave
, "toggled");
769 g_signal_emit_by_name(pref_widgets
.checkbox_enable_backupcopy
, "toggled");
771 gtk_widget_show_all(vbox
);
772 g_signal_connect(dialog
, "response", G_CALLBACK(configure_response_cb
), NULL
);
778 void plugin_cleanup(void)
780 if (autosave_src_id
!= 0)
781 g_source_remove(autosave_src_id
);
783 g_free(instantsave_default_ft
);
785 g_free(backupcopy_backup_dir
);
786 g_free(backupcopy_time_fmt
);