1 | /* $Id: UIProgressObject.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIProgressObject 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 | /* GUI includes: */
|
---|
29 | #include "UIErrorString.h"
|
---|
30 | #include "UIProgressEventHandler.h"
|
---|
31 | #include "UIProgressObject.h"
|
---|
32 |
|
---|
33 | /* COM includes: */
|
---|
34 | #include "CProgress.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | UIProgressObject::UIProgressObject(CProgress &comProgress, QObject *pParent /* = 0 */)
|
---|
38 | : QObject(pParent)
|
---|
39 | , m_comProgress(comProgress)
|
---|
40 | , m_fCancelable(false)
|
---|
41 | , m_pEventHandler(0)
|
---|
42 | {
|
---|
43 | prepare();
|
---|
44 | }
|
---|
45 |
|
---|
46 | UIProgressObject::~UIProgressObject()
|
---|
47 | {
|
---|
48 | cleanup();
|
---|
49 | }
|
---|
50 |
|
---|
51 | void UIProgressObject::exec()
|
---|
52 | {
|
---|
53 | /* Make sure progress hasn't aborted/finished already: */
|
---|
54 | if (!m_comProgress.isOk() || m_comProgress.GetCompleted())
|
---|
55 | return;
|
---|
56 |
|
---|
57 | /* We are creating a locally-scoped event-loop object,
|
---|
58 | * but holding a pointer to it for a control needs: */
|
---|
59 | QEventLoop eventLoop;
|
---|
60 | m_pEventLoopExec = &eventLoop;
|
---|
61 |
|
---|
62 | /* Guard ourself for the case
|
---|
63 | * we self-destroyed in our event-loop: */
|
---|
64 | QPointer<UIProgressObject> guard = this;
|
---|
65 |
|
---|
66 | /* Start the blocking event-loop: */
|
---|
67 | eventLoop.exec();
|
---|
68 |
|
---|
69 | /* Event-loop object unblocked,
|
---|
70 | * Are we still valid? */
|
---|
71 | if (guard.isNull())
|
---|
72 | return;
|
---|
73 |
|
---|
74 | /* Cleanup the pointer finally: */
|
---|
75 | m_pEventLoopExec = 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | void UIProgressObject::cancel()
|
---|
79 | {
|
---|
80 | /* Make sure progress hasn't aborted/finished already: */
|
---|
81 | if (!m_comProgress.isOk() || m_comProgress.GetCompleted())
|
---|
82 | return;
|
---|
83 |
|
---|
84 | /* Cancel progress first of all: */
|
---|
85 | m_comProgress.Cancel();
|
---|
86 |
|
---|
87 | /* We are creating a locally-scoped event-loop object,
|
---|
88 | * but holding a pointer to it for a control needs: */
|
---|
89 | QEventLoop eventLoop;
|
---|
90 | m_pEventLoopCancel = &eventLoop;
|
---|
91 |
|
---|
92 | /* Guard ourself for the case
|
---|
93 | * we self-destroyed in our event-loop: */
|
---|
94 | QPointer<UIProgressObject> guard = this;
|
---|
95 |
|
---|
96 | /* Start the blocking event-loop: */
|
---|
97 | eventLoop.exec();
|
---|
98 |
|
---|
99 | /* Event-loop object unblocked,
|
---|
100 | * Are we still valid? */
|
---|
101 | if (guard.isNull())
|
---|
102 | return;
|
---|
103 |
|
---|
104 | /* Cleanup the pointer finally: */
|
---|
105 | m_pEventLoopCancel = 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void UIProgressObject::sltHandleProgressPercentageChange(const QUuid &, const int iPercent)
|
---|
109 | {
|
---|
110 | /* Update cancelable value: */
|
---|
111 | m_fCancelable = m_comProgress.GetCancelable();
|
---|
112 |
|
---|
113 | /* Notify listeners: */
|
---|
114 | emit sigProgressChange(m_comProgress.GetOperationCount(),
|
---|
115 | m_comProgress.GetOperationDescription(),
|
---|
116 | m_comProgress.GetOperation(),
|
---|
117 | iPercent);
|
---|
118 | }
|
---|
119 |
|
---|
120 | void UIProgressObject::sltHandleProgressTaskComplete(const QUuid &)
|
---|
121 | {
|
---|
122 | /* Notify listeners about the operation progress error: */
|
---|
123 | if (!m_comProgress.isOk() || m_comProgress.GetResultCode() != 0)
|
---|
124 | emit sigProgressError(UIErrorString::formatErrorInfo(m_comProgress));
|
---|
125 |
|
---|
126 | /* Exit from the exec event-loop if there is any: */
|
---|
127 | if (m_pEventLoopExec)
|
---|
128 | m_pEventLoopExec->exit();
|
---|
129 | /* Exit from the cancel event-loop if there is any: */
|
---|
130 | if (m_pEventLoopCancel)
|
---|
131 | m_pEventLoopCancel->exit();
|
---|
132 |
|
---|
133 | emit sigProgressComplete();
|
---|
134 | }
|
---|
135 |
|
---|
136 | void UIProgressObject::prepare()
|
---|
137 | {
|
---|
138 | /* Init cancelable value: */
|
---|
139 | m_fCancelable = m_comProgress.GetCancelable();
|
---|
140 |
|
---|
141 | /* Create CProgress event handler: */
|
---|
142 | m_pEventHandler = new UIProgressEventHandler(this, m_comProgress);
|
---|
143 | if (m_pEventHandler)
|
---|
144 | {
|
---|
145 | connect(m_pEventHandler, &UIProgressEventHandler::sigProgressPercentageChange,
|
---|
146 | this, &UIProgressObject::sltHandleProgressPercentageChange);
|
---|
147 | connect(m_pEventHandler, &UIProgressEventHandler::sigProgressTaskComplete,
|
---|
148 | this, &UIProgressObject::sltHandleProgressTaskComplete);
|
---|
149 | connect(m_pEventHandler, &UIProgressEventHandler::sigHandlingFinished,
|
---|
150 | this, &UIProgressObject::sigProgressEventHandlingFinished);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | void UIProgressObject::cleanup()
|
---|
155 | {
|
---|
156 | /* Destroy CProgress event handler: */
|
---|
157 | disconnect(m_pEventHandler, &UIProgressEventHandler::sigProgressPercentageChange,
|
---|
158 | this, &UIProgressObject::sltHandleProgressPercentageChange);
|
---|
159 | disconnect(m_pEventHandler, &UIProgressEventHandler::sigProgressTaskComplete,
|
---|
160 | this, &UIProgressObject::sltHandleProgressTaskComplete);
|
---|
161 | disconnect(m_pEventHandler, &UIProgressEventHandler::sigHandlingFinished,
|
---|
162 | this, &UIProgressObject::sigProgressEventHandlingFinished);
|
---|
163 | delete m_pEventHandler;
|
---|
164 | m_pEventHandler = 0;
|
---|
165 | }
|
---|