Make a single NetworkingUtil

CURA-6483
This commit is contained in:
Lipu Fei 2019-04-30 09:43:48 +02:00
parent bb1442a04e
commit ed8127777c
5 changed files with 48 additions and 57 deletions

View file

@ -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")

View file

@ -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"]

View file

@ -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)

View file

@ -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"]

View file

@ -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