VirtualBox

Ignore:
Timestamp:
Mar 6, 2014 5:51:18 PM (11 years ago)
Author:
vboxsync
Message:

FE/Qt: Medium Manager rework/cleanup: Rework medium-item *release* method to be polymorphic call.

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

Legend:

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

    r49596 r50719  
    11671167}
    11681168
    1169 bool UIMessageCenter::confirmMediumRelease(const UIMedium &medium, const QString &strUsage, QWidget *pParent /* = 0*/) const
    1170 {
     1169bool UIMessageCenter::confirmMediumRelease(const UIMedium &medium, QWidget *pParent /* = 0*/) const
     1170{
     1171    /* Prepare the usage: */
     1172    QStringList usage;
     1173    CVirtualBox vbox = vboxGlobal().virtualBox();
     1174    foreach (const QString &strMachineID, medium.curStateMachineIds())
     1175    {
     1176        CMachine machine = vbox.FindMachine(strMachineID);
     1177        if (!vbox.isOk() || machine.isNull())
     1178            continue;
     1179        usage << machine.GetName();
     1180    }
    11711181    /* Prepare the message: */
    11721182    QString strMessage;
     
    11961206    /* Show the question: */
    11971207    return questionBinary(pParent, MessageType_Question,
    1198                           strMessage.arg(medium.location(), strUsage),
     1208                          strMessage.arg(medium.location(), usage.join(", ")),
    11991209                          0 /* auto-confirm id */,
    12001210                          tr("Release", "detach medium"));
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIMessageCenter.h

    r49596 r50719  
    242242    /* API: Virtual Medium Manager warnings: */
    243243    void cannotChangeMediumType(const CMedium &medium, KMediumType oldMediumType, KMediumType newMediumType, QWidget *pParent = 0) const;
    244     bool confirmMediumRelease(const UIMedium &medium, const QString &strUsage, QWidget *pParent = 0) const;
     244    bool confirmMediumRelease(const UIMedium &medium, QWidget *pParent = 0) const;
    245245    bool confirmMediumRemoval(const UIMedium &medium, QWidget *pParent = 0) const;
    246246    int confirmDeleteHardDiskStorage(const QString &strLocation, QWidget *pParent = 0) const;
  • trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumManager.cpp

    r50717 r50719  
    8080    /** Remove UIMedium wrapped by <i>this</i> item. */
    8181    virtual bool remove() = 0;
    82 
    83     /** Refreshes item fully. */
     82    /** Release UIMedium wrapped by <i>this</i> item. */
     83    virtual bool release()
     84    {
     85        /* Refresh: */
     86        refreshAll();
     87
     88        /* Make sure medium was not released yet: */
     89        if (medium().curStateMachineIds().isEmpty())
     90            return true;
     91
     92        /* Confirm release: */
     93        if (!msgCenter().confirmMediumRelease(medium(), treeWidget()))
     94            return false;
     95
     96        /* Release: */
     97        foreach (const QString &strMachineID, medium().curStateMachineIds())
     98            if (!releaseFrom(strMachineID))
     99                return false;
     100
     101        /* True by default: */
     102        return true;
     103    }
     104
     105    /** Refresh item fully. */
    84106    void refreshAll()
    85107    {
     
    136158    }
    137159
     160protected:
     161
     162    /** Release UIMedium wrapped by <i>this</i> item from virtual @a machine. */
     163    virtual bool releaseFrom(CMachine machine) = 0;
     164
    138165private:
    139166
    140     /** Refreshes item information such as icon, text and tool-tip. */
     167    /** Refresh item information such as icon, text and tool-tip. */
    141168    void refresh()
    142169    {
     
    152179    }
    153180
     181    /** Release UIMedium wrapped by <i>this</i> item from virtual machine with @a strMachineID. */
     182    bool releaseFrom(const QString &strMachineID)
     183    {
     184        /* Open session: */
     185        CSession session = vboxGlobal().openSession(strMachineID);
     186        if (session.isNull())
     187            return false;
     188
     189        /* Get machine: */
     190        CMachine machine = session.GetMachine();
     191
     192        /* Prepare result: */
     193        bool fSuccess = false;
     194
     195        /* Release medium from machine: */
     196        if (releaseFrom(machine))
     197        {
     198            /* Save machine settings: */
     199            machine.SaveSettings();
     200            if (!machine.isOk())
     201                msgCenter().cannotSaveMachineSettings(machine, treeWidget());
     202            else
     203                fSuccess = true;
     204        }
     205
     206        /* Close session: */
     207        session.UnlockMachine();
     208
     209        /* Return result: */
     210        return fSuccess;
     211    }
     212
    154213    /** UIMedium wrapped by <i>this</i> item. */
    155214    UIMedium m_medium;
     
    242301        /* True by default: */
    243302        return true;
     303    }
     304
     305    /** Release UIMedium wrapped by <i>this</i> item from virtual @a machine. */
     306    bool releaseFrom(CMachine machine)
     307    {
     308        /* Enumerate attachments: */
     309        CMediumAttachmentVector attachments = machine.GetMediumAttachments();
     310        foreach (const CMediumAttachment &attachment, attachments)
     311        {
     312            /* Skip non-hard-disks: */
     313            if (attachment.GetType() != KDeviceType_HardDisk)
     314                continue;
     315
     316            /* Skip unrelated hard-disks: */
     317            if (attachment.GetMedium().GetId() != id())
     318                continue;
     319
     320            /* Remember controller: */
     321            CStorageController controller = machine.GetStorageControllerByName(attachment.GetController());
     322
     323            /* Try to detach device: */
     324            machine.DetachDevice(attachment.GetController(), attachment.GetPort(), attachment.GetDevice());
     325            if (!machine.isOk())
     326            {
     327                /* Return failure: */
     328                msgCenter().cannotDetachDevice(machine, UIMediumType_HardDisk, location(),
     329                                               StorageSlot(controller.GetBus(), attachment.GetPort(), attachment.GetDevice()),
     330                                               treeWidget());
     331                return false;
     332            }
     333
     334            /* Return success: */
     335            return true;
     336        }
     337
     338        /* False by default: */
     339        return false;
    244340    }
    245341
     
    344440        return true;
    345441    }
     442
     443    /** Release UIMedium wrapped by <i>this</i> item from virtual @a machine. */
     444    bool releaseFrom(CMachine machine)
     445    {
     446        /* Enumerate attachments: */
     447        CMediumAttachmentVector attachments = machine.GetMediumAttachments();
     448        foreach (const CMediumAttachment &attachment, attachments)
     449        {
     450            /* Skip non-optical-disks: */
     451            if (attachment.GetType() != KDeviceType_DVD)
     452                continue;
     453
     454            /* Skip unrelated optical-disks: */
     455            if (attachment.GetMedium().GetId() != id())
     456                continue;
     457
     458            /* Try to unmount device: */
     459            machine.MountMedium(attachment.GetController(), attachment.GetPort(), attachment.GetDevice(), CMedium(), false /* force */);
     460            if (!machine.isOk())
     461            {
     462                /* Return failure: */
     463                msgCenter().cannotRemountMedium(machine, medium(), false /* mount? */, false /* retry? */, treeWidget());
     464                return false;
     465            }
     466
     467            /* Return success: */
     468            return true;
     469        }
     470
     471        /* Return failure: */
     472        return false;
     473    }
    346474};
    347475
     
    394522        /* True by default: */
    395523        return true;
     524    }
     525
     526    /** Release UIMedium wrapped by <i>this</i> item from virtual @a machine. */
     527    bool releaseFrom(CMachine machine)
     528    {
     529        /* Enumerate attachments: */
     530        CMediumAttachmentVector attachments = machine.GetMediumAttachments();
     531        foreach (const CMediumAttachment &attachment, attachments)
     532        {
     533            /* Skip non-floppy-disks: */
     534            if (attachment.GetType() != KDeviceType_Floppy)
     535                continue;
     536
     537            /* Skip unrelated floppy-disks: */
     538            if (attachment.GetMedium().GetId() != id())
     539                continue;
     540
     541            /* Try to unmount device: */
     542            machine.MountMedium(attachment.GetController(), attachment.GetPort(), attachment.GetDevice(), CMedium(), false /* force */);
     543            if (!machine.isOk())
     544            {
     545                /* Return failure: */
     546                msgCenter().cannotRemountMedium(machine, medium(), false /* mount? */, false /* retry? */, treeWidget());
     547                return false;
     548            }
     549
     550            /* Return success: */
     551            return true;
     552        }
     553
     554        /* Return failure: */
     555        return false;
    396556    }
    397557};
     
    670830    AssertReturnVoid(!pMediumItem->id().isNull());
    671831
    672     /* Refresh attached VM id list: */
    673     pMediumItem->refreshAll();
    674     const QList<QString> machineIds(pMediumItem->medium().curStateMachineIds());
    675     /* If the machine id list is empty: */
    676     if (machineIds.isEmpty())
    677     {
    678         /* This may happen if medium was already released by a third party,
    679          * re-fetch currently chosen medium-item and silently return. */
    680         return refetchCurrentChosenMediumItem();
    681     }
    682 
    683     /* Gather usage list: */
    684     QStringList usage;
    685     foreach (const QString &strMachineId, machineIds)
    686     {
    687         CMachine machine = m_vbox.FindMachine(strMachineId);
    688         if (!m_vbox.isOk())
    689             continue;
    690         usage << machine.GetName();
    691     }
    692     AssertReturnVoid(!usage.isEmpty());
    693 
    694     /* Confirm release: */
    695     if (!msgCenter().confirmMediumRelease(pMediumItem->medium(), usage.join(", "), this))
    696         return;
    697 
    698     /* Release: */
    699     foreach (const QString &strMachineId, machineIds)
    700         if (!releaseMediumFrom(pMediumItem->medium(), strMachineId))
    701             break;
     832    /* Remove current medium-item: */
     833    bool fResult = pMediumItem->release();
     834
     835    /* Refetch currently chosen medium-item: */
     836    if (fResult)
     837        refetchCurrentChosenMediumItem();
    702838}
    703839
     
    16341770}
    16351771
    1636 bool UIMediumManager::releaseMediumFrom(const UIMedium &medium, const QString &strMachineId)
    1637 {
    1638     /* Open session: */
    1639     CSession session = vboxGlobal().openSession(strMachineId);
    1640     if (session.isNull())
    1641         return false;
    1642 
    1643     /* Get machine: */
    1644     CMachine machine = session.GetMachine();
    1645 
    1646     /* Prepare result: */
    1647     bool fSuccess = true;
    1648 
    1649     /* Depending on medium-type: */
    1650     switch (medium.type())
    1651     {
    1652         case UIMediumType_HardDisk: fSuccess = releaseHardDiskFrom(medium, machine); break;
    1653         case UIMediumType_DVD:      fSuccess = releaseOpticalDiskFrom(medium, machine); break;
    1654         case UIMediumType_Floppy:   fSuccess = releaseFloppyDiskFrom(medium, machine); break;
    1655         default: AssertMsgFailed(("Medium-type unknown: %d\n", medium.type())); break;
    1656     }
    1657 
    1658     /* If medium was released: */
    1659     if (fSuccess)
    1660     {
    1661         /* Save machine settings: */
    1662         machine.SaveSettings();
    1663         if (!machine.isOk())
    1664         {
    1665             msgCenter().cannotSaveMachineSettings(machine, this);
    1666             fSuccess = false;
    1667         }
    1668     }
    1669 
    1670     /* Close session: */
    1671     session.UnlockMachine();
    1672 
    1673     /* Return result: */
    1674     return fSuccess;
    1675 }
    1676 
    1677 bool UIMediumManager::releaseHardDiskFrom(const UIMedium &medium, CMachine &machine)
    1678 {
    1679     /* Enumerate attachments: */
    1680     CMediumAttachmentVector attachments = machine.GetMediumAttachments();
    1681     foreach (const CMediumAttachment &attachment, attachments)
    1682     {
    1683         /* Skip non-hard-disks: */
    1684         if (attachment.GetType() != KDeviceType_HardDisk)
    1685             continue;
    1686 
    1687         /* Skip unrelated hard-disks: */
    1688         if (attachment.GetMedium().GetId() != medium.id())
    1689             continue;
    1690 
    1691         /* Try detaching device: */
    1692         machine.DetachDevice(attachment.GetController(), attachment.GetPort(), attachment.GetDevice());
    1693         if (machine.isOk())
    1694         {
    1695             /* Return success: */
    1696             return true;
    1697         }
    1698         else
    1699         {
    1700             /* Show error: */
    1701             CStorageController controller = machine.GetStorageControllerByName(attachment.GetController());
    1702             msgCenter().cannotDetachDevice(machine, UIMediumType_HardDisk, medium.location(),
    1703                                            StorageSlot(controller.GetBus(), attachment.GetPort(), attachment.GetDevice()), this);
    1704             /* Return failure: */
    1705             return false;
    1706         }
    1707     }
    1708     /* Return failure: */
    1709     return false;
    1710 }
    1711 
    1712 bool UIMediumManager::releaseOpticalDiskFrom(const UIMedium &medium, CMachine &machine)
    1713 {
    1714     /* Enumerate attachments: */
    1715     CMediumAttachmentVector attachments = machine.GetMediumAttachments();
    1716     foreach (const CMediumAttachment &attachment, attachments)
    1717     {
    1718         /* Skip non-optical-disks: */
    1719         if (attachment.GetType() != KDeviceType_DVD)
    1720             continue;
    1721 
    1722         /* Skip unrelated optical-disks: */
    1723         if (attachment.GetMedium().GetId() != medium.id())
    1724             continue;
    1725 
    1726         /* Try device unmounting: */
    1727         machine.MountMedium(attachment.GetController(), attachment.GetPort(), attachment.GetDevice(), CMedium(), false /* force */);
    1728         if (machine.isOk())
    1729         {
    1730             /* Return success: */
    1731             return true;
    1732         }
    1733         else
    1734         {
    1735             /* Show error: */
    1736             msgCenter().cannotRemountMedium(machine, medium, false /* mount? */, false /* retry? */, this);
    1737             /* Return failure: */
    1738             return false;
    1739         }
    1740     }
    1741     /* Return failure: */
    1742     return false;
    1743 }
    1744 
    1745 bool UIMediumManager::releaseFloppyDiskFrom(const UIMedium &medium, CMachine &machine)
    1746 {
    1747     /* Enumerate attachments: */
    1748     CMediumAttachmentVector attachments = machine.GetMediumAttachments();
    1749     foreach (const CMediumAttachment &attachment, attachments)
    1750     {
    1751         /* Skip non-floppy-disks: */
    1752         if (attachment.GetType() != KDeviceType_Floppy)
    1753             continue;
    1754 
    1755         /* Skip unrelated floppy-disks: */
    1756         if (attachment.GetMedium().GetId() != medium.id())
    1757             continue;
    1758 
    1759         /* Try device unmounting: */
    1760         machine.MountMedium(attachment.GetController(), attachment.GetPort(), attachment.GetDevice(), CMedium(), false /* force */);
    1761         if (machine.isOk())
    1762         {
    1763             /* Return success: */
    1764             return true;
    1765         }
    1766         else
    1767         {
    1768             /* Show error: */
    1769             msgCenter().cannotRemountMedium(machine, medium, false /* mount? */, false /* retry? */, this);
    1770             /* Return failure: */
    1771             return false;
    1772         }
    1773     }
    1774     /* Return failure: */
    1775     return false;
    1776 }
    1777 
    17781772UIMediumType UIMediumManager::mediumType(QTreeWidget *pTreeWidget) const
    17791773{
  • trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumManager.h

    r50715 r50719  
    164164    void deleteMediumItem(const QString &strMediumID);
    165165
    166     /* Helpers: Medium-modification stuff: */
    167     bool releaseMediumFrom(const UIMedium &medium, const QString &strMachineId);
    168     bool releaseHardDiskFrom(const UIMedium &medium, CMachine &machine);
    169     bool releaseOpticalDiskFrom(const UIMedium &medium, CMachine &machine);
    170     bool releaseFloppyDiskFrom(const UIMedium &medium, CMachine &machine);
    171 
    172166    /** Determines medium type for passed @a pTreeWidget. */
    173167    UIMediumType mediumType(QTreeWidget *pTreeWidget) const;
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