VirtualBox

Ignore:
Timestamp:
May 3, 2011 7:35:48 AM (14 years ago)
Author:
vboxsync
Message:

FE/Qt: 4989: UISettingsCache template integrated into Storage page.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/settings/machine
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/machine/UIMachineSettingsStorage.cpp

    r36922 r36923  
    17841784    UISettingsPageMachine::fetchData(data);
    17851785
    1786     /* Fill internal variables with corresponding values: */
    1787     m_cache.m_strMachineId = m_machine.GetId();
    1788     /* Load controllers list: */
     1786    /* Prepare storage data: */
     1787    UIDataSettingsMachineStorage storageData;
     1788
     1789    /* Gather storage data: */
     1790    storageData.m_strMachineId = m_machine.GetId();
     1791
     1792    /* For each controller: */
    17891793    const CStorageControllerVector &controllers = m_machine.GetStorageControllers();
    17901794    for (int iControllerIndex = 0; iControllerIndex < controllers.size(); ++iControllerIndex)
    17911795    {
    1792         /* Prepare controller item: */
     1796        /* Prepare storage controller data: */
     1797        UIDataSettingsMachineStorageController storageControllerData;
     1798
     1799        /* Check if controller is valid: */
    17931800        const CStorageController &controller = controllers[iControllerIndex];
    1794         UIStorageControllerData controllerData;
    1795         controllerData.m_strControllerName = controller.GetName();
    1796         controllerData.m_controllerBus = controller.GetBus();
    1797         controllerData.m_controllerType = controller.GetControllerType();
    1798         controllerData.m_fUseHostIOCache = controller.GetUseHostIOCache();
    1799         /* Load attachments list: */
    1800         const CMediumAttachmentVector &attachments = m_machine.GetMediumAttachmentsOfController(controllerData.m_strControllerName);
    1801         for (int iAttachmentIndex = 0; iAttachmentIndex < attachments.size(); ++iAttachmentIndex)
    1802         {
    1803             /* Prepare attachment item: */
    1804             const CMediumAttachment &attachment = attachments[iAttachmentIndex];
    1805             UIStorageAttachmentData attachmentData;
    1806             attachmentData.m_attachmentType = attachment.GetType();
    1807             attachmentData.m_iAttachmentPort = attachment.GetPort();
    1808             attachmentData.m_iAttachmentDevice = attachment.GetDevice();
    1809             attachmentData.m_fAttachmentPassthrough = attachment.GetPassthrough();
    1810             CMedium comMedium(attachment.GetMedium());
    1811             VBoxMedium vboxMedium;
    1812             vboxGlobal().findMedium(comMedium, vboxMedium);
    1813             attachmentData.m_strAttachmentMediumId = vboxMedium.id();
    1814             controllerData.m_items << attachmentData;
    1815         }
    1816         m_cache.m_items << controllerData;
    1817     }
     1801        if (!controller.isNull())
     1802        {
     1803            /* Gather storage controller data: */
     1804            storageControllerData.m_strControllerName = controller.GetName();
     1805            storageControllerData.m_controllerBus = controller.GetBus();
     1806            storageControllerData.m_controllerType = controller.GetControllerType();
     1807            storageControllerData.m_fUseHostIOCache = controller.GetUseHostIOCache();
     1808
     1809            /* For each attachment: */
     1810            const CMediumAttachmentVector &attachments = m_machine.GetMediumAttachmentsOfController(storageControllerData.m_strControllerName);
     1811            for (int iAttachmentIndex = 0; iAttachmentIndex < attachments.size(); ++iAttachmentIndex)
     1812            {
     1813                /* Prepare storage attachment data: */
     1814                UIDataSettingsMachineStorageAttachment storageAttachmentData;
     1815
     1816                /* Check if attachment is valid: */
     1817                const CMediumAttachment &attachment = attachments[iAttachmentIndex];
     1818                if (!attachment.isNull())
     1819                {
     1820                    /* Gather storage attachment data: */
     1821                    storageAttachmentData.m_attachmentType = attachment.GetType();
     1822                    storageAttachmentData.m_iAttachmentPort = attachment.GetPort();
     1823                    storageAttachmentData.m_iAttachmentDevice = attachment.GetDevice();
     1824                    storageAttachmentData.m_fAttachmentPassthrough = attachment.GetPassthrough();
     1825                    CMedium comMedium(attachment.GetMedium());
     1826                    VBoxMedium vboxMedium;
     1827                    vboxGlobal().findMedium(comMedium, vboxMedium);
     1828                    storageAttachmentData.m_strAttachmentMediumId = vboxMedium.id();
     1829                }
     1830
     1831                /* Cache storage attachment data: */
     1832                m_cache.child(iControllerIndex).child(iAttachmentIndex).cacheInitialData(storageAttachmentData);
     1833            }
     1834        }
     1835
     1836        /* Cache storage controller data: */
     1837        m_cache.child(iControllerIndex).cacheInitialData(storageControllerData);
     1838    }
     1839
     1840    /* Cache storage data: */
     1841    m_cache.cacheInitialData(storageData);
    18181842
    18191843    /* Upload machine to data: */
     
    18251849void UIMachineSettingsStorage::getFromCache()
    18261850{
    1827     /* Apply internal variables data to QWidget(s): */
    1828     mStorageModel->setMachineId(m_cache.m_strMachineId);
    1829     for (int iControllerIndex = 0; iControllerIndex < m_cache.m_items.size(); ++iControllerIndex)
    1830     {
    1831         /* Get iterated controller: */
    1832         const UIStorageControllerData &controllerData = m_cache.m_items[iControllerIndex];
     1851    /* Get storage data from cache: */
     1852    const UIDataSettingsMachineStorage &storageData = m_cache.base();
     1853
     1854    /* Load storage data to page: */
     1855    mStorageModel->setMachineId(storageData.m_strMachineId);
     1856
     1857    /* For each storage controller: */
     1858    for (int iControllerIndex = 0; iControllerIndex < m_cache.childCount(); ++iControllerIndex)
     1859    {
     1860        /* Get storage controller cache: */
     1861        const UICacheSettingsMachineStorageController &controllerCache = m_cache.child(iControllerIndex);
     1862        /* Get storage controller data from cache: */
     1863        const UIDataSettingsMachineStorageController &controllerData = controllerCache.base();
     1864
     1865        /* Load storage controller data to page: */
    18331866        QModelIndex controllerIndex = mStorageModel->addController(controllerData.m_strControllerName,
    18341867                                                                   controllerData.m_controllerBus,
     
    18361869        QUuid controllerId = QUuid(mStorageModel->data(controllerIndex, StorageModel::R_ItemId).toString());
    18371870        mStorageModel->setData(controllerIndex, controllerData.m_fUseHostIOCache, StorageModel::R_CtrIoCache);
    1838         for (int iAttachmentIndex = 0; iAttachmentIndex < controllerData.m_items.size(); ++iAttachmentIndex)
    1839         {
    1840             /* Get iterated attachment: */
    1841             const UIStorageAttachmentData &attachmentData = controllerData.m_items[iAttachmentIndex];
     1871
     1872        /* For each storage attachment: */
     1873        for (int iAttachmentIndex = 0; iAttachmentIndex < controllerCache.childCount(); ++iAttachmentIndex)
     1874        {
     1875            /* Get storage attachment cache: */
     1876            const UICacheSettingsMachineStorageAttachment &attachmentCache = controllerCache.child(iAttachmentIndex);
     1877            /* Get storage controller data from cache: */
     1878            const UIDataSettingsMachineStorageAttachment &attachmentData = attachmentCache.base();
     1879
     1880            /* Load storage attachment data to page: */
    18421881            QModelIndex attachmentIndex = mStorageModel->addAttachment(controllerId, attachmentData.m_attachmentType, attachmentData.m_strAttachmentMediumId);
    18431882            StorageSlot attachmentStorageSlot(controllerData.m_controllerBus,
     
    18631902void UIMachineSettingsStorage::putToCache()
    18641903{
    1865     /* Gather internal variables data from QWidget(s): */
    1866     m_cache.m_items.clear();
     1904    /* Prepare storage data: */
     1905    UIDataSettingsMachineStorage storageData;
     1906
     1907    /* For each storage controller: */
    18671908    QModelIndex rootIndex = mStorageModel->root();
    18681909    for (int iControllerIndex = 0; iControllerIndex < mStorageModel->rowCount(rootIndex); ++iControllerIndex)
    18691910    {
     1911        /* Prepare storage controller data & key: */
     1912        UIDataSettingsMachineStorageController controllerData;
     1913
     1914        /* Gather storage controller data: */
    18701915        QModelIndex controllerIndex = mStorageModel->index(iControllerIndex, 0, rootIndex);
    1871         UIStorageControllerData controllerData;
    18721916        controllerData.m_strControllerName = mStorageModel->data(controllerIndex, StorageModel::R_CtrName).toString();
    18731917        controllerData.m_controllerBus = mStorageModel->data(controllerIndex, StorageModel::R_CtrBusType).value<KStorageBus>();
    18741918        controllerData.m_controllerType = mStorageModel->data(controllerIndex, StorageModel::R_CtrType).value<KStorageControllerType>();
    18751919        controllerData.m_fUseHostIOCache = mStorageModel->data(controllerIndex, StorageModel::R_CtrIoCache).toBool();
     1920
     1921        /* For each storage attachment: */
    18761922        for (int iAttachmentIndex = 0; iAttachmentIndex < mStorageModel->rowCount(controllerIndex); ++iAttachmentIndex)
    18771923        {
     1924            /* Prepare storage attachment data & key: */
     1925            UIDataSettingsMachineStorageAttachment attachmentData;
     1926
     1927            /* Gather storage controller data: */
    18781928            QModelIndex attachmentIndex = mStorageModel->index(iAttachmentIndex, 0, controllerIndex);
    1879             UIStorageAttachmentData attachmentData;
    18801929            attachmentData.m_attachmentType = mStorageModel->data(attachmentIndex, StorageModel::R_AttDevice).value<KDeviceType>();
    18811930            StorageSlot attachmentSlot = mStorageModel->data(attachmentIndex, StorageModel::R_AttSlot).value<StorageSlot>();
     
    18841933            attachmentData.m_fAttachmentPassthrough = mStorageModel->data(attachmentIndex, StorageModel::R_AttIsPassthrough).toBool();
    18851934            attachmentData.m_strAttachmentMediumId = mStorageModel->data(attachmentIndex, StorageModel::R_AttMediumId).toString();
    1886             controllerData.m_items << attachmentData;
    1887         }
    1888         m_cache.m_items << controllerData;
    1889     }
     1935
     1936            /* Recache storage attachment data: */
     1937            m_cache.child(iControllerIndex).child(iAttachmentIndex).cacheCurrentData(attachmentData);
     1938        }
     1939
     1940        /* Recache storage controller data: */
     1941        m_cache.child(iControllerIndex).cacheCurrentData(controllerData);
     1942    }
     1943
     1944    /* Recache storage data: */
     1945    m_cache.cacheCurrentData(storageData);
    18901946}
    18911947
     
    18971953    UISettingsPageMachine::fetchData(data);
    18981954
    1899     /* Save settings depending on dialog type: */
    1900     switch (dialogType())
    1901     {
    1902         /* Here come the properties which could be changed only in offline state: */
    1903         case SettingsDialogType_Offline:
    1904         {
    1905             /* Remove currently present controllers & attachments */
    1906             const CStorageControllerVector &controllers = m_machine.GetStorageControllers();
    1907             for (int iControllerIndex = 0; iControllerIndex < controllers.size(); ++iControllerIndex)
    1908             {
    1909                 /* Get iterated controller: */
    1910                 const CStorageController &controller = controllers[iControllerIndex];
    1911                 QString strControllerName(controller.GetName());
    1912                 const CMediumAttachmentVector &attachments = m_machine.GetMediumAttachmentsOfController(strControllerName);
    1913                 /* Remove all the attachments first: */
    1914                 for (int iAttachmentIndex = 0; iAttachmentIndex < attachments.size(); ++iAttachmentIndex)
    1915                 {
    1916                     /* Get iterated attachment: */
    1917                     const CMediumAttachment &attachment = attachments[iAttachmentIndex];
    1918                     m_machine.DetachDevice(strControllerName, attachment.GetPort(), attachment.GetDevice());
    1919                 }
    1920                 /* Remove the controller itself finally: */
    1921                 m_machine.RemoveStorageController(strControllerName);
    1922             }
    1923             /* Save created controllers: */
    1924             for (int iControllerIndex = 0; iControllerIndex < m_cache.m_items.size() && !failed(); ++iControllerIndex)
    1925             {
    1926                 /* Get iterated controller's data: */
    1927                 const UIStorageControllerData &controllerData = m_cache.m_items[iControllerIndex];
    1928                 CStorageController controller = m_machine.AddStorageController(controllerData.m_strControllerName, controllerData.m_controllerBus);
    1929                 controller.SetControllerType(controllerData.m_controllerType);
    1930                 controller.SetUseHostIOCache(controllerData.m_fUseHostIOCache);
    1931                 int cMaxUsedPort = -1;
    1932                 /* Save created attachments: */
    1933                 for (int iAttachmentIndex = 0; iAttachmentIndex < controllerData.m_items.size() && !failed(); ++iAttachmentIndex)
    1934                 {
    1935                     /* Get iterated attachment: */
    1936                     const UIStorageAttachmentData &attachmentData = controllerData.m_items[iAttachmentIndex];
    1937                     /* Search for newly assigned medium in GUI cache: */
    1938                     VBoxMedium vboxMedium = vboxGlobal().findMedium(attachmentData.m_strAttachmentMediumId);
    1939                     /* Get corresponding COM-wrapped medium object: */
    1940                     CMedium comMedium = vboxMedium.medium();
    1941                     /* Add corresponding attachment: */
    1942                     m_machine.AttachDevice(controllerData.m_strControllerName,
    1943                                            attachmentData.m_iAttachmentPort, attachmentData.m_iAttachmentDevice,
    1944                                            attachmentData.m_attachmentType, comMedium);
    1945                     if (m_machine.isOk())
    1946                     {
    1947                         if (attachmentData.m_attachmentType == KDeviceType_DVD)
    1948                             m_machine.PassthroughDevice(controllerData.m_strControllerName,
    1949                                                         attachmentData.m_iAttachmentPort, attachmentData.m_iAttachmentDevice,
    1950                                                         attachmentData.m_fAttachmentPassthrough);
    1951                         cMaxUsedPort = attachmentData.m_iAttachmentPort > cMaxUsedPort ? attachmentData.m_iAttachmentPort : cMaxUsedPort;
    1952                     }
    1953                     else
    1954                     {
    1955                         /* Mark the page as failed: */
    1956                         setFailed(true);
    1957                         /* Show error message: */
    1958                         vboxProblem().cannotAttachDevice(m_machine,
    1959                                                          vboxGlobal().mediumTypeToLocal(attachmentData.m_attachmentType),
    1960                                                          vboxMedium.location(),
    1961                                                          StorageSlot(controllerData.m_controllerBus,
    1962                                                                      attachmentData.m_iAttachmentPort,
    1963                                                                      attachmentData.m_iAttachmentDevice),
    1964                                                          this);
    1965                     }
    1966                 }
    1967                 if (!failed() && controllerData.m_controllerBus == KStorageBus_SATA)
    1968                 {
    1969                     ULONG uSataPortsCount = cMaxUsedPort + 1;
    1970                     uSataPortsCount = qMax(uSataPortsCount, controller.GetMinPortCount());
    1971                     uSataPortsCount = qMin(uSataPortsCount, controller.GetMaxPortCount());
    1972                     controller.SetPortCount(uSataPortsCount);
    1973                 }
    1974             }
    1975             break;
    1976         }
    1977         /* Here come the properties which could be changed at runtime too: */
    1978         case SettingsDialogType_Online:
    1979         {
    1980             /* Iterate all the controllers and update changed CD/DVD and floppy attachments: */
    1981             for (int iControllerIndex = 0; iControllerIndex < m_cache.m_items.size() && !failed(); ++iControllerIndex)
    1982             {
    1983                 /* Get iterated controller: */
    1984                 const UIStorageControllerData &controllerData = m_cache.m_items[iControllerIndex];
    1985                 CStorageController controller = m_machine.GetStorageControllerByName(controllerData.m_strControllerName);
    1986                 AssertMsg(!controller.isNull(), ("Corresponding storage controller must be present!\n"));
    1987                 /* Save changed attachments: */
    1988                 for (int iAttachmentIndex = 0; iAttachmentIndex < controllerData.m_items.size() && !failed(); ++iAttachmentIndex)
    1989                 {
    1990                     /* Get iterated attachment: */
    1991                     const UIStorageAttachmentData &attachmentData = controllerData.m_items[iAttachmentIndex];
    1992                     /* Update only DVD and floppy attachments: */
    1993                     if (attachmentData.m_attachmentType != KDeviceType_DVD &&
    1994                         attachmentData.m_attachmentType != KDeviceType_Floppy)
    1995                         continue;
    1996                     /* Search for newly assigned medium in GUI cache: */
    1997                     VBoxMedium vboxMedium = vboxGlobal().findMedium(attachmentData.m_strAttachmentMediumId);
    1998                     /* Get corresponding COM-wrapped medium object: */
    1999                     CMedium comMedium = vboxMedium.medium();
    2000                     /* Update corresponding medium: */
    2001                     m_machine.MountMedium(controllerData.m_strControllerName,
    2002                                           attachmentData.m_iAttachmentPort, attachmentData.m_iAttachmentDevice,
    2003                                           comMedium, true /* may the force be with you */);
    2004                     if (!m_machine.isOk())
    2005                     {
    2006                         /* Mark the page as failed: */
    2007                         setFailed(true);
    2008                         /* Show error message: */
    2009                         vboxProblem().cannotAttachDevice(m_machine,
    2010                                                          vboxGlobal().mediumTypeToLocal(attachmentData.m_attachmentType),
    2011                                                          vboxMedium.location(),
    2012                                                          StorageSlot(controllerData.m_controllerBus,
    2013                                                                      attachmentData.m_iAttachmentPort,
    2014                                                                      attachmentData.m_iAttachmentDevice),
    2015                                                          this);
    2016                     }
    2017                 }
    2018             }
    2019             break;
    2020         }
    2021         default:
    2022             break;
    2023     }
     1955    /* Update storage data, update failing state: */
     1956    setFailed(!updateStorageData());
    20241957
    20251958    /* Upload machine to data: */
     
    30843017}
    30853018
     3019bool UIMachineSettingsStorage::updateStorageData()
     3020{
     3021    /* Prepare result: */
     3022    bool fSuccess = m_machine.isOk();
     3023    if (fSuccess)
     3024    {
     3025        /* Check if storage data was changed: */
     3026        if (m_cache.wasChanged())
     3027        {
     3028            /* For each controller (removing step): */
     3029            for (int iControllerIndex = 0; iControllerIndex < m_cache.childCount(); ++iControllerIndex)
     3030            {
     3031                /* Get controller cache: */
     3032                const UICacheSettingsMachineStorageController &controllerCache = m_cache.child(iControllerIndex);
     3033
     3034                /* Remove controllers marked for 'remove' and 'update' (if they can't be updated): */
     3035                if (controllerCache.wasRemoved() || (controllerCache.wasUpdated() && !isControllerCouldBeUpdated(controllerCache)))
     3036                    fSuccess = removeStorageController(controllerCache);
     3037
     3038                /* Update controllers marked for 'update' (if they can be updated): */
     3039                if (controllerCache.wasUpdated() && isControllerCouldBeUpdated(controllerCache))
     3040                {
     3041                    /* For each attachment (removing step): */
     3042                    for (int iAttachmentIndex = 0; iAttachmentIndex < controllerCache.childCount(); ++iAttachmentIndex)
     3043                    {
     3044                        /* Get attachment cache: */
     3045                        const UICacheSettingsMachineStorageAttachment &attachmentCache = controllerCache.child(iAttachmentIndex);
     3046
     3047                        /* Remove attachments marked for 'remove' and 'update' (if they can't be updated): */
     3048                        if (attachmentCache.wasRemoved() || (attachmentCache.wasUpdated() && !isAttachmentCouldBeUpdated(attachmentCache)))
     3049                            fSuccess = removeStorageAttachment(controllerCache, attachmentCache);
     3050                    }
     3051                }
     3052            }
     3053            /* For each controller (creating step): */
     3054            for (int iControllerIndex = 0; iControllerIndex < m_cache.childCount(); ++iControllerIndex)
     3055            {
     3056                /* Get controller cache: */
     3057                UICacheSettingsMachineStorageController controllerCache = m_cache.child(iControllerIndex);
     3058
     3059                /* Create controllers marked for 'create' or 'update' (if they can't be updated): */
     3060                if (controllerCache.wasCreated() || (controllerCache.wasUpdated() && !isControllerCouldBeUpdated(controllerCache)))
     3061                    fSuccess = createStorageController(controllerCache);
     3062
     3063                /* Update controllers marked for 'update' (if they can be updated): */
     3064                if (controllerCache.wasUpdated() && isControllerCouldBeUpdated(controllerCache))
     3065                    fSuccess = updateStorageController(controllerCache);
     3066            }
     3067        }
     3068    }
     3069    /* Return result: */
     3070    return fSuccess;
     3071}
     3072
     3073bool UIMachineSettingsStorage::removeStorageController(const UICacheSettingsMachineStorageController &controllerCache)
     3074{
     3075    /* Prepare result: */
     3076    bool fSuccess = m_machine.isOk();
     3077    if (fSuccess)
     3078    {
     3079        /* Get storage controller data form cache: */
     3080        const UIDataSettingsMachineStorageController &controllerData = controllerCache.base();
     3081
     3082        /* Get storage controller name: */
     3083        QString strControllerName = controllerData.m_strControllerName;
     3084
     3085        /* Check that storage controller exists: */
     3086        const CStorageController &controller = m_machine.GetStorageControllerByName(strControllerName);
     3087        /* Check that machine is OK: */
     3088        fSuccess = m_machine.isOk();
     3089        if (fSuccess && !controller.isNull())
     3090        {
     3091            /* Remove storage attachments first: */
     3092            // TODO: Later, it will be possible to remove controller with all the attachments at one shot!
     3093            /* For each storage attachment: */
     3094            for (int iAttachmentIndex = 0; fSuccess && iAttachmentIndex < controllerCache.childCount(); ++iAttachmentIndex)
     3095                fSuccess = removeStorageAttachment(controllerCache, controllerCache.child(iAttachmentIndex));
     3096            /* Remove storage controller finally: */
     3097            if (fSuccess)
     3098            {
     3099                m_machine.RemoveStorageController(strControllerName);
     3100                /* Check that machine is OK: */
     3101                fSuccess = m_machine.isOk();
     3102            }
     3103        }
     3104    }
     3105    /* Return result: */
     3106    return fSuccess;
     3107}
     3108
     3109bool UIMachineSettingsStorage::createStorageController(const UICacheSettingsMachineStorageController &controllerCache)
     3110{
     3111    /* Prepare result: */
     3112    bool fSuccess = m_machine.isOk();
     3113    if (fSuccess)
     3114    {
     3115        /* Get controller data form cache: */
     3116        const UIDataSettingsMachineStorageController &controllerData = controllerCache.data();
     3117
     3118        /* Get storage controller attributes: */
     3119        QString strControllerName = controllerData.m_strControllerName;
     3120        KStorageBus controllerBus = controllerData.m_controllerBus;
     3121        KStorageControllerType controllerType = controllerData.m_controllerType;
     3122        bool fUseHostIOCache = controllerData.m_fUseHostIOCache;
     3123
     3124        /* Check that storage controller doesn't exists: */
     3125        CStorageController controller = m_machine.GetStorageControllerByName(strControllerName);
     3126        /* Check that machine is not OK: */
     3127        fSuccess = !m_machine.isOk();
     3128        /* If controller doesn't exists: */
     3129        if (controller.isNull())
     3130        {
     3131            /* Create new storage controller: */
     3132            controller = m_machine.AddStorageController(strControllerName, controllerBus);
     3133            /* Check that machine is OK: */
     3134            fSuccess = m_machine.isOk();
     3135            if (!controller.isNull())
     3136            {
     3137                /* Set storage controller attributes: */
     3138                controller.SetControllerType(controllerType);
     3139                controller.SetUseHostIOCache(fUseHostIOCache);
     3140
     3141                /* Prepare max used port for SATA controller: */
     3142                int cMaxUsedPort = -1;
     3143
     3144                /* For each storage attachment: */
     3145                for (int iAttachmentIndex = 0; fSuccess && iAttachmentIndex < controllerCache.childCount(); ++iAttachmentIndex)
     3146                {
     3147                    /* Get storage attachment cache: */
     3148                    const UICacheSettingsMachineStorageAttachment &attachmentCache = controllerCache.child(iAttachmentIndex);
     3149
     3150                    /* If storage attachment data was not removed: */
     3151                    if (!attachmentCache.wasRemoved())
     3152                        fSuccess = createStorageAttachment(controllerCache, attachmentCache);
     3153
     3154                    /* Check that machine is OK: */
     3155                    if (fSuccess)
     3156                    {
     3157                        /* Get storage attachment data: */
     3158                        const UIDataSettingsMachineStorageAttachment &attachmentData = attachmentCache.data();
     3159                        /* Calculate the number of used ports: */
     3160                        cMaxUsedPort = attachmentData.m_iAttachmentPort > cMaxUsedPort ? attachmentData.m_iAttachmentPort : cMaxUsedPort;
     3161                    }
     3162                }
     3163
     3164                /* Set max used port for SATA controller: */
     3165                if (fSuccess && controllerData.m_controllerBus == KStorageBus_SATA)
     3166                {
     3167                    ULONG uSataPortsCount = cMaxUsedPort + 1;
     3168                    uSataPortsCount = qMax(uSataPortsCount, controller.GetMinPortCount());
     3169                    uSataPortsCount = qMin(uSataPortsCount, controller.GetMaxPortCount());
     3170                    controller.SetPortCount(uSataPortsCount);
     3171                }
     3172            }
     3173        }
     3174    }
     3175    /* Return result: */
     3176    return fSuccess;
     3177}
     3178
     3179bool UIMachineSettingsStorage::updateStorageController(const UICacheSettingsMachineStorageController &controllerCache)
     3180{
     3181    /* Prepare result: */
     3182    bool fSuccess = m_machine.isOk();
     3183    if (fSuccess)
     3184    {
     3185        /* Get controller data form cache: */
     3186        const UIDataSettingsMachineStorageController &controllerData = controllerCache.data();
     3187
     3188        /* Get storage controller attributes: */
     3189        QString strControllerName = controllerData.m_strControllerName;
     3190        KStorageBus controllerBus = controllerData.m_controllerBus;
     3191        KStorageControllerType controllerType = controllerData.m_controllerType;
     3192        bool fUseHostIOCache = controllerData.m_fUseHostIOCache;
     3193
     3194        /* Check that controller exists: */
     3195        CStorageController controller = m_machine.GetStorageControllerByName(strControllerName);
     3196        /* Check that machine is OK: */
     3197        fSuccess = m_machine.isOk();
     3198        if (!controller.isNull())
     3199        {
     3200            /* Set storage controller attributes: */
     3201            controller.SetControllerType(controllerType);
     3202            controller.SetUseHostIOCache(fUseHostIOCache);
     3203
     3204            /* Prepare max used port for SATA controller: */
     3205            int cMaxUsedPort = -1;
     3206
     3207            /* For each storage attachment: */
     3208            for (int iAttachmentIndex = 0; fSuccess && iAttachmentIndex < controllerCache.childCount(); ++iAttachmentIndex)
     3209            {
     3210                /* Get storage attachment cache: */
     3211                const UICacheSettingsMachineStorageAttachment &attachmentCache = controllerCache.child(iAttachmentIndex);
     3212
     3213                /* Create attachments marked for 'create' and 'update' (if they can't be updated): */
     3214                if (attachmentCache.wasCreated() || (attachmentCache.wasUpdated() && !isAttachmentCouldBeUpdated(attachmentCache)))
     3215                    fSuccess = createStorageAttachment(controllerCache, attachmentCache);
     3216
     3217                /* Update attachments marked for 'update' (if they can be updated): */
     3218                if (attachmentCache.wasUpdated() && isAttachmentCouldBeUpdated(attachmentCache))
     3219                    fSuccess = updateStorageAttachment(controllerCache, attachmentCache);
     3220
     3221                /* Check that machine is OK: */
     3222                if (fSuccess && (attachmentCache.wasCreated() || attachmentCache.wasUpdated()))
     3223                {
     3224                    /* Get storage attachment data: */
     3225                    const UIDataSettingsMachineStorageAttachment &attachmentData = attachmentCache.data();
     3226                    /* Calculate the number of used ports: */
     3227                    cMaxUsedPort = attachmentData.m_iAttachmentPort > cMaxUsedPort ? attachmentData.m_iAttachmentPort : cMaxUsedPort;
     3228                }
     3229            }
     3230
     3231            /* Set max used port for SATA controller: */
     3232            if (fSuccess && controllerBus == KStorageBus_SATA)
     3233            {
     3234                ULONG uSataPortsCount = cMaxUsedPort + 1;
     3235                uSataPortsCount = qMax(uSataPortsCount, controller.GetMinPortCount());
     3236                uSataPortsCount = qMin(uSataPortsCount, controller.GetMaxPortCount());
     3237                controller.SetPortCount(uSataPortsCount);
     3238            }
     3239        }
     3240    }
     3241    /* Return result: */
     3242    return fSuccess;
     3243}
     3244
     3245bool UIMachineSettingsStorage::removeStorageAttachment(const UICacheSettingsMachineStorageController &controllerCache,
     3246                                                       const UICacheSettingsMachineStorageAttachment &attachmentCache)
     3247{
     3248    /* Prepare result: */
     3249    bool fSuccess = m_machine.isOk();
     3250    if (fSuccess)
     3251    {
     3252        /* Get storage controller data from cache: */
     3253        const UIDataSettingsMachineStorageController &controllerData = controllerCache.base();
     3254        /* Get storage attachment data from cache: */
     3255        const UIDataSettingsMachineStorageAttachment &attachmentData = attachmentCache.base();
     3256
     3257        /* Get storage controller attributes: */
     3258        QString strControllerName = controllerData.m_strControllerName;
     3259        /* Get storage attachment attributes: */
     3260        int iAttachmentPort = attachmentData.m_iAttachmentPort;
     3261        int iAttachmentDevice = attachmentData.m_iAttachmentDevice;
     3262
     3263        /* Check that storage attachment exists: */
     3264        const CMediumAttachment &attachment = m_machine.GetMediumAttachment(strControllerName, iAttachmentPort, iAttachmentDevice);
     3265        /* Check that machine is OK: */
     3266        fSuccess = m_machine.isOk();
     3267        if (fSuccess && !attachment.isNull())
     3268        {
     3269            /* Remove storage attachment: */
     3270            m_machine.DetachDevice(strControllerName, iAttachmentPort, iAttachmentDevice);
     3271            /* Check that machine is OK: */
     3272            fSuccess = m_machine.isOk();
     3273        }
     3274    }
     3275    /* Return result: */
     3276    return fSuccess;
     3277}
     3278
     3279bool UIMachineSettingsStorage::createStorageAttachment(const UICacheSettingsMachineStorageController &controllerCache,
     3280                                                       const UICacheSettingsMachineStorageAttachment &attachmentCache)
     3281{
     3282    /* Prepare result: */
     3283    bool fSuccess = m_machine.isOk();
     3284    if (fSuccess)
     3285    {
     3286        /* Get storage controller data from cache: */
     3287        const UIDataSettingsMachineStorageController &controllerData = controllerCache.data();
     3288        /* Get storage attachment data from cache: */
     3289        const UIDataSettingsMachineStorageAttachment &attachmentData = attachmentCache.data();
     3290
     3291        /* Get storage controller attributes: */
     3292        QString strControllerName = controllerData.m_strControllerName;
     3293        KStorageBus controllerBus = controllerData.m_controllerBus;
     3294        /* Get storage attachment attributes: */
     3295        int iAttachmentPort = attachmentData.m_iAttachmentPort;
     3296        int iAttachmentDevice = attachmentData.m_iAttachmentDevice;
     3297        KDeviceType attachmentDeviceType = attachmentData.m_attachmentType;
     3298        QString strAttachmentMediumId = attachmentData.m_strAttachmentMediumId;
     3299        bool fAttachmentPassthrough = attachmentData.m_fAttachmentPassthrough;
     3300        /* Get GUI medium object: */
     3301        VBoxMedium vboxMedium = vboxGlobal().findMedium(strAttachmentMediumId);
     3302        /* Get COM medium object: */
     3303        CMedium comMedium = vboxMedium.medium();
     3304
     3305        /* Check that storage attachment doesn't exists: */
     3306        const CMediumAttachment &attachment = m_machine.GetMediumAttachment(strControllerName, iAttachmentPort, iAttachmentDevice);
     3307        /* Check that machine is not OK: */
     3308        fSuccess = !m_machine.isOk();
     3309        if (attachment.isNull())
     3310        {
     3311            /* Create storage attachment: */
     3312            m_machine.AttachDevice(strControllerName, iAttachmentPort, iAttachmentDevice, attachmentDeviceType, comMedium);
     3313            /* Check that machine is OK: */
     3314            fSuccess = m_machine.isOk();
     3315            if (fSuccess)
     3316            {
     3317                if (attachmentDeviceType == KDeviceType_DVD)
     3318                {
     3319                    m_machine.PassthroughDevice(strControllerName, iAttachmentPort, iAttachmentDevice, fAttachmentPassthrough);
     3320                    /* Check that machine is OK: */
     3321                    fSuccess = m_machine.isOk();
     3322                }
     3323            }
     3324            else
     3325            {
     3326                /* Show error message: */
     3327                vboxProblem().cannotAttachDevice(m_machine, vboxGlobal().mediumTypeToLocal(attachmentDeviceType),
     3328                                                 vboxMedium.location(),
     3329                                                 StorageSlot(controllerBus, iAttachmentPort, iAttachmentDevice), this);
     3330            }
     3331        }
     3332    }
     3333    /* Return result: */
     3334    return fSuccess;
     3335}
     3336
     3337bool UIMachineSettingsStorage::updateStorageAttachment(const UICacheSettingsMachineStorageController &controllerCache,
     3338                                                       const UICacheSettingsMachineStorageAttachment &attachmentCache)
     3339{
     3340    /* Prepare result: */
     3341    bool fSuccess = m_machine.isOk();
     3342    if (fSuccess)
     3343    {
     3344        /* Get storage controller data from cache: */
     3345        const UIDataSettingsMachineStorageController &controllerData = controllerCache.data();
     3346        /* Get current storage attachment data from cache: */
     3347        const UIDataSettingsMachineStorageAttachment &attachmentData = attachmentCache.data();
     3348
     3349        /* Get storage controller attributes: */
     3350        QString strControllerName = controllerData.m_strControllerName;
     3351        KStorageBus controllerBus = controllerData.m_controllerBus;
     3352        /* Get storage attachment attributes: */
     3353        int iAttachmentPort = attachmentData.m_iAttachmentPort;
     3354        int iAttachmentDevice = attachmentData.m_iAttachmentDevice;
     3355        QString strAttachmentMediumId = attachmentData.m_strAttachmentMediumId;
     3356        bool fAttachmentPassthrough = attachmentData.m_fAttachmentPassthrough;
     3357        KDeviceType attachmentDeviceType = attachmentData.m_attachmentType;
     3358
     3359        /* Check that storage attachment exists: */
     3360        CMediumAttachment attachment = m_machine.GetMediumAttachment(strControllerName, iAttachmentPort, iAttachmentDevice);
     3361        /* Check that machine is OK: */
     3362        fSuccess = m_machine.isOk();
     3363        if (!attachment.isNull())
     3364        {
     3365            /* Get GUI medium object: */
     3366            VBoxMedium vboxMedium = vboxGlobal().findMedium(strAttachmentMediumId);
     3367            /* Get COM medium object: */
     3368            CMedium comMedium = vboxMedium.medium();
     3369            /* Remount storage attachment: */
     3370            m_machine.MountMedium(strControllerName, iAttachmentPort, iAttachmentDevice, comMedium, true /* force? */);
     3371            /* Check that machine is OK: */
     3372            fSuccess = m_machine.isOk();
     3373            if (fSuccess)
     3374            {
     3375                if (isMachineOffline())
     3376                {
     3377                    if (attachmentDeviceType == KDeviceType_DVD)
     3378                    {
     3379                        m_machine.PassthroughDevice(strControllerName, iAttachmentPort, iAttachmentDevice, fAttachmentPassthrough);
     3380                        /* Check that machine is OK: */
     3381                        fSuccess = m_machine.isOk();
     3382                    }
     3383                }
     3384            }
     3385            else
     3386            {
     3387                /* Show error message: */
     3388                vboxProblem().cannotAttachDevice(m_machine, vboxGlobal().mediumTypeToLocal(attachmentDeviceType),
     3389                                                 vboxMedium.location(),
     3390                                                 StorageSlot(controllerBus, iAttachmentPort, iAttachmentDevice), this);
     3391            }
     3392        }
     3393    }
     3394    /* Return result: */
     3395    return fSuccess;
     3396}
     3397
     3398bool UIMachineSettingsStorage::isControllerCouldBeUpdated(const UICacheSettingsMachineStorageController &controllerCache) const
     3399{
     3400    /* IController interface doesn't allows to change 'name' and 'bus' attributes.
     3401     * But those attributes could be changed in GUI directly or indirectly.
     3402     * For such cases we have to recreate IController instance,
     3403     * for other cases we will update controller attributes only. */
     3404    const UIDataSettingsMachineStorageController &oldControllerData = controllerCache.base();
     3405    const UIDataSettingsMachineStorageController &newControllerData = controllerCache.data();
     3406    return newControllerData.m_controllerBus == oldControllerData.m_controllerBus &&
     3407           newControllerData.m_strControllerName == oldControllerData.m_strControllerName;
     3408}
     3409
     3410bool UIMachineSettingsStorage::isAttachmentCouldBeUpdated(const UICacheSettingsMachineStorageAttachment &attachmentCache) const
     3411{
     3412    /* IMediumAttachment could be indirectly updated through IMachine
     3413     * only if attachment device type was NOT changed and is one of the next types:
     3414     * KDeviceType_Floppy or KDeviceType_DVD.
     3415     * For other cases we will recreate attachment fully: */
     3416    const UIDataSettingsMachineStorageAttachment &initialAttachmentData = attachmentCache.base();
     3417    const UIDataSettingsMachineStorageAttachment &currentAttachmentData = attachmentCache.data();
     3418    KDeviceType initialAttachmentDeviceType = initialAttachmentData.m_attachmentType;
     3419    KDeviceType currentAttachmentDeviceType = currentAttachmentData.m_attachmentType;
     3420    return (currentAttachmentDeviceType == initialAttachmentDeviceType) &&
     3421           (currentAttachmentDeviceType == KDeviceType_Floppy || currentAttachmentDeviceType == KDeviceType_DVD);
     3422}
     3423
    30863424void UIMachineSettingsStorage::setDialogType(SettingsDialogType settingsDialogType)
    30873425{
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/machine/UIMachineSettingsStorage.h

    r36589 r36923  
    543543};
    544544
    545 /* Machine settings / Storage page / Attachment data: */
    546 struct UIStorageAttachmentData
    547 {
     545/* Machine settings / Storage page / Storage attachment data: */
     546struct UIDataSettingsMachineStorageAttachment
     547{
     548    /* Default constructor: */
     549    UIDataSettingsMachineStorageAttachment()
     550        : m_attachmentType(KDeviceType_Null)
     551        , m_iAttachmentPort(-1)
     552        , m_iAttachmentDevice(-1)
     553        , m_strAttachmentMediumId(QString())
     554        , m_fAttachmentPassthrough(false) {}
     555    /* Functions: */
     556    bool equal(const UIDataSettingsMachineStorageAttachment &other) const
     557    {
     558        return (m_attachmentType == other.m_attachmentType) &&
     559               (m_iAttachmentPort == other.m_iAttachmentPort) &&
     560               (m_iAttachmentDevice == other.m_iAttachmentDevice) &&
     561               (m_strAttachmentMediumId == other.m_strAttachmentMediumId) &&
     562               (m_fAttachmentPassthrough == other.m_fAttachmentPassthrough);
     563    }
     564    /* Operators: */
     565    bool operator==(const UIDataSettingsMachineStorageAttachment &other) const { return equal(other); }
     566    bool operator!=(const UIDataSettingsMachineStorageAttachment &other) const { return !equal(other); }
     567    /* Variables: */
    548568    KDeviceType m_attachmentType;
    549569    LONG m_iAttachmentPort;
     
    552572    bool m_fAttachmentPassthrough;
    553573};
    554 
    555 /* Machine settings / Storage page / Controller data: */
    556 struct UIStorageControllerData
    557 {
     574typedef UISettingsCache<UIDataSettingsMachineStorageAttachment> UICacheSettingsMachineStorageAttachment;
     575
     576/* Machine settings / Storage page / Storage controller data: */
     577struct UIDataSettingsMachineStorageController
     578{
     579    /* Default constructor: */
     580    UIDataSettingsMachineStorageController()
     581        : m_strControllerName(QString())
     582        , m_controllerBus(KStorageBus_Null)
     583        , m_controllerType(KStorageControllerType_Null)
     584        , m_fUseHostIOCache(false) {}
     585    /* Functions: */
     586    bool equal(const UIDataSettingsMachineStorageController &other) const
     587    {
     588        return (m_strControllerName == other.m_strControllerName) &&
     589               (m_controllerBus == other.m_controllerBus) &&
     590               (m_controllerType == other.m_controllerType) &&
     591               (m_fUseHostIOCache == other.m_fUseHostIOCache);
     592    }
     593    /* Operators: */
     594    bool operator==(const UIDataSettingsMachineStorageController &other) const { return equal(other); }
     595    bool operator!=(const UIDataSettingsMachineStorageController &other) const { return !equal(other); }
     596    /* Variables: */
    558597    QString m_strControllerName;
    559598    KStorageBus m_controllerBus;
    560599    KStorageControllerType m_controllerType;
    561600    bool m_fUseHostIOCache;
    562     QList<UIStorageAttachmentData> m_items;
    563 };
    564 
    565 /* Machine settings / Storage page / Cache: */
    566 struct UISettingsCacheMachineStorage
    567 {
     601};
     602typedef UISettingsCachePool<UIDataSettingsMachineStorageController, UICacheSettingsMachineStorageAttachment> UICacheSettingsMachineStorageController;
     603
     604/* Machine settings / Storage page / Storage data: */
     605struct UIDataSettingsMachineStorage
     606{
     607    /* Default constructor: */
     608    UIDataSettingsMachineStorage()
     609        : m_strMachineId(QString()) {}
     610    /* Operators: */
     611    bool operator==(const UIDataSettingsMachineStorage &other) const { return m_strMachineId == other.m_strMachineId; }
     612    bool operator!=(const UIDataSettingsMachineStorage &other) const { return m_strMachineId != other.m_strMachineId; }
     613    /* Variables: */
    568614    QString m_strMachineId;
    569     QList<UIStorageControllerData> m_items;
    570 };
     615};
     616typedef UISettingsCachePool<UIDataSettingsMachineStorage, UICacheSettingsMachineStorageController> UICacheSettingsMachineStorage;
    571617
    572618/* Machine settings / Storage page: */
     
    672718    void addChooseHostDriveActions(QMenu *pOpenMediumMenu);
    673719    void addRecentMediumActions(QMenu *pOpenMediumMenu, VBoxDefs::MediumType recentMediumType);
     720
     721    bool updateStorageData();
     722    bool removeStorageController(const UICacheSettingsMachineStorageController &controllerCache);
     723    bool createStorageController(const UICacheSettingsMachineStorageController &controllerCache);
     724    bool updateStorageController(const UICacheSettingsMachineStorageController &controllerCache);
     725    bool removeStorageAttachment(const UICacheSettingsMachineStorageController &controllerCache,
     726                                 const UICacheSettingsMachineStorageAttachment &attachmentCache);
     727    bool createStorageAttachment(const UICacheSettingsMachineStorageController &controllerCache,
     728                                 const UICacheSettingsMachineStorageAttachment &attachmentCache);
     729    bool updateStorageAttachment(const UICacheSettingsMachineStorageController &controllerCache,
     730                                 const UICacheSettingsMachineStorageAttachment &attachmentCache);
     731    bool isControllerCouldBeUpdated(const UICacheSettingsMachineStorageController &controllerCache) const;
     732    bool isAttachmentCouldBeUpdated(const UICacheSettingsMachineStorageAttachment &attachmentCache) const;
    674733
    675734    void setDialogType(SettingsDialogType settingsDialogType);
     
    700759
    701760    /* Cache: */
    702     UISettingsCacheMachineStorage m_cache;
     761    UICacheSettingsMachineStorage m_cache;
    703762};
    704763
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