5de39ea62fe0d7c56689ee29e5542a86b76faf22
[profile/tv/apps/native/musicplayer.git] / src / data / category_info.cpp
1 /*
2  * Copyright (c) 2014 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 #include <stdlib.h>
18 #include <string.h>
19 #include <Eina.h>
20 #include "dbg.h"
21 #include "i18n.h"
22 #include "category_info.h"
23
24
25 struct SCategoryInfo {
26         int categoryId;
27         int albumCount;
28         int songCount;
29         Eina_List *albumList;
30         Eina_List *memberList;
31
32         SCategoryInfo() {
33                 memset(this, 0, sizeof(SCategoryInfo));
34         }
35 };
36
37
38 CCategoryInfo::CListMember *CCategoryInfo::m_MemberDuplicate(CListMember *member)
39 {
40         CListMember *item;
41
42         if (!member)
43                 return NULL;
44
45         item = new CListMember;
46         if (!item)
47                 return NULL;
48
49         item->memberId = member->memberId;
50         item->mediaId = t_Clone(member->mediaId);
51
52         return item;
53 }
54
55
56 bool CCategoryInfo::Create(void)
57 {
58         if (m)
59                 return false;
60
61         m = new SCategoryInfo();
62         if (!m) {
63                 _ERR("Mem allocation failed");
64                 return false;
65         }
66
67         if (!CExtNameInfo::Create()) {
68                 delete m;
69                 m = NULL;
70                 return false;
71         }
72
73         return true;
74 }
75
76
77 void CCategoryInfo::Destroy(void)
78 {
79         CListMember *item;
80         void *obj;
81
82         if (!m)
83                 return;
84
85         CExtNameInfo::Destroy();
86
87         eina_list_free(m->albumList);
88         if (m->memberList) {
89                 EINA_LIST_FREE(m->memberList, obj) {
90                         item = (CListMember*)obj;
91                         delete item->mediaId;
92                         delete item;
93                 }
94         }
95
96         delete m;
97         m = NULL;
98 }
99
100
101 int CCategoryInfo::CategoryId(void)
102 {
103         if (!m)
104                 return 0;
105
106         return m->categoryId;
107 }
108
109
110 int CCategoryInfo::AlbumCount(void)
111 {
112         if (!m)
113                 return 0;
114
115         return m->albumCount;
116 }
117
118
119 int CCategoryInfo::SongCount(void)
120 {
121         if (!m)
122                 return 0;
123
124         return m->songCount;
125 }
126
127
128 Eina_List *CCategoryInfo::AlbumList(void)
129 {
130         if (!m)
131                 return NULL;
132
133         return m->albumList;
134 }
135
136
137 Eina_List *CCategoryInfo::MemberList(void)
138 {
139         if (!m)
140                 return NULL;
141
142         return m->memberList;
143 }
144
145
146 bool CCategoryInfo::SetCategoryId(int categoryId)
147 {
148         if (!m)
149                 return false;
150
151         m->categoryId = categoryId;
152
153         return true;
154 }
155
156
157 bool CCategoryInfo::SetAlbumCount(int albumCount)
158 {
159         if (!m)
160                 return false;
161
162         m->albumCount = albumCount;
163
164         return true;
165 }
166
167
168 bool CCategoryInfo::SetSongCount(int songCount)
169 {
170         if (!m)
171                 return false;
172
173         m->songCount = songCount;
174
175         return true;
176 }
177
178
179 bool CCategoryInfo::SetAlbumInfo(CAlbumInfo *albumInfo)
180 {
181         if (!m)
182                 return false;
183
184         m->albumList = eina_list_append(m->albumList, albumInfo);
185
186         return true;
187 }
188
189
190 bool CCategoryInfo::SetMember(CListMember *member)
191 {
192         if (!m)
193                 return false;
194
195         m->memberList = eina_list_append(m->memberList, member);
196
197         return true;
198 }
199
200
201 bool CCategoryInfo::Duplicate(CNameInfo *obj)
202 {
203         if (!m)
204                 return false;
205
206         CCategoryInfo* dst = (CCategoryInfo*)obj;
207
208         if (!CExtNameInfo::Duplicate(obj))
209                 return false;
210
211         if (!SetCategoryId(dst->CategoryId()))
212                 return false;
213         if (!SetAlbumCount(dst->AlbumCount()))
214                 return false;
215         if (!SetSongCount(dst->SongCount()))
216                 return false;
217
218         m->albumList = eina_list_clone(dst->AlbumList());
219
220         CListMember *item, *dupItem;
221         Eina_List *l;
222         void *listObj;
223         EINA_LIST_FOREACH(dst->MemberList(), l, listObj) {
224                 item = (CListMember *)listObj;
225                 dupItem = m_MemberDuplicate(item);
226                 if (!dupItem)
227                         return false;
228                 if (!SetMember(dupItem))
229                         return false;
230         }
231
232         return true;
233 }
234
235
236 CCategoryInfo *CCategoryInfo::FindCategory(Eina_List *list, const char *categoryName)
237 {
238         Eina_List *l;
239         CCategoryInfo *catinfo;
240         void *obj;
241
242         if (!list || !categoryName)
243                 return NULL;
244
245         EINA_LIST_FOREACH(list, l, obj) {
246                 catinfo = (CCategoryInfo*)obj;
247                 if (!strcmp(catinfo->Name(), categoryName))
248                         return catinfo;
249         }
250
251         return NULL;
252 }