| 1 | /* |
| 2 | * Copyright (C) 2011 Igalia S.L. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "cmakeconfig.h" |
| 27 | #include "BrowserDownloadsBar.h" |
| 28 | |
| 29 | #include <glib/gi18n.h> |
| 30 | |
| 31 | #define BROWSER_TYPE_DOWNLOAD (browser_download_get_type()) |
| 32 | #define BROWSER_DOWNLOAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), BROWSER_TYPE_DOWNLOAD, BrowserDownload)) |
| 33 | |
| 34 | typedef struct _BrowserDownload BrowserDownload; |
| 35 | typedef struct _BrowserDownloadClass BrowserDownloadClass; |
| 36 | |
| 37 | static GType browser_download_get_type(); |
| 38 | |
| 39 | struct _BrowserDownloadsBar { |
| 40 | GtkInfoBar parent; |
| 41 | }; |
| 42 | |
| 43 | struct _BrowserDownloadsBarClass { |
| 44 | GtkInfoBarClass parentClass; |
| 45 | }; |
| 46 | |
| 47 | G_DEFINE_TYPE(BrowserDownloadsBar, browser_downloads_bar, GTK_TYPE_INFO_BAR) |
| 48 | |
| 49 | static void |
| 50 | browserDownloadsBarChildRemoved(GtkContainer *infoBar, GtkWidget *widget, BrowserDownloadsBar *downloadsBar) |
| 51 | { |
| 52 | GList *children = gtk_container_get_children(infoBar); |
| 53 | if (g_list_length(children) == 1) |
| 54 | gtk_info_bar_response(GTK_INFO_BAR(downloadsBar), GTK_RESPONSE_CLOSE); |
| 55 | g_list_free(children); |
| 56 | } |
| 57 | |
| 58 | static void browserDownloadsBarResponse(GtkInfoBar *infoBar, gint responseId) |
| 59 | { |
| 60 | gtk_widget_destroy(GTK_WIDGET(infoBar)); |
| 61 | } |
| 62 | |
| 63 | static void browser_downloads_bar_init(BrowserDownloadsBar *downloadsBar) |
| 64 | { |
| 65 | GtkWidget *contentBox = gtk_info_bar_get_content_area(GTK_INFO_BAR(downloadsBar)); |
| 66 | g_signal_connect_after(contentBox, "remove" , G_CALLBACK(browserDownloadsBarChildRemoved), downloadsBar); |
| 67 | gtk_orientable_set_orientation(GTK_ORIENTABLE(contentBox), GTK_ORIENTATION_VERTICAL); |
| 68 | |
| 69 | GtkWidget *title = gtk_label_new(NULL); |
| 70 | gtk_label_set_markup(GTK_LABEL(title), "<span size='xx-large' weight='bold'>Downloads</span>" ); |
| 71 | gtk_misc_set_alignment(GTK_MISC(title), 0., 0.5); |
| 72 | gtk_box_pack_start(GTK_BOX(contentBox), title, FALSE, FALSE, 12); |
| 73 | gtk_widget_show(title); |
| 74 | } |
| 75 | |
| 76 | static void browser_downloads_bar_class_init(BrowserDownloadsBarClass *klass) |
| 77 | { |
| 78 | GtkInfoBarClass *infoBarClass = GTK_INFO_BAR_CLASS(klass); |
| 79 | infoBarClass->response = browserDownloadsBarResponse; |
| 80 | } |
| 81 | |
| 82 | GtkWidget *browser_downloads_bar_new() |
| 83 | { |
| 84 | GtkInfoBar *downloadsBar = GTK_INFO_BAR(g_object_new(BROWSER_TYPE_DOWNLOADS_BAR, NULL)); |
| 85 | gtk_info_bar_add_buttons(downloadsBar, GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL); |
| 86 | return GTK_WIDGET(downloadsBar); |
| 87 | } |
| 88 | |
| 89 | struct _BrowserDownload { |
| 90 | GtkBox parent; |
| 91 | |
| 92 | WebKitDownload *download; |
| 93 | guint64 contentLength; |
| 94 | guint64 downloadedSize; |
| 95 | gboolean finished; |
| 96 | |
| 97 | GtkWidget *statusLabel; |
| 98 | GtkWidget *remainingLabel; |
| 99 | GtkWidget *progressBar; |
| 100 | GtkWidget *actionButton; |
| 101 | }; |
| 102 | |
| 103 | struct _BrowserDownloadClass { |
| 104 | GtkBoxClass parentClass; |
| 105 | }; |
| 106 | |
| 107 | G_DEFINE_TYPE(BrowserDownload, browser_download, GTK_TYPE_BOX) |
| 108 | |
| 109 | static void actionButtonClicked(GtkButton *button, BrowserDownload *browserDownload) |
| 110 | { |
| 111 | if (!browserDownload->finished) { |
| 112 | webkit_download_cancel(browserDownload->download); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | gtk_show_uri(gtk_widget_get_screen(GTK_WIDGET(browserDownload)), |
| 117 | webkit_download_get_destination(browserDownload->download), |
| 118 | gtk_get_current_event_time(), NULL); |
| 119 | gtk_widget_destroy(GTK_WIDGET(browserDownload)); |
| 120 | } |
| 121 | |
| 122 | static void browserDownloadFinalize(GObject *object) |
| 123 | { |
| 124 | BrowserDownload *browserDownload = BROWSER_DOWNLOAD(object); |
| 125 | |
| 126 | if (browserDownload->download) { |
| 127 | g_object_unref(browserDownload->download); |
| 128 | browserDownload->download = NULL; |
| 129 | } |
| 130 | |
| 131 | G_OBJECT_CLASS(browser_download_parent_class)->finalize(object); |
| 132 | } |
| 133 | |
| 134 | static void browser_download_init(BrowserDownload *download) |
| 135 | { |
| 136 | GtkWidget *mainBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6); |
| 137 | gtk_box_pack_start(GTK_BOX(download), mainBox, FALSE, FALSE, 0); |
| 138 | gtk_widget_show(mainBox); |
| 139 | |
| 140 | GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 141 | gtk_box_pack_start(GTK_BOX(mainBox), vbox, TRUE, TRUE, 0); |
| 142 | gtk_widget_show(vbox); |
| 143 | |
| 144 | GtkWidget *statusBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); |
| 145 | gtk_box_pack_start(GTK_BOX(vbox), statusBox, TRUE, TRUE, 0); |
| 146 | gtk_widget_show(statusBox); |
| 147 | |
| 148 | download->statusLabel = gtk_label_new("Starting Download" ); |
| 149 | gtk_label_set_ellipsize(GTK_LABEL(download->statusLabel), PANGO_ELLIPSIZE_END); |
| 150 | gtk_misc_set_alignment(GTK_MISC(download->statusLabel), 0., 0.5); |
| 151 | gtk_box_pack_start(GTK_BOX(statusBox), download->statusLabel, TRUE, TRUE, 0); |
| 152 | gtk_widget_show(download->statusLabel); |
| 153 | |
| 154 | download->remainingLabel = gtk_label_new(NULL); |
| 155 | gtk_misc_set_alignment(GTK_MISC(download->remainingLabel), 1., 0.5); |
| 156 | gtk_box_pack_end(GTK_BOX(statusBox), download->remainingLabel, TRUE, TRUE, 0); |
| 157 | gtk_widget_show(download->remainingLabel); |
| 158 | |
| 159 | download->progressBar = gtk_progress_bar_new(); |
| 160 | gtk_box_pack_start(GTK_BOX(vbox), download->progressBar, FALSE, FALSE, 0); |
| 161 | gtk_widget_show(download->progressBar); |
| 162 | |
| 163 | download->actionButton = gtk_button_new_from_stock(GTK_STOCK_CANCEL); |
| 164 | g_signal_connect(download->actionButton, "clicked" , G_CALLBACK(actionButtonClicked), download); |
| 165 | gtk_box_pack_end(GTK_BOX(mainBox), download->actionButton, FALSE, FALSE, 0); |
| 166 | gtk_widget_show(download->actionButton); |
| 167 | } |
| 168 | |
| 169 | static void browser_download_class_init(BrowserDownloadClass *klass) |
| 170 | { |
| 171 | GObjectClass *objectClass = G_OBJECT_CLASS(klass); |
| 172 | |
| 173 | objectClass->finalize = browserDownloadFinalize; |
| 174 | } |
| 175 | |
| 176 | static void downloadReceivedResponse(WebKitDownload *download, GParamSpec *paramSpec, BrowserDownload *browserDownload) |
| 177 | { |
| 178 | WebKitURIResponse *response = webkit_download_get_response(download); |
| 179 | browserDownload->contentLength = webkit_uri_response_get_content_length(response); |
| 180 | char *text = g_strdup_printf("Downloading %s" , webkit_uri_response_get_uri(response)); |
| 181 | gtk_label_set_text(GTK_LABEL(browserDownload->statusLabel), text); |
| 182 | g_free(text); |
| 183 | } |
| 184 | |
| 185 | static gchar *remainingTime(BrowserDownload *browserDownload) |
| 186 | { |
| 187 | guint64 total = browserDownload->contentLength; |
| 188 | guint64 current = browserDownload->downloadedSize; |
| 189 | gdouble elapsedTime = webkit_download_get_elapsed_time(browserDownload->download); |
| 190 | |
| 191 | if (current <= 0) |
| 192 | return NULL; |
| 193 | |
| 194 | gdouble perByteTime = elapsedTime / current; |
| 195 | gdouble interval = perByteTime * (total - current); |
| 196 | |
| 197 | int hours = (int) (interval / 3600); |
| 198 | interval -= hours * 3600; |
| 199 | int mins = (int) (interval / 60); |
| 200 | interval -= mins * 60; |
| 201 | int secs = (int) interval; |
| 202 | |
| 203 | if (hours > 0) { |
| 204 | if (mins > 0) |
| 205 | return g_strdup_printf (ngettext ("%u:%02u hour left" , "%u:%02u hours left" , hours), hours, mins); |
| 206 | return g_strdup_printf (ngettext ("%u hour left" , "%u hours left" , hours), hours); |
| 207 | } |
| 208 | |
| 209 | if (mins > 0) |
| 210 | return g_strdup_printf (ngettext ("%u:%02u minute left" , "%u:%02u minutes left" , mins), mins, secs); |
| 211 | return g_strdup_printf (ngettext ("%u second left" , "%u seconds left" , secs), secs); |
| 212 | } |
| 213 | |
| 214 | static void downloadProgress(WebKitDownload *download, GParamSpec *paramSpec, BrowserDownload *browserDownload) |
| 215 | { |
| 216 | gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(browserDownload->progressBar), |
| 217 | webkit_download_get_estimated_progress(download)); |
| 218 | char *remaining = remainingTime(browserDownload); |
| 219 | gtk_label_set_text(GTK_LABEL(browserDownload->remainingLabel), remaining); |
| 220 | g_free(remaining); |
| 221 | } |
| 222 | |
| 223 | static void downloadReceivedData(WebKitDownload *download, guint64 dataLength, BrowserDownload *browserDownload) |
| 224 | { |
| 225 | browserDownload->downloadedSize += dataLength; |
| 226 | } |
| 227 | |
| 228 | static void downloadFinished(WebKitDownload *download, BrowserDownload *browserDownload) |
| 229 | { |
| 230 | gchar *text = g_strdup_printf("Download completed: %s" , webkit_download_get_destination(download)); |
| 231 | gtk_label_set_text(GTK_LABEL(browserDownload->statusLabel), text); |
| 232 | g_free(text); |
| 233 | gtk_label_set_text(GTK_LABEL(browserDownload->remainingLabel), NULL); |
| 234 | gtk_button_set_image(GTK_BUTTON(browserDownload->actionButton), |
| 235 | gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON)); |
| 236 | gtk_button_set_label(GTK_BUTTON(browserDownload->actionButton), "Open ..." ); |
| 237 | browserDownload->finished = TRUE; |
| 238 | } |
| 239 | |
| 240 | static void downloadFailed(WebKitDownload *download, GError *error, BrowserDownload *browserDownload) |
| 241 | { |
| 242 | g_signal_handlers_disconnect_by_func(browserDownload->download, downloadFinished, browserDownload); |
| 243 | if (g_error_matches(error, WEBKIT_DOWNLOAD_ERROR, WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER)) { |
| 244 | gtk_widget_destroy(GTK_WIDGET(browserDownload)); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | char *errorMessage = g_strdup_printf("Download failed: %s" , error->message); |
| 249 | gtk_label_set_text(GTK_LABEL(browserDownload->statusLabel), errorMessage); |
| 250 | g_free(errorMessage); |
| 251 | gtk_label_set_text(GTK_LABEL(browserDownload->remainingLabel), NULL); |
| 252 | gtk_widget_set_sensitive(browserDownload->actionButton, FALSE); |
| 253 | } |
| 254 | |
| 255 | GtkWidget *browserDownloadNew(WebKitDownload *download) |
| 256 | { |
| 257 | BrowserDownload *browserDownload = BROWSER_DOWNLOAD(g_object_new(BROWSER_TYPE_DOWNLOAD, |
| 258 | "orientation" , GTK_ORIENTATION_VERTICAL, |
| 259 | NULL)); |
| 260 | |
| 261 | browserDownload->download = g_object_ref(download); |
| 262 | g_signal_connect(browserDownload->download, "notify::response" , G_CALLBACK(downloadReceivedResponse), browserDownload); |
| 263 | g_signal_connect(browserDownload->download, "notify::estimated-progress" , G_CALLBACK(downloadProgress), browserDownload); |
| 264 | g_signal_connect(browserDownload->download, "received-data" , G_CALLBACK(downloadReceivedData), browserDownload); |
| 265 | g_signal_connect(browserDownload->download, "finished" , G_CALLBACK(downloadFinished), browserDownload); |
| 266 | g_signal_connect(browserDownload->download, "failed" , G_CALLBACK(downloadFailed), browserDownload); |
| 267 | |
| 268 | return GTK_WIDGET(browserDownload); |
| 269 | } |
| 270 | |
| 271 | void browser_downloads_bar_add_download(BrowserDownloadsBar *downloadsBar, WebKitDownload *download) |
| 272 | { |
| 273 | GtkWidget *browserDownload = browserDownloadNew(download); |
| 274 | GtkWidget *contentBox = gtk_info_bar_get_content_area(GTK_INFO_BAR(downloadsBar)); |
| 275 | gtk_box_pack_start(GTK_BOX(contentBox), browserDownload, FALSE, TRUE, 0); |
| 276 | gtk_widget_show(browserDownload); |
| 277 | } |
| 278 | |