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. 115
      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/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/locale.hpp> #include <boost/locale.hpp>
namespace bf = boost::filesystem;
#ifdef __APPLE__ #ifdef __APPLE__
#undef ENABLE_NLS #undef ENABLE_NLS
@ -26,8 +27,6 @@
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
#include <string>
using std::string;
#include <iostream> #include <iostream>
using std::cout; using std::cout;
using std::cerr; using std::cerr;
@ -45,7 +44,7 @@ XournalMain::~XournalMain()
XOJ_RELEASE_TYPE(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() void XournalMain::initLocalisation()
{ {
XOJ_CHECK_TYPE(XournalMain); XOJ_CHECK_TYPE(XournalMain);
@ -60,7 +59,7 @@ void XournalMain::initLocalisation()
textdomain(GETTEXT_PACKAGE); textdomain(GETTEXT_PACKAGE);
#endif //ENABLE_NLS #endif //ENABLE_NLS
std::locale::global(gen("")); //"" - system default locale std::locale::global(gen("")); // "" - system default locale
std::cout.imbue(std::locale()); std::cout.imbue(std::locale());
} }
@ -256,7 +255,10 @@ int XournalMain::run(int argc, char* argv[])
// Init GTK Display // Init GTK Display
gdk_display_open_default_libgtk_only(); gdk_display_open_default_libgtk_only();
GladeSearchpath* gladePath = initPath(argv[0]); initSettingsPath();
GladeSearchpath* gladePath = new GladeSearchpath();
initResourcePath(gladePath);
// init singleton // init singleton
string colorNameFile = Util::getConfigFile("colornames.ini").string(); string colorNameFile = Util::getConfigFile("colornames.ini").string();
@ -327,63 +329,92 @@ int XournalMain::run(int argc, char* argv[])
return 0; return 0;
} }
#define GLADE_UI_PATH "ui" void XournalMain::initSettingsPath()
/**
* Path for glade files and Pixmaps, first searches in the home folder, so you can customize glade files
*/
GladeSearchpath* XournalMain::initPath(const char* argv0)
{ {
XOJ_CHECK_TYPE(XournalMain); XOJ_CHECK_TYPE(XournalMain);
GladeSearchpath* gladePath = new GladeSearchpath();
// Create config directory if not exists // Create config directory if not exists
path file = Util::getConfigSubfolder(""); path file = Util::getConfigSubfolder("");
bf::create_directories(file); bf::create_directories(file);
bf::permissions(file, bf::perms::owner_all); 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"); return relative1.parent_path().string();
if (bf::exists(searchPath) && bf::is_directory(searchPath)) }
// -----------------------------------------------------------------------
// 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); // Not found
gchar* searchPath = g_build_filename(path, GLADE_UI_PATH, NULL); return "";
gladePath->addSearchDirectory(searchPath); }
g_free(searchPath);
void XournalMain::initResourcePath(GladeSearchpath* gladePath)
{
XOJ_CHECK_TYPE(XournalMain);
searchPath = g_build_filename(path, "..", GLADE_UI_PATH, NULL); string uiPath = findResourcePath("ui/about.glade");
gladePath->addSearchDirectory(searchPath);
g_free(searchPath);
g_free(path);
char buffer[512] = { 0 }; if (uiPath != "")
path = getcwd(buffer, sizeof(buffer));
if (path == NULL)
{ {
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); // Check at the target installation directory
gladePath->addSearchDirectory(searchPath); path absolute = PACKAGE_DATA_DIR;
g_free(searchPath); absolute /= PROJECT_PACKAGE;
absolute /= "ui/about.glade";
searchPath = g_build_filename(PROJECT_SOURCE_DIR, GLADE_UI_PATH, NULL); if (bf::exists(absolute))
gladePath->addSearchDirectory(searchPath); {
g_free(searchPath); gladePath->addSearchDirectory(absolute.parent_path().string());
return;
}
searchPath = g_build_filename(PACKAGE_DATA_DIR, PROJECT_PACKAGE, GLADE_UI_PATH, NULL); 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);
gladePath->addSearchDirectory(searchPath); Util::showErrorToUser(NULL, msg.c_str());
g_free(searchPath);
return gladePath; exit(12);
} }

@ -14,6 +14,9 @@
#include <config.h> #include <config.h>
#include <XournalType.h> #include <XournalType.h>
#include <string>
using std::string;
class GladeSearchpath; class GladeSearchpath;
class XournalMain class XournalMain
@ -32,7 +35,9 @@ private:
void checkForEmergencySave(); void checkForEmergencySave();
int exportPdf(const char* input, const char* output); int exportPdf(const char* input, const char* output);
GladeSearchpath* initPath(const char* argv0); void initSettingsPath();
void initResourcePath(GladeSearchpath* gladePath);
string findResourcePath(string searchFile);
private: private:
XOJ_TYPE_ATTRIB; XOJ_TYPE_ATTRIB;

@ -56,7 +56,7 @@ bool CustomExportJob::isUriValid(string& uri)
string ext = filename.extension().string(); string ext = filename.extension().string();
if (ext != ".pdf" && ext != ".png" && ext != ".xoj") 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); Util::showErrorToUser(control->getGtkWindow(), msg);
return false; return false;
} }

@ -47,7 +47,7 @@ string GladeSearchpath::findFile(string subdir, string file)
return ""; return "";
} }
/* /**
* Use this function to set the directory containing installed pixmaps and Glade XML files. * Use this function to set the directory containing installed pixmaps and Glade XML files.
*/ */
void GladeSearchpath::addSearchDirectory(string directory) void GladeSearchpath::addSearchDirectory(string directory)

Loading…
Cancel
Save