Release 4.0.0-preview1-00172
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / AlbumCommand.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 commands to manage albums in the database.
23     /// </summary>
24     /// <seealso cref="Album"/>
25     public class AlbumCommand : MediaCommand
26     {
27         /// <summary>
28         /// Initializes a new instance of the <see cref="AlbumCommand"/> class with the specified <see cref="MediaDatabase"/>.
29         /// </summary>
30         /// <param name="database">The <see cref="MediaDatabase"/> that the commands run on.</param>
31         /// <exception cref="ArgumentNullException"><paramref name="database"/> is null.</exception>
32         /// <exception cref="ObjectDisposedException"><paramref name="database"/> has already been disposed of.</exception>
33         public AlbumCommand(MediaDatabase database) : base(database)
34         {
35         }
36
37         /// <summary>
38         /// Retrieves the number of albums.
39         /// </summary>
40         /// <returns>The number of albums.</returns>
41         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
42         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
43         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
44         public int Count()
45         {
46             return Count(null);
47         }
48
49         /// <summary>
50         /// Retrieves the number of albums with <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 albums.</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.Album.GetAlbumCountFromDb, arguments);
62         }
63
64         /// <summary>
65         /// Retrieves all the albums.
66         /// </summary>
67         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
68         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
69         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
70         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
71         public MediaDataReader<Album> Select()
72         {
73             return Select(null);
74         }
75
76         /// <summary>
77         /// Retrieves the albums with <see cref="SelectArguments"/>.
78         /// </summary>
79         /// <param name="filter">The criteria to use to filter. This value can be null.</param>
80         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
81         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
82         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
83         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
84         public MediaDataReader<Album> Select(SelectArguments filter)
85         {
86             ValidateDatabase();
87
88             return CommandHelper.Select(filter, Interop.Album.ForeachAlbumFromDb, Album.FromHandle);
89         }
90
91         /// <summary>
92         /// Retrieves an album with the album ID.
93         /// </summary>
94         /// <param name="albumId">The ID of the album to query with.</param>
95         /// <returns>The <see cref="Album"/> if <paramref name="albumId"/> exists, otherwise null.</returns>
96         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
97         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
98         /// <exception cref="ArgumentOutOfRangeException"><paramref name="albumId"/> is equal to or less than zero.</exception>
99         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
100         public Album Select(int albumId)
101         {
102             ValidateDatabase();
103
104             if (albumId <= 0)
105             {
106                 throw new ArgumentOutOfRangeException(nameof(albumId), albumId,
107                     "The album id can't be equal to or less than zero.");
108             }
109
110             IntPtr handle = IntPtr.Zero;
111             try
112             {
113                 Interop.Album.GetAlbumFromDb(albumId, out handle).ThrowIfError("Failed to query");
114
115                 return handle == IntPtr.Zero ? null : new Album(handle);
116             }
117             finally
118             {
119                 Interop.Album.Destroy(handle);
120             }
121         }
122
123         /// <summary>
124         /// Retrieves the number of media information that belongs to the album.
125         /// </summary>
126         /// <param name="albumId">The ID of the album to query with.</param>
127         /// <returns>The number of media information.</returns>
128         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
129         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
130         /// <exception cref="ArgumentOutOfRangeException"><paramref name="albumId"/> is equal to or less than zero.</exception>
131         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
132         public int CountMember(int albumId)
133         {
134             return CountMember(albumId, null);
135         }
136
137         /// <summary>
138         /// Retrieves the number of media information that belongs to the album with <see cref="CountArguments"/>.
139         /// </summary>
140         /// <param name="albumId">The ID of the album to count media.</param>
141         /// <param name="arguments">The criteria to use to filter. This value can be null.</param>
142         /// <returns>The number of media information.</returns>
143         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
144         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
145         /// <exception cref="ArgumentOutOfRangeException"><paramref name="albumId"/> is equal to or less than zero.</exception>
146         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
147         public int CountMember(int albumId, CountArguments arguments)
148         {
149             ValidateDatabase();
150
151             if (albumId <= 0)
152             {
153                 throw new ArgumentOutOfRangeException(nameof(albumId), albumId,
154                     "The album id can't be equal to or less than zero.");
155             }
156
157             return CommandHelper.Count(Interop.Album.GetMediaCountFromDb, albumId, arguments);
158         }
159
160         /// <summary>
161         /// Retrieves the media information of the album.
162         /// </summary>
163         /// <param name="albumId">The ID of the album to select media.</param>
164         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
165         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
166         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
167         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
168         public MediaDataReader<MediaInfo> SelectMember(int albumId)
169         {
170             return SelectMember(albumId, null);
171         }
172
173         /// <summary>
174         /// Retrieves the media information of the album with <see cref="SelectArguments"/>.
175         /// </summary>
176         /// <param name="albumId">The ID of the album to query with.</param>
177         /// <param name="filter">The criteria to use to filter. This value can be null.</param>
178         /// <returns>The <see cref="MediaDataReader{TRecord}"/> containing the results.</returns>
179         /// <exception cref="InvalidOperationException">The <see cref="MediaDatabase"/> is disconnected.</exception>
180         /// <exception cref="ObjectDisposedException">The <see cref="MediaDatabase"/> has already been disposed of.</exception>
181         /// <exception cref="MediaDatabaseException">An error occurred while executing the command.</exception>
182         public MediaDataReader<MediaInfo> SelectMember(int albumId, SelectArguments filter)
183         {
184             ValidateDatabase();
185
186             if (albumId <= 0)
187             {
188                 throw new ArgumentOutOfRangeException(nameof(albumId), albumId,
189                     "The album id can't be equal to or less than zero.");
190             }
191
192             return CommandHelper.SelectMedia(Interop.Album.ForeachMediaFromDb, albumId, filter);
193         }
194     }
195 }