1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// from webkit-gir-files
// DO NOT EDIT

use crate::{ffi, URIRequest, URIResponse, WebView};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// Object used to communicate with the application when downloading.
    ///
    /// #WebKitDownload carries information about a download request and
    /// response, including a #WebKitURIRequest and a #WebKitURIResponse
    /// objects. The application may use this object to control the
    /// download process, or to simply figure out what is to be downloaded,
    /// and handle the download process itself.
    ///
    /// ## Properties
    ///
    ///
    /// #### `allow-overwrite`
    ///  Whether or not the download is allowed to overwrite an existing file on
    /// disk. If this property is [`false`] and the destination already exists,
    /// the download will fail.
    ///
    /// Readable | Writeable
    ///
    ///
    /// #### `destination`
    ///  The local path to where the download will be saved.
    ///
    /// Readable
    ///
    ///
    /// #### `estimated-progress`
    ///  An estimate of the percent completion for the download operation.
    /// This value will range from 0.0 to 1.0. The value is an estimate
    /// based on the total number of bytes expected to be received for
    /// a download.
    /// If you need a more accurate progress information you can connect to
    /// #WebKitDownload::received-data signal to track the progress.
    ///
    /// Readable
    ///
    ///
    /// #### `response`
    ///  The #WebKitURIResponse associated with this download.
    ///
    /// Readable
    ///
    /// ## Signals
    ///
    ///
    /// #### `created-destination`
    ///  This signal is emitted after #WebKitDownload::decide-destination and before
    /// #WebKitDownload::received-data to notify that destination file has been
    /// created successfully at @destination.
    ///
    ///
    ///
    ///
    /// #### `decide-destination`
    ///  This signal is emitted after response is received to
    /// decide a destination for the download using
    /// webkit_download_set_destination(). If this signal is not
    /// handled, the file will be downloaded to `G_USER_DIRECTORY_DOWNLOAD`
    /// directory using @suggested_filename.
    ///
    /// Since 2.40, you may handle this signal asynchronously by
    /// returning [`true`] without calling webkit_download_set_destination().
    /// This indicates intent to eventually call webkit_download_set_destination().
    /// In this case, the download will not proceed until the destination is set
    /// or cancelled with webkit_download_cancel().
    ///
    ///
    ///
    ///
    /// #### `failed`
    ///  This signal is emitted when an error occurs during the download
    /// operation. The given @error, of the domain `WEBKIT_DOWNLOAD_ERROR`,
    /// contains further details of the failure. If the download is cancelled
    /// with webkit_download_cancel(), this signal is emitted with error
    /// [`DownloadError::CancelledByUser`][crate::DownloadError::CancelledByUser]. The download operation finishes
    /// after an error and #WebKitDownload::finished signal is emitted after this one.
    ///
    ///
    ///
    ///
    /// #### `finished`
    ///  This signal is emitted when download finishes successfully or due to an error.
    /// In case of errors #WebKitDownload::failed signal is emitted before this one.
    ///
    ///
    ///
    ///
    /// #### `received-data`
    ///  This signal is emitted after response is received,
    /// every time new data has been written to the destination. It's
    /// useful to know the progress of the download operation.
    ///
    ///
    #[doc(alias = "WebKitDownload")]
    pub struct Download(Object<ffi::WebKitDownload, ffi::WebKitDownloadClass>);

    match fn {
        type_ => || ffi::webkit_download_get_type(),
    }
}

impl Download {
    /// Cancels the download.
    ///
    /// When the ongoing download
    /// operation is effectively cancelled the signal
    /// #WebKitDownload::failed is emitted with
    /// [`DownloadError::CancelledByUser`][crate::DownloadError::CancelledByUser] error.
    #[doc(alias = "webkit_download_cancel")]
    pub fn cancel(&self) {
        unsafe {
            ffi::webkit_download_cancel(self.to_glib_none().0);
        }
    }

    /// Returns the current value of the #WebKitDownload:allow-overwrite property.
    ///
    /// Returns the current value of the #WebKitDownload:allow-overwrite property,
    /// which determines whether the download will overwrite an existing file on
    /// disk, or if it will fail if the destination already exists.
    ///
    /// # Returns
    ///
    /// the current value of the #WebKitDownload:allow-overwrite property
    #[doc(alias = "webkit_download_get_allow_overwrite")]
    #[doc(alias = "get_allow_overwrite")]
    #[doc(alias = "allow-overwrite")]
    pub fn allows_overwrite(&self) -> bool {
        unsafe {
            from_glib(ffi::webkit_download_get_allow_overwrite(
                self.to_glib_none().0,
            ))
        }
    }

    /// Obtains the destination to which the downloaded file will be written.
    ///
    /// You can connect to #WebKitDownload::created-destination to make
    /// sure this method returns a valid destination.
    ///
    /// # Returns
    ///
    /// the destination or [`None`]
    #[doc(alias = "webkit_download_get_destination")]
    #[doc(alias = "get_destination")]
    pub fn destination(&self) -> Option<glib::GString> {
        unsafe { from_glib_none(ffi::webkit_download_get_destination(self.to_glib_none().0)) }
    }

    /// Gets the elapsed time in seconds, including any fractional part.
    ///
    /// If the download finished, had an error or was cancelled this is
    /// the time between its start and the event.
    ///
    /// # Returns
    ///
    /// seconds since the download was started
    #[doc(alias = "webkit_download_get_elapsed_time")]
    #[doc(alias = "get_elapsed_time")]
    pub fn elapsed_time(&self) -> f64 {
        unsafe { ffi::webkit_download_get_elapsed_time(self.to_glib_none().0) }
    }

    /// Gets the value of the #WebKitDownload:estimated-progress property.
    /// Gets the value of the #WebKitDownload:estimated-progress property.
    /// You can monitor the estimated progress of the download operation by
    /// connecting to the notify::estimated-progress signal of @self.
    ///
    /// # Returns
    ///
    /// an estimate of the of the percent complete for a download
    ///     as a range from 0.0 to 1.0.
    #[doc(alias = "webkit_download_get_estimated_progress")]
    #[doc(alias = "get_estimated_progress")]
    #[doc(alias = "estimated-progress")]
    pub fn estimated_progress(&self) -> f64 {
        unsafe { ffi::webkit_download_get_estimated_progress(self.to_glib_none().0) }
    }

    /// Gets the length of the data already downloaded for @self.
    ///
    /// Gets the length of the data already downloaded for @self
    /// in bytes.
    ///
    /// # Returns
    ///
    /// the amount of bytes already downloaded.
    #[doc(alias = "webkit_download_get_received_data_length")]
    #[doc(alias = "get_received_data_length")]
    pub fn received_data_length(&self) -> u64 {
        unsafe { ffi::webkit_download_get_received_data_length(self.to_glib_none().0) }
    }

    /// Retrieves the #WebKitURIRequest object that backs the download
    /// process.
    ///
    /// # Returns
    ///
    /// the #WebKitURIRequest of @self
    #[doc(alias = "webkit_download_get_request")]
    #[doc(alias = "get_request")]
    pub fn request(&self) -> Option<URIRequest> {
        unsafe { from_glib_none(ffi::webkit_download_get_request(self.to_glib_none().0)) }
    }

    /// Retrieves the #WebKitURIResponse object that backs the download process.
    ///
    /// Retrieves the #WebKitURIResponse object that backs the download
    /// process. This method returns [`None`] if called before the response
    /// is received from the server. You can connect to notify::response
    /// signal to be notified when the response is received.
    ///
    /// # Returns
    ///
    /// the #WebKitURIResponse, or [`None`] if
    ///     the response hasn't been received yet.
    #[doc(alias = "webkit_download_get_response")]
    #[doc(alias = "get_response")]
    pub fn response(&self) -> Option<URIResponse> {
        unsafe { from_glib_none(ffi::webkit_download_get_response(self.to_glib_none().0)) }
    }

    /// Get the #WebKitWebView that initiated the download.
    ///
    /// # Returns
    ///
    /// the #WebKitWebView that initiated @self,
    ///    or [`None`] if @self was not initiated by a #WebKitWebView.
    #[doc(alias = "webkit_download_get_web_view")]
    #[doc(alias = "get_web_view")]
    pub fn web_view(&self) -> Option<WebView> {
        unsafe { from_glib_none(ffi::webkit_download_get_web_view(self.to_glib_none().0)) }
    }

    /// Sets the #WebKitDownload:allow-overwrite property.
    ///
    /// Sets the #WebKitDownload:allow-overwrite property, which determines whether
    /// the download may overwrite an existing file on disk, or if it will fail if
    /// the destination already exists.
    /// ## `allowed`
    /// the new value for the #WebKitDownload:allow-overwrite property
    #[doc(alias = "webkit_download_set_allow_overwrite")]
    #[doc(alias = "allow-overwrite")]
    pub fn set_allow_overwrite(&self, allowed: bool) {
        unsafe {
            ffi::webkit_download_set_allow_overwrite(self.to_glib_none().0, allowed.into_glib());
        }
    }

    /// Sets the destination to which the downloaded file will be written.
    ///
    /// This method should be called before the download transfer
    /// starts or it will not have any effect on the ongoing download
    /// operation. To set the destination using the filename suggested
    /// by the server connect to #WebKitDownload::decide-destination
    /// signal and call webkit_download_set_destination(). If you want to
    /// set a fixed destination that doesn't depend on the suggested
    /// filename you can connect to notify::response signal and call
    /// webkit_download_set_destination().
    ///
    /// If #WebKitDownload::decide-destination signal is not handled
    /// and destination is not set when the download transfer starts,
    /// the file will be saved with the filename suggested by the server in
    /// `G_USER_DIRECTORY_DOWNLOAD` directory.
    /// ## `destination`
    /// the destination
    #[doc(alias = "webkit_download_set_destination")]
    pub fn set_destination(&self, destination: &str) {
        unsafe {
            ffi::webkit_download_set_destination(
                self.to_glib_none().0,
                destination.to_glib_none().0,
            );
        }
    }

    /// This signal is emitted after #WebKitDownload::decide-destination and before
    /// #WebKitDownload::received-data to notify that destination file has been
    /// created successfully at @destination.
    /// ## `destination`
    /// the destination
    #[doc(alias = "created-destination")]
    pub fn connect_created_destination<F: Fn(&Self, &str) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn created_destination_trampoline<F: Fn(&Download, &str) + 'static>(
            this: *mut ffi::WebKitDownload,
            destination: *mut libc::c_char,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                &from_glib_borrow(this),
                &glib::GString::from_glib_borrow(destination),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"created-destination\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    created_destination_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// This signal is emitted after response is received to
    /// decide a destination for the download using
    /// webkit_download_set_destination(). If this signal is not
    /// handled, the file will be downloaded to `G_USER_DIRECTORY_DOWNLOAD`
    /// directory using @suggested_filename.
    ///
    /// Since 2.40, you may handle this signal asynchronously by
    /// returning [`true`] without calling webkit_download_set_destination().
    /// This indicates intent to eventually call webkit_download_set_destination().
    /// In this case, the download will not proceed until the destination is set
    /// or cancelled with webkit_download_cancel().
    /// ## `suggested_filename`
    /// the filename suggested for the download
    ///
    /// # Returns
    ///
    /// [`true`] to stop other handlers from being invoked for the event,
    ///   or [`false`] to propagate the event further.
    #[doc(alias = "decide-destination")]
    pub fn connect_decide_destination<F: Fn(&Self, &str) -> bool + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn decide_destination_trampoline<
            F: Fn(&Download, &str) -> bool + 'static,
        >(
            this: *mut ffi::WebKitDownload,
            suggested_filename: *mut libc::c_char,
            f: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let f: &F = &*(f as *const F);
            f(
                &from_glib_borrow(this),
                &glib::GString::from_glib_borrow(suggested_filename),
            )
            .into_glib()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"decide-destination\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    decide_destination_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// This signal is emitted when an error occurs during the download
    /// operation. The given @error, of the domain `WEBKIT_DOWNLOAD_ERROR`,
    /// contains further details of the failure. If the download is cancelled
    /// with webkit_download_cancel(), this signal is emitted with error
    /// [`DownloadError::CancelledByUser`][crate::DownloadError::CancelledByUser]. The download operation finishes
    /// after an error and #WebKitDownload::finished signal is emitted after this one.
    /// ## `error`
    /// the #GError that was triggered
    #[doc(alias = "failed")]
    pub fn connect_failed<F: Fn(&Self, &glib::Error) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn failed_trampoline<F: Fn(&Download, &glib::Error) + 'static>(
            this: *mut ffi::WebKitDownload,
            error: *mut glib::ffi::GError,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this), &from_glib_borrow(error))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"failed\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    failed_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// This signal is emitted when download finishes successfully or due to an error.
    /// In case of errors #WebKitDownload::failed signal is emitted before this one.
    #[doc(alias = "finished")]
    pub fn connect_finished<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn finished_trampoline<F: Fn(&Download) + 'static>(
            this: *mut ffi::WebKitDownload,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"finished\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    finished_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// This signal is emitted after response is received,
    /// every time new data has been written to the destination. It's
    /// useful to know the progress of the download operation.
    /// ## `data_length`
    /// the length of data received in bytes
    #[doc(alias = "received-data")]
    pub fn connect_received_data<F: Fn(&Self, u64) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn received_data_trampoline<F: Fn(&Download, u64) + 'static>(
            this: *mut ffi::WebKitDownload,
            data_length: u64,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this), data_length)
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"received-data\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    received_data_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "allow-overwrite")]
    pub fn connect_allow_overwrite_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_allow_overwrite_trampoline<F: Fn(&Download) + 'static>(
            this: *mut ffi::WebKitDownload,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::allow-overwrite\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_allow_overwrite_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "destination")]
    pub fn connect_destination_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_destination_trampoline<F: Fn(&Download) + 'static>(
            this: *mut ffi::WebKitDownload,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::destination\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_destination_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "estimated-progress")]
    pub fn connect_estimated_progress_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_estimated_progress_trampoline<F: Fn(&Download) + 'static>(
            this: *mut ffi::WebKitDownload,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::estimated-progress\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_estimated_progress_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "response")]
    pub fn connect_response_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_response_trampoline<F: Fn(&Download) + 'static>(
            this: *mut ffi::WebKitDownload,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::response\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_response_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}