mirror of
https://github.com/subsurface/subsurface.git
synced 2024-12-01 06:30:26 +00:00
Export dives in UDDF format
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@gmail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
bdedf46e4c
commit
3e48511318
5 changed files with 83 additions and 2 deletions
1
dive.h
1
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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ public slots:
|
|||
void mergeTripBelow();
|
||||
void mergeDives();
|
||||
void saveSelectedDivesAs();
|
||||
void exportSelectedDivesAsUDDF();
|
||||
|
||||
signals:
|
||||
void currentDiveChanged(int divenr);
|
||||
|
|
|
@ -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()
|
||||
|
|
60
save-xml.c
60
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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue