]>
vaikene.ee Git - evaf/blob - src/libs/Common/app.cpp
60ee5bf698d00b809e6050bd2b6f17c9b4f8d76c
3 * @brief Application 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.
22 #include "iregistry.h"
23 #include "ieventqueue.h"
30 //-------------------------------------------------------------------
32 using namespace eVaf::Common
;
34 iApp
* iApp::instance()
36 static Internal::App singleton
;
40 char const * const iApp::EV_QUIT
= "iApp::quit";
41 char const * const iApp::EV_RESTART
= "iApp::restart";
42 char const * const iApp::EV_READY
= "iApp::ready";
43 char const * const iApp::EV_TERMINATING
= "iApp::terminating";
46 //-------------------------------------------------------------------
48 using namespace eVaf::Common::Internal
;
53 , mName(VER_PRODUCT_NAME_STR
)
59 setObjectName(QString("%1.iApp").arg(VER_MODULE_NAME_STR
));
69 // Register our interface
70 iRegistry::instance()->registerInterface("iApp", this);
73 mEvQuit
= iEventQueue::instance()->subscribeEvent(iEventQueue::instance()->registerEvent(EV_QUIT
), this);
74 mEvRestart
= iEventQueue::instance()->subscribeEvent(iEventQueue::instance()->registerEvent(EV_RESTART
), this);
75 mEvReady
= iEventQueue::instance()->registerEvent(EV_READY
);
76 mEvTerminating
= iEventQueue::instance()->registerEvent(EV_TERMINATING
);
78 // Set the default application name and language
79 mName
= VER_PRODUCT_NAME_STR
;
80 mLanguage
= QLocale::system().name();
82 // Clear the XML file name
85 // Set initial bin and root directories
86 mRootDir
= mBinDir
= qApp
->applicationDirPath();
87 int t
= mBinDir
.lastIndexOf(QChar('/'), -1);
89 mRootDir
= mBinDir
.left(t
);
91 if (!mBinDir
.endsWith('/'))
93 if (!mRootDir
.endsWith('/'))
96 // Clear other directories
98 mQtPluginsDir
.clear();
103 // Process environment variables
104 QStringList env
= QProcess::systemEnvironment();
105 for (int i
= 0; i
< env
.size(); ++i
) {
106 // Get the name/value pair
107 QString name
= env
.at(i
).section('=', 0, 0).trimmed();
108 QString value
= env
.at(i
).section('=', 1).trimmed();
110 if (name
== "EVAF_APP_NAME")
112 else if (name
== "EVAF_LANGUAGE")
114 else if (name
== "EVAF_ROOT_DIR") {
116 if (!mRootDir
.endsWith('/'))
117 mRootDir
.append('/');
119 else if (name
== "EVAF_DATA_ROOT_DIR") {
120 mDataRootDir
= value
;
121 if (!mDataRootDir
.endsWith('/'))
122 mDataRootDir
.append('/');
124 else if (name
== "EVAF_ETC_DIR") {
126 if (!mEtcDir
.endsWith('/'))
129 else if (name
== "EVAF_LOG_DIR") {
131 if (!mLogDir
.endsWith('/'))
134 else if (name
== "EVAF_DOC_DIR") {
136 if (!mDocDir
.endsWith('/'))
139 else if (name
== "EVAF_QT_PLUGINS_DIR") {
140 mQtPluginsDir
= value
;
141 if (!mQtPluginsDir
.endsWith('/'))
142 mQtPluginsDir
.append('/');
146 // Then process command-line arguments
147 env
= QCoreApplication::arguments();
148 for (int i
= 0; i
< env
.size(); ++i
) {
149 // Get the name and optional value
150 QStringList arg
= env
.at(i
).simplified().split('=');
152 if (QRegExp("-[-]?app(lication)?").exactMatch(arg
.at(0)) && arg
.size() > 1)
154 else if (QRegExp("-[-]?lang(uage)?").exactMatch(arg
.at(0)) && arg
.size() > 1)
155 mLanguage
= arg
.at(1);
156 else if (QRegExp("-[-]?root(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
157 mRootDir
= arg
.at(1);
158 if (!mRootDir
.endsWith('/'))
159 mRootDir
.append('/');
161 else if (QRegExp("-[-]?dataroot(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
162 mDataRootDir
= arg
.at(1);
163 if (!mDataRootDir
.endsWith('/'))
164 mDataRootDir
.append('/');
166 else if (QRegExp("-[-]?etc(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
168 if (!mEtcDir
.endsWith('/'))
171 else if (QRegExp("-[-]?log(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
173 if (!mLogDir
.endsWith('/'))
176 else if (QRegExp("-[-]?doc(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
178 if (!mDocDir
.endsWith('/'))
181 else if (QRegExp("-[-]?qtplugins(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
182 mQtPluginsDir
= arg
.at(1);
183 if (!mQtPluginsDir
.endsWith('/'))
184 mQtPluginsDir
.append('/');
191 bool App::event(QEvent
* e
)
193 if (e
->type() == Event::eVafEvent
) {
194 Event
* event
= static_cast<Event
*>(e
);
195 if (event
->id() == mEvQuit
)
197 else if (event
->id() == mEvRestart
)
203 return iApp::event(e
);
206 QString
const App::dataRootDir() const
208 if (mDataRootDir
.isEmpty()) {
210 QString dataLoc
= QDir::homePath();
211 if (!dataLoc
.endsWith('/'))
213 dataLoc
.append(".local/share/data/");
214 mDataRootDir
= dataLoc
+ name();
215 if (!mDataRootDir
.endsWith('/'))
216 mDataRootDir
.append('/');
218 /// @TODO: Needs local data directory on Windows
219 mDataRootDir
= rootDir();
225 QString
const App::etcDir() const
227 if (mEtcDir
.isEmpty())
228 mEtcDir
= dataRootDir() + "etc/";
232 QString
const App::logDir() const
234 if (mLogDir
.isEmpty())
235 mLogDir
= dataRootDir() + "log/";
239 QString
const App::docDir() const
241 if (mDocDir
.isEmpty())
242 mDocDir
= rootDir() + "doc/";
246 QString
const App::qtPluginsDir() const
248 if (mQtPluginsDir
.isEmpty())
249 mQtPluginsDir
= binDir();
250 return mQtPluginsDir
;
253 QString
const App::xmlFileName() const
255 if (mXmlFile
.isEmpty()) {
258 // Try the full application name + country + language combination
259 QString name
= mName
+ "_" + mLanguage
+ ".xml";
260 fi
.setFile(etcDir() + name
);
264 // Try application name + country
265 name
= mName
+ "_" + mLanguage
.left(2) + ".xml";
266 fi
.setFile(etcDir() + name
);
270 // Fall-back to the generic name
271 mXmlFile
= mName
+ ".xml";
280 int rval
= QCoreApplication::exec();
287 QCoreApplication::exit(RC_Restart
);
290 void App::quit(bool err
)
292 QCoreApplication::exit(err
? RC_Error
: RC_Quit
);
295 void App::setReady(bool value
)
297 if (mReady
!= value
) {
299 iEventQueue::instance()->broadcastEvent(new Event(mReady
? mEvReady
: mEvTerminating
));