VirtualBox

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

Last change on this file since 36928 was 36928, checked in by vboxsync, 14 years ago

FE/Qt: 4989: UISettingsCache template integrated into Network page.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 41.6 KB
Line 
1/* $Id: UIMachineSettingsNetwork.cpp 36928 2011-05-03 12:30:11Z vboxsync $ */
2/** @file
3 *
4 * VBox frontends: Qt4 GUI ("VirtualBox"):
5 * UIMachineSettingsNetwork class implementation
6 */
7
8/*
9 * Copyright (C) 2008-2011 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/* Qt includes */
21#include <QTimer>
22#include <QCompleter>
23
24/* Local includes */
25#include "QIWidgetValidator.h"
26#include "QIArrowButtonSwitch.h"
27#include "VBoxGlobal.h"
28#include "UIMachineSettingsNetwork.h"
29#include "QITabWidget.h"
30
31#ifdef VBOX_WITH_VDE
32# include <iprt/ldr.h>
33# include <VBox/VDEPlug.h>
34#endif /* VBOX_WITH_VDE */
35
36/* Empty item extra-code: */
37const char *emptyItemCode = "#empty#";
38
39UIMachineSettingsNetwork::UIMachineSettingsNetwork(UIMachineSettingsNetworkPage *pParent)
40 : QIWithRetranslateUI<QWidget>(0)
41 , m_pParent(pParent)
42 , m_pValidator(0)
43 , m_iSlot(-1)
44 , m_fPolished(false)
45{
46 /* Apply UI decorations: */
47 Ui::UIMachineSettingsNetwork::setupUi(this);
48
49 /* Setup widgets: */
50 m_pAdapterNameCombo->setInsertPolicy(QComboBox::NoInsert);
51 m_pMACEditor->setValidator(new QRegExpValidator(QRegExp("[0-9A-Fa-f][02468ACEace][0-9A-Fa-f]{10}"), this));
52 m_pMACEditor->setMinimumWidthByText(QString().fill('0', 12));
53
54 /* Setup connections: */
55 connect(m_pAdvancedArrow, SIGNAL(clicked()), this, SLOT(sltToggleAdvanced()));
56 connect(m_pMACButton, SIGNAL(clicked()), this, SLOT(sltGenerateMac()));
57 connect(m_pPortForwardingButton, SIGNAL(clicked()), this, SLOT(sltOpenPortForwardingDlg()));
58
59 /* Applying language settings: */
60 retranslateUi();
61}
62
63void UIMachineSettingsNetwork::polishTab()
64{
65 /* Basic attributes: */
66 mCbEnableAdapter->setEnabled(m_pParent->isMachineOffline());
67 m_pAttachmentTypeLabel->setEnabled(m_pParent->isMachineInValidMode());
68 m_pAttachmentTypeCombo->setEnabled(m_pParent->isMachineInValidMode());
69 m_pAdapterNameLabel->setEnabled(m_pParent->isMachineInValidMode() &&
70 attachmentType() != KNetworkAttachmentType_Null &&
71 attachmentType() != KNetworkAttachmentType_NAT);
72 m_pAdapterNameCombo->setEnabled(m_pParent->isMachineInValidMode() &&
73 attachmentType() != KNetworkAttachmentType_Null &&
74 attachmentType() != KNetworkAttachmentType_NAT);
75 m_pAdvancedArrow->setEnabled(m_pParent->isMachineInValidMode());
76
77 /* Advanced attributes: */
78 m_pAdapterTypeLabel->setEnabled(m_pParent->isMachineOffline());
79 m_pAdapterTypeCombo->setEnabled(m_pParent->isMachineOffline());
80 m_pPromiscuousModeLabel->setEnabled(m_pParent->isMachineInValidMode() &&
81 attachmentType() != KNetworkAttachmentType_Null &&
82 attachmentType() != KNetworkAttachmentType_NAT);
83 m_pPromiscuousModeCombo->setEnabled(m_pParent->isMachineInValidMode() &&
84 attachmentType() != KNetworkAttachmentType_Null &&
85 attachmentType() != KNetworkAttachmentType_NAT);
86 m_pMACLabel->setEnabled(m_pParent->isMachineOffline());
87 m_pMACEditor->setEnabled(m_pParent->isMachineOffline());
88 m_pMACButton->setEnabled(m_pParent->isMachineOffline());
89
90 /* Postprocessing: */
91 if ((m_pParent->isMachineSaved() || m_pParent->isMachineOnline()) && !m_pAdvancedArrow->isExpanded())
92 m_pAdvancedArrow->animateClick();
93 sltToggleAdvanced();
94}
95
96void UIMachineSettingsNetwork::fetchAdapterCache(const UICacheSettingsMachineNetworkAdapter &adapterCache)
97{
98 /* Get adapter data: */
99 const UIDataSettingsMachineNetworkAdapter &adapterData = adapterCache.base();
100
101 /* Load slot number: */
102 m_iSlot = adapterData.m_iSlot;
103
104 /* Load adapter activity state: */
105 mCbEnableAdapter->setChecked(adapterData.m_fAdapterEnabled);
106
107 /* Load attachment type: */
108 int iAttachmentPos = m_pAttachmentTypeCombo->findData(adapterData.m_attachmentType);
109 m_pAttachmentTypeCombo->setCurrentIndex(iAttachmentPos == -1 ? 0 : iAttachmentPos);
110
111 /* Load alternative name: */
112 switch (attachmentType())
113 {
114 case KNetworkAttachmentType_Bridged:
115 m_strBrgName = adapterData.m_strBridgedAdapterName;
116 if (m_strBrgName.isEmpty()) m_strBrgName = QString();
117 break;
118 case KNetworkAttachmentType_Internal:
119 m_strIntName = adapterData.m_strInternalNetworkName;
120 if (m_strIntName.isEmpty()) m_strIntName = QString();
121 break;
122 case KNetworkAttachmentType_HostOnly:
123 m_strHoiName = adapterData.m_strHostInterfaceName;
124 if (m_strHoiName.isEmpty()) m_strHoiName = QString();
125 break;
126#ifdef VBOX_WITH_VDE
127 case KNetworkAttachmentType_VDE:
128 mVDEName = adapterData.m_strVDENetworkName;
129 if (mVDEName.isEmpty()) mVDEName = QString();
130 break;
131#endif /* VBOX_WITH_VDE */
132 default:
133 break;
134 }
135 sltUpdateAttachmentAlternative();
136
137 /* Load adapter type: */
138 int iAdapterPos = m_pAdapterTypeCombo->findData(adapterData.m_adapterType);
139 m_pAdapterTypeCombo->setCurrentIndex(iAdapterPos == -1 ? 0 : iAdapterPos);
140
141 /* Load promiscuous mode type: */
142 int iPromiscuousModePos = m_pPromiscuousModeCombo->findData(adapterData.m_promiscuousMode);
143 m_pPromiscuousModeCombo->setCurrentIndex(iPromiscuousModePos == -1 ? 0 : iPromiscuousModePos);
144
145 /* Other options: */
146 m_pMACEditor->setText(adapterData.m_strMACAddress);
147 m_pCableConnectedCheckBox->setChecked(adapterData.m_fCableConnected);
148
149 /* Load port forwarding rules: */
150 m_portForwardingRules = adapterData.m_redirects;
151}
152
153void UIMachineSettingsNetwork::uploadAdapterCache(UICacheSettingsMachineNetworkAdapter &adapterCache)
154{
155 /* Prepare adapter data: */
156 UIDataSettingsMachineNetworkAdapter adapterData = adapterCache.base();
157
158 /* Save adapter activity state: */
159 adapterData.m_fAdapterEnabled = mCbEnableAdapter->isChecked();
160
161 /* Save attachment type & alternative name: */
162 adapterData.m_attachmentType = attachmentType();
163 switch (adapterData.m_attachmentType)
164 {
165 case KNetworkAttachmentType_Null:
166 break;
167 case KNetworkAttachmentType_NAT:
168 break;
169 case KNetworkAttachmentType_Bridged:
170 adapterData.m_strBridgedAdapterName = alternativeName();
171 break;
172 case KNetworkAttachmentType_Internal:
173 adapterData.m_strInternalNetworkName = alternativeName();
174 break;
175 case KNetworkAttachmentType_HostOnly:
176 adapterData.m_strHostInterfaceName = alternativeName();
177 break;
178#ifdef VBOX_WITH_VDE
179 case KNetworkAttachmentType_VDE:
180 adapterData.m_strVDENetworkName = alternativeName();
181 break;
182#endif /* VBOX_WITH_VDE */
183 default:
184 break;
185 }
186
187 /* Save adapter type: */
188 adapterData.m_adapterType = (KNetworkAdapterType)m_pAdapterTypeCombo->itemData(m_pAdapterTypeCombo->currentIndex()).toInt();
189
190 /* Save promiscuous mode type: */
191 adapterData.m_promiscuousMode = (KNetworkAdapterPromiscModePolicy)m_pPromiscuousModeCombo->itemData(m_pPromiscuousModeCombo->currentIndex()).toInt();
192
193 /* Other options: */
194 adapterData.m_strMACAddress = m_pMACEditor->text().isEmpty() ? QString() : m_pMACEditor->text();
195 adapterData.m_fCableConnected = m_pCableConnectedCheckBox->isChecked();
196
197 /* Save port forwarding rules: */
198 adapterData.m_redirects = m_portForwardingRules;
199
200 /* Cache adapter data: */
201 adapterCache.cacheCurrentData(adapterData);
202}
203
204void UIMachineSettingsNetwork::setValidator(QIWidgetValidator *pValidator)
205{
206 m_pValidator = pValidator;
207
208 connect(mCbEnableAdapter, SIGNAL(toggled(bool)), m_pValidator, SLOT(revalidate()));
209 connect(m_pAttachmentTypeCombo, SIGNAL(activated(const QString&)), this, SLOT(sltUpdateAttachmentAlternative()));
210 connect(m_pAdapterNameCombo, SIGNAL(activated(const QString&)), this, SLOT(sltUpdateAlternativeName()));
211 connect(m_pAdapterNameCombo, SIGNAL(editTextChanged(const QString&)), this, SLOT(sltUpdateAlternativeName()));
212
213 m_pValidator->revalidate();
214}
215
216bool UIMachineSettingsNetwork::revalidate(QString &strWarning, QString &strTitle)
217{
218 /* 'True' for disabled adapter: */
219 if (!mCbEnableAdapter->isChecked())
220 return true;
221
222 /* Validate alternatives: */
223 bool fValid = true;
224 switch (attachmentType())
225 {
226 case KNetworkAttachmentType_Bridged:
227 if (alternativeName().isNull())
228 {
229 strWarning = tr("no bridged network adapter is selected");
230 fValid = false;
231 }
232 break;
233 case KNetworkAttachmentType_Internal:
234 if (alternativeName().isNull())
235 {
236 strWarning = tr("no internal network name is specified");
237 fValid = false;
238 }
239 break;
240 case KNetworkAttachmentType_HostOnly:
241 if (alternativeName().isNull())
242 {
243 strWarning = tr("no host-only network adapter is selected");
244 fValid = false;
245 }
246 break;
247 default:
248 break;
249 }
250 if (!fValid)
251 strTitle += ": " + vboxGlobal().removeAccelMark(pageTitle());
252
253 return fValid;
254}
255
256QWidget* UIMachineSettingsNetwork::setOrderAfter(QWidget *pAfter)
257{
258 setTabOrder(pAfter, mCbEnableAdapter);
259 setTabOrder(mCbEnableAdapter, m_pAttachmentTypeCombo);
260 setTabOrder(m_pAttachmentTypeCombo, m_pAdapterNameCombo);
261 setTabOrder(m_pAdapterNameCombo, m_pAdvancedArrow);
262 setTabOrder(m_pAdvancedArrow, m_pAdapterTypeCombo);
263 setTabOrder(m_pAdapterTypeCombo, m_pPromiscuousModeCombo);
264 setTabOrder(m_pPromiscuousModeCombo, m_pMACEditor);
265 setTabOrder(m_pMACEditor, m_pMACButton);
266 setTabOrder(m_pMACButton, m_pCableConnectedCheckBox);
267 setTabOrder(m_pCableConnectedCheckBox, m_pPortForwardingButton);
268 return m_pPortForwardingButton;
269}
270
271QString UIMachineSettingsNetwork::pageTitle() const
272{
273 return VBoxGlobal::tr("Adapter %1", "network").arg(QString("&%1").arg(m_iSlot + 1));;
274}
275
276KNetworkAttachmentType UIMachineSettingsNetwork::attachmentType() const
277{
278 return (KNetworkAttachmentType)m_pAttachmentTypeCombo->itemData(m_pAttachmentTypeCombo->currentIndex()).toInt();
279}
280
281QString UIMachineSettingsNetwork::alternativeName(int type) const
282{
283 if (type == -1)
284 type = attachmentType();
285 QString strResult;
286 switch (type)
287 {
288 case KNetworkAttachmentType_Bridged:
289 strResult = m_strBrgName;
290 break;
291 case KNetworkAttachmentType_Internal:
292 strResult = m_strIntName;
293 break;
294 case KNetworkAttachmentType_HostOnly:
295 strResult = m_strHoiName;
296 break;
297#ifdef VBOX_WITH_VDE
298 case KNetworkAttachmentType_VDE:
299 strResult = mVDEName;
300 break;
301#endif
302 default:
303 break;
304 }
305 Assert(strResult.isNull() || !strResult.isEmpty());
306 return strResult;
307}
308
309void UIMachineSettingsNetwork::showEvent(QShowEvent *pEvent)
310{
311 /* Polish page if necessary: */
312 if (!m_fPolished)
313 {
314 m_fPolished = true;
315 /* Give the minimum size hint to the first layout column: */
316 m_pNetworkChildGridLayout->setColumnMinimumWidth (0, m_pAttachmentTypeLabel->width());
317 }
318 /* Call for base-class: */
319 QWidget::showEvent(pEvent);
320}
321
322void UIMachineSettingsNetwork::retranslateUi()
323{
324 /* Translate uic generated strings: */
325 Ui::UIMachineSettingsNetwork::retranslateUi(this);
326
327 /* Translate combo-boxes content: */
328 populateComboboxes();
329
330 /* Translate attachment info: */
331 sltUpdateAttachmentAlternative();
332}
333
334void UIMachineSettingsNetwork::sltUpdateAttachmentAlternative()
335{
336 /* Blocking signals to change content manually: */
337 m_pAdapterNameCombo->blockSignals(true);
338
339 /* Update alternative-name combo-box availability: */
340 m_pAdapterNameLabel->setEnabled(attachmentType() != KNetworkAttachmentType_Null &&
341 attachmentType() != KNetworkAttachmentType_NAT);
342 m_pAdapterNameCombo->setEnabled(attachmentType() != KNetworkAttachmentType_Null &&
343 attachmentType() != KNetworkAttachmentType_NAT);
344 m_pPromiscuousModeLabel->setEnabled(attachmentType() != KNetworkAttachmentType_Null &&
345 attachmentType() != KNetworkAttachmentType_NAT);
346 m_pPromiscuousModeCombo->setEnabled(attachmentType() != KNetworkAttachmentType_Null &&
347 attachmentType() != KNetworkAttachmentType_NAT);
348
349 /* Refresh list: */
350 m_pAdapterNameCombo->clear();
351 switch (attachmentType())
352 {
353 case KNetworkAttachmentType_Bridged:
354 m_pAdapterNameCombo->insertItems(0, m_pParent->brgList());
355 m_pAdapterNameCombo->setEditable(false);
356 break;
357 case KNetworkAttachmentType_Internal:
358 m_pAdapterNameCombo->insertItems(0, m_pParent->fullIntList());
359 m_pAdapterNameCombo->setEditable(true);
360 m_pAdapterNameCombo->setCompleter(0);
361 break;
362 case KNetworkAttachmentType_HostOnly:
363 m_pAdapterNameCombo->insertItems(0, m_pParent->hoiList());
364 m_pAdapterNameCombo->setEditable(false);
365 break;
366#ifdef VBOX_WITH_VDE
367 case KNetworkAttachmentType_VDE:
368 m_pAdapterNameCombo->insertItem(0, alternativeName());
369 m_pAdapterNameCombo->setEditable(true);
370 m_pAdapterNameCombo->setCompleter(0);
371 break;
372#endif
373 default:
374 break;
375 }
376
377 /* Prepend 'empty' or 'default' item: */
378 if (m_pAdapterNameCombo->count() == 0)
379 {
380 switch (attachmentType())
381 {
382 case KNetworkAttachmentType_Bridged:
383 case KNetworkAttachmentType_HostOnly:
384 {
385 /* Adapters list 'empty': */
386 int pos = m_pAdapterNameCombo->findData(emptyItemCode);
387 if (pos == -1)
388 m_pAdapterNameCombo->insertItem(0, tr("Not selected", "network adapter name"), emptyItemCode);
389 else
390 m_pAdapterNameCombo->setItemText(pos, tr("Not selected", "network adapter name"));
391 break;
392 }
393 case KNetworkAttachmentType_Internal:
394 {
395 /* Internal network 'default' name: */
396 if (m_pAdapterNameCombo->findText("intnet") == -1)
397 m_pAdapterNameCombo->insertItem(0, "intnet");
398 break;
399 }
400 default:
401 break;
402 }
403 }
404
405 /* Select previous or default item: */
406 switch (attachmentType())
407 {
408 case KNetworkAttachmentType_Bridged:
409 case KNetworkAttachmentType_HostOnly:
410 {
411 int pos = m_pAdapterNameCombo->findText(alternativeName());
412 m_pAdapterNameCombo->setCurrentIndex(pos == -1 ? 0 : pos);
413 break;
414 }
415 case KNetworkAttachmentType_Internal:
416 {
417 int pos = m_pAdapterNameCombo->findText(alternativeName());
418 m_pAdapterNameCombo->setCurrentIndex(pos == -1 ? 0 : pos);
419 break;
420 }
421 default:
422 break;
423 }
424
425 /* Remember selected item: */
426 sltUpdateAlternativeName();
427
428 /* Update Forwarding rules button availability: */
429 m_pPortForwardingButton->setEnabled(attachmentType() == KNetworkAttachmentType_NAT);
430
431 /* Unblocking signals as content is changed already: */
432 m_pAdapterNameCombo->blockSignals(false);
433}
434
435void UIMachineSettingsNetwork::sltUpdateAlternativeName()
436{
437 switch (attachmentType())
438 {
439 case KNetworkAttachmentType_Bridged:
440 {
441 QString newName(m_pAdapterNameCombo->itemData(m_pAdapterNameCombo->currentIndex()).toString() ==
442 QString(emptyItemCode) ||
443 m_pAdapterNameCombo->currentText().isEmpty() ?
444 QString::null : m_pAdapterNameCombo->currentText());
445 if (m_strBrgName != newName)
446 m_strBrgName = newName;
447 break;
448 }
449 case KNetworkAttachmentType_Internal:
450 {
451 QString newName((m_pAdapterNameCombo->itemData(m_pAdapterNameCombo->currentIndex()).toString() ==
452 QString(emptyItemCode) &&
453 m_pAdapterNameCombo->currentText() ==
454 m_pAdapterNameCombo->itemText(m_pAdapterNameCombo->currentIndex())) ||
455 m_pAdapterNameCombo->currentText().isEmpty() ?
456 QString::null : m_pAdapterNameCombo->currentText());
457 if (m_strIntName != newName)
458 {
459 m_strIntName = newName;
460 if (!m_strIntName.isNull())
461 QTimer::singleShot(0, m_pParent, SLOT (updatePages()));
462 }
463 break;
464 }
465 case KNetworkAttachmentType_HostOnly:
466 {
467 QString newName(m_pAdapterNameCombo->itemData(m_pAdapterNameCombo->currentIndex()).toString() ==
468 QString(emptyItemCode) ||
469 m_pAdapterNameCombo->currentText().isEmpty() ?
470 QString::null : m_pAdapterNameCombo->currentText());
471 if (m_strHoiName != newName)
472 m_strHoiName = newName;
473 break;
474 }
475#ifdef VBOX_WITH_VDE
476 case KNetworkAttachmentType_VDE:
477 {
478 QString newName((m_pAdapterNameCombo->itemData(m_pAdapterNameCombo->currentIndex()).toString() ==
479 QString(emptyItemCode) &&
480 m_pAdapterNameCombo->currentText() ==
481 m_pAdapterNameCombo->itemText(m_pAdapterNameCombo->currentIndex())) ||
482 m_pAdapterNameCombo->currentText().isEmpty() ?
483 QString::null : m_pAdapterNameCombo->currentText());
484 if (mVDEName != newName)
485 mVDEName = newName;
486 break;
487 }
488#endif
489 default:
490 break;
491 }
492
493 if (m_pValidator)
494 m_pValidator->revalidate();
495}
496
497void UIMachineSettingsNetwork::sltToggleAdvanced()
498{
499 m_pAdapterTypeLabel->setVisible(m_pAdvancedArrow->isExpanded());
500 m_pAdapterTypeCombo->setVisible(m_pAdvancedArrow->isExpanded());
501 m_pPromiscuousModeLabel->setVisible(m_pAdvancedArrow->isExpanded());
502 m_pPromiscuousModeCombo->setVisible(m_pAdvancedArrow->isExpanded());
503 m_pMACLabel->setVisible(m_pAdvancedArrow->isExpanded());
504 m_pMACEditor->setVisible(m_pAdvancedArrow->isExpanded());
505 m_pMACButton->setVisible(m_pAdvancedArrow->isExpanded());
506 m_pCableConnectedCheckBox->setVisible(m_pAdvancedArrow->isExpanded());
507 m_pPortForwardingButton->setVisible(m_pAdvancedArrow->isExpanded());
508}
509
510void UIMachineSettingsNetwork::sltGenerateMac()
511{
512 m_pMACEditor->setText(vboxGlobal().virtualBox().GetHost().GenerateMACAddress());
513}
514
515void UIMachineSettingsNetwork::sltOpenPortForwardingDlg()
516{
517 UIMachineSettingsPortForwardingDlg dlg(this, m_portForwardingRules);
518 if (dlg.exec() == QDialog::Accepted)
519 m_portForwardingRules = dlg.rules();
520}
521
522void UIMachineSettingsNetwork::populateComboboxes()
523{
524 /* Attachment type: */
525 {
526 /* Remember the currently selected attachment type: */
527 int iCurrentAttachment = m_pAttachmentTypeCombo->currentIndex();
528
529 /* Clear the attachments combo-box: */
530 m_pAttachmentTypeCombo->clear();
531
532 /* Populate attachments: */
533 int iAttachmentTypeIndex = 0;
534 m_pAttachmentTypeCombo->insertItem(iAttachmentTypeIndex, vboxGlobal().toString(KNetworkAttachmentType_Null));
535 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_Null);
536 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeCombo->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
537 ++iAttachmentTypeIndex;
538 m_pAttachmentTypeCombo->insertItem(iAttachmentTypeIndex, vboxGlobal().toString(KNetworkAttachmentType_NAT));
539 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_NAT);
540 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeCombo->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
541 ++iAttachmentTypeIndex;
542 m_pAttachmentTypeCombo->insertItem(iAttachmentTypeIndex, vboxGlobal().toString(KNetworkAttachmentType_Bridged));
543 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_Bridged);
544 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeCombo->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
545 ++iAttachmentTypeIndex;
546 m_pAttachmentTypeCombo->insertItem(iAttachmentTypeIndex, vboxGlobal().toString(KNetworkAttachmentType_Internal));
547 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_Internal);
548 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeCombo->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
549 ++iAttachmentTypeIndex;
550 m_pAttachmentTypeCombo->insertItem(iAttachmentTypeIndex, vboxGlobal().toString(KNetworkAttachmentType_HostOnly));
551 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_HostOnly);
552 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeCombo->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
553 ++iAttachmentTypeIndex;
554#ifdef VBOX_WITH_VDE
555 RTLDRMOD hLdrDummy;
556 if (RT_SUCCESS(RTLdrLoad(VBOX_LIB_VDE_PLUG_NAME, &hLdrDummy)))
557 {
558 m_pAttachmentTypeCombo->insertItem(iAttachmentTypeIndex, vboxGlobal().toString(KNetworkAttachmentType_VDE));
559 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, KNetworkAttachmentType_VDE);
560 m_pAttachmentTypeCombo->setItemData(iAttachmentTypeIndex, m_pAttachmentTypeCombo->itemText(iAttachmentTypeIndex), Qt::ToolTipRole);
561 ++iAttachmentTypeIndex;
562 }
563#endif
564
565 /* Restore the previously selected attachment type: */
566 m_pAttachmentTypeCombo->setCurrentIndex(iCurrentAttachment);
567 }
568
569 /* Adapter type: */
570 {
571 /* Remember the currently selected adapter type: */
572 int iCurrentAdapter = m_pAdapterTypeCombo->currentIndex();
573
574 /* Clear the adapter type combo-box: */
575 m_pAdapterTypeCombo->clear();
576
577 /* Populate adapter types: */
578 int iAdapterTypeIndex = 0;
579 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, vboxGlobal().toString(KNetworkAdapterType_Am79C970A));
580 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_Am79C970A);
581 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
582 ++iAdapterTypeIndex;
583 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, vboxGlobal().toString(KNetworkAdapterType_Am79C973));
584 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_Am79C973);
585 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
586 ++iAdapterTypeIndex;
587#ifdef VBOX_WITH_E1000
588 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, vboxGlobal().toString(KNetworkAdapterType_I82540EM));
589 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_I82540EM);
590 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
591 ++iAdapterTypeIndex;
592 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, vboxGlobal().toString(KNetworkAdapterType_I82543GC));
593 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_I82543GC);
594 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
595 ++iAdapterTypeIndex;
596 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, vboxGlobal().toString(KNetworkAdapterType_I82545EM));
597 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_I82545EM);
598 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
599 ++iAdapterTypeIndex;
600#endif /* VBOX_WITH_E1000 */
601#ifdef VBOX_WITH_VIRTIO
602 m_pAdapterTypeCombo->insertItem(iAdapterTypeIndex, vboxGlobal().toString(KNetworkAdapterType_Virtio));
603 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, KNetworkAdapterType_Virtio);
604 m_pAdapterTypeCombo->setItemData(iAdapterTypeIndex, m_pAdapterTypeCombo->itemText(iAdapterTypeIndex), Qt::ToolTipRole);
605 ++iAdapterTypeIndex;
606#endif /* VBOX_WITH_VIRTIO */
607
608 /* Restore the previously selected adapter type: */
609 m_pAdapterTypeCombo->setCurrentIndex(iCurrentAdapter == -1 ? 0 : iCurrentAdapter);
610 }
611
612 /* Promiscuous Mode type: */
613 {
614 /* Remember the currently selected promiscuous mode type: */
615 int iCurrentPromiscuousMode = m_pPromiscuousModeCombo->currentIndex();
616
617 /* Clear the promiscuous mode combo-box: */
618 m_pPromiscuousModeCombo->clear();
619
620 /* Populate promiscuous modes: */
621 int iPromiscuousModeIndex = 0;
622 m_pPromiscuousModeCombo->insertItem(iPromiscuousModeIndex, vboxGlobal().toString(KNetworkAdapterPromiscModePolicy_Deny));
623 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, KNetworkAdapterPromiscModePolicy_Deny);
624 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, m_pPromiscuousModeCombo->itemText(iPromiscuousModeIndex), Qt::ToolTipRole);
625 ++iPromiscuousModeIndex;
626 m_pPromiscuousModeCombo->insertItem(iPromiscuousModeIndex, vboxGlobal().toString(KNetworkAdapterPromiscModePolicy_AllowNetwork));
627 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, KNetworkAdapterPromiscModePolicy_AllowNetwork);
628 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, m_pPromiscuousModeCombo->itemText(iPromiscuousModeIndex), Qt::ToolTipRole);
629 ++iPromiscuousModeIndex;
630 m_pPromiscuousModeCombo->insertItem(iPromiscuousModeIndex, vboxGlobal().toString(KNetworkAdapterPromiscModePolicy_AllowAll));
631 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, KNetworkAdapterPromiscModePolicy_AllowAll);
632 m_pPromiscuousModeCombo->setItemData(iPromiscuousModeIndex, m_pPromiscuousModeCombo->itemText(iPromiscuousModeIndex), Qt::ToolTipRole);
633 ++iPromiscuousModeIndex;
634
635 /* Restore the previously selected promiscuous mode type: */
636 m_pPromiscuousModeCombo->setCurrentIndex(iCurrentPromiscuousMode);
637 }
638}
639
640/* UIMachineSettingsNetworkPage Stuff */
641UIMachineSettingsNetworkPage::UIMachineSettingsNetworkPage()
642 : m_pValidator(0)
643 , m_pTwAdapters(0)
644{
645 /* Setup main layout: */
646 QVBoxLayout *pMainLayout = new QVBoxLayout(this);
647 pMainLayout->setContentsMargins(0, 5, 0, 5);
648
649 /* Creating tab-widget: */
650 m_pTwAdapters = new QITabWidget(this);
651 pMainLayout->addWidget(m_pTwAdapters);
652
653 /* How many adapters to display: */
654 ulong uCount = qMin((ULONG)4, vboxGlobal().virtualBox().GetSystemProperties().GetMaxNetworkAdapters(KChipsetType_PIIX3));
655 /* Add corresponding tab pages to parent tab widget: */
656 for (ulong uSlot = 0; uSlot < uCount; ++uSlot)
657 {
658 /* Creating adapter page: */
659 UIMachineSettingsNetwork *pPage = new UIMachineSettingsNetwork(this);
660 m_pTwAdapters->addTab(pPage, pPage->pageTitle());
661 }
662}
663
664QStringList UIMachineSettingsNetworkPage::brgList(bool fRefresh)
665{
666 if (fRefresh)
667 {
668 /* Load & filter interface list: */
669 m_brgList.clear();
670 CHostNetworkInterfaceVector interfaces = vboxGlobal().virtualBox().GetHost().GetNetworkInterfaces();
671 for (CHostNetworkInterfaceVector::ConstIterator it = interfaces.begin();
672 it != interfaces.end(); ++it)
673 {
674 if (it->GetInterfaceType() == KHostNetworkInterfaceType_Bridged)
675 m_brgList << it->GetName();
676 }
677 }
678
679 return m_brgList;
680}
681
682QStringList UIMachineSettingsNetworkPage::intList(bool fRefresh)
683{
684 if (fRefresh)
685 {
686 /* Load total network list of all VMs: */
687 m_intList.clear();
688 CVirtualBox vbox = vboxGlobal().virtualBox();
689 ulong count = qMin ((ULONG) 4, vbox.GetSystemProperties().GetMaxNetworkAdapters(KChipsetType_PIIX3));
690 CMachineVector vec = vbox.GetMachines();
691 for (CMachineVector::ConstIterator m = vec.begin(); m != vec.end(); ++m)
692 {
693 if (m->GetAccessible())
694 {
695 for (ulong slot = 0; slot < count; ++slot)
696 {
697 QString strName = m->GetNetworkAdapter(slot).GetInternalNetwork();
698 if (!strName.isEmpty() && !m_intList.contains(strName))
699 m_intList << strName;
700 }
701 }
702 }
703 }
704
705 return m_intList;
706}
707
708QStringList UIMachineSettingsNetworkPage::fullIntList(bool fRefresh)
709{
710 QStringList list(intList(fRefresh));
711 /* Append network list with names from all the pages: */
712 for (int index = 0; index < m_pTwAdapters->count(); ++index)
713 {
714 UIMachineSettingsNetwork *pPage =
715 qobject_cast <UIMachineSettingsNetwork*>(m_pTwAdapters->widget(index));
716 if (pPage)
717 {
718 QString strName = pPage->alternativeName(KNetworkAttachmentType_Internal);
719 if (!strName.isEmpty() && !list.contains(strName))
720 list << strName;
721 }
722 }
723 return list;
724}
725
726QStringList UIMachineSettingsNetworkPage::hoiList(bool fRefresh)
727{
728 if (fRefresh)
729 {
730 /* Load & filter interface list: */
731 m_hoiList.clear();
732 CHostNetworkInterfaceVector interfaces = vboxGlobal().virtualBox().GetHost().GetNetworkInterfaces();
733 for (CHostNetworkInterfaceVector::ConstIterator it = interfaces.begin();
734 it != interfaces.end(); ++it)
735 {
736 if (it->GetInterfaceType() == KHostNetworkInterfaceType_HostOnly)
737 m_hoiList << it->GetName();
738 }
739 }
740
741 return m_hoiList;
742}
743
744/* Load data to cashe from corresponding external object(s),
745 * this task COULD be performed in other than GUI thread: */
746void UIMachineSettingsNetworkPage::loadToCacheFrom(QVariant &data)
747{
748 /* Fetch data to machine: */
749 UISettingsPageMachine::fetchData(data);
750
751 /* Cache names lists: */
752 brgList(true);
753 intList(true);
754 hoiList(true);
755
756 /* For each network adapter: */
757 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
758 {
759 /* Prepare adapter data: */
760 UIDataSettingsMachineNetworkAdapter adapterData;
761
762 /* Check if adapter is valid: */
763 const CNetworkAdapter &adapter = m_machine.GetNetworkAdapter(iSlot);
764 if (!adapter.isNull())
765 {
766 /* Gather main options: */
767 adapterData.m_iSlot = iSlot;
768 adapterData.m_fAdapterEnabled = adapter.GetEnabled();
769 adapterData.m_attachmentType = adapter.GetAttachmentType();
770 switch (adapterData.m_attachmentType)
771 {
772 case KNetworkAttachmentType_Bridged:
773 adapterData.m_strBridgedAdapterName = adapter.GetHostInterface();
774 if (adapterData.m_strBridgedAdapterName.isEmpty())
775 adapterData.m_strBridgedAdapterName = QString();
776 break;
777 case KNetworkAttachmentType_Internal:
778 adapterData.m_strInternalNetworkName = adapter.GetInternalNetwork();
779 if (adapterData.m_strInternalNetworkName.isEmpty())
780 adapterData.m_strInternalNetworkName = QString();
781 break;
782 case KNetworkAttachmentType_HostOnly:
783 adapterData.m_strHostInterfaceName = adapter.GetHostInterface();
784 if (adapterData.m_strHostInterfaceName.isEmpty())
785 adapterData.m_strHostInterfaceName = QString();
786 break;
787#ifdef VBOX_WITH_VDE
788 case KNetworkAttachmentType_VDE:
789 adapterData.m_strVDENetworkName = adapter.GetVDENetwork();
790 if (adapterData.m_strVDENetworkName.isEmpty())
791 adapterData.m_strVDENetworkName = QString();
792 break;
793#endif /* VBOX_WITH_VDE */
794 default:
795 break;
796 }
797
798 /* Gather advanced options: */
799 adapterData.m_adapterType = adapter.GetAdapterType();
800 adapterData.m_promiscuousMode = adapter.GetPromiscModePolicy();
801 adapterData.m_strMACAddress = adapter.GetMACAddress();
802 adapterData.m_fCableConnected = adapter.GetCableConnected();
803
804 /* Gather redirect options: */
805 QVector<QString> redirects = adapter.GetNatDriver().GetRedirects();
806 for (int i = 0; i < redirects.size(); ++i)
807 {
808 QStringList redirectData = redirects[i].split(',');
809 AssertMsg(redirectData.size() == 6, ("Redirect rule should be composed of 6 parts!\n"));
810 adapterData.m_redirects << UIPortForwardingData(redirectData[0],
811 (KNATProtocol)redirectData[1].toUInt(),
812 redirectData[2],
813 redirectData[3].toUInt(),
814 redirectData[4],
815 redirectData[5].toUInt());
816 }
817 }
818
819 /* Cache adapter data: */
820 m_cache.child(iSlot).cacheInitialData(adapterData);
821 }
822
823 /* Upload machine to data: */
824 UISettingsPageMachine::uploadData(data);
825}
826
827/* Load data to corresponding widgets from cache,
828 * this task SHOULD be performed in GUI thread only: */
829void UIMachineSettingsNetworkPage::getFromCache()
830{
831 /* Setup tab order: */
832 Assert(firstWidget());
833 setTabOrder(firstWidget(), m_pTwAdapters->focusProxy());
834 QWidget *pLastFocusWidget = m_pTwAdapters->focusProxy();
835
836 /* For each network adapter: */
837 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
838 {
839 /* Get adapter page: */
840 UIMachineSettingsNetwork *pPage = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(iSlot));
841
842 /* Load adapter data to page: */
843 pPage->fetchAdapterCache(m_cache.child(iSlot));
844
845 /* Setup page validation: */
846 pPage->setValidator(m_pValidator);
847
848 /* Setup tab order: */
849 pLastFocusWidget = pPage->setOrderAfter(pLastFocusWidget);
850 }
851
852 /* Applying language settings: */
853 retranslateUi();
854
855 /* Revalidate if possible: */
856 if (m_pValidator) m_pValidator->revalidate();
857}
858
859/* Save data from corresponding widgets to cache,
860 * this task SHOULD be performed in GUI thread only: */
861void UIMachineSettingsNetworkPage::putToCache()
862{
863 /* For each network adapter: */
864 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
865 {
866 /* Get adapter page: */
867 UIMachineSettingsNetwork *pPage = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(iSlot));
868
869 /* Gather & cache adapter data: */
870 pPage->uploadAdapterCache(m_cache.child(iSlot));
871 }
872}
873
874/* Save data from cache to corresponding external object(s),
875 * this task COULD be performed in other than GUI thread: */
876void UIMachineSettingsNetworkPage::saveFromCacheTo(QVariant &data)
877{
878 /* Fetch data to machine: */
879 UISettingsPageMachine::fetchData(data);
880
881 /* Check if network data was changed: */
882 if (m_cache.wasChanged())
883 {
884 /* For each network adapter: */
885 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
886 {
887 /* Check if adapter data was changed: */
888 const UICacheSettingsMachineNetworkAdapter &adapterCache = m_cache.child(iSlot);
889 if (adapterCache.wasChanged())
890 {
891 /* Check if adapter still valid: */
892 CNetworkAdapter adapter = m_machine.GetNetworkAdapter(iSlot);
893 if (!adapter.isNull())
894 {
895 /* Get adapter data from cache: */
896 const UIDataSettingsMachineNetworkAdapter &adapterData = adapterCache.data();
897
898 /* Store adapter data: */
899 if (isMachineOffline())
900 {
901 /* Basic attributes: */
902 adapter.SetEnabled(adapterData.m_fAdapterEnabled);
903 adapter.SetAdapterType(adapterData.m_adapterType);
904 adapter.SetMACAddress(adapterData.m_strMACAddress);
905 }
906 if (isMachineInValidMode())
907 {
908 /* Attachment type: */
909 switch (adapterData.m_attachmentType)
910 {
911 case KNetworkAttachmentType_Null:
912 adapter.Detach();
913 break;
914 case KNetworkAttachmentType_NAT:
915 adapter.AttachToNAT();
916 break;
917 case KNetworkAttachmentType_Bridged:
918 adapter.SetHostInterface(adapterData.m_strBridgedAdapterName);
919 adapter.AttachToBridgedInterface();
920 break;
921 case KNetworkAttachmentType_Internal:
922 adapter.SetInternalNetwork(adapterData.m_strInternalNetworkName);
923 adapter.AttachToInternalNetwork();
924 break;
925 case KNetworkAttachmentType_HostOnly:
926 adapter.SetHostInterface(adapterData.m_strHostInterfaceName);
927 adapter.AttachToHostOnlyInterface();
928 break;
929#ifdef VBOX_WITH_VDE
930 case KNetworkAttachmentType_VDE:
931 adapter.SetVDENetwork(adapterData.m_strVDENetworkName);
932 adapter.AttachToVDE();
933 break;
934#endif /* VBOX_WITH_VDE */
935 default:
936 break;
937 }
938 /* Advanced attributes: */
939 adapter.SetPromiscModePolicy(adapterData.m_promiscuousMode);
940 /* Cable connected flag: */
941 adapter.SetCableConnected(adapterData.m_fCableConnected);
942 /* Redirect options: */
943 QVector<QString> oldRedirects = adapter.GetNatDriver().GetRedirects();
944 for (int i = 0; i < oldRedirects.size(); ++i)
945 adapter.GetNatDriver().RemoveRedirect(oldRedirects[i].section(',', 0, 0));
946 UIPortForwardingDataList newRedirects = adapterData.m_redirects;
947 for (int i = 0; i < newRedirects.size(); ++i)
948 {
949 UIPortForwardingData newRedirect = newRedirects[i];
950 adapter.GetNatDriver().AddRedirect(newRedirect.name, newRedirect.protocol,
951 newRedirect.hostIp, newRedirect.hostPort.value(),
952 newRedirect.guestIp, newRedirect.guestPort.value());
953 }
954 }
955 }
956 }
957 }
958 }
959
960 /* Upload machine to data: */
961 UISettingsPageMachine::uploadData(data);
962}
963
964void UIMachineSettingsNetworkPage::setValidator(QIWidgetValidator *pValidator)
965{
966 m_pValidator = pValidator;
967}
968
969bool UIMachineSettingsNetworkPage::revalidate(QString &strWarning, QString &strTitle)
970{
971 bool fValid = true;
972
973 for (int i = 0; i < m_pTwAdapters->count(); ++i)
974 {
975 UIMachineSettingsNetwork *pPage = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(i));
976 Assert(pPage);
977 fValid = pPage->revalidate(strWarning, strTitle);
978 if (!fValid)
979 break;
980 }
981
982 return fValid;
983}
984
985void UIMachineSettingsNetworkPage::retranslateUi()
986{
987 for (int i = 0; i < m_pTwAdapters->count(); ++ i)
988 {
989 UIMachineSettingsNetwork *pPage = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(i));
990 Assert(pPage);
991 m_pTwAdapters->setTabText(i, pPage->pageTitle());
992 }
993}
994
995void UIMachineSettingsNetworkPage::updatePages()
996{
997 for (int i = 0; i < m_pTwAdapters->count(); ++ i)
998 {
999 /* Get the iterated page: */
1000 UIMachineSettingsNetwork *pPage = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(i));
1001 Assert(pPage);
1002
1003 /* Update the page if the attachment type is 'internal network' */
1004 if (pPage->attachmentType() == KNetworkAttachmentType_Internal)
1005 QTimer::singleShot(0, pPage, SLOT(sltUpdateAttachmentAlternative()));
1006 }
1007}
1008
1009void UIMachineSettingsNetworkPage::polishPage()
1010{
1011 /* Get the count of network adapter tabs: */
1012 for (int iSlot = 0; iSlot < m_pTwAdapters->count(); ++iSlot)
1013 {
1014 m_pTwAdapters->setTabEnabled(iSlot,
1015 isMachineOffline() ||
1016 (isMachineInValidMode() && m_cache.child(iSlot).base().m_fAdapterEnabled));
1017 UIMachineSettingsNetwork *pTab = qobject_cast<UIMachineSettingsNetwork*>(m_pTwAdapters->widget(iSlot));
1018 pTab->polishTab();
1019 }
1020}
1021
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