File Reading: add wrapper function for WM_file_read

also return cancelled when an operator fails to load a file
This commit is contained in:
Campbell Barton 2014-01-29 05:33:05 +11:00
parent 39202a53b5
commit 8c688a052c
3 changed files with 53 additions and 35 deletions

@ -106,7 +106,7 @@ bool WM_is_draw_triple(struct wmWindow *win);
/* files */
void WM_file_autoexec_init(const char *filepath);
void WM_file_read(struct bContext *C, const char *filepath, struct ReportList *reports);
bool WM_file_read(struct bContext *C, const char *filepath, struct ReportList *reports);
void WM_autosave_init(struct wmWindowManager *wm);
void WM_recover_last_session(struct bContext *C, struct ReportList *reports);

@ -388,8 +388,9 @@ void WM_file_autoexec_init(const char *filepath)
}
}
void WM_file_read(bContext *C, const char *filepath, ReportList *reports)
bool WM_file_read(bContext *C, const char *filepath, ReportList *reports)
{
bool success = false;
int retval;
/* so we can get the error message */
@ -412,7 +413,7 @@ void WM_file_read(bContext *C, const char *filepath, ReportList *reports)
ListBase wmbase;
/* assume automated tasks with background, don't write recent file list */
const int do_history = (G.background == FALSE) && (CTX_wm_manager(C)->op_undo_depth == 0);
const bool do_history = (G.background == FALSE) && (CTX_wm_manager(C)->op_undo_depth == 0);
/* put aside screens to match with persistent windows later */
/* also exit screens and editors */
@ -496,6 +497,8 @@ void WM_file_read(bContext *C, const char *filepath, ReportList *reports)
BKE_reset_undo();
BKE_write_undo(C, "original"); /* save current state */
success = true;
}
else if (retval == BKE_READ_EXOTIC_OK_OTHER)
BKE_write_undo(C, "Import file");
@ -516,6 +519,8 @@ void WM_file_read(bContext *C, const char *filepath, ReportList *reports)
WM_cursor_wait(0);
return success;
}

@ -2127,6 +2127,27 @@ static void WM_OT_read_factory_settings(wmOperatorType *ot)
/* *************** open file **************** */
/**
* Wrap #WM_file_read, shared by file reading operators.
*/
static bool wm_file_read_opwrap(bContext *C, const char *filepath, ReportList *reports,
const bool autoexec_init)
{
bool success;
/* XXX wm in context is not set correctly after WM_file_read -> crash */
/* do it before for now, but is this correct with multiple windows? */
WM_event_add_notifier(C, NC_WINDOW, NULL);
if (autoexec_init) {
WM_file_autoexec_init(filepath);
}
success = WM_file_read(C, filepath, reports);
return success;
}
/* currently fits in a pointer */
struct FileRuntime {
bool is_untrusted;
@ -2186,9 +2207,10 @@ static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, const wmEvent *U
static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
{
char path[FILE_MAX];
char filepath[FILE_MAX];
bool success;
RNA_string_get(op->ptr, "filepath", path);
RNA_string_get(op->ptr, "filepath", filepath);
/* re-use last loaded setting so we can reload a file without changing */
open_set_load_ui(op, false);
@ -2204,20 +2226,17 @@ static int wm_open_mainfile_exec(bContext *C, wmOperator *op)
else
G.f &= ~G_SCRIPT_AUTOEXEC;
/* XXX wm in context is not set correctly after WM_file_read -> crash */
/* do it before for now, but is this correct with multiple windows? */
WM_event_add_notifier(C, NC_WINDOW, NULL);
/* autoexec is already set correctly for invoke() for exec() though we need to initialize */
if (!RNA_struct_property_is_set(op->ptr, "use_scripts")) {
WM_file_autoexec_init(path);
}
WM_file_read(C, path, op->reports);
success = wm_file_read_opwrap(C, filepath, op->reports, !(G.f & G_SCRIPT_AUTOEXEC));
/* for file open also popup for warnings, not only errors */
BKE_report_print_level_set(op->reports, RPT_WARNING);
return OPERATOR_FINISHED;
if (success) {
return OPERATOR_FINISHED;
}
else {
return OPERATOR_CANCELLED;
}
}
static bool wm_open_mainfile_check(bContext *UNUSED(C), wmOperator *op)
@ -2498,20 +2517,14 @@ static void WM_OT_link_append(wmOperatorType *ot)
void WM_recover_last_session(bContext *C, ReportList *reports)
{
char filename[FILE_MAX];
char filepath[FILE_MAX];
BLI_make_file_string("/", filename, BLI_temporary_dir(), BLENDER_QUIT_FILE);
BLI_make_file_string("/", filepath, BLI_temporary_dir(), BLENDER_QUIT_FILE);
/* if reports==NULL, it's called directly without operator, we add a quick check here */
if (reports || BLI_exists(filename)) {
if (reports || BLI_exists(filepath)) {
G.fileflags |= G_FILE_RECOVER;
/* XXX wm in context is not set correctly after WM_file_read -> crash */
/* do it before for now, but is this correct with multiple windows? */
WM_event_add_notifier(C, NC_WINDOW, NULL);
/* load file */
WM_file_autoexec_init(filename);
WM_file_read(C, filename, reports);
wm_file_read_opwrap(C, filepath, reports, true);
G.fileflags &= ~G_FILE_RECOVER;
@ -2545,23 +2558,23 @@ static void WM_OT_recover_last_session(wmOperatorType *ot)
static int wm_recover_auto_save_exec(bContext *C, wmOperator *op)
{
char path[FILE_MAX];
char filepath[FILE_MAX];
bool success;
RNA_string_get(op->ptr, "filepath", path);
RNA_string_get(op->ptr, "filepath", filepath);
G.fileflags |= G_FILE_RECOVER;
/* XXX wm in context is not set correctly after WM_file_read -> crash */
/* do it before for now, but is this correct with multiple windows? */
WM_event_add_notifier(C, NC_WINDOW, NULL);
/* load file */
WM_file_autoexec_init(path);
WM_file_read(C, path, op->reports);
success = wm_file_read_opwrap(C, filepath, op->reports, true);
G.fileflags &= ~G_FILE_RECOVER;
return OPERATOR_FINISHED;
if (success) {
return OPERATOR_FINISHED;
}
else {
return OPERATOR_CANCELLED;
}
}
static int wm_recover_auto_save_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))