offset fixes, admit to textline, proper updating of height/font at attr set,
[dia.git] / app / autosave.c
blob6475d223707bb19c5db36c220c4f24f8c8623503
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 /* Autosave */
21 /* Automatically save a copy with the .autosave extension after idle time.
22 * Don't autosave unmodified diagrams, and remove the autosave file when
23 * the diagram is saved successfully. Also remove autosave file when the
24 * diagram is closed, even if it was modified.
25 * If (auto)saving crashes you, this will really fuck you over!
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
40 #include "intl.h"
41 #include "dia_dirs.h"
42 #include "diagram.h"
43 #include "load_save.h"
44 #include "dialogs.h"
45 #include <gtk/gtk.h>
47 gboolean autosave_check_autosave(gpointer data);
49 static void
50 autosave_save_diagram(gpointer data)
52 Diagram *dia = (Diagram *)data;
54 gtk_idle_remove_by_data(data);
56 diagram_autosave(dia);
59 /** Makes autosave copies of the diagrams in the appropriate directory
60 * This function will be called after a diagram is modified, and will
61 * only save documents that are modified and not already autosaved,
62 * and only at the next idle period.
64 gboolean
65 autosave_check_autosave(gpointer data)
67 GList *diagrams = dia_open_diagrams();
68 Diagram *diagram;
70 while (diagrams != NULL) {
71 diagram = (Diagram *)diagrams->data;
72 if (diagram_is_modified(diagram) &&
73 !diagram->autosaved) {
74 /* Diagram has been modified. At next idleness, save it */
75 gtk_idle_add((GtkFunction)autosave_save_diagram, diagram);
77 diagrams = g_list_next(diagrams);
79 return TRUE;
82 /* This is old stuff for a restore dialog */
83 #if 0
84 /* Doesn't work with autosave files stored in the files dir */
86 /** Create a dialog that asks for files to be restore */
87 static void
88 autosave_make_restore_dialog(GList *files)
90 GtkWidget *ok, *cancel;
91 GtkWidget *dialog = dialog_make(_("Recovering autosaved diagrams"),
92 NULL, NULL, &ok, &cancel);
93 GtkWidget *vbox = GTK_DIALOG(dialog)->vbox;
94 GtkWidget *selectarea = gtk_clist_new(1);
95 GList *iter;
96 gchar **filearray = (gchar**)g_new(gchar *, g_list_length(files)+1);
97 int i;
99 gtk_box_pack_start_defaults(GTK_BOX(vbox), gtk_label_new(_("Autosaved files exist.\nPlease select those you wish to recover.")));
101 for (i = 0, iter = files; iter != NULL; i++, iter = g_list_next(iter)) {
102 filearray[i] = (gchar *)iter->data;
104 filearray[i] = 0;
105 gtk_clist_append(GTK_CLIST(selectarea), filearray);
107 gtk_box_pack_start_defaults(GTK_BOX(vbox), selectarea);
108 gtk_widget_show_all(dialog);
111 /** If autosave files exist, ask the user if they should be restored
113 void
114 autosave_restore_documents(void)
116 gchar *savedir = dia_config_filename("autosave" G_DIR_SEPARATOR_S);
117 GDir *dir = g_dir_open(savedir, 0, NULL);
118 const char *ent;
119 GList *files = NULL;
121 if (dir == NULL) return;
122 while ((ent = g_dir_read_name(dir)) != NULL) {
123 printf("Found autosave file %s\n", ent);
124 files = g_list_prepend(files, g_strdup(ent));
127 if (files != NULL) {
128 autosave_make_restore_dialog(files);
130 g_dir_close(dir);
131 g_free(savedir);
132 g_list_free(files);
134 #endif