diff options
author | William Wennerström <william@wstrm.dev> | 2020-07-21 10:34:29 +0200 |
---|---|---|
committer | William Wennerström <william@wstrm.dev> | 2020-11-16 21:58:09 +0100 |
commit | 3d344cfeaa7708b5786e55d18af0cbcfc8ed25ab (patch) | |
tree | 83028c43e6227e54808dcdeaa06841df138da9ff /src | |
parent | 7a1eb730b1fff7b49def2b86730642cbe0f74d41 (diff) | |
download | profani-tty-3d344cfeaa7708b5786e55d18af0cbcfc8ed25ab.tar.gz |
Move common http tool code to http_common
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/aesgcm_download.c | 1 | ||||
-rw-r--r-- | src/tools/aesgcm_download.h | 3 | ||||
-rw-r--r-- | src/tools/http_common.c | 98 | ||||
-rw-r--r-- | src/tools/http_common.h | 48 | ||||
-rw-r--r-- | src/tools/http_download.c | 52 | ||||
-rw-r--r-- | src/tools/http_download.h | 5 |
6 files changed, 154 insertions, 53 deletions
diff --git a/src/tools/aesgcm_download.c b/src/tools/aesgcm_download.c index 6e8b89c5..6b60ba08 100644 --- a/src/tools/aesgcm_download.c +++ b/src/tools/aesgcm_download.c @@ -50,6 +50,7 @@ #include "profanity.h" #include "event/client_events.h" +#include "tools/http_common.h" #include "tools/aesgcm_download.h" #include "omemo/omemo.h" #include "config/preferences.h" diff --git a/src/tools/aesgcm_download.h b/src/tools/aesgcm_download.h index ebc8a5f9..e172b89a 100644 --- a/src/tools/aesgcm_download.h +++ b/src/tools/aesgcm_download.h @@ -43,6 +43,7 @@ #include <sys/select.h> #include <curl/curl.h> +#include "tools/http_common.h" #include "tools/http_download.h" #include "ui/win_types.h" @@ -61,6 +62,4 @@ void* aesgcm_file_get(void* userdata); void aesgcm_download_cancel_processes(ProfWin* window); void aesgcm_download_add_download(AESGCMDownload* download); -char* http_basename_from_url(const char* url); - #endif diff --git a/src/tools/http_common.c b/src/tools/http_common.c new file mode 100644 index 00000000..df6f9a64 --- /dev/null +++ b/src/tools/http_common.c @@ -0,0 +1,98 @@ +/* + * http_common.c + * vim: expandtab:ts=4:sts=4:sw=4 + * + * Copyright (C) 2020 William Wennerström <william@wstrm.dev> + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see <https://www.gnu.org/licenses/>. + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +#define _GNU_SOURCE 1 + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <gio/gio.h> + +#include "tools/http_common.h" + +#define FALLBACK_MSG "" + +char* +http_basename_from_url(const char* url) +{ + const char* default_name = "index.html"; + + GFile* file = g_file_new_for_uri(url); + char* filename = g_file_get_basename(file); + g_object_unref(file); + + if (g_strcmp0(filename, ".") == 0 + || g_strcmp0(filename, "..") == 0 + || g_strcmp0(filename, G_DIR_SEPARATOR_S) == 0) { + g_free(filename); + return strdup(default_name); + } + + return filename; +} + +void +http_print_transfer_update(ProfWin* window, char* url, + const char* fmt, ...) +{ + va_list args; + + va_start(args, fmt); + char* msg; + if (vasprintf(&msg, fmt, args) == -1) { + msg = strdup(FALLBACK_MSG); + } + va_end(args); + + win_update_entry_message(window, url, msg); + free(msg); +} + +void +http_print_transfer(ProfWin* window, char* url, + const char* fmt, ...) +{ + va_list args; + + va_start(args, fmt); + char* msg; + if (vasprintf(&msg, fmt, args) == -1) { + msg = strdup(FALLBACK_MSG); + } + va_end(args); + + win_print_http_transfer(window, msg, url); + free(msg); +} diff --git a/src/tools/http_common.h b/src/tools/http_common.h new file mode 100644 index 00000000..41f16200 --- /dev/null +++ b/src/tools/http_common.h @@ -0,0 +1,48 @@ +/* + * http_common.h + * vim: expandtab:ts=4:sts=4:sw=4 + * + * Copyright (C) 2020 William Wennerström <william@wstrm.dev> + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see <https://www.gnu.org/licenses/>. + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +#ifndef TOOLS_HTTP_COMMON_H +#define TOOLS_HTTP_COMMON_H + +#define _GNU_SOURCE 1 + +#include "ui/window.h" + +char* http_basename_from_url(const char* url); +void http_print_transfer(ProfWin* window, char* url, const char* fmt, ...); +void http_print_transfer_update(ProfWin* window, char* url, + const char* fmt, ...); + +#endif diff --git a/src/tools/http_download.c b/src/tools/http_download.c index 5859dc70..a470feec 100644 --- a/src/tools/http_download.c +++ b/src/tools/http_download.c @@ -56,8 +56,6 @@ #include "ui/window.h" #include "common.h" -#define FALLBACK_MSG "" - GSList* download_processes = NULL; static int @@ -84,12 +82,8 @@ _xferinfo(void* userdata, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultot dlperc = (100 * dlnow) / dltotal; } - char* msg; - if (asprintf(&msg, "Downloading '%s': %d%%", download->url, dlperc) == -1) { - msg = strdup(FALLBACK_MSG); - } - win_update_entry_message(download->window, download->url, msg); - free(msg); + http_print_transfer_update(download->window, download->url, + "Downloading '%s': %d%%", download->url, dlperc); pthread_mutex_unlock(&lock); @@ -121,8 +115,8 @@ http_file_get(void* userdata) download->bytes_received = 0; pthread_mutex_lock(&lock); - http_print_transfer_update(download->window, download->url, - "Downloading '%s': 0%%", download->url); + http_print_transfer(download->window, download->url, + "Downloading '%s': 0%%", download->url); FILE* outfh = fopen(download->filename, "wb"); if (outfh == NULL) { @@ -188,7 +182,7 @@ http_file_get(void* userdata) } else { if (!download->cancel) { http_print_transfer_update(download->window, download->url, - "Downloading '%s': 100%%", + "Downloading '%s': done", download->url); win_mark_received(download->window, download->url); } @@ -223,39 +217,3 @@ http_download_add_download(HTTPDownload* download) { download_processes = g_slist_append(download_processes, download); } - -char* -http_basename_from_url(const char* url) -{ - const char* default_name = "index.html"; - - GFile* file = g_file_new_for_uri(url); - char* filename = g_file_get_basename(file); - g_object_unref(file); - - if (g_strcmp0(filename, ".") == 0 - || g_strcmp0(filename, "..") == 0 - || g_strcmp0(filename, G_DIR_SEPARATOR_S) == 0) { - g_free(filename); - return strdup(default_name); - } - - return filename; -} - -void -http_print_transfer_update(ProfWin* window, char* url, - const char* fmt, ...) -{ - va_list args; - - va_start(args, fmt); - char* msg; - if (vasprintf(&msg, fmt, args) == -1) { - msg = strdup(FALLBACK_MSG); - } - va_end(args); - - win_print_http_transfer(window, msg, url); - free(msg); -} diff --git a/src/tools/http_download.h b/src/tools/http_download.h index 077e3e3d..b6ce42ca 100644 --- a/src/tools/http_download.h +++ b/src/tools/http_download.h @@ -45,6 +45,7 @@ #include <curl/curl.h> #include "ui/win_types.h" +#include "tools/http_common.h" typedef struct http_download_t { @@ -61,8 +62,4 @@ void* http_file_get(void* userdata); void http_download_cancel_processes(ProfWin* window); void http_download_add_download(HTTPDownload* download); -char* http_basename_from_url(const char* url); -void http_print_transfer_update(ProfWin* window, char* url, - const char* fmt, ...); - #endif |