Statistics
| Branch: | Tag: | Revision:

qgis / src / app / qgsversioninfo.cpp @ master

History | View | Annotate | Download (2.97 KB)

1
/***************************************************************************
2

3
               ----------------------------------------------------
4
              date                 : 18.8.2015
5
              copyright            : (C) 2015 by Matthias Kuhn
6
              email                : matthias (at) opengis.ch
7
 ***************************************************************************
8
 *                                                                         *
9
 *   This program is free software; you can redistribute it and/or modify  *
10
 *   it under the terms of the GNU General Public License as published by  *
11
 *   the Free Software Foundation; either version 2 of the License, or     *
12
 *   (at your option) any later version.                                   *
13
 *                                                                         *
14
 ***************************************************************************/
15

    
16
#include "qgsversioninfo.h"
17
#include "qgis.h"
18
#include "qgsapplication.h"
19
#include "qgsnetworkaccessmanager.h"
20

    
21
QgsVersionInfo::QgsVersionInfo( QObject *parent )
22
  : QObject( parent )
23
{
24

    
25
}
26

    
27
void QgsVersionInfo::checkVersion()
28
{
29
  QNetworkReply *reply = QgsNetworkAccessManager::instance()->get( QNetworkRequest( QUrl( QStringLiteral( "https://version.qgis.org/version.txt" ) ) ) );
30
  connect( reply, &QNetworkReply::finished, this, &QgsVersionInfo::versionReplyFinished );
31
}
32

    
33
bool QgsVersionInfo::newVersionAvailable() const
34
{
35
  return mLatestVersion > Qgis::versionInt();
36
}
37

    
38
bool QgsVersionInfo::isDevelopmentVersion() const
39
{
40
  return Qgis::versionInt() > mLatestVersion;
41
}
42

    
43
void QgsVersionInfo::versionReplyFinished()
44
{
45
  QNetworkReply *reply = qobject_cast<QNetworkReply *>( sender() );
46
  Q_ASSERT( reply );
47

    
48
  mError = reply->error();
49
  mErrorString = reply->errorString();
50

    
51
  if ( mError == QNetworkReply::NoError )
52
  {
53
    QString versionMessage = reply->readAll();
54

    
55
    // strip the header
56
    QString contentFlag = QStringLiteral( "#QGIS Version" );
57
    int pos = versionMessage.indexOf( contentFlag );
58

    
59
    if ( pos > -1 )
60
    {
61
      pos += contentFlag.length();
62

    
63
      versionMessage = versionMessage.mid( pos );
64
      QStringList parts = versionMessage.split( '|', QString::SkipEmptyParts );
65
      // check the version from the  server against our version
66
      mLatestVersion = parts[0].toInt();
67
      mDownloadInfo = parts.value( 1 );
68
      mAdditionalHtml = parts.value( 2 );
69
    }
70
  }
71

    
72
  // get error type
73
  switch ( mError )
74
  {
75
    case QNetworkReply::ConnectionRefusedError:
76
      mErrorString = tr( "Connection refused - server may be down" );
77
      break;
78
    case QNetworkReply::HostNotFoundError:
79
      mErrorString = tr( "The host name %1 could not be resolved. Check your DNS settings or contact your system administrator." ).arg( reply->request().url().host() );
80
      break;
81
    case QNetworkReply::NoError:
82
      mErrorString.clear();
83
      break;
84
    default:
85
      mErrorString = reply->errorString();
86
      break;
87
  }
88

    
89
  reply->deleteLater();
90

    
91
  emit versionInfoAvailable();
92
}