Revert "Apply ASSERT"
[profile/tv/apps/native/filebrowser.git] / src / data / DirectoryInfo.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 "dbg.h"
20 #include "i18n.h"
21 #include "common.h"
22 #include <media_content.h>
23
24 #include "ExtNameInfo.h"
25 #include "DirectoryInfo.h"
26
27
28 struct SDirectoryInfo {
29         int nCount;
30
31         filter_h filter;
32         media_folder_h folder;
33 };
34
35
36 bool CDirectoryInfo::Create(CNameInfo::EClassType type)
37 {
38         if (m)
39                 return false;
40
41         m = new SDirectoryInfo;
42         if (!m)
43                 return false;
44
45         if (!CExtNameInfo::Create(CLASS_TYPE_DIRECTORY)) {
46                 delete m;
47                 m = NULL;
48                 return false;
49         }
50
51         return true;
52 }
53
54
55 void CDirectoryInfo::Destroy(void)
56 {
57         if (!m)
58                 return;
59
60         CExtNameInfo::Destroy();
61
62         delete m;
63         m = NULL;
64 }
65
66
67 bool CDirectoryInfo::SetCount(int nCount)
68 {
69         if (!m)
70                 return false;
71
72         m->nCount = nCount;
73
74         return true;
75 }
76
77
78 int CDirectoryInfo::Count(void)
79 {
80         if (!m)
81                 return 0;
82
83         return m->nCount;
84 }
85
86
87 bool CDirectoryInfo::Duplicate(CNameInfo *obj)
88 {
89         if (!m)
90                 return false;
91
92         CDirectoryInfo *dst = (CDirectoryInfo*)obj;
93
94         if (!CExtNameInfo::Duplicate(obj))
95                 return false;
96
97         if (!SetCount(dst->Count()))
98                 return false;
99
100         return true;
101 }
102
103
104 static bool gFolderSize(media_info_h media_h, void *dt)
105 {
106         long long unsigned int size;
107         long long unsigned int *total_size;
108
109         if (!dt)
110                 return false;
111
112         total_size = (long long unsigned int *)dt;
113
114         if (media_info_get_size(media_h, &size) == MEDIA_CONTENT_ERROR_NONE)
115                 *total_size = *total_size + size;
116
117         return true;
118 }
119
120
121 bool CDirectoryInfo::SetLoadInfo(filter_h filter, media_folder_h folder)
122 {
123         if (!m)
124                 return false;
125
126         m->filter = filter;
127         m->folder = folder;
128
129         return true;
130 }
131
132
133 bool CDirectoryInfo::Load(void)
134 {
135         if (!m)
136                 return false;
137
138         int r;
139         bool ret = false;
140         do {
141                 char* str;
142                 r = media_folder_get_name(m->folder, &str);
143                 if (r != MEDIA_CONTENT_ERROR_NONE) {
144                         _ERR("Media folder get name failed");
145                         break;
146                 }
147                 SetName(str);
148                 free(str);
149
150                 r = media_folder_get_path(m->folder, &str);
151                 if (r != MEDIA_CONTENT_ERROR_NONE) {
152                         _ERR("Media folder get path failed");
153                         break;
154                 }
155                 SetPath(str);
156                 free(str);
157
158                 r = media_folder_get_folder_id(m->folder, &str);
159                 if (r != MEDIA_CONTENT_ERROR_NONE) {
160                         _ERR("Media folder get id failed");
161                         break;
162                 }
163                 SetId(str);
164                 free(str);
165
166                 int count = 0;
167                 r = media_folder_get_media_count_from_db(Id(), m->filter, &count);
168                 if (r != MEDIA_CONTENT_ERROR_NONE) {
169                         _ERR("Media folder get id failed");
170                         break;
171                 }
172                 SetCount(count);
173
174                 SetType(E_GRP_FOLDER);
175
176                 TTime time;
177                 r = media_folder_get_modified_time(m->folder, &time);
178                 if (r != MEDIA_CONTENT_ERROR_NONE) {
179                         _ERR("Media folder get mod time failed");
180                         break;
181                 }
182                 SetModifiedTime(time);
183
184                 TSize size;
185                 r = media_folder_foreach_media_from_db(Id(), m->filter, gFolderSize, &size);
186                 if (r != MEDIA_CONTENT_ERROR_NONE) {
187                         _ERR("Media folder get size failed");
188                         break;
189                 }
190
191                 SetSize(size);
192
193                 ret = true;
194         } while (0);
195
196         return ret;
197 }