Changeset 88746 in vbox for trunk/src/VBox/Frontends
- Timestamp:
- Apr 28, 2021 1:08:53 PM (4 years ago)
- 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 201 201 bool UIMediumItem::changeMediumType(KMediumType enmOldType, KMediumType enmNewType) 202 202 { 203 /* Cache the list of VMs the medium is attached to. We will need this for the re-attachment: */ 203 204 QList<AttachmentCache> attachmentCacheList; 204 /* Cache the list of vms the medium is attached to. We will need this for the re-attachment: */205 205 foreach (const QUuid &uMachineId, medium().curStateMachineIds()) 206 206 { … … 208 208 if (comMachine.isNull()) 209 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()) 210 foreach (const CStorageController &comController, comMachine.GetStorageControllers()) 211 { 212 if (comController.isNull()) 215 213 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: */ 226 234 if (!release(true)) 227 235 return false; 228 236 229 /* Search for corresponding medium: */230 CMedium comMedium = uiCommon().medium(id()).medium();231 232 237 /* Attempt to change medium type: */ 238 CMedium comMedium = medium().medium(); 233 239 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: */ 242 247 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; 245 253 } 246 254 … … 350 358 bool UIMediumItem::attachTo(const AttachmentCache &attachmentCache) 351 359 { 360 /* Open session: */ 361 CSession comSession = uiCommon().openSession(attachmentCache.m_uMachineId); 362 if (comSession.isNull()) 363 return false; 364 365 /* Attach device to machine: */ 352 366 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()); 376 388 } 377 389 378 390 /* Close session: */ 379 session.UnlockMachine();380 381 /* Return result: */382 return fSuccess;391 comSession.UnlockMachine(); 392 393 /* True finally: */ 394 return true; 383 395 } 384 396 -
trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumItem.h
r82968 r88746 121 121 122 122 private: 123 123 124 /** 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. */ 125 126 struct AttachmentCache 126 127 { 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; 131 138 }; 132 139
Note:
See TracChangeset
for help on using the changeset viewer.