From 30d66fb8de37cf252a5eb8fa13f19621110f93b2 Mon Sep 17 00:00:00 2001 From: ChrisTerBeke Date: Mon, 14 May 2018 09:47:44 +0200 Subject: [PATCH] Copy preferences under Linux to add them to backup, notification messages --- cura/Backups/Backup.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/cura/Backups/Backup.py b/cura/Backups/Backup.py index 4c149b757a..375a1fa691 100644 --- a/cura/Backups/Backup.py +++ b/cura/Backups/Backup.py @@ -7,7 +7,11 @@ import shutil from typing import Optional from zipfile import ZipFile, ZIP_DEFLATED, BadZipfile +from UM import i18nCatalog from UM.Logger import Logger +from UM.Message import Message +from UM.Platform import Platform +from UM.Preferences import Preferences from UM.Resources import Resources from cura.CuraApplication import CuraApplication @@ -21,6 +25,9 @@ class Backup: # These files should be ignored when making a backup. IGNORED_FILES = {"cura.log", "cache"} + # Re-use translation catalog. + catalog = i18nCatalog("cura") + def __init__(self, zip_file: bytes = None, meta_data: dict = None): self.zip_file = zip_file # type: Optional[bytes] self.meta_data = meta_data # type: Optional[dict] @@ -31,10 +38,15 @@ class Backup: """ cura_release = CuraApplication.getInstance().getVersion() version_data_dir = Resources.getDataStoragePath() + preferences_file_name = CuraApplication.getInstance().getApplicationName() + preferences_file = Resources.getPath(Resources.Preferences, "{}.cfg".format(preferences_file_name)) Logger.log("d", "Creating backup for Cura %s, using folder %s", cura_release, version_data_dir) - - # TODO: support preferences file in backup under Linux (is in different directory). + + # We copy the preferences file to the user data directory in Linux as it's in a different location there. + # When restoring a backup on Linux, we copy it back. + if Platform.isLinux(): + shutil.copyfile(preferences_file, os.path.join(version_data_dir, "{}.cfg".format(preferences_file_name))) # Ensure all current settings are saved. CuraApplication.getInstance().saveSettings() @@ -86,9 +98,15 @@ class Backup: return archive except (IOError, OSError, BadZipfile) as error: Logger.log("e", "Could not create archive from user data directory: %s", error) - # TODO: show message. + self._showMessage( + self.catalog.i18nc("@info:backup_failed", + "Could not create archive from user data directory: {}".format(error))) return None + def _showMessage(self, message: str) -> None: + """Show a UI message""" + Message(message, title=self.catalog.i18nc("@info:title", "Backup"), lifetime=30).show() + def restore(self) -> bool: """ Restore this backups @@ -97,16 +115,23 @@ class Backup: if not self.zip_file or not self.meta_data or not self.meta_data.get("cura_release", None): # We can restore without the minimum required information. Logger.log("w", "Tried to restore a Cura backup without having proper data or meta data.") - # TODO: show message. + self._showMessage( + self.catalog.i18nc("@info:backup_failed", + "Tried to restore a Cura backup without having proper data or meta data.")) return False # TODO: handle restoring older data version. - # TODO: support preferences file in backup under Linux (is in different directory). - # global_data_dir = os.path.dirname(version_data_dir) version_data_dir = Resources.getDataStoragePath() archive = ZipFile(io.BytesIO(self.zip_file), "r") extracted = self._extractArchive(archive, version_data_dir) + + # Under Linux, preferences are stored elsewhere, so we copy the file to there. + preferences_file_name = CuraApplication.getInstance().getApplicationName() + preferences_file = Resources.getPath(Resources.Preferences, "{}.cfg".format(preferences_file_name)) + if Platform.isLinux(): + shutil.move(os.path.join(version_data_dir, "{}.cfg".format(preferences_file)), preferences_file) + return extracted @staticmethod