/*! * * 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" //Qt #include #include #include // Project #include "storageglobals.h" #include "usbwatcher.h" #include "applicationcontroller.h" // namespace using namespace Storage; // Singleton SINGLETON_INIT(FileHandler) FileHandler::FileHandler(QObject *parent) : QObject(parent) { } bool FileHandler::init() { initConnections(); return true; } void FileHandler::quit() { } void FileHandler::initConnections() { connect(_ApplicationController, SIGNAL(didExportLog()), this , SLOT( onExportLog())); } 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; } bool FileHandler::onWrite(const QString &vFileName, const QString &vContent, bool vAppend) { bool ok = write(vFileName, vContent, vAppend); if (ok) { emit didWrite(vFileName); } return ok; } bool FileHandler::onRead(const QString &vFileName) { QString mContent; bool ok = read(vFileName, mContent); if (ok) { emit didRead(vFileName, mContent); } return ok; } bool FileHandler::onExportLog() { QString vSource = Storage::Log_Base_Path_Name_Location; QString vDestination = Storage::USB_Mount_Point; QString cp = "cp"; QStringList arguments; arguments << "-r" << vSource << vDestination; _processCopyFolder.start(cp, arguments); return true; }