VirtualBox

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

Last change on this file since 56217 was 56217, checked in by vboxsync, 10 years ago

FE/Qt: A bunch of casting build-warnings.

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

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