|
| 1 | +/*************************************************************************** |
| 2 | + qgshelp.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : December 2016 |
| 5 | + Copyright : (C) 2016 by Alexander Bruy |
| 6 | + Email : alexander dot bruy at gmail dot com |
| 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 "qgshelp.h" |
| 17 | + |
| 18 | +#include <QSettings> |
| 19 | +#include <QLocale> |
| 20 | +#include <QUrl> |
| 21 | +#include <QFileInfo> |
| 22 | +#include <QTcpSocket> |
| 23 | +#include <QDesktopServices> |
| 24 | + |
| 25 | +#include "qgis.h" |
| 26 | +#include "qgsapplication.h" |
| 27 | + |
| 28 | +QgsHelp *QgsHelp::sHelp = nullptr; // Singleton instance |
| 29 | + |
| 30 | +void QgsHelp::openHelp( const QString& key ) |
| 31 | +{ |
| 32 | + if ( !sHelp ) |
| 33 | + { |
| 34 | + sHelp = new QgsHelp(); |
| 35 | + } |
| 36 | + |
| 37 | + QDesktopServices::openUrl( sHelp->helpUrl( key ) ); |
| 38 | +} |
| 39 | + |
| 40 | +QUrl QgsHelp::helpUrl( const QString& key ) |
| 41 | +{ |
| 42 | + if ( !sHelp ) |
| 43 | + { |
| 44 | + sHelp = new QgsHelp(); |
| 45 | + } |
| 46 | + |
| 47 | + QSettings settings; |
| 48 | + QUrl helpNotFound = QUrl::fromLocalFile( QgsApplication::pkgDataPath() + "/doc/nohelp.html" ); |
| 49 | + |
| 50 | + QString paths = settings.value( QStringLiteral( "help/helpSearchPath" ), "" ).toString(); |
| 51 | + if ( paths.isEmpty() ) |
| 52 | + { |
| 53 | + return helpNotFound; |
| 54 | + } |
| 55 | + |
| 56 | + QString qgisLocale; |
| 57 | + bool overrideLocale = settings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool(); |
| 58 | + if ( overrideLocale ) |
| 59 | + { |
| 60 | + qgisLocale = settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString(); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + qgisLocale = QLocale::system().name().left( 2 ); |
| 65 | + } |
| 66 | + |
| 67 | + QString qgisVersion; |
| 68 | + if ( Qgis::QGIS_VERSION_INT / 100 % 100 == 99 ) |
| 69 | + { |
| 70 | + qgisVersion = QStringLiteral( "testing" ); |
| 71 | + qgisLocale = QStringLiteral( "en" ); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + qgisVersion = QStringLiteral( "%1.%2" ).arg( Qgis::QGIS_VERSION_INT / 10000 ).arg( Qgis::QGIS_VERSION_INT / 100 % 100 ); |
| 76 | + } |
| 77 | + |
| 78 | + QString suffix = QStringLiteral( "%1/%2/docs/user_manual/%3" ).arg( qgisVersion ).arg( qgisLocale ).arg( key ); |
| 79 | + |
| 80 | + QUrl myUrl; |
| 81 | + QString helpPath; |
| 82 | + bool helpFound = false; |
| 83 | + |
| 84 | + QStringList pathList = paths.split( '|' ); |
| 85 | + QStringList::const_iterator pathIt = pathList.constBegin(); |
| 86 | + for ( ; pathIt != pathList.constEnd(); ++pathIt ) |
| 87 | + { |
| 88 | + helpPath = QStringLiteral( "%1/%2" ).arg( *pathIt ).arg( suffix ); |
| 89 | + |
| 90 | + if (( *pathIt ).startsWith( QStringLiteral( "http://" ) ) ) |
| 91 | + { |
| 92 | + if ( !sHelp->urlExists( helpPath ) ) |
| 93 | + { |
| 94 | + continue; |
| 95 | + } |
| 96 | + myUrl = QUrl( helpPath ); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + if ( !QFileInfo( helpPath.mid( 0, helpPath.lastIndexOf( "#" ) ) ).exists() ) |
| 101 | + { |
| 102 | + continue; |
| 103 | + } |
| 104 | + myUrl = QUrl::fromLocalFile( helpPath ); |
| 105 | + myUrl.setFragment( helpPath.mid( helpPath.lastIndexOf( "#" ), -1 ) ); |
| 106 | + } |
| 107 | + |
| 108 | + helpFound = true; |
| 109 | + break; |
| 110 | + } |
| 111 | + |
| 112 | + return helpFound ? myUrl : helpNotFound; |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +QgsHelp::QgsHelp() |
| 117 | +{ |
| 118 | +} |
| 119 | + |
| 120 | +QgsHelp::~QgsHelp() |
| 121 | +{ |
| 122 | +} |
| 123 | + |
| 124 | +bool QgsHelp::urlExists( const QString& url ) const |
| 125 | +{ |
| 126 | + QUrl myUrl( url ); |
| 127 | + QTcpSocket socket; |
| 128 | + |
| 129 | + socket.connectToHost( myUrl.host(), 80 ); |
| 130 | + if ( socket.waitForConnected() ) |
| 131 | + { |
| 132 | + socket.write( "HEAD " + myUrl.path().toUtf8() + " HTTP/1.1\r\n" |
| 133 | + "Host: " + myUrl.host().toUtf8() + "\r\n\r\n" ); |
| 134 | + if ( socket.waitForReadyRead() ) |
| 135 | + { |
| 136 | + QByteArray bytes = socket.readAll(); |
| 137 | + if ( bytes.contains( "200 OK" ) || bytes.contains( "302 Found" ) ) |
| 138 | + { |
| 139 | + return true; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return false; |
| 145 | +} |
0 commit comments