mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
First simplistic implementation of a divelogs.de upload
This has no user interface and hardcodes a testing username / password. But it can successfully create a DLD file (thanks to Miika and Lubomir) and then uses libsoup to upload that to the server. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
3e1098bd03
commit
bd9f8ad7f8
5 changed files with 53 additions and 21 deletions
31
divelist.c
31
divelist.c
|
@ -29,6 +29,7 @@
|
|||
#include "dive.h"
|
||||
#include "display.h"
|
||||
#include "display-gtk.h"
|
||||
#include "webservice.h"
|
||||
|
||||
#include <gdk-pixbuf/gdk-pixdata.h>
|
||||
#include "satellite.h"
|
||||
|
@ -1992,7 +1993,6 @@ static void export_selected_dives_cb(GtkWidget *menuitem, GtkTreePath *path)
|
|||
* Creating a temporary .DLD file to be eventually uploaded to
|
||||
* divelogs.de. I wonder if this could be done in-memory.
|
||||
*/
|
||||
|
||||
tempfile = g_build_filename(tmpdir, "export.DLD-XXXXXX", NULL);
|
||||
int fd = g_mkstemp(tempfile);
|
||||
if (fd != -1)
|
||||
|
@ -2032,7 +2032,6 @@ static void export_selected_dives_cb(GtkWidget *menuitem, GtkTreePath *path)
|
|||
* transform it to divelogs.de format, finally dumping
|
||||
* the XML into a character buffer.
|
||||
*/
|
||||
|
||||
doc = xmlReadMemory(membuf, strlen(membuf), "divelog", NULL, 0);
|
||||
if (!doc)
|
||||
continue;
|
||||
|
@ -2050,27 +2049,19 @@ static void export_selected_dives_cb(GtkWidget *menuitem, GtkTreePath *path)
|
|||
/*
|
||||
* Save the XML document into a zip file.
|
||||
*/
|
||||
|
||||
snprintf(filename, PATH_MAX, "%d.xml", i + 1);
|
||||
if ((s[i]=zip_source_buffer(zip, membuf, streamsize, 0)) == NULL || zip_add(zip, filename, s[i]) == -1)
|
||||
fprintf(stderr, "failed to include dive %d\n", i);
|
||||
if (membuf)
|
||||
free((void *)membuf);
|
||||
s[i] = zip_source_buffer(zip, membuf, streamsize, 1);
|
||||
if (s[i]) {
|
||||
zip_int64_t ret = zip_add(zip, filename, s[i]);
|
||||
if (ret == -1)
|
||||
fprintf(stderr, "failed to include dive %d\n", i);
|
||||
}
|
||||
}
|
||||
zip_close(zip);
|
||||
|
||||
/*
|
||||
* divelogs.de upload functionality should get rid of this
|
||||
* message and use proper dialog to inform user of the success
|
||||
* or failure of the upload
|
||||
*/
|
||||
fprintf(stderr, "Created .DLD file %s\n", tempfile);
|
||||
|
||||
/* TODO: the file needs to be deleted after the upload
|
||||
|
||||
g_unlink(tempfile);
|
||||
|
||||
*/
|
||||
if (divelogde_upload(tempfile))
|
||||
g_unlink(tempfile);
|
||||
else
|
||||
fprintf(stderr,"upload of %s failed\n", tempfile);
|
||||
g_free(tempfile);
|
||||
}
|
||||
#endif
|
||||
|
|
2
file.c
2
file.c
|
@ -14,7 +14,7 @@
|
|||
#define O_BINARY 0
|
||||
#endif
|
||||
|
||||
static int readfile(const char *filename, struct memblock *mem)
|
||||
int readfile(const char *filename, struct memblock *mem)
|
||||
{
|
||||
int ret, fd;
|
||||
struct stat st;
|
||||
|
|
1
file.h
1
file.h
|
@ -7,5 +7,6 @@ struct memblock {
|
|||
};
|
||||
|
||||
extern int try_to_open_cochran(const char *filename, struct memblock *mem, GError **error);
|
||||
extern int readfile(const char *filename, struct memblock *mem);
|
||||
|
||||
#endif
|
||||
|
|
39
webservice.c
39
webservice.c
|
@ -6,6 +6,7 @@
|
|||
#include "dive.h"
|
||||
#include "divelist.h"
|
||||
#include "display-gtk.h"
|
||||
#include "file.h"
|
||||
|
||||
struct dive_table gps_location_table;
|
||||
static gboolean merge_locations_into_dives(void);
|
||||
|
@ -306,3 +307,41 @@ void webservice_download_dialog(void)
|
|||
if (has_previous_uid)
|
||||
free((void *)current_uid);
|
||||
}
|
||||
|
||||
int divelogde_upload(char *fn)
|
||||
{
|
||||
SoupMessage *msg;
|
||||
SoupMultipart *multipart;
|
||||
SoupSession *session;
|
||||
SoupBuffer *sbuf;
|
||||
gboolean ret = FALSE;
|
||||
gchar url[256] = "http://divelogs.de/DivelogsDirectImport.php";
|
||||
struct memblock mem;
|
||||
|
||||
if (readfile(fn, &mem) < 0)
|
||||
return ret;
|
||||
|
||||
sbuf = soup_buffer_new(SOUP_MEMORY_STATIC, mem.buffer, mem.size);
|
||||
session = soup_session_async_new();
|
||||
multipart = soup_multipart_new(SOUP_FORM_MIME_TYPE_MULTIPART);
|
||||
|
||||
/* this needs to come from a dialog box and be stored in the user config */
|
||||
soup_multipart_append_form_string(multipart, "user", "subsurfacetest");
|
||||
soup_multipart_append_form_string(multipart, "pass", "geheim");
|
||||
|
||||
soup_multipart_append_form_file(multipart, "userfile", fn, NULL, sbuf);
|
||||
msg = soup_form_request_new_from_multipart(url, multipart);
|
||||
soup_message_headers_append(msg->request_headers, "Accept", "text/xml");
|
||||
soup_session_send_message(session, msg);
|
||||
if (SOUP_STATUS_IS_SUCCESSFUL(msg->status_code)) {
|
||||
/* we should really check if the XML returned indicates that
|
||||
* the profiles were successfully uploaded...
|
||||
*/
|
||||
fprintf(stderr, "%s\n", (gchar *)msg->response_body->data);
|
||||
ret = TRUE;
|
||||
}
|
||||
soup_session_abort(session);
|
||||
g_object_unref(G_OBJECT(msg));
|
||||
g_object_unref(G_OBJECT(session));
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
extern void webservice_download_dialog(void);
|
||||
extern gboolean webservice_request_user_xml(const gchar *, gchar **, guint *, guint *);
|
||||
extern int divelogde_upload(char *fn);
|
||||
|
|
Loading…
Add table
Reference in a new issue