Skip to content

Commit

Permalink
QgsHelp is not singletone
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Jan 10, 2017
1 parent 4ce3bd2 commit 17905ca
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 51 deletions.
8 changes: 8 additions & 0 deletions src/core/qgsapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "gps/qgsgpsconnectionregistry.h"
#include "qgspluginlayerregistry.h"
#include "qgsmessagelog.h"
#include "qgshelp.h"

#include <QDir>
#include <QFile>
Expand Down Expand Up @@ -146,6 +147,7 @@ QgsApplication::QgsApplication( int & argc, char ** argv, bool GUIenabled, const
mRasterRendererRegistry = new QgsRasterRendererRegistry();
mGpsConnectionRegistry = new QgsGPSConnectionRegistry();
mPluginLayerRegistry = new QgsPluginLayerRegistry();
mHelpViewer = new QgsHelp();

init( customConfigPath ); // init can also be called directly by e.g. unit tests that don't inherit QApplication.
}
Expand Down Expand Up @@ -291,6 +293,7 @@ QgsApplication::~QgsApplication()
delete mDataItemProviderRegistry;
delete mProfiler;
delete mMessageLog;
delete mHelpViewer;
}

QgsApplication* QgsApplication::instance()
Expand Down Expand Up @@ -1597,3 +1600,8 @@ QgsFieldFormatterRegistry* QgsApplication::fieldFormatterRegistry()
{
return instance()->mFieldFormatterRegistry;
}

QgsHelp* QgsApplication::helpViewer()
{
return instance()->mHelpViewer;
}
11 changes: 11 additions & 0 deletions src/core/qgsapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class QgsGPSConnectionRegistry;
class QgsDataItemProviderRegistry;
class QgsPluginLayerRegistry;
class QgsMessageLog;
class QgsHelp;

/** \ingroup core
* Extends QApplication to provide access to QGIS specific resources such
Expand Down Expand Up @@ -449,6 +450,15 @@ class CORE_EXPORT QgsApplication : public QApplication
*/
static QgsMessageLog* messageLog();

/**
* Returns the application's help viewer, used for displaying
* help topic for the given key
* @note added in QGIS 3.0
*/
static QgsHelp* helpViewer();



#ifdef ANDROID
//dummy method to workaround sip generation issue issue
bool x11EventFilter( XEvent * event )
Expand Down Expand Up @@ -582,6 +592,7 @@ class CORE_EXPORT QgsApplication : public QApplication
QgsActionScopeRegistry* mActionScopeRegistry;
QgsRuntimeProfiler* mProfiler;
QgsTaskManager* mTaskManager;
QgsHelp* mHelpViewer;
QgsFieldFormatterRegistry* mFieldFormatterRegistry;
QgsColorSchemeRegistry* mColorSchemeRegistry;
QgsPaintEffectRegistry* mPaintEffectRegistry;
Expand Down
69 changes: 28 additions & 41 deletions src/core/qgshelp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,26 @@
#include "qgis.h"
#include "qgsapplication.h"

QgsHelp *QgsHelp::sHelp = nullptr; // Singleton instance

void QgsHelp::openHelp( const QString& key )
QgsHelp::QgsHelp( QObject* parent )
: QObject( parent )
{
if ( !sHelp )
{
sHelp = new QgsHelp();
}
}

QDesktopServices::openUrl( sHelp->helpUrl( key ) );
QgsHelp::~QgsHelp()
{
}

QUrl QgsHelp::helpUrl( const QString& key )
void QgsHelp::openHelp( const QString& key ) const
{
if ( !sHelp )
{
sHelp = new QgsHelp();
}
QDesktopServices::openUrl( helpUrl( key ) );
}

QSettings settings;
QUrl QgsHelp::helpUrl( const QString& key ) const
{
QUrl helpNotFound = QUrl::fromLocalFile( QgsApplication::pkgDataPath() + "/doc/nohelp.html" );

QString paths = settings.value( QStringLiteral( "help/helpSearchPath" ), "" ).toString();
QSettings settings;
QStringList paths = settings.value( QStringLiteral( "help/helpSearchPath" ) ).toStringList();
if ( paths.isEmpty() )
{
return helpNotFound;
Expand All @@ -57,7 +54,7 @@ QUrl QgsHelp::helpUrl( const QString& key )
bool overrideLocale = settings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool();
if ( overrideLocale )
{
qgisLocale = settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString();
qgisLocale = settings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString();
}
else
{
Expand All @@ -77,60 +74,50 @@ QUrl QgsHelp::helpUrl( const QString& key )

QString suffix = QStringLiteral( "%1/%2/docs/user_manual/%3" ).arg( qgisVersion ).arg( qgisLocale ).arg( key );

QUrl myUrl;
QUrl helpUrl;
QString helpPath;
bool helpFound = false;

QStringList pathList = paths.split( '|' );
QStringList::const_iterator pathIt = pathList.constBegin();
for ( ; pathIt != pathList.constEnd(); ++pathIt )
Q_FOREACH ( const QString& path, paths )
{
helpPath = QStringLiteral( "%1/%2" ).arg( *pathIt ).arg( suffix );
helpPath = QStringLiteral( "%1/%2" ).arg( path ).arg( suffix );

if (( *pathIt ).startsWith( QStringLiteral( "http://" ) ) )
if ( path.startsWith( QStringLiteral( "http://" ) ) )
{
if ( !sHelp->urlExists( helpPath ) )
if ( !urlExists( helpPath ) )
{
continue;
}
myUrl = QUrl( helpPath );
helpUrl = QUrl( helpPath );
}
else
{
if ( !QFileInfo( helpPath.mid( 0, helpPath.lastIndexOf( "#" ) ) ).exists() )
QString filePath = helpPath.mid( 0, helpPath.lastIndexOf( "#" ) );
if ( !QFileInfo::exists( filePath ) )
{
continue;
}
myUrl = QUrl::fromLocalFile( helpPath );
myUrl.setFragment( helpPath.mid( helpPath.lastIndexOf( "#" ), -1 ) );
helpUrl = QUrl::fromLocalFile( filePath );
helpUrl.setFragment( helpPath.mid( helpPath.lastIndexOf( "#" ) + 1, -1 ) );
}

helpFound = true;
break;
}

return helpFound ? myUrl : helpNotFound;
}


QgsHelp::QgsHelp()
{
}

QgsHelp::~QgsHelp()
{
return helpFound ? helpUrl : helpNotFound;
}

bool QgsHelp::urlExists( const QString& url ) const
{
QUrl myUrl( url );
QUrl helpUrl( url );
QTcpSocket socket;

socket.connectToHost( myUrl.host(), 80 );
socket.connectToHost( helpUrl.host(), 80 );
if ( socket.waitForConnected() )
{
socket.write( "HEAD " + myUrl.path().toUtf8() + " HTTP/1.1\r\n"
"Host: " + myUrl.host().toUtf8() + "\r\n\r\n" );
socket.write( "HEAD " + helpUrl.path().toUtf8() + " HTTP/1.1\r\n"
"Host: " + helpUrl.host().toUtf8() + "\r\n\r\n" );
if ( socket.waitForReadyRead() )
{
QByteArray bytes = socket.readAll();
Expand Down
26 changes: 16 additions & 10 deletions src/core/qgshelp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#include <QObject>

/** \ingroup core
* @brief The class provides help URI for the given key.
* \class QgsHelp
* @brief Helper class for showing help topic URI for the given key.
*
* Help can be stored online, on the local directory or on the intranet
* server. Location of the QGIS help can be configured in QGIS options.
Expand All @@ -33,41 +34,46 @@
* If no help found, default error page with information how to setup
* help system will be shown.
*
* This class can be created directly, or accessed via
* QgsApplication::helpViewer().
*
* @note added in QGIS 3.0
*/
class CORE_EXPORT QgsHelp : public QObject
{
Q_OBJECT

public:

/** Constructor for QgsHelp.
* @param parent parent QObject
*/
QgsHelp( QObject* parent = nullptr );

virtual ~QgsHelp();

/** Opens help topic for the given help key using default system
* web browser. If help topic not found, builtin error page shown.
* @param key key which identified help topic
* @note added in QGIS 3.0
*/
static void openHelp( const QString& key );
void openHelp( const QString& key ) const;

/** Returns URI of the help topic for the given key. If help topic
* not found, URI of the builtin error page returned.
* @param key key which identified help topic
* @note added in QGIS 3.0
*/
static QUrl helpUrl( const QString& key );
QUrl helpUrl( const QString& key ) const;

private:
//! Constructor
QgsHelp();

//! Destructor
~QgsHelp();

/** Check if given URL accessible by issuing HTTP HEAD request.
* Returns true if URL accessible, false otherwise.
* @param url URL to check
* @note added in QGIS 3.0
*/
bool urlExists( const QString& url ) const;

static QgsHelp* sHelp;
};

#endif // QGSHELP_H

0 comments on commit 17905ca

Please sign in to comment.