1 module webkit2.Download; 2 3 private import glib.ErrorG; 4 private import glib.Str; 5 private import gobject.ObjectG; 6 private import gobject.Signals; 7 private import std.algorithm; 8 private import webkit2.URIRequest; 9 private import webkit2.URIResponse; 10 private import webkit2.WebView; 11 private import webkit2.c.functions; 12 public import webkit2.c.types; 13 14 15 /** 16 * #WebKitDownload carries information about a download request and 17 * response, including a #WebKitURIRequest and a #WebKitURIResponse 18 * objects. The application may use this object to control the 19 * download process, or to simply figure out what is to be downloaded, 20 * and handle the download process itself. 21 */ 22 public class Download : ObjectG 23 { 24 /** the main Gtk struct */ 25 protected WebKitDownload* webKitDownload; 26 27 /** Get the main Gtk struct */ 28 public WebKitDownload* getDownloadStruct(bool transferOwnership = false) 29 { 30 if (transferOwnership) 31 ownedRef = false; 32 return webKitDownload; 33 } 34 35 /** the main Gtk struct as a void* */ 36 protected override void* getStruct() 37 { 38 return cast(void*)webKitDownload; 39 } 40 41 /** 42 * Sets our main struct and passes it to the parent class. 43 */ 44 public this (WebKitDownload* webKitDownload, bool ownedRef = false) 45 { 46 this.webKitDownload = webKitDownload; 47 super(cast(GObject*)webKitDownload, ownedRef); 48 } 49 50 51 /** */ 52 public static GType getType() 53 { 54 return webkit_download_get_type(); 55 } 56 57 /** 58 * Cancels the download. When the ongoing download 59 * operation is effectively cancelled the signal 60 * #WebKitDownload::failed is emitted with 61 * %WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER error. 62 */ 63 public void cancel() 64 { 65 webkit_download_cancel(webKitDownload); 66 } 67 68 /** 69 * Returns the current value of the #WebKitDownload:allow-overwrite property, 70 * which determines whether the download will overwrite an existing file on 71 * disk, or if it will fail if the destination already exists. 72 * 73 * Returns: the current value of the #WebKitDownload:allow-overwrite property 74 * 75 * Since: 2.6 76 */ 77 public bool getAllowOverwrite() 78 { 79 return webkit_download_get_allow_overwrite(webKitDownload) != 0; 80 } 81 82 /** 83 * Obtains the URI to which the downloaded file will be written. You 84 * can connect to #WebKitDownload::created-destination to make 85 * sure this method returns a valid destination. 86 * 87 * Returns: the destination URI or %NULL 88 */ 89 public string getDestination() 90 { 91 return Str.toString(webkit_download_get_destination(webKitDownload)); 92 } 93 94 /** 95 * Gets the elapsed time in seconds, including any fractional part. 96 * If the download finished, had an error or was cancelled this is 97 * the time between its start and the event. 98 * 99 * Returns: seconds since the download was started 100 */ 101 public double getElapsedTime() 102 { 103 return webkit_download_get_elapsed_time(webKitDownload); 104 } 105 106 /** 107 * Gets the value of the #WebKitDownload:estimated-progress property. 108 * You can monitor the estimated progress of the download operation by 109 * connecting to the notify::estimated-progress signal of @download. 110 * 111 * Returns: an estimate of the of the percent complete for a download 112 * as a range from 0.0 to 1.0. 113 */ 114 public double getEstimatedProgress() 115 { 116 return webkit_download_get_estimated_progress(webKitDownload); 117 } 118 119 /** 120 * Gets the length of the data already downloaded for @download 121 * in bytes. 122 * 123 * Returns: the amount of bytes already downloaded. 124 */ 125 public ulong getReceivedDataLength() 126 { 127 return webkit_download_get_received_data_length(webKitDownload); 128 } 129 130 /** 131 * Retrieves the #WebKitURIRequest object that backs the download 132 * process. 133 * 134 * Returns: the #WebKitURIRequest of @download 135 */ 136 public URIRequest getRequest() 137 { 138 auto __p = webkit_download_get_request(webKitDownload); 139 140 if(__p is null) 141 { 142 return null; 143 } 144 145 return ObjectG.getDObject!(URIRequest)(cast(WebKitURIRequest*) __p); 146 } 147 148 /** 149 * Retrieves the #WebKitURIResponse object that backs the download 150 * process. This method returns %NULL if called before the response 151 * is received from the server. You can connect to notify::response 152 * signal to be notified when the response is received. 153 * 154 * Returns: the #WebKitURIResponse, or %NULL if 155 * the response hasn't been received yet. 156 */ 157 public URIResponse getResponse() 158 { 159 auto __p = webkit_download_get_response(webKitDownload); 160 161 if(__p is null) 162 { 163 return null; 164 } 165 166 return ObjectG.getDObject!(URIResponse)(cast(WebKitURIResponse*) __p); 167 } 168 169 /** 170 * Get the #WebKitWebView that initiated the download. 171 * 172 * Returns: the #WebKitWebView that initiated @download, 173 * or %NULL if @download was not initiated by a #WebKitWebView. 174 */ 175 public WebView getWebView() 176 { 177 auto __p = webkit_download_get_web_view(webKitDownload); 178 179 if(__p is null) 180 { 181 return null; 182 } 183 184 return ObjectG.getDObject!(WebView)(cast(WebKitWebView*) __p); 185 } 186 187 /** 188 * Sets the #WebKitDownload:allow-overwrite property, which determines whether 189 * the download may overwrite an existing file on disk, or if it will fail if 190 * the destination already exists. 191 * 192 * Params: 193 * allowed = the new value for the #WebKitDownload:allow-overwrite property 194 * 195 * Since: 2.6 196 */ 197 public void setAllowOverwrite(bool allowed) 198 { 199 webkit_download_set_allow_overwrite(webKitDownload, allowed); 200 } 201 202 /** 203 * Sets the URI to which the downloaded file will be written. 204 * This method should be called before the download transfer 205 * starts or it will not have any effect on the ongoing download 206 * operation. To set the destination using the filename suggested 207 * by the server connect to #WebKitDownload::decide-destination 208 * signal and call webkit_download_set_destination(). If you want to 209 * set a fixed destination URI that doesn't depend on the suggested 210 * filename you can connect to notify::response signal and call 211 * webkit_download_set_destination(). 212 * If #WebKitDownload::decide-destination signal is not handled 213 * and destination URI is not set when the download transfer starts, 214 * the file will be saved with the filename suggested by the server in 215 * %G_USER_DIRECTORY_DOWNLOAD directory. 216 * 217 * Params: 218 * uri = the destination URI 219 */ 220 public void setDestination(string uri) 221 { 222 webkit_download_set_destination(webKitDownload, Str.toStringz(uri)); 223 } 224 225 /** 226 * This signal is emitted after #WebKitDownload::decide-destination and before 227 * #WebKitDownload::received-data to notify that destination file has been 228 * created successfully at @destination. 229 * 230 * Params: 231 * destination = the destination URI 232 */ 233 gulong addOnCreatedDestination(void delegate(string, Download) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 234 { 235 return Signals.connect(this, "created-destination", dlg, connectFlags ^ ConnectFlags.SWAPPED); 236 } 237 238 /** 239 * This signal is emitted after response is received to 240 * decide a destination URI for the download. If this signal is not 241 * handled the file will be downloaded to %G_USER_DIRECTORY_DOWNLOAD 242 * directory using @suggested_filename. 243 * 244 * Params: 245 * suggestedFilename = the filename suggested for the download 246 * 247 * Returns: %TRUE to stop other handlers from being invoked for the event. 248 * %FALSE to propagate the event further. 249 */ 250 gulong addOnDecideDestination(bool delegate(string, Download) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 251 { 252 return Signals.connect(this, "decide-destination", dlg, connectFlags ^ ConnectFlags.SWAPPED); 253 } 254 255 /** 256 * This signal is emitted when an error occurs during the download 257 * operation. The given @error, of the domain %WEBKIT_DOWNLOAD_ERROR, 258 * contains further details of the failure. If the download is cancelled 259 * with webkit_download_cancel(), this signal is emitted with error 260 * %WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER. The download operation finishes 261 * after an error and #WebKitDownload::finished signal is emitted after this one. 262 * 263 * Params: 264 * error = the #GError that was triggered 265 */ 266 gulong addOnFailed(void delegate(ErrorG, Download) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 267 { 268 return Signals.connect(this, "failed", dlg, connectFlags ^ ConnectFlags.SWAPPED); 269 } 270 271 /** 272 * This signal is emitted when download finishes successfully or due to an error. 273 * In case of errors #WebKitDownload::failed signal is emitted before this one. 274 */ 275 gulong addOnFinished(void delegate(Download) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 276 { 277 return Signals.connect(this, "finished", dlg, connectFlags ^ ConnectFlags.SWAPPED); 278 } 279 280 /** 281 * This signal is emitted after response is received, 282 * every time new data has been written to the destination. It's 283 * useful to know the progress of the download operation. 284 * 285 * Params: 286 * dataLength = the length of data received in bytes 287 */ 288 gulong addOnReceivedData(void delegate(ulong, Download) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 289 { 290 return Signals.connect(this, "received-data", dlg, connectFlags ^ ConnectFlags.SWAPPED); 291 } 292 }