bb91bffdb1e7b1f2a551e296a5ea09030ed31187
[platform/core/csapi/media-content.git] / Tizen.Content / Tizen.Content.MediaContent / MediaFolder.cs
1 /// Copyright 2016 by Samsung Electronics, Inc.,
2 ///
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc.("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
8
9
10 using System;
11 using System.Collections.Generic;
12 using System.Threading.Tasks;
13
14 namespace Tizen.Content.MediaContent
15 {
16     /// <summary>
17     /// A Folder is used to organize media content files i.e. image, audio, video files, in the physical storage of the device.
18     /// The Media Folder API provides functions to get basic information about existing folders e.g. folder name, path and storage type.
19     /// It also provides functions to get information related to media items present in the folder.
20     /// </summary>
21     public class MediaFolder : ContentCollection
22     {
23         private IntPtr _folderHandle;
24         private bool _disposedValue = false;
25         internal IntPtr Handle
26         {
27             get
28             {
29                 return _folderHandle;
30             }
31             set
32             {
33                 _folderHandle = value;
34             }
35         }
36         /// <summary>
37         /// The ID of the media folder
38         /// </summary>
39         public string Id
40         {
41             get
42             {
43                 string id;
44                 MediaContentError res = (MediaContentError)Interop.Folder.GetFolderId(_folderHandle, out id);
45                 if (res != MediaContentError.None)
46                 {
47                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get Id for the MediaFolder");
48                 }
49                 return id;
50             }
51         }
52
53         /// <summary>
54         /// The ID of the upper media folder
55         /// </summary>
56         public string ParentId
57         {
58             get
59             {
60                 string parentId;
61                 MediaContentError res = (MediaContentError)Interop.Folder.GetParentFolderId(_folderHandle, out parentId);
62                 if (res != MediaContentError.None)
63                 {
64                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get parents folder Id for the MediaFolder");
65                 }
66                 return parentId;
67             }
68         }
69
70         /// <summary>
71         /// The path of the media folder
72         /// </summary>
73         public string Path
74         {
75             get
76             {
77                 string path;
78                 MediaContentError res = (MediaContentError)Interop.Folder.GetPath(_folderHandle, out path);
79                 if (res != MediaContentError.None)
80                 {
81                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get path for the MediaFolder");
82                 }
83                 return path;
84             }
85         }
86
87         /// <summary>
88         /// The name of the media folder
89         /// </summary>
90         public string Name
91         {
92             get
93             {
94                 string name;
95                 MediaContentError res = (MediaContentError)Interop.Folder.GetName(_folderHandle, out name);
96                 if (res != MediaContentError.None)
97                 {
98                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get name for the MediaFolder");
99                 }
100                 return name;
101             }
102             set
103             {
104                 MediaContentError res = (MediaContentError)Interop.Folder.SetName(_folderHandle, value);
105                 if (res != MediaContentError.None)
106                 {
107                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to set name for the MediaFolder");
108                 }
109             }
110         }
111
112         /// <summary>
113         /// The storage type of the media folder
114         /// </summary>
115         public ContentStorageType StorageType
116         {
117             get
118             {
119                 int type;
120                 MediaContentError res = (MediaContentError)Interop.Folder.GetStorageType(_folderHandle, out type);
121                 if (res != MediaContentError.None)
122                 {
123                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get storage type for the MediaFolder");
124                 }
125                 return (ContentStorageType)type;
126             }
127         }
128
129         /// <summary>
130         /// The storage id of the media folder
131         /// </summary>
132         public string StorageId
133         {
134             get
135             {
136                 string storageId;
137                 MediaContentError res = (MediaContentError)Interop.Folder.GetStorageId(_folderHandle, out storageId);
138                 if (res != MediaContentError.None)
139                 {
140                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get storage Id for the MediaFolder");
141                 }
142                 return storageId;
143             }
144         }
145
146         /// <summary>
147         /// The modified date of the media folder
148         /// </summary>
149         public DateTime ModifiedTime
150         {
151             get
152             {
153                 DateTime date;
154                 MediaContentError res = (MediaContentError)Interop.Folder.GetModifiedTime(_folderHandle, out date);
155                 if (res != MediaContentError.None)
156                 {
157                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get modified date for the MediaFolder");
158                 }
159                 return date;
160             }
161         }
162
163         /// <summary>
164         /// The folder order info. Get/Set the folder viewing order
165         /// </summary>
166         public ContentOrder Order
167         {
168             get
169             {
170                 int order;
171                 MediaContentError res = (MediaContentError)Interop.Folder.GetOrder(_folderHandle, out order);
172                 if (res != MediaContentError.None)
173                 {
174                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get modified date for the MediaFolder");
175                 }
176                 return (ContentOrder)order;
177             }
178             set
179             {
180                 MediaContentError res = (MediaContentError)Interop.Folder.SetOrder(_folderHandle, (int)value);
181                 if (res != MediaContentError.None)
182                 {
183                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to set viewing order for the MediaFolder");
184                 }
185             }
186         }
187
188         internal MediaFolder(IntPtr _folderHandle)
189         {
190             this.Handle = _folderHandle;
191         }
192
193         /// <summary>
194         /// Gets the count of media files for the passed filter in the given folder from the media database.
195         /// </summary>
196         /// <param name="filter">ContentFilter used to match media content from teh media database.</param>
197         /// <returns>The number of media contents matching the filter passed</returns>
198         public override int GetMediaInformationCount(ContentFilter filter)
199         {
200             int mediaCount;
201             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
202             MediaContentError res = (MediaContentError)Interop.Folder.GetMediaCountFromDb(Id, handle, out mediaCount);
203             if (res != MediaContentError.None)
204             {
205                 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get media count for the folder");
206             }
207             return mediaCount;
208         }
209
210         ~MediaFolder()
211         {
212             Dispose(false);
213         }
214        public override void Dispose()
215         {
216             Dispose(true);
217             GC.SuppressFinalize(this);
218         }
219
220         protected virtual void Dispose(bool disposing)
221         {
222             if (!_disposedValue)
223             {
224                 if (_folderHandle != IntPtr.Zero)
225                 {
226                     Console.WriteLine("Before destroy");
227                     Interop.Folder.Destroy(_folderHandle);
228                     _folderHandle = IntPtr.Zero;
229                     Console.WriteLine("After destroy");
230                 }
231                 _disposedValue = true;
232             }
233         }
234         /// <summary>
235         /// Iterates through the media files with an optional filter in the given folder from the media database.
236         /// This function gets all media files associated with the given folder and meeting desired filter option.
237         /// If NULL is passed to the filter, no filtering is applied.
238         /// </summary>
239         /// <param name="filter">ContentFilter used to match media content from the media database.</param>
240         /// <returns>List of content media items matching the passed filter</returns>
241         public override Task<IEnumerable<MediaInformation>> GetMediaInformationsAsync(ContentFilter filter)
242         {
243             var tcs = new TaskCompletionSource<IEnumerable<MediaInformation>>();
244             List<MediaInformation> mediaContents = new List<MediaInformation>();
245             IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
246             MediaContentError res;
247             Interop.Folder.MediaInfoCallback callback = (Interop.MediaInformation.SafeMediaInformationHandle mediaHandle, IntPtr data) =>
248             {
249                 //TODO: Create MediaInformation using safehandle.
250                 Interop.MediaInformation.SafeMediaInformationHandle newHandle;
251                 res = (MediaContentError)Interop.MediaInformation.Clone(out newHandle, mediaHandle.DangerousGetHandle());
252                 if (res != MediaContentError.None)
253                 {
254                     Log.Warn(MediaContentErrorFactory.LogTag, "Failed to clone media information");
255                 }
256                 MediaInformation info = new MediaInformation(newHandle);
257                 mediaContents.Add(info);
258             };
259             res = (MediaContentError)Interop.Folder.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero);
260             if (res != MediaContentError.None)
261             {
262                 Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get media information for the folder");
263             }
264             tcs.TrySetResult(mediaContents);
265             return tcs.Task;
266         }
267     }
268 }