[PATCH 1/2] Move the XSLT files into a Qt resource

Thiago Macieira thiago at macieira.org
Mon Dec 16 09:09:41 UTC 2013


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 <thiago at macieira.org>
---
 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 e8246fc..2803a04 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 3a3f654..ea9e96a 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 78ce3c9..67061ef 100644
--- a/qt-gui.cpp
+++ b/qt-gui.cpp
@@ -9,6 +9,8 @@
 #include <sys/time.h>
 #include <ctype.h>
 
+#include <libxslt/documents.h>
+
 #include "dive.h"
 #include "divelist.h"
 #include "display.h"
@@ -35,6 +37,7 @@
 #include <QNetworkProxy>
 #include <QDateTime>
 #include <QRegExp>
+#include <QResource>
 #include <QLibraryInfo>
 
 #include <gettextfromc.h>
@@ -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 8e97ba3..1a29d0f 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 796695e..970ac33 100644
--- a/subsurface.qrc
+++ b/subsurface.qrc
@@ -29,5 +29,17 @@
 	<file alias="left_wing">icons/planner/left_wing.png</file>
 	<file alias="plan_minus">icons/planner/minus.png</file>
 	<file alias="plan_plus">icons/planner/plus.png</file>
+        <file>xslt/commonTemplates.xsl</file>
+        <file>xslt/csv2xml.xslt</file>
+        <file>xslt/divelogs-export.xslt</file>
+        <file>xslt/divelogs.xslt</file>
+        <file>xslt/DivingLog.xslt</file>
+        <file>xslt/jdivelog2subsurface.xslt</file>
+        <file>xslt/MacDive.xslt</file>
+        <file>xslt/SuuntoDM4.xslt</file>
+        <file>xslt/SuuntoSDM.xslt</file>
+        <file>xslt/udcf.xslt</file>
+        <file>xslt/uddf-export.xslt</file>
+        <file>xslt/uddf.xslt</file>
  </qresource>
- </RCC>
\ No newline at end of file
+ </RCC>
-- 
1.7.11.7



More information about the subsurface mailing list