Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[BACKPORT] port qgis version check to network access manager/qgis.org
  • Loading branch information
jef-n committed Aug 3, 2011
1 parent b86243e commit 9551745
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 101 deletions.
163 changes: 68 additions & 95 deletions src/app/qgisapp.cpp 100644 → 100755
Expand Up @@ -4675,128 +4675,101 @@ void QgisApp::loadPythonSupport()
void QgisApp::checkQgisVersion()
{
QApplication::setOverrideCursor( Qt::WaitCursor );
/* QUrlOperator op = new QUrlOperator( "http://mrcc.com/qgis/version.txt" );
connect(op, SIGNAL(data()), SLOT(urlData()));
connect(op, SIGNAL(finished(QNetworkOperation)), SLOT(urlFinished(QNetworkOperation)));

op.get(); */
mSocket = new QTcpSocket( this );
connect( mSocket, SIGNAL( connected() ), SLOT( socketConnected() ) );
connect( mSocket, SIGNAL( connectionClosed() ), SLOT( socketConnectionClosed() ) );
connect( mSocket, SIGNAL( readyRead() ), SLOT( socketReadyRead() ) );
connect( mSocket, SIGNAL( error( QAbstractSocket::SocketError ) ),
SLOT( socketError( QAbstractSocket::SocketError ) ) );
mSocket->connectToHost( "mrcc.com", 80 );
QNetworkReply *reply = QgsNetworkAccessManager::instance()->get( QNetworkRequest( QUrl( "http://qgis.org/version.txt" ) ) );
connect( reply, SIGNAL( finished() ), this, SLOT( versionReplyFinished() ) );
}

void QgisApp::socketConnected()
void QgisApp::versionReplyFinished()
{
QTextStream os( mSocket );
mVersionMessage = "";
// send the qgis version string
// os << QGIS_VERSION << "\r\n";
os << "GET /qgis/version.txt HTTP/1.0\n\n";
QApplication::restoreOverrideCursor();

QNetworkReply *reply = qobject_cast<QNetworkReply*>( sender() );
if ( !reply )
return;

}
QNetworkReply::NetworkError error = reply->error();

void QgisApp::socketConnectionClosed()
{
QApplication::restoreOverrideCursor();
// strip the header
QString contentFlag = "#QGIS Version";
int pos = mVersionMessage.indexOf( contentFlag );
if ( pos > -1 )
{
pos += contentFlag.length();
// QgsDebugMsg(mVersionMessage);
// QgsDebugMsg(QString("Pos is %1").arg(pos));
mVersionMessage = mVersionMessage.mid( pos );
QStringList parts = mVersionMessage.split( "|", QString::SkipEmptyParts );
// check the version from the server against our version
QString versionInfo;
int currentVersion = parts[0].toInt();
if ( currentVersion > QGis::QGIS_VERSION_INT )
{
// show version message from server
versionInfo = tr( "There is a new version of QGIS available" ) + "\n";
}
else
if ( error == QNetworkReply::NoError )
{
QString versionMessage = reply->readAll();
QgsDebugMsg( QString( "version message: %1" ).arg( versionMessage ) );

// strip the header
QString contentFlag = "#QGIS Version";
int pos = versionMessage.indexOf( contentFlag );
if ( pos > -1 )
{
if ( QGis::QGIS_VERSION_INT > currentVersion )
pos += contentFlag.length();
QgsDebugMsg( QString( "Pos is %1" ).arg( pos ) );

versionMessage = versionMessage.mid( pos );
QStringList parts = versionMessage.split( "|", QString::SkipEmptyParts );
// check the version from the server against our version
QString versionInfo;
int currentVersion = parts[0].toInt();
if ( currentVersion > QGis::QGIS_VERSION_INT )
{
// show version message from server
versionInfo = tr( "There is a new version of QGIS available" ) + "\n";
}
else if ( QGis::QGIS_VERSION_INT > currentVersion )
{
versionInfo = tr( "You are running a development version of QGIS" ) + "\n";
}
else
{
versionInfo = tr( "You are running the current version of QGIS" ) + "\n";
}
}
if ( parts.count() > 1 )
{
versionInfo += parts[1] + "\n\n" + tr( "Would you like more information?" );
;
QMessageBox::StandardButton result = QMessageBox::information( this,
tr( "QGIS Version Information" ), versionInfo, QMessageBox::Ok |
QMessageBox::Cancel );
if ( result == QMessageBox::Ok )

if ( parts.count() > 1 )
{
// show more info
QgsMessageViewer *mv = new QgsMessageViewer( this );
mv->setWindowTitle( tr( "QGIS - Changes since last release" ) );
mv->setMessageAsHtml( parts[2] );
mv->exec();
versionInfo += parts[1] + "\n\n" + tr( "Would you like more information?" );

QMessageBox::StandardButton result = QMessageBox::information( this,
tr( "QGIS Version Information" ), versionInfo, QMessageBox::Ok |
QMessageBox::Cancel );
if ( result == QMessageBox::Ok )
{
// show more info
QgsMessageViewer *mv = new QgsMessageViewer( this );
mv->setWindowTitle( tr( "QGIS - Changes since last release" ) );
mv->setMessageAsHtml( parts[2] );
mv->exec();
}
}
else
{
QMessageBox::information( this, tr( "QGIS Version Information" ), versionInfo );
}
}
else
{
QMessageBox::information( this, tr( "QGIS Version Information" ), versionInfo );
QMessageBox::warning( this, tr( "QGIS Version Information" ), tr( "Unable to get current version information from server" ) );
}
}
else
{
QMessageBox::warning( this, tr( "QGIS Version Information" ), tr( "Unable to get current version information from server" ) );
}
}
void QgisApp::socketError( QAbstractSocket::SocketError e )
{
if ( e == QAbstractSocket::RemoteHostClosedError )
return;

QApplication::restoreOverrideCursor();
// get error type
QString detail;
switch ( e )
{
case QAbstractSocket::ConnectionRefusedError:
detail = tr( "Connection refused - server may be down" );
break;
case QAbstractSocket::HostNotFoundError:
detail = tr( "QGIS server was not found" );
break;
case QAbstractSocket::NetworkError:
detail = tr( "Network error while communicating with server" );
break;
default:
detail = tr( "Unknown network socket error" );
break;
}

// show version message from server
QMessageBox::critical( this, tr( "QGIS Version Information" ), tr( "Unable to communicate with QGIS Version server\n%1" ).arg( detail ) );
}
// get error type
QString detail;
switch ( error )
{
case QNetworkReply::ConnectionRefusedError:
detail = tr( "Connection refused - server may be down" );
break;
case QNetworkReply::HostNotFoundError:
detail = tr( "QGIS server was not found" );
break;
default:
detail = tr( "Unknown network socket error: %1" ).arg( error );
break;
}

void QgisApp::socketReadyRead()
{
while ( mSocket->bytesAvailable() > 0 )
{
char *data = new char[mSocket->bytesAvailable() + 1];
memset( data, '\0', mSocket->bytesAvailable() + 1 );
mSocket->read( data, mSocket->bytesAvailable() );
mVersionMessage += data;
delete[]data;
// show version message from server
QMessageBox::critical( this, tr( "QGIS Version Information" ), tr( "Unable to communicate with QGIS Version server\n%1" ).arg( detail ) );
}

reply->deleteLater();
}

void QgisApp::configureShortcuts()
Expand Down
9 changes: 3 additions & 6 deletions src/app/qgisapp.h
Expand Up @@ -431,6 +431,9 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
QStringList const & styles,
QString const & format,
QString const & crs );

void versionReplyFinished();

protected:

//! Handle state changes (WindowTitleChange)
Expand Down Expand Up @@ -592,10 +595,6 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
void options();
//! Whats-this help slot
void whatsThis();
void socketConnected();
void socketConnectionClosed();
void socketReadyRead();
void socketError( QAbstractSocket::SocketError e );
//! Set project properties, including map untis
void projectProperties();
//! Open project properties dialog and show the projections tab
Expand Down Expand Up @@ -998,8 +997,6 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
QgisAppInterface *mQgisInterface;
friend class QgisAppInterface;

QTcpSocket *mSocket;
QString mVersionMessage;
QSplashScreen *mSplash;
//! list of recently opened/saved project files
QStringList mRecentProjectPaths;
Expand Down

0 comments on commit 9551745

Please sign in to comment.