|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2024 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 |
|
7 """ |
|
8 Module implementing a dialog to enter the parameters for a connection to an eric-ide |
|
9 server. |
|
10 """ |
|
11 |
|
12 import ipaddress |
|
13 |
|
14 from PyQt6.QtCore import pyqtSlot |
|
15 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
16 |
|
17 from eric7 import Preferences |
|
18 |
|
19 from .Ui_EricServerConnectionDialog import Ui_EricServerConnectionDialog |
|
20 |
|
21 |
|
22 class EricServerConnectionDialog(QDialog, Ui_EricServerConnectionDialog): |
|
23 """ |
|
24 Class implementing a dialog to enter the parameters for a connection to an eric-ide |
|
25 server. |
|
26 """ |
|
27 |
|
28 def __init__(self, profileNames=None, parent=None): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param profileNames list of defined connection profile names (defaults to None) |
|
33 @type list of str (optional) |
|
34 @param parent reference to the parent widget (defaults to None) |
|
35 @type QWidget (optional) |
|
36 """ |
|
37 super().__init__(parent) |
|
38 self.setupUi(self) |
|
39 |
|
40 self.timeoutSpinBox.setToolTip( |
|
41 self.tr("Enter the timeout for the connection attempt (default: {0} s.") |
|
42 .format(Preferences.getEricServer("ConnectionTimeout")) |
|
43 ) |
|
44 |
|
45 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(False) |
|
46 |
|
47 if profileNames is None: |
|
48 self.nameLabel.setVisible(False) |
|
49 self.nameEdit.setVisible(False) |
|
50 self.nameEdit.setEnabled(False) |
|
51 |
|
52 self.__profileNames = profileNames[:] if bool(profileNames) else [] |
|
53 self.__originalName = "" |
|
54 |
|
55 self.nameEdit.textChanged.connect(self.__updateOK) |
|
56 self.hostnameEdit.textChanged.connect(self.__updateOK) |
|
57 |
|
58 msh = self.minimumSizeHint() |
|
59 self.resize(max(self.width(), msh.width()), msh.height()) |
|
60 |
|
61 @pyqtSlot(str) |
|
62 def on_hostnameEdit_textChanged(self, hostname): |
|
63 """ |
|
64 Private slot handling a change of the hostname. |
|
65 |
|
66 @param hostname text of the host name field |
|
67 @type str |
|
68 """ |
|
69 @pyqtSlot() |
|
70 def __updateOK(self): |
|
71 """ |
|
72 Private slot to update the enabled state of the OK button. |
|
73 """ |
|
74 hostname = self.hostnameEdit.text() |
|
75 |
|
76 if hostname and hostname[0] in "0123456789" and ":" not in hostname: |
|
77 # possibly an IPv4 address |
|
78 try: |
|
79 ipaddress.IPv4Address(hostname) |
|
80 valid = True |
|
81 except ipaddress.AddressValueError: |
|
82 # leading zeros are not allowed |
|
83 valid = False |
|
84 elif ":" in hostname: |
|
85 # possibly an IPv6 address |
|
86 try: |
|
87 ipaddress.IPv6Address(hostname) |
|
88 valid = True |
|
89 except ipaddress.AddressValueError: |
|
90 # leading zeros are not allowed |
|
91 valid = False |
|
92 elif ":" not in hostname: |
|
93 valid = bool(hostname) |
|
94 else: |
|
95 valid = False |
|
96 |
|
97 if self.nameEdit.isEnabled(): |
|
98 # connection profile mode |
|
99 name = self.nameEdit.text() |
|
100 valid &= name == self.__originalName or name not in self.__profileNames |
|
101 |
|
102 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(valid) |
|
103 |
|
104 def getData(self): |
|
105 """ |
|
106 Public method to get the entered data. |
|
107 |
|
108 @return tuple containing the entered host name or IP address, the port number |
|
109 and the timeout (in seconds) |
|
110 @rtype tuple of (str, int, int) |
|
111 """ |
|
112 port = self.portSpinBox.value() |
|
113 if port == self.portSpinBox.minimum(): |
|
114 port = None |
|
115 |
|
116 timeout = self.timeoutSpinBox.value() |
|
117 if timeout == self.timeoutSpinBox.minimum(): |
|
118 timeout = None |
|
119 |
|
120 return self.hostnameEdit.text(), port, timeout |
|
121 |
|
122 def getProfileData(self): |
|
123 """ |
|
124 Public method to get the entered data for connection profile mode. |
|
125 |
|
126 @return tuple containing the profile name, host name or IP address, |
|
127 the port number and the timeout (in seconds) |
|
128 @rtype tuple of (str, str, int, int) |
|
129 """ |
|
130 port = self.portSpinBox.value() |
|
131 if port == self.portSpinBox.minimum(): |
|
132 port = 0 |
|
133 |
|
134 timeout = self.timeoutSpinBox.value() |
|
135 if timeout == self.timeoutSpinBox.minimum(): |
|
136 timeout = 0 |
|
137 |
|
138 return self.nameEdit.text(), self.hostnameEdit.text(), port, timeout |
|
139 |
|
140 def setProfileData(self, name, hostname, port, timeout): |
|
141 """ |
|
142 Public method to set the connection profile data to be edited. |
|
143 |
|
144 @param name profile name |
|
145 @type str |
|
146 @param hostname host name or IP address |
|
147 @type str |
|
148 @param port port number |
|
149 @type int |
|
150 @param timeout timeout value in seconds |
|
151 @type int |
|
152 """ |
|
153 # adjust some values |
|
154 if not bool(port): |
|
155 port = self.portSpinBox.minimum() |
|
156 if not bool(timeout): |
|
157 timeout = self.timeoutSpinBox.minimum() |
|
158 |
|
159 self.__originalName = name |
|
160 |
|
161 self.nameEdit.setText(name) |
|
162 self.hostnameEdit.setText(hostname) |
|
163 self.portSpinBox.setValue(port) |
|
164 self.timeoutSpinBox.setValue(timeout) |