/*! * * 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 #include // Project #include "storageglobals.h" #include "logger.h" #include "threads.h" // namespace using namespace Storage; /*! * \brief FileHandler::write * \details Writes the content of vContent into the file vFileName. * \param vFileName - Source file name * \param vContent - The content which is going to be written in the file. * \param vAppend - if set to true the content will be appended at the end of the file. * \return false if file can't be opened. */ 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)) { LOG_ERROR(QObject::tr("Can't open file for write (%1)").arg(vFileName)); return false; } QTextStream out(&file); out << vContent; out.flush(); return true; } /*! * \brief FileHandler::read * \details reads file vFilename content into vContent variable. * \param vFileName - Source file name * \param vContent - The content of the file which will be set when done. * \return false if file can't be opened. */ bool FileHandler::read(const QString &vFileName, QString &vContent) { QFile file(vFileName); if (! file.open(QFile::Text | QFile::ReadOnly)) { LOG_ERROR(QObject::tr("Can't open file for read (%1)").arg(vFileName)); return false; } QTextStream in(&file); vContent = in.readAll(); return true; } /*! * \brief FileHandler::copyFolder * \details Copies all the file and folders recursively. * \param vSource - The source folder * \param vDestination - The destination folder * \return Tue on successful execution. * \note This method uses the Linux "cp -r vSource vDestination" command * Not a Thread-Safe. * */ int FileHandler::copyFolder(const QString &vSource, const QString &vDestination ) { QString cp = "cp"; QStringList arguments; arguments << "-r" << vSource << vDestination; return QProcess::execute(cp, arguments); }