VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/medium/UIFDCreationDialog.cpp@ 104158

Last change on this file since 104158 was 103771, checked in by vboxsync, 9 months ago

FE/Qt: UICommon: Switching dependency from UICommon to UIGlobalSession whenever is possible.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1/* $Id: UIFDCreationDialog.cpp 103771 2024-03-11 15:16:04Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIFDCreationDialog class implementation.
4 */
5
6/*
7 * Copyright (C) 2008-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/* Qt includes */
29#include <QCheckBox>
30#include <QDir>
31#include <QGridLayout>
32#include <QLabel>
33#include <QPushButton>
34
35/* GUI includes */
36#include "QIDialogButtonBox.h"
37#include "UICommon.h"
38#include "UIFDCreationDialog.h"
39#include "UIFilePathSelector.h"
40#include "UIGlobalSession.h"
41#include "UIIconPool.h"
42#include "UIMedium.h"
43#include "UIMessageCenter.h"
44#include "UINotificationCenter.h"
45#include "UIModalWindowManager.h"
46
47/* COM includes: */
48#include "CSystemProperties.h"
49#include "CMediumFormat.h"
50
51
52UIFDCreationDialog::UIFDCreationDialog(QWidget *pParent,
53 const QString &strDefaultFolder,
54 const QString &strMachineName /* = QString() */)
55 : QIWithRetranslateUI<QDialog>(pParent)
56 , m_strDefaultFolder(strDefaultFolder)
57 , m_strMachineName(strMachineName)
58 , m_pPathLabel(0)
59 , m_pFilePathSelector(0)
60 , m_pSizeLabel(0)
61 , m_pSizeCombo(0)
62 , m_pFormatCheckBox(0)
63 , m_pButtonBox(0)
64{
65 prepare();
66}
67
68QUuid UIFDCreationDialog::mediumID() const
69{
70 return m_uMediumID;
71}
72
73/* static */
74QUuid UIFDCreationDialog::createFloppyDisk(QWidget *pParent, const QString &strDefaultFolder /* QString() */,
75 const QString &strMachineName /* = QString() */ )
76{
77 QString strStartPath(strDefaultFolder);
78
79 if (strStartPath.isEmpty())
80 strStartPath = uiCommon().defaultFolderPathForType(UIMediumDeviceType_Floppy);
81
82 QWidget *pDialogParent = windowManager().realParentWindow(pParent);
83
84 UIFDCreationDialog *pDialog = new UIFDCreationDialog(pParent, strStartPath, strMachineName);
85 if (!pDialog)
86 return QUuid();
87 windowManager().registerNewParent(pDialog, pDialogParent);
88
89 if (pDialog->exec())
90 {
91 QUuid uMediumID = pDialog->mediumID();
92 delete pDialog;
93 return uMediumID;
94 }
95 delete pDialog;
96 return QUuid();
97}
98
99void UIFDCreationDialog::accept()
100{
101 /* Make Ok button disabled first of all: */
102 m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
103
104 /* Acquire medium path & formats: */
105 const QString strMediumLocation = m_pFilePathSelector->path();
106 const QVector<CMediumFormat> mediumFormats = getFormatsForDeviceType(KDeviceType_Floppy);
107 /* Make sure we have both path and formats selected: */
108 if (strMediumLocation.isEmpty() || mediumFormats.isEmpty())
109 return;
110
111 /* Get VBox for further activities: */
112 CVirtualBox comVBox = gpGlobalSession->virtualBox();
113
114 /* Create medium: */
115 CMedium comMedium = comVBox.CreateMedium(mediumFormats[0].GetName(), strMediumLocation,
116 KAccessMode_ReadWrite, KDeviceType_Floppy);
117 if (!comVBox.isOk())
118 {
119 msgCenter().cannotCreateMediumStorage(comVBox, strMediumLocation, this);
120 return;
121 }
122
123 /* Compose medium storage variants: */
124 QVector<KMediumVariant> variants(1, KMediumVariant_Fixed);
125 /* Decide if disk formatting is required: */
126 if (m_pFormatCheckBox && m_pFormatCheckBox->checkState() == Qt::Checked)
127 variants.push_back(KMediumVariant_Formatted);
128
129 /* Create medium storage, asynchronously: */
130 UINotificationProgressMediumCreate *pNotification =
131 new UINotificationProgressMediumCreate(comMedium, m_pSizeCombo->currentData().toLongLong(), variants);
132 connect(pNotification, &UINotificationProgressMediumCreate::sigMediumCreated,
133 &uiCommon(), &UICommon::sltHandleMediumCreated);
134 connect(pNotification, &UINotificationProgressMediumCreate::sigMediumCreated,
135 this, &UIFDCreationDialog::sltHandleMediumCreated);
136 gpNotificationCenter->append(pNotification);
137}
138
139void UIFDCreationDialog::retranslateUi()
140{
141 if (m_strMachineName.isEmpty())
142 setWindowTitle(QString("%1").arg(tr("Floppy Disk Creator")));
143 else
144 setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Floppy Disk Creator")));
145 if (m_pPathLabel)
146 m_pPathLabel->setText(tr("File &Path:"));
147 if (m_pSizeLabel)
148 {
149 m_pSizeLabel->setText(tr("&Size:"));
150 m_pSizeLabel->setToolTip(tr("Sets the size of the floppy disk."));
151 }
152 if (m_pButtonBox)
153 m_pButtonBox->button(QDialogButtonBox::Ok)->setText("C&reate");
154 if (m_pFormatCheckBox)
155 {
156 m_pFormatCheckBox->setText(tr("&Format disk as FAT12"));
157 m_pFormatCheckBox->setToolTip(tr("Formats the floppy disk as FAT12."));
158 }
159 if (m_pSizeCombo)
160 {
161 m_pSizeCombo->setItemText(FDSize_2_88M, tr("2.88M"));
162 m_pSizeCombo->setItemText(FDSize_1_44M, tr("1.44M"));
163 m_pSizeCombo->setItemText(FDSize_1_2M, tr("1.2M"));
164 m_pSizeCombo->setItemText(FDSize_720K, tr("720K"));
165 m_pSizeCombo->setItemText(FDSize_360K, tr("360K"));
166 }
167
168 if (m_pButtonBox && m_pButtonBox->button(QDialogButtonBox::Ok))
169 m_pButtonBox->button(QDialogButtonBox::Ok)->setToolTip(tr("Create the disk and close this dialog."));
170
171 if (m_pButtonBox && m_pButtonBox->button(QDialogButtonBox::Cancel))
172 m_pButtonBox->button(QDialogButtonBox::Cancel)->setToolTip(tr("Cancel"));
173}
174
175void UIFDCreationDialog::sltHandleMediumCreated(const CMedium &comMedium)
176{
177 /* Store the ID of the newly created medium: */
178 m_uMediumID = comMedium.GetId();
179
180 /* Close the dialog now: */
181 QDialog::accept();
182}
183
184void UIFDCreationDialog::sltPathChanged(const QString &strPath)
185{
186 bool fIsFileUnique = checkFilePath(strPath);
187 m_pFilePathSelector->mark(!fIsFileUnique, tr("File already exists"));
188
189 if (m_pButtonBox && m_pButtonBox->button(QDialogButtonBox::Ok))
190 m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(fIsFileUnique);
191}
192
193void UIFDCreationDialog::prepare()
194{
195#ifndef VBOX_WS_MAC
196 /* Assign window icon: */
197 setWindowIcon(UIIconPool::iconSetFull(":/fd_add_32px.png", ":/fd_add_16px.png"));
198#endif
199
200 setWindowModality(Qt::WindowModal);
201 setSizeGripEnabled(false);
202
203 /* Prepare main layout: */
204 QGridLayout *pLayoutMain = new QGridLayout(this);
205 if (pLayoutMain)
206 {
207 /* Prepare path label: */
208 m_pPathLabel = new QLabel(this);
209 if (m_pPathLabel)
210 {
211 m_pPathLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
212 pLayoutMain->addWidget(m_pPathLabel, 0, 0);
213 }
214 /* Prepare file path selector: */
215 m_pFilePathSelector = new UIFilePathSelector(this);
216 if (m_pFilePathSelector)
217 {
218 m_pFilePathSelector->setMode(UIFilePathSelector::Mode_File_Save);
219 const QString strFilePath = getDefaultFilePath();
220 m_pFilePathSelector->setDefaultPath(strFilePath);
221 m_pFilePathSelector->setPath(strFilePath);
222
223 pLayoutMain->addWidget(m_pFilePathSelector, 0, 1, 1, 3);
224 connect(m_pFilePathSelector, &UIFilePathSelector::pathChanged,
225 this, &UIFDCreationDialog::sltPathChanged);
226 if (m_pPathLabel)
227 m_pPathLabel->setBuddy(m_pFilePathSelector);
228 }
229
230 /* Prepare size label: */
231 m_pSizeLabel = new QLabel(this);
232 if (m_pSizeLabel)
233 {
234 m_pSizeLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
235 pLayoutMain->addWidget(m_pSizeLabel, 1, 0);
236 }
237 /* Prepare size combo: */
238 m_pSizeCombo = new QComboBox(this);
239 if (m_pSizeCombo)
240 {
241 m_pSizeCombo->insertItem(FDSize_2_88M, "2.88M", 2949120);
242 m_pSizeCombo->insertItem(FDSize_1_44M, "1.44M", 1474560);
243 m_pSizeCombo->insertItem(FDSize_1_2M, "1.2M", 1228800);
244 m_pSizeCombo->insertItem(FDSize_720K, "720K", 737280);
245 m_pSizeCombo->insertItem(FDSize_360K, "360K", 368640);
246 m_pSizeCombo->setCurrentIndex(FDSize_1_44M);
247
248 pLayoutMain->addWidget(m_pSizeCombo, 1, 1);
249
250 if (m_pSizeLabel)
251 m_pSizeLabel->setBuddy(m_pSizeCombo);
252 }
253
254 /* Prepare format check-box: */
255 m_pFormatCheckBox = new QCheckBox;
256 if (m_pFormatCheckBox)
257 {
258 m_pFormatCheckBox->setCheckState(Qt::Checked);
259 pLayoutMain->addWidget(m_pFormatCheckBox, 2, 1, 1, 2);
260 }
261
262 /* Prepare button-box: */
263 m_pButtonBox = new QIDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
264 if (m_pButtonBox)
265 {
266 uiCommon().setHelpKeyword(m_pButtonBox->button(QDialogButtonBox::Help), "create-floppy-disk-image");
267 connect(m_pButtonBox, &QDialogButtonBox::accepted, this, &UIFDCreationDialog::accept);
268 connect(m_pButtonBox, &QDialogButtonBox::rejected, this, &UIFDCreationDialog::reject);
269 connect(m_pButtonBox->button(QDialogButtonBox::Help), &QPushButton::pressed,
270 m_pButtonBox, &QIDialogButtonBox::sltHandleHelpRequest);
271 pLayoutMain->addWidget(m_pButtonBox, 3, 0, 1, 3);
272 }
273 }
274
275 /* Apply language settings: */
276 retranslateUi();
277
278#ifdef VBOX_WS_MAC
279 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
280 setFixedSize(minimumSize());
281#endif /* VBOX_WS_MAC */
282
283 /* Adjust dialog size: */
284 adjustSize();
285}
286
287QString UIFDCreationDialog::getDefaultFilePath() const
288{
289 /* Prepare default file-path on the basis of passerd default folder: */
290 QString strDefaultFilePath = m_strDefaultFolder;
291
292 /* Make sure it's not empty if possible: */
293 if (strDefaultFilePath.isEmpty())
294 strDefaultFilePath = gpGlobalSession->virtualBox().GetSystemProperties().GetDefaultMachineFolder();
295 if (strDefaultFilePath.isEmpty())
296 return strDefaultFilePath;
297
298 /* Append file-path with disc name, generate unique file-name if necessary: */
299 QString strDiskname = !m_strMachineName.isEmpty() ? m_strMachineName : "NewFloppyDisk";
300 strDiskname = UICommon::findUniqueFileName(m_strDefaultFolder, strDiskname);
301
302 /* Append file-path with preferred extension finally: */
303 const QString strPreferredExtension = UIMediumDefs::getPreferredExtensionForMedium(KDeviceType_Floppy);
304 strDefaultFilePath = QDir(strDefaultFilePath).absoluteFilePath(strDiskname + "." + strPreferredExtension);
305
306 /* Return default file-path: */
307 return strDefaultFilePath;
308}
309
310bool UIFDCreationDialog::checkFilePath(const QString &strPath) const
311{
312 return !QFileInfo(strPath).exists();
313}
314
315/* static */
316QVector<CMediumFormat> UIFDCreationDialog::getFormatsForDeviceType(KDeviceType enmDeviceType)
317{
318 CSystemProperties comSystemProperties = gpGlobalSession->virtualBox().GetSystemProperties();
319 QVector<CMediumFormat> mediumFormats = comSystemProperties.GetMediumFormats();
320 QVector<CMediumFormat> formatList;
321 for (int i = 0; i < mediumFormats.size(); ++i)
322 {
323 /* File extensions */
324 QVector<QString> fileExtensions;
325 QVector<KDeviceType> deviceTypes;
326
327 mediumFormats[i].DescribeFileExtensions(fileExtensions, deviceTypes);
328 if (deviceTypes.contains(enmDeviceType))
329 formatList.push_back(mediumFormats[i]);
330 }
331 return formatList;
332}
Note: See TracBrowser for help on using the repository browser.

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