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