Index: sources/storage/filehandler.cpp =================================================================== diff -u -r90d3f5b31186834168c9ad869f8d2d038200dfcf -rfbeafa0714f065bce0403e2e8ce68f6d8fbea6bd --- sources/storage/filehandler.cpp (.../filehandler.cpp) (revision 90d3f5b31186834168c9ad869f8d2d038200dfcf) +++ sources/storage/filehandler.cpp (.../filehandler.cpp) (revision fbeafa0714f065bce0403e2e8ce68f6d8fbea6bd) @@ -13,66 +13,126 @@ */ #include "filehandler.h" +// Linux #include +#include +//#include + //Qt #include #include // Project +#include "maintimer.h" +// namespace using namespace Storage; -FileHandler::FileHandler(QObject *parent) : QObject(parent) + +// Singleton +SINGLETON_INIT(FileHandler) + +FileHandler::FileHandler(QObject *parent) : QObject(parent) { } + +bool FileHandler::umounted() const { - connect(&fsWatcher, SIGNAL(directoryChanged(QString)), - this , SLOT(directoryChanged(QString))); + return _umounted; } +void FileHandler::umounted(bool vUmounted) +{ + _umounted = vUmounted; +} + bool FileHandler::init() { - fsWatcher.addPath("/dev/"); + initConnections(); return true; } -void FileHandler::directoryChanged(const QString &vPath) +void FileHandler::initConnections() { - Q_UNUSED(vPath) - bool available = false; + connect(_MainTimer , SIGNAL( didTimeout()), + this , SLOT(onMainTimerTimeout())); +} + +bool FileHandler::usbSeek(QString &vDevice) { QString device = ""; - if ( ! _mounted ) { - for (int a = 'a'; a <= 'z'; a++) { - device = QString("/dev/sd%1%2").arg(QChar(a)).arg(QChar('1')); - if (QFileInfo::exists(device)) { - available = true; - break; - } - umount(device.toLatin1().constData()); + for (char a = 'a'; a <= 'z'; a++) { + device = QString("/dev/sd%1%2").arg(a).arg('1'); + if (QFileInfo::exists(device)) { + vDevice = device; + return true; // application is deciding on the first existing drive } } - if (available) { - if ( ! _mounted ) { - if ( mountUsb(device) ) { - _mounted = true; - } else { - qDebug() << tr("1 - USB drive %1 can't be mounted").arg(_usbMount); - } + vDevice = device; + return false; +} + +void FileHandler::doUSBDriveMount() +{ + QString device = ""; + if (usbSeek(device)) { + if (! umounted()) { + usbMount(device); + } else { + usbUmount(_usbMount); // release + qDebug() << tr("USB drive umounted"); } } else { - umount(_usbMount); + usbRemove(); + } +} + +void FileHandler::usbError(const QString &vDevice) +{ + switch (errno) { + case EBUSY: + qDebug() << tr("%1 - Device or resource busy (%2)").arg(errno).arg(vDevice); + break; + default: + qDebug() << tr("%1 - %2 (%3 , %4)").arg(errno).arg(strerror(errno)).arg(vDevice).arg(_usbMount); + break; + } +} + +bool FileHandler::usbMount(const QString &vDevice) +{ + bool ok; + _usbDrive = vDevice.toLatin1().constData(); + ok = mount(_usbDrive, _usbMount, _usbfsys, 0, "") == 0; + if (ok) { _mounted = false; - qDebug() << tr("2 - USB drive %1 can't be mounted").arg(_usbMount); - return; + qDebug() << tr("USB flash drive %1 has been mounted on %2").arg(vDevice).arg(_usbMount); + emit didUSBDriveMount(); + } else { + usbError(vDevice); } - emit usbStatusChanged(available); + return ok; } -bool FileHandler::mountUsb(QString device) +bool FileHandler::usbUmount(const QString &vDevice) { - int result = mount(device.toLatin1().constData(), _usbMount, _usbfsys, 0, ""); - if (result == 0) { - qDebug() << tr("USB flash drive %1 has been mounted to %2").arg(device).arg(_usbMount); + bool ok; + ok = umount(vDevice.toLatin1().constData()) == 0; + if (ok) { + emit didUSBDriveUmount(); } else { - return false; + // the error is irrelevant, commented out for now + //usbError(vDevice); } - return true; + return ok; } +void FileHandler::usbRemove() +{ + usbUmount(_usbMount); + umounted(false); + emit didUSBDriveRemove(); + qDebug() << tr("USB drive removed"); +} + +void FileHandler::onMainTimerTimeout() +{ + doUSBDriveMount(); +} +