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