2004-05-17 Hans Breuer <hans@breuer.org>
[dia.git] / app / autosave.c
blobb236f4639906c515d36fc47d3f772ca8ad75c502
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 autosave_make_restore_dialog(GList *files);
50 static void autosave_restore_documents(void);
52 static void
53 autosave_save_diagram(gpointer data)
55 Diagram *dia = (Diagram *)data;
57 gtk_idle_remove_by_data(data);
59 diagram_autosave(dia);
62 /** Makes autosave copies of the diagrams in the appropriate directory
63 * This function will be called after a diagram is modified, and will
64 * only save documents that are modified and not already autosaved,
65 * and only at the next idle period.
67 gboolean
68 autosave_check_autosave(gpointer data)
70 GList *diagrams = dia_open_diagrams();
71 Diagram *diagram;
73 while (diagrams != NULL) {
74 diagram = (Diagram *)diagrams->data;
75 if (diagram_is_modified(diagram) &&
76 !diagram->autosaved) {
77 /* Diagram has been modified. At next idleness, save it */
78 gtk_idle_add((GtkFunction)autosave_save_diagram, diagram);
80 diagrams = g_list_next(diagrams);
82 return TRUE;
85 /* This is old stuff for a restore dialog */
86 /* Doesn't work with autosave files stored in the files dir */
88 /** Create a dialog that asks for files to be restore */
89 static void
90 autosave_make_restore_dialog(GList *files)
92 GtkWidget *ok, *cancel;
93 GtkWidget *dialog = dialog_make(_("Recovering autosaved diagrams"),
94 NULL, NULL, &ok, &cancel);
95 GtkWidget *vbox = GTK_DIALOG(dialog)->vbox;
96 GtkWidget *selectarea = gtk_clist_new(1);
97 GList *iter;
98 gchar **filearray = (gchar**)g_new(gchar *, g_list_length(files)+1);
99 int i;
101 gtk_box_pack_start_defaults(GTK_BOX(vbox), gtk_label_new(_("Autosaved files exist.\nPlease select those you wish to recover.")));
103 for (i = 0, iter = files; iter != NULL; i++, iter = g_list_next(iter)) {
104 filearray[i] = (gchar *)iter->data;
106 filearray[i] = 0;
107 gtk_clist_append(GTK_CLIST(selectarea), filearray);
109 gtk_box_pack_start_defaults(GTK_BOX(vbox), selectarea);
110 gtk_widget_show_all(dialog);
113 /** If autosave files exist, ask the user if they should be restored
115 void
116 autosave_restore_documents(void)
118 gchar *savedir = dia_config_filename("autosave" G_DIR_SEPARATOR_S);
119 GDir *dir = g_dir_open(savedir, 0, NULL);
120 const char *ent;
121 GList *files = NULL;
123 if (dir == NULL) return;
124 while ((ent = g_dir_read_name(dir)) != NULL) {
125 printf("Found autosave file %s\n", ent);
126 files = g_list_prepend(files, g_strdup(ent));
129 if (files != NULL) {
130 autosave_make_restore_dialog(files);
132 g_dir_close(dir);
133 g_free(savedir);
134 g_list_free(files);