VirtualBox

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

Last change on this file was 104226, checked in by vboxsync, 6 weeks ago

FE/Qt. bugref:10622. Using new UITranslationEventListener in medium manager related classes.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use