Changeset 66644 in vbox
- Timestamp:
- Apr 21, 2017 12:07:50 PM (8 years ago)
- svn:sync-xref-src-repo-rev:
- 114699
- Location:
- trunk/src/VBox/Frontends/VirtualBox/src/settings/global
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/settings/global/UIGlobalSettingsNetwork.cpp
r66626 r66644 599 599 void UIGlobalSettingsNetwork::sltAddNetworkNAT() 600 600 { 601 /* Prepare useful variables: */ 602 CVirtualBox vbox = vboxGlobal().virtualBox(); 603 604 /* Compose a pool of busy names: */ 605 QList<QString> names; 606 for (int iItemIndex = 0; iItemIndex < m_pTreeNetworkNAT->topLevelItemCount(); ++iItemIndex) 607 { 608 UIItemNetworkNAT *pItem = static_cast<UIItemNetworkNAT*>(m_pTreeNetworkNAT->topLevelItem(iItemIndex)); 609 if (!names.contains(pItem->name())) 610 names << pItem->name(); 611 } 612 /* Search for the name with maximum index: */ 613 int iMaximumIndex = -1; 601 /* Compose a set of busy names: */ 602 QSet<QString> names; 603 for (int i = 0; i < m_pTreeNetworkNAT->topLevelItemCount(); ++i) 604 names << static_cast<UIItemNetworkNAT*>(m_pTreeNetworkNAT->topLevelItem(i))->name(); 605 /* Compose a map of busy indexes: */ 606 QMap<int, bool> presence; 614 607 const QString strNameTemplate("NatNetwork%1"); 608 const QRegExp regExp(strNameTemplate.arg("([\\d]*)")); 615 609 foreach (const QString &strName, names) 616 {617 const QRegExp regExp(strNameTemplate.arg("([\\d]*)"));618 610 if (regExp.indexIn(strName) != -1) 619 iMaximumIndex = qMax(iMaximumIndex, regExp.cap(1).toInt()); 620 } 621 622 /* Create NAT network: */ 623 const QString strIndex(iMaximumIndex == -1 ? QString() : QString::number(iMaximumIndex + 1)); 624 const CNATNetwork network = vbox.CreateNATNetwork(strNameTemplate.arg(strIndex)); 625 if (!vbox.isOk()) 626 return msgCenter().cannotCreateNATNetwork(vbox, this); 627 AssertReturnVoid(!network.isNull()); 628 629 /* Update tree: */ 630 const QString strCacheKey = network.GetNetworkName(); 631 loadToCacheFromNetworkNAT(network, m_pCache->child1(strCacheKey)); 632 createTreeWidgetItemForNetworkNAT(m_pCache->child1(strCacheKey), true); 611 presence[regExp.cap(1).toInt()] = true; 612 /* Search for a minimum index: */ 613 int iMinimumIndex = 0; 614 for (int i = 0; !presence.isEmpty() && i <= presence.lastKey() + 1; ++i) 615 if (!presence.contains(i)) 616 { 617 iMinimumIndex = i; 618 break; 619 } 620 621 /* Compose resulting index and name: */ 622 const QString strNetworkIndex(iMinimumIndex == 0 ? QString() : QString::number(iMinimumIndex)); 623 const QString strNetworkName = strNameTemplate.arg(strNetworkIndex); 624 625 /* Compose new item data: */ 626 UIDataSettingsGlobalNetworkNAT data; 627 data.m_fEnabled = true; 628 data.m_strName = strNetworkName; 629 data.m_strNewName = strNetworkName; 630 data.m_strCIDR = "10.0.2.0/24"; 631 data.m_fSupportsDHCP = true; 632 633 /* Create tree-widget item: */ 634 createTreeWidgetItemForNetworkNAT(data, UIPortForwardingDataList(), UIPortForwardingDataList(), true); 633 635 m_pTreeNetworkNAT->sortByColumn(1, Qt::AscendingOrder); 634 636 } … … 638 640 /* Get network item: */ 639 641 UIItemNetworkNAT *pItem = static_cast<UIItemNetworkNAT*>(m_pTreeNetworkNAT->currentItem()); 640 Assert Msg(pItem, ("Current item should present!\n"));642 AssertPtrReturnVoid(pItem); 641 643 642 644 /* Edit current item data: */ … … 662 664 /* Get network item: */ 663 665 UIItemNetworkNAT *pItem = static_cast<UIItemNetworkNAT*>(m_pTreeNetworkNAT->currentItem()); 664 AssertMsg(pItem, ("Current item should present!\n")); 665 /* Get network name: */ 666 const QString strNetworkName(pItem->name()); 667 668 /* Confirm NAT network removal: */ 669 if (!msgCenter().confirmNATNetworkRemoval(strNetworkName, this)) 666 AssertPtrReturnVoid(pItem); 667 668 /* Confirm network removal: */ 669 if (!msgCenter().confirmNATNetworkRemoval(pItem->name(), this)) 670 670 return; 671 671 672 /* Prepare useful variables: */ 673 CVirtualBox vbox = vboxGlobal().virtualBox(); 674 675 /* Find corresponding interface: */ 676 const CNATNetwork &network = vbox.FindNATNetworkByName(strNetworkName); 677 AssertReturnVoid(vbox.isOk() && !network.isNull()); 678 679 /* Remove NAT network: */ 680 vbox.RemoveNATNetwork(network); 681 if (!vbox.isOk()) 682 return msgCenter().cannotRemoveNATNetwork(vbox, strNetworkName, this); 683 684 /* Update tree: */ 672 /* Remove tree-widget item: */ 685 673 removeTreeWidgetItemOfNetworkNAT(pItem); 686 674 } … … 1019 1007 if (fSuccess && m_pCache->wasChanged()) 1020 1008 { 1021 /* Save new network data from the cache: */ 1009 /* For each NAT network ('removing' step): */ 1010 // We need to separately remove NAT networks first because 1011 // there could be name collisions with the existing NAT networks. 1022 1012 for (int i = 0; fSuccess && i < m_pCache->childCount1(); ++i) 1023 1013 { 1014 /* Get NAT network cache: */ 1024 1015 const UISettingsCacheGlobalNetworkNAT &cache = m_pCache->child1(i); 1025 if (cache.wasUpdated()) 1026 fSuccess = saveDataNetworkNAT(cache); 1027 } 1016 1017 /* Remove NAT network marked for 'remove' or 'update' (if it can't be updated): */ 1018 if (cache.wasRemoved() || (cache.wasUpdated() && !isNetworkCouldBeUpdated(cache))) 1019 fSuccess = removeNetworkNAT(cache); 1020 } 1021 /* For each NAT network ('creating' step): */ 1022 for (int i = 0; fSuccess && i < m_pCache->childCount1(); ++i) 1023 { 1024 /* Get NAT network cache: */ 1025 const UISettingsCacheGlobalNetworkNAT &cache = m_pCache->child1(i); 1026 1027 /* Create NAT network marked for 'create' or 'update' (if it can't be updated): */ 1028 if (cache.wasCreated() || (cache.wasUpdated() && !isNetworkCouldBeUpdated(cache))) 1029 fSuccess = createNetworkNAT(cache); 1030 1031 else 1032 1033 /* Update NAT network marked for 'update' (if it can be updated): */ 1034 if (cache.wasUpdated() && isNetworkCouldBeUpdated(cache)) 1035 fSuccess = updateNetworkNAT(cache); 1036 } 1037 1038 /* For each Host network ('updating' step): */ 1028 1039 for (int i = 0; fSuccess && i < m_pCache->childCount2(); ++i) 1029 1040 { … … 1101 1112 } 1102 1113 1103 bool UIGlobalSettingsNetwork:: saveDataNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache)1114 bool UIGlobalSettingsNetwork::removeNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache) 1104 1115 { 1105 1116 /* Prepare result: */ … … 1111 1122 const UIDataSettingsGlobalNetworkNAT &oldNatData = cache.base(); 1112 1123 /* Get new NAT data from the cache: */ 1113 const UIDataSettingsGlobalNetworkNAT &newNatData = cache.data();1124 //const UIDataSettingsGlobalNetworkNAT &newNatData = cache.data(); 1114 1125 1115 1126 /* Get VBox for further activities: */ 1116 1127 CVirtualBox comVBox = vboxGlobal().virtualBox(); 1117 /* Search for a NAT network with the same name: */ 1118 CNATNetwork comNetwork = comVBox.FindNATNetworkByName(newNatData.m_strName); 1128 /* Search for a NAT network with required name: */ 1129 CNATNetwork comNetwork = comVBox.FindNATNetworkByName(oldNatData.m_strName); 1130 fSuccess = comVBox.isOk() && comNetwork.isNotNull(); 1131 1132 /* Remove NAT network: */ 1133 if (fSuccess) 1134 { 1135 comVBox.RemoveNATNetwork(comNetwork); 1136 fSuccess = comVBox.isOk(); 1137 } 1138 1139 /* Show error message if necessary: */ 1140 if (!fSuccess) 1141 notifyOperationProgressError(UIMessageCenter::formatErrorInfo(comVBox)); 1142 } 1143 /* Return result: */ 1144 return fSuccess; 1145 } 1146 1147 bool UIGlobalSettingsNetwork::createNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache) 1148 { 1149 /* Prepare result: */ 1150 bool fSuccess = true; 1151 /* Save NAT settings from the cache: */ 1152 if (fSuccess) 1153 { 1154 /* Get old NAT data from the cache: */ 1155 //const UIDataSettingsGlobalNetworkNAT &oldNatData = cache.base(); 1156 /* Get new NAT data from the cache: */ 1157 const UIDataSettingsGlobalNetworkNAT &newNatData = cache.data(); 1158 1159 /* Get VBox for further activities: */ 1160 CVirtualBox comVBox = vboxGlobal().virtualBox(); 1161 /* Create NAT network with required name: */ 1162 CNATNetwork comNetwork = comVBox.CreateNATNetwork(newNatData.m_strNewName); 1119 1163 fSuccess = comVBox.isOk() && comNetwork.isNotNull(); 1120 1164 … … 1125 1169 { 1126 1170 /* Save whether NAT network is enabled: */ 1127 if (fSuccess && newNatData.m_fEnabled != oldNatData.m_fEnabled)1171 if (fSuccess) 1128 1172 { 1129 1173 comNetwork.SetEnabled(newNatData.m_fEnabled); … … 1131 1175 } 1132 1176 /* Save NAT network name: */ 1133 if (fSuccess && newNatData.m_strNewName != oldNatData.m_strNewName)1177 if (fSuccess) 1134 1178 { 1135 1179 comNetwork.SetNetworkName(newNatData.m_strNewName); … … 1137 1181 } 1138 1182 /* Save NAT network CIDR: */ 1139 if (fSuccess && newNatData.m_strCIDR != oldNatData.m_strCIDR)1183 if (fSuccess) 1140 1184 { 1141 1185 comNetwork.SetNetwork(newNatData.m_strCIDR); … … 1143 1187 } 1144 1188 /* Save whether NAT network needs DHCP server: */ 1145 if (fSuccess && newNatData.m_fSupportsDHCP != oldNatData.m_fSupportsDHCP)1189 if (fSuccess) 1146 1190 { 1147 1191 comNetwork.SetNeedDhcpServer(newNatData.m_fSupportsDHCP); … … 1149 1193 } 1150 1194 /* Save whether NAT network supports IPv6: */ 1151 if (fSuccess && newNatData.m_fSupportsIPv6 != oldNatData.m_fSupportsIPv6)1195 if (fSuccess) 1152 1196 { 1153 1197 comNetwork.SetIPv6Enabled(newNatData.m_fSupportsIPv6); … … 1155 1199 } 1156 1200 /* Save whether NAT network should advertise default IPv6 route: */ 1157 if (fSuccess && newNatData.m_fAdvertiseDefaultIPv6Route != oldNatData.m_fAdvertiseDefaultIPv6Route)1201 if (fSuccess) 1158 1202 { 1159 1203 comNetwork.SetAdvertiseDefaultIPv6RouteEnabled(newNatData.m_fAdvertiseDefaultIPv6Route); … … 1161 1205 } 1162 1206 1163 /* Get IPv4 port forwarding rules for further activities: */ 1164 QVector<QString> ipv4rules; 1165 if (fSuccess) 1166 { 1167 ipv4rules = comNetwork.GetPortForwardRules4(); 1207 /* Save IPv4 forwarding rules: */ 1208 for (int i = 0; fSuccess && i < cache.childCount1(); ++i) 1209 { 1210 /* Get rule cache: */ 1211 const UISettingsCachePortForwardingRule &ruleCache = cache.child1(i); 1212 1213 /* Create rule not marked for 'remove': */ 1214 if (!ruleCache.wasRemoved()) 1215 { 1216 comNetwork.AddPortForwardRule(false, 1217 ruleCache.data().name, ruleCache.data().protocol, 1218 ruleCache.data().hostIp, ruleCache.data().hostPort.value(), 1219 ruleCache.data().guestIp, ruleCache.data().guestPort.value()); 1220 fSuccess = comNetwork.isOk(); 1221 } 1222 } 1223 1224 /* Save IPv6 forwarding rules: */ 1225 for (int i = 0; fSuccess && i < cache.childCount2(); ++i) 1226 { 1227 /* Get rule cache: */ 1228 const UISettingsCachePortForwardingRule &ruleCache = cache.child2(i); 1229 1230 /* Create rule not marked for 'remove': */ 1231 if (!ruleCache.wasRemoved()) 1232 { 1233 comNetwork.AddPortForwardRule(true, 1234 ruleCache.data().name, ruleCache.data().protocol, 1235 ruleCache.data().hostIp, ruleCache.data().hostPort.value(), 1236 ruleCache.data().guestIp, ruleCache.data().guestPort.value()); 1237 fSuccess = comNetwork.isOk(); 1238 } 1239 } 1240 1241 /* Show error message if necessary: */ 1242 if (!fSuccess) 1243 notifyOperationProgressError(UIMessageCenter::formatErrorInfo(comNetwork)); 1244 } 1245 } 1246 /* Return result: */ 1247 return fSuccess; 1248 } 1249 1250 bool UIGlobalSettingsNetwork::updateNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache) 1251 { 1252 /* Prepare result: */ 1253 bool fSuccess = true; 1254 /* Save NAT settings from the cache: */ 1255 if (fSuccess) 1256 { 1257 /* Get old NAT data from the cache: */ 1258 const UIDataSettingsGlobalNetworkNAT &oldNatData = cache.base(); 1259 /* Get new NAT data from the cache: */ 1260 const UIDataSettingsGlobalNetworkNAT &newNatData = cache.data(); 1261 1262 /* Get VBox for further activities: */ 1263 CVirtualBox comVBox = vboxGlobal().virtualBox(); 1264 /* Search for a NAT network with required name: */ 1265 CNATNetwork comNetwork = comVBox.FindNATNetworkByName(oldNatData.m_strName); 1266 fSuccess = comVBox.isOk() && comNetwork.isNotNull(); 1267 1268 /* Show error message if necessary: */ 1269 if (!fSuccess) 1270 notifyOperationProgressError(UIMessageCenter::formatErrorInfo(comVBox)); 1271 else 1272 { 1273 /* Save whether NAT network is enabled: */ 1274 if (fSuccess && newNatData.m_fEnabled != oldNatData.m_fEnabled) 1275 { 1276 comNetwork.SetEnabled(newNatData.m_fEnabled); 1168 1277 fSuccess = comNetwork.isOk(); 1169 1278 } 1170 /* Get IPv6 port forwarding rules for further activities: */ 1171 QVector<QString> ipv6rules; 1172 if (fSuccess) 1173 { 1174 ipv6rules = comNetwork.GetPortForwardRules6(); 1279 /* Save NAT network name: */ 1280 if (fSuccess && newNatData.m_strNewName != oldNatData.m_strNewName) 1281 { 1282 comNetwork.SetNetworkName(newNatData.m_strNewName); 1283 fSuccess = comNetwork.isOk(); 1284 } 1285 /* Save NAT network CIDR: */ 1286 if (fSuccess && newNatData.m_strCIDR != oldNatData.m_strCIDR) 1287 { 1288 comNetwork.SetNetwork(newNatData.m_strCIDR); 1289 fSuccess = comNetwork.isOk(); 1290 } 1291 /* Save whether NAT network needs DHCP server: */ 1292 if (fSuccess && newNatData.m_fSupportsDHCP != oldNatData.m_fSupportsDHCP) 1293 { 1294 comNetwork.SetNeedDhcpServer(newNatData.m_fSupportsDHCP); 1295 fSuccess = comNetwork.isOk(); 1296 } 1297 /* Save whether NAT network supports IPv6: */ 1298 if (fSuccess && newNatData.m_fSupportsIPv6 != oldNatData.m_fSupportsIPv6) 1299 { 1300 comNetwork.SetIPv6Enabled(newNatData.m_fSupportsIPv6); 1301 fSuccess = comNetwork.isOk(); 1302 } 1303 /* Save whether NAT network should advertise default IPv6 route: */ 1304 if (fSuccess && newNatData.m_fAdvertiseDefaultIPv6Route != oldNatData.m_fAdvertiseDefaultIPv6Route) 1305 { 1306 comNetwork.SetAdvertiseDefaultIPv6RouteEnabled(newNatData.m_fAdvertiseDefaultIPv6Route); 1175 1307 fSuccess = comNetwork.isOk(); 1176 1308 } … … 1245 1377 } 1246 1378 1247 void UIGlobalSettingsNetwork::createTreeWidgetItemForNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache, bool fChooseItem) 1248 { 1249 /* Add new item to the tree: */ 1250 UIItemNetworkNAT *pItem = new UIItemNetworkNAT; 1251 pItem->UIDataSettingsGlobalNetworkNAT::operator=(cache.base()); 1379 void UIGlobalSettingsNetwork::createTreeWidgetItemForNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache) 1380 { 1381 /* Get old NAT data: */ 1382 const UIDataSettingsGlobalNetworkNAT oldNATData = cache.base(); 1383 1384 /* Load old port forwarding rules: */ 1252 1385 UIPortForwardingDataList ipv4rules; 1253 1386 UIPortForwardingDataList ipv6rules; … … 1256 1389 for (int i = 0; i < cache.childCount2(); ++i) 1257 1390 ipv6rules << cache.child2(i).base(); 1258 pItem->setIpv4rules(ipv4rules); 1259 pItem->setIpv6rules(ipv6rules); 1260 pItem->updateFields(); 1261 m_pTreeNetworkNAT->addTopLevelItem(pItem); 1391 1392 /* Pass to wrapper below: */ 1393 createTreeWidgetItemForNetworkNAT(oldNATData, ipv4rules, ipv6rules, false /* choose item? */); 1394 } 1395 1396 void UIGlobalSettingsNetwork::createTreeWidgetItemForNetworkNAT(const UIDataSettingsGlobalNetworkNAT &data, 1397 const UIPortForwardingDataList &ipv4rules, 1398 const UIPortForwardingDataList &ipv6rules, 1399 bool fChooseItem /* = false */) 1400 { 1401 /* Create tree-widget item: */ 1402 UIItemNetworkNAT *pItem = new UIItemNetworkNAT; 1403 AssertPtrReturnVoid(pItem); 1404 { 1405 /* Configure item: */ 1406 pItem->UIDataSettingsGlobalNetworkNAT::operator=(data); 1407 pItem->setIpv4rules(ipv4rules); 1408 pItem->setIpv6rules(ipv6rules); 1409 pItem->updateFields(); 1410 1411 /* Add item to the tree-widget: */ 1412 m_pTreeNetworkNAT->addTopLevelItem(pItem); 1413 } 1414 1262 1415 /* And choose it as current if necessary: */ 1263 1416 if (fChooseItem) … … 1269 1422 /* Delete passed item: */ 1270 1423 delete pItem; 1424 } 1425 1426 bool UIGlobalSettingsNetwork::isNetworkCouldBeUpdated(const UISettingsCacheGlobalNetworkNAT &cache) const 1427 { 1428 /* INATNetwork interface allows to set 'name' attribute 1429 * which can conflict with another one NAT network name. 1430 * This attribute could be changed in GUI directly or indirectly. 1431 * For such cases we have to recreate INATNetwork instance, 1432 * for other cases we will update NAT network attributes only. */ 1433 const UIDataSettingsGlobalNetworkNAT &oldNATData = cache.base(); 1434 const UIDataSettingsGlobalNetworkNAT &newNATData = cache.data(); 1435 return true 1436 && (newNATData.m_strNewName == oldNATData.m_strNewName) 1437 ; 1271 1438 } 1272 1439 -
trunk/src/VBox/Frontends/VirtualBox/src/settings/global/UIGlobalSettingsNetwork.h
r66623 r66644 116 116 bool saveNetworkData(); 117 117 118 /** Uploads NAT @a network data into passed @a datastorage unit. */118 /** Uploads NAT @a network data into passed @a cache storage unit. */ 119 119 void loadToCacheFromNetworkNAT(const CNATNetwork &network, UISettingsCacheGlobalNetworkNAT &cache); 120 /** Saves @a data to corresponding NAT network. */ 121 bool saveDataNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache); 122 /** Creates a new item in the NAT network tree on the basis of passed @a data, @a fChooseItem if requested. */ 123 void createTreeWidgetItemForNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache, bool fChooseItem = false); 120 /** Removes corresponding NAT network on the basis of @a cache. */ 121 bool removeNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache); 122 /** Creates corresponding NAT network on the basis of @a cache. */ 123 bool createNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache); 124 /** Updates @a cache of corresponding NAT network. */ 125 bool updateNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache); 126 /** Creates a new item in the NAT network tree on the basis of passed @a cache. */ 127 void createTreeWidgetItemForNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache); 128 /** Creates a new item in the NAT network tree on the basis of passed @a data, @a ipv4rules, @a ipv6rules, @a fChooseItem if requested. */ 129 void createTreeWidgetItemForNetworkNAT(const UIDataSettingsGlobalNetworkNAT &data, 130 const UIPortForwardingDataList &ipv4rules, 131 const UIPortForwardingDataList &ipv6rules, 132 bool fChooseItem = false); 124 133 /** Removes existing @a pItem from the NAT network tree. */ 125 134 void removeTreeWidgetItemOfNetworkNAT(UIItemNetworkNAT *pItem); 135 /** Returns whether the NAT network described by the @a cache could be updated or recreated otherwise. */ 136 bool isNetworkCouldBeUpdated(const UISettingsCacheGlobalNetworkNAT &cache) const; 126 137 127 138 /** Uploads host @a network data into passed @a data storage unit. */
Note:
See TracChangeset
for help on using the changeset viewer.