aboutsummaryrefslogtreecommitdiffstats
path: root/qt-ui
diff options
context:
space:
mode:
authorGravatar Thiago Macieira <thiago@macieira.org>2013-12-19 17:03:21 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2013-12-20 09:53:05 -0800
commitb654bcdd7aefc71e7fa367aae171b3afac8c4a41 (patch)
tree2fede4b2c6e3010e65b23014b80528a2de243566 /qt-ui
parent8eb6dfdb02cf7b9d2c92f9e6ef28ad5312415302 (diff)
downloadsubsurface-b654bcdd7aefc71e7fa367aae171b3afac8c4a41.tar.gz
Generate the file name for the dive log upload in the caller
This is a cleaner approach, according to the discussion in the mailing list. It is also better because we can use QTemporaryDir in Qt 5. Finally, it avoids having to remember to free it at every point.s Signed-off-by: Thiago Macieira <thiago@macieira.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui')
-rw-r--r--qt-ui/subsurfacewebservices.cpp31
1 files changed, 13 insertions, 18 deletions
diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp
index ef3c9ffce..afa368b7d 100644
--- a/qt-ui/subsurfacewebservices.cpp
+++ b/qt-ui/subsurfacewebservices.cpp
@@ -98,18 +98,18 @@ static void clear_table(struct dive_table *table)
table->nr = 0;
}
-static char *prepare_dives_for_divelogs(const bool selected)
+static bool prepare_dives_for_divelogs(const QString &tempfile, const bool selected)
{
static const char errPrefix[] = "divelog.de-upload:";
if (!amount_selected) {
qDebug() << errPrefix << "no dives selected";
- return NULL;
+ return false;
}
int i;
struct dive *dive;
FILE *f;
- char filename[PATH_MAX], *tempfile;
+ char filename[PATH_MAX];
int streamsize;
char *membuf;
xsltStylesheetPtr xslt = NULL;
@@ -120,18 +120,14 @@ static char *prepare_dives_for_divelogs(const bool selected)
xslt = get_stylesheet("divelogs-export.xslt");
if (!xslt) {
qDebug() << errPrefix << "missing stylesheet";
- return NULL;
+ return false;
}
- /* generate a random filename and create/open that file with zip_open */
- QString tempfileQ = QDir::tempPath() + "/import-" + QString::number(qrand() % 99999999) + ".dld";
- tempfile = strdup(tempfileQ.toUtf8().data());
- zip = zip_open(tempfile, ZIP_CREATE, NULL);
+ zip = zip_open(QFile::encodeName(tempfile), ZIP_CREATE, NULL);
if (!zip) {
qDebug() << errPrefix << "cannot open file as zip";
- free((void *)tempfile);
- return NULL;
+ return false;
}
/* walk the dive list in chronological order */
@@ -188,14 +184,13 @@ static char *prepare_dives_for_divelogs(const bool selected)
}
zip_close(zip);
xsltFreeStylesheet(xslt);
- return tempfile;
+ return true;
error_close_zip:
zip_close(zip);
- QFile::remove(tempfileQ);
- free(tempfile);
+ QFile::remove(tempfile);
xsltFreeStylesheet(xslt);
- return NULL;
+ return false;
}
WebServices::WebServices(QWidget* parent, Qt::WindowFlags f): QDialog(parent, f)
@@ -555,18 +550,18 @@ void DivelogsDeWebServices::downloadDives()
void DivelogsDeWebServices::prepareDivesForUpload()
{
QString errorText(tr("Cannot create DLD file"));
- char *filename = prepare_dives_for_divelogs(true);
- if (filename) {
+
+ /* generate a random filename and create/open that file with zip_open */
+ QString filename = QDir::tempPath() + "/import-" + QString::number(qrand() % 99999999) + ".dld";
+ if (prepare_dives_for_divelogs(filename, true)) {
QFile f(filename);
if (f.open(QIODevice::ReadOnly)) {
uploadDives((QIODevice *)&f);
f.close();
f.remove();
- free((void *)filename);
return;
}
mainWindow()->showError(errorText.append(": ").append(filename));
- free((void *)filename);
return;
}
mainWindow()->showError(errorText.append("!"));