File indexing completed on 2022-09-27 16:29:35

0001 /*
0002     SPDX-FileCopyrightText: 2016 ROSA
0003     SPDX-License-Identifier: GPL-3.0-or-later
0004 */
0005 
0006 #ifndef COMMON_H
0007 #define COMMON_H
0008 
0009 ////////////////////////////////////////////////////////////////////////////////
0010 // This file contains some commonly-used constants and function declarations
0011 
0012 #include <QDebug>
0013 #include <QObject>
0014 #include <QString>
0015 #include <QLoggingCategory>
0016 
0017 #include <type_traits>
0018 
0019 #include "platform.h"
0020 
0021 class UsbDevice;
0022 
0023 // Default unit to be used when displaying file/device sizes (MB)
0024 const quint64 DEFAULT_UNIT = 1048576;
0025 
0026 // Application name used for titles in messageboxes
0027 #if defined(ROSA_BRANDING)
0028 const QString ApplicationTitle = "ROSA Image Writer";
0029 #else
0030 const QString ApplicationTitle = "ISO Image Writer";
0031 #endif
0032 
0033 // Pointer to correctly typed application instance
0034 #define mApp (static_cast<MainApplication*>qApp)
0035 
0036 // Returns the number of blocks required to contain some number of bytes
0037 // Input:
0038 //  T      - any integer type
0039 //  val    - number of bytes
0040 //  factor - size of the block
0041 // Returns:
0042 //  the number of blocks of size <factor> required for <val> to fit in
0043 template <typename T> T alignNumberDiv(T val, T factor)
0044 {
0045     static_assert(std::is_integral<T>::value, "Only integer types are supported!");
0046     return ((val + factor - 1) / factor);
0047 }
0048 
0049 // Returns the total size of blocks required to contain some number of bytes
0050 // Input:
0051 //  T      - any integer type
0052 //  val    - number of bytes
0053 //  factor - size of the block
0054 // Returns:
0055 //  the total size of blocks of size <factor> required for <val> to fit in
0056 template <typename T> T alignNumber(T val, T factor)
0057 {
0058     static_assert(std::is_integral<T>::value, "Only integer types are supported!");
0059     return alignNumberDiv(val, factor) * factor;
0060 }
0061 
0062 #if defined(Q_OS_WIN32)
0063 // Converts the WinAPI and COM error code into text message
0064 // Input:
0065 //  errorCode - error code (GetLastError() is used by default)
0066 // Returns:
0067 //  system error message for the errorCode
0068 QString errorMessageFromCode(DWORD errorCode = GetLastError());
0069 
0070 // Converts the WinAPI and COM error code into text message
0071 // Input:
0072 //  prefixMessage - error description
0073 //  errorCode     - error code (GetLastError() is used by default)
0074 // Returns:
0075 //  prefixMessage followed by a newline and the system error message for the errorCode
0076 QString formatErrorMessageFromCode(QString prefixMessage, DWORD errorCode = GetLastError());
0077 #endif
0078 
0079 // Gets the contents of the specified file
0080 // Input:
0081 //  fileName - path to the file to read
0082 // Returns:
0083 //  the file contents or empty string if an error occurred
0084 QString readFileContents(const QString& fileName);
0085 
0086 // Callback function type for platformEnumFlashDevices (see below)
0087 // Input:
0088 //  cbParam        - parameter passed to the enumeration function
0089 //  DeviceVendor   - vendor of the USB device
0090 //  DeviceName     - name of the USB device
0091 //  PhysicalDevice - OS-specific path to the physical device
0092 //  Volumes        - list of volumes this device contains
0093 //  NumVolumes     - number of volumes in the list
0094 //  Size           - size of the disk in bytes
0095 //  SectorSize     - sector size of the device
0096 // Returns:
0097 //  nothing
0098 typedef void (*AddFlashDeviceCallbackProc)(void* cbParam, UsbDevice* device);
0099 
0100 // Performs platform-specific enumeration of USB flash disks and calls the callback
0101 // function for adding these devices into the application GUI structure
0102 // Input:
0103 //  callback - callback function to be called for each new device
0104 //  cbParam  - parameter to be passed to this callback function
0105 // Returns:
0106 //  true if enumeration completed successfully, false otherwise
0107 bool platformEnumFlashDevices(AddFlashDeviceCallbackProc callback, void* cbParam);
0108 
0109 // Checks the application privileges and if they are not sufficient, restarts
0110 // itself requesting higher privileges
0111 // Input:
0112 //  appPath - path to the application executable
0113 // Returns:
0114 //  true if already running elevated
0115 //  false if error occurs
0116 //  does not return if elevation request succeeded (the current instance terminates)
0117 bool ensureElevated();
0118 
0119 #endif // COMMON_H