/
Request.cs
executable file
·848 lines (800 loc) · 38.9 KB
/
Request.cs
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Tizen.Content.Download
{
/// <summary>
/// The Request class provides the functions to create and manage a single download request.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class Request : IDisposable
{
private int _downloadId;
private Notification _notificationProperties;
private IDictionary<string, string> _httpHeaders;
private EventHandler<StateChangedEventArgs> _downloadStateChanged;
private Interop.Download.StateChangedCallback _downloadStateChangedCallback;
private EventHandler<ProgressChangedEventArgs> _downloadProgressChanged;
private Interop.Download.ProgressChangedCallback _downloadProgressChangedCallback;
private bool _disposed = false;
/// <summary>
/// Creates a Request object.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="url">The URL to download.</param>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public Request(string url)
{
if (String.IsNullOrEmpty(url))
{
DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
}
int ret = Interop.Download.CreateRequest(out _downloadId);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Request creation failed");
}
ret = Interop.Download.SetUrl(_downloadId, url);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
}
_notificationProperties = new Notification(_downloadId);
_httpHeaders = new Dictionary<string, string>();
}
/// <summary>
/// Creates a Request object.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="url">The URL to download</param>
/// <param name="destinationPath">The directory path where downloaded file is stored.</param>
/// <param name="fileName">The name of the downloaded file.</param>
/// <param name="type">The network type which the download request must adhere to.</param>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <feature>http://tizen.org/feature/network.wifi.direct</feature>
/// <feature>http://tizen.org/feature/network.telephony</feature>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
/// <exception cref="NotSupportedException">Thrown when a feature is not supported.</exception>
public Request(string url, string destinationPath, string fileName, NetworkType type)
{
if (String.IsNullOrEmpty(url))
{
DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
}
int ret = Interop.Download.CreateRequest(out _downloadId);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Request creation failed");
}
ret = Interop.Download.SetUrl(_downloadId, url);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
}
ret = Interop.Download.SetDestination(_downloadId, destinationPath);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting DestinationPath failed");
}
ret = Interop.Download.SetFileName(_downloadId, fileName);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting FileName failed");
}
ret = Interop.Download.SetNetworkType(_downloadId, (int)type);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting NetworkType failed");
}
_notificationProperties = new Notification(_downloadId);
_httpHeaders = new Dictionary<string, string>();
}
/// <summary>
/// Creates a Request object.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="url">The URL to download.</param>
/// <param name="destinationPath">The directory path where the downloaded file is stored.</param>
/// <param name="fileName">The name of the downloaded file.</param>
/// <param name="type">The network type which the download request must adhere to.</param>
/// <param name="httpHeaders">HTTP header fields for the download request.</param>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <feature>http://tizen.org/feature/network.wifi.direct</feature>
/// <feature>http://tizen.org/feature/network.telephony</feature>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
/// <exception cref="NotSupportedException">Thrown when a feature is not supported.</exception>
public Request(string url, string destinationPath, string fileName, NetworkType type, IDictionary<string, string> httpHeaders)
{
if (String.IsNullOrEmpty(url))
{
DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
}
int ret = Interop.Download.CreateRequest(out _downloadId);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Request creation failed");
}
ret = Interop.Download.SetUrl(_downloadId, url);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
}
ret = Interop.Download.SetDestination(_downloadId, destinationPath);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting DestinationPath failed");
}
ret = Interop.Download.SetFileName(_downloadId, fileName);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting FileName failed");
}
ret = Interop.Download.SetNetworkType(_downloadId, (int)type);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting NetworkType failed");
}
_notificationProperties = new Notification(_downloadId);
_httpHeaders = httpHeaders;
}
/// <summary>
/// Destructor of the Request class.
/// </summary>
~Request()
{
Dispose(false);
}
/// <summary>
/// An event that occurs when the download state changes.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public event EventHandler<StateChangedEventArgs> StateChanged
{
add
{
if (_downloadStateChanged == null)
{
RegisterStateChangedEvent();
}
_downloadStateChanged += value;
}
remove
{
_downloadStateChanged -= value;
if (_downloadStateChanged == null)
{
UnregisterStateChangedEvent();
}
}
}
/// <summary>
/// An event that occurs when the download progress changes.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public event EventHandler<ProgressChangedEventArgs> ProgressChanged
{
add
{
if (_downloadProgressChanged == null)
{
RegisterProgressChangedEvent();
}
_downloadProgressChanged += value;
}
remove
{
_downloadProgressChanged -= value;
if (_downloadProgressChanged == null)
{
UnregisterProgressChangedEvent();
}
}
}
/// <summary>
/// The absolute path where the file will be downloaded.
/// If you try to get this property value before calling Start(), an empty string is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// Returns an empty string if the download is not completed or if a state has not yet changed to completed or if any other error occurs.
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public string DownloadedPath
{
get
{
string path;
int ret = Interop.Download.GetDownloadedPath(_downloadId, out path);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get DownloadedPath, " + (DownloadError)ret);
return String.Empty;
}
return path;
}
}
/// <summary>
/// The MIME type of the downloaded content.
/// If you try to get this property value before calling Start(), an empty string is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public string MimeType
{
get
{
string mime;
int ret = Interop.Download.GetMimeType(_downloadId, out mime);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get MimeType, " + (DownloadError)ret);
return String.Empty;
}
return mime;
}
}
/// <summary>
/// The current state of the download.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public DownloadState State
{
get
{
int state;
int ret = Interop.Download.GetState(_downloadId, out state);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get DownloadState, " + (DownloadError)ret);
return DownloadState.None;
}
return (DownloadState)state;
}
}
/// <summary>
/// The content name of the downloaded file.
/// This can be defined with reference of the HTTP response header data. The content name can be received when the HTTP response header is received.
/// If you try to get this property value before calling Start(), an empty string is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public string ContentName
{
get
{
string name;
int ret = Interop.Download.GetContentName(_downloadId, out name);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get ContentName, " + (DownloadError)ret);
return String.Empty;
}
return name;
}
}
/// <summary>
/// The total size of the downloaded content.
/// This information is received from the server. If the server does not send the total size of the content, the content size is set to zero.
/// If you try to get this property value before calling Start(), 0 is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public ulong ContentSize
{
get
{
ulong size;
int ret = Interop.Download.GetContentSize(_downloadId, out size);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get ContentSize, " + (DownloadError)ret);
return 0;
}
return size;
}
}
/// <summary>
/// The HTTP status code when a download exception occurs.
/// If you try to get this property value before calling Start(), 0 is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// The state of the download request must be DownlodState.Failed.
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public int HttpStatus
{
get
{
int status;
int ret = Interop.Download.GetHttpStatus(_downloadId, out status);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get HttpStatus, " + (DownloadError)ret);
return 0;
}
return status;
}
}
/// <summary>
/// The ETag value from the HTTP response header when making a HTTP request for resume.
/// If you try to get this property value before calling Start() or if any other error occurs, an empty string is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// The ETag value is either available or not dependent on the web server. If not available, then, on getting the property, a null value is returned.
/// After the download is started, it can get the ETag value.
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public string ETagValue
{
get
{
string etag;
int ret = Interop.Download.GetETag(_downloadId, out etag);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get ETagValue, " + (DownloadError)ret);
return String.Empty;
}
return etag;
}
}
/// <summary>
/// Contains properties required for creating download notifications.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <remarks>
/// When the notification message is clicked, the action taken by the system is decided by the application control properties of the NotificationProperties instance.
/// If the app control is not set, the following default operation is executed when the notification message is clicked:
/// 1) The download completed state - the viewer application is executed according to the extension name of the downloaded content.
/// 2) The download failed state and ongoing state - the client application is executed.
/// This property should be set before calling Start().
/// </remarks>
public Notification NotificationProperties
{
get
{
return _notificationProperties;
}
}
/// <summary>
/// The full path of the temporary file stores the downloaded content.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// The download state must be one of the states after downloading.
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public string TemporaryPath
{
get
{
string path;
int ret = Interop.Download.GetTempFilePath(_downloadId, out path);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get TemporaryPath, " + (DownloadError)ret);
return String.Empty;
}
return path;
}
}
/// <summary>
/// The URL to download.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// Should be set before calling Start().
/// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public string Url
{
get
{
string url;
int ret = Interop.Download.GetUrl(_downloadId, out url);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get Url, " + (DownloadError)ret);
return String.Empty;
}
return url;
}
set
{
int ret = Interop.Download.SetUrl(_downloadId, value);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Failed to set Url");
}
}
}
/// <summary>
/// The allowed network type for downloading the file.
/// The file will be downloaded only under the allowed network.
/// If you try to get this property value before setting or if any other error occurs, the default value NetworkType All is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <feature>http://tizen.org/feature/network.wifi.direct</feature>
/// <feature>http://tizen.org/feature/network.telephony</feature>
/// <remarks>
/// Should be set before calling Start().
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
/// <exception cref="NotSupportedException">Thrown when feature is not supported.</exception>
public NetworkType AllowedNetworkType
{
get
{
int type;
int ret = Interop.Download.GetNetworkType(_downloadId, out type);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get AllowedNetworkType, " + (DownloadError)ret);
return NetworkType.All;
}
return (NetworkType)type;
}
set
{
int ret = Interop.Download.SetNetworkType(_downloadId, (int)value);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Failed to set AllowedNetworkType");
}
}
}
/// <summary>
/// The file will be downloaded to the set the destination file path. The downloaded file is saved to an auto-generated file name in the destination. If the destination is not specified, the file will be downloaded to the default storage.
/// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// Should be set before calling Start().
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public string DestinationPath
{
get
{
string path;
int ret = Interop.Download.GetDestination(_downloadId, out path);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get DestinationPath, " + (DownloadError)ret);
return String.Empty;
}
return path;
}
set
{
int ret = Interop.Download.SetDestination(_downloadId, value);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Failed to set DestinationPath");
}
}
}
/// <summary>
/// The file will be saved in the specified destination or the default storage with the set file name. If the file name is not specified, the downloaded file will be saved with an auto-generated file name in the destination.
/// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// Should be set before calling Start().
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public string FileName
{
get
{
string name;
int ret = Interop.Download.GetFileName(_downloadId, out name);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get FileName, " + (DownloadError)ret);
return String.Empty;
}
return name;
}
set
{
int ret = Interop.Download.SetFileName(_downloadId, value);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Failed to set FileName");
}
}
}
/// <summary>
/// Enables or disables auto download.
/// If this option is enabled, the previous downloading item is restarted automatically as soon as the download daemon is restarted. The download progress continues after the client process is terminated.
/// If you try to get this property value before setting, the default value false is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// The default value is false.
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public bool AutoDownload
{
get
{
bool value;
int ret = Interop.Download.GetAutoDownload(_downloadId, out value);
if (ret != (int)DownloadError.None)
{
Log.Error(Globals.LogTag, "Failed to get AutoDownload, " + (DownloadError)ret);
return false;
}
return value;
}
set
{
int ret = Interop.Download.SetAutoDownload(_downloadId, value);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Failed to set AutoDownload");
}
}
}
/// <summary>
/// The HTTP header field and value pairs to the download request.
/// The HTTP header <field,value> pair is the <key,value> pair in the dictionary HttpHeaders.
/// The given HTTP header field will be included with the HTTP request of the download request.
/// If you try to get this property value before setting, an empty dictionary is returned.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <remarks>
/// HTTP header fields should be set before calling Start().
/// HTTP header fields can be removed before calling Start().
/// </remarks>
public IDictionary<string, string> HttpHeaders
{
get
{
return _httpHeaders;
}
}
/// <summary>
/// Sets the directory path of a temporary file used in a previous download request.
/// This is only useful when resuming download to make the HTTP request header at the client side. Otherwise, the path is ignored.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// If the ETag value is not present in the download database, it is not useful to set the temporary file path.
/// When resuming the download request, the data is attached at the end of this temporary file.
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public void SetTemporaryFilePath(string path)
{
int ret = Interop.Download.SetTempFilePath(_downloadId, path);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Failed to set TemporaryFilePath");
}
}
/// <summary>
/// Starts or resumes the download.
/// Starts to download the current URL, or resumes the download if paused.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// The URL is the mandatory information to start the download.
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public void Start()
{
int ret;
foreach (KeyValuePair<string, string> entry in _httpHeaders)
{
ret = Interop.Download.AddHttpHeaderField(_downloadId, entry.Key, entry.Value);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Failed to set HttpHeaders");
}
}
ret = Interop.Download.StartDownload(_downloadId);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Failed to start download request");
}
}
/// <summary>
/// Pauses the download request.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// The paused download request can be restarted with Start() or canceled with Cancel().
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public void Pause()
{
int ret = Interop.Download.PauseDownload(_downloadId);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Failed to pause download request");
}
}
/// <summary>
/// Cancels the download request.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// The canceled download can be restarted with Start().
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public void Cancel()
{
int ret = Interop.Download.CancelDownload(_downloadId);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Failed to cancel download request");
}
}
/// <summary>
/// Releases all the resources used by the Request class.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <remarks>
/// After calling this method, the download request related data exists in the download database for a certain period of time. Within that time, it is possible to use other APIs with this data.
/// </remarks>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Deletes the corresponding download request.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <privilege>http://tizen.org/privilege/download</privilege>
/// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when it is failed due to an invalid operation.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when a permission is denied.</exception>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
// Free managed objects.
}
Interop.Download.DestroyRequest(_downloadId);
_disposed = true;
}
private void RegisterStateChangedEvent()
{
_downloadStateChangedCallback = (int downloadId, int downloadState, IntPtr userData) =>
{
StateChangedEventArgs eventArgs = new StateChangedEventArgs((DownloadState)downloadState);
_downloadStateChanged?.Invoke(this, eventArgs);
};
int ret = Interop.Download.SetStateChangedCallback(_downloadId, _downloadStateChangedCallback, IntPtr.Zero);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting StateChanged callback failed");
}
}
private void UnregisterStateChangedEvent()
{
int ret = Interop.Download.UnsetStateChangedCallback(_downloadId);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Unsetting StateChanged callback failed");
}
}
private void RegisterProgressChangedEvent()
{
_downloadProgressChangedCallback = (int downloadId, ulong size, IntPtr userData) =>
{
ProgressChangedEventArgs eventArgs = new ProgressChangedEventArgs(size);
_downloadProgressChanged?.Invoke(this, eventArgs);
};
int ret = Interop.Download.SetProgressCallback(_downloadId, _downloadProgressChangedCallback, IntPtr.Zero);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Setting ProgressChanged callback failed");
}
}
private void UnregisterProgressChangedEvent()
{
int ret = Interop.Download.UnsetProgressCallback(_downloadId);
if (ret != (int)DownloadError.None)
{
DownloadErrorFactory.ThrowException(ret, "Unsetting ProgressChanged callback failed");
}
}
}
}