File indexing completed on 2024-04-28 09:43:51

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 const QString ApplicationTitle = "ISO Image Writer";
0028 
0029 // Pointer to correctly typed application instance
0030 #define mApp (static_cast<MainApplication*>qApp)
0031 
0032 // Returns the number of blocks required to contain some number of bytes
0033 // Input:
0034 //  T      - any integer type
0035 //  val    - number of bytes
0036 //  factor - size of the block
0037 // Returns:
0038 //  the number of blocks of size <factor> required for <val> to fit in
0039 template <typename T> T alignNumberDiv(T val, T factor)
0040 {
0041     static_assert(std::is_integral<T>::value, "Only integer types are supported!");
0042     return ((val + factor - 1) / factor);
0043 }
0044 
0045 // Returns the total size of blocks required to contain some number of bytes
0046 // Input:
0047 //  T      - any integer type
0048 //  val    - number of bytes
0049 //  factor - size of the block
0050 // Returns:
0051 //  the total size of blocks of size <factor> required for <val> to fit in
0052 template <typename T> T alignNumber(T val, T factor)
0053 {
0054     static_assert(std::is_integral<T>::value, "Only integer types are supported!");
0055     return alignNumberDiv(val, factor) * factor;
0056 }
0057 
0058 #if defined(Q_OS_WIN32)
0059 // Converts the WinAPI and COM error code into text message
0060 // Input:
0061 //  errorCode - error code (GetLastError() is used by default)
0062 // Returns:
0063 //  system error message for the errorCode
0064 QString errorMessageFromCode(DWORD errorCode = GetLastError());
0065 
0066 // Converts the WinAPI and COM error code into text message
0067 // Input:
0068 //  prefixMessage - error description
0069 //  errorCode     - error code (GetLastError() is used by default)
0070 // Returns:
0071 //  prefixMessage followed by a newline and the system error message for the errorCode
0072 QString formatErrorMessageFromCode(QString prefixMessage, DWORD errorCode = GetLastError());
0073 #endif
0074 
0075 // Gets the contents of the specified file
0076 // Input:
0077 //  fileName - path to the file to read
0078 // Returns:
0079 //  the file contents or empty string if an error occurred
0080 QString readFileContents(const QString& fileName);
0081 
0082 // Callback function type for platformEnumFlashDevices (see below)
0083 // Input:
0084 //  cbParam        - parameter passed to the enumeration function
0085 //  DeviceVendor   - vendor of the USB device
0086 //  DeviceName     - name of the USB device
0087 //  PhysicalDevice - OS-specific path to the physical device
0088 //  Volumes        - list of volumes this device contains
0089 //  NumVolumes     - number of volumes in the list
0090 //  Size           - size of the disk in bytes
0091 //  SectorSize     - sector size of the device
0092 // Returns:
0093 //  nothing
0094 typedef void (*AddFlashDeviceCallbackProc)(void* cbParam, UsbDevice* device);
0095 
0096 // Performs platform-specific enumeration of USB flash disks and calls the callback
0097 // function for adding these devices into the application GUI structure
0098 // Input:
0099 //  callback - callback function to be called for each new device
0100 //  cbParam  - parameter to be passed to this callback function
0101 // Returns:
0102 //  true if enumeration completed successfully, false otherwise
0103 bool platformEnumFlashDevices(AddFlashDeviceCallbackProc callback, void* cbParam);
0104 
0105 // Checks the application privileges and if they are not sufficient, restarts
0106 // itself requesting higher privileges
0107 // Input:
0108 //  appPath - path to the application executable
0109 // Returns:
0110 //  true if already running elevated
0111 //  false if error occurs
0112 //  does not return if elevation request succeeded (the current instance terminates)
0113 bool ensureElevated();
0114 
0115 #endif // COMMON_H