logo

badwolf

minimalist and privacy-oriented web browser based on WebKitGTK git clone https://hacktivis.me/git/badwolf.git

downloads.c (8407B)


  1. // BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
  2. // SPDX-FileCopyrightText: 2019-2022 Badwolf Authors <https://hacktivis.me/projects/badwolf>
  3. // SPDX-License-Identifier: BSD-3-Clause
  4. #include "downloads.h"
  5. #include "badwolf.h"
  6. #include "config.h"
  7. #include <assert.h>
  8. #include <glib/gi18n.h> /* _() and other internationalization/localization helpers */
  9. static void
  10. download_stop_iconCb_clicked(GtkButton *UNUSED(stop_icon), gpointer user_data)
  11. {
  12. WebKitDownload *webkit_download = (WebKitDownload *)user_data;
  13. webkit_download_cancel(webkit_download);
  14. }
  15. void
  16. download_format_elapsed(char *restrict formatted,
  17. size_t formatted_size,
  18. char *restrict format,
  19. int total)
  20. {
  21. /* flawfinder: ignore, format string only passed by ourselves */
  22. snprintf(formatted,
  23. formatted_size,
  24. format,
  25. total / 3600, /* hours */
  26. (total % 3600) / 60, /* minutes */
  27. total % 60); /* seconds */
  28. }
  29. void
  30. download_new_entry(WebKitDownload *webkit_download, struct Download *download)
  31. {
  32. download->error = 0;
  33. download->container = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, BADWOLF_DOWNLOAD_PADDING);
  34. download->progress = gtk_progress_bar_new();
  35. download->file_path = gtk_label_new(NULL);
  36. download->status = gtk_label_new(_("Download starting…"));
  37. download->icon =
  38. gtk_image_new_from_icon_name("network-idle-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
  39. download->stop_icon = gtk_button_new_from_icon_name("process-stop", GTK_ICON_SIZE_SMALL_TOOLBAR);
  40. gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(download->progress), TRUE);
  41. gtk_label_set_ellipsize(GTK_LABEL(download->file_path), BADWOLF_DOWNLOAD_FILE_PATH_ELLIPSIZE);
  42. g_signal_connect(
  43. download->stop_icon, "clicked", G_CALLBACK(download_stop_iconCb_clicked), webkit_download);
  44. gtk_box_pack_start(GTK_BOX(download->container), download->icon, FALSE, FALSE, 0);
  45. gtk_box_pack_start(GTK_BOX(download->container), download->progress, FALSE, FALSE, 0);
  46. gtk_box_pack_start(GTK_BOX(download->container), download->stop_icon, FALSE, FALSE, 0);
  47. gtk_box_pack_start(GTK_BOX(download->container), download->status, FALSE, FALSE, 0);
  48. gtk_box_pack_start(GTK_BOX(download->container), download->file_path, FALSE, FALSE, 0);
  49. gtk_list_box_insert(GTK_LIST_BOX(window->downloads_tab), download->container, -1);
  50. gtk_widget_show_all(download->container);
  51. }
  52. void
  53. downloadCb_created_destination(WebKitDownload *UNUSED(webkit_download),
  54. gchar *destination,
  55. gpointer user_data)
  56. {
  57. char *markup;
  58. struct Download *download = (struct Download *)user_data;
  59. markup = g_markup_printf_escaped(
  60. "<a href=\"%s\">%s</a>", destination, webkit_uri_for_display(destination));
  61. gtk_label_set_markup(GTK_LABEL(download->file_path), markup);
  62. g_free(markup);
  63. }
  64. gboolean
  65. downloadCb_decide_destination(WebKitDownload *webkit_download,
  66. gchar *suggested_filename,
  67. gpointer UNUSED(user_data))
  68. {
  69. gint chooser_response;
  70. GtkWindow *parent_window = GTK_WINDOW(window->main_window);
  71. GtkFileChooserNative *file_dialog =
  72. gtk_file_chooser_native_new(NULL, parent_window, GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL);
  73. GtkFileChooser *file_chooser = GTK_FILE_CHOOSER(file_dialog);
  74. gtk_file_chooser_set_current_name(file_chooser, suggested_filename);
  75. gtk_file_chooser_set_do_overwrite_confirmation(file_chooser, TRUE);
  76. webkit_download_set_allow_overwrite(webkit_download, TRUE);
  77. chooser_response = gtk_native_dialog_run(GTK_NATIVE_DIALOG(file_dialog));
  78. if(chooser_response == GTK_RESPONSE_ACCEPT)
  79. webkit_download_set_destination(webkit_download, gtk_file_chooser_get_uri(file_chooser));
  80. else
  81. webkit_download_cancel(webkit_download);
  82. g_object_unref(file_dialog);
  83. return FALSE; /* Let it propagate */
  84. }
  85. void
  86. downloadCb_failed(WebKitDownload *webkit_download, GError *error, gpointer user_data)
  87. {
  88. struct Download *download = (struct Download *)user_data;
  89. /* flawfinder: ignore. proper buffer limits are used */
  90. char formatted[BUFSIZ];
  91. int total = (int)webkit_download_get_elapsed_time(webkit_download);
  92. char *format;
  93. download->error = error;
  94. if(g_error_matches(error, WEBKIT_DOWNLOAD_ERROR, WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER))
  95. format = _("%02i:%02i:%02i Download cancelled");
  96. else
  97. format = _("%02i:%02i:%02i Download error");
  98. download_format_elapsed(formatted, sizeof(formatted), format, total);
  99. gtk_label_set_text(GTK_LABEL(download->status), formatted);
  100. gtk_image_set_from_icon_name(
  101. GTK_IMAGE(download->icon), "network-error-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
  102. }
  103. void
  104. downloadCb_finished(WebKitDownload *webkit_download, gpointer user_data)
  105. {
  106. struct Download *download = (struct Download *)user_data;
  107. /* flawfinder: ignore. proper buffer limits are used */
  108. char formatted[BUFSIZ];
  109. int total = (int)webkit_download_get_elapsed_time(webkit_download);
  110. gchar *format_size = g_format_size(webkit_download_get_received_data_length(webkit_download));
  111. download_format_elapsed(
  112. formatted, sizeof(formatted), _("%02i:%02i:%02i Download finished"), total);
  113. gtk_widget_destroy(download->stop_icon);
  114. if(download->error == 0)
  115. {
  116. gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(download->progress), 1);
  117. gtk_progress_bar_set_text(GTK_PROGRESS_BAR(download->progress), format_size);
  118. gtk_label_set_text(GTK_LABEL(download->status), formatted);
  119. gtk_image_set_from_icon_name(
  120. GTK_IMAGE(download->icon), "network-idle-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
  121. }
  122. // TODO: Send notification
  123. }
  124. void
  125. downloadCb_received_data(WebKitDownload *webkit_download,
  126. guint64 UNUSED(data_lenght),
  127. gpointer user_data)
  128. {
  129. struct Download *download = (struct Download *)user_data;
  130. /* flawfinder: ignore. proper buffer limits are used */
  131. char formatted[BUFSIZ];
  132. int total = (int)webkit_download_get_elapsed_time(webkit_download);
  133. gchar *format_size = g_format_size(webkit_download_get_received_data_length(webkit_download));
  134. download_format_elapsed(formatted, sizeof(formatted), _("%02i:%02i:%02i Downloading…"), total);
  135. gtk_image_set_from_icon_name(
  136. GTK_IMAGE(download->icon), "network-receive-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
  137. gtk_label_set_text(GTK_LABEL(download->status), formatted);
  138. gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(download->progress),
  139. webkit_download_get_estimated_progress(webkit_download));
  140. gtk_progress_bar_set_text(GTK_PROGRESS_BAR(download->progress), format_size);
  141. g_free(format_size);
  142. }
  143. GtkWidget *
  144. badwolf_downloads_tab_new()
  145. {
  146. return gtk_list_box_new();
  147. }
  148. void
  149. badwolf_downloads_tab_attach()
  150. {
  151. assert(window != NULL);
  152. GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
  153. gtk_widget_set_name(scrolled_window, "browser__scrollwin_downloads");
  154. gtk_container_add(GTK_CONTAINER(scrolled_window), window->downloads_tab);
  155. gtk_notebook_insert_page(GTK_NOTEBOOK(window->notebook), scrolled_window, NULL, 0);
  156. gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(window->notebook), scrolled_window, TRUE);
  157. GtkWidget *tab_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
  158. gtk_widget_set_name(tab_box, "browser__tabbox");
  159. GtkWidget *icon = gtk_image_new_from_icon_name("emblem-downloads", GTK_ICON_SIZE_SMALL_TOOLBAR);
  160. gtk_widget_set_name(icon, "browser__tabbox__icon");
  161. GtkWidget *label = gtk_label_new(_("Badwolf Downloads"));
  162. gtk_widget_set_name(label, "browser__tabbox__label");
  163. #ifdef BADWOLF_TAB_BOX_WIDTH
  164. gtk_widget_set_size_request(label, BADWOLF_TAB_BOX_WIDTH, -1);
  165. #endif
  166. #ifdef BADWOLF_TAB_LABEL_CHARWIDTH
  167. gtk_label_set_width_chars(GTK_LABEL(label), BADWOLF_TAB_LABEL_CHARWIDTH);
  168. #endif
  169. gtk_widget_set_hexpand(tab_box, BADWOLF_TAB_HEXPAND);
  170. gtk_label_set_ellipsize(GTK_LABEL(label), BADWOLF_TAB_LABEL_ELLIPSIZE);
  171. gtk_label_set_single_line_mode(GTK_LABEL(label), TRUE);
  172. gtk_box_pack_start(GTK_BOX(tab_box), icon, FALSE, FALSE, 0);
  173. gtk_box_pack_start(GTK_BOX(tab_box), label, TRUE, TRUE, 0);
  174. gtk_widget_set_tooltip_text(tab_box, _("Badwolf Downloads"));
  175. gtk_notebook_set_tab_label(GTK_NOTEBOOK(window->notebook), scrolled_window, tab_box);
  176. gtk_notebook_set_menu_label_text(
  177. GTK_NOTEBOOK(window->notebook), scrolled_window, _("Badwolf Downloads"));
  178. gtk_widget_show_all(tab_box);
  179. }