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