QGIS API Documentation  3.6.0-Noosa (5873452)
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
28 QgsPythonUtils *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_MAC) || defined(Q_OS_LINUX)
43  pythonlibName.prepend( QgsApplication::libraryPath() );
44 #endif
45 #ifdef __MINGW32__
46  pythonlibName.prepend( "lib" );
47 #endif
48  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 );
49  QgsMessageLog::logMessage( QStringLiteral( "load library %1 (%2)" ).arg( pythonlibName, version ), __FILE__, Qgis::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::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  for ( const QString &pluginName : sPythonUtils->pluginList() )
92  {
93  QString pluginService = sPythonUtils->getPluginMetadata( pluginName, QStringLiteral( "server" ) );
94  if ( pluginService == QLatin1String( "True" ) )
95  {
96  if ( sPythonUtils->loadPlugin( pluginName ) )
97  {
98  if ( sPythonUtils->startServerPlugin( pluginName ) )
99  {
100  atLeastOneEnabled = true;
101  serverPlugins().append( pluginName );
102  QgsMessageLog::logMessage( QStringLiteral( "Server plugin %1 loaded!" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::Info );
103  }
104  else
105  {
106  QgsMessageLog::logMessage( QStringLiteral( "Error loading server plugin %1" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::Critical );
107  }
108  }
109  else
110  {
111  QgsMessageLog::logMessage( QStringLiteral( "Error starting server plugin %1" ).arg( pluginName ), QStringLiteral( "Server" ), Qgis::Critical );
112  }
113  }
114  }
115  return sPythonUtils && sPythonUtils->isEnabled() && atLeastOneEnabled;
116 }
117 
118 
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:53
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:158
QgsServerInterface Class defining interfaces exposed by QGIS Server and made available to plugins...
static QStringList & serverPlugins()
List of available server plugin names.