1 | /* $Id: UIPortForwardingTable.h 104358 2024-04-18 05:33:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIPortForwardingTable class declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef FEQT_INCLUDED_SRC_widgets_UIPortForwardingTable_h
|
---|
29 | #define FEQT_INCLUDED_SRC_widgets_UIPortForwardingTable_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | /* Qt includes: */
|
---|
35 | #include <QString>
|
---|
36 | #include <QWidget>
|
---|
37 |
|
---|
38 | /* GUI includes: */
|
---|
39 | #include "UILibraryDefs.h"
|
---|
40 |
|
---|
41 | /* COM includes: */
|
---|
42 | #include "KNATProtocol.h"
|
---|
43 |
|
---|
44 | /* Forward declarations: */
|
---|
45 | class QAction;
|
---|
46 | class QHBoxLayout;
|
---|
47 | class QItemEditorFactory;
|
---|
48 | class QIDialogButtonBox;
|
---|
49 | class QITableView;
|
---|
50 | class UIPortForwardingModel;
|
---|
51 | class QIToolBar;
|
---|
52 |
|
---|
53 | /** QString subclass used to distinguish name data from simple QString. */
|
---|
54 | class NameData : public QString
|
---|
55 | {
|
---|
56 | public:
|
---|
57 |
|
---|
58 | /** Constructs null name data. */
|
---|
59 | NameData() : QString() {}
|
---|
60 | /** Constructs name data passing @a strName to the base-class. */
|
---|
61 | NameData(const QString &strName) : QString(strName) {}
|
---|
62 | };
|
---|
63 | Q_DECLARE_METATYPE(NameData);
|
---|
64 |
|
---|
65 |
|
---|
66 | /** QString subclass used to distinguish IP data from simple QString. */
|
---|
67 | class IpData : public QString
|
---|
68 | {
|
---|
69 | public:
|
---|
70 |
|
---|
71 | /** Constructs null IP data. */
|
---|
72 | IpData() : QString() {}
|
---|
73 | /** Constructs name data passing @a strIp to the base-class. */
|
---|
74 | IpData(const QString &strIp) : QString(strIp) {}
|
---|
75 | };
|
---|
76 | Q_DECLARE_METATYPE(IpData);
|
---|
77 |
|
---|
78 |
|
---|
79 | /** Wrapper for ushort used to distinguish port data from simple ushort. */
|
---|
80 | class PortData
|
---|
81 | {
|
---|
82 | public:
|
---|
83 |
|
---|
84 | /** Constructs null port data. */
|
---|
85 | PortData() : m_uValue(0) {}
|
---|
86 | /** Constructs port data based on @a uValue. */
|
---|
87 | PortData(ushort uValue) : m_uValue(uValue) {}
|
---|
88 |
|
---|
89 | /** Returns whether this port data is equal to @a another. */
|
---|
90 | bool operator==(const PortData &another) const { return m_uValue == another.m_uValue; }
|
---|
91 |
|
---|
92 | /** Returns serialized port data value. */
|
---|
93 | ushort value() const { return m_uValue; }
|
---|
94 |
|
---|
95 | private:
|
---|
96 |
|
---|
97 | /** Holds the port data value. */
|
---|
98 | ushort m_uValue;
|
---|
99 | };
|
---|
100 | Q_DECLARE_METATYPE(PortData);
|
---|
101 |
|
---|
102 |
|
---|
103 | /** Port Forwarding Rule structure. */
|
---|
104 | struct UIDataPortForwardingRule
|
---|
105 | {
|
---|
106 | /** Constructs data. */
|
---|
107 | UIDataPortForwardingRule()
|
---|
108 | : name(QString())
|
---|
109 | , protocol(KNATProtocol_UDP)
|
---|
110 | , hostIp(IpData())
|
---|
111 | , hostPort(PortData())
|
---|
112 | , guestIp(IpData())
|
---|
113 | , guestPort(PortData())
|
---|
114 | {}
|
---|
115 |
|
---|
116 | /** Constructs data on the basis of passed arguments.
|
---|
117 | * @param strName Brings the rule name.
|
---|
118 | * @param enmProtocol Brings the rule protocol.
|
---|
119 | * @param strHostIP Brings the rule host IP.
|
---|
120 | * @param uHostPort Brings the rule host port.
|
---|
121 | * @param strGuestIP Brings the rule guest IP.
|
---|
122 | * @param uGuestPort Brings the rule guest port. */
|
---|
123 | UIDataPortForwardingRule(const NameData &strName,
|
---|
124 | KNATProtocol enmProtocol,
|
---|
125 | const IpData &strHostIP,
|
---|
126 | PortData uHostPort,
|
---|
127 | const IpData &strGuestIP,
|
---|
128 | PortData uGuestPort)
|
---|
129 | : name(strName)
|
---|
130 | , protocol(enmProtocol)
|
---|
131 | , hostIp(strHostIP)
|
---|
132 | , hostPort(uHostPort)
|
---|
133 | , guestIp(strGuestIP)
|
---|
134 | , guestPort(uGuestPort)
|
---|
135 | {}
|
---|
136 |
|
---|
137 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
138 | bool equal(const UIDataPortForwardingRule &other) const
|
---|
139 | {
|
---|
140 | return true
|
---|
141 | && (name == other.name)
|
---|
142 | && (protocol == other.protocol)
|
---|
143 | && (hostIp == other.hostIp)
|
---|
144 | && (hostPort == other.hostPort)
|
---|
145 | && (guestIp == other.guestIp)
|
---|
146 | && (guestPort == other.guestPort)
|
---|
147 | ;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
151 | bool operator==(const UIDataPortForwardingRule &other) const { return equal(other); }
|
---|
152 | /** Returns whether the @a other passed data is different from this one. */
|
---|
153 | bool operator!=(const UIDataPortForwardingRule &other) const { return !equal(other); }
|
---|
154 |
|
---|
155 | /** Holds the rule name. */
|
---|
156 | NameData name;
|
---|
157 | /** Holds the rule protocol. */
|
---|
158 | KNATProtocol protocol;
|
---|
159 | /** Holds the rule host IP. */
|
---|
160 | IpData hostIp;
|
---|
161 | /** Holds the rule host port. */
|
---|
162 | PortData hostPort;
|
---|
163 | /** Holds the rule guest IP. */
|
---|
164 | IpData guestIp;
|
---|
165 | /** Holds the rule guest port. */
|
---|
166 | PortData guestPort;
|
---|
167 | };
|
---|
168 |
|
---|
169 | /** Port Forwarding Data list. */
|
---|
170 | typedef QList<UIDataPortForwardingRule> UIPortForwardingDataList;
|
---|
171 |
|
---|
172 |
|
---|
173 | /** Unique part of port forwarding data. */
|
---|
174 | struct UIPortForwardingDataUnique
|
---|
175 | {
|
---|
176 | /** Constructs unique port forwarding data based on
|
---|
177 | * @a enmProtocol, @a uHostPort and @a uHostPort. */
|
---|
178 | UIPortForwardingDataUnique(KNATProtocol enmProtocol,
|
---|
179 | PortData uHostPort,
|
---|
180 | const IpData &strHostIp)
|
---|
181 | : protocol(enmProtocol)
|
---|
182 | , hostPort(uHostPort)
|
---|
183 | , hostIp(strHostIp)
|
---|
184 | {}
|
---|
185 |
|
---|
186 | /** Returns whether this port data is equal to @a another. */
|
---|
187 | bool operator==(const UIPortForwardingDataUnique &another) const
|
---|
188 | {
|
---|
189 | return protocol == another.protocol
|
---|
190 | && hostPort == another.hostPort
|
---|
191 | && ( hostIp.isEmpty() || another.hostIp.isEmpty()
|
---|
192 | || hostIp == "0.0.0.0" || another.hostIp == "0.0.0.0"
|
---|
193 | || hostIp == another.hostIp);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** Holds the port forwarding data protocol type. */
|
---|
197 | KNATProtocol protocol;
|
---|
198 | /** Holds the port forwarding data host port. */
|
---|
199 | PortData hostPort;
|
---|
200 | /** Holds the port forwarding data host IP. */
|
---|
201 | IpData hostIp;
|
---|
202 | };
|
---|
203 |
|
---|
204 |
|
---|
205 | /** QWidget subclass representig Port Forwarding table. */
|
---|
206 | class SHARED_LIBRARY_STUFF UIPortForwardingTable : public QWidget
|
---|
207 | {
|
---|
208 | Q_OBJECT;
|
---|
209 |
|
---|
210 | signals:
|
---|
211 |
|
---|
212 | /** Notifies listeners about table data changed. */
|
---|
213 | void sigDataChanged();
|
---|
214 |
|
---|
215 | public:
|
---|
216 |
|
---|
217 | /** Constructs Port Forwarding table.
|
---|
218 | * @param rules Brings the current list of Port Forwarding rules.
|
---|
219 | * @param fIPv6 Brings whether this table contains IPv6 rules, not IPv4.
|
---|
220 | * @param fAllowEmptyGuestIPs Brings whether this table allows empty guest IPs. */
|
---|
221 | UIPortForwardingTable(const UIPortForwardingDataList &rules, bool fIPv6, bool fAllowEmptyGuestIPs);
|
---|
222 | /** Destructs Port Forwarding table. */
|
---|
223 | virtual ~UIPortForwardingTable() RT_OVERRIDE;
|
---|
224 | /** Returns the list of port forwarding rules. */
|
---|
225 | UIPortForwardingDataList rules() const;
|
---|
226 | /** Defines the list of port forwarding @a newRules.
|
---|
227 | * @param fHoldPosition Holds whether we should try to keep
|
---|
228 | * port forwarding rule position intact. */
|
---|
229 | void setRules(const UIPortForwardingDataList &newRules,
|
---|
230 | bool fHoldPosition = false);
|
---|
231 |
|
---|
232 | /** Defines guest address @a strHint. */
|
---|
233 | void setGuestAddressHint(const QString &strHint);
|
---|
234 |
|
---|
235 | /** Validates the table. */
|
---|
236 | bool validate() const;
|
---|
237 |
|
---|
238 | /** Returns whether the table data was changed. */
|
---|
239 | bool isChanged() const { return m_fTableDataChanged; }
|
---|
240 |
|
---|
241 | /** Makes sure current editor data committed. */
|
---|
242 | void makeSureEditorDataCommitted();
|
---|
243 |
|
---|
244 | protected:
|
---|
245 |
|
---|
246 | /** Preprocesses any Qt @a pEvent for passed @a pObject. */
|
---|
247 | virtual bool eventFilter(QObject *pObject, QEvent *pEvent) RT_OVERRIDE;
|
---|
248 |
|
---|
249 | private slots:
|
---|
250 |
|
---|
251 | /** Handles translation event. */
|
---|
252 | void sltRetranslateUI();
|
---|
253 | /** Adds the rule. */
|
---|
254 | void sltAddRule();
|
---|
255 | /** Copies the rule. */
|
---|
256 | void sltCopyRule();
|
---|
257 | /** Removes the rule. */
|
---|
258 | void sltRemoveRule();
|
---|
259 |
|
---|
260 | /** Marks table data as changed. */
|
---|
261 | void sltTableDataChanged();
|
---|
262 |
|
---|
263 | /** Handles current item change. */
|
---|
264 | void sltCurrentChanged();
|
---|
265 | /** Handles request to show context-menu in certain @a position. */
|
---|
266 | void sltShowTableContexMenu(const QPoint &position);
|
---|
267 | /** Adjusts table column sizes. */
|
---|
268 | void sltAdjustTable();
|
---|
269 |
|
---|
270 | private:
|
---|
271 |
|
---|
272 | /** Prepares all. */
|
---|
273 | void prepare();
|
---|
274 | /** Prepares layout. */
|
---|
275 | void prepareLayout();
|
---|
276 | /** Prepares table-view. */
|
---|
277 | void prepareTableView();
|
---|
278 | /** Prepares table-model. */
|
---|
279 | void prepareTableModel();
|
---|
280 | /** Prepares table-delegates. */
|
---|
281 | void prepareTableDelegates();
|
---|
282 | /** Prepares toolbar. */
|
---|
283 | void prepareToolbar();
|
---|
284 | /** Cleanups all. */
|
---|
285 | void cleanup();
|
---|
286 |
|
---|
287 | /** Holds the list of port forwarding rules. */
|
---|
288 | UIPortForwardingDataList m_rules;
|
---|
289 |
|
---|
290 | /** Holds the guest address hint. */
|
---|
291 | QString m_strGuestAddressHint;
|
---|
292 |
|
---|
293 | /** Holds whether this table contains IPv6 rules, not IPv4. */
|
---|
294 | bool m_fIPv6 : 1;
|
---|
295 | /** Holds whether this table allows empty guest IPs. */
|
---|
296 | bool m_fAllowEmptyGuestIPs : 1;
|
---|
297 | /** Holds whether this table data was changed. */
|
---|
298 | bool m_fTableDataChanged : 1;
|
---|
299 |
|
---|
300 | /** Holds the layout instance. */
|
---|
301 | QHBoxLayout *m_pLayout;
|
---|
302 | /** Holds the table-view instance. */
|
---|
303 | QITableView *m_pTableView;
|
---|
304 | /** Holds the tool-bar instance. */
|
---|
305 | QIToolBar *m_pToolBar;
|
---|
306 | /** Holds the item editor factory instance. */
|
---|
307 | QItemEditorFactory *m_pItemEditorFactory;
|
---|
308 |
|
---|
309 | /** Holds the table-model instance. */
|
---|
310 | UIPortForwardingModel *m_pTableModel;
|
---|
311 |
|
---|
312 | /** Holds the Add action instance. */
|
---|
313 | QAction *m_pActionAdd;
|
---|
314 | /** Holds the Copy action instance. */
|
---|
315 | QAction *m_pActionCopy;
|
---|
316 | /** Holds the Remove action instance. */
|
---|
317 | QAction *m_pActionRemove;
|
---|
318 | };
|
---|
319 |
|
---|
320 |
|
---|
321 | #endif /* !FEQT_INCLUDED_SRC_widgets_UIPortForwardingTable_h */
|
---|