]> vaikene.ee Git - evaf/blob - src/libs/Common/env.cpp
e0c08348628741c1723d31a2b694ebfbd6a26eee
[evaf] / src / libs / Common / env.cpp
1 /**
2 * @file Common/env.cpp
3 * @brief iEnv interface implementation
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
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.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #include "env.h"
21 #include "iregistry.h"
22 #include "globals.h"
23 #include "version.h"
24 #include "iapp.h"
25
26 #include <QtCore>
27 #include <QDesktopServices>
28
29
30 //-------------------------------------------------------------------
31
32 using namespace eVaf::Common;
33
34 iEnv * iEnv::instance()
35 {
36 static eVaf::Common::Internal::Env singleton;
37 return &singleton;
38 }
39
40
41 //-------------------------------------------------------------------
42
43 using namespace eVaf::Common::Internal;
44
45 Env::Env()
46 : iEnv()
47 {
48 setObjectName(QString("%1-iEnv").arg(VER_MODULE_NAME_STR));
49 }
50
51 Env::~Env()
52 {
53 }
54
55 bool Env::init()
56 {
57 // Register out interface
58 iRegistry::instance()->registerInterface("iEnv", this);
59
60 // Set initial bin and root directories
61 mRootDir = mBinDir = qApp->applicationDirPath();
62 int t = mBinDir.lastIndexOf(QChar('/'), -1);
63 if (t >= 0)
64 mRootDir = mBinDir.left(t);
65
66 if (!mBinDir.endsWith('/'))
67 mBinDir.append('/');
68 if (!mRootDir.endsWith('/'))
69 mRootDir.append('/');
70
71 // Clear other directories
72 mDataRootDir.clear();
73 mQtPluginsDir.clear();
74 mEtcDir.clear();
75 mLogDir.clear();
76 mDocDir.clear();
77
78 // Process the environment
79 QStringList env = QProcess::systemEnvironment();
80 int sz = env.size();
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();
85
86 if (name == "EVAF_ROOT_DIR") {
87 mRootDir = value;
88 if (!mRootDir.endsWith('/'))
89 mRootDir.append('/');
90 }
91 else if (name == "EVAF_DATA_ROOT_DIR") {
92 mDataRootDir = value;
93 if (!mDataRootDir.endsWith('/'))
94 mDataRootDir.append('/');
95 }
96 else if (name == "EVAF_ETC_DIR") {
97 mEtcDir = value;
98 if (!mEtcDir.endsWith('/'))
99 mEtcDir.append('/');
100 }
101 else if (name == "EVAF_LOG_DIR") {
102 mLogDir = value;
103 if (!mLogDir.endsWith('/'))
104 mLogDir.append('/');
105 }
106 else if (name == "EVAF_DOC_DIR") {
107 mDocDir = value;
108 if (!mDocDir.endsWith('/'))
109 mDocDir.append('/');
110 }
111 else if (name == "EVAF_QT_PLUGINS_DIR") {
112 mQtPluginsDir = value;
113 if (!mQtPluginsDir.endsWith('/'))
114 mQtPluginsDir.append('/');
115 }
116 }
117
118 // Then process comman-line arguments
119 env = QCoreApplication::arguments();
120 sz = env.size();
121 for (int i = 0; i < sz; ++i) {
122 // Get the name and optional value
123 QStringList arg = env.at(i).simplified().split(QChar('='));
124
125 if (QRegExp("-[-]?root(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
126 mRootDir = arg.at(1);
127 if (!mRootDir.endsWith('/'))
128 mRootDir.append('/');
129 }
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('/');
134 }
135 else if (QRegExp("-[-]?etc(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
136 mEtcDir = arg.at(1);
137 if (!mEtcDir.endsWith('/'))
138 mEtcDir.append('/');
139 }
140 else if (QRegExp("-[-]?log(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
141 mLogDir = arg.at(1);
142 if (!mLogDir.endsWith('/'))
143 mLogDir.append('/');
144 }
145 else if (QRegExp("-[-]?doc(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
146 mDocDir = arg.at(1);
147 if (!mDocDir.endsWith('/'))
148 mDocDir.append('/');
149 }
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('/');
154 }
155 }
156
157 return true;
158 }
159
160 QString const Env::dataRootDir() const
161 {
162 if (mDataRootDir.isEmpty()) {
163 QString dataLoc = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
164 if (!dataLoc.endsWith('/'))
165 dataLoc.append('/');
166 mDataRootDir = dataLoc.append(iApp::instance()->name());
167 if (!mDataRootDir.endsWith('/'))
168 mDataRootDir.append('/');
169 }
170
171 return mDataRootDir;
172 }
173
174 QString const Env::etcDir() const
175 {
176 if (mEtcDir.isEmpty())
177 mEtcDir = dataRootDir() + "etc/";
178 return mEtcDir;
179 }
180
181 QString const Env::logDir() const
182 {
183 if (mLogDir.isEmpty())
184 mLogDir = dataRootDir() + "log/";
185 return mLogDir;
186 }
187
188 QString const Env::docDir() const
189 {
190 if (mDocDir.isEmpty())
191 mDocDir = rootDir() + "doc/";
192 return mDocDir;
193 }
194
195 QString const Env::qtPluginsDir() const
196 {
197 if (mQtPluginsDir.isEmpty())
198 mQtPluginsDir = binDir();
199 return mQtPluginsDir;
200 }