VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/ui/VBoxVMNetworkSettings.ui.h@ 2394

Last change on this file since 2394 was 2394, checked in by vboxsync, 18 years ago

1942: Improve automatic naming for "Add New..." Uis

Feature implemented as designed:
Search algorithm will find the last created default named interface (filter) and will use its index+1 as a starting index for the newly created ones.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.0 KB
Line 
1/**
2 *
3 * VBox frontends: Qt GUI ("VirtualBox"):
4 * "VM network settings" dialog UI include (Qt Designer)
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.215389.xyz. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23/****************************************************************************
24** ui.h extension file, included from the uic-generated form implementation.
25**
26** If you wish to add, delete or rename functions or slots use
27** Qt Designer which will update this file, preserving your code. Create an
28** init() function in place of a constructor, and a destroy() function in
29** place of a destructor.
30*****************************************************************************/
31
32/**
33 * QDialog class reimplementation to use for adding network interface.
34 * It has one line-edit field for entering network interface's name and
35 * common dialog's ok/cancel buttons.
36 */
37#if defined Q_WS_WIN
38class VBoxAddNIDialog : public QDialog
39{
40 Q_OBJECT
41
42public:
43
44 VBoxAddNIDialog (QWidget *aParent, const QString &aIfaceName) :
45 QDialog (aParent, "VBoxAddNIDialog", true /* modal */),
46 mLeName (0)
47 {
48 setCaption (tr ("Add Host Interface"));
49 QVBoxLayout *mainLayout = new QVBoxLayout (this, 10, 10, "mainLayout");
50
51 /* Setup Input layout */
52 QHBoxLayout *inputLayout = new QHBoxLayout (mainLayout, 10, "inputLayout");
53 QLabel *lbName = new QLabel (tr ("Interface Name"), this);
54 mLeName = new QLineEdit (aIfaceName, this);
55 QWhatsThis::add (mLeName, tr ("Descriptive name of the new network interface"));
56 inputLayout->addWidget (lbName);
57 inputLayout->addWidget (mLeName);
58 connect (mLeName, SIGNAL (textChanged (const QString &)),
59 this, SLOT (validate()));
60
61 /* Setup Button layout */
62 QHBoxLayout *buttonLayout = new QHBoxLayout (mainLayout, 10, "buttonLayout");
63 mBtOk = new QPushButton (tr ("&OK"), this, "mBtOk");
64 QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
65 QPushButton *btCancel = new QPushButton (tr ("Cancel"), this, "btCancel");
66 connect (mBtOk, SIGNAL (clicked()), this, SLOT (accept()));
67 connect (btCancel, SIGNAL (clicked()), this, SLOT (reject()));
68 buttonLayout->addWidget (mBtOk);
69 buttonLayout->addItem (spacer);
70 buttonLayout->addWidget (btCancel);
71
72 /* Validate interface name field */
73 validate();
74 }
75
76 ~VBoxAddNIDialog() {}
77
78 QString getName() { return mLeName->text(); }
79
80private slots:
81
82 void validate()
83 {
84 mBtOk->setEnabled (!mLeName->text().isEmpty());
85 }
86
87private:
88
89 void showEvent (QShowEvent *aEvent)
90 {
91 setFixedHeight (height());
92 QDialog::showEvent (aEvent);
93 }
94
95 QPushButton *mBtOk;
96 QLineEdit *mLeName;
97};
98#endif
99
100
101/**
102 * VBoxVMNetworkSettings class to use as network interface setup page.
103 */
104void VBoxVMNetworkSettings::init()
105{
106 mInterfaceNumber = 0;
107 mNoInterfaces = tr ("<No suitable interfaces>");
108
109 leMACAddress->setValidator (new QRegExpValidator (QRegExp ("[0-9,A-F]{12,12}"), this));
110
111 cbNetworkAttachment->insertItem (vboxGlobal().toString (CEnums::NoNetworkAttachment));
112 cbNetworkAttachment->insertItem (vboxGlobal().toString (CEnums::NATNetworkAttachment));
113#ifndef Q_WS_MAC /* not yet on the Mac */
114 cbNetworkAttachment->insertItem (vboxGlobal().toString (CEnums::HostInterfaceNetworkAttachment));
115 cbNetworkAttachment->insertItem (vboxGlobal().toString (CEnums::InternalNetworkAttachment));
116#endif
117
118#if defined Q_WS_X11
119 leTAPDescriptor->setValidator (new QIntValidator (-1, std::numeric_limits <LONG>::max(), this));
120#else
121 /* hide unavailable settings (TAP setup and terminate apps) */
122 frmTAPSetupTerminate->setHidden (true);
123 /* disable unused interface name UI */
124 frmHostInterface_X11->setHidden (true);
125#endif
126
127#if defined Q_WS_WIN
128 /* setup iconsets */
129 pbHostAdd->setIconSet (VBoxGlobal::iconSet ("add_host_iface_16px.png",
130 "add_host_iface_disabled_16px.png"));
131 pbHostRemove->setIconSet (VBoxGlobal::iconSet ("remove_host_iface_16px.png",
132 "remove_host_iface_disabled_16px.png"));
133 /* setup languages */
134 QToolTip::add (pbHostAdd, tr ("Add"));
135 QToolTip::add (pbHostRemove, tr ("Remove"));
136 /* setup connections */
137 connect (grbEnabled, SIGNAL (toggled (bool)),
138 this, SLOT (grbEnabledToggled (bool)));
139#else
140 /* disable unused interface name UI */
141 frmHostInterface_WIN->setHidden (true);
142 /* setup iconsets */
143 pbTAPSetup->setIconSet (VBoxGlobal::iconSet ("select_file_16px.png",
144 "select_file_dis_16px.png"));
145 pbTAPTerminate->setIconSet (VBoxGlobal::iconSet ("select_file_16px.png",
146 "select_file_dis_16px.png"));
147#endif
148
149 /* the TAP file descriptor setting is always invisible -- currently not used
150 * (remove the relative code at all? -- just leave for some time...) */
151 frmTAPDescriptor->setHidden (true);
152
153#if defined Q_WS_MAC
154 /* no Host Interface Networking on the Mac yet */
155 grbTAP->setHidden (true);
156#endif
157}
158
159bool VBoxVMNetworkSettings::isPageValid (const QStringList &aList)
160{
161#if defined Q_WS_WIN
162 CEnums::NetworkAttachmentType type =
163 vboxGlobal().toNetworkAttachmentType (cbNetworkAttachment->currentText());
164
165 return !(type == CEnums::HostInterfaceNetworkAttachment &&
166 isInterfaceInvalid (aList, leHostInterfaceName->text()));
167#else
168 NOREF (aList);
169 return true;
170#endif
171}
172
173void VBoxVMNetworkSettings::loadList (const QStringList &aList,
174 int aInterfaceNumber)
175{
176#if defined Q_WS_WIN
177 mInterfaceNumber = aInterfaceNumber;
178 /* save current list item name */
179 QString currentListItemName = leHostInterfaceName->text();
180 /* load current list items */
181 lbHostInterface->clearFocus();
182 lbHostInterface->clear();
183 if (aList.count())
184 lbHostInterface->insertStringList (aList);
185 else
186 lbHostInterface->insertItem (mNoInterfaces);
187 selectListItem (currentListItemName);
188 /* disable interface delete button */
189 pbHostRemove->setEnabled (!aList.isEmpty());
190#else
191 NOREF (aList);
192 NOREF (aInterfaceNumber);
193#endif
194}
195
196void VBoxVMNetworkSettings::getFromAdapter (const CNetworkAdapter &adapter)
197{
198 cadapter = adapter;
199
200 grbEnabled->setChecked (adapter.GetEnabled());
201
202 CEnums::NetworkAttachmentType type = adapter.GetAttachmentType();
203 cbNetworkAttachment->setCurrentItem (0);
204 for (int i = 0; i < cbNetworkAttachment->count(); i ++)
205 if (vboxGlobal().toNetworkAttachmentType (cbNetworkAttachment->text (i)) == type)
206 {
207 cbNetworkAttachment->setCurrentItem (i);
208 cbNetworkAttachment_activated (cbNetworkAttachment->currentText());
209 break;
210 }
211
212 leMACAddress->setText (adapter.GetMACAddress());
213
214 chbCableConnected->setChecked (adapter.GetCableConnected());
215
216#if defined Q_WS_WIN
217 selectListItem (adapter.GetHostInterface());
218#else
219 leHostInterface->setText (adapter.GetHostInterface());
220#endif
221
222#if defined Q_WS_X11
223 leTAPDescriptor->setText (QString::number (adapter.GetTAPFileDescriptor()));
224 leTAPSetup->setText (adapter.GetTAPSetupApplication());
225 leTAPTerminate->setText (adapter.GetTAPTerminateApplication());
226#endif
227}
228
229void VBoxVMNetworkSettings::putBackToAdapter()
230{
231 cadapter.SetEnabled (grbEnabled->isChecked());
232
233 CEnums::NetworkAttachmentType type =
234 vboxGlobal().toNetworkAttachmentType (cbNetworkAttachment->currentText());
235 switch (type)
236 {
237 case CEnums::NoNetworkAttachment:
238 cadapter.Detach();
239 break;
240 case CEnums::NATNetworkAttachment:
241 cadapter.AttachToNAT();
242 break;
243 case CEnums::HostInterfaceNetworkAttachment:
244 cadapter.AttachToHostInterface();
245 break;
246 case CEnums::InternalNetworkAttachment:
247 cadapter.AttachToInternalNetwork();
248 break;
249 default:
250 AssertMsgFailed (("Invalid network attachment type: %d", type));
251 break;
252 }
253
254 cadapter.SetMACAddress (leMACAddress->text());
255
256 cadapter.SetCableConnected (chbCableConnected->isChecked());
257
258 if (type == CEnums::HostInterfaceNetworkAttachment)
259 {
260#if defined Q_WS_WIN
261 if (!lbHostInterface->currentText().isEmpty())
262 cadapter.SetHostInterface (lbHostInterface->currentText());
263#else
264 QString iface = leHostInterface->text();
265 cadapter.SetHostInterface (iface.isEmpty() ? QString::null : iface);
266#endif
267#if defined Q_WS_X11
268 cadapter.SetTAPFileDescriptor (leTAPDescriptor->text().toLong());
269 QString setup = leTAPSetup->text();
270 cadapter.SetTAPSetupApplication (setup.isEmpty() ? QString::null : setup);
271 QString term = leTAPTerminate->text();
272 cadapter.SetTAPTerminateApplication (term.isEmpty() ? QString::null : term);
273#endif
274 }
275}
276
277void VBoxVMNetworkSettings::setValidator (QIWidgetValidator *aWalidator)
278{
279 mWalidator = aWalidator;
280}
281
282void VBoxVMNetworkSettings::revalidate()
283{
284 mWalidator->revalidate();
285}
286
287void VBoxVMNetworkSettings::grbEnabledToggled (bool aOn)
288{
289#if defined Q_WS_WIN
290 if (!aOn)
291 {
292 lbHostInterface->clearFocus();
293 cbNetworkAttachment->setCurrentItem (0);
294 cbNetworkAttachment_activated (cbNetworkAttachment->currentText());
295 if (lbHostInterface->selectedItem())
296 lbHostInterface->setSelected (lbHostInterface->selectedItem(), false);
297 }
298 if (lbHostInterface->currentItem() != -1)
299 lbHostInterface->setSelected (lbHostInterface->currentItem(), aOn);
300#else
301 NOREF (aOn);
302#endif
303}
304
305void VBoxVMNetworkSettings::selectListItem (const QString &aItemName)
306{
307 if (!aItemName.isEmpty())
308 {
309#if defined Q_WS_WIN
310 leHostInterfaceName->setText (aItemName);
311 QListBoxItem* adapterNode = lbHostInterface->findItem (aItemName);
312 if (adapterNode)
313 {
314 lbHostInterface->setCurrentItem (adapterNode);
315 lbHostInterface->setSelected (adapterNode, true);
316 }
317#endif
318 }
319}
320
321void VBoxVMNetworkSettings::cbNetworkAttachment_activated (const QString &aString)
322{
323#if defined Q_WS_WIN
324 bool enableHostIf = vboxGlobal().toNetworkAttachmentType (aString) ==
325 CEnums::HostInterfaceNetworkAttachment;
326 txHostInterface_WIN->setEnabled (enableHostIf);
327 leHostInterfaceName->setEnabled (enableHostIf);
328 lbHostInterface_highlighted (lbHostInterface->selectedItem());
329#else
330 NOREF (aString);
331#endif
332}
333
334void VBoxVMNetworkSettings::lbHostInterface_highlighted (QListBoxItem *aItem)
335{
336 if (!aItem) return;
337#if defined Q_WS_WIN
338 leHostInterfaceName->setText (leHostInterfaceName->isEnabled() ?
339 aItem->text() : QString::null);
340 if (!lbHostInterface->isSelected (aItem))
341 lbHostInterface->setSelected (aItem, true);
342#endif
343}
344
345bool VBoxVMNetworkSettings::isInterfaceInvalid (const QStringList &aList,
346 const QString &aIface)
347{
348#if defined Q_WS_WIN
349 return aList.find (aIface) == aList.end();
350#else
351 NOREF (aList);
352 NOREF (aIface);
353 return false;
354#endif
355}
356
357void VBoxVMNetworkSettings::pbGenerateMAC_clicked()
358{
359 cadapter.SetMACAddress (QString::null);
360 leMACAddress->setText (cadapter.GetMACAddress());
361}
362
363void VBoxVMNetworkSettings::pbTAPSetup_clicked()
364{
365 QString selected = QFileDialog::getOpenFileName (
366 "/",
367 QString::null,
368 this,
369 NULL,
370 tr ("Select TAP setup application"));
371
372 if (selected)
373 leTAPSetup->setText (selected);
374}
375
376void VBoxVMNetworkSettings::pbTAPTerminate_clicked()
377{
378 QString selected = QFileDialog::getOpenFileName (
379 "/",
380 QString::null,
381 this,
382 NULL,
383 tr ("Select TAP terminate application"));
384
385 if (selected)
386 leTAPTerminate->setText (selected);
387}
388
389void VBoxVMNetworkSettings::hostInterfaceAdd()
390{
391#if defined Q_WS_WIN
392
393 /* allow the started helper process to make itself the foreground window */
394 AllowSetForegroundWindow (ASFW_ANY);
395
396 /* creating add host interface dialog */
397 VBoxAddNIDialog dlg (this, lbHostInterface->currentItem() != -1 ?
398 tr ("VirtualBox Host Interface %1").arg (mInterfaceNumber) :
399 leHostInterfaceName->text());
400 if (dlg.exec() != QDialog::Accepted)
401 return;
402 QString iName = dlg.getName();
403
404 /* create interface */
405 CHost host = vboxGlobal().virtualBox().GetHost();
406 CHostNetworkInterface iFace;
407 CProgress progress = host.CreateHostNetworkInterface (iName, iFace);
408 if (host.isOk())
409 {
410 vboxProblem().showModalProgressDialog (progress, iName, this);
411 if (progress.GetResultCode() == 0)
412 {
413 ++ mInterfaceNumber;
414 /* add&select newly created created interface */
415 delete lbHostInterface->findItem (mNoInterfaces);
416 lbHostInterface->insertItem (iName);
417 selectListItem (iName);
418 emit listChanged (this);
419 }
420 else
421 vboxProblem().cannotCreateHostInterface (progress, iName, this);
422 }
423 else
424 vboxProblem().cannotCreateHostInterface (host, iName, this);
425
426 /* allow the started helper process to make itself the foreground window */
427 AllowSetForegroundWindow (ASFW_ANY);
428
429#endif
430}
431
432void VBoxVMNetworkSettings::hostInterfaceRemove()
433{
434#if defined Q_WS_WIN
435
436 /* allow the started helper process to make itself the foreground window */
437 AllowSetForegroundWindow (ASFW_ANY);
438
439 /* check interface name */
440 QString iName = lbHostInterface->currentText();
441 if (iName.isEmpty())
442 return;
443
444 /* asking user about deleting selected network interface */
445 int delNetIface = vboxProblem().message (this, VBoxProblemReporter::Question,
446 tr ("<p>Do you want to remove the selected host network interface "
447 "<nobr><b>%1</b>?</nobr></p>"
448 "<p><b>Note:</b> This interface may be in use by one or more "
449 "network adapters of this or another VM. After it is removed, these "
450 "adapters will no longer work until you correct their settings by "
451 "either choosing a differnet interface name or a different adapter "
452 "attachment type.</p>").arg (iName),
453 0, /* autoConfirmId */
454 QIMessageBox::Ok | QIMessageBox::Default,
455 QIMessageBox::Cancel | QIMessageBox::Escape);
456 if (delNetIface == QIMessageBox::Cancel)
457 return;
458
459 CHost host = vboxGlobal().virtualBox().GetHost();
460 CHostNetworkInterface iFace = host.GetNetworkInterfaces().FindByName (iName);
461 if (host.isOk())
462 {
463 /* delete interface */
464 CProgress progress = host.RemoveHostNetworkInterface (iFace.GetId(), iFace);
465 if (host.isOk())
466 {
467 vboxProblem().showModalProgressDialog (progress, iName, this);
468 if (progress.GetResultCode() == 0)
469 {
470 if (lbHostInterface->count() == 1)
471 lbHostInterface->insertItem (mNoInterfaces);
472 delete lbHostInterface->findItem (iName);
473 emit listChanged (this);
474 }
475 else
476 vboxProblem().cannotRemoveHostInterface (progress, iFace, this);
477 }
478 }
479
480 if (!host.isOk())
481 vboxProblem().cannotRemoveHostInterface (host, iFace, this);
482#endif
483}
484
485#if defined Q_WS_WIN
486#include "VBoxVMNetworkSettings.ui.moc"
487#endif
Note: See TracBrowser for help on using the repository browser.

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