#include "httpwindow.h"
extern CReadImagesDatabase d;
HttpWindow
::HttpWindow(QWidget *parent,
const char * Dir,
const char * Dir2,
const char *Url,
const char *Subdir
)/* typical values:
dir (const char *): ....../images_data/
dir2 (const char *): ......./images_data/tmp/
url (QUrl): [url]http://localhost:8000/[/url]
subdir / subdir of images.csv and nbytes.txt on the server / (QString): ./
*/
our_parent = parent;
dir = Dir;
dir2 = Dir2;
subdir = Subdir;
user_abort = false;
httpRequestAborted = false;
reqId.insert(http->setHost(url->host(), url->port() != -1 ? url->port() : 80), SET_HOST);
if (!url->userName().isEmpty())
reqId.insert(http->setUser(url->userName(), url->password()), SET_USER);
progressBar
->setMinimumWidth
(progressBar
->fontMetrics
().
width(QLatin1String("X")) * MAX_WIDTH
);
connect(http, SIGNAL(requestFinished(int, bool)),
this, SLOT(httpRequestFinished(int, bool)));
connect(quitButton, SIGNAL(clicked()),
this, SLOT(cancelDownload()));
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(progressBar);
mainLayout->addWidget(quitButton, 0, Qt::AlignRight);
setLayout(mainLayout);
setWindowTitle(tr("Updating Isis Images"));
}
void HttpWindow::updateArchive() {
note(0, tr("compare byte count of .csv files"));
// no error message, since it is OK to work from a read-only media
delete file; file = 0;
return;
}
user_abort = false;
httpRequestAborted = false;
reqId.insert(http->get(url->path() + subdir, file), GET_N_BYTES);
}
void HttpWindow::httpRequestFinished(int reqId_, bool error) {
if (user_abort) {
if (file) { file->remove(); delete file; file = 0; }
myQuit();
}
qint64 i, tmpi = 0;
bool index_in_tmp;
QList<QString>::iterator j;
QList<QString> new_list;
CReadImagesDatabase e;
switch (reqId.value(reqId_)) {
case SET_HOST: case SET_USER:
break;
case GET_N_BYTES:
// ...
break;
case GET_NEW_INDEX:
// ...
break;
default: // 0, 1, ... - get fixed image; GET_N_BYTES, etc. are negative values
if (reqId.value(reqId_) >= 0)
// ...
else
// ...
} // end of switch
}
void HttpWindow::myQuit() {
// ...
}
void HttpWindow::cancelDownload() {
// ...
}
// ...
}
void HttpWindow::updateDataReadProgress(int bytesRead, int totalBytes) {
// ...
}
// ...
}
#include "httpwindow.h"
extern CReadImagesDatabase d;
extern QTimer *timer;
HttpWindow::HttpWindow(QWidget *parent, const char * Dir, const char * Dir2, const char *Url, const char *Subdir)
: QWidget(parent) {
/* typical values:
dir (const char *): ....../images_data/
dir2 (const char *): ......./images_data/tmp/
url (QUrl): [url]http://localhost:8000/[/url]
subdir / subdir of images.csv and nbytes.txt on the server / (QString): ./
*/
our_parent = parent;
dir = Dir;
dir2 = Dir2;
subdir = Subdir;
http = new QHttp;
url = new QUrl(QString(Url), QUrl::TolerantMode);
user_abort = false;
httpRequestAborted = false;
reqId.insert(http->setHost(url->host(), url->port() != -1 ? url->port() : 80), SET_HOST);
if (!url->userName().isEmpty())
reqId.insert(http->setUser(url->userName(), url->password()), SET_USER);
progressBar = new QProgressBar;
progressBar->setMinimumWidth(progressBar->fontMetrics().width(QLatin1String("X")) * MAX_WIDTH);
statusLabel = new QLabel;
quitButton = new QPushButton(tr("&Quit"));
connect(http, SIGNAL(requestFinished(int, bool)),
this, SLOT(httpRequestFinished(int, bool)));
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
connect(quitButton, SIGNAL(clicked()),
this, SLOT(cancelDownload()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(progressBar);
mainLayout->addWidget(quitButton, 0, Qt::AlignRight);
setLayout(mainLayout);
setWindowTitle(tr("Updating Isis Images"));
}
void HttpWindow::updateArchive() {
note(0, tr("compare byte count of .csv files"));
file = new QFile(dir2 + QString("nbytes.txt"));
if (!file->open(QIODevice::ReadWrite)) {
// no error message, since it is OK to work from a read-only media
delete file; file = 0;
return;
}
user_abort = false;
httpRequestAborted = false;
reqId.insert(http->get(url->path() + subdir, file), GET_N_BYTES);
}
void HttpWindow::httpRequestFinished(int reqId_, bool error) {
if (user_abort) {
if (file) { file->remove(); delete file; file = 0; }
myQuit();
}
qint64 i, tmpi = 0;
bool index_in_tmp;
QList<QString>::iterator j;
QList<QString> new_list;
CReadImagesDatabase e;
switch (reqId.value(reqId_)) {
case SET_HOST: case SET_USER:
break;
case GET_N_BYTES:
// ...
break;
case GET_NEW_INDEX:
// ...
break;
default: // 0, 1, ... - get fixed image; GET_N_BYTES, etc. are negative values
if (reqId.value(reqId_) >= 0)
// ...
else
// ...
} // end of switch
}
void HttpWindow::myQuit() {
// ...
}
void HttpWindow::cancelDownload() {
// ...
}
void HttpWindow::readResponseHeader(const QHttpResponseHeader &responseHeader) {
// ...
}
void HttpWindow::updateDataReadProgress(int bytesRead, int totalBytes) {
// ...
}
void HttpWindow::note(QString s, QString t) {
// ...
}
To copy to clipboard, switch view to plain text mode
and the header:
#ifndef HTTPWINDOW_H
#define HTTPWINDOW_H
#include <QtGui>
#include <QtNetwork>
#include <QtAlgorithms>
#include <stdio.h>
#include <stdlib.h>
#include "ReadImagesDatabase.h"
// these consts must not be >= 0
#define SET_HOST -1
#define SET_USER -2
#define GET_N_BYTES -3
#define GET_NEW_INDEX -4
#define MAX_WIDTH 30
#define DEBUG
{
Q_OBJECT
public:
HttpWindow
(QWidget *parent,
const char *Dir,
const char *Dir2,
const char *Url,
const char *Subdir
);
public slots:
void updateArchive();
private slots:
void cancelDownload();
void httpRequestFinished(int requestId, bool error);
void updateDataReadProgress(int bytesRead, int totalBytes);
private:
bool user_abort;
void myQuit();
const char *dir, *dir2;
QMap<int, int> reqId;
bool httpRequestAborted;
};
#endif
#ifndef HTTPWINDOW_H
#define HTTPWINDOW_H
#include <QtGui>
#include <QtNetwork>
#include <QtAlgorithms>
#include <stdio.h>
#include <stdlib.h>
#include "ReadImagesDatabase.h"
// these consts must not be >= 0
#define SET_HOST -1
#define SET_USER -2
#define GET_N_BYTES -3
#define GET_NEW_INDEX -4
#define MAX_WIDTH 30
#define DEBUG
class HttpWindow : public QWidget
{
Q_OBJECT
public:
HttpWindow(QWidget *parent, const char *Dir, const char *Dir2, const char *Url, const char *Subdir);
public slots:
void updateArchive();
private slots:
void cancelDownload();
void httpRequestFinished(int requestId, bool error);
void readResponseHeader(const QHttpResponseHeader &responseHeader);
void updateDataReadProgress(int bytesRead, int totalBytes);
private:
void note(QString, QString);
QWidget *our_parent;
bool user_abort;
QString subdir;
void downloadFile(QString);
void myQuit();
QProgressBar *progressBar;
QPushButton *quitButton;
QLabel *statusLabel;
QHttp *http;
QFile *file, *file1;
QUrl *url;
const char *dir, *dir2;
QMap<int, int> reqId;
bool httpRequestAborted;
};
#endif
To copy to clipboard, switch view to plain text mode
Bookmarks