VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/settings/machine/UIMachineSettingsNetwork.cpp@ 52727

Last change on this file since 52727 was 52727, checked in by vboxsync, 11 years ago

FE/Qt: file header cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 51.5 KB
Line 
1/* $Id: UIMachineSettingsNetwork.cpp 52727 2014-09-12 14:09:27Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIMachineSettingsNetwork class implementation.
4 */
5
6/*
7 * Copyright (C) 2008-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/* GUI includes: */
19#include "QIWidgetValidator.h"
20#include "QIArrowButtonSwitch.h"
21#include "UIMachineSettingsNetwork.h"
22#include "QITabWidget.h"
23#include "VBoxGlobal.h"
24#include "UIConverter.h"
25#include "UIIconPool.h"
26
27/* COM includes: */
28#include "CNetworkAdapter.h"
29#include "CNATEngine.h"
30#include "CHostNetworkInterface.h"
31#include "CNATNetwork.h"
32
33/* Other VBox includes: */
34#ifdef VBOX_WITH_VDE
35# include <iprt/ldr.h>
36# include <VBox/VDEPlug.h>
37#endif /* VBOX_WITH_VDE */
38
39/* Empty item extra-code: */
40const char *pEmptyItemCode = "#empty#";
41
42QString wipedOutString(const QString &strInputString)
43{
44 return strInputString.isEmpty() ? QString() : strInputString;
45}
46
47UIMachineSettingsNetwork::UIMachineSettingsNetwork(UIMachineSettingsNetworkPage *pParent)
48 : QIWithRetranslateUI<QWidget>(0)
49 , m_pParent(pParent)
50 , m_iSlot(-1)
51{
52 /* Apply UI decorations: */
53 Ui::UIMachineSettingsNetwork::setupUi(this);
54
55 /* Setup widgets: */
56 m_pAdapterNameCombo->setInsertPolicy(QComboBox::NoInsert);
57 m_pMACEditor->setValidator(new QRegExpValidator(QRegExp("[0-9A-Fa-f]{12}"), this));
58 m_pMACEditor->setMinimumWidthByText(QString().fill('0', 12));
59 m_pMACButton->setIcon(UIIconPool::iconSet(":/refresh_16px.png"));
60 m_pAdvancedArrow->setIconSize(QSize(10, 10));
61 m_pAdvancedArrow->setIconForButtonState(QIArrowButtonSwitch::ButtonState_Collapsed,
62 UIIconPool::iconSet(":/arrow_right_10px.png"));
63 m_pAdvancedArrow->setIconForButtonState(QIArrowButtonSwitch::ButtonState_Expanded,
64 UIIconPool::iconSet(":/arrow_down_10px.png"));
65
66 /* Setup connections: */
67 connect(m_pEnableAdapterCheckBox, SIGNAL(toggled(bool)), this, SLOT(sltHandleAdapterActivityChange()));
68 connect(m_pAttachmentTypeComboBox, SIGNAL(activated(int)), this, SLOT(sltHandleAttachmentTypeChange()));
69 connect(m_pAdapterNameCombo, SIGNAL(activated(int)), this, SLOT(sltHandleAlternativeNameChange()));
70 connect(m_pAdapterNameCombo, SIGNAL(editTextChanged(const QString&)), this, SLOT(sltHandleAlternativeNameChange()));
71 connect(m_pAdvancedArrow, SIGNAL(sigClicked()), this, SLOT(sltHandleAdvancedButtonStateChange()));
72 connect(m_pMACButton, SIGNAL(clicked()), this, SLOT(sltGenerateMac()));
73 connect(m_pPortForwardingButton, SIGNAL(clicked()), this, SLOT(sltOpenPortForwardingDlg()));
74 connect(this, SIGNAL(sigTabUpdated()), m_pParent, SLOT(sltHandleUpdatedTab()));
75
76 /* Prepare validation: */
77 prepareValidation();
78
79 /* Applying language settings: */
80 retranslateUi();
81}
82
83void UIMachineSettingsNetwork::fetchAdapterCache(const UICacheSettingsMachineNetworkAdapter &adapterCache)
84{
85 /* Get adapter data: */
86 const UIDataSettingsMachineNetworkAdapter &adapterData = adapterCache.base();
87
88 /* Load slot number: */
89 m_iSlot = adapterData.m_iSlot;
90
91 /* Load adapter activity state: */
92 m_pEnableAdapterCheckBox->setChecked(adapterData.m_fAdapterEnabled);
93 /* Handle adapter activity change: */
94 sltHandleAdapterActivityChange();
95
96 /* Load attachment type: */
97 m_pAttachmentTypeComboBox->setCurrentIndex(position(m_pAttachmentTypeComboBox, adapterData.m_attachmentType));
98 /* Load alternative name: */
99 m_strBridgedAdapterName = wipedOutString(adapterData.m_strBridgedAdapterName);
100 m_strInternalNetworkName = wipedOutString(adapterData.m_strInternalNetworkName);
101 m_strHostInterfaceName = wipedOutString(adapterData.m_strHostInterfaceName);
102 m_strGenericDriverName = wipedOutString(adapterData.m_strGenericDriverName);
103 m_strNATNetworkName = wipedOutString(adapterData.m_strNATNetworkName);
104 /* Handle attachment type change: */
105 sltHandleAttachmentTypeChange();
106
107 /* Load adapter type: */
108 m_pAdapterTypeCombo->setCurrentIndex(position(m_pAdapterTypeCombo, adapterData.m_adapterType));
109
110 /* Load promiscuous mode type: */
111 m_pPromiscuousModeCombo->setCurrentIndex(position(m_pPromiscuousModeCombo, adapterData.m_promiscuousMode));
112
113 /* Other options: */
114 m_pMACEditor->setText(adapterData.m_strMACAddress);
115 m_pGenericPropertiesTextEdit->setText(adapterData.m_strGenericProperties);
116 m_pCableConnectedCheckBox->setChecked(adapterData.m_fCableConnected);
117
118 /* Load port forwarding rules: */
119 m_portForwardingRules = adapterData.m_redirects;
120}
121
122void UIMachineSettingsNetwork::uploadAdapterCache(UICacheSettingsMachineNetworkAdapter &adapterCache)
123{
124 /* Prepare adapter data: */
125 UIDataSettingsMachineNetworkAdapter adapterData = adapterCache.base();
126
127 /* Save adapter activity state: */
128 adapterData.m_fAdapterEnabled = m_pEnableAdapterCheckBox->isChecked();
129
130 /* Save attachment type & alternative name: */
131 adapterData.m_attachmentType = attachmentType();
132 switch (adapterData.m_attachmentType)
133 {
134 case KNetworkAttachmentType_Null:
135 break;
136 case KNetworkAttachmentType_NAT:
137 break;
138 case KNetworkAttachmentType_Bridged:
139 adapterData.m_strBridgedAdapterName = alternativeName();
140 break;
141 case KNetworkAttachmentType_Internal:
142 adapterData.m_strInternalNetworkName = alternativeName();
143 break;
144 case KNetworkAttachmentType_HostOnly:
145 adapterData.m_strHostInterfaceName = alternativeName();
146 break;
147 case KNetworkAttachmentType_Generic:
148 adapterData.m_strGenericDriverName = alternativeName();
149 adapterData.m_strGenericProperties = m_pGenericPropertiesTextEdit->toPlainText();
150 break;
151 case KNetworkAttachmentType_NATNetwork:
152 adapterData.m_strNATNetworkName = alternativeName();
153 break;
154 default:
155 break;
156 }
157
158 /* Save adapter type: */
159 adapterData.m_adapterType = (KNetworkAdapterType)m_pAdapterTypeCombo->itemData(m_pAdapterTypeCombo->currentIndex()).toInt();
160
161 /* Save promiscuous mode type: */
162 adapterData.m_promiscuousMode = (KNetworkAdapterPromiscModePolicy)m_pPromiscuousModeCombo->itemData(m_pPromiscuousModeCombo->currentIndex()).toInt();
163
164 /* Other options: */
165 adapterData.m_strMACAddress = m_pMACEditor->text().isEmpty() ? QString() : m_pMACEditor->text();
166 adapterData.m_fCableConnected = m_pCableConnectedCheckBox->isChecked();
167
168 /* Save port forwarding rules: */
169 adapterData.m_redirects = m_portForwardingRules;
170
171 /* Cache adapter data: */
172 adapterCache.cacheCurrentData(adapterData);
173}
174
175bool UIMachineSettingsNetwork::validate(QList<UIValidationMessage> &messages)
176{
177 /* Pass if adapter is disabled: */
178 if (!m_pEnableAdapterCheckBox->isChecked())
179 return true;
180
181 /* Pass by default: */
182 bool fPass = true;
183
184 /* Prepare message: */
185 UIValidationMessage message;
186 message.first = vboxGlobal().removeAccelMark(tabTitle());
187
188 /* Validate alternatives: */
189 switch (attachmentType())
190 {
191 case KNetworkAttachmentType_Bridged:
192 {
193 if (alternativeName().isNull())
194 {
195 message.second << tr("No bridged network adapter is currently selected.");
196 fPass = false;
197 }
198 break;
199 }
200 case KNetworkAttachmentType_Internal:
201 {
202 if (alternativeName().isNull())
203 {
204 message.second << tr("No internal network name is currently specified.");
205 fPass = false;
206 }
207 break;
208 }
209 case KNetworkAttachmentType_HostOnly:
210 {
211 if (alternativeName().isNull())
212 {
213 message.second << tr("No host-only network adapter is currently selected.");
214 fPass = false;
215 }
216 break;
217 }
218 case KNetworkAttachmentType_Generic:
219 {
220 if (alternativeName().isNull())
221 {
222 message.second << tr("No generic driver is currently selected.");
223 fPass = false;
224 }
225 break;
226 }
227 case KNetworkAttachmentType_NATNetwork:
228 {
229 if (alternativeName().isNull())
230 {
231 message.second << tr("No NAT network name is currently specified.");
232 fPass = false;
233 }
234 break;
235 }
236 default:
237 break;
238 }
239
240 /* Validate MAC-address length: */
241 if (m_pMACEditor->text().size() < 12)
242 {
243 message.second << tr("The MAC address must be 12 hexadecimal digits long.");
244 fPass = false;
245 }
246
247 /* Make sure MAC-address is unicast: */
248 if (m_pMACEditor->text().size() >= 2)
249 {
250 QRegExp validator("^[0-9A-Fa-f][02468ACEace]");
251 if (validator.indexIn(m_pMACEditor->text()) != 0)
252 {
253 message.second << tr("The second digit in the MAC address may not be odd as only unicast addresses are allowed.");
254 fPass = false;
255 }
256 }
257
258 /* Serialize message: */
259 if (!message.second.isEmpty())
260 messages << message;
261
262 /* Return result: */
263 return fPass;
264}
265
266QWidget* UIMachineSettingsNetwork::setOrderAfter(QWidget *pAfter)
267{
268 setTabOrder(pAfter, m_pEnableAdapterCheckBox);
269 setTabOrder(m_pEnableAdapterCheckBox, m_pAttachmentTypeComboBox);
270 setTabOrder(m_pAttachmentTypeComboBox, m_pAdapterNameCombo);
271 setTabOrder(m_pAdapterNameCombo, m_pAdvancedArrow);
272 setTabOrder(m_pAdvancedArrow, m_pAdapterTypeCombo);
273 setTabOrder(m_pAdapterTypeCombo, m_pPromiscuousModeCombo);
274 setTabOrder(m_pPromiscuousModeCombo, m_pMACEditor);
275 setTabOrder(m_pMACEditor, m_pMACButton);
276 setTabOrder(m_pMACButton, m_pGenericPropertiesTextEdit);
277 setTabOrder(m_pGenericPropertiesTextEdit, m_pCableConnectedCheckBox);
278 setTabOrder(m_pCableConnectedCheckBox, m_pPortForwardingButton);
279 return m_pPortForwardingButton;
280}
281
282QString UIMachineSettingsNetwork::tabTitle() const
283{
284 return VBoxGlobal::tr("Adapter %1").arg(QString("&%1").arg(m_iSlot + 1));
285}
286
287KNetworkAttachmentType UIMachineSettingsNetwork::attachmentType() const
288{
289 return (KNetworkAttachmentType)m_pAttachmentTypeComboBox->itemData(m_pAttachmentTypeComboBox->currentIndex()).toInt();
290}
291
292QString UIMachineSettingsNetwork::alternativeName(int iType) const
293{
294 if (iType == -1)
295 iType = attachmentType();
296 QString strResult;
297 switch (iType)
298 {
299 case KNetworkAttachmentType_Bridged:
300 strResult = m_strBridgedAdapterName;
301 break;
302 case KNetworkAttachmentType_Internal:
303 strResult = m_strInternalNetworkName;
304 break;
305 case KNetworkAttachmentType_HostOnly:
306 strResult = m_strHostInterfaceName;
307 break;
308 case KNetworkAttachmentType_Generic:
309 strResult = m_strGenericDriverName;
310 break;
311 case KNetworkAttachmentType_NATNetwork:
312 strResult = m_strNATNetworkName;
313 break;
314 default:
315 break;
316 }
317 Assert(strResult.isNull() || !strResult.isEmpty());
318 return strResult;
319}
320
321void UIMachineSettingsNetwork::polishTab()
322{
323 /* Basic attributes: */
324 m_pEnableAdapterCheckBox->setEnabled(m_pParent->isMachineOffline());
325 m_pAttachmentTypeLabel->setEnabled(m_pParent->isMachineInValidMode());
326 m_pAttachmentTypeComboBox->setEnabled(m_pParent->isMachineInValidMode());
327 m_pAdapterNameLabel->setEnabled(m_pParent->isMachineInValidMode() &&
328 attachmentType() != KNetworkAttachmentType_Null &&
329 attachmentType() != KNetworkAttachmentType_NAT);
330 m_pAdapterNameCombo->setEnabled(m_pParent->isMachineInValidMode() &&
331 attachmentType() != KNetworkAttachmentType_Null &&
332 attachmentType() != KNetworkAttachmentType_NAT);
333 m_pAdvancedArrow->setEnabled(m_pParent->isMachineInValidMode());
334
335 /* Advanced attributes: */
336 m_pAdapterTypeLabel->setEnabled(m_pParent->isMachineOffline());
337 m_pAdapterTypeCombo->setEnabled(m_pParent->isMachineOffline());
338 m_pPromiscuousModeLabel->setEnabled(m_pParent->isMachineInValidMode() &&
339 attachmentType() != KNetworkAttachmentType_Null &&
340 attachmentType() != KNetworkAttachmentType_Generic &&
341 attachmentType() != KNetworkAttachmentType_NAT);
342 m_pPromiscuousModeCombo->setEnabled(m_pParent->isMachineInValidMode() &&
343 attachmentType() != KNetworkAttachmentType_Null &&
344 attachmentType() != KNetworkAttachmentType_Generic &&
345 attachmentType() != KNetworkAttachmentType_NAT);
346 m_pMACLabel->setEnabled(m_pParent->isMachineOffline());
347 m_pMACEditor->setEnabled(m_pParent->isMachineOffline());
348 m_pMACButton->setEnabled(m_pParent->isMachineOffline());
349 m_pGenericPropertiesLabel->setEnabled(m_pParent->isMachineInValidMode());
350 m_pGenericPropertiesTextEdit->setEnabled(m_pParent->isMachineInValidMode());
351 m_pPortForwardingButton->setEnabled(m_pParent->isMachineInValidMode() &&
352 attachmentType() == KNetworkAttachmentType_NAT);
353
354 /* Postprocessing: */
355 if ((m_pParent->isMachineSaved() || m_pParent->isMachineOnline()) && !m_pAdvancedArrow->isExpanded())
356 m_pAdvancedArrow->animateClick();
357 sltHandleAdvancedButtonStateChange();
358}
359
360void UIMachineSettingsNetwork::reloadAlternative()
361{
362 /* Repopulate alternative-name combo-box content: */
363 updateAlternativeList();
364 /* Select previous or default alternative-name combo-box item: */
365 updateAlternativeName();
366}
367
368void UIMachineSettingsNetwork::retranslateUi()
369{
370 /* Translate uic generated strings: */
371 Ui::UIMachineSettingsNetwork::retranslateUi(this);
372
373 /* Translate combo-boxes content: */
374 populateComboboxes();
375
376 /* Translate attachment info: */
377 sltHandleAttachmentTypeChange();
378}
379
380void UIMachineSettingsNetwork::sltHandleAdapterActivityChange()
381{
382 /* Update availability: */
383 m_pAdapterOptionsContainer->setEnabled(m_pEnableAdapterCheckBox->isChecked());
384
385 /* Revalidate: */
386 m_pParent->revalidate();
387}
388
389void UIMachineSettingsNetwork::sltHandleAttachmentTypeChange()
390{
391 /* Update alternative-name combo-box availability: */
392 m_pAdapterNameLabel->setEnabled(attachmentType() != KNetworkAttachmentType_Null &&
393 attachmentType() != KNetworkAttachmentType_NAT);
394 m_pAdapterNameCombo->setEnabled(attachmentType() != KNetworkAttachmentType_Null &&
395 attachmentType() != KNetworkAttachmentType_NAT);
396 /* Update promiscuous-mode combo-box availability: */
397 m_pPromiscuousModeLabel->setEnabled(attachmentType() != KNetworkAttachmentType_Null &&
398 attachmentType() != KNetworkAttachmentType_Generic &&
399 attachmentType() != KNetworkAttachmentType_NAT);
400 m_pPromiscuousModeCombo->setEnabled(attachmentType() != KNetworkAttachmentType_Null &&
401 attachmentType() != KNetworkAttachmentType_Generic &&
402 attachmentType() != KNetworkAttachmentType_NAT);
403 /* Update generic-properties editor visibility: */
404 m_pGenericPropertiesLabel->setVisible(attachmentType() == KNetworkAttachmentType_Generic &&
405 m_pAdvancedArrow->isExpanded());
406 m_pGenericPropertiesTextEdit->setVisible(attachmentType() == KNetworkAttachmentType_Generic &&
407 m_pAdvancedArrow->isExpanded());
408 /* Update forwarding-rules button availability: */
409 m_pPortForwardingButton->setEnabled(attachmentType() == KNetworkAttachmentType_NAT);
410 /* Update alternative-name combo-box whats-this and editable state: */
411 switch (attachmentType())
412 {
413 case KNetworkAttachmentType_Bridged:
414 {
415 m_pAdapterNameCombo->setWhatsThis(tr("Selects the network adapter on the host system that traffic "
416 "to and from this network card will go through."));
417 m_pAdapterNameCombo->setEditable(false);
418 break;
419 }
420 case KNetworkAttachmentType_Internal:
421 {
422 m_pAdapterNameCombo->setWhatsThis(tr("Holds the name of the internal network that this network card "
423 "will be connected to. You can create a new internal network by "
424 "choosing a name which is not used by any other network cards "
425 "in this virtual machine or others."));
426 m_pAdapterNameCombo->setEditable(true);
427 break;
428 }
429 case KNetworkAttachmentType_HostOnly:
430 {
431 m_pAdapterNameCombo->setWhatsThis(tr("Selects the virtual network adapter on the host system that traffic "
432 "to and from this network card will go through. "
433 "You can create and remove adapters using the global network "
434 "settings in the virtual machine manager window."));
435 m_pAdapterNameCombo->setEditable(false);
436 break;
437 }
438 case KNetworkAttachmentType_Generic:
439 {
440 m_pAdapterNameCombo->setWhatsThis(tr("Selects the driver to be used with this network card."));
441 m_pAdapterNameCombo->setEditable(true);
442 break;
443 }
444 case KNetworkAttachmentType_NATNetwork:
445 {
446 m_pAdapterNameCombo->setWhatsThis(tr("Holds the name of the NAT network that this network card "
447 "will be connected to. You can create and remove networks "
448 "using the global network settings in the virtual machine "
449 "manager window."));
450 m_pAdapterNameCombo->setEditable(false);
451 break;
452 }
453 default:
454 {
455 m_pAdapterNameCombo->setWhatsThis(QString());
456 break;
457 }
458 }
459
460 /* Update alternative combo: */
461 reloadAlternative();
462
463 /* Handle alternative-name cange: */
464 sltHandleAlternativeNameChange();
465}
466
467void UIMachineSettingsNetwork::sltHandleAlternativeNameChange()
468{
469 /* Remember new name if its changed,
470 * Call for other pages update, if necessary: */
471 switch (attachmentType())
472 {
473 case KNetworkAttachmentType_Bridged:
474 {
475 QString newName(m_pAdapterNameCombo->itemData(m_pAdapterNameCombo->currentIndex()).toString() == QString(pEmptyItemCode) ||
476 m_pAdapterNameCombo->currentText().isEmpty() ? QString() : m_pAdapterNameCombo->currentText());
477 if (m_strBridgedAdapterName != newName)
478 m_strBridgedAdapterName = newName;
479 break;
480 }
481 case KNetworkAttachmentType_Internal:
482 {
483 QString newName((m_pAdapterNameCombo->itemData(m_pAdapterNameCombo->currentIndex()).toString() == QString(pEmptyItemCode) &&
484 m_pAdapterNameCombo->currentText() == m_pAdapterNameCombo->itemText(m_pAdapterNameCombo->currentIndex())) ||
485 m_pAdapterNameCombo->currentText().isEmpty() ? QString() : m_pAdapterNameCombo->currentText());
486 if (m_strInternalNetworkName != newName)
487 {
488 m_strInternalNetworkName = newName;
489 if (!m_strInternalNetworkName.isNull())
490 emit sigTabUpdated();
491 }
492 break;
493 }
494 case KNetworkAttachmentType_HostOnly:
495 {
496 QString newName(m_pAdapterNameCombo->itemData(m_pAdapterNameCombo->currentIndex()).toString() == QString(pEmptyItemCode) ||
497 m_pAdapterNameCombo->currentText().isEmpty() ? QString() : m_pAdapterNameCombo->currentText());
498 if (m_strHostInterfaceName != newName)
499 m_strHostInterfaceName = newName;
500 break;
501 }
502 case KNetworkAttachmentType_Generic:
503 {
504 QString newName((m_pAdapterNameCombo->itemData(m_pAdapterNameCombo->currentIndex()).toString() == QString(pEmptyItemCode) &&
505 m_pAdapterNameCombo->currentText() == m_pAdapterNameCombo->itemText(m_pAdapterNameCombo->currentIndex())) ||
506 m_pAdapterNameCombo->currentText().isEmpty() ? QString() : m_pAdapterNameCombo->currentText());
507 if (m_strGenericDriverName != newName)
508 {
509 m_strGenericDriverName = newName;
510 if (!m_strGenericDriverName.isNull())
511 emit sigTabUpdated();
512 }
513 break;
514 }
515 case KNetworkAttachmentType_NATNetwork:
516 {
517 QString newName(m_pAdapterNameCombo->itemData(m_pAdapterNameCombo->currentIndex()).toString() == QString(pEmptyItemCode) ||
518 m_pAdapterNameCombo->currentText().isEmpty() ? QString() : m_pAdapterNameCombo->currentText());
519 if (m_strNATNetworkName != newName)
520 m_strNATNetworkName = newName;
521 break;
522 }
523 default:
524 break;
525 }
526
527 /* Revalidate: */
528 m_pParent->revalidate();
529}
530
531void UIMachineSettingsNetwork::sltHandleAdvancedButtonStateChange()
532{
533 /* Update visibility of advanced options: */
534 m_pAdapterTypeLabel->setVisible(m_pAdvancedArrow->isExpanded());
535 m_pAdapterTypeCombo->setVisible(m_pAdvancedArrow->isExpanded());
536 m_pPromiscuousModeLabel->setVisible(m_pAdvancedArrow->isExpanded());
537 m_pPromiscuousModeCombo->setVisible(m_pAdvancedArrow->isExpanded());
538 m_pGenericPropertiesLabel->setVisible(attachmentType() == KNetworkAttachmentType_Generic &&
539 m_pAdvancedArrow->isExpanded());
540 m_pGenericPropertiesTextEdit->setVisible(attachmentType() == KNetworkAttachmentType_Generic &&
541 m_pAdvancedArrow->isExpanded());
542 m_pMACLabel->setVisible(m_pAdvancedArrow->isExpanded());
543 m_pMACEditor->setVisible(m_pAdvancedArrow->isExpanded());
544 m_pMACButton->setVisible(m_pAdvancedArrow->isExpanded());
545 m_pCableConnectedCheckBox->setVisible(m_pAdvancedArrow->isExpanded());
546 m_pPortForwardingButton->setVisible(m_pAdvancedArrow->isExpanded());
547}
548
549void UIMachineSettingsNetwork::sltGenerateMac()
550{
551 m_pMACEditor->setText(vboxGlobal().host().GenerateMACAddress());
552}
553
554void UIMachineSettingsNetwork::sltOpenPortForwardingDlg()
555{
556 UIMachineSettingsPortForwardingDlg dlg(this, m_portForwardingRules);
557 if (dlg.exec() == QDialog::Accepted)
558 m_portForwardingRules = dlg.rules();
559}
560
561void UIMachineSettingsNetwork::prepareValidation()
562{
563 /* Configure validation: */
564 connect(m_pMACEditor, SIGNAL(textEdited(const QString &)), m_pParent, SLOT(revalidate()));
565}
566
567void UIMachineSettingsNetwork::populateComboboxes()
568{
569 /* Attachment type: */
570 {
571 /* Remember the currently selected attachment type: */
572 int iCurrentAttachment = m_pAttachmentTypeComboBox->currentIndex();
573
574 /* Clear the attachments combo-box: */
575 m_pAttachmentTypeComboBox->clear();
576
577 /* Populate attachments: */
578 int iAttachmentTypeIndex = 0;
579 m_pAttachmentTypeComboBox->insertItem(iAttachmentTypeIndex, gpConverter->toString(KNetworkAttachmentType_Null));
580 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_Null);
581 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeComboBox->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
582 ++iAttachmentTypeIndex;
583 m_pAttachmentTypeComboBox->insertItem(iAttachmentTypeIndex, gpConverter->toString(KNetworkAttachmentType_NAT));
584 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_NAT);
585 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeComboBox->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
586 ++iAttachmentTypeIndex;
587 m_pAttachmentTypeComboBox->insertItem(iAttachmentTypeIndex, gpConverter->toString(KNetworkAttachmentType_NATNetwork));
588 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_NATNetwork);
589 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeComboBox->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
590 ++iAttachmentTypeIndex;
591 m_pAttachmentTypeComboBox->insertItem(iAttachmentTypeIndex, gpConverter->toString(KNetworkAttachmentType_Bridged));
592 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_Bridged);
593 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeComboBox->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
594 ++iAttachmentTypeIndex;
595 m_pAttachmentTypeComboBox->insertItem(iAttachmentTypeIndex, gpConverter->toString(KNetworkAttachmentType_Internal));
596 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_Internal);
597 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeComboBox->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
598 ++iAttachmentTypeIndex;
599 m_pAttachmentTypeComboBox->insertItem(iAttachmentTypeIndex, gpConverter->toString(KNetworkAttachmentType_HostOnly));
600 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_HostOnly);
601 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeComboBox->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
602 ++iAttachmentTypeIndex;
603 m_pAttachmentTypeComboBox->insertItem(iAttachmentTypeIndex, gpConverter->toString(KNetworkAttachmentType_Generic));
604 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_Generic);
605 m_pAttachmentTypeComboBox->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeComboBox->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
606 ++iAttachmentTypeIndex;
607
608 /* Restore the previously selected attachment type: */
609 m_pAttachmentTypeComboBox->setCurrentIndex(iCurrentAttachment == -1 ? 0 : iCurrentAttachment);
610 }
611
612 /* Adapter type: */
613 {
614 /* Remember the currently selected adapter type: */
615 int iCurrentAdapter = m_pAdapterTypeCombo->currentIndex();
616
617 /* Clear the adapter type combo-box: */
618 m_pAdapterTypeCombo->clear();
619
620 /* Populate adapter types: */
621 int iAdapterTypeIndex = 0;
622 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, gpConverter->toString(KNetworkAdapterType_Am79C970A));
623 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_Am79C970A);
624 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
625 ++iAdapterTypeIndex;
626 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, gpConverter->toString(KNetworkAdapterType_Am79C973));
627 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_Am79C973);
628 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
629 ++iAdapterTypeIndex;
630#ifdef VBOX_WITH_E1000
631 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, gpConverter->toString(KNetworkAdapterType_I82540EM));
632 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_I82540EM);
633 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
634 ++iAdapterTypeIndex;
635 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, gpConverter->toString(KNetworkAdapterType_I82543GC));
636 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_I82543GC);
637 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
638 ++iAdapterTypeIndex;
639 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, gpConverter->toString(KNetworkAdapterType_I82545EM));
640 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_I82545EM);
641 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
642 ++iAdapterTypeIndex;
643#endif /* VBOX_WITH_E1000 */
644#ifdef VBOX_WITH_VIRTIO
645 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, gpConverter->toString(KNetworkAdapterType_Virtio));
646 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_Virtio);
647 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
648 ++iAdapterTypeIndex;
649#endif /* VBOX_WITH_VIRTIO */
650
651 /* Restore the previously selected adapter type: */
652 m_pAdapterTypeCombo->setCurrentIndex(iCurrentAdapter == -1 ? 0 : iCurrentAdapter);
653 }
654
655 /* Promiscuous Mode type: */
656 {
657 /* Remember the currently selected promiscuous mode type: */
658 int iCurrentPromiscuousMode = m_pPromiscuousModeCombo->currentIndex();
659
660 /* Clear the promiscuous mode combo-box: */
661 m_pPromiscuousModeCombo->clear();
662
663 /* Populate promiscuous modes: */
664 int iPromiscuousModeIndex = 0;
665 m_pPromiscuousModeCombo->insertItem(iPromiscuousModeIndex, gpConverter->toString(KNetworkAdapterPromiscModePolicy_Deny));
666 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, KNetworkAdapterPromiscModePolicy_Deny);
667 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, m_pPromiscuousModeCombo->itemText(iPromiscuousModeIndex), Qt::ToolTipRole);
668 ++iPromiscuousModeIndex;
669 m_pPromiscuousModeCombo->insertItem(iPromiscuousModeIndex, gpConverter->toString(KNetworkAdapterPromiscModePolicy_AllowNetwork));
670 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, KNetworkAdapterPromiscModePolicy_AllowNetwork);
671 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, m_pPromiscuousModeCombo->itemText(iPromiscuousModeIndex), Qt::ToolTipRole);
672 ++iPromiscuousModeIndex;
673 m_pPromiscuousModeCombo->insertItem(iPromiscuousModeIndex, gpConverter->toString(KNetworkAdapterPromiscModePolicy_AllowAll));
674 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, KNetworkAdapterPromiscModePolicy_AllowAll);
675 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, m_pPromiscuousModeCombo->itemText(iPromiscuousModeIndex), Qt::ToolTipRole);
676 ++iPromiscuousModeIndex;
677
678 /* Restore the previously selected promiscuous mode type: */
679 m_pPromiscuousModeCombo->setCurrentIndex(iCurrentPromiscuousMode == -1 ? 0 : iCurrentPromiscuousMode);
680 }
681}
682
683void UIMachineSettingsNetwork::updateAlternativeList()
684{
685 /* Block signals initially: */
686 m_pAdapterNameCombo->blockSignals(true);
687
688 /* Repopulate alternative-name combo: */
689 m_pAdapterNameCombo->clear();
690 switch (attachmentType())
691 {
692 case KNetworkAttachmentType_Bridged:
693 m_pAdapterNameCombo->insertItems(0, m_pParent->bridgedAdapterList());
694 break;
695 case KNetworkAttachmentType_Internal:
696 m_pAdapterNameCombo->insertItems(0, m_pParent->internalNetworkList());
697 break;
698 case KNetworkAttachmentType_HostOnly:
699 m_pAdapterNameCombo->insertItems(0, m_pParent->hostInterfaceList());
700 break;
701 case KNetworkAttachmentType_Generic:
702 m_pAdapterNameCombo->insertItems(0, m_pParent->genericDriverList());
703 break;
704 case KNetworkAttachmentType_NATNetwork:
705 m_pAdapterNameCombo->insertItems(0, m_pParent->natNetworkList());
706 break;
707 default:
708 break;
709 }
710
711 /* Prepend 'empty' or 'default' item to alternative-name combo: */
712 if (m_pAdapterNameCombo->count() == 0)
713 {
714 switch (attachmentType())
715 {
716 case KNetworkAttachmentType_Bridged:
717 case KNetworkAttachmentType_HostOnly:
718 case KNetworkAttachmentType_NATNetwork:
719 {
720 /* If adapter list is empty => add 'Not selected' item: */
721 int pos = m_pAdapterNameCombo->findData(pEmptyItemCode);
722 if (pos == -1)
723 m_pAdapterNameCombo->insertItem(0, tr("Not selected", "network adapter name"), pEmptyItemCode);
724 else
725 m_pAdapterNameCombo->setItemText(pos, tr("Not selected", "network adapter name"));
726 break;
727 }
728 case KNetworkAttachmentType_Internal:
729 {
730 /* Internal network list should have a default item: */
731 if (m_pAdapterNameCombo->findText("intnet") == -1)
732 m_pAdapterNameCombo->insertItem(0, "intnet");
733 break;
734 }
735 default:
736 break;
737 }
738 }
739
740 /* Unblock signals finally: */
741 m_pAdapterNameCombo->blockSignals(false);
742}
743
744void UIMachineSettingsNetwork::updateAlternativeName()
745{
746 /* Block signals initially: */
747 m_pAdapterNameCombo->blockSignals(true);
748
749 switch (attachmentType())
750 {
751 case KNetworkAttachmentType_Bridged:
752 case KNetworkAttachmentType_Internal:
753 case KNetworkAttachmentType_HostOnly:
754 case KNetworkAttachmentType_Generic:
755 case KNetworkAttachmentType_NATNetwork:
756 {
757 m_pAdapterNameCombo->setCurrentIndex(position(m_pAdapterNameCombo, alternativeName()));
758 break;
759 }
760 default:
761 break;
762 }
763
764 /* Unblock signals finally: */
765 m_pAdapterNameCombo->blockSignals(false);
766}
767
768/* static */
769int UIMachineSettingsNetwork::position(QComboBox *pComboBox, int iData)
770{
771 int iPosition = pComboBox->findData(iData);
772 return iPosition == -1 ? 0 : iPosition;
773}
774
775/* static */
776int UIMachineSettingsNetwork::position(QComboBox *pComboBox, const QString &strText)
777{
778 int iPosition = pComboBox->findText(strText);
779 return iPosition == -1 ? 0 : iPosition;
780}
781
782/* UIMachineSettingsNetworkPage Stuff: */
783UIMachineSettingsNetworkPage::UIMachineSettingsNetworkPage()
784 : m_pTwAdapters(0)
785{
786 /* Setup main layout: */
787 QVBoxLayout *pMainLayout = new QVBoxLayout(this);
788 pMainLayout->setContentsMargins(0, 5, 0, 5);
789
790 /* Creating tab-widget: */
791 m_pTwAdapters = new QITabWidget(this);
792 pMainLayout->addWidget(m_pTwAdapters);
793
794 /* How many adapters to display: */
795 ulong uCount = qMin((ULONG)4, vboxGlobal().virtualBox().GetSystemProperties().GetMaxNetworkAdapters(KChipsetType_PIIX3));
796 /* Add corresponding tab pages to parent tab widget: */
797 for (ulong uSlot = 0; uSlot < uCount; ++uSlot)
798 {
799 /* Creating adapter tab: */
800 UIMachineSettingsNetwork *pTab = new UIMachineSettingsNetwork(this);
801 m_pTwAdapters->addTab(pTab, pTab->tabTitle());
802 }
803}
804
805/* Load data to cache from corresponding external object(s),
806 * this task COULD be performed in other than GUI thread: */
807void UIMachineSettingsNetworkPage::loadToCacheFrom(QVariant &data)
808{
809 /* Fetch data to machine: */
810 UISettingsPageMachine::fetchData(data);
811
812 /* Clear cache initially: */
813 m_cache.clear();
814
815 /* Cache name lists: */
816 refreshBridgedAdapterList();
817 refreshInternalNetworkList(true);
818 refreshHostInterfaceList();
819 refreshGenericDriverList(true);
820 refreshNATNetworkList();
821
822 /* For each network adapter: */
823 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
824 {
825 /* Prepare adapter data: */
826 UIDataSettingsMachineNetworkAdapter adapterData;
827
828 /* Check if adapter is valid: */
829 const CNetworkAdapter &adapter = m_machine.GetNetworkAdapter(iSlot);
830 if (!adapter.isNull())
831 {
832 /* Gather main options: */
833 adapterData.m_iSlot = iSlot;
834 adapterData.m_fAdapterEnabled = adapter.GetEnabled();
835 adapterData.m_attachmentType = adapter.GetAttachmentType();
836 adapterData.m_strBridgedAdapterName = wipedOutString(adapter.GetBridgedInterface());
837 adapterData.m_strInternalNetworkName = wipedOutString(adapter.GetInternalNetwork());
838 adapterData.m_strHostInterfaceName = wipedOutString(adapter.GetHostOnlyInterface());
839 adapterData.m_strGenericDriverName = wipedOutString(adapter.GetGenericDriver());
840 adapterData.m_strNATNetworkName = wipedOutString(adapter.GetNATNetwork());
841
842 /* Gather advanced options: */
843 adapterData.m_adapterType = adapter.GetAdapterType();
844 adapterData.m_promiscuousMode = adapter.GetPromiscModePolicy();
845 adapterData.m_strMACAddress = adapter.GetMACAddress();
846 adapterData.m_strGenericProperties = summarizeGenericProperties(adapter);
847 adapterData.m_fCableConnected = adapter.GetCableConnected();
848
849 /* Gather redirect options: */
850 QVector<QString> redirects = adapter.GetNATEngine().GetRedirects();
851 for (int i = 0; i < redirects.size(); ++i)
852 {
853 QStringList redirectData = redirects[i].split(',');
854 AssertMsg(redirectData.size() == 6, ("Redirect rule should be composed of 6 parts!\n"));
855 adapterData.m_redirects << UIPortForwardingData(redirectData[0],
856 (KNATProtocol)redirectData[1].toUInt(),
857 redirectData[2],
858 redirectData[3].toUInt(),
859 redirectData[4],
860 redirectData[5].toUInt());
861 }
862 }
863
864 /* Cache adapter data: */
865 m_cache.child(iSlot).cacheInitialData(adapterData);
866 }
867
868 /* Upload machine to data: */
869 UISettingsPageMachine::uploadData(data);
870}
871
872/* Load data to corresponding widgets from cache,
873 * this task SHOULD be performed in GUI thread only: */
874void UIMachineSettingsNetworkPage::getFromCache()
875{
876 /* Setup tab order: */
877 Assert(firstWidget());
878 setTabOrder(firstWidget(), m_pTwAdapters->focusProxy());
879 QWidget *pLastFocusWidget = m_pTwAdapters->focusProxy();
880
881 /* For each network adapter: */
882 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
883 {
884 /* Get adapter page: */
885 UIMachineSettingsNetwork *pTab = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(iSlot));
886
887 /* Load adapter data to page: */
888 pTab->fetchAdapterCache(m_cache.child(iSlot));
889
890 /* Setup tab order: */
891 pLastFocusWidget = pTab->setOrderAfter(pLastFocusWidget);
892 }
893
894 /* Applying language settings: */
895 retranslateUi();
896
897 /* Polish page finally: */
898 polishPage();
899
900 /* Revalidate: */
901 revalidate();
902}
903
904/* Save data from corresponding widgets to cache,
905 * this task SHOULD be performed in GUI thread only: */
906void UIMachineSettingsNetworkPage::putToCache()
907{
908 /* For each network adapter: */
909 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
910 {
911 /* Get adapter page: */
912 UIMachineSettingsNetwork *pTab = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(iSlot));
913
914 /* Gather & cache adapter data: */
915 pTab->uploadAdapterCache(m_cache.child(iSlot));
916 }
917}
918
919/* Save data from cache to corresponding external object(s),
920 * this task COULD be performed in other than GUI thread: */
921void UIMachineSettingsNetworkPage::saveFromCacheTo(QVariant &data)
922{
923 /* Fetch data to machine: */
924 UISettingsPageMachine::fetchData(data);
925
926 /* Check if network data was changed: */
927 if (m_cache.wasChanged())
928 {
929 /* For each network adapter: */
930 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
931 {
932 /* Check if adapter data was changed: */
933 const UICacheSettingsMachineNetworkAdapter &adapterCache = m_cache.child(iSlot);
934 if (adapterCache.wasChanged())
935 {
936 /* Check if adapter still valid: */
937 CNetworkAdapter adapter = m_machine.GetNetworkAdapter(iSlot);
938 if (!adapter.isNull())
939 {
940 /* Get adapter data from cache: */
941 const UIDataSettingsMachineNetworkAdapter &adapterData = adapterCache.data();
942
943 /* Store adapter data: */
944 if (isMachineOffline())
945 {
946 /* Basic attributes: */
947 adapter.SetEnabled(adapterData.m_fAdapterEnabled);
948 adapter.SetAdapterType(adapterData.m_adapterType);
949 adapter.SetMACAddress(adapterData.m_strMACAddress);
950 }
951 if (isMachineInValidMode())
952 {
953 /* Attachment type: */
954 switch (adapterData.m_attachmentType)
955 {
956 case KNetworkAttachmentType_Bridged:
957 adapter.SetBridgedInterface(adapterData.m_strBridgedAdapterName);
958 break;
959 case KNetworkAttachmentType_Internal:
960 adapter.SetInternalNetwork(adapterData.m_strInternalNetworkName);
961 break;
962 case KNetworkAttachmentType_HostOnly:
963 adapter.SetHostOnlyInterface(adapterData.m_strHostInterfaceName);
964 break;
965 case KNetworkAttachmentType_Generic:
966 adapter.SetGenericDriver(adapterData.m_strGenericDriverName);
967 updateGenericProperties(adapter, adapterData.m_strGenericProperties);
968 break;
969 case KNetworkAttachmentType_NATNetwork:
970 adapter.SetNATNetwork(adapterData.m_strNATNetworkName);
971 break;
972 default:
973 break;
974 }
975 adapter.SetAttachmentType(adapterData.m_attachmentType);
976 /* Advanced attributes: */
977 adapter.SetPromiscModePolicy(adapterData.m_promiscuousMode);
978 /* Cable connected flag: */
979 adapter.SetCableConnected(adapterData.m_fCableConnected);
980 /* Redirect options: */
981 QVector<QString> oldRedirects = adapter.GetNATEngine().GetRedirects();
982 for (int i = 0; i < oldRedirects.size(); ++i)
983 adapter.GetNATEngine().RemoveRedirect(oldRedirects[i].section(',', 0, 0));
984 UIPortForwardingDataList newRedirects = adapterData.m_redirects;
985 for (int i = 0; i < newRedirects.size(); ++i)
986 {
987 UIPortForwardingData newRedirect = newRedirects[i];
988 adapter.GetNATEngine().AddRedirect(newRedirect.name, newRedirect.protocol,
989 newRedirect.hostIp, newRedirect.hostPort.value(),
990 newRedirect.guestIp, newRedirect.guestPort.value());
991 }
992 }
993 }
994 }
995 }
996 }
997
998 /* Upload machine to data: */
999 UISettingsPageMachine::uploadData(data);
1000}
1001
1002bool UIMachineSettingsNetworkPage::validate(QList<UIValidationMessage> &messages)
1003{
1004 /* Pass by default: */
1005 bool fValid = true;
1006
1007 /* Delegate validation to adapter tabs: */
1008 for (int i = 0; i < m_pTwAdapters->count(); ++i)
1009 {
1010 UIMachineSettingsNetwork *pTab = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(i));
1011 AssertMsg(pTab, ("Can't get adapter tab!\n"));
1012 if (!pTab->validate(messages))
1013 fValid = false;
1014 }
1015
1016 /* Return result: */
1017 return fValid;
1018}
1019
1020void UIMachineSettingsNetworkPage::retranslateUi()
1021{
1022 for (int i = 0; i < m_pTwAdapters->count(); ++ i)
1023 {
1024 UIMachineSettingsNetwork *pTab = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(i));
1025 Assert(pTab);
1026 m_pTwAdapters->setTabText(i, pTab->tabTitle());
1027 }
1028}
1029
1030void UIMachineSettingsNetworkPage::sltHandleUpdatedTab()
1031{
1032 /* Determine the sender: */
1033 UIMachineSettingsNetwork *pSender = qobject_cast<UIMachineSettingsNetwork*>(sender());
1034 AssertMsg(pSender, ("This slot should be called only through signal<->slot mechanism from one of UIMachineSettingsNetwork tabs!\n"));
1035
1036 /* Determine sender's attachment type: */
1037 KNetworkAttachmentType senderAttachmentType = pSender->attachmentType();
1038 switch (senderAttachmentType)
1039 {
1040 case KNetworkAttachmentType_Internal:
1041 {
1042 refreshInternalNetworkList();
1043 break;
1044 }
1045 case KNetworkAttachmentType_Generic:
1046 {
1047 refreshGenericDriverList();
1048 break;
1049 }
1050 default:
1051 break;
1052 }
1053
1054 /* Update all the tabs except the sender: */
1055 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
1056 {
1057 /* Get the iterated tab: */
1058 UIMachineSettingsNetwork *pTab = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(iSlot));
1059 AssertMsg(pTab, ("All the tabs of m_pTwAdapters should be of the UIMachineSettingsNetwork type!\n"));
1060
1061 /* Update all the tabs (except sender) with the same attachment type as sender have: */
1062 if (pTab != pSender && pTab->attachmentType() == senderAttachmentType)
1063 pTab->reloadAlternative();
1064 }
1065}
1066
1067void UIMachineSettingsNetworkPage::polishPage()
1068{
1069 /* Get the count of network adapter tabs: */
1070 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
1071 {
1072 m_pTwAdapters->setTabEnabled(iSlot,
1073 isMachineOffline() ||
1074 (isMachineInValidMode() && m_cache.child(iSlot).base().m_fAdapterEnabled));
1075 UIMachineSettingsNetwork *pTab = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(iSlot));
1076 pTab->polishTab();
1077 }
1078}
1079
1080void UIMachineSettingsNetworkPage::refreshBridgedAdapterList()
1081{
1082 /* Reload bridged interface list: */
1083 m_bridgedAdapterList.clear();
1084 const CHostNetworkInterfaceVector &ifaces = vboxGlobal().host().GetNetworkInterfaces();
1085 for (int i = 0; i < ifaces.size(); ++i)
1086 {
1087 const CHostNetworkInterface &iface = ifaces[i];
1088 if (iface.GetInterfaceType() == KHostNetworkInterfaceType_Bridged && !m_bridgedAdapterList.contains(iface.GetName()))
1089 m_bridgedAdapterList << iface.GetName();
1090 }
1091}
1092
1093void UIMachineSettingsNetworkPage::refreshInternalNetworkList(bool fFullRefresh /* = false */)
1094{
1095 /* Reload internal network list: */
1096 m_internalNetworkList.clear();
1097 /* Get internal network names from other VMs: */
1098 if (fFullRefresh)
1099 m_internalNetworkList << otherInternalNetworkList();
1100 /* Append internal network list with names from all the tabs: */
1101 for (int iTab = 0; iTab < m_pTwAdapters->count(); ++iTab)
1102 {
1103 UIMachineSettingsNetwork *pTab = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(iTab));
1104 if (pTab)
1105 {
1106 QString strName = pTab->alternativeName(KNetworkAttachmentType_Internal);
1107 if (!strName.isEmpty() && !m_internalNetworkList.contains(strName))
1108 m_internalNetworkList << strName;
1109 }
1110 }
1111}
1112
1113void UIMachineSettingsNetworkPage::refreshHostInterfaceList()
1114{
1115 /* Reload host-only interface list: */
1116 m_hostInterfaceList.clear();
1117 const CHostNetworkInterfaceVector &ifaces = vboxGlobal().host().GetNetworkInterfaces();
1118 for (int i = 0; i < ifaces.size(); ++i)
1119 {
1120 const CHostNetworkInterface &iface = ifaces[i];
1121 if (iface.GetInterfaceType() == KHostNetworkInterfaceType_HostOnly && !m_hostInterfaceList.contains(iface.GetName()))
1122 m_hostInterfaceList << iface.GetName();
1123 }
1124}
1125
1126void UIMachineSettingsNetworkPage::refreshGenericDriverList(bool fFullRefresh /* = false */)
1127{
1128 /* Load generic driver list: */
1129 m_genericDriverList.clear();
1130 /* Get generic driver names from other VMs: */
1131 if (fFullRefresh)
1132 m_genericDriverList << otherGenericDriverList();
1133 /* Append generic driver list with names from all the tabs: */
1134 for (int iTab = 0; iTab < m_pTwAdapters->count(); ++iTab)
1135 {
1136 UIMachineSettingsNetwork *pTab = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(iTab));
1137 if (pTab)
1138 {
1139 QString strName = pTab->alternativeName(KNetworkAttachmentType_Generic);
1140 if (!strName.isEmpty() && !m_genericDriverList.contains(strName))
1141 m_genericDriverList << strName;
1142 }
1143 }
1144}
1145
1146void UIMachineSettingsNetworkPage::refreshNATNetworkList()
1147{
1148 /* Reload NAT network list: */
1149 m_natNetworkList.clear();
1150 const CNATNetworkVector &nws = vboxGlobal().virtualBox().GetNATNetworks();
1151 for (int i = 0; i < nws.size(); ++i)
1152 {
1153 const CNATNetwork &nw = nws[i];
1154 m_natNetworkList << nw.GetNetworkName();
1155 }
1156}
1157
1158/* static */
1159QStringList UIMachineSettingsNetworkPage::otherInternalNetworkList()
1160{
1161 /* Load total internal network list of all VMs: */
1162 CVirtualBox vbox = vboxGlobal().virtualBox();
1163 QStringList otherInternalNetworks(QList<QString>::fromVector(vbox.GetInternalNetworks()));
1164 return otherInternalNetworks;
1165}
1166
1167/* static */
1168QStringList UIMachineSettingsNetworkPage::otherGenericDriverList()
1169{
1170 /* Load total generic driver list of all VMs: */
1171 CVirtualBox vbox = vboxGlobal().virtualBox();
1172 QStringList otherGenericDrivers(QList<QString>::fromVector(vbox.GetGenericNetworkDrivers()));
1173 return otherGenericDrivers;
1174}
1175
1176/* static */
1177QString UIMachineSettingsNetworkPage::summarizeGenericProperties(const CNetworkAdapter &adapter)
1178{
1179 /* Prepare formatted string: */
1180 QVector<QString> names;
1181 QVector<QString> props;
1182 props = adapter.GetProperties(QString(), names);
1183 QString strResult;
1184 /* Load generic properties: */
1185 for (int i = 0; i < names.size(); ++i)
1186 {
1187 strResult += names[i] + "=" + props[i];
1188 if (i < names.size() - 1)
1189 strResult += "\n";
1190 }
1191 /* Return formatted string: */
1192 return strResult;
1193}
1194
1195/* static */
1196void UIMachineSettingsNetworkPage::updateGenericProperties(CNetworkAdapter &adapter, const QString &strPropText)
1197{
1198 /* Parse new properties: */
1199 QStringList newProps = strPropText.split("\n");
1200 QHash<QString, QString> hash;
1201
1202 /* Save new properties: */
1203 for (int i = 0; i < newProps.size(); ++i)
1204 {
1205 QString strLine = newProps[i];
1206 int iSplitPos = strLine.indexOf("=");
1207 if (iSplitPos)
1208 {
1209 QString strKey = strLine.left(iSplitPos);
1210 QString strVal = strLine.mid(iSplitPos+1);
1211 adapter.SetProperty(strKey, strVal);
1212 hash[strKey] = strVal;
1213 }
1214 }
1215
1216 /* Removing deleted properties: */
1217 QVector<QString> names;
1218 QVector<QString> props;
1219 props = adapter.GetProperties(QString(), names);
1220 for (int i = 0; i < names.size(); ++i)
1221 {
1222 QString strName = names[i];
1223 QString strValue = props[i];
1224 if (strValue != hash[strName])
1225 adapter.SetProperty(strName, hash[strName]);
1226 }
1227}
1228
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette