]> vaikene.ee Git - evaf/blob - src/libs/Common/app.cpp
53efce95d319ad6cc12eea546d5b9f1189202275
[evaf] / src / libs / Common / app.cpp
1 /**
2 * @file Common/app.cpp
3 * @brief Application 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 "app.h"
21 #include "globals.h"
22 #include "registry.h"
23 #include "version.h"
24
25 #include <QtCore>
26
27
28 //-------------------------------------------------------------------
29
30 using namespace eVaf::Common;
31
32 iApp * iApp::instance()
33 {
34 static Internal::App singleton;
35 return &singleton;
36 }
37
38 char const * const iApp::EV_QUIT = "iApp::quit";
39 char const * const iApp::EV_RESTART = "iApp::restart";
40 char const * const iApp::EV_READY = "iApp::ready";
41 char const * const iApp::EV_TERMINATING = "iApp::terminating";
42
43
44 //-------------------------------------------------------------------
45
46 using namespace eVaf::Common::Internal;
47
48 App::App()
49 : iApp()
50 , mReady(false)
51 , mName(VER_PRODUCT_NAME_STR)
52 {
53 setObjectName(QString("%1.iApp").arg(VER_MODULE_NAME_STR));
54
55 }
56
57 App::~App()
58 {
59 }
60
61 bool App::init()
62 {
63 // Register our interface
64 iRegistry::instance()->registerInterface("iApp", this);
65
66 // Set the default application name and language
67 mName = VER_PRODUCT_NAME_STR;
68 mLanguage = QLocale::system().name();
69
70 // Clear the XML file name
71 mXmlFile.clear();
72
73 // Set initial bin and root directories
74 mRootDir = mBinDir = qApp->applicationDirPath();
75 int t = mBinDir.lastIndexOf(QChar('/'), -1);
76 if (t >= 0)
77 mRootDir = mBinDir.left(t);
78
79 if (!mBinDir.endsWith('/'))
80 mBinDir.append('/');
81 if (!mRootDir.endsWith('/'))
82 mRootDir.append('/');
83
84 // Clear other directories
85 mDataRootDir.clear();
86 mQtPluginsDir.clear();
87 mEtcDir.clear();
88 mLogDir.clear();
89 mDocDir.clear();
90
91 // Process environment variables
92 QStringList env = QProcess::systemEnvironment();
93 for (int i = 0; i < env.size(); ++i) {
94 // Get the name/value pair
95 QString name = env.at(i).section('=', 0, 0).trimmed();
96 QString value = env.at(i).section('=', 1).trimmed();
97
98 if (name == "EVAF_APP_NAME")
99 mName = value;
100 else if (name == "EVAF_LANGUAGE")
101 mLanguage = value;
102 else if (name == "EVAF_ROOT_DIR") {
103 mRootDir = value;
104 if (!mRootDir.endsWith('/'))
105 mRootDir.append('/');
106 }
107 else if (name == "EVAF_DATA_ROOT_DIR") {
108 mDataRootDir = value;
109 if (!mDataRootDir.endsWith('/'))
110 mDataRootDir.append('/');
111 }
112 else if (name == "EVAF_ETC_DIR") {
113 mEtcDir = value;
114 if (!mEtcDir.endsWith('/'))
115 mEtcDir.append('/');
116 }
117 else if (name == "EVAF_LOG_DIR") {
118 mLogDir = value;
119 if (!mLogDir.endsWith('/'))
120 mLogDir.append('/');
121 }
122 else if (name == "EVAF_DOC_DIR") {
123 mDocDir = value;
124 if (!mDocDir.endsWith('/'))
125 mDocDir.append('/');
126 }
127 else if (name == "EVAF_QT_PLUGINS_DIR") {
128 mQtPluginsDir = value;
129 if (!mQtPluginsDir.endsWith('/'))
130 mQtPluginsDir.append('/');
131 }
132 }
133
134 // Then process command-line arguments
135 env = QCoreApplication::arguments();
136 for (int i = 0; i < env.size(); ++i) {
137 // Get the name and optional value
138 QStringList arg = env.at(i).simplified().split('=');
139
140 if (QRegExp("-[-]?app(lication)?").exactMatch(arg.at(0)) && arg.size() > 1)
141 mName = arg.at(1);
142 else if (QRegExp("-[-]?lang(uage)?").exactMatch(arg.at(0)) && arg.size() > 1)
143 mLanguage = arg.at(1);
144 else if (QRegExp("-[-]?root(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
145 mRootDir = arg.at(1);
146 if (!mRootDir.endsWith('/'))
147 mRootDir.append('/');
148 }
149 else if (QRegExp("-[-]?dataroot(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
150 mDataRootDir = arg.at(1);
151 if (!mDataRootDir.endsWith('/'))
152 mDataRootDir.append('/');
153 }
154 else if (QRegExp("-[-]?etc(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
155 mEtcDir = arg.at(1);
156 if (!mEtcDir.endsWith('/'))
157 mEtcDir.append('/');
158 }
159 else if (QRegExp("-[-]?log(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
160 mLogDir = arg.at(1);
161 if (!mLogDir.endsWith('/'))
162 mLogDir.append('/');
163 }
164 else if (QRegExp("-[-]?doc(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
165 mDocDir = arg.at(1);
166 if (!mDocDir.endsWith('/'))
167 mDocDir.append('/');
168 }
169 else if (QRegExp("-[-]?qtplugins(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
170 mQtPluginsDir = arg.at(1);
171 if (!mQtPluginsDir.endsWith('/'))
172 mQtPluginsDir.append('/');
173 }
174 }
175
176 return true;
177 }
178
179 QString const App::dataRootDir() const
180 {
181 if (mDataRootDir.isEmpty()) {
182 #ifdef Q_OS_LINUX
183 QString dataLoc = QDir::homePath();
184 if (!dataLoc.endsWith('/'))
185 dataLoc.append('/');
186 dataLoc.append(".local/share/data/");
187 mDataRootDir = dataLoc + name();
188 if (!mDataRootDir.endsWith('/'))
189 mDataRootDir.append('/');
190 #endif
191 /// @TODO: Needs local data directory on Windows
192 mDataRootDir = rootDir();
193 }
194
195 return mDataRootDir;
196 }
197
198 QString const App::etcDir() const
199 {
200 if (mEtcDir.isEmpty())
201 mEtcDir = dataRootDir() + "etc/";
202 return mEtcDir;
203 }
204
205 QString const App::logDir() const
206 {
207 if (mLogDir.isEmpty())
208 mLogDir = dataRootDir() + "log/";
209 return mLogDir;
210 }
211
212 QString const App::docDir() const
213 {
214 if (mDocDir.isEmpty())
215 mDocDir = rootDir() + "doc/";
216 return mDocDir;
217 }
218
219 QString const App::qtPluginsDir() const
220 {
221 if (mQtPluginsDir.isEmpty())
222 mQtPluginsDir = binDir();
223 return mQtPluginsDir;
224 }
225
226 QString const App::xmlFileName() const
227 {
228 if (mXmlFile.isEmpty()) {
229 QFileInfo fi;
230
231 // Try the full application name + country + language combination
232 QString name = mName + "_" + mLanguage + ".xml";
233 fi.setFile(etcDir() + name);
234 if (fi.isReadable())
235 mXmlFile = name;
236 else {
237 // Try application name + country
238 name = mName + "_" + mLanguage.left(2) + ".xml";
239 fi.setFile(etcDir() + name);
240 if (fi.isReadable())
241 mXmlFile = name;
242 else
243 // Fall-back to the generic name
244 mXmlFile = mName + ".xml";
245 }
246 }
247 return mXmlFile;
248 }
249
250 void App::restart()
251 {
252 QCoreApplication::exit(RC_Restart);
253 }
254
255 void App::quit(bool err)
256 {
257 QCoreApplication::exit(err ? RC_Error : RC_Quit);
258 }