]>
vaikene.ee Git - evaf/blob - src/libs/Common/app.cpp
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.
28 //-------------------------------------------------------------------
30 using namespace eVaf::Common
;
32 iApp
* iApp::instance()
34 static Internal::App singleton
;
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";
44 //-------------------------------------------------------------------
46 using namespace eVaf::Common::Internal
;
51 , mName(VER_PRODUCT_NAME_STR
)
53 setObjectName(QString("%1.iApp").arg(VER_MODULE_NAME_STR
));
63 // Register our interface
64 iRegistry::instance()->registerInterface("iApp", this);
66 // Set the default application name and language
67 mName
= VER_PRODUCT_NAME_STR
;
68 mLanguage
= QLocale::system().name();
70 // Clear the XML file name
73 // Set initial bin and root directories
74 mRootDir
= mBinDir
= qApp
->applicationDirPath();
75 int t
= mBinDir
.lastIndexOf(QChar('/'), -1);
77 mRootDir
= mBinDir
.left(t
);
79 if (!mBinDir
.endsWith('/'))
81 if (!mRootDir
.endsWith('/'))
84 // Clear other directories
86 mQtPluginsDir
.clear();
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();
98 if (name
== "EVAF_APP_NAME")
100 else if (name
== "EVAF_LANGUAGE")
102 else if (name
== "EVAF_ROOT_DIR") {
104 if (!mRootDir
.endsWith('/'))
105 mRootDir
.append('/');
107 else if (name
== "EVAF_DATA_ROOT_DIR") {
108 mDataRootDir
= value
;
109 if (!mDataRootDir
.endsWith('/'))
110 mDataRootDir
.append('/');
112 else if (name
== "EVAF_ETC_DIR") {
114 if (!mEtcDir
.endsWith('/'))
117 else if (name
== "EVAF_LOG_DIR") {
119 if (!mLogDir
.endsWith('/'))
122 else if (name
== "EVAF_DOC_DIR") {
124 if (!mDocDir
.endsWith('/'))
127 else if (name
== "EVAF_QT_PLUGINS_DIR") {
128 mQtPluginsDir
= value
;
129 if (!mQtPluginsDir
.endsWith('/'))
130 mQtPluginsDir
.append('/');
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('=');
140 if (QRegExp("-[-]?app(lication)?").exactMatch(arg
.at(0)) && arg
.size() > 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('/');
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('/');
154 else if (QRegExp("-[-]?etc(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
156 if (!mEtcDir
.endsWith('/'))
159 else if (QRegExp("-[-]?log(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
161 if (!mLogDir
.endsWith('/'))
164 else if (QRegExp("-[-]?doc(dir)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
166 if (!mDocDir
.endsWith('/'))
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('/');
179 QString
const App::dataRootDir() const
181 if (mDataRootDir
.isEmpty()) {
183 QString dataLoc
= QDir::homePath();
184 if (!dataLoc
.endsWith('/'))
186 dataLoc
.append(".local/share/data/");
187 mDataRootDir
= dataLoc
+ name();
188 if (!mDataRootDir
.endsWith('/'))
189 mDataRootDir
.append('/');
191 /// @TODO: Needs local data directory on Windows
192 mDataRootDir
= rootDir();
198 QString
const App::etcDir() const
200 if (mEtcDir
.isEmpty())
201 mEtcDir
= dataRootDir() + "etc/";
205 QString
const App::logDir() const
207 if (mLogDir
.isEmpty())
208 mLogDir
= dataRootDir() + "log/";
212 QString
const App::docDir() const
214 if (mDocDir
.isEmpty())
215 mDocDir
= rootDir() + "doc/";
219 QString
const App::qtPluginsDir() const
221 if (mQtPluginsDir
.isEmpty())
222 mQtPluginsDir
= binDir();
223 return mQtPluginsDir
;
226 QString
const App::xmlFileName() const
228 if (mXmlFile
.isEmpty()) {
231 // Try the full application name + country + language combination
232 QString name
= mName
+ "_" + mLanguage
+ ".xml";
233 fi
.setFile(etcDir() + name
);
237 // Try application name + country
238 name
= mName
+ "_" + mLanguage
.left(2) + ".xml";
239 fi
.setFile(etcDir() + name
);
243 // Fall-back to the generic name
244 mXmlFile
= mName
+ ".xml";
252 QCoreApplication::exit(RC_Restart
);
255 void App::quit(bool err
)
257 QCoreApplication::exit(err
? RC_Error
: RC_Quit
);