1 | /* $Id: UIMediumDefs.cpp 103771 2024-03-11 15:16:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIMedium related implementations.
|
---|
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 "UIGlobalSession.h"
|
---|
30 | #include "UIMediumDefs.h"
|
---|
31 |
|
---|
32 | /* COM includes: */
|
---|
33 | #include "CMediumFormat.h"
|
---|
34 | #include "CSystemProperties.h"
|
---|
35 |
|
---|
36 | /* COM includes: */
|
---|
37 | #include "CMediumFormat.h"
|
---|
38 | #include "CSystemProperties.h"
|
---|
39 | #include "CVirtualBox.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /* Convert global medium type (KDeviceType) to local (UIMediumDeviceType): */
|
---|
43 | UIMediumDeviceType UIMediumDefs::mediumTypeToLocal(KDeviceType globalType)
|
---|
44 | {
|
---|
45 | switch (globalType)
|
---|
46 | {
|
---|
47 | case KDeviceType_HardDisk:
|
---|
48 | return UIMediumDeviceType_HardDisk;
|
---|
49 | case KDeviceType_DVD:
|
---|
50 | return UIMediumDeviceType_DVD;
|
---|
51 | case KDeviceType_Floppy:
|
---|
52 | return UIMediumDeviceType_Floppy;
|
---|
53 | default:
|
---|
54 | break;
|
---|
55 | }
|
---|
56 | return UIMediumDeviceType_Invalid;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /* Convert local medium type (UIMediumDeviceType) to global (KDeviceType): */
|
---|
60 | KDeviceType UIMediumDefs::mediumTypeToGlobal(UIMediumDeviceType localType)
|
---|
61 | {
|
---|
62 | switch (localType)
|
---|
63 | {
|
---|
64 | case UIMediumDeviceType_HardDisk:
|
---|
65 | return KDeviceType_HardDisk;
|
---|
66 | case UIMediumDeviceType_DVD:
|
---|
67 | return KDeviceType_DVD;
|
---|
68 | case UIMediumDeviceType_Floppy:
|
---|
69 | return KDeviceType_Floppy;
|
---|
70 | default:
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | return KDeviceType_Null;
|
---|
74 | }
|
---|
75 |
|
---|
76 | QList<QPair<QString, QString> > UIMediumDefs::MediumBackends(const CVirtualBox &comVBox, KDeviceType enmType)
|
---|
77 | {
|
---|
78 | /* Prepare a list of pairs with the form <tt>{"Backend Name", "*.suffix1 .suffix2 ..."}</tt>. */
|
---|
79 | const CSystemProperties comSystemProperties = comVBox.GetSystemProperties();
|
---|
80 | QVector<CMediumFormat> mediumFormats = comSystemProperties.GetMediumFormats();
|
---|
81 | QList<QPair<QString, QString> > backendPropList;
|
---|
82 | for (int i = 0; i < mediumFormats.size(); ++i)
|
---|
83 | {
|
---|
84 | /* Acquire file extensions & device types: */
|
---|
85 | QVector<QString> fileExtensions;
|
---|
86 | QVector<KDeviceType> deviceTypes;
|
---|
87 | mediumFormats[i].DescribeFileExtensions(fileExtensions, deviceTypes);
|
---|
88 |
|
---|
89 | /* Compose filters list: */
|
---|
90 | QStringList filters;
|
---|
91 | for (int iExtensionIndex = 0; iExtensionIndex < fileExtensions.size(); ++iExtensionIndex)
|
---|
92 | if (deviceTypes[iExtensionIndex] == enmType)
|
---|
93 | filters << QString("*.%1").arg(fileExtensions[iExtensionIndex]);
|
---|
94 | /* Create a pair out of the backend description and all suffix's. */
|
---|
95 | if (!filters.isEmpty())
|
---|
96 | backendPropList << QPair<QString, QString>(mediumFormats[i].GetName(), filters.join(" "));
|
---|
97 | }
|
---|
98 | return backendPropList;
|
---|
99 | }
|
---|
100 |
|
---|
101 | QList<QPair<QString, QString> > UIMediumDefs::HDDBackends(const CVirtualBox &comVBox)
|
---|
102 | {
|
---|
103 | return MediumBackends(comVBox, KDeviceType_HardDisk);
|
---|
104 | }
|
---|
105 |
|
---|
106 | QList<QPair<QString, QString> > UIMediumDefs::DVDBackends(const CVirtualBox &comVBox)
|
---|
107 | {
|
---|
108 | return MediumBackends(comVBox, KDeviceType_DVD);
|
---|
109 | }
|
---|
110 |
|
---|
111 | QList<QPair<QString, QString> > UIMediumDefs::FloppyBackends(const CVirtualBox &comVBox)
|
---|
112 | {
|
---|
113 | return MediumBackends(comVBox, KDeviceType_Floppy);
|
---|
114 | }
|
---|
115 |
|
---|
116 | QString UIMediumDefs::getPreferredExtensionForMedium(KDeviceType enmDeviceType)
|
---|
117 | {
|
---|
118 | CSystemProperties comSystemProperties = gpGlobalSession->virtualBox().GetSystemProperties();
|
---|
119 | QVector<CMediumFormat> mediumFormats = comSystemProperties.GetMediumFormats();
|
---|
120 | for (int i = 0; i < mediumFormats.size(); ++i)
|
---|
121 | {
|
---|
122 | /* File extensions */
|
---|
123 | QVector <QString> fileExtensions;
|
---|
124 | QVector <KDeviceType> deviceTypes;
|
---|
125 |
|
---|
126 | mediumFormats[i].DescribeFileExtensions(fileExtensions, deviceTypes);
|
---|
127 | if (fileExtensions.size() != deviceTypes.size())
|
---|
128 | continue;
|
---|
129 | for (int a = 0; a < fileExtensions.size(); ++a)
|
---|
130 | {
|
---|
131 | if (deviceTypes[a] == enmDeviceType)
|
---|
132 | return fileExtensions[a];
|
---|
133 | }
|
---|
134 | }
|
---|
135 | return QString();
|
---|
136 | }
|
---|