19 #include "qgssettings.h"
28 #include <QDesktopServices>
29 #include <QRegularExpression>
30 #include <QNetworkProxy>
31 #include <QNetworkProxyFactory>
46 QStringList paths = settings.value( QStringLiteral(
"help/helpSearchPath" ) ).toStringList();
47 if ( paths.isEmpty() )
56 QString helpPath, fullPath;
57 bool helpFound =
false;
59 const auto constPaths = paths;
60 for (
const QString &path : constPaths )
62 if ( path.endsWith( QLatin1String(
"\\" ) ) || path.endsWith( QLatin1Char(
'/' ) ) )
64 fullPath = path.left( path.size() - 1 );
71 const auto constVariableNames = scope->variableNames();
72 for (
const QString &var : constVariableNames )
74 QRegularExpression rx( QStringLiteral(
"(<!\\$\\$)*(\\$%1)" ).arg( var ) );
75 fullPath.replace( rx, scope->variable( var ).toString() );
77 fullPath.replace( QRegularExpression( QStringLiteral(
"(\\$\\$)" ) ), QStringLiteral(
"$" ) );
79 helpPath = QStringLiteral(
"%1/%2" ).arg( fullPath, key );
81 QgsMessageLog::logMessage( QObject::tr(
"Trying to open help using key '%1'. Full URI is '%2'…" ).arg( key ).arg( helpPath ), QObject::tr(
"QGIS Help" ), Qgis::MessageLevel::Info );
83 if ( helpPath.startsWith( QLatin1String(
"http" ) ) )
85 if ( !QgsHelp::urlExists( helpPath ) )
93 QString filePath = helpPath.mid( 0, helpPath.lastIndexOf( QLatin1Char(
'#' ) ) );
94 if ( !QFileInfo::exists( filePath ) )
98 helpUrl = QUrl::fromLocalFile( filePath );
99 int pos = helpPath.lastIndexOf( QLatin1Char(
'#' ) );
102 helpUrl.setFragment( helpPath.mid( helpPath.lastIndexOf( QLatin1Char(
'#' ) ) + 1, -1 ) );
110 return helpFound ?
helpUrl : helpNotFound;
113 bool QgsHelp::urlExists(
const QString &url )
118 QgsSettings settings;
119 bool proxyEnabled = settings.value( QStringLiteral(
"proxy/proxyEnabled" ),
false ).toBool();
123 QString proxyHost = settings.value( QStringLiteral(
"proxy/proxyHost" ), QString() ).toString();
124 int proxyPort = settings.value( QStringLiteral(
"proxy/proxyPort" ), QString() ).toString().toInt();
125 QString proxyUser = settings.value( QStringLiteral(
"proxy/proxyUser" ), QString() ).toString();
126 QString proxyPassword = settings.value( QStringLiteral(
"proxy/proxyPassword" ), QString() ).toString();
128 QString proxyTypeString = settings.value( QStringLiteral(
"proxy/proxyType" ), QString() ).toString();
130 if ( proxyTypeString == QLatin1String(
"DefaultProxy" ) )
132 QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery();
133 if ( !proxies.isEmpty() )
135 proxy = proxies.first();
140 QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
141 if ( proxyTypeString == QLatin1String(
"Socks5Proxy" ) )
143 proxyType = QNetworkProxy::Socks5Proxy;
145 else if ( proxyTypeString == QLatin1String(
"HttpProxy" ) )
147 proxyType = QNetworkProxy::HttpProxy;
149 else if ( proxyTypeString == QLatin1String(
"HttpCachingProxy" ) )
151 proxyType = QNetworkProxy::HttpCachingProxy;
153 else if ( proxyTypeString == QLatin1String(
"FtpCachingProxy" ) )
155 proxyType = QNetworkProxy::FtpCachingProxy;
157 proxy = QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
159 socket.setProxy( proxy );
162 socket.connectToHost(
helpUrl.host(), 80 );
163 if ( socket.waitForConnected() )
165 socket.write(
"HEAD " +
helpUrl.path().toUtf8() +
" HTTP/1.1\r\n"
166 "Host: " +
helpUrl.host().toUtf8() +
"\r\n\r\n" );
167 if ( socket.waitForReadyRead() )
169 QByteArray bytes = socket.readAll();
170 if ( bytes.contains(
"200 OK" ) || bytes.contains(
"302 Found" ) || bytes.contains(
"301 Moved" ) )
static QString pkgDataPath()
Returns the common root path of all application data directories.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
static QUrl helpUrl(const QString &key)
Returns URI of the help topic for the given key.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).