/*! * * 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" // Linux #include #include //#include //Qt #include #include #include // Project #include "maintimer.h" // namespace using namespace Storage; // Singleton SINGLETON_INIT(FileHandler) FileHandler::FileHandler(QObject *parent) : QObject(parent) { } bool FileHandler::umounted() const { return _umounted; } void FileHandler::umounted(bool vUmounted) { _umounted = vUmounted; } bool FileHandler::init() { initConnections(); return true; } void FileHandler::quit() { } void FileHandler::initConnections() { connect(_MainTimer , SIGNAL( didTimeout()), this , SLOT(onMainTimerTimeout())); //connect(&_processCopyFolder, SIGNAL(finished(int)), // this , SLOT(copyFolder)); } bool FileHandler::usbSeek(QString &vDevice) { QString device = ""; 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 } } vDevice = device; return false; } bool FileHandler::write(const QString &vFileName, const QString &vContent, bool vAppend) { QFile file(vFileName); QIODevice::OpenMode openMode = vAppend ? QFile::Text | QFile::Append : QFile::Text | QFile::WriteOnly; if (! file.open(openMode)) { qDebug() << "ERROR :" << tr("Can't open file for write (%1)").arg(vFileName); return false; } QTextStream out(&file); out << vContent; out.flush(); return true; } bool FileHandler::read(const QString &vFileName, QString &vContent) { QFile file(vFileName); if (! file.open(QFile::Text | QFile::ReadOnly)) { qDebug() << "ERROR :" << tr("Can't open file for read (%1)").arg(vFileName); return false; } QTextStream in(&file); vContent = in.readAll(); return true; } void FileHandler::doUSBDriveMount() { QString device = ""; if (usbSeek(device)) { if (! umounted()) { usbMount(device); } else { usbUmount(_usbMount); // release qDebug() << tr("USB drive umounted"); } } else { usbRemove(); } } bool FileHandler::doWrite(const QString &vFileName, const QString &vContent, bool vAppend) { bool ok = write(vFileName, vContent, vAppend); if (ok) { emit didWrite(vFileName); } return ok; } bool FileHandler::doRead(const QString &vFileName) { QString mContent; bool ok = read(vFileName, mContent); if (ok) { emit didRead(vFileName, mContent); } return ok; } bool FileHandler::doExportFolder(const QString &vFolder) { if ( _mounted ) { QString cp = "cp"; QStringList arguments; arguments << "-r" << vFolder << _usbMount; _processCopyFolder.start(cp, arguments); return true; } return false; } void FileHandler::usbError(const QString &vDevice) { switch (errno) { case EBUSY: qDebug() << tr("%1 - Device or resource busy (%2)").arg(errno).arg(vDevice); _mounted = true; 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 = true; qDebug() << tr("USB flash drive %1 has been mounted on %2").arg(vDevice).arg(_usbMount); emit didUSBDriveMount(); } else { usbError(vDevice); } return ok; } bool FileHandler::usbUmount(const QString &vDevice) { bool ok; ok = umount(vDevice.toLatin1().constData()) == 0; if (ok) { _mounted = false; emit didUSBDriveUmount(); } else { // the error is irrelevant, commented out for now //usbError(vDevice); } return ok; } void FileHandler::usbRemove() { usbUmount(_usbMount); umounted(false); emit didUSBDriveRemove(); qDebug() << tr("USB drive removed"); } void FileHandler::onMainTimerTimeout() { doUSBDriveMount(); }