baaba13a5dc93383499ea2da08de0cbcfd2f76e7
[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         retval= BKE_read_exotic(name);
485         
486         /* we didn't succeed, now try to read Blender file */
487         if (retval== 0) {
488                 ListBase wmbase;
489
490                 /* put aside screens to match with persistant windows later */
491                 wm_window_match_init(C, &wmbase); 
492                 
493                 retval= BKE_read_file(C, name, NULL);
494
495                 /* match the read WM with current WM */
496                 wm_window_match_do(C, &wmbase); 
497                 
498 // XXX          mainwindow_set_filename_to_title(G.main->name);
499 //              countall(); <-- will be listener
500 // XXX          sound_initialize_sounds();
501
502 //              winqueue_break= 1;      /* leave queues everywhere */
503
504 // XXX          if(retval==2) init_userdef_themes();    // in case a userdef is read from regular .blend
505                 
506                 if (retval!=0) G.relbase_valid = 1;
507
508 // XXX          undo_editmode_clear();
509                 BKE_reset_undo();
510                 BKE_write_undo(C, "original");  /* save current state */
511
512 //              refresh_interface_font();
513         }
514 //      else if(retval==1)
515 // XXX          BIF_undo_push("Import file");
516 }
517
518 static void outliner_242_patch(void)
519 {
520         ScrArea *sa;
521         
522 // XXX  
523         if(G.curscreen==NULL) return;
524         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
525                 SpaceLink *sl= sa->spacedata.first;
526                 for(; sl; sl= sl->next) {
527                         if(sl->spacetype==SPACE_OOPS) {
528                                 SpaceOops *soops= (SpaceOops *)sl;
529                                 if(soops->type!=SO_OUTLINER) {
530                                         soops->type= SO_OUTLINER;
531 // XXX                                  init_v2d_oops(sa, soops);
532                                 }
533                         }
534                 }
535         }
536         G.fileflags |= G_FILE_GAME_MAT;
537 }
538
539 /* called on startup,  (context entirely filled with NULLs) */
540 /* or called for 'Erase All' */
541 int WM_read_homefile(bContext *C, int from_memory)
542 {
543         ListBase wmbase;
544         char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
545         char *home= BLI_gethome();
546         int success;
547         
548         BLI_clean(home);
549         
550         free_ttfont(); /* still weird... what does it here? */
551                 
552         G.relbase_valid = 0;
553         if (!from_memory) BLI_make_file_string(G.sce, tstr, home, ".B.blend");
554         strcpy(scestr, G.sce);  /* temporary store */
555         
556         /* prevent loading no UI */
557         G.fileflags &= ~G_FILE_NO_UI;
558         
559         /* put aside screens to match with persistant windows later */
560         wm_window_match_init(C, &wmbase); 
561         
562         if (!from_memory && BLI_exists(tstr)) {
563                 success = BKE_read_file(C, tstr, NULL);
564         } else {
565                 success = BKE_read_file_from_memory(C, datatoc_B_blend, datatoc_B_blend_size, NULL);
566                 /* outliner patch for 2.42 .b.blend */
567                 outliner_242_patch();
568         }
569         
570         /* match the read WM with current WM */
571         wm_window_match_do(C, &wmbase); 
572         
573         strcpy(G.sce, scestr); /* restore */
574
575         init_userdef_themes();
576         
577         /* XXX */
578         G.save_over = 0;        // start with save preference untitled.blend
579         G.fileflags &= ~G_FILE_AUTOPLAY;        /*  disable autoplay in .B.blend... */
580 //      mainwindow_set_filename_to_title("");   // empty string re-initializes title to "Blender"
581
582 #ifdef INTERNATIONAL
583 // XXX  read_languagefile();
584 #endif
585         
586 //      refresh_interface_font();
587         
588 //      undo_editmode_clear();
589         BKE_reset_undo();
590         BKE_write_undo(C, "original");  /* save current state */
591         
592         return success;
593 }
594
595
596 static void get_autosave_location(char buf[FILE_MAXDIR+FILE_MAXFILE])
597 {
598         char pidstr[32];
599 #ifdef WIN32
600         char subdir[9];
601         char savedir[FILE_MAXDIR];
602 #endif
603
604         sprintf(pidstr, "%d.blend", abs(getpid()));
605         
606 #ifdef WIN32
607         if (!BLI_exists(U.tempdir)) {
608                 BLI_strncpy(subdir, "autosave", sizeof(subdir));
609                 BLI_make_file_string("/", savedir, BLI_gethome(), subdir);
610                 
611                 /* create a new autosave dir
612                  * function already checks for existence or not */
613                 BLI_recurdir_fileops(savedir);
614         
615                 BLI_make_file_string("/", buf, savedir, pidstr);
616                 return;
617         }
618 #endif
619         
620         BLI_make_file_string("/", buf, U.tempdir, pidstr);
621 }
622
623 void WM_read_autosavefile(bContext *C)
624 {
625         char tstr[FILE_MAX], scestr[FILE_MAX];
626         int save_over;
627
628         BLI_strncpy(scestr, G.sce, FILE_MAX);   /* temporal store */
629         
630         get_autosave_location(tstr);
631
632         save_over = G.save_over;
633         BKE_read_file(C, tstr, NULL);
634         G.save_over = save_over;
635         BLI_strncpy(G.sce, scestr, FILE_MAX);
636 }
637
638
639 void read_Blog(void)
640 {
641         char name[FILE_MAX], filename[FILE_MAX];
642         LinkNode *l, *lines;
643         struct RecentFile *recent;
644         char *line;
645         int num;
646
647         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
648         lines= BLI_read_file_as_lines(name);
649
650         G.recent_files.first = G.recent_files.last = NULL;
651
652         /* read list of recent opend files from .Blog to memory */
653         for (l= lines, num= 0; l && (num<U.recent_files); l= l->next, num++) {
654                 line = l->link;
655                 if (!BLI_streq(line, "")) {
656                         if (num==0) 
657                                 strcpy(G.sce, line);
658                         
659                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
660                         BLI_addtail(&(G.recent_files), recent);
661                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(line)+1), "name of file");
662                         recent->filename[0] = '\0';
663                         
664                         strcpy(recent->filename, line);
665                 }
666         }
667
668         if(G.sce[0] == 0)
669                 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
670         
671         BLI_free_file_lines(lines);
672
673 #ifdef WIN32
674         /* Add the drive names to the listing */
675         {
676                 __int64 tmp;
677                 char folder[MAX_PATH];
678                 char tmps[4];
679                 int i;
680                         
681                 tmp= GetLogicalDrives();
682                 
683                 for (i=2; i < 26; i++) {
684                         if ((tmp>>i) & 1) {
685                                 tmps[0]='a'+i;
686                                 tmps[1]=':';
687                                 tmps[2]='\\';
688                                 tmps[3]=0;
689                                 
690 // XX                           fsmenu_insert_entry(tmps, 0, 0);
691                         }
692                 }
693
694                 /* Adding Desktop and My Documents */
695 // XXX          fsmenu_append_separator();
696
697                 SHGetSpecialFolderPath(0, folder, CSIDL_PERSONAL, 0);
698 // XXX          fsmenu_insert_entry(folder, 0, 0);
699                 SHGetSpecialFolderPath(0, folder, CSIDL_DESKTOPDIRECTORY, 0);
700 // XXX          fsmenu_insert_entry(folder, 0, 0);
701
702 // XXX          fsmenu_append_separator();
703         }
704 #endif
705
706         BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
707         lines= BLI_read_file_as_lines(name);
708
709         for (l= lines; l; l= l->next) {
710                 char *line= l->link;
711                         
712                 if (!BLI_streq(line, "")) {
713 // XXX                  fsmenu_insert_entry(line, 0, 1);
714                 }
715         }
716
717 // XXX  fsmenu_append_separator();
718         
719         /* add last saved file */
720         BLI_split_dirfile(G.sce, name, filename); /* G.sce shouldn't be relative */
721         
722 // XXX  fsmenu_insert_entry(name, 0, 0);
723         
724         BLI_free_file_lines(lines);
725 }
726
727 static void writeBlog(void)
728 {
729         struct RecentFile *recent, *next_recent;
730         char name[FILE_MAXDIR+FILE_MAXFILE];
731         FILE *fp;
732         int i;
733
734         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
735
736         recent = G.recent_files.first;
737         /* refresh .Blog of recent opened files, when current file was changed */
738         if(!(recent) || (strcmp(recent->filename, G.sce)!=0)) {
739                 fp= fopen(name, "w");
740                 if (fp) {
741                         /* add current file to the beginning of list */
742                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
743                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(G.sce)+1), "name of file");
744                         recent->filename[0] = '\0';
745                         strcpy(recent->filename, G.sce);
746                         BLI_addhead(&(G.recent_files), recent);
747                         /* write current file to .Blog */
748                         fprintf(fp, "%s\n", recent->filename);
749                         recent = recent->next;
750                         i=1;
751                         /* write rest of recent opened files to .Blog */
752                         while((i<U.recent_files) && (recent)){
753                                 /* this prevents to have duplicities in list */
754                                 if (strcmp(recent->filename, G.sce)!=0) {
755                                         fprintf(fp, "%s\n", recent->filename);
756                                         recent = recent->next;
757                                 }
758                                 else {
759                                         next_recent = recent->next;
760                                         MEM_freeN(recent->filename);
761                                         BLI_freelinkN(&(G.recent_files), recent);
762                                         recent = next_recent;
763                                 }
764                                 i++;
765                         }
766                         fclose(fp);
767                 }
768         }
769 }
770
771 static void do_history(char *name)
772 {
773         char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
774         int hisnr= U.versions;
775         
776         if(U.versions==0) return;
777         if(strlen(name)<2) return;
778                 
779         while(  hisnr > 1) {
780                 sprintf(tempname1, "%s%d", name, hisnr-1);
781                 sprintf(tempname2, "%s%d", name, hisnr);
782         
783 //              if(BLI_rename(tempname1, tempname2))
784 // XXX                  error("Unable to make version backup");
785                         
786                 hisnr--;
787         }
788                 
789         /* is needed when hisnr==1 */
790         sprintf(tempname1, "%s%d", name, hisnr);
791         
792 //      if(BLI_rename(name, tempname1))
793 // XXX          error("Unable to make version backup");
794 }
795
796 void WM_write_file(bContext *C, char *target)
797 {
798         Library *li;
799         int writeflags, len;
800         char di[FILE_MAX];
801         ReportList reports;
802         
803         len = strlen(target);
804         
805         if (len == 0) return;
806         if (len >= FILE_MAX) {
807 // XXX          error("Path too long, cannot save");
808                 return;
809         }
810  
811         /* send the OnSave event */
812 // XXX  if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript(&CTX_data_scene(C)->id, SCRIPT_ONSAVE);
813
814         for (li= G.main->library.first; li; li= li->id.next) {
815                 if (BLI_streq(li->name, target)) {
816 // XXX                  error("Cannot overwrite used library");
817                         return;
818                 }
819         }
820         
821         if (!BLO_has_bfile_extension(target) && (len+6 < FILE_MAX)) {
822                 sprintf(di, "%s.blend", target);
823         } else {
824                 strcpy(di, target);
825         }
826
827         if (BLI_exists(di)) {
828 // XXX          if(!saveover(di))
829 // XXX                  return; 
830         }
831         
832         if(G.obedit) {
833 // XXX          exit_editmode(0);       /* 0 = no free data */
834         }
835         if (G.fileflags & G_AUTOPACK) {
836                 packAll();
837         }
838         
839 // XXX  waitcursor(1);  // exit_editmode sets cursor too
840
841         do_history(di);
842         
843         /* we use the UserDef to define compression flag */
844         writeflags= G.fileflags & ~G_FILE_COMPRESS;
845         if(U.flag & USER_FILECOMPRESS)
846                 writeflags |= G_FILE_COMPRESS;
847
848         BKE_reports_init(&reports, RPT_STORE);
849         
850         if (BLO_write_file(CTX_data_main(C), di, writeflags, &reports)) {
851                 strcpy(G.sce, di);
852                 G.relbase_valid = 1;
853                 strcpy(G.main->name, di);       /* is guaranteed current file */
854
855 // XXX          mainwindow_set_filename_to_title(G.main->name);
856
857                 G.save_over = 1;
858
859                 writeBlog();
860         } else {
861 // XXX          error("%s", err);
862         }
863
864         BKE_reports_clear(&reports);
865
866 // XXX  waitcursor(0);
867 }
868
869 /* operator entry */
870 int WM_write_homefile(bContext *C, wmOperator *op)
871 {
872         ReportList reports;
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         // XXX error reporting to the user
882         BKE_reports_init(&reports, RPT_PRINT);
883         BLO_write_file(CTX_data_main(C), tstr, write_flags, &reports);
884         BKE_reports_clear(&reports);
885         
886         return OPERATOR_FINISHED;
887 }
888
889 void WM_write_autosave(bContext *C)
890 {
891         ReportList reports;
892         char tstr[FILE_MAXDIR+FILE_MAXFILE];
893         int write_flags;
894         
895         get_autosave_location(tstr);
896
897                 /*  force save as regular blend file */
898         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
899
900         BKE_reports_init(&reports, RPT_PRINT);
901         BLO_write_file(CTX_data_main(C), tstr, write_flags, &reports);
902         BKE_reports_clear(&reports);
903 }
904
905 /* if global undo; remove tempsave, otherwise rename */
906 void delete_autosave(void)
907 {
908         char tstr[FILE_MAXDIR+FILE_MAXFILE];
909         
910         get_autosave_location(tstr);
911
912         if (BLI_exists(tstr)) {
913                 char str[FILE_MAXDIR+FILE_MAXFILE];
914                 BLI_make_file_string("/", str, U.tempdir, "quit.blend");
915
916                 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(tstr, 0, 0);
917                 else BLI_rename(tstr, str);
918         }
919 }
920
921 /***/
922
923
924
925
926