Port translation code to use resource path from PlatformUtils.

master
Torsten Paul 2014-10-19 01:19:53 +02:00
parent e4071472ad
commit 6cd441e0f1
10 changed files with 40 additions and 73 deletions

View File

@ -90,7 +90,7 @@ FontCache::FontCache()
// If we've got a bundled fonts.conf, initialize fontconfig with our own config // If we've got a bundled fonts.conf, initialize fontconfig with our own config
// by overriding the built-in fontconfig path. // by overriding the built-in fontconfig path.
// For system installs and dev environments, we leave this alone // For system installs and dev environments, we leave this alone
fs::path fontdir(fs::path(PlatformUtils::resourcesPath()) / "fonts"); fs::path fontdir(PlatformUtils::resourcePath("fonts"));
if (fs::is_regular_file(fontdir / "fonts.conf")) { if (fs::is_regular_file(fontdir / "fonts.conf")) {
PlatformUtils::setenv("FONTCONFIG_PATH", boosty::stringy(boosty::absolute(fontdir)).c_str(), 0); PlatformUtils::setenv("FONTCONFIG_PATH", boosty::stringy(boosty::absolute(fontdir)).c_str(), 0);
} }
@ -103,7 +103,7 @@ FontCache::FontCache()
} }
// Add the built-in fonts & config // Add the built-in fonts & config
fs::path builtinfontpath = fs::path(PlatformUtils::resourcesPath()) / "fonts"; fs::path builtinfontpath(PlatformUtils::resourcePath("fonts"));
if (fs::is_directory(builtinfontpath)) { if (fs::is_directory(builtinfontpath)) {
FcConfigParseAndLoad(this->config, reinterpret_cast<const FcChar8 *>(boosty::stringy(builtinfontpath).c_str()), false); FcConfigParseAndLoad(this->config, reinterpret_cast<const FcChar8 *>(boosty::stringy(builtinfontpath).c_str()), false);
add_font_dir(boosty::stringy(boosty::canonical(builtinfontpath))); add_font_dir(boosty::stringy(boosty::canonical(builtinfontpath)));

View File

@ -1,7 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include "PlatformUtils.h" #include "PlatformUtils.h"
#include "boosty.h"
#include <Eigen/Core> #include <Eigen/Core>
#ifdef USE_SCINTILLA_EDITOR #ifdef USE_SCINTILLA_EDITOR
#include <Qsci/qsciglobal.h> #include <Qsci/qsciglobal.h>
@ -97,7 +96,7 @@ bool PlatformUtils::createBackupPath()
} }
// This is the built-in read-only resources path // This is the built-in read-only resources path
std::string PlatformUtils::resourcesPath() std::string PlatformUtils::resourceBasePath()
{ {
fs::path resourcedir(applicationPath()); fs::path resourcedir(applicationPath());
fs::path tmpdir; fs::path tmpdir;
@ -130,6 +129,21 @@ std::string PlatformUtils::resourcesPath()
return boosty::stringy(boosty::canonical(resourcedir)); return boosty::stringy(boosty::canonical(resourcedir));
} }
fs::path PlatformUtils::resourcePath(const std::string &resource)
{
fs::path base(resourceBasePath());
if (!fs::is_directory(base)) {
return fs::path();
}
fs::path resource_dir = base / resource;
if (!fs::is_directory(resource_dir)) {
return fs::path();
}
return resource_dir;
}
int PlatformUtils::setenv(const char *name, const char *value, int overwrite) int PlatformUtils::setenv(const char *name, const char *value, int overwrite)
{ {
#if defined(WIN32) #if defined(WIN32)

View File

@ -2,13 +2,16 @@
#include <string> #include <string>
#include "boosty.h"
namespace PlatformUtils { namespace PlatformUtils {
void registerApplicationPath(const std::string &applicationpath); void registerApplicationPath(const std::string &applicationpath);
std::string applicationPath(); std::string applicationPath();
std::string documentsPath(); std::string documentsPath();
std::string resourcesPath(); std::string resourceBasePath();
fs::path resourcePath(const std::string& resource);
std::string userLibraryPath(); std::string userLibraryPath();
bool createUserLibraryPath(); bool createUserLibraryPath();
std::string backupPath(); std::string backupPath();

View File

@ -89,8 +89,8 @@ QStringList UIUtils::exampleCategories()
QFileInfoList UIUtils::exampleFiles(const QString &category) QFileInfoList UIUtils::exampleFiles(const QString &category)
{ {
QDir dir(QString::fromStdString(PlatformUtils::resourcesPath())); QDir dir(QString::fromStdString(PlatformUtils::resourcePath("examples").string()));
if (!dir.cd("examples") || !dir.cd(category)) { if (!dir.cd(category)) {
return QFileInfoList(); return QFileInfoList();
} }

View File

@ -164,7 +164,7 @@ void localization_init() {
#ifdef LOCALE_PREFIX #ifdef LOCALE_PREFIX
std::string locale_path(LOCALE_PREFIX); std::string locale_path(LOCALE_PREFIX);
#else #else
fs::path po_dir = get_resource_dir("locale"); fs::path po_dir(PlatformUtils::resourcePath("locale"));
std::string locale_path(po_dir.string()); std::string locale_path(po_dir.string());
#endif #endif
if (fs::is_directory(locale_path)) { if (fs::is_directory(locale_path)) {
@ -279,7 +279,7 @@ int cmdline(const char *deps_output_file, const std::string &filename, Camera &c
const std::string application_path = boosty::stringy(boosty::absolute(boost::filesystem::path(argv[0]).parent_path())); const std::string application_path = boosty::stringy(boosty::absolute(boost::filesystem::path(argv[0]).parent_path()));
#endif #endif
PlatformUtils::registerApplicationPath(application_path); PlatformUtils::registerApplicationPath(application_path);
parser_init(PlatformUtils::applicationPath()); parser_init();
localization_init(); localization_init();
Tree tree; Tree tree;
@ -578,7 +578,7 @@ int gui(vector<string> &inputFiles, const fs::path &original_path, int argc, cha
const QString &app_path = app.applicationDirPath(); const QString &app_path = app.applicationDirPath();
PlatformUtils::registerApplicationPath(app_path.toLocal8Bit().constData()); PlatformUtils::registerApplicationPath(app_path.toLocal8Bit().constData());
parser_init(PlatformUtils::applicationPath()); parser_init();
QSettings settings; QSettings settings;
if (settings.value("advanced/localization", true).toBool()) { if (settings.value("advanced/localization", true).toBool()) {

View File

@ -7,7 +7,6 @@
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
static std::string applicationdir;
std::vector<std::string> librarypath; std::vector<std::string> librarypath;
static void add_librarydir(const std::string &libdir) static void add_librarydir(const std::string &libdir)
@ -15,47 +14,6 @@ static void add_librarydir(const std::string &libdir)
librarypath.push_back(libdir); librarypath.push_back(libdir);
} }
fs::path get_resource_dir(const std::string &resource_folder)
{
if (!fs::is_directory(applicationdir)) {
return fs::path();
}
fs::path basepath(applicationdir);
fs::path paths[] = {
#if __APPLE__
// Application layout when installed on MacOS
basepath.parent_path().parent_path() / "Contents" / "Resources",
#endif
#ifdef __unix__
// Different unix installation layouts are possible, this
// tries to capture the most obvious cases.
basepath.parent_path() / "share" / "openscad",
basepath.parent_path().parent_path() / "share" / "openscad",
fs::path("..") / "..",
#endif
#ifdef OPENSCAD_TESTING
// Used when running the test cases from source code layout.
fs::path(".."),
#endif
// Try to fall back to path relative to the executable and
// relative to the current working directory.
basepath,
fs::path("."),
fs::path(), // end of list marker
};
for (int a = 0;!paths[a].empty();a++) {
fs::path resource_dir = paths[a] / resource_folder;
if (fs::is_directory(resource_dir)) {
return resource_dir;
}
}
return fs::path();
}
/*! /*!
Searces for the given file in library paths and returns the full path if found. Searces for the given file in library paths and returns the full path if found.
Returns an empty path if file cannot be found or filename is a directory. Returns an empty path if file cannot be found or filename is a directory.
@ -131,9 +89,8 @@ fs::path find_valid_path(const fs::path &sourcepath,
return fs::path(); return fs::path();
} }
void parser_init(const std::string &applicationpath) void parser_init()
{ {
applicationdir = applicationpath;
// Add paths from OPENSCADPATH before adding built-in paths // Add paths from OPENSCADPATH before adding built-in paths
const char *openscadpaths = getenv("OPENSCADPATH"); const char *openscadpaths = getenv("OPENSCADPATH");
if (openscadpaths) { if (openscadpaths) {
@ -149,5 +106,5 @@ void parser_init(const std::string &applicationpath)
add_librarydir(PlatformUtils::userLibraryPath()); add_librarydir(PlatformUtils::userLibraryPath());
#endif #endif
add_librarydir(boosty::absolute(fs::path(PlatformUtils::resourcesPath()) / "libraries").string()); add_librarydir(boosty::absolute(PlatformUtils::resourcePath("libraries")).string());
} }

View File

@ -8,22 +8,9 @@ namespace fs = boost::filesystem;
extern int parser_error_pos; extern int parser_error_pos;
/** /**
* Initialize application an library path. * Initialize library path.
*
* @param applicationpath path of the application binary, this is usually
* derived from the Qt application object. If Qt is disabled, argv[0] is used.
*/ */
void parser_init(const std::string &applicationpath); void parser_init();
/**
* Return a path to specific resources relative to the application binary.
* This is used to find resources bundled with the application, e.g. the
* translation files for the gettext library.
*
* @param folder subfolder for the resources (e.g. "po").
* @return the resource path.
*/
fs::path get_resource_dir(const std::string &resource_folder);
fs::path search_libs(const fs::path &localpath); fs::path search_libs(const fs::path &localpath);
fs::path find_valid_path(const fs::path &sourcepath, fs::path find_valid_path(const fs::path &sourcepath,

View File

@ -52,6 +52,7 @@ namespace fs = boost::filesystem;
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
namespace po = boost::program_options; namespace po = boost::program_options;
#include "boosty.h" #include "boosty.h"
#include "PlatformUtils.h"
std::string commandline_commands; std::string commandline_commands;
std::string currentdir; std::string currentdir;
@ -116,7 +117,8 @@ int main(int argc, char **argv)
currentdir = boosty::stringy(fs::current_path()); currentdir = boosty::stringy(fs::current_path());
parser_init(boosty::stringy(fs::path(argv[0]).branch_path())); PlatformUtils::registerApplicationPath(boosty::stringy(fs::path(argv[0]).branch_path()));
parser_init();
ModuleContext top_ctx; ModuleContext top_ctx;
top_ctx.registerBuiltin(); top_ctx.registerBuiltin();

View File

@ -48,6 +48,7 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
#include "boosty.h" #include "boosty.h"
#include "PlatformUtils.h"
std::string commandline_commands; std::string commandline_commands;
std::string currentdir; std::string currentdir;
@ -77,7 +78,8 @@ int main(int argc, char **argv)
currentdir = boosty::stringy( fs::current_path() ); currentdir = boosty::stringy( fs::current_path() );
parser_init(boosty::stringy(fs::path(argv[0]).branch_path())); PlatformUtils::registerApplicationPath(boosty::stringy(fs::path(argv[0]).branch_path()));
parser_init();
ModuleContext top_ctx; ModuleContext top_ctx;
top_ctx.registerBuiltin(); top_ctx.registerBuiltin();

View File

@ -46,6 +46,7 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
#include "boosty.h" #include "boosty.h"
#include "PlatformUtils.h"
std::string commandline_commands; std::string commandline_commands;
std::string currentdir; std::string currentdir;
@ -73,7 +74,8 @@ int main(int argc, char **argv)
currentdir = boosty::stringy( fs::current_path() ); currentdir = boosty::stringy( fs::current_path() );
parser_init(boosty::stringy(fs::path(argv[0]).branch_path())); PlatformUtils::registerApplicationPath(boosty::stringy(fs::path(argv[0]).branch_path()));
parser_init();
ModuleContext top_ctx; ModuleContext top_ctx;
top_ctx.registerBuiltin(); top_ctx.registerBuiltin();