VirtualBox

Changeset 78374 in vbox for trunk/src


Ignore:
Timestamp:
May 3, 2019 11:41:50 AM (6 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:8400. Reataching the medium to the vms after a medium type change.

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

Legend:

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

    r78354 r78374  
    197197        return false;
    198198   return medium().curStateMachineIds().contains(uId);
     199}
     200
     201bool UIMediumItem::changeMediumType(KMediumType enmOldType, KMediumType enmNewType)
     202{
     203    QList<AttachmentCache> attachmentCacheList;
     204    /* Cache the list of vms the medium is attached to. We will need this for the re-attachment: */
     205    foreach (const QUuid &uMachineId, medium().curStateMachineIds())
     206    {
     207        const CMachine &comMachine = vboxGlobal().virtualBox().FindMachine(uMachineId.toString());
     208        if (comMachine.isNull())
     209            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())
     215                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: */
     226    if (!release(true))
     227        return false;
     228
     229    /* Search for corresponding medium: */
     230    CMedium comMedium = vboxGlobal().medium(id()).medium();
     231
     232    /* Attempt to change medium type: */
     233    comMedium.SetType(enmNewType);
     234
     235    /* Show error message if necessary: */
     236    if (!comMedium.isOk() && parentTree())
     237    {
     238        msgCenter().cannotChangeMediumType(comMedium, enmOldType, enmNewType, treeWidget());
     239        return false;
     240    }
     241    /* Reattach the medium to all the vms it was previously attached: */
     242    foreach (const AttachmentCache &attachmentCache, attachmentCacheList)
     243        attachTo(attachmentCache);
     244    return true;
    199245}
    200246
     
    302348}
    303349
     350bool UIMediumItem::attachTo(const AttachmentCache &attachmentCache)
     351{
     352    CMedium comMedium = medium().medium();
     353
     354    if (comMedium.isNull())
     355        return false;
     356
     357    /* Open session: */
     358    CSession session = vboxGlobal().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;
     376    }
     377
     378    /* Close session: */
     379    session.UnlockMachine();
     380
     381    /* Return result: */
     382    return fSuccess;
     383}
     384
    304385/* static */
    305386QString UIMediumItem::formatFieldText(const QString &strText, bool fCompact /* = true */,
  • trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumItem.h

    r78353 r78374  
    110110    /** Returns true if the medium is attached to the vm with @p uId. */
    111111    bool isMediumAttachedTo(QUuid uId);
     112    bool changeMediumType(KMediumType enmOldType, KMediumType enmNewType);
    112113
    113114protected:
     
    120121
    121122private:
     123    /** 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    struct AttachmentCache
     126    {
     127        QString m_strControllerName;
     128        QUuid   m_uMachineId;
     129        LONG    m_port;
     130        LONG    m_device;
     131    };
    122132
    123133    /** Refreshes item information such as icon, text and tool-tip. */
     
    126136    /** Releases UIMedium wrapped by <i>this</i> item from virtual machine with @a uMachineId. */
    127137    bool releaseFrom(const QUuid &uMachineId);
     138    /** Is called by detaching the medium and modifiying it to restore the attachement. */
     139    bool attachTo(const AttachmentCache &attachmentCache);
    128140
    129141    /** Formats field text. */
  • trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumManager.cpp

    r78353 r78374  
    274274        && newData.m_options.m_enmMediumType != oldData.m_options.m_enmMediumType)
    275275    {
    276         /* Check if we need to release medium first: */
    277         bool fDo = true;
    278         if (   pMediumItem->machineIds().size() > 1
    279             || (   (   newData.m_options.m_enmMediumType == KMediumType_Immutable
    280                     || newData.m_options.m_enmMediumType == KMediumType_MultiAttach)
    281                 && pMediumItem->machineIds().size() > 0))
    282             fDo = pMediumItem->release(true);
    283 
    284         if (fDo)
    285         {
    286             comMedium.SetType(newData.m_options.m_enmMediumType);
    287 
    288             /* Show error message if necessary: */
    289             if (!comMedium.isOk())
    290                 msgCenter().cannotChangeMediumType(comMedium, oldData.m_options.m_enmMediumType, newData.m_options.m_enmMediumType, this);
    291         }
     276        pMediumItem->changeMediumType(oldData.m_options.m_enmMediumType, newData.m_options.m_enmMediumType);
    292277    }
    293278
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