1 | /* $Id: UINotificationModel.cpp 98928 2023-03-13 10:37:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UINotificationModel class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2021-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 "UIExtraDataManager.h"
|
---|
34 | #include "UINotificationModel.h"
|
---|
35 | #include "UINotificationObject.h"
|
---|
36 |
|
---|
37 | /* Other VBox includes: */
|
---|
38 | #include "iprt/assert.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | UINotificationModel::UINotificationModel(QObject *pParent)
|
---|
42 | : QObject(pParent)
|
---|
43 | {
|
---|
44 | prepare();
|
---|
45 | }
|
---|
46 |
|
---|
47 | UINotificationModel::~UINotificationModel()
|
---|
48 | {
|
---|
49 | cleanup();
|
---|
50 | }
|
---|
51 |
|
---|
52 | QUuid UINotificationModel::appendObject(UINotificationObject *pObject)
|
---|
53 | {
|
---|
54 | /* [Re]generate ID until unique: */
|
---|
55 | QUuid uId = QUuid::createUuid();
|
---|
56 | while (m_ids.contains(uId))
|
---|
57 | uId = QUuid::createUuid();
|
---|
58 | /* Append ID and object: */
|
---|
59 | m_ids << uId;
|
---|
60 | m_objects[uId] = pObject;
|
---|
61 | /* Connect object close signal: */
|
---|
62 | connect(pObject, &UINotificationObject::sigAboutToClose,
|
---|
63 | this, &UINotificationModel::sltHandleAboutToClose);
|
---|
64 | /* Notify listeners: */
|
---|
65 | emit sigItemAdded(uId);
|
---|
66 | /* Handle object: */
|
---|
67 | pObject->handle();
|
---|
68 | /* Return ID: */
|
---|
69 | return uId;
|
---|
70 | }
|
---|
71 |
|
---|
72 | void UINotificationModel::revokeObject(const QUuid &uId)
|
---|
73 | {
|
---|
74 | /* Remove id first of all: */
|
---|
75 | m_ids.removeAll(uId);
|
---|
76 | /* Notify listeners before object is deleted: */
|
---|
77 | emit sigItemRemoved(uId);
|
---|
78 | /* Delete object itself finally: */
|
---|
79 | delete m_objects.take(uId);
|
---|
80 | }
|
---|
81 |
|
---|
82 | bool UINotificationModel::hasObject(const QUuid &uId) const
|
---|
83 | {
|
---|
84 | return m_objects.contains(uId);
|
---|
85 | }
|
---|
86 |
|
---|
87 | void UINotificationModel::revokeFinishedObjects()
|
---|
88 | {
|
---|
89 | /* Check whether there are done objects: */
|
---|
90 | foreach (const QUuid &uId, m_ids)
|
---|
91 | {
|
---|
92 | UINotificationObject *pObject = m_objects.value(uId);
|
---|
93 | AssertPtrReturnVoid(pObject);
|
---|
94 | if (pObject->isDone())
|
---|
95 | revokeObject(uId);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | QList<QUuid> UINotificationModel::ids() const
|
---|
100 | {
|
---|
101 | return m_ids;
|
---|
102 | }
|
---|
103 |
|
---|
104 | UINotificationObject *UINotificationModel::objectById(const QUuid &uId)
|
---|
105 | {
|
---|
106 | return m_objects.value(uId);
|
---|
107 | }
|
---|
108 |
|
---|
109 | void UINotificationModel::sltHandleAboutToClose(bool fDismiss)
|
---|
110 | {
|
---|
111 | /* Determine sender: */
|
---|
112 | UINotificationObject *pSender = qobject_cast<UINotificationObject*>(sender());
|
---|
113 | AssertPtrReturnVoid(pSender);
|
---|
114 |
|
---|
115 | /* Dismiss message if requested: */
|
---|
116 | if (fDismiss && !pSender->internalName().isEmpty())
|
---|
117 | {
|
---|
118 | QStringList suppressedMessages = gEDataManager->suppressedMessages();
|
---|
119 | if (!suppressedMessages.contains(pSender->internalName()))
|
---|
120 | {
|
---|
121 | suppressedMessages.push_back(pSender->internalName());
|
---|
122 | gEDataManager->setSuppressedMessages(suppressedMessages);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* Revoke it from internal storage: */
|
---|
127 | const QUuid uId = m_objects.key(pSender);
|
---|
128 | AssertReturnVoid(!uId.isNull());
|
---|
129 | revokeObject(uId);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void UINotificationModel::sltDetachCOM()
|
---|
133 | {
|
---|
134 | cleanup();
|
---|
135 | }
|
---|
136 |
|
---|
137 | void UINotificationModel::prepare()
|
---|
138 | {
|
---|
139 | connect(&uiCommon(), &UICommon::sigAskToDetachCOM,
|
---|
140 | this, &UINotificationModel::sltDetachCOM);
|
---|
141 | }
|
---|
142 |
|
---|
143 | void UINotificationModel::cleanup()
|
---|
144 | {
|
---|
145 | /* Wipe out all the objects: */
|
---|
146 | foreach (const QUuid &uId, m_ids)
|
---|
147 | revokeObject(uId);
|
---|
148 | m_objects.clear();
|
---|
149 | m_ids.clear();
|
---|
150 | }
|
---|