28 #include <QFileOpenEvent>
29 #include <QMessageBox>
35 #include <QThreadPool>
38 #include <netinet/in.h>
43 #include "qgsconfig.h"
61 bool ABISYM( QgsApplication::mRunningFromBuildDir ) =
false;
68 int ABISYM( QgsApplication::mMaxThreads );
90 init( customConfigPath );
95 if ( customConfigPath.
isEmpty() )
97 if ( getenv(
"QGIS_CUSTOM_CONFIG_PATH" ) )
99 customConfigPath = getenv(
"QGIS_CUSTOM_CONFIG_PATH" );
107 qRegisterMetaType<QgsGeometry::Error>(
"QgsGeometry::Error" );
117 f.
setFileName( prefixPath + path +
"/qgisbuildpath.txt" );
121 if ( f.
exists() && f.
open( QIODevice::ReadOnly ) )
123 ABISYM( mRunningFromBuildDir ) =
true;
124 ABISYM( mBuildSourcePath ) = f.
readLine().trimmed();
125 ABISYM( mBuildOutputPath ) = f.
readLine().trimmed();
126 qDebug(
"Running from build directory!" );
127 qDebug(
"- source directory: %s", ABISYM( mBuildSourcePath ).toUtf8().data() );
128 qDebug(
"- output directory of the build: %s", ABISYM( mBuildOutputPath ).toUtf8().data() );
130 ABISYM( mCfgIntDir ) = prefixPath.split(
"/", QString::SkipEmptyParts ).last();
131 qDebug(
"- cfg: %s", ABISYM( mCfgIntDir ).toUtf8().data() );
135 if ( ABISYM( mRunningFromBuildDir ) )
138 ABISYM( mPrefixPath ) =
QString();
139 #if defined(_MSC_VER) && !defined(USING_NMAKE)
140 setPluginPath( ABISYM( mBuildOutputPath ) +
"/" +
QString( QGIS_PLUGIN_SUBDIR ) +
"/" + ABISYM( mCfgIntDir ) );
145 ABISYM( mLibraryPath ) = ABISYM( mBuildOutputPath ) +
"/" + QGIS_LIB_SUBDIR +
"/";
146 #if defined(_MSC_VER) && !defined(USING_NMAKE)
147 ABISYM( mLibexecPath ) = ABISYM( mBuildOutputPath ) +
"/" + QGIS_LIBEXEC_SUBDIR +
"/" + ABISYM( mCfgIntDir ) +
"/";
149 ABISYM( mLibexecPath ) = ABISYM( mBuildOutputPath ) +
"/" + QGIS_LIBEXEC_SUBDIR +
"/";
154 char *prefixPath = getenv(
"QGIS_PREFIX_PATH" );
157 #if defined(Q_OS_MACX) || defined(Q_OS_WIN32) || defined(WIN32)
159 #elif defined(ANDROID)
178 if ( !customConfigPath.
isEmpty() )
180 ABISYM( mConfigPath ) = customConfigPath +
"/";
194 systemEnvVarMap.
insert( varStrName, varStrValue );
196 ABISYM( mSystemEnvVars ) = systemEnvVarMap;
204 ABISYM( mMaxThreads ) = -1;
214 if ( event->
type() == QEvent::FileOpen )
217 if ( ABISYM( mFileOpenEventReceiver ) )
220 done =
notify( ABISYM( mFileOpenEventReceiver ), event );
227 ABISYM( mFileOpenEventList ).append( static_cast<QFileOpenEvent *>( event )->file() );
244 emit
preNotify( receiver, event, &done );
261 catch ( std::exception & e )
269 QgsDebugMsg(
"Caught unhandled unknown exception" );
280 ABISYM( mFileOpenEventReceiver ) = receiver;
282 if ( ABISYM( mFileOpenEventList ).count() > 0 )
284 QStringListIterator i( ABISYM( mFileOpenEventList ) );
285 while ( i.hasNext() )
290 ABISYM( mFileOpenEventList ).clear();
296 ABISYM( mPrefixPath ) = thePrefixPath;
297 #if defined(_MSC_VER)
298 if ( ABISYM( mPrefixPath ).endsWith(
"/bin" ) )
300 ABISYM( mPrefixPath ).chop( 4 );
303 if ( useDefaultPaths && !ABISYM( mRunningFromBuildDir ) )
308 ABISYM( mLibraryPath ) = ABISYM( mPrefixPath ) +
"/" + QGIS_LIB_SUBDIR +
"/";
309 ABISYM( mLibexecPath ) = ABISYM( mPrefixPath ) +
"/" + QGIS_LIBEXEC_SUBDIR +
"/";
314 ABISYM( mPluginPath ) = thePluginPath;
319 ABISYM( mPkgDataPath ) = thePkgDataPath;
320 QString mySvgPath = thePkgDataPath + ( ABISYM( mRunningFromBuildDir ) ?
"/images/svg/" :
"/svg/" );
322 if ( !ABISYM( mDefaultSvgPaths ).contains( mySvgPath ) )
323 ABISYM( mDefaultSvgPaths ) << mySvgPath;
328 ABISYM( mDefaultSvgPaths ) = pathList;
333 if ( ABISYM( mRunningFromBuildDir ) )
335 static bool once =
true;
337 qWarning(
"!!! prefix path was requested, but it is not valid - we do not run from installed path !!!" );
341 return ABISYM( mPrefixPath );
345 return ABISYM( mPluginPath );
349 return ABISYM( mPkgDataPath );
353 return ":/images/themes/default/";
357 return ":/images/themes/" +
themeName() +
"/";
366 return path + iconFile;
378 return QIcon( myPreferredPath );
384 return QIcon( myDefaultPath );
399 return QPixmap( myPreferredPath );
405 return QPixmap( myDefaultPath );
414 QString myPath =
":/images/themes/" + theThemeName +
"/";
418 ABISYM( mThemeName ) = theThemeName;
422 ABISYM( mThemeName ) =
"default";
430 return ABISYM( mThemeName );
437 return ABISYM( mPkgDataPath ) +
QString(
"/doc/AUTHORS" );
444 return ABISYM( mPkgDataPath ) +
QString(
"/doc/CONTRIBUTORS" );
448 return ABISYM( mPkgDataPath ) +
QString(
"/doc/developersmap.html" );
455 return ABISYM( mPkgDataPath ) +
QString(
"/doc/SPONSORS" );
463 return ABISYM( mPkgDataPath ) +
QString(
"/doc/DONORS" );
469 return ABISYM( mPkgDataPath ) +
QString(
"/doc/TRANSLATORS" );
475 return ABISYM( mPkgDataPath ) +
QString(
"/doc/LICENSE" );
489 helpAppPath +=
"/qgis_help";
491 helpAppPath +=
".exe";
500 if ( ABISYM( mRunningFromBuildDir ) )
501 return ABISYM( mBuildOutputPath ) +
QString(
"/i18n" );
503 return ABISYM( mPkgDataPath ) +
QString(
"/i18n/" );
511 return ABISYM( mPkgDataPath ) +
QString(
"/resources/qgis.db" );
519 return ABISYM( mConfigPath );
535 return QString(
":/images/splash/" );
543 return ABISYM( mPkgDataPath ) +
QString(
"/images/icons/" );
550 if ( ABISYM( mRunningFromBuildDir ) )
554 if ( !
QFile( tempCopy ).exists() )
556 QFile f( ABISYM( mPkgDataPath ) +
"/resources/srs.db" );
557 if ( !f.
copy( tempCopy ) )
559 qFatal(
"Could not create temporary copy" );
567 return ABISYM( mPkgDataPath ) +
QString(
"/resources/srs.db" );
583 myPathList = myPaths.
split(
"|" );
586 myPathList << ABISYM( mDefaultSvgPaths );
597 return ABISYM( mPkgDataPath ) +
QString(
"/resources/symbology-ng-style.db" );
602 return ABISYM( mLibraryPath );
607 return ABISYM( mLibexecPath );
612 return ( htonl( 1 ) == 1 ) ?
XDR :
NDR;
631 QString myEnvironmentVar( getenv(
"QGIS_PREFIX_PATH" ) );
632 QString myState =
tr(
"Application state:\n"
633 "QGIS_PREFIX_PATH env var:\t\t%1\n"
635 "Plugin Path:\t\t%3\n"
636 "Package Data Path:\t%4\n"
637 "Active Theme Name:\t%5\n"
638 "Active Theme Path:\t%6\n"
639 "Default Theme Path:\t%7\n"
640 "SVG Search Paths:\t%8\n"
641 "User DB Path:\t%9\n" )
642 .
arg( myEnvironmentVar )
649 .
arg(
svgPaths().join(
tr(
"\n\t\t",
"match indentation of application state" ) ) )
661 QColor myColor1( Qt::lightGray );
662 QColor myColor2 = myColor1;
663 myColor2 = myColor2.
lighter( 110 );
665 myStyle =
"p.glossy{ background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
666 " stop: 0 " + myColor1.
name() +
","
667 " stop: 0.1 " + myColor2.
name() +
","
668 " stop: 0.5 " + myColor1.
name() +
","
669 " stop: 0.9 " + myColor2.
name() +
","
670 " stop: 1 " + myColor1.
name() +
");"
672 " padding-left: 4px;"
673 " padding-top: 20px;"
674 " padding-bottom: 8px;"
675 " border: 1px solid #6c6c6c;"
677 "p.subheaderglossy{ background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
678 " stop: 0 " + myColor1.
name() +
","
679 " stop: 0.1 " + myColor2.
name() +
","
680 " stop: 0.5 " + myColor1.
name() +
","
681 " stop: 0.9 " + myColor2.
name() +
","
682 " stop: 1 " + myColor1.
name() +
");"
683 " font-weight: bold;"
684 " font-size: medium;"
685 " line-height: 1.1em;"
688 " padding-left: 4px;"
689 " padding-right: 4px;"
690 " padding-top: 20px;"
691 " padding-bottom: 8px;"
692 " border: 1px solid #6c6c6c;"
694 "th.glossy{ background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
695 " stop: 0 " + myColor1.
name() +
","
696 " stop: 0.1 " + myColor2.
name() +
","
697 " stop: 0.5 " + myColor1.
name() +
","
698 " stop: 0.9 " + myColor2.
name() +
","
699 " stop: 1 " + myColor1.
name() +
");"
701 " border: 1px solid #6c6c6c;"
703 ".overview{ font: 1.82em; font-weight: bold;}"
704 "body{ background: white;"
706 " font-family: arial,sans-serif;"
708 "h1{ background-color: #F6F6F6;"
710 " font-size: x-large; "
711 " font-weight: normal;"
712 " font-family: luxi serif, georgia, times new roman, times, serif;"
714 " padding: 0.75em 0 0;"
718 "h2{ background-color: #F6F6F6;"
720 " font-size: medium; "
721 " font-weight: normal;"
722 " font-family: luxi serif, georgia, times new roman, times, serif;"
724 " padding: 0.75em 0 0;"
726 " line-height: 1.1em;"
728 "h3{ background-color: #F6F6F6;"
730 " font-family: luxi serif, georgia, times new roman, times, serif;"
731 " font-weight: bold;"
733 " text-align: right;"
734 " border-bottom: 5px solid #DCEB5C;"
736 "h4{ background-color: #F6F6F6;"
738 " font-family: luxi serif, georgia, times new roman, times, serif;"
739 " font-weight: bold;"
740 " font-size: medium;"
741 " text-align: right;"
743 "h5{ background-color: #F6F6F6;"
745 " font-family: luxi serif, georgia, times new roman, times, serif;"
746 " font-weight: bold;"
748 " text-align: right;"
751 " font-family: arial,sans-serif;"
754 "label{ background-color: #FFFFCC;"
755 " border: 1px solid black;"
757 " padding: 0px 3px; "
765 if ( 0 >= OGRGetDriverCount() )
773 #if defined( Q_OS_WIN )
774 const Qt::CaseSensitivity cs = Qt::CaseInsensitive;
780 aPath =
"\\\\" + aPath.
mid( 2 );
783 targetPath.
replace(
"\\",
"/" );
787 targetPath =
"\\\\" + targetPath.
mid( 2 );
790 const Qt::CaseSensitivity cs = Qt::CaseSensitive;
801 while ( aPathElems.
size() > 0 &&
802 targetElems.
size() > 0 &&
803 aPathElems[0].compare( targetElems[0], cs ) == 0 )
816 if ( targetElems.
size() > 0 )
819 for (
int i = 0; i < targetElems.
size(); i++ )
821 aPathElems.
insert( 0,
".." );
828 aPathElems.
insert( 0,
"." );
831 return aPathElems.
join(
"/" );
842 #if defined(Q_OS_WIN)
844 targetPath.
replace(
"\\",
"/" );
852 #if defined(Q_OS_WIN)
855 targetElems.
insert( 0,
"" );
856 targetElems.
insert( 0,
"" );
861 targetElems << srcElems;
866 while (( pos = targetElems.
indexOf(
".." ) ) > 0 )
873 #if !defined(Q_OS_WIN)
878 return targetElems.
join(
"/" );
883 if ( ABISYM( mGdalSkipList ).contains( theDriver ) || theDriver.
isEmpty() )
887 ABISYM( mGdalSkipList ) << theDriver;
893 if ( !ABISYM( mGdalSkipList ).contains( theDriver ) )
897 int myPos = ABISYM( mGdalSkipList ).indexOf( theDriver );
900 ABISYM( mGdalSkipList ).removeAt( myPos );
907 ABISYM( mGdalSkipList ).removeDuplicates();
908 QString myDriverList = ABISYM( mGdalSkipList ).join(
" " );
911 CPLSetConfigOption(
"GDAL_SKIP", myDriverList.
toUtf8() );
921 QDir myDir( myPamPath );
924 myDir.
mkpath( myPamPath );
927 #if defined(Q_OS_WIN32) || defined(WIN32)
928 CPLSetConfigOption(
"GDAL_PAM_PROXY_DIR", myPamPath.
toUtf8() );
932 int myChangeFlag = 0;
933 setenv(
"GDAL_PAM_PROXY_DIR", myPamPath.
toUtf8(), myChangeFlag );
940 if ( !qgisPrivateDbFile.exists() )
944 QFile masterFile( qgisMasterDbFileName );
950 bool isDbFileCopied = masterFile.
copy( qgisPrivateDbFile.fileName() );
952 if ( !isDbFileCopied )
956 *errorMessage =
tr(
"[ERROR] Can not make qgis.db private copy" );
969 *errorMessage =
tr(
"Could not open qgis.db" );
975 int res = sqlite3_exec( db,
"SELECT epsg FROM tbl_srs LIMIT 0", 0, 0, &errmsg );
976 if ( res == SQLITE_OK )
979 if ( sqlite3_exec( db,
980 "ALTER TABLE tbl_srs RENAME TO tbl_srs_bak;"
981 "CREATE TABLE tbl_srs ("
982 "srs_id INTEGER PRIMARY KEY,"
983 "description text NOT NULL,"
984 "projection_acronym text NOT NULL,"
985 "ellipsoid_acronym NOT NULL,"
986 "parameters text NOT NULL,"
990 "is_geo integer NOT NULL,"
991 "deprecated boolean);"
992 "CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);"
993 "INSERT INTO tbl_srs(srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,auth_name,auth_id,is_geo,deprecated) SELECT srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,'','',is_geo,0 FROM tbl_srs_bak;"
994 "DROP TABLE tbl_srs_bak", 0, 0, &errmsg ) != SQLITE_OK
1001 sqlite3_free( errmsg );
1002 sqlite3_close( db );
1008 sqlite3_free( errmsg );
1011 if ( sqlite3_exec( db,
"DROP VIEW vw_srs", 0, 0, &errmsg ) != SQLITE_OK )
1013 QgsDebugMsg(
QString(
"vw_srs didn't exists in private qgis.db: %1" ).arg( errmsg ) );
1016 if ( sqlite3_exec( db,
1017 "CREATE VIEW vw_srs AS"
1019 " a.description AS description"
1020 ",a.srs_id AS srs_id"
1021 ",a.is_geo AS is_geo"
1022 ",coalesce(b.name,a.projection_acronym) AS name"
1023 ",a.parameters AS parameters"
1024 ",a.auth_name AS auth_name"
1025 ",a.auth_id AS auth_id"
1026 ",a.deprecated AS deprecated"
1028 " LEFT OUTER JOIN tbl_projection b ON a.projection_acronym=b.acronym"
1029 " ORDER BY coalesce(b.name,a.projection_acronym),a.description", 0, 0, &errmsg ) != SQLITE_OK
1034 *errorMessage =
tr(
"Update of view in private qgis.db failed.\n%1" ).
arg(
QString::fromUtf8( errmsg ) );
1036 sqlite3_free( errmsg );
1037 sqlite3_close( db );
1041 sqlite3_close( db );
1059 if ( maxThreads == -1 )
static const QString pkgDataPath()
Returns the common root path of all application data directories.
static void init(QString customConfigPath=QString())
This method initialises paths etc for QGIS.
QString fromAscii(const char *str, int size)
static const QString i18nPath()
Returns the path to the translation directory.
static const QString contributorsFilePath()
Returns the path to the contributors file.
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
enum QgsApplication::ENDIAN endian_t
constants for endian-ness
static const QString qgisMasterDbFilePath()
Returns the path to the master qgis.db file.
static const QString libexecPath()
Returns the path with utility executables (help viewer, crssync, ...)
static const QString activeThemePath()
Returns the path to the currently active theme directory.
QStringList systemEnvironment()
QStringList ABISYM(QgsApplication::mFileOpenEventList)
static const QString prefixPath()
Returns the path to the application prefix directory.
static const QString helpAppPath()
Returns the path to the help application.
static const QStringList svgPaths()
Returns the pathes to svg directories.
virtual bool event(QEvent *event) override
Watch for QFileOpenEvent.
static const QString licenceFilePath()
Returns the path to the licence file.
static QgsProviderRegistry * instance(QString pluginPath=QString::null)
means of accessing canonical single instance
static void setPrefixPath(const QString &thePrefixPath, bool useDefaultPaths=false)
Alters prefix path - used by 3rd party apps.
static QString iconPath(QString iconFile)
Returns path to the desired icon file.
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
static const QString libraryPath()
Returns the path containing qgis_core, qgis_gui, qgispython (and other) libraries.
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
QThreadPool * globalInstance()
void setFileName(const QString &name)
static void registerOgrDrivers()
Register OGR drivers ensuring this only happens once.
static const QString translatorsFilePath()
Returns the path to the sponsors file.
static const QString authorsFilePath()
Returns the path to the authors file.
QString join(const QString &separator) const
static QString absolutePathToRelativePath(QString apath, QString targetPath)
Converts absolute path to path relative to target.
static void setFileOpenEventReceiver(QObject *receiver)
Set the FileOpen event receiver.
static QString reportStyleSheet()
get a standard css style sheet for reports.
static int maxThreads()
Get maximum concurrent thread count.
static endian_t endian()
Returns whether this machine uses big or little endian.
static const QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
QString tr(const char *sourceText, const char *disambiguation, int n)
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
bool copy(const QString &newName)
void addLibraryPath(const QString &path)
static void applyGdalSkippedDrivers()
Apply the skipped drivers list to gdal.
QString fromUtf8(const char *str, int size)
void setMaxThreadCount(int maxThreadCount)
static void setThemeName(const QString &theThemeName)
Set the active theme to the specified theme.
static const QString iconsPath()
Returns the path to the icons image directory.
virtual ~QgsApplication()
int removeAll(const T &value)
bool sendEvent(QObject *receiver, QEvent *event)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
static const QString splashPath()
Returns the path to the splash screen image directory.
static const QString defaultThemePath()
Returns the path to the default theme directory.
static void setPkgDataPath(const QString &thePkgDataPath)
Alters pkg data path - used by 3rd party apps.
static void restoreGdalDriver(QString theDriver)
Sets the GDAL_SKIP environment variable to exclude the specified driver and then calls GDALDriverMana...
static const char * QGIS_ORGANIZATION_NAME
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
static bool createDB(QString *errorMessage=0)
initialise qgis.db
static void setPluginPath(const QString &thePluginPath)
Alters plugin path - used by 3rd party apps.
static const QString defaultStyleV2Path()
Returns the path to default style (works as a starting point).
static void initQgis()
loads providers
static void setDefaultSvgPaths(const QStringList &pathList)
Alters default svg paths - used by 3rd party apps.
QColor lighter(int factor) const
static QString relativePathToAbsolutePath(QString rpath, QString targetPath)
Converts path relative to target to an absolute path.
static void skipGdalDriver(QString theDriver)
Sets the GDAL_SKIP environment variable to include the specified driver and then calls GDALDriverMana...
static const QString developersMapFilePath()
Returns the path to the developers map file.
QString & replace(int position, int n, QChar after)
static QString showSettings()
Convenience function to get a summary of the paths used in this application instance useful for debug...
QVariant value(const QString &key, const QVariant &defaultValue) const
static const char * QGIS_ORGANIZATION_DOMAIN
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QString mid(int position, int n) const
virtual bool notify(QObject *receiver, QEvent *event) override
Catch exceptions when sending event to receiver.
static const QString donorsFilePath()
Returns the path to the donors file.
virtual bool event(QEvent *e)
QString absolutePath() const
void insert(int i, const T &value)
QThread * currentThread()
static const QString srsDbFilePath()
Returns the path to the srs.db file.
StandardButton critical(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
QString left(int n) const
static void exitQgis()
deletes provider registry and map layer registry
static const QString sponsorsFilePath()
Returns the path to the sponsors file.
int indexOf(const QRegExp &rx, int from) const
void prepend(const T &value)
iterator insert(const Key &key, const T &value)
QgsApplication(int &argc, char **argv, bool GUIenabled, QString customConfigPath=QString())
static const char * QGIS_APPLICATION_NAME
QString applicationDirPath()
virtual bool notify(QObject *receiver, QEvent *e)
static const QString qgisUserDbFilePath()
Returns the path to the user qgis.db file.
static const QString pluginPath()
Returns the path to the application plugin directory.
static const QString userStyleV2Path()
Returns the path to user's style.
static const QString themeName()
Set the active theme to the specified theme.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
static const int QGIS_VERSION_INT
Defines a qgis exception class.
static void setMaxThreads(int maxThreads)
Set maximum concurrent thread count.
bool mkpath(const QString &dirPath) const
qint64 readLine(char *data, qint64 maxSize)
void preNotify(QObject *receiver, QEvent *event, bool *done)
QByteArray toUtf8() const