VirtualBox

Changeset 88746 in vbox for trunk/src/VBox/Frontends


Ignore:
Timestamp:
Apr 28, 2021 1:08:53 PM (4 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:8400: Rework r130366; More careful checks and coding-style; Return early on errors; Also, no need to search for medium while it's there already.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/medium
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumItem.cpp

    r86065 r88746  
    201201bool UIMediumItem::changeMediumType(KMediumType enmOldType, KMediumType enmNewType)
    202202{
     203    /* Cache the list of VMs the medium is attached to. We will need this for the re-attachment: */
    203204    QList<AttachmentCache> attachmentCacheList;
    204     /* Cache the list of vms the medium is attached to. We will need this for the re-attachment: */
    205205    foreach (const QUuid &uMachineId, medium().curStateMachineIds())
    206206    {
     
    208208        if (comMachine.isNull())
    209209            continue;
    210         CMediumAttachmentVector attachments = comMachine.GetMediumAttachments();
    211         foreach (const CMediumAttachment &attachment, attachments)
    212         {
    213             const CMedium& comMedium = attachment.GetMedium();
    214             if (comMedium.isNull() || comMedium.GetId() != id())
     210        foreach (const CStorageController &comController, comMachine.GetStorageControllers())
     211        {
     212            if (comController.isNull())
    215213                continue;
    216             AttachmentCache attachmentCache;
    217             attachmentCache.m_uMachineId = uMachineId;
    218             attachmentCache.m_strControllerName = attachment.GetController();
    219             attachmentCache.m_port = attachment.GetPort();
    220             attachmentCache.m_device = attachment.GetDevice();
    221             attachmentCacheList << attachmentCache;
    222         }
    223     }
    224 
    225     /* Detach the medium from all the vms it is already attached to: */
     214            const QString strControllerName = comController.GetName();
     215            foreach (const CMediumAttachment &comAttachment, comMachine.GetMediumAttachmentsOfController(strControllerName))
     216            {
     217                if (comAttachment.isNull())
     218                    continue;
     219                const CMedium &comMedium = comAttachment.GetMedium();
     220                if (comMedium.isNull() || comMedium.GetId() != id())
     221                    continue;
     222                AttachmentCache attachmentCache;
     223                attachmentCache.m_uMachineId = uMachineId;
     224                attachmentCache.m_strControllerName = strControllerName;
     225                attachmentCache.m_enmControllerBus = comController.GetBus();
     226                attachmentCache.m_iAttachmentPort = comAttachment.GetPort();
     227                attachmentCache.m_iAttachmentDevice = comAttachment.GetDevice();
     228                attachmentCacheList << attachmentCache;
     229            }
     230        }
     231    }
     232
     233    /* Detach the medium from all the VMs it's attached to: */
    226234    if (!release(true))
    227235        return false;
    228236
    229     /* Search for corresponding medium: */
    230     CMedium comMedium = uiCommon().medium(id()).medium();
    231 
    232237    /* Attempt to change medium type: */
     238    CMedium comMedium = medium().medium();
    233239    comMedium.SetType(enmNewType);
    234     bool fSuccess = true;
    235     /* Show error message if necessary: */
    236     if (!comMedium.isOk() && parentTree())
    237     {
    238         msgCenter().cannotChangeMediumType(comMedium, enmOldType, enmNewType, treeWidget());
    239         fSuccess = false;
    240     }
    241     /* Reattach the medium to all the vms it was previously attached: */
     240    if (!comMedium.isOk())
     241    {
     242        msgCenter().cannotChangeMediumType(comMedium, enmOldType, enmNewType, parentTree());
     243        return false;
     244    }
     245
     246    /* Reattach the medium to all the VMs it was previously attached: */
    242247    foreach (const AttachmentCache &attachmentCache, attachmentCacheList)
    243         attachTo(attachmentCache);
    244     return fSuccess;
     248        if (!attachTo(attachmentCache))
     249            return false;
     250
     251    /* True finally: */
     252    return true;
    245253}
    246254
     
    350358bool UIMediumItem::attachTo(const AttachmentCache &attachmentCache)
    351359{
     360    /* Open session: */
     361    CSession comSession = uiCommon().openSession(attachmentCache.m_uMachineId);
     362    if (comSession.isNull())
     363        return false;
     364
     365    /* Attach device to machine: */
    352366    CMedium comMedium = medium().medium();
    353 
    354     if (comMedium.isNull())
    355         return false;
    356 
    357     /* Open session: */
    358     CSession session = uiCommon().openSession(attachmentCache.m_uMachineId);
    359     if (session.isNull())
    360         return false;
    361 
    362     /* Get machine: */
    363     CMachine machine = session.GetMachine();
    364 
    365     bool fSuccess = false;
    366     machine.AttachDevice(attachmentCache.m_strControllerName, attachmentCache.m_port,
    367                          attachmentCache.m_device, comMedium.GetDeviceType(), comMedium);
    368 
    369     if (machine.isOk())
    370     {
    371         machine.SaveSettings();
    372         if (!machine.isOk())
    373             msgCenter().cannotSaveMachineSettings(machine, treeWidget());
    374         else
    375             fSuccess = true;
     367    KDeviceType enmDeviceType = comMedium.GetDeviceType();
     368    CMachine comMachine = comSession.GetMachine();
     369    comMachine.AttachDevice(attachmentCache.m_strControllerName,
     370                            attachmentCache.m_iAttachmentPort,
     371                            attachmentCache.m_iAttachmentDevice,
     372                            enmDeviceType,
     373                            comMedium);
     374    if (!comMachine.isOk())
     375        msgCenter().cannotAttachDevice(comMachine,
     376                                       mediumTypeToLocal(enmDeviceType),
     377                                       comMedium.GetLocation(),
     378                                       StorageSlot(attachmentCache.m_enmControllerBus,
     379                                                   attachmentCache.m_iAttachmentPort,
     380                                                   attachmentCache.m_iAttachmentDevice),
     381                                       parentTree());
     382    else
     383    {
     384        /* Save machine settings: */
     385        comMachine.SaveSettings();
     386        if (!comMachine.isOk())
     387            msgCenter().cannotSaveMachineSettings(comMachine, parentTree());
    376388    }
    377389
    378390    /* Close session: */
    379     session.UnlockMachine();
    380 
    381     /* Return result: */
    382     return fSuccess;
     391    comSession.UnlockMachine();
     392
     393    /* True finally: */
     394    return true;
    383395}
    384396
  • trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumItem.h

    r82968 r88746  
    121121
    122122private:
     123
    123124    /** A simple struct used to save some parameters of machine device attachment.
    124       * Used for re-attaching the medium to vms after a medium type change. */
     125      * Used for re-attaching the medium to VMs after a medium type change. */
    125126    struct AttachmentCache
    126127    {
    127         QString m_strControllerName;
    128         QUuid   m_uMachineId;
    129         LONG    m_port;
    130         LONG    m_device;
     128        /** Holds the machine ID. */
     129        QUuid        m_uMachineId;
     130        /** Holds the controller name. */
     131        QString      m_strControllerName;
     132        /** Holds the controller bus. */
     133        KStorageBus  m_enmControllerBus;
     134        /** Holds the attachment port. */
     135        LONG         m_iAttachmentPort;
     136        /** Holds the attachment device. */
     137        LONG         m_iAttachmentDevice;
    131138    };
    132139
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