summaryrefslogtreecommitdiff
path: root/src/VBox/Frontends/VirtualBox/src/medium/UIMediumDefs.cpp
blob: 7765b3a9560889406a7fd7274d9169e502d42970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* $Id$ */
/** @file
 * VBox Qt GUI - UIMedium related implementations.
 */

/*
 * Copyright (C) 2006-2020 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

/* GUI includes: */
#include "UIMediumDefs.h"
#include "UICommon.h"

/* COM includes: */
#include "CMediumFormat.h"
#include "CSystemProperties.h"

/* COM includes: */
#include "CMediumFormat.h"
#include "CSystemProperties.h"
#include "CVirtualBox.h"


/* Convert global medium type (KDeviceType) to local (UIMediumDeviceType): */
UIMediumDeviceType UIMediumDefs::mediumTypeToLocal(KDeviceType globalType)
{
    switch (globalType)
    {
        case KDeviceType_HardDisk:
            return UIMediumDeviceType_HardDisk;
        case KDeviceType_DVD:
            return UIMediumDeviceType_DVD;
        case KDeviceType_Floppy:
            return UIMediumDeviceType_Floppy;
        default:
            break;
    }
    return UIMediumDeviceType_Invalid;
}

/* Convert local medium type (UIMediumDeviceType) to global (KDeviceType): */
KDeviceType UIMediumDefs::mediumTypeToGlobal(UIMediumDeviceType localType)
{
    switch (localType)
    {
        case UIMediumDeviceType_HardDisk:
            return KDeviceType_HardDisk;
        case UIMediumDeviceType_DVD:
            return KDeviceType_DVD;
        case UIMediumDeviceType_Floppy:
            return KDeviceType_Floppy;
        default:
            break;
    }
    return KDeviceType_Null;
}

QList<QPair<QString, QString> > UIMediumDefs::MediumBackends(const CVirtualBox &comVBox, KDeviceType enmType)
{
    /* Prepare a list of pairs with the form <tt>{"Backend Name", "*.suffix1 .suffix2 ..."}</tt>. */
    const CSystemProperties comSystemProperties = comVBox.GetSystemProperties();
    QVector<CMediumFormat> mediumFormats = comSystemProperties.GetMediumFormats();
    QList<QPair<QString, QString> > backendPropList;
    for (int i = 0; i < mediumFormats.size(); ++i)
    {
        /* Acquire file extensions & device types: */
        QVector<QString> fileExtensions;
        QVector<KDeviceType> deviceTypes;
        mediumFormats[i].DescribeFileExtensions(fileExtensions, deviceTypes);

        /* Compose filters list: */
        QStringList filters;
        for (int iExtensionIndex = 0; iExtensionIndex < fileExtensions.size(); ++iExtensionIndex)
            if (deviceTypes[iExtensionIndex] == enmType)
                filters << QString("*.%1").arg(fileExtensions[iExtensionIndex]);
        /* Create a pair out of the backend description and all suffix's. */
        if (!filters.isEmpty())
            backendPropList << QPair<QString, QString>(mediumFormats[i].GetName(), filters.join(" "));
    }
    return backendPropList;
}

QList<QPair<QString, QString> > UIMediumDefs::HDDBackends(const CVirtualBox &comVBox)
{
    return MediumBackends(comVBox, KDeviceType_HardDisk);
}

QList<QPair<QString, QString> > UIMediumDefs::DVDBackends(const CVirtualBox &comVBox)
{
    return MediumBackends(comVBox, KDeviceType_DVD);
}

QList<QPair<QString, QString> > UIMediumDefs::FloppyBackends(const CVirtualBox &comVBox)
{
    return MediumBackends(comVBox, KDeviceType_Floppy);
}

QString UIMediumDefs::getPreferredExtensionForMedium(KDeviceType enmDeviceType)
{
    CSystemProperties comSystemProperties = uiCommon().virtualBox().GetSystemProperties();
    QVector<CMediumFormat> mediumFormats = comSystemProperties.GetMediumFormats();
    for (int i = 0; i < mediumFormats.size(); ++i)
    {
        /* File extensions */
        QVector <QString> fileExtensions;
        QVector <KDeviceType> deviceTypes;

        mediumFormats[i].DescribeFileExtensions(fileExtensions, deviceTypes);
        if (fileExtensions.size() != deviceTypes.size())
            continue;
        for (int a = 0; a < fileExtensions.size(); ++a)
        {
            if (deviceTypes[a] == enmDeviceType)
                return fileExtensions[a];
        }
    }
    return QString();
}

QVector<CMediumFormat> UIMediumDefs::getFormatsForDeviceType(KDeviceType enmDeviceType)
{
    CSystemProperties comSystemProperties = uiCommon().virtualBox().GetSystemProperties();
    QVector<CMediumFormat> mediumFormats = comSystemProperties.GetMediumFormats();
    QVector<CMediumFormat> formatList;
    for (int i = 0; i < mediumFormats.size(); ++i)
    {
        /* File extensions */
        QVector <QString> fileExtensions;
        QVector <KDeviceType> deviceTypes;

        mediumFormats[i].DescribeFileExtensions(fileExtensions, deviceTypes);
        if (deviceTypes.contains(enmDeviceType))
            formatList.push_back(mediumFormats[i]);
    }
    return formatList;
}