+/**
+ * @file Common/env.cpp
+ * @brief iEnv 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 "env.h"
+#include "iregistry.h"
+#include "globals.h"
+#include "version.h"
+#include "iapp.h"
+
+#include <QtCore>
+#include <QDesktopServices>
+
+
+//-------------------------------------------------------------------
+
+using namespace eVaf::Common;
+
+iEnv * iEnv::instance()
+{
+ static Internal::Env singleton;
+ return &singleton;
+}
+
+
+//-------------------------------------------------------------------
+
+using namespace eVaf::Common::Internal;
+
+Env::Env()
+ : iEnv()
+{
+ setObjectName(QString("%1-iEnv").arg(VER_MODULE_NAME_STR));
+
+ // Set initial bin and root directories
+ mRootDir = mBinDir = qApp->applicationDirPath();
+ int t = mBinDir.lastIndexOf(QChar('/'), -1);
+ if (t >= 0)
+ mRootDir = mBinDir.left(t);
+
+ if (!mBinDir.endsWith('/'))
+ mBinDir.append('/');
+ if (!mRootDir.endsWith('/'))
+ mRootDir.append('/');
+}
+
+Env::~Env()
+{
+}
+
+bool Env::init()
+{
+ // Register out interface
+ iRegistry::instance()->registerInterface("iEnv", this);
+
+ // Clear directories
+ mDataRootDir.clear();
+ mQtPluginsDir.clear();
+ mEtcDir.clear();
+ mLogDir.clear();
+ mDocDir.clear();
+
+ // Process the environment
+ QStringList env = QProcess::systemEnvironment();
+ int sz = env.size();
+ for (int i = 0; i < sz; ++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_ROOT_DIR") {
+ mRootDir = value;
+ if (!mRootDir.endsWith('/'))
+ mRootDir.append('/');
+ }
+ else if (name == "EVAF_DATA_ROOT_DIR") {
+ mDataRootDir = value;
+ if (!mDataRootDir.endsWith('/'))
+ mDataRootDir.append('/');
+ }
+ else if (name == "EVAF_ETC_DIR") {
+ mEtcDir = value;
+ if (!mEtcDir.endsWith('/'))
+ mEtcDir.append('/');
+ }
+ else if (name == "EVAF_LOG_DIR") {
+ mLogDir = value;
+ if (!mLogDir.endsWith('/'))
+ mLogDir.append('/');
+ }
+ else if (name == "EVAF_DOC_DIR") {
+ mDocDir = value;
+ if (!mDocDir.endsWith('/'))
+ mDocDir.append('/');
+ }
+ else if (name == "EVAF_QT_PLUGINS_DIR") {
+ mQtPluginsDir = value;
+ if (!mQtPluginsDir.endsWith('/'))
+ mQtPluginsDir.append('/');
+ }
+ }
+
+ // Then process comman-line arguments
+ env = QCoreApplication::arguments();
+ sz = env.size();
+ for (int i = 0; i < sz; ++i) {
+ // Get the name and optional value
+ QStringList arg = env.at(i).simplified().split(QChar('='));
+
+ if (QRegExp("-[-]?root(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
+ mRootDir = arg.at(1);
+ if (!mRootDir.endsWith('/'))
+ mRootDir.append('/');
+ }
+ else if (QRegExp("-[-]?dataroot(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
+ mDataRootDir = arg.at(1);
+ if (!mDataRootDir.endsWith('/'))
+ mDataRootDir.append('/');
+ }
+ else if (QRegExp("-[-]?etc(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
+ mEtcDir = arg.at(1);
+ if (!mEtcDir.endsWith('/'))
+ mEtcDir.append('/');
+ }
+ else if (QRegExp("-[-]?log(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
+ mLogDir = arg.at(1);
+ if (!mLogDir.endsWith('/'))
+ mLogDir.append('/');
+ }
+ else if (QRegExp("-[-]?doc(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
+ mDocDir = arg.at(1);
+ if (!mDocDir.endsWith('/'))
+ mDocDir.append('/');
+ }
+ else if (QRegExp("-[-]?qtplugins(dir)?").exactMatch(arg.at(0)) && arg.size() > 1) {
+ mQtPluginsDir = arg.at(1);
+ if (!mQtPluginsDir.endsWith('/'))
+ mQtPluginsDir.append('/');
+ }
+ }
+
+ return true;
+}
+
+QString const Env::dataRootDir() const
+{
+ if (mDataRootDir.isEmpty()) {
+ QString dataLoc = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
+ if (!dataLoc.endsWith('/'))
+ dataLoc.append('/');
+ mDataRootDir = dataLoc.append(iApp::instance()->name());
+ if (!mDataRootDir.endsWith('/'))
+ mDataRootDir.append('/');
+ }
+
+ return mDataRootDir;
+}
+
+QString const Env::etcDir() const
+{
+ if (mEtcDir.isEmpty())
+ mEtcDir = dataRootDir() + "etc/";
+ return mEtcDir;
+}
+
+QString const Env::logDir() const
+{
+ if (mLogDir.isEmpty())
+ mLogDir = dataRootDir() + "log/";
+ return mLogDir;
+}
+
+QString const Env::docDir() const
+{
+ if (mDocDir.isEmpty())
+ mDocDir = rootDir() + "doc/";
+ return mDocDir;
+}
+
+QString const Env::qtPluginsDir() const
+{
+ if (mQtPluginsDir.isEmpty())
+ mQtPluginsDir = binDir();
+ return mQtPluginsDir;
+}