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 FilterHandle = Interop.FilterHandle;
21 namespace Tizen.Content.MediaContent
23 internal static class CommandHelper
25 internal delegate MediaContentError CountFunc(FilterHandle filter, out int count);
27 internal delegate MediaContentError CountFunc<T>(T param, FilterHandle filter,
30 internal static int Count(CountFunc countFunc, CountArguments arguments)
32 using (var filter = QueryArguments.ToNativeHandle(arguments))
34 countFunc(filter, out var count).ThrowIfError("Failed to query count");
39 internal static int Count(CountFunc countFunc, string filter)
41 return Count(countFunc, new CountArguments { FilterExpression = filter });
44 internal static int Count<T>(CountFunc<T> countFunc, T param, CountArguments arguments)
46 using (var filter = QueryArguments.ToNativeHandle(arguments))
48 countFunc(param, filter, out var count).ThrowIfError("Failed to query count");
53 internal static void Delete<T>(Func<T, MediaContentError> func, T param)
55 func(param).ThrowIfError("Failed to execute the command");
58 internal delegate MediaContentError ForeachFunc<T>(T param, FilterHandle filter,
59 Interop.Common.ItemCallback callback, IntPtr data = default(IntPtr));
61 internal static MediaDataReader<MediaInfo> SelectMedia<T>(ForeachFunc<T> func, T param,
62 SelectArguments arguments)
64 using (var filter = QueryArguments.ToNativeHandle(arguments))
66 var items = new List<MediaInfo>();
67 Exception exception = null;
69 func(param, filter, (mediaInfoHandle, _) =>
73 items.Add(MediaInfo.FromHandle(mediaInfoHandle));
81 }).ThrowIfError("Failed to query");
83 if (exception != null)
88 return new MediaDataReader<MediaInfo>(items);
92 internal delegate MediaContentError ForeachFunc(FilterHandle filter,
93 Interop.Common.ItemCallback callback, IntPtr data = default(IntPtr));
95 internal static MediaDataReader<TRecord> Select<TRecord>(SelectArguments arguments,
96 ForeachFunc foreachFunc,
97 Func<IntPtr, TRecord> factoryFunc)
99 using (var filter = QueryArguments.ToNativeHandle(arguments))
101 Exception caught = null;
102 var items = new List<TRecord>();
104 foreachFunc(filter, (itemHandle, _) =>
108 items.Add(factoryFunc(itemHandle));
116 }).ThrowIfError("Failed to execute query");
123 return new MediaDataReader<TRecord>(items);
128 internal delegate MediaContentError CloneFunc(out IntPtr output, IntPtr input);
130 internal static IntPtr SelectScalar(ForeachFunc foreachFunc, string filterExpression,
133 using (var filter = QueryArguments.CreateNativeHandle(filterExpression))
135 IntPtr handle = IntPtr.Zero;
137 foreachFunc(filter, (itemHandle, _) =>
139 cloneFunc(out handle, itemHandle);
141 }).ThrowIfError("Failed to execute query");
147 internal delegate MediaContentError ForeachMemberFunc<TParam>(TParam param,
148 FilterHandle filter, Interop.Common.ItemCallback callback, IntPtr data = default(IntPtr));
150 internal static MediaDataReader<TRecord> SelectMembers<TParam, TRecord>(TParam param,
151 SelectArguments arguments, ForeachMemberFunc<TParam> foreachFunc,
152 Func<IntPtr, TRecord> factoryFunc)
154 using (var filter = QueryArguments.ToNativeHandle(arguments))
156 Exception caught = null;
157 var items = new List<TRecord>();
159 foreachFunc(param, filter, (itemHandle, _) =>
163 items.Add(factoryFunc(itemHandle));
172 }).ThrowIfError("Failed to execute query");
179 return new MediaDataReader<TRecord>(items);