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_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 return; 
372         if(wm==NULL) return;
373         if(G.fileflags & G_FILE_NO_UI) return;
374         
375         /* we take apart the used screens from non-active window */
376         for(win= wm->windows.first; win; win= win->next) {
377                 BLI_strncpy(win->screenname, win->screen->id.name, MAX_ID_NAME);
378                 if(win!=wm->winactive) {
379                         BLI_remlink(&G.main->screen, win->screen);
380                         //BLI_addtail(screenbase, win->screen);
381                 }
382         }
383 }
384
385 /* match old WM with new, 4 cases:
386   1- no current wm, no read wm: make new default
387   2- no current wm, but read wm: that's OK, do nothing
388   3- current wm, but not in file: try match screen names
389   4- current wm, and wm in file: try match ghostwin 
390
391 */
392 static void wm_window_match_do(bContext *C, ListBase *wmlist)
393 {
394         wmWindowManager *oldwm, *wm;
395         wmWindow *oldwin, *win;
396         
397         /* cases 1 and 2 */
398         if(wmlist->first==NULL) {
399                 if(G.main->wm.first); /* nothing todo */
400                 else
401                         wm_add_default(C);
402         }
403         else {
404                 /* cases 3 and 4 */
405                 
406                 /* we've read file without wm... */
407                 if(G.main->wm.first==NULL) {
408                         /* match oldwm to new dbase, only old files */
409                         
410                         for(wm= wmlist->first; wm; wm= wm->id.next) {
411                                 for(win= wm->windows.first; win; win= win->next) {
412                                         win->screen= (bScreen *)find_id("SR", win->screenname);
413
414                                         if(win->screen==NULL)
415                                                 win->screen= ED_screen_duplicate(win, CTX_wm_screen(C)); /* active screen */
416                                                         
417                                         if(win->screen->winid==0)
418                                                 win->screen->winid= win->winid;
419                                 }
420                         }
421                         /* XXX still solve, case where multiple windows open */
422                         
423                         G.main->wm= *wmlist;
424                 }
425                 else {
426                         /* what if old was 3, and loaded 1? */
427                         /* this code could move to setup_appdata */
428                         oldwm= wmlist->first;
429                         wm= G.main->wm.first;
430                         /* only first wm in list has ghostwins */
431                         for(win= wm->windows.first; win; win= win->next) {
432                                 for(oldwin= oldwm->windows.first; oldwin; oldwin= oldwin->next) {
433                                         
434                                         if(oldwin->winid == win->winid ) {
435                                                 win->ghostwin= oldwin->ghostwin;
436                                                 oldwin->ghostwin= NULL;
437                                         }
438                                 }
439                         }
440                         wm_close_and_free_all(C, wmlist);
441                 }
442         }
443 }
444
445 #ifdef WITH_VERSE
446 static void verse_unsub(void)
447 {
448         extern ListBase session_list;
449         struct VerseSession *session;
450         struct VNode *vnode;
451         
452         session = session_list.first;
453         while(session) {
454                 vnode = session->nodes.lb.first;
455                 while(vnode) {
456                         switch(vnode->type) {
457                                 case V_NT_OBJECT:
458 //XXX                                   unsubscribe_from_obj_node(vnode);
459                                         break;
460                                 case V_NT_GEOMETRY:
461 //XXX                                   unsubscribe_from_geom_node(vnode);
462                                         break;
463                                 case V_NT_BITMAP:
464 //XXX                                   unsubscribe_from_bitmap_node(vnode);
465                                         break;
466                         }
467                         vnode = vnode->next;
468                 }
469                 session = session->next;
470         }
471 }
472 #endif
473
474 void WM_read_file(bContext *C, char *name)
475 {
476         int retval;
477
478 #ifdef WITH_VERSE
479         verse_unsub();          /* bad call here (ton) */
480 #endif
481         
482         /* first try to append data from exotic file formats... */
483         /* it throws error box when file doesnt exist and returns -1 */
484         /* note; it should set some error message somewhere... (ton) */
485         retval= BKE_read_exotic(name);
486         
487         /* we didn't succeed, now try to read Blender file */
488         if (retval== 0) {
489                 ListBase wmbase;
490
491                 /* put aside screens to match with persistant windows later */
492                 wm_window_match_init(C, &wmbase); 
493                 
494                 retval= BKE_read_file(C, name, NULL);
495
496                 /* match the read WM with current WM */
497                 wm_window_match_do(C, &wmbase); 
498                 
499 // XXX          mainwindow_set_filename_to_title(G.main->name);
500 //              countall(); <-- will be listener
501 // XXX          sound_initialize_sounds();
502
503 //              winqueue_break= 1;      /* leave queues everywhere */
504
505 // XXX          if(retval==2) init_userdef_themes();    // in case a userdef is read from regular .blend
506                 
507                 if (retval!=0) G.relbase_valid = 1;
508
509 // XXX          undo_editmode_clear();
510                 BKE_reset_undo();
511                 BKE_write_undo(C, "original");  /* save current state */
512
513 //              refresh_interface_font();
514         }
515 //      else if(retval==1)
516 // XXX          BIF_undo_push("Import file");
517         else if(retval == -1) 
518                 WM_error(C, "Cannot read file");
519 }
520
521 static void outliner_242_patch(void)
522 {
523         ScrArea *sa;
524         
525 // XXX  
526         if(G.curscreen==NULL) return;
527         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
528                 SpaceLink *sl= sa->spacedata.first;
529                 for(; sl; sl= sl->next) {
530                         if(sl->spacetype==SPACE_OOPS) {
531                                 SpaceOops *soops= (SpaceOops *)sl;
532                                 if(soops->type!=SO_OUTLINER) {
533                                         soops->type= SO_OUTLINER;
534 // XXX                                  init_v2d_oops(sa, soops);
535                                 }
536                         }
537                 }
538         }
539         G.fileflags |= G_FILE_GAME_MAT;
540 }
541
542 /* called on startup,  (context entirely filled with NULLs) */
543 /* or called for 'Erase All' */
544 int WM_read_homefile(bContext *C, int from_memory)
545 {
546         ListBase wmbase;
547         char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
548         char *home= BLI_gethome();
549         int success;
550         
551         BLI_clean(home);
552         
553         free_ttfont(); /* still weird... what does it here? */
554                 
555         G.relbase_valid = 0;
556         if (!from_memory) BLI_make_file_string(G.sce, tstr, home, ".B.blend");
557         strcpy(scestr, G.sce);  /* temporary store */
558         
559         /* prevent loading no UI */
560         G.fileflags &= ~G_FILE_NO_UI;
561         
562         /* put aside screens to match with persistant windows later */
563         wm_window_match_init(C, &wmbase); 
564         
565         if (!from_memory && BLI_exists(tstr)) {
566                 success = BKE_read_file(C, tstr, NULL);
567         } else {
568                 success = BKE_read_file_from_memory(C, datatoc_B_blend, datatoc_B_blend_size, NULL);
569                 /* outliner patch for 2.42 .b.blend */
570                 outliner_242_patch();
571         }
572         
573         /* match the read WM with current WM */
574         wm_window_match_do(C, &wmbase); 
575         
576         strcpy(G.sce, scestr); /* restore */
577
578         init_userdef_themes();
579         
580         /* XXX */
581         G.save_over = 0;        // start with save preference untitled.blend
582         G.fileflags &= ~G_FILE_AUTOPLAY;        /*  disable autoplay in .B.blend... */
583 //      mainwindow_set_filename_to_title("");   // empty string re-initializes title to "Blender"
584
585 #ifdef INTERNATIONAL
586 // XXX  read_languagefile();
587 #endif
588         
589 //      refresh_interface_font();
590         
591 //      undo_editmode_clear();
592         BKE_reset_undo();
593         BKE_write_undo(C, "original");  /* save current state */
594         
595         return success;
596 }
597
598
599 static void get_autosave_location(char buf[FILE_MAXDIR+FILE_MAXFILE])
600 {
601         char pidstr[32];
602 #ifdef WIN32
603         char subdir[9];
604         char savedir[FILE_MAXDIR];
605 #endif
606
607         sprintf(pidstr, "%d.blend", abs(getpid()));
608         
609 #ifdef WIN32
610         if (!BLI_exists(U.tempdir)) {
611                 BLI_strncpy(subdir, "autosave", sizeof(subdir));
612                 BLI_make_file_string("/", savedir, BLI_gethome(), subdir);
613                 
614                 /* create a new autosave dir
615                  * function already checks for existence or not */
616                 BLI_recurdir_fileops(savedir);
617         
618                 BLI_make_file_string("/", buf, savedir, pidstr);
619                 return;
620         }
621 #endif
622         
623         BLI_make_file_string("/", buf, U.tempdir, pidstr);
624 }
625
626 void WM_read_autosavefile(bContext *C)
627 {
628         char tstr[FILE_MAX], scestr[FILE_MAX];
629         int save_over;
630
631         BLI_strncpy(scestr, G.sce, FILE_MAX);   /* temporal store */
632         
633         get_autosave_location(tstr);
634
635         save_over = G.save_over;
636         BKE_read_file(C, tstr, NULL);
637         G.save_over = save_over;
638         BLI_strncpy(G.sce, scestr, FILE_MAX);
639 }
640
641
642 void read_Blog(void)
643 {
644         char name[FILE_MAX], filename[FILE_MAX];
645         LinkNode *l, *lines;
646         struct RecentFile *recent;
647         char *line;
648         int num;
649
650         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
651         lines= BLI_read_file_as_lines(name);
652
653         G.recent_files.first = G.recent_files.last = NULL;
654
655         /* read list of recent opend files from .Blog to memory */
656         for (l= lines, num= 0; l && (num<U.recent_files); l= l->next, num++) {
657                 line = l->link;
658                 if (!BLI_streq(line, "")) {
659                         if (num==0) 
660                                 strcpy(G.sce, line);
661                         
662                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
663                         BLI_addtail(&(G.recent_files), recent);
664                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(line)+1), "name of file");
665                         recent->filename[0] = '\0';
666                         
667                         strcpy(recent->filename, line);
668                 }
669         }
670
671         if(G.sce[0] == 0)
672                 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
673         
674         BLI_free_file_lines(lines);
675
676 #ifdef WIN32
677         /* Add the drive names to the listing */
678         {
679                 __int64 tmp;
680                 char folder[MAX_PATH];
681                 char tmps[4];
682                 int i;
683                         
684                 tmp= GetLogicalDrives();
685                 
686                 for (i=2; i < 26; i++) {
687                         if ((tmp>>i) & 1) {
688                                 tmps[0]='a'+i;
689                                 tmps[1]=':';
690                                 tmps[2]='\\';
691                                 tmps[3]=0;
692                                 
693 // XX                           fsmenu_insert_entry(tmps, 0, 0);
694                         }
695                 }
696
697                 /* Adding Desktop and My Documents */
698 // XXX          fsmenu_append_separator();
699
700                 SHGetSpecialFolderPath(0, folder, CSIDL_PERSONAL, 0);
701 // XXX          fsmenu_insert_entry(folder, 0, 0);
702                 SHGetSpecialFolderPath(0, folder, CSIDL_DESKTOPDIRECTORY, 0);
703 // XXX          fsmenu_insert_entry(folder, 0, 0);
704
705 // XXX          fsmenu_append_separator();
706         }
707 #endif
708
709         BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
710         lines= BLI_read_file_as_lines(name);
711
712         for (l= lines; l; l= l->next) {
713                 char *line= l->link;
714                         
715                 if (!BLI_streq(line, "")) {
716 // XXX                  fsmenu_insert_entry(line, 0, 1);
717                 }
718         }
719
720 // XXX  fsmenu_append_separator();
721         
722         /* add last saved file */
723         BLI_split_dirfile(G.sce, name, filename); /* G.sce shouldn't be relative */
724         
725 // XXX  fsmenu_insert_entry(name, 0, 0);
726         
727         BLI_free_file_lines(lines);
728 }
729
730 static void writeBlog(void)
731 {
732         struct RecentFile *recent, *next_recent;
733         char name[FILE_MAXDIR+FILE_MAXFILE];
734         FILE *fp;
735         int i;
736
737         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
738
739         recent = G.recent_files.first;
740         /* refresh .Blog of recent opened files, when current file was changed */
741         if(!(recent) || (strcmp(recent->filename, G.sce)!=0)) {
742                 fp= fopen(name, "w");
743                 if (fp) {
744                         /* add current file to the beginning of list */
745                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
746                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(G.sce)+1), "name of file");
747                         recent->filename[0] = '\0';
748                         strcpy(recent->filename, G.sce);
749                         BLI_addhead(&(G.recent_files), recent);
750                         /* write current file to .Blog */
751                         fprintf(fp, "%s\n", recent->filename);
752                         recent = recent->next;
753                         i=1;
754                         /* write rest of recent opened files to .Blog */
755                         while((i<U.recent_files) && (recent)){
756                                 /* this prevents to have duplicities in list */
757                                 if (strcmp(recent->filename, G.sce)!=0) {
758                                         fprintf(fp, "%s\n", recent->filename);
759                                         recent = recent->next;
760                                 }
761                                 else {
762                                         next_recent = recent->next;
763                                         MEM_freeN(recent->filename);
764                                         BLI_freelinkN(&(G.recent_files), recent);
765                                         recent = next_recent;
766                                 }
767                                 i++;
768                         }
769                         fclose(fp);
770                 }
771         }
772 }
773
774 static void do_history(char *name)
775 {
776         char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
777         int hisnr= U.versions;
778         
779         if(U.versions==0) return;
780         if(strlen(name)<2) return;
781                 
782         while(  hisnr > 1) {
783                 sprintf(tempname1, "%s%d", name, hisnr-1);
784                 sprintf(tempname2, "%s%d", name, hisnr);
785         
786 //              if(BLI_rename(tempname1, tempname2))
787 // XXX                  error("Unable to make version backup");
788                         
789                 hisnr--;
790         }
791                 
792         /* is needed when hisnr==1 */
793         sprintf(tempname1, "%s%d", name, hisnr);
794         
795 //      if(BLI_rename(name, tempname1))
796 // XXX          error("Unable to make version backup");
797 }
798
799 void WM_write_file(bContext *C, char *target)
800 {
801         Library *li;
802         int writeflags, len;
803         char di[FILE_MAX];
804         ReportList reports;
805         
806         len = strlen(target);
807         
808         if (len == 0) return;
809         if (len >= FILE_MAX) {
810 // XXX          error("Path too long, cannot save");
811                 return;
812         }
813  
814         /* send the OnSave event */
815 // XXX  if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript(&CTX_data_scene(C)->id, SCRIPT_ONSAVE);
816
817         for (li= G.main->library.first; li; li= li->id.next) {
818                 if (BLI_streq(li->name, target)) {
819 // XXX                  error("Cannot overwrite used library");
820                         return;
821                 }
822         }
823         
824         if (!BLO_has_bfile_extension(target) && (len+6 < FILE_MAX)) {
825                 sprintf(di, "%s.blend", target);
826         } else {
827                 strcpy(di, target);
828         }
829
830         if (BLI_exists(di)) {
831 // XXX          if(!saveover(di))
832 // XXX                  return; 
833         }
834         
835         if(G.obedit) {
836 // XXX          exit_editmode(0);       /* 0 = no free data */
837         }
838         if (G.fileflags & G_AUTOPACK) {
839                 packAll();
840         }
841         
842 // XXX  waitcursor(1);  // exit_editmode sets cursor too
843
844         do_history(di);
845         
846         /* we use the UserDef to define compression flag */
847         writeflags= G.fileflags & ~G_FILE_COMPRESS;
848         if(U.flag & USER_FILECOMPRESS)
849                 writeflags |= G_FILE_COMPRESS;
850
851         BKE_reports_init(&reports, RPT_STORE);
852         
853         if (BLO_write_file(CTX_data_main(C), di, writeflags, &reports)) {
854                 strcpy(G.sce, di);
855                 G.relbase_valid = 1;
856                 strcpy(G.main->name, di);       /* is guaranteed current file */
857
858 // XXX          mainwindow_set_filename_to_title(G.main->name);
859
860                 G.save_over = 1;
861
862                 writeBlog();
863         } else {
864 // XXX          error("%s", err);
865         }
866
867         BKE_reports_clear(&reports);
868
869 // XXX  waitcursor(0);
870 }
871
872 /* operator entry */
873 int WM_write_homefile(bContext *C, wmOperator *op)
874 {
875         ReportList reports;
876         char tstr[FILE_MAXDIR+FILE_MAXFILE];
877         int write_flags;
878         
879         BLI_make_file_string("/", tstr, BLI_gethome(), ".B.blend");
880                 
881         /*  force save as regular blend file */
882         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
883
884         // XXX error reporting to the user
885         BKE_reports_init(&reports, RPT_PRINT);
886         BLO_write_file(CTX_data_main(C), tstr, write_flags, &reports);
887         BKE_reports_clear(&reports);
888         
889         return OPERATOR_FINISHED;
890 }
891
892 void WM_write_autosave(bContext *C)
893 {
894         ReportList reports;
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         BKE_reports_init(&reports, RPT_PRINT);
904         BLO_write_file(CTX_data_main(C), tstr, write_flags, &reports);
905         BKE_reports_clear(&reports);
906 }
907
908 /* if global undo; remove tempsave, otherwise rename */
909 void delete_autosave(void)
910 {
911         char tstr[FILE_MAXDIR+FILE_MAXFILE];
912         
913         get_autosave_location(tstr);
914
915         if (BLI_exists(tstr)) {
916                 char str[FILE_MAXDIR+FILE_MAXFILE];
917                 BLI_make_file_string("/", str, U.tempdir, "quit.blend");
918
919                 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(tstr, 0, 0);
920                 else BLI_rename(tstr, str);
921         }
922 }
923
924 /***/
925
926
927
928
929