File indexing completed on 2024-04-21 05:48:32

0001 /*
0002     SPDX-FileCopyrightText: 2016 ROSA
0003     SPDX-License-Identifier: GPL-3.0-or-later
0004 */
0005 
0006 #include "common.h"
0007 
0008 #include <KLocalizedString>
0009 
0010 #include <QFile>
0011 #include <QStringList>
0012 ////////////////////////////////////////////////////////////////////////////////
0013 // Implementation of the non-template cross-platform functions from common.h
0014 
0015 
0016 #if defined(Q_OS_WIN32)
0017 // Converts the WinAPI and COM error code into text message
0018 // Input:
0019 //  errorCode - error code (GetLastError() is used by default)
0020 // Returns:
0021 //  system error message for the errorCode
0022 QString errorMessageFromCode(DWORD errorCode)
0023 {
0024     LPTSTR msgBuffer;
0025     DWORD res = FormatMessage(
0026         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
0027         NULL,
0028         errorCode,
0029         0,
0030         reinterpret_cast<LPTSTR>(&msgBuffer),
0031         0,
0032         NULL
0033     );
0034     if (res)
0035     {
0036         QString ret = QString::fromWCharArray(msgBuffer);
0037         LocalFree(msgBuffer);
0038         return ret;
0039     }
0040     else
0041         return i18n("Error code: %1", QString::number(errorCode));
0042 }
0043 
0044 // Converts the WinAPI and COM error code into text message
0045 // Input:
0046 //  prefixMessage - error description
0047 //  errorCode     - error code (GetLastError() is used by default)
0048 // Returns:
0049 //  prefixMessage followed by a newline and the system error message for the errorCode
0050 QString formatErrorMessageFromCode(QString prefixMessage, DWORD errorCode)
0051 {
0052     return prefixMessage + "\n" + errorMessageFromCode(errorCode);
0053 }
0054 
0055 // This constant is declared in wbemprov.h and defined in wbemuuid.lib. If building with MinGW, the header is available but not library,
0056 // and the constant remains unresolved. So we define it here.
0057 const CLSID CLSID_WbemAdministrativeLocator = {0xCB8555CC, 0x9128, 0x11D1, {0xAD, 0x9B, 0x00, 0xC0, 0x4F, 0xD8, 0xFD, 0xFF}};
0058 #endif
0059 
0060 // Gets the contents of the specified file
0061 // Input:
0062 //  fileName - path to the file to read
0063 // Returns:
0064 //  the file contents or empty string if an error occurred
0065 QString readFileContents(const QString& fileName)
0066 {
0067     QFile f(fileName);
0068     if (!f.open(QFile::ReadOnly))
0069         return "";
0070     QString ret = f.readAll();
0071     f.close();
0072     return ret;
0073 }