QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 "qgsmapserviceexception.h"
21 #include "qgsapplication.h"
22 #include "qgslogger.h"
23 #include "qgspythonutils.h"
24 #include "qgsserverlogger.h"
25 
26 #include <QLibrary>
27 
28 
29 // Initialize static members
30 QgsPythonUtils *QgsServerPlugins::sPythonUtils;
31 
32 // Construct on first use
34 {
35  static QStringList *pluginList = new QStringList();
36  return *pluginList;
37 }
38 
39 
40 // This code is mainly borrowed from QGIS desktop Python plugin initialization
42 {
43  QString pythonlibName( QStringLiteral( "qgispython" ) );
44 #if defined(Q_OS_UNIX)
45  pythonlibName.prepend( QgsApplication::libraryPath() );
46 #endif
47 #ifdef __MINGW32__
48  pythonlibName.prepend( "lib" );
49 #endif
50  QString version = QStringLiteral( "%1.%2.%3" ).arg( Qgis::QGIS_VERSION_INT / 10000 ).arg( Qgis::QGIS_VERSION_INT / 100 % 100 ).arg( Qgis::QGIS_VERSION_INT % 100 );
51  QgsMessageLog::logMessage( QStringLiteral( "load library %1 (%2)" ).arg( pythonlibName, version ), __FILE__, Qgis::Info );
52  QLibrary pythonlib( pythonlibName, version );
53  // It's necessary to set these two load hints, otherwise Python library won't work correctly
54  // see http://lists.kde.org/?l=pykde&m=117190116820758&w=2
55  pythonlib.setLoadHints( QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint );
56  if ( !pythonlib.load() )
57  {
58  pythonlib.setFileName( pythonlibName );
59  if ( !pythonlib.load() )
60  {
61  QgsMessageLog::logMessage( QStringLiteral( "Couldn't load Python support library: %1" ).arg( pythonlib.errorString() ) );
62  return false;
63  }
64  }
65 
66  QgsMessageLog::logMessage( QStringLiteral( "Python support library loaded successfully." ), __FILE__, Qgis::Info );
67  typedef QgsPythonUtils*( *inst )();
68  inst pythonlib_inst = ( inst ) cast_to_fptr( pythonlib.resolve( "instance" ) );
69  if ( !pythonlib_inst )
70  {
71  //using stderr on purpose because we want end users to see this [TS]
72  QgsDebugMsg( QStringLiteral( "Couldn't resolve python support library's instance() symbol." ) );
73  return false;
74  }
75 
76  QgsDebugMsg( QStringLiteral( "Python support library's instance() symbol resolved." ) );
77  sPythonUtils = pythonlib_inst();
78  sPythonUtils->initServerPython( interface );
79 
80  if ( sPythonUtils && sPythonUtils->isEnabled() )
81  {
82  QgsDebugMsg( QStringLiteral( "Python support ENABLED :-)" ) );
83  }
84  else
85  {
86  QgsDebugMsg( QStringLiteral( "Python support FAILED :-(" ) );
87  return false;
88  }
89 
90  //Init plugins: loads a list of installed plugins and filter them
91  //for "server" metadata
92  bool atLeastOneEnabled = false;
93  for ( const QString &pluginName : sPythonUtils->pluginList() )
94  {
95  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::Info );
105  }
106  else
107  {
108  QgsMessageLog::logMessage( QStringLiteral( "Error loading server plugin %1" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::Critical );
109  }
110  }
111  else
112  {
113  QgsMessageLog::logMessage( QStringLiteral( "Error starting server plugin %1" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::Critical );
114  }
115  }
116  }
117  return sPythonUtils && sPythonUtils->isEnabled() && atLeastOneEnabled;
118 }
119 
120 
static QString libraryPath()
Returns the path containing qgis_core, qgis_gui, qgispython (and other) libraries.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
static bool initPlugins(QgsServerInterface *interface)
Initializes the Python plugins.
static const int QGIS_VERSION_INT
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.h:66
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
static QgsPythonUtils * sPythonUtils
Pointer to QgsPythonUtils.
#define cast_to_fptr(f)
Definition: qgis.h:171
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins...
static QStringList & serverPlugins()
List of available server plugin names.