/*! * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file DriveWatcher.h * \author (last) Behrouz NematiPour * \date (last) 16-Oct-2020 * \author (original) Behrouz NemaiPour * \date (original) 17-Jul-2020 * */ #pragma once // Qt #include // Project #include "main.h" // Doxygen : don't remove #include "Threads.h" // Define #define _DriveWatcher Storage::DriveWatcher::I() // forward declarations class tst_initializations; namespace Storage { /*! * \brief The DriveWatcher class * \details This class is watching for the USB and SD-Card drives in Linux file system. * It has the interval of 1000 ms and will look if any device between /dev/sda1 to /dev/sdz1 exists then will mount it in /media/usb, * and does check if the SD-Card has been mounted under /media/sdcard, * but for SD-Card doesn't try to mount it and it has to be mounted prior to UI Application start * and that's because from UI Application perspective it is not a removable/hot-plug device (although physically it is). * This class works in its own thread and will send notification by emitting signals about the USB device status * which are Mounted, Unmounted, Removed * and SD-Card space conditions changes. * \note : 'Removed' is when no USB device present. * */ class DriveWatcher : public QObject { Q_OBJECT // Singleton SINGLETON(DriveWatcher) // friends friend class ::tst_initializations; bool _mounted = false; bool _umounted = false; bool _removed = false; const char *_usbDrive = ""; const int _interval = 1000; // in ms const qint8 _minRequiredAvailableSpacePercent = 10; QThread *_thread = nullptr; bool _init = false; bool _pauseSpaceCheck = false; public slots: bool init(); bool init(QThread &vThread); private slots: void quit(); void onRemoveLogs(bool vInProgress); protected: void timerEvent(QTimerEvent *) override; private: void initConnections(); void initThread(QThread &vThread); void quitThread(); bool usbSeek(QString &vDevice); bool driveSpaceCheck(const QString &vPath, qint64 &vTotalBytes, qint64 &vAvailableBytes, bool *vIsReadOnly = nullptr); signals: /*! * \brief didUSBDriveMount * \details notifies UI when USB device is available and has been mounted. */ void didUSBDriveMount (); /*! * \brief didUSBDriveUmount * \details notifies USB watcher on UI(user) request for USB umount. */ void didUSBDriveUmount(); /*! * \brief didUSBDriveRemove * \details notifies UI when USB device has been removed(not exists). */ void didUSBDriveRemove(); /*! * \brief didSDCardFreeSpaceChange * \param vReady - Device is mounted and ready * \note if device ejected manually system assumes it's still ready. * \param vTotal - Returns the total volume size in bytes. * Returns -1 if QStorageInfo object is not valid * \param vAvailable - Returns the size (in bytes) available for the current user. * It returns the total size available if the user is the root user or a system administrator. * This size can be less than or equal to the free size returned by bytesFree() function. * Returns -1 if QStorageInfo object is not valid. * \param vPercent - The percentage of available space. * \note Will emitted if only one of the publishing parameter changes. */ void didSDCardSpaceChange(bool vReady, qint64 vTotal, qint64 vAvailable, quint8 vPercent); /*! * \brief didSDCardStateChange * \details If SDCard state changes like removed or is not present this signal will emit. */ void didSDCardStateChange(bool vIsReady, bool vIsReadOnly); /*! * \brief didSDCardSpaceTooLow * \details this signal will emit ones the available space left on the SD-Card * is less than minimum required percentage defined in _minRequiredAvailableSpacePercent. * \param vAvailablePercent */ void didSDCardSpaceTooLow(quint8 vAvailablePercent); private slots: // ----- USB void usbCheck(); bool usbMount (const QString &vDevice); bool usbUmount(const QString &vDevice); void usbRemove(); void usbError (const QString &vDevice); void onUSBDriveUmount(); // ----- SDCard void sdcardSpaceCheck(); }; }