]>
vaikene.ee Git - evaf/blob - src/libs/Common/env.cpp
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"
29 //-------------------------------------------------------------------
31 using namespace eVaf::Common
;
33 iEnv
* iEnv::instance()
35 static eVaf::Common::Internal::Env singleton
;
40 //-------------------------------------------------------------------
42 using namespace eVaf::Common::Internal
;
47 setObjectName(QString("%1-iEnv").arg(VER_MODULE_NAME_STR
));
56 // Register out interface
57 iRegistry::instance()->registerInterface("iEnv", this);
59 // Set initial bin and root directories
60 mRootDir
= mBinDir
= qApp
->applicationDirPath();
61 int t
= mBinDir
.lastIndexOf(QChar('/'), -1);
63 mRootDir
= mBinDir
.left(t
);
65 if (!mBinDir
.endsWith('/'))
67 if (!mRootDir
.endsWith('/'))
70 // Clear other directories
72 mQtPluginsDir
.clear();
77 // Set the data root directory
79 QString dataLoc
= QDir::homePath();
80 if (!dataLoc
.endsWith('/'))
82 dataLoc
.append(".local/share/data/");
83 mDataRootDir
= dataLoc
+ iApp::instance()->name();
84 if (!mDataRootDir
.endsWith('/'))
85 mDataRootDir
.append('/');
87 /// @TODO: Needs local data directory on Windows
89 // Process the environment
90 QStringList env
= QProcessEnvironment::systemEnvironment().toStringList();
92 for (int i
= 0; i
< sz
; ++i
) {
93 // Get the name/value pair
94 QString name
= env
.at(i
).section('=', 0, 0).trimmed();
95 QString value
= env
.at(i
).section('=', 1).trimmed();
97 if (name
== "EVAF_ROOT_DIR") {
99 if (!mRootDir
.endsWith('/'))
100 mRootDir
.append('/');
102 else if (name
== "EVAF_DATA_ROOT_DIR") {
103 mDataRootDir
= value
;
104 if (!mDataRootDir
.endsWith('/'))
105 mDataRootDir
.append('/');
107 else if (name
== "EVAF_ETC_DIR") {
109 if (!mEtcDir
.endsWith('/'))
112 else if (name
== "EVAF_LOG_DIR") {
114 if (!mLogDir
.endsWith('/'))
117 else if (name
== "EVAF_DOC_DIR") {
119 if (!mDocDir
.endsWith('/'))
122 else if (name
== "EVAF_QT_PLUGINS_DIR") {
123 mQtPluginsDir
= value
;
124 if (!mQtPluginsDir
.endsWith('/'))
125 mQtPluginsDir
.append('/');
129 // Then process comman-line arguments
130 env
= QCoreApplication::arguments();
132 for (int i
= 0; i
< sz
; ++i
) {
133 // Get the name and optional value
134 QStringList arg
= env
.at(i
).simplified().split(QChar('='));
136 if (QRegExp("-[-]?root(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
137 mRootDir
= arg
.at(1);
138 if (!mRootDir
.endsWith('/'))
139 mRootDir
.append('/');
141 else if (QRegExp("-[-]?dataroot(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
142 mDataRootDir
= arg
.at(1);
143 if (!mDataRootDir
.endsWith('/'))
144 mDataRootDir
.append('/');
146 else if (QRegExp("-[-]?etc(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
148 if (!mEtcDir
.endsWith('/'))
151 else if (QRegExp("-[-]?log(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
153 if (!mLogDir
.endsWith('/'))
156 else if (QRegExp("-[-]?doc(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
158 if (!mDocDir
.endsWith('/'))
161 else if (QRegExp("-[-]?qtplugins(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
162 mQtPluginsDir
= arg
.at(1);
163 if (!mQtPluginsDir
.endsWith('/'))
164 mQtPluginsDir
.append('/');
171 QString
const Env::dataRootDir() const
173 // Fall-back to the application's root directory if the data root directory is empty
174 if (mDataRootDir
.isEmpty())
175 mDataRootDir
= rootDir();
179 QString
const Env::etcDir() const
181 if (mEtcDir
.isEmpty())
182 mEtcDir
= dataRootDir() + "etc/";
186 QString
const Env::logDir() const
188 if (mLogDir
.isEmpty())
189 mLogDir
= dataRootDir() + "log/";
193 QString
const Env::docDir() const
195 if (mDocDir
.isEmpty())
196 mDocDir
= rootDir() + "doc/";
200 QString
const Env::qtPluginsDir() const
202 if (mQtPluginsDir
.isEmpty())
203 mQtPluginsDir
= binDir();
204 return mQtPluginsDir
;