-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1317 lines (1177 loc) · 45.2 KB
/
main.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
/***************************************************************************
main.cpp - description
-------------------
begin : Fri Jun 21 10:48:28 AKDT 2002
copyright : (C) 2002 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
//qt includes
#include <QBitmap>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFont>
#include <QFontDatabase>
#include <QPixmap>
#include <QLocale>
#include <QSplashScreen>
#include <QString>
#include <QStringList>
#include <QStyle>
#include <QDesktopWidget>
#include <QTranslator>
#include <QImageReader>
#include <QMessageBox>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef WIN32
// Open files in binary mode
#include <fcntl.h> /* _O_BINARY */
#include <windows.h>
#include <dbghelp.h>
#include <time.h>
#ifdef MSVC
#undef _fmode
int _fmode = _O_BINARY;
#else
// Only do this if we are not building on windows with msvc.
// Recommended method for doing this with msvc is with a call to _set_fmode
// which is the first thing we do in main().
// Similarly, with MinGW set _fmode in main().
#endif //_MSC_VER
#else
#include <getopt.h>
#endif
#ifdef Q_OS_MACX
#include <ApplicationServices/ApplicationServices.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1050
typedef SInt32 SRefCon;
#endif
// For setting the maximum open files limit higher
#include <sys/resource.h>
#include <limits.h>
#endif
#if ((defined(linux) || defined(__linux__)) && !defined(ANDROID)) || defined(__FreeBSD__)
#include <unistd.h>
#include <execinfo.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#endif
#include "qgscustomization.h"
#include "qgssettings.h"
#include "qgsfontutils.h"
#include "qgspluginregistry.h"
#include "qgsmessagelog.h"
#include "qgspythonrunner.h"
#include "qgslocalec.h"
#include "qgisapp.h"
#include "qgsmapcanvas.h"
#include "qgsapplication.h"
#include "qgsconfig.h"
#include "qgsversion.h"
#include "qgsexception.h"
#include "qgsproject.h"
#include "qgsrectangle.h"
#include "qgslogger.h"
#include "qgsdxfexport.h"
#include "qgsmapthemes.h"
#include "qgsvectorlayer.h"
#include "qgis_app.h"
/** Print usage text
*/
void usage( const QString &appName )
{
QStringList msg;
msg
<< QStringLiteral( "QGIS - " ) << VERSION << QStringLiteral( " '" ) << RELEASE_NAME << QStringLiteral( "' (" )
<< QGSVERSION << QStringLiteral( ")\n" )
<< QStringLiteral( "QGIS is a user friendly Open Source Geographic Information System.\n" )
<< QStringLiteral( "Usage: " ) << appName << QStringLiteral( " [OPTION] [FILE]\n" )
<< QStringLiteral( " OPTION:\n" )
<< QStringLiteral( "\t[--snapshot filename]\temit snapshot of loaded datasets to given file\n" )
<< QStringLiteral( "\t[--width width]\twidth of snapshot to emit\n" )
<< QStringLiteral( "\t[--height height]\theight of snapshot to emit\n" )
<< QStringLiteral( "\t[--lang language]\tuse language for interface text\n" )
<< QStringLiteral( "\t[--project projectfile]\tload the given QGIS project\n" )
<< QStringLiteral( "\t[--extent xmin,ymin,xmax,ymax]\tset initial map extent\n" )
<< QStringLiteral( "\t[--nologo]\thide splash screen\n" )
<< QStringLiteral( "\t[--noversioncheck]\tdon't check for new version of QGIS at startup\n" )
<< QStringLiteral( "\t[--noplugins]\tdon't restore plugins on startup\n" )
<< QStringLiteral( "\t[--nocustomization]\tdon't apply GUI customization\n" )
<< QStringLiteral( "\t[--customizationfile path]\tuse the given ini file as GUI customization\n" )
<< QStringLiteral( "\t[--globalsettingsfile path]\tuse the given ini file as Global Settings (defaults)\n" )
<< QStringLiteral( "\t[--optionspath path]\tuse the given QgsSettings path\n" )
<< QStringLiteral( "\t[--configpath path]\tuse the given path for all user configuration\n" )
<< QStringLiteral( "\t[--authdbdirectory path] use the given directory for authentication database\n" )
<< QStringLiteral( "\t[--code path]\trun the given python file on load\n" )
<< QStringLiteral( "\t[--defaultui]\tstart by resetting user ui settings to default\n" )
<< QStringLiteral( "\t[--dxf-export filename.dxf]\temit dxf output of loaded datasets to given file\n" )
<< QStringLiteral( "\t[--dxf-extent xmin,ymin,xmax,ymax]\tset extent to export to dxf\n" )
<< QStringLiteral( "\t[--dxf-symbology-mode none|symbollayer|feature]\tsymbology mode for dxf output\n" )
<< QStringLiteral( "\t[--dxf-scale-denom scale]\tscale for dxf output\n" )
<< QStringLiteral( "\t[--dxf-encoding encoding]\tencoding to use for dxf output\n" )
<< QStringLiteral( "\t[--dxf-preset maptheme]\tmap theme to use for dxf output\n" )
<< QStringLiteral( "\t[--help]\t\tthis text\n" )
<< QStringLiteral( "\t[--]\t\ttreat all following arguments as FILEs\n\n" )
<< QStringLiteral( " FILE:\n" )
<< QStringLiteral( " Files specified on the command line can include rasters,\n" )
<< QStringLiteral( " vectors, and QGIS project files (.qgs): \n" )
<< QStringLiteral( " 1. Rasters - supported formats include GeoTiff, DEM \n" )
<< QStringLiteral( " and others supported by GDAL\n" )
<< QStringLiteral( " 2. Vectors - supported formats include ESRI Shapefiles\n" )
<< QStringLiteral( " and others supported by OGR and PostgreSQL layers using\n" )
<< QStringLiteral( " the PostGIS extension\n" ) ; // OK
#ifdef Q_OS_WIN
MessageBox( nullptr,
msg.join( QString() ).toLocal8Bit().constData(),
"QGIS command line options",
MB_OK );
#else
std::cerr << msg.join( QString() ).toLocal8Bit().constData();
#endif
} // usage()
/////////////////////////////////////////////////////////////////
// Command line options 'behavior' flag setup
////////////////////////////////////////////////////////////////
// These two are global so that they can be set by the OpenDocuments
// AppleEvent handler as well as by the main routine argv processing
// This behavior will cause QGIS to autoload a project
static QString sProjectFileName = QLatin1String( "" );
// This is the 'leftover' arguments collection
static QStringList sFileList;
/* Test to determine if this program was started on Mac OS X by double-clicking
* the application bundle rather then from a command line. If clicked, argv[1]
* contains a process serial number in the form -psn_0_1234567. Don't process
* the command line arguments in this case because argv[1] confuses the processing.
*/
bool bundleclicked( int argc, char *argv[] )
{
return ( argc > 1 && memcmp( argv[1], "-psn_", 5 ) == 0 );
}
void myPrint( const char *fmt, ... )
{
va_list ap;
va_start( ap, fmt );
#if defined(Q_OS_WIN)
char buffer[1024];
vsnprintf( buffer, sizeof buffer, fmt, ap );
OutputDebugString( buffer );
#else
vfprintf( stderr, fmt, ap );
#endif
va_end( ap );
}
static void dumpBacktrace( unsigned int depth )
{
if ( depth == 0 )
depth = 20;
#if ((defined(linux) || defined(__linux__)) && !defined(ANDROID)) || defined(__FreeBSD__)
// Below there is a bunch of operations that are not safe in multi-threaded
// environment (dup()+close() combo, wait(), juggling with file descriptors).
// Maybe some problems could be resolved with dup2() and waitpid(), but it seems
// that if the operations on descriptors are not serialized, things will get nasty.
// That's why there's this lovely mutex here...
static QMutex sMutex;
QMutexLocker locker( &sMutex );
int stderr_fd = -1;
if ( access( "/usr/bin/c++filt", X_OK ) < 0 )
{
myPrint( "Stacktrace (c++filt NOT FOUND):\n" );
}
else
{
int fd[2];
if ( pipe( fd ) == 0 && fork() == 0 )
{
close( STDIN_FILENO ); // close stdin
// stdin from pipe
if ( dup( fd[0] ) != STDIN_FILENO )
{
QgsDebugMsg( "dup to stdin failed" );
}
close( fd[1] ); // close writing end
execl( "/usr/bin/c++filt", "c++filt", static_cast< char * >( nullptr ) );
perror( "could not start c++filt" );
exit( 1 );
}
myPrint( "Stacktrace (piped through c++filt):\n" );
stderr_fd = dup( STDERR_FILENO );
close( fd[0] ); // close reading end
close( STDERR_FILENO ); // close stderr
// stderr to pipe
int stderr_new = dup( fd[1] );
if ( stderr_new != STDERR_FILENO )
{
if ( stderr_new >= 0 )
close( stderr_new );
QgsDebugMsg( "dup to stderr failed" );
}
close( fd[1] ); // close duped pipe
}
void **buffer = new void *[ depth ];
int nptrs = backtrace( buffer, depth );
backtrace_symbols_fd( buffer, nptrs, STDERR_FILENO );
delete [] buffer;
if ( stderr_fd >= 0 )
{
int status;
close( STDERR_FILENO );
int dup_stderr = dup( stderr_fd );
if ( dup_stderr != STDERR_FILENO )
{
close( dup_stderr );
QgsDebugMsg( "dup to stderr failed" );
}
close( stderr_fd );
wait( &status );
}
#elif defined(Q_OS_WIN)
void **buffer = new void *[ depth ];
SymSetOptions( SYMOPT_DEFERRED_LOADS | SYMOPT_INCLUDE_32BIT_MODULES | SYMOPT_UNDNAME );
SymInitialize( GetCurrentProcess(), "http://msdl.microsoft.com/download/symbols;http://download.osgeo.org/osgeo4w/symstore", TRUE );
unsigned short nFrames = CaptureStackBackTrace( 1, depth, buffer, nullptr );
SYMBOL_INFO *symbol = ( SYMBOL_INFO * ) qgsMalloc( sizeof( SYMBOL_INFO ) + 256 );
symbol->MaxNameLen = 255;
symbol->SizeOfStruct = sizeof( SYMBOL_INFO );
IMAGEHLP_LINE *line = ( IMAGEHLP_LINE * ) qgsMalloc( sizeof( IMAGEHLP_LINE ) );
line->SizeOfStruct = sizeof( IMAGEHLP_LINE );
for ( int i = 0; i < nFrames; i++ )
{
DWORD dwDisplacement;
SymFromAddr( GetCurrentProcess(), ( DWORD64 )( buffer[ i ] ), 0, symbol );
symbol->Name[ 255 ] = 0;
if ( SymGetLineFromAddr( GetCurrentProcess(), ( DWORD64 )( buffer[i] ), &dwDisplacement, line ) )
{
myPrint( "%s(%d) : (%s) frame %d, address %x\n", line->FileName, line->LineNumber, symbol->Name, i, symbol->Address );
}
else
{
myPrint( "%s(%d) : (%s) unknown source location, frame %d, address %x [GetLastError()=%d]\n", __FILE__, __LINE__, symbol->Name, i, symbol->Address, GetLastError() );
}
}
qgsFree( symbol );
qgsFree( line );
#else
Q_UNUSED( depth );
#endif
}
#if (defined(linux) && !defined(ANDROID)) || defined(__FreeBSD__)
void qgisCrash( int signal )
{
fprintf( stderr, "QGIS died on signal %d", signal );
if ( access( "/usr/bin/gdb", X_OK ) == 0 )
{
// take full stacktrace using gdb
// http://stackoverflow.com/questions/3151779/how-its-better-to-invoke-gdb-from-program-to-print-its-stacktrace
// unfortunately, this is not so simple. the proper method is way more OS-specific
// than this code would suggest, see http://stackoverflow.com/a/1024937
char exename[512];
#if defined(__FreeBSD__)
int len = readlink( "/proc/curproc/file", exename, sizeof( exename ) - 1 );
#else
int len = readlink( "/proc/self/exe", exename, sizeof( exename ) - 1 );
#endif
if ( len < 0 )
{
myPrint( "Could not read link (%d:%s)\n", errno, strerror( errno ) );
}
else
{
exename[ len ] = 0;
char pidstr[32];
snprintf( pidstr, sizeof pidstr, "--pid=%d", getpid() );
int gdbpid = fork();
if ( gdbpid == 0 )
{
// attach, backtrace and continue
execl( "/usr/bin/gdb", "gdb", "-q", "-batch", "-n", pidstr, "-ex", "thread", "-ex", "bt full", exename, NULL );
perror( "cannot exec gdb" );
exit( 1 );
}
else if ( gdbpid >= 0 )
{
int status;
waitpid( gdbpid, &status, 0 );
myPrint( "gdb returned %d\n", status );
}
else
{
myPrint( "Cannot fork (%d:%s)\n", errno, strerror( errno ) );
dumpBacktrace( 256 );
}
}
}
abort();
}
#endif
/*
* Hook into the qWarning/qFatal mechanism so that we can channel messages
* from libpng to the user.
*
* Some JPL WMS images tend to overload the libpng 1.2.2 implementation
* somehow (especially when zoomed in)
* and it would be useful for the user to know why their picture turned up blank
*
* Based on qInstallMsgHandler example code in the Qt documentation.
*
*/
void myMessageOutput( QtMsgType type, const char *msg )
{
switch ( type )
{
case QtDebugMsg:
myPrint( "%s\n", msg );
if ( strncmp( msg, "Backtrace", 9 ) == 0 )
dumpBacktrace( atoi( msg + 9 ) );
break;
case QtCriticalMsg:
myPrint( "Critical: %s\n", msg );
break;
case QtWarningMsg:
myPrint( "Warning: %s\n", msg );
#ifdef QGISDEBUG
// Print all warnings except setNamedColor.
// Only seems to happen on windows
if ( 0 != strncmp( msg, "QColor::setNamedColor: Unknown color name 'param", 48 ) )
{
// TODO: Verify this code in action.
dumpBacktrace( 20 );
QgsMessageLog::logMessage( msg, QStringLiteral( "Qt" ) );
}
#endif
// TODO: Verify this code in action.
if ( 0 == strncmp( msg, "libpng error:", 13 ) )
{
// Let the user know
QgsMessageLog::logMessage( msg, QStringLiteral( "libpng" ) );
}
break;
case QtFatalMsg:
{
myPrint( "Fatal: %s\n", msg );
#if (defined(linux) && !defined(ANDROID)) || defined(__FreeBSD__)
qgisCrash( -1 );
#else
dumpBacktrace( 256 );
abort(); // deliberately dump core
#endif
}
#if QT_VERSION >= 0x050500
case QtInfoMsg:
myPrint( "Info: %s\n", msg );
break;
#endif
}
}
#ifdef _MSC_VER
#undef APP_EXPORT
#define APP_EXPORT __declspec(dllexport)
#endif
#if defined(ANDROID) || defined(Q_OS_WIN)
// On Android, there there is a libqgis.so instead of a qgis executable.
// The main method symbol of this library needs to be exported so it can be called by java
// On Windows this main is included in qgis_app and called from mainwin.cpp
APP_EXPORT
#endif
int main( int argc, char *argv[] )
{
#ifdef Q_OS_MACX
// Increase file resource limits (i.e., number of allowed open files)
// (from code provided by Larry Biehl, Purdue University, USA, from 'MultiSpec' project)
// This is generally 256 for the soft limit on Mac
// NOTE: setrlimit() must come *before* initialization of stdio strings,
// e.g. before any debug messages, or setrlimit() gets ignored
// see: http://stackoverflow.com/a/17726104/2865523
struct rlimit rescLimit;
if ( getrlimit( RLIMIT_NOFILE, &rescLimit ) == 0 )
{
rlim_t oldSoft( rescLimit.rlim_cur );
rlim_t oldHard( rescLimit.rlim_max );
#ifdef OPEN_MAX
rlim_t newSoft( OPEN_MAX );
rlim_t newHard( std::min( oldHard, newSoft ) );
#else
rlim_t newSoft( 4096 );
rlim_t newHard( std::min( ( rlim_t )8192, oldHard ) );
#endif
if ( rescLimit.rlim_cur < newSoft )
{
rescLimit.rlim_cur = newSoft;
rescLimit.rlim_max = newHard;
if ( setrlimit( RLIMIT_NOFILE, &rescLimit ) == 0 )
{
QgsDebugMsg( QString( "Mac RLIMIT_NOFILE Soft/Hard NEW: %1 / %2" )
.arg( rescLimit.rlim_cur ).arg( rescLimit.rlim_max ) );
}
}
Q_UNUSED( oldSoft ); //avoid warnings
QgsDebugMsg( QString( "Mac RLIMIT_NOFILE Soft/Hard ORIG: %1 / %2" )
.arg( oldSoft ).arg( oldHard ) );
}
#endif
QgsDebugMsg( QString( "Starting qgis main" ) );
#ifdef WIN32 // Windows
#ifdef _MSC_VER
_set_fmode( _O_BINARY );
#else //MinGW
_fmode = _O_BINARY;
#endif // _MSC_VER
#endif // WIN32
// Set up the custom qWarning/qDebug custom handler
#ifndef ANDROID
qInstallMsgHandler( myMessageOutput );
#endif
#if (defined(linux) && !defined(ANDROID)) || defined(__FreeBSD__)
signal( SIGQUIT, qgisCrash );
signal( SIGILL, qgisCrash );
signal( SIGFPE, qgisCrash );
signal( SIGSEGV, qgisCrash );
signal( SIGBUS, qgisCrash );
signal( SIGSYS, qgisCrash );
signal( SIGTRAP, qgisCrash );
signal( SIGXCPU, qgisCrash );
signal( SIGXFSZ, qgisCrash );
#endif
#ifdef Q_OS_WIN
SetUnhandledExceptionFilter( QgisApp::qgisCrashDump );
#endif
// initialize random number seed
qsrand( time( nullptr ) );
/////////////////////////////////////////////////////////////////
// Command line options 'behavior' flag setup
////////////////////////////////////////////////////////////////
//
// Parse the command line arguments, looking to see if the user has asked for any
// special behaviors. Any remaining non command arguments will be kept aside to
// be passed as a list of layers and / or a project that should be loaded.
//
// This behavior is used to load the app, snapshot the map,
// save the image to disk and then exit
QString mySnapshotFileName = QLatin1String( "" );
int mySnapshotWidth = 800;
int mySnapshotHeight = 600;
bool myHideSplash = false;
bool mySkipVersionCheck = false;
#if defined(ANDROID)
QgsDebugMsg( QString( "Android: Splash hidden" ) );
myHideSplash = true;
#endif
bool myRestoreDefaultWindowState = false;
bool myRestorePlugins = true;
bool myCustomization = true;
QString dxfOutputFile;
QgsDxfExport::SymbologyExport dxfSymbologyMode = QgsDxfExport::SymbolLayerSymbology;
double dxfScaleDenom = 50000.0;
QString dxfEncoding = QStringLiteral( "CP1252" );
QString dxfPreset;
QgsRectangle dxfExtent;
// This behavior will set initial extent of map canvas, but only if
// there are no command line arguments. This gives a usable map
// extent when qgis starts with no layers loaded. When layers are
// loaded, we let the layers define the initial extent.
QString myInitialExtent = QLatin1String( "" );
if ( argc == 1 )
myInitialExtent = QStringLiteral( "-1,-1,1,1" );
// This behavior will allow you to force the use of a translation file
// which is useful for testing
QString myTranslationCode;
// The user can specify a path which will override the default path of custom
// user settings (~/.qgis) and it will be used for QgsSettings INI file
QString configpath;
QString optionpath;
QString authdbdirectory;
QString pythonfile;
QString customizationfile;
QString globalsettingsfile;
#if defined(ANDROID)
QgsDebugMsg( QString( "Android: All params stripped" ) );// Param %1" ).arg( argv[0] ) );
//put all QGIS settings in the same place
configpath = QgsApplication::qgisSettingsDirPath();
QgsDebugMsg( QString( "Android: configpath set to %1" ).arg( configpath ) );
#endif
QStringList args;
if ( !bundleclicked( argc, argv ) )
{
// Build a local QCoreApplication from arguments. This way, arguments are correctly parsed from their native locale
// It will use QString::fromLocal8Bit( argv ) under Unix and GetCommandLine() under Windows.
QCoreApplication coreApp( argc, argv );
args = QCoreApplication::arguments();
for ( int i = 1; i < args.size(); ++i )
{
const QString &arg = args[i];
if ( arg == QLatin1String( "--help" ) || arg == QLatin1String( "-?" ) )
{
usage( args[0] );
return 2;
}
else if ( arg == QLatin1String( "--nologo" ) || arg == QLatin1String( "-n" ) )
{
myHideSplash = true;
}
else if ( arg == QLatin1String( "--noversioncheck" ) || arg == QLatin1String( "-V" ) )
{
mySkipVersionCheck = true;
}
else if ( arg == QLatin1String( "--noplugins" ) || arg == QLatin1String( "-P" ) )
{
myRestorePlugins = false;
}
else if ( arg == QLatin1String( "--nocustomization" ) || arg == QLatin1String( "-C" ) )
{
myCustomization = false;
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--snapshot" ) || arg == QLatin1String( "-s" ) ) )
{
mySnapshotFileName = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--width" ) || arg == QLatin1String( "-w" ) ) )
{
mySnapshotWidth = QString( args[++i] ).toInt();
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--height" ) || arg == QLatin1String( "-h" ) ) )
{
mySnapshotHeight = QString( args[++i] ).toInt();
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--lang" ) || arg == QLatin1String( "-l" ) ) )
{
myTranslationCode = args[++i];
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--project" ) || arg == QLatin1String( "-p" ) ) )
{
sProjectFileName = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--extent" ) || arg == QLatin1String( "-e" ) ) )
{
myInitialExtent = args[++i];
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--optionspath" ) || arg == QLatin1String( "-o" ) ) )
{
optionpath = QDir::toNativeSeparators( QDir( args[++i] ).absolutePath() );
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--configpath" ) || arg == QLatin1String( "-c" ) ) )
{
configpath = QDir::toNativeSeparators( QDir( args[++i] ).absolutePath() );
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--authdbdirectory" ) || arg == QLatin1String( "-a" ) ) )
{
authdbdirectory = QDir::toNativeSeparators( QDir( args[++i] ).absolutePath() );
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--code" ) || arg == QLatin1String( "-f" ) ) )
{
pythonfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--customizationfile" ) || arg == QLatin1String( "-z" ) ) )
{
customizationfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
}
else if ( i + 1 < argc && ( arg == QLatin1String( "--globalsettingsfile" ) || arg == QLatin1String( "-g" ) ) )
{
globalsettingsfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
}
else if ( arg == QLatin1String( "--defaultui" ) || arg == QLatin1String( "-d" ) )
{
myRestoreDefaultWindowState = true;
}
else if ( arg == QLatin1String( "--dxf-export" ) )
{
dxfOutputFile = args[++i];
}
else if ( arg == QLatin1String( "--dxf-extent" ) )
{
QgsLocaleNumC l;
QString ext( args[++i] );
QStringList coords( ext.split( ',' ) );
if ( coords.size() != 4 )
{
std::cerr << "invalid dxf extent " << ext.toStdString() << std::endl;
return 2;
}
for ( int i = 0; i < 4; i++ )
{
bool ok;
double d;
d = coords[i].toDouble( &ok );
if ( !ok )
{
std::cerr << "invalid dxf coordinate " << coords[i].toStdString() << " in extent " << ext.toStdString() << std::endl;
return 2;
}
switch ( i )
{
case 0:
dxfExtent.setXMinimum( d );
break;
case 1:
dxfExtent.setYMinimum( d );
break;
case 2:
dxfExtent.setXMaximum( d );
break;
case 3:
dxfExtent.setYMaximum( d );
break;
}
}
}
else if ( arg == QLatin1String( "--dxf-symbology-mode" ) )
{
QString mode( args[++i] );
if ( mode == QLatin1String( "none" ) )
{
dxfSymbologyMode = QgsDxfExport::NoSymbology;
}
else if ( mode == QLatin1String( "symbollayer" ) )
{
dxfSymbologyMode = QgsDxfExport::SymbolLayerSymbology;
}
else if ( mode == QLatin1String( "feature" ) )
{
dxfSymbologyMode = QgsDxfExport::FeatureSymbology;
}
else
{
std::cerr << "invalid dxf symbology mode " << mode.toStdString() << std::endl;
return 2;
}
}
else if ( arg == QLatin1String( "--dxf-scale-denom" ) )
{
bool ok;
QString scale( args[++i] );
dxfScaleDenom = scale.toDouble( &ok );
if ( !ok )
{
std::cerr << "invalid dxf scale " << scale.toStdString() << std::endl;
return 2;
}
}
else if ( arg == QLatin1String( "--dxf-encoding" ) )
{
dxfEncoding = args[++i];
}
else if ( arg == QLatin1String( "--dxf-preset" ) )
{
dxfPreset = args[++i];
}
else if ( arg == QLatin1String( "--" ) )
{
for ( i++; i < args.size(); ++i )
sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) );
}
else
{
sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) );
}
}
}
/////////////////////////////////////////////////////////////////////
// If no --project was specified, parse the args to look for a //
// .qgs file and set myProjectFileName to it. This allows loading //
// of a project file by clicking on it in various desktop managers //
// where an appropriate mime-type has been set up. //
/////////////////////////////////////////////////////////////////////
if ( sProjectFileName.isEmpty() )
{
// check for a .qgs
for ( int i = 0; i < args.size(); i++ )
{
QString arg = QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() );
if ( arg.contains( QLatin1String( ".qgs" ) ) )
{
sProjectFileName = arg;
break;
}
}
}
/////////////////////////////////////////////////////////////////////
// Now we have the handlers for the different behaviors...
////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// Initialize the application and the translation stuff
/////////////////////////////////////////////////////////////////////
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(ANDROID)
bool myUseGuiFlag = nullptr != getenv( "DISPLAY" );
#else
bool myUseGuiFlag = true;
#endif
if ( !myUseGuiFlag )
{
std::cerr << QObject::tr(
"QGIS starting in non-interactive mode not supported.\n"
"You are seeing this message most likely because you "
"have no DISPLAY environment variable set.\n"
).toUtf8().constData();
exit( 1 ); //exit for now until a version of qgis is capabable of running non interactive
}
if ( !optionpath.isEmpty() || !configpath.isEmpty() )
{
// tell QgsSettings to use INI format and save the file in custom config path
QSettings::setDefaultFormat( QSettings::IniFormat );
QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, optionpath.isEmpty() ? configpath : optionpath );
}
// GUI customization is enabled according to settings (loaded when instance is created)
// we force disabled here if --nocustomization argument is used
if ( !myCustomization )
{
QgsCustomization::instance()->setEnabled( false );
}
QgsApplication myApp( argc, argv, myUseGuiFlag, configpath );
#ifdef Q_OS_MAC
// Set 1024x1024 icon for dock, app switcher, etc., rendering
myApp.setWindowIcon( QIcon( QgsApplication::iconsPath() + QStringLiteral( "qgis-icon-macos.png" ) ) );
#else
myApp.setWindowIcon( QIcon( QgsApplication::appIconPath() ) );
#endif
//
// Set up the QSettings environment must be done after qapp is created
QCoreApplication::setOrganizationName( QgsApplication::QGIS_ORGANIZATION_NAME );
QCoreApplication::setOrganizationDomain( QgsApplication::QGIS_ORGANIZATION_DOMAIN );
QCoreApplication::setApplicationName( QgsApplication::QGIS_APPLICATION_NAME );
QCoreApplication::setAttribute( Qt::AA_DontShowIconsInMenus, false );
// SetUp the QgsSettings Global Settings:
// - use the path specified with --globalsettings path,
// - use the environment if not found
// - use a default location as a fallback
if ( globalsettingsfile.isEmpty( ) )
{
globalsettingsfile = getenv( "QGIS_GLOBAL_SETTINGS_FILE" );
}
if ( globalsettingsfile.isEmpty( ) )
{
QString default_globalsettingsfile = QgsApplication::pkgDataPath( ) + "/qgis_global_settings.ini";
if ( QFile::exists( default_globalsettingsfile ) )
{
globalsettingsfile = default_globalsettingsfile;
}
}
if ( !globalsettingsfile.isEmpty() )
{
if ( ! QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
{
QgsMessageLog::logMessage( QString( "Invalid globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
}
else
{
QgsMessageLog::logMessage( QString( "Successfully loaded globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
}
}
// TODO: use QgsSettings
QSettings *customizationsettings = nullptr;
if ( !optionpath.isEmpty() || !configpath.isEmpty() )
{
// tell QgsSettings to use INI format and save the file in custom config path
QSettings::setDefaultFormat( QSettings::IniFormat );
QString path = optionpath.isEmpty() ? configpath : optionpath;
QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, path );
customizationsettings = new QSettings( QSettings::IniFormat, QSettings::UserScope, QStringLiteral( "QGIS" ), QStringLiteral( "QGISCUSTOMIZATION2" ) );
}
else
{
customizationsettings = new QSettings( QStringLiteral( "QGIS" ), QStringLiteral( "QGISCUSTOMIZATION2" ) );
}
// Using the customizationfile option always overrides the option and config path options.
if ( !customizationfile.isEmpty() )
{
customizationsettings = new QSettings( customizationfile, QSettings::IniFormat );
QgsCustomization::instance()->setEnabled( true );
}
// Load and set possible default customization, must be done afterQgsApplication init and QgsSettings ( QCoreApplication ) init
QgsCustomization::instance()->setSettings( customizationsettings );
QgsCustomization::instance()->loadDefault();
#ifdef Q_OS_MACX
// If the GDAL plugins are bundled with the application and GDAL_DRIVER_PATH
// is not already defined, use the GDAL plugins in the application bundle.
QString gdalPlugins( QCoreApplication::applicationDirPath().append( "/lib/gdalplugins" ) );
if ( QFile::exists( gdalPlugins ) && !getenv( "GDAL_DRIVER_PATH" ) )
{
setenv( "GDAL_DRIVER_PATH", gdalPlugins.toUtf8(), 1 );
}
// Point GDAL_DATA at any GDAL share directory embedded in the app bundle
if ( !getenv( "GDAL_DATA" ) )
{
QStringList gdalShares;
QString appResources( QDir::cleanPath( QgsApplication::pkgDataPath() ) );
gdalShares << QCoreApplication::applicationDirPath().append( "/share/gdal" )
<< appResources.append( "/share/gdal" )
<< appResources.append( "/gdal" );
Q_FOREACH ( const QString &gdalShare, gdalShares )
{
if ( QFile::exists( gdalShare ) )
{
setenv( "GDAL_DATA", gdalShare.toUtf8().constData(), 1 );
break;
}
}
}
#endif
QgsSettings mySettings;
// update any saved setting for older themes to new default 'gis' theme (2013-04-15)
if ( mySettings.contains( QStringLiteral( "/Themes" ) ) )
{
QString theme = mySettings.value( QStringLiteral( "Themes" ), "default" ).toString();
if ( theme == QLatin1String( "gis" )
|| theme == QLatin1String( "classic" )
|| theme == QLatin1String( "nkids" ) )
{
mySettings.setValue( QStringLiteral( "Themes" ), QStringLiteral( "default" ) );
}
}
// custom environment variables
QMap<QString, QString> systemEnvVars = QgsApplication::systemEnvVars();
bool useCustomVars = mySettings.value( QStringLiteral( "qgis/customEnvVarsUse" ), QVariant( false ) ).toBool();
if ( useCustomVars )
{
QStringList customVarsList = mySettings.value( QStringLiteral( "qgis/customEnvVars" ), "" ).toStringList();
if ( !customVarsList.isEmpty() )
{
Q_FOREACH ( const QString &varStr, customVarsList )
{
int pos = varStr.indexOf( QLatin1Char( '|' ) );
if ( pos == -1 )
continue;
QString envVarApply = varStr.left( pos );
QString varStrNameValue = varStr.mid( pos + 1 );
pos = varStrNameValue.indexOf( QLatin1Char( '=' ) );
if ( pos == -1 )
continue;
QString envVarName = varStrNameValue.left( pos );
QString envVarValue = varStrNameValue.mid( pos + 1 );
if ( systemEnvVars.contains( envVarName ) )
{
if ( envVarApply == QLatin1String( "prepend" ) )
{
envVarValue += systemEnvVars.value( envVarName );
}
else if ( envVarApply == QLatin1String( "append" ) )
{
envVarValue = systemEnvVars.value( envVarName ) + envVarValue;
}
}
if ( systemEnvVars.contains( envVarName ) && envVarApply == QLatin1String( "unset" ) )
{
#ifdef Q_OS_WIN
putenv( envVarName.toUtf8().constData() );
#else
unsetenv( envVarName.toUtf8().constData() );
#endif
}
else
{
#ifdef Q_OS_WIN
if ( envVarApply != "undefined" || !getenv( envVarName.toUtf8().constData() ) )
putenv( QString( "%1=%2" ).arg( envVarName ).arg( envVarValue ).toUtf8().constData() );
#else
setenv( envVarName.toUtf8().constData(), envVarValue.toUtf8().constData(), envVarApply == QLatin1String( "undefined" ) ? 0 : 1 );
#endif
}
}
}
}
#ifdef QGISDEBUG
QgsFontUtils::loadStandardTestFonts( QStringList() << QStringLiteral( "Roman" ) << QStringLiteral( "Bold" ) );
#endif
// Set the application style. If it's not set QT will use the platform style except on Windows
// as it looks really ugly so we use QPlastiqueStyle.
QString style = mySettings.value( QStringLiteral( "qgis/style" ) ).toString();
if ( !style.isNull() )
{
QApplication::setStyle( style );
mySettings.setValue( QStringLiteral( "qgis/style" ), QApplication::style()->objectName() );
}
/* Translation file for QGIS.
*/
QString i18nPath = QgsApplication::i18nPath();
QString myUserLocale = mySettings.value( QStringLiteral( "locale/userLocale" ), "" ).toString();
bool myLocaleOverrideFlag = mySettings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool();
//
// Priority of translation is:
// - command line
// - user specified in options dialog (with group checked on)