Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #46308 from qgis/backport-46300-to-release-3_22
[Backport release-3_22] Default to "external" for QgsApplication::platformName()
  • Loading branch information
nirvn committed Dec 2, 2021
2 parents 9ff66d5 + c3fe4cc commit a46a111
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
11 changes: 10 additions & 1 deletion python/core/auto_generated/qgsapplication.sip.in
Expand Up @@ -84,7 +84,16 @@ to change due to centralization.
static const char *QGIS_ORGANIZATION_NAME;
static const char *QGIS_ORGANIZATION_DOMAIN;
static const char *QGIS_APPLICATION_NAME;

QgsApplication( SIP_PYLIST argv, bool GUIenabled, QString profileFolder = QString(), QString platformName = "desktop" ) / PostHook = __pyQtQAppHook__ / [( int &argc, char **argv, bool GUIenabled, const QString &profileFolder = QString(), const QString &platformName = "desktop" )];
%Docstring
Constructor for QgsApplication.

:param argv: command line arguments
:param GUIenabled: set to ``True`` if a GUI application is required, or ``False`` for a console only application
:param profileFolder: optional string representing the profile to load at startup
:param platformName: the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "external" (for external CLI scripts)
%End
%MethodCode
// The Python interface is a list of argument strings that is modified.

Expand Down Expand Up @@ -420,7 +429,7 @@ Returns a string name of the operating system QGIS is running on.

static QString platform();
%Docstring
Returns the QGIS platform name, e.g., "desktop" or "server".
Returns the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "external" (for external CLI scripts).

.. seealso:: :py:func:`osName`

Expand Down
2 changes: 1 addition & 1 deletion src/app/main.cpp
Expand Up @@ -1032,7 +1032,7 @@ int main( int argc, char *argv[] )
QgsApplication::setTranslation( translationCode );
}

QgsApplication myApp( argc, argv, myUseGuiFlag );
QgsApplication myApp( argc, argv, myUseGuiFlag, QString(), QStringLiteral( "desktop" ) );

//write the log messages written before creating QgsApplication
for ( const QString &preApplicationLogMessage : std::as_const( preApplicationLogMessages ) )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsapplication.cpp
Expand Up @@ -173,7 +173,7 @@ Q_GLOBAL_STATIC( QString, sAuthDbDirPath )

Q_GLOBAL_STATIC( QString, sUserName )
Q_GLOBAL_STATIC( QString, sUserFullName )
Q_GLOBAL_STATIC_WITH_ARGS( QString, sPlatformName, ( "desktop" ) )
Q_GLOBAL_STATIC_WITH_ARGS( QString, sPlatformName, ( "external" ) )
Q_GLOBAL_STATIC( QString, sTranslation )

Q_GLOBAL_STATIC( QTemporaryDir, sIconCacheDir )
Expand Down
23 changes: 21 additions & 2 deletions src/core/qgsapplication.h
Expand Up @@ -160,8 +160,27 @@ class CORE_EXPORT QgsApplication : public QApplication
static const char *QGIS_ORGANIZATION_DOMAIN;
static const char *QGIS_APPLICATION_NAME;
#ifndef SIP_RUN
QgsApplication( int &argc, char **argv, bool GUIenabled, const QString &profileFolder = QString(), const QString &platformName = "desktop" );

/**
* Constructor for QgsApplication.
*
* \param argc command line argument count
* \param argv command line arguments
* \param GUIenabled set to TRUE if a GUI application is required, or FALSE for a console only application
* \param profileFolder optional string representing the profile to load at startup
* \param platformName the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "external" (for external CLI scripts)
*/
QgsApplication( int &argc, char **argv, bool GUIenabled, const QString &profileFolder = QString(), const QString &platformName = "external" );
#else

/**
* Constructor for QgsApplication.
*
* \param argv command line arguments
* \param GUIenabled set to TRUE if a GUI application is required, or FALSE for a console only application
* \param profileFolder optional string representing the profile to load at startup
* \param platformName the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "external" (for external CLI scripts)
*/
QgsApplication( SIP_PYLIST argv, bool GUIenabled, QString profileFolder = QString(), QString platformName = "desktop" ) / PostHook = __pyQtQAppHook__ / [( int &argc, char **argv, bool GUIenabled, const QString &profileFolder = QString(), const QString &platformName = "desktop" )];
% MethodCode
// The Python interface is a list of argument strings that is modified.
Expand Down Expand Up @@ -437,7 +456,7 @@ class CORE_EXPORT QgsApplication : public QApplication
static QString osName();

/**
* Returns the QGIS platform name, e.g., "desktop" or "server".
* Returns the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "external" (for external CLI scripts).
* \see osName()
* \since QGIS 2.14
*/
Expand Down
2 changes: 1 addition & 1 deletion src/process/main.cpp
Expand Up @@ -86,7 +86,7 @@ int main( int argc, char *argv[] )
#endif // _MSC_VER
#endif // Q_OS_WIN

QgsApplication app( argc, argv, false );
QgsApplication app( argc, argv, false, QString(), QStringLiteral( "qgis_process" ) );
QString myPrefixPath;
if ( myPrefixPath.isEmpty() )
{
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgsapplication.cpp
Expand Up @@ -94,8 +94,8 @@ void TestQgsApplication::osName()

void TestQgsApplication::platformName()
{
// test will always be run under desktop platform
QCOMPARE( QgsApplication::platform(), QString( "desktop" ) );
// test will always be run under external platform
QCOMPARE( QgsApplication::platform(), QString( "external" ) );
}

void TestQgsApplication::themeIcon()
Expand Down
9 changes: 8 additions & 1 deletion tests/src/python/test_qgsappstartup.py
Expand Up @@ -86,6 +86,12 @@ def doTestStartup(self, option='', testDir='', testFile='',
if s > timeOut:
raise Exception('Timed out waiting for application start, Call: "{}", Env: {}'.format(' '.join(call), env))

with open(myTestFile, 'rt', encoding='utf-8') as res_file:
lines = res_file.readlines()

# platform should be "Desktop"
self.assertEqual(lines, ['Platform: desktop'])

try:
p.terminate()
except OSError as e:
Expand All @@ -98,8 +104,9 @@ def testPyQgisStartupEnvVar(self):
testfile = 'pyqgis_startup.txt'
testfilepath = os.path.join(self.TMP_DIR, testfile).replace('\\', '/')
testcode = [
"from qgis.core import QgsApplication\n"
"f = open('{0}', 'w')\n".format(testfilepath),
"f.write('This is a test')\n",
"f.write('Platform: ' + QgsApplication.platform())\n",
"f.close()\n"
]
testmod = os.path.join(self.TMP_DIR, 'pyqgis_startup.py').replace('\\', '/')
Expand Down

0 comments on commit a46a111

Please sign in to comment.