1 | /* $Id: UIMediumEnumerator.cpp 103803 2024-03-12 11:15:18Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIMediumEnumerator class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-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 <QSet>
|
---|
30 |
|
---|
31 | /* GUI includes: */
|
---|
32 | #include "UICommon.h"
|
---|
33 | #include "UIErrorString.h"
|
---|
34 | #include "UIGlobalSession.h"
|
---|
35 | #include "UILoggingDefs.h"
|
---|
36 | #include "UIMediumEnumerator.h"
|
---|
37 | #include "UINotificationCenter.h"
|
---|
38 | #include "UITask.h"
|
---|
39 | #include "UIThreadPool.h"
|
---|
40 | #include "UIVirtualBoxEventHandler.h"
|
---|
41 |
|
---|
42 | /* COM includes: */
|
---|
43 | #include "CMachine.h"
|
---|
44 | #include "CMediumAttachment.h"
|
---|
45 | #include "CSnapshot.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /** Template function to convert a list of
|
---|
49 | * abstract objects to a human readable string list.
|
---|
50 | * @note T should have .toString() member implemented. */
|
---|
51 | template<class T> static QStringList toStringList(const QList<T> &list)
|
---|
52 | {
|
---|
53 | QStringList l;
|
---|
54 | foreach(const T &t, list)
|
---|
55 | l << t.toString();
|
---|
56 | return l;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /** UITask extension used for medium-enumeration purposes.
|
---|
61 | * @note We made setting/getting medium a thread-safe stuff. But this wasn't
|
---|
62 | * dangerous for us before since setter/getter calls are splitted in time
|
---|
63 | * by enumeration logic. Previously we were even using
|
---|
64 | * property/setProperty API for that but latest Qt versions prohibits
|
---|
65 | * property/setProperty API usage from other than the GUI thread so we
|
---|
66 | * had to rework that stuff to be thread-safe for Qt >= 5.11. */
|
---|
67 | class UITaskMediumEnumeration : public UITask
|
---|
68 | {
|
---|
69 | Q_OBJECT;
|
---|
70 |
|
---|
71 | public:
|
---|
72 |
|
---|
73 | /** Constructs @a guiMedium enumeration task. */
|
---|
74 | UITaskMediumEnumeration(const UIMedium &guiMedium)
|
---|
75 | : UITask(UITask::Type_MediumEnumeration)
|
---|
76 | , m_guiMedium(guiMedium)
|
---|
77 | {}
|
---|
78 |
|
---|
79 | /** Returns GUI medium. */
|
---|
80 | UIMedium medium() const
|
---|
81 | {
|
---|
82 | /* Acquire under a proper lock: */
|
---|
83 | m_mutex.lock();
|
---|
84 | const UIMedium guiMedium = m_guiMedium;
|
---|
85 | m_mutex.unlock();
|
---|
86 | return guiMedium;
|
---|
87 | }
|
---|
88 |
|
---|
89 | private:
|
---|
90 |
|
---|
91 | /** Contains medium-enumeration task body. */
|
---|
92 | virtual void run() RT_OVERRIDE
|
---|
93 | {
|
---|
94 | /* Enumerate under a proper lock: */
|
---|
95 | m_mutex.lock();
|
---|
96 | m_guiMedium.blockAndQueryState();
|
---|
97 | m_mutex.unlock();
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Holds the mutex to access m_guiMedium member. */
|
---|
101 | mutable QMutex m_mutex;
|
---|
102 | /** Holds the medium being enumerated. */
|
---|
103 | UIMedium m_guiMedium;
|
---|
104 | };
|
---|
105 |
|
---|
106 |
|
---|
107 | /*********************************************************************************************************************************
|
---|
108 | * Class UIMediumEnumerator implementation. *
|
---|
109 | *********************************************************************************************************************************/
|
---|
110 |
|
---|
111 | UIMediumEnumerator::UIMediumEnumerator()
|
---|
112 | : m_fFullMediumEnumerationRequested(false)
|
---|
113 | , m_fMediumEnumerationInProgress(false)
|
---|
114 | {
|
---|
115 | /* Allow UIMedium to be used in inter-thread signals: */
|
---|
116 | qRegisterMetaType<UIMedium>();
|
---|
117 |
|
---|
118 | /* Prepare Main event handlers: */
|
---|
119 | /* Machine related events: */
|
---|
120 | connect(gVBoxEvents, &UIVirtualBoxEventHandler::sigMachineDataChange,
|
---|
121 | this, &UIMediumEnumerator::sltHandleMachineDataChange);
|
---|
122 | /* Medium related events: */
|
---|
123 | connect(gVBoxEvents, &UIVirtualBoxEventHandler::sigStorageControllerChange,
|
---|
124 | this, &UIMediumEnumerator::sltHandleStorageControllerChange);
|
---|
125 | connect(gVBoxEvents, &UIVirtualBoxEventHandler::sigStorageDeviceChange,
|
---|
126 | this, &UIMediumEnumerator::sltHandleStorageDeviceChange);
|
---|
127 | connect(gVBoxEvents, &UIVirtualBoxEventHandler::sigMediumChange,
|
---|
128 | this, &UIMediumEnumerator::sltHandleMediumChange);
|
---|
129 | connect(gVBoxEvents, &UIVirtualBoxEventHandler::sigMediumConfigChange,
|
---|
130 | this, &UIMediumEnumerator::sltHandleMediumConfigChange);
|
---|
131 | connect(gVBoxEvents, &UIVirtualBoxEventHandler::sigMediumRegistered,
|
---|
132 | this, &UIMediumEnumerator::sltHandleMediumRegistered);
|
---|
133 |
|
---|
134 | /* Prepare global thread-pool listener: */
|
---|
135 | connect(uiCommon().threadPool(), &UIThreadPool::sigTaskComplete,
|
---|
136 | this, &UIMediumEnumerator::sltHandleMediumEnumerationTaskComplete);
|
---|
137 |
|
---|
138 | /* We should make sure media map contains at least NULL medium object: */
|
---|
139 | addNullMediumToMap(m_media);
|
---|
140 | /* Notify listener about initial enumeration started/finished instantly: */
|
---|
141 | LogRel(("GUI: UIMediumEnumerator: Initial medium-enumeration finished!\n"));
|
---|
142 | emit sigMediumEnumerationStarted();
|
---|
143 | emit sigMediumEnumerationFinished();
|
---|
144 | }
|
---|
145 |
|
---|
146 | QList<QUuid> UIMediumEnumerator::mediumIDs() const
|
---|
147 | {
|
---|
148 | /* Return keys of current media map: */
|
---|
149 | return m_media.keys();
|
---|
150 | }
|
---|
151 |
|
---|
152 | UIMedium UIMediumEnumerator::medium(const QUuid &uMediumID) const
|
---|
153 | {
|
---|
154 | /* Search through current media map
|
---|
155 | * for the UIMedium with passed ID: */
|
---|
156 | if (m_media.contains(uMediumID))
|
---|
157 | return m_media.value(uMediumID);
|
---|
158 | /* Return NULL UIMedium otherwise: */
|
---|
159 | return UIMedium();
|
---|
160 | }
|
---|
161 |
|
---|
162 | void UIMediumEnumerator::createMedium(const UIMedium &guiMedium)
|
---|
163 | {
|
---|
164 | /* Get UIMedium ID: */
|
---|
165 | const QUuid uMediumID = guiMedium.id();
|
---|
166 |
|
---|
167 | /* Do not create UIMedium(s) with incorrect ID: */
|
---|
168 | AssertReturnVoid(!uMediumID.isNull());
|
---|
169 | /* Make sure UIMedium doesn't exist already: */
|
---|
170 | if (m_media.contains(uMediumID))
|
---|
171 | return;
|
---|
172 |
|
---|
173 | /* Insert UIMedium: */
|
---|
174 | m_media[uMediumID] = guiMedium;
|
---|
175 | LogRel(("GUI: UIMediumEnumerator: Medium with key={%s} created\n", uMediumID.toString().toUtf8().constData()));
|
---|
176 |
|
---|
177 | /* Notify listener: */
|
---|
178 | emit sigMediumCreated(uMediumID);
|
---|
179 | }
|
---|
180 |
|
---|
181 | void UIMediumEnumerator::enumerateMedia(const CMediumVector &comMedia /* = CMediumVector() */)
|
---|
182 | {
|
---|
183 | /* Compose new map of currently cached media & their children.
|
---|
184 | * While composing we are using data from already cached media. */
|
---|
185 | UIMediumMap guiMedia;
|
---|
186 | addNullMediumToMap(guiMedia);
|
---|
187 | if (comMedia.isEmpty())
|
---|
188 | {
|
---|
189 | /* Compose new map of all known media & their children: */
|
---|
190 | addMediaToMap(gpGlobalSession->virtualBox().GetHardDisks(), guiMedia);
|
---|
191 | addMediaToMap(gpGlobalSession->host().GetDVDDrives(), guiMedia);
|
---|
192 | addMediaToMap(gpGlobalSession->virtualBox().GetDVDImages(), guiMedia);
|
---|
193 | addMediaToMap(gpGlobalSession->host().GetFloppyDrives(), guiMedia);
|
---|
194 | addMediaToMap(gpGlobalSession->virtualBox().GetFloppyImages(), guiMedia);
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | /* Compose new map of passed media & their children: */
|
---|
199 | addMediaToMap(comMedia, guiMedia);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /* UICommon is cleaning up, abort immediately: */
|
---|
203 | if (uiCommon().isCleaningUp())
|
---|
204 | return;
|
---|
205 |
|
---|
206 | if (comMedia.isEmpty())
|
---|
207 | {
|
---|
208 | /* Replace existing media map since
|
---|
209 | * we have full medium enumeration: */
|
---|
210 | m_fFullMediumEnumerationRequested = true;
|
---|
211 | m_media = guiMedia;
|
---|
212 | }
|
---|
213 | else
|
---|
214 | {
|
---|
215 | /* Throw the media to existing map: */
|
---|
216 | foreach (const QUuid &uMediumId, guiMedia.keys())
|
---|
217 | m_media[uMediumId] = guiMedia.value(uMediumId);
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* If enumeration hasn't yet started: */
|
---|
221 | if (!m_fMediumEnumerationInProgress)
|
---|
222 | {
|
---|
223 | /* Notify listener about enumeration started: */
|
---|
224 | LogRel(("GUI: UIMediumEnumerator: Medium-enumeration started...\n"));
|
---|
225 | m_fMediumEnumerationInProgress = true;
|
---|
226 | emit sigMediumEnumerationStarted();
|
---|
227 |
|
---|
228 | /* Make sure we really have more than one UIMedium (which is NULL): */
|
---|
229 | if ( guiMedia.size() == 1
|
---|
230 | && guiMedia.first().id() == UIMedium::nullID())
|
---|
231 | {
|
---|
232 | /* Notify listener about enumeration finished instantly: */
|
---|
233 | LogRel(("GUI: UIMediumEnumerator: Medium-enumeration finished!\n"));
|
---|
234 | m_fMediumEnumerationInProgress = false;
|
---|
235 | emit sigMediumEnumerationFinished();
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | /* Start enumeration for media with non-NULL ID: */
|
---|
240 | foreach (const QUuid &uMediumID, guiMedia.keys())
|
---|
241 | if (!uMediumID.isNull())
|
---|
242 | createMediumEnumerationTask(guiMedia[uMediumID]);
|
---|
243 | }
|
---|
244 |
|
---|
245 | void UIMediumEnumerator::refreshMedia()
|
---|
246 | {
|
---|
247 | /* Make sure we are not already in progress: */
|
---|
248 | AssertReturnVoid(!m_fMediumEnumerationInProgress);
|
---|
249 |
|
---|
250 | /* Refresh all cached media we have: */
|
---|
251 | foreach (const QUuid &uMediumID, m_media.keys())
|
---|
252 | m_media[uMediumID].refresh();
|
---|
253 | }
|
---|
254 |
|
---|
255 | void UIMediumEnumerator::retranslateUi()
|
---|
256 | {
|
---|
257 | /* Translating NULL UIMedium by recreating it: */
|
---|
258 | if (m_media.contains(UIMedium::nullID()))
|
---|
259 | m_media[UIMedium::nullID()] = UIMedium();
|
---|
260 | }
|
---|
261 |
|
---|
262 | void UIMediumEnumerator::sltHandleMachineDataChange(const QUuid &uMachineId)
|
---|
263 | {
|
---|
264 | //printf("MachineDataChange: machine-id=%s\n",
|
---|
265 | // uMachineId.toString().toUtf8().constData());
|
---|
266 | LogRel2(("GUI: UIMediumEnumerator: MachineDataChange event received, Machine ID = {%s}\n",
|
---|
267 | uMachineId.toString().toUtf8().constData()));
|
---|
268 |
|
---|
269 | /* Enumerate all the media of machine with this ID: */
|
---|
270 | QList<QUuid> result;
|
---|
271 | enumerateAllMediaOfMachineWithId(uMachineId, result);
|
---|
272 | }
|
---|
273 |
|
---|
274 | void UIMediumEnumerator::sltHandleStorageControllerChange(const QUuid &uMachineId, const QString &strControllerName)
|
---|
275 | {
|
---|
276 | //printf("StorageControllerChanged: machine-id=%s, controller-name=%s\n",
|
---|
277 | // uMachineId.toString().toUtf8().constData(), strControllerName.toUtf8().constData());
|
---|
278 | LogRel2(("GUI: UIMediumEnumerator: StorageControllerChanged event received, Medium ID = {%s}, Controller Name = {%s}\n",
|
---|
279 | uMachineId.toString().toUtf8().constData(), strControllerName.toUtf8().constData()));
|
---|
280 | }
|
---|
281 |
|
---|
282 | void UIMediumEnumerator::sltHandleStorageDeviceChange(CMediumAttachment comAttachment, bool fRemoved, bool fSilent)
|
---|
283 | {
|
---|
284 | //printf("StorageDeviceChanged: removed=%d, silent=%d\n",
|
---|
285 | // fRemoved, fSilent);
|
---|
286 | LogRel2(("GUI: UIMediumEnumerator: StorageDeviceChanged event received, Removed = {%d}, Silent = {%d}\n",
|
---|
287 | fRemoved, fSilent));
|
---|
288 |
|
---|
289 | /* Parse attachment: */
|
---|
290 | QList<QUuid> result;
|
---|
291 | parseAttachment(comAttachment, result);
|
---|
292 | }
|
---|
293 |
|
---|
294 | void UIMediumEnumerator::sltHandleMediumChange(CMediumAttachment comAttachment)
|
---|
295 | {
|
---|
296 | //printf("MediumChanged\n");
|
---|
297 | LogRel2(("GUI: UIMediumEnumerator: MediumChanged event received\n"));
|
---|
298 |
|
---|
299 | /* Parse attachment: */
|
---|
300 | QList<QUuid> result;
|
---|
301 | parseAttachment(comAttachment, result);
|
---|
302 | }
|
---|
303 |
|
---|
304 | void UIMediumEnumerator::sltHandleMediumConfigChange(CMedium comMedium)
|
---|
305 | {
|
---|
306 | //printf("MediumConfigChanged\n");
|
---|
307 | LogRel2(("GUI: UIMediumEnumerator: MediumConfigChanged event received\n"));
|
---|
308 |
|
---|
309 | /* Parse medium: */
|
---|
310 | QList<QUuid> result;
|
---|
311 | parseMedium(comMedium, result);
|
---|
312 | }
|
---|
313 |
|
---|
314 | void UIMediumEnumerator::sltHandleMediumRegistered(const QUuid &uMediumId, KDeviceType enmMediumType, bool fRegistered)
|
---|
315 | {
|
---|
316 | //printf("MediumRegistered: medium-id=%s, medium-type=%d, registered=%d\n",
|
---|
317 | // uMediumId.toString().toUtf8().constData(), enmMediumType, fRegistered);
|
---|
318 | //printf(" Medium to recache: %s\n",
|
---|
319 | // uMediumId.toString().toUtf8().constData());
|
---|
320 | LogRel2(("GUI: UIMediumEnumerator: MediumRegistered event received, Medium ID = {%s}, Medium type = {%d}, Registered = {%d}\n",
|
---|
321 | uMediumId.toString().toUtf8().constData(), enmMediumType, fRegistered));
|
---|
322 |
|
---|
323 | /* New medium registered: */
|
---|
324 | if (fRegistered)
|
---|
325 | {
|
---|
326 | /* Make sure this medium isn't already cached: */
|
---|
327 | if (!medium(uMediumId).isNull())
|
---|
328 | {
|
---|
329 | /* This medium can be known because of async event nature. Currently medium registration event comes
|
---|
330 | * very late and other even unrelated events can come before it and request for this particular medium
|
---|
331 | * enumeration, so we just ignore repetitive events but enumerate this UIMedium at least once if it
|
---|
332 | * wasn't registered before. */
|
---|
333 | if (!m_registeredMediaIds.contains(uMediumId))
|
---|
334 | {
|
---|
335 | LogRel2(("GUI: UIMediumEnumerator: Medium {%s} is cached but not registered already, so will be enumerated..\n",
|
---|
336 | uMediumId.toString().toUtf8().constData()));
|
---|
337 | createMediumEnumerationTask(m_media.value(uMediumId));
|
---|
338 |
|
---|
339 | /* Mark medium registered: */
|
---|
340 | m_registeredMediaIds << uMediumId;
|
---|
341 | }
|
---|
342 | }
|
---|
343 | else
|
---|
344 | {
|
---|
345 | /* Get VBox for temporary usage, it will cache the error info: */
|
---|
346 | CVirtualBox comVBox = gpGlobalSession->virtualBox();
|
---|
347 | /* Open existing medium, this API can be used to open known medium as well, using ID as location for that: */
|
---|
348 | CMedium comMedium = comVBox.OpenMedium(uMediumId.toString(), enmMediumType, KAccessMode_ReadWrite, false);
|
---|
349 | if (!comVBox.isOk())
|
---|
350 | LogRel(("GUI: UIMediumEnumerator: Unable to open registered medium! %s\n",
|
---|
351 | UIErrorString::simplifiedErrorInfo(comVBox).toUtf8().constData()));
|
---|
352 | else
|
---|
353 | {
|
---|
354 | /* Create new UIMedium: */
|
---|
355 | const UIMedium guiMedium(comMedium, UIMediumDefs::mediumTypeToLocal(comMedium.GetDeviceType()));
|
---|
356 | const QUuid &uUIMediumKey = guiMedium.key();
|
---|
357 |
|
---|
358 | /* Cache corresponding UIMedium: */
|
---|
359 | m_media.insert(uUIMediumKey, guiMedium);
|
---|
360 | LogRel2(("GUI: UIMediumEnumerator: Medium {%s} is now cached and will be enumerated..\n",
|
---|
361 | uUIMediumKey.toString().toUtf8().constData()));
|
---|
362 |
|
---|
363 | /* And notify listeners: */
|
---|
364 | emit sigMediumCreated(uUIMediumKey);
|
---|
365 |
|
---|
366 | /* Enumerate corresponding UIMedium: */
|
---|
367 | createMediumEnumerationTask(m_media.value(uMediumId));
|
---|
368 |
|
---|
369 | /* Mark medium registered: */
|
---|
370 | m_registeredMediaIds << uMediumId;
|
---|
371 | }
|
---|
372 | }
|
---|
373 | }
|
---|
374 | /* Old medium unregistered: */
|
---|
375 | else
|
---|
376 | {
|
---|
377 | /* Make sure this medium is still cached: */
|
---|
378 | if (medium(uMediumId).isNull())
|
---|
379 | {
|
---|
380 | /* This medium can be wiped out already because of async event nature. Currently
|
---|
381 | * medium unregistration event comes very late and other even unrealted events
|
---|
382 | * can come before it and request for this particular medium enumeration. If medium
|
---|
383 | * enumeration is performed fast enough (before medium unregistration event comes),
|
---|
384 | * medium will be wiped out already, so we just ignore it. */
|
---|
385 | LogRel2(("GUI: UIMediumEnumerator: Medium {%s} was not currently cached!\n",
|
---|
386 | uMediumId.toString().toUtf8().constData()));
|
---|
387 | }
|
---|
388 | else
|
---|
389 | {
|
---|
390 | /* Forget corresponding UIMedium: */
|
---|
391 | m_media.remove(uMediumId);
|
---|
392 | LogRel2(("GUI: UIMediumEnumerator: Medium {%s} is no more cached!\n",
|
---|
393 | uMediumId.toString().toUtf8().constData()));
|
---|
394 |
|
---|
395 | /* And notify listeners: */
|
---|
396 | emit sigMediumDeleted(uMediumId);
|
---|
397 |
|
---|
398 | /* Besides that we should enumerate all the
|
---|
399 | * 1st level children of deleted medium: */
|
---|
400 | QList<QUuid> result;
|
---|
401 | enumerateAllMediaOfMediumWithId(uMediumId, result);
|
---|
402 | }
|
---|
403 |
|
---|
404 | /* Mark medium unregistered: */
|
---|
405 | m_registeredMediaIds.remove(uMediumId);
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | void UIMediumEnumerator::sltHandleMediumEnumerationTaskComplete(UITask *pTask)
|
---|
410 | {
|
---|
411 | /* Make sure that is one of our tasks: */
|
---|
412 | if (pTask->type() != UITask::Type_MediumEnumeration)
|
---|
413 | return;
|
---|
414 | AssertReturnVoid(m_tasks.contains(pTask));
|
---|
415 |
|
---|
416 | /* Get enumerated UIMedium: */
|
---|
417 | const UIMedium guiMedium = qobject_cast<UITaskMediumEnumeration*>(pTask)->medium();
|
---|
418 | const QUuid uMediumKey = guiMedium.key();
|
---|
419 | LogRel2(("GUI: UIMediumEnumerator: Medium with key={%s} enumerated\n",
|
---|
420 | uMediumKey.toString().toUtf8().constData()));
|
---|
421 |
|
---|
422 | /* Remove task from internal set: */
|
---|
423 | m_tasks.remove(pTask);
|
---|
424 |
|
---|
425 | /* Make sure such UIMedium still exists: */
|
---|
426 | if (!m_media.contains(uMediumKey))
|
---|
427 | {
|
---|
428 | LogRel2(("GUI: UIMediumEnumerator: Medium with key={%s} already deleted by a third party\n",
|
---|
429 | uMediumKey.toString().toUtf8().constData()));
|
---|
430 | return;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /* Check if UIMedium ID was changed: */
|
---|
434 | const QUuid uMediumID = guiMedium.id();
|
---|
435 | /* UIMedium ID was changed to nullID: */
|
---|
436 | if (uMediumID == UIMedium::nullID())
|
---|
437 | {
|
---|
438 | /* Delete this UIMedium: */
|
---|
439 | m_media.remove(uMediumKey);
|
---|
440 | LogRel2(("GUI: UIMediumEnumerator: Medium with key={%s} closed and deleted (after enumeration)\n",
|
---|
441 | uMediumKey.toString().toUtf8().constData()));
|
---|
442 |
|
---|
443 | /* And notify listener about delete: */
|
---|
444 | emit sigMediumDeleted(uMediumKey);
|
---|
445 | }
|
---|
446 | /* UIMedium ID was changed to something proper: */
|
---|
447 | else if (uMediumID != uMediumKey)
|
---|
448 | {
|
---|
449 | /* We have to reinject enumerated UIMedium: */
|
---|
450 | m_media.remove(uMediumKey);
|
---|
451 | m_media[uMediumID] = guiMedium;
|
---|
452 | m_media[uMediumID].setKey(uMediumID);
|
---|
453 | LogRel2(("GUI: UIMediumEnumerator: Medium with key={%s} has it changed to {%s}\n",
|
---|
454 | uMediumKey.toString().toUtf8().constData(),
|
---|
455 | uMediumID.toString().toUtf8().constData()));
|
---|
456 |
|
---|
457 | /* And notify listener about delete/create: */
|
---|
458 | emit sigMediumDeleted(uMediumKey);
|
---|
459 | emit sigMediumCreated(uMediumID);
|
---|
460 | }
|
---|
461 | /* UIMedium ID was not changed: */
|
---|
462 | else
|
---|
463 | {
|
---|
464 | /* Just update enumerated UIMedium: */
|
---|
465 | m_media[uMediumID] = guiMedium;
|
---|
466 | LogRel2(("GUI: UIMediumEnumerator: Medium with key={%s} updated\n",
|
---|
467 | uMediumID.toString().toUtf8().constData()));
|
---|
468 |
|
---|
469 | /* And notify listener about update: */
|
---|
470 | emit sigMediumEnumerated(uMediumID);
|
---|
471 | }
|
---|
472 |
|
---|
473 | /* If there are no more tasks we know about: */
|
---|
474 | if (m_tasks.isEmpty())
|
---|
475 | {
|
---|
476 | /* Notify listener: */
|
---|
477 | LogRel(("GUI: UIMediumEnumerator: Medium-enumeration finished!\n"));
|
---|
478 | m_fMediumEnumerationInProgress = false;
|
---|
479 | emit sigMediumEnumerationFinished();
|
---|
480 | }
|
---|
481 | }
|
---|
482 |
|
---|
483 | void UIMediumEnumerator::createMediumEnumerationTask(const UIMedium &guiMedium)
|
---|
484 | {
|
---|
485 | /* Prepare medium-enumeration task: */
|
---|
486 | UITask *pTask = new UITaskMediumEnumeration(guiMedium);
|
---|
487 | /* Append to internal set: */
|
---|
488 | m_tasks << pTask;
|
---|
489 | /* Post into global thread-pool: */
|
---|
490 | uiCommon().threadPool()->enqueueTask(pTask);
|
---|
491 | }
|
---|
492 |
|
---|
493 | void UIMediumEnumerator::addNullMediumToMap(UIMediumMap &media)
|
---|
494 | {
|
---|
495 | /* Insert NULL UIMedium to the passed media map.
|
---|
496 | * Get existing one from the previous map if any. */
|
---|
497 | const UIMedium guiMedium = m_media.contains(UIMedium::nullID())
|
---|
498 | ? m_media[UIMedium::nullID()]
|
---|
499 | : UIMedium();
|
---|
500 | media.insert(UIMedium::nullID(), guiMedium);
|
---|
501 | }
|
---|
502 |
|
---|
503 | void UIMediumEnumerator::addMediaToMap(const CMediumVector &inputMedia, UIMediumMap &outputMedia)
|
---|
504 | {
|
---|
505 | /* Iterate through passed inputMedia vector: */
|
---|
506 | foreach (const CMedium &comMedium, inputMedia)
|
---|
507 | {
|
---|
508 | /* If UICommon is cleaning up, abort immediately: */
|
---|
509 | if (uiCommon().isCleaningUp())
|
---|
510 | break;
|
---|
511 |
|
---|
512 | /* Insert UIMedium to the passed media map.
|
---|
513 | * Get existing one from the previous map if any.
|
---|
514 | * Create on the basis of comMedium otherwise. */
|
---|
515 | const QUuid uMediumID = comMedium.GetId();
|
---|
516 | const UIMedium guiMedium = m_media.contains(uMediumID)
|
---|
517 | ? m_media.value(uMediumID)
|
---|
518 | : UIMedium(comMedium, UIMediumDefs::mediumTypeToLocal(comMedium.GetDeviceType()));
|
---|
519 | outputMedia.insert(guiMedium.id(), guiMedium);
|
---|
520 |
|
---|
521 | /* Insert comMedium children into map as well: */
|
---|
522 | addMediaToMap(comMedium.GetChildren(), outputMedia);
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | void UIMediumEnumerator::parseAttachment(CMediumAttachment comAttachment, QList<QUuid> &result)
|
---|
527 | {
|
---|
528 | /* Make sure attachment is valid: */
|
---|
529 | if (comAttachment.isNull())
|
---|
530 | {
|
---|
531 | LogRel2(("GUI: UIMediumEnumerator: Attachment is NULL!\n"));
|
---|
532 | /// @todo is this possible case?
|
---|
533 | AssertFailed();
|
---|
534 | }
|
---|
535 | else
|
---|
536 | {
|
---|
537 | /* Acquire attachment medium: */
|
---|
538 | CMedium comMedium = comAttachment.GetMedium();
|
---|
539 | if (!comAttachment.isOk())
|
---|
540 | LogRel(("GUI: UIMediumEnumerator: Unable to acquire attachment medium! %s\n",
|
---|
541 | UIErrorString::simplifiedErrorInfo(comAttachment).toUtf8().constData()));
|
---|
542 | else
|
---|
543 | {
|
---|
544 | /* Parse medium: */
|
---|
545 | parseMedium(comMedium, result);
|
---|
546 |
|
---|
547 | // WORKAROUND:
|
---|
548 | // In current architecture there is no way to determine medium previously mounted
|
---|
549 | // to this attachment, so we will have to enumerate all other cached media which
|
---|
550 | // belongs to the same VM, since they may no longer belong to it.
|
---|
551 |
|
---|
552 | /* Acquire parent VM: */
|
---|
553 | CMachine comMachine = comAttachment.GetMachine();
|
---|
554 | if (!comAttachment.isOk())
|
---|
555 | LogRel(("GUI: UIMediumEnumerator: Unable to acquire attachment parent machine! %s\n",
|
---|
556 | UIErrorString::simplifiedErrorInfo(comAttachment).toUtf8().constData()));
|
---|
557 | else
|
---|
558 | {
|
---|
559 | /* Acquire machine ID: */
|
---|
560 | const QUuid uMachineId = comMachine.GetId();
|
---|
561 | if (!comMachine.isOk())
|
---|
562 | LogRel(("GUI: UIMediumEnumerator: Unable to acquire machine ID! %s\n",
|
---|
563 | UIErrorString::simplifiedErrorInfo(comMachine).toUtf8().constData()));
|
---|
564 | else
|
---|
565 | {
|
---|
566 | /* Enumerate all the media of machine with this ID: */
|
---|
567 | enumerateAllMediaOfMachineWithId(uMachineId, result);
|
---|
568 | }
|
---|
569 | }
|
---|
570 | }
|
---|
571 | }
|
---|
572 | }
|
---|
573 |
|
---|
574 | void UIMediumEnumerator::parseMedium(CMedium comMedium, QList<QUuid> &result)
|
---|
575 | {
|
---|
576 | /* Make sure medium is valid: */
|
---|
577 | if (comMedium.isNull())
|
---|
578 | {
|
---|
579 | /* This medium is NULL by some reason, the obvious case when this
|
---|
580 | * can happen is when optical/floppy device is created empty. */
|
---|
581 | LogRel2(("GUI: UIMediumEnumerator: Medium is NULL!\n"));
|
---|
582 | }
|
---|
583 | else
|
---|
584 | {
|
---|
585 | /* Acquire medium ID: */
|
---|
586 | const QUuid uMediumId = comMedium.GetId();
|
---|
587 | if (!comMedium.isOk())
|
---|
588 | LogRel(("GUI: UIMediumEnumerator: Unable to acquire medium ID! %s\n",
|
---|
589 | UIErrorString::simplifiedErrorInfo(comMedium).toUtf8().constData()));
|
---|
590 | else
|
---|
591 | {
|
---|
592 | //printf(" Medium to recache: %s\n", uMediumId.toString().toUtf8().constData());
|
---|
593 |
|
---|
594 | /* Make sure this medium is already cached: */
|
---|
595 | if (medium(uMediumId).isNull())
|
---|
596 | {
|
---|
597 | /* This medium isn't cached by some reason, which can be different.
|
---|
598 | * One of such reasons is when config-changed event comes earlier than
|
---|
599 | * corresponding registration event. For now we are ignoring that at all. */
|
---|
600 | LogRel2(("GUI: UIMediumEnumerator: Medium {%s} isn't cached yet!\n",
|
---|
601 | uMediumId.toString().toUtf8().constData()));
|
---|
602 | }
|
---|
603 | else
|
---|
604 | {
|
---|
605 | /* Enumerate corresponding UIMedium: */
|
---|
606 | LogRel2(("GUI: UIMediumEnumerator: Medium {%s} will be enumerated..\n",
|
---|
607 | uMediumId.toString().toUtf8().constData()));
|
---|
608 | createMediumEnumerationTask(m_media.value(uMediumId));
|
---|
609 | result << uMediumId;
|
---|
610 | }
|
---|
611 | }
|
---|
612 | }
|
---|
613 | }
|
---|
614 |
|
---|
615 | void UIMediumEnumerator::enumerateAllMediaOfMachineWithId(const QUuid &uMachineId, QList<QUuid> &result)
|
---|
616 | {
|
---|
617 | /* For each the cached UIMedium we have: */
|
---|
618 | foreach (const QUuid &uMediumId, mediumIDs())
|
---|
619 | {
|
---|
620 | /* Check if medium isn't NULL, used by our
|
---|
621 | * machine and wasn't already enumerated. */
|
---|
622 | const UIMedium guiMedium = medium(uMediumId);
|
---|
623 | if ( !guiMedium.isNull()
|
---|
624 | && guiMedium.machineIds().contains(uMachineId)
|
---|
625 | && !result.contains(uMediumId))
|
---|
626 | {
|
---|
627 | /* Enumerate corresponding UIMedium: */
|
---|
628 | //printf(" Medium to recache: %s\n",
|
---|
629 | // uMediumId.toString().toUtf8().constData());
|
---|
630 | LogRel2(("GUI: UIMediumEnumerator: Medium {%s} of machine {%s} will be enumerated..\n",
|
---|
631 | uMediumId.toString().toUtf8().constData(),
|
---|
632 | uMachineId.toString().toUtf8().constData()));
|
---|
633 | createMediumEnumerationTask(guiMedium);
|
---|
634 | result << uMediumId;
|
---|
635 | }
|
---|
636 | }
|
---|
637 | }
|
---|
638 |
|
---|
639 | void UIMediumEnumerator::enumerateAllMediaOfMediumWithId(const QUuid &uParentMediumId, QList<QUuid> &result)
|
---|
640 | {
|
---|
641 | /* For each the cached UIMedium we have: */
|
---|
642 | foreach (const QUuid &uMediumId, mediumIDs())
|
---|
643 | {
|
---|
644 | /* Check if medium isn't NULL, and is
|
---|
645 | * a child of specified parent medium. */
|
---|
646 | const UIMedium guiMedium = medium(uMediumId);
|
---|
647 | if ( !guiMedium.isNull()
|
---|
648 | && guiMedium.parentID() == uParentMediumId)
|
---|
649 | {
|
---|
650 | /* Enumerate corresponding UIMedium: */
|
---|
651 | //printf(" Medium to recache: %s\n",
|
---|
652 | // uMediumId.toString().toUtf8().constData());
|
---|
653 | LogRel2(("GUI: UIMediumEnumerator: Medium {%s} a child of medium {%s} will be enumerated..\n",
|
---|
654 | uMediumId.toString().toUtf8().constData(),
|
---|
655 | uParentMediumId.toString().toUtf8().constData()));
|
---|
656 | createMediumEnumerationTask(guiMedium);
|
---|
657 | result << uMediumId;
|
---|
658 | }
|
---|
659 | }
|
---|
660 | }
|
---|
661 |
|
---|
662 |
|
---|
663 | #include "UIMediumEnumerator.moc"
|
---|