geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

saveactions.c
Go to the documentation of this file.
1/*
2 * saveactions.c - this file is part of Geany, a fast and lightweight IDE
3 *
4 * Copyright 2007 The Geany contributors
5 *
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.
10 *
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.
15 *
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.
19 */
20
21
22#ifdef HAVE_CONFIG_H
23# include "config.h"
24#endif
25
26#include "geanyplugin.h"
27#include "gtkcompat.h"
28
29#include <stdio.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <errno.h>
33#include <glib/gstdio.h>
34
35
38
39
41
42PLUGIN_SET_INFO(_("Save Actions"), _("This plugin provides different actions related to saving of files."),
43 VERSION, _("The Geany developer team"))
44
45
46enum
47{
51};
52
53static struct
54{
59
64
67
71}
73
74
75static gboolean enable_autosave;
77static gboolean enable_instantsave;
78static gboolean enable_backupcopy;
79
81static gboolean autosave_print_msg;
82static gboolean autosave_save_all;
83static guint autosave_src_id = 0;
84
87
88static gchar *backupcopy_backup_dir; /* path to an existing directory in locale encoding */
89static gchar *backupcopy_time_fmt;
91
92static gchar *config_file;
93
94
95/* Ensures utf8_dir exists and is writable and
96 * set target to the locale encoded form of utf8_dir */
97static gboolean store_target_directory(const gchar *utf8_dir, gchar **target)
98{
99 gchar *tmp;
100
101 if (G_UNLIKELY(EMPTY(utf8_dir)) || target == NULL)
102 return FALSE;
103
104 tmp = utils_get_locale_from_utf8(utf8_dir);
105
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))
109 {
110 g_free(tmp);
111 return FALSE;
112 }
113 /** TODO add utils_is_file_writeable() to the plugin API and make use of it **/
114
115 SETPTR(*target, tmp);
116
117 return TRUE;
118}
119
120
121static gchar *backupcopy_skip_root(gchar *filename)
122{
123 /* first skip the root (e.g. c:\ on windows) */
124 const gchar *dir = g_path_skip_root(filename);
125
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++;
132
133 return (gchar *) dir;
134}
135
136
137static gchar *backupcopy_create_dir_parts(const gchar *filename)
138{
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;
146
147 if (backupcopy_dir_levels == 0)
148 return g_strdup("");
149
150 dirname = g_path_get_dirname(filename);
151
152 cp = dirname;
153 /* walk to the end of the string */
154 while (*cp != '\0')
155 cp++;
156
157 /* walk backwards to find directory parts */
158 while (cp > dirname)
159 {
160 if (*cp == G_DIR_SEPARATOR && last_char != G_DIR_SEPARATOR)
161 cnt_dir_parts++;
162
163 if (cnt_dir_parts == backupcopy_dir_levels)
164 break;
165
166 last_char = *cp;
167 cp--;
168 }
169
170 result = backupcopy_skip_root(cp); /* skip leading slash/backslash and c:\ */
171 target_dir = g_build_filename(backupcopy_backup_dir, result, NULL);
172
173 error = utils_mkdir(target_dir, TRUE);
174 if (error != 0)
175 {
176 ui_set_statusbar(FALSE, _("Backup Copy: Directory could not be created (%s)."),
177 g_strerror(error));
178
179 result = g_strdup(""); /* return an empty string in case of an error */
180 }
181 else
182 result = g_strdup(result);
183
184 g_free(dirname);
185 g_free(target_dir);
186
187 return result;
188}
189
190
191static void backupcopy_document_save_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
192{
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;
201
202 if (! enable_backupcopy)
203 return;
204
205 locale_filename_src = utils_get_locale_from_utf8(doc->file_name);
206
207 if ((src = g_fopen(locale_filename_src, "r")) == NULL)
208 {
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;
214 }
215
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);
225
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
234 {
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;
244 }
245
246 while (fgets(buf, sizeof(buf), src) != NULL)
247 {
248 fputs(buf, dst);
249 }
250
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);
258}
259
260
261static void instantsave_document_new_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
262{
263 if (enable_instantsave && doc->file_name == NULL)
264 {
265 const gchar *directory;
266 gchar *new_filename;
267 gint fd;
268 GeanyFiletype *ft = doc->file_type;
269
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 */
274
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));
280
281 /* create new file */
282 fd = g_mkstemp(new_filename);
283 if (fd == -1)
284 {
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;
292 }
293
294 close(fd); /* close the returned file descriptor as we only need the filename */
295
296 doc->file_name = new_filename;
297
298 if (ft != NULL && ft->id == GEANY_FILETYPES_NONE)
300
301 /* force saving the file to enable all the related actions(tab name, filetype, etc.) */
302 document_save_file(doc, TRUE);
303 }
304}
305
306
307/* Save when focus out
308 *
309 * @param pointer ref to the current doc (struct GeanyDocument *)
310 *
311 * @return always FALSE = Just a one shot execution
312 *
313 */
314static gboolean save_on_focus_out_idle(gpointer p_cur_doc)
315{
316 GeanyDocument *cur_doc = p_cur_doc;
317
318 if (DOC_VALID(cur_doc) && (cur_doc->file_name != NULL))
319 document_save_file(cur_doc, FALSE);
320
321 return FALSE;
322}
323
324
325/* Autosave the current file when the focus out of the _editor_
326 *
327 * Get the SCN_FOCUSOUT signal, and then ask plugin_idle_add()
328 * to save the current doc when idle
329 *
330 * @return always FALSE = Non block signals
331 *
332 */
333static gboolean on_document_focus_out(GObject *object, GeanyEditor *editor,
334 SCNotification *nt, gpointer data)
335{
336 if (nt->nmhdr.code == SCN_FOCUSOUT
338 && editor->document->file_name != NULL)
339 {
341 }
342
343 return FALSE;
344}
345
346
348{
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 }
353};
354
355
356static gboolean auto_save(gpointer data)
357{
358 GeanyDocument *doc;
360 gint i, max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(geany->main_widgets->notebook));
361 gint saved_files = 0;
362
363 if (cur_doc == NULL)
364 return TRUE;
365
367 {
368 for (i = 0; i < max; i++)
369 {
370 doc = document_get_from_page(i);
371
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++;
376 }
377 }
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++;
383
384 if (saved_files > 0 && autosave_print_msg)
386 "Autosave: Saved %d file automatically.",
387 "Autosave: Saved %d files automatically.", saved_files),
388 saved_files);
389
390 return TRUE;
391}
392
393
394static void autosave_set_timeout(void)
395{
396 if (! enable_autosave)
397 return;
398
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);
402}
403
404
406{
407 GKeyFile *config = g_key_file_new();
408 gchar *tmp;
409
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);
412
413 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
414
416 config, "saveactions", "enable_autosave", FALSE);
418 config, "saveactions", "enable_autosave_losing_focus", FALSE);
420 config, "saveactions", "enable_instantsave", FALSE);
422 config, "saveactions", "enable_backupcopy", FALSE);
423
424 instantsave_default_ft = utils_get_setting_string(config, "instantsave", "default_ft",
426 tmp = utils_get_setting_string(config, "instantsave", "target_dir", NULL);
428 g_free(tmp);
429
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)
436
437 backupcopy_dir_levels = utils_get_setting_integer(config, "backupcopy", "dir_levels", 0);
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());
442 g_free(tmp);
443
444 g_key_file_free(config);
445}
446
447
448static void target_directory_button_clicked_cb(GtkButton *button, gpointer item)
449{
450 GtkWidget *dialog;
451 gchar *text;
452
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);
458
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);
462
463 /* run it */
464 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
465 {
466 gchar *utf8_filename, *tmp;
467
468 tmp = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
469 utf8_filename = utils_get_utf8_from_locale(tmp);
470
471 gtk_entry_set_text(GTK_ENTRY(item), utf8_filename);
472
473 g_free(utf8_filename);
474 g_free(tmp);
475 }
476
477 gtk_widget_destroy(dialog);
478}
479
480
481static void configure_response_cb(GtkDialog *dialog, gint response, G_GNUC_UNUSED gpointer data)
482{
483 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
484 {
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);
489
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));
498
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));
505
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));
510
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));
515
516
517 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
518
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);
523
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);
527
529 g_key_file_set_string(config, "instantsave", "default_ft", instantsave_default_ft);
531 {
532 if (EMPTY(instantsave_text_dir))
533 {
534 g_key_file_set_string(config, "instantsave", "target_dir", "");
536 }
537 else if (store_target_directory(instantsave_text_dir, &instantsave_target_dir))
538 {
539 g_key_file_set_string(config, "instantsave", "target_dir", instantsave_target_dir);
540 }
541 else
542 {
543 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
544 _("Instantsave directory does not exist or is not writable."));
545 }
546 }
547
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));
552 {
553 if (!EMPTY(backupcopy_text_dir) && store_target_directory(
554 backupcopy_text_dir, &backupcopy_backup_dir))
555 {
556 g_key_file_set_string(config, "backupcopy", "backup_dir", backupcopy_text_dir);
557 }
558 else
559 {
560 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
561 _("Backup directory does not exist or is not writable."));
562 }
563 }
564
565
566 if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
567 {
568 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
569 _("Plugin configuration directory could not be created."));
570 }
571 else
572 {
573 /* write config to file */
574 str = g_key_file_to_data(config, NULL, NULL);
576 g_free(str);
577 }
578
579 if (enable_autosave)
580 autosave_set_timeout(); /* apply the changes */
581
582 g_free(config_dir);
583 g_key_file_free(config);
584 }
585}
586
587
588static void checkbox_toggled_cb(GtkToggleButton *tb, gpointer data)
589{
590 gboolean enable = gtk_toggle_button_get_active(tb);
591
592 switch (GPOINTER_TO_INT(data))
593 {
595 {
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;
601 }
603 {
604 gtk_widget_set_sensitive(pref_widgets.instantsave_ft_combo, enable);
605 break;
606 }
608 {
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;
613 }
614 }
615
616}
617
618
619GtkWidget *plugin_configure(GtkDialog *dialog)
620{
621 GtkWidget *vbox, *label, *notebook_vbox, *checkbox_enable;
622 GtkWidget *notebook, *inner_vbox;
623
624 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
625
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);
630
631 /*
632 * Auto Save
633 */
634 {
635 GtkWidget *spin, *hbox, *checkbox, *checkbox_enable_as_lf, *radio1, *radio2;
636
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);
643
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);
649
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));
657
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);
661
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);
665
666 label = gtk_label_new(_("seconds"));
667
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);
671
672 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 5);
673
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;
681
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);
688
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);
696 }
697 /*
698 * Instant Save
699 */
700 {
701 GtkWidget *combo, *hbox, *entry_dir, *button, *image, *help_label;
702 guint i;
703 const GSList *node;
704 gchar *entry_dir_label_text;
705
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);
712
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));
720
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);
724
725 pref_widgets.instantsave_ft_combo = combo = gtk_combo_box_text_new();
726 i = 0;
728 {
729 GeanyFiletype *ft = node->data;
730
731 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), ft->name);
732
734 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), i);
735 i++;
736 }
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);
740
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);
747
748 pref_widgets.instantsave_entry_dir = entry_dir = gtk_entry_new();
749 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry_dir);
751 gtk_entry_set_text(GTK_ENTRY(entry_dir), instantsave_target_dir);
752
753 button = gtk_button_new();
754 g_signal_connect(button, "clicked",
755 G_CALLBACK(target_directory_button_clicked_cb), entry_dir);
756
757 image = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
758 gtk_container_add(GTK_CONTAINER(button), image);
759
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);
764
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);
772 }
773 /*
774 * Backup Copy
775 */
776 {
777 GtkWidget *hbox, *entry_dir, *entry_time, *button, *image, *spin_dir_levels;
778
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);
785
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));
793
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);
797
798 pref_widgets.backupcopy_entry_dir = entry_dir = gtk_entry_new();
799 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry_dir);
801 gtk_entry_set_text(GTK_ENTRY(entry_dir), backupcopy_backup_dir);
802
803 button = gtk_button_new();
804 g_signal_connect(button, "clicked",
805 G_CALLBACK(target_directory_button_clicked_cb), entry_dir);
806
807 image = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
808 gtk_container_add(GTK_CONTAINER(button), image);
809
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);
813
814 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 0);
815
816 label = gtk_label_new_with_mnemonic(
817 _("Date/_Time format for backup files (\"man strftime\" for details):"));
818 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
819 gtk_box_pack_start(GTK_BOX(inner_vbox), label, FALSE, FALSE, 7);
820
821 pref_widgets.backupcopy_entry_time = entry_time = gtk_entry_new();
822 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry_time);
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);
826
827 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
828
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);
833
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);
839
840 gtk_box_pack_start(GTK_BOX(inner_vbox), hbox, FALSE, FALSE, 7);
841 }
842
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");
847
849 g_signal_connect(dialog, "response", G_CALLBACK(configure_response_cb), NULL);
850
851 return vbox;
852}
853
854
856{
857 if (autosave_src_id != 0)
858 g_source_remove(autosave_src_id);
859
862
863 g_free(backupcopy_backup_dir);
864 g_free(backupcopy_time_fmt);
865
866 g_free(config_file);
867}
#define SCN_FOCUSOUT
Definition: Scintilla.h:1154
const gchar * label
Definition: build.c:2676
void dialogs_show_msgbox(GtkMessageType type, const gchar *text,...)
Shows a message box of the type type with text.
Definition: dialogs.c:729
GeanyDocument * document_get_current(void)
Finds the current document.
Definition: document.c:371
GeanyDocument * document_get_from_page(guint page_num)
Finds the document for the given notebook page page_num.
Definition: document.c:352
gboolean document_save_file(GeanyDocument *doc, gboolean force)
Saves the document.
Definition: document.c:2103
void document_set_filetype(GeanyDocument *doc, GeanyFiletype *type)
Sets the filetype of the document (which controls syntax highlighting and tags)
Definition: document.c:2826
const gchar * name
Definition: document.c:3219
#define DOC_VALID(doc_ptr)
Null-safe way to check GeanyDocument::is_valid.
Definition: document.h:162
gchar * text
Definition: editor.c:83
void error(const errorSelection selection, const char *const format,...)
Definition: error.c:53
@ GEANY_FILETYPES_NONE
Definition: filetypes.h:46
#define filetypes
Wraps GeanyData::filetypes_array so it can be used with C array syntax.
Definition: filetypes.h:178
int errno
unsigned int max
Single include for plugins.
#define GEANY_API_VERSION
The Application Programming Interface (API) version, incremented whenever any plugin data types are m...
Definition: plugindata.h:61
#define PLUGIN_SET_INFO(p_name, p_description, p_version, p_author)
Sets the plugin name and some other basic information about a plugin.
Definition: plugindata.h:117
#define PLUGIN_VERSION_CHECK(api_required)
Defines a function to check the plugin is safe to load.
Definition: plugindata.h:83
#define geany
Simple macro for geany_data that reduces typing.
Definition: plugindata.h:225
guint plugin_idle_add(GeanyPlugin *plugin, GSourceFunc function, gpointer data)
<simplesect kind="geany:skip"></simplesect> Adds a GLib main loop IDLE callback that will be removed ...
Definition: pluginutils.c:314
#define NULL
Definition: rbtree.h:150
#define S_IRUSR
Definition: routines.c:108
#define S_IWUSR
Definition: routines.c:111
static gchar * instantsave_target_dir
Definition: saveactions.c:86
GtkWidget * autosave_save_all_radio2
Definition: saveactions.c:63
GtkWidget * autosave_print_msg_checkbox
Definition: saveactions.c:61
static gchar * backupcopy_skip_root(gchar *filename)
Definition: saveactions.c:121
GtkWidget * autosave_save_all_radio1
Definition: saveactions.c:62
static gchar * backupcopy_time_fmt
Definition: saveactions.c:89
GtkWidget * instantsave_ft_combo
Definition: saveactions.c:65
static gint backupcopy_dir_levels
Definition: saveactions.c:90
static struct @37 pref_widgets
@ NOTEBOOK_PAGE_INSTANTSAVE
Definition: saveactions.c:49
@ NOTEBOOK_PAGE_AUTOSAVE
Definition: saveactions.c:48
@ NOTEBOOK_PAGE_BACKUPCOPY
Definition: saveactions.c:50
GtkWidget * checkbox_enable_backupcopy
Definition: saveactions.c:58
GtkWidget * checkbox_enable_instantsave
Definition: saveactions.c:57
GtkWidget * backupcopy_spin_dir_levels
Definition: saveactions.c:70
static gboolean enable_backupcopy
Definition: saveactions.c:78
static gboolean save_on_focus_out_idle(gpointer p_cur_doc)
Definition: saveactions.c:314
static gboolean auto_save(gpointer data)
Definition: saveactions.c:356
GeanyPlugin * geany_plugin
Definition: saveactions.c:36
static gint autosave_interval
Definition: saveactions.c:80
static void instantsave_document_new_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
Definition: saveactions.c:261
static gchar * config_file
Definition: saveactions.c:92
void plugin_cleanup(void)
Called before unloading the plugin.
Definition: saveactions.c:855
PluginCallback plugin_callbacks[]
Definition: saveactions.c:347
static void backupcopy_document_save_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
Definition: saveactions.c:191
static void target_directory_button_clicked_cb(GtkButton *button, gpointer item)
Definition: saveactions.c:448
static gboolean autosave_print_msg
Definition: saveactions.c:81
GtkWidget * backupcopy_entry_time
Definition: saveactions.c:69
static gchar * backupcopy_backup_dir
Definition: saveactions.c:88
GtkWidget * autosave_interval_spin
Definition: saveactions.c:60
static gboolean enable_autosave
Definition: saveactions.c:75
static gboolean autosave_save_all
Definition: saveactions.c:82
static gchar * backupcopy_create_dir_parts(const gchar *filename)
Definition: saveactions.c:137
GtkWidget * backupcopy_entry_dir
Definition: saveactions.c:68
static guint autosave_src_id
Definition: saveactions.c:83
void plugin_init(GeanyData *data)
Called after loading the plugin.
Definition: saveactions.c:405
static void configure_response_cb(GtkDialog *dialog, gint response, G_GNUC_UNUSED gpointer data)
Definition: saveactions.c:481
static gchar * instantsave_default_ft
Definition: saveactions.c:85
static gboolean enable_autosave_losing_focus
Definition: saveactions.c:76
GeanyData * geany_data
Definition: saveactions.c:37
GtkWidget * checkbox_enable_autosave_losing_focus
Definition: saveactions.c:56
static void autosave_set_timeout(void)
Definition: saveactions.c:394
GtkWidget * plugin_configure(GtkDialog *dialog)
Called before showing the plugin preferences dialog for multiple plugins.
Definition: saveactions.c:619
static void checkbox_toggled_cb(GtkToggleButton *tb, gpointer data)
Definition: saveactions.c:588
static gboolean store_target_directory(const gchar *utf8_dir, gchar **target)
Definition: saveactions.c:97
static gboolean on_document_focus_out(GObject *object, GeanyEditor *editor, SCNotification *nt, gpointer data)
Definition: saveactions.c:333
GtkWidget * checkbox_enable_autosave
Definition: saveactions.c:55
GtkWidget * instantsave_entry_dir
Definition: saveactions.c:66
static gboolean enable_instantsave
Definition: saveactions.c:77
GtkWidget * close
Definition: sidebar.c:56
const GSList * filetypes_get_sorted_by_name(void)
Gets a list of filetype pointers sorted by name.
Definition: filetypes.c:234
GeanyFiletype * filetypes_lookup_by_name(const gchar *name)
Finds a filetype pointer from its name field.
Definition: filetypes.c:1242
const gchar filename[]
Definition: stash-example.c:4
GtkWidget * dialog
gtk_container_add(GTK_CONTAINER(dialog->vbox), check_button)
gtk_widget_show_all(dialog)
This contains pointers to global variables owned by Geany for plugins to use.
Definition: plugindata.h:167
Structure for representing an open tab with all its properties.
Definition: document.h:81
gchar * file_name
The UTF-8 encoded file name.
Definition: document.h:92
GeanyFiletype * file_type
The filetype for this document, it's only a reference to one of the elements of the global filetypes ...
Definition: document.h:101
Editor-owned fields for each document.
Definition: editor.h:150
struct GeanyDocument * document
The document associated with the editor.
Definition: editor.h:151
Represents a filetype.
Definition: filetypes.h:144
gchar * name
Untranslated short name, such as "C", "None".
Definition: filetypes.h:152
GeanyFiletypeID id
Index in filetypes.
Definition: filetypes.h:145
gchar * extension
Default file extension for new files, or NULL.
Definition: filetypes.h:155
Basic information for the plugin and identification.
Definition: plugindata.h:233
Callback array entry type used with the plugin_callbacks symbol.
Definition: plugindata.h:148
Sci_NotifyHeader nmhdr
Definition: Scintilla.h:1229
unsigned int code
Definition: Scintilla.h:1225
#define _(String)
Definition: support.h:42
#define ngettext(String, PluralString, Number)
Definition: support.h:41
void ui_set_statusbar(gboolean log, const gchar *format,...)
Displays text on the statusbar.
Definition: ui_utils.c:168
gint utils_write_file(const gchar *filename, const gchar *text)
Writes text into a file named filename.
Definition: utils.c:209
gint utils_get_setting_integer(GKeyFile *config, const gchar *section, const gchar *key, const gint default_value)
Wraps g_key_file_get_integer() to add a default value argument.
Definition: utils.c:805
gboolean utils_get_setting_boolean(GKeyFile *config, const gchar *section, const gchar *key, const gboolean default_value)
Wraps g_key_file_get_boolean() to add a default value argument.
Definition: utils.c:836
gchar * utils_get_utf8_from_locale(const gchar *locale_text)
Converts the given string (in locale encoding) into UTF-8 encoding.
Definition: utils.c:1272
gchar * utils_get_date_time(const gchar *format, time_t *time_to_use)
Retrieves a formatted date/time string from strftime().
Definition: utils.c:736
gint utils_mkdir(const gchar *path, gboolean create_parent_dirs)
Creates a directory if it doesn't already exist.
Definition: utils.c:1359
gboolean utils_str_equal(const gchar *a, const gchar *b)
NULL-safe string comparison.
Definition: utils.c:599
gchar * utils_get_setting_string(GKeyFile *config, const gchar *section, const gchar *key, const gchar *default_value)
Wraps g_key_file_get_string() to add a default value argument.
Definition: utils.c:867
gchar * utils_get_locale_from_utf8(const gchar *utf8_text)
Converts the given UTF-8 encoded string into locale encoding.
Definition: utils.c:1243
#define foreach_slist(node, list)
Iterates all the nodes in list.
Definition: utils.h:121
#define SETPTR(ptr, result)
Assigns result to ptr, then frees the old value.
Definition: utils.h:50
#define EMPTY(ptr)
Returns TRUE if ptr is NULL or *ptr is FALSE.
Definition: utils.h:38