[refactor] cleand up get.c

This commit is contained in:
Julian Sparber
2017-09-18 12:06:15 +02:00
parent ceedb1ae43
commit 69b1bfc2ed

View File

@@ -1,9 +1,3 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2001-2003, Ximian, Inc.
* Copyright (C) 2013 Igalia, S.L.
*/
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include <config.h> #include <config.h>
#endif #endif
@@ -43,8 +37,10 @@ finished (SoupSession *session, SoupMessage *msg, gpointer target)
} }
int int
get (char *url, const gchar *originDevice, const gchar *downloadDirectory, const gchar *outputFilename) get (char *url,
{ const gchar *originDevice,
const gchar *downloadDirectory,
const gchar *outputFilename) {
GError *error = NULL; GError *error = NULL;
SoupLogger *logger = NULL; SoupLogger *logger = NULL;
@@ -94,11 +90,13 @@ get (char *url, const gchar *originDevice, const gchar *downloadDirectory, const
return 0; return 0;
} }
int saveFile (SoupMessage *msg, const gchar *outputDirectory, const gchar *outputFilename) { int
saveFile (SoupMessage *msg,
const gchar *outputDirectory,
const gchar *outputFilename) {
const char *name; const char *name;
FILE *output_file = NULL; FILE *outputFile = NULL;
//get (g_strdup(url), g_strdup_printf("./test_download/%s", g_uri_escape_string(filename, NULL, TRUE)));
name = soup_message_get_uri (msg)->path; name = soup_message_get_uri (msg)->path;
if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code)) if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code))
@@ -112,40 +110,42 @@ int saveFile (SoupMessage *msg, const gchar *outputDirectory, const gchar *outpu
//g_print ("%s: Got a file offered form a other peer. Will not save anything.\n", name); //g_print ("%s: Got a file offered form a other peer. Will not save anything.\n", name);
} }
else { else {
output_file = fopen (getFilePath(outputDirectory, outputFilename), "w"); outputFile = fopen (getFilePath(outputDirectory, outputFilename), "w");
if (!output_file) if (!outputFile)
g_printerr ("Error trying to create file %s.\n", outputFilename); g_printerr ("Error trying to create file %s.\n", outputFilename);
if (output_file) { if (outputFile) {
fwrite (msg->response_body->data, fwrite (msg->response_body->data,
1, 1,
msg->response_body->length, msg->response_body->length,
output_file); outputFile);
fclose (output_file); fclose (outputFile);
} }
} }
} }
return 0; return 0;
} }
int do_client_notify (char *url) int
{ do_client_notify (char *url) {
get (g_strdup(url), NULL, NULL, NULL); get (g_strdup(url), NULL, NULL, NULL);
g_print("Offering selected file to other machine.\n"); g_print("Offering selected file to other machine.\n");
return 0; return 0;
} }
gchar *
gchar * getFilePath (const gchar *outputDirectory, const gchar *outputFilename) { getFilePath (const gchar *outputDirectory,
const gchar *outputFilename) {
return g_strdup_printf("%s%s", outputDirectory, return g_strdup_printf("%s%s", outputDirectory,
g_uri_escape_string(outputFilename, NULL, TRUE)); g_uri_escape_string(outputFilename, NULL, TRUE));
} }
int int
do_downloading (const char *originDevice, const char *url, const char *filename) do_downloading (const char *originDevice,
{ const char *url,
const char *filename) {
gchar *outputDirectory = "./test_download/"; gchar *outputDirectory = "./test_download/";
g_print("Downloading %s to %s\n", url, g_uri_escape_string(filename, NULL, TRUE)); g_print("Downloading %s to %s\n", url, g_uri_escape_string(filename, NULL, TRUE));
get (g_strdup(url), originDevice, outputDirectory, filename); get (g_strdup(url), originDevice, outputDirectory, filename);