1 | /* $Id: UIWizardCloneVM.cpp 103961 2024-03-20 14:34:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIWizardCloneVM class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* GUI includes: */
|
---|
29 | #include "UICommon.h"
|
---|
30 | #include "UIGlobalSession.h"
|
---|
31 | #include "UINotificationCenter.h"
|
---|
32 | #include "UIWizardCloneVM.h"
|
---|
33 | #include "UIWizardCloneVMNamePathPage.h"
|
---|
34 | #include "UIWizardCloneVMTypePage.h"
|
---|
35 | #include "UIWizardCloneVMModePage.h"
|
---|
36 | #include "UIWizardCloneVMExpertPage.h"
|
---|
37 |
|
---|
38 | /* COM includes: */
|
---|
39 | #include "CSystemProperties.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | UIWizardCloneVM::UIWizardCloneVM(QWidget *pParent, const CMachine &machine,
|
---|
43 | const QString &strGroup, CSnapshot snapshot /* = CSnapshot() */)
|
---|
44 | : UINativeWizard(pParent, WizardType_CloneVM, WizardMode_Auto, "clone" /* help keyword */)
|
---|
45 | , m_machine(machine)
|
---|
46 | , m_snapshot(snapshot)
|
---|
47 | , m_strGroup(strGroup)
|
---|
48 | , m_iCloneModePageIndex(-1)
|
---|
49 | , m_strCloneName(!machine.isNull() ? machine.GetName() : QString())
|
---|
50 | , m_enmCloneMode(KCloneMode_MachineState)
|
---|
51 | {
|
---|
52 | #ifndef VBOX_WS_MAC
|
---|
53 | /* Assign watermark: */
|
---|
54 | setPixmapName(":/wizard_clone.png");
|
---|
55 | #else /* VBOX_WS_MAC */
|
---|
56 | /* Assign background image: */
|
---|
57 | setPixmapName(":/wizard_clone_bg.png");
|
---|
58 | #endif /* VBOX_WS_MAC */
|
---|
59 | }
|
---|
60 |
|
---|
61 | void UIWizardCloneVM::setCloneModePageVisible(bool fIsFullClone)
|
---|
62 | {
|
---|
63 | if (m_iCloneModePageIndex == -1)
|
---|
64 | return;
|
---|
65 | setPageVisible(m_iCloneModePageIndex, fIsFullClone);
|
---|
66 | }
|
---|
67 |
|
---|
68 | bool UIWizardCloneVM::isCloneModePageVisible() const
|
---|
69 | {
|
---|
70 | /* If we did not create the clone mode page return false: */
|
---|
71 | if (m_iCloneModePageIndex == -1)
|
---|
72 | return false;
|
---|
73 | return isPageVisible(m_iCloneModePageIndex);
|
---|
74 | }
|
---|
75 |
|
---|
76 | void UIWizardCloneVM::setCloneName(const QString &strCloneName)
|
---|
77 | {
|
---|
78 | m_strCloneName = strCloneName;
|
---|
79 | }
|
---|
80 |
|
---|
81 | const QString &UIWizardCloneVM::cloneName() const
|
---|
82 | {
|
---|
83 | return m_strCloneName;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void UIWizardCloneVM::setCloneFilePath(const QString &strCloneFilePath)
|
---|
87 | {
|
---|
88 | m_strCloneFilePath = strCloneFilePath;
|
---|
89 | }
|
---|
90 |
|
---|
91 | const QString &UIWizardCloneVM::cloneFilePath() const
|
---|
92 | {
|
---|
93 | return m_strCloneFilePath;
|
---|
94 | }
|
---|
95 |
|
---|
96 | MACAddressClonePolicy UIWizardCloneVM::macAddressClonePolicy() const
|
---|
97 | {
|
---|
98 | return m_enmMACAddressClonePolicy;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void UIWizardCloneVM::setMacAddressPolicy(MACAddressClonePolicy enmMACAddressClonePolicy)
|
---|
102 | {
|
---|
103 | m_enmMACAddressClonePolicy = enmMACAddressClonePolicy;
|
---|
104 | }
|
---|
105 |
|
---|
106 | bool UIWizardCloneVM::keepDiskNames() const
|
---|
107 | {
|
---|
108 | return m_fKeepDiskNames;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void UIWizardCloneVM::setKeepDiskNames(bool fKeepDiskNames)
|
---|
112 | {
|
---|
113 | m_fKeepDiskNames = fKeepDiskNames;
|
---|
114 | }
|
---|
115 |
|
---|
116 | bool UIWizardCloneVM::keepHardwareUUIDs() const
|
---|
117 | {
|
---|
118 | return m_fKeepHardwareUUIDs;
|
---|
119 | }
|
---|
120 |
|
---|
121 | void UIWizardCloneVM::setKeepHardwareUUIDs(bool fKeepHardwareUUIDs)
|
---|
122 | {
|
---|
123 | m_fKeepHardwareUUIDs = fKeepHardwareUUIDs;
|
---|
124 | }
|
---|
125 |
|
---|
126 | bool UIWizardCloneVM::linkedClone() const
|
---|
127 | {
|
---|
128 | return m_fLinkedClone;
|
---|
129 | }
|
---|
130 |
|
---|
131 | void UIWizardCloneVM::setLinkedClone(bool fLinkedClone)
|
---|
132 | {
|
---|
133 | m_fLinkedClone = fLinkedClone;
|
---|
134 | }
|
---|
135 |
|
---|
136 | KCloneMode UIWizardCloneVM::cloneMode() const
|
---|
137 | {
|
---|
138 | return m_enmCloneMode;
|
---|
139 | }
|
---|
140 |
|
---|
141 | void UIWizardCloneVM::setCloneMode(KCloneMode enmCloneMode)
|
---|
142 | {
|
---|
143 | m_enmCloneMode = enmCloneMode;
|
---|
144 | }
|
---|
145 |
|
---|
146 | bool UIWizardCloneVM::machineHasSnapshot() const
|
---|
147 | {
|
---|
148 | AssertReturn(!m_machine.isNull(), false);
|
---|
149 | return m_machine.GetSnapshotCount() > 0;
|
---|
150 | }
|
---|
151 |
|
---|
152 | bool UIWizardCloneVM::cloneVM()
|
---|
153 | {
|
---|
154 | /* Prepare machine for cloning: */
|
---|
155 | CMachine srcMachine = m_machine;
|
---|
156 |
|
---|
157 | /* If the user like to create a linked clone from the current machine, we have to take a little bit more action.
|
---|
158 | * First we create an snapshot, so that new differencing images on the source VM are created. Based on that we
|
---|
159 | * could use the new snapshot machine for cloning. */
|
---|
160 | if (m_fLinkedClone && m_snapshot.isNull())
|
---|
161 | {
|
---|
162 | /* Compose snapshot name: */
|
---|
163 | const QString strSnapshotName = tr("Linked Base for %1 and %2").arg(m_machine.GetName()).arg(m_strCloneName);
|
---|
164 |
|
---|
165 | /* Take the snapshot: */
|
---|
166 | UINotificationProgressSnapshotTake *pNotification = new UINotificationProgressSnapshotTake(srcMachine,
|
---|
167 | strSnapshotName,
|
---|
168 | QString());
|
---|
169 | UINotificationReceiver receiver;
|
---|
170 | connect(pNotification, &UINotificationProgressSnapshotTake::sigSnapshotTaken,
|
---|
171 | &receiver, &UINotificationReceiver::setReceiverProperty);
|
---|
172 | if (!handleNotificationProgressNow(pNotification))
|
---|
173 | return false;
|
---|
174 |
|
---|
175 | /* Acquire created snapshot id: */
|
---|
176 | QUuid uSnapshotId = receiver.property("received_value").toUuid();
|
---|
177 |
|
---|
178 | /* Look for created snapshot: */
|
---|
179 | const CSnapshot comCreatedSnapshot = m_machine.FindSnapshot(uSnapshotId.toString());
|
---|
180 | if (comCreatedSnapshot.isNull())
|
---|
181 | {
|
---|
182 | UINotificationMessage::cannotFindSnapshotByName(m_machine, strSnapshotName, notificationCenter());
|
---|
183 | return false;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* Update machine for cloning finally: */
|
---|
187 | srcMachine = comCreatedSnapshot.GetMachine();
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* Get VBox object: */
|
---|
191 | CVirtualBox comVBox = gpGlobalSession->virtualBox();
|
---|
192 | /* Create a new machine object: */
|
---|
193 | CMachine cloneMachine = comVBox.CreateMachine(m_strCloneFilePath, m_strCloneName, KPlatformArchitecture_x86,
|
---|
194 | QVector<QString>(), QString(), QString(),
|
---|
195 | QString(), QString(), QString());
|
---|
196 | if (!comVBox.isOk())
|
---|
197 | {
|
---|
198 | UINotificationMessage::cannotCreateMachine(comVBox, notificationCenter());
|
---|
199 | return false;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /* Clone options vector to pass to cloning: */
|
---|
203 | QVector<KCloneOptions> options;
|
---|
204 | /* Set the selected MAC address policy: */
|
---|
205 | switch (m_enmMACAddressClonePolicy)
|
---|
206 | {
|
---|
207 | case MACAddressClonePolicy_KeepAllMACs:
|
---|
208 | options.append(KCloneOptions_KeepAllMACs);
|
---|
209 | break;
|
---|
210 | case MACAddressClonePolicy_KeepNATMACs:
|
---|
211 | options.append(KCloneOptions_KeepNATMACs);
|
---|
212 | break;
|
---|
213 | default:
|
---|
214 | break;
|
---|
215 | }
|
---|
216 | if (m_fKeepDiskNames)
|
---|
217 | options.append(KCloneOptions_KeepDiskNames);
|
---|
218 | if (m_fKeepHardwareUUIDs)
|
---|
219 | options.append(KCloneOptions_KeepHwUUIDs);
|
---|
220 | /* Linked clones requested? */
|
---|
221 | if (m_fLinkedClone)
|
---|
222 | options.append(KCloneOptions_Link);
|
---|
223 |
|
---|
224 | /* Clone VM: */
|
---|
225 | UINotificationProgressMachineCopy *pNotification = new UINotificationProgressMachineCopy(srcMachine,
|
---|
226 | cloneMachine,
|
---|
227 | m_enmCloneMode,
|
---|
228 | options);
|
---|
229 | connect(pNotification, &UINotificationProgressMachineCopy::sigMachineCopied,
|
---|
230 | &uiCommon(), &UICommon::sltHandleMachineCreated);
|
---|
231 | gpNotificationCenter->append(pNotification);
|
---|
232 |
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 |
|
---|
236 | void UIWizardCloneVM::sltRetranslateUI()
|
---|
237 | {
|
---|
238 | /* Call to base-class: */
|
---|
239 | UINativeWizard::sltRetranslateUI();
|
---|
240 |
|
---|
241 | /* Translate wizard: */
|
---|
242 | setWindowTitle(tr("Clone Virtual Machine"));
|
---|
243 | }
|
---|
244 |
|
---|
245 | void UIWizardCloneVM::populatePages()
|
---|
246 | {
|
---|
247 | QString strDefaultMachineFolder = gpGlobalSession->virtualBox().GetSystemProperties().GetDefaultMachineFolder();
|
---|
248 | /* Create corresponding pages: */
|
---|
249 | switch (mode())
|
---|
250 | {
|
---|
251 | case WizardMode_Basic:
|
---|
252 | {
|
---|
253 | addPage(new UIWizardCloneVMNamePathPage(m_strCloneName, strDefaultMachineFolder, m_strGroup));
|
---|
254 | addPage(new UIWizardCloneVMTypePage(m_snapshot.isNull()));
|
---|
255 | if (machineHasSnapshot())
|
---|
256 | m_iCloneModePageIndex = addPage(new UIWizardCloneVMModePage(m_snapshot.isNull() ? false : m_snapshot.GetChildrenCount() > 0));
|
---|
257 | break;
|
---|
258 | }
|
---|
259 | case WizardMode_Expert:
|
---|
260 | {
|
---|
261 | addPage(new UIWizardCloneVMExpertPage(m_machine.GetName(),
|
---|
262 | strDefaultMachineFolder,
|
---|
263 | m_snapshot.isNull(),
|
---|
264 | m_snapshot.isNull() ? false : m_snapshot.GetChildrenCount() > 0,
|
---|
265 | m_strGroup));
|
---|
266 | break;
|
---|
267 | }
|
---|
268 | default:
|
---|
269 | {
|
---|
270 | AssertMsgFailed(("Invalid mode: %d", mode()));
|
---|
271 | break;
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|