From fdc85f45fc67bed8edf2665db574d67b1ee208a5 Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Sat, 1 Dec 2018 10:46:04 +0100 Subject: [PATCH] Resource loading / finding fixed #418 --- src/control/XournalMain.cpp | 117 +++++++++++++++++---------- src/control/XournalMain.h | 7 +- src/control/jobs/CustomExportJob.cpp | 2 +- src/gui/GladeSearchpath.cpp | 2 +- 4 files changed, 82 insertions(+), 46 deletions(-) diff --git a/src/control/XournalMain.cpp b/src/control/XournalMain.cpp index 87508129..40d6746e 100644 --- a/src/control/XournalMain.cpp +++ b/src/control/XournalMain.cpp @@ -19,6 +19,7 @@ #include #include #include +namespace bf = boost::filesystem; #ifdef __APPLE__ #undef ENABLE_NLS @@ -26,8 +27,6 @@ namespace bf = boost::filesystem; -#include -using std::string; #include using std::cout; using std::cerr; @@ -45,7 +44,7 @@ XournalMain::~XournalMain() XOJ_RELEASE_TYPE(XournalMain); } -//it HAS to be done – otherwise such things like boost::algorithm::to_lower wont work, throwing casting exceptions +// it HAS to be done – otherwise such things like boost::algorithm::to_lower wont work, throwing casting exceptions void XournalMain::initLocalisation() { XOJ_CHECK_TYPE(XournalMain); @@ -60,7 +59,7 @@ void XournalMain::initLocalisation() textdomain(GETTEXT_PACKAGE); #endif //ENABLE_NLS - std::locale::global(gen("")); //"" - system default locale + std::locale::global(gen("")); // "" - system default locale std::cout.imbue(std::locale()); } @@ -256,7 +255,10 @@ int XournalMain::run(int argc, char* argv[]) // Init GTK Display gdk_display_open_default_libgtk_only(); - GladeSearchpath* gladePath = initPath(argv[0]); + initSettingsPath(); + + GladeSearchpath* gladePath = new GladeSearchpath(); + initResourcePath(gladePath); // init singleton string colorNameFile = Util::getConfigFile("colornames.ini").string(); @@ -327,63 +329,92 @@ int XournalMain::run(int argc, char* argv[]) return 0; } -#define GLADE_UI_PATH "ui" - -/** - * Path for glade files and Pixmaps, first searches in the home folder, so you can customize glade files - */ -GladeSearchpath* XournalMain::initPath(const char* argv0) +void XournalMain::initSettingsPath() { XOJ_CHECK_TYPE(XournalMain); - GladeSearchpath* gladePath = new GladeSearchpath(); - // Create config directory if not exists path file = Util::getConfigSubfolder(""); bf::create_directories(file); bf::permissions(file, bf::perms::owner_all); +} - // Add first home dir to search path, to add custom glade XMLs +/** + * Find a file in a resource folder, and return the resource folder path + * Return an empty string, if the folder was not found + */ +string XournalMain::findResourcePath(string searchFile) +{ + XOJ_CHECK_TYPE(XournalMain); + + // First check if the files are available relative to the executable + // So a "portable" installation will be possible + path relative1 = searchFile; + + if (bf::exists(relative1)) { - path searchPath = Util::getConfigSubfolder("ui"); - if (bf::exists(searchPath) && bf::is_directory(searchPath)) + return relative1.parent_path().string(); + } + + // ----------------------------------------------------------------------- + + // Check if we are in the "build" directory, and therefore the resources + // are installed two folders back + path relative2 = "../.."; + relative2 /= searchFile; + + if (bf::exists(relative2)) + { + return relative2.string(); + } + + // ----------------------------------------------------------------------- + + // Check if the files are in the current working directory + char buffer[512] = { 0 }; + char* workingDir = getcwd(buffer, sizeof(buffer)); + if (workingDir != NULL) + { + path relative3 = workingDir; + relative3 /= searchFile; + + if (bf::exists(relative3)) { - gladePath->addSearchDirectory(searchPath.c_str()); + return relative3.string(); } } - gchar* path = g_path_get_dirname(argv0); - gchar* searchPath = g_build_filename(path, GLADE_UI_PATH, NULL); - gladePath->addSearchDirectory(searchPath); - g_free(searchPath); + // Not found + return ""; +} - searchPath = g_build_filename(path, "..", GLADE_UI_PATH, NULL); - gladePath->addSearchDirectory(searchPath); - g_free(searchPath); - g_free(path); +void XournalMain::initResourcePath(GladeSearchpath* gladePath) +{ + XOJ_CHECK_TYPE(XournalMain); - char buffer[512] = { 0 }; - path = getcwd(buffer, sizeof(buffer)); - if (path == NULL) + string uiPath = findResourcePath("ui/about.glade"); + + if (uiPath != "") { - return gladePath; + gladePath->addSearchDirectory(uiPath); + return; } - searchPath = g_build_filename(path, GLADE_UI_PATH, NULL); - gladePath->addSearchDirectory(searchPath); - g_free(searchPath); + // ----------------------------------------------------------------------- - searchPath = g_build_filename(path, "..", GLADE_UI_PATH, NULL); - gladePath->addSearchDirectory(searchPath); - g_free(searchPath); - - searchPath = g_build_filename(PROJECT_SOURCE_DIR, GLADE_UI_PATH, NULL); - gladePath->addSearchDirectory(searchPath); - g_free(searchPath); + // Check at the target installation directory + path absolute = PACKAGE_DATA_DIR; + absolute /= PROJECT_PACKAGE; + absolute /= "ui/about.glade"; + + if (bf::exists(absolute)) + { + gladePath->addSearchDirectory(absolute.parent_path().string()); + return; + } - searchPath = g_build_filename(PACKAGE_DATA_DIR, PROJECT_PACKAGE, GLADE_UI_PATH, NULL); - gladePath->addSearchDirectory(searchPath); - g_free(searchPath); + string msg = _("Missing the needed UI file, could not find them at any location.\nNot relative\nNot in the Working Path\nNot in " PACKAGE_DATA_DIR); + Util::showErrorToUser(NULL, msg.c_str()); - return gladePath; + exit(12); } diff --git a/src/control/XournalMain.h b/src/control/XournalMain.h index 944ea9b2..d44cfff3 100644 --- a/src/control/XournalMain.h +++ b/src/control/XournalMain.h @@ -14,6 +14,9 @@ #include #include +#include +using std::string; + class GladeSearchpath; class XournalMain @@ -32,7 +35,9 @@ private: void checkForEmergencySave(); int exportPdf(const char* input, const char* output); - GladeSearchpath* initPath(const char* argv0); + void initSettingsPath(); + void initResourcePath(GladeSearchpath* gladePath); + string findResourcePath(string searchFile); private: XOJ_TYPE_ATTRIB; diff --git a/src/control/jobs/CustomExportJob.cpp b/src/control/jobs/CustomExportJob.cpp index 4fc06de1..97b9da3e 100644 --- a/src/control/jobs/CustomExportJob.cpp +++ b/src/control/jobs/CustomExportJob.cpp @@ -56,7 +56,7 @@ bool CustomExportJob::isUriValid(string& uri) string ext = filename.extension().string(); if (ext != ".pdf" && ext != ".png" && ext != ".xoj") { - string msg = _C("File name needs to end with .pdf, .png or .xoj"); + string msg = _("File name needs to end with .pdf, .png or .xoj"); Util::showErrorToUser(control->getGtkWindow(), msg); return false; } diff --git a/src/gui/GladeSearchpath.cpp b/src/gui/GladeSearchpath.cpp index 284cd60c..442a0fab 100644 --- a/src/gui/GladeSearchpath.cpp +++ b/src/gui/GladeSearchpath.cpp @@ -47,7 +47,7 @@ string GladeSearchpath::findFile(string subdir, string file) return ""; } -/* +/** * Use this function to set the directory containing installed pixmaps and Glade XML files. */ void GladeSearchpath::addSearchDirectory(string directory)