VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/wizards/newvd/UIWizardNewVD.cpp@ 104899

Last change on this file since 104899 was 104899, checked in by vboxsync, 10 months ago

FE/Qt: UICommon: Get rid of last access media stuff by moving it to UIMediumEnumerator; Adjust the GUI to work accodringly.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: UIWizardNewVD.cpp 104899 2024-06-12 14:28:47Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIWizardNewVD class implementation.
4 */
5
6/*
7 * Copyright (C) 2006-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 "UIMediumEnumerator.h"
32#include "UIModalWindowManager.h"
33#include "UINotificationCenter.h"
34#include "UIWizardNewVD.h"
35#include "UIWizardNewVDFileTypePage.h"
36#include "UIWizardNewVDVariantPage.h"
37#include "UIWizardNewVDSizeLocationPage.h"
38#include "UIWizardNewVDExpertPage.h"
39
40/* COM includes: */
41#include "CGuestOSType.h"
42
43UIWizardNewVD::UIWizardNewVD(QWidget *pParent,
44 const QString &strDefaultName,
45 const QString &strDefaultPath,
46 qulonglong uDefaultSize)
47 : UINativeWizard(pParent, WizardType_NewVD, WizardMode_Auto, "create-virtual-hard-disk-image" /* help keyword */)
48 , m_strDefaultName(strDefaultName)
49 , m_strDefaultPath(strDefaultPath)
50 , m_uDefaultSize(uDefaultSize)
51 , m_iMediumVariantPageIndex(-1)
52{
53#ifndef VBOX_WS_MAC
54 /* Assign watermark: */
55 setPixmapName(":/wizard_new_harddisk.png");
56#else /* VBOX_WS_MAC */
57 /* Assign background image: */
58 setPixmapName(":/wizard_new_harddisk_bg.png");
59#endif /* VBOX_WS_MAC */
60}
61
62qulonglong UIWizardNewVD::mediumVariant() const
63{
64 return m_uMediumVariant;
65}
66
67void UIWizardNewVD::setMediumVariant(qulonglong uMediumVariant)
68{
69 m_uMediumVariant = uMediumVariant;
70}
71
72const CMediumFormat &UIWizardNewVD::mediumFormat()
73{
74 return m_comMediumFormat;
75}
76
77void UIWizardNewVD::setMediumFormat(const CMediumFormat &mediumFormat)
78{
79 m_comMediumFormat = mediumFormat;
80 if (mode() == WizardMode_Basic)
81 setMediumVariantPageVisibility();
82}
83
84const QString &UIWizardNewVD::mediumPath() const
85{
86 return m_strMediumPath;
87}
88
89void UIWizardNewVD::setMediumPath(const QString &strMediumPath)
90{
91 m_strMediumPath = strMediumPath;
92}
93
94qulonglong UIWizardNewVD::mediumSize() const
95{
96 return m_uMediumSize;
97}
98
99void UIWizardNewVD::setMediumSize(qulonglong uMediumSize)
100{
101 m_uMediumSize = uMediumSize;
102}
103
104QUuid UIWizardNewVD::mediumId() const
105{
106 return m_uMediumId;
107}
108
109void UIWizardNewVD::populatePages()
110{
111 switch (mode())
112 {
113 case WizardMode_Basic:
114 {
115 addPage(new UIWizardNewVDFileTypePage);
116 m_iMediumVariantPageIndex = addPage(new UIWizardNewVDVariantPage);
117 addPage(new UIWizardNewVDSizeLocationPage(m_strDefaultName, m_strDefaultPath, m_uDefaultSize));
118 break;
119 }
120 case WizardMode_Expert:
121 {
122 addPage(new UIWizardNewVDExpertPage(m_strDefaultName, m_strDefaultPath, m_uDefaultSize));
123 break;
124 }
125 default:
126 {
127 AssertMsgFailed(("Invalid mode: %d", mode()));
128 break;
129 }
130 }
131}
132
133bool UIWizardNewVD::createVirtualDisk()
134{
135 AssertReturn(!m_strMediumPath.isNull(), false);
136 AssertReturn(m_uMediumSize > 0, false);
137
138 /* Get VBox object: */
139 CVirtualBox comVBox = gpGlobalSession->virtualBox();
140
141 /* Create new virtual disk image: */
142 CMedium comVirtualDisk = comVBox.CreateMedium(m_comMediumFormat.GetName(),
143 m_strMediumPath, KAccessMode_ReadWrite, KDeviceType_HardDisk);
144 if (!comVBox.isOk())
145 {
146 UINotificationMessage::cannotCreateMediumStorage(comVBox, m_strMediumPath, notificationCenter());
147 return false;
148 }
149
150 /* Compose medium-variant: */
151 QVector<KMediumVariant> variants(sizeof(qulonglong) * 8);
152 for (int i = 0; i < variants.size(); ++i)
153 {
154 qulonglong temp = m_uMediumVariant;
155 temp &= Q_UINT64_C(1) << i;
156 variants[i] = (KMediumVariant)temp;
157 }
158
159 UINotificationProgressMediumCreate *pNotification = new UINotificationProgressMediumCreate(comVirtualDisk,
160 m_uMediumSize,
161 variants);
162 connect(pNotification, &UINotificationProgressMediumCreate::sigMediumCreated,
163 &uiCommon(), &UICommon::sltHandleMediumCreated);
164
165 m_uMediumId = comVirtualDisk.GetId();
166
167 gpNotificationCenter->append(pNotification);
168
169 /* Positive: */
170 return true;
171}
172
173/* static */
174QUuid UIWizardNewVD::createVDWithWizard(QWidget *pParent,
175 const QString &strMachineFolder /* = QString() */,
176 const QString &strMachineName /* = QString() */,
177 const QString &strMachineGuestOSTypeId /* = QString() */)
178{
179 /* Default path: */
180 const QString strDefaultPath = !strMachineFolder.isEmpty()
181 ? strMachineFolder
182 : UIMediumEnumerator::defaultFolderPathForType(UIMediumDeviceType_HardDisk);
183
184 /* Default name: */
185 const QString strDiskName = uiCommon().findUniqueFileName(strDefaultPath,
186 !strMachineName.isEmpty()
187 ? strMachineName
188 : "NewVirtualDisk");
189
190 /* Default size: */
191 const CGuestOSType comGuestOSType = gpGlobalSession->virtualBox().GetGuestOSType( !strMachineGuestOSTypeId.isEmpty()
192 ? strMachineGuestOSTypeId
193 : "Other");
194 const qulonglong uDefaultSize = comGuestOSType.GetRecommendedHDD();
195
196 /* Show New VD wizard the safe way: */
197 QWidget *pRealParent = windowManager().realParentWindow(pParent);
198 UISafePointerWizardNewVD pWizard = new UIWizardNewVD(pRealParent,
199 strDiskName,
200 strDefaultPath,
201 uDefaultSize);
202 if (!pWizard)
203 return QUuid();
204 windowManager().registerNewParent(pWizard, pRealParent);
205 pWizard->exec();
206 const QUuid uMediumId = pWizard->mediumId();
207 delete pWizard;
208 return uMediumId;
209}
210
211void UIWizardNewVD::sltRetranslateUI()
212{
213 UINativeWizard::sltRetranslateUI();
214 setWindowTitle(tr("Create Virtual Hard Disk"));
215}
216
217void UIWizardNewVD::setMediumVariantPageVisibility()
218{
219 AssertReturnVoid(!m_comMediumFormat.isNull());
220 ULONG uCapabilities = 0;
221 QVector<KMediumFormatCapabilities> capabilities;
222 capabilities = m_comMediumFormat.GetCapabilities();
223 for (int i = 0; i < capabilities.size(); i++)
224 uCapabilities |= capabilities[i];
225
226 int cTest = 0;
227 if (uCapabilities & KMediumFormatCapabilities_CreateDynamic)
228 ++cTest;
229 if (uCapabilities & KMediumFormatCapabilities_CreateFixed)
230 ++cTest;
231 if (uCapabilities & KMediumFormatCapabilities_CreateSplit2G)
232 ++cTest;
233 setPageVisible(m_iMediumVariantPageIndex, cTest > 1);
234}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette