]>
vaikene.ee Git - evaf/blob - src/libs/Common/env.cpp
55b133ed3814a10fcbd79879b8601b2223ec40b6
3 * @brief iEnv interface implementation
6 * Copyright (c) 2011 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
21 #include "iregistry.h"
27 #include <QDesktopServices>
30 //-------------------------------------------------------------------
32 using namespace eVaf::Common
;
34 iEnv
* iEnv::instance()
36 static Internal::Env singleton
;
41 //-------------------------------------------------------------------
43 using namespace eVaf::Common::Internal
;
48 setObjectName(QString("%1-iEnv").arg(VER_MODULE_NAME_STR
));
50 // Set initial bin and root directories
51 mRootDir
= mBinDir
= qApp
->applicationDirPath();
52 int t
= mBinDir
.lastIndexOf(QChar('/'), -1);
54 mRootDir
= mBinDir
.left(t
);
56 if (!mBinDir
.endsWith('/'))
58 if (!mRootDir
.endsWith('/'))
68 // Register out interface
69 iRegistry::instance()->registerInterface("iEnv", this);
73 mQtPluginsDir
.clear();
78 // Process the environment
79 QStringList env
= QProcess::systemEnvironment();
81 for (int i
= 0; i
< sz
; ++i
) {
82 // Get the name/value pair
83 QString name
= env
.at(i
).section('=', 0, 0).trimmed();
84 QString value
= env
.at(i
).section('=', 1).trimmed();
86 if (name
== "EVAF_ROOT_DIR") {
88 if (!mRootDir
.endsWith('/'))
91 else if (name
== "EVAF_DATA_ROOT_DIR") {
93 if (!mDataRootDir
.endsWith('/'))
94 mDataRootDir
.append('/');
96 else if (name
== "EVAF_ETC_DIR") {
98 if (!mEtcDir
.endsWith('/'))
101 else if (name
== "EVAF_LOG_DIR") {
103 if (!mLogDir
.endsWith('/'))
106 else if (name
== "EVAF_DOC_DIR") {
108 if (!mDocDir
.endsWith('/'))
111 else if (name
== "EVAF_QT_PLUGINS_DIR") {
112 mQtPluginsDir
= value
;
113 if (!mQtPluginsDir
.endsWith('/'))
114 mQtPluginsDir
.append('/');
118 // Then process comman-line arguments
119 env
= QCoreApplication::arguments();
121 for (int i
= 0; i
< sz
; ++i
) {
122 // Get the name and optional value
123 QStringList arg
= env
.at(i
).simplified().split(QChar('='));
125 if (QRegExp("-[-]?root(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
126 mRootDir
= arg
.at(1);
127 if (!mRootDir
.endsWith('/'))
128 mRootDir
.append('/');
130 else if (QRegExp("-[-]?dataroot(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
131 mDataRootDir
= arg
.at(1);
132 if (!mDataRootDir
.endsWith('/'))
133 mDataRootDir
.append('/');
135 else if (QRegExp("-[-]?etc(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
137 if (!mEtcDir
.endsWith('/'))
140 else if (QRegExp("-[-]?log(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
142 if (!mLogDir
.endsWith('/'))
145 else if (QRegExp("-[-]?doc(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
147 if (!mDocDir
.endsWith('/'))
150 else if (QRegExp("-[-]?qtplugins(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
151 mQtPluginsDir
= arg
.at(1);
152 if (!mQtPluginsDir
.endsWith('/'))
153 mQtPluginsDir
.append('/');
160 QString
const Env::dataRootDir() const
162 if (mDataRootDir
.isEmpty()) {
163 QString dataLoc
= QDesktopServices::storageLocation(QDesktopServices::DataLocation
);
164 if (!dataLoc
.endsWith('/'))
166 mDataRootDir
= dataLoc
.append(iApp::instance()->name());
167 if (!mDataRootDir
.endsWith('/'))
168 mDataRootDir
.append('/');
174 QString
const Env::etcDir() const
176 if (mEtcDir
.isEmpty())
177 mEtcDir
= dataRootDir() + "etc/";
181 QString
const Env::logDir() const
183 if (mLogDir
.isEmpty())
184 mLogDir
= dataRootDir() + "log/";
188 QString
const Env::docDir() const
190 if (mDocDir
.isEmpty())
191 mDocDir
= rootDir() + "doc/";
195 QString
const Env::qtPluginsDir() const
197 if (mQtPluginsDir
.isEmpty())
198 mQtPluginsDir
= binDir();
199 return mQtPluginsDir
;