Skip to content

Commit

Permalink
Default to "external" for QgsApplication::platformName(), and only
Browse files Browse the repository at this point in the history
use "desktop" when actually run from the desktop. Also add
"qgis_process" platform.
  • Loading branch information
nyalldawson committed Nov 30, 2021
1 parent 563d41d commit 2ce70c5
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgsapplication.sip.in
Expand Up @@ -420,7 +420,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 @@ -1036,7 +1036,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
4 changes: 2 additions & 2 deletions src/core/qgsapplication.h
Expand Up @@ -160,7 +160,7 @@ 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" );
QgsApplication( int &argc, char **argv, bool GUIenabled, const QString &profileFolder = QString(), const QString &platformName = "external" );
#else
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
Expand Down Expand Up @@ -437,7 +437,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 2ce70c5

Please sign in to comment.