Index: sources/storage/filehandler.cpp =================================================================== diff -u -r62efc6d8ead9b39b47859fdc9c0661f30b5941d0 -rc6a09899d2e46dc0bda5a6b994aa257953626f97 --- sources/storage/filehandler.cpp (.../filehandler.cpp) (revision 62efc6d8ead9b39b47859fdc9c0661f30b5941d0) +++ sources/storage/filehandler.cpp (.../filehandler.cpp) (revision c6a09899d2e46dc0bda5a6b994aa257953626f97) @@ -14,32 +14,83 @@ #include "filehandler.h" //Qt -#include #include +#include +#include +#include // Project +#include "storageglobals.h" +#include "logger.h" +#include "threads.h" +// namespace using namespace Storage; -FileHandler::FileHandler(QObject *parent) : QObject(parent) -{ - connect(&fsWatcher, SIGNAL(directoryChanged(QString)), - this , SLOT(directoryChanged(QString))); -} -bool FileHandler::init() +/*! + * \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) { - fsWatcher.addPath("/dev/"); + 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; } -void FileHandler::directoryChanged(const QString &vPath) +/*! + * \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) { - Q_UNUSED(vPath) - // qDebug() << QFileInfo::exists("/dev/sda"); + 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; } -bool FileHandler::mountUsb() +/*! + * \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 ) { - return true; + QString cp = "cp"; + QStringList arguments; + arguments << "-r" << vSource << vDestination; + int result = QProcess::execute(cp, arguments); + + cp = "sync;sync;sync;"; + arguments.clear(); + QProcess::execute(cp, arguments); + + return result; }