]> vaikene.ee Git - evaf/blob - src/libs/Common/app.cpp
3956548f4e1656e1c1e7f44d343d0ab89fccec19
[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 "ienv.h"
24 #include "version.h"
25
26 #include <QtCore>
27
28
29 //-------------------------------------------------------------------
30
31 using namespace eVaf::Common;
32
33 iApp * iApp::instance()
34 {
35 static Internal::App singleton;
36 return &singleton;
37 }
38
39 char const * const iApp::EV_QUIT = "iApp::quit";
40 char const * const iApp::EV_RESTART = "iApp::restart";
41 char const * const iApp::EV_READY = "iApp::ready";
42 char const * const iApp::EV_TERMINATING = "iApp::terminating";
43
44
45 //-------------------------------------------------------------------
46
47 using namespace eVaf::Common::Internal;
48
49 App::App()
50 : iApp()
51 , mReady(false)
52 , mName(VER_PRODUCT_NAME_STR)
53 {
54 setObjectName(QString("%1.iApp").arg(VER_MODULE_NAME_STR));
55
56 }
57
58 App::~App()
59 {
60 }
61
62 bool App::init()
63 {
64 // Register our interface
65 iRegistry::instance()->registerInterface("iApp", this);
66
67 // Set the default application name and language
68 mName = VER_PRODUCT_NAME_STR;
69 mLanguage = QLocale::system().name();
70
71 // Clear the XML file name
72 mXmlFile.clear();
73
74 // Process environment variables
75 QStringList env = QProcess::systemEnvironment();
76 for (int i = 0; i < env.size(); ++i) {
77 // Get the name/value pair
78 QString name = env.at(i).section('=', 0, 0).trimmed();
79 QString value = env.at(i).section('=', 1).trimmed();
80
81 if (name == "EVAF_APP_NAME")
82 mName = value;
83 else if (name == "EVAF_LANGUAGE")
84 mLanguage = value;
85 }
86
87 // Then process command-line arguments
88 env = QCoreApplication::arguments();
89 for (int i = 0; i < env.size(); ++i) {
90 // Get the name and optional value
91 QStringList arg = env.at(i).simplified().split('=');
92
93 if (QRegExp("-[-]?app(lication)?").exactMatch(arg.at(0)) && arg.size() > 1)
94 mName = arg.at(1);
95 else if (QRegExp("-[-]?lang(uage)?").exactMatch(arg.at(0)) && arg.size() > 1)
96 mLanguage = arg.at(1);
97 }
98
99 return true;
100 }
101
102 QString const App::xmlFileName() const
103 {
104 if (mXmlFile.isEmpty()) {
105 QFileInfo fi;
106
107 // Try the full application name + country + language combination
108 QString name = mName + "_" + mLanguage + ".xml";
109 fi.setFile(iEnv::instance()->etcDir() + name);
110 if (fi.isReadable())
111 mXmlFile = name;
112 else {
113 // Try application name + country
114 name = mName + "_" + mLanguage.left(2) + ".xml";
115 fi.setFile(iEnv::instance()->etcDir() + name);
116 if (fi.isReadable())
117 mXmlFile = name;
118 else
119 // Fall-back to the generic name
120 mXmlFile = mName + ".xml";
121 }
122 }
123 return mXmlFile;
124 }
125
126 void App::restart()
127 {
128 QCoreApplication::exit(RC_Restart);
129 }
130
131 void App::quit(bool err)
132 {
133 QCoreApplication::exit(err ? RC_Error : RC_Quit);
134 }