58209c63508f11fe71a576fda0fa9cfc7db8accf
[blender.git] / source / blender / windowmanager / intern / wm_files.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
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.
13  *
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 Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation 2007
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/windowmanager/intern/wm_files.c
27  *  \ingroup wm
28  *
29  * User level access for blend file read/write, file-history and userprefs.
30  */
31
32
33 /* placed up here because of crappy
34  * winsock stuff.
35  */
36 #include <stddef.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #include "zlib.h" /* wm_read_exotic() */
41
42 #ifdef WIN32
43 #  include <windows.h> /* need to include windows.h so _WIN32_IE is defined  */
44 #  ifndef _WIN32_IE
45 #    define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */
46 #  endif
47 #  include <shlobj.h>  /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff
48                         * because 'near' is disabled through BLI_windstuff */
49 #  include <process.h> /* getpid */
50 #  include "BLI_winstuff.h"
51 #else
52 #  include <unistd.h> /* getpid */
53 #endif
54
55 #include "MEM_guardedalloc.h"
56 #include "MEM_CacheLimiterC-Api.h"
57
58 #include "BLI_blenlib.h"
59 #include "BLI_linklist.h"
60 #include "BLI_utildefines.h"
61 #include "BLI_threads.h"
62 #include "BLI_callbacks.h"
63
64 #include "BLF_translation.h"
65
66 #include "DNA_anim_types.h"
67 #include "DNA_object_types.h"
68 #include "DNA_space_types.h"
69 #include "DNA_userdef_types.h"
70 #include "DNA_scene_types.h"
71 #include "DNA_screen_types.h"
72 #include "DNA_windowmanager_types.h"
73
74 #include "BKE_autoexec.h"
75 #include "BKE_blender.h"
76 #include "BKE_context.h"
77 #include "BKE_depsgraph.h"
78 #include "BKE_DerivedMesh.h"
79 #include "BKE_font.h"
80 #include "BKE_global.h"
81 #include "BKE_library.h"
82 #include "BKE_main.h"
83 #include "BKE_multires.h"
84 #include "BKE_packedFile.h"
85 #include "BKE_report.h"
86 #include "BKE_sound.h"
87 #include "BKE_screen.h"
88 #include "BKE_texture.h"
89
90
91 #include "BLO_readfile.h"
92 #include "BLO_writefile.h"
93
94 #include "RNA_access.h"
95
96 #include "IMB_imbuf.h"
97 #include "IMB_imbuf_types.h"
98 #include "IMB_thumbs.h"
99
100 #include "ED_datafiles.h"
101 #include "ED_fileselect.h"
102 #include "ED_object.h"
103 #include "ED_screen.h"
104 #include "ED_sculpt.h"
105 #include "ED_view3d.h"
106 #include "ED_util.h"
107
108 #include "RE_pipeline.h" /* only to report missing engine */
109
110 #include "GHOST_C-api.h"
111 #include "GHOST_Path-api.h"
112
113 #include "UI_interface.h"
114
115 #include "GPU_draw.h"
116
117 #ifdef WITH_PYTHON
118 #include "BPY_extern.h"
119 #endif
120
121 #include "WM_api.h"
122 #include "WM_types.h"
123 #include "wm.h"
124 #include "wm_files.h"
125 #include "wm_window.h"
126 #include "wm_event_system.h"
127
128 static void write_history(void);
129
130 /* To be able to read files without windows closing, opening, moving
131  * we try to prepare for worst case:
132  * - active window gets active screen from file
133  * - restoring the screens from non-active windows
134  * Best case is all screens match, in that case they get assigned to proper window
135  */
136 static void wm_window_match_init(bContext *C, ListBase *wmlist)
137 {
138         wmWindowManager *wm;
139         wmWindow *win, *active_win;
140         
141         *wmlist = G.main->wm;
142         G.main->wm.first = G.main->wm.last = NULL;
143         
144         active_win = CTX_wm_window(C);
145
146         /* first wrap up running stuff */
147         /* code copied from wm_init_exit.c */
148         for (wm = wmlist->first; wm; wm = wm->id.next) {
149                 
150                 WM_jobs_kill_all(wm);
151                 
152                 for (win = wm->windows.first; win; win = win->next) {
153                 
154                         CTX_wm_window_set(C, win);  /* needed by operator close callbacks */
155                         WM_event_remove_handlers(C, &win->handlers);
156                         WM_event_remove_handlers(C, &win->modalhandlers);
157                         ED_screen_exit(C, win, win->screen);
158                 }
159         }
160         
161         /* reset active window */
162         CTX_wm_window_set(C, active_win);
163
164         ED_editors_exit(C);
165
166         /* just had return; here from r12991, this code could just get removed?*/
167 #if 0
168         if (wm == NULL) return;
169         if (G.fileflags & G_FILE_NO_UI) return;
170         
171         /* we take apart the used screens from non-active window */
172         for (win = wm->windows.first; win; win = win->next) {
173                 BLI_strncpy(win->screenname, win->screen->id.name, MAX_ID_NAME);
174                 if (win != wm->winactive) {
175                         BLI_remlink(&G.main->screen, win->screen);
176                         //BLI_addtail(screenbase, win->screen);
177                 }
178         }
179 #endif
180 }
181
182 /* match old WM with new, 4 cases:
183  * 1- no current wm, no read wm: make new default
184  * 2- no current wm, but read wm: that's OK, do nothing
185  * 3- current wm, but not in file: try match screen names
186  * 4- current wm, and wm in file: try match ghostwin
187  */
188
189 static void wm_window_match_do(bContext *C, ListBase *oldwmlist)
190 {
191         wmWindowManager *oldwm, *wm;
192         wmWindow *oldwin, *win;
193         
194         /* cases 1 and 2 */
195         if (oldwmlist->first == NULL) {
196                 if (G.main->wm.first) {
197                         /* nothing todo */
198                 }
199                 else {
200                         wm_add_default(C);
201                 }
202         }
203         else {
204                 /* cases 3 and 4 */
205                 
206                 /* we've read file without wm..., keep current one entirely alive */
207                 if (G.main->wm.first == NULL) {
208                         bScreen *screen = NULL;
209
210                         /* when loading without UI, no matching needed */
211                         if (!(G.fileflags & G_FILE_NO_UI) && (screen = CTX_wm_screen(C))) {
212
213                                 /* match oldwm to new dbase, only old files */
214                                 for (wm = oldwmlist->first; wm; wm = wm->id.next) {
215                                         
216                                         for (win = wm->windows.first; win; win = win->next) {
217                                                 /* all windows get active screen from file */
218                                                 if (screen->winid == 0)
219                                                         win->screen = screen;
220                                                 else 
221                                                         win->screen = ED_screen_duplicate(win, screen);
222                                                 
223                                                 BLI_strncpy(win->screenname, win->screen->id.name + 2, sizeof(win->screenname));
224                                                 win->screen->winid = win->winid;
225                                         }
226                                 }
227                         }
228                         
229                         G.main->wm = *oldwmlist;
230                         
231                         /* screens were read from file! */
232                         ED_screens_initialize(G.main->wm.first);
233                 }
234                 else {
235                         /* what if old was 3, and loaded 1? */
236                         /* this code could move to setup_appdata */
237                         oldwm = oldwmlist->first;
238                         wm = G.main->wm.first;
239
240                         /* preserve key configurations in new wm, to preserve their keymaps */
241                         wm->keyconfigs = oldwm->keyconfigs;
242                         wm->addonconf = oldwm->addonconf;
243                         wm->defaultconf = oldwm->defaultconf;
244                         wm->userconf = oldwm->userconf;
245
246                         oldwm->keyconfigs.first = oldwm->keyconfigs.last = NULL;
247                         oldwm->addonconf = NULL;
248                         oldwm->defaultconf = NULL;
249                         oldwm->userconf = NULL;
250
251                         /* ensure making new keymaps and set space types */
252                         wm->initialized = 0;
253                         wm->winactive = NULL;
254                         
255                         /* only first wm in list has ghostwins */
256                         for (win = wm->windows.first; win; win = win->next) {
257                                 for (oldwin = oldwm->windows.first; oldwin; oldwin = oldwin->next) {
258                                         
259                                         if (oldwin->winid == win->winid) {
260                                                 win->ghostwin = oldwin->ghostwin;
261                                                 win->active = oldwin->active;
262                                                 if (win->active)
263                                                         wm->winactive = win;
264
265                                                 if (!G.background) /* file loading in background mode still calls this */
266                                                         GHOST_SetWindowUserData(win->ghostwin, win);    /* pointer back */
267
268                                                 oldwin->ghostwin = NULL;
269                                                 
270                                                 win->eventstate = oldwin->eventstate;
271                                                 oldwin->eventstate = NULL;
272                                                 
273                                                 /* ensure proper screen rescaling */
274                                                 win->sizex = oldwin->sizex;
275                                                 win->sizey = oldwin->sizey;
276                                                 win->posx = oldwin->posx;
277                                                 win->posy = oldwin->posy;
278                                         }
279                                 }
280                         }
281                         wm_close_and_free_all(C, oldwmlist);
282                 }
283         }
284 }
285
286 /* in case UserDef was read, we re-initialize all, and do versioning */
287 static void wm_init_userdef(bContext *C, const bool from_memory)
288 {
289         /* versioning is here */
290         UI_init_userdef();
291         
292         MEM_CacheLimiter_set_maximum(((size_t)U.memcachelimit) * 1024 * 1024);
293         sound_init(CTX_data_main(C));
294
295         /* needed so loading a file from the command line respects user-pref [#26156] */
296         if (U.flag & USER_FILENOUI) G.fileflags |= G_FILE_NO_UI;
297         else G.fileflags &= ~G_FILE_NO_UI;
298
299         /* set the python auto-execute setting from user prefs */
300         /* enabled by default, unless explicitly enabled in the command line which overrides */
301         if ((G.f & G_SCRIPT_OVERRIDE_PREF) == 0) {
302                 if ((U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0) G.f |=  G_SCRIPT_AUTOEXEC;
303                 else G.f &= ~G_SCRIPT_AUTOEXEC;
304         }
305
306         /* avoid re-saving for every small change to our prefs, allow overrides */
307         if (from_memory) {
308                 UI_init_userdef_factory();
309         }
310
311         /* update tempdir from user preferences */
312         BLI_init_temporary_dir(U.tempdir);
313
314         BKE_userdef_state();
315 }
316
317
318
319 /* return codes */
320 #define BKE_READ_EXOTIC_FAIL_PATH       -3 /* file format is not supported */
321 #define BKE_READ_EXOTIC_FAIL_FORMAT     -2 /* file format is not supported */
322 #define BKE_READ_EXOTIC_FAIL_OPEN       -1 /* Can't open the file */
323 #define BKE_READ_EXOTIC_OK_BLEND         0 /* .blend file */
324 #define BKE_READ_EXOTIC_OK_OTHER         1 /* other supported formats */
325
326
327 /* intended to check for non-blender formats but for now it only reads blends */
328 static int wm_read_exotic(Scene *UNUSED(scene), const char *name)
329 {
330         int len;
331         gzFile gzfile;
332         char header[7];
333         int retval;
334
335         /* make sure we're not trying to read a directory.... */
336
337         len = strlen(name);
338         if (len > 0 && ELEM(name[len - 1], '/', '\\')) {
339                 retval = BKE_READ_EXOTIC_FAIL_PATH;
340         }
341         else {
342                 gzfile = BLI_gzopen(name, "rb");
343                 if (gzfile == NULL) {
344                         retval = BKE_READ_EXOTIC_FAIL_OPEN;
345                 }
346                 else {
347                         len = gzread(gzfile, header, sizeof(header));
348                         gzclose(gzfile);
349                         if (len == sizeof(header) && strncmp(header, "BLENDER", 7) == 0) {
350                                 retval = BKE_READ_EXOTIC_OK_BLEND;
351                         }
352                         else {
353 #if 0           /* historic stuff - no longer used */
354                                 WM_cursor_wait(TRUE);
355
356                                 if (is_foo_format(name)) {
357                                         read_foo(name);
358                                         retval = BKE_READ_EXOTIC_OK_OTHER;
359                                 }
360                                 else
361 #endif
362                                 {
363                                         retval = BKE_READ_EXOTIC_FAIL_FORMAT;
364                                 }
365 #if 0
366                                 WM_cursor_wait(FALSE);
367 #endif
368                         }
369                 }
370         }
371
372         return retval;
373 }
374
375 void WM_file_autoexec_init(const char *filepath)
376 {
377         if (G.f & G_SCRIPT_OVERRIDE_PREF) {
378                 return;
379         }
380
381         if (G.f & G_SCRIPT_AUTOEXEC) {
382                 char path[FILE_MAX];
383                 BLI_split_dir_part(filepath, path, sizeof(path));
384                 if (BKE_autoexec_match(path)) {
385                         G.f &= ~G_SCRIPT_AUTOEXEC;
386                 }
387         }
388 }
389
390 void WM_file_read(bContext *C, const char *filepath, ReportList *reports)
391 {
392         int retval;
393
394         /* so we can get the error message */
395         errno = 0;
396
397         WM_cursor_wait(1);
398
399         BLI_callback_exec(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_PRE);
400
401         /* first try to append data from exotic file formats... */
402         /* it throws error box when file doesn't exist and returns -1 */
403         /* note; it should set some error message somewhere... (ton) */
404         retval = wm_read_exotic(CTX_data_scene(C), filepath);
405         
406         /* we didn't succeed, now try to read Blender file */
407         if (retval == BKE_READ_EXOTIC_OK_BLEND) {
408                 int G_f = G.f;
409                 ListBase wmbase;
410
411                 /* assume automated tasks with background, don't write recent file list */
412                 const int do_history = (G.background == FALSE) && (CTX_wm_manager(C)->op_undo_depth == 0);
413
414                 /* put aside screens to match with persistent windows later */
415                 /* also exit screens and editors */
416                 wm_window_match_init(C, &wmbase); 
417                 
418                 /* confusing this global... */
419                 G.relbase_valid = 1;
420                 retval = BKE_read_file(C, filepath, reports);
421                 /* when loading startup.blend's, we can be left with a blank path */
422                 if (G.main->name[0]) {
423                         G.save_over = 1;
424                 }
425                 else {
426                         G.save_over = 0;
427                         G.relbase_valid = 0;
428                 }
429
430                 /* this flag is initialized by the operator but overwritten on read.
431                  * need to re-enable it here else drivers + registered scripts wont work. */
432                 if (G.f != G_f) {
433                         const int flags_keep = (G_SCRIPT_AUTOEXEC | G_SCRIPT_OVERRIDE_PREF);
434                         G.f = (G.f & ~flags_keep) | (G_f & flags_keep);
435                 }
436
437                 /* match the read WM with current WM */
438                 wm_window_match_do(C, &wmbase);
439                 WM_check(C); /* opens window(s), checks keymaps */
440
441                 if (retval == BKE_READ_FILE_OK_USERPREFS) {
442                         /* in case a userdef is read from regular .blend */
443                         wm_init_userdef(C, false);
444                 }
445                 
446                 if (retval != BKE_READ_FILE_FAIL) {
447                         if (do_history) {
448                                 write_history();
449                         }
450                 }
451
452
453                 WM_event_add_notifier(C, NC_WM | ND_FILEREAD, NULL);
454 //              refresh_interface_font();
455
456                 CTX_wm_window_set(C, CTX_wm_manager(C)->windows.first);
457
458                 ED_editors_init(C);
459                 DAG_on_visible_update(CTX_data_main(C), TRUE);
460
461 #ifdef WITH_PYTHON
462                 /* run any texts that were loaded in and flagged as modules */
463                 BPY_python_reset(C);
464 #endif
465
466                 /* important to do before NULL'ing the context */
467                 BLI_callback_exec(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_POST);
468
469                 if (!G.background) {
470                         /* in background mode this makes it hard to load
471                          * a blend file and do anything since the screen
472                          * won't be set to a valid value again */
473                         CTX_wm_window_set(C, NULL); /* exits queues */
474                 }
475
476 #if 0
477                 /* gives popups on windows but not linux, bug in report API
478                  * but disable for now to stop users getting annoyed  */
479                 /* TODO, make this show in header info window */
480                 {
481                         Scene *sce;
482                         for (sce = G.main->scene.first; sce; sce = sce->id.next) {
483                                 if (sce->r.engine[0] &&
484                                     BLI_findstring(&R_engines, sce->r.engine, offsetof(RenderEngineType, idname)) == NULL)
485                                 {
486                                         BKE_reportf(reports, RPT_ERROR, "Engine '%s' not available for scene '%s' "
487                                                     "(an addon may need to be installed or enabled)",
488                                                     sce->r.engine, sce->id.name + 2);
489                                 }
490                         }
491                 }
492 #endif
493
494                 BKE_reset_undo();
495                 BKE_write_undo(C, "original");  /* save current state */
496         }
497         else if (retval == BKE_READ_EXOTIC_OK_OTHER)
498                 BKE_write_undo(C, "Import file");
499         else if (retval == BKE_READ_EXOTIC_FAIL_OPEN) {
500                 BKE_reportf(reports, RPT_ERROR, "Cannot read file '%s': %s", filepath,
501                             errno ? strerror(errno) : TIP_("unable to open the file"));
502         }
503         else if (retval == BKE_READ_EXOTIC_FAIL_FORMAT) {
504                 BKE_reportf(reports, RPT_ERROR, "File format is not supported in file '%s'", filepath);
505         }
506         else if (retval == BKE_READ_EXOTIC_FAIL_PATH) {
507                 BKE_reportf(reports, RPT_ERROR, "File path '%s' invalid", filepath);
508         }
509         else {
510                 BKE_reportf(reports, RPT_ERROR, "Unknown error loading '%s'", filepath);
511                 BLI_assert(!"invalid 'retval'");
512         }
513
514         WM_cursor_wait(0);
515
516 }
517
518
519 /* called on startup,  (context entirely filled with NULLs) */
520 /* or called for 'New File' */
521 /* both startup.blend and userpref.blend are checked */
522 /* the optional paramater custom_file points to an alterntive startup page */
523 /* custom_file can be NULL */
524 int wm_homefile_read(bContext *C, ReportList *reports, bool from_memory, const char *custom_file)
525 {
526         ListBase wmbase;
527         char startstr[FILE_MAX];
528         char prefstr[FILE_MAX];
529         int success = 0;
530
531         /* Indicates whether user prefereneces were really load from memory.
532          *
533          * This is used for versioning code, and for this we can not rely on from_memory
534          * passed via argument. This is because there might be configuration folder
535          * exists but it might not have userpref.blend and in this case we fallback to
536          * reading home file from memory.
537          *
538          * And in this case versioning code is to be run.
539          */
540         bool read_userdef_from_memory = true;
541
542         /* options exclude eachother */
543         BLI_assert((from_memory && custom_file) == 0);
544
545         BLI_callback_exec(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_PRE);
546
547         G.relbase_valid = 0;
548         if (!from_memory) {
549                 const char * const cfgdir = BLI_get_folder(BLENDER_USER_CONFIG, NULL);
550                 if (custom_file) {
551                         BLI_strncpy(startstr, custom_file, FILE_MAX);
552
553                         if (cfgdir) {
554                                 BLI_make_file_string(G.main->name, prefstr, cfgdir, BLENDER_USERPREF_FILE);
555                         }
556                         else {
557                                 prefstr[0] = '\0';
558                         }
559                 }
560                 else if (cfgdir) {
561                         BLI_make_file_string(G.main->name, startstr, cfgdir, BLENDER_STARTUP_FILE);
562                         BLI_make_file_string(G.main->name, prefstr, cfgdir, BLENDER_USERPREF_FILE);
563                 }
564                 else {
565                         startstr[0] = '\0';
566                         prefstr[0] = '\0';
567                         from_memory = 1;
568                 }
569         }
570         
571         /* prevent loading no UI */
572         G.fileflags &= ~G_FILE_NO_UI;
573         
574         /* put aside screens to match with persistent windows later */
575         wm_window_match_init(C, &wmbase);
576         
577         if (!from_memory) {
578                 if (BLI_access(startstr, R_OK) == 0) {
579                         success = (BKE_read_file(C, startstr, NULL) != BKE_READ_FILE_FAIL);
580                 }
581                 if (U.themes.first == NULL) {
582                         if (G.debug & G_DEBUG)
583                                 printf("\nNote: No (valid) '%s' found, fall back to built-in default.\n\n", startstr);
584                         success = 0;
585                 }
586         }
587
588         if (success == 0 && custom_file && reports) {
589                 BKE_reportf(reports, RPT_ERROR, "Could not read '%s'", custom_file);
590                 /*We can not return from here because wm is already reset*/
591         }
592
593         if (success == 0) {
594                 success = BKE_read_file_from_memory(C, datatoc_startup_blend, datatoc_startup_blend_size, NULL, true);
595                 if (wmbase.first == NULL) wm_clear_default_size(C);
596                 BLI_init_temporary_dir(U.tempdir);
597
598 #ifdef WITH_PYTHON_SECURITY
599                 /* use alternative setting for security nuts
600                  * otherwise we'd need to patch the binary blob - startup.blend.c */
601                 U.flag |= USER_SCRIPT_AUTOEXEC_DISABLE;
602 #endif
603         }
604         
605         /* check new prefs only after startup.blend was finished */
606         if (!from_memory && BLI_exists(prefstr)) {
607                 int done = BKE_read_file_userdef(prefstr, NULL);
608                 if (done) {
609                         read_userdef_from_memory = false;
610                         printf("Read new prefs: %s\n", prefstr);
611                 }
612         }
613         
614         /* prevent buggy files that had G_FILE_RELATIVE_REMAP written out by mistake. Screws up autosaves otherwise
615          * can remove this eventually, only in a 2.53 and older, now its not written */
616         G.fileflags &= ~G_FILE_RELATIVE_REMAP;
617         
618         /* check userdef before open window, keymaps etc */
619         wm_init_userdef(C, read_userdef_from_memory);
620         
621         /* match the read WM with current WM */
622         wm_window_match_do(C, &wmbase); 
623         WM_check(C); /* opens window(s), checks keymaps */
624
625         G.main->name[0] = '\0';
626
627         /* When loading factory settings, the reset solid OpenGL lights need to be applied. */
628         if (!G.background) GPU_default_lights();
629         
630         /* XXX */
631         G.save_over = 0;    // start with save preference untitled.blend
632         G.fileflags &= ~G_FILE_AUTOPLAY;    /*  disable autoplay in startup.blend... */
633
634 //      refresh_interface_font();
635         
636 //      undo_editmode_clear();
637         BKE_reset_undo();
638         BKE_write_undo(C, "original");  /* save current state */
639
640         ED_editors_init(C);
641         DAG_on_visible_update(CTX_data_main(C), TRUE);
642
643 #ifdef WITH_PYTHON
644         if (CTX_py_init_get(C)) {
645                 /* sync addons, these may have changed from the defaults */
646                 BPY_string_exec(C, "__import__('addon_utils').reset_all()");
647
648                 BPY_python_reset(C);
649         }
650 #endif
651
652         /* important to do before NULL'ing the context */
653         BLI_callback_exec(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_POST);
654
655         WM_event_add_notifier(C, NC_WM | ND_FILEREAD, NULL);
656
657         /* in background mode the scene will stay NULL */
658         if (!G.background) {
659                 CTX_wm_window_set(C, NULL); /* exits queues */
660         }
661
662         return TRUE;
663 }
664
665 int wm_history_read_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
666 {
667         ED_file_read_bookmarks();
668         wm_read_history();
669         return OPERATOR_FINISHED;
670 }
671
672 int wm_homefile_read_exec(bContext *C, wmOperator *op)
673 {
674         const bool from_memory = (STREQ(op->type->idname, "WM_OT_read_factory_settings"));
675         char filepath_buf[FILE_MAX];
676         const char *filepath = NULL;
677
678         if (!from_memory) {
679                 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "filepath");
680                 if (RNA_property_is_set(op->ptr, prop)) {
681                         RNA_property_string_get(op->ptr, prop, filepath_buf);
682                         filepath = filepath_buf;
683                         if (BLI_access(filepath, R_OK)) {
684                                 BKE_reportf(op->reports, RPT_ERROR, "Can't read alternative start-up file: '%s'", filepath);
685                                 return OPERATOR_CANCELLED;
686                         }
687                 }
688         }
689
690         return wm_homefile_read(C, op->reports, from_memory, filepath) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
691 }
692
693 void wm_read_history(void)
694 {
695         char name[FILE_MAX];
696         LinkNode *l, *lines;
697         struct RecentFile *recent;
698         char *line;
699         int num;
700         const char * const cfgdir = BLI_get_folder(BLENDER_USER_CONFIG, NULL);
701
702         if (!cfgdir) return;
703
704         BLI_make_file_string("/", name, cfgdir, BLENDER_HISTORY_FILE);
705
706         lines = BLI_file_read_as_lines(name);
707
708         G.recent_files.first = G.recent_files.last = NULL;
709
710         /* read list of recent opened files from recent-files.txt to memory */
711         for (l = lines, num = 0; l && (num < U.recent_files); l = l->next) {
712                 line = l->link;
713                 if (line[0] && BLI_exists(line)) {
714                         recent = (RecentFile *)MEM_mallocN(sizeof(RecentFile), "RecentFile");
715                         BLI_addtail(&(G.recent_files), recent);
716                         recent->filepath = BLI_strdup(line);
717                         num++;
718                 }
719         }
720         
721         BLI_file_free_lines(lines);
722 }
723
724 static void write_history(void)
725 {
726         struct RecentFile *recent, *next_recent;
727         char name[FILE_MAX];
728         const char *user_config_dir;
729         FILE *fp;
730         int i;
731
732         /* no write history for recovered startup files */
733         if (G.main->name[0] == 0)
734                 return;
735         
736         /* will be NULL in background mode */
737         user_config_dir = BLI_get_folder_create(BLENDER_USER_CONFIG, NULL);
738         if (!user_config_dir)
739                 return;
740
741         BLI_make_file_string("/", name, user_config_dir, BLENDER_HISTORY_FILE);
742
743         recent = G.recent_files.first;
744         /* refresh recent-files.txt of recent opened files, when current file was changed */
745         if (!(recent) || (BLI_path_cmp(recent->filepath, G.main->name) != 0)) {
746                 fp = BLI_fopen(name, "w");
747                 if (fp) {
748                         /* add current file to the beginning of list */
749                         recent = (RecentFile *)MEM_mallocN(sizeof(RecentFile), "RecentFile");
750                         recent->filepath = BLI_strdup(G.main->name);
751                         BLI_addhead(&(G.recent_files), recent);
752                         /* write current file to recent-files.txt */
753                         fprintf(fp, "%s\n", recent->filepath);
754                         recent = recent->next;
755                         i = 1;
756                         /* write rest of recent opened files to recent-files.txt */
757                         while ((i < U.recent_files) && (recent)) {
758                                 /* this prevents to have duplicities in list */
759                                 if (BLI_path_cmp(recent->filepath, G.main->name) != 0) {
760                                         fprintf(fp, "%s\n", recent->filepath);
761                                         recent = recent->next;
762                                 }
763                                 else {
764                                         next_recent = recent->next;
765                                         MEM_freeN(recent->filepath);
766                                         BLI_freelinkN(&(G.recent_files), recent);
767                                         recent = next_recent;
768                                 }
769                                 i++;
770                         }
771                         fclose(fp);
772                 }
773
774                 /* also update most recent files on System */
775                 GHOST_addToSystemRecentFiles(G.main->name);
776         }
777 }
778
779 /* screen can be NULL */
780 static ImBuf *blend_file_thumb(Scene *scene, bScreen *screen, int **thumb_pt)
781 {
782         /* will be scaled down, but gives some nice oversampling */
783         ImBuf *ibuf;
784         int *thumb;
785         char err_out[256] = "unknown";
786
787         /* screen if no camera found */
788         ScrArea *sa = NULL;
789         ARegion *ar = NULL;
790         View3D *v3d = NULL;
791
792         *thumb_pt = NULL;
793
794         /* scene can be NULL if running a script at startup and calling the save operator */
795         if (G.background || scene == NULL)
796                 return NULL;
797
798         if ((scene->camera == NULL) && (screen != NULL)) {
799                 sa = BKE_screen_find_big_area(screen, SPACE_VIEW3D, 0);
800                 ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
801                 if (ar) {
802                         v3d = sa->spacedata.first;
803                 }
804         }
805
806         if (scene->camera == NULL && v3d == NULL) {
807                 return NULL;
808         }
809
810         /* gets scaled to BLEN_THUMB_SIZE */
811         if (scene->camera) {
812                 ibuf = ED_view3d_draw_offscreen_imbuf_simple(scene, scene->camera,
813                                                              BLEN_THUMB_SIZE * 2, BLEN_THUMB_SIZE * 2,
814                                                              IB_rect, OB_SOLID, FALSE, FALSE, R_ADDSKY, err_out);
815         }
816         else {
817                 ibuf = ED_view3d_draw_offscreen_imbuf(scene, v3d, ar, BLEN_THUMB_SIZE * 2, BLEN_THUMB_SIZE * 2,
818                                                       IB_rect, FALSE, R_ADDSKY, err_out);
819         }
820
821         if (ibuf) {
822                 float aspect = (scene->r.xsch * scene->r.xasp) / (scene->r.ysch * scene->r.yasp);
823
824                 /* dirty oversampling */
825                 IMB_scaleImBuf(ibuf, BLEN_THUMB_SIZE, BLEN_THUMB_SIZE);
826
827                 /* add pretty overlay */
828                 IMB_overlayblend_thumb(ibuf->rect, ibuf->x, ibuf->y, aspect);
829                 
830                 /* first write into thumb buffer */
831                 thumb = MEM_mallocN(((2 + (BLEN_THUMB_SIZE * BLEN_THUMB_SIZE))) * sizeof(int), "write_file thumb");
832
833                 thumb[0] = BLEN_THUMB_SIZE;
834                 thumb[1] = BLEN_THUMB_SIZE;
835
836                 memcpy(thumb + 2, ibuf->rect, BLEN_THUMB_SIZE * BLEN_THUMB_SIZE * sizeof(int));
837         }
838         else {
839                 /* '*thumb_pt' needs to stay NULL to prevent a bad thumbnail from being handled */
840                 fprintf(stderr, "blend_file_thumb failed to create thumbnail: %s\n", err_out);
841                 thumb = NULL;
842         }
843         
844         /* must be freed by caller */
845         *thumb_pt = thumb;
846         
847         return ibuf;
848 }
849
850 /* easy access from gdb */
851 bool write_crash_blend(void)
852 {
853         char path[FILE_MAX];
854         int fileflags = G.fileflags & ~(G_FILE_HISTORY); /* don't do file history on crash file */
855
856         BLI_strncpy(path, G.main->name, sizeof(path));
857         BLI_replace_extension(path, sizeof(path), "_crash.blend");
858         if (BLO_write_file(G.main, path, fileflags, NULL, NULL)) {
859                 printf("written: %s\n", path);
860                 return 1;
861         }
862         else {
863                 printf("failed: %s\n", path);
864                 return 0;
865         }
866 }
867
868 /**
869  * Flush any temp data from object editing to DNA before writing the blend file to disk.
870  */
871 static void write_flush_editdata(bContext *C)
872 {
873         Object *obedit;
874
875         obedit = CTX_data_edit_object(C);
876
877         if (obedit) {
878                 ED_object_editmode_load(obedit);
879         }
880
881         ED_sculpt_force_update(C);
882 }
883
884 /**
885  * \see #wm_homefile_write_exec wraps #BLO_write_file in a similar way.
886  */
887 int wm_file_write(bContext *C, const char *filepath, int fileflags, ReportList *reports)
888 {
889         Library *li;
890         int len;
891         int *thumb = NULL;
892         ImBuf *ibuf_thumb = NULL;
893
894         len = strlen(filepath);
895         
896         if (len == 0) {
897                 BKE_report(reports, RPT_ERROR, "Path is empty, cannot save");
898                 return -1;
899         }
900
901         if (len >= FILE_MAX) {
902                 BKE_report(reports, RPT_ERROR, "Path too long, cannot save");
903                 return -1;
904         }
905         
906         /* Check if file write permission is ok */
907         if (BLI_exists(filepath) && !BLI_file_is_writable(filepath)) {
908                 BKE_reportf(reports, RPT_ERROR, "Cannot save blend file, path '%s' is not writable", filepath);
909                 return -1;
910         }
911  
912         /* note: used to replace the file extension (to ensure '.blend'),
913          * no need to now because the operator ensures,
914          * its handy for scripts to save to a predefined name without blender editing it */
915         
916         /* send the OnSave event */
917         for (li = G.main->library.first; li; li = li->id.next) {
918                 if (BLI_path_cmp(li->filepath, filepath) == 0) {
919                         BKE_reportf(reports, RPT_ERROR, "Cannot overwrite used library '%.240s'", filepath);
920                         return -1;
921                 }
922         }
923
924         /* blend file thumbnail */
925         /* save before exit_editmode, otherwise derivedmeshes for shared data corrupt #27765) */
926         if ((U.flag & USER_SAVE_PREVIEWS) && BLI_thread_is_main()) {
927                 ibuf_thumb = blend_file_thumb(CTX_data_scene(C), CTX_wm_screen(C), &thumb);
928         }
929
930         BLI_callback_exec(G.main, NULL, BLI_CB_EVT_SAVE_PRE);
931
932         /* operator now handles overwrite checks */
933
934         if (G.fileflags & G_AUTOPACK) {
935                 packAll(G.main, reports);
936         }
937
938         /* don't forget not to return without! */
939         WM_cursor_wait(1);
940         
941         write_flush_editdata(C);
942
943         fileflags |= G_FILE_HISTORY; /* write file history */
944
945         /* first time saving */
946         /* XXX temp solution to solve bug, real fix coming (ton) */
947         if ((G.main->name[0] == '\0') && !(fileflags & G_FILE_SAVE_COPY)) {
948                 BLI_strncpy(G.main->name, filepath, sizeof(G.main->name));
949         }
950
951         /* XXX temp solution to solve bug, real fix coming (ton) */
952         G.main->recovered = 0;
953         
954         if (BLO_write_file(CTX_data_main(C), filepath, fileflags, reports, thumb)) {
955                 if (!(fileflags & G_FILE_SAVE_COPY)) {
956                         G.relbase_valid = 1;
957                         BLI_strncpy(G.main->name, filepath, sizeof(G.main->name));  /* is guaranteed current file */
958         
959                         G.save_over = 1; /* disable untitled.blend convention */
960                 }
961
962                 if (fileflags & G_FILE_COMPRESS) G.fileflags |= G_FILE_COMPRESS;
963                 else G.fileflags &= ~G_FILE_COMPRESS;
964                 
965                 if (fileflags & G_FILE_AUTOPLAY) G.fileflags |= G_FILE_AUTOPLAY;
966                 else G.fileflags &= ~G_FILE_AUTOPLAY;
967
968                 /* prevent background mode scripts from clobbering history */
969                 if (!G.background) {
970                         write_history();
971                 }
972
973                 BLI_callback_exec(G.main, NULL, BLI_CB_EVT_SAVE_POST);
974
975                 /* run this function after because the file cant be written before the blend is */
976                 if (ibuf_thumb) {
977                         IMB_thumb_delete(filepath, THB_FAIL); /* without this a failed thumb overrides */
978                         ibuf_thumb = IMB_thumb_create(filepath, THB_NORMAL, THB_SOURCE_BLEND, ibuf_thumb);
979                         IMB_freeImBuf(ibuf_thumb);
980                 }
981
982                 if (thumb) MEM_freeN(thumb);
983         }
984         else {
985                 if (ibuf_thumb) IMB_freeImBuf(ibuf_thumb);
986                 if (thumb) MEM_freeN(thumb);
987                 
988                 WM_cursor_wait(0);
989                 return -1;
990         }
991
992         WM_cursor_wait(0);
993         
994         return 0;
995 }
996
997 /**
998  * \see #wm_file_write wraps #BLO_write_file in a similar way.
999  */
1000 int wm_homefile_write_exec(bContext *C, wmOperator *op)
1001 {
1002         wmWindowManager *wm = CTX_wm_manager(C);
1003         wmWindow *win = CTX_wm_window(C);
1004         char filepath[FILE_MAX];
1005         int fileflags;
1006
1007         /* check current window and close it if temp */
1008         if (win && win->screen->temp)
1009                 wm_window_close(C, wm, win);
1010         
1011         /* update keymaps in user preferences */
1012         WM_keyconfig_update(wm);
1013         
1014         BLI_make_file_string("/", filepath, BLI_get_folder_create(BLENDER_USER_CONFIG, NULL), BLENDER_STARTUP_FILE);
1015         printf("trying to save homefile at %s ", filepath);
1016         
1017         write_flush_editdata(C);
1018
1019         /*  force save as regular blend file */
1020         fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_AUTOPLAY | G_FILE_LOCK | G_FILE_SIGN | G_FILE_HISTORY);
1021
1022         if (BLO_write_file(CTX_data_main(C), filepath, fileflags | G_FILE_USERPREFS, op->reports, NULL) == 0) {
1023                 printf("fail\n");
1024                 return OPERATOR_CANCELLED;
1025         }
1026         
1027         printf("ok\n");
1028
1029         G.save_over = 0;
1030
1031         return OPERATOR_FINISHED;
1032 }
1033
1034 /* Only save the prefs block. operator entry */
1035 int wm_userpref_write_exec(bContext *C, wmOperator *op)
1036 {
1037         wmWindowManager *wm = CTX_wm_manager(C);
1038         char filepath[FILE_MAX];
1039         
1040         /* update keymaps in user preferences */
1041         WM_keyconfig_update(wm);
1042         
1043         BLI_make_file_string("/", filepath, BLI_get_folder_create(BLENDER_USER_CONFIG, NULL), BLENDER_USERPREF_FILE);
1044         printf("trying to save userpref at %s ", filepath);
1045         
1046         if (BKE_write_file_userdef(filepath, op->reports) == 0) {
1047                 printf("fail\n");
1048                 return OPERATOR_CANCELLED;
1049         }
1050         
1051         printf("ok\n");
1052         
1053         return OPERATOR_FINISHED;
1054 }
1055
1056 /************************ autosave ****************************/
1057
1058 void wm_autosave_location(char *filepath)
1059 {
1060         char pidstr[32];
1061 #ifdef WIN32
1062         const char *savedir;
1063 #endif
1064
1065         BLI_snprintf(pidstr, sizeof(pidstr), "%d.blend", abs(getpid()));
1066
1067 #ifdef WIN32
1068         /* XXX Need to investigate how to handle default location of '/tmp/'
1069          * This is a relative directory on Windows, and it may be
1070          * found. Example:
1071          * Blender installed on D:\ drive, D:\ drive has D:\tmp\
1072          * Now, BLI_exists() will find '/tmp/' exists, but
1073          * BLI_make_file_string will create string that has it most likely on C:\
1074          * through get_default_root().
1075          * If there is no C:\tmp autosave fails. */
1076         if (!BLI_exists(BLI_temporary_dir())) {
1077                 savedir = BLI_get_folder_create(BLENDER_USER_AUTOSAVE, NULL);
1078                 BLI_make_file_string("/", filepath, savedir, pidstr);
1079                 return;
1080         }
1081 #endif
1082
1083         BLI_make_file_string("/", filepath, BLI_temporary_dir(), pidstr);
1084 }
1085
1086 void WM_autosave_init(wmWindowManager *wm)
1087 {
1088         wm_autosave_timer_ended(wm);
1089
1090         if (U.flag & USER_AUTOSAVE)
1091                 wm->autosavetimer = WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime * 60.0);
1092 }
1093
1094 void wm_autosave_timer(const bContext *C, wmWindowManager *wm, wmTimer *UNUSED(wt))
1095 {
1096         wmWindow *win;
1097         wmEventHandler *handler;
1098         char filepath[FILE_MAX];
1099         
1100         Scene *scene = CTX_data_scene(C);
1101
1102         WM_event_remove_timer(wm, NULL, wm->autosavetimer);
1103
1104         /* if a modal operator is running, don't autosave, but try again in 10 seconds */
1105         for (win = wm->windows.first; win; win = win->next) {
1106                 for (handler = win->modalhandlers.first; handler; handler = handler->next) {
1107                         if (handler->op) {
1108                                 wm->autosavetimer = WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, 10.0);
1109                                 return;
1110                         }
1111                 }
1112         }
1113
1114         if (scene) {
1115                 Object *ob = OBACT;
1116
1117                 if (ob && ob->mode & OB_MODE_SCULPT)
1118                         multires_force_update(ob);
1119         }
1120
1121         wm_autosave_location(filepath);
1122
1123         if (U.uiflag & USER_GLOBALUNDO) {
1124                 /* fast save of last undobuffer, now with UI */
1125                 BKE_undo_save_file(filepath);
1126         }
1127         else {
1128                 /*  save as regular blend file */
1129                 int fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_AUTOPLAY | G_FILE_LOCK | G_FILE_SIGN | G_FILE_HISTORY);
1130
1131                 /* no error reporting to console */
1132                 BLO_write_file(CTX_data_main(C), filepath, fileflags, NULL, NULL);
1133         }
1134         /* do timer after file write, just in case file write takes a long time */
1135         wm->autosavetimer = WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime * 60.0);
1136 }
1137
1138 void wm_autosave_timer_ended(wmWindowManager *wm)
1139 {
1140         if (wm->autosavetimer) {
1141                 WM_event_remove_timer(wm, NULL, wm->autosavetimer);
1142                 wm->autosavetimer = NULL;
1143         }
1144 }
1145
1146 void wm_autosave_delete(void)
1147 {
1148         char filename[FILE_MAX];
1149         
1150         wm_autosave_location(filename);
1151
1152         if (BLI_exists(filename)) {
1153                 char str[FILE_MAX];
1154                 BLI_make_file_string("/", str, BLI_temporary_dir(), BLENDER_QUIT_FILE);
1155
1156                 /* if global undo; remove tempsave, otherwise rename */
1157                 if (U.uiflag & USER_GLOBALUNDO) BLI_delete(filename, false, false);
1158                 else BLI_rename(filename, str);
1159         }
1160 }
1161
1162 void wm_autosave_read(bContext *C, ReportList *reports)
1163 {
1164         char filename[FILE_MAX];
1165
1166         wm_autosave_location(filename);
1167         WM_file_read(C, filename, reports);
1168 }
1169