Tizen 2.1 base
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / tizen / OpenPanel.cpp
1 /*
2    Copyright (C) 2012 Samsung Electronics
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include "ewk_view.h"
22 #include "OpenPanel.h"
23 #include "WKString.h"
24 #include <Ecore_X.h>
25 #include <ui-gadget.h>
26
27 #if OS(TIZEN)
28
29 namespace WebKit {
30
31 OpenPanel::OpenPanel(Evas_Object* ewkView)
32     : m_ewkView(ewkView)
33 {
34 }
35
36 OpenPanel::~OpenPanel()
37 {
38     close();
39 }
40
41 static void layout_cb(ui_gadget_h ug, enum ug_mode mode, void* priv)
42 {
43         Evas_Object* base, *win;
44
45         if (!ug || !priv)
46                 return;
47
48         base = (Evas_Object*)ug_get_layout(ug);
49         if (!base)
50                 return;
51
52         win = (Evas_Object*)ug_get_window();
53
54         switch (mode)
55         {
56                 case UG_MODE_FULLVIEW:
57                     evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
58                     elm_win_resize_object_add(win, base);
59                     evas_object_show(base);
60                     break;
61                 default:
62                     break;
63         }
64 }
65
66 static void result_cb(ui_gadget_h ug, service_h service, void* priv)
67 {
68     if (ug == NULL || priv == NULL)
69         return;
70
71     OpenPanel* openPanel = (OpenPanel*)priv;
72     Evas_Object* ewk_view = openPanel->ewkView();
73     if (!ewk_view)
74         return;
75
76     Eina_List* selectedFiles = NULL;
77     unsigned int i = 0;
78     char* filename = NULL;
79
80     service_get_extra_data(service, "result", &filename);
81
82     if (filename && strlen(filename)) {
83         char* filelist = strdup(filename);
84         char* file = NULL;
85         file = strtok(filelist, "?");
86         int stringlength = 0;
87         do {
88             stringlength = 0;
89             if ((stringlength = strlen(file)) > 0) {
90                 char* buffer = new char[stringlength+1];
91                 memset(buffer, 0x00, sizeof(buffer));
92                 strncpy(buffer, file, strlen(file));
93                 buffer[stringlength] = 0x00;
94                 selectedFiles = eina_list_append(selectedFiles, buffer);
95             }
96             i++;
97         }while((file = strtok(NULL, "?")));
98         free(filelist);
99     } else {
100         return;
101     }
102     ewk_view_open_panel_reply(ewk_view, selectedFiles, EINA_TRUE);
103     return;
104 }
105
106 static void destroy_cb(ui_gadget_h ug, void* priv)
107 {
108     ug_destroy(ug);
109
110     OpenPanel* openPanel = (OpenPanel*)priv;
111     Evas_Object* ewk_view = openPanel->ewkView();
112     if (!ewk_view)
113                     return;
114
115     ewk_view_open_panel_reply(ewk_view, NULL, EINA_FALSE);
116     return;
117 }
118
119 static void _cameraResultCb(service_h request, service_h reply, service_result_e result, void* data)
120 {
121     OpenPanel* openPanel = static_cast<OpenPanel*>(data);
122
123     if (result == SERVICE_RESULT_SUCCEEDED) {
124         char* resultFilename = 0;
125         int ret = service_get_extra_data(reply, SERVICE_DATA_SELECTED, &resultFilename);
126         if (ret != SERVICE_ERROR_NONE)
127             return;
128
129         if (resultFilename) {
130             Eina_List* list = 0;
131             int len = strlen(resultFilename) + 1;
132             char* fileName = static_cast<char*>(malloc(sizeof(char) * len));
133             memset(fileName, 0, sizeof(char) * len);
134             strcpy(fileName, resultFilename);
135             list = eina_list_append(list, fileName);
136             ewk_view_open_panel_reply(openPanel->ewkView(), list, true);
137         } else {
138             ewk_view_open_panel_reply(openPanel->ewkView(), 0, false);
139         }
140     } else {
141         ewk_view_open_panel_reply(openPanel->ewkView(), 0, false);
142     }
143 }
144
145 bool OpenPanel::_capturePicture()
146 {
147     service_h svcHandle = 0;
148     if (service_create(&svcHandle) < 0 || !svcHandle)
149         return false;
150
151     service_set_window(svcHandle, ecore_x_window_root_first_get());
152     service_set_operation(svcHandle, SERVICE_OPERATION_CREATE_CONTENT);
153     service_set_mime(svcHandle, "image/jpg");
154     service_add_extra_data(svcHandle, "CALLER", "webkit");
155
156     int ret = service_send_launch_request(svcHandle, _cameraResultCb, this);
157     if (ret != SERVICE_ERROR_NONE) {
158         service_destroy(svcHandle);
159         return false;
160     }
161
162     service_destroy(svcHandle);
163
164     return true;
165 }
166
167 bool OpenPanel::_recordVideo()
168 {
169     service_h svcHandle = 0;
170     if (service_create(&svcHandle) < 0 || !svcHandle)
171         return false;
172
173     service_set_window(svcHandle, ecore_x_window_root_first_get());
174     service_set_operation(svcHandle, SERVICE_OPERATION_CREATE_CONTENT);
175     service_set_mime(svcHandle, "video/3gp");
176     service_add_extra_data(svcHandle, "CALLER", "webkit");
177
178     int ret = service_send_launch_request(svcHandle, _cameraResultCb, this);
179     if (ret != SERVICE_ERROR_NONE) {
180         service_destroy(svcHandle);
181         return false;
182     }
183
184     service_destroy(svcHandle);
185
186     return true;
187 }
188
189 bool OpenPanel::_recordVoice()
190 {
191     service_h svcHandle = 0;
192     if (service_create(&svcHandle) < 0 || !svcHandle)
193         return false;
194
195     service_set_operation(svcHandle, SERVICE_OPERATION_CREATE_CONTENT);
196     service_set_mime(svcHandle, "audio/amr");
197
198     int ret = service_send_launch_request(svcHandle, _cameraResultCb, this);
199     if (ret != SERVICE_ERROR_NONE) {
200         service_destroy(svcHandle);
201         return false;
202     }
203
204     service_destroy(svcHandle);
205
206     return true;
207 }
208
209 bool OpenPanel::openPanel(Evas_Object* ewkView, Eina_Bool allow_multiple_files, Eina_List* accepted_mime_types, const char* capture, void* userData)
210 {
211     unsigned int i, n;
212
213     Eina_Bool is_audio_extension_type = EINA_FALSE;
214     Eina_Bool is_video_extension_type = EINA_FALSE;
215     Eina_Bool is_image_extension_type = EINA_FALSE;
216     Eina_Bool is_audio_all_type = EINA_FALSE;
217     Eina_Bool is_video_all_type = EINA_FALSE;
218     Eina_Bool is_image_all_type = EINA_FALSE;
219
220     n = eina_list_count(accepted_mime_types);
221
222     char extensions[1024] = {0, };
223     char* accept_type = NULL;
224
225     /* set accept type */
226     for (i = 0; i < n; i++) {
227         accept_type = (char*)eina_list_nth(accepted_mime_types, i);
228         if (strncmp(accept_type, "audio/*", strlen("audio/*")) == 0) {
229             is_audio_all_type = EINA_TRUE;
230         } else if (strncmp(accept_type, "video/*", strlen("video/*")) == 0) {
231             is_video_all_type = EINA_TRUE;
232         } else if (strncmp(accept_type, "image/*", strlen("image/*")) == 0) {
233             is_image_all_type = EINA_TRUE;
234         } else if (strncmp(accept_type, "audio/", strlen("audio/")) == 0) {
235             is_audio_extension_type = EINA_TRUE;
236             accept_type = strchr(accept_type, '/') + 1;
237             strncat(extensions, accept_type, strlen(accept_type));
238             strncat(extensions, ";", 1);
239         } else if (strncmp(accept_type, "video/", strlen("video/")) == 0) {
240             is_video_extension_type = EINA_TRUE;
241             accept_type = strchr(accept_type, '/') + 1;
242             strncat(extensions, accept_type, strlen(accept_type));
243             strncat(extensions, ";", 1);
244         } else if (strncmp(accept_type, "image/", strlen("image/")) == 0) {
245             is_image_extension_type = EINA_TRUE;
246             accept_type = strchr(accept_type, '/') + 1;
247             strncat(extensions, accept_type, strlen(accept_type));
248             strncat(extensions, ";", 1);
249         }
250     }
251
252     if (capture) {
253         if (accept_type == NULL) {
254             if (!strcmp(capture, "camera"))
255                 return _capturePicture();
256             else if (!strcmp(capture, "camcorder"))
257                 return _recordVideo();
258             else if (!strcmp(capture, "microphone"))
259                 return _recordVoice();
260         } else {
261             if (strstr(accept_type, "image"))
262                 return _capturePicture();
263             else if (strstr(accept_type, "video"))
264                 return _recordVideo();
265             else if (strstr(accept_type, "audio"))
266                 return _recordVoice();
267         }
268     }
269
270     service_h service = NULL;
271     if (service_create(&service) != SERVICE_ERROR_NONE)
272         return false;
273     service_add_extra_data(service, "path", "/opt/media");
274
275     if (is_audio_all_type && !is_video_all_type && !is_image_all_type && !is_video_extension_type && !is_image_extension_type) {
276         service_add_extra_data(service, "file_type", "SOUND");
277     } else if (is_video_all_type && !is_image_all_type && !is_audio_all_type && !is_image_extension_type && !is_audio_extension_type) {
278         service_add_extra_data(service, "file_type", "VIDEO");
279     } else if (is_image_all_type && !is_audio_all_type && !is_video_all_type && !is_audio_extension_type && !is_video_extension_type) {
280         service_add_extra_data(service, "file_type", "IMAGE");
281     } else if (is_audio_all_type && is_video_all_type && !is_image_all_type && !is_image_extension_type) {
282         service_add_extra_data(service, "file_type", "VS");
283     } else if (is_video_all_type && is_image_all_type && !is_audio_all_type && !is_audio_extension_type) {
284         service_add_extra_data(service, "file_type", "IV");
285     } else if (is_image_all_type && is_audio_all_type && !is_video_all_type && !is_video_extension_type) {
286         service_add_extra_data(service, "file_type", "IS");
287     } else if (!is_audio_all_type && !is_video_all_type && !is_image_all_type &&
288                 (is_video_extension_type || is_image_extension_type || is_audio_extension_type)) {
289         service_add_extra_data(service, "file_type", extensions);
290     } else {
291         service_add_extra_data(service, "file_type", "ALL");
292     }
293
294     /* Set file select mode : multiple/single */
295     if (allow_multiple_files == TRUE){
296         service_add_extra_data(service, "select_type", "MULTI_FILE");
297     } else {
298         service_add_extra_data(service, "select_type", "SINGLE_FILE");
299     }
300
301     struct ug_cbs cbs = {0, 0, 0, 0};
302     cbs.layout_cb = layout_cb;
303     cbs.result_cb = result_cb;
304     cbs.destroy_cb = destroy_cb;
305     cbs.priv = (void*)this;
306
307     if(!ug_create(NULL, "myfile-efl", UG_MODE_FULLVIEW, service, &cbs))
308         return false;
309
310     service_destroy(service);
311
312     return true;
313 }
314
315
316 Evas_Object* OpenPanel::ewkView()
317 {
318     return m_ewkView;
319 }
320
321 void OpenPanel::close()
322 {
323 }
324
325 } // namespace WebKit
326
327 #endif // OS(TIZEN)
328