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