/src/VBox/Frontends/VirtualBox/src/settings/vm/VBoxVMSettingsNetwork.cpp

https://gitlab.com/ufo/virtualbox-ose-3-1-8 · C++ · 752 lines · 577 code · 86 blank · 89 comment · 80 complexity · 8fcf61ccc89b439d2015d7970bec83f3 MD5 · raw file

  1. /** @file
  2. *
  3. * VBox frontends: Qt4 GUI ("VirtualBox"):
  4. * VBoxVMSettingsNetwork class implementation
  5. */
  6. /*
  7. * Copyright (C) 2008 Sun Microsystems, Inc.
  8. *
  9. * This file is part of VirtualBox Open Source Edition (OSE), as
  10. * available from http://www.virtualbox.org. This file is free software;
  11. * you can redistribute it and/or modify it under the terms of the GNU
  12. * General Public License (GPL) as published by the Free Software
  13. * Foundation, in version 2 as it comes in the "COPYING" file of the
  14. * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
  15. * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
  16. *
  17. * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
  18. * Clara, CA 95054 USA or visit http://www.sun.com if you need
  19. * additional information or have any questions.
  20. */
  21. /* VBox Includes */
  22. #include "QIWidgetValidator.h"
  23. #include "QIArrowButtonSwitch.h"
  24. #include "VBoxGlobal.h"
  25. #include "VBoxVMSettingsNetwork.h"
  26. /* Qt Includes */
  27. #include <QTimer>
  28. #include <QCompleter>
  29. /* Empty item extra-code */
  30. const char *emptyItemCode = "#empty#";
  31. /* VBoxVMSettingsNetwork Stuff */
  32. VBoxVMSettingsNetwork::VBoxVMSettingsNetwork (VBoxVMSettingsNetworkPage *aParent, bool aDisableStaticControls)
  33. : QIWithRetranslateUI <QWidget> (0)
  34. , mParent (aParent)
  35. , mValidator (0)
  36. , mPolished (false)
  37. , mDisableStaticControls (false)
  38. {
  39. /* Apply UI decorations */
  40. Ui::VBoxVMSettingsNetwork::setupUi (this);
  41. /* Setup widgets */
  42. mCbAdapterName->setInsertPolicy (QComboBox::NoInsert);
  43. mLeMAC->setValidator (new QRegExpValidator (QRegExp (
  44. "[0-9A-Fa-f][02468ACEace][0-9A-Fa-f]{10}"), this));
  45. mLeMAC->setMinimumWidthByText (QString().fill ('0', 12));
  46. /* Setup connections */
  47. connect (mAbsAdvanced, SIGNAL (clicked()), this, SLOT (toggleAdvanced()));
  48. connect (mTbMAC, SIGNAL (clicked()), this, SLOT (generateMac()));
  49. #ifdef Q_WS_MAC
  50. /* Prevent this widgets to go in the Small/Mini size state which is
  51. * available on Mac OS X. Not sure why this happens but this seems to help
  52. * against. */
  53. QList <QWidget*> list = findChildren <QWidget*>();
  54. foreach (QWidget *w, list)
  55. if (w->parent() == this)
  56. w->setFixedHeight (w->sizeHint().height());
  57. #endif /* Q_WS_MAC */
  58. /* Applying language settings */
  59. retranslateUi();
  60. /* If some controls should be disabled or not when the
  61. * same tab widgets are shown during runtime
  62. */
  63. mDisableStaticControls = aDisableStaticControls;
  64. }
  65. void VBoxVMSettingsNetwork::getFromAdapter (const CNetworkAdapter &aAdapter)
  66. {
  67. mAdapter = aAdapter;
  68. /* Load adapter activity state */
  69. mCbEnableAdapter->setChecked (aAdapter.GetEnabled());
  70. /* Load adapter type */
  71. int adapterPos = mCbAdapterType->findData (aAdapter.GetAdapterType());
  72. mCbAdapterType->setCurrentIndex (adapterPos == -1 ? 0 : adapterPos);
  73. /* Load attachment type */
  74. int attachmentPos = mCbAttachmentType->findData (aAdapter.GetAttachmentType());
  75. mCbAttachmentType->setCurrentIndex (attachmentPos == -1 ? 0 : attachmentPos);
  76. /* Load alternative name */
  77. switch (attachmentType())
  78. {
  79. case KNetworkAttachmentType_Bridged:
  80. mBrgName = mAdapter.GetHostInterface();
  81. if (mBrgName.isEmpty()) mBrgName = QString::null;
  82. break;
  83. case KNetworkAttachmentType_Internal:
  84. mIntName = mAdapter.GetInternalNetwork();
  85. if (mIntName.isEmpty()) mIntName = QString::null;
  86. break;
  87. case KNetworkAttachmentType_HostOnly:
  88. mHoiName = mAdapter.GetHostInterface();
  89. if (mHoiName.isEmpty()) mHoiName = QString::null;
  90. break;
  91. default:
  92. break;
  93. }
  94. updateAttachmentAlternative();
  95. mLeMAC->setText (mAdapter.GetMACAddress());
  96. mCbCableConnected->setChecked (mAdapter.GetCableConnected());
  97. }
  98. void VBoxVMSettingsNetwork::putBackToAdapter()
  99. {
  100. /* Save adapter activity state */
  101. mAdapter.SetEnabled (mCbEnableAdapter->isChecked());
  102. /* Save adapter type */
  103. KNetworkAdapterType type = (KNetworkAdapterType)
  104. mCbAdapterType->itemData (mCbAdapterType->currentIndex()).toInt();
  105. mAdapter.SetAdapterType (type);
  106. /* Save attachment type & alternative name */
  107. switch (attachmentType())
  108. {
  109. case KNetworkAttachmentType_Null:
  110. mAdapter.Detach();
  111. break;
  112. case KNetworkAttachmentType_NAT:
  113. mAdapter.AttachToNAT();
  114. break;
  115. case KNetworkAttachmentType_Bridged:
  116. mAdapter.SetHostInterface (alternativeName());
  117. mAdapter.AttachToBridgedInterface();
  118. break;
  119. case KNetworkAttachmentType_Internal:
  120. mAdapter.SetInternalNetwork (alternativeName());
  121. mAdapter.AttachToInternalNetwork();
  122. break;
  123. case KNetworkAttachmentType_HostOnly:
  124. mAdapter.SetHostInterface (alternativeName());
  125. mAdapter.AttachToHostOnlyInterface();
  126. break;
  127. default:
  128. break;
  129. }
  130. mAdapter.SetMACAddress (mLeMAC->text().isEmpty() ? QString::null : mLeMAC->text());
  131. mAdapter.SetCableConnected (mCbCableConnected->isChecked());
  132. }
  133. void VBoxVMSettingsNetwork::setValidator (QIWidgetValidator *aValidator)
  134. {
  135. mValidator = aValidator;
  136. if (!mDisableStaticControls)
  137. connect (mCbEnableAdapter, SIGNAL (toggled (bool)),
  138. mValidator, SLOT (revalidate()));
  139. connect (mCbAttachmentType, SIGNAL (activated (const QString&)),
  140. this, SLOT (updateAttachmentAlternative()));
  141. connect (mCbAdapterName, SIGNAL (activated (const QString&)),
  142. this, SLOT (updateAlternativeName()));
  143. connect (mCbAdapterName, SIGNAL (editTextChanged (const QString&)),
  144. this, SLOT (updateAlternativeName()));
  145. if (!mDisableStaticControls)
  146. mValidator->revalidate();
  147. }
  148. bool VBoxVMSettingsNetwork::revalidate (QString &aWarning, QString &aTitle)
  149. {
  150. /* 'True' for disabled adapter */
  151. if (!mCbEnableAdapter->isChecked())
  152. return true;
  153. /* Validate alternatives */
  154. bool valid = true;
  155. switch (attachmentType())
  156. {
  157. case KNetworkAttachmentType_Bridged:
  158. if (alternativeName().isNull())
  159. {
  160. aWarning = tr ("no bridged network adapter is selected");
  161. valid = false;
  162. }
  163. break;
  164. case KNetworkAttachmentType_Internal:
  165. if (alternativeName().isNull())
  166. {
  167. aWarning = tr ("no internal network name is specified");
  168. valid = false;
  169. }
  170. break;
  171. case KNetworkAttachmentType_HostOnly:
  172. if (alternativeName().isNull())
  173. {
  174. aWarning = tr ("no host-only network adapter is selected");
  175. valid = false;
  176. }
  177. break;
  178. default:
  179. break;
  180. }
  181. if (!valid)
  182. aTitle += ": " + vboxGlobal().removeAccelMark (pageTitle());
  183. return valid;
  184. }
  185. QWidget* VBoxVMSettingsNetwork::setOrderAfter (QWidget *aAfter)
  186. {
  187. setTabOrder (aAfter, mCbEnableAdapter);
  188. setTabOrder (mCbEnableAdapter, mCbAttachmentType);
  189. setTabOrder (mCbAttachmentType, mCbAdapterName);
  190. setTabOrder (mCbAdapterName, mAbsAdvanced);
  191. setTabOrder (mAbsAdvanced, mCbAdapterType);
  192. setTabOrder (mCbAdapterType, mLeMAC);
  193. setTabOrder (mLeMAC, mTbMAC);
  194. setTabOrder (mTbMAC, mCbCableConnected);
  195. return mCbCableConnected;
  196. }
  197. QString VBoxVMSettingsNetwork::pageTitle() const
  198. {
  199. QString title;
  200. if (!mAdapter.isNull())
  201. {
  202. title = VBoxGlobal::tr ("Adapter %1", "network")
  203. .arg (QString ("&%1").arg (mAdapter.GetSlot() + 1));
  204. }
  205. return title;
  206. }
  207. KNetworkAttachmentType VBoxVMSettingsNetwork::attachmentType() const
  208. {
  209. return (KNetworkAttachmentType) mCbAttachmentType->itemData (
  210. mCbAttachmentType->currentIndex()).toInt();
  211. }
  212. QString VBoxVMSettingsNetwork::alternativeName (int aType) const
  213. {
  214. if (aType == -1) aType = attachmentType();
  215. QString result;
  216. switch (aType)
  217. {
  218. case KNetworkAttachmentType_Bridged:
  219. result = mBrgName;
  220. break;
  221. case KNetworkAttachmentType_Internal:
  222. result = mIntName;
  223. break;
  224. case KNetworkAttachmentType_HostOnly:
  225. result = mHoiName;
  226. break;
  227. default:
  228. break;
  229. }
  230. Assert (result.isNull() || !result.isEmpty());
  231. return result;
  232. }
  233. void VBoxVMSettingsNetwork::showEvent (QShowEvent *aEvent)
  234. {
  235. if (!mPolished)
  236. {
  237. mPolished = true;
  238. /* Give the minimum size hint to the first layout column */
  239. mNetworkChildGridLayout->setColumnMinimumWidth (0, mLbAttachmentType->width());
  240. if (mDisableStaticControls)
  241. {
  242. /* Disable controls for dynamically displayed page */
  243. mCbEnableAdapter->setEnabled (false);
  244. mCbAdapterType->setEnabled (false);
  245. mLeMAC->setEnabled (false);
  246. mTbMAC->setEnabled (false);
  247. mLbAdapterType->setEnabled (false);
  248. mLbMAC->setEnabled (false);
  249. mAbsAdvanced->animateClick();
  250. }
  251. else
  252. {
  253. /* Hide advanced items initially */
  254. toggleAdvanced();
  255. }
  256. }
  257. QWidget::showEvent (aEvent);
  258. }
  259. void VBoxVMSettingsNetwork::retranslateUi()
  260. {
  261. /* Translate uic generated strings */
  262. Ui::VBoxVMSettingsNetwork::retranslateUi (this);
  263. /* Translate combo-boxes content */
  264. populateComboboxes();
  265. /* Translate attachment info */
  266. updateAttachmentAlternative();
  267. }
  268. void VBoxVMSettingsNetwork::updateAttachmentAlternative()
  269. {
  270. /* Blocking signals to change content manually */
  271. mCbAdapterName->blockSignals (true);
  272. /* Update alternative-name combo-box availability */
  273. mLbAdapterName->setEnabled (attachmentType() != KNetworkAttachmentType_Null &&
  274. attachmentType() != KNetworkAttachmentType_NAT);
  275. mCbAdapterName->setEnabled (attachmentType() != KNetworkAttachmentType_Null &&
  276. attachmentType() != KNetworkAttachmentType_NAT);
  277. /* Refresh list */
  278. mCbAdapterName->clear();
  279. switch (attachmentType())
  280. {
  281. case KNetworkAttachmentType_Bridged:
  282. mCbAdapterName->insertItems (0, mParent->brgList());
  283. mCbAdapterName->setEditable (false);
  284. break;
  285. case KNetworkAttachmentType_Internal:
  286. mCbAdapterName->insertItems (0, mParent->intList());
  287. mCbAdapterName->setEditable (true);
  288. mCbAdapterName->setCompleter (0);
  289. break;
  290. case KNetworkAttachmentType_HostOnly:
  291. mCbAdapterName->insertItems (0, mParent->hoiList());
  292. mCbAdapterName->setEditable (false);
  293. break;
  294. default:
  295. break;
  296. }
  297. /* Prepend 'empty' or 'default' item */
  298. if (mCbAdapterName->count() == 0)
  299. {
  300. switch (attachmentType())
  301. {
  302. case KNetworkAttachmentType_Bridged:
  303. case KNetworkAttachmentType_HostOnly:
  304. {
  305. /* Adapters list 'empty' */
  306. int pos = mCbAdapterName->findData (emptyItemCode);
  307. if (pos == -1)
  308. mCbAdapterName->insertItem (0, tr ("Not selected", "network adapter name"), emptyItemCode);
  309. else
  310. mCbAdapterName->setItemText (pos, tr ("Not selected", "network adapter name"));
  311. break;
  312. }
  313. case KNetworkAttachmentType_Internal:
  314. {
  315. /* Internal network 'default' name */
  316. if (mCbAdapterName->findText ("intnet") == -1)
  317. mCbAdapterName->insertItem (0, "intnet");
  318. break;
  319. }
  320. default:
  321. break;
  322. }
  323. }
  324. /* Select previous or default item */
  325. switch (attachmentType())
  326. {
  327. case KNetworkAttachmentType_Bridged:
  328. case KNetworkAttachmentType_HostOnly:
  329. {
  330. int pos = mCbAdapterName->findText (alternativeName());
  331. mCbAdapterName->setCurrentIndex (pos == -1 ? 0 : pos);
  332. break;
  333. }
  334. case KNetworkAttachmentType_Internal:
  335. {
  336. int pos = mCbAdapterName->findText (alternativeName());
  337. mCbAdapterName->setCurrentIndex (pos == -1 ? 0 : pos);
  338. break;
  339. }
  340. default:
  341. break;
  342. }
  343. /* Remember selected item */
  344. updateAlternativeName();
  345. /* Unblocking signals as content is changed already */
  346. mCbAdapterName->blockSignals (false);
  347. }
  348. void VBoxVMSettingsNetwork::updateAlternativeName()
  349. {
  350. switch (attachmentType())
  351. {
  352. case KNetworkAttachmentType_Bridged:
  353. {
  354. QString newName (mCbAdapterName->itemData (mCbAdapterName->currentIndex()).toString() ==
  355. QString (emptyItemCode) ||
  356. mCbAdapterName->currentText().isEmpty() ?
  357. QString::null : mCbAdapterName->currentText());
  358. if (mBrgName != newName)
  359. mBrgName = newName;
  360. break;
  361. }
  362. case KNetworkAttachmentType_Internal:
  363. {
  364. QString newName ((mCbAdapterName->itemData (mCbAdapterName->currentIndex()).toString() ==
  365. QString (emptyItemCode) &&
  366. mCbAdapterName->currentText() ==
  367. mCbAdapterName->itemText (mCbAdapterName->currentIndex())) ||
  368. mCbAdapterName->currentText().isEmpty() ?
  369. QString::null : mCbAdapterName->currentText());
  370. if (mIntName != newName)
  371. {
  372. mIntName = newName;
  373. if (!mIntName.isNull())
  374. QTimer::singleShot (0, mParent, SLOT (updatePages()));
  375. }
  376. break;
  377. }
  378. case KNetworkAttachmentType_HostOnly:
  379. {
  380. QString newName (mCbAdapterName->itemData (mCbAdapterName->currentIndex()).toString() ==
  381. QString (emptyItemCode) ||
  382. mCbAdapterName->currentText().isEmpty() ?
  383. QString::null : mCbAdapterName->currentText());
  384. if (mHoiName != newName)
  385. mHoiName = newName;
  386. break;
  387. }
  388. default:
  389. break;
  390. }
  391. if (mValidator)
  392. mValidator->revalidate();
  393. }
  394. void VBoxVMSettingsNetwork::toggleAdvanced()
  395. {
  396. mLbAdapterType->setVisible (mAbsAdvanced->isExpanded());
  397. mCbAdapterType->setVisible (mAbsAdvanced->isExpanded());
  398. mLbMAC->setVisible (mAbsAdvanced->isExpanded());
  399. mLeMAC->setVisible (mAbsAdvanced->isExpanded());
  400. mTbMAC->setVisible (mAbsAdvanced->isExpanded());
  401. mCbCableConnected->setVisible (mAbsAdvanced->isExpanded());
  402. }
  403. void VBoxVMSettingsNetwork::generateMac()
  404. {
  405. mAdapter.SetMACAddress (QString::null);
  406. mLeMAC->setText (mAdapter.GetMACAddress());
  407. }
  408. void VBoxVMSettingsNetwork::populateComboboxes()
  409. {
  410. /* Save the current selected adapter */
  411. int currentAdapter = mCbAdapterType->currentIndex();
  412. /* Clear the adapters combo-box */
  413. mCbAdapterType->clear();
  414. /* Populate adapters */
  415. mCbAdapterType->insertItem (0,
  416. vboxGlobal().toString (KNetworkAdapterType_Am79C970A));
  417. mCbAdapterType->setItemData (0,
  418. KNetworkAdapterType_Am79C970A);
  419. mCbAdapterType->setItemData (0,
  420. mCbAdapterType->itemText (0), Qt::ToolTipRole);
  421. mCbAdapterType->insertItem (1,
  422. vboxGlobal().toString (KNetworkAdapterType_Am79C973));
  423. mCbAdapterType->setItemData (1,
  424. KNetworkAdapterType_Am79C973);
  425. mCbAdapterType->setItemData (1,
  426. mCbAdapterType->itemText (1), Qt::ToolTipRole);
  427. #ifdef VBOX_WITH_E1000
  428. mCbAdapterType->insertItem (2,
  429. vboxGlobal().toString (KNetworkAdapterType_I82540EM));
  430. mCbAdapterType->setItemData (2,
  431. KNetworkAdapterType_I82540EM);
  432. mCbAdapterType->setItemData (2,
  433. mCbAdapterType->itemText (2), Qt::ToolTipRole);
  434. mCbAdapterType->insertItem (3,
  435. vboxGlobal().toString (KNetworkAdapterType_I82543GC));
  436. mCbAdapterType->setItemData (3,
  437. KNetworkAdapterType_I82543GC);
  438. mCbAdapterType->setItemData (3,
  439. mCbAdapterType->itemText (3), Qt::ToolTipRole);
  440. mCbAdapterType->insertItem (4,
  441. vboxGlobal().toString (KNetworkAdapterType_I82545EM));
  442. mCbAdapterType->setItemData (4,
  443. KNetworkAdapterType_I82545EM);
  444. mCbAdapterType->setItemData (4,
  445. mCbAdapterType->itemText (4), Qt::ToolTipRole);
  446. #endif /* VBOX_WITH_E1000 */
  447. #ifdef VBOX_WITH_VIRTIO
  448. mCbAdapterType->insertItem (5,
  449. vboxGlobal().toString (KNetworkAdapterType_Virtio));
  450. mCbAdapterType->setItemData (5,
  451. KNetworkAdapterType_Virtio);
  452. mCbAdapterType->setItemData (5,
  453. mCbAdapterType->itemText (5), Qt::ToolTipRole);
  454. #endif /* VBOX_WITH_VIRTIO */
  455. /* Set the old value */
  456. mCbAdapterType->setCurrentIndex (currentAdapter == -1 ?
  457. 0 : currentAdapter);
  458. /* Save the current selected attachment type */
  459. int currentAttachment = mCbAttachmentType->currentIndex();
  460. /* Clear the attachments combo-box */
  461. mCbAttachmentType->clear();
  462. /* Populate attachments */
  463. mCbAttachmentType->insertItem (0,
  464. vboxGlobal().toString (KNetworkAttachmentType_Null));
  465. mCbAttachmentType->setItemData (0,
  466. KNetworkAttachmentType_Null);
  467. mCbAttachmentType->setItemData (0,
  468. mCbAttachmentType->itemText (0), Qt::ToolTipRole);
  469. mCbAttachmentType->insertItem (1,
  470. vboxGlobal().toString (KNetworkAttachmentType_NAT));
  471. mCbAttachmentType->setItemData (1,
  472. KNetworkAttachmentType_NAT);
  473. mCbAttachmentType->setItemData (1,
  474. mCbAttachmentType->itemText (1), Qt::ToolTipRole);
  475. mCbAttachmentType->insertItem (2,
  476. vboxGlobal().toString (KNetworkAttachmentType_Bridged));
  477. mCbAttachmentType->setItemData (2,
  478. KNetworkAttachmentType_Bridged);
  479. mCbAttachmentType->setItemData (2,
  480. mCbAttachmentType->itemText (2), Qt::ToolTipRole);
  481. mCbAttachmentType->insertItem (3,
  482. vboxGlobal().toString (KNetworkAttachmentType_Internal));
  483. mCbAttachmentType->setItemData (3,
  484. KNetworkAttachmentType_Internal);
  485. mCbAttachmentType->setItemData (3,
  486. mCbAttachmentType->itemText (3), Qt::ToolTipRole);
  487. mCbAttachmentType->insertItem (4,
  488. vboxGlobal().toString (KNetworkAttachmentType_HostOnly));
  489. mCbAttachmentType->setItemData (4,
  490. KNetworkAttachmentType_HostOnly);
  491. mCbAttachmentType->setItemData (4,
  492. mCbAttachmentType->itemText (4), Qt::ToolTipRole);
  493. /* Set the old value */
  494. mCbAttachmentType->setCurrentIndex (currentAttachment);
  495. }
  496. /* VBoxVMSettingsNetworkPage Stuff */
  497. VBoxVMSettingsNetworkPage::VBoxVMSettingsNetworkPage(bool aDisableStaticControls)
  498. : mValidator (0)
  499. , mDisableStaticControls (false)
  500. {
  501. /* Setup Main Layout */
  502. QVBoxLayout *mainLayout = new QVBoxLayout (this);
  503. mainLayout->setContentsMargins (0, 5, 0, 5);
  504. /* Creating Tab Widget */
  505. mTwAdapters = new QTabWidget (this);
  506. mainLayout->addWidget (mTwAdapters);
  507. /* If some controls should be disabled or not when the
  508. * same tab widgets are shown during runtime
  509. */
  510. mDisableStaticControls = aDisableStaticControls;
  511. }
  512. QStringList VBoxVMSettingsNetworkPage::brgList (bool aRefresh)
  513. {
  514. if (aRefresh)
  515. {
  516. /* Load & filter interface list */
  517. mBrgList.clear();
  518. CHostNetworkInterfaceVector interfaces =
  519. vboxGlobal().virtualBox().GetHost().GetNetworkInterfaces();
  520. for (CHostNetworkInterfaceVector::ConstIterator it = interfaces.begin();
  521. it != interfaces.end(); ++ it)
  522. {
  523. if (it->GetInterfaceType() == KHostNetworkInterfaceType_Bridged)
  524. mBrgList << it->GetName();
  525. }
  526. }
  527. return mBrgList;
  528. }
  529. QStringList VBoxVMSettingsNetworkPage::intList (bool aRefresh)
  530. {
  531. if (aRefresh)
  532. {
  533. /* Load total network list of all VMs */
  534. mIntList.clear();
  535. CVirtualBox vbox = vboxGlobal().virtualBox();
  536. ulong count = qMin ((ULONG) 4, vbox.GetSystemProperties().GetNetworkAdapterCount());
  537. CMachineVector vec = vbox.GetMachines();
  538. for (CMachineVector::ConstIterator m = vec.begin(); m != vec.end(); ++ m)
  539. {
  540. if (m->GetAccessible())
  541. {
  542. for (ulong slot = 0; slot < count; ++ slot)
  543. {
  544. QString name = m->GetNetworkAdapter (slot).GetInternalNetwork();
  545. if (!name.isEmpty() && !mIntList.contains (name))
  546. mIntList << name;
  547. }
  548. }
  549. }
  550. }
  551. /* Append network list with names from all the pages */
  552. QStringList list (mIntList);
  553. for (int index = 0; index < mTwAdapters->count(); ++ index)
  554. {
  555. VBoxVMSettingsNetwork *page =
  556. qobject_cast <VBoxVMSettingsNetwork*> (mTwAdapters->widget (index));
  557. if (page)
  558. {
  559. QString name = page->alternativeName (KNetworkAttachmentType_Internal);
  560. if (!name.isEmpty() && !list.contains (name))
  561. list << name;
  562. }
  563. }
  564. return list;
  565. }
  566. QStringList VBoxVMSettingsNetworkPage::hoiList (bool aRefresh)
  567. {
  568. if (aRefresh)
  569. {
  570. /* Load & filter interface list */
  571. mHoiList.clear();
  572. CHostNetworkInterfaceVector interfaces =
  573. vboxGlobal().virtualBox().GetHost().GetNetworkInterfaces();
  574. for (CHostNetworkInterfaceVector::ConstIterator it = interfaces.begin();
  575. it != interfaces.end(); ++ it)
  576. {
  577. if (it->GetInterfaceType() == KHostNetworkInterfaceType_HostOnly)
  578. mHoiList << it->GetName();
  579. }
  580. }
  581. return mHoiList;
  582. }
  583. void VBoxVMSettingsNetworkPage::getFrom (const CMachine &aMachine)
  584. {
  585. /* Setup tab order */
  586. Assert (mFirstWidget);
  587. setTabOrder (mFirstWidget, mTwAdapters->focusProxy());
  588. QWidget *lastFocusWidget = mTwAdapters->focusProxy();
  589. /* Cache data */
  590. brgList (true);
  591. intList (true);
  592. hoiList (true);
  593. /* Creating Tab Pages */
  594. CVirtualBox vbox = vboxGlobal().virtualBox();
  595. ulong count = qMin ((ULONG) 4, vbox.GetSystemProperties().GetNetworkAdapterCount());
  596. for (ulong slot = 0; slot < count; ++ slot)
  597. {
  598. /* Get Adapter */
  599. CNetworkAdapter adapter = aMachine.GetNetworkAdapter (slot);
  600. /* Creating Adapter's page */
  601. VBoxVMSettingsNetwork *page = new VBoxVMSettingsNetwork (this, mDisableStaticControls);
  602. /* Loading Adapter's data into page */
  603. page->getFromAdapter (adapter);
  604. /* Attach Adapter's page to Tab Widget */
  605. mTwAdapters->addTab (page, page->pageTitle());
  606. /* Disable tab page if adapter is being configured dynamically */
  607. if (mDisableStaticControls && !adapter.GetEnabled())
  608. mTwAdapters->setTabEnabled(slot, false);
  609. /* Setup validation */
  610. page->setValidator (mValidator);
  611. /* Setup tab order */
  612. lastFocusWidget = page->setOrderAfter (lastFocusWidget);
  613. }
  614. /* Applying language settings */
  615. retranslateUi();
  616. }
  617. void VBoxVMSettingsNetworkPage::putBackTo()
  618. {
  619. for (int i = 0; i < mTwAdapters->count(); ++ i)
  620. {
  621. VBoxVMSettingsNetwork *page =
  622. qobject_cast <VBoxVMSettingsNetwork*> (mTwAdapters->widget (i));
  623. Assert (page);
  624. page->putBackToAdapter();
  625. }
  626. }
  627. void VBoxVMSettingsNetworkPage::setValidator (QIWidgetValidator *aVal)
  628. {
  629. mValidator = aVal;
  630. }
  631. bool VBoxVMSettingsNetworkPage::revalidate (QString &aWarning, QString &aTitle)
  632. {
  633. bool valid = true;
  634. for (int i = 0; i < mTwAdapters->count(); ++ i)
  635. {
  636. VBoxVMSettingsNetwork *page =
  637. qobject_cast <VBoxVMSettingsNetwork*> (mTwAdapters->widget (i));
  638. Assert (page);
  639. valid = page->revalidate (aWarning, aTitle);
  640. if (!valid) break;
  641. }
  642. return valid;
  643. }
  644. void VBoxVMSettingsNetworkPage::retranslateUi()
  645. {
  646. for (int i = 0; i < mTwAdapters->count(); ++ i)
  647. {
  648. VBoxVMSettingsNetwork *page =
  649. qobject_cast <VBoxVMSettingsNetwork*> (mTwAdapters->widget (i));
  650. Assert (page);
  651. mTwAdapters->setTabText (i, page->pageTitle());
  652. }
  653. }
  654. void VBoxVMSettingsNetworkPage::updatePages()
  655. {
  656. for (int i = 0; i < mTwAdapters->count(); ++ i)
  657. {
  658. /* Get the iterated page */
  659. VBoxVMSettingsNetwork *page =
  660. qobject_cast <VBoxVMSettingsNetwork*> (mTwAdapters->widget (i));
  661. Assert (page);
  662. /* Update the page if the attachment type is 'internal network' */
  663. if (page->attachmentType() == KNetworkAttachmentType_Internal)
  664. QTimer::singleShot (0, page, SLOT (updateAttachmentAlternative()));
  665. }
  666. }