1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "autosave.h"
31
32 #include "baseviews.h"
33 #include "fontforgevw.h"
34 #include "sfd.h"
35 /*#include "ustring.h"*/
36 #include "gfile.h"
37 #include "gwidget.h"
38 #include "ustring.h"
39 #include "views.h"
40
41 #include <dirent.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdlib.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48
49 int AutoSaveFrequency=5;
50
51 #if !defined(__MINGW32__)
52 # include <pwd.h>
53 #endif
54
getAutoDirName(char * buffer)55 static char *getAutoDirName(char *buffer) {
56 char *dir=getFontForgeUserDir(Config);
57
58 if ( dir!=NULL ) {
59 sprintf(buffer,"%s/autosave", dir);
60 free(dir);
61 if ( access(buffer,F_OK)==-1 )
62 if ( GFileMkDir(buffer, 0755)==-1 )
63 return( NULL );
64 dir = copy(buffer);
65 }
66 return( dir );
67 }
68
MakeAutoSaveName(SplineFont * sf)69 static void MakeAutoSaveName(SplineFont *sf) {
70 char buffer[1025];
71 char *autosavedir;
72 static int cnt=0;
73
74 if ( sf->autosavename )
75 return;
76 autosavedir = getAutoDirName(buffer);
77 if ( autosavedir==NULL )
78 return;
79 while ( 1 ) {
80 sprintf( buffer, "%s/auto%06x-%d.asfd", autosavedir, getpid(), ++cnt );
81 if ( access(buffer,F_OK)==-1 ) {
82 sf->autosavename = copy(buffer);
83 free(autosavedir);
84 return;
85 }
86 }
87 }
88
89
DoAutoRecoveryExtended(int inquire)90 int DoAutoRecoveryExtended(int inquire)
91 {
92 char buffer[1025];
93 char *recoverdir = getAutoDirName(buffer);
94 DIR *dir;
95 struct dirent *entry;
96 int any = false;
97 SplineFont *sf = NULL;
98 int inquire_state=0;
99
100 if ( recoverdir==NULL )
101 return( false );
102 if ( (dir = opendir(recoverdir))==NULL ) {
103 free(recoverdir);
104 return( false );
105 }
106 while ( (entry=readdir(dir))!=NULL ) {
107 if ( strcmp(entry->d_name,".")==0 || strcmp(entry->d_name,"..")==0 )
108 continue;
109 sprintf(buffer,"%s/%s",recoverdir,entry->d_name);
110 fprintf( stderr, "Recovering from %s... ", buffer);
111 if ( (sf = SFRecoverFile(buffer,inquire,&inquire_state)) ) {
112 any=true;
113 if ( sf->fv==NULL ) /* Doesn't work, cli arguments not parsed yet */
114 FontViewCreate(sf,false);
115 fprintf( stderr, " Done\n" );
116 }
117 }
118 free(recoverdir);
119 closedir(dir);
120 return( any );
121 }
122
DoAutoRecovery(int inquire)123 int DoAutoRecovery(int inquire )
124 {
125 return DoAutoRecoveryExtended( inquire );
126 }
127
128
CleanAutoRecovery(void)129 void CleanAutoRecovery(void) {
130 char buffer[1025];
131 char *recoverdir = getAutoDirName(buffer);
132 DIR *dir;
133 struct dirent *entry;
134
135 if ( recoverdir==NULL )
136 return;
137 if ( (dir = opendir(recoverdir))==NULL ) {
138 free(recoverdir);
139 return;
140 }
141 while ( (entry=readdir(dir))!=NULL ) {
142 if ( strcmp(entry->d_name,".")==0 || strcmp(entry->d_name,"..")==0 )
143 continue;
144 sprintf(buffer,"%s/%s",recoverdir,entry->d_name);
145 if ( unlink(buffer)!=0 ) {
146 fprintf( stderr, "Failed to clean " );
147 perror(buffer);
148 }
149 }
150 free(recoverdir);
151 closedir(dir);
152 }
153
154
_DoAutoSaves(FontViewBase * fvs)155 static void _DoAutoSaves(FontViewBase *fvs) {
156 FontViewBase *fv;
157 SplineFont *sf;
158
159 if ( AutoSaveFrequency<=0 )
160 return;
161
162 for ( fv=fvs; fv!=NULL; fv=fv->next ) {
163 sf = fv->cidmaster?fv->cidmaster:fv->sf;
164 if ( sf->changed_since_autosave ) {
165 if ( sf->autosavename==NULL )
166 MakeAutoSaveName(sf);
167 if ( sf->autosavename!=NULL )
168 SFAutoSave(sf,fv->map);
169 }
170 }
171 }
172
DoAutoSaves(void)173 void DoAutoSaves(void) {
174 _DoAutoSaves(FontViewFirst());
175 }
176