diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index 3a67b9b95c..bd54209e36 100644 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -554,6 +554,7 @@ class CuraApplication(QtApplication): qmlRegisterType(cura.Settings.UserProfilesModel, "Cura", 1, 0, "UserProfilesModel") qmlRegisterType(cura.Settings.MaterialSettingsVisibilityHandler, "Cura", 1, 0, "MaterialSettingsVisibilityHandler") qmlRegisterType(cura.Settings.QualitySettingsModel, "Cura", 1, 0, "QualitySettingsModel") + qmlRegisterType(cura.Settings.MachineNameValidator, "Cura", 1, 0, "MachineNameValidator") qmlRegisterSingletonType(cura.Settings.ContainerManager, "Cura", 1, 0, "ContainerManager", cura.Settings.ContainerManager.createContainerManager) diff --git a/cura/Settings/MachineNameValidator.py b/cura/Settings/MachineNameValidator.py new file mode 100644 index 0000000000..c4f3d87582 --- /dev/null +++ b/cura/Settings/MachineNameValidator.py @@ -0,0 +1,33 @@ +# Copyright (c) 2016 Ultimaker B.V. +# Cura is released under the terms of the AGPLv3 or higher. + +import UM.Resources +import UM.Settings.ContainerRegistry +import UM.Settings.InstanceContainer + +from PyQt5.QtGui import QValidator +import os #For statvfs. +import urllib #To escape machine names for how they're saved to file. + +## Are machine names valid? +# +# Performs checks based on the length of the name. +class MachineNameValidator(QValidator): + ## Check if a specified machine name is allowed. + # + # \param name The machine name to check. + # \param position The current position of the cursor in the text box. + # \return ``QValidator.Invalid`` if it's disallowed, or + # ``QValidator.Acceptable`` if it's allowed. + def validate(self, name, position): + #Check for file name length of the current settings container (which is the longest file we're saving with the name). + try: + filename_max_length = os.statvfs(UM.Resources.getDataStoragePath()) + except AttributeError: #Doesn't support statvfs. Probably because it's not a Unix system. + filename_max_length = 255 #Assume it's Windows on NTFS. + escaped_name = urllib.parse.quote_plus(name) + current_settings_filename = escaped_name + "_current_settings." + UM.Settings.ContainerRegistry.getMimeTypeForContainer(UM.Settings.InstanceContainer).preferredSuffix + if current_settings_filename > filename_max_length: + return QValidator.Invalid + + return QValidator.Acceptable #All checks succeeded. \ No newline at end of file diff --git a/cura/Settings/__init__.py b/cura/Settings/__init__.py index a28073fa7f..0298ac886d 100644 --- a/cura/Settings/__init__.py +++ b/cura/Settings/__init__.py @@ -8,6 +8,7 @@ from .CuraContainerRegistry import CuraContainerRegistry from .ExtruderManager import ExtruderManager from .ExtrudersModel import ExtrudersModel from .MachineManager import MachineManager +from .MachineNameValidator import MachineNameValidator from .MaterialSettingsVisibilityHandler import MaterialSettingsVisibilityHandler from .SettingOverrideDecorator import SettingOverrideDecorator from .QualitySettingsModel import QualitySettingsModel