QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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 QgsDebugError( QStringLiteral( "Couldn't resolve python support library's instance() symbol." ) );
71 return false;
72 }
73
74 QgsDebugMsgLevel( QStringLiteral( "Python support library's instance() symbol resolved." ), 2 );
75 sPythonUtils = pythonlib_inst();
76 if ( sPythonUtils )
77 {
78 sPythonUtils->initServerPython( interface );
79 }
80
81 if ( sPythonUtils && sPythonUtils->isEnabled() )
82 {
83 QgsDebugMsgLevel( QStringLiteral( "Python support ENABLED :-)" ), 2 );
84 }
85 else
86 {
87 QgsDebugError( QStringLiteral( "Python support FAILED :-(" ) );
88 return false;
89 }
90
91 //Init plugins: loads a list of installed plugins and filter them
92 //for "server" metadata
93 bool atLeastOneEnabled = false;
94
95 const auto constPluginList( sPythonUtils->pluginList() );
96 for ( const QString &pluginName : constPluginList )
97 {
98 const QString pluginService = sPythonUtils->getPluginMetadata( pluginName, QStringLiteral( "server" ) );
99 if ( pluginService == QLatin1String( "True" ) )
100 {
101 if ( sPythonUtils->loadPlugin( pluginName ) )
102 {
103 if ( sPythonUtils->startServerPlugin( pluginName ) )
104 {
105 atLeastOneEnabled = true;
106 serverPlugins().append( pluginName );
107 QgsMessageLog::logMessage( QStringLiteral( "Server plugin %1 loaded!" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
108 }
109 else
110 {
111 QgsMessageLog::logMessage( QStringLiteral( "Error loading server plugin %1" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::MessageLevel::Critical );
112 }
113 }
114 else
115 {
116 QgsMessageLog::logMessage( QStringLiteral( "Error starting server plugin %1" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::MessageLevel::Critical );
117 }
118 }
119 }
120 return sPythonUtils && sPythonUtils->isEnabled() && atLeastOneEnabled;
121}
122
123
@ Critical
Critical/error message.
Definition qgis.h:157
@ Info
Information message.
Definition qgis.h:155
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
Definition qgis.cpp:264
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:5810
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38