diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index cc80c6dbfe..a43af578a1 100755 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -117,7 +117,7 @@ from cura.UI.AddPrinterPagesModel import AddPrinterPagesModel from cura.UI.WelcomePagesModel import WelcomePagesModel from cura.UI.WhatsNewPagesModel import WhatsNewPagesModel -from cura.Utils.QtUtil import QtUtil +from cura.Utils.NetworkingUtil import NetworkingUtil from .SingleInstance import SingleInstance from .AutoSave import AutoSave @@ -1030,7 +1030,7 @@ class CuraApplication(QtApplication): qmlRegisterSingletonType(SimpleModeSettingsManager, "Cura", 1, 0, "SimpleModeSettingsManager", self.getSimpleModeSettingsManager) qmlRegisterSingletonType(MachineActionManager.MachineActionManager, "Cura", 1, 0, "MachineActionManager", self.getMachineActionManager) - qmlRegisterType(QtUtil, "Cura", 1, 0, "QtUtil") + qmlRegisterType(NetworkingUtil, "Cura", 1, 0, "NetworkingUtil") qmlRegisterType(WelcomePagesModel, "Cura", 1, 0, "WelcomePagesModel") qmlRegisterType(WhatsNewPagesModel, "Cura", 1, 0, "WhatsNewPagesModel") diff --git a/cura/Utils/NetworkingUtil.py b/cura/Utils/NetworkingUtil.py new file mode 100644 index 0000000000..b13f7903b9 --- /dev/null +++ b/cura/Utils/NetworkingUtil.py @@ -0,0 +1,44 @@ +# Copyright (c) 2019 Ultimaker B.V. +# Cura is released under the terms of the LGPLv3 or higher. + +import socket +from typing import Optional + +from PyQt5.QtCore import QObject, pyqtSlot + + +# +# This is a QObject because some of the functions can be used (and are useful) in QML. +# +class NetworkingUtil(QObject): + + def __init__(self, parent: Optional["QObject"] = None) -> None: + super().__init__(parent = parent) + + # Checks if the given string is a valid IPv4 address. + @pyqtSlot(str, result = bool) + def isIPv4(self, address: str) -> bool: + try: + socket.inet_pton(socket.AF_INET, address) + result = True + except: + result = False + return result + + # Checks if the given string is a valid IPv6 address. + @pyqtSlot(str, result = bool) + def isIPv6(self, address: str) -> bool: + try: + socket.inet_pton(socket.AF_INET6, address) + result = True + except: + result = False + return result + + # Checks if the given string is a valid IPv4 or IPv6 address. + @pyqtSlot(str, result = bool) + def isValidIP(self, address: str) -> bool: + return self.isIPv4(address) or self.isIPv6(address) + + +__all__ = ["NetworkingUtil"] diff --git a/cura/Utils/QtUtil.py b/cura/Utils/QtUtil.py deleted file mode 100644 index 60860fcd78..0000000000 --- a/cura/Utils/QtUtil.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright (c) 2019 Ultimaker B.V. -# Cura is released under the terms of the LGPLv3 or higher. - -from typing import Optional - -from PyQt5.QtCore import QObject, pyqtSlot - -from . import networking - - -# -# Exposes the util functions to QML using a QObject. -# -class QtUtil(QObject): - - def __init__(self, parent: Optional["QObject"] = None) -> None: - super().__init__(parent = parent) - - @pyqtSlot(str, result = bool) - def isValidIP(self, address: str) -> bool: - return networking.isValidIP(address) diff --git a/cura/Utils/networking.py b/cura/Utils/networking.py deleted file mode 100644 index 0456191cee..0000000000 --- a/cura/Utils/networking.py +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright (c) 2019 Ultimaker B.V. -# Cura is released under the terms of the LGPLv3 or higher. - -import socket - - -# Checks if the given string is a valid IPv4 address. -def isIPv4(address: str) -> bool: - try: - socket.inet_pton(socket.AF_INET, address) - result = True - except: - result = False - return result - - -# Checks if the given string is a valid IPv6 address. -def isIPv6(address: str) -> bool: - try: - socket.inet_pton(socket.AF_INET6, address) - result = True - except: - result = False - return result - - -# Checks if the given string is a valid IPv4 or IPv6 address. -def isValidIP(address: str) -> bool: - return isIPv4(address) or isIPv6(address) - - -__all__ = ["isIPv4", "isIPv6", "isValidIP"] diff --git a/resources/qml/WelcomePages/AddPrinterByIpContent.qml b/resources/qml/WelcomePages/AddPrinterByIpContent.qml index 565fa325cb..0862727d6e 100644 --- a/resources/qml/WelcomePages/AddPrinterByIpContent.qml +++ b/resources/qml/WelcomePages/AddPrinterByIpContent.qml @@ -27,7 +27,7 @@ Item property var isPrinterDiscovered: discoveredPrinter != null // For validating IP address - property var util: Cura.QtUtil{} + property var networkingUtil: Cura.NetworkingUtil {} // Make sure to cancel the current request when this page closes. onVisibleChanged: @@ -137,7 +137,7 @@ Item onClicked: { const address = hostnameField.text - if (!util.isValidIP(address)) + if (!networkingUtil.isValidIP(address)) { hostnameField.invalidInputDetected() return