[Bf-blender-cvs] [374f6472a6] app-templates: Add support for saving user-local app-template preferences

Campbell Barton noreply at git.blender.org
Fri Mar 24 08:38:42 CET 2017


Commit: 374f6472a6d5347db6ba8bf988c61989914625a5
Author: Campbell Barton
Date:   Fri Mar 24 18:37:29 2017 +1100
Branches: app-templates
https://developer.blender.org/rB374f6472a6d5347db6ba8bf988c61989914625a5

Add support for saving user-local app-template preferences

This means even though the template can define theme, keymaps etc.
the user can override them.. reset back to template factory defaults
etc.

===================================================================

M	source/blender/windowmanager/intern/wm_files.c

===================================================================

diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c
index 5a9543bc1d..ccaabeb9dc 100644
--- a/source/blender/windowmanager/intern/wm_files.c
+++ b/source/blender/windowmanager/intern/wm_files.c
@@ -647,11 +647,15 @@ int wm_homefile_read(
         const char *filepath_startup_override, const char *app_template_override)
 {
 	ListBase wmbase;
+	bool success = false;
+
 	char filepath_startup[FILE_MAX];
 	char filepath_userdef[FILE_MAX];
+
+	/* When 'app_template' is set: '{config}/{app_template}' */
+	char app_template_factory[FILE_MAX];
 	/* When 'app_template' is set: '{scripts}/startup/bl_app_templates_system/{app_template}' */
-	char template_directory[FILE_MAX];
-	bool success = false;
+	char app_template_config[FILE_MAX];
 
 	/* Indicates whether user preferences were really load from memory.
 	 *
@@ -683,13 +687,14 @@ int wm_homefile_read(
 
 	filepath_startup[0] = '\0';
 	filepath_userdef[0] = '\0';
-	template_directory[0] = '\0';
+	app_template_factory[0] = '\0';
+	app_template_config[0] = '\0';
 
+	const char * const cfgdir = BKE_appdir_folder_id(BLENDER_USER_CONFIG, NULL);
 	if (!use_factory_settings) {
-		const char * const cfgdir = BKE_appdir_folder_id(BLENDER_USER_CONFIG, NULL);
 		if (cfgdir) {
-			BLI_make_file_string("/", filepath_startup, cfgdir, BLENDER_STARTUP_FILE);
-			BLI_make_file_string("/", filepath_userdef, cfgdir, BLENDER_USERPREF_FILE);
+			BLI_path_join(filepath_startup, sizeof(filepath_startup), cfgdir, BLENDER_STARTUP_FILE, NULL);
+			BLI_path_join(filepath_userdef, sizeof(filepath_startup), cfgdir, BLENDER_USERPREF_FILE, NULL);
 		}
 		else {
 			use_factory_settings = true;
@@ -726,22 +731,26 @@ int wm_homefile_read(
 	}
 
 	if (app_template != NULL) {
-		BKE_appdir_app_template_id_search(app_template, template_directory, sizeof(template_directory));
+		BKE_appdir_app_template_id_search(app_template, app_template_factory, sizeof(app_template_factory));
+		BLI_path_join(app_template_config, sizeof(app_template_config), cfgdir, app_template, NULL);
 	}
 
 	/* insert template name into startup file */
 	if (app_template != NULL) {
+		/* note that the path is being set even when 'use_factory_settings == true'
+		 * this is done so we can load a templates factory-settings */
 		if (!use_factory_settings) {
-			BLI_path_suffix(filepath_startup, sizeof(filepath_startup), app_template, "_");
+			BLI_path_join(filepath_startup, sizeof(filepath_startup), app_template_config, BLENDER_STARTUP_FILE, NULL);
+			if (BLI_access(filepath_startup, R_OK) != 0) {
+				filepath_startup[0] = '\0';
+			}
+		}
+		else {
+			filepath_startup[0] = '\0';
 		}
 
-		if (use_factory_settings || BLI_access(filepath_startup, R_OK) != 0) {
-			if (template_directory[0] != '\0') {
-				/* note that the path is being set even when 'use_factory_settings == true' */
-				BLI_join_dirfile(
-				        filepath_startup, sizeof(filepath_startup),
-				        template_directory, BLENDER_STARTUP_FILE);
-			}
+		if (filepath_startup[0] == '\0') {
+			BLI_path_join(filepath_startup, sizeof(filepath_startup), app_template_factory, BLENDER_STARTUP_FILE, NULL);
 		}
 	}
 
@@ -776,9 +785,20 @@ int wm_homefile_read(
 	}
 
 	/* load template preferences */
-	if (template_directory[0] != '\0') {
+	if (app_template_factory[0] != '\0') {
 		char temp_path[FILE_MAX];
-		BLI_join_dirfile(temp_path, sizeof(temp_path), template_directory, BLENDER_USERPREF_FILE);
+		temp_path[0] = '\0';
+		if (!use_factory_settings) {
+			BLI_path_join(temp_path, sizeof(temp_path), app_template_config, BLENDER_USERPREF_FILE, NULL);
+			if (BLI_access(temp_path, R_OK) != 0) {
+				temp_path[0] = '\0';
+			}
+		}
+
+		if (temp_path[0] == '\0') {
+			BLI_path_join(temp_path, sizeof(temp_path), app_template_factory, BLENDER_USERPREF_FILE, NULL);
+		}
+
 		UserDef *userdef_template = NULL;
 		/* just avoids missing file warning */
 		if (BLI_exists(temp_path)) {
@@ -1337,6 +1357,13 @@ static int wm_homefile_write_exec(bContext *C, wmOperator *op)
 	char filepath[FILE_MAX];
 	int fileflags;
 
+	const char *app_template = U.app_template[0] ? U.app_template : NULL;
+	const char * const cfgdir = BKE_appdir_folder_id_create(BLENDER_USER_CONFIG, app_template);
+	if (cfgdir == NULL) {
+		BKE_report(op->reports, RPT_ERROR, "Unable to create user config path");
+		return OPERATOR_CANCELLED;
+	}
+
 	BLI_callback_exec(G.main, NULL, BLI_CB_EVT_SAVE_PRE);
 
 	/* check current window and close it if temp */
@@ -1346,10 +1373,7 @@ static int wm_homefile_write_exec(bContext *C, wmOperator *op)
 	/* update keymaps in user preferences */
 	WM_keyconfig_update(wm);
 
-	BLI_make_file_string("/", filepath, BKE_appdir_folder_id_create(BLENDER_USER_CONFIG, NULL), BLENDER_STARTUP_FILE);
-	if (U.app_template[0]) {
-		BLI_path_suffix(filepath, sizeof(filepath), U.app_template, "_");
-	}
+	BLI_path_join(filepath, sizeof(filepath), cfgdir, BLENDER_STARTUP_FILE, NULL);
 
 	printf("trying to save homefile at %s ", filepath);
 
@@ -1428,21 +1452,44 @@ static int wm_userpref_write_exec(bContext *C, wmOperator *op)
 {
 	wmWindowManager *wm = CTX_wm_manager(C);
 	char filepath[FILE_MAX];
+	const char *cfgdir;
+	bool ok = false;
 
 	/* update keymaps in user preferences */
 	WM_keyconfig_update(wm);
 
-	BLI_make_file_string("/", filepath, BKE_appdir_folder_id_create(BLENDER_USER_CONFIG, NULL), BLENDER_USERPREF_FILE);
-	printf("trying to save userpref at %s ", filepath);
-
-	if (BKE_blendfile_userdef_write(filepath, op->reports) == 0) {
-		printf("fail\n");
-		return OPERATOR_CANCELLED;
+	if ((cfgdir = BKE_appdir_folder_id_create(BLENDER_USER_CONFIG, NULL))) {
+		BLI_path_join(filepath, sizeof(filepath), cfgdir, BLENDER_USERPREF_FILE, NULL);
+		printf("trying to save userpref at %s ", filepath);
+		if (BKE_blendfile_userdef_write(filepath, op->reports) != 0) {
+			printf("ok\n");
+			ok = true;
+		}
+		else {
+			printf("fail\n");
+		}
+	}
+	else {
+		BKE_report(op->reports, RPT_ERROR, "Unable to create userpref path");
 	}
 
-	printf("ok\n");
+	if (U.app_template[0] && (cfgdir = BKE_appdir_folder_id_create(BLENDER_USER_CONFIG, U.app_template))) {
+		/* Also save app-template prefs */
+		BLI_path_join(filepath, sizeof(filepath), cfgdir, BLENDER_USERPREF_FILE, NULL);
+		printf("trying to save app-template userpref at %s ", filepath);
+		if (BKE_blendfile_userdef_write(filepath, op->reports) == 0) {
+			printf("fail\n");
+			ok = true;
+		}
+		else {
+			printf("ok\n");
+		}
+	}
+	else if (U.app_template[0]) {
+		BKE_report(op->reports, RPT_ERROR, "Unable to create app-template userpref path");
+	}
 
-	return OPERATOR_FINISHED;
+	return ok ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
 }
 
 void WM_OT_save_userpref(wmOperatorType *ot)




More information about the Bf-blender-cvs mailing list