qad_locations
This commit is contained in:
2
pip
2
pip
Submodule pip updated: de17f93679...2c5e4e9b4c
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#ifdef QAD_STATIC_DEFINE
|
||||
#if defined(QAD_STATIC_DEFINE) || defined(Q_CC_GNU) || defined(DOXYGEN)
|
||||
# define QAD_EXPORT
|
||||
#else
|
||||
# ifdef QAD_SHARED_DEFINE
|
||||
|
||||
116
qad/utils/qad_locations.cpp
Normal file
116
qad/utils/qad_locations.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "qad_locations.h"
|
||||
#include <QDir>
|
||||
#include <QDirIterator>
|
||||
#include <QTranslator>
|
||||
#include <QApplication>
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
||||
# include <QStandardPaths>
|
||||
#else
|
||||
# include <QDesktopServices>
|
||||
#endif
|
||||
|
||||
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
|
||||
# define MOBILE_VIEW
|
||||
#endif
|
||||
|
||||
|
||||
QString QAD::userPath(QAD::LocationType loc, QString name) {
|
||||
QString dir, ext;
|
||||
switch (loc) {
|
||||
case ctConfig: ext = ".conf"; break;
|
||||
case ctCache : ext = ".cache"; break;
|
||||
}
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
||||
QStandardPaths::StandardLocation l = QStandardPaths::AppConfigLocation;
|
||||
switch (loc) {
|
||||
case ctConfig: l = QStandardPaths::AppConfigLocation; break;
|
||||
case ctCache : l = QStandardPaths::CacheLocation; break;
|
||||
}
|
||||
dir = QStandardPaths::writableLocation(l);
|
||||
#else
|
||||
QDesktopServices::StandardLocation l = QDesktopServices::DataLocation;
|
||||
switch (loc) {
|
||||
case ctConfig: l = QDesktopServices::DataLocation; break;
|
||||
case ctCache : l = QDesktopServices::CacheLocation; break;
|
||||
}
|
||||
dir = QDesktopServices::storageLocation(l);
|
||||
#endif
|
||||
if (!QDir(dir).exists())
|
||||
QDir().mkpath(dir);
|
||||
return dir + "/" + name + ext;
|
||||
}
|
||||
|
||||
|
||||
QStringList QAD::resourcePaths(QString type) {
|
||||
QStringList ret;
|
||||
ret << (QApplication::applicationDirPath() + "/" + type);
|
||||
#ifdef MOBILE_VIEW
|
||||
ret << (":/" + type);
|
||||
#elif defined(Q_OS_MACOS)
|
||||
ret << (QApplication::applicationDirPath() + "/../Resources/" + type);
|
||||
#elif defined(Q_OS_WINDOWS)
|
||||
#else
|
||||
ret << QString("/usr/share/%1/%2/%3").arg(QApplication::organizationName(), QApplication::applicationName(), type);
|
||||
ret << QString("/usr/share/%1/%2").arg(QApplication::organizationName(), type);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void QAD::loadTranslations(QString lang) {
|
||||
if (lang.isEmpty())
|
||||
lang = QLocale().bcp47Name();
|
||||
QString short_lang = lang.left(2);
|
||||
QStringList dirs = resourcePaths("lang");
|
||||
foreach (const QString & d, dirs) {
|
||||
QDirIterator dit(d);
|
||||
while (dit.hasNext()) {
|
||||
dit.next();
|
||||
if (!dit.filePath().endsWith(".qm")) continue;
|
||||
if (!dit.fileInfo().baseName().endsWith(lang) &&
|
||||
!dit.fileInfo().baseName().endsWith(short_lang)) continue;
|
||||
QTranslator * tr = new QTranslator();
|
||||
if (tr->load(dit.filePath())) {
|
||||
qApp->installTranslator(tr);
|
||||
qDebug() << "Add tr" << dit.fileName();
|
||||
} else {
|
||||
qDebug() << "Can`t load translation" << dit.fileName();
|
||||
delete tr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QStringList QAD::availableTranslations() {
|
||||
QSet<QString> ret;
|
||||
QStringList dirs = resourcePaths("lang");
|
||||
foreach (const QString & d, dirs) {
|
||||
QDirIterator dit(d);
|
||||
while (dit.hasNext()) {
|
||||
dit.next();
|
||||
if (!dit.filePath().endsWith(".qm")) continue;
|
||||
QTranslator * tr = new QTranslator();
|
||||
if (tr->load(dit.filePath())) {
|
||||
QString fn = dit.fileInfo().baseName(), lang[2];
|
||||
if (fn.contains("_")) {
|
||||
lang[0] = fn.right(fn.size() - fn.lastIndexOf("_") - 1);
|
||||
fn.chop(lang[0].size() + 1);
|
||||
if (fn.contains("_")) {
|
||||
lang[1] = fn.right(fn.size() - fn.lastIndexOf("_") - 1);
|
||||
fn.chop(lang[1].size() + 1);
|
||||
lang[1].append("_" + lang[0]);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
QLocale loc(lang[i]);
|
||||
if (loc.language() != QLocale::C)
|
||||
ret << lang[i];
|
||||
}
|
||||
//qDebug() << "Can`t load translation" << dit.fileName();
|
||||
delete tr;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret.toList();
|
||||
}
|
||||
59
qad/utils/qad_locations.h
Normal file
59
qad/utils/qad_locations.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
QAD - Qt ADvanced
|
||||
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program 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 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef QAD_LOCATIONS_H
|
||||
#define QAD_LOCATIONS_H
|
||||
|
||||
#include "qad_export.h"
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
namespace QAD {
|
||||
|
||||
enum QAD_EXPORT LocationType {
|
||||
ctConfig,
|
||||
ctCache,
|
||||
};
|
||||
|
||||
//! Create QStandardPaths::writableLocation(<loc>) directory
|
||||
//! and returns file name "<name>.<ext>".
|
||||
//! Extension is selected by "loc":
|
||||
//! * ctConfig - "conf"
|
||||
//! * ctCache - "cache"
|
||||
QAD_EXPORT QString userPath(LocationType loc, QString name = QString());
|
||||
|
||||
|
||||
//! Returns search directories for resource "type"
|
||||
//! * <applicationDirPath>/<type> presents on all platforms
|
||||
//! * :/<type> on mobile platforms
|
||||
//! * <.app>/Resources/<type> on MacOS
|
||||
//! * /usr/share/[organizationName/]<applicationName>/<type> on Linux
|
||||
QAD_EXPORT QStringList resourcePaths(QString type);
|
||||
|
||||
|
||||
QAD_EXPORT void loadTranslations(QString lang = QString());
|
||||
|
||||
|
||||
QAD_EXPORT QStringList availableTranslations();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // QAD_LOCATIONS_H
|
||||
Reference in New Issue
Block a user