From ca8aee4701f95a758f93033eb88ca851d61b2008 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 16 Dec 2013 09:09:41 -0800 Subject: [PATCH] Move the XSLT files into a Qt resource This means we no longer need to keep them on disk and worry about installing / uninstalling them. They will always be kept in-memory (compressed). Signed-off-by: Thiago Macieira Signed-off-by: Dirk Hohndel --- packaging/windows/subsurface.nsi.in | 3 -- parse-xml.c | 51 ----------------------------- qt-gui.cpp | 36 ++++++++++++++++++++ subsurface-install.pri | 11 ++----- subsurface.qrc | 14 +++++++- 5 files changed, 52 insertions(+), 63 deletions(-) diff --git a/packaging/windows/subsurface.nsi.in b/packaging/windows/subsurface.nsi.in index e8246fc00..2803a049e 100644 --- a/packaging/windows/subsurface.nsi.in +++ b/packaging/windows/subsurface.nsi.in @@ -99,7 +99,6 @@ Section # Files to include in installer File subsurface.exe - File /r xslt File /r data File /r images File /r plugins @@ -156,14 +155,12 @@ Section "Uninstall" # Delete installed files Delete "$INSTDIR\*.dll" - Delete "$INSTDIR\xslt\*.xslt" Delete "$INSTDIR\freetype-config" Delete "$INSTDIR\subsurface.exe" Delete "$INSTDIR\subsurface.ico" Delete "$INSTDIR\Uninstall.exe" Delete "$INSTDIR\qt.conf" RMDir /r "$INSTDIR\share" - RMDir /r "$INSTDIR\xslt" RMDir /r "$INSTDIR\data" RMDir /r "$INSTDIR\images" RMDir /r "$INSTDIR\translations" diff --git a/parse-xml.c b/parse-xml.c index 3a3f65419..ea9e96a5e 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -1970,57 +1970,6 @@ void parse_xml_exit(void) xmlCleanupParser(); } -static xsltStylesheetPtr try_get_stylesheet(const char *path, int len, const char *name) -{ - xsltStylesheetPtr ret; - int namelen = strlen(name); - char *filename = malloc(len+1+namelen+1); - - if (!filename) - return NULL; - - memcpy(filename, path, len); -#ifdef WIN32 - filename[len] = '\\'; -#else - filename[len] = '/'; -#endif - memcpy(filename + len + 1, name, namelen+1); - - ret = NULL; - if (!access(filename, R_OK)) - ret = xsltParseStylesheetFile(filename); - free(filename); - - return ret; -} - -xsltStylesheetPtr get_stylesheet(const char *name) -{ - const char *path, *next; - - path = getenv("SUBSURFACE_XSLT_PATH"); - if (!path) - path = xslt_path; - - do { - int len; - xsltStylesheetPtr ret; - - next = strchr(path, ':'); - len = strlen(path); - if (next) { - len = next - path; - next++; - } - ret = try_get_stylesheet(path, len, name); - if (ret) - return ret; - } while ((path = next) != NULL); - - return NULL; -} - static struct xslt_files { const char *root; const char *file; diff --git a/qt-gui.cpp b/qt-gui.cpp index 78ce3c9a3..67061ef81 100644 --- a/qt-gui.cpp +++ b/qt-gui.cpp @@ -9,6 +9,8 @@ #include #include +#include + #include "dive.h" #include "divelist.h" #include "display.h" @@ -35,6 +37,7 @@ #include #include #include +#include #include #include @@ -507,3 +510,36 @@ QString get_trip_date_string(timestamp_t when, int nr) .arg(monthname(tm.tm_mon)) .arg(tm.tm_year + 1900); } + +static xmlDocPtr get_stylesheet_doc(const xmlChar *uri, xmlDictPtr, int, void *, xsltLoadType) +{ + QFile f(QLatin1String(":/xslt/") + (const char *)uri); + if (!f.open(QIODevice::ReadOnly)) + return NULL; + + /* Load and parse the data */ + QByteArray source = f.readAll(); + + xmlDocPtr doc = xmlParseMemory(source, source.size()); + return doc; +} + +xsltStylesheetPtr get_stylesheet(const char *name) +{ + // this needs to be done only once, but doesn't hurt to run every time + xsltSetLoaderFunc(get_stylesheet_doc); + + // get main document: + xmlDocPtr doc = get_stylesheet_doc((const xmlChar *)name, NULL, 0, NULL, XSLT_LOAD_START); + if (!doc) + return NULL; + +// xsltSetGenericErrorFunc(stderr, NULL); + xsltStylesheetPtr xslt = xsltParseStylesheetDoc(doc); + if (!xslt) { + xmlFreeDoc(doc); + return NULL; + } + + return xslt; +} diff --git a/subsurface-install.pri b/subsurface-install.pri index 8e97ba3bf..1a29d0f3a 100644 --- a/subsurface-install.pri +++ b/subsurface-install.pri @@ -1,5 +1,4 @@ marbledir.files = $$MARBLEDIR -xslt.files = $$XSLT_FILES doc.files = $$DOC_FILES translation.files = $$replace(TRANSLATIONS, .ts, .qm) exists($$[QT_INSTALL_TRANSLATIONS]) { @@ -27,11 +26,10 @@ mac { datadir = Contents/Resources/share marbledir.path = Contents/Resources/data - xslt.path = $$datadir doc.path = $$datadir/Documentation translation.path = Contents/Resources/translations qttranslation.path = Contents/Resources/translations - QMAKE_BUNDLE_DATA += marbledir xslt doc translation qttranslation + QMAKE_BUNDLE_DATA += marbledir doc translation qttranslation mac_deploy.target = mac-deploy mac_deploy.commands += $$[QT_INSTALL_BINS]/macdeployqt $${TARGET}.app @@ -54,7 +52,6 @@ mac { doc.path = $$WINDOWSSTAGING/Documentation CONFIG -= copy_dir_files deploy.path = $$WINDOWSSTAGING - deploy.files += $$xslt.files deploy.CONFIG += no_check_exist target.path = $$WINDOWSSTAGING marbledir.path = $$WINDOWSSTAGING/data @@ -127,13 +124,12 @@ mac { # This is a fake rule just to create some rules in the target file nl = $$escape_expand(\\n) dummy.target = dummy-only-for-var-expansion - dummy.commands = $${nl}prefix = /usr$${nl}\ + dummy.commands = $${nl}prefix = /usr$${nl}\ BINDIR = $(prefix)/bin$${nl}\ DATADIR = $(prefix)/share$${nl}\ DOCDIR = $(DATADIR)/subsurface/Documentation$${nl}\ DESKTOPDIR = $(DATADIR)/applications$${nl}\ MANDIR = $(DATADIR)/man/man1$${nl}\ -XSLTDIR = $(DATADIR)/subsurface QMAKE_EXTRA_TARGETS += dummy WINDOWSSTAGING = ./packaging/windows @@ -146,7 +142,6 @@ XSLTDIR = $(DATADIR)/subsurface manpage.path = /$(MANDIR) manpage.files = $$MANPAGE - xslt.path = /$(XSLTDIR) marbledir.path = /$(DATADIR)/subsurface/data doc.path = /$(DOCDIR) @@ -155,7 +150,7 @@ XSLTDIR = $(DATADIR)/subsurface translation.path = /$(DATADIR)/subsurface/translations translation.CONFIG += no_check_exist - INSTALLS += target desktop manpage xslt doc marbledir translation + INSTALLS += target desktop manpage doc marbledir translation install.target = install } !isEmpty(TRANSLATIONS) { diff --git a/subsurface.qrc b/subsurface.qrc index 796695eb8..970ac33f0 100644 --- a/subsurface.qrc +++ b/subsurface.qrc @@ -29,5 +29,17 @@ icons/planner/left_wing.png icons/planner/minus.png icons/planner/plus.png + xslt/commonTemplates.xsl + xslt/csv2xml.xslt + xslt/divelogs-export.xslt + xslt/divelogs.xslt + xslt/DivingLog.xslt + xslt/jdivelog2subsurface.xslt + xslt/MacDive.xslt + xslt/SuuntoDM4.xslt + xslt/SuuntoSDM.xslt + xslt/udcf.xslt + xslt/uddf-export.xslt + xslt/uddf.xslt - \ No newline at end of file +