3
* VBox frontends: Qt4 GUI ("VirtualBox"):
4
* VBoxVMSettingsNetwork class implementation
8
* Copyright (C) 2008 Sun Microsystems, Inc.
10
* This file is part of VirtualBox Open Source Edition (OSE), as
11
* available from http://www.virtualbox.org. This file is free software;
12
* you can redistribute it and/or modify it under the terms of the GNU
13
* General Public License (GPL) as published by the Free Software
14
* Foundation, in version 2 as it comes in the "COPYING" file of the
15
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19
* Clara, CA 95054 USA or visit http://www.sun.com if you need
20
* additional information or have any questions.
24
#include "QIWidgetValidator.h"
25
#include "VBoxGlobal.h"
26
#include "VBoxVMSettingsNetwork.h"
27
#include "VBoxVMSettingsNetworkDetails.h"
33
/* Empty item extra-code */
34
const char *emptyItemCode = "#empty#";
36
/* VBoxVMSettingsNetwork Stuff */
37
VBoxVMSettingsNetwork::VBoxVMSettingsNetwork (VBoxVMSettingsNetworkPage *aParent)
38
: QIWithRetranslateUI <QWidget> (0)
40
, mDetails (new VBoxVMSettingsNetworkDetails (this))
43
/* Apply UI decorations */
44
Ui::VBoxVMSettingsNetwork::setupUi (this);
47
mTbDetails->setIcon (VBoxGlobal::iconSet (
48
":/settings_16px.png", ":/settings_dis_16px.png"));
49
mCbName->setInsertPolicy (QComboBox::NoInsert);
51
/* Applying language settings */
55
/* Prevent this widgets to go in the Small/Mini size state which is
56
* available on Mac OS X. Not sure why this happens but this seems to help
58
QList <QWidget*> list = findChildren <QWidget*>();
59
foreach (QWidget *w, list)
60
if (w->parent() == this)
61
w->setFixedHeight (w->sizeHint().height());
63
/* Remove tool-button border at MAC */
64
mTbDetails->setStyleSheet ("QToolButton {border: 0px none black;}");
68
void VBoxVMSettingsNetwork::getFromAdapter (const CNetworkAdapter &aAdapter)
72
/* Load adapter activity state */
73
mGbAdapter->setChecked (aAdapter.GetEnabled());
75
/* Load adapter type */
76
int adapterPos = mCbAdapterType->findData (aAdapter.GetAdapterType());
77
mCbAdapterType->setCurrentIndex (adapterPos == -1 ? 0 : adapterPos);
79
/* Load attachment type */
80
int attachmentPos = mCbAttachmentType->findData (aAdapter.GetAttachmentType());
81
mCbAttachmentType->setCurrentIndex (attachmentPos == -1 ? 0 : attachmentPos);
83
/* Load alternative name */
84
switch (attachmentType())
86
case KNetworkAttachmentType_Bridged:
87
mBrgName = mAdapter.GetHostInterface();
88
if (mBrgName.isEmpty()) mBrgName = QString::null;
90
case KNetworkAttachmentType_Internal:
91
mIntName = mAdapter.GetInternalNetwork();
92
if (mIntName.isEmpty()) mIntName = QString::null;
94
case KNetworkAttachmentType_HostOnly:
95
mHoiName = mAdapter.GetHostInterface();
96
if (mHoiName.isEmpty()) mHoiName = QString::null;
101
updateAttachmentAlternative();
103
/* Load details page */
104
mDetails->getFromAdapter (aAdapter);
107
void VBoxVMSettingsNetwork::putBackToAdapter()
109
/* Save adapter activity state */
110
mAdapter.SetEnabled (mGbAdapter->isChecked());
112
/* Save adapter type */
113
KNetworkAdapterType type = (KNetworkAdapterType)
114
mCbAdapterType->itemData (mCbAdapterType->currentIndex()).toInt();
115
mAdapter.SetAdapterType (type);
117
/* Save attachment type & alternative name */
118
switch (attachmentType())
120
case KNetworkAttachmentType_Null:
123
case KNetworkAttachmentType_NAT:
124
mAdapter.AttachToNAT();
126
case KNetworkAttachmentType_Bridged:
127
mAdapter.AttachToBridgedInterface();
128
mAdapter.SetHostInterface (alternativeName());
130
case KNetworkAttachmentType_Internal:
131
mAdapter.AttachToInternalNetwork();
132
mAdapter.SetInternalNetwork (alternativeName());
134
case KNetworkAttachmentType_HostOnly:
135
mAdapter.AttachToHostOnlyInterface();
136
mAdapter.SetHostInterface (alternativeName());
142
/* Save details page */
143
mDetails->putBackToAdapter();
146
void VBoxVMSettingsNetwork::setValidator (QIWidgetValidator *aValidator)
148
mValidator = aValidator;
150
connect (mGbAdapter, SIGNAL (toggled (bool)),
151
mValidator, SLOT (revalidate()));
152
connect (mCbAttachmentType, SIGNAL (activated (const QString&)),
153
this, SLOT (updateAttachmentAlternative()));
154
connect (mTbDetails, SIGNAL (clicked (bool)),
155
this, SLOT (detailsClicked()));
156
connect (mCbName, SIGNAL (activated (const QString&)),
157
this, SLOT (updateAlternativeName()));
158
connect (mCbName, SIGNAL (editTextChanged (const QString&)),
159
this, SLOT (updateAlternativeName()));
161
mValidator->revalidate();
164
bool VBoxVMSettingsNetwork::revalidate (QString &aWarning, QString &aTitle)
166
/* 'True' for disabled adapter */
167
if (!mGbAdapter->isChecked())
170
/* Validate alternatives */
172
switch (attachmentType())
174
case KNetworkAttachmentType_Bridged:
175
if (alternativeName().isNull())
177
aWarning = tr ("no bridged network adapter is selected");
181
case KNetworkAttachmentType_Internal:
182
if (alternativeName().isNull())
184
aWarning = tr ("no internal network name is specified");
188
case KNetworkAttachmentType_HostOnly:
189
if (alternativeName().isNull())
191
aWarning = tr ("no host-only network adapter is selected");
200
aTitle += ": " + vboxGlobal().removeAccelMark (pageTitle());
205
QWidget* VBoxVMSettingsNetwork::setOrderAfter (QWidget *aAfter)
207
setTabOrder (aAfter, mGbAdapter);
208
setTabOrder (mGbAdapter, mCbAdapterType);
209
setTabOrder (mCbAdapterType, mCbAttachmentType);
210
setTabOrder (mCbAttachmentType, mTbDetails);
211
setTabOrder (mTbDetails, mCbName);
215
QString VBoxVMSettingsNetwork::pageTitle() const
218
if (!mAdapter.isNull())
220
title = VBoxGlobal::tr ("Adapter %1", "network")
221
.arg (QString ("&%1").arg (mAdapter.GetSlot() + 1));
226
KNetworkAttachmentType VBoxVMSettingsNetwork::attachmentType() const
228
return (KNetworkAttachmentType) mCbAttachmentType->itemData (
229
mCbAttachmentType->currentIndex()).toInt();
232
QString VBoxVMSettingsNetwork::alternativeName (int aType) const
234
if (aType == -1) aType = attachmentType();
238
case KNetworkAttachmentType_Bridged:
241
case KNetworkAttachmentType_Internal:
244
case KNetworkAttachmentType_HostOnly:
250
Assert (result.isNull() || !result.isEmpty());
254
void VBoxVMSettingsNetwork::retranslateUi()
256
/* Translate uic generated strings */
257
Ui::VBoxVMSettingsNetwork::retranslateUi (this);
259
/* Translate combo-boxes content */
260
populateComboboxes();
262
/* Translate attachment info */
263
updateAttachmentAlternative();
266
void VBoxVMSettingsNetwork::updateAttachmentAlternative()
268
/* Blocking signals to change content manually */
269
mCbName->blockSignals (true);
271
/* Update alternative-name combo-box availability */
272
mLbName->setEnabled (attachmentType() != KNetworkAttachmentType_Null &&
273
attachmentType() != KNetworkAttachmentType_NAT);
274
mCbName->setEnabled (attachmentType() != KNetworkAttachmentType_Null &&
275
attachmentType() != KNetworkAttachmentType_NAT);
279
switch (attachmentType())
281
case KNetworkAttachmentType_Bridged:
282
mCbName->insertItems (0, mParent->brgList());
283
mCbName->setEditable (false);
285
case KNetworkAttachmentType_Internal:
286
mCbName->insertItems (0, mParent->intList());
287
mCbName->setEditable (true);
288
mCbName->setCompleter (0);
290
case KNetworkAttachmentType_HostOnly:
291
mCbName->insertItems (0, mParent->hoiList());
292
mCbName->setEditable (false);
298
/* Prepend 'empty' or 'default' item */
299
if (mCbName->count() == 0)
301
switch (attachmentType())
303
case KNetworkAttachmentType_Bridged:
304
case KNetworkAttachmentType_HostOnly:
306
/* Adapters list 'empty' */
307
int pos = mCbName->findData (emptyItemCode);
309
mCbName->insertItem (0, tr ("Not selected", "network adapter name"), emptyItemCode);
311
mCbName->setItemText (pos, tr ("Not selected", "network adapter name"));
314
case KNetworkAttachmentType_Internal:
316
/* Internal network 'default' name */
317
if (mCbName->findText ("intnet") == -1)
318
mCbName->insertItem (0, "intnet");
326
/* Select previous or default item */
327
switch (attachmentType())
329
case KNetworkAttachmentType_Bridged:
330
case KNetworkAttachmentType_HostOnly:
332
int pos = mCbName->findText (alternativeName());
333
mCbName->setCurrentIndex (pos == -1 ? 0 : pos);
336
case KNetworkAttachmentType_Internal:
338
int pos = mCbName->findText (alternativeName());
339
mCbName->setCurrentIndex (pos == -1 ? 0 : pos);
346
/* Remember selected item */
347
updateAlternativeName();
349
/* Unblocking signals as content is changed already */
350
mCbName->blockSignals (false);
353
void VBoxVMSettingsNetwork::updateAlternativeName()
355
switch (attachmentType())
357
case KNetworkAttachmentType_Bridged:
359
QString newName (mCbName->itemData (mCbName->currentIndex()).toString() ==
360
QString (emptyItemCode) ||
361
mCbName->currentText().isEmpty() ?
362
QString::null : mCbName->currentText());
363
if (mBrgName != newName)
367
case KNetworkAttachmentType_Internal:
369
QString newName ((mCbName->itemData (mCbName->currentIndex()).toString() ==
370
QString (emptyItemCode) &&
371
mCbName->currentText() ==
372
mCbName->itemText (mCbName->currentIndex())) ||
373
mCbName->currentText().isEmpty() ?
374
QString::null : mCbName->currentText());
375
if (mIntName != newName)
378
if (!mIntName.isNull())
379
QTimer::singleShot (0, mParent, SLOT (updatePages()));
383
case KNetworkAttachmentType_HostOnly:
385
QString newName (mCbName->itemData (mCbName->currentIndex()).toString() ==
386
QString (emptyItemCode) ||
387
mCbName->currentText().isEmpty() ?
388
QString::null : mCbName->currentText());
389
if (mHoiName != newName)
398
mValidator->revalidate();
401
void VBoxVMSettingsNetwork::detailsClicked()
403
/* Lock the button to avoid double-click bug */
404
mTbDetails->setEnabled (false);
406
/* Show details sub-dialog */
407
mDetails->activateWindow();
411
/* Unlock the previously locked button */
412
mTbDetails->setEnabled (true);
415
void VBoxVMSettingsNetwork::populateComboboxes()
417
/* Save the current selected adapter */
418
int currentAdapter = mCbAdapterType->currentIndex();
420
/* Clear the adapters combo-box */
421
mCbAdapterType->clear();
423
/* Populate adapters */
424
mCbAdapterType->insertItem (0,
425
vboxGlobal().toString (KNetworkAdapterType_Am79C970A));
426
mCbAdapterType->setItemData (0,
427
KNetworkAdapterType_Am79C970A);
428
mCbAdapterType->setItemData (0,
429
mCbAdapterType->itemText (0), Qt::ToolTipRole);
430
mCbAdapterType->insertItem (1,
431
vboxGlobal().toString (KNetworkAdapterType_Am79C973));
432
mCbAdapterType->setItemData (1,
433
KNetworkAdapterType_Am79C973);
434
mCbAdapterType->setItemData (1,
435
mCbAdapterType->itemText (1), Qt::ToolTipRole);
436
#ifdef VBOX_WITH_E1000
437
mCbAdapterType->insertItem (2,
438
vboxGlobal().toString (KNetworkAdapterType_I82540EM));
439
mCbAdapterType->setItemData (2,
440
KNetworkAdapterType_I82540EM);
441
mCbAdapterType->setItemData (2,
442
mCbAdapterType->itemText (2), Qt::ToolTipRole);
443
mCbAdapterType->insertItem (3,
444
vboxGlobal().toString (KNetworkAdapterType_I82543GC));
445
mCbAdapterType->setItemData (3,
446
KNetworkAdapterType_I82543GC);
447
mCbAdapterType->setItemData (3,
448
mCbAdapterType->itemText (3), Qt::ToolTipRole);
449
mCbAdapterType->insertItem (4,
450
vboxGlobal().toString (KNetworkAdapterType_I82545EM));
451
mCbAdapterType->setItemData (4,
452
KNetworkAdapterType_I82545EM);
453
mCbAdapterType->setItemData (4,
454
mCbAdapterType->itemText (4), Qt::ToolTipRole);
455
#endif /* VBOX_WITH_E1000 */
457
/* Set the old value */
458
mCbAdapterType->setCurrentIndex (currentAdapter == -1 ?
462
/* Save the current selected attachment type */
463
int currentAttachment = mCbAttachmentType->currentIndex();
465
/* Clear the attachments combo-box */
466
mCbAttachmentType->clear();
468
/* Populate attachments */
469
mCbAttachmentType->insertItem (0,
470
vboxGlobal().toString (KNetworkAttachmentType_Null));
471
mCbAttachmentType->setItemData (0,
472
KNetworkAttachmentType_Null);
473
mCbAttachmentType->setItemData (0,
474
mCbAttachmentType->itemText (0), Qt::ToolTipRole);
475
mCbAttachmentType->insertItem (1,
476
vboxGlobal().toString (KNetworkAttachmentType_NAT));
477
mCbAttachmentType->setItemData (1,
478
KNetworkAttachmentType_NAT);
479
mCbAttachmentType->setItemData (1,
480
mCbAttachmentType->itemText (1), Qt::ToolTipRole);
481
mCbAttachmentType->insertItem (2,
482
vboxGlobal().toString (KNetworkAttachmentType_Bridged));
483
mCbAttachmentType->setItemData (2,
484
KNetworkAttachmentType_Bridged);
485
mCbAttachmentType->setItemData (2,
486
mCbAttachmentType->itemText (2), Qt::ToolTipRole);
487
mCbAttachmentType->insertItem (3,
488
vboxGlobal().toString (KNetworkAttachmentType_Internal));
489
mCbAttachmentType->setItemData (3,
490
KNetworkAttachmentType_Internal);
491
mCbAttachmentType->setItemData (3,
492
mCbAttachmentType->itemText (3), Qt::ToolTipRole);
493
mCbAttachmentType->insertItem (4,
494
vboxGlobal().toString (KNetworkAttachmentType_HostOnly));
495
mCbAttachmentType->setItemData (4,
496
KNetworkAttachmentType_HostOnly);
497
mCbAttachmentType->setItemData (4,
498
mCbAttachmentType->itemText (4), Qt::ToolTipRole);
500
/* Set the old value */
501
mCbAttachmentType->setCurrentIndex (currentAttachment);
504
/* VBoxVMSettingsNetworkPage Stuff */
505
VBoxVMSettingsNetworkPage::VBoxVMSettingsNetworkPage()
508
/* Setup Main Layout */
509
QVBoxLayout *mainLayout = new QVBoxLayout (this);
510
mainLayout->setContentsMargins (0, 5, 0, 5);
512
/* Creating Tab Widget */
513
mTwAdapters = new QTabWidget (this);
514
mainLayout->addWidget (mTwAdapters);
517
QStringList VBoxVMSettingsNetworkPage::brgList (bool aRefresh)
521
/* Load & filter interface list */
523
CHostNetworkInterfaceVector interfaces =
524
vboxGlobal().virtualBox().GetHost().GetNetworkInterfaces();
525
for (CHostNetworkInterfaceVector::ConstIterator it = interfaces.begin();
526
it != interfaces.end(); ++ it)
528
if (it->GetInterfaceType() == KHostNetworkInterfaceType_Bridged)
529
mBrgList << it->GetName();
536
QStringList VBoxVMSettingsNetworkPage::intList (bool aRefresh)
540
/* Load total network list of all VMs */
542
CVirtualBox vbox = vboxGlobal().virtualBox();
543
ulong count = qMin ((ULONG) 4, vbox.GetSystemProperties().GetNetworkAdapterCount());
544
CMachineVector vec = vbox.GetMachines();
545
for (CMachineVector::ConstIterator m = vec.begin(); m != vec.end(); ++ m)
547
if (m->GetAccessible())
549
for (ulong slot = 0; slot < count; ++ slot)
551
QString name = m->GetNetworkAdapter (slot).GetInternalNetwork();
552
if (!name.isEmpty() && !mIntList.contains (name))
559
/* Append network list with names from all the pages */
560
QStringList list (mIntList);
561
for (int index = 0; index < mTwAdapters->count(); ++ index)
563
VBoxVMSettingsNetwork *page =
564
qobject_cast <VBoxVMSettingsNetwork*> (mTwAdapters->widget (index));
567
QString name = page->alternativeName (KNetworkAttachmentType_Internal);
568
if (!name.isEmpty() && !list.contains (name))
576
QStringList VBoxVMSettingsNetworkPage::hoiList (bool aRefresh)
580
/* Load & filter interface list */
582
CHostNetworkInterfaceVector interfaces =
583
vboxGlobal().virtualBox().GetHost().GetNetworkInterfaces();
584
for (CHostNetworkInterfaceVector::ConstIterator it = interfaces.begin();
585
it != interfaces.end(); ++ it)
587
if (it->GetInterfaceType() == KHostNetworkInterfaceType_HostOnly)
588
mHoiList << it->GetName();
595
void VBoxVMSettingsNetworkPage::getFrom (const CMachine &aMachine)
597
/* Setup tab order */
598
Assert (mFirstWidget);
599
setTabOrder (mFirstWidget, mTwAdapters->focusProxy());
600
QWidget *lastFocusWidget = mTwAdapters->focusProxy();
607
/* Creating Tab Pages */
608
CVirtualBox vbox = vboxGlobal().virtualBox();
609
ulong count = qMin ((ULONG) 4, vbox.GetSystemProperties().GetNetworkAdapterCount());
610
for (ulong slot = 0; slot < count; ++ slot)
613
CNetworkAdapter adapter = aMachine.GetNetworkAdapter (slot);
615
/* Creating Adapter's page */
616
VBoxVMSettingsNetwork *page = new VBoxVMSettingsNetwork (this);
618
/* Loading Adapter's data into page */
619
page->getFromAdapter (adapter);
621
/* Attach Adapter's page to Tab Widget */
622
mTwAdapters->addTab (page, page->pageTitle());
624
/* Setup validation */
625
page->setValidator (mValidator);
627
/* Setup tab order */
628
lastFocusWidget = page->setOrderAfter (lastFocusWidget);
631
/* Applying language settings */
635
void VBoxVMSettingsNetworkPage::putBackTo()
637
for (int i = 0; i < mTwAdapters->count(); ++ i)
639
VBoxVMSettingsNetwork *page =
640
qobject_cast <VBoxVMSettingsNetwork*> (mTwAdapters->widget (i));
642
page->putBackToAdapter();
646
void VBoxVMSettingsNetworkPage::setValidator (QIWidgetValidator *aVal)
651
bool VBoxVMSettingsNetworkPage::revalidate (QString &aWarning, QString &aTitle)
655
for (int i = 0; i < mTwAdapters->count(); ++ i)
657
VBoxVMSettingsNetwork *page =
658
qobject_cast <VBoxVMSettingsNetwork*> (mTwAdapters->widget (i));
660
valid = page->revalidate (aWarning, aTitle);
667
void VBoxVMSettingsNetworkPage::retranslateUi()
669
for (int i = 0; i < mTwAdapters->count(); ++ i)
671
VBoxVMSettingsNetwork *page =
672
qobject_cast <VBoxVMSettingsNetwork*> (mTwAdapters->widget (i));
674
mTwAdapters->setTabText (i, page->pageTitle());
678
void VBoxVMSettingsNetworkPage::updatePages()
680
for (int i = 0; i < mTwAdapters->count(); ++ i)
682
/* Get the iterated page */
683
VBoxVMSettingsNetwork *page =
684
qobject_cast <VBoxVMSettingsNetwork*> (mTwAdapters->widget (i));
687
/* Update the page if the attachment type is 'internal network' */
688
if (page->attachmentType() == KNetworkAttachmentType_Internal)
689
QTimer::singleShot (0, page, SLOT (updateAttachmentAlternative()));