/*! * * Copyright (c) 2019-2019 Diality Inc. - All Rights Reserved. * \copyright \n * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, \n * IN PART OR IN WHOLE, \n * WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. \n * * \file filehandler.cpp * \date 2019/09/30 * \author Behrouz NematiPour * */ #include "filehandler.h" #include //Qt #include #include // Project using namespace Storage; FileHandler::FileHandler(QObject *parent) : QObject(parent) { connect(&fsWatcher, SIGNAL(directoryChanged(QString)), this , SLOT(directoryChanged(QString))); } bool FileHandler::init() { fsWatcher.addPath("/dev/"); return true; } void FileHandler::directoryChanged(const QString &vPath) { Q_UNUSED(vPath) bool available = false; 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()); } } if (available) { if ( ! _mounted ) { if ( mountUsb(device) ) { _mounted = true; } else { qDebug() << tr("1 - USB drive %1 can't be mounted").arg(_usbMount); } } } else { umount(_usbMount); _mounted = false; qDebug() << tr("2 - USB drive %1 can't be mounted").arg(_usbMount); return; } emit usbStatusChanged(available); } bool FileHandler::mountUsb(QString device) { 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); } else { return false; } return true; }