summaryrefslogtreecommitdiff
path: root/Tizen.Content/Tizen.Content.MediaContent/Storage.cs
blob: b34e6a11952042a6abfa4c51c40be028b330b097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/// Copyright 2016 by Samsung Electronics, Inc.,
///
/// This software is the confidential and proprietary information
/// of Samsung Electronics, Inc.("Confidential Information"). You
/// shall not disclose such Confidential Information and shall use
/// it only in accordance with the terms of the license agreement
/// you entered into with Samsung.


using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Tizen.Content.MediaContent
{
    /// <summary>
    /// A Storage allows you to manage external storage.
    /// The system generates the storage id when the external storage is added.And the system manages the media information in each of the storage by using storage id.
    /// So you can get the information from the storage that you want to view.
    /// </summary>
    internal class Storage : ContentCollection
    {
        private IntPtr _storageHandle;
        internal IntPtr Handle
        {
            get
            {
                return _storageHandle;
            }
        }
        /// <summary>
        /// The storage id of the media storage
        /// </summary>
        public string Id
        {
            get
            {
                string id;
                MediaContentError res = (MediaContentError)Interop.Storage.GetId(_storageHandle, out id);
                if (res != MediaContentError.None)
                {
                    Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get Id for the Storage");
                }
                return id;
            }
        }

        /// <summary>
        /// The storage path of the media storage
        /// </summary>
        public string Path
        {
            get
            {
                string path;
                MediaContentError res = (MediaContentError)Interop.Storage.GetPath(_storageHandle, out path);
                if (res != MediaContentError.None)
                {
                    Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get Path for the Storage");
                }
                return path;
            }
        }

        /// <summary>
        /// The storage name of the media storage
        /// </summary>
        public string Name
        {
            get
            {
                string name;
                MediaContentError res = (MediaContentError)Interop.Storage.GetName(_storageHandle, out name);
                if (res != MediaContentError.None)
                {
                    Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get Name for the Storage");
                }
                return name;
            }
        }

        /// <summary>
        /// The storage type of the media storage
        /// </summary>
        public ContentStorageType StorageType
        {
            get
            {
                int storageType;
                MediaContentError res = (MediaContentError)Interop.Storage.GetType(_storageHandle, out storageType);
                if (res != MediaContentError.None)
                {
                    Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get type for the Storage");
                }
                return (ContentStorageType)storageType;
            }
        }

        internal Storage(IntPtr _storageHandle)
        {
            this._storageHandle = _storageHandle;
        }

        /// <summary>
        /// Gets the count of media files for the passed filter in the given storage from the media database.
        /// </summary>
        /// <param name="filter">ContentFilter used to match media content from the media database.</param>
        /// <returns>The number of media contents matching the filter passed</returns>
        public override int GetMediaInformationCount(ContentFilter filter)
        {
            int mediaCount;
            IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
            MediaContentError res = (MediaContentError)Interop.Storage.GetMediaCountFromDb(Id, handle, out mediaCount);
            if (res != MediaContentError.None)
            {
                Log.Warn(MediaContentErrorFactory.LogTag, "Failed to get media count for the storage");
            }
            return mediaCount;
        }

        public override void Dispose()
        {
            MediaContentError res = (MediaContentError)Interop.Storage.Destroy(_storageHandle);
            if (res != MediaContentError.None)
            {
                Log.Warn(MediaContentErrorFactory.LogTag, "Failed to dispose the storage");
            }
        }

        /// <summary>
        /// Iterates through the media files with an optional filter in the given storage from the media database.
        /// This function gets all media files associated with the given storage and meeting desired filter option.
        /// If NULL is passed to the filter, no filtering is applied.
        /// </summary>
        /// <param name="filter">ContentFilter used to match media content from the media database.</param>
        /// <returns>List of content media items matching the passed filter</returns>
        public override Task<IEnumerable<MediaInformation>> GetMediaInformationsAsync(ContentFilter filter)
        {
            var tcs = new TaskCompletionSource<IEnumerable<MediaInformation>>();
            List<MediaInformation> mediaContents = new List<MediaInformation>();
            MediaContentError res;
            IntPtr handle = (filter != null) ? filter.Handle : IntPtr.Zero;
            Interop.Storage.MediaInfoCallback callback = (Interop.MediaInformation.SafeMediaInformationHandle mediaHandle, IntPtr data) =>
            {
                Interop.MediaInformation.SafeMediaInformationHandle newHandle;
                res = (MediaContentError)Interop.MediaInformation.Clone(out newHandle, mediaHandle.DangerousGetHandle());
                if (res != MediaContentError.None)
                {
                    throw MediaContentErrorFactory.CreateException(res, "Failed to clone media");
                }
                MediaInformation info = new MediaInformation(newHandle);
                mediaContents.Add(info);
            };
            res = (MediaContentError)Interop.Storage.ForeachMediaFromDb(Id, handle, callback, IntPtr.Zero);
            if (res != MediaContentError.None)
            {
                throw MediaContentErrorFactory.CreateException(res, "Failed to get media information for the storage");
            }
            tcs.TrySetResult(mediaContents);
            return tcs.Task;
        }
    }
}