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