1 | /* $Id: UIMachineSettingsAudio.cpp 101230 2023-09-21 20:17:24Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIMachineSettingsAudio class implementation.
|
---|
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 | /* Qt includes: */
|
---|
29 | #include <QVBoxLayout>
|
---|
30 |
|
---|
31 | /* GUI includes: */
|
---|
32 | #include "UIAudioSettingsEditor.h"
|
---|
33 | #include "UIErrorString.h"
|
---|
34 | #include "UIMachineSettingsAudio.h"
|
---|
35 |
|
---|
36 | /* COM includes: */
|
---|
37 | #include "CAudioAdapter.h"
|
---|
38 | #include "CAudioSettings.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /** Machine settings: Audio page data structure. */
|
---|
42 | struct UIDataSettingsMachineAudio
|
---|
43 | {
|
---|
44 | /** Constructs data. */
|
---|
45 | UIDataSettingsMachineAudio()
|
---|
46 | : m_fAudioEnabled(false)
|
---|
47 | , m_audioDriverType(KAudioDriverType_Null)
|
---|
48 | , m_audioControllerType(KAudioControllerType_AC97)
|
---|
49 | , m_fAudioOutputEnabled(false)
|
---|
50 | , m_fAudioInputEnabled(false)
|
---|
51 | {}
|
---|
52 |
|
---|
53 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
54 | bool equal(const UIDataSettingsMachineAudio &other) const
|
---|
55 | {
|
---|
56 | return true
|
---|
57 | && (m_fAudioEnabled == other.m_fAudioEnabled)
|
---|
58 | && (m_audioDriverType == other.m_audioDriverType)
|
---|
59 | && (m_audioControllerType == other.m_audioControllerType)
|
---|
60 | && (m_fAudioOutputEnabled == other.m_fAudioOutputEnabled)
|
---|
61 | && (m_fAudioInputEnabled == other.m_fAudioInputEnabled)
|
---|
62 | ;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
66 | bool operator==(const UIDataSettingsMachineAudio &other) const { return equal(other); }
|
---|
67 | /** Returns whether the @a other passed data is different from this one. */
|
---|
68 | bool operator!=(const UIDataSettingsMachineAudio &other) const { return !equal(other); }
|
---|
69 |
|
---|
70 | /** Holds whether the audio is enabled. */
|
---|
71 | bool m_fAudioEnabled;
|
---|
72 | /** Holds the audio driver type. */
|
---|
73 | KAudioDriverType m_audioDriverType;
|
---|
74 | /** Holds the audio controller type. */
|
---|
75 | KAudioControllerType m_audioControllerType;
|
---|
76 | /** Holds whether the audio output is enabled. */
|
---|
77 | bool m_fAudioOutputEnabled;
|
---|
78 | /** Holds whether the audio input is enabled. */
|
---|
79 | bool m_fAudioInputEnabled;
|
---|
80 | };
|
---|
81 |
|
---|
82 |
|
---|
83 | UIMachineSettingsAudio::UIMachineSettingsAudio()
|
---|
84 | : m_pCache(0)
|
---|
85 | , m_pEditorAudioSettings(0)
|
---|
86 | {
|
---|
87 | prepare();
|
---|
88 | }
|
---|
89 |
|
---|
90 | UIMachineSettingsAudio::~UIMachineSettingsAudio()
|
---|
91 | {
|
---|
92 | cleanup();
|
---|
93 | }
|
---|
94 |
|
---|
95 | bool UIMachineSettingsAudio::changed() const
|
---|
96 | {
|
---|
97 | return m_pCache ? m_pCache->wasChanged() : false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | void UIMachineSettingsAudio::loadToCacheFrom(QVariant &data)
|
---|
101 | {
|
---|
102 | /* Sanity check: */
|
---|
103 | if (!m_pCache)
|
---|
104 | return;
|
---|
105 |
|
---|
106 | /* Fetch data to machine: */
|
---|
107 | UISettingsPageMachine::fetchData(data);
|
---|
108 |
|
---|
109 | /* Clear cache initially: */
|
---|
110 | m_pCache->clear();
|
---|
111 |
|
---|
112 | /* Prepare old data: */
|
---|
113 | UIDataSettingsMachineAudio oldAudioData;
|
---|
114 |
|
---|
115 | /* Check whether adapter is valid: */
|
---|
116 | const CAudioSettings &comAudioSettings = m_machine.GetAudioSettings();
|
---|
117 | const CAudioAdapter &comAdapter = comAudioSettings.GetAdapter();
|
---|
118 | if (!comAdapter.isNull())
|
---|
119 | {
|
---|
120 | /* Gather old data: */
|
---|
121 | oldAudioData.m_fAudioEnabled = comAdapter.GetEnabled();
|
---|
122 | oldAudioData.m_audioDriverType = comAdapter.GetAudioDriver();
|
---|
123 | oldAudioData.m_audioControllerType = comAdapter.GetAudioController();
|
---|
124 | oldAudioData.m_fAudioOutputEnabled = comAdapter.GetEnabledOut();
|
---|
125 | oldAudioData.m_fAudioInputEnabled = comAdapter.GetEnabledIn();
|
---|
126 | }
|
---|
127 |
|
---|
128 | /* Cache old data: */
|
---|
129 | m_pCache->cacheInitialData(oldAudioData);
|
---|
130 |
|
---|
131 | /* Upload machine to data: */
|
---|
132 | UISettingsPageMachine::uploadData(data);
|
---|
133 | }
|
---|
134 |
|
---|
135 | void UIMachineSettingsAudio::getFromCache()
|
---|
136 | {
|
---|
137 | /* Sanity check: */
|
---|
138 | if (!m_pCache)
|
---|
139 | return;
|
---|
140 |
|
---|
141 | /* Get old data from cache: */
|
---|
142 | const UIDataSettingsMachineAudio &oldAudioData = m_pCache->base();
|
---|
143 |
|
---|
144 | /* Load old data from cache: */
|
---|
145 | if (m_pEditorAudioSettings)
|
---|
146 | {
|
---|
147 | m_pEditorAudioSettings->setFeatureEnabled(oldAudioData.m_fAudioEnabled);
|
---|
148 | m_pEditorAudioSettings->setHostDriverType(oldAudioData.m_audioDriverType);
|
---|
149 | m_pEditorAudioSettings->setControllerType(oldAudioData.m_audioControllerType);
|
---|
150 | m_pEditorAudioSettings->setEnableOutput(oldAudioData.m_fAudioOutputEnabled);
|
---|
151 | m_pEditorAudioSettings->setEnableInput(oldAudioData.m_fAudioInputEnabled);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /* Polish page finally: */
|
---|
155 | polishPage();
|
---|
156 | }
|
---|
157 |
|
---|
158 | void UIMachineSettingsAudio::putToCache()
|
---|
159 | {
|
---|
160 | /* Sanity check: */
|
---|
161 | if (!m_pCache)
|
---|
162 | return;
|
---|
163 |
|
---|
164 | /* Prepare new data: */
|
---|
165 | UIDataSettingsMachineAudio newAudioData;
|
---|
166 |
|
---|
167 | /* Cache new data: */
|
---|
168 | if (m_pEditorAudioSettings)
|
---|
169 | {
|
---|
170 | newAudioData.m_fAudioEnabled = m_pEditorAudioSettings->isFeatureEnabled();
|
---|
171 | newAudioData.m_audioDriverType = m_pEditorAudioSettings->hostDriverType();
|
---|
172 | newAudioData.m_audioControllerType = m_pEditorAudioSettings->controllerType();
|
---|
173 | newAudioData.m_fAudioOutputEnabled = m_pEditorAudioSettings->outputEnabled();
|
---|
174 | newAudioData.m_fAudioInputEnabled = m_pEditorAudioSettings->inputEnabled();
|
---|
175 | }
|
---|
176 | m_pCache->cacheCurrentData(newAudioData);
|
---|
177 | }
|
---|
178 |
|
---|
179 | void UIMachineSettingsAudio::saveFromCacheTo(QVariant &data)
|
---|
180 | {
|
---|
181 | /* Fetch data to machine: */
|
---|
182 | UISettingsPageMachine::fetchData(data);
|
---|
183 |
|
---|
184 | /* Update data and failing state: */
|
---|
185 | setFailed(!saveData());
|
---|
186 |
|
---|
187 | /* Upload machine to data: */
|
---|
188 | UISettingsPageMachine::uploadData(data);
|
---|
189 | }
|
---|
190 |
|
---|
191 | void UIMachineSettingsAudio::retranslateUi()
|
---|
192 | {
|
---|
193 | }
|
---|
194 |
|
---|
195 | void UIMachineSettingsAudio::polishPage()
|
---|
196 | {
|
---|
197 | /* Polish audio page availability: */
|
---|
198 | if (m_pEditorAudioSettings)
|
---|
199 | {
|
---|
200 | m_pEditorAudioSettings->setFeatureAvailable(isMachineOffline());
|
---|
201 | m_pEditorAudioSettings->setHostDriverOptionAvailable(isMachineOffline() || isMachineSaved());
|
---|
202 | m_pEditorAudioSettings->setControllerOptionAvailable(isMachineOffline());
|
---|
203 | m_pEditorAudioSettings->setFeatureOptionsAvailable(isMachineInValidMode());
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | void UIMachineSettingsAudio::prepare()
|
---|
208 | {
|
---|
209 | /* Prepare cache: */
|
---|
210 | m_pCache = new UISettingsCacheMachineAudio;
|
---|
211 | AssertPtrReturnVoid(m_pCache);
|
---|
212 |
|
---|
213 | /* Prepare everything: */
|
---|
214 | prepareWidgets();
|
---|
215 | prepareConnections();
|
---|
216 |
|
---|
217 | /* Apply language settings: */
|
---|
218 | retranslateUi();
|
---|
219 | }
|
---|
220 |
|
---|
221 | void UIMachineSettingsAudio::prepareWidgets()
|
---|
222 | {
|
---|
223 | /* Prepare main layout: */
|
---|
224 | QVBoxLayout *pLayout = new QVBoxLayout(this);
|
---|
225 | if (pLayout)
|
---|
226 | {
|
---|
227 | /* Prepare settings editor: */
|
---|
228 | m_pEditorAudioSettings = new UIAudioSettingsEditor(this);
|
---|
229 | if (m_pEditorAudioSettings)
|
---|
230 | {
|
---|
231 | addEditor(m_pEditorAudioSettings);
|
---|
232 | pLayout->addWidget(m_pEditorAudioSettings);
|
---|
233 | }
|
---|
234 |
|
---|
235 | pLayout->addStretch();
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | void UIMachineSettingsAudio::prepareConnections()
|
---|
240 | {
|
---|
241 | }
|
---|
242 |
|
---|
243 | void UIMachineSettingsAudio::cleanup()
|
---|
244 | {
|
---|
245 | /* Cleanup cache: */
|
---|
246 | delete m_pCache;
|
---|
247 | m_pCache = 0;
|
---|
248 | }
|
---|
249 |
|
---|
250 | bool UIMachineSettingsAudio::saveData()
|
---|
251 | {
|
---|
252 | /* Sanity check: */
|
---|
253 | if (!m_pCache)
|
---|
254 | return false;
|
---|
255 |
|
---|
256 | /* Prepare result: */
|
---|
257 | bool fSuccess = true;
|
---|
258 | /* Save audio settings from cache: */
|
---|
259 | if (fSuccess && isMachineInValidMode() && m_pCache->wasChanged())
|
---|
260 | {
|
---|
261 | /* Get old data from cache: */
|
---|
262 | const UIDataSettingsMachineAudio &oldAudioData = m_pCache->base();
|
---|
263 | /* Get new data from cache: */
|
---|
264 | const UIDataSettingsMachineAudio &newAudioData = m_pCache->data();
|
---|
265 |
|
---|
266 | /* Get audio adapter for further activities: */
|
---|
267 | const CAudioSettings comAudioSettings = m_machine.GetAudioSettings();
|
---|
268 |
|
---|
269 | CAudioAdapter comAdapter = comAudioSettings.GetAdapter();
|
---|
270 | fSuccess = m_machine.isOk() && comAdapter.isNotNull();
|
---|
271 |
|
---|
272 | /* Show error message if necessary: */
|
---|
273 | if (!fSuccess)
|
---|
274 | notifyOperationProgressError(UIErrorString::formatErrorInfo(m_machine));
|
---|
275 | else
|
---|
276 | {
|
---|
277 | /* Save whether audio is enabled: */
|
---|
278 | if (fSuccess && isMachineOffline() && newAudioData.m_fAudioEnabled != oldAudioData.m_fAudioEnabled)
|
---|
279 | {
|
---|
280 | comAdapter.SetEnabled(newAudioData.m_fAudioEnabled);
|
---|
281 | fSuccess = comAdapter.isOk();
|
---|
282 | }
|
---|
283 | /* Save audio driver type: */
|
---|
284 | if (fSuccess && (isMachineOffline() || isMachineSaved()) && newAudioData.m_audioDriverType != oldAudioData.m_audioDriverType)
|
---|
285 | {
|
---|
286 | comAdapter.SetAudioDriver(newAudioData.m_audioDriverType);
|
---|
287 | fSuccess = comAdapter.isOk();
|
---|
288 | }
|
---|
289 | /* Save audio controller type: */
|
---|
290 | if (fSuccess && isMachineOffline() && newAudioData.m_audioControllerType != oldAudioData.m_audioControllerType)
|
---|
291 | {
|
---|
292 | comAdapter.SetAudioController(newAudioData.m_audioControllerType);
|
---|
293 | fSuccess = comAdapter.isOk();
|
---|
294 | }
|
---|
295 | /* Save whether audio output is enabled: */
|
---|
296 | if (fSuccess && isMachineInValidMode() && newAudioData.m_fAudioOutputEnabled != oldAudioData.m_fAudioOutputEnabled)
|
---|
297 | {
|
---|
298 | comAdapter.SetEnabledOut(newAudioData.m_fAudioOutputEnabled);
|
---|
299 | fSuccess = comAdapter.isOk();
|
---|
300 | }
|
---|
301 | /* Save whether audio input is enabled: */
|
---|
302 | if (fSuccess && isMachineInValidMode() && newAudioData.m_fAudioInputEnabled != oldAudioData.m_fAudioInputEnabled)
|
---|
303 | {
|
---|
304 | comAdapter.SetEnabledIn(newAudioData.m_fAudioInputEnabled);
|
---|
305 | fSuccess = comAdapter.isOk();
|
---|
306 | }
|
---|
307 |
|
---|
308 | /* Show error message if necessary: */
|
---|
309 | if (!fSuccess)
|
---|
310 | notifyOperationProgressError(UIErrorString::formatErrorInfo(comAdapter));
|
---|
311 | }
|
---|
312 | }
|
---|
313 | /* Return result: */
|
---|
314 | return fSuccess;
|
---|
315 | }
|
---|