QGIS API Documentation 3.99.0-Master (d270888f95f)
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
21#include "qgsapplication.h"
22#include "qgslogger.h"
23#include "qgspythonutils.h"
24
25#include <QLibrary>
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
30// Initialize static members
31QgsPythonUtils *QgsServerPlugins::sPythonUtils;
32
33// Construct on first use
35{
36 static QStringList *pluginList = new QStringList();
37 return *pluginList;
38}
39
40
41// This code is mainly borrowed from QGIS desktop Python plugin initialization
43{
44 QString pythonlibName( u"qgispython"_s );
45#if defined( Q_OS_UNIX )
46 pythonlibName.prepend( QgsApplication::libraryPath() );
47#endif
48#ifdef __MINGW32__
49 pythonlibName.prepend( "lib" );
50#endif
51 const QString version = u"%1.%2.%3"_s.arg( Qgis::versionInt() / 10000 ).arg( Qgis::versionInt() / 100 % 100 ).arg( Qgis::versionInt() % 100 );
52 QgsMessageLog::logMessage( u"load library %1 (%2)"_s.arg( pythonlibName, version ), __FILE__, Qgis::MessageLevel::Info );
53 QLibrary pythonlib( pythonlibName, version );
54 // It's necessary to set these two load hints, otherwise Python library won't work correctly
55 // see http://lists.kde.org/?l=pykde&m=117190116820758&w=2
56 pythonlib.setLoadHints( QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint );
57 if ( !pythonlib.load() )
58 {
59 pythonlib.setFileName( pythonlibName );
60 if ( !pythonlib.load() )
61 {
62 QgsMessageLog::logMessage( u"Couldn't load Python support library: %1"_s.arg( pythonlib.errorString() ) );
63 return false;
64 }
65 }
66
67 QgsMessageLog::logMessage( u"Python support library loaded successfully."_s, __FILE__, Qgis::MessageLevel::Info );
68 typedef QgsPythonUtils *( *inst )();
69 inst pythonlib_inst = ( inst ) cast_to_fptr( pythonlib.resolve( "instance" ) );
70 if ( !pythonlib_inst )
71 {
72 //using stderr on purpose because we want end users to see this [TS]
73 QgsDebugError( u"Couldn't resolve python support library's instance() symbol."_s );
74 return false;
75 }
76
77 QgsDebugMsgLevel( u"Python support library's instance() symbol resolved."_s, 2 );
78 sPythonUtils = pythonlib_inst();
79 if ( sPythonUtils )
80 {
81 sPythonUtils->initServerPython( interface );
82 }
83
84 if ( sPythonUtils && sPythonUtils->isEnabled() )
85 {
86 QgsDebugMsgLevel( u"Python support ENABLED :-)"_s, 2 );
87 }
88 else
89 {
90 QgsDebugError( u"Python support FAILED :-("_s );
91 return false;
92 }
93
94 //Init plugins: loads a list of installed plugins and filter them
95 //for "server" metadata
96 bool atLeastOneEnabled = false;
97
98 const auto constPluginList( sPythonUtils->pluginList() );
99 for ( const QString &pluginName : constPluginList )
100 {
101 const QString pluginService = sPythonUtils->getPluginMetadata( pluginName, u"server"_s );
102 if ( pluginService == "True"_L1 )
103 {
104 if ( sPythonUtils->loadPlugin( pluginName ) )
105 {
106 if ( sPythonUtils->startServerPlugin( pluginName ) )
107 {
108 atLeastOneEnabled = true;
109 serverPlugins().append( pluginName );
110 QgsMessageLog::logMessage( u"Server plugin %1 loaded!"_s.arg( pluginName ), u"Server"_s, Qgis::MessageLevel::Info );
111 }
112 else
113 {
114 QgsMessageLog::logMessage( u"Error loading server plugin %1"_s.arg( pluginName ), u"Server"_s, Qgis::MessageLevel::Critical );
115 }
116 }
117 else
118 {
119 QgsMessageLog::logMessage( u"Error starting server plugin %1"_s.arg( pluginName ), u"Server"_s, Qgis::MessageLevel::Critical );
120 }
121 }
122 }
123 return sPythonUtils && sPythonUtils->isEnabled() && atLeastOneEnabled;
124}
@ Critical
Critical/error message.
Definition qgis.h:162
@ Info
Information message.
Definition qgis.h:160
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
Definition qgis.cpp:687
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, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
Defines 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:6752
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59