[MediaContent] Deprecate underutilized fields (#5850)
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.MediaContent / Tizen.Content.MediaContent / CommandHelper.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 using System.Collections.Generic;
19 using FilterHandle = Interop.FilterHandle;
20
21 namespace Tizen.Content.MediaContent
22 {
23     internal static class CommandHelper
24     {
25         internal delegate MediaContentError CountFunc(FilterHandle filter, out int count);
26
27         internal delegate MediaContentError CountFunc<T>(T param, FilterHandle filter,
28             out int count);
29
30         internal static int Count(CountFunc countFunc, CountArguments arguments)
31         {
32             using (var filter = QueryArguments.ToNativeHandle(arguments))
33             {
34                 countFunc(filter, out var count).ThrowIfError("Failed to query count");
35                 return count;
36             }
37         }
38
39         internal static int Count(CountFunc countFunc, string filter)
40         {
41             return Count(countFunc, new CountArguments { FilterExpression = filter });
42         }
43
44         internal static int Count<T>(CountFunc<T> countFunc, T param, CountArguments arguments)
45         {
46             using (var filter = QueryArguments.ToNativeHandle(arguments))
47             {
48                 countFunc(param, filter, out var count).ThrowIfError("Failed to query count");
49                 return count;
50             }
51         }
52
53         internal static void Delete<T>(Func<T, MediaContentError> func, T param)
54         {
55             func(param).ThrowIfError("Failed to execute the command");
56         }
57
58         internal delegate MediaContentError ForeachFunc<T>(T param, FilterHandle filter,
59             Interop.Common.ItemCallback callback, IntPtr data = default(IntPtr));
60
61         internal static MediaDataReader<MediaInfo> SelectMedia<T>(ForeachFunc<T> func, T param,
62             SelectArguments arguments)
63         {
64             using (var filter = QueryArguments.ToNativeHandle(arguments))
65             {
66                 var items = new List<MediaInfo>();
67                 Exception exception = null;
68
69                 func(param, filter, (mediaInfoHandle, _) =>
70                 {
71                     try
72                     {
73                         items.Add(MediaInfo.FromHandle(mediaInfoHandle));
74                         return true;
75                     }
76                     catch (Exception e)
77                     {
78                         exception = e;
79                         return false;
80                     }
81                 }).ThrowIfError("Failed to query");
82
83                 if (exception != null)
84                 {
85                     throw exception;
86                 }
87
88                 return new MediaDataReader<MediaInfo>(items);
89             }
90         }
91
92         internal delegate MediaContentError ForeachFunc(FilterHandle filter,
93             Interop.Common.ItemCallback callback, IntPtr data = default(IntPtr));
94
95         internal static MediaDataReader<TRecord> Select<TRecord>(SelectArguments arguments,
96             ForeachFunc foreachFunc,
97             Func<IntPtr, TRecord> factoryFunc)
98         {
99             using (var filter = QueryArguments.ToNativeHandle(arguments))
100             {
101                 Exception caught = null;
102                 var items = new List<TRecord>();
103
104                 foreachFunc(filter, (itemHandle, _) =>
105                 {
106                     try
107                     {
108                         items.Add(factoryFunc(itemHandle));
109                         return true;
110                     }
111                     catch (Exception e)
112                     {
113                         caught = e;
114                         return false;
115                     }
116                 }).ThrowIfError("Failed to execute query");
117
118                 if (caught != null)
119                 {
120                     throw caught;
121                 }
122
123                 return new MediaDataReader<TRecord>(items);
124             }
125         }
126
127
128         internal delegate MediaContentError CloneFunc(out IntPtr output, IntPtr input);
129
130         internal static IntPtr SelectScalar(ForeachFunc foreachFunc, string filterExpression,
131             CloneFunc cloneFunc)
132         {
133             using (var filter = QueryArguments.CreateNativeHandle(filterExpression))
134             {
135                 IntPtr handle = IntPtr.Zero;
136
137                 foreachFunc(filter, (itemHandle, _) =>
138                 {
139                     cloneFunc(out handle, itemHandle);
140                     return false;
141                 }).ThrowIfError("Failed to execute query");
142
143                 return handle;
144             }
145         }
146
147         internal delegate MediaContentError ForeachMemberFunc<TParam>(TParam param,
148             FilterHandle filter, Interop.Common.ItemCallback callback, IntPtr data = default(IntPtr));
149
150         internal static MediaDataReader<TRecord> SelectMembers<TParam, TRecord>(TParam param,
151             SelectArguments arguments, ForeachMemberFunc<TParam> foreachFunc,
152             Func<IntPtr, TRecord> factoryFunc)
153         {
154             using (var filter = QueryArguments.ToNativeHandle(arguments))
155             {
156                 Exception caught = null;
157                 var items = new List<TRecord>();
158
159                 foreachFunc(param, filter, (itemHandle, _) =>
160                 {
161                     try
162                     {
163                         items.Add(factoryFunc(itemHandle));
164
165                         return true;
166                     }
167                     catch (Exception e)
168                     {
169                         caught = e;
170                         return false;
171                     }
172                 }).ThrowIfError("Failed to execute query");
173
174                 if (caught != null)
175                 {
176                     throw caught;
177                 }
178
179                 return new MediaDataReader<TRecord>(items);
180             }
181         }
182     }
183
184     static internal class MediaContentLog
185     {
186         internal const string Tag = "Tizen.Content.MediaContent";
187     }
188 }