Index: sources/storage/usbwatcher.cpp =================================================================== diff -u -re1605219ac2baf49ef21d0889f845ac53d59c3c1 -r56d00a82669a7a2c00ab90109a89dbec8db27527 --- sources/storage/usbwatcher.cpp (.../usbwatcher.cpp) (revision e1605219ac2baf49ef21d0889f845ac53d59c3c1) +++ sources/storage/usbwatcher.cpp (.../usbwatcher.cpp) (revision 56d00a82669a7a2c00ab90109a89dbec8db27527) @@ -29,54 +29,101 @@ // namespace using namespace Storage; -USBWatcher::USBWatcher(QObject *parent) : QObject(parent) { - startTimer(_checkInterval); -} +USBWatcher::USBWatcher(QObject *parent) : QObject(parent) { } +/*! + * \brief USBWatcher::init + * \details Initializes the class by setting the connections and starting the timer + * \return False if it has been called before. + */ bool USBWatcher::init() { - // runs in main thread - Q_ASSERT_X(QThread::currentThread() == qApp->thread() , "_USBWatcher::init", "The Class initialization must be done in Main Thread" ); - _USBWatcher_Thread.setObjectName("USB Watcher Thread"); - connect(qApp, &QApplication::aboutToQuit, this, &USBWatcher::quit); - _USBWatcher_Thread.start(); - moveToThread(&_USBWatcher_Thread); + if ( _init ) return false; + _init = true; - // runs in USBWatcher thread initConnections(); + startTimer(_checkInterval); return true; } -void USBWatcher::quit() +/*! + * \brief USBWatcher::init + * \details Initialized the Class by calling the init() method first + * And initializes the thread vThread by calling initThread + * on success init(). + * \param vThread - the thread + * \return returns the return value of the init() method + */ +bool USBWatcher::init(QThread &vThread) { - // runs in Logger thread - moveToThread(qApp->thread()); + if ( init() ) return false; + initThread(vThread); + return true; +} - // runs in main thread - _USBWatcher_Thread.quit(); - _USBWatcher_Thread.wait(); +/*! + * \brief USBWatcher::quit + * \details quits the class + * Calls quitThread + */ +void USBWatcher::quit() +{ + quitThread(); } +/*! + * \brief USBWatcher::initConnections + * \details Initializes the required signal/slot connection between this class and other objects + * to be able to communicate. + */ void USBWatcher::initConnections() { connect(&_ApplicationController, SIGNAL(didUSBDriveUmount()), this , SLOT( onUSBDriveUmount())); } -bool USBWatcher::isMounted() const +/*! + * \brief ApplicationController::initThread + * \details Moves this object into the thread vThread. + * And checks that this method is called from main thread. + * Also connects quitThread to application aboutToQuit. + * \param vThread - the thread + */ +void USBWatcher::initThread(QThread &vThread) { - return _mounted; + // runs in main thread + Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); + _thread = &vThread; + _thread->setObjectName(QString("%1_Thread").arg(metaObject()->className())); + connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit())); + _thread->start(); + moveToThread(_thread); } -bool USBWatcher::isUmounted() const +/*! + * \brief USBWatcher::quitThread + * \details Moves this object to main thread to be handled by QApplicaiton + * And to be destroyed there. + */ +void USBWatcher::quitThread() { - return _umounted; + if ( ! _thread ) return; + + // runs in thread + moveToThread(qApp->thread()); } +/*! + * \brief USBWatcher::usbSeek + * \details Tries to look for the available USB devices + * Starts from sda1 to sdz1. + * \note will only look for the first partition if there is any + * \param vDevice - Found device (/dev/sda1) + * \return true if a device found + */ bool USBWatcher::usbSeek(QString &vDevice) { - PRINT_THREAD_NAME; QString device = ""; for (char a = 'a'; a <= 'z'; a++) { device = QString("/dev/sd%1%2").arg(a).arg('1'); @@ -89,15 +136,25 @@ return false; } +/*! + * \brief USBWatcher::timerEvent + * \details This event handler has been re-implemented in here + * to receive timer events for the object + * for the timer which has been set to _checkInterval + * Runs the usbCheck on interval + */ void USBWatcher::timerEvent(QTimerEvent *) { - PRINT_THREAD_NAME; usbcheck(); } +/*! + * \brief USBWatcher::usbcheck + * \details Runs usbSeek to mount or umount or remove it + * regarding the state it's in. + */ void USBWatcher::usbcheck() { - PRINT_THREAD_NAME; QString device = ""; if (usbSeek(device)) { if (! _umounted ) { @@ -112,6 +169,14 @@ } } +/*! + * \brief USBWatcher::usbError + * \details Logs any error which has been happened + * On USB device vDevice + * \note When this method has been called error number will be read from errno variable, + * Which has been set by umount or mount. + * \param vDevice + */ void USBWatcher::usbError(const QString &vDevice) { QString error; @@ -131,17 +196,28 @@ } } +/*! + * \brief USBWatcher::onUSBDriveUmount + * \details This is the slot connected to the _ApplicationController's didUSBDriveUmount SIGNAL, + * To notify the USB drive detach. + */ void USBWatcher::onUSBDriveUmount() { _umounted = true; } +/*! + * \brief USBWatcher::usbMount + * \details Mounts the USB device vDevice + * \note Emits didUSBDriveMount signal + * \param vDevice - USB device to be mounted (eg. /dev/sda1) + * \return true on successful mount + */ bool USBWatcher::usbMount(const QString &vDevice) { - PRINT_THREAD_NAME; bool ok; _usbDrive = vDevice.toLatin1().constData(); - ok = mount(_usbDrive, USB_Mount_Point, USB_File_System, 0, "") == 0; + ok = ::mount(_usbDrive, USB_Mount_Point, USB_File_System, 0, "") == 0; if (ok) { _mounted = true; _removed = false; @@ -153,9 +229,15 @@ return ok; } +/*! + * \brief USBWatcher::usbUmount + * \details Unmounts the USB device vDevice + * \note Emits didUSBDriveUmount signal + * \param vDevice - USB device to be unmounted (eg. /dev/sda1) + * \return true on successful unmount + */ bool USBWatcher::usbUmount(const QString &vDevice) { - PRINT_THREAD_NAME; bool ok; ok = umount(vDevice.toLatin1().constData()) == 0; if (ok) { @@ -169,9 +251,14 @@ return ok; } +/*! + * \brief USBWatcher::usbRemove + * \details Removed the USB mount point + * So next time it is not mounted as next device. + * \note Emits didUSBDriveRemove signal + */ void USBWatcher::usbRemove() { - PRINT_THREAD_NAME; usbUmount(USB_Mount_Point); _umounted = false; _removed = true;