39
39
40
40
#ifndef Q_OS_WIN
41
41
#include < netinet/in.h>
42
+ #include < pwd.h>
42
43
#else
43
44
#include < winsock.h>
45
+ #include < windows.h>
46
+ #include < Lmcons.h>
47
+ #define SECURITY_WIN32
48
+ #include < Security.h>
49
+ #pragma comment( lib, "Secur32.lib" )
44
50
#endif
45
51
46
52
#include " qgsconfig.h"
@@ -72,6 +78,10 @@ QStringList ABISYM( QgsApplication::mGdalSkipList );
72
78
int ABISYM ( QgsApplication::mMaxThreads );
73
79
QString ABISYM ( QgsApplication::mAuthDbDirPath );
74
80
81
+ QString QgsApplication::sUserName ;
82
+ QString QgsApplication::sUserFullName ;
83
+ QString QgsApplication::sPlatformName = " desktop" ;
84
+
75
85
const char * QgsApplication::QGIS_ORGANIZATION_NAME = " QGIS" ;
76
86
const char * QgsApplication::QGIS_ORGANIZATION_DOMAIN = " qgis.org" ;
77
87
const char * QgsApplication::QGIS_APPLICATION_NAME = " QGIS2" ;
@@ -89,9 +99,11 @@ const char* QgsApplication::QGIS_APPLICATION_NAME = "QGIS2";
89
99
so that platform-conditional code is minimized and paths are easier
90
100
to change due to centralization.
91
101
*/
92
- QgsApplication::QgsApplication ( int & argc, char ** argv, bool GUIenabled, const QString& customConfigPath )
102
+ QgsApplication::QgsApplication ( int & argc, char ** argv, bool GUIenabled, const QString& customConfigPath, const QString& platformName )
93
103
: QApplication( argc, argv, GUIenabled )
94
104
{
105
+ sPlatformName = platformName;
106
+
95
107
init ( customConfigPath ); // init can also be called directly by e.g. unit tests that don't inherit QApplication.
96
108
}
97
109
@@ -722,6 +734,93 @@ QRegExp QgsApplication::shortNameRegExp()
722
734
return QRegExp ( " ^[A-Za-z][A-Za-z0-9\\ ._-]*" );
723
735
}
724
736
737
+ QString QgsApplication::userLoginName ()
738
+ {
739
+ if ( !sUserName .isEmpty () )
740
+ return sUserName ;
741
+
742
+ #ifdef Q_OS_WIN
743
+ TCHAR name [ UNLEN + 1 ];
744
+ DWORD size = UNLEN + 1 ;
745
+
746
+ if ( GetUserName (( TCHAR* )name, &size ) )
747
+ {
748
+ sUserName = QString ( name );
749
+ }
750
+
751
+ #else
752
+ QProcess process;
753
+
754
+ process.start ( " whoami" );
755
+ process.waitForFinished ();
756
+ sUserName = process.readAllStandardOutput ().trimmed ();
757
+ #endif
758
+
759
+ if ( !sUserName .isEmpty () )
760
+ return sUserName ;
761
+
762
+ // backup plan - use environment variables
763
+ sUserName = qgetenv ( " USER" );
764
+ if ( !sUserName .isEmpty () )
765
+ return sUserName ;
766
+
767
+ // last resort
768
+ sUserName = qgetenv ( " USERNAME" );
769
+ return sUserName ;
770
+ }
771
+
772
+ QString QgsApplication::userFullName ()
773
+ {
774
+ if ( !sUserFullName .isEmpty () )
775
+ return sUserFullName ;
776
+
777
+ #ifdef Q_OS_WIN
778
+ TCHAR name [ UNLEN + 1 ];
779
+ DWORD size = UNLEN + 1 ;
780
+
781
+ // note - this only works for accounts connected to domain
782
+ if ( GetUserNameEx ( NameDisplay, ( TCHAR* )name, &size ) )
783
+ {
784
+ sUserFullName = QString ( name );
785
+ }
786
+
787
+ // fall back to login name
788
+ if ( sUserFullName .isEmpty () )
789
+ sUserFullName = userLoginName ();
790
+ #else
791
+ struct passwd *p = getpwuid ( getuid () );
792
+
793
+ if ( p )
794
+ {
795
+ QString gecosName = QString ( p->pw_gecos );
796
+ sUserFullName = gecosName.left ( gecosName.indexOf ( ' ,' , 0 ) );
797
+ }
798
+
799
+ #endif
800
+
801
+ return sUserFullName ;
802
+ }
803
+
804
+ QString QgsApplication::osName ()
805
+ {
806
+ #if defined(Q_OS_ANDROID)
807
+ return QLatin1String ( " android" );
808
+ #elif defined(Q_OS_MAC)
809
+ return QLatin1String ( " osx" );
810
+ #elif defined(Q_OS_WIN)
811
+ return QLatin1String ( " windows" );
812
+ #elif defined(Q_OS_LINUX)
813
+ return QLatin1String ( " linux" );
814
+ #else
815
+ return QLatin1String ( " unknown" );
816
+ #endif
817
+ }
818
+
819
+ QString QgsApplication::platform ()
820
+ {
821
+ return sPlatformName ;
822
+ }
823
+
725
824
QString QgsApplication::userThemesFolder ()
726
825
{
727
826
return qgisSettingsDirPath () + QLatin1String ( " /themes" );
0 commit comments