5086c9725244e80bdcf7c4e35759aa2b76343493
[profile/tv/apps/native/filebrowser.git] / src / views / BaseView / GengridItemClass.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 <Elementary.h>
18 #include <Eina.h>
19 #include <Ecore.h>
20 #include <aul.h>
21 #include "i18n.h"
22 #include "define.h"
23 #include "common.h"
24 #include "dbg.h"
25 #include <media_content.h>
26 #include "ExtNameInfo.h"
27 #include "DirectoryInfo.h"
28 #include "GengridItemClass.h"
29
30 const char *typestr[] = {
31         N_(""),
32         N_("Photo"),
33         N_("Video"),
34         N_("Music"),
35         N_("Other"),
36         N_("Folder"),
37 };
38
39 void CGengridItemClass::sm_SetTypeimage(Evas_Object *img, int type)
40 {
41         char buf[MAX_LENGTH];
42
43         if (!img)
44                 return;
45
46         switch (type) {
47         case E_GRP_VIDEO:
48                 snprintf(buf, sizeof(buf), "%s/%s", IMAGEDIR,
49                         FBR_IMAGE_NORMAL_VIDEO);
50                 break;
51         case E_GRP_MUSIC:
52                 snprintf(buf, sizeof(buf), "%s/%s", IMAGEDIR,
53                         FBR_IMAGE_NORMAL_MUSIC);
54                 break;
55         default:
56                 snprintf(buf, sizeof(buf), "%s/%s", IMAGEDIR,
57                         FBR_IMAGE_NORMAL_OTHER);
58                 break;
59         }
60
61         elm_image_file_set(img, buf, NULL);
62 }
63
64 Evas_Object *CGengridItemClass::sm_GetIcon(Evas_Object *obj, CExtNameInfo *pInfo)
65 {
66         Evas_Object *icon;
67         char *path;
68
69         path = pInfo->Path();
70         if (!path)
71                 return NULL;
72
73         icon = elm_icon_add(obj);
74         if (!icon)
75                 return NULL;
76
77         elm_need_ethumb();
78         elm_icon_thumb_set(icon, path, NULL);
79         elm_image_aspect_fixed_set(icon, EINA_FALSE);
80
81         return icon;
82 }
83
84 Evas_Object *CGengridItemClass::sm_AddThumbnail(Evas_Object *obj, CExtNameInfo *pInfo)
85 {
86         char *path;
87         char buf[MAX_LENGTH];
88         int type;
89         Evas_Object *img;
90
91         if (!obj || !pInfo)
92                 return NULL;
93
94         type = pInfo->Type();
95         path = pInfo->ThumbnailPath();
96         _DBG("path: %x", path);
97
98         if (!path && type == E_GRP_PHOTO) {
99                 img = sm_GetIcon(obj, pInfo);
100                 return img;
101         }
102
103         img = elm_image_add(obj);
104         if (!img)
105                 return NULL;
106         evas_object_show(img);
107
108         if (path) {
109                 elm_image_file_set(img, path, "NULL");
110                 elm_image_aspect_fixed_set(img, EINA_FALSE);
111                 return img;
112         }
113
114         switch (type) {
115         case E_GRP_FOLDER:
116                 snprintf(buf, sizeof(buf), "%s/%s", IMAGEDIR,
117                         FBR_IMAGE_FOLDER);
118                 break;
119         case E_GRP_VIDEO:
120                 snprintf(buf, sizeof(buf), "%s/%s", IMAGEDIR,
121                         FBR_IMAGE_DEFAULT_VIDEO);
122                 break;
123         case E_GRP_MUSIC:
124                 snprintf(buf, sizeof(buf), "%s/%s", IMAGEDIR,
125                         FBR_IMAGE_DEFAULT_MUSIC);
126                 break;
127         case E_GRP_OTHER:
128                 snprintf(buf, sizeof(buf), "%s/%s", IMAGEDIR,
129                         FBR_IMAGE_DEFAULT_OTHER);
130                 break;
131         }
132         elm_image_no_scale_set(img, EINA_TRUE);
133         elm_image_aspect_fixed_set(img, EINA_TRUE);
134         elm_image_file_set(img, buf, NULL);
135         return img;
136 }
137
138
139 char *CGengridItemClass::sm_CbTextGet(void *data, Evas_Object *obj, const char *part)
140 {
141         if (!data || !obj)
142                 return NULL;
143
144         SItemInfo *itinfo = (SItemInfo *)data;
145         CExtNameInfo *pInfo  = itinfo->pInfo;
146
147         char *txt;
148         char buf[MAX_LENGTH];
149         int count;
150         int type;
151
152         if (!strcmp(part, "elm.text")) {
153                 txt = pInfo->Name();
154                 if (txt)
155                         return strdup(txt);
156         }
157         else if (!strcmp(part, "elm.text1")) {
158                 type = pInfo->Type();
159                 if (type == E_GRP_FOLDER) {
160                         CDirectoryInfo* pDirInfo = (CDirectoryInfo*)pInfo;
161                         count = pDirInfo->Count();
162                         snprintf(buf, sizeof(buf), "%d", count);
163                         return strdup(buf);
164                 }
165                 else
166                         return strdup(typestr[type]);
167         }
168
169         return NULL;
170 }
171
172 Evas_Object *CGengridItemClass::sm_CbContentGet(void *data, Evas_Object *obj, const char *part)
173 {
174         CExtNameInfo *pInfo;
175         SItemInfo *itinfo;
176         Evas_Object *img;
177         int type;
178         char *path;
179
180         if (!data || !obj)
181                 return NULL;
182
183         itinfo = (SItemInfo *)data;
184         pInfo = itinfo->pInfo;
185
186         if (!strcmp(part, "elm.swallow.icon")) {
187                 img = sm_AddThumbnail(obj, pInfo);
188                 return img;
189         }
190         else if (!strcmp(part, "elm.swallow.icon1")) {
191                 type = pInfo->Type();
192                 if (type == E_GRP_FOLDER || type == E_GRP_PHOTO)
193                         return NULL;
194
195                 path = pInfo->ThumbnailPath();
196                 if (!path)
197                         return NULL;
198
199                 img = elm_image_add(obj);
200                 if (!img)
201                         return NULL;
202
203                 sm_SetTypeimage(img, type);
204                 elm_image_no_scale_set(img, EINA_TRUE);
205                 evas_object_show(img);
206                 return img;
207         }
208
209         return NULL;
210 }
211
212 void CGengridItemClass::sm_CbRemove(void *data, Evas_Object *obj)
213 {
214         free(data);
215 }
216
217 bool CGengridItemClass::Create(void)
218 {
219         if (m_pItemClass)
220                 return false;
221
222         m_pItemClass = elm_gengrid_item_class_new();
223         if (!m_pItemClass) {
224                 return false;
225         }
226
227         m_pItemClass->item_style = FBR_STYLE_MEDIA_GRID;
228         m_pItemClass->func.text_get    = sm_CbTextGet;
229         m_pItemClass->func.content_get = sm_CbContentGet;
230         m_pItemClass->func.state_get   = NULL;
231         m_pItemClass->func.del         = sm_CbRemove;
232
233         return true;
234 }
235
236 void CGengridItemClass::Destroy(void)
237 {
238         if (!m_pItemClass)
239                 return;
240
241         elm_gengrid_item_class_free(m_pItemClass);
242         m_pItemClass = NULL;
243 }
244
245 Elm_Gengrid_Item_Class* CGengridItemClass::Handle(void)
246 {
247         return m_pItemClass;
248 }