/
qgsapplication.cpp
1368 lines (1216 loc) · 39.7 KB
/
qgsapplication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsapplication.cpp - Accessors for application-wide data
--------------------------------------
Date : 02-Jan-2006
Copyright : (C) 2006 by Tom Elwertowski
Email : telwertowski at users dot sourceforge dot net
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsapplication.h"
#include "qgsauthmanager.h"
#include "qgscrscache.h"
#include "qgsdataitemproviderregistry.h"
#include "qgsexception.h"
#include "qgsgeometry.h"
#include "qgslogger.h"
#include "qgsmaplayerregistry.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsproviderregistry.h"
#include "qgsexpression.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFileOpenEvent>
#include <QMessageBox>
#include <QPalette>
#include <QProcess>
#include <QSettings>
#include <QIcon>
#include <QPixmap>
#include <QThreadPool>
#ifndef Q_OS_WIN
#include <netinet/in.h>
#include <pwd.h>
#else
#include <winsock.h>
#include <windows.h>
#include <Lmcons.h>
#define SECURITY_WIN32
#include <Security.h>
#pragma comment( lib, "Secur32.lib" )
#endif
#include "qgsconfig.h"
#include <gdal.h>
#include <ogr_api.h>
#include <cpl_conv.h> // for setting gdal options
#include <sqlite3.h>
QObject * ABISYM( QgsApplication::mFileOpenEventReceiver );
QStringList ABISYM( QgsApplication::mFileOpenEventList );
QString ABISYM( QgsApplication::mPrefixPath );
QString ABISYM( QgsApplication::mPluginPath );
QString ABISYM( QgsApplication::mPkgDataPath );
QString ABISYM( QgsApplication::mLibraryPath );
QString ABISYM( QgsApplication::mLibexecPath );
QString ABISYM( QgsApplication::mThemeName );
QString ABISYM( QgsApplication::mUIThemeName );
QStringList ABISYM( QgsApplication::mDefaultSvgPaths );
QMap<QString, QString> ABISYM( QgsApplication::mSystemEnvVars );
QString ABISYM( QgsApplication::mConfigPath );
bool ABISYM( QgsApplication::mRunningFromBuildDir ) = false;
QString ABISYM( QgsApplication::mBuildSourcePath );
#ifdef _MSC_VER
QString ABISYM( QgsApplication::mCfgIntDir );
#endif
QString ABISYM( QgsApplication::mBuildOutputPath );
QStringList ABISYM( QgsApplication::mGdalSkipList );
int ABISYM( QgsApplication::mMaxThreads );
QString ABISYM( QgsApplication::mAuthDbDirPath );
QString QgsApplication::sUserName;
QString QgsApplication::sUserFullName;
QString QgsApplication::sPlatformName = "desktop";
const char* QgsApplication::QGIS_ORGANIZATION_NAME = "QGIS";
const char* QgsApplication::QGIS_ORGANIZATION_DOMAIN = "qgis.org";
const char* QgsApplication::QGIS_APPLICATION_NAME = "QGIS2";
/*!
\class QgsApplication
\brief The QgsApplication class manages application-wide information.
This is a subclass of QApplication and should be instantiated in place of
QApplication. Most methods are static in keeping witn the design of QApplication.
This class hides platform-specific path information and provides
a portable way of referencing specific files and directories.
Ideally, hard-coded paths should appear only here and not in other modules
so that platform-conditional code is minimized and paths are easier
to change due to centralization.
*/
QgsApplication::QgsApplication( int & argc, char ** argv, bool GUIenabled, const QString& customConfigPath, const QString& platformName )
: QApplication( argc, argv, GUIenabled )
{
sPlatformName = platformName;
init( customConfigPath ); // init can also be called directly by e.g. unit tests that don't inherit QApplication.
}
void QgsApplication::init( QString customConfigPath )
{
if ( customConfigPath.isEmpty() )
{
if ( getenv( "QGIS_CUSTOM_CONFIG_PATH" ) )
{
customConfigPath = getenv( "QGIS_CUSTOM_CONFIG_PATH" );
}
else
{
customConfigPath = QString( "%1/.qgis%2/" ).arg( QDir::homePath() ).arg( QGis::QGIS_VERSION_INT / 10000 );
}
}
qRegisterMetaType<QgsGeometry::Error>( "QgsGeometry::Error" );
QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : applicationDirPath() );
// QgsDebugMsg( QString( "prefixPath(): %1" ).arg( prefixPath ) );
// check if QGIS is run from build directory (not the install directory)
QFile f;
// "/../../.." is for Mac bundled app in build directory
Q_FOREACH ( const QString& path, QStringList() << "" << "/.." << "/bin" << "/../../.." )
{
f.setFileName( prefixPath + path + "/qgisbuildpath.txt" );
if ( f.exists() )
break;
}
if ( f.exists() && f.open( QIODevice::ReadOnly ) )
{
ABISYM( mRunningFromBuildDir ) = true;
ABISYM( mBuildSourcePath ) = f.readLine().trimmed();
ABISYM( mBuildOutputPath ) = f.readLine().trimmed();
qDebug( "Running from build directory!" );
qDebug( "- source directory: %s", ABISYM( mBuildSourcePath ).toUtf8().data() );
qDebug( "- output directory of the build: %s", ABISYM( mBuildOutputPath ).toUtf8().data() );
#ifdef _MSC_VER
ABISYM( mCfgIntDir ) = prefixPath.split( '/', QString::SkipEmptyParts ).last();
qDebug( "- cfg: %s", ABISYM( mCfgIntDir ).toUtf8().data() );
#endif
}
if ( ABISYM( mRunningFromBuildDir ) )
{
// we run from source directory - not installed to destination (specified prefix)
ABISYM( mPrefixPath ) = QString(); // set invalid path
#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
setPluginPath( ABISYM( mBuildOutputPath ) + '/' + QString( QGIS_PLUGIN_SUBDIR ) + '/' + ABISYM( mCfgIntDir ) );
#else
setPluginPath( ABISYM( mBuildOutputPath ) + '/' + QString( QGIS_PLUGIN_SUBDIR ) );
#endif
setPkgDataPath( ABISYM( mBuildSourcePath ) ); // directly source path - used for: doc, resources, svg
ABISYM( mLibraryPath ) = ABISYM( mBuildOutputPath ) + '/' + QGIS_LIB_SUBDIR + '/';
#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
ABISYM( mLibexecPath ) = ABISYM( mBuildOutputPath ) + '/' + QGIS_LIBEXEC_SUBDIR + '/' + ABISYM( mCfgIntDir ) + '/';
#else
ABISYM( mLibexecPath ) = ABISYM( mBuildOutputPath ) + '/' + QGIS_LIBEXEC_SUBDIR + '/';
#endif
}
else
{
char *prefixPath = getenv( "QGIS_PREFIX_PATH" );
if ( !prefixPath )
{
#if defined(Q_OS_MACX) || defined(Q_OS_WIN)
setPrefixPath( applicationDirPath(), true );
#elif defined(ANDROID)
// this is "/data/data/org.qgis.qgis" in android
QDir myDir( QDir::homePath() );
myDir.cdUp();
QString myPrefix = myDir.absolutePath();
setPrefixPath( myPrefix, true );
#else
QDir myDir( applicationDirPath() );
myDir.cdUp();
QString myPrefix = myDir.absolutePath();
setPrefixPath( myPrefix, true );
#endif
}
else
{
setPrefixPath( prefixPath, true );
}
}
if ( !customConfigPath.isEmpty() )
{
ABISYM( mConfigPath ) = customConfigPath + '/'; // make sure trailing slash is included
}
ABISYM( mDefaultSvgPaths ) << qgisSettingsDirPath() + QLatin1String( "svg/" );
ABISYM( mAuthDbDirPath ) = qgisSettingsDirPath();
if ( getenv( "QGIS_AUTH_DB_DIR_PATH" ) )
{
setAuthDbDirPath( getenv( "QGIS_AUTH_DB_DIR_PATH" ) );
}
// store system environment variables passed to application, before they are adjusted
QMap<QString, QString> systemEnvVarMap;
QString passfile( "QGIS_AUTH_PASSWORD_FILE" ); // QString, for comparison
Q_FOREACH ( const QString &varStr, QProcess::systemEnvironment() )
{
int pos = varStr.indexOf( QLatin1Char( '=' ) );
if ( pos == -1 )
continue;
QString varStrName = varStr.left( pos );
QString varStrValue = varStr.mid( pos + 1 );
if ( varStrName != passfile )
{
systemEnvVarMap.insert( varStrName, varStrValue );
}
}
ABISYM( mSystemEnvVars ) = systemEnvVarMap;
// allow Qt to search for Qt plugins (e.g. sqldrivers) in our plugin directory
QCoreApplication::addLibraryPath( pluginPath() );
// set max. thread count to -1
// this should be read from QSettings but we don't know where they are at this point
// so we read actual value in main.cpp
ABISYM( mMaxThreads ) = -1;
}
QgsApplication::~QgsApplication()
{
}
bool QgsApplication::event( QEvent * event )
{
bool done = false;
if ( event->type() == QEvent::FileOpen )
{
// handle FileOpen event (double clicking a file icon in Mac OS X Finder)
if ( ABISYM( mFileOpenEventReceiver ) )
{
// Forward event to main window.
done = notify( ABISYM( mFileOpenEventReceiver ), event );
}
else
{
// Store filename because receiver has not registered yet.
// If QGIS has been launched by double clicking a file icon, FileOpen will be
// the first event; the main window is not yet ready to handle the event.
ABISYM( mFileOpenEventList ).append( static_cast<QFileOpenEvent *>( event )->file() );
done = true;
}
}
else
{
// pass other events to base class
done = QApplication::event( event );
}
return done;
}
bool QgsApplication::notify( QObject * receiver, QEvent * event )
{
bool done = false;
// Crashes in customization (especially on Mac), if we're not in the main/UI thread, see #5597
if ( thread() == receiver->thread() )
emit preNotify( receiver, event, &done );
if ( done )
return true;
// Send event to receiver and catch unhandled exceptions
done = true;
try
{
done = QApplication::notify( receiver, event );
}
catch ( QgsException & e )
{
QgsDebugMsg( "Caught unhandled QgsException: " + e.what() );
if ( qApp->thread() == QThread::currentThread() )
QMessageBox::critical( activeWindow(), tr( "Exception" ), e.what() );
}
catch ( std::exception & e )
{
QgsDebugMsg( "Caught unhandled std::exception: " + QString::fromAscii( e.what() ) );
if ( qApp->thread() == QThread::currentThread() )
QMessageBox::critical( activeWindow(), tr( "Exception" ), e.what() );
}
catch ( ... )
{
QgsDebugMsg( "Caught unhandled unknown exception" );
if ( qApp->thread() == QThread::currentThread() )
QMessageBox::critical( activeWindow(), tr( "Exception" ), tr( "unknown exception" ) );
}
return done;
}
void QgsApplication::setFileOpenEventReceiver( QObject * receiver )
{
// Set receiver for FileOpen events
ABISYM( mFileOpenEventReceiver ) = receiver;
// Propagate any events collected before the receiver has registered.
if ( ABISYM( mFileOpenEventList ).count() > 0 )
{
QStringListIterator i( ABISYM( mFileOpenEventList ) );
while ( i.hasNext() )
{
QFileOpenEvent foe( i.next() );
QgsApplication::sendEvent( ABISYM( mFileOpenEventReceiver ), &foe );
}
ABISYM( mFileOpenEventList ).clear();
}
}
void QgsApplication::setPrefixPath( const QString &thePrefixPath, bool useDefaultPaths )
{
ABISYM( mPrefixPath ) = thePrefixPath;
#if defined(_MSC_VER)
if ( ABISYM( mPrefixPath ).endsWith( "/bin" ) )
{
ABISYM( mPrefixPath ).chop( 4 );
}
#endif
if ( useDefaultPaths && !ABISYM( mRunningFromBuildDir ) )
{
setPluginPath( ABISYM( mPrefixPath ) + '/' + QString( QGIS_PLUGIN_SUBDIR ) );
setPkgDataPath( ABISYM( mPrefixPath ) + '/' + QString( QGIS_DATA_SUBDIR ) );
}
ABISYM( mLibraryPath ) = ABISYM( mPrefixPath ) + '/' + QGIS_LIB_SUBDIR + '/';
ABISYM( mLibexecPath ) = ABISYM( mPrefixPath ) + '/' + QGIS_LIBEXEC_SUBDIR + '/';
}
void QgsApplication::setPluginPath( const QString &thePluginPath )
{
ABISYM( mPluginPath ) = thePluginPath;
}
void QgsApplication::setPkgDataPath( const QString &thePkgDataPath )
{
ABISYM( mPkgDataPath ) = thePkgDataPath;
QString mySvgPath = thePkgDataPath + ( ABISYM( mRunningFromBuildDir ) ? "/images/svg/" : "/svg/" );
// avoid duplicate entries
if ( !ABISYM( mDefaultSvgPaths ).contains( mySvgPath ) )
ABISYM( mDefaultSvgPaths ) << mySvgPath;
}
void QgsApplication::setDefaultSvgPaths( const QStringList& pathList )
{
ABISYM( mDefaultSvgPaths ) = pathList;
}
void QgsApplication::setAuthDbDirPath( const QString& theAuthDbDirPath )
{
QFileInfo fi( theAuthDbDirPath );
if ( fi.exists() && fi.isDir() && fi.isWritable() )
{
ABISYM( mAuthDbDirPath ) = fi.canonicalFilePath() + QDir::separator();
}
}
QString QgsApplication::prefixPath()
{
if ( ABISYM( mRunningFromBuildDir ) )
{
static bool once = true;
if ( once )
qWarning( "!!! prefix path was requested, but it is not valid - we do not run from installed path !!!" );
once = false;
}
return ABISYM( mPrefixPath );
}
QString QgsApplication::pluginPath()
{
return ABISYM( mPluginPath );
}
QString QgsApplication::pkgDataPath()
{
return ABISYM( mPkgDataPath );
}
QString QgsApplication::defaultThemePath()
{
return ":/images/themes/default/";
}
QString QgsApplication::activeThemePath()
{
return userThemesFolder() + QDir::separator() + themeName() + QDir::separator() + "icons/";
}
QString QgsApplication::appIconPath()
{
return iconsPath() + ( QDate::currentDate().month() == 12 ? tr( "qgis-icon-60x60_xmas.png", "December application icon" ) : QString( "qgis-icon-60x60.png" ) );
}
QString QgsApplication::iconPath( const QString& iconFile )
{
// try active theme
QString path = activeThemePath();
if ( QFile::exists( path + iconFile ) )
return path + iconFile;
// use default theme
return defaultThemePath() + iconFile;
}
QIcon QgsApplication::getThemeIcon( const QString &theName )
{
QString myPreferredPath = activeThemePath() + QDir::separator() + theName;
QString myDefaultPath = defaultThemePath() + QDir::separator() + theName;
if ( QFile::exists( myPreferredPath ) )
{
return QIcon( myPreferredPath );
}
else if ( QFile::exists( myDefaultPath ) )
{
//could still return an empty icon if it
//doesnt exist in the default theme either!
return QIcon( myDefaultPath );
}
else
{
return QIcon();
}
}
// TODO: add some caching mechanism ?
QPixmap QgsApplication::getThemePixmap( const QString &theName )
{
QString myPreferredPath = activeThemePath() + QDir::separator() + theName;
QString myDefaultPath = defaultThemePath() + QDir::separator() + theName;
if ( QFile::exists( myPreferredPath ) )
{
return QPixmap( myPreferredPath );
}
else
{
//could still return an empty icon if it
//doesnt exist in the default theme either!
return QPixmap( myDefaultPath );
}
}
/*!
Set the theme path to the specified theme.
*/
void QgsApplication::setThemeName( const QString &theThemeName )
{
ABISYM( mThemeName ) = theThemeName;
}
/*!
* Get the active theme name
*/
QString QgsApplication::themeName()
{
return ABISYM( mThemeName );
}
void QgsApplication::setUITheme( const QString &themeName )
{
// Loop all style sheets, find matching name, load it.
QHash<QString, QString> themes = QgsApplication::uiThemes();
QString themename = themeName;
if ( !themes.contains( themename ) )
themename = "default";
QString path = themes[themename];
QString stylesheetname = path + "/style.qss";
QString autostylesheet = stylesheetname + ".auto";
QFile file( stylesheetname );
QFile variablesfile( path + "/variables.qss" );
QFile fileout( autostylesheet );
QFileInfo variableInfo( variablesfile );
if ( variableInfo.exists() && variablesfile.open( QIODevice::ReadOnly ) )
{
if ( !file.open( QIODevice::ReadOnly ) || !fileout.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
return;
}
QHash<QString, QString> variables;
QString styledata = file.readAll();
QTextStream in( &variablesfile );
while ( !in.atEnd() )
{
QString line = in.readLine();
// This is is a variable
if ( line.startsWith( '@' ) )
{
int index = line.indexOf( ':' );
QString name = line.mid( 0, index );
QString value = line.mid( index + 1, line.length() );
styledata.replace( name, value );
}
}
variablesfile.close();
QTextStream out( &fileout );
out << styledata;
fileout.close();
file.close();
stylesheetname = autostylesheet;
}
QString styleSheet = QLatin1String( "file:///" );
styleSheet.append( stylesheetname );
qApp->setStyleSheet( styleSheet );
setThemeName( themename );
}
QHash<QString, QString> QgsApplication::uiThemes()
{
QStringList paths = QStringList() << userThemesFolder();
QHash<QString, QString> mapping;
mapping.insert( "default", "" );
Q_FOREACH ( const QString& path, paths )
{
QDir folder( path );
QFileInfoList styleFiles = folder.entryInfoList( QDir::Dirs | QDir::NoDotAndDotDot );
Q_FOREACH ( const QFileInfo& info, styleFiles )
{
QFileInfo styleFile( info.absoluteFilePath() + "/style.qss" );
if ( !styleFile.exists() )
continue;
QString name = info.baseName();
QString path = info.absoluteFilePath();
mapping.insert( name, path );
}
}
return mapping;
}
/*!
Returns the path to the authors file.
*/
QString QgsApplication::authorsFilePath()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/doc/AUTHORS" );
}
/*!
Returns the path to the contributors file.
*/
QString QgsApplication::contributorsFilePath()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/doc/CONTRIBUTORS" );
}
QString QgsApplication::developersMapFilePath()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/doc/developersmap.html" );
}
/*!
Returns the path to the sponsors file.
*/
QString QgsApplication::sponsorsFilePath()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/doc/SPONSORS" );
}
/*!
Returns the path to the donors file.
*/
QString QgsApplication::donorsFilePath()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/doc/DONORS" );
}
/** Returns the path to the sponsors file. */
QString QgsApplication::translatorsFilePath()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/doc/TRANSLATORS" );
}
/** Returns the path to the licence file. */
QString QgsApplication::licenceFilePath()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/doc/LICENSE" );
}
/*!
Returns the path to the help application.
*/
QString QgsApplication::helpAppPath()
{
QString helpAppPath;
#ifdef Q_OS_MACX
helpAppPath = applicationDirPath() + "/bin/qgis_help.app/Contents/MacOS";
#else
helpAppPath = libexecPath();
#endif
helpAppPath += "/qgis_help";
#ifdef Q_OS_WIN
helpAppPath += ".exe";
#endif
return helpAppPath;
}
/*!
Returns the path to the translation directory.
*/
QString QgsApplication::i18nPath()
{
if ( ABISYM( mRunningFromBuildDir ) )
return ABISYM( mBuildOutputPath ) + QLatin1String( "/i18n" );
else
return ABISYM( mPkgDataPath ) + QLatin1String( "/i18n/" );
}
/*!
Returns the path to the master qgis.db file.
*/
QString QgsApplication::qgisMasterDbFilePath()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/resources/qgis.db" );
}
/*!
Returns the path to the settings directory in user's home dir
*/
QString QgsApplication::qgisSettingsDirPath()
{
return ABISYM( mConfigPath );
}
/*!
Returns the path to the user qgis.db file.
*/
QString QgsApplication::qgisUserDbFilePath()
{
return qgisSettingsDirPath() + QLatin1String( "qgis.db" );
}
/*!
Returns the path to the user authentication database file: qgis-auth.db.
*/
QString QgsApplication::qgisAuthDbFilePath()
{
return ABISYM( mAuthDbDirPath ) + QLatin1String( "qgis-auth.db" );
}
/*!
Returns the path to the splash screen image directory.
*/
QString QgsApplication::splashPath()
{
return QString( ":/images/splash/" );
}
/*!
Returns the path to the icons image directory.
*/
QString QgsApplication::iconsPath()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/images/icons/" );
}
/*!
Returns the path to the srs.db file.
*/
QString QgsApplication::srsDbFilePath()
{
if ( ABISYM( mRunningFromBuildDir ) )
{
QString tempCopy = QDir::tempPath() + "/srs.db";
if ( !QFile( tempCopy ).exists() )
{
QFile f( ABISYM( mPkgDataPath ) + "/resources/srs.db" );
if ( !f.copy( tempCopy ) )
{
qFatal( "Could not create temporary copy" );
}
}
return tempCopy;
}
else
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/resources/srs.db" );
}
}
/*!
Returns the paths to the svg directories.
*/
QStringList QgsApplication::svgPaths()
{
//local directories to search when looking for an SVG with a given basename
//defined by user in options dialog
QSettings settings;
QStringList myPathList;
QString myPaths = settings.value( "svg/searchPathsForSVG", QString() ).toString();
if ( !myPaths.isEmpty() )
{
myPathList = myPaths.split( '|' );
}
myPathList << ABISYM( mDefaultSvgPaths );
return myPathList;
}
/*!
Returns the paths to the composer template directories.
*/
QStringList QgsApplication::composerTemplatePaths()
{
//local directories to search when looking for an SVG with a given basename
//defined by user in options dialog
QSettings settings;
QStringList myPathList;
QString myPaths = settings.value( "composer/searchPathsForTemplates", QString() ).toString();
if ( !myPaths.isEmpty() )
{
myPathList = myPaths.split( '|' );
}
return myPathList;
}
QString QgsApplication::userStyleV2Path()
{
return qgisSettingsDirPath() + QLatin1String( "symbology-ng-style.db" );
}
QRegExp QgsApplication::shortNameRegExp()
{
return QRegExp( "^[A-Za-z][A-Za-z0-9\\._-]*" );
}
QString QgsApplication::userLoginName()
{
if ( !sUserName.isEmpty() )
return sUserName;
#ifdef Q_OS_WIN
TCHAR name [ UNLEN + 1 ];
DWORD size = UNLEN + 1;
if ( GetUserName(( TCHAR* )name, &size ) )
{
sUserName = QString( name );
}
#else
QProcess process;
process.start( "whoami" );
process.waitForFinished();
sUserName = process.readAllStandardOutput().trimmed();
#endif
if ( !sUserName.isEmpty() )
return sUserName;
//backup plan - use environment variables
sUserName = qgetenv( "USER" );
if ( !sUserName.isEmpty() )
return sUserName;
//last resort
sUserName = qgetenv( "USERNAME" );
return sUserName;
}
QString QgsApplication::userFullName()
{
if ( !sUserFullName.isEmpty() )
return sUserFullName;
#ifdef Q_OS_WIN
TCHAR name [ UNLEN + 1 ];
DWORD size = UNLEN + 1;
//note - this only works for accounts connected to domain
if ( GetUserNameEx( NameDisplay, ( TCHAR* )name, &size ) )
{
sUserFullName = QString( name );
}
//fall back to login name
if ( sUserFullName.isEmpty() )
sUserFullName = userLoginName();
#elif defined(Q_OS_ANDROID)
sUserFullName = "Not available";
#else
struct passwd *p = getpwuid( getuid() );
if ( p )
{
QString gecosName = QString( p->pw_gecos );
sUserFullName = gecosName.left( gecosName.indexOf( ',', 0 ) );
}
#endif
return sUserFullName;
}
QString QgsApplication::osName()
{
#if defined(Q_OS_ANDROID)
return QLatin1String( "android" );
#elif defined(Q_OS_MAC)
return QLatin1String( "osx" );
#elif defined(Q_OS_WIN)
return QLatin1String( "windows" );
#elif defined(Q_OS_LINUX)
return QLatin1String( "linux" );
#else
return QLatin1String( "unknown" );
#endif
}
QString QgsApplication::platform()
{
return sPlatformName;
}
QString QgsApplication::userThemesFolder()
{
return qgisSettingsDirPath() + QLatin1String( "/themes" );
}
QString QgsApplication::defaultStyleV2Path()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/resources/symbology-ng-style.db" );
}
QString QgsApplication::defaultThemesFolder()
{
return ABISYM( mPkgDataPath ) + QLatin1String( "/resources/themes" );
}
QString QgsApplication::libraryPath()
{
return ABISYM( mLibraryPath );
}
QString QgsApplication::libexecPath()
{
return ABISYM( mLibexecPath );
}
QgsApplication::endian_t QgsApplication::endian()
{
return ( htonl( 1 ) == 1 ) ? XDR : NDR;
}
void QgsApplication::initQgis()
{
// set the provider plugin path (this creates provider registry)
QgsProviderRegistry::instance( pluginPath() );
// create map layer registry if doesn't exist
QgsMapLayerRegistry::instance();
// initialize authentication manager and connect to database
QgsAuthManager::instance()->init( pluginPath() );
}
void QgsApplication::exitQgis()
{
delete QgsAuthManager::instance();
//Ensure that all remaining deleteLater QObjects are actually deleted before we exit.
//This isn't strictly necessary (since we're exiting anyway) but doing so prevents a lot of
//LeakSanitiser noise which hides real issues
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
delete QgsProviderRegistry::instance();
//delete all registered functions from expression engine (see above comment)
QgsExpression::cleanRegisteredFunctions();
// tear-down GDAL/OGR
OGRCleanupAll();
GDALDestroyDriverManager();
}
QString QgsApplication::showSettings()
{
QString myEnvironmentVar( getenv( "QGIS_PREFIX_PATH" ) );
QString myState = tr( "Application state:\n"
"QGIS_PREFIX_PATH env var:\t\t%1\n"
"Prefix:\t\t%2\n"
"Plugin Path:\t\t%3\n"
"Package Data Path:\t%4\n"
"Active Theme Name:\t%5\n"
"Active Theme Path:\t%6\n"
"Default Theme Path:\t%7\n"
"SVG Search Paths:\t%8\n"
"User DB Path:\t%9\n"
"Auth DB Path:\t%10\n" )
.arg( myEnvironmentVar,
prefixPath(),
pluginPath(),
pkgDataPath(),
themeName(),
activeThemePath(),
defaultThemePath(),
svgPaths().join( tr( "\n\t\t", "match indentation of application state" ) ),
qgisMasterDbFilePath() )
.arg( qgisAuthDbFilePath() );
return myState;
}
QString QgsApplication::reportStyleSheet()
{
//
// Make the style sheet desktop preferences aware by using qappliation
// palette as a basis for colors where appropriate
//
// QColor myColor1 = palette().highlight().color();
QColor myColor1( Qt::lightGray );
QColor myColor2 = myColor1;
myColor2 = myColor2.lighter( 110 ); //10% lighter
QString myStyle;
myStyle = "p.glossy{ background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
" stop: 0 " + myColor1.name() + ","
" stop: 0.1 " + myColor2.name() + ","
" stop: 0.5 " + myColor1.name() + ","
" stop: 0.9 " + myColor2.name() + ","
" stop: 1 " + myColor1.name() + ");"
" color: black;"
" padding-left: 4px;"
" padding-top: 20px;"
" padding-bottom: 8px;"
" border: 1px solid #6c6c6c;"
"}"
"p.subheaderglossy{ background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
" stop: 0 " + myColor1.name() + ","
" stop: 0.1 " + myColor2.name() + ","
" stop: 0.5 " + myColor1.name() + ","
" stop: 0.9 " + myColor2.name() + ","
" stop: 1 " + myColor1.name() + ");"
" font-weight: bold;"
" font-size: medium;"
" line-height: 1.1em;"
" width: 100%;"
" color: black;"
" padding-left: 4px;"
" padding-right: 4px;"
" padding-top: 20px;"
" padding-bottom: 8px;"
" border: 1px solid #6c6c6c;"
"}"
"th.glossy{ background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
" stop: 0 " + myColor1.name() + ","
" stop: 0.1 " + myColor2.name() + ","
" stop: 0.5 " + myColor1.name() + ","
" stop: 0.9 " + myColor2.name() + ","
" stop: 1 " + myColor1.name() + ");"
" color: black;"
" border: 1px solid #6c6c6c;"
"}"
".overview{ font: 1.82em; font-weight: bold;}"
"body{ background: white;"
" color: black;"
" font-family: arial,sans-serif;"
"}"
"h1{ background-color: #F6F6F6;"
" color: #8FB171; "
" font-size: x-large; "
" font-weight: normal;"
" font-family: luxi serif, georgia, times new roman, times, serif;"
" background: none;"
" padding: 0.75em 0 0;"
" margin: 0;"
" line-height: 3em;"
"}"
"h2{ background-color: #F6F6F6;"
" color: #8FB171; "
" font-size: medium; "
" font-weight: normal;"
" font-family: luxi serif, georgia, times new roman, times, serif;"
" background: none;"
" padding: 0.75em 0 0;"
" margin: 0;"
" line-height: 1.1em;"
"}"
"h3{ background-color: #F6F6F6;"
" color: #729FCF;"
" font-family: luxi serif, georgia, times new roman, times, serif;"
" font-weight: bold;"
" font-size: large;"
" text-align: right;"
" border-bottom: 5px solid #DCEB5C;"
"}"
"h4{ background-color: #F6F6F6;"
" color: #729FCF;"
" font-family: luxi serif, georgia, times new roman, times, serif;"
" font-weight: bold;"
" font-size: medium;"