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