1 | /* $Id: UIErrorString.cpp 79365 2019-06-26 15:57:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIErrorString class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /* Qt includes: */
|
---|
19 | #include <QApplication>
|
---|
20 | #include <QObject>
|
---|
21 |
|
---|
22 | /* GUI includes: */
|
---|
23 | #include "UICommon.h"
|
---|
24 | #include "UIErrorString.h"
|
---|
25 |
|
---|
26 | /* COM includes: */
|
---|
27 | #include "COMDefs.h"
|
---|
28 | #include "CProgress.h"
|
---|
29 | #include "CVirtualBoxErrorInfo.h"
|
---|
30 |
|
---|
31 |
|
---|
32 | /* static */
|
---|
33 | QString UIErrorString::formatRC(HRESULT rc)
|
---|
34 | {
|
---|
35 | QString str;
|
---|
36 |
|
---|
37 | PCRTCOMERRMSG msg = NULL;
|
---|
38 | const char *pErrMsg = NULL;
|
---|
39 |
|
---|
40 | /* First, try as is (only set bit 31 bit for warnings): */
|
---|
41 | if (SUCCEEDED_WARNING(rc))
|
---|
42 | msg = RTErrCOMGet(rc | 0x80000000);
|
---|
43 | else
|
---|
44 | msg = RTErrCOMGet(rc);
|
---|
45 |
|
---|
46 | if (msg != NULL)
|
---|
47 | pErrMsg = msg->pszDefine;
|
---|
48 |
|
---|
49 | #ifdef VBOX_WS_WIN
|
---|
50 | PCRTWINERRMSG winMsg = NULL;
|
---|
51 |
|
---|
52 | /* If not found, try again using RTErrWinGet with masked off top 16bit: */
|
---|
53 | if (msg == NULL)
|
---|
54 | {
|
---|
55 | winMsg = RTErrWinGet(rc & 0xFFFF);
|
---|
56 |
|
---|
57 | if (winMsg != NULL)
|
---|
58 | pErrMsg = winMsg->pszDefine;
|
---|
59 | }
|
---|
60 | #endif /* VBOX_WS_WIN */
|
---|
61 |
|
---|
62 | if (pErrMsg != NULL && *pErrMsg != '\0')
|
---|
63 | str.sprintf("%s", pErrMsg);
|
---|
64 |
|
---|
65 | return str;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* static */
|
---|
69 | QString UIErrorString::formatRCFull(HRESULT rc)
|
---|
70 | {
|
---|
71 | QString str;
|
---|
72 |
|
---|
73 | PCRTCOMERRMSG msg = NULL;
|
---|
74 | const char *pErrMsg = NULL;
|
---|
75 |
|
---|
76 | /* First, try as is (only set bit 31 bit for warnings): */
|
---|
77 | if (SUCCEEDED_WARNING(rc))
|
---|
78 | msg = RTErrCOMGet(rc | 0x80000000);
|
---|
79 | else
|
---|
80 | msg = RTErrCOMGet(rc);
|
---|
81 |
|
---|
82 | if (msg != NULL)
|
---|
83 | pErrMsg = msg->pszDefine;
|
---|
84 |
|
---|
85 | #ifdef VBOX_WS_WIN
|
---|
86 | PCRTWINERRMSG winMsg = NULL;
|
---|
87 |
|
---|
88 | /* If not found, try again using RTErrWinGet with masked off top 16bit: */
|
---|
89 | if (msg == NULL)
|
---|
90 | {
|
---|
91 | winMsg = RTErrWinGet(rc & 0xFFFF);
|
---|
92 |
|
---|
93 | if (winMsg != NULL)
|
---|
94 | pErrMsg = winMsg->pszDefine;
|
---|
95 | }
|
---|
96 | #endif /* VBOX_WS_WIN */
|
---|
97 |
|
---|
98 | if (pErrMsg != NULL && *pErrMsg != '\0')
|
---|
99 | str.sprintf("%s (0x%08X)", pErrMsg, rc);
|
---|
100 | else
|
---|
101 | str.sprintf("0x%08X", rc);
|
---|
102 |
|
---|
103 | return str;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /* static */
|
---|
107 | QString UIErrorString::formatErrorInfo(const CProgress &comProgress)
|
---|
108 | {
|
---|
109 | /* Check for API errors first: */
|
---|
110 | if (!comProgress.isOk())
|
---|
111 | return formatErrorInfo(static_cast<COMBaseWithEI>(comProgress));
|
---|
112 |
|
---|
113 | /* For progress errors otherwise: */
|
---|
114 | CVirtualBoxErrorInfo comErrorInfo = comProgress.GetErrorInfo();
|
---|
115 | /* Handle valid error-info first: */
|
---|
116 | if (!comErrorInfo.isNull())
|
---|
117 | return formatErrorInfo(comErrorInfo);
|
---|
118 | /* Handle NULL error-info otherwise: */
|
---|
119 | return QString("<table bgcolor=#EEEEEE border=0 cellspacing=5 cellpadding=0 width=100%>"
|
---|
120 | "<tr><td>%1</td><td><tt>%2</tt></td></tr></table>")
|
---|
121 | .arg(QApplication::translate("UIErrorString", "Result Code: ", "error info"))
|
---|
122 | .arg(formatRCFull(comProgress.GetResultCode()))
|
---|
123 | .prepend("<!--EOM-->") /* move to details */;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* static */
|
---|
127 | QString UIErrorString::formatErrorInfo(const COMErrorInfo &comInfo, HRESULT wrapperRC /* = S_OK */)
|
---|
128 | {
|
---|
129 | return QString("<qt>%1</qt>").arg(UIErrorString::errorInfoToString(comInfo, wrapperRC));
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* static */
|
---|
133 | QString UIErrorString::formatErrorInfo(const CVirtualBoxErrorInfo &comInfo)
|
---|
134 | {
|
---|
135 | return formatErrorInfo(COMErrorInfo(comInfo));
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* static */
|
---|
139 | QString UIErrorString::formatErrorInfo(const COMBaseWithEI &comWrapper)
|
---|
140 | {
|
---|
141 | Assert(comWrapper.lastRC() != S_OK);
|
---|
142 | return formatErrorInfo(comWrapper.errorInfo(), comWrapper.lastRC());
|
---|
143 | }
|
---|
144 |
|
---|
145 | /* static */
|
---|
146 | QString UIErrorString::formatErrorInfo(const COMResult &comRc)
|
---|
147 | {
|
---|
148 | Assert(comRc.rc() != S_OK);
|
---|
149 | return formatErrorInfo(comRc.errorInfo(), comRc.rc());
|
---|
150 | }
|
---|
151 |
|
---|
152 | /* static */
|
---|
153 | QString UIErrorString::errorInfoToString(const COMErrorInfo &comInfo, HRESULT wrapperRC)
|
---|
154 | {
|
---|
155 | /* Compose complex details string with internal <!--EOM--> delimiter to
|
---|
156 | * make it possible to split string into info & details parts which will
|
---|
157 | * be used separately in QIMessageBox. */
|
---|
158 | QString strFormatted;
|
---|
159 |
|
---|
160 | /* Check if details text is NOT empty: */
|
---|
161 | const QString strDetailsInfo = comInfo.text();
|
---|
162 | if (!strDetailsInfo.isEmpty())
|
---|
163 | {
|
---|
164 | /* Check if details text written in English (latin1) and translated: */
|
---|
165 | if ( strDetailsInfo == QString::fromLatin1(strDetailsInfo.toLatin1())
|
---|
166 | && strDetailsInfo != QObject::tr(strDetailsInfo.toLatin1().constData()))
|
---|
167 | strFormatted += QString("<p>%1.</p>").arg(uiCommon().emphasize(QObject::tr(strDetailsInfo.toLatin1().constData())));
|
---|
168 | else
|
---|
169 | strFormatted += QString("<p>%1.</p>").arg(uiCommon().emphasize(strDetailsInfo));
|
---|
170 | }
|
---|
171 |
|
---|
172 | strFormatted += "<!--EOM--><table bgcolor=#EEEEEE border=0 cellspacing=5 "
|
---|
173 | "cellpadding=0 width=100%>";
|
---|
174 |
|
---|
175 | bool fHaveResultCode = false;
|
---|
176 |
|
---|
177 | if (comInfo.isBasicAvailable())
|
---|
178 | {
|
---|
179 | #ifdef VBOX_WS_WIN
|
---|
180 | fHaveResultCode = comInfo.isFullAvailable();
|
---|
181 | bool fHaveComponent = true;
|
---|
182 | bool fHaveInterfaceID = true;
|
---|
183 | #else /* !VBOX_WS_WIN */
|
---|
184 | fHaveResultCode = true;
|
---|
185 | bool fHaveComponent = comInfo.isFullAvailable();
|
---|
186 | bool fHaveInterfaceID = comInfo.isFullAvailable();
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | if (fHaveResultCode)
|
---|
190 | {
|
---|
191 | strFormatted += QString("<tr><td>%1</td><td><tt>%2</tt></td></tr>")
|
---|
192 | .arg(QApplication::translate("UIErrorString", "Result Code: ", "error info"))
|
---|
193 | .arg(formatRCFull(comInfo.resultCode()));
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (fHaveComponent)
|
---|
197 | strFormatted += QString("<tr><td>%1</td><td>%2</td></tr>")
|
---|
198 | .arg(QApplication::translate("UIErrorString", "Component: ", "error info"), comInfo.component());
|
---|
199 |
|
---|
200 | if (fHaveInterfaceID)
|
---|
201 | {
|
---|
202 | QString s = comInfo.interfaceID().toString();
|
---|
203 | if (!comInfo.interfaceName().isEmpty())
|
---|
204 | s = comInfo.interfaceName() + ' ' + s;
|
---|
205 | strFormatted += QString("<tr><td>%1</td><td>%2</td></tr>")
|
---|
206 | .arg(QApplication::translate("UIErrorString", "Interface: ", "error info"), s);
|
---|
207 | }
|
---|
208 |
|
---|
209 | if (!comInfo.calleeIID().isNull() && comInfo.calleeIID() != comInfo.interfaceID())
|
---|
210 | {
|
---|
211 | QString s = comInfo.calleeIID().toString();
|
---|
212 | if (!comInfo.calleeName().isEmpty())
|
---|
213 | s = comInfo.calleeName() + ' ' + s;
|
---|
214 | strFormatted += QString("<tr><td>%1</td><td>%2</td></tr>")
|
---|
215 | .arg(QApplication::translate("UIErrorString", "Callee: ", "error info"), s);
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | if ( FAILED(wrapperRC)
|
---|
220 | && (!fHaveResultCode || wrapperRC != comInfo.resultCode()))
|
---|
221 | {
|
---|
222 | strFormatted += QString("<tr><td>%1</td><td><tt>%2</tt></td></tr>")
|
---|
223 | .arg(QApplication::translate("UIErrorString", "Callee RC: ", "error info"))
|
---|
224 | .arg(formatRCFull(wrapperRC));
|
---|
225 | }
|
---|
226 |
|
---|
227 | strFormatted += "</table>";
|
---|
228 |
|
---|
229 | if (comInfo.next())
|
---|
230 | strFormatted = strFormatted + "<!--EOP-->" + errorInfoToString(*comInfo.next());
|
---|
231 |
|
---|
232 | return strFormatted;
|
---|
233 | }
|
---|
234 |
|
---|