VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMedium.h@ 102493

Last change on this file since 102493 was 98103, checked in by vboxsync, 20 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.8 KB
Line 
1/* $Id: UIMedium.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIMedium class declaration.
4 */
5
6/*
7 * Copyright (C) 2009-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#ifndef FEQT_INCLUDED_SRC_medium_UIMedium_h
29#define FEQT_INCLUDED_SRC_medium_UIMedium_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34/* Qt includes: */
35#include <QMap>
36#include <QPixmap>
37
38/* GUI includes: */
39#include "UILibraryDefs.h"
40#include "UIMediumDefs.h"
41
42/* COM includes: */
43#include "COMEnums.h"
44#include "CMedium.h"
45
46/* Other VBox includes: */
47#include "iprt/cpp/utils.h"
48
49/** Storage medium cache used to
50 * override some UIMedium attributes in the
51 * user-friendly "don't show diffs" mode. */
52struct NoDiffsCache
53{
54 /** Constructor. */
55 NoDiffsCache() : isSet(false), state(KMediumState_NotCreated) {}
56
57 /** Operator= reimplementation. */
58 NoDiffsCache& operator=(const NoDiffsCache &other)
59 {
60 isSet = other.isSet;
61 state = other.state;
62 result = other.result;
63 toolTip = other.toolTip;
64 return *this;
65 }
66
67 /** Holds whether the cache is set. */
68 bool isSet : 1;
69
70 /** Holds overriden medium state. */
71 KMediumState state;
72 /** Holds overriden medium acquiring result. */
73 COMResult result;
74 /** Holds overriden medium tool-tip. */
75 QString toolTip;
76};
77
78/** Storage medium descriptor wrapping CMedium wrapper for IMedium interface.
79 *
80 * Maintains the results of the last CMedium state (accessibility) check and precomposes
81 * string parameters such as name, location and size which can be used for various GUI tasks.
82 *
83 * Many getter methods take the boolean @a fNoDiffs argument.
84 * Unless explicitly stated otherwise, this argument, when set to @c true,
85 * will cause the corresponding property of this object's root medium to be returned instead
86 * of its own one. This is useful when hard drive medium is reflected in the user-friendly
87 * "don't show diffs" mode. For non-hard drive media, the value of this argument is irrelevant
88 * because the root object for such medium is the medium itself.
89 *
90 * Note that this class "abuses" the KMediumState_NotCreated state value to indicate that the
91 * accessibility check of the given medium (see #blockAndQueryState()) has not been done yet
92 * and therefore some parameters such as #size() are meaningless because they can be read only
93 * from the accessible medium. The real KMediumState_NotCreated state is not necessary because
94 * this class is only used with created (existing) media. */
95class SHARED_LIBRARY_STUFF UIMedium
96{
97public:
98
99 /** Default constructor.
100 * Creates NULL UIMedium which is not associated with any CMedium. */
101 UIMedium();
102
103 /** Lazy wrapping constructor.
104 * Creates the UIMedium associated with the given @a medium of the given @a type. */
105 UIMedium(const CMedium &medium, UIMediumDeviceType type);
106
107 /** Wrapping constructor with known medium state.
108 * Similarly to the previous one it creates the UIMedium associated with the
109 * given @a medium of the given @a type but sets the UIMedium @a state to passed one.
110 * Suitable when the medium state is known such as right after the medium creation. */
111 UIMedium(const CMedium &medium, UIMediumDeviceType type, KMediumState state);
112
113 /** Copy constructor.
114 * Creates the UIMedium on the basis of the passed @a other one. */
115 UIMedium(const UIMedium &other);
116
117 /** Operator= reimplementation. */
118 UIMedium& operator=(const UIMedium &other);
119
120 /** Queries the actual medium state.
121 * @note This method blocks for the duration of the state check.
122 * Since this check may take quite a while,
123 * the calling thread must not be the UI thread. */
124 void blockAndQueryState();
125
126 /** Refreshes the precomposed user-readable strings.
127 * @note Note that some string such as #size() are meaningless if the medium state is
128 * KMediumState_NotCreated (i.e. the medium has not yet been checked for accessibility). */
129 void refresh();
130
131 /** Returns the type of UIMedium object. */
132 UIMediumDeviceType type() const { return m_type; }
133
134 /** Returns the CMedium wrapped by this UIMedium object. */
135 const CMedium& medium() const { return m_medium; }
136
137 /** Returns @c true if CMedium wrapped by this UIMedium object has ID == #nullID().
138 * @note Also make sure wrapped CMedium is NULL object if his ID == #nullID(). */
139 bool isNull() const
140 {
141 AssertReturn(m_uId != nullID() || m_medium.isNull(), true);
142 return m_uId == nullID();
143 }
144
145 /** Returns the medium state.
146 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
147 * @note In "don't show diffs" mode, this method returns the worst state
148 * (in terms of inaccessibility) detected on the given hard drive chain. */
149 KMediumState state(bool fNoDiffs = false) const
150 {
151 unconst(this)->checkNoDiffs(fNoDiffs);
152 return fNoDiffs ? m_noDiffs.state : m_state;
153 }
154
155 /** Returns the result of the last blockAndQueryState() call.
156 * Indicates an error and contain a proper error info if the last state check fails.
157 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
158 * @note In "don't show diffs" mode, this method returns the worst result
159 * (in terms of inaccessibility) detected on the given hard drive chain. */
160 const COMResult& result(bool fNoDiffs = false) const
161 {
162 unconst(this)->checkNoDiffs(fNoDiffs);
163 return fNoDiffs ? m_noDiffs.result : m_result;
164 }
165
166 /** Returns the error result of the last blockAndQueryState() call. */
167 QString lastAccessError() const { return m_strLastAccessError; }
168
169 /** Returns the medium ID. */
170 QUuid id() const { return m_uId; }
171
172 /** Returns the medium root ID. */
173 QUuid rootID() const { return m_uRootId; }
174 /** Returns the medium parent ID. */
175 QUuid parentID() const { return m_uParentId; }
176
177 /** Updates medium parent. */
178 void updateParentID();
179
180 /** Returns the medium cache key. */
181 QUuid key() const { return m_uKey; }
182 /** Defines the medium cache @a uKey. */
183 void setKey(const QUuid &uKey) { m_uKey = uKey; }
184
185 /** Returns the medium name.
186 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
187 * @note In "don't show diffs" mode, this method returns the name of root in the given hard drive chain. */
188 QString name(bool fNoDiffs = false) const { return fNoDiffs ? root().m_strName : m_strName; }
189 /** Returns the medium location.
190 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
191 * @note In "don't show diffs" mode, this method returns the location of root in the given hard drive chain. */
192 QString location(bool fNoDiffs = false) const { return fNoDiffs ? root().m_strLocation : m_strLocation; }
193 /** Returns the medium description.
194 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
195 * @note In "don't show diffs" mode, this method returns the description of root in the given hard drive chain. */
196 QString description(bool fNoDiffs = false) const { return fNoDiffs ? root().m_strDescription : m_strDescription; }
197
198 /** Returns the medium size in bytes.
199 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
200 * @note In "don't show diffs" mode, this method returns the size of root in the given hard drive chain. */
201 qulonglong sizeInBytes(bool fNoDiffs = false) const { return fNoDiffs ? root().m_uSize : m_uSize; }
202 /** Returns the logical medium size in bytes.
203 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
204 * @note In "don't show diffs" mode, this method returns the size of root in the given hard drive chain. */
205 qulonglong logicalSizeInBytes(bool fNoDiffs = false) const { return fNoDiffs ? root().m_uLogicalSize : m_uLogicalSize; }
206 /** Returns the medium size.
207 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
208 * @note In "don't show diffs" mode, this method returns the size of root in the given hard drive chain. */
209 QString size(bool fNoDiffs = false) const { return fNoDiffs ? root().m_strSize : m_strSize; }
210 /** Returns the logical medium size.
211 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
212 * @note In "don't show diffs" mode, this method returns the logical size of root in the given hard drive chain. */
213 QString logicalSize(bool fNoDiffs = false) const { return fNoDiffs ? root().m_strLogicalSize : m_strLogicalSize; }
214
215 /** Returns the medium disk type.
216 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
217 * @note In "don't show diffs" mode, this method returns the disk type of root in the given hard drive chain. */
218 KMediumType mediumType(bool fNoDiffs = false) const { return fNoDiffs ? root().m_enmMediumType : m_enmMediumType; }
219 /** Returns the medium disk variant.
220 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
221 * @note In "don't show diffs" mode, this method returns the disk variant of root in the given hard drive chain. */
222 KMediumVariant mediumVariant(bool fNoDiffs = false) const { return fNoDiffs ? root().m_enmMediumVariant : m_enmMediumVariant; }
223
224 /** Returns the hard drive medium disk type.
225 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
226 * @note In "don't show diffs" mode, this method returns the disk type of root in the given hard drive chain. */
227 QString hardDiskType(bool fNoDiffs = false) const { return fNoDiffs ? root().m_strHardDiskType : m_strHardDiskType; }
228 /** Returns the hard drive medium disk format.
229 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
230 * @note In "don't show diffs" mode, this method returns the disk format of root in the given hard drive chain. */
231 QString hardDiskFormat(bool fNoDiffs = false) const { return fNoDiffs ? root().m_strHardDiskFormat : m_strHardDiskFormat; }
232
233 /** Returns whether the hard drive medium disk has childred.
234 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
235 * @note In "don't show diffs" mode, this method returns the disk format of root in the given hard drive chain. */
236 bool hasChildren(bool fNoDiffs = false) const { return fNoDiffs ? root().m_fHasChildren : m_fHasChildren; }
237
238 /** Returns the hard drive medium storage details. */
239 QString storageDetails() const { return m_strStorageDetails; }
240 /** Returns the hard drive medium encryption password ID. */
241 QString encryptionPasswordID() const { return m_strEncryptionPasswordID; }
242
243 /** Returns the medium usage data.
244 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
245 * @note In "don't show diffs" mode, this method returns the usage data of root in the given hard drive chain. */
246 QString usage(bool fNoDiffs = false) const { return fNoDiffs ? root().m_strUsage : m_strUsage; }
247
248 /** Returns the short version of medium tool-tip. */
249 QString tip() const { return m_strToolTip; }
250
251 /** Returns the full version of medium tool-tip.
252 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
253 * @param fCheckRO @c true to perform the #readOnly() check and add a notice accordingly.
254 * @param fNullAllowed @c true to allow NULL medium description to be mentioned in the tool-tip.
255 * @note In "don't show diffs" mode (where the attributes of the base hard drive are shown instead
256 * of the attributes of the differencing hard drive), extra information will be added to the
257 * tooltip to give the user a hint that the medium is actually a differencing hard drive. */
258 QString toolTip(bool fNoDiffs = false, bool fCheckRO = false, bool fNullAllowed = false) const;
259
260 /** Shortcut to <tt>#toolTip(fNoDiffs, true, fNullAllowed)</tt>. */
261 QString toolTipCheckRO(bool fNoDiffs = false, bool fNullAllowed = false) const { return toolTip(fNoDiffs, true, fNullAllowed); }
262
263 /** Returns an icon corresponding to the medium state.
264 * Distinguishes between the Inaccessible state and the situation when querying the state itself failed.
265 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
266 * @param fCheckRO @c true to perform the #readOnly() check and change the icon accordingly.
267 * @note In "don't show diffs" mode (where the attributes of the base hard drive are shown instead
268 * of the attributes of the differencing hard drive), the most worst medium state on the given
269 * hard drive chain will be used to select the medium icon. */
270 QPixmap icon(bool fNoDiffs = false, bool fCheckRO = false) const;
271
272 /** Shortcut to <tt>#icon(fNoDiffs, true)</tt>. */
273 QPixmap iconCheckRO(bool fNoDiffs = false) const { return icon(fNoDiffs, true); }
274
275 /** Returns the details of this medium as a single-line string.
276 * @param fNoDiffs @c true to enable user-friendly "don't show diffs" mode.
277 * @param fPredictDiff @c true to mark the hard drive as differencing if attaching
278 * it would create a differencing hard drive.
279 * @param fUseHTML @c true to allow for emphasizing using bold and italics.
280 * @note For hard drives, the details include the location, type and the logical size of the hard drive.
281 * Note that if @a fNoDiffs is @c true, these properties are queried on the root hard drive of the
282 * given hard drive because the primary purpose of the returned string is to be human readable
283 * (so that seeing a complex diff hard drive name is usually not desirable).
284 * @note For other medium types, the location and the actual size are returned.
285 * Arguments @a fPredictDiff and @a fNoDiffs are ignored in this case.
286 * @note Use #detailsHTML() instead of passing @c true for @a fUseHTML.
287 * @note The medium object may become uninitialized by a third party while this method is reading its properties.
288 * In this case, the method will return an empty string. */
289 QString details(bool fNoDiffs = false, bool fPredictDiff = false, bool fUseHTML = false) const;
290
291 /** Shortcut to <tt>#details(fNoDiffs, fPredictDiff, true)</tt>. */
292 QString detailsHTML(bool fNoDiffs = false, bool fPredictDiff = false) const { return details(fNoDiffs, fPredictDiff, true); }
293
294 /** Returns the medium cache for "don't show diffs" mode. */
295 const NoDiffsCache& cache() const { return m_noDiffs; }
296
297 /** Returns whether this medium is hidden.
298 * @note The medium is considered 'hidden' if it has corresponding
299 * medium property or is connected to 'hidden' VMs only. */
300 bool isHidden() const { return m_fHidden || m_fUsedByHiddenMachinesOnly; }
301
302 /** Returns whether this medium is read-only
303 * (either because it is Immutable or because it has child hard drives).
304 * @note Read-only medium can only be attached indirectly. */
305 bool isReadOnly() const { return m_fReadOnly; }
306
307 /** Returns whether this medium is attached to any VM in any snapshot. */
308 bool isUsedInSnapshots() const { return m_fUsedInSnapshots; }
309
310 /** Returns whether this medium corresponds to real host drive. */
311 bool isHostDrive() const { return m_fHostDrive; }
312
313 /** Returns whether this medium is encrypted. */
314 bool isEncrypted() const { return m_fEncrypted; }
315
316 /** Returns whether this medium is attached to any VM (in the current state or in a snapshot) in which case
317 * #usage() will contain a string with comma-separated VM names (with snapshot names, if any, in parenthesis). */
318 bool isUsed() const { return !m_strUsage.isNull(); }
319
320 /** Returns whether this medium is attached to the given machine in the current state. */
321 bool isAttachedInCurStateTo(const QUuid &uMachineId) const { return m_curStateMachineIds.indexOf(uMachineId) >= 0; }
322
323 /** Returns a vector of IDs of all machines this medium is attached to. */
324 const QList<QUuid>& machineIds() const { return m_machineIds; }
325 /** Returns a vector of IDs of all machines this medium is attached to
326 * in their current state (i.e. excluding snapshots). */
327 const QList<QUuid>& curStateMachineIds() const { return m_curStateMachineIds; }
328
329 /** Returns NULL medium ID. */
330 static QUuid nullID();
331
332 /** Returns passed @a uID if it's valid or #nullID() overwise. */
333 static QUuid normalizedID(const QUuid &uID);
334
335 /** Determines if passed @a medium is attached to hidden machines only. */
336 static bool isMediumAttachedToHiddenMachinesOnly(const UIMedium &medium);
337
338private:
339
340 /** Returns medium root. */
341 UIMedium root() const;
342 /** Returns medium parent. */
343 UIMedium parent() const;
344
345 /** Checks if m_noDiffs is filled in and does it if not.
346 * @param fNoDiffs @if false, this method immediately returns. */
347 void checkNoDiffs(bool fNoDiffs);
348
349 /** Returns string representation for passed @a comMedium type. */
350 static QString mediumTypeToString(const CMedium &comMedium);
351
352 /** Holds the type of UIMedium object. */
353 UIMediumDeviceType m_type;
354
355 /** Holds the CMedium wrapped by this UIMedium object. */
356 CMedium m_medium;
357
358 /** Holds the medium state. */
359 KMediumState m_state;
360 /** Holds the result of the last blockAndQueryState() call. */
361 COMResult m_result;
362 /** Holds the error result of the last blockAndQueryState() call. */
363 QString m_strLastAccessError;
364
365 /** Holds the medium ID. */
366 QUuid m_uId;
367 /** Holds the medium root ID. */
368 QUuid m_uRootId;
369 /** Holds the medium parent ID. */
370 QUuid m_uParentId;
371
372 /** Holds the medium cache key. */
373 QUuid m_uKey;
374
375 /** Holds the medium name. */
376 QString m_strName;
377 /** Holds the medium location. */
378 QString m_strLocation;
379 /** Holds the medium description. */
380 QString m_strDescription;
381
382 /** Holds the medium size in bytes. */
383 qulonglong m_uSize;
384 /** Holds the logical medium size in bytes. */
385 qulonglong m_uLogicalSize;
386 /** Holds the medium size. */
387 QString m_strSize;
388 /** Holds the logical medium size. */
389 QString m_strLogicalSize;
390
391 /** Holds the medium disk type. */
392 KMediumType m_enmMediumType;
393 /** Holds the medium disk variant. */
394 KMediumVariant m_enmMediumVariant;
395
396 /** Holds the hard drive medium disk type. */
397 QString m_strHardDiskType;
398 /** Holds the hard drive medium disk format. */
399 QString m_strHardDiskFormat;
400 /** Holds whether the hard drive medium disk has children. */
401 bool m_fHasChildren;
402 /** Holds the hard drive medium storage details. */
403 QString m_strStorageDetails;
404 /** Holds the hard drive medium encryption password ID. */
405 QString m_strEncryptionPasswordID;
406
407 /** Holds the medium usage. */
408 QString m_strUsage;
409 /** Holds the medium tool-tip. */
410 QString m_strToolTip;
411 /** Holds the vector of IDs of all machines this medium is attached to. */
412 QList<QUuid> m_machineIds;
413 /** Hodls the vector of IDs of all machines this medium is attached to
414 * in their current state (i.e. excluding snapshots). */
415 QList<QUuid> m_curStateMachineIds;
416
417 /** Holds the medium cache for "don't show diffs" mode. */
418 NoDiffsCache m_noDiffs;
419
420 /** Holds whether this medium is 'hidden' by the corresponding medium property. */
421 bool m_fHidden : 1;
422 /** Holds whether this medium is 'hidden' because it's used by 'hidden' VMs only. */
423 bool m_fUsedByHiddenMachinesOnly : 1;
424 /** Holds whether this medium is read-only. */
425 bool m_fReadOnly : 1;
426 /** Holds whether this medium is attached to any VM in any snapshot. */
427 bool m_fUsedInSnapshots : 1;
428 /** Holds whether this medium corresponds to real host drive. */
429 bool m_fHostDrive : 1;
430 /** Holds whether this medium is encrypted. */
431 bool m_fEncrypted : 1;
432
433 /** Holds the NULL medium ID. */
434 static QUuid m_uNullID;
435 /** Holds the medium tool-tip table template. */
436 static QString m_sstrTable;
437 /** Holds the medium tool-tip table row template. */
438 static QString m_sstrRow;
439};
440Q_DECLARE_METATYPE(UIMedium);
441
442typedef QMap<QUuid, UIMedium> UIMediumMap;
443
444#endif /* !FEQT_INCLUDED_SRC_medium_UIMedium_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette