Index: sources/storage/filehandler.cpp =================================================================== diff -u -rfbeafa0714f065bce0403e2e8ce68f6d8fbea6bd -r5963f00ffd2c557d3ae06a5deea05032a3a3bd68 --- sources/storage/filehandler.cpp (.../filehandler.cpp) (revision fbeafa0714f065bce0403e2e8ce68f6d8fbea6bd) +++ sources/storage/filehandler.cpp (.../filehandler.cpp) (revision 5963f00ffd2c557d3ae06a5deea05032a3a3bd68) @@ -21,6 +21,7 @@ //Qt #include #include +#include // Project #include "maintimer.h" @@ -49,10 +50,16 @@ 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) { @@ -68,6 +75,34 @@ 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 = ""; @@ -83,11 +118,43 @@ } } +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); @@ -101,7 +168,7 @@ _usbDrive = vDevice.toLatin1().constData(); ok = mount(_usbDrive, _usbMount, _usbfsys, 0, "") == 0; if (ok) { - _mounted = false; + _mounted = true; qDebug() << tr("USB flash drive %1 has been mounted on %2").arg(vDevice).arg(_usbMount); emit didUSBDriveMount(); } else { @@ -115,6 +182,7 @@ bool ok; ok = umount(vDevice.toLatin1().constData()) == 0; if (ok) { + _mounted = false; emit didUSBDriveUmount(); } else { // the error is irrelevant, commented out for now