Merge pull request #427 from andreasb242/master

Resource loading / finding
presentation
andreasb242 7 years ago committed by GitHub
commit 89287d5dfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 117
      src/control/XournalMain.cpp
  2. 7
      src/control/XournalMain.h
  3. 2
      src/control/jobs/CustomExportJob.cpp
  4. 2
      src/gui/GladeSearchpath.cpp

@ -19,6 +19,7 @@
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
#include <boost/locale.hpp>
namespace bf = boost::filesystem;
#ifdef __APPLE__
#undef ENABLE_NLS
@ -26,8 +27,6 @@
namespace bf = boost::filesystem;
#include <string>
using std::string;
#include <iostream>
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);
}

@ -14,6 +14,9 @@
#include <config.h>
#include <XournalType.h>
#include <string>
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;

@ -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;
}

@ -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)

Loading…
Cancel
Save