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