2.5
[blender-staging.git] / source / blender / windowmanager / intern / wm_files.c
1 /**
2  * $Id: wm_files.c
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 "BLI_winstuff.h"
41 #include <process.h> /* getpid */
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_texture.h"
72 #include "BKE_utildefines.h"
73
74 #include "BLO_readfile.h"
75 #include "BLO_writefile.h"
76
77 #include "RNA_access.h"
78 #include "RNA_define.h"
79
80 #include "ED_datafiles.h"
81 #include "ED_screen.h"
82 #include "ED_util.h"
83
84 #include "GHOST_C-api.h"
85
86 #include "UI_interface.h"
87
88 #include "GPU_draw.h"
89
90 // XXX #include "BPY_extern.h"
91
92 #include "WM_api.h"
93 #include "WM_types.h"
94 #include "wm.h"
95 #include "wm_window.h"
96
97 /***/
98
99 /* define for setting colors in theme below */
100 #define SETCOL(col, r, g, b, a)  {col[0]=r; col[1]=g; col[2]= b; col[3]= a;}
101
102 /* patching UserDef struct and Themes */
103 static void init_userdef_themes(void)
104 {
105 //      countall();
106         
107         /* the UserDef struct is not corrected with do_versions() .... ugh! */
108         if(U.wheellinescroll == 0) U.wheellinescroll = 3;
109         if(U.menuthreshold1==0) {
110                 U.menuthreshold1= 5;
111                 U.menuthreshold2= 2;
112         }
113         if(U.tb_leftmouse==0) {
114                 U.tb_leftmouse= 5;
115                 U.tb_rightmouse= 5;
116         }
117         if(U.mixbufsize==0) U.mixbufsize= 2048;
118         if (BLI_streq(U.tempdir, "/")) {
119                 char *tmp= getenv("TEMP");
120                 
121                 strcpy(U.tempdir, tmp?tmp:"/tmp/");
122         }
123         if (U.savetime <= 0) {
124                 U.savetime = 1;
125 // XXX          error(".B.blend is buggy, please consider removing it.\n");
126         }
127         /* transform widget settings */
128         if(U.tw_hotspot==0) {
129                 U.tw_hotspot= 14;
130                 U.tw_size= 20;                  // percentage of window size
131                 U.tw_handlesize= 16;    // percentage of widget radius
132         }
133         if(U.pad_rot_angle==0)
134                 U.pad_rot_angle= 15;
135         
136         if(U.flag & USER_CUSTOM_RANGE) 
137                 vDM_ColorBand_store(&U.coba_weight); /* signal for derivedmesh to use colorband */
138         
139         if (G.main->versionfile <= 191) {
140                 strcpy(U.plugtexdir, U.textudir);
141                 strcpy(U.sounddir, "/");
142         }
143         
144         /* patch to set Dupli Armature */
145         if (G.main->versionfile < 220) {
146                 U.dupflag |= USER_DUP_ARM;
147         }
148         
149         /* userdef new option */
150         if (G.main->versionfile <= 222) {
151                 U.vrmlflag= USER_VRML_LAYERS;
152         }
153         
154         /* added seam, normal color, undo */
155         if (G.main->versionfile <= 234) {
156                 bTheme *btheme;
157                 
158                 U.uiflag |= USER_GLOBALUNDO;
159                 if (U.undosteps==0) U.undosteps=32;
160                 
161                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
162                         /* check for alpha==0 is safe, then color was never set */
163                         if(btheme->tv3d.edge_seam[3]==0) {
164                                 SETCOL(btheme->tv3d.edge_seam, 230, 150, 50, 255);
165                         }
166                         if(btheme->tv3d.normal[3]==0) {
167                                 SETCOL(btheme->tv3d.normal, 0x22, 0xDD, 0xDD, 255);
168                         }
169                         if(btheme->tv3d.face_dot[3]==0) {
170                                 SETCOL(btheme->tv3d.face_dot, 255, 138, 48, 255);
171                                 btheme->tv3d.facedot_size= 4;
172                         }
173                 }
174         }
175         if (G.main->versionfile <= 235) {
176                 /* illegal combo... */
177                 if (U.flag & USER_LMOUSESELECT) 
178                         U.flag &= ~USER_TWOBUTTONMOUSE;
179         }
180         if (G.main->versionfile <= 236) {
181                 bTheme *btheme;
182                 /* new space type */
183                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
184                         /* check for alpha==0 is safe, then color was never set */
185                         if(btheme->ttime.back[3]==0) {
186                                 btheme->ttime = btheme->tsnd;   // copy from sound
187                         }
188                         if(btheme->text.syntaxn[3]==0) {
189                                 SETCOL(btheme->text.syntaxn,    0, 0, 200, 255);        /* Numbers  Blue*/
190                                 SETCOL(btheme->text.syntaxl,    100, 0, 0, 255);        /* Strings  red */
191                                 SETCOL(btheme->text.syntaxc,    0, 100, 50, 255);       /* Comments greenish */
192                                 SETCOL(btheme->text.syntaxv,    95, 95, 0, 255);        /* Special */
193                                 SETCOL(btheme->text.syntaxb,    128, 0, 80, 255);       /* Builtin, red-purple */
194                         }
195                 }
196         }
197         if (G.main->versionfile <= 237) {
198                 bTheme *btheme;
199                 /* bone colors */
200                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
201                         /* check for alpha==0 is safe, then color was never set */
202                         if(btheme->tv3d.bone_solid[3]==0) {
203                                 SETCOL(btheme->tv3d.bone_solid, 200, 200, 200, 255);
204                                 SETCOL(btheme->tv3d.bone_pose, 80, 200, 255, 80);
205                         }
206                 }
207         }
208         if (G.main->versionfile <= 238) {
209                 bTheme *btheme;
210                 /* bone colors */
211                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
212                         /* check for alpha==0 is safe, then color was never set */
213                         if(btheme->tnla.strip[3]==0) {
214                                 SETCOL(btheme->tnla.strip_select,       0xff, 0xff, 0xaa, 255);
215                                 SETCOL(btheme->tnla.strip, 0xe4, 0x9c, 0xc6, 255);
216                         }
217                 }
218         }
219         if (G.main->versionfile <= 239) {
220                 bTheme *btheme;
221                 
222                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
223                         /* Lamp theme, check for alpha==0 is safe, then color was never set */
224                         if(btheme->tv3d.lamp[3]==0) {
225                                 SETCOL(btheme->tv3d.lamp,       0, 0, 0, 40);
226 /* TEMPORAL, remove me! (ton) */                                
227                                 U.uiflag |= USER_PLAINMENUS;
228                         }
229                         
230                         /* check for text field selection highlight, set it to text editor highlight by default */
231                         if(btheme->tui.textfield_hi[3]==0) {
232                                 SETCOL(btheme->tui.textfield_hi,        
233                                         btheme->text.shade2[0], 
234                                         btheme->text.shade2[1], 
235                                         btheme->text.shade2[2],
236                                         255);
237                         }
238                 }
239                 if(U.obcenter_dia==0) U.obcenter_dia= 6;
240         }
241         if (G.main->versionfile <= 241) {
242                 bTheme *btheme;
243                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
244                         /* Node editor theme, check for alpha==0 is safe, then color was never set */
245                         if(btheme->tnode.syntaxn[3]==0) {
246                                 /* re-uses syntax color storage */
247                                 btheme->tnode= btheme->tv3d;
248                                 SETCOL(btheme->tnode.edge_select, 255, 255, 255, 255);
249                                 SETCOL(btheme->tnode.syntaxl, 150, 150, 150, 255);      /* TH_NODE, backdrop */
250                                 SETCOL(btheme->tnode.syntaxn, 129, 131, 144, 255);      /* in/output */
251                                 SETCOL(btheme->tnode.syntaxb, 127,127,127, 255);        /* operator */
252                                 SETCOL(btheme->tnode.syntaxv, 142, 138, 145, 255);      /* generator */
253                                 SETCOL(btheme->tnode.syntaxc, 120, 145, 120, 255);      /* group */
254                         }
255                         /* Group theme colors */
256                         if(btheme->tv3d.group[3]==0) {
257                                 SETCOL(btheme->tv3d.group, 0x10, 0x40, 0x10, 255);
258                                 SETCOL(btheme->tv3d.group_active, 0x66, 0xFF, 0x66, 255);
259                         }
260                         /* Sequence editor theme*/
261                         if(btheme->tseq.movie[3]==0) {
262                                 SETCOL(btheme->tseq.movie,      81, 105, 135, 255);
263                                 SETCOL(btheme->tseq.image,      109, 88, 129, 255);
264                                 SETCOL(btheme->tseq.scene,      78, 152, 62, 255);
265                                 SETCOL(btheme->tseq.audio,      46, 143, 143, 255);
266                                 SETCOL(btheme->tseq.effect,     169, 84, 124, 255);
267                                 SETCOL(btheme->tseq.plugin,     126, 126, 80, 255);
268                                 SETCOL(btheme->tseq.transition, 162, 95, 111, 255);
269                                 SETCOL(btheme->tseq.meta,       109, 145, 131, 255);
270                         }
271                         if(!(btheme->tui.iconfile)) {
272                                 BLI_strncpy(btheme->tui.iconfile, "", sizeof(btheme->tui.iconfile));
273                         }
274                 }
275                 
276                 /* set defaults for 3D View rotating axis indicator */ 
277                 /* since size can't be set to 0, this indicates it's not saved in .B.blend */
278                 if (U.rvisize == 0) {
279                         U.rvisize = 15;
280                         U.rvibright = 8;
281                         U.uiflag |= USER_SHOW_ROTVIEWICON;
282                 }
283                 
284         }
285         if (G.main->versionfile <= 242) {
286                 bTheme *btheme;
287                 
288                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
289                         /* long keyframe color */
290                         /* check for alpha==0 is safe, then color was never set */
291                         if(btheme->tact.strip[3]==0) {
292                                 SETCOL(btheme->tv3d.edge_sharp, 255, 32, 32, 255);
293                                 SETCOL(btheme->tact.strip_select,       0xff, 0xff, 0xaa, 204);
294                                 SETCOL(btheme->tact.strip, 0xe4, 0x9c, 0xc6, 204);
295                         }
296                         
297                         /* IPO-Editor - Vertex Size*/
298                         if(btheme->tipo.vertex_size == 0) {
299                                 btheme->tipo.vertex_size= 3;
300                         }
301                 }
302         }
303         if (G.main->versionfile <= 243) {
304                 /* set default number of recently-used files (if not set) */
305                 if (U.recent_files == 0) U.recent_files = 10;
306         }
307         if (G.main->versionfile < 245 || (G.main->versionfile == 245 && G.main->subversionfile < 3)) {
308                 bTheme *btheme;
309                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
310                         SETCOL(btheme->tv3d.editmesh_active, 255, 255, 255, 128);
311                 }
312                 if(U.coba_weight.tot==0)
313                         init_colorband(&U.coba_weight, 1);
314         }
315         if ((G.main->versionfile < 245) || (G.main->versionfile == 245 && G.main->subversionfile < 11)) {
316                 bTheme *btheme;
317                 for (btheme= U.themes.first; btheme; btheme= btheme->next) {
318                         /* these should all use the same colour */
319                         SETCOL(btheme->tv3d.cframe, 0x60, 0xc0, 0x40, 255);
320                         SETCOL(btheme->tipo.cframe, 0x60, 0xc0, 0x40, 255);
321                         SETCOL(btheme->tact.cframe, 0x60, 0xc0, 0x40, 255);
322                         SETCOL(btheme->tnla.cframe, 0x60, 0xc0, 0x40, 255);
323                         SETCOL(btheme->tseq.cframe, 0x60, 0xc0, 0x40, 255);
324                         SETCOL(btheme->tsnd.cframe, 0x60, 0xc0, 0x40, 255);
325                         SETCOL(btheme->ttime.cframe, 0x60, 0xc0, 0x40, 255);
326                 }
327         }
328         if ((G.main->versionfile < 248) || (G.main->versionfile == 248 && G.main->subversionfile < 3)) {
329                 bTheme *btheme;
330                 
331                 /* adjust themes */
332                 for (btheme= U.themes.first; btheme; btheme= btheme->next) {
333                         /* DopeSheet - (Object) Channel color */
334                         SETCOL(btheme->tact.ds_channel,         82, 96, 110, 255);
335                         SETCOL(btheme->tact.ds_subchannel,      124, 137, 150, 255);
336                         
337                         /* Graph Editor - (Object) Channel color */
338                         SETCOL(btheme->tipo.ds_channel,         82, 96, 110, 255);
339                         SETCOL(btheme->tipo.ds_subchannel,      124, 137, 150, 255);
340                         /* Graph Editor - Group Channel color */
341                         SETCOL(btheme->tipo.group, 22, 112, 0, 255);
342                         SETCOL(btheme->tipo.group_active, 125, 233, 96, 255);
343                 }
344                 
345                 /* adjust grease-pencil distances */
346                 U.gp_manhattendist= 1;
347                 U.gp_euclideandist= 2;
348                 
349                 /* adjust default interpolation for new IPO-curves */
350                 U.ipo_new= BEZT_IPO_BEZ;
351         }
352         if (G.main->versionfile < 250) {
353                 bTheme *btheme;
354
355                 /* this was not properly initialized in 2.45 */
356                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
357                         if(btheme->tima.face_dot[3]==0) {
358                                 SETCOL(btheme->tima.editmesh_active, 255, 255, 255, 128);
359                                 SETCOL(btheme->tima.face_dot, 255, 133, 0, 255);
360                                 btheme->tima.facedot_size= 2;
361                         }
362                 }
363         }
364         
365         /* GL Texture Garbage Collection (variable abused above!) */
366         if (U.textimeout == 0) {
367                 U.texcollectrate = 60;
368                 U.textimeout = 120;
369         }
370         if (U.memcachelimit <= 0) {
371                 U.memcachelimit = 32;
372         }
373         if (U.frameserverport == 0) {
374                 U.frameserverport = 8080;
375         }
376
377         MEM_CacheLimiter_set_maximum(U.memcachelimit * 1024 * 1024);
378         
379         // sets themes, fonts, .. from userdef
380         UI_init_userdef();
381         
382         /* funny name, but it is GE stuff, moves userdef stuff to engine */
383 // XXX  space_set_commmandline_options();
384         /* this timer uses U */
385 // XXX  reset_autosave();
386
387 }
388
389 /* To be able to read files without windows closing, opening, moving 
390    we try to prepare for worst case:
391    - active window gets active screen from file 
392    - restoring the screens from non-active windows 
393    Best case is all screens match, in that case they get assigned to proper window  
394 */
395 static void wm_window_match_init(bContext *C, ListBase *wmlist)
396 {
397         wmWindowManager *wm= G.main->wm.first;
398         wmWindow *win;
399         
400         *wmlist= G.main->wm;
401         G.main->wm.first= G.main->wm.last= NULL;
402         
403         /* first wrap up running stuff */
404         /* code copied from wm_init_exit.c */
405         for(wm= wmlist->first; wm; wm= wm->id.next) {
406                 
407                 WM_jobs_stop_all(wm);
408                 
409                 for(win= wm->windows.first; win; win= win->next) {
410                 
411                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
412                         ED_screen_exit(C, win, win->screen);
413                 }
414         }
415         
416         ED_editors_exit(C);
417         
418 return; 
419         if(wm==NULL) return;
420         if(G.fileflags & G_FILE_NO_UI) return;
421         
422         /* we take apart the used screens from non-active window */
423         for(win= wm->windows.first; win; win= win->next) {
424                 BLI_strncpy(win->screenname, win->screen->id.name, MAX_ID_NAME);
425                 if(win!=wm->winactive) {
426                         BLI_remlink(&G.main->screen, win->screen);
427                         //BLI_addtail(screenbase, win->screen);
428                 }
429         }
430 }
431
432 /* match old WM with new, 4 cases:
433   1- no current wm, no read wm: make new default
434   2- no current wm, but read wm: that's OK, do nothing
435   3- current wm, but not in file: try match screen names
436   4- current wm, and wm in file: try match ghostwin 
437 */
438
439 static void wm_window_match_do(bContext *C, ListBase *oldwmlist)
440 {
441         wmWindowManager *oldwm, *wm;
442         wmWindow *oldwin, *win;
443         
444         /* cases 1 and 2 */
445         if(oldwmlist->first==NULL) {
446                 if(G.main->wm.first); /* nothing todo */
447                 else
448                         wm_add_default(C);
449         }
450         else {
451                 /* cases 3 and 4 */
452                 
453                 /* we've read file without wm..., keep current one entirely alive */
454                 if(G.main->wm.first==NULL) {
455                         bScreen *screen= CTX_wm_screen(C);
456                         
457                         /* match oldwm to new dbase, only old files */
458                         
459                         for(wm= oldwmlist->first; wm; wm= wm->id.next) {
460                                 
461                                 for(win= wm->windows.first; win; win= win->next) {
462                                         /* all windows get active screen from file */
463                                         if(screen->winid==0)
464                                                 win->screen= screen;
465                                         else 
466                                                 win->screen= ED_screen_duplicate(win, screen);
467                                         
468                                         BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
469                                         win->screen->winid= win->winid;
470                                 }
471                         }
472                         
473                         G.main->wm= *oldwmlist;
474                         
475                         /* screens were read from file! */
476                         ED_screens_initialize(G.main->wm.first);
477                 }
478                 else {
479                         /* what if old was 3, and loaded 1? */
480                         /* this code could move to setup_appdata */
481                         oldwm= oldwmlist->first;
482                         wm= G.main->wm.first;
483
484                         /* ensure making new keymaps and set space types */
485                         wm->initialized= 0;
486                         
487                         /* only first wm in list has ghostwins */
488                         for(win= wm->windows.first; win; win= win->next) {
489                                 for(oldwin= oldwm->windows.first; oldwin; oldwin= oldwin->next) {
490                                         
491                                         if(oldwin->winid == win->winid ) {
492                                                 win->ghostwin= oldwin->ghostwin;
493                                                 GHOST_SetWindowUserData(win->ghostwin, win);    /* pointer back */
494                                                 oldwin->ghostwin= NULL;
495                                                 
496                                                 win->eventstate= oldwin->eventstate;
497                                                 oldwin->eventstate= NULL;
498                                         }
499                                 }
500                         }
501                         wm_close_and_free_all(C, oldwmlist);
502                 }
503         }
504 }
505
506 void WM_read_file(bContext *C, char *name, ReportList *reports)
507 {
508         int retval;
509
510         /* first try to append data from exotic file formats... */
511         /* it throws error box when file doesnt exist and returns -1 */
512         /* note; it should set some error message somewhere... (ton) */
513         retval= BKE_read_exotic(CTX_data_scene(C), name);
514         
515         /* we didn't succeed, now try to read Blender file */
516         if (retval== 0) {
517                 ListBase wmbase;
518
519                 /* put aside screens to match with persistant windows later */
520                 /* also exit screens and editors */
521                 wm_window_match_init(C, &wmbase); 
522                 
523                 retval= BKE_read_file(C, name, NULL, reports);
524                 G.save_over = 1;
525
526                 /* match the read WM with current WM */
527                 wm_window_match_do(C, &wmbase); 
528                 wm_check(C); /* opens window(s), checks keymaps */
529                 
530 // XXX          mainwindow_set_filename_to_title(G.main->name);
531 // XXX          sound_initialize_sounds();
532
533                 if(retval==2) init_userdef_themes();    // in case a userdef is read from regular .blend
534                 
535                 if (retval!=0) G.relbase_valid = 1;
536
537 // XXX          undo_editmode_clear();
538                 BKE_reset_undo();
539                 BKE_write_undo(C, "original");  /* save current state */
540
541                 WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
542 //              refresh_interface_font();
543                                            
544                 CTX_wm_window_set(C, NULL); /* exits queues */
545         }
546         else if(retval==1)
547                 BKE_write_undo(C, "Import file");
548         else if(retval == -1) {
549                 if(reports && reports->list.first == NULL)
550                         BKE_report(reports, RPT_ERROR, "Cannot read file.");
551         }
552 }
553
554
555 /* called on startup,  (context entirely filled with NULLs) */
556 /* or called for 'Erase All' */
557 int WM_read_homefile(bContext *C, wmOperator *op)
558 {
559         ListBase wmbase;
560         char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
561         char *home= BLI_gethome();
562         int from_memory= op?RNA_boolean_get(op->ptr, "factory"):0;
563         int success;
564                 
565         BLI_clean(home);
566         
567         free_ttfont(); /* still weird... what does it here? */
568                 
569         G.relbase_valid = 0;
570         if (!from_memory) BLI_make_file_string(G.sce, tstr, home, ".B.blend");
571         strcpy(scestr, G.sce);  /* temporary store */
572         
573         /* prevent loading no UI */
574         G.fileflags &= ~G_FILE_NO_UI;
575         
576         /* put aside screens to match with persistant windows later */
577         wm_window_match_init(C, &wmbase); 
578         
579         if (!from_memory && BLI_exists(tstr)) {
580                 success = BKE_read_file(C, tstr, NULL, NULL);
581         } else {
582                 success = BKE_read_file_from_memory(C, datatoc_B_blend, datatoc_B_blend_size, NULL, NULL);
583         }
584         
585         /* match the read WM with current WM */
586         wm_window_match_do(C, &wmbase); 
587         wm_check(C); /* opens window(s), checks keymaps */
588
589         strcpy(G.sce, scestr); /* restore */
590         
591         init_userdef_themes();
592         
593         /* When loading factory settings, the reset solid OpenGL lights need to be applied. */
594         GPU_default_lights();
595         
596         /* XXX */
597         G.save_over = 0;        // start with save preference untitled.blend
598         G.fileflags &= ~G_FILE_AUTOPLAY;        /*  disable autoplay in .B.blend... */
599 //      mainwindow_set_filename_to_title("");   // empty string re-initializes title to "Blender"
600         
601 //      refresh_interface_font();
602         
603 //      undo_editmode_clear();
604         BKE_reset_undo();
605         BKE_write_undo(C, "original");  /* save current state */
606         
607         WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
608         CTX_wm_window_set(C, NULL); /* exits queues */
609                                    
610         return success;
611 }
612
613
614 static void get_autosave_location(char buf[FILE_MAXDIR+FILE_MAXFILE])
615 {
616         char pidstr[32];
617 #ifdef WIN32
618         char subdir[9];
619         char savedir[FILE_MAXDIR];
620 #endif
621
622         sprintf(pidstr, "%d.blend", abs(getpid()));
623         
624 #ifdef WIN32
625         if (!BLI_exists(U.tempdir)) {
626                 BLI_strncpy(subdir, "autosave", sizeof(subdir));
627                 BLI_make_file_string("/", savedir, BLI_gethome(), subdir);
628                 
629                 /* create a new autosave dir
630                  * function already checks for existence or not */
631                 BLI_recurdir_fileops(savedir);
632         
633                 BLI_make_file_string("/", buf, savedir, pidstr);
634                 return;
635         }
636 #endif
637         
638         BLI_make_file_string("/", buf, U.tempdir, pidstr);
639 }
640
641 void WM_read_autosavefile(bContext *C)
642 {
643         char tstr[FILE_MAX], scestr[FILE_MAX];
644         int save_over;
645
646         BLI_strncpy(scestr, G.sce, FILE_MAX);   /* temporal store */
647         
648         get_autosave_location(tstr);
649
650         save_over = G.save_over;
651         BKE_read_file(C, tstr, NULL, NULL);
652         G.save_over = save_over;
653         BLI_strncpy(G.sce, scestr, FILE_MAX);
654 }
655
656
657 void read_Blog(void)
658 {
659         char name[FILE_MAX], filename[FILE_MAX];
660         LinkNode *l, *lines;
661         struct RecentFile *recent;
662         char *line;
663         int num;
664
665         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
666         lines= BLI_read_file_as_lines(name);
667
668         G.recent_files.first = G.recent_files.last = NULL;
669
670         /* read list of recent opend files from .Blog to memory */
671         for (l= lines, num= 0; l && (num<U.recent_files); l= l->next, num++) {
672                 line = l->link;
673                 if (!BLI_streq(line, "")) {
674                         if (num==0) 
675                                 strcpy(G.sce, line);
676                         
677                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
678                         BLI_addtail(&(G.recent_files), recent);
679                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(line)+1), "name of file");
680                         recent->filename[0] = '\0';
681                         
682                         strcpy(recent->filename, line);
683                 }
684         }
685
686         if(G.sce[0] == 0)
687                 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
688         
689         BLI_free_file_lines(lines);
690
691 #ifdef WIN32
692         /* Add the drive names to the listing */
693         {
694                 __int64 tmp;
695                 char folder[MAX_PATH];
696                 char tmps[4];
697                 int i;
698                         
699                 tmp= GetLogicalDrives();
700                 
701                 for (i=2; i < 26; i++) {
702                         if ((tmp>>i) & 1) {
703                                 tmps[0]='a'+i;
704                                 tmps[1]=':';
705                                 tmps[2]='\\';
706                                 tmps[3]=0;
707                                 
708 // XX                           fsmenu_insert_entry(tmps, 0, 0);
709                         }
710                 }
711
712                 /* Adding Desktop and My Documents */
713 // XXX          fsmenu_append_separator();
714
715                 SHGetSpecialFolderPath(0, folder, CSIDL_PERSONAL, 0);
716 // XXX          fsmenu_insert_entry(folder, 0, 0);
717                 SHGetSpecialFolderPath(0, folder, CSIDL_DESKTOPDIRECTORY, 0);
718 // XXX          fsmenu_insert_entry(folder, 0, 0);
719
720 // XXX          fsmenu_append_separator();
721         }
722 #endif
723
724         BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
725         lines= BLI_read_file_as_lines(name);
726
727         for (l= lines; l; l= l->next) {
728                 char *line= l->link;
729                         
730                 if (!BLI_streq(line, "")) {
731 // XXX                  fsmenu_insert_entry(line, 0, 1);
732                 }
733         }
734
735 // XXX  fsmenu_append_separator();
736         
737         /* add last saved file */
738         BLI_split_dirfile(G.sce, name, filename); /* G.sce shouldn't be relative */
739         
740 // XXX  fsmenu_insert_entry(name, 0, 0);
741         
742         BLI_free_file_lines(lines);
743 }
744
745 static void writeBlog(void)
746 {
747         struct RecentFile *recent, *next_recent;
748         char name[FILE_MAXDIR+FILE_MAXFILE];
749         FILE *fp;
750         int i;
751
752         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
753
754         recent = G.recent_files.first;
755         /* refresh .Blog of recent opened files, when current file was changed */
756         if(!(recent) || (strcmp(recent->filename, G.sce)!=0)) {
757                 fp= fopen(name, "w");
758                 if (fp) {
759                         /* add current file to the beginning of list */
760                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
761                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(G.sce)+1), "name of file");
762                         recent->filename[0] = '\0';
763                         strcpy(recent->filename, G.sce);
764                         BLI_addhead(&(G.recent_files), recent);
765                         /* write current file to .Blog */
766                         fprintf(fp, "%s\n", recent->filename);
767                         recent = recent->next;
768                         i=1;
769                         /* write rest of recent opened files to .Blog */
770                         while((i<U.recent_files) && (recent)){
771                                 /* this prevents to have duplicities in list */
772                                 if (strcmp(recent->filename, G.sce)!=0) {
773                                         fprintf(fp, "%s\n", recent->filename);
774                                         recent = recent->next;
775                                 }
776                                 else {
777                                         next_recent = recent->next;
778                                         MEM_freeN(recent->filename);
779                                         BLI_freelinkN(&(G.recent_files), recent);
780                                         recent = next_recent;
781                                 }
782                                 i++;
783                         }
784                         fclose(fp);
785                 }
786         }
787 }
788
789 static void do_history(char *name, ReportList *reports)
790 {
791         char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
792         int hisnr= U.versions;
793         
794         if(U.versions==0) return;
795         if(strlen(name)<2) return;
796                 
797         while(hisnr > 1) {
798                 sprintf(tempname1, "%s%d", name, hisnr-1);
799                 sprintf(tempname2, "%s%d", name, hisnr);
800         
801                 if(BLI_rename(tempname1, tempname2))
802                         BKE_report(reports, RPT_ERROR, "Unable to make version backup");
803                         
804                 hisnr--;
805         }
806                 
807         /* is needed when hisnr==1 */
808         sprintf(tempname1, "%s%d", name, hisnr);
809         
810         if(BLI_rename(name, tempname1))
811                 BKE_report(reports, RPT_ERROR, "Unable to make version backup");
812 }
813
814 void WM_write_file(bContext *C, char *target, ReportList *reports)
815 {
816         Library *li;
817         int writeflags, len;
818         char di[FILE_MAX];
819         
820         len = strlen(target);
821         
822         if (len == 0) return;
823         if (len >= FILE_MAX) {
824                 BKE_report(reports, RPT_ERROR, "Path too long, cannot save");
825                 return;
826         }
827  
828         /* send the OnSave event */
829 // XXX  if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript(&CTX_data_scene(C)->id, SCRIPT_ONSAVE);
830
831         for (li= G.main->library.first; li; li= li->id.next) {
832                 if (BLI_streq(li->name, target)) {
833                         BKE_report(reports, RPT_ERROR, "Cannot overwrite used library");
834                         return;
835                 }
836         }
837         
838         if (!BLO_has_bfile_extension(target) && (len+6 < FILE_MAX)) {
839                 sprintf(di, "%s.blend", target);
840         } else {
841                 strcpy(di, target);
842         }
843
844         if (BLI_exists(di)) {
845 // XXX          if(!saveover(di))
846 // XXX                  return; 
847         }
848         
849         if (G.fileflags & G_AUTOPACK) {
850                 packAll();
851         }
852         
853 // XXX  waitcursor(1);  // exit_editmode sets cursor too
854
855         do_history(di, reports);
856         
857         /* we use the UserDef to define compression flag */
858         writeflags= G.fileflags & ~G_FILE_COMPRESS;
859         if(U.flag & USER_FILECOMPRESS)
860                 writeflags |= G_FILE_COMPRESS;
861         
862         if (BLO_write_file(CTX_data_main(C), di, writeflags, reports)) {
863                 strcpy(G.sce, di);
864                 G.relbase_valid = 1;
865                 strcpy(G.main->name, di);       /* is guaranteed current file */
866
867                 G.save_over = 1; /* disable untitled.blend convention */
868                 
869                 writeBlog();
870         }
871
872 // XXX  waitcursor(0);
873 }
874
875 /* operator entry */
876 int WM_write_homefile(bContext *C, wmOperator *op)
877 {
878         char tstr[FILE_MAXDIR+FILE_MAXFILE];
879         int write_flags;
880         
881         BLI_make_file_string("/", tstr, BLI_gethome(), ".B.blend");
882                 
883         /*  force save as regular blend file */
884         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
885
886         BLO_write_file(CTX_data_main(C), tstr, write_flags, op->reports);
887         
888         G.save_over= 0;
889         
890         return OPERATOR_FINISHED;
891 }
892
893 void WM_write_autosave(bContext *C)
894 {
895         char tstr[FILE_MAXDIR+FILE_MAXFILE];
896         int write_flags;
897         
898         get_autosave_location(tstr);
899
900                 /*  force save as regular blend file */
901         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
902
903                 /* error reporting to console */
904         BLO_write_file(CTX_data_main(C), tstr, write_flags, NULL);
905 }
906
907 /* if global undo; remove tempsave, otherwise rename */
908 void delete_autosave(void)
909 {
910         char tstr[FILE_MAXDIR+FILE_MAXFILE];
911         
912         get_autosave_location(tstr);
913
914         if (BLI_exists(tstr)) {
915                 char str[FILE_MAXDIR+FILE_MAXFILE];
916                 BLI_make_file_string("/", str, U.tempdir, "quit.blend");
917
918                 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(tstr, 0, 0);
919                 else BLI_rename(tstr, str);
920         }
921 }
922
923 /***/
924