Update nl.po (#3881)
[geany-mirror.git] / plugins / saveactions.c
blobdaa4a434f7c8086156cde0bd805d0117eada3837
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 PACKAGE_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;
66 GtkWidget *instantsave_entry_dir;
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;
86 static gchar *instantsave_target_dir;
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 target to the locale encoded form of utf8_dir */
97 static gboolean store_target_directory(const gchar *utf8_dir, gchar **target)
99 gchar *tmp;
101 if (G_UNLIKELY(EMPTY(utf8_dir)) || target == NULL)
102 return FALSE;
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))
110 g_free(tmp);
111 return FALSE;
113 /** TODO add utils_is_file_writeable() to the plugin API and make use of it **/
115 SETPTR(*target, tmp);
117 return TRUE;
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 */
127 if (dir == NULL)
128 dir = filename;
129 /* check again for leading / or \ */
130 while (*dir == G_DIR_SEPARATOR)
131 dir++;
133 return (gchar *) dir;
137 static gchar *backupcopy_create_dir_parts(const gchar *filename)
139 gint cnt_dir_parts = 0;
140 gchar *cp;
141 gchar *dirname;
142 gchar last_char = 0;
143 gint error;
144 gchar *result;
145 gchar *target_dir;
147 if (backupcopy_dir_levels == 0)
148 return g_strdup("");
150 dirname = g_path_get_dirname(filename);
152 cp = dirname;
153 /* walk to the end of the string */
154 while (*cp != '\0')
155 cp++;
157 /* walk backwards to find directory parts */
158 while (cp > dirname)
160 if (*cp == G_DIR_SEPARATOR && last_char != G_DIR_SEPARATOR)
161 cnt_dir_parts++;
163 if (cnt_dir_parts == backupcopy_dir_levels)
164 break;
166 last_char = *cp;
167 cp--;
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);
174 if (error != 0)
176 ui_set_statusbar(FALSE, _("Backup Copy: Directory could not be created (%s)."),
177 g_strerror(error));
179 result = g_strdup(""); /* return an empty string in case of an error */
181 else
182 result = g_strdup(result);
184 g_free(dirname);
185 g_free(target_dir);
187 return result;
191 static void backupcopy_document_save_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
193 FILE *src, *dst;
194 gchar *locale_filename_src;
195 gchar *locale_filename_dst;
196 gchar *basename_src;
197 gchar *dir_parts_src;
198 gchar *stamp;
199 gchar buf[512];
200 gint fd_dst = -1;
202 if (! enable_backupcopy)
203 return;
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)."),
211 g_strerror(errno));
212 g_free(locale_filename_src);
213 return;
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);
226 #ifdef G_OS_WIN32
227 if ((dst = g_fopen(locale_filename_dst, "wb")) == NULL)
228 #else
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)
233 #endif
235 ui_set_statusbar(FALSE, _("Backup Copy: File could not be saved (%s)."),
236 g_strerror(errno));
237 g_free(locale_filename_src);
238 g_free(locale_filename_dst);
239 g_free(stamp);
240 fclose(src);
241 if (fd_dst != -1)
242 close(fd_dst);
243 return;
246 while (fgets(buf, sizeof(buf), src) != NULL)
248 fputs(buf, dst);
251 fclose(src);
252 fclose(dst);
253 if (fd_dst != -1)
254 close(fd_dst);
255 g_free(locale_filename_src);
256 g_free(locale_filename_dst);
257 g_free(stamp);
261 static void instantsave_document_new_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
263 if (enable_instantsave && doc->file_name == NULL)
265 const gchar *directory;
266 gchar *new_filename;
267 gint fd;
268 GeanyFiletype *ft = doc->file_type;
270 if (ft == NULL || ft->id == GEANY_FILETYPES_NONE)
271 /* ft is NULL when a new file without template was opened, so use the
272 * configured default file type */
273 ft = filetypes_lookup_by_name(instantsave_default_ft);
275 /* construct filename */
276 directory = !EMPTY(instantsave_target_dir) ? instantsave_target_dir : g_get_tmp_dir();
277 new_filename = g_build_filename(directory, "gis_XXXXXX", NULL);
278 if (ft != NULL && !EMPTY(ft->extension))
279 SETPTR(new_filename, g_strconcat(new_filename, ".", ft->extension, NULL));
281 /* create new file */
282 fd = g_mkstemp(new_filename);
283 if (fd == -1)
285 gchar *message = g_strdup_printf(
286 _("Instant Save filename could not be generated (%s)."), g_strerror(errno));
287 ui_set_statusbar(TRUE, "%s", message);
288 g_warning("%s", message);
289 g_free(message);
290 g_free(new_filename);
291 return;
294 close(fd); /* close the returned file descriptor as we only need the filename */
296 doc->file_name = new_filename;
298 if (ft != NULL && ft->id == GEANY_FILETYPES_NONE)
299 document_set_filetype(doc, filetypes_lookup_by_name(instantsave_default_ft));
301 /* force saving the file to enable all the related actions(tab name, filetype, etc.) */
302 document_save_file(doc, TRUE);
307 /* Save when focus out
309 * @param pointer ref to the current doc (struct GeanyDocument *)
311 * @return always FALSE = Just a one shot execution
314 static gboolean save_on_focus_out_idle(gpointer p_cur_doc)
316 GeanyDocument *cur_doc = p_cur_doc;
318 if (DOC_VALID(cur_doc) && (cur_doc->file_name != NULL))
319 document_save_file(cur_doc, FALSE);
321 return FALSE;
325 /* Autosave the current file when the focus out of the _editor_
327 * Get the SCN_FOCUSOUT signal, and then ask plugin_idle_add()
328 * to save the current doc when idle
330 * @return always FALSE = Non block signals
333 static gboolean on_document_focus_out(GObject *object, GeanyEditor *editor,
334 SCNotification *nt, gpointer data)
336 if (nt->nmhdr.code == SCN_FOCUSOUT
337 && enable_autosave_losing_focus
338 && editor->document->file_name != NULL)
340 plugin_idle_add(geany_plugin, save_on_focus_out_idle, editor->document);
343 return FALSE;
347 PluginCallback plugin_callbacks[] =
349 { "document-new", (GCallback) &instantsave_document_new_cb, FALSE, NULL },
350 { "document-save", (GCallback) &backupcopy_document_save_cb, FALSE, NULL },
351 { "editor-notify", (GCallback) &on_document_focus_out, FALSE, NULL },
352 { NULL, NULL, FALSE, NULL }
356 static gboolean auto_save(gpointer data)
358 GeanyDocument *doc;
359 GeanyDocument *cur_doc = document_get_current();
360 gint i, max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(geany->main_widgets->notebook));
361 gint saved_files = 0;
363 if (cur_doc == NULL)
364 return TRUE;
366 if (autosave_save_all)
368 for (i = 0; i < max; i++)
370 doc = document_get_from_page(i);
372 /* skip current file (save it last), skip files without name */
373 if (doc != cur_doc && doc->file_name != NULL)
374 if (document_save_file(doc, FALSE))
375 saved_files++;
378 /* finally save current file, do it after all other files to get correct window title and
379 * symbol list */
380 if (cur_doc->file_name != NULL)
381 if (document_save_file(cur_doc, FALSE))
382 saved_files++;
384 if (saved_files > 0 && autosave_print_msg)
385 ui_set_statusbar(FALSE, ngettext(
386 "Autosave: Saved %d file automatically.",
387 "Autosave: Saved %d files automatically.", saved_files),
388 saved_files);
390 return TRUE;
394 static void autosave_set_timeout(void)
396 if (! enable_autosave)
397 return;
399 if (autosave_src_id != 0)
400 g_source_remove(autosave_src_id);
401 autosave_src_id = g_timeout_add(autosave_interval * 1000, (GSourceFunc) auto_save, NULL);
405 void plugin_init(GeanyData *data)
407 GKeyFile *config = g_key_file_new();
408 gchar *tmp;
410 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S, "plugins",
411 G_DIR_SEPARATOR_S, "saveactions", G_DIR_SEPARATOR_S, "saveactions.conf", NULL);
413 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
415 enable_autosave = utils_get_setting_boolean(
416 config, "saveactions", "enable_autosave", FALSE);
417 enable_autosave_losing_focus = utils_get_setting_boolean(
418 config, "saveactions", "enable_autosave_losing_focus", FALSE);
419 enable_instantsave = utils_get_setting_boolean(
420 config, "saveactions", "enable_instantsave", FALSE);
421 enable_backupcopy = utils_get_setting_boolean(
422 config, "saveactions", "enable_backupcopy", FALSE);
424 instantsave_default_ft = utils_get_setting_string(config, "instantsave", "default_ft",
425 filetypes[GEANY_FILETYPES_NONE]->name);
426 tmp = utils_get_setting_string(config, "instantsave", "target_dir", NULL);
427 store_target_directory(tmp, &instantsave_target_dir);
428 g_free(tmp);
430 autosave_src_id = 0; /* mark as invalid */
431 autosave_interval = utils_get_setting_integer(config, "autosave", "interval", 300);
432 autosave_print_msg = utils_get_setting_boolean(config, "autosave", "print_messages", FALSE);
433 autosave_save_all = utils_get_setting_boolean(config, "autosave", "save_all", FALSE);
434 if (enable_autosave)
435 autosave_set_timeout();
437 backupcopy_dir_levels = utils_get_setting_integer(config, "backupcopy", "dir_levels", 0);
438 backupcopy_time_fmt = utils_get_setting_string(
439 config, "backupcopy", "time_fmt", "%Y-%m-%d-%H-%M-%S");
440 tmp = utils_get_setting_string(config, "backupcopy", "backup_dir", g_get_tmp_dir());
441 store_target_directory(tmp, &backupcopy_backup_dir);
442 g_free(tmp);
444 g_key_file_free(config);
448 static void target_directory_button_clicked_cb(GtkButton *button, gpointer item)
450 GtkWidget *dialog;
451 gchar *text;
453 /* initialize the dialog */
454 dialog = gtk_file_chooser_dialog_new(_("Select Directory"), NULL,
455 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
456 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
457 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
459 text = utils_get_locale_from_utf8(gtk_entry_get_text(GTK_ENTRY(item)));
460 if (!EMPTY(text))
461 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), text);
463 /* run it */
464 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
466 gchar *utf8_filename, *tmp;
468 tmp = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
469 utf8_filename = utils_get_utf8_from_locale(tmp);
471 gtk_entry_set_text(GTK_ENTRY(item), utf8_filename);
473 g_free(utf8_filename);
474 g_free(tmp);
477 gtk_widget_destroy(dialog);
481 static void configure_response_cb(GtkDialog *dialog, gint response, G_GNUC_UNUSED gpointer data)
483 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
485 GKeyFile *config = g_key_file_new();
486 gchar *str;
487 const gchar *backupcopy_text_dir, *instantsave_text_dir, *text_time;
488 gchar *config_dir = g_path_get_dirname(config_file);
490 enable_autosave = gtk_toggle_button_get_active(
491 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_autosave));
492 enable_autosave_losing_focus = gtk_toggle_button_get_active(
493 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_autosave_losing_focus));
494 enable_instantsave = gtk_toggle_button_get_active(
495 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_instantsave));
496 enable_backupcopy = gtk_toggle_button_get_active(
497 GTK_TOGGLE_BUTTON(pref_widgets.checkbox_enable_backupcopy));
499 autosave_interval = gtk_spin_button_get_value_as_int(
500 GTK_SPIN_BUTTON(pref_widgets.autosave_interval_spin));
501 autosave_print_msg = gtk_toggle_button_get_active(
502 GTK_TOGGLE_BUTTON(pref_widgets.autosave_print_msg_checkbox));
503 autosave_save_all = gtk_toggle_button_get_active(
504 GTK_TOGGLE_BUTTON(pref_widgets.autosave_save_all_radio2));
506 g_free(instantsave_default_ft);
507 instantsave_default_ft = gtk_combo_box_text_get_active_text(
508 GTK_COMBO_BOX_TEXT(pref_widgets.instantsave_ft_combo));
509 instantsave_text_dir = gtk_entry_get_text(GTK_ENTRY(pref_widgets.instantsave_entry_dir));
511 backupcopy_text_dir = gtk_entry_get_text(GTK_ENTRY(pref_widgets.backupcopy_entry_dir));
512 text_time = gtk_entry_get_text(GTK_ENTRY(pref_widgets.backupcopy_entry_time));
513 backupcopy_dir_levels = gtk_spin_button_get_value_as_int(
514 GTK_SPIN_BUTTON(pref_widgets.backupcopy_spin_dir_levels));
517 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
519 g_key_file_set_boolean(config, "saveactions", "enable_autosave", enable_autosave);
520 g_key_file_set_boolean(config, "saveactions", "enable_autosave_losing_focus", enable_autosave_losing_focus);
521 g_key_file_set_boolean(config, "saveactions", "enable_instantsave", enable_instantsave);
522 g_key_file_set_boolean(config, "saveactions", "enable_backupcopy", enable_backupcopy);
524 g_key_file_set_boolean(config, "autosave", "print_messages", autosave_print_msg);
525 g_key_file_set_boolean(config, "autosave", "save_all", autosave_save_all);
526 g_key_file_set_integer(config, "autosave", "interval", autosave_interval);
528 if (instantsave_default_ft != NULL)
529 g_key_file_set_string(config, "instantsave", "default_ft", instantsave_default_ft);
530 if (enable_instantsave)
532 if (EMPTY(instantsave_text_dir))
534 g_key_file_set_string(config, "instantsave", "target_dir", "");
535 SETPTR(instantsave_target_dir, NULL);
537 else if (store_target_directory(instantsave_text_dir, &instantsave_target_dir))
539 g_key_file_set_string(config, "instantsave", "target_dir", instantsave_target_dir);
541 else
543 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
544 _("Instantsave directory does not exist or is not writable."));
548 g_key_file_set_integer(config, "backupcopy", "dir_levels", backupcopy_dir_levels);
549 g_key_file_set_string(config, "backupcopy", "time_fmt", text_time);
550 SETPTR(backupcopy_time_fmt, g_strdup(text_time));
551 if (enable_backupcopy)
553 if (!EMPTY(backupcopy_text_dir) && store_target_directory(
554 backupcopy_text_dir, &backupcopy_backup_dir))
556 g_key_file_set_string(config, "backupcopy", "backup_dir", backupcopy_text_dir);
558 else
560 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
561 _("Backup directory does not exist or is not writable."));
566 if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
568 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
569 _("Plugin configuration directory could not be created."));
571 else
573 /* write config to file */
574 str = g_key_file_to_data(config, NULL, NULL);
575 utils_write_file(config_file, str);
576 g_free(str);
579 if (enable_autosave)
580 autosave_set_timeout(); /* apply the changes */
582 g_free(config_dir);
583 g_key_file_free(config);
588 static void checkbox_toggled_cb(GtkToggleButton *tb, gpointer data)
590 gboolean enable = gtk_toggle_button_get_active(tb);
592 switch (GPOINTER_TO_INT(data))
594 case NOTEBOOK_PAGE_AUTOSAVE:
596 gtk_widget_set_sensitive(pref_widgets.autosave_interval_spin, enable);
597 gtk_widget_set_sensitive(pref_widgets.autosave_print_msg_checkbox, enable);
598 gtk_widget_set_sensitive(pref_widgets.autosave_save_all_radio1, enable);
599 gtk_widget_set_sensitive(pref_widgets.autosave_save_all_radio2, enable);
600 break;
602 case NOTEBOOK_PAGE_INSTANTSAVE:
604 gtk_widget_set_sensitive(pref_widgets.instantsave_ft_combo, enable);
605 break;
607 case NOTEBOOK_PAGE_BACKUPCOPY:
609 gtk_widget_set_sensitive(pref_widgets.backupcopy_entry_dir, enable);
610 gtk_widget_set_sensitive(pref_widgets.backupcopy_entry_time, enable);
611 gtk_widget_set_sensitive(pref_widgets.backupcopy_spin_dir_levels, enable);
612 break;
619 GtkWidget *plugin_configure(GtkDialog *dialog)
621 GtkWidget *vbox, *label, *notebook_vbox, *checkbox_enable;
622 GtkWidget *notebook, *inner_vbox;
624 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
626 notebook = gtk_notebook_new();
627 gtk_widget_set_can_focus(notebook, FALSE);
628 gtk_container_set_border_width(GTK_CONTAINER(notebook), 5);
629 gtk_box_pack_start(GTK_BOX(vbox), notebook, FALSE, TRUE, 0);
632 * Auto Save
635 GtkWidget *spin, *hbox, *checkbox, *checkbox_enable_as_lf, *radio1, *radio2;
637 notebook_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
638 inner_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
639 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox), 5);
640 gtk_box_pack_start(GTK_BOX(notebook_vbox), inner_vbox, TRUE, TRUE, 5);
641 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook),
642 notebook_vbox, gtk_label_new(_("Auto Save")), NOTEBOOK_PAGE_AUTOSAVE);
644 checkbox_enable_as_lf = gtk_check_button_new_with_mnemonic(_("Enable save when losing _focus"));
645 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable_as_lf), FALSE);
646 pref_widgets.checkbox_enable_autosave_losing_focus = checkbox_enable_as_lf;
647 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable_as_lf, FALSE, FALSE, 6);
648 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable_as_lf), enable_autosave_losing_focus);
650 checkbox_enable = gtk_check_button_new_with_mnemonic(_("_Enable"));
651 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable), FALSE);
652 pref_widgets.checkbox_enable_autosave = checkbox_enable;
653 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable, FALSE, FALSE, 6);
654 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable), enable_autosave);
655 g_signal_connect(checkbox_enable, "toggled",
656 G_CALLBACK(checkbox_toggled_cb), GINT_TO_POINTER(NOTEBOOK_PAGE_AUTOSAVE));
658 label = gtk_label_new_with_mnemonic(_("Auto save _interval:"));
659 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
660 gtk_box_pack_start(GTK_BOX(inner_vbox), label, TRUE, TRUE, 0);
662 pref_widgets.autosave_interval_spin = spin = gtk_spin_button_new_with_range(1, 1800, 1);
663 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), autosave_interval);
664 gtk_label_set_mnemonic_widget(GTK_LABEL(label), spin);
666 label = gtk_label_new(_("seconds"));
668 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
669 gtk_box_pack_start(GTK_BOX(hbox), spin, TRUE, TRUE, 0);
670 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
672 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 5);
674 checkbox = gtk_check_button_new_with_mnemonic(
675 _("_Print status message if files have been automatically saved"));
676 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox), FALSE);
677 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), autosave_print_msg);
678 gtk_label_set_mnemonic_widget(GTK_LABEL(label), checkbox);
679 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox, FALSE, FALSE, 5);
680 pref_widgets.autosave_print_msg_checkbox = checkbox;
682 radio1 = gtk_radio_button_new_with_mnemonic(NULL,
683 _("Save only current open _file"));
684 pref_widgets.autosave_save_all_radio1 = radio1;
685 gtk_label_set_mnemonic_widget(GTK_LABEL(label), radio1);
686 gtk_button_set_focus_on_click(GTK_BUTTON(radio1), FALSE);
687 gtk_container_add(GTK_CONTAINER(inner_vbox), radio1);
689 radio2 = gtk_radio_button_new_with_mnemonic_from_widget(
690 GTK_RADIO_BUTTON(radio1), _("Sa_ve all open files"));
691 pref_widgets.autosave_save_all_radio2 = radio2;
692 gtk_label_set_mnemonic_widget(GTK_LABEL(label), radio2);
693 gtk_button_set_focus_on_click(GTK_BUTTON(radio2), FALSE);
694 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio2), autosave_save_all);
695 gtk_container_add(GTK_CONTAINER(inner_vbox), radio2);
698 * Instant Save
701 GtkWidget *combo, *hbox, *entry_dir, *button, *image, *help_label;
702 guint i;
703 const GSList *node;
704 gchar *entry_dir_label_text;
706 notebook_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
707 inner_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
708 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox), 5);
709 gtk_box_pack_start(GTK_BOX(notebook_vbox), inner_vbox, TRUE, TRUE, 5);
710 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook),
711 notebook_vbox, gtk_label_new(_("Instant Save")), NOTEBOOK_PAGE_INSTANTSAVE);
713 checkbox_enable = gtk_check_button_new_with_mnemonic(_("_Enable"));
714 pref_widgets.checkbox_enable_instantsave = checkbox_enable;
715 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable), FALSE);
716 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable), enable_instantsave);
717 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable, FALSE, FALSE, 6);
718 g_signal_connect(checkbox_enable, "toggled",
719 G_CALLBACK(checkbox_toggled_cb), GINT_TO_POINTER(NOTEBOOK_PAGE_INSTANTSAVE));
721 label = gtk_label_new_with_mnemonic(_("Default _filetype to use for new files:"));
722 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
723 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 0);
725 pref_widgets.instantsave_ft_combo = combo = gtk_combo_box_text_new();
726 i = 0;
727 foreach_slist(node, filetypes_get_sorted_by_name())
729 GeanyFiletype *ft = node->data;
731 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), ft->name);
733 if (utils_str_equal(ft->name, instantsave_default_ft))
734 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), i);
735 i++;
737 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(combo), 3);
738 gtk_label_set_mnemonic_widget(GTK_LABEL(label), combo);
739 gtk_box_pack_start(GTK_BOX(inner_vbox), combo, FALSE, FALSE, 0);
741 entry_dir_label_text = g_strdup_printf(
742 _("_Directory to save files in (leave empty to use the default: %s):"), g_get_tmp_dir());
743 label = gtk_label_new_with_mnemonic(entry_dir_label_text);
744 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
745 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 0);
746 g_free(entry_dir_label_text);
748 pref_widgets.instantsave_entry_dir = entry_dir = gtk_entry_new();
749 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry_dir);
750 if (!EMPTY(instantsave_target_dir))
751 gtk_entry_set_text(GTK_ENTRY(entry_dir), instantsave_target_dir);
753 button = gtk_button_new();
754 g_signal_connect(button, "clicked",
755 G_CALLBACK(target_directory_button_clicked_cb), entry_dir);
757 image = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
758 gtk_container_add(GTK_CONTAINER(button), image);
760 hbox = gtk_hbox_new(FALSE, 6);
761 gtk_box_pack_start(GTK_BOX(hbox), entry_dir, TRUE, TRUE, 0);
762 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
763 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 0);
765 help_label = gtk_label_new(
766 _("<i>If you set the Instant Save directory to a directory "
767 "which is not automatically cleared,\nyou will need to cleanup instantly saved files "
768 "manually. The Instant Save plugin will not delete the created files.</i>"));
769 gtk_label_set_use_markup(GTK_LABEL(help_label), TRUE);
770 gtk_misc_set_alignment(GTK_MISC(help_label), 0, 0.5);
771 gtk_box_pack_start(GTK_BOX(inner_vbox), help_label, FALSE, FALSE, 0);
774 * Backup Copy
777 GtkWidget *hbox, *entry_dir, *entry_time, *button, *image, *spin_dir_levels;
779 notebook_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
780 inner_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
781 gtk_container_set_border_width(GTK_CONTAINER(inner_vbox), 5);
782 gtk_box_pack_start(GTK_BOX(notebook_vbox), inner_vbox, TRUE, TRUE, 5);
783 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook),
784 notebook_vbox, gtk_label_new(_("Backup Copy")), NOTEBOOK_PAGE_BACKUPCOPY);
786 checkbox_enable = gtk_check_button_new_with_mnemonic(_("_Enable"));
787 pref_widgets.checkbox_enable_backupcopy = checkbox_enable;
788 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_enable), FALSE);
789 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_enable), enable_backupcopy);
790 gtk_box_pack_start(GTK_BOX(inner_vbox), checkbox_enable, FALSE, FALSE, 6);
791 g_signal_connect(checkbox_enable, "toggled",
792 G_CALLBACK(checkbox_toggled_cb), GINT_TO_POINTER(NOTEBOOK_PAGE_BACKUPCOPY));
794 label = gtk_label_new_with_mnemonic(_("_Directory to save backup files in:"));
795 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
796 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 0);
798 pref_widgets.backupcopy_entry_dir = entry_dir = gtk_entry_new();
799 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry_dir);
800 if (!EMPTY(backupcopy_backup_dir))
801 gtk_entry_set_text(GTK_ENTRY(entry_dir), backupcopy_backup_dir);
803 button = gtk_button_new();
804 g_signal_connect(button, "clicked",
805 G_CALLBACK(target_directory_button_clicked_cb), entry_dir);
807 image = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
808 gtk_container_add(GTK_CONTAINER(button), image);
810 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
811 gtk_box_pack_start(GTK_BOX(hbox), entry_dir, TRUE, TRUE, 0);
812 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
814 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 0);
816 label = gtk_label_new_with_mnemonic(
817 _("Date/_Time format for backup files (for a list of available conversion specifiers see https://docs.gtk.org/glib/method.DateTime.format.html):"));
818 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
819 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 7);
821 pref_widgets.backupcopy_entry_time = entry_time = gtk_entry_new();
822 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry_time);
823 if (!EMPTY(backupcopy_time_fmt))
824 gtk_entry_set_text(GTK_ENTRY(entry_time), backupcopy_time_fmt);
825 gtk_box_pack_start(GTK_BOX(inner_vbox), entry_time, FALSE, FALSE, 0);
827 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
829 label = gtk_label_new_with_mnemonic(
830 _("Directory _levels to include in the backup destination:"));
831 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
832 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
834 spin_dir_levels = gtk_spin_button_new_with_range(0, 20, 1);
835 pref_widgets.backupcopy_spin_dir_levels = spin_dir_levels;
836 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin_dir_levels), backupcopy_dir_levels);
837 gtk_label_set_mnemonic_widget(GTK_LABEL(label), spin_dir_levels);
838 gtk_box_pack_start(GTK_BOX(hbox), spin_dir_levels, FALSE, FALSE, 0);
840 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 7);
843 /* manually emit the toggled signal of the enable checkboxes to update the widget sensitivity */
844 g_signal_emit_by_name(pref_widgets.checkbox_enable_autosave, "toggled");
845 g_signal_emit_by_name(pref_widgets.checkbox_enable_instantsave, "toggled");
846 g_signal_emit_by_name(pref_widgets.checkbox_enable_backupcopy, "toggled");
848 gtk_widget_show_all(vbox);
849 g_signal_connect(dialog, "response", G_CALLBACK(configure_response_cb), NULL);
851 return vbox;
855 void plugin_cleanup(void)
857 if (autosave_src_id != 0)
858 g_source_remove(autosave_src_id);
860 g_free(instantsave_default_ft);
861 g_free(instantsave_target_dir);
863 g_free(backupcopy_backup_dir);
864 g_free(backupcopy_time_fmt);
866 g_free(config_file);