1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
DirectoryWatcher::DirectoryWatcher(QWidget* parent) : QWidget(parent)
{
lockSend = false;
}
void DirectoryWatcher::directoryChanged(const QString& str) {
directoryLastChanged = str;
QByteArray byteArray = str.toUtf8();
const char* cString = byteArray.constData();
sendChangedFiles(cString);
}
void DirectoryWatcher::sendChangedFiles(const char* path) {
DIR *dir;
struct dirent *ent;
if ((dir = opendir (path)) != NULL) {
QString str;
while ((ent = readdir (dir)) != NULL) {
str = QString("%1/%2").arg(path, ent->d_name);
QFileInfo info(str);
if (lockSend == false && (info.completeSuffix() == "xml" || info.completeSuffix() == "XML") && info.baseName() != "" && !info.baseName().startsWith("REDM") && !info.baseName().startsWith("REFT")) {
// reset the counter.
this->resendCounter = 0;
sendFileAndAccept(str.toUtf8().constData());
}
}
closedir (dir);
} else {
qDebug() << "Could not open directory" << endl;
}
}
class QNetworkRequest;
class QNetworkReply;
void DirectoryWatcher::sendFileAndAccept(const char* path) {
// increment the resend counter
this->resendCounter++;
QFileInfo fileInfo(path);
QNetworkAccessManager * mgr = new QNetworkAccessManager(this);
connect(mgr,SIGNAL(finished(QNetworkReply*)),this,SLOT(saveResponse(QNetworkReply*)));
connect(mgr,SIGNAL(finished(QNetworkReply*)),mgr,SLOT(deleteLater())); // @todo delete later
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/xml")); // @todo test
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"someFile\"; filename=\"" + fileInfo.baseName() + ".xml\""));
currentFileSent = fileInfo.baseName();
QFile *file = new QFile(path);
file->open(QIODevice::ReadOnly);
filePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(filePart);
// POST request
QNetworkReply *reply = mgr->post(QNetworkRequest(QUrl(XXXXXX)), multiPart);
multiPart->setParent(reply); // delete the multiPart with the reply
// lock
lockSend = true;
}
void DirectoryWatcher::saveResponse(QNetworkReply *rep) {
// get the response
QByteArray bts = rep->readAll();
QString str(bts);
// compute new path
QString partName = currentFileSent.mid(1, currentFileSent.length());
QString newPath = QString("%1/A%2.xml").arg(directoryLastChanged, partName);
qDebug() << "new path: " << newPath << endl;
switch (rep->error()) {
case QNetworkReply::NoError: {
qDebug() << "NO ERROR" << endl;
// save response to a file.
QFile file(newPath);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out << str;
file.close();
break;
}
default:
// case QNetworkReply::TimeoutError :
// case QNetworkReply::HostNotFoundError :
qDebug() << "NETWORK REPLY ERROR" << endl;
// resend the file if the counter is < 10
if (this->resendCounter < 5) {
// delay by n sec
QTime dieTime = QTime::currentTime().addSecs(1);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
sendFileAndAccept(this->lastPathSent.toStdString().c_str());
} else {
// after 10 attempts, we're probably sure that the network is down
// save the file somewhere and generate a default one to prevent timeouts.
qDebug() << "Saving file for later..." << endl;
if (!saveFileForLater(lastPathSent.toStdString().c_str())) {
qDebug() << "ERROR SAVING FILE, CHECK IF FOLDER EXISTS AND THE PERMISSIONS." << endl;
}
// generate a default one to prevent timeouts.
qDebug() << "Generate a default file..." << endl;
// ...
}
break;
}
// unlock
lockSend = false;
rep->deleteLater(); // prevent memory leak
}
bool DirectoryWatcher::saveFileForLater(const char* pathToRequestFile) {
QFile file(pathToRequestFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "readonly and text" << endl;
return false;
}
QString path(pathToRequestFile);
QFileInfo fileinfo(path);
QString newPath = "C:\\data\\offline\\" + fileinfo.fileName();
return file.copy(newPath);
}
|