Release 4.0.0-preview1-00172
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / StorageCommand.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18
19 namespace Tizen.Content.MediaContent
20 {
21     /// <summary>
22     /// Provides the commands to manage external storages in the database.
23     /// </summary>
24     /// <remarks>
25     /// The internal storage is not managed.
26     /// </remarks>
27     /// <seealso cref="Storage"/>
28     public class StorageCommand : MediaCommand
29     {
30         /// <summary>
31         /// Initializes a new instance of the <see cref="StorageCommand"/> class with the specified <see cref="MediaDatabase"/>.
32         /// </summary>
33         /// <param name="database">The <see cref="MediaDatabase"/> that the commands run on.</param>
34         /// <exception cref="ArgumentNullException"><paramref name="database"/> is null.</exception>
35         /// <exception cref="ObjectDisposedException"><paramref name="database"/> has already been disposed of.</exception>
36         public StorageCommand(MediaDatabase database) : base(database)
37         {
38         }
39
40         /// <summary>
41         /// Retrieves the number of storages.
42         /// </summary>
43         /// <returns>The number of storages.</returns>
44         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
45         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
46         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
47         public int Count() => Count(arguments: null);
48
49         /// <summary>
50         /// Retrieves the number of storages with the <see cref="CountArguments"/>.
51         /// </summary>
52         /// <param name="arguments">The criteria to use to filter. This value can be null.</param>
53         /// <returns>The number of storages filtered.</returns>
54         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
55         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
56         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
57         public int Count(CountArguments arguments)
58         {
59             ValidateDatabase();
60
61             return CommandHelper.Count(Interop.Storage.GetStorageCountFromDb, arguments);
62         }
63
64         /// <summary>
65         /// Retrieves the storage with the specified ID.
66         /// </summary>
67         /// <param name="storageId">The storage ID to select.</param>
68         /// <returns>The <see cref="Storage"/> instance if the matched record was found in the database, otherwise null.</returns>
69         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
70         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
71         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
72         /// <exception cref="ArgumentNullException"><paramref name="storageId"/> is null.</exception>
73         /// <exception cref="ArgumentException"><paramref name="storageId"/> is a zero-length string, contains only white space.</exception>
74         public Storage Select(string storageId)
75         {
76             ValidateDatabase();
77
78             ValidationUtil.ValidateNotNullOrEmpty(storageId, nameof(storageId));
79
80             IntPtr handle = IntPtr.Zero;
81
82             try
83             {
84                 Interop.Storage.GetStorageInfoFromDb(storageId, out handle).ThrowIfError("Failed to query");
85
86                 return handle == IntPtr.Zero ? null : new Storage(handle);
87             }
88             finally
89             {
90                 if (handle != IntPtr.Zero)
91                 {
92                     Interop.Storage.Destroy(handle);
93                 }
94             }
95         }
96
97         /// <summary>
98         /// Retrieves all the storages.
99         /// </summary>
100         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
101         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
102         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
103         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
104         public MediaDataReader<Storage> Select() => Select(arguments: null);
105
106         /// <summary>
107         /// Retrieves the storages with the <see cref="SelectArguments"/>.
108         /// </summary>
109         /// <param name="arguments">The criteria to use to filter. This value can be null.</param>
110         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
111         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
112         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
113         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
114         public MediaDataReader<Storage> Select(SelectArguments arguments)
115         {
116             ValidateDatabase();
117
118             return CommandHelper.Select(arguments, Interop.Storage.ForeachStorageFromDb, Storage.FromHandle);
119         }
120
121         private bool Exists(string storageId)
122         {
123             ValidateDatabase();
124
125             ValidationUtil.ValidateNotNullOrEmpty(storageId, nameof(storageId));
126
127             return Count(new CountArguments { FilterExpression = $"{StorageColumns.Id}='{storageId}'" }) != 0;
128         }
129
130         /// <summary>
131         /// Retrieves the number of media information of the storage.
132         /// </summary>
133         /// <param name="storageId">The storage ID.</param>
134         /// <returns>The number of media information.</returns>
135         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
136         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
137         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
138         /// <exception cref="ArgumentNullException"><paramref name="storageId"/> is null.</exception>
139         /// <exception cref="ArgumentException"><paramref name="storageId"/> is a zero-length string, contains only white space.</exception>
140         public int CountMedia(string storageId) => CountMedia(storageId, null);
141
142         /// <summary>
143         /// Retrieves the number of media information of the storage with the <see cref="CountArguments"/>.
144         /// </summary>
145         /// <param name="storageId">The storage ID to query with.</param>
146         /// <param name="arguments">The criteria to use to filter. This value can be null.</param>
147         /// <returns>The number of media information.</returns>
148         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
149         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
150         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
151         /// <exception cref="ArgumentNullException"><paramref name="storageId"/> is null.</exception>
152         /// <exception cref="ArgumentException"><paramref name="storageId"/> is a zero-length string, contains only white space.</exception>
153         public int CountMedia(string storageId, CountArguments arguments)
154         {
155             if (Exists(storageId) == false)
156             {
157                 return 0;
158             }
159
160             return CommandHelper.Count(Interop.Storage.GetMediaCountFromDb, storageId, arguments);
161         }
162
163         /// <summary>
164         /// Retrieves the media information of the storage.
165         /// </summary>
166         /// <param name="storageId">The storage ID.</param>
167         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
168         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
169         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
170         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
171         /// <exception cref="ArgumentNullException"><paramref name="storageId"/> is null.</exception>
172         /// <exception cref="ArgumentException"><paramref name="storageId"/> is a zero-length string, contains only white space.</exception>
173         public MediaDataReader<MediaInfo> SelectMedia(string storageId) => SelectMedia(storageId, null);
174
175         /// <summary>
176         /// Retrieves the media information of the storage with the <see cref="SelectArguments"/>.
177         /// </summary>
178         /// <param name="storageId">The storage ID.</param>
179         /// <param name="filter">The criteria to use to filter. This value can be null.</param>
180         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
181         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
182         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
183         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
184         /// <exception cref="ArgumentNullException"><paramref name="storageId"/> is null.</exception>
185         /// <exception cref="ArgumentException"><paramref name="storageId"/> is a zero-length string, contains only white space.</exception>
186         public MediaDataReader<MediaInfo> SelectMedia(string storageId, SelectArguments filter)
187         {
188             if (Exists(storageId) == false)
189             {
190                 return new MediaDataReader<MediaInfo>(new MediaInfo[0]);
191             }
192
193             return CommandHelper.SelectMedia(Interop.Storage.ForeachMediaFromDb, storageId, filter);
194         }
195
196     }
197 }