The color space that is used by thumbnail server is changed to RGB, because gdk suppo...
[platform/core/multimedia/libmedia-thumbnail.git] / src / ipc / media-thumb-ipc.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "media-thumbnail.h"
23 #include "media-thumb-ipc.h"
24 #include "media-thumb-util.h"
25 #include "media-thumb-db.h"
26 #include <glib.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <grp.h>
31 #include <pwd.h>
32
33 #define GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
34
35 static __thread GQueue *g_request_queue = NULL;
36 typedef struct {
37         GIOChannel *channel;
38         char *path;
39         int source_id;
40 } thumbReq;
41
42 int
43 _media_thumb_create_socket(int sock_type, int *sock)
44 {
45         int sock_fd = 0;
46
47         if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
48                 thumb_err("socket failed: %s", strerror(errno));
49                 return MEDIA_THUMB_ERROR_NETWORK;
50         }
51
52         if (sock_type == CLIENT_SOCKET) {
53
54 #ifdef _USE_MEDIA_UTIL_
55                 struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
56 #else
57                 struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
58 #endif
59
60                 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
61                         thumb_err("setsockopt failed: %s", strerror(errno));
62                         close(sock_fd);
63                         return MEDIA_THUMB_ERROR_NETWORK;
64                 }
65         } else if (sock_type == SERVER_SOCKET) {
66
67                 int n_reuse = 1;
68
69                 if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) {
70                         thumb_err("setsockopt failed: %s", strerror(errno));
71                         close(sock_fd);
72                         return MEDIA_THUMB_ERROR_NETWORK;
73                 }
74         }
75
76         *sock = sock_fd;
77
78         return MEDIA_THUMB_ERROR_NONE;
79 }
80
81
82 int
83 _media_thumb_create_udp_socket(int *sock)
84 {
85         int sock_fd = 0;
86
87         if ((sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
88                 thumb_err("socket failed: %s", strerror(errno));
89                 return MEDIA_THUMB_ERROR_NETWORK;
90         }
91
92 #ifdef _USE_MEDIA_UTIL_
93         struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
94 #else
95         struct timeval tv_timeout = { TIMEOUT_SEC, 0 };
96 #endif
97
98         if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
99                 thumb_err("setsockopt failed: %s", strerror(errno));
100                 close(sock_fd);
101                 return MEDIA_THUMB_ERROR_NETWORK;
102         }
103
104         *sock = sock_fd;
105
106         return MEDIA_THUMB_ERROR_NONE;
107 }
108
109 int _media_thumb_get_error()
110 {
111         if (errno == EWOULDBLOCK) {
112                 thumb_err("Timeout. Can't try any more");
113                 return MEDIA_THUMB_ERROR_TIMEOUT;
114         } else {
115                 thumb_err("recvfrom failed : %s", strerror(errno));
116                 return MEDIA_THUMB_ERROR_NETWORK;
117         }
118 }
119
120 int __media_thumb_pop_req_queue(const char *path, bool shutdown_channel)
121 {
122         int req_len = 0, i;
123
124         if (g_request_queue == NULL) return -1;
125         req_len = g_queue_get_length(g_request_queue);
126
127 //      thumb_dbg("Queue length : %d", req_len);
128 //      thumb_dbg("Queue path : %s", path);
129
130         if (req_len <= 0) {
131 //              thumb_dbg("There is no request in the queue");
132         } else {
133
134                 for (i = 0; i < req_len; i++) {
135                         thumbReq *req = NULL;
136                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
137                         if (req == NULL) continue;
138
139                         if (strncmp(path, req->path, strlen(path)) == 0) {
140                                 //thumb_dbg("Popped %s", path);
141                                 if (shutdown_channel) {
142                                         g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id));
143
144                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
145                                         g_io_channel_unref(req->channel);
146                                 }
147                                 g_queue_pop_nth(g_request_queue, i);
148
149                                 SAFE_FREE(req->path);
150                                 SAFE_FREE(req);
151
152                                 break;
153                         }
154                 }
155                 if (i == req_len) {
156 //                      thumb_dbg("There's no %s in the queue", path);
157                 }
158         }
159
160         return MEDIA_THUMB_ERROR_NONE;
161 }
162
163 int __media_thumb_check_req_queue(const char *path)
164 {
165         int req_len = 0, i;
166
167         if (g_request_queue == NULL) return MEDIA_THUMB_ERROR_NONE;
168         req_len = g_queue_get_length(g_request_queue);
169
170 //      thumb_dbg("Queue length : %d", req_len);
171 //      thumb_dbg("Queue path : %s", path);
172
173         if (req_len <= 0) {
174 //              thumb_dbg("There is no request in the queue");
175         } else {
176
177                 for (i = 0; i < req_len; i++) {
178                         thumbReq *req = NULL;
179                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
180                         if (req == NULL) continue;
181
182                         if (strncmp(path, req->path, strlen(path)) == 0) {
183                                 //thumb_dbg("Same Request - %s", path);
184                                 return -1;
185
186                                 break;
187                         }
188                 }
189         }
190
191         return MEDIA_THUMB_ERROR_NONE;
192 }
193
194 int
195 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
196 {
197         int recv_msg_len = 0;
198         unsigned char *buf = NULL;
199
200         buf = (unsigned char*)malloc(header_size);
201
202         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
203                 thumb_err("recvfrom failed : %s", strerror(errno));
204                 SAFE_FREE(buf);
205                 return _media_thumb_get_error();
206         }
207
208         memcpy(msg, buf, header_size);
209         //thumb_dbg("origin_path_size : %d, dest_path_size : %d", msg->origin_path_size, msg->dest_path_size);
210
211         SAFE_FREE(buf);
212
213         buf = (unsigned char*)malloc(msg->origin_path_size);
214
215         if ((recv_msg_len = recv(sock, buf, msg->origin_path_size, 0)) < 0) {
216                 thumb_err("recvfrom failed : %s", strerror(errno));
217                 SAFE_FREE(buf);
218                 return _media_thumb_get_error();
219         }
220
221         strncpy(msg->org_path, (char*)buf, msg->origin_path_size);
222         //thumb_dbg("original path : %s", msg->org_path);
223
224         SAFE_FREE(buf);
225         buf = (unsigned char*)malloc(msg->dest_path_size);
226
227         if ((recv_msg_len = recv(sock, buf, msg->dest_path_size, 0)) < 0) {
228                 thumb_err("recvfrom failed : %s", strerror(errno));
229                 SAFE_FREE(buf);
230                 return _media_thumb_get_error();
231         }
232
233         strncpy(msg->dst_path, (char*)buf, msg->dest_path_size);
234         //thumb_dbg("destination path : %s", msg->dst_path);
235
236         SAFE_FREE(buf);
237         return MEDIA_THUMB_ERROR_NONE;
238 }
239
240 #ifdef _USE_UDS_SOCKET_
241 int
242 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
243 #else
244 int
245 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_in *from_addr, unsigned int *from_size)
246 #endif
247 {
248         int recv_msg_len = 0;
249 #ifdef _USE_UDS_SOCKET_
250         unsigned int from_addr_size = sizeof(struct sockaddr_un);
251 #else
252         unsigned int from_addr_size = sizeof(struct sockaddr_in);
253 #endif
254         unsigned char *buf = NULL;
255
256         buf = (unsigned char*)malloc(sizeof(thumbMsg));
257
258         if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
259                 thumb_err("recvfrom failed : %s", strerror(errno));
260                 SAFE_FREE(buf);
261                 return _media_thumb_get_error();
262         }
263
264         memcpy(msg, buf, header_size);
265
266         if (msg->origin_path_size <= 0  || msg->origin_path_size > MAX_PATH_SIZE) {
267                 SAFE_FREE(buf);
268                 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size );
269                 return MEDIA_THUMB_ERROR_NETWORK;
270         }
271
272         strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
273         //thumb_dbg("original path : %s", msg->org_path);
274
275         if (msg->dest_path_size <= 0  || msg->dest_path_size > MAX_PATH_SIZE) {
276                 SAFE_FREE(buf);
277                 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size );
278                 return MEDIA_THUMB_ERROR_NETWORK;
279         }
280
281         strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
282         //thumb_dbg("destination path : %s", msg->dst_path);
283
284         SAFE_FREE(buf);
285         *from_size = from_addr_size;
286
287         return MEDIA_THUMB_ERROR_NONE;
288 }
289
290 int
291 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
292 {
293         if (req_msg == NULL || buf == NULL) {
294                 return -1;
295         }
296
297         int org_path_len = 0;
298         int dst_path_len = 0;
299         int size = 0;
300         int header_size = 0;
301
302         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
303         org_path_len = strlen(req_msg->org_path) + 1;
304         dst_path_len = strlen(req_msg->dst_path) + 1;
305
306         //thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d]", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len);
307
308         size = header_size + org_path_len + dst_path_len;
309         *buf = malloc(size);
310         memcpy(*buf, req_msg, header_size);
311         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
312         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
313
314         *buf_size = size;
315
316         return 0;
317 }
318
319 int
320 _media_thumb_request(int msg_type, media_thumb_type thumb_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
321 {
322         int sock;
323 #ifdef _USE_UDS_SOCKET_
324         struct sockaddr_un serv_addr;
325 #elif defined(_USE_UDS_SOCKET_TCP_)
326         struct sockaddr_un serv_addr;
327 #else
328         const char *serv_ip = "127.0.0.1";
329         struct sockaddr_in serv_addr;
330 #endif
331
332         int recv_str_len = 0;
333         int err;
334         int pid;
335
336
337 #ifdef _USE_MEDIA_UTIL_
338 #ifdef _USE_UDS_SOCKET_
339         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
340 #elif defined(_USE_UDS_SOCKET_TCP_)
341         if (ms_ipc_create_client_tcp_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_TCP_PORT) < 0) {
342 #else
343         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
344 #endif
345                 thumb_err("ms_ipc_create_client_socket failed");
346                 return MEDIA_THUMB_ERROR_NETWORK;
347         }
348 #else
349         /* Creaete a TCP socket */
350         if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
351                 thumb_err("_media_thumb_create_socket failed");
352                 return MEDIA_THUMB_ERROR_NETWORK;
353         }
354 #endif
355
356         memset(&serv_addr, 0, sizeof(serv_addr));
357 #ifdef _USE_UDS_SOCKET_
358         serv_addr.sun_family = AF_UNIX;
359 #elif defined(_USE_UDS_SOCKET_TCP_)
360         serv_addr.sun_family = AF_UNIX;
361 #else
362         serv_addr.sin_family = AF_INET;
363         serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
364 #endif
365
366 #ifdef _USE_MEDIA_UTIL_
367 #ifdef _USE_UDS_SOCKET_
368         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
369 #elif defined(_USE_UDS_SOCKET_TCP_)
370         thumb_dbg("");
371         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
372 #else
373         serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
374 #endif
375 #else
376         serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
377 #endif
378
379         /* Connecting to the thumbnail server */
380         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
381                 thumb_err("connect error : %s", strerror(errno));
382                 return MEDIA_THUMB_ERROR_NETWORK;
383         }
384
385         thumbMsg req_msg;
386         thumbMsg recv_msg;
387
388         memset((void *)&req_msg, 0, sizeof(thumbMsg));
389         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
390
391         /* Get PID of client*/
392         pid = getpid();
393         req_msg.pid = pid;
394
395         /* Set requset message */
396         req_msg.msg_type = msg_type;
397         req_msg.thumb_type = thumb_type;
398         req_msg.uid = uid;
399         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
400         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
401
402         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
403                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
404                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
405         }
406
407         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
408         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
409
410         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
411                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
412                 close(sock);
413                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
414         }
415
416         unsigned char *buf = NULL;
417         int buf_size = 0;
418         int header_size = 0;
419
420         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
421         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
422
423         if (send(sock, buf, buf_size, 0) != buf_size) {
424                 thumb_err("sendto failed: %d\n", errno);
425                 SAFE_FREE(buf);
426                 close(sock);
427                 return MEDIA_THUMB_ERROR_NETWORK;
428         }
429
430         thumb_dbg("Sending msg to thumbnail daemon is successful");
431
432         SAFE_FREE(buf);
433
434         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
435                 thumb_err("_media_thumb_recv_msg failed ");
436                 close(sock);
437                 return err;
438         }
439
440         recv_str_len = strlen(recv_msg.org_path);
441         thumb_dbg("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
442
443         close(sock);
444
445         if (recv_str_len > max_length) {
446                 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
447                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
448         }
449
450         if (recv_msg.status == THUMB_FAIL) {
451                 thumb_err("Failed to make thumbnail");
452                 return -1;
453         }
454
455         if (msg_type != THUMB_REQUEST_SAVE_FILE) {
456                 strncpy(thumb_path, recv_msg.dst_path, max_length);
457         }
458
459         thumb_info->origin_width = recv_msg.origin_width;
460         thumb_info->origin_height = recv_msg.origin_height;
461
462         return 0;
463 }
464
465 static int _mkdir(const char *dir, mode_t mode) {
466         char tmp[256];
467         char *p = NULL;
468         size_t len;
469
470         snprintf(tmp, sizeof(tmp),"%s",dir);
471         len = strlen(tmp);
472         if(tmp[len - 1] == '/')
473                 tmp[len - 1] = 0;
474         for(p = tmp + 1; *p; p++)
475                 if(*p == '/') {
476                         *p = 0;
477                         mkdir(tmp, mode);
478                         *p = '/';
479                 }
480         return mkdir(tmp, mode);
481 }
482
483 static char* _media_thumb_get_default_path(uid_t uid)
484 {
485         char *result_psswd = NULL;
486         struct group *grpinfo = NULL;
487         if(uid == getuid())
488         {
489                 result_psswd = strdup(THUMB_DEFAULT_PATH);
490                 grpinfo = getgrnam("users");
491                 if(grpinfo == NULL) {
492                         thumb_err("getgrnam(users) returns NULL !");
493                         return NULL;
494                 }
495         }
496         else
497         {
498                 struct passwd *userinfo = getpwuid(uid);
499                 if(userinfo == NULL) {
500                         thumb_err("getpwuid(%d) returns NULL !", uid);
501                         return NULL;
502                 }
503                 grpinfo = getgrnam("users");
504                 if(grpinfo == NULL) {
505                         thumb_err("getgrnam(users) returns NULL !");
506                         return NULL;
507                 }
508                 // Compare git_t type and not group name
509                 if (grpinfo->gr_gid != userinfo->pw_gid) {
510                         thumb_err("UID [%d] does not belong to 'users' group!", uid);
511                         return NULL;
512                 }
513                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
514         }
515
516         /* create dir */
517         _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
518
519         return result_psswd;
520 }
521
522 int
523 _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg, uid_t uid)
524 {
525         int err = -1;
526         GdkPixbuf *gdkdata = NULL;
527         int thumb_size = 0;
528         int thumb_w = 0;
529         int thumb_h = 0;
530         int origin_w = 0;
531         int origin_h = 0;
532         int max_length = 0;
533         char *thumb_path = NULL;
534         int need_update_db = 0;
535         int alpha = 0;
536
537         if (req_msg == NULL || res_msg == NULL) {
538                 thumb_err("Invalid msg!");
539                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
540         }
541
542         int msg_type = req_msg->msg_type;
543         media_thumb_type thumb_type = req_msg->thumb_type;
544         const char *origin_path = req_msg->org_path;
545
546         // Currently, The color space that is supported by the gdk-pixbuf is only RGB.
547         media_thumb_format thumb_format = MEDIA_THUMB_RGB888;
548
549         thumb_path = res_msg->dst_path;
550         thumb_path[0] = '\0';
551         max_length = sizeof(res_msg->dst_path);
552
553         err = _media_thumb_db_connect(uid);
554         if (err < 0) {
555                 thumb_err("_media_thumb_mb_svc_connect failed: %d", err);
556                 return MEDIA_THUMB_ERROR_DB;
557         }
558
559         if (msg_type == THUMB_REQUEST_DB_INSERT) {
560                 err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h);
561                 if (err == 0) {
562                         res_msg->origin_width = origin_w;
563                         res_msg->origin_height = origin_h;
564                         _media_thumb_db_disconnect();
565                         return MEDIA_THUMB_ERROR_NONE;
566                 } else {
567                         if (strlen(thumb_path) == 0) {
568                                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
569                                 if (err < 0) {
570                                         thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
571                                         strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
572                                         _media_thumb_db_disconnect();
573                                         return err;
574                                 }
575
576                                 thumb_path[strlen(thumb_path)] = '\0';
577                         }
578                 }
579
580         } else if (msg_type == THUMB_REQUEST_SAVE_FILE) {
581                 strncpy(thumb_path, req_msg->dst_path, max_length);
582
583         } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) {
584                 err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid);
585                 if (err < 0) {
586                         thumb_err("_media_thumb_get_hash_name failed - %d\n", err);
587                         strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
588                         _media_thumb_db_disconnect();
589                         return err;
590                 }
591
592                 thumb_path[strlen(thumb_path)] = '\0';
593         }
594
595         if (g_file_test(thumb_path, 
596                                         G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
597                 thumb_warn("thumb path already exists in file system.. remove the existed file");
598                 _media_thumb_remove_file(thumb_path);
599         }
600
601         err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &gdkdata, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, uid);
602         if (err < 0) {
603                 thumb_err("_thumbnail_get_data failed - %d\n", err);
604                 if ( gdkdata != NULL ){
605                         g_object_unref(gdkdata);
606                 }
607                 strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
608                 _media_thumb_db_disconnect();
609                 return err;
610         }
611
612         //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h);
613         //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h);
614         //thumb_dbg("Thumb : %s", thumb_path);
615
616         res_msg->msg_type = THUMB_RESPONSE;
617         res_msg->thumb_size = thumb_size;
618         res_msg->thumb_width = thumb_w;
619         res_msg->thumb_height = thumb_h;
620         res_msg->origin_width = origin_w;
621         res_msg->origin_height = origin_h;
622
623         /* If the image is transparent PNG format, make png file as thumbnail of this image */
624         if (alpha) {
625                 char file_ext[10];
626                 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
627                 if (strncasecmp(file_ext, "png", 3) == 0) {
628                         int len = strlen(thumb_path);
629                         thumb_path[len - 3] = 'p';
630                         thumb_path[len - 2] = 'n';
631                         thumb_path[len - 1] = 'g';
632                 }
633                 thumb_dbg("Thumb path is changed : %s", thumb_path);
634         }
635
636         err = _media_thumb_save_to_file_with_gdk(gdkdata, thumb_w, thumb_h, alpha, thumb_path);
637         if (err < 0) {
638                 thumb_err("save_to_file_with_gdk failed - %d\n", err);
639                 if ( gdkdata != NULL ){
640                         g_object_unref(gdkdata);
641                 }
642
643                 if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA)
644                         strncpy(thumb_path, _media_thumb_get_default_path(uid), max_length);
645
646                 _media_thumb_db_disconnect();
647                 return err;
648         } else {
649                 thumb_dbg("file save success\n");
650         }
651
652         /* fsync */
653         int fd = 0;
654         fd = open(thumb_path, O_WRONLY);
655         if (fd < 0) {
656                 thumb_warn("open failed");
657         } else {
658                 err = fsync(fd);
659                 if (err == -1) {
660                         thumb_warn("fsync failed");
661                 }
662
663                 close(fd);
664         }
665         /* End of fsync */
666
667         g_object_unref(gdkdata);
668
669         /* DB update if needed */
670         if (need_update_db == 1) {
671                 err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, uid);
672                 if (err < 0) {
673                         thumb_err("_media_thumb_update_db failed : %d", err);
674                 }
675         }
676
677         _media_thumb_db_disconnect();
678
679         return 0;
680 }
681
682 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
683 {
684         thumbMsg recv_msg;
685         int header_size = 0;
686         int recv_str_len = 0;
687         int sock = 0;
688         int err = MEDIA_THUMB_ERROR_NONE;
689
690         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
691         sock = g_io_channel_unix_get_fd(src);
692
693         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2;
694
695         thumb_err("_media_thumb_write_socket socket : %d", sock);
696
697         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
698                 thumb_err("_media_thumb_recv_msg failed ");
699                 g_io_channel_shutdown(src, TRUE, NULL);
700                 return FALSE;
701         }
702
703         recv_str_len = strlen(recv_msg.dst_path);
704         thumb_dbg("recv %s(%d) in  [ %s ] from thumb daemon is successful", recv_msg.dst_path, recv_str_len, recv_msg.org_path);
705
706         g_io_channel_shutdown(src, TRUE, NULL);
707
708         //thumb_dbg("Completed..%s", recv_msg.org_path);
709         __media_thumb_pop_req_queue(recv_msg.org_path, FALSE);
710
711         if (recv_msg.status == THUMB_FAIL) {
712                 thumb_err("Failed to make thumbnail");
713                 err = MEDIA_THUMB_ERROR_UNSUPPORTED;
714                 goto callback;
715         }
716
717 callback:
718         if (data) {
719                 thumbUserData* cb = (thumbUserData*)data;
720                 cb->func(err, recv_msg.dst_path, cb->user_data);
721                 free(cb);
722                 cb = NULL;
723         }
724
725         thumb_dbg("Done");
726         return FALSE;
727 }
728
729 int
730 _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const char *origin_path, thumbUserData *userData, uid_t uid)
731 {
732         int sock;
733 #ifdef _USE_UDS_SOCKET_
734         struct sockaddr_un serv_addr;
735 #elif defined(_USE_UDS_SOCKET_TCP_)
736         struct sockaddr_un serv_addr;
737 #else
738         const char *serv_ip = "127.0.0.1";
739         struct sockaddr_in serv_addr;
740 #endif
741
742         int pid;
743
744         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
745                 return MEDIA_THUMB_ERROR_DUPLICATED_REQUEST;
746         }
747
748 #ifdef _USE_MEDIA_UTIL_
749 #ifdef _USE_UDS_SOCKET_
750         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_PORT) < 0) {
751 #elif defined(_USE_UDS_SOCKET_TCP_)
752         if (ms_ipc_create_client_tcp_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock, MS_THUMB_CREATOR_TCP_PORT) < 0) {
753 #else
754         if (ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock) < 0) {
755 #endif
756                 thumb_err("ms_ipc_create_client_socket failed");
757                 return MEDIA_THUMB_ERROR_NETWORK;
758         }
759 #else
760         /* Creaete a TCP socket */
761         if (_media_thumb_create_socket(CLIENT_SOCKET, &sock) < 0) {
762                 thumb_err("_media_thumb_create_socket failed");
763                 return MEDIA_THUMB_ERROR_NETWORK;
764         }
765 #endif
766
767         GIOChannel *channel = NULL;
768         channel = g_io_channel_unix_new(sock);
769         int source_id = -1;
770
771
772         memset(&serv_addr, 0, sizeof(serv_addr));
773 #ifdef _USE_UDS_SOCKET_
774         serv_addr.sun_family = AF_UNIX;
775 #elif defined(_USE_UDS_SOCKET_TCP_)
776         serv_addr.sun_family = AF_UNIX;
777 #else
778         serv_addr.sin_family = AF_INET;
779         serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
780 #endif
781
782 #ifdef _USE_MEDIA_UTIL_
783 #ifdef _USE_UDS_SOCKET_
784         strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket");
785 #elif defined(_USE_UDS_SOCKET_TCP_)
786         strcpy(serv_addr.sun_path, "/tmp/media_ipc_thumbcreator.dat");
787 #else
788         serv_addr.sin_port = htons(MS_THUMB_CREATOR_PORT);
789 #endif
790 #else
791         serv_addr.sin_port = htons(THUMB_DAEMON_PORT);
792 #endif
793
794         /* Connecting to the thumbnail server */
795         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
796                 thumb_err("connect error : %s", strerror(errno));
797                 g_io_channel_shutdown(channel, TRUE, NULL);
798                 return MEDIA_THUMB_ERROR_NETWORK;
799         }
800
801         if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
802                 //source_id = g_io_add_watch(channel, G_IO_IN, _media_thumb_write_socket, userData );
803
804                 /* Create new channel to watch udp socket */
805                 GSource *source = NULL;
806                 source = g_io_create_watch(channel, G_IO_IN);
807
808                 /* Set callback to be called when socket is readable */
809                 g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, userData, NULL);
810                 source_id = g_source_attach(source, g_main_context_get_thread_default());
811         }
812
813         thumbMsg req_msg;
814         memset((void *)&req_msg, 0, sizeof(thumbMsg));
815
816         pid = getpid();
817         req_msg.pid = pid;
818         req_msg.msg_type = msg_type;
819         req_msg.thumb_type = thumb_type;
820         req_msg.uid = uid;      
821         
822         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
823         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
824
825         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
826         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
827
828         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
829                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
830                 g_io_channel_shutdown(channel, TRUE, NULL);
831                 return MEDIA_THUMB_ERROR_INVALID_PARAMETER;
832         }
833
834         unsigned char *buf = NULL;
835         int buf_size = 0;
836         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
837
838         //thumb_dbg("buffer size : %d", buf_size);
839
840         if (send(sock, buf, buf_size, 0) != buf_size) {
841                 thumb_err("sendto failed: %d\n", errno);
842                 SAFE_FREE(buf);
843                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
844                 g_io_channel_shutdown(channel, TRUE, NULL);
845                 return MEDIA_THUMB_ERROR_NETWORK;
846         }
847
848         SAFE_FREE(buf);
849         thumb_dbg("Sending msg to thumbnail daemon is successful");
850
851 #if 0
852         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
853                 g_io_channel_shutdown(channel, TRUE, NULL);
854         }
855 #else
856         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA) {
857                 g_io_channel_shutdown(channel, TRUE, NULL);
858
859                 //thumb_dbg("Cancel : %s[%d]", origin_path, sock);
860                 __media_thumb_pop_req_queue(origin_path, TRUE);
861         } else if (msg_type == THUMB_REQUEST_DB_INSERT) {
862                 if (g_request_queue == NULL) {
863                         g_request_queue = g_queue_new();
864                 }
865
866                 thumbReq *thumb_req = NULL;
867                 thumb_req = calloc(1, sizeof(thumbReq));
868                 if (thumb_req == NULL) {
869                         thumb_err("Failed to create request element");
870                         return 0;
871                 }
872
873                 thumb_req->channel = channel;
874                 thumb_req->path = strdup(origin_path);
875                 thumb_req->source_id = source_id;
876
877                 //thumb_dbg("Push : %s [%d]", origin_path, sock);
878                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
879         }
880 #endif
881
882         return 0;
883 }
884