From 36c4ded89a72e609944c6fff261be77bb0697d45 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Mon, 25 Aug 2014 20:28:13 +0400 Subject: [PATCH] init --- LICENSE => COPYING | 0 README.md | 7 +++ language/language.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++ language/language.h | 43 ++++++++++++++++ pdebug/pdebug.h | 45 +++++++++++++++++ 5 files changed, 210 insertions(+) rename LICENSE => COPYING (100%) create mode 100644 language/language.cpp create mode 100644 language/language.h create mode 100644 pdebug/pdebug.h diff --git a/LICENSE b/COPYING similarity index 100% rename from LICENSE rename to COPYING diff --git a/README.md b/README.md index f82f4a1..dfa3003 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,10 @@ qtadds ====== Some additional Qt/C++ headers + + +Description +----------- + +* `language` - class which can be used to define application language using command line options, configuration file or system locale +* `pdebug` - inline which returns `[CLASS::Method]` diff --git a/language/language.cpp b/language/language.cpp new file mode 100644 index 0000000..c3699f3 --- /dev/null +++ b/language/language.cpp @@ -0,0 +1,115 @@ +/*************************************************************************** + * Copyright (C) 2014 Evgeniy Alekseev * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 3.0 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library. * + ***************************************************************************/ + + +#include "language.h" + +#include +#include + + +Language::Language() +{ +} + + +QString Language::checkLanguage(const QString language, const QString defaultLanguage) +{ + QStringList availableLanguages = getAvailableLanguages(); + for (int i=0; i optionsDict = parseOptions(options); + if (optionsDict.contains(QString("LANGUAGE"))) + if (getAvailableLanguages().contains(optionsDict[QString("LANGUAGE")])) + return optionsDict[QString("LANGUAGE")]; + + QString language; + language = defineLanguageFromFile(configPath); + if (language.isEmpty()) + language = defineLanguageFromLocale(); + language = checkLanguage(language, QString("en")); + + return language; +} + + +QString Language::defineLanguageFromFile(const QString configPath) +{ + QMap settings; + if (configPath.isEmpty()) + return QString(""); + QFile configFile(configPath); + QString fileStr; + if (!configFile.open(QIODevice::ReadOnly)) + return QString(""); + while (true) { + fileStr = QString(configFile.readLine()).trimmed(); + if ((fileStr.isEmpty()) && (!configFile.atEnd())) continue; + if ((fileStr[0] == QChar('#')) && (!configFile.atEnd())) continue; + if ((fileStr[0] == QChar(';')) && (!configFile.atEnd())) continue; + if (fileStr.contains(QChar('='))) + settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1]; + if (configFile.atEnd()) break; + } + configFile.close(); + + if (settings.contains(QString("LANGUAGE"))) + return settings[QString("LANGUAGE")]; + else + return QString(""); +} + + +QString Language::defineLanguageFromLocale() +{ + return QLocale::system().name(); +} + + +QStringList Language::getAvailableLanguages() +{ + QStringList languages; + languages.append(QString("en")); + // put your languages here + + return languages; +} + + +QMap Language::parseOptions(const QString options) +{ + QMap optionsDict; + for (int i=0; i