2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
21 namespace Tizen.Content.Download
24 /// The Request class provides functions to create and manage a single download request.
26 /// <since_tizen> 3 </since_tizen>
27 public class Request : IDisposable
29 private int _downloadId;
30 private Notification _notificationProperties;
31 private IDictionary<string, string> _httpHeaders;
32 private EventHandler<StateChangedEventArgs> _downloadStateChanged;
33 private Interop.Download.StateChangedCallback _downloadStateChangedCallback;
34 private EventHandler<ProgressChangedEventArgs> _downloadProgressChanged;
35 private Interop.Download.ProgressChangedCallback _downloadProgressChangedCallback;
36 private bool _disposed = false;
39 /// Creates a Request object.
41 /// <since_tizen> 3 </since_tizen>
42 /// <param name="url"> URL to download</param>
43 /// <privilege>http://tizen.org/privilege/download</privilege>
44 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
45 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
46 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
47 public Request(string url)
49 if (String.IsNullOrEmpty(url))
51 DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
53 int ret = Interop.Download.CreateRequest(out _downloadId);
54 if (ret != (int)DownloadError.None)
56 DownloadErrorFactory.ThrowException(ret, "Request creation failed");
58 ret = Interop.Download.SetUrl(_downloadId, url);
59 if (ret != (int)DownloadError.None)
61 DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
63 _notificationProperties = new Notification(_downloadId);
64 _httpHeaders = new Dictionary<string, string>();
68 /// Creates a Request object.
70 /// <since_tizen> 3 </since_tizen>
71 /// <param name="url"> URL to download</param>
72 /// <param name="destinationPath"> Directory path where downloaded file is stored </param>
73 /// <param name="fileName"> Name of the downloaded file </param>
74 /// <param name="type"> Network type which the download request must adhere to </param>
75 /// <privilege>http://tizen.org/privilege/download</privilege>
76 /// <feature>http://tizen.org/feature/network.wifi</feature>
77 /// <feature>http://tizen.org/feature/network.wifi.direct</feature>
78 /// <feature>http://tizen.org/feature/network.telephony</feature>
79 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
80 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
81 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
82 /// <exception cref="NotSupportedException">Thrown when feature is not supported.</exception>
83 public Request(string url, string destinationPath, string fileName, NetworkType type)
85 if (String.IsNullOrEmpty(url))
87 DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
89 int ret = Interop.Download.CreateRequest(out _downloadId);
90 if (ret != (int)DownloadError.None)
92 DownloadErrorFactory.ThrowException(ret, "Request creation failed");
95 ret = Interop.Download.SetUrl(_downloadId, url);
96 if (ret != (int)DownloadError.None)
98 DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
101 ret = Interop.Download.SetDestination(_downloadId, destinationPath);
102 if (ret != (int)DownloadError.None)
104 DownloadErrorFactory.ThrowException(ret, "Setting DestinationPath failed");
107 ret = Interop.Download.SetFileName(_downloadId, fileName);
108 if (ret != (int)DownloadError.None)
110 DownloadErrorFactory.ThrowException(ret, "Setting FileName failed");
113 ret = Interop.Download.SetNetworkType(_downloadId, (int)type);
114 if (ret != (int)DownloadError.None)
116 DownloadErrorFactory.ThrowException(ret, "Setting NetworkType failed");
119 _notificationProperties = new Notification(_downloadId);
120 _httpHeaders = new Dictionary<string, string>();
124 /// Creates a Request object.
126 /// <since_tizen> 3 </since_tizen>
127 /// <param name="url"> URL to download</param>
128 /// <param name="destinationPath"> Directory path where downloaded file is stored </param>
129 /// <param name="fileName"> Name of the downloaded file </param>
130 /// <param name="type"> Network type which the download request must adhere to </param>
131 /// <param name="httpHeaders"> HTTP header fields for download request </param>
132 /// <privilege>http://tizen.org/privilege/download</privilege>
133 /// <feature>http://tizen.org/feature/network.wifi</feature>
134 /// <feature>http://tizen.org/feature/network.wifi.direct</feature>
135 /// <feature>http://tizen.org/feature/network.telephony</feature>
136 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
137 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
138 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
139 /// <exception cref="NotSupportedException">Thrown when features is not supported.</exception>
140 public Request(string url, string destinationPath, string fileName, NetworkType type, IDictionary<string, string> httpHeaders)
142 if (String.IsNullOrEmpty(url))
144 DownloadErrorFactory.ThrowException((int)DownloadError.InvalidParameter, "url cannot be null or empty");
146 int ret = Interop.Download.CreateRequest(out _downloadId);
147 if (ret != (int)DownloadError.None)
149 DownloadErrorFactory.ThrowException(ret, "Request creation failed");
152 ret = Interop.Download.SetUrl(_downloadId, url);
153 if (ret != (int)DownloadError.None)
155 DownloadErrorFactory.ThrowException(ret, "Setting Url failed");
158 ret = Interop.Download.SetDestination(_downloadId, destinationPath);
159 if (ret != (int)DownloadError.None)
161 DownloadErrorFactory.ThrowException(ret, "Setting DestinationPath failed");
164 ret = Interop.Download.SetFileName(_downloadId, fileName);
165 if (ret != (int)DownloadError.None)
167 DownloadErrorFactory.ThrowException(ret, "Setting FileName failed");
170 ret = Interop.Download.SetNetworkType(_downloadId, (int)type);
171 if (ret != (int)DownloadError.None)
173 DownloadErrorFactory.ThrowException(ret, "Setting NetworkType failed");
176 _notificationProperties = new Notification(_downloadId);
177 _httpHeaders = httpHeaders;
186 /// Event that occurs when the download state changes.
188 /// <since_tizen> 3 </since_tizen>
189 /// <privilege>http://tizen.org/privilege/download</privilege>
190 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
191 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
192 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
193 public event EventHandler<StateChangedEventArgs> StateChanged
197 if (_downloadStateChanged == null)
199 RegisterStateChangedEvent();
201 _downloadStateChanged += value;
205 _downloadStateChanged -= value;
206 if (_downloadStateChanged == null)
208 UnregisterStateChangedEvent();
214 /// Event that occurs when the download progress changes.
216 /// <since_tizen> 3 </since_tizen>
217 /// <privilege>http://tizen.org/privilege/download</privilege>
218 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
219 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
220 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
221 public event EventHandler<ProgressChangedEventArgs> ProgressChanged
225 if (_downloadProgressChanged == null)
227 RegisterProgressChangedEvent();
229 _downloadProgressChanged += value;
233 _downloadProgressChanged -= value;
234 if (_downloadProgressChanged == null)
236 UnregisterProgressChangedEvent();
242 /// Absolute path where the file will be downloaded.
243 /// If you try to get this property value before calling Start(), an empty string is returned.
245 /// <since_tizen> 3 </since_tizen>
246 /// <privilege>http://tizen.org/privilege/download</privilege>
248 /// Returns empty string if download is not completed or if state has not yet changed to Completed or if any other error occurs.
250 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
251 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
252 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
253 public string DownloadedPath
258 int ret = Interop.Download.GetDownloadedPath(_downloadId, out path);
259 if (ret != (int)DownloadError.None)
261 Log.Error(Globals.LogTag, "Failed to get DownloadedPath, " + (DownloadError)ret);
269 /// MIME type of the downloaded content.
270 /// If you try to get this property value before calling Start(), an empty string is returned.
272 /// <since_tizen> 3 </since_tizen>
273 /// <privilege>http://tizen.org/privilege/download</privilege>
274 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
275 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
276 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
277 public string MimeType
282 int ret = Interop.Download.GetMimeType(_downloadId, out mime);
283 if (ret != (int)DownloadError.None)
285 Log.Error(Globals.LogTag, "Failed to get MimeType, " + (DownloadError)ret);
293 /// Current state of the download.
295 /// <since_tizen> 3 </since_tizen>
296 /// <privilege>http://tizen.org/privilege/download</privilege>
297 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
298 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
299 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
300 public DownloadState State
305 int ret = Interop.Download.GetState(_downloadId, out state);
306 if (ret != (int)DownloadError.None)
308 Log.Error(Globals.LogTag, "Failed to get DownloadState, " + (DownloadError)ret);
309 return DownloadState.None;
311 return (DownloadState)state;
316 /// The content name of the downloaded file.
317 /// This can be defined with reference of HTTP response header data. The content name can be received when HTTP response header is received.
318 /// If you try to get this property value before calling Start(), an empty string is returned.
320 /// <since_tizen> 3 </since_tizen>
321 /// <privilege>http://tizen.org/privilege/download</privilege>
322 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
323 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
324 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
325 public string ContentName
330 int ret = Interop.Download.GetContentName(_downloadId, out name);
331 if (ret != (int)DownloadError.None)
333 Log.Error(Globals.LogTag, "Failed to get ContentName, " + (DownloadError)ret);
341 /// Total size of downloaded content.
342 /// 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.
343 /// If you try to get this property value before calling Start(), 0 is returned.
345 /// <since_tizen> 3 </since_tizen>
346 /// <privilege>http://tizen.org/privilege/download</privilege>
347 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
348 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
349 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
350 public ulong ContentSize
355 int ret = Interop.Download.GetContentSize(_downloadId, out size);
356 if (ret != (int)DownloadError.None)
358 Log.Error(Globals.LogTag, "Failed to get ContentSize, " + (DownloadError)ret);
366 /// HTTP status code when a download exception occurs.
367 /// If you try to get this property value before calling Start(), 0 is returned.
369 /// <since_tizen> 3 </since_tizen>
370 /// <privilege>http://tizen.org/privilege/download</privilege>
372 /// State of download request must be DownlodState.Failed.
374 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
375 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
376 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
377 public int HttpStatus
382 int ret = Interop.Download.GetHttpStatus(_downloadId, out status);
383 if (ret != (int)DownloadError.None)
385 Log.Error(Globals.LogTag, "Failed to get HttpStatus, " + (DownloadError)ret);
393 /// ETag value from the HTTP response header when making a HTTP request for resume.
394 /// If you try to get this property value before calling Start() or if any other error occurs, an empty string is returned.
396 /// <since_tizen> 3 </since_tizen>
397 /// <privilege>http://tizen.org/privilege/download</privilege>
399 /// The etag value is available or not depending on the web server. If not available, then on get of the property null is returned.
400 /// After download is started, it can get the etag value.
402 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
403 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
404 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
405 public string ETagValue
410 int ret = Interop.Download.GetETag(_downloadId, out etag);
411 if (ret != (int)DownloadError.None)
413 Log.Error(Globals.LogTag, "Failed to get ETagValue, " + (DownloadError)ret);
421 /// Contains properties required for creating download notifications.
423 /// <since_tizen> 3 </since_tizen>
425 /// When the notification message is clicked, the action taken by the system is decided by the app control properties of the NotificationProperties instance.
426 /// If the app control is not set, the following default operation is executed when the notification message is clicked:
427 /// 1) download completed state - the viewer application is executed according to extension name of downloaded content,
428 /// 2) download failed state and ongoing state - the client application is executed.
429 /// This property should be set before calling Start().
431 public Notification NotificationProperties
435 return _notificationProperties;
440 /// Full path of the temporary file which stores downloaded content.
442 /// <since_tizen> 3 </since_tizen>
443 /// <privilege>http://tizen.org/privilege/download</privilege>
445 /// The download state must be one of the states after Downloading.
447 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
448 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
449 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
450 public string TemporaryPath
455 int ret = Interop.Download.GetTempFilePath(_downloadId, out path);
456 if (ret != (int)DownloadError.None)
458 Log.Error(Globals.LogTag, "Failed to get TemporaryPath, " + (DownloadError)ret);
468 /// <since_tizen> 3 </since_tizen>
469 /// <privilege>http://tizen.org/privilege/download</privilege>
471 /// Should be set before calling Start().
472 /// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
474 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
475 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
476 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
482 int ret = Interop.Download.GetUrl(_downloadId, out url);
483 if (ret != (int)DownloadError.None)
485 Log.Error(Globals.LogTag, "Failed to get Url, " + (DownloadError)ret);
492 int ret = Interop.Download.SetUrl(_downloadId, value);
493 if (ret != (int)DownloadError.None)
495 DownloadErrorFactory.ThrowException(ret, "Failed to set Url");
501 /// Allowed network type for downloading the file.
502 /// The file will be downloaded only under the allowed network.
503 /// If you try to get this property value before setting or if any other error occurs, default value NetworkType All is returned.
505 /// <since_tizen> 3 </since_tizen>
506 /// <privilege>http://tizen.org/privilege/download</privilege>
507 /// <feature>http://tizen.org/feature/network.wifi</feature>
508 /// <feature>http://tizen.org/feature/network.wifi.direct</feature>
509 /// <feature>http://tizen.org/feature/network.telephony</feature>
511 /// Should be set before calling Start().
513 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
514 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
515 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
516 /// <exception cref="NotSupportedException">Thrown when feature is not supported.</exception>
517 public NetworkType AllowedNetworkType
522 int ret = Interop.Download.GetNetworkType(_downloadId, out type);
523 if (ret != (int)DownloadError.None)
525 Log.Error(Globals.LogTag, "Failed to get AllowedNetworkType, " + (DownloadError)ret);
526 return NetworkType.All;
528 return (NetworkType)type;
532 int ret = Interop.Download.SetNetworkType(_downloadId, (int)value);
533 if (ret != (int)DownloadError.None)
535 DownloadErrorFactory.ThrowException(ret, "Failed to set AllowedNetworkType");
541 /// The file will be downloaded to the set 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 default storage.
542 /// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
544 /// <since_tizen> 3 </since_tizen>
545 /// <privilege>http://tizen.org/privilege/download</privilege>
547 /// Should be set before calling Start().
549 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
550 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
551 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
552 public string DestinationPath
557 int ret = Interop.Download.GetDestination(_downloadId, out path);
558 if (ret != (int)DownloadError.None)
560 Log.Error(Globals.LogTag, "Failed to get DestinationPath, " + (DownloadError)ret);
567 int ret = Interop.Download.SetDestination(_downloadId, value.ToString());
568 if (ret != (int)DownloadError.None)
570 DownloadErrorFactory.ThrowException(ret, "Failed to set DestinationPath");
576 /// The file will be saved in the specified destination or 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.
577 /// If you try to get this property value before setting or if any other error occurs, an empty string is returned.
579 /// <since_tizen> 3 </since_tizen>
580 /// <privilege>http://tizen.org/privilege/download</privilege>
582 /// Should be set before calling Start().
584 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
585 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
586 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
587 public string FileName
592 int ret = Interop.Download.GetFileName(_downloadId, out name);
593 if (ret != (int)DownloadError.None)
595 Log.Error(Globals.LogTag, "Failed to get FileName, " + (DownloadError)ret);
602 int ret = Interop.Download.SetFileName(_downloadId, value.ToString());
603 if (ret != (int)DownloadError.None)
605 DownloadErrorFactory.ThrowException(ret, "Failed to set FileName");
611 /// Enables or disables auto download.
612 /// 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.
613 /// If you try to get this property value before setting, default value false is returned.
615 /// <since_tizen> 3 </since_tizen>
616 /// <privilege>http://tizen.org/privilege/download</privilege>
618 /// The default value is false.
620 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
621 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
622 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
623 public bool AutoDownload
628 int ret = Interop.Download.GetAutoDownload(_downloadId, out value);
629 if (ret != (int)DownloadError.None)
631 Log.Error(Globals.LogTag, "Failed to get AutoDownload, " + (DownloadError)ret);
638 int ret = Interop.Download.SetAutoDownload(_downloadId, value);
639 if (ret != (int)DownloadError.None)
641 DownloadErrorFactory.ThrowException(ret, "Failed to set AutoDownload");
647 /// HTTP header field and value pairs to the download request.
648 /// HTTP header <field,value> pair is the <key,value> pair in the Dictionary HttpHeaders
649 /// The given HTTP header field will be included with the HTTP request of the download request.
650 /// If you try to get this property value before setting, an empty dictionary is returned.
652 /// <since_tizen> 3 </since_tizen>
654 /// HTTP header fields should be set before calling Start().
655 /// HTTP header fields can be removed before calling Start().
657 public IDictionary<string, string> HttpHeaders
666 /// Sets the directory path of a temporary file used in a previous download request.
667 /// This is only useful when resuming download to make HTTP request header at the client side. Otherwise, the path is ignored.
669 /// <since_tizen> 3 </since_tizen>
670 /// <privilege>http://tizen.org/privilege/download</privilege>
672 /// If the etag value is not present in the download database, it is not useful to set the temporary file path.
673 /// When resuming the download request, the data is attached at the end of this temporary file.
675 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
676 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
677 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
678 public void SetTemporaryFilePath(string path)
680 int ret = Interop.Download.SetTempFilePath(_downloadId, path);
681 if (ret != (int)DownloadError.None)
683 DownloadErrorFactory.ThrowException(ret, "Failed to set TemporaryFilePath");
688 /// Starts or resumes download.
689 /// Starts to download the current URL, or resumes the download if paused.
691 /// <since_tizen> 3 </since_tizen>
692 /// <privilege>http://tizen.org/privilege/download</privilege>
694 /// The URL is the mandatory information to start the download.
696 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
697 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
698 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
701 int ret = (int)DownloadError.None;
702 foreach (KeyValuePair<string, string> entry in _httpHeaders)
704 ret = Interop.Download.AddHttpHeaderField(_downloadId, entry.Key, entry.Value);
705 if (ret != (int)DownloadError.None)
707 DownloadErrorFactory.ThrowException(ret, "Failed to set HttpHeaders");
711 ret = Interop.Download.StartDownload(_downloadId);
712 if (ret != (int)DownloadError.None)
714 DownloadErrorFactory.ThrowException(ret, "Failed to start download request");
719 /// Pauses download request.
721 /// <since_tizen> 3 </since_tizen>
722 /// <privilege>http://tizen.org/privilege/download</privilege>
724 /// The paused download request can be restarted with Start() or canceled with Cancel().
726 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
727 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
728 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
731 int ret = Interop.Download.PauseDownload(_downloadId);
732 if (ret != (int)DownloadError.None)
734 DownloadErrorFactory.ThrowException(ret, "Failed to pause download request");
739 /// Cancels download request.
741 /// <since_tizen> 3 </since_tizen>
742 /// <privilege>http://tizen.org/privilege/download</privilege>
744 /// The canceled download can be restarted with Start().
746 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
747 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
748 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
751 int ret = Interop.Download.CancelDownload(_downloadId);
752 if (ret != (int)DownloadError.None)
754 DownloadErrorFactory.ThrowException(ret, "Failed to cancel download request");
759 /// Releases all resources used by the Request class.
761 /// <since_tizen> 3 </since_tizen>
762 /// <privilege>http://tizen.org/privilege/download</privilege>
764 /// After calling this method, 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.
766 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
767 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
768 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
769 public void Dispose()
772 GC.SuppressFinalize(this);
776 /// Deletes the corresponding download request.
778 /// <since_tizen> 3 </since_tizen>
779 /// <privilege>http://tizen.org/privilege/download</privilege>
780 /// <exception cref="ArgumentException">Thrown when it is failed due to an invalid parameter.</exception>
781 /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation</exception>
782 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
783 protected virtual void Dispose(bool disposing)
790 // Free managed objects.
792 Interop.Download.DestroyRequest(_downloadId);
796 static private void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray)
798 managedArray = new string[size];
799 IntPtr[] IntPtrArray = new IntPtr[size];
801 Marshal.Copy(unmanagedArray, IntPtrArray, 0, size);
803 for (int iterator = 0; iterator < size; iterator++)
805 managedArray[iterator] = Marshal.PtrToStringAnsi(IntPtrArray[iterator]);
809 private void RegisterStateChangedEvent()
811 _downloadStateChangedCallback = (int downloadId, int downloadState, IntPtr userData) =>
813 StateChangedEventArgs eventArgs = new StateChangedEventArgs((DownloadState)downloadState);
814 _downloadStateChanged?.Invoke(this, eventArgs);
817 int ret = Interop.Download.SetStateChangedCallback(_downloadId, _downloadStateChangedCallback, IntPtr.Zero);
818 if (ret != (int)DownloadError.None)
820 DownloadErrorFactory.ThrowException(ret, "Setting StateChanged callback failed");
824 private void UnregisterStateChangedEvent()
826 int ret = Interop.Download.UnsetStateChangedCallback(_downloadId);
827 if (ret != (int)DownloadError.None)
829 DownloadErrorFactory.ThrowException(ret, "Unsetting StateChanged callback failed");
833 private void RegisterProgressChangedEvent()
835 _downloadProgressChangedCallback = (int downloadId, ulong size, IntPtr userData) =>
837 ProgressChangedEventArgs eventArgs = new ProgressChangedEventArgs(size);
838 _downloadProgressChanged?.Invoke(this, eventArgs);
841 int ret = Interop.Download.SetProgressCallback(_downloadId, _downloadProgressChangedCallback, IntPtr.Zero);
842 if (ret != (int)DownloadError.None)
844 DownloadErrorFactory.ThrowException(ret, "Setting ProgressChanged callback failed");
848 private void UnregisterProgressChangedEvent()
850 int ret = Interop.Download.UnsetProgressCallback(_downloadId);
851 if (ret != (int)DownloadError.None)
853 DownloadErrorFactory.ThrowException(ret, "Unsetting ProgressChanged callback failed");