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