Merge with trunk, revision 28528 - 28976.
[blender.git] / source / blender / windowmanager / intern / wm_files.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (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
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation 2007
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28         /* placed up here because of crappy
29          * winsock stuff.
30          */
31 #include <stdio.h>
32 #include <string.h>
33
34 #ifdef WIN32
35 #include <windows.h> /* need to include windows.h so _WIN32_IE is defined  */
36 #ifndef _WIN32_IE
37 #define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */
38 #endif
39 #include <shlobj.h> /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff because 'near' is disabled through BLI_windstuff */
40 #include <process.h> /* getpid */
41 #include "BLI_winstuff.h"
42 #else
43 #include <unistd.h> /* getpid */
44 #endif
45
46 #include "MEM_guardedalloc.h"
47 #include "MEM_CacheLimiterC-Api.h"
48
49 #include "BLI_blenlib.h"
50 #include "BLI_linklist.h"
51
52 #include "DNA_anim_types.h"
53 #include "DNA_ipo_types.h" // XXX old animation system
54 #include "DNA_object_types.h"
55 #include "DNA_space_types.h"
56 #include "DNA_userdef_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_screen_types.h"
59 #include "DNA_windowmanager_types.h"
60
61 #include "BKE_blender.h"
62 #include "BKE_context.h"
63 #include "BKE_DerivedMesh.h"
64 #include "BKE_exotic.h"
65 #include "BKE_font.h"
66 #include "BKE_global.h"
67 #include "BKE_library.h"
68 #include "BKE_main.h"
69 #include "BKE_packedFile.h"
70 #include "BKE_report.h"
71 #include "BKE_sound.h"
72 #include "BKE_texture.h"
73 #include "BKE_utildefines.h"
74
75 #include "BLO_readfile.h"
76 #include "BLO_writefile.h"
77
78 #include "RNA_access.h"
79
80 #include "IMB_imbuf.h"
81 #include "IMB_imbuf_types.h"
82 #include "IMB_thumbs.h"
83
84 #include "ED_datafiles.h"
85 #include "ED_object.h"
86 #include "ED_screen.h"
87 #include "ED_sculpt.h"
88 #include "ED_view3d.h"
89 #include "ED_util.h"
90
91 #include "GHOST_C-api.h"
92
93 #include "UI_interface.h"
94
95 #include "GPU_draw.h"
96
97 #include "BPY_extern.h"
98
99 #include "WM_api.h"
100 #include "WM_types.h"
101 #include "wm.h"
102 #include "wm_window.h"
103 #include "wm_event_system.h"
104
105 static void writeBlog(void);
106
107 /* To be able to read files without windows closing, opening, moving 
108    we try to prepare for worst case:
109    - active window gets active screen from file 
110    - restoring the screens from non-active windows 
111    Best case is all screens match, in that case they get assigned to proper window  
112 */
113 static void wm_window_match_init(bContext *C, ListBase *wmlist)
114 {
115         wmWindowManager *wm= G.main->wm.first;
116         wmWindow *win, *active_win;
117         
118         *wmlist= G.main->wm;
119         G.main->wm.first= G.main->wm.last= NULL;
120         
121         active_win = CTX_wm_window(C);
122
123         /* first wrap up running stuff */
124         /* code copied from wm_init_exit.c */
125         for(wm= wmlist->first; wm; wm= wm->id.next) {
126                 
127                 WM_jobs_stop_all(wm);
128                 
129                 for(win= wm->windows.first; win; win= win->next) {
130                 
131                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
132                         WM_event_remove_handlers(C, &win->handlers);
133                         WM_event_remove_handlers(C, &win->modalhandlers);
134                         ED_screen_exit(C, win, win->screen);
135                 }
136         }
137         
138         /* reset active window */
139         CTX_wm_window_set(C, active_win);
140
141         ED_editors_exit(C);
142         
143 return; 
144         if(wm==NULL) return;
145         if(G.fileflags & G_FILE_NO_UI) return;
146         
147         /* we take apart the used screens from non-active window */
148         for(win= wm->windows.first; win; win= win->next) {
149                 BLI_strncpy(win->screenname, win->screen->id.name, MAX_ID_NAME);
150                 if(win!=wm->winactive) {
151                         BLI_remlink(&G.main->screen, win->screen);
152                         //BLI_addtail(screenbase, win->screen);
153                 }
154         }
155 }
156
157 /* match old WM with new, 4 cases:
158   1- no current wm, no read wm: make new default
159   2- no current wm, but read wm: that's OK, do nothing
160   3- current wm, but not in file: try match screen names
161   4- current wm, and wm in file: try match ghostwin 
162 */
163
164 static void wm_window_match_do(bContext *C, ListBase *oldwmlist)
165 {
166         wmWindowManager *oldwm, *wm;
167         wmWindow *oldwin, *win;
168         
169         /* cases 1 and 2 */
170         if(oldwmlist->first==NULL) {
171                 if(G.main->wm.first); /* nothing todo */
172                 else
173                         wm_add_default(C);
174         }
175         else {
176                 /* cases 3 and 4 */
177                 
178                 /* we've read file without wm..., keep current one entirely alive */
179                 if(G.main->wm.first==NULL) {
180                         bScreen *screen= CTX_wm_screen(C);
181                         
182                         /* match oldwm to new dbase, only old files */
183                         
184                         for(wm= oldwmlist->first; wm; wm= wm->id.next) {
185                                 
186                                 for(win= wm->windows.first; win; win= win->next) {
187                                         /* all windows get active screen from file */
188                                         if(screen->winid==0)
189                                                 win->screen= screen;
190                                         else 
191                                                 win->screen= ED_screen_duplicate(win, screen);
192                                         
193                                         BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
194                                         win->screen->winid= win->winid;
195                                 }
196                         }
197                         
198                         G.main->wm= *oldwmlist;
199                         
200                         /* screens were read from file! */
201                         ED_screens_initialize(G.main->wm.first);
202                 }
203                 else {
204                         /* what if old was 3, and loaded 1? */
205                         /* this code could move to setup_appdata */
206                         oldwm= oldwmlist->first;
207                         wm= G.main->wm.first;
208
209                         /* ensure making new keymaps and set space types */
210                         wm->initialized= 0;
211                         wm->winactive= NULL;
212                         
213                         /* only first wm in list has ghostwins */
214                         for(win= wm->windows.first; win; win= win->next) {
215                                 for(oldwin= oldwm->windows.first; oldwin; oldwin= oldwin->next) {
216                                         
217                                         if(oldwin->winid == win->winid ) {
218                                                 win->ghostwin= oldwin->ghostwin;
219                                                 win->active= oldwin->active;
220                                                 if(win->active)
221                                                         wm->winactive= win;
222
223                                                 GHOST_SetWindowUserData(win->ghostwin, win);    /* pointer back */
224                                                 oldwin->ghostwin= NULL;
225                                                 
226                                                 win->eventstate= oldwin->eventstate;
227                                                 oldwin->eventstate= NULL;
228                                                 
229                                                 /* ensure proper screen rescaling */
230                                                 win->sizex= oldwin->sizex;
231                                                 win->sizey= oldwin->sizey;
232                                                 win->posx= oldwin->posx;
233                                                 win->posy= oldwin->posy;
234                                         }
235                                 }
236                         }
237                         wm_close_and_free_all(C, oldwmlist);
238                 }
239         }
240 }
241
242 /* in case UserDef was read, we re-initialize all, and do versioning */
243 static void wm_init_userdef(bContext *C)
244 {
245         extern char btempdir[];
246
247         UI_init_userdef();
248         MEM_CacheLimiter_set_maximum(U.memcachelimit * 1024 * 1024);
249         sound_init(CTX_data_main(C));
250
251         /* set the python auto-execute setting from user prefs */
252         /* disabled by default, unless explicitly enabled in the command line */
253         if ((U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0) G.f |=  G_SCRIPT_AUTOEXEC;
254
255         if(U.tempdir[0]) strncpy(btempdir, U.tempdir, FILE_MAXDIR+FILE_MAXFILE);
256 }
257
258 void WM_read_file(bContext *C, char *name, ReportList *reports)
259 {
260         int retval;
261
262         /* first try to append data from exotic file formats... */
263         /* it throws error box when file doesnt exist and returns -1 */
264         /* note; it should set some error message somewhere... (ton) */
265         retval= BKE_read_exotic(CTX_data_scene(C), name);
266         
267         /* we didn't succeed, now try to read Blender file */
268         if (retval== 0) {
269                 int G_f= G.f;
270                 ListBase wmbase;
271
272                 /* put aside screens to match with persistant windows later */
273                 /* also exit screens and editors */
274                 wm_window_match_init(C, &wmbase); 
275                 
276                 retval= BKE_read_file(C, name, NULL, reports);
277                 G.save_over = 1;
278
279                 /* this flag is initialized by the operator but overwritten on read.
280                  * need to re-enable it here else drivers + registered scripts wont work. */
281                 if(G_f & G_SCRIPT_AUTOEXEC) G.f |= G_SCRIPT_AUTOEXEC;
282                 else                                            G.f &= ~G_SCRIPT_AUTOEXEC;
283
284                 /* match the read WM with current WM */
285                 wm_window_match_do(C, &wmbase);
286                 WM_check(C); /* opens window(s), checks keymaps */
287                 
288 // XXX          mainwindow_set_filename_to_title(G.main->name);
289
290                 if(retval==2) wm_init_userdef(C);       // in case a userdef is read from regular .blend
291                 
292                 if (retval!=0) {
293                         G.relbase_valid = 1;
294                         if(!G.background) /* assume automated tasks with background, dont write recent file list */
295                                 writeBlog();
296                 }
297
298 // XXX          undo_editmode_clear();
299                 BKE_reset_undo();
300                 BKE_write_undo(C, "original");  /* save current state */
301
302                 WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
303 //              refresh_interface_font();
304
305                 CTX_wm_window_set(C, CTX_wm_manager(C)->windows.first);
306                 ED_editors_init(C);
307
308 #ifndef DISABLE_PYTHON
309                 /* run any texts that were loaded in and flagged as modules */
310                 BPY_load_user_modules(C);
311 #endif
312                 CTX_wm_window_set(C, NULL); /* exits queues */
313         }
314         else if(retval==1)
315                 BKE_write_undo(C, "Import file");
316         else if(retval == -1) {
317                 if(reports)
318                         BKE_reportf(reports, RPT_ERROR, "Can't read file \"%s\".", name);
319         }
320 }
321
322
323 /* called on startup,  (context entirely filled with NULLs) */
324 /* or called for 'New File' */
325 /* op can be NULL */
326 int WM_read_homefile(bContext *C, wmOperator *op)
327 {
328         ListBase wmbase;
329         char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
330         char *home= BLI_gethome();
331         int from_memory= op?RNA_boolean_get(op->ptr, "factory"):0;
332         int success;
333                 
334         BLI_clean(home);
335         
336         free_ttfont(); /* still weird... what does it here? */
337                 
338         G.relbase_valid = 0;
339         if (!from_memory) {
340                 BLI_make_file_string(G.sce, tstr, home, ".B25.blend");
341         }
342         strcpy(scestr, G.sce);  /* temporary store */
343         
344         /* prevent loading no UI */
345         G.fileflags &= ~G_FILE_NO_UI;
346         
347         /* put aside screens to match with persistant windows later */
348         wm_window_match_init(C, &wmbase); 
349         
350         if (!from_memory && BLI_exists(tstr)) {
351                 success = BKE_read_file(C, tstr, NULL, NULL);
352         } else {
353                 success = BKE_read_file_from_memory(C, datatoc_B_blend, datatoc_B_blend_size, NULL, NULL);
354                 if (wmbase.first == NULL) wm_clear_default_size(C);
355         }
356         
357         /* match the read WM with current WM */
358         wm_window_match_do(C, &wmbase); 
359         WM_check(C); /* opens window(s), checks keymaps */
360
361         strcpy(G.sce, scestr); /* restore */
362         
363         wm_init_userdef(C);
364         
365         /* When loading factory settings, the reset solid OpenGL lights need to be applied. */
366         if (!G.background) GPU_default_lights();
367         
368         /* XXX */
369         G.save_over = 0;        // start with save preference untitled.blend
370         G.fileflags &= ~G_FILE_AUTOPLAY;        /*  disable autoplay in .B.blend... */
371 //      mainwindow_set_filename_to_title("");   // empty string re-initializes title to "Blender"
372         
373 //      refresh_interface_font();
374         
375 //      undo_editmode_clear();
376         BKE_reset_undo();
377         BKE_write_undo(C, "original");  /* save current state */
378
379         ED_editors_init(C);
380         
381         WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
382         CTX_wm_window_set(C, NULL); /* exits queues */
383
384         return OPERATOR_FINISHED;
385 }
386
387
388 void read_Blog(void)
389 {
390         char name[FILE_MAX];
391         LinkNode *l, *lines;
392         struct RecentFile *recent;
393         char *line;
394         int num;
395
396         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
397         lines= BLI_read_file_as_lines(name);
398
399         G.recent_files.first = G.recent_files.last = NULL;
400
401         /* read list of recent opend files from .Blog to memory */
402         for (l= lines, num= 0; l && (num<U.recent_files); l= l->next) {
403                 line = l->link;
404                 if (line[0] && BLI_exists(line)) {
405                         if (num==0) 
406                                 strcpy(G.sce, line); /* note: this seems highly dodgy since the file isnt actually read. please explain. - campbell */
407                         
408                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
409                         BLI_addtail(&(G.recent_files), recent);
410                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(line)+1), "name of file");
411                         recent->filename[0] = '\0';
412                         
413                         strcpy(recent->filename, line);
414                         num++;
415                 }
416         }
417
418         if(G.sce[0] == 0)
419                 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
420         
421         BLI_free_file_lines(lines);
422
423 }
424
425 static void writeBlog(void)
426 {
427         struct RecentFile *recent, *next_recent;
428         char name[FILE_MAXDIR+FILE_MAXFILE];
429         FILE *fp;
430         int i;
431
432         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
433
434         recent = G.recent_files.first;
435         /* refresh .Blog of recent opened files, when current file was changed */
436         if(!(recent) || (strcmp(recent->filename, G.sce)!=0)) {
437                 fp= fopen(name, "w");
438                 if (fp) {
439                         /* add current file to the beginning of list */
440                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
441                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(G.sce)+1), "name of file");
442                         recent->filename[0] = '\0';
443                         strcpy(recent->filename, G.sce);
444                         BLI_addhead(&(G.recent_files), recent);
445                         /* write current file to .Blog */
446                         fprintf(fp, "%s\n", recent->filename);
447                         recent = recent->next;
448                         i=1;
449                         /* write rest of recent opened files to .Blog */
450                         while((i<U.recent_files) && (recent)){
451                                 /* this prevents to have duplicities in list */
452                                 if (strcmp(recent->filename, G.sce)!=0) {
453                                         fprintf(fp, "%s\n", recent->filename);
454                                         recent = recent->next;
455                                 }
456                                 else {
457                                         next_recent = recent->next;
458                                         MEM_freeN(recent->filename);
459                                         BLI_freelinkN(&(G.recent_files), recent);
460                                         recent = next_recent;
461                                 }
462                                 i++;
463                         }
464                         fclose(fp);
465                 }
466         }
467 }
468
469 static void do_history(char *name, ReportList *reports)
470 {
471         char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
472         int hisnr= U.versions;
473         
474         if(U.versions==0) return;
475         if(strlen(name)<2) return;
476                 
477         while(hisnr > 1) {
478                 sprintf(tempname1, "%s%d", name, hisnr-1);
479                 sprintf(tempname2, "%s%d", name, hisnr);
480         
481                 if(BLI_rename(tempname1, tempname2))
482                         BKE_report(reports, RPT_ERROR, "Unable to make version backup");
483                         
484                 hisnr--;
485         }
486                 
487         /* is needed when hisnr==1 */
488         sprintf(tempname1, "%s%d", name, hisnr);
489         
490         if(BLI_rename(name, tempname1))
491                 BKE_report(reports, RPT_ERROR, "Unable to make version backup");
492 }
493
494 /* writes a thumbnail for a blendfile */
495 static void writeThumb(const char *path, Scene *scene, int **thumb_pt)
496 {
497         /* will be scaled down, but gives some nice oversampling */
498         ImBuf *ibuf;
499         int *thumb;
500
501         *thumb_pt= NULL;
502         
503         if(G.background || scene->camera==NULL)
504                 return;
505         
506         thumb = MEM_mallocN(((2 + (BLEN_THUMB_SIZE * BLEN_THUMB_SIZE))) * sizeof(int), "write_file thumb");
507         
508         /* gets scaled to BLEN_THUMB_SIZE */
509         ibuf= ED_view3d_draw_offscreen_imbuf_simple(scene, BLEN_THUMB_SIZE * 2, BLEN_THUMB_SIZE * 2, OB_SOLID);
510         
511         if(ibuf) {              
512                 
513                 /* dirty oversampling */
514                 IMB_scaleImBuf(ibuf, BLEN_THUMB_SIZE, BLEN_THUMB_SIZE);
515                 
516                 /* first write into thumb buffer */
517                 thumb[0] = BLEN_THUMB_SIZE;
518                 thumb[1] = BLEN_THUMB_SIZE;
519                 memcpy(thumb + 2, ibuf->rect, BLEN_THUMB_SIZE * BLEN_THUMB_SIZE * sizeof(int));
520                 
521                 /* the image is scaled here */
522                 ibuf= IMB_thumb_create(path, THB_NORMAL, THB_SOURCE_BLEND, ibuf);
523         }
524
525         if (ibuf) {             
526                 IMB_freeImBuf(ibuf);
527         }
528         
529         /* must be freed by caller */
530         *thumb_pt= thumb;
531 }
532
533 int WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports)
534 {
535         Library *li;
536         int len;
537         char di[FILE_MAX];
538
539         int *thumb= NULL;
540
541         len = strlen(target);
542         
543         if (len == 0) {
544                 BKE_report(reports, RPT_ERROR, "Path is empty, cannot save");
545                 return -1;
546         }
547
548         if (len >= FILE_MAX) {
549                 BKE_report(reports, RPT_ERROR, "Path too long, cannot save");
550                 return -1;
551         }
552  
553         /* send the OnSave event */
554         for (li= G.main->library.first; li; li= li->id.next) {
555                 if (BLI_streq(li->name, target)) {
556                         BKE_report(reports, RPT_ERROR, "Cannot overwrite used library");
557                         return -1;
558                 }
559         }
560         
561         if (!BLO_has_bfile_extension(target) && (len+6 < FILE_MAX)) {
562                 sprintf(di, "%s.blend", target);
563         } else {
564                 strcpy(di, target);
565         }
566
567 //      if (BLI_exists(di)) {
568 // XXX          if(!saveover(di))
569 // XXX                  return; 
570 //      }
571         
572         if (G.fileflags & G_AUTOPACK) {
573                 packAll(G.main, reports);
574         }
575         
576         ED_object_exit_editmode(C, EM_DO_UNDO);
577         ED_sculpt_force_update(C);
578
579         do_history(di, reports);
580         
581         /* blend file thumbnail */
582         writeThumb(di, CTX_data_scene(C), &thumb);
583
584         if (BLO_write_file(CTX_data_main(C), di, fileflags, reports, thumb)) {
585                 strcpy(G.sce, di);
586                 G.relbase_valid = 1;
587                 strcpy(G.main->name, di);       /* is guaranteed current file */
588
589                 G.save_over = 1; /* disable untitled.blend convention */
590
591                 if(fileflags & G_FILE_COMPRESS) G.fileflags |= G_FILE_COMPRESS;
592                 else G.fileflags &= ~G_FILE_COMPRESS;
593                 
594                 if(fileflags & G_FILE_AUTOPLAY) G.fileflags |= G_FILE_AUTOPLAY;
595                 else G.fileflags &= ~G_FILE_AUTOPLAY;
596
597                 writeBlog();
598
599                 if(thumb) MEM_freeN(thumb);
600         }
601         else {
602                 if(thumb) MEM_freeN(thumb);
603                 return -1;
604         }
605
606 // XXX  waitcursor(0);
607         return 0;
608 }
609
610 /* operator entry */
611 int WM_write_homefile(bContext *C, wmOperator *op)
612 {
613         wmWindowManager *wm= CTX_wm_manager(C);
614         wmWindow *win= CTX_wm_window(C);
615         char tstr[FILE_MAXDIR+FILE_MAXFILE];
616         int fileflags;
617         
618         /* check current window and close it if temp */
619         if(win->screen->full == SCREENTEMP)
620                 wm_window_close(C, wm, win);
621         
622         BLI_make_file_string("/", tstr, BLI_gethome(), ".B25.blend");
623         
624         /*  force save as regular blend file */
625         fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_AUTOPLAY | G_FILE_LOCK | G_FILE_SIGN);
626
627         BLO_write_file(CTX_data_main(C), tstr, fileflags, op->reports, NULL);
628         
629         G.save_over= 0;
630         
631         return OPERATOR_FINISHED;
632 }
633
634 /************************ autosave ****************************/
635
636 void wm_autosave_location(char *filename)
637 {
638         char pidstr[32];
639 #ifdef WIN32
640         char subdir[9];
641         char savedir[FILE_MAXDIR];
642 #endif
643
644         sprintf(pidstr, "%d.blend", abs(getpid()));
645         
646 #ifdef WIN32
647         if (!BLI_exists(U.tempdir)) {
648                 BLI_strncpy(subdir, "autosave", sizeof(subdir));
649                 BLI_make_file_string("/", savedir, BLI_gethome(), subdir);
650                 
651                 /* create a new autosave dir
652                  * function already checks for existence or not */
653                 BLI_recurdir_fileops(savedir);
654         
655                 BLI_make_file_string("/", filename, savedir, pidstr);
656                 return;
657         }
658 #endif
659         
660         BLI_make_file_string("/", filename, U.tempdir, pidstr);
661 }
662
663 void WM_autosave_init(wmWindowManager *wm)
664 {
665         wm_autosave_timer_ended(wm);
666
667         if(U.flag & USER_AUTOSAVE)
668                 wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime*60.0);
669 }
670
671 void wm_autosave_timer(const bContext *C, wmWindowManager *wm, wmTimer *wt)
672 {
673         wmWindow *win;
674         wmEventHandler *handler;
675         char filename[FILE_MAX];
676         int fileflags;
677
678         WM_event_remove_timer(wm, NULL, wm->autosavetimer);
679
680         /* if a modal operator is running, don't autosave, but try again in 10 seconds */
681         for(win=wm->windows.first; win; win=win->next) {
682                 for(handler=win->modalhandlers.first; handler; handler=handler->next) {
683                         if(handler->op) {
684                                 wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, 10.0);
685                                 return;
686                         }
687                 }
688         }
689         
690         wm_autosave_location(filename);
691
692         /*  force save as regular blend file */
693         fileflags = G.fileflags & ~(G_FILE_COMPRESS|G_FILE_AUTOPLAY |G_FILE_LOCK|G_FILE_SIGN);
694
695         /* no error reporting to console */
696         BLO_write_file(CTX_data_main(C), filename, fileflags, NULL, NULL);
697
698         /* do timer after file write, just in case file write takes a long time */
699         wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime*60.0);
700 }
701
702 void wm_autosave_timer_ended(wmWindowManager *wm)
703 {
704         if(wm->autosavetimer) {
705                 WM_event_remove_timer(wm, NULL, wm->autosavetimer);
706                 wm->autosavetimer= NULL;
707         }
708 }
709
710 void wm_autosave_delete(void)
711 {
712         char filename[FILE_MAX];
713         
714         wm_autosave_location(filename);
715
716         if(BLI_exists(filename)) {
717                 char str[FILE_MAXDIR+FILE_MAXFILE];
718                 BLI_make_file_string("/", str, U.tempdir, "quit.blend");
719
720                 /* if global undo; remove tempsave, otherwise rename */
721                 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(filename, 0, 0);
722                 else BLI_rename(filename, str);
723         }
724 }
725
726 void wm_autosave_read(bContext *C, ReportList *reports)
727 {
728         char filename[FILE_MAX];
729
730         wm_autosave_location(filename);
731         WM_read_file(C, filename, reports);
732 }
733