QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsserverplugins.cpp
Go to the documentation of this file.
1/***************************************************************************
2 QgsServerPlugins.cpp
3 -------------------------
4 begin : August 28, 2014
5 copyright : (C) 2014 by Alessandro Pasotti - ItOpen
6 email : apasotti at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
19#include "qgsserverplugins.h"
20#include "qgsapplication.h"
21#include "qgslogger.h"
22#include "qgspythonutils.h"
23
24#include <QLibrary>
25
26
27// Initialize static members
28QgsPythonUtils *QgsServerPlugins::sPythonUtils;
29
30// Construct on first use
32{
33 static QStringList *pluginList = new QStringList();
34 return *pluginList;
35}
36
37
38// This code is mainly borrowed from QGIS desktop Python plugin initialization
40{
41 QString pythonlibName( QStringLiteral( "qgispython" ) );
42#if defined(Q_OS_UNIX)
43 pythonlibName.prepend( QgsApplication::libraryPath() );
44#endif
45#ifdef __MINGW32__
46 pythonlibName.prepend( "lib" );
47#endif
48 const QString version = QStringLiteral( "%1.%2.%3" ).arg( Qgis::versionInt() / 10000 ).arg( Qgis::versionInt() / 100 % 100 ).arg( Qgis::versionInt() % 100 );
49 QgsMessageLog::logMessage( QStringLiteral( "load library %1 (%2)" ).arg( pythonlibName, version ), __FILE__, Qgis::MessageLevel::Info );
50 QLibrary pythonlib( pythonlibName, version );
51 // It's necessary to set these two load hints, otherwise Python library won't work correctly
52 // see http://lists.kde.org/?l=pykde&m=117190116820758&w=2
53 pythonlib.setLoadHints( QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint );
54 if ( !pythonlib.load() )
55 {
56 pythonlib.setFileName( pythonlibName );
57 if ( !pythonlib.load() )
58 {
59 QgsMessageLog::logMessage( QStringLiteral( "Couldn't load Python support library: %1" ).arg( pythonlib.errorString() ) );
60 return false;
61 }
62 }
63
64 QgsMessageLog::logMessage( QStringLiteral( "Python support library loaded successfully." ), __FILE__, Qgis::MessageLevel::Info );
65 typedef QgsPythonUtils*( *inst )();
66 inst pythonlib_inst = ( inst ) cast_to_fptr( pythonlib.resolve( "instance" ) );
67 if ( !pythonlib_inst )
68 {
69 //using stderr on purpose because we want end users to see this [TS]
70 QgsDebugMsg( QStringLiteral( "Couldn't resolve python support library's instance() symbol." ) );
71 return false;
72 }
73
74 QgsDebugMsg( QStringLiteral( "Python support library's instance() symbol resolved." ) );
75 sPythonUtils = pythonlib_inst();
76 sPythonUtils->initServerPython( interface );
77
78 if ( sPythonUtils && sPythonUtils->isEnabled() )
79 {
80 QgsDebugMsg( QStringLiteral( "Python support ENABLED :-)" ) );
81 }
82 else
83 {
84 QgsDebugMsg( QStringLiteral( "Python support FAILED :-(" ) );
85 return false;
86 }
87
88 //Init plugins: loads a list of installed plugins and filter them
89 //for "server" metadata
90 bool atLeastOneEnabled = false;
91
92 const auto constPluginList( sPythonUtils->pluginList() );
93 for ( const QString &pluginName : constPluginList )
94 {
95 const QString pluginService = sPythonUtils->getPluginMetadata( pluginName, QStringLiteral( "server" ) );
96 if ( pluginService == QLatin1String( "True" ) )
97 {
98 if ( sPythonUtils->loadPlugin( pluginName ) )
99 {
100 if ( sPythonUtils->startServerPlugin( pluginName ) )
101 {
102 atLeastOneEnabled = true;
103 serverPlugins().append( pluginName );
104 QgsMessageLog::logMessage( QStringLiteral( "Server plugin %1 loaded!" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
105 }
106 else
107 {
108 QgsMessageLog::logMessage( QStringLiteral( "Error loading server plugin %1" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::MessageLevel::Critical );
109 }
110 }
111 else
112 {
113 QgsMessageLog::logMessage( QStringLiteral( "Error starting server plugin %1" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::MessageLevel::Critical );
114 }
115 }
116 }
117 return sPythonUtils && sPythonUtils->isEnabled() && atLeastOneEnabled;
118}
119
120
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.cpp:282
static QString libraryPath()
Returns the path containing qgis_core, qgis_gui, qgispython (and other) libraries.
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).
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins.
static QStringList & serverPlugins()
List of available server plugin names.
static QgsPythonUtils * sPythonUtils
Pointer to QgsPythonUtils.
static bool initPlugins(QgsServerInterface *interface)
Initializes the Python plugins.
#define cast_to_fptr(f)
Definition: qgis.h:3381
#define QgsDebugMsg(str)
Definition: qgslogger.h:38