VirtualBox

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

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

FE/Qt: language string updates.

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