rename and negate DISABLE_PYTHON --> WITH_PYTHON
[blender.git] / source / blender / windowmanager / intern / wm_files.c
index fc7bf9b79c7bf1ddc9243b9b0f91d0d0761b98f5..aa99e8fcb1c18fbd3e9f17963da240220766c928 100644 (file)
@@ -96,7 +96,9 @@
 
 #include "GPU_draw.h"
 
+#ifdef WITH_PYTHON
 #include "BPY_extern.h"
+#endif
 
 #include "WM_api.h"
 #include "WM_types.h"
@@ -179,21 +181,23 @@ static void wm_window_match_do(bContext *C, ListBase *oldwmlist)
                
                /* we've read file without wm..., keep current one entirely alive */
                if(G.main->wm.first==NULL) {
-                       bScreen *screen= CTX_wm_screen(C);
-                       
-                       /* match oldwm to new dbase, only old files */
-                       
-                       for(wm= oldwmlist->first; wm; wm= wm->id.next) {
-                               
-                               for(win= wm->windows.first; win; win= win->next) {
-                                       /* all windows get active screen from file */
-                                       if(screen->winid==0)
-                                               win->screen= screen;
-                                       else 
-                                               win->screen= ED_screen_duplicate(win, screen);
+                       /* when loading without UI, no matching needed */
+                       if(!(G.fileflags & G_FILE_NO_UI)) {
+                               bScreen *screen= CTX_wm_screen(C);
+
+                               /* match oldwm to new dbase, only old files */
+                               for(wm= oldwmlist->first; wm; wm= wm->id.next) {
                                        
-                                       BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
-                                       win->screen->winid= win->winid;
+                                       for(win= wm->windows.first; win; win= win->next) {
+                                               /* all windows get active screen from file */
+                                               if(screen->winid==0)
+                                                       win->screen= screen;
+                                               else 
+                                                       win->screen= ED_screen_duplicate(win, screen);
+                                               
+                                               BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
+                                               win->screen->winid= win->winid;
+                                       }
                                }
                        }
                        
@@ -255,8 +259,7 @@ static void wm_init_userdef(bContext *C)
        /* set the python auto-execute setting from user prefs */
        /* disabled by default, unless explicitly enabled in the command line */
        if ((U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0) G.f |=  G_SCRIPT_AUTOEXEC;
-
-       if(U.tempdir[0]) strncpy(btempdir, U.tempdir, FILE_MAXDIR+FILE_MAXFILE);
+       if(U.tempdir[0]) BLI_where_is_temp(btempdir, 1);
 }
 
 void WM_read_file(bContext *C, char *name, ReportList *reports)
@@ -280,7 +283,7 @@ void WM_read_file(bContext *C, char *name, ReportList *reports)
                /* also exit screens and editors */
                wm_window_match_init(C, &wmbase); 
                
-               retval= BKE_read_file(C, name, NULL, reports);
+               retval= BKE_read_file(C, name, reports);
                G.save_over = 1;
 
                /* this flag is initialized by the operator but overwritten on read.
@@ -312,9 +315,9 @@ void WM_read_file(bContext *C, char *name, ReportList *reports)
                CTX_wm_window_set(C, CTX_wm_manager(C)->windows.first);
 
                ED_editors_init(C);
-               DAG_on_load_update();
+               DAG_on_load_update(CTX_data_main(C));
 
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
                /* run any texts that were loaded in and flagged as modules */
                BPY_load_user_modules(C);
 #endif
@@ -335,8 +338,8 @@ void WM_read_file(bContext *C, char *name, ReportList *reports)
 int WM_read_homefile(bContext *C, wmOperator *op)
 {
        ListBase wmbase;
-       char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
-       int from_memory= op?RNA_boolean_get(op->ptr, "factory"):0;
+       char tstr[FILE_MAXDIR+FILE_MAXFILE];
+       int from_memory= op && strcmp(op->type->idname, "WM_OT_read_factory_settings")==0;
        int success;
        
        free_ttfont(); /* still weird... what does it here? */
@@ -345,7 +348,7 @@ int WM_read_homefile(bContext *C, wmOperator *op)
        if (!from_memory) {
                char *cfgdir = BLI_get_folder(BLENDER_USER_CONFIG, NULL);
                if (cfgdir) {
-                       BLI_make_file_string(G.sce, tstr, cfgdir, BLENDER_STARTUP_FILE);
+                       BLI_make_file_string(G.main->name, tstr, cfgdir, BLENDER_STARTUP_FILE);
                } else {
                        tstr[0] = '\0';
                        from_memory = 1;
@@ -354,7 +357,6 @@ int WM_read_homefile(bContext *C, wmOperator *op)
                        }
                }
        }
-       strcpy(scestr, G.sce);  /* temporary store */
        
        /* prevent loading no UI */
        G.fileflags &= ~G_FILE_NO_UI;
@@ -363,18 +365,22 @@ int WM_read_homefile(bContext *C, wmOperator *op)
        wm_window_match_init(C, &wmbase); 
        
        if (!from_memory && BLI_exists(tstr)) {
-               success = BKE_read_file(C, tstr, NULL, NULL);
+               success = BKE_read_file(C, tstr, NULL);
        } else {
-               success = BKE_read_file_from_memory(C, datatoc_startup_blend, datatoc_startup_blend_size, NULL, NULL);
+               success = BKE_read_file_from_memory(C, datatoc_startup_blend, datatoc_startup_blend_size, NULL);
                if (wmbase.first == NULL) wm_clear_default_size(C);
        }
        
+       /* prevent buggy files that had G_FILE_RELATIVE_REMAP written out by mistake. Screws up autosaves otherwise
+        * can remove this eventually, only in a 2.53 and older, now its not written */
+       G.fileflags &= ~G_FILE_RELATIVE_REMAP;
+
        /* match the read WM with current WM */
        wm_window_match_do(C, &wmbase); 
        WM_check(C); /* opens window(s), checks keymaps */
 
-       strcpy(G.sce, scestr); /* restore */
-       
+       G.main->name[0]= '\0';
+
        wm_init_userdef(C);
        
        /* When loading factory settings, the reset solid OpenGL lights need to be applied. */
@@ -392,7 +398,14 @@ int WM_read_homefile(bContext *C, wmOperator *op)
        BKE_write_undo(C, "original");  /* save current state */
 
        ED_editors_init(C);
-       DAG_on_load_update();
+       DAG_on_load_update(CTX_data_main(C));
+
+#ifdef WITH_PYTHON
+       if(CTX_py_init_get(C)) {
+               /* sync addons, these may have changed from the defaults */
+               BPY_eval_string(C, "__import__('bpy').utils.addon_reset_all()");
+       }
+#endif
        
        WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
        CTX_wm_window_set(C, NULL); /* exits queues */
@@ -422,9 +435,6 @@ void read_history(void)
        for (l= lines, num= 0; l && (num<U.recent_files); l= l->next) {
                line = l->link;
                if (line[0] && BLI_exists(line)) {
-                       if (num==0) 
-                               strcpy(G.sce, line); /* note: this seems highly dodgy since the file isnt actually read. please explain. - campbell */
-                       
                        recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
                        BLI_addtail(&(G.recent_files), recent);
                        recent->filepath = (char*)MEM_mallocN(sizeof(char)*(strlen(line)+1), "name of file");
@@ -443,21 +453,27 @@ static void write_history(void)
 {
        struct RecentFile *recent, *next_recent;
        char name[FILE_MAXDIR+FILE_MAXFILE];
+       char *user_config_dir;
        FILE *fp;
        int i;
 
-       BLI_make_file_string("/", name, BLI_get_folder_create(BLENDER_USER_CONFIG, NULL), BLENDER_HISTORY_FILE);
+       /* will be NULL in background mode */
+       user_config_dir = BLI_get_folder_create(BLENDER_USER_CONFIG, NULL);
+       if(!user_config_dir)
+               return;
+
+       BLI_make_file_string("/", name, user_config_dir, BLENDER_HISTORY_FILE);
 
        recent = G.recent_files.first;
        /* refresh recent-files.txt of recent opened files, when current file was changed */
-       if(!(recent) || (strcmp(recent->filepath, G.sce)!=0)) {
+       if(!(recent) || (strcmp(recent->filepath, G.main->name)!=0)) {
                fp= fopen(name, "w");
                if (fp) {
                        /* add current file to the beginning of list */
                        recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
-                       recent->filepath = (char*)MEM_mallocN(sizeof(char)*(strlen(G.sce)+1), "name of file");
+                       recent->filepath = (char*)MEM_mallocN(sizeof(char)*(strlen(G.main->name)+1), "name of file");
                        recent->filepath[0] = '\0';
-                       strcpy(recent->filepath, G.sce);
+                       strcpy(recent->filepath, G.main->name);
                        BLI_addhead(&(G.recent_files), recent);
                        /* write current file to recent-files.txt */
                        fprintf(fp, "%s\n", recent->filepath);
@@ -466,7 +482,7 @@ static void write_history(void)
                        /* write rest of recent opened files to recent-files.txt */
                        while((i<U.recent_files) && (recent)){
                                /* this prevents to have duplicities in list */
-                               if (strcmp(recent->filepath, G.sce)!=0) {
+                               if (strcmp(recent->filepath, G.main->name)!=0) {
                                        fprintf(fp, "%s\n", recent->filepath);
                                        recent = recent->next;
                                }
@@ -508,7 +524,7 @@ static void do_history(char *name, ReportList *reports)
                BKE_report(reports, RPT_ERROR, "Unable to make version backup");
 }
 
-static ImBuf *blend_file_thumb(const char *path, Scene *scene, int **thumb_pt)
+static ImBuf *blend_file_thumb(Scene *scene, int **thumb_pt)
 {
        /* will be scaled down, but gives some nice oversampling */
        ImBuf *ibuf;
@@ -520,7 +536,7 @@ static ImBuf *blend_file_thumb(const char *path, Scene *scene, int **thumb_pt)
                return NULL;
 
        /* gets scaled to BLEN_THUMB_SIZE */
-       ibuf= ED_view3d_draw_offscreen_imbuf_simple(scene, BLEN_THUMB_SIZE * 2, BLEN_THUMB_SIZE * 2, OB_SOLID);
+       ibuf= ED_view3d_draw_offscreen_imbuf_simple(scene, BLEN_THUMB_SIZE * 2, BLEN_THUMB_SIZE * 2, IB_rect, OB_SOLID);
        
        if(ibuf) {              
                float aspect= (scene->r.xsch*scene->r.xasp) / (scene->r.ysch*scene->r.yasp);
@@ -554,7 +570,7 @@ static ImBuf *blend_file_thumb(const char *path, Scene *scene, int **thumb_pt)
 int write_crash_blend(void)
 {
        char path[FILE_MAX];
-       BLI_strncpy(path, G.sce, sizeof(path));
+       BLI_strncpy(path, G.main->name, sizeof(path));
        BLI_replace_extension(path, sizeof(path), "_crash.blend");
        if(BLO_write_file(G.main, path, G.fileflags, NULL, NULL)) {
                printf("written: %s\n", path);
@@ -566,7 +582,7 @@ int write_crash_blend(void)
        }
 }
 
-int WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports)
+int WM_write_file(bContext *C, const char *target, int fileflags, ReportList *reports, int copy)
 {
        Library *li;
        int len;
@@ -587,25 +603,20 @@ int WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports)
                return -1;
        }
  
+       BLI_strncpy(di, target, FILE_MAX);
+       BLI_replace_extension(di, FILE_MAX, ".blend");
+       /* dont use 'target' anymore */
+       
        /* send the OnSave event */
        for (li= G.main->library.first; li; li= li->id.next) {
-               if (BLI_streq(li->name, target)) {
-                       BKE_report(reports, RPT_ERROR, "Cannot overwrite used library");
+               if (strcmp(li->filepath, di) == 0) {
+                       BKE_reportf(reports, RPT_ERROR, "Can't overwrite used library '%f'", di);
                        return -1;
                }
        }
-       
-       if (!BLO_has_bfile_extension(target) && (len+6 < FILE_MAX)) {
-               sprintf(di, "%s.blend", target);
-       } else {
-               strcpy(di, target);
-       }
 
-//     if (BLI_exists(di)) {
-// XXX         if(!saveover(di))
-// XXX                 return; 
-//     }
-       
+       /* operator now handles overwrite checks */
+
        if (G.fileflags & G_AUTOPACK) {
                packAll(G.main, reports);
        }
@@ -614,17 +625,18 @@ int WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports)
        ED_sculpt_force_update(C);
 
        /* blend file thumbnail */
-       ibuf_thumb= blend_file_thumb(di, CTX_data_scene(C), &thumb);
+       ibuf_thumb= blend_file_thumb(CTX_data_scene(C), &thumb);
 
        /* rename to .blend1, do this as last before write */
        do_history(di, reports);
 
        if (BLO_write_file(CTX_data_main(C), di, fileflags, reports, thumb)) {
-               strcpy(G.sce, di);
-               G.relbase_valid = 1;
-               strcpy(G.main->name, di);       /* is guaranteed current file */
-
-               G.save_over = 1; /* disable untitled.blend convention */
+               if(!copy) {
+                       G.relbase_valid = 1;
+                       strcpy(G.main->name, di);       /* is guaranteed current file */
+       
+                       G.save_over = 1; /* disable untitled.blend convention */
+               }
 
                if(fileflags & G_FILE_COMPRESS) G.fileflags |= G_FILE_COMPRESS;
                else G.fileflags &= ~G_FILE_COMPRESS;
@@ -665,15 +677,20 @@ int WM_write_homefile(bContext *C, wmOperator *op)
                wm_window_close(C, wm, win);
        
        BLI_make_file_string("/", tstr, BLI_get_folder_create(BLENDER_USER_CONFIG, NULL), BLENDER_STARTUP_FILE);
-       printf("trying to save homefile at %s \n", tstr);
+       printf("trying to save homefile at %s ", tstr);
        
        /*  force save as regular blend file */
        fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_AUTOPLAY | G_FILE_LOCK | G_FILE_SIGN);
 
-       BLO_write_file(CTX_data_main(C), tstr, fileflags, op->reports, NULL);
+       if(BLO_write_file(CTX_data_main(C), tstr, fileflags, op->reports, NULL) == 0) {
+               printf("fail\n");
+               return OPERATOR_CANCELLED;
+       }
        
+       printf("ok\n");
+
        G.save_over= 0;
-       
+
        return OPERATOR_FINISHED;
 }
 
@@ -689,14 +706,14 @@ void wm_autosave_location(char *filename)
        sprintf(pidstr, "%d.blend", abs(getpid()));
        
 #ifdef WIN32
-       // XXX Need to investigate how to handle default location of '/tmp/'
-       // This is a relative directory on Windows, and it may be
-       // found. Example:
-       // Blender installed on D:\ drive, D:\ drive has D:\tmp\
-       // Now, BLI_exists() will find '/tmp/' exists, but
-       // BLI_make_file_string will create string that has it most likely on C:\
-       // through get_default_root().
-       // If there is no C:\tmp autosave fails.
+       /* XXX Need to investigate how to handle default location of '/tmp/'
+        * This is a relative directory on Windows, and it may be
+        * found. Example:
+        * Blender installed on D:\ drive, D:\ drive has D:\tmp\
+        * Now, BLI_exists() will find '/tmp/' exists, but
+        * BLI_make_file_string will create string that has it most likely on C:\
+        * through get_default_root().
+        * If there is no C:\tmp autosave fails. */
        if (!BLI_exists(U.tempdir)) {
                savedir = BLI_get_folder_create(BLENDER_USER_AUTOSAVE, NULL);
                BLI_make_file_string("/", filename, savedir, pidstr);
@@ -715,7 +732,7 @@ void WM_autosave_init(wmWindowManager *wm)
                wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime*60.0);
 }
 
-void wm_autosave_timer(const bContext *C, wmWindowManager *wm, wmTimer *wt)
+void wm_autosave_timer(const bContext *C, wmWindowManager *wm, wmTimer *UNUSED(wt))
 {
        wmWindow *win;
        wmEventHandler *handler;