void VBoxSnapshotsWgt::sltRestoreSnapshot(bool fSuppressNonCriticalWarnings /* = false*/) { /* Get currently chosen item: */ SnapshotWgtItem *pItem = mTreeWidget->currentItem() ? static_cast<SnapshotWgtItem*>(mTreeWidget->currentItem()) : 0; AssertReturn(pItem, (void)0); /* Detemine snapshot id: */ QString strSnapshotId = pItem->snapshotId(); AssertReturn(!strSnapshotId.isNull(), (void)0); /* Get currently desired snapshot: */ CSnapshot snapshot = mMachine.FindSnapshot(strSnapshotId); /* Ask the user if he really wants to restore the snapshot: */ int iResultCode = AlertButton_Ok; if (!fSuppressNonCriticalWarnings || mMachine.GetCurrentStateModified()) { iResultCode = msgCenter().confirmSnapshotRestoring(snapshot.GetName(), mMachine.GetCurrentStateModified()); if (iResultCode & AlertButton_Cancel) return; } /* If user also confirmed new snapshot creation: */ if (iResultCode & AlertOption_CheckBox) { /* Take snapshot of changed current state: */ mTreeWidget->setCurrentItem(curStateItem()); if (!takeSnapshot()) return; } /* Open a direct session (this call will handle all errors): */ CSession session = vboxGlobal().openSession(mMachineId); if (session.isNull()) return; /* Restore chosen snapshot: */ CConsole console = session.GetConsole(); CProgress progress = console.RestoreSnapshot(snapshot); if (console.isOk()) { msgCenter().showModalProgressDialog(progress, mMachine.GetName(), ":/progress_snapshot_restore_90px.png"); if (progress.GetResultCode() != 0) msgCenter().cannotRestoreSnapshot(progress, snapshot.GetName(), mMachine.GetName()); } else msgCenter().cannotRestoreSnapshot(console, snapshot.GetName(), mMachine.GetName()); /* Unlock machine finally: */ session.UnlockMachine(); }
void VBoxSnapshotsWgt::sltDeleteSnapshot() { SnapshotWgtItem *item = !mTreeWidget->currentItem() ? 0 : static_cast <SnapshotWgtItem*> (mTreeWidget->currentItem()); AssertReturn (item, (void) 0); QString snapId = item->snapshotId(); AssertReturn (!snapId.isNull(), (void) 0); CSnapshot snapshot = mMachine.FindSnapshot(snapId); if (!msgCenter().confirmSnapshotRemoval(snapshot.GetName())) return; /** @todo check available space on the target filesystem etc etc. */ #if 0 if (!msgCenter().warnAboutSnapshotRemovalFreeSpace(snapshot.GetName(), "/home/juser/.VirtualBox/Machines/SampleVM/Snapshots/{01020304-0102-0102-0102-010203040506}.vdi", "59 GiB", "15 GiB")) return; #endif /* Open a direct session (this call will handle all errors) */ bool busy = mSessionState != KSessionState_Unlocked; CSession session; if (busy) session = vboxGlobal().openExistingSession(mMachineId); else session = vboxGlobal().openSession(mMachineId); if (session.isNull()) return; CConsole console = session.GetConsole(); CProgress progress = console.DeleteSnapshot (snapId); if (console.isOk()) { /* Show the progress dialog */ msgCenter().showModalProgressDialog(progress, mMachine.GetName(), ":/progress_snapshot_discard_90px.png"); if (progress.GetResultCode() != 0) msgCenter().cannotRemoveSnapshot(progress, snapshot.GetName(), mMachine.GetName()); } else msgCenter().cannotRemoveSnapshot(console, snapshot.GetName(), mMachine.GetName()); session.UnlockMachine(); }
bool VBoxSnapshotsWgt::takeSnapshot() { /* Prepare result: */ bool fIsValid = true; /* Get currently chosen item: */ SnapshotWgtItem *pItem = mTreeWidget->currentItem() ? static_cast<SnapshotWgtItem*>(mTreeWidget->currentItem()) : 0; AssertReturn(pItem, (bool)0); /* Open a session to work with corresponding VM: */ CSession session; if (mSessionState != KSessionState_Unlocked) session = vboxGlobal().openExistingSession(mMachineId); else session = vboxGlobal().openSession(mMachineId); fIsValid = !session.isNull(); if (fIsValid) { /* Get corresponding console object also: */ CConsole console = session.GetConsole(); /* Remember runtime state: */ bool fAtRuntime = mMachine.GetState() == KMachineState_Running; /* Remember paused state: */ bool fWasPaused = mMachine.GetState() == KMachineState_Paused || mMachine.GetState() == KMachineState_TeleportingPausedVM; /* Pause VM if necessary: */ if (fIsValid && fAtRuntime && !fWasPaused) { /* Pausing VM: */ console.Pause(); if (!console.isOk()) { msgCenter().cannotPauseMachine(console); fIsValid = false; } } if (fIsValid) { /* Create take-snapshot dialog: */ QWidget *pDlgParent = windowManager().realParentWindow(this); QPointer<VBoxTakeSnapshotDlg> pDlg = new VBoxTakeSnapshotDlg(pDlgParent, mMachine); windowManager().registerNewParent(pDlg, pDlgParent); /* Assign corresponding icon: */ pDlg->mLbIcon->setPixmap(vboxGlobal().vmGuestOSTypeIcon(mMachine.GetOSTypeId())); /* Search for the max available snapshot index: */ int iMaxSnapShotIndex = 0; QString snapShotName = tr("Snapshot %1"); QRegExp regExp(QString("^") + snapShotName.arg("([0-9]+)") + QString("$")); QTreeWidgetItemIterator iterator(mTreeWidget); while (*iterator) { QString snapShot = static_cast<SnapshotWgtItem*>(*iterator)->text(0); int pos = regExp.indexIn(snapShot); if (pos != -1) iMaxSnapShotIndex = regExp.cap(1).toInt() > iMaxSnapShotIndex ? regExp.cap(1).toInt() : iMaxSnapShotIndex; ++iterator; } pDlg->mLeName->setText(snapShotName.arg(iMaxSnapShotIndex + 1)); /* Exec the dialog: */ bool fDialogAccepted = pDlg->exec() == QDialog::Accepted; /* Is the dialog still valid? */ if (pDlg) { /* Acquire variables: */ QString strSnapshotName = pDlg->mLeName->text().trimmed(); QString strSnapshotDescription = pDlg->mTeDescription->toPlainText(); /* Destroy dialog early: */ delete pDlg; /* Was the dialog accepted? */ if (fDialogAccepted) { /* Prepare the take-snapshot progress: */ CProgress progress = console.TakeSnapshot(strSnapshotName, strSnapshotDescription); if (console.isOk()) { /* Show the take-snapshot progress: */ msgCenter().showModalProgressDialog(progress, mMachine.GetName(), ":/progress_snapshot_create_90px.png"); if (!progress.isOk() || progress.GetResultCode() != 0) { msgCenter().cannotTakeSnapshot(progress, mMachine.GetName()); fIsValid = false; } } else { msgCenter().cannotTakeSnapshot(console, mMachine.GetName()); fIsValid = false; } } else fIsValid = false; } else fIsValid = false; } /* Resume VM if necessary: */ if (fIsValid && fAtRuntime && !fWasPaused) { /* Resuming VM: */ console.Resume(); if (!console.isOk()) { msgCenter().cannotResumeMachine(console); fIsValid = false; } } /* Unlock machine finally: */ session.UnlockMachine(); } /* Return result: */ return fIsValid; }
bool UIWizardCloneVM::cloneVM() { /* Get clone name: */ QString strName = field("cloneName").toString(); /* Should we reinit mac status? */ bool fReinitMACs = field("reinitMACs").toBool(); /* Should we create linked clone? */ bool fLinked = field("linkedClone").toBool(); /* Get clone mode: */ KCloneMode cloneMode = (mode() == UIWizardMode_Basic && page(Page3)) || (mode() == UIWizardMode_Expert && page(PageExpert)) ? field("cloneMode").value<KCloneMode>() : KCloneMode_MachineState; /* Get VBox object: */ CVirtualBox vbox = vboxGlobal().virtualBox(); /* Prepare machine for cloning: */ CMachine srcMachine = m_machine; /* If the user like to create a linked clone from the current machine, we * have to take a little bit more action. First we create an snapshot, so * that new differencing images on the source VM are created. Based on that * we could use the new snapshot machine for cloning. */ if (fLinked && m_snapshot.isNull()) { /* Open session: */ CSession session = vboxGlobal().openSession(m_machine.GetId()); if (session.isNull()) return false; /* Prepare console: */ CConsole console = session.GetConsole(); /* Take the snapshot: */ QString strSnapshotName = tr("Linked Base for %1 and %2").arg(m_machine.GetName()).arg(strName); CProgress progress = console.TakeSnapshot(strSnapshotName, ""); if (console.isOk()) { /* Show the "Taking Snapshot" progress dialog: */ msgCenter().showModalProgressDialog(progress, m_machine.GetName(), ":/progress_snapshot_create_90px.png", this, true); if (!progress.isOk() || progress.GetResultCode() != 0) { msgCenter().cannotTakeSnapshot(progress); return false; } } else { msgCenter().cannotTakeSnapshot(console); return false; } /* Unlock machine finally: */ session.UnlockMachine(); /* Get the new snapshot and the snapshot machine. */ const CSnapshot &newSnapshot = m_machine.FindSnapshot(strSnapshotName); if (newSnapshot.isNull()) { msgCenter().cannotFindSnapshotByName(this, m_machine, strSnapshotName); return false; } srcMachine = newSnapshot.GetMachine(); } /* Create a new machine object. */ const QString &strSettingsFile = vbox.ComposeMachineFilename(strName, QString::null /**< @todo group support */, QString::null, QString::null); CMachine cloneMachine = vbox.CreateMachine(strSettingsFile, strName, QVector<QString>(), QString::null, QString::null); if (!vbox.isOk()) { msgCenter().cannotCreateMachine(vbox, this); return false; } /* Add the keep all MACs option to the import settings when requested. */ QVector<KCloneOptions> options; if (!fReinitMACs) options.append(KCloneOptions_KeepAllMACs); /* Linked clones requested? */ if (fLinked) options.append(KCloneOptions_Link); /* Start cloning. */ CProgress progress = srcMachine.CloneTo(cloneMachine, cloneMode, options); if (!srcMachine.isOk()) { msgCenter().cannotCreateClone(srcMachine, this); return false; } /* Wait until done. */ msgCenter().showModalProgressDialog(progress, windowTitle(), ":/progress_clone_90px.png", this, true); if (progress.GetCanceled()) return false; if (!progress.isOk() || progress.GetResultCode() != 0) { msgCenter().cannotCreateClone(srcMachine, progress, this); return false; } /* Finally register the clone machine. */ vbox.RegisterMachine(cloneMachine); if (!vbox.isOk()) { msgCenter().cannotRegisterMachine(vbox, cloneMachine, this); return false; } return true; }
bool UICloneVMWizard::createClone(const QString &strName, KCloneMode mode, bool fReinitMACs, bool fLinked /* = false */) { CVirtualBox vbox = vboxGlobal().virtualBox(); const QString &strSettingsFile = vbox.ComposeMachineFilename(strName, QString::null); CMachine srcMachine = m_machine; /* If the user like to create a linked clone from the current machine, we * have to take a little bit more action. First we create an snapshot, so * that new differencing images on the source VM are created. Based on that * we could use the new snapshot machine for cloning. */ if ( fLinked && m_snapshot.isNull()) { const QString &strId = m_machine.GetId(); CSession session = vboxGlobal().openSession(strId); if (session.isNull()) return false; CConsole console = session.GetConsole(); /* Take the snapshot */ QString strSnapshotName = tr("Linked Base for %1 and %2").arg(m_machine.GetName()).arg(strName); CProgress progress = console.TakeSnapshot(strSnapshotName, ""); if (console.isOk()) { /* Show the "Taking Snapshot" progress dialog */ msgCenter().showModalProgressDialog(progress, m_machine.GetName(), ":/progress_snapshot_create_90px.png", this, true); if (!progress.isOk() || progress.GetResultCode() != 0) { msgCenter().cannotTakeSnapshot(progress); return false; } } else { msgCenter().cannotTakeSnapshot(console); return false; } /* Unlock machine finally: */ session.UnlockMachine(); /* Get the new snapshot and the snapshot machine. */ const CSnapshot &newSnapshot = m_machine.FindSnapshot(strSnapshotName); if (newSnapshot.isNull()) { msgCenter().cannotFindSnapshotByName(this, m_machine, strSnapshotName); return false; } srcMachine = newSnapshot.GetMachine(); } /* Create a new machine object. */ CMachine cloneMachine = vbox.CreateMachine(strSettingsFile, strName, QString::null, QString::null, false); if (!vbox.isOk()) { msgCenter().cannotCreateMachine(vbox, this); return false; } /* Add the keep all MACs option to the import settings when requested. */ QVector<KCloneOptions> options; if (!fReinitMACs) options.append(KCloneOptions_KeepAllMACs); /* Linked clones requested? */ if (fLinked) options.append(KCloneOptions_Link); /* Start cloning. */ CProgress progress = srcMachine.CloneTo(cloneMachine, mode, options); if (!srcMachine.isOk()) { msgCenter().cannotCreateClone(srcMachine, this); return false; } /* Wait until done. */ msgCenter().showModalProgressDialog(progress, windowTitle(), ":/progress_clone_90px.png", this, true); if (progress.GetCanceled()) return false; if (!progress.isOk() || progress.GetResultCode() != 0) { msgCenter().cannotCreateClone(srcMachine, progress, this); return false; } /* Finally register the clone machine. */ vbox.RegisterMachine(cloneMachine); if (!vbox.isOk()) { msgCenter().cannotRegisterMachine(vbox, cloneMachine, this); return false; } return true; }