VirtualBox

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

Last change on this file was 104915, checked in by vboxsync, 3 months ago

FE/Qt: UIMediumEnumerator: Moving some unrelated stuff to UIMediumTools namespace as that's more suitable place for this one.

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