]> vaikene.ee Git - evaf/blob - src/libs/Common/env.cpp
Seems that Windows did not like exporting/importing the eVaf::PswGen::Storage::Data...
[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
28
29 //-------------------------------------------------------------------
30
31 using namespace eVaf::Common;
32
33 iEnv * iEnv::instance()
34 {
35 static eVaf::Common::Internal::Env singleton;
36 return &singleton;
37 }
38
39
40 //-------------------------------------------------------------------
41
42 using namespace eVaf::Common::Internal;
43
44 Env::Env()
45 : iEnv()
46 {
47 setObjectName(QString("%1-iEnv").arg(VER_MODULE_NAME_STR));
48 }
49
50 Env::~Env()
51 {
52 }
53
54 bool Env::init()
55 {
56 // Register out interface
57 iRegistry::instance()->registerInterface("iEnv", this);
58
59 // Set initial bin and root directories
60 mRootDir = mBinDir = qApp->applicationDirPath();
61 int t = mBinDir.lastIndexOf(QChar('/'), -1);
62 if (t >= 0)
63 mRootDir = mBinDir.left(t);
64
65 if (!mBinDir.endsWith('/'))
66 mBinDir.append('/');
67 if (!mRootDir.endsWith('/'))
68 mRootDir.append('/');
69
70 // Clear other directories
71 mDataRootDir.clear();
72 mQtPluginsDir.clear();
73 mEtcDir.clear();
74 mLogDir.clear();
75 mDocDir.clear();
76
77 // Set the data root directory
78 #ifdef Q_OS_LINUX
79 QString dataLoc = QDir::homePath();
80 if (!dataLoc.endsWith('/'))
81 dataLoc.append('/');
82 dataLoc.append(".local/share/data/");
83 mDataRootDir = dataLoc + iApp::instance()->name();
84 if (!mDataRootDir.endsWith('/'))
85 mDataRootDir.append('/');
86 #endif
87 /// @TODO: Needs local data directory on Windows
88
89 // Process the environment
90 QStringList env = QProcessEnvironment::systemEnvironment().toStringList();
91 int sz = env.size();
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();
96
97 if (name == "EVAF_ROOT_DIR") {
98 mRootDir = value;
99 if (!mRootDir.endsWith('/'))
100 mRootDir.append('/');
101 }
102 else if (name == "EVAF_DATA_ROOT_DIR") {
103 mDataRootDir = value;
104 if (!mDataRootDir.endsWith('/'))
105 mDataRootDir.append('/');
106 }
107 else if (name == "EVAF_ETC_DIR") {
108 mEtcDir = value;
109 if (!mEtcDir.endsWith('/'))
110 mEtcDir.append('/');
111 }
112 else if (name == "EVAF_LOG_DIR") {
113 mLogDir = value;
114 if (!mLogDir.endsWith('/'))
115 mLogDir.append('/');
116 }
117 else if (name == "EVAF_DOC_DIR") {
118 mDocDir = value;
119 if (!mDocDir.endsWith('/'))
120 mDocDir.append('/');
121 }
122 else if (name == "EVAF_QT_PLUGINS_DIR") {
123 mQtPluginsDir = value;
124 if (!mQtPluginsDir.endsWith('/'))
125 mQtPluginsDir.append('/');
126 }
127 }
128
129 // Then process comman-line arguments
130 env = QCoreApplication::arguments();
131 sz = env.size();
132 for (int i = 0; i < sz; ++i) {
133 // Get the name and optional value
134 QStringList arg = env.at(i).simplified().split(QChar('='));
135
136 if (QRegExp("-[-]?root(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
137 mRootDir = arg.at(1);
138 if (!mRootDir.endsWith('/'))
139 mRootDir.append('/');
140 }
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('/');
145 }
146 else if (QRegExp("-[-]?etc(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
147 mEtcDir = arg.at(1);
148 if (!mEtcDir.endsWith('/'))
149 mEtcDir.append('/');
150 }
151 else if (QRegExp("-[-]?log(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
152 mLogDir = arg.at(1);
153 if (!mLogDir.endsWith('/'))
154 mLogDir.append('/');
155 }
156 else if (QRegExp("-[-]?doc(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
157 mDocDir = arg.at(1);
158 if (!mDocDir.endsWith('/'))
159 mDocDir.append('/');
160 }
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('/');
165 }
166 }
167
168 return true;
169 }
170
171 QString const Env::dataRootDir() const
172 {
173 // Fall-back to the application's root directory if the data root directory is empty
174 if (mDataRootDir.isEmpty())
175 mDataRootDir = rootDir();
176 return mDataRootDir;
177 }
178
179 QString const Env::etcDir() const
180 {
181 if (mEtcDir.isEmpty())
182 mEtcDir = dataRootDir() + "etc/";
183 return mEtcDir;
184 }
185
186 QString const Env::logDir() const
187 {
188 if (mLogDir.isEmpty())
189 mLogDir = dataRootDir() + "log/";
190 return mLogDir;
191 }
192
193 QString const Env::docDir() const
194 {
195 if (mDocDir.isEmpty())
196 mDocDir = rootDir() + "doc/";
197 return mDocDir;
198 }
199
200 QString const Env::qtPluginsDir() const
201 {
202 if (mQtPluginsDir.isEmpty())
203 mQtPluginsDir = binDir();
204 return mQtPluginsDir;
205 }