Update image/desc filename management code
[platform/framework/web/livebox-viewer.git] / src / client.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 <stdio.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <pthread.h>
23
24 #include <dlog.h>
25 #include <glib.h>
26
27 #include <vconf.h>
28 #include <vconf-keys.h>
29
30 #include <packet.h>
31 #include <com-core.h>
32 #include <com-core_packet.h>
33 #include <livebox-errno.h>
34 #include <secure_socket.h>
35
36 #include "debug.h"
37 #include "client.h"
38 #include "livebox.h"
39 #include "livebox_internal.h"
40 #include "desc_parser.h"
41 #include "fb.h"
42 #include "util.h"
43 #include "master_rpc.h"
44 #include "conf.h"
45 #include "critical_log.h"
46 #include "file_service.h"
47
48 int errno;
49
50 static struct info {
51         int fd;
52         guint timer_id;
53         char *client_addr;
54 } s_info = {
55         .fd = -1,
56         .timer_id = 0,
57         .client_addr = NULL,
58 };
59
60 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
61 {
62         const char *pkgname;
63         const char *id;
64         const char *function;
65
66         if (packet_get(packet, "sss", &pkgname, &id, &function) != 3) {
67                 ErrPrint("Invalid arguments\n");
68                 return NULL;
69         }
70
71         master_rpc_clear_fault_package(pkgname);
72         lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
73         return NULL;
74 }
75
76 static struct packet *master_hold_scroll(pid_t pid, int handle, const struct packet *packet)
77 {
78         struct livebox *handler;
79         const char *pkgname;
80         const char *id;
81         int seize;
82         int ret;
83
84         ret = packet_get(packet, "ssi", &pkgname, &id, &seize);
85         if (ret != 3) {
86                 ErrPrint("Invalid argument\n");
87                 goto out;
88         }
89
90         handler = lb_find_livebox(pkgname, id);
91         if (!handler) {
92                 ErrPrint("Instance(%s) is not exists\n", id);
93                 goto out;
94         }
95
96         DbgPrint("HOLD: %s %d\n", id, seize);
97         lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
98
99 out:
100         return NULL;
101 }
102
103 static struct packet *master_pinup(pid_t pid, int handle, const struct packet *packet)
104 {
105         const char *pkgname;
106         const char *id;
107         const char *content;
108         struct livebox *handler;
109         char *new_content;
110         int ret;
111         int status;
112         int pinup;
113
114         ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
115         if (ret != 5) {
116                 ErrPrint("Invalid argument\n");
117                 goto out;
118         }
119
120         handler = lb_find_livebox(pkgname, id);
121         if (!handler) {
122                 ErrPrint("Instance (%s) is not exists\n", id);
123                 goto out;
124         }
125
126         if (status == 0) {
127                 new_content = strdup(content);
128                 if (new_content) {
129                         free(handler->content);
130                         handler->content = new_content;
131                         handler->is_pinned_up = pinup;
132                 } else {
133                         ErrPrint("Heap: %s\n", strerror(errno));
134                         status = LB_STATUS_ERROR_MEMORY;
135                 }
136         }
137
138         if (handler->pinup_cb) {
139                 ret_cb_t cb;
140                 void *cbdata;
141
142                 /* Make sure that user can call pinup API in its result callback */
143                 cb = handler->pinup_cb;
144                 cbdata = handler->pinup_cbdata;
145
146                 handler->pinup_cb = NULL;
147                 handler->pinup_cbdata = NULL;
148
149                 cb(handler, status, cbdata);
150         } else if (status == 0) {
151                 lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
152         }
153
154 out:
155         return NULL;
156 }
157
158 static struct packet *master_deleted(pid_t pid, int handle, const struct packet *packet)
159 {
160         const char *pkgname;
161         const char *id;
162         double timestamp;
163         struct livebox *handler;
164
165         if (packet_get(packet, "ssd", &pkgname, &id, &timestamp) != 3) {
166                 ErrPrint("Invalid arguemnt\n");
167                 goto out;
168         }
169
170         handler = lb_find_livebox_by_timestamp(timestamp);
171         if (!handler) {
172                 /*!
173                  * \note
174                  * This can be happens only if the user delete a livebox
175                  * right after create it before receive created event.
176                  */
177                 goto out;
178         }
179
180         /*!< Check validity of this "handler" */
181         if (handler->state != CREATE) {
182                 if (handler->state != DELETE) {
183                         /*!
184                          * \note
185                          * This is not possible
186                          */
187                         CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
188                         return NULL;
189                 }
190         }
191
192         if (handler->created_cb) {
193                 ret_cb_t cb;
194                 void *cbdata;
195                 /*!
196                  * \note
197                  *
198                  * "if (handler->id == NULL) {"
199                  *
200                  * The instance is not created yet.
201                  * But the master forcely destroy it and send destroyed event to this
202                  * without the created event.
203                  *
204                  * It could be destroyed when a slave has critical error(fault)
205                  * before creating an instance successfully.
206                  */
207                 if (handler->created_cb == handler->deleted_cb) {
208                         if (handler->created_cbdata != handler->deleted_cbdata) {
209                                 DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
210                         }
211
212                         handler->deleted_cb = NULL;
213                         handler->deleted_cbdata = NULL;
214                 }
215
216                 cb = handler->created_cb;
217                 cbdata = handler->created_cbdata;
218
219                 handler->created_cb = NULL;
220                 handler->created_cbdata = NULL;
221
222                 cb(handler, LB_STATUS_ERROR_CANCEL, cbdata);
223         } else if (handler->id) {
224                 if (handler->deleted_cb) {
225                         ret_cb_t cb;
226                         void *cbdata;
227
228                         cb = handler->deleted_cb;
229                         cbdata = handler->deleted_cbdata;
230
231                         handler->deleted_cb = NULL;
232                         handler->deleted_cbdata = NULL;
233
234                         cb(handler, LB_STATUS_SUCCESS, cbdata);
235                 } else {
236                         lb_invoke_event_handler(handler, LB_EVENT_DELETED);
237                 }
238         }
239
240         /* Just try to delete it, if a user didn't remove it from the live box list */
241         lb_unref(handler);
242
243 out:
244         return NULL;
245 }
246
247 static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct packet *packet)
248 {
249         struct livebox *handler;
250         const char *pkgname;
251         const char *id;
252         const char *content;
253         const char *title;
254         const char *fbfile;
255         double priority;
256         int ret;
257
258         ret = packet_get(packet, "ssdsss", &pkgname, &id, &priority, &content, &title, &fbfile);
259         if (ret != 6) {
260                 ErrPrint("Invalid argument\n");
261                 goto out;
262         }
263
264         handler = lb_find_livebox(pkgname, id);
265         if (!handler) {
266                 ErrPrint("Instance[%s] is not exists\n", id);
267                 goto out;
268         }
269
270         if (handler->state != CREATE) {
271                 ErrPrint("(%s) is not created\n", id);
272                 goto out;
273         }
274
275         lb_set_priority(handler, priority);
276         lb_set_content(handler, content);
277         lb_set_title(handler, title);
278
279         /*!
280          * \NOTE
281          * Width & Height is not changed in this case.
282          * If the active update is began, the size should not be changed,
283          * And if the size is changed, the provider should finish the updating first.
284          * And then begin updating again after change its size.
285          */
286         if (lb_get_lb_fb(handler)) {
287                 lb_set_lb_fb(handler, fbfile);
288
289                 ret = fb_sync(lb_get_lb_fb(handler));
290                 if (ret != LB_STATUS_SUCCESS) {
291                         ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
292                 } else {
293                         lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
294                 }
295         } else {
296                 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
297         }
298
299 out:
300         return NULL;
301 }
302
303 static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct packet *packet)
304 {
305         struct livebox *handler;
306         const char *pkgname;
307         const char *id;
308         const char *fbfile;
309         int ret;
310
311         ret = packet_get(packet, "sss", &pkgname, &id, &fbfile);
312         if (ret != 2) {
313                 ErrPrint("Invalid argument\n");
314                 goto out;
315         }
316
317         handler = lb_find_livebox(pkgname, id);
318         if (!handler) {
319                 ErrPrint("Instance[%s] is not exists\n", id);
320                 goto out;
321         }
322
323         if (handler->state != CREATE) {
324                 ErrPrint("[%s] is not created\n", id);
325                 goto out;
326         }
327
328         if (lb_get_pd_fb(handler)) {
329                 lb_set_lb_fb(handler, fbfile);
330
331                 ret = fb_sync(lb_get_lb_fb(handler));
332                 if (ret != LB_STATUS_SUCCESS) {
333                         ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
334                 } else {
335                         lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
336                 }
337         } else {
338                 ErrPrint("Invalid request[%s], %s\n", id, fbfile);
339         }
340
341 out:
342         return NULL;
343 }
344
345 static struct packet *master_lb_update_end(pid_t pid, int handle, const struct packet *packet)
346 {
347         struct livebox *handler;
348         const char *pkgname;
349         const char *id;
350         int ret;
351
352         ret = packet_get(packet, "ss", &pkgname, &id);
353         if (ret != 2) {
354                 ErrPrint("Invalid argument\n");
355                 goto out;
356         }
357
358         handler = lb_find_livebox(pkgname, id);
359         if (!handler) {
360                 ErrPrint("Instance[%s] is not exists\n", id);
361                 goto out;
362         }
363
364         if (handler->state != CREATE) {
365                 ErrPrint("[%s] is not created\n", id);
366                 goto out;
367         }
368
369         if (lb_get_lb_fb(handler)) {
370                 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_END);
371         } else {
372                 ErrPrint("Invalid request[%s]\n", id);
373         }
374
375 out:
376         return NULL;
377 }
378
379 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
380 {
381         struct livebox *handler;
382         const char *pkgname;
383         const char *id;
384         int ret;
385         int status;
386
387         ret = packet_get(packet, "ssi", &pkgname, &id, &status);
388         if (ret != 3) {
389                 ErrPrint("Invalid argument\n");
390                 goto out;
391         }
392
393         handler = lb_find_livebox(pkgname, id);
394         if (!handler) {
395                 ErrPrint("Instance[%s] is not exists\n", id);
396                 goto out;
397         }
398
399         if (handler->state != CREATE) {
400                 ErrPrint("[%s] is not created\n", id);
401                 goto out;
402         }
403
404         if (handler->access_event_cb) {
405                 ret_cb_t cb;
406                 void *cbdata;
407
408                 cb = handler->access_event_cb;
409                 cbdata = handler->access_event_cbdata;
410
411                 handler->access_event_cb = NULL;
412                 handler->access_event_cbdata = NULL;
413
414                 cb(handler, status, cbdata);
415         } else {
416                 ErrPrint("Invalid event[%s]\n", id);
417         }
418 out:
419         return NULL;
420 }
421
422 static struct packet *master_pd_update_end(pid_t pid, int handle, const struct packet *packet)
423 {
424         struct livebox *handler;
425         const char *pkgname;
426         const char *id;
427         int ret;
428
429         ret = packet_get(packet, "ss", &pkgname, &id);
430         if (ret != 2) {
431                 ErrPrint("Invalid argument\n");
432                 goto out;
433         }
434
435         handler = lb_find_livebox(pkgname, id);
436         if (!handler) {
437                 ErrPrint("Instance[%s] is not exists\n", id);
438                 goto out;
439         }
440
441         if (handler->state != CREATE) {
442                 ErrPrint("[%s] is not created\n", id);
443                 goto out;
444         }
445
446         if (lb_get_lb_fb(handler)) {
447                 lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_END);
448         } else {
449                 ErrPrint("Invalid request[%s]", id);
450         }
451
452 out:
453         return NULL;
454 }
455
456 static struct packet *master_lb_updated(pid_t pid, int handle, const struct packet *packet)
457 {
458         const char *pkgname;
459         const char *id;
460         const char *fbfile;
461         const char *content;
462         const char *title;
463         const char *safe_file;
464         struct livebox *handler;
465         int lb_w;
466         int lb_h;
467         double priority;
468         int ret;
469
470         ret = packet_get(packet, "sssiidsss",
471                                 &pkgname, &id,
472                                 &fbfile, &lb_w, &lb_h,
473                                 &priority, &content, &title,
474                                 &safe_file);
475         if (ret != 9) {
476                 ErrPrint("Invalid argument\n");
477                 goto out;
478         }
479
480         handler = lb_find_livebox(pkgname, id);
481         if (!handler) {
482                 ErrPrint("instance(%s) is not exists\n", id);
483                 goto out;
484         }
485
486         if (handler->state != CREATE) {
487                 /*!
488                  * \note
489                  * Already deleted by the user.
490                  * Don't try to notice anything with this, Just ignore all events
491                  * Beacuse the user doesn't wants know about this anymore
492                  */
493                 ErrPrint("(%s) is not exists, but updated\n", id);
494                 goto out;
495         }
496
497         lb_set_priority(handler, priority);
498         lb_set_content(handler, content);
499         lb_set_title(handler, title);
500         lb_set_size(handler, lb_w, lb_h);
501         lb_set_filename(handler, safe_file);
502
503         if (lb_text_lb(handler)) {
504                 (void)parse_desc(handler, livebox_filename(handler), 0);
505                 /*!
506                  * \note
507                  * DESC parser will call the "text event callback".
508                  * Don't need to call global event callback in this case.
509                  */
510                 goto out;
511         } else if (lb_get_lb_fb(handler)) {
512                 lb_set_lb_fb(handler, fbfile);
513                 ret = fb_sync(lb_get_lb_fb(handler));
514                 if (ret != LB_STATUS_SUCCESS) {
515                         ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
516                 }
517         } else {
518                 ret = LB_STATUS_SUCCESS;
519         }
520
521         if (ret == LB_STATUS_SUCCESS) {
522                 lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
523         }
524
525 out:
526         return NULL;
527 }
528
529 static struct packet *master_pd_created(pid_t pid, int handle, const struct packet *packet)
530 {
531         struct livebox *handler;
532         const char *pkgname;
533         const char *id;
534         const char *buf_id;
535         int width;
536         int height;
537         int ret;
538         int status;
539
540         ret = packet_get(packet, "sssiii", &pkgname, &id, &buf_id, &width, &height, &status);
541         if (ret != 6) {
542                 ErrPrint("Invalid argument\n");
543                 goto out;
544         }
545
546         handler = lb_find_livebox(pkgname, id);
547         if (!handler) {
548                 ErrPrint("Instance(%s) is not exists\n", id);
549                 goto out;
550         }
551
552         if (handler->state != CREATE) {
553                 ErrPrint("Instance(%s) is not created\n", id);
554                 goto out;
555         }
556
557         lb_set_pdsize(handler, width, height);
558         if (lb_text_pd(handler)) {
559                 DbgPrint("Text TYPE does not need to handle this\n");
560         } else {
561                 (void)lb_set_pd_fb(handler, buf_id);
562                 ret = fb_sync(lb_get_pd_fb(handler));
563                 if (ret < 0) {
564                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
565                 }
566         }
567
568         handler->is_pd_created = (status == 0);
569
570         if (handler->pd_created_cb) {
571                 ret_cb_t cb;
572                 void *cbdata;
573
574                 cb = handler->pd_created_cb;
575                 cbdata = handler->pd_created_cbdata;
576
577                 handler->pd_created_cb = NULL;
578                 handler->pd_created_cbdata = NULL;
579
580                 /*!
581                  * Before call the Callback function,
582                  * pd_create_cb must be reset.
583                  * Because, in the create callback, user can call create_pd function again.
584                  */
585                 DbgPrint("PERF_DBOX\n");
586                 cb(handler, status, cbdata);
587         } else if (handler->is_pd_created) {
588                 lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
589         }
590
591 out:
592         return NULL;
593 }
594
595 static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct packet *packet)
596 {
597         struct livebox *handler;
598         const char *pkgname;
599         const char *id;
600         int ret;
601         int status;
602
603         ret = packet_get(packet, "ssi", &pkgname, &id, &status);
604         if (ret != 3) {
605                 ErrPrint("Invalid argument\n");
606                 goto out;
607         }
608
609         handler = lb_find_livebox(pkgname, id);
610         if (!handler) {
611                 ErrPrint("Instance(%s) is not exists\n", id);
612                 goto out;
613         }
614
615         if (handler->state != CREATE) {
616                 ErrPrint("Instance(%s) is not created\n", id);
617                 goto out;
618         }
619
620         handler->is_pd_created = 0;
621
622         if (handler->pd_destroyed_cb) {
623                 ret_cb_t cb;
624                 void *cbdata;
625
626                 cb = handler->pd_destroyed_cb;
627                 cbdata = handler->pd_destroyed_cbdata;
628
629                 handler->pd_destroyed_cb = NULL;
630                 handler->pd_destroyed_cbdata = NULL;
631
632                 /*!
633                  * Before call the Callback function,
634                  * pd_destroyed_cb must be reset.
635                  * Because, in the create callback, user can call destroy_pd function again.
636                  */
637                 cb(handler, status, cbdata);
638         } else if (status == 0) {
639                 lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
640         }
641
642 out:
643         return NULL;
644 }
645
646 static struct packet *master_pd_updated(pid_t pid, int handle, const struct packet *packet)
647 {
648         const char *pkgname;
649         const char *id;
650         const char *descfile;
651         const char *fbfile;
652         int ret;
653         struct livebox *handler;
654         int pd_w;
655         int pd_h;
656
657         ret = packet_get(packet, "ssssii",
658                                 &pkgname, &id,
659                                 &descfile, &fbfile,
660                                 &pd_w, &pd_h);
661         if (ret != 6) {
662                 ErrPrint("Invalid argument\n");
663                 goto out;
664         }
665
666         handler = lb_find_livebox(pkgname, id);
667         if (!handler) {
668                 ErrPrint("Instance(%s) is not exists\n", id);
669                 goto out;
670         }
671
672         if (handler->state != CREATE) {
673                 /*!
674                  * \note
675                  * This handler is already deleted by the user.
676                  * So don't try to notice anything about this anymore.
677                  * Just ignore all events.
678                  */
679                 ErrPrint("Instance(%s) is not created\n", id);
680                 goto out;
681         }
682
683         lb_set_pdsize(handler, pd_w, pd_h);
684
685         if (lb_text_pd(handler)) {
686                 (void)parse_desc(handler, descfile, 1);
687         } else {
688                 (void)lb_set_pd_fb(handler, fbfile);
689
690                 ret = fb_sync(lb_get_pd_fb(handler));
691                 if (ret < 0) {
692                         ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
693                 } else {
694                         lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
695                 }
696         }
697
698 out:
699         return NULL;
700 }
701
702 static struct packet *master_update_mode(pid_t pid, int handle, const struct packet *packet)
703 {
704         struct livebox *handler;
705         const char *pkgname;
706         const char *id;
707         int active_mode;
708         int status;
709         int ret;
710
711         if (!packet) {
712                 ErrPrint("Invalid packet\n");
713                 goto out;
714         }
715
716         ret = packet_get(packet, "ssii", &pkgname, &id, &status, &active_mode);
717         if (ret != 4) {
718                 ErrPrint("Invalid argument\n");
719                 goto out;
720         }
721
722         handler = lb_find_livebox(pkgname, id);
723         if (!handler) {
724                 ErrPrint("Livebox(%s) is not found\n", id);
725                 goto out;
726         }
727
728         if (handler->state != CREATE) {
729                 ErrPrint("Livebox(%s) is not created yet\n", id);
730                 goto out;
731         }
732
733         if (status == LB_STATUS_SUCCESS) {
734                 lb_set_update_mode(handler, active_mode);
735         }
736
737         if (handler->update_mode_cb) {
738                 ret_cb_t cb;
739                 void *cbdata;
740
741                 cb = handler->update_mode_cb;
742                 cbdata = handler->update_mode_cbdata;
743
744                 handler->update_mode_cb = NULL;
745                 handler->update_mode_cbdata = NULL;
746
747                 cb(handler, status, cbdata);
748         } else {
749                 lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
750         }
751
752 out:
753         return NULL;
754 }
755
756 static struct packet *master_size_changed(pid_t pid, int handle, const struct packet *packet)
757 {
758         struct livebox *handler;
759         const char *pkgname;
760         const char *id;
761         const char *fbfile;
762         int status;
763         int ret;
764         int w;
765         int h;
766         int is_pd;
767
768         if (!packet) {
769                 ErrPrint("Invalid packet\n");
770                 goto out;
771         }
772
773         ret = packet_get(packet, "sssiiii", &pkgname, &id, &fbfile, &is_pd, &w, &h, &status);
774         if (ret != 7) {
775                 ErrPrint("Invalid argument\n");
776                 goto out;
777         }
778
779         handler = lb_find_livebox(pkgname, id);
780         if (!handler) {
781                 ErrPrint("Livebox(%s) is not found\n", id);
782                 goto out;
783         }
784
785         if (handler->state != CREATE) {
786                 ErrPrint("Livebox(%s) is not created yet\n", id);
787                 goto out;
788         }
789
790         if (is_pd) {
791                 /*!
792                  * \NOTE
793                  * PD is not able to resized by the client.
794                  * PD is only can be managed by the provider.
795                  * So the PD has no private resized event handler.
796                  * Notify it via global event handler only.
797                  */
798                 if (status == 0) {
799                         lb_set_pdsize(handler, w, h);
800                         lb_invoke_event_handler(handler, LB_EVENT_PD_SIZE_CHANGED);
801                 } else {
802                         ErrPrint("This is not possible. PD Size is changed but the return value is not ZERO (%d)\n", status);
803                 }
804         } else {
805                 if (status == 0) {
806                         lb_set_size(handler, w, h);
807
808                         /*!
809                          * \NOTE
810                          * If there is a created LB FB, 
811                          * Update it too.
812                          */
813                         if (lb_get_lb_fb(handler)) {
814                                 lb_set_lb_fb(handler, fbfile);
815
816                                 ret = fb_sync(lb_get_lb_fb(handler));
817                                 if (ret < 0) {
818                                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
819                                 }
820
821                                 /* Just update the size info only. */
822                         }
823
824                         /*!
825                          * \NOTE
826                          * I cannot believe client.
827                          * So I added some log before & after call the user callback.
828                          */
829                         if (handler->size_changed_cb) {
830                                 ret_cb_t cb;
831                                 void *cbdata;
832
833                                 cb = handler->size_changed_cb;
834                                 cbdata = handler->size_cbdata;
835
836                                 handler->size_changed_cb = NULL;
837                                 handler->size_cbdata = NULL;
838
839                                 cb(handler, status, cbdata);
840                         } else {
841                                 lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
842                         }
843                 } else {
844                         if (handler->size_changed_cb) {
845                                 ret_cb_t cb;
846                                 void *cbdata;
847
848                                 cb = handler->size_changed_cb;
849                                 cbdata = handler->size_cbdata;
850
851                                 handler->size_changed_cb = NULL;
852                                 handler->size_cbdata = NULL;
853
854                                 cb(handler, status, cbdata);
855                         }
856                 }
857         }
858
859 out:
860         return NULL;
861 }
862
863 static struct packet *master_period_changed(pid_t pid, int handle, const struct packet *packet)
864 {
865         struct livebox *handler;
866         const char *pkgname;
867         const char *id;
868         int ret;
869         double period;
870         int status;
871
872         ret = packet_get(packet, "idss", &status, &period, &pkgname, &id);
873         if (ret != 4) {
874                 ErrPrint("Invalid argument\n");
875                 goto out;
876         }
877
878         handler = lb_find_livebox(pkgname, id);
879         if (!handler) {
880                 ErrPrint("Livebox(%s) is not found\n", id);
881                 goto out;
882         }
883
884         if (handler->state != CREATE) {
885                 ErrPrint("Livebox(%s) is not created\n", id);
886                 goto out;
887         }
888
889         if (status == 0) {
890                 lb_set_period(handler, period);
891         }
892
893         if (handler->period_changed_cb) {
894                 ret_cb_t cb;
895                 void *cbdata;
896
897                 cb = handler->period_changed_cb;
898                 cbdata = handler->period_cbdata;
899
900                 handler->period_changed_cb = NULL;
901                 handler->period_cbdata = NULL;
902
903                 cb(handler, status, cbdata);
904         } else if (status == 0) {
905                 lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
906         }
907
908 out:
909         return NULL;
910 }
911
912 static struct packet *master_group_changed(pid_t pid, int handle, const struct packet *packet)
913 {
914         struct livebox *handler;
915         const char *pkgname;
916         const char *id;
917         int ret;
918         const char *cluster;
919         const char *category;
920         int status;
921
922         ret = packet_get(packet, "ssiss", &pkgname, &id, &status, &cluster, &category);
923         if (ret != 5) {
924                 ErrPrint("Invalid argument\n");
925                 goto out;
926         }
927
928         handler = lb_find_livebox(pkgname, id);
929         if (!handler) {
930                 ErrPrint("Livebox(%s) is not exists\n", id);
931                 goto out;
932         }
933
934         if (handler->state != CREATE) {
935                 /*!
936                  * \note
937                  * Do no access this handler,
938                  * You cannot believe this handler anymore.
939                  */
940                 ErrPrint("Livebox(%s) is not created\n", id);
941                 goto out;
942         }
943
944         if (status == 0) {
945                 (void)lb_set_group(handler, cluster, category);
946         }
947
948         if (handler->group_changed_cb) {
949                 ret_cb_t cb;
950                 void *cbdata;
951
952                 cb = handler->group_changed_cb;
953                 cbdata = handler->group_cbdata;
954
955                 handler->group_changed_cb = NULL;
956                 handler->group_cbdata = NULL;
957
958                 cb(handler, status, cbdata);
959         } else if (status == 0) {
960                 lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
961         }
962
963 out:
964         return NULL;
965 }
966
967 static struct packet *master_created(pid_t pid, int handle, const struct packet *packet)
968 {
969         struct livebox *handler;
970
971         int lb_w;
972         int lb_h;
973         int pd_w;
974         int pd_h;
975         const char *pkgname;
976         const char *id;
977
978         const char *content;
979         const char *cluster;
980         const char *category;
981         const char *lb_fname;
982         const char *pd_fname;
983         const char *title;
984
985         double timestamp;
986         const char *auto_launch;
987         double priority;
988         int size_list;
989         int user;
990         int pinup_supported;
991         enum lb_type lb_type;
992         enum pd_type pd_type;
993         double period;
994         int is_pinned_up;
995
996         int old_state = DESTROYED;
997
998         int ret;
999
1000         ret = packet_get(packet, "dsssiiiisssssdiiiiidsi",
1001                         &timestamp,
1002                         &pkgname, &id, &content,
1003                         &lb_w, &lb_h, &pd_w, &pd_h,
1004                         &cluster, &category, &lb_fname, &pd_fname,
1005                         &auto_launch, &priority, &size_list, &user, &pinup_supported,
1006                         &lb_type, &pd_type, &period, &title, &is_pinned_up);
1007         if (ret != 22) {
1008                 ErrPrint("Invalid argument\n");
1009                 ret = LB_STATUS_ERROR_INVALID;
1010                 goto out;
1011         }
1012
1013         ErrPrint("[%lf] pkgname: %s, id: %s, content: %s, "
1014                 "pd_w: %d, pd_h: %d, lb_w: %d, lb_h: %d, "
1015                 "cluster: %s, category: %s, lb_fname: \"%s\", pd_fname: \"%s\", "
1016                 "auto_launch: %s, priority: %lf, size_list: %d, user: %d, pinup: %d, "
1017                 "lb_type: %d, pd_type: %d, period: %lf, title: [%s], is_pinned_up: %d\n",
1018                 timestamp, pkgname, id, content,
1019                 pd_w, pd_h, lb_w, lb_h,
1020                 cluster, category, lb_fname, pd_fname,
1021                 auto_launch, priority, size_list, user, pinup_supported,
1022                 lb_type, pd_type, period, title, is_pinned_up);
1023
1024         handler = lb_find_livebox_by_timestamp(timestamp);
1025         if (!handler) {
1026                 handler = lb_new_livebox(pkgname, id, timestamp);
1027                 if (!handler) {
1028                         ErrPrint("Failed to create a new livebox\n");
1029                         ret = LB_STATUS_ERROR_FAULT;
1030                         goto out;
1031                 }
1032
1033                 old_state = handler->state;
1034         } else {
1035                 if (handler->state != CREATE) {
1036                         if (handler->state != DELETE) {
1037                                 /*!
1038                                  * \note
1039                                  * This is not possible!!!
1040                                  */
1041                                 ErrPrint("Invalid handler\n");
1042                                 ret = LB_STATUS_ERROR_INVALID;
1043                                 goto out;
1044                         }
1045
1046                         /*!
1047                          * \note
1048                          * After get the delete states,
1049                          * call the create callback with deleted result.
1050                          */
1051                 }
1052
1053                 old_state = handler->state;
1054
1055                 if (handler->id) {
1056                         ErrPrint("Already created: timestamp[%lf] "
1057                                 "pkgname[%s], id[%s] content[%s] "
1058                                 "cluster[%s] category[%s] lb_fname[%s] pd_fname[%s]\n",
1059                                         timestamp, pkgname, id,
1060                                         content, cluster, category,
1061                                         lb_fname, pd_fname);
1062
1063                         ret = LB_STATUS_ERROR_ALREADY;
1064                         goto out;
1065                 }
1066
1067                 lb_set_id(handler, id);
1068         }
1069
1070         lb_set_size(handler, lb_w, lb_h);
1071         handler->lb.type = lb_type;
1072         handler->is_pinned_up = is_pinned_up;
1073
1074         switch (lb_type) {
1075         case _LB_TYPE_FILE:
1076                 break;
1077         case _LB_TYPE_SCRIPT:
1078         case _LB_TYPE_BUFFER:
1079                 if (!strlen(lb_fname)) {
1080                         break;
1081                 }
1082                 lb_set_lb_fb(handler, lb_fname);
1083                 ret = fb_sync(lb_get_lb_fb(handler));
1084                 if (ret < 0) {
1085                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1086                 }
1087                 break;
1088         case _LB_TYPE_TEXT:
1089                 lb_set_text_lb(handler);
1090                 break;
1091         default:
1092                 break;
1093         }
1094
1095         handler->pd.type = pd_type;
1096         lb_set_pdsize(handler, pd_w, pd_h);
1097         lb_set_default_pdsize(handler, pd_w, pd_h);
1098         switch (pd_type) {
1099         case _PD_TYPE_SCRIPT:
1100         case _PD_TYPE_BUFFER:
1101                 if (!strlen(pd_fname)) {
1102                         break;
1103                 }
1104
1105                 lb_set_pd_fb(handler, pd_fname);
1106                 ret = fb_sync(lb_get_pd_fb(handler));
1107                 if (ret < 0) {
1108                         ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
1109                 }
1110                 break;
1111         case _PD_TYPE_TEXT:
1112                 lb_set_text_pd(handler);
1113                 break;
1114         default:
1115                 break;
1116         }
1117
1118         lb_set_priority(handler, priority);
1119
1120         lb_set_size_list(handler, size_list);
1121         lb_set_group(handler, cluster, category);
1122
1123         lb_set_content(handler, content);
1124         lb_set_title(handler, title);
1125
1126         lb_set_user(handler, user);
1127
1128         lb_set_auto_launch(handler, auto_launch);
1129         lb_set_pinup(handler, pinup_supported);
1130
1131         lb_set_period(handler, period);
1132
1133         ret = 0;
1134
1135         if (handler->state == CREATE) {
1136                 /*!
1137                  * \note
1138                  * These callback can change the handler->state.
1139                  * So we have to use the "old_state" which stored state before call these callbacks
1140                  */
1141
1142                 if (handler->created_cb) {
1143                         ret_cb_t cb;
1144                         void *cbdata;
1145
1146                         cb = handler->created_cb;
1147                         cbdata = handler->created_cbdata;
1148
1149                         handler->created_cb = NULL;
1150                         handler->created_cbdata = NULL;
1151
1152                         cb(handler, ret, cbdata);
1153                 } else {
1154                         lb_invoke_event_handler(handler, LB_EVENT_CREATED);
1155                 }
1156         }
1157
1158 out:
1159         if (ret == 0 && old_state == DELETE) {
1160                 lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
1161
1162                 /*!
1163                  * \note
1164                  * handler->created_cb = NULL;
1165                  * handler->created_cbdata = NULL;
1166                  *
1167                  * Do not clear this to use this from the deleted event callback.
1168                  * if this value is not cleared when the deleted event callback check it,
1169                  * it means that the created function is not called yet.
1170                  * Then the call the deleted event callback with LB_STATUS_ERROR_CANCEL errno.
1171                  */
1172         }
1173
1174         return NULL;
1175 }
1176
1177 static struct method s_table[] = {
1178         {
1179                 .cmd = "lb_updated", /* pkgname, id, lb_w, lb_h, priority, ret */
1180                 .handler = master_lb_updated,
1181         },
1182         {
1183                 .cmd = "pd_updated", /* pkgname, id, descfile, pd_w, pd_h, ret */
1184                 .handler = master_pd_updated,
1185         },
1186         {
1187                 .cmd = "pd_created",
1188                 .handler = master_pd_created,
1189         },
1190         {
1191                 .cmd = "pd_destroyed",
1192                 .handler = master_pd_destroyed,
1193         },
1194         {
1195                 .cmd = "fault_package", /* pkgname, id, function, ret */
1196                 .handler = master_fault_package,
1197         },
1198         {
1199                 .cmd = "deleted", /* pkgname, id, timestamp, ret */
1200                 .handler = master_deleted,
1201         },
1202         {
1203                 .cmd = "created", /* timestamp, pkgname, id, content, lb_w, lb_h, pd_w, pd_h, cluster, category, lb_file, pd_file, auto_launch, priority, size_list, is_user, pinup_supported, text_lb, text_pd, period, ret */
1204                 .handler = master_created,
1205         },
1206         {
1207                 .cmd = "group_changed",
1208                 .handler = master_group_changed,
1209         },
1210         {
1211                 .cmd = "period_changed",
1212                 .handler = master_period_changed,
1213         },
1214         {
1215                 .cmd = "size_changed",
1216                 .handler = master_size_changed,
1217         },
1218         {
1219                 .cmd = "pinup",
1220                 .handler = master_pinup,
1221         },
1222         {
1223                 .cmd = "scroll",
1224                 .handler = master_hold_scroll,
1225         },
1226
1227         {
1228                 .cmd = "update_mode",
1229                 .handler = master_update_mode,
1230         },
1231
1232         {
1233                 .cmd = "lb_update_begin",
1234                 .handler = master_lb_update_begin,
1235         },
1236         {
1237                 .cmd = "lb_update_end",
1238                 .handler = master_lb_update_end,
1239         },
1240
1241         {
1242                 .cmd = "pd_update_begin",
1243                 .handler = master_pd_update_begin,
1244         },
1245         {
1246                 .cmd = "pd_update_end",
1247                 .handler = master_pd_update_end,
1248         },
1249
1250         {
1251                 .cmd = "access_status",
1252                 .handler = master_access_status,
1253         },
1254
1255         {
1256                 .cmd = NULL,
1257                 .handler = NULL,
1258         },
1259 };
1260
1261 static void acquire_cb(struct livebox *handler, const struct packet *result, void *data)
1262 {
1263         if (!result) {
1264                 DbgPrint("Result packet is not valid\n");
1265         } else {
1266                 int ret;
1267
1268                 if (packet_get(result, "i", &ret) != 1) {
1269                         ErrPrint("Invalid argument\n");
1270                 } else {
1271                         DbgPrint("Acquire returns: %d\n", ret);
1272                 }
1273         }
1274
1275         return;
1276 }
1277
1278 static inline int make_connection(void)
1279 {
1280         struct packet *packet;
1281         int ret;
1282
1283         DbgPrint("Let's making connection!\n");
1284
1285         s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
1286         if (s_info.fd < 0) {
1287                 ErrPrint("Try this again later\n");
1288                 return LB_STATUS_ERROR_IO;
1289         }
1290
1291         packet = packet_create("acquire", "d", util_timestamp());
1292         if (!packet) {
1293                 com_core_packet_client_fini(s_info.fd);
1294                 s_info.fd = -1;
1295                 return LB_STATUS_ERROR_FAULT;
1296         }
1297
1298         ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
1299         if (ret < 0) {
1300                 ErrPrint("Master RPC returns %d\n", ret);
1301                 com_core_packet_client_fini(s_info.fd);
1302                 s_info.fd = -1;
1303                 return LB_STATUS_ERROR_IO;
1304         }
1305
1306         return LB_STATUS_SUCCESS;
1307 }
1308
1309 static int connected_cb(int handle, void *data)
1310 {
1311         master_rpc_check_and_fire_consumer();
1312         return 0;
1313 }
1314
1315 static void master_started_cb(keynode_t *node, void *data)
1316 {
1317         int state = 0;
1318
1319         if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) {
1320                 ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
1321         }
1322
1323         DbgPrint("Master state: %d\n", state);
1324         if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
1325                 int ret;
1326                 ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1327                 DbgPrint("master_started vconf key de-registered [%d]\n", ret);
1328         }
1329 }
1330
1331 static gboolean timeout_cb(gpointer data)
1332 {
1333         if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1334                 ErrPrint("Failed to add vconf for monitoring service state\n");
1335         } else {
1336                 DbgPrint("vconf event callback is registered\n");
1337         }
1338
1339         master_started_cb(NULL, NULL);
1340
1341         s_info.timer_id = 0;
1342         return FALSE;
1343 }
1344
1345 static int disconnected_cb(int handle, void *data)
1346 {
1347         if (s_info.fd != handle) {
1348                 /*!< This handle is not my favor */
1349                 return 0;
1350         }
1351
1352         s_info.fd = -1; /*!< Disconnected */
1353
1354         master_rpc_clear_all_request();
1355         lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
1356
1357         lb_delete_all();
1358
1359         /* Try to reconnect after 1 sec later */
1360         if (!s_info.timer_id) {
1361                 DbgPrint("Reconnecting timer is added\n");
1362                 s_info.timer_id = g_timeout_add(1000, timeout_cb, NULL);
1363                 if (s_info.timer_id == 0) {
1364                         ErrPrint("Unable to add reconnecting timer\n");
1365                         return 0;
1366                 }
1367         } else {
1368                 ErrPrint("Reconnecting timer is already exists\n");
1369         }
1370
1371         return 0;
1372 }
1373
1374 int client_init(void)
1375 {
1376         s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
1377         if (!s_info.client_addr) {
1378                 s_info.client_addr = strdup(CLIENT_SOCKET);
1379                 if (!s_info.client_addr) {
1380                         ErrPrint("Heap: %s\n", strerror(errno));
1381                         return -ENOMEM;
1382                 }
1383         }
1384
1385         (void)file_service_init();
1386
1387         DbgPrint("Server Address: %s\n", s_info.client_addr);
1388
1389         com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1390         com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1391         if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
1392                 ErrPrint("Failed to add vconf for service state\n");
1393         } else {
1394                 DbgPrint("vconf event callback is registered\n");
1395         }
1396
1397         master_started_cb(NULL, NULL);
1398         return 0;
1399 }
1400
1401 int client_fd(void)
1402 {
1403         return s_info.fd;
1404 }
1405
1406 const char *client_addr(void)
1407 {
1408         return s_info.client_addr;
1409 }
1410
1411 int client_fini(void)
1412 {
1413         int ret;
1414
1415         (void)file_service_fini();
1416
1417         ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
1418         if (ret < 0) {
1419                 DbgPrint("Ignore vconf key: %d\n", ret);
1420         }
1421
1422         com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
1423         com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
1424         com_core_packet_client_fini(s_info.fd);
1425         s_info.fd = -1;
1426         free(s_info.client_addr);
1427         s_info.client_addr = NULL;
1428         return LB_STATUS_SUCCESS;
1429 }
1430
1431 /* End of a file */