VirtualBox

Changeset 66644 in vbox


Ignore:
Timestamp:
Apr 21, 2017 12:07:50 PM (8 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
114699
Message:

FE/Qt: Machine settings: Network page: NAT networks tab: Reworking to apply settings on Ok press, not on-the-fly.

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  
    599599void UIGlobalSettingsNetwork::sltAddNetworkNAT()
    600600{
    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;
    614607    const QString strNameTemplate("NatNetwork%1");
     608    const QRegExp regExp(strNameTemplate.arg("([\\d]*)"));
    615609    foreach (const QString &strName, names)
    616     {
    617         const QRegExp regExp(strNameTemplate.arg("([\\d]*)"));
    618610        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);
    633635    m_pTreeNetworkNAT->sortByColumn(1, Qt::AscendingOrder);
    634636}
     
    638640    /* Get network item: */
    639641    UIItemNetworkNAT *pItem = static_cast<UIItemNetworkNAT*>(m_pTreeNetworkNAT->currentItem());
    640     AssertMsg(pItem, ("Current item should present!\n"));
     642    AssertPtrReturnVoid(pItem);
    641643
    642644    /* Edit current item data: */
     
    662664    /* Get network item: */
    663665    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))
    670670        return;
    671671
    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: */
    685673    removeTreeWidgetItemOfNetworkNAT(pItem);
    686674}
     
    10191007    if (fSuccess && m_pCache->wasChanged())
    10201008    {
    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.
    10221012        for (int i = 0; fSuccess && i < m_pCache->childCount1(); ++i)
    10231013        {
     1014            /* Get NAT network cache: */
    10241015            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): */
    10281039        for (int i = 0; fSuccess && i < m_pCache->childCount2(); ++i)
    10291040        {
     
    11011112}
    11021113
    1103 bool UIGlobalSettingsNetwork::saveDataNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache)
     1114bool UIGlobalSettingsNetwork::removeNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache)
    11041115{
    11051116    /* Prepare result: */
     
    11111122        const UIDataSettingsGlobalNetworkNAT &oldNatData = cache.base();
    11121123        /* Get new NAT data from the cache: */
    1113         const UIDataSettingsGlobalNetworkNAT &newNatData = cache.data();
     1124        //const UIDataSettingsGlobalNetworkNAT &newNatData = cache.data();
    11141125
    11151126        /* Get VBox for further activities: */
    11161127        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
     1147bool 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);
    11191163        fSuccess = comVBox.isOk() && comNetwork.isNotNull();
    11201164
     
    11251169        {
    11261170            /* Save whether NAT network is enabled: */
    1127             if (fSuccess && newNatData.m_fEnabled != oldNatData.m_fEnabled)
     1171            if (fSuccess)
    11281172            {
    11291173                comNetwork.SetEnabled(newNatData.m_fEnabled);
     
    11311175            }
    11321176            /* Save NAT network name: */
    1133             if (fSuccess && newNatData.m_strNewName != oldNatData.m_strNewName)
     1177            if (fSuccess)
    11341178            {
    11351179                comNetwork.SetNetworkName(newNatData.m_strNewName);
     
    11371181            }
    11381182            /* Save NAT network CIDR: */
    1139             if (fSuccess && newNatData.m_strCIDR != oldNatData.m_strCIDR)
     1183            if (fSuccess)
    11401184            {
    11411185                comNetwork.SetNetwork(newNatData.m_strCIDR);
     
    11431187            }
    11441188            /* Save whether NAT network needs DHCP server: */
    1145             if (fSuccess && newNatData.m_fSupportsDHCP != oldNatData.m_fSupportsDHCP)
     1189            if (fSuccess)
    11461190            {
    11471191                comNetwork.SetNeedDhcpServer(newNatData.m_fSupportsDHCP);
     
    11491193            }
    11501194            /* Save whether NAT network supports IPv6: */
    1151             if (fSuccess && newNatData.m_fSupportsIPv6 != oldNatData.m_fSupportsIPv6)
     1195            if (fSuccess)
    11521196            {
    11531197                comNetwork.SetIPv6Enabled(newNatData.m_fSupportsIPv6);
     
    11551199            }
    11561200            /* Save whether NAT network should advertise default IPv6 route: */
    1157             if (fSuccess && newNatData.m_fAdvertiseDefaultIPv6Route != oldNatData.m_fAdvertiseDefaultIPv6Route)
     1201            if (fSuccess)
    11581202            {
    11591203                comNetwork.SetAdvertiseDefaultIPv6RouteEnabled(newNatData.m_fAdvertiseDefaultIPv6Route);
     
    11611205            }
    11621206
    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
     1250bool 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);
    11681277                fSuccess = comNetwork.isOk();
    11691278            }
    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);
    11751307                fSuccess = comNetwork.isOk();
    11761308            }
     
    12451377}
    12461378
    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());
     1379void UIGlobalSettingsNetwork::createTreeWidgetItemForNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache)
     1380{
     1381    /* Get old NAT data: */
     1382    const UIDataSettingsGlobalNetworkNAT oldNATData = cache.base();
     1383
     1384    /* Load old port forwarding rules: */
    12521385    UIPortForwardingDataList ipv4rules;
    12531386    UIPortForwardingDataList ipv6rules;
     
    12561389    for (int i = 0; i < cache.childCount2(); ++i)
    12571390        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
     1396void 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
    12621415    /* And choose it as current if necessary: */
    12631416    if (fChooseItem)
     
    12691422    /* Delete passed item: */
    12701423    delete pItem;
     1424}
     1425
     1426bool 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           ;
    12711438}
    12721439
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/global/UIGlobalSettingsNetwork.h

    r66623 r66644  
    116116    bool saveNetworkData();
    117117
    118     /** Uploads NAT @a network data into passed @a data storage unit. */
     118    /** Uploads NAT @a network data into passed @a cache storage unit. */
    119119    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);
    124133    /** Removes existing @a pItem from the NAT network tree. */
    125134    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;
    126137
    127138    /** Uploads host @a network data into passed @a data storage unit. */
Note: See TracChangeset for help on using the changeset viewer.

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