Line data Source code
1 : /*! 2 : * @file SystemInfoProvider.cpp 3 : * @brief Implementation of the SystemInfoProvider class. 4 : * @version 0.1 5 : * @date 2025-02-12 6 : * @details This file contains the implementation of the SystemInfoProvider 7 : * @author Félix LE BIHAN (@Fle-bihh) 8 : * @author Tiago Pereira (@t-pereira06) 9 : * @author Ricardo Melo (@reomelo) 10 : * @author Michel Batista (@MicchelFAB) 11 : * 12 : * @copyright Copyright (c) 2025 13 : */ 14 : 15 : #include "SystemInfoProvider.hpp" 16 : #include <QDebug> 17 : #include "SystemCommandExecutor.hpp" 18 : 19 : /*! 20 : * @brief Constructor for the SystemInfoProvider class. 21 : * @details Allocates a SystemCommandExecutor if the one provided is nullptr. 22 : * @param executor The executor to use. If nullptr, a new SystemCommandExecutor is 23 : * allocated. 24 : */ 25 7 : SystemInfoProvider::SystemInfoProvider(ISystemCommandExecutor *executor) 26 7 : : m_executor(executor ? executor : new SystemCommandExecutor()) 27 7 : , m_ownExecutor(executor == nullptr) 28 7 : {} 29 : 30 : 31 : /*! 32 : * @brief Destructor for the SystemInfoProvider class. 33 : * @details Deletes the executor if it was allocated by the SystemInfoProvider. */ 34 14 : SystemInfoProvider::~SystemInfoProvider() 35 : { 36 7 : if (m_ownExecutor) 37 0 : delete m_executor; 38 14 : } 39 : 40 : /*! 41 : * @brief Gets the current WiFi status. 42 : * @param wifiName The name of the WiFi network we are connected to, or an empty 43 : * string if not connected. 44 : * @return The current WiFi status as a QString: 45 : * - "Connected" if connected to a network 46 : * - "Disconnected" if not connected to a network 47 : * - "No interface detected" if no wlan interface is detected 48 : */ 49 3 : QString SystemInfoProvider::getWifiStatus(QString &wifiName) const 50 : { 51 6 : QString output = m_executor->executeCommand("nmcli -t -f DEVICE,STATE,CONNECTION dev"); 52 6 : QStringList lines = output.split('\n'); 53 : 54 4 : for (const QString &line : lines) { 55 3 : if (line.startsWith("wlan")) { 56 2 : QStringList parts = line.split(':'); 57 2 : if (parts.size() >= 3) { 58 2 : wifiName = parts[2]; 59 2 : return (parts[1] == "connected") ? "Connected" : "Disconnected"; 60 : } 61 : } 62 : } 63 1 : wifiName.clear(); 64 1 : return "No interface detected"; 65 : } 66 : 67 : 68 : /*! 69 : * @brief Gets the current temperature in degrees Celsius. 70 : * @return The current temperature as a QString, e.g. "45.6°C". 71 : * If no temperature is available, returns "N/A". 72 : */ 73 2 : QString SystemInfoProvider::getTemperature() const 74 : { 75 4 : QString tempStr = m_executor->readFile("/sys/class/hwmon/hwmon0/temp1_input").trimmed(); 76 : 77 : bool ok; 78 2 : double tempMillidegrees = tempStr.toDouble(&ok); 79 4 : return ok ? QString("%1°C").arg(tempMillidegrees / 1000.0, 0, 'f', 1) : "N/A"; 80 : } 81 : /*! 82 : * @brief Gets the current IP address of the WiFi interface. 83 : * @return The current IP address as a QString. 84 : * If no IP address is available, returns "No IP address". 85 : */ 86 : 87 2 : QString SystemInfoProvider::getIpAddress() const 88 : { 89 2 : QString output = m_executor->executeCommand( 90 2 : "sh -c \"ip -4 addr show wlan0 | grep -oP '(?<=inet\\s)\\d+\\.\\d+\\.\\d+\\.\\d+'\""); 91 : 92 6 : return output.trimmed().isEmpty() ? "No IP address" : output.trimmed(); 93 : }