VirtualBox

Changeset 66614 in vbox for trunk/src


Ignore:
Timestamp:
Apr 19, 2017 3:58:29 PM (8 years ago)
Author:
vboxsync
Message:

FE/Qt: Global preferences: Network page: Reworking caching, saving and error-checking (step 1).

Location:
trunk/src/VBox/Frontends/VirtualBox/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIMessageCenter.cpp

    r66600 r66614  
    5757
    5858/* COM includes: */
     59# include "CNATNetwork.h"
     60# include "CDHCPServer.h"
    5961# include "CAudioAdapter.h"
    6062# include "CNATEngine.h"
     
    10951097          tr("Cannot save language settings."),
    10961098          formatErrorInfo(comProperties));
     1099}
     1100
     1101void UIMessageCenter::cannotLoadNetworkSettings(const CVirtualBox &comVBox, QWidget *pParent /* = 0 */)
     1102{
     1103    error(pParent, MessageType_Error,
     1104          tr("Cannot load network settings."),
     1105          formatErrorInfo(comVBox));
     1106}
     1107
     1108void UIMessageCenter::cannotLoadNetworkSettings(const CHost &comHost, QWidget *pParent /* = 0 */)
     1109{
     1110    error(pParent, MessageType_Error,
     1111          tr("Cannot load network settings."),
     1112          formatErrorInfo(comHost));
     1113}
     1114
     1115void UIMessageCenter::cannotSaveNetworkNatSettings(const CNATNetwork &comNetwork, QWidget *pParent /* = 0 */)
     1116{
     1117    error(pParent, MessageType_Error,
     1118          tr("Cannot save NAT network settings."),
     1119          formatErrorInfo(comNetwork));
     1120}
     1121
     1122void UIMessageCenter::cannotSaveNetworkHostSettings(const CHostNetworkInterface &comInterface, QWidget *pParent /* = 0 */)
     1123{
     1124    error(pParent, MessageType_Error,
     1125          tr("Cannot save host-only network settings."),
     1126          formatErrorInfo(comInterface));
     1127}
     1128
     1129void UIMessageCenter::cannotSaveDHCPServerSettings(const CDHCPServer &comServer, QWidget *pParent /* = 0 */)
     1130{
     1131    error(pParent, MessageType_Error,
     1132          tr("Cannot save DHCP server settings."),
     1133          formatErrorInfo(comServer));
    10971134}
    10981135
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIMessageCenter.h

    r66600 r66614  
    233233    void cannotSaveInputSettings(const CSystemProperties &comProperties, QWidget *pParent = 0);
    234234    void cannotSaveLanguageSettings(const CSystemProperties &comProperties, QWidget *pParent = 0);
     235    void cannotLoadNetworkSettings(const CVirtualBox &comVBox, QWidget *pParent = 0);
     236    void cannotLoadNetworkSettings(const CHost &comHost, QWidget *pParent = 0);
     237    void cannotSaveNetworkNatSettings(const CNATNetwork &comNetwork, QWidget *pParent = 0);
     238    void cannotSaveNetworkHostSettings(const CHostNetworkInterface &comInterface, QWidget *pParent = 0);
     239    void cannotSaveDHCPServerSettings(const CDHCPServer &comServer, QWidget *pParent = 0);
    235240    void cannotSaveProxySettings(const CSystemProperties &comProperties, QWidget *pParent = 0);
    236241    void cannotSaveUpdateSettings(const CSystemProperties &comProperties, QWidget *pParent = 0);
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/global/UIGlobalSettingsNetwork.cpp

    r66612 r66614  
    4949{
    5050    /** Constructs data. */
    51     UIDataSettingsGlobalNetwork()
    52         : m_networksNAT(QList<UIDataSettingsGlobalNetworkNAT>())
    53         , m_networksHost(QList<UIDataSettingsGlobalNetworkHost>())
    54     {}
     51    UIDataSettingsGlobalNetwork() {}
    5552
    5653    /** Returns whether the @a other passed data is equal to this one. */
    57     bool equal(const UIDataSettingsGlobalNetwork &other) const
    58     {
    59         return true
    60                && (m_networksNAT == other.m_networksNAT)
    61                && (m_networksHost == other.m_networksHost)
    62                ;
    63     }
    64 
    65     /** Returns whether the @a other passed data is equal to this one. */
    66     bool operator==(const UIDataSettingsGlobalNetwork &other) const { return equal(other); }
     54    bool operator==(const UIDataSettingsGlobalNetwork & /* other */) const { return true; }
    6755    /** Returns whether the @a other passed data is different from this one. */
    68     bool operator!=(const UIDataSettingsGlobalNetwork &other) const { return !equal(other); }
    69 
    70     /** Holds the NAT network data. */
    71     QList<UIDataSettingsGlobalNetworkNAT> m_networksNAT;
    72     /** Holds the host network data. */
    73     QList<UIDataSettingsGlobalNetworkHost> m_networksHost;
     56    bool operator!=(const UIDataSettingsGlobalNetwork & /* other */) const { return false; }
    7457};
    7558
     
    416399    /* Gather old network data: */
    417400    foreach (const CNATNetwork &network, vboxGlobal().virtualBox().GetNATNetworks())
    418     {
    419         UIDataSettingsGlobalNetworkNAT data;
    420         loadDataNetworkNAT(network, data);
    421         oldNetworkData.m_networksNAT << data;
    422     }
     401        loadToCacheFromNetworkNAT(network, m_pCache->child1(network.GetNetworkName()));
    423402    foreach (const CHostNetworkInterface &iface, vboxGlobal().host().GetNetworkInterfaces())
    424403        if (iface.GetInterfaceType() == KHostNetworkInterfaceType_HostOnly)
    425         {
    426             UIDataSettingsGlobalNetworkHost data;
    427             loadDataNetworkHost(iface, data);
    428             oldNetworkData.m_networksHost << data;
    429         }
     404            loadToCacheFromNetworkHost(iface, m_pCache->child2(iface.GetName()));
    430405
    431406    /* Cache old network data: */
     
    438413void UIGlobalSettingsNetwork::getFromCache()
    439414{
    440     /* Get old network data from the cache: */
    441     const UIDataSettingsGlobalNetwork &oldNetworkData = m_pCache->base();
    442 
    443415    /* Load old network data from the cache: */
    444     foreach (const UIDataSettingsGlobalNetworkNAT &network, oldNetworkData.m_networksNAT)
    445         createTreeWidgetItemForNetworkNAT(network);
     416    for (int i = 0; i < m_pCache->childCount1(); ++i)
     417        createTreeWidgetItemForNetworkNAT(m_pCache->child1(i).base());
    446418    m_pTreeNetworkNAT->sortByColumn(1, Qt::AscendingOrder);
    447419    m_pTreeNetworkNAT->setCurrentItem(m_pTreeNetworkNAT->topLevelItem(0));
    448420    sltHandleCurrentItemChangeNetworkNAT();
    449     foreach (const UIDataSettingsGlobalNetworkHost &network, oldNetworkData.m_networksHost)
    450         createTreeWidgetItemForNetworkHost(network);
     421    for (int i = 0; i < m_pCache->childCount2(); ++i)
     422        createTreeWidgetItemForNetworkHost(m_pCache->child2(i).base());
    451423    m_pTreeNetworkHost->sortByColumn(0, Qt::AscendingOrder);
    452424    m_pTreeNetworkHost->setCurrentItem(m_pTreeNetworkHost->topLevelItem(0));
     
    460432{
    461433    /* Prepare new network data: */
    462     UIDataSettingsGlobalNetwork newNetworkData = m_pCache->base();
     434    UIDataSettingsGlobalNetwork newNetworkData;
    463435
    464436    /* Gather new network data: */
    465     newNetworkData.m_networksNAT.clear();
    466     for (int iNetworkIndex = 0; iNetworkIndex < m_pTreeNetworkNAT->topLevelItemCount(); ++iNetworkIndex)
    467     {
    468         const UIItemNetworkNAT *pItem = static_cast<UIItemNetworkNAT*>(m_pTreeNetworkNAT->topLevelItem(iNetworkIndex));
    469         newNetworkData.m_networksNAT << *pItem;
    470     }
    471     newNetworkData.m_networksHost.clear();
    472     for (int iNetworkIndex = 0; iNetworkIndex < m_pTreeNetworkHost->topLevelItemCount(); ++iNetworkIndex)
    473     {
    474         const UIItemNetworkHost *pItem = static_cast<UIItemNetworkHost*>(m_pTreeNetworkHost->topLevelItem(iNetworkIndex));
    475         newNetworkData.m_networksHost << *pItem;
     437    for (int i = 0; i < m_pTreeNetworkNAT->topLevelItemCount(); ++i)
     438    {
     439        const UIItemNetworkNAT *pItem = static_cast<UIItemNetworkNAT*>(m_pTreeNetworkNAT->topLevelItem(i));
     440        m_pCache->child1(pItem->m_strName).cacheCurrentData(*pItem);
     441    }
     442    for (int i = 0; i < m_pTreeNetworkHost->topLevelItemCount(); ++i)
     443    {
     444        const UIItemNetworkHost *pItem = static_cast<UIItemNetworkHost*>(m_pTreeNetworkHost->topLevelItem(i));
     445        m_pCache->child2(pItem->m_interface.m_strName).cacheCurrentData(*pItem);
    476446    }
    477447
     
    485455    UISettingsPageGlobal::fetchData(data);
    486456
    487     /* Make sure network data was changed: */
    488     if (m_pCache->wasChanged())
    489     {
    490         /* Save new network data from the cache: */
    491         if (m_pCache->data().m_networksNAT != m_pCache->base().m_networksNAT)
    492             foreach (const UIDataSettingsGlobalNetworkNAT &data, m_pCache->data().m_networksNAT)
    493                 saveDataNetworkNAT(data);
    494         if (m_pCache->data().m_networksHost != m_pCache->base().m_networksHost)
    495             foreach (const UIDataSettingsGlobalNetworkHost &data, m_pCache->data().m_networksHost)
    496                 saveDataNetworkHost(data);
    497     }
     457    /* Update network data and failing state: */
     458    setFailed(!saveNetworkData());
    498459
    499460    /* Upload properties to data: */
     
    513474
    514475        /* Validate items first: */
    515         for (int iNetworkIndex = 0; iNetworkIndex < m_pTreeNetworkNAT->topLevelItemCount(); ++iNetworkIndex)
    516         {
    517             UIItemNetworkNAT *pItem = static_cast<UIItemNetworkNAT*>(m_pTreeNetworkNAT->topLevelItem(iNetworkIndex));
     476        for (int i = 0; i < m_pTreeNetworkNAT->topLevelItemCount(); ++i)
     477        {
     478            UIItemNetworkNAT *pItem = static_cast<UIItemNetworkNAT*>(m_pTreeNetworkNAT->topLevelItem(i));
    518479            if (!pItem->validate(message))
    519480                fPass = false;
     
    550511
    551512        /* Validate items only: */
    552         for (int iNetworkIndex = 0; iNetworkIndex < m_pTreeNetworkHost->topLevelItemCount(); ++iNetworkIndex)
    553         {
    554             UIItemNetworkHost *pItem = static_cast<UIItemNetworkHost*>(m_pTreeNetworkHost->topLevelItem(iNetworkIndex));
     513        for (int i = 0; i < m_pTreeNetworkHost->topLevelItemCount(); ++i)
     514        {
     515            UIItemNetworkHost *pItem = static_cast<UIItemNetworkHost*>(m_pTreeNetworkHost->topLevelItem(i));
    555516            if (!pItem->validate(message))
    556517                fPass = false;
     
    647608
    648609    /* Update tree: */
    649     UIDataSettingsGlobalNetworkNAT data;
    650     loadDataNetworkNAT(network, data);
    651     createTreeWidgetItemForNetworkNAT(data, true);
     610    const QString strCacheKey = network.GetNetworkName();
     611    loadToCacheFromNetworkNAT(network, m_pCache->child1(strCacheKey));
     612    createTreeWidgetItemForNetworkNAT(m_pCache->child1(strCacheKey).base(), true);
    652613    m_pTreeNetworkNAT->sortByColumn(1, Qt::AscendingOrder);
    653614}
     
    764725
    765726    /* Update tree: */
    766     UIDataSettingsGlobalNetworkHost data;
    767     loadDataNetworkHost(iface, data);
    768     createTreeWidgetItemForNetworkHost(data, true);
     727    const QString strCacheKey = iface.GetName();
     728    loadToCacheFromNetworkHost(iface, m_pCache->child2(strCacheKey));
     729    createTreeWidgetItemForNetworkHost(m_pCache->child2(strCacheKey).base(), true);
    769730    m_pTreeNetworkHost->sortByColumn(0, Qt::AscendingOrder);
    770731}
     
    1027988}
    1028989
    1029 void UIGlobalSettingsNetwork::loadDataNetworkNAT(const CNATNetwork &network, UIDataSettingsGlobalNetworkNAT &data)
    1030 {
     990bool UIGlobalSettingsNetwork::saveNetworkData()
     991{
     992    /* Prepare result: */
     993    bool fSuccess = true;
     994    /* Save network settings from the cache: */
     995    if (fSuccess && m_pCache->wasChanged())
     996    {
     997        /* Save new network data from the cache: */
     998        for (int i = 0; fSuccess && i < m_pCache->childCount1(); ++i)
     999        {
     1000            const UISettingsCacheGlobalNetworkNAT &cache = m_pCache->child1(i);
     1001            if (cache.wasUpdated())
     1002                fSuccess = saveDataNetworkNAT(cache);
     1003        }
     1004        for (int i = 0; fSuccess && i < m_pCache->childCount2(); ++i)
     1005        {
     1006            const UISettingsCacheGlobalNetworkHost &cache = m_pCache->child2(i);
     1007            if (cache.wasUpdated())
     1008                fSuccess = saveDataNetworkHost(m_pCache->child2(i));
     1009        }
     1010    }
     1011    /* Return result: */
     1012    return fSuccess;
     1013}
     1014
     1015void UIGlobalSettingsNetwork::loadToCacheFromNetworkNAT(const CNATNetwork &network, UISettingsCacheGlobalNetworkNAT &cache)
     1016{
     1017    /* Prepare old NAT data: */
     1018    UIDataSettingsGlobalNetworkNAT oldNATData;
     1019
    10311020    /* Load NAT network settings: */
    1032     data.m_fEnabled = network.GetEnabled();
    1033     data.m_strName = network.GetNetworkName();
    1034     data.m_strNewName = data.m_strName;
    1035     data.m_strCIDR = network.GetNetwork();
    1036     data.m_fSupportsDHCP = network.GetNeedDhcpServer();
    1037     data.m_fSupportsIPv6 = network.GetIPv6Enabled();
    1038     data.m_fAdvertiseDefaultIPv6Route = network.GetAdvertiseDefaultIPv6RouteEnabled();
     1021    oldNATData.m_fEnabled = network.GetEnabled();
     1022    oldNATData.m_strName = network.GetNetworkName();
     1023    oldNATData.m_strNewName = oldNATData.m_strName;
     1024    oldNATData.m_strCIDR = network.GetNetwork();
     1025    oldNATData.m_fSupportsDHCP = network.GetNeedDhcpServer();
     1026    oldNATData.m_fSupportsIPv6 = network.GetIPv6Enabled();
     1027    oldNATData.m_fAdvertiseDefaultIPv6Route = network.GetAdvertiseDefaultIPv6RouteEnabled();
    10391028
    10401029    /* Load IPv4 rules: */
     
    10481037        if (rules.size() != 6)
    10491038            continue;
    1050         data.m_ipv4rules << UIDataPortForwardingRule(rules[0],
    1051                                                      gpConverter->fromInternalString<KNATProtocol>(rules[1]),
    1052                                                      QString(rules[2]).remove('[').remove(']'),
    1053                                                      rules[3].toUInt(),
    1054                                                      QString(rules[4]).remove('[').remove(']'),
    1055                                                      rules[5].toUInt());
     1039        oldNATData.m_ipv4rules << UIDataPortForwardingRule(rules[0],
     1040                                                           gpConverter->fromInternalString<KNATProtocol>(rules[1]),
     1041                                                           QString(rules[2]).remove('[').remove(']'),
     1042                                                           rules[3].toUInt(),
     1043                                                           QString(rules[4]).remove('[').remove(']'),
     1044                                                           rules[5].toUInt());
    10561045    }
    10571046
     
    10761065        if (rules.size() != 6)
    10771066            continue;
    1078         data.m_ipv6rules << UIDataPortForwardingRule(rules[0],
    1079                                                      gpConverter->fromInternalString<KNATProtocol>(rules[1]),
    1080                                                      QString(rules[2]).remove('[').remove(']'),
    1081                                                      rules[3].toUInt(),
    1082                                                      QString(rules[4]).remove('[').remove(']'),
    1083                                                      rules[5].toUInt());
    1084     }
    1085 }
    1086 
    1087 void UIGlobalSettingsNetwork::saveDataNetworkNAT(const UIDataSettingsGlobalNetworkNAT &data)
    1088 {
    1089     /* Make sure corresponding NAT network exists: */
    1090     CVirtualBox vbox = vboxGlobal().virtualBox();
    1091     CNATNetwork network = vbox.FindNATNetworkByName(data.m_strName);
    1092     AssertReturnVoid(vbox.isOk() && !network.isNull());
    1093 
    1094     /* Save NAT network options: */
    1095     network.SetEnabled(data.m_fEnabled);
    1096     network.SetNetworkName(data.m_strNewName);
    1097     network.SetNetwork(data.m_strCIDR);
    1098     network.SetNeedDhcpServer(data.m_fSupportsDHCP);
    1099     network.SetIPv6Enabled(data.m_fSupportsIPv6);
    1100     network.SetAdvertiseDefaultIPv6RouteEnabled(data.m_fAdvertiseDefaultIPv6Route);
    1101 
    1102     /* Rewrite IPv4 rules: */
    1103     foreach (const QString &strRule, network.GetPortForwardRules4())
    1104         network.RemovePortForwardRule(false, strRule.section(':', 0, 0));
    1105     foreach (const UIDataPortForwardingRule &newRule, data.m_ipv4rules)
    1106         network.AddPortForwardRule(false,
    1107                                    newRule.name, newRule.protocol,
    1108                                    newRule.hostIp, newRule.hostPort.value(),
    1109                                    newRule.guestIp, newRule.guestPort.value());
    1110 
    1111     /* Rewrite IPv6 rules: */
    1112     foreach (const QString &strRule, network.GetPortForwardRules6())
    1113         network.RemovePortForwardRule(true, strRule.section(':', 0, 0));
    1114     foreach (const UIDataPortForwardingRule &newRule, data.m_ipv6rules)
    1115         network.AddPortForwardRule(true,
    1116                                    newRule.name, newRule.protocol,
    1117                                    newRule.hostIp, newRule.hostPort.value(),
    1118                                    newRule.guestIp, newRule.guestPort.value());
     1067        oldNATData.m_ipv6rules << UIDataPortForwardingRule(rules[0],
     1068                                                           gpConverter->fromInternalString<KNATProtocol>(rules[1]),
     1069                                                           QString(rules[2]).remove('[').remove(']'),
     1070                                                           rules[3].toUInt(),
     1071                                                           QString(rules[4]).remove('[').remove(']'),
     1072                                                           rules[5].toUInt());
     1073    }
     1074
     1075    /* Cache old NAT data: */
     1076    cache.cacheInitialData(oldNATData);
     1077}
     1078
     1079bool UIGlobalSettingsNetwork::saveDataNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache)
     1080{
     1081    /* Prepare result: */
     1082    bool fSuccess = true;
     1083    /* Save NAT settings from the cache: */
     1084    if (fSuccess)
     1085    {
     1086        /* Get old NAT data from the cache: */
     1087        const UIDataSettingsGlobalNetworkNAT &oldNatData = cache.base();
     1088        /* Get new NAT data from the cache: */
     1089        const UIDataSettingsGlobalNetworkNAT &newNatData = cache.data();
     1090
     1091        /* Get VBox for further activities: */
     1092        CVirtualBox comVBox = vboxGlobal().virtualBox();
     1093        /* Search for a NAT network with the same name: */
     1094        CNATNetwork comNetwork = comVBox.FindNATNetworkByName(newNatData.m_strName);
     1095        fSuccess = comVBox.isOk() && comNetwork.isNotNull();
     1096
     1097        /* Show error message if necessary: */
     1098        if (!fSuccess)
     1099            msgCenter().cannotLoadNetworkSettings(comVBox, this);
     1100        else
     1101        {
     1102            /* Save whether NAT network is enabled: */
     1103            if (fSuccess && newNatData.m_fEnabled != oldNatData.m_fEnabled)
     1104            {
     1105                comNetwork.SetEnabled(newNatData.m_fEnabled);
     1106                fSuccess = comNetwork.isOk();
     1107            }
     1108            /* Save NAT network name: */
     1109            if (fSuccess && newNatData.m_strNewName != oldNatData.m_strNewName)
     1110            {
     1111                comNetwork.SetNetworkName(newNatData.m_strNewName);
     1112                fSuccess = comNetwork.isOk();
     1113            }
     1114            /* Save NAT network CIDR: */
     1115            if (fSuccess && newNatData.m_strCIDR != oldNatData.m_strCIDR)
     1116            {
     1117                comNetwork.SetNetwork(newNatData.m_strCIDR);
     1118                fSuccess = comNetwork.isOk();
     1119            }
     1120            /* Save whether NAT network needs DHCP server: */
     1121            if (fSuccess && newNatData.m_fSupportsDHCP != oldNatData.m_fSupportsDHCP)
     1122            {
     1123                comNetwork.SetNeedDhcpServer(newNatData.m_fSupportsDHCP);
     1124                fSuccess = comNetwork.isOk();
     1125            }
     1126            /* Save whether NAT network supports IPv6: */
     1127            if (fSuccess && newNatData.m_fSupportsIPv6 != oldNatData.m_fSupportsIPv6)
     1128            {
     1129                comNetwork.SetIPv6Enabled(newNatData.m_fSupportsIPv6);
     1130                fSuccess = comNetwork.isOk();
     1131            }
     1132            /* Save whether NAT network should advertise default IPv6 route: */
     1133            if (fSuccess && newNatData.m_fAdvertiseDefaultIPv6Route != oldNatData.m_fAdvertiseDefaultIPv6Route)
     1134            {
     1135                comNetwork.SetAdvertiseDefaultIPv6RouteEnabled(newNatData.m_fAdvertiseDefaultIPv6Route);
     1136                fSuccess = comNetwork.isOk();
     1137            }
     1138
     1139            /* Get IPv4 port forwarding rules for further activities: */
     1140            QVector<QString> ipv4rules;
     1141            if (fSuccess)
     1142            {
     1143                ipv4rules = comNetwork.GetPortForwardRules4();
     1144                fSuccess = comNetwork.isOk();
     1145            }
     1146            /* Get IPv6 port forwarding rules for further activities: */
     1147            QVector<QString> ipv6rules;
     1148            if (fSuccess)
     1149            {
     1150                ipv6rules = comNetwork.GetPortForwardRules6();
     1151                fSuccess = comNetwork.isOk();
     1152            }
     1153
     1154            /* Save IPv4 forwarding rules: */
     1155            if (fSuccess && newNatData.m_ipv4rules != oldNatData.m_ipv4rules)
     1156            {
     1157                for (int i = 0; fSuccess && i < ipv4rules.size(); ++i)
     1158                {
     1159                    const QString &strOldRule = ipv4rules.at(i);
     1160                    comNetwork.RemovePortForwardRule(false,
     1161                                                     strOldRule.section(':', 0, 0));
     1162                    fSuccess = comNetwork.isOk();
     1163                }
     1164                for (int i = 0; fSuccess && i < newNatData.m_ipv4rules.size(); ++i)
     1165                {
     1166                    const UIDataPortForwardingRule &newRule = newNatData.m_ipv4rules.at(i);
     1167                    comNetwork.AddPortForwardRule(false,
     1168                                                  newRule.name, newRule.protocol,
     1169                                                  newRule.hostIp, newRule.hostPort.value(),
     1170                                                  newRule.guestIp, newRule.guestPort.value());
     1171                    fSuccess = comNetwork.isOk();
     1172                }
     1173            }
     1174
     1175            /* Save IPv6 forwarding rules: */
     1176            if (fSuccess && newNatData.m_ipv6rules != oldNatData.m_ipv6rules)
     1177            {
     1178                for (int i = 0; fSuccess && i < ipv6rules.size(); ++i)
     1179                {
     1180                    const QString &strOldRule = ipv6rules.at(i);
     1181                    comNetwork.RemovePortForwardRule(true,
     1182                                                     strOldRule.section(':', 0, 0));
     1183                    fSuccess = comNetwork.isOk();
     1184                }
     1185                for (int i = 0; fSuccess && i < newNatData.m_ipv6rules.size(); ++i)
     1186                {
     1187                    const UIDataPortForwardingRule &newRule = newNatData.m_ipv6rules.at(i);
     1188                    comNetwork.AddPortForwardRule(true,
     1189                                                  newRule.name, newRule.protocol,
     1190                                                  newRule.hostIp, newRule.hostPort.value(),
     1191                                                  newRule.guestIp, newRule.guestPort.value());
     1192                    fSuccess = comNetwork.isOk();
     1193                }
     1194            }
     1195
     1196            /* Show error message if necessary: */
     1197            if (!fSuccess)
     1198                msgCenter().cannotSaveNetworkNatSettings(comNetwork, this);
     1199        }
     1200    }
     1201    /* Return result: */
     1202    return fSuccess;
    11191203}
    11201204
     
    11371221}
    11381222
    1139 void UIGlobalSettingsNetwork::loadDataNetworkHost(const CHostNetworkInterface &iface, UIDataSettingsGlobalNetworkHost &data)
    1140 {
     1223void UIGlobalSettingsNetwork::loadToCacheFromNetworkHost(const CHostNetworkInterface &iface, UISettingsCacheGlobalNetworkHost &cache)
     1224{
     1225    /* Prepare old NAT data: */
     1226    UIDataSettingsGlobalNetworkHost oldHostData;
     1227
    11411228    /* Get DHCP server (create if necessary): */
    11421229    CDHCPServer dhcp = vboxGlobal().virtualBox().FindDHCPServerByNetworkName(iface.GetNetworkName());
     
    11571244
    11581245    /* Host interface settings: */
    1159     data.m_interface.m_strName = iface.GetName();
    1160     data.m_interface.m_fDhcpClientEnabled = iface.GetDHCPEnabled();
    1161     data.m_interface.m_strInterfaceAddress = iface.GetIPAddress();
    1162     data.m_interface.m_strInterfaceMask = iface.GetNetworkMask();
    1163     data.m_interface.m_fIpv6Supported = iface.GetIPV6Supported();
    1164     data.m_interface.m_strInterfaceAddress6 = iface.GetIPV6Address();
    1165     data.m_interface.m_strInterfaceMaskLength6 = QString::number(iface.GetIPV6NetworkMaskPrefixLength());
     1246    oldHostData.m_interface.m_strName = iface.GetName();
     1247    oldHostData.m_interface.m_fDhcpClientEnabled = iface.GetDHCPEnabled();
     1248    oldHostData.m_interface.m_strInterfaceAddress = iface.GetIPAddress();
     1249    oldHostData.m_interface.m_strInterfaceMask = iface.GetNetworkMask();
     1250    oldHostData.m_interface.m_fIpv6Supported = iface.GetIPV6Supported();
     1251    oldHostData.m_interface.m_strInterfaceAddress6 = iface.GetIPV6Address();
     1252    oldHostData.m_interface.m_strInterfaceMaskLength6 = QString::number(iface.GetIPV6NetworkMaskPrefixLength());
    11661253
    11671254    /* DHCP server settings: */
    1168     data.m_dhcpserver.m_fDhcpServerEnabled = dhcp.GetEnabled();
    1169     data.m_dhcpserver.m_strDhcpServerAddress = dhcp.GetIPAddress();
    1170     data.m_dhcpserver.m_strDhcpServerMask = dhcp.GetNetworkMask();
    1171     data.m_dhcpserver.m_strDhcpLowerAddress = dhcp.GetLowerIP();
    1172     data.m_dhcpserver.m_strDhcpUpperAddress = dhcp.GetUpperIP();
    1173 }
    1174 
    1175 void UIGlobalSettingsNetwork::saveDataNetworkHost(const UIDataSettingsGlobalNetworkHost &data)
    1176 {
    1177     /* Make sure corresponding Host interface exists: */
    1178     CHost host = vboxGlobal().host();
    1179     CHostNetworkInterface iface = host.FindHostNetworkInterfaceByName(data.m_interface.m_strName);
    1180     AssertReturnVoid(host.isOk() && !iface.isNull());
    1181 
    1182     /* Automatic host interface configuration: */
    1183     if (data.m_interface.m_fDhcpClientEnabled)
    1184     {
    1185         iface.EnableDynamicIPConfig();
    1186     }
    1187     /* Manual host interface configuration: */
    1188     else
    1189     {
    1190         AssertMsg(data.m_interface.m_strInterfaceAddress.trimmed().isEmpty() ||
    1191                   RTNetIsIPv4AddrStr(data.m_interface.m_strInterfaceAddress.toUtf8().constData()),
    1192                   ("Interface IPv4 address must be empty or IPv4-valid!\n"));
    1193         AssertMsg(data.m_interface.m_strInterfaceMask.trimmed().isEmpty() ||
    1194                   RTNetIsIPv4AddrStr(data.m_interface.m_strInterfaceMask.toUtf8().constData()),
    1195                   ("Interface IPv4 network mask must be empty or IPv4-valid!\n"));
    1196         if (   (   data.m_interface.m_strInterfaceAddress.trimmed().isEmpty()
    1197                 || RTNetIsIPv4AddrStr(data.m_interface.m_strInterfaceAddress.toUtf8().constData()))
    1198             && (   data.m_interface.m_strInterfaceMask.trimmed().isEmpty()
    1199                 || RTNetIsIPv4AddrStr(data.m_interface.m_strInterfaceMask.toUtf8().constData())))
    1200             iface.EnableStaticIPConfig(data.m_interface.m_strInterfaceAddress, data.m_interface.m_strInterfaceMask);
    1201         if (iface.GetIPV6Supported())
    1202         {
    1203             AssertMsg(data.m_interface.m_strInterfaceAddress6.trimmed().isEmpty() ||
    1204                       RTNetIsIPv6AddrStr(data.m_interface.m_strInterfaceAddress6.toUtf8().constData()),
    1205                       ("Interface IPv6 address must be empty or IPv6-valid!\n"));
    1206             bool fIsMaskPrefixLengthNumber = false;
    1207             const int iMaskPrefixLength = data.m_interface.m_strInterfaceMaskLength6.trimmed().toInt(&fIsMaskPrefixLengthNumber);
    1208             AssertMsg(fIsMaskPrefixLengthNumber && iMaskPrefixLength >= 0 && iMaskPrefixLength <= 128,
    1209                       ("Interface IPv6 network mask prefix length must be empty or IPv6-valid!\n"));
    1210             if (   (   data.m_interface.m_strInterfaceAddress6.trimmed().isEmpty()
    1211                     || RTNetIsIPv6AddrStr(data.m_interface.m_strInterfaceAddress6.toUtf8().constData()))
    1212                 && (   fIsMaskPrefixLengthNumber
    1213                     && iMaskPrefixLength >= 0
    1214                     && iMaskPrefixLength <= 128))
    1215                 iface.EnableStaticIPConfigV6(data.m_interface.m_strInterfaceAddress6, data.m_interface.m_strInterfaceMaskLength6.toULong());
    1216         }
    1217     }
    1218 
    1219     /* Make sure corresponding DHCP server exists: */
    1220     CVirtualBox vbox = vboxGlobal().virtualBox();
    1221     CDHCPServer dhcp = vbox.FindDHCPServerByNetworkName(iface.GetNetworkName());
    1222     AssertReturnVoid(vbox.isOk() && !dhcp.isNull());
    1223 
    1224     /* Save DHCP server configuration: */
    1225     dhcp.SetEnabled(data.m_dhcpserver.m_fDhcpServerEnabled);
    1226     if (data.m_dhcpserver.m_fDhcpServerEnabled)
    1227     {
    1228         AssertMsg(RTNetIsIPv4AddrStr(data.m_dhcpserver.m_strDhcpServerAddress.toUtf8().constData()),
    1229                   ("DHCP server IPv4 address must be IPv4-valid!\n"));
    1230         AssertMsg(RTNetIsIPv4AddrStr(data.m_dhcpserver.m_strDhcpServerMask.toUtf8().constData()),
    1231                   ("DHCP server IPv4 network mask must be IPv4-valid!\n"));
    1232         AssertMsg(RTNetIsIPv4AddrStr(data.m_dhcpserver.m_strDhcpLowerAddress.toUtf8().constData()),
    1233                   ("DHCP server IPv4 lower bound must be IPv4-valid!\n"));
    1234         AssertMsg(RTNetIsIPv4AddrStr(data.m_dhcpserver.m_strDhcpUpperAddress.toUtf8().constData()),
    1235                   ("DHCP server IPv4 upper bound must be IPv4-valid!\n"));
    1236         if (RTNetIsIPv4AddrStr(data.m_dhcpserver.m_strDhcpServerAddress.toUtf8().constData()) &&
    1237             RTNetIsIPv4AddrStr(data.m_dhcpserver.m_strDhcpServerMask.toUtf8().constData()) &&
    1238             RTNetIsIPv4AddrStr(data.m_dhcpserver.m_strDhcpLowerAddress.toUtf8().constData()) &&
    1239             RTNetIsIPv4AddrStr(data.m_dhcpserver.m_strDhcpUpperAddress.toUtf8().constData()))
    1240             dhcp.SetConfiguration(data.m_dhcpserver.m_strDhcpServerAddress, data.m_dhcpserver.m_strDhcpServerMask,
    1241                                   data.m_dhcpserver.m_strDhcpLowerAddress, data.m_dhcpserver.m_strDhcpUpperAddress);
    1242     }
    1243     if (!dhcp.isOk())
    1244         emit sigOperationProgressError(UIMessageCenter::formatErrorInfo(dhcp));
     1255    oldHostData.m_dhcpserver.m_fDhcpServerEnabled = dhcp.GetEnabled();
     1256    oldHostData.m_dhcpserver.m_strDhcpServerAddress = dhcp.GetIPAddress();
     1257    oldHostData.m_dhcpserver.m_strDhcpServerMask = dhcp.GetNetworkMask();
     1258    oldHostData.m_dhcpserver.m_strDhcpLowerAddress = dhcp.GetLowerIP();
     1259    oldHostData.m_dhcpserver.m_strDhcpUpperAddress = dhcp.GetUpperIP();
     1260
     1261    /* Cache old NAT data: */
     1262    cache.cacheInitialData(oldHostData);
     1263}
     1264
     1265bool UIGlobalSettingsNetwork::saveDataNetworkHost(const UISettingsCacheGlobalNetworkHost &cache)
     1266{
     1267    /* Prepare result: */
     1268    bool fSuccess = true;
     1269    /* Save host-only settings from the cache: */
     1270    if (fSuccess)
     1271    {
     1272        /* Get old host-only data from the cache: */
     1273        const UIDataSettingsGlobalNetworkHost &oldHostOnlyData = cache.base();
     1274        /* Get new host-only data from the cache: */
     1275        const UIDataSettingsGlobalNetworkHost &newHostOnlyData = cache.data();
     1276
     1277        /* Get VBox for further activities: */
     1278        CHost comHost = vboxGlobal().host();
     1279        /* Search for a host-only interface with the same name: */
     1280        CHostNetworkInterface comInterface = comHost.FindHostNetworkInterfaceByName(newHostOnlyData.m_interface.m_strName);
     1281        fSuccess = comHost.isOk() && comInterface.isNotNull();
     1282
     1283        /* Show error message if necessary: */
     1284        if (!fSuccess)
     1285            msgCenter().cannotLoadNetworkSettings(comHost, this);
     1286        else
     1287        {
     1288            /* Automatic host interface configuration: */
     1289            if (   fSuccess
     1290                && newHostOnlyData.m_interface.m_fDhcpClientEnabled
     1291                && newHostOnlyData.m_interface.m_fDhcpClientEnabled != oldHostOnlyData.m_interface.m_fDhcpClientEnabled)
     1292            {
     1293                comInterface.EnableDynamicIPConfig();
     1294                fSuccess = comInterface.isOk();
     1295            }
     1296
     1297            else
     1298
     1299            /* Manual host interface configuration: */
     1300            if (   fSuccess
     1301                && !newHostOnlyData.m_interface.m_fDhcpClientEnabled)
     1302            {
     1303                if (   fSuccess
     1304                    && (   newHostOnlyData.m_interface.m_fDhcpClientEnabled != oldHostOnlyData.m_interface.m_fDhcpClientEnabled
     1305                        || newHostOnlyData.m_interface.m_strInterfaceAddress != oldHostOnlyData.m_interface.m_strInterfaceAddress
     1306                        || newHostOnlyData.m_interface.m_strInterfaceMask != oldHostOnlyData.m_interface.m_strInterfaceMask))
     1307                {
     1308                    comInterface.EnableStaticIPConfig(newHostOnlyData.m_interface.m_strInterfaceAddress, newHostOnlyData.m_interface.m_strInterfaceMask);
     1309                    fSuccess = comInterface.isOk();
     1310                }
     1311
     1312                if (   fSuccess
     1313                    && newHostOnlyData.m_interface.m_fIpv6Supported
     1314                    && (   newHostOnlyData.m_interface.m_fDhcpClientEnabled != oldHostOnlyData.m_interface.m_fDhcpClientEnabled
     1315                        || newHostOnlyData.m_interface.m_strInterfaceAddress6 != oldHostOnlyData.m_interface.m_strInterfaceAddress6
     1316                        || newHostOnlyData.m_interface.m_strInterfaceMaskLength6 != oldHostOnlyData.m_interface.m_strInterfaceMaskLength6))
     1317                {
     1318                    comInterface.EnableStaticIPConfigV6(newHostOnlyData.m_interface.m_strInterfaceAddress6, newHostOnlyData.m_interface.m_strInterfaceMaskLength6.toULong());
     1319                    fSuccess = comInterface.isOk();
     1320                }
     1321            }
     1322
     1323            /* Get network name for further activities: */
     1324            QString strNetworkName;
     1325            if (fSuccess)
     1326            {
     1327                strNetworkName = comInterface.GetNetworkName();
     1328                fSuccess = comInterface.isOk();
     1329            }
     1330
     1331            /* Show error message if necessary: */
     1332            if (!fSuccess)
     1333                msgCenter().cannotSaveNetworkHostSettings(comInterface, this);
     1334            else
     1335            {
     1336                /* Get VBox for further activities: */
     1337                CVirtualBox comVBox = vboxGlobal().virtualBox();
     1338                /* Search for a DHCP server with the same name: */
     1339                CDHCPServer comServer = comVBox.FindDHCPServerByNetworkName(strNetworkName);
     1340                fSuccess = comVBox.isOk() && comServer.isNotNull();
     1341
     1342                /* Show error message if necessary: */
     1343                if (!fSuccess)
     1344                    msgCenter().cannotLoadNetworkSettings(comVBox, this);
     1345                else
     1346                {
     1347                    /* Save whether DHCP server is enabled: */
     1348                    if (fSuccess && newHostOnlyData.m_dhcpserver.m_fDhcpServerEnabled != oldHostOnlyData.m_dhcpserver.m_fDhcpServerEnabled)
     1349                    {
     1350                        comServer.SetEnabled(newHostOnlyData.m_dhcpserver.m_fDhcpServerEnabled);
     1351                        fSuccess = comServer.isOk();
     1352                    }
     1353                    if (   fSuccess
     1354                        && newHostOnlyData.m_dhcpserver.m_fDhcpServerEnabled
     1355                        && (   newHostOnlyData.m_dhcpserver.m_strDhcpServerAddress != oldHostOnlyData.m_dhcpserver.m_strDhcpServerAddress
     1356                            || newHostOnlyData.m_dhcpserver.m_strDhcpServerMask != oldHostOnlyData.m_dhcpserver.m_strDhcpServerMask
     1357                            || newHostOnlyData.m_dhcpserver.m_strDhcpLowerAddress != oldHostOnlyData.m_dhcpserver.m_strDhcpLowerAddress
     1358                            || newHostOnlyData.m_dhcpserver.m_strDhcpUpperAddress != oldHostOnlyData.m_dhcpserver.m_strDhcpUpperAddress))
     1359                    {
     1360                        comServer.SetConfiguration(newHostOnlyData.m_dhcpserver.m_strDhcpServerAddress, newHostOnlyData.m_dhcpserver.m_strDhcpServerMask,
     1361                                                   newHostOnlyData.m_dhcpserver.m_strDhcpLowerAddress, newHostOnlyData.m_dhcpserver.m_strDhcpUpperAddress);
     1362                        fSuccess = comServer.isOk();
     1363                    }
     1364
     1365                    /* Show error message if necessary: */
     1366                    if (!fSuccess)
     1367                        msgCenter().cannotSaveDHCPServerSettings(comServer, this);
     1368                    // if (!comServer.isOk())
     1369                    //    emit sigOperationProgressError(UIMessageCenter::formatErrorInfo(comServer));
     1370                }
     1371            }
     1372        }
     1373    }
     1374    /* Return result: */
     1375    return fSuccess;
    12451376}
    12461377
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/global/UIGlobalSettingsNetwork.h

    r66612 r66614  
    2929struct UIDataSettingsGlobalNetworkNAT;
    3030struct UIDataSettingsGlobalNetworkHost;
    31 typedef UISettingsCache<UIDataSettingsGlobalNetwork> UISettingsCacheGlobalNetwork;
     31typedef UISettingsCache<UIDataSettingsGlobalNetworkNAT> UISettingsCacheGlobalNetworkNAT;
     32typedef UISettingsCache<UIDataSettingsGlobalNetworkHost> UISettingsCacheGlobalNetworkHost;
     33typedef UISettingsCachePoolOfTwo<UIDataSettingsGlobalNetwork, UISettingsCacheGlobalNetworkNAT, UISettingsCacheGlobalNetworkHost> UISettingsCacheGlobalNetwork;
    3234
    3335
     
    109111    void cleanup();
    110112
     113    /** Saves existing network data from the cache. */
     114    bool saveNetworkData();
     115
    111116    /** Uploads NAT @a network data into passed @a data storage unit. */
    112     void loadDataNetworkNAT(const CNATNetwork &network, UIDataSettingsGlobalNetworkNAT &data);
     117    void loadToCacheFromNetworkNAT(const CNATNetwork &network, UISettingsCacheGlobalNetworkNAT &cache);
    113118    /** Saves @a data to corresponding NAT network. */
    114     void saveDataNetworkNAT(const UIDataSettingsGlobalNetworkNAT &data);
     119    bool saveDataNetworkNAT(const UISettingsCacheGlobalNetworkNAT &cache);
    115120    /** Creates a new item in the NAT network tree on the basis of passed @a data, @a fChooseItem if requested. */
    116121    void createTreeWidgetItemForNetworkNAT(const UIDataSettingsGlobalNetworkNAT &data, bool fChooseItem = false);
     
    119124
    120125    /** Uploads host @a network data into passed @a data storage unit. */
    121     void loadDataNetworkHost(const CHostNetworkInterface &iface, UIDataSettingsGlobalNetworkHost &data);
     126    void loadToCacheFromNetworkHost(const CHostNetworkInterface &iface, UISettingsCacheGlobalNetworkHost &cache);
    122127    /** Saves @a data to corresponding host network. */
    123     void saveDataNetworkHost(const UIDataSettingsGlobalNetworkHost &data);
     128    bool saveDataNetworkHost(const UISettingsCacheGlobalNetworkHost &cache);
    124129    /** Creates a new item in the host network tree on the basis of passed @a data, @a fChooseItem if requested. */
    125130    void createTreeWidgetItemForNetworkHost(const UIDataSettingsGlobalNetworkHost &data, bool fChooseItem = false);
Note: See TracChangeset for help on using the changeset viewer.

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