PDA

View Full Version : QHttp Problem



musaulker
28th March 2007, 22:00
Hello,

I want to add checkLatestVersion functionality to my program. My program will connect foo.com/bar.file and get the latestVersion and compares with currentVersion. If latestVersion is greater then currentVersion that it will give a message (there is a new version, download it) and then quit.

Here is my code:

void VersionChecker::checkVersion()
{
versionHttp = new QHttp();
connect(versionHttp,SIGNAL(requestFinished(int,boo l)),this,SLOT(versionHttpData(int,bool)));
versionHttp>setHost("www.foo.com", 80);
versionHttpRequestId = versionHttp->get("/bar/latest.txt");
}

void VersionChecker::versionHttpData(int id, bool error)
{
if (id != versionHttpRequestId)
{
return;
}
if(error)
{
latestVersionString = "2.0.0.0";
return;
}
latestVersionString = versionHttp->readAll();
}

QString VersionChecker::getLatestVersion()
{
return latestVersionString;
}


and how i use it:


QApplication a(argc, argv);

VersionChecker *versionControl = new VersionChecker();
versionControl->checkVersion();
QString latestVersion = versionControl->getLatestVersion();

if(latestVersion != _PROGRAM_VERSION)
{
NewVersionAvailableDialog *newVersionDialog = new NewVersionAvailableDialog();
newVersionDialog->setModal(true);
newVersionDialog->setAttribute( Qt::WA_DeleteOnClose );
newVersionDialog->show();
return a.exec();
}


MainForm w;
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();

The problem is, the program runs, QHttp connects and gets the latest version but, while these are being MainWindow is shown. I dont want it to be shown. Firstly latestVersion control must be finished. If current version is the latest version, then MainWindow must shown, otherwise i want the program to quit.

How can I do that? Where's the problem?

jacek
28th March 2007, 22:21
QHttp needs time and a running event loop to fetch the results. You can try:

versionControl->checkVersion();
QCoreApplication::processEvents();
QString latestVersion = versionControl->getLatestVersion();
You can also change VersionChecker a bit and make it show either the main window or NewVersionAvailableDialog. In such case your code will look like this:
int main( int argc, char **argv )
{
QApplication app(argc, argv);

VersionChecker checker;
checker->checkVersion();

return app.exec();
}
...
void VersionChecker::runningOldVersion()
{
NewVersionAvailableDialog dlg;
dlg.exec();
}
...
void VersionChecker::runningNewestVersion()
{
MainForm *mf = new MainForm();
mf->setAttribute( Qt::WA_DeleteOnClose );
mf->show();
}
...

musaulker
28th March 2007, 23:26
Ok I solved like in your second solution. Thanks for help

musaulker
29th March 2007, 00:09
But now there exists another problem. I have a firewall. Each time after i compile my program, firewall asks me permission for accessing internet. I say yes. No problem, it works sucessfully. But when i say no, the program still tries to connect to my server. It waits xx seconds (or minutes).

1- How can I solve it?
2- Is there a timeout mechanism for QHttp?
3- What should be if no firewall but slow internet connection?

jacek
29th March 2007, 00:40
2- Is there a timeout mechanism for QHttp?
Yes, it is, but it's well hidden. ;) You can create an ordinary socket and set a timeout using setsockopt(), next you'll need to wrap it in a QTcpSocket and use QHttp::setSocket().

It would be nice, if there was some Qt-way to change socket options, but so far I haven't seen any.


1- How can I solve it?
As a workaround you can make your own timeout mechanism using QTimer.