/** * @file Common/app.cpp * @brief Application interface implementation * @author Enar Vaikene * * Copyright (c) 2011 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * * This file can be used under the terms of the GNU General Public License * version 3.0 as published by the Free Software Foundation and appearing in * the file LICENSE included in the packaging of this file. Please review the * the following information to ensure the GNU General Public License version * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. * * Alternatively, this file may be used in accordance with the Commercial License * Agreement provided with the Software. */ #include "app.h" #include "globals.h" #include "registry.h" #include "ienv.h" #include "version.h" #include //------------------------------------------------------------------- using namespace eVaf::Common; iApp * iApp::instance() { Internal::App singleton; return &singleton; } char const * const iApp::EV_QUIT = "iApp::quit"; char const * const iApp::EV_RESTART = "iApp::restart"; char const * const iApp::EV_READY = "iApp::ready"; char const * const iApp::EV_TERMINATING = "iApp::terminating"; //------------------------------------------------------------------- using namespace eVaf::Common::Internal; App::App() : iApp() , mReady(false) , mName(VER_PRODUCT_NAME_STR) { setObjectName(QString("%1.iApp").arg(VER_MODULE_NAME_STR)); } App::~App() { } bool App::init() { // Register our interface iRegistry::instance()->registerInterface("iApp", this); // Set the default application name and language mName = VER_PRODUCT_NAME_STR; mLanguage = QLocale::system().name(); // Clear the XML file name mXmlFile.clear(); // Process environment variables QStringList env = QProcess::systemEnvironment(); for (int i = 0; i < env.size(); ++i) { // Get the name/value pair QString name = env.at(i).section('=', 0, 0).trimmed(); QString value = env.at(i).section('=', 1).trimmed(); if (name == "EVAF_APP_NAME") mName = value; else if (name == "EVAF_LANGUAGE") mLanguage = value; } // Then process command-line arguments env = QCoreApplication::arguments(); for (int i = 0; i < env.size(); ++i) { // Get the name and optional value QStringList arg = env.at(i).simplified().split('='); if (QRegExp("-[-]?app(lication)?").exactMatch(arg.at(0)) && arg.size() > 1) mName = arg.at(1); else if (QRegExp("-[-]?lang(uage)?").exactMatch(arg.at(0)) && arg.size() > 1) mLanguage = arg.at(1); } } QString const App::xmlFileName() const { if (mXmlFile.isEmpty()) { QFileInfo fi; // Try the full application name + country + language combination QString name = mName + "_" + mLanguage + ".xml"; fi.setFile(iEnv::instance()->etcDir() + name); if (fi.isReadable()) mXmlFile = name; else { // Try application name + country name = mName + "_" + mLanguage.left(2) + ".xml"; fi.setFile(iEnv::instance()->etcDir() + name); if (fi.isReadable()) mName = name; else // Fall-back to the generic name mXmlFile = mName + ".xml"; } } return mXmlFile; } void App::restart() { QCoreApplication::exit(RC_Restart); } void App::quit(bool err) { QCoreApplication::exit(err ? RC_Error : RC_Quit); }