ContainerManager.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import os.path
  4. import urllib
  5. from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty, pyqtSignal, QUrl
  6. from PyQt5.QtWidgets import QMessageBox
  7. import UM.PluginRegistry
  8. import UM.Settings
  9. import UM.SaveFile
  10. import UM.Platform
  11. import UM.MimeTypeDatabase
  12. import UM.Logger
  13. from UM.MimeTypeDatabase import MimeTypeNotFoundError
  14. from UM.i18n import i18nCatalog
  15. catalog = i18nCatalog("cura")
  16. ## Manager class that contains common actions to deal with containers in Cura.
  17. #
  18. # This is primarily intended as a class to be able to perform certain actions
  19. # from within QML. We want to be able to trigger things like removing a container
  20. # when a certain action happens. This can be done through this class.
  21. class ContainerManager(QObject):
  22. def __init__(self, parent = None):
  23. super().__init__(parent)
  24. self._registry = UM.Settings.ContainerRegistry.getInstance()
  25. self._container_name_filters = {}
  26. ## Create a duplicate of the specified container
  27. #
  28. # This will create and add a duplicate of the container corresponding
  29. # to the container ID.
  30. #
  31. # \param container_id \type{str} The ID of the container to duplicate.
  32. #
  33. # \return The ID of the new container, or an empty string if duplication failed.
  34. @pyqtSlot(str, result = str)
  35. def duplicateContainer(self, container_id):
  36. containers = self._registry.findContainers(None, id = container_id)
  37. if not containers:
  38. UM.Logger.log("w", "Could duplicate container %s because it was not found.", container_id)
  39. return ""
  40. container = containers[0]
  41. new_container = None
  42. new_name = self._registry.uniqueName(container.getName())
  43. # Only InstanceContainer has a duplicate method at the moment.
  44. # So fall back to serialize/deserialize when no duplicate method exists.
  45. if hasattr(container, "duplicate"):
  46. new_container = container.duplicate(new_name)
  47. else:
  48. new_container = container.__class__(new_name)
  49. new_container.deserialize(container.serialize())
  50. new_container.setName(new_name)
  51. if new_container:
  52. self._registry.addContainer(new_container)
  53. return new_container.getId()
  54. ## Change the name of a specified container to a new name.
  55. #
  56. # \param container_id \type{str} The ID of the container to change the name of.
  57. # \param new_id \type{str} The new ID of the container.
  58. # \param new_name \type{str} The new name of the specified container.
  59. #
  60. # \return True if successful, False if not.
  61. @pyqtSlot(str, str, str, result = bool)
  62. def renameContainer(self, container_id, new_id, new_name):
  63. containers = self._registry.findContainers(None, id = container_id)
  64. if not containers:
  65. UM.Logger.log("w", "Could rename container %s because it was not found.", container_id)
  66. return False
  67. container = containers[0]
  68. # First, remove the container from the registry. This will clean up any files related to the container.
  69. self._registry.removeContainer(container)
  70. # Ensure we have a unique name for the container
  71. new_name = self._registry.uniqueName(new_name)
  72. # Then, update the name and ID of the container
  73. container.setName(new_name)
  74. container._id = new_id # TODO: Find a nicer way to set a new, unique ID
  75. # Finally, re-add the container so it will be properly serialized again.
  76. self._registry.addContainer(container)
  77. return True
  78. ## Remove the specified container.
  79. #
  80. # \param container_id \type{str} The ID of the container to remove.
  81. #
  82. # \return True if the container was successfully removed, False if not.
  83. @pyqtSlot(str, result = bool)
  84. def removeContainer(self, container_id):
  85. containers = self._registry.findContainers(None, id = container_id)
  86. if not containers:
  87. UM.Logger.log("w", "Could remove container %s because it was not found.", container_id)
  88. return False
  89. self._registry.removeContainer(containers[0].getId())
  90. return True
  91. ## Merge a container with another.
  92. #
  93. # This will try to merge one container into the other, by going through the container
  94. # and setting the right properties on the other container.
  95. #
  96. # \param merge_into_id \type{str} The ID of the container to merge into.
  97. # \param merge_id \type{str} The ID of the container to merge.
  98. #
  99. # \return True if successfully merged, False if not.
  100. @pyqtSlot(str, result = bool)
  101. def mergeContainers(self, merge_into_id, merge_id):
  102. containers = self._registry.findContainers(None, id = merge_into_id)
  103. if not containers:
  104. UM.Logger.log("w", "Could merge into container %s because it was not found.", merge_into_id)
  105. return False
  106. merge_into = containers[0]
  107. containers = self._registry.findContainers(None, id = merge_id)
  108. if not containers:
  109. UM.Logger.log("w", "Could not merge container %s because it was not found", merge_id)
  110. return False
  111. merge = containers[0]
  112. if type(merge) != type(merge_into):
  113. UM.Logger.log("w", "Cannot merge two containers of different types")
  114. return False
  115. for key in merge.getAllKeys():
  116. merge_into.setProperty(key, "value", merge.getProperty(key, "value"))
  117. return True
  118. ## Clear the contents of a container.
  119. #
  120. # \param container_id \type{str} The ID of the container to clear.
  121. #
  122. # \return True if successful, False if not.
  123. @pyqtSlot(str, result = bool)
  124. def clearContainer(self, container_id):
  125. containers = self._registry.findContainers(None, id = container_id)
  126. if not containers:
  127. UM.Logger.log("w", "Could clear container %s because it was not found.", container_id)
  128. return False
  129. if containers[0].isReadOnly():
  130. UM.Logger.log("w", "Cannot clear read-only container %s", container_id)
  131. return False
  132. containers[0].clear()
  133. return True
  134. ## Set a metadata entry of the specified container.
  135. #
  136. # This will set the specified entry of the container's metadata to the specified
  137. # value. Note that entries containing dictionaries can have their entries changed
  138. # by using "/" as a separator. For example, to change an entry "foo" in a
  139. # dictionary entry "bar", you can specify "bar/foo" as entry name.
  140. #
  141. # \param container_id \type{str} The ID of the container to change.
  142. # \param entry_name \type{str} The name of the metadata entry to change.
  143. # \param entry_value The new value of the entry.
  144. #
  145. # \return True if successful, False if not.
  146. @pyqtSlot(str, str, str, result = bool)
  147. def setContainerMetaDataEntry(self, container_id, entry_name, entry_value):
  148. containers = UM.Settings.ContainerRegistry.getInstance().findContainers(None, id = container_id)
  149. if not containers:
  150. UM.Logger.log("w", "Could set metadata of container %s because it was not found.", container_id)
  151. return False
  152. container = containers[0]
  153. if container.isReadOnly():
  154. UM.Logger.log("w", "Cannot set metadata of read-only container %s.", container_id)
  155. return False
  156. entries = entry_name.split("/")
  157. entry_name = entries.pop()
  158. if entries:
  159. root_name = entries.pop(0)
  160. root = container.getMetaDataEntry(root_name)
  161. item = root
  162. for entry in entries:
  163. item = item.get(entries.pop(0), { })
  164. item[entry_name] = entry_value
  165. entry_name = root_name
  166. entry_value = root
  167. container.setMetaDataEntry(entry_name, entry_value)
  168. return True
  169. ## Find instance containers matching certain criteria.
  170. #
  171. # This effectively forwards to ContainerRegistry::findInstanceContainers.
  172. #
  173. # \param criteria A dict of key - value pairs to search for.
  174. #
  175. # \return A list of container IDs that match the given criteria.
  176. @pyqtSlot("QVariantMap", result = "QVariantList")
  177. def findInstanceContainers(self, criteria):
  178. result = []
  179. for entry in self._registry.findInstanceContainers(**criteria):
  180. result.append(entry.getId())
  181. return result
  182. ## Get a list of string that can be used as name filters for a Qt File Dialog
  183. #
  184. # This will go through the list of available container types and generate a list of strings
  185. # out of that. The strings are formatted as "description (*.extension)" and can be directly
  186. # passed to a nameFilters property of a Qt File Dialog.
  187. #
  188. # \param type_name Which types of containers to list. These types correspond to the "type"
  189. # key of the plugin metadata.
  190. #
  191. # \return A string list with name filters.
  192. @pyqtSlot(str, result = "QStringList")
  193. def getContainerNameFilters(self, type_name):
  194. if not self._container_name_filters:
  195. self._updateContainerNameFilters()
  196. filters = []
  197. for filter_string, entry in self._container_name_filters.items():
  198. if not type_name or entry["type"] == type_name:
  199. filters.append(filter_string)
  200. return filters
  201. ## Export a container to a file
  202. #
  203. # \param container_id The ID of the container to export
  204. # \param file_type The type of file to save as. Should be in the form of "description (*.extension, *.ext)"
  205. # \param file_url The URL where to save the file.
  206. #
  207. # \return A dictionary containing a key "status" with a status code and a key "message" with a message
  208. # explaining the status.
  209. # The status code can be one of "error", "cancelled", "success"
  210. @pyqtSlot(str, str, QUrl, result = "QVariantMap")
  211. def exportContainer(self, container_id, file_type, file_url):
  212. if not container_id or not file_type or not file_url:
  213. return { "status": "error", "message": "Invalid arguments"}
  214. if isinstance(file_url, QUrl):
  215. file_url = file_url.toLocalFile()
  216. if not file_url:
  217. return { "status": "error", "message": "Invalid path"}
  218. mime_type = None
  219. if not file_type in self._container_name_filters:
  220. try:
  221. mime_type = UM.MimeTypeDatabase.getMimeTypeForFile(file_url)
  222. except MimeTypeNotFoundError:
  223. return { "status": "error", "message": "Unknown File Type" }
  224. else:
  225. mime_type = self._container_name_filters[file_type]["mime"]
  226. containers = UM.Settings.ContainerRegistry.getInstance().findContainers(None, id = container_id)
  227. if not containers:
  228. return { "status": "error", "message": "Container not found"}
  229. container = containers[0]
  230. for suffix in mime_type.suffixes:
  231. if file_url.endswith(suffix):
  232. break
  233. else:
  234. file_url += "." + mime_type.preferredSuffix
  235. if not UM.Platform.isWindows():
  236. if os.path.exists(file_url):
  237. result = QMessageBox.question(None, catalog.i18nc("@title:window", "File Already Exists"),
  238. catalog.i18nc("@label", "The file <filename>{0}</filename> already exists. Are you sure you want to overwrite it?").format(file_url))
  239. if result == QMessageBox.No:
  240. return { "status": "cancelled", "message": "User cancelled"}
  241. try:
  242. contents = container.serialize()
  243. except NotImplementedError:
  244. return { "status": "error", "message": "Unable to serialize container"}
  245. with UM.SaveFile(file_url, "w") as f:
  246. f.write(contents)
  247. return { "status": "success", "message": "Succesfully exported container"}
  248. ## Imports a profile from a file
  249. #
  250. # \param file_url A URL that points to the file to import.
  251. #
  252. # \return \type{Dict} dict with a 'status' key containing the string 'success' or 'error', and a 'message' key
  253. # containing a message for the user
  254. @pyqtSlot(QUrl, result = "QVariantMap")
  255. def importContainer(self, file_url):
  256. if not file_url:
  257. return { "status": "error", "message": "Invalid path"}
  258. if isinstance(file_url, QUrl):
  259. file_url = file_url.toLocalFile()
  260. if not file_url or not os.path.exists(file_url):
  261. return { "status": "error", "message": "Invalid path" }
  262. try:
  263. mime_type = UM.MimeTypeDatabase.getMimeTypeForFile(file_url)
  264. except MimeTypeNotFoundError:
  265. return { "status": "error", "message": "Could not determine mime type of file" }
  266. container_type = UM.Settings.ContainerRegistry.getContainerForMimeType(mime_type)
  267. if not container_type:
  268. return { "status": "error", "message": "Could not find a container to handle the specified file."}
  269. container_id = urllib.parse.unquote_plus(mime_type.stripExtension(os.path.basename(file_url)))
  270. container_id = UM.Settings.ContainerRegistry.getInstance().uniqueName(container_id)
  271. container = container_type(container_id)
  272. try:
  273. with open(file_url, "rt") as f:
  274. container.deserialize(f.read())
  275. except PermissionError:
  276. return { "status": "error", "message": "Permission denied when trying to read the file"}
  277. container.setName(container_id)
  278. UM.Settings.ContainerRegistry.getInstance().addContainer(container)
  279. return { "status": "success", "message": "Successfully imported container {0}".format(container.getName()) }
  280. def _updateContainerNameFilters(self):
  281. self._container_name_filters = {}
  282. for plugin_id, container_type in UM.Settings.ContainerRegistry.getContainerTypes():
  283. # Ignore default container types since those are not plugins
  284. if container_type in (UM.Settings.InstanceContainer, UM.Settings.ContainerStack, UM.Settings.DefinitionContainer):
  285. continue
  286. serialize_type = ""
  287. try:
  288. plugin_metadata = UM.PluginRegistry.getInstance().getMetaData(plugin_id)
  289. if plugin_metadata:
  290. serialize_type = plugin_metadata["settings_container"]["type"]
  291. else:
  292. continue
  293. except KeyError as e:
  294. continue
  295. mime_type = UM.Settings.ContainerRegistry.getMimeTypeForContainer(container_type)
  296. entry = {
  297. "type": serialize_type,
  298. "mime": mime_type,
  299. "container": container_type
  300. }
  301. suffix_list = "*." + mime_type.preferredSuffix
  302. for suffix in mime_type.suffixes:
  303. if suffix == mime_type.preferredSuffix:
  304. continue
  305. suffix_list += ", *." + suffix
  306. name_filter = "{0} ({1})".format(mime_type.comment, suffix_list)
  307. self._container_name_filters[name_filter] = entry
  308. # Factory function, used by QML
  309. @staticmethod
  310. def createContainerManager(engine, js_engine):
  311. return ContainerManager()