Yes, this is what I thought... First, I tried creating and exec()-ing QEventLoop. It worked, but I did not like it, partially because I was not sure how exactly it worked and how expensive it was... The QT docs for QEventLoop are not very good I am afraid
I think that the solution I have right now is slightly better (it does not interfere with the main even loop), but I may be wrong. Here it is (maybe someone else will find it helpful, the timeout parameter is currently ignored):
//
// fetches the file data over http and writes it to the device; returns true if succeeds and false if an error is encountered or the http request is timed-out.
//
bool HttpFileFetcher::FetchFile(const QString* url, QIODevice* device, UINT32 timeout) {
bool rval = IS_TRUE(url != NULL && device != NULL);
if (rval) {
QUrl qUrl(*url);
rval = IS_TRUE(qUrl.isValid());
if (rval) {
setHost(qUrl.host());
requestId_ = get(*url, device);
while(currentId() != 0) {
qApp->processEvents();
}
rval = !error_;
}
}
return rval;
}
//
// called when the http request is done.
//
void HttpFileFetcher::RequestFinishedSlot(int requestId, bool error) {
if (requestId_ == requestId) {
if (error) {
error_ = error;
Log(MessageFilter::ALERT1) << L"failed to fetch an http resource: " << errorString().toAscii().data() << Log::endl;
}
}
}
//
// fetches the file data over http and writes it to the device; returns true if succeeds and false if an error is encountered or the http request is timed-out.
//
bool HttpFileFetcher::Fetch(const QString* url, QIODevice* device, UINT32 timeout) {
HttpFileFetcher fileFetcher;
return fileFetcher.FetchFile(url, device, timeout);
}
//
// fetches the file data over http and writes it to the buffer; returns true if succeeds and false if an error is encountered or the http request is timed-out.
//
bool HttpFileFetcher::Fetch(const QString* url, QByteArray* buffer, UINT32 timeout) {
bool rval = IS_TRUE(url != NULL && buffer != NULL);
if (rval) {
QBuffer device(buffer);
device.open(QIODevice::WriteOnly);
rval = IS_TRUE(device.isOpen());
if (rval) {
HttpFileFetcher fileFetcher;
rval = fileFetcher.FetchFile(url, &device, timeout);
}
}
return rval;
}





Reply With Quote



Bookmarks