[PATCH] Export dives in UDDF format

Miika Turkia miika.turkia at gmail.com
Sat Oct 19 10:35:36 UTC 2013


Implement exporting in UDDF format as was done in Gtk version. File menu
exports all the dives, right click on selection exports the selected
ones.

Signed-off-by: Miika Turkia <miika.turkia at gmail.com>
---
 dive.h                 |  1 +
 qt-ui/divelistview.cpp | 15 ++++++++++++-
 qt-ui/divelistview.h   |  1 +
 qt-ui/mainwindow.cpp   |  8 ++++++-
 save-xml.c             | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/dive.h b/dive.h
index b1a3614..3724cef 100644
--- a/dive.h
+++ b/dive.h
@@ -621,6 +621,7 @@ extern void parse_csv_file(const char *filename, int time, int depth, int temp,
 extern void save_dives(const char *filename);
 extern void save_dives_logic(const char *filename, bool select_only);
 extern void save_dive(FILE *f, struct dive *dive);
+extern void export_dives_uddf(const char *filename, const bool selected);
 
 extern xsltStylesheetPtr get_stylesheet(const char *name);
 
diff --git a/qt-ui/divelistview.cpp b/qt-ui/divelistview.cpp
index 19fc51a..e64ad21 100644
--- a/qt-ui/divelistview.cpp
+++ b/qt-ui/divelistview.cpp
@@ -411,8 +411,10 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event)
 		popup.addAction(tr("delete dive"), this, SLOT(deleteDive()));
 	if (amount_selected > 1 && consecutive_selected())
 		popup.addAction(tr("merge selected dives"), this, SLOT(mergeDives()));
-	if (amount_selected >= 1)
+	if (amount_selected >= 1) {
 		popup.addAction(tr("save As"), this, SLOT(saveSelectedDivesAs()));
+		popup.addAction(tr("export As UDDF"), this, SLOT(exportSelectedDivesAsUDDF()));
+	}
 	// "collapse all" really closes all trips,
 	// "collapse" keeps the trip with the selected dive open
 	QAction * actionTaken = popup.exec(event->globalPos());
@@ -450,3 +452,14 @@ void DiveListView::saveSelectedDivesAs()
 	QByteArray bt = fileName.toLocal8Bit();
 	save_dives_logic(bt.data(), TRUE);
 }
+
+void DiveListView::exportSelectedDivesAsUDDF()
+{
+	QString filename;
+	QFileInfo fi(system_default_filename());
+
+	filename = QFileDialog::getSaveFileName(this, tr("Save File as"), fi.absolutePath(),
+						tr("UDDF files (*.uddf *.UDDF)"));
+	if (!filename.isNull() && !filename.isEmpty())
+		export_dives_uddf((const char *)filename.toStdString().c_str(), true);
+}
diff --git a/qt-ui/divelistview.h b/qt-ui/divelistview.h
index 9e0713b..c1222ab 100644
--- a/qt-ui/divelistview.h
+++ b/qt-ui/divelistview.h
@@ -41,6 +41,7 @@ public slots:
 	void mergeTripBelow();
 	void mergeDives();
 	void saveSelectedDivesAs();
+	void exportSelectedDivesAsUDDF();
 
 signals:
 	void currentDiveChanged(int divenr);
diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp
index 72dc1a0..69cf363 100644
--- a/qt-ui/mainwindow.cpp
+++ b/qt-ui/mainwindow.cpp
@@ -189,7 +189,13 @@ void MainWindow::on_actionImport_triggered()
 
 void MainWindow::on_actionExportUDDF_triggered()
 {
-	qDebug("actionExportUDDF");
+	QString filename;
+	QFileInfo fi(system_default_filename());
+
+	filename = QFileDialog::getSaveFileName(this, tr("Save File as"), fi.absolutePath(),
+						tr("UDDF files (*.uddf *.UDDF)"));
+	if (!filename.isNull() && !filename.isEmpty())
+		export_dives_uddf((const char *)filename.toStdString().c_str(), false);
 }
 
 void MainWindow::on_actionPrint_triggered()
diff --git a/save-xml.c b/save-xml.c
index 6d95cb4..29c7be4 100644
--- a/save-xml.c
+++ b/save-xml.c
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <time.h>
+#include <unistd.h>
 
 #include "dive.h"
 #include "device.h"
@@ -601,3 +602,62 @@ void save_dives_logic(const char *filename, const bool select_only)
 	fprintf(f, "</dives>\n</divelog>\n");
 	fclose(f);
 }
+
+void export_dives_uddf(const char *filename, const bool selected)
+{
+	FILE *f;
+	size_t streamsize;
+	char *membuf;
+	xmlDoc *doc;
+	xsltStylesheetPtr xslt = NULL;
+	xmlDoc *transformed;
+
+	if (!filename)
+		return;
+ 
+	/* Save XML to file and convert it into a memory buffer */
+	save_dives_logic(filename, selected);
+	f = fopen(filename, "r");
+	fseek(f, 0, SEEK_END);
+	streamsize = ftell(f);
+	rewind(f);
+
+	membuf = malloc(streamsize + 1);
+	if (!membuf || !fread(membuf, streamsize, 1, f)) {
+		fprintf(stderr, "Failed to read memory buffer\n");
+		return;
+	}
+	membuf[streamsize] = 0;
+	fclose(f);
+	unlink(filename);
+
+	/*
+	 * Parse the memory buffer into XML document and
+	 * transform it to UDDF format, finally dumping
+	 * the XML into a character buffer.
+	 */
+	doc = xmlReadMemory(membuf, strlen(membuf), "divelog", NULL, 0);
+	if (!doc) {
+		fprintf(stderr, "Failed to read XML memory\n");
+		return;
+	}
+	free((void *)membuf);
+	
+	/* Convert to UDDF format */
+	xslt = get_stylesheet("uddf-export.xslt");
+
+	if (!xslt) {
+		fprintf(stderr, "Failed to open UDDF conversion stylesheet\n");
+		return;
+	}
+	transformed = xsltApplyStylesheet(xslt, doc, NULL);
+	xsltFreeStylesheet(xslt);
+	xmlFreeDoc(doc);
+	
+	/* Write the transformed XML to file */
+	f = fopen(filename, "w");
+	xmlDocFormatDump(f, transformed, 1);
+	xmlFreeDoc(transformed);
+	
+	fclose(f);
+}
-- 
1.8.3.2



More information about the subsurface mailing list