Add net.stc-popup application with syspopup_launch and PO files
[platform/core/connectivity/stc-manager.git] / plugin / stc-popup / stc-plugin-stc-popup.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <signal.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
21 #include <string.h>
22
23 #include <bundle.h>
24 #include <bundle_internal.h>
25 #include <syspopup_caller.h>
26
27 #include "stc-plugin-popup.h"
28 #include "stc-error.h"
29 #include "stc-manager-util.h"
30
31 #define STC_SYSPOPUP_PACKAGE_NAME "net.stc-popup"
32 #define STC_SYSPOPUP_RETRY_MAX 10
33 #define STC_SYSPOPUP_TIMER 10
34
35 static gboolean __stc_syspopup_timer_cb(gpointer user_data)
36 {
37         __STC_LOG_FUNC_ENTER__;
38         int ret;
39         static int retry_count;
40         bundle *b = (bundle *)user_data;
41         if (user_data == NULL)
42                 return FALSE;
43
44         ++retry_count;
45
46         ret = syspopup_launch(STC_SYSPOPUP_PACKAGE_NAME, b);
47         if (ret < 0) {
48                 STC_LOGE("Sorry! Can't launch popup, ret=%d, Re-try[%d] time..",
49                          ret, retry_count);
50                 if (retry_count >= STC_SYSPOPUP_RETRY_MAX) {
51                         STC_LOGE("Sorry!! Max retry %d reached", retry_count);
52                         bundle_free(b);
53                         retry_count = 0;
54                         return FALSE;
55                 }
56         } else {
57                 STC_LOGD("Hurray!! Finally Popup launched");
58                 retry_count = 0;
59                 bundle_free(b);
60         }
61
62         __STC_LOG_FUNC_EXIT__;
63         return (ret < 0) ? TRUE : FALSE;
64 }
65
66 int stc_plugin_popup_initialize(void)
67 {
68         return STC_ERROR_NONE;
69 }
70
71 int stc_plugin_popup_deinitialize(void)
72 {
73         return STC_ERROR_NONE;
74 }
75
76 int stc_plugin_popup_show(const char *content, const char *type,
77                           const char *app_id, const char *iftype,
78                           const char *limit)
79 {
80         __STC_LOG_FUNC_ENTER__;
81
82         int ret = 0;
83         bundle *b = bundle_create();
84         if (b == NULL) {
85                 __STC_LOG_FUNC_EXIT__;
86                 return STC_ERROR_FAIL;
87         }
88
89         bundle_add(b, "_SYSPOPUP_CONTENT_", content);
90         bundle_add(b, "_SYSPOPUP_TYPE_", type);
91         bundle_add(b, "_APP_ID_", app_id);
92         bundle_add(b, "_IF_TYPE_", iftype);
93
94         if (g_strcmp0(type, "warning_noti") == 0) {
95                 STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] limit[%s]",
96                          content, type, app_id, limit);
97
98                 bundle_add(b, "_WARN_LIMIT_", limit);
99         } else {
100                 STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]",
101                          content, type, app_id, limit);
102
103                 bundle_add(b, "_RESTRICTION_LIMIT_", limit);
104         }
105
106         ret = syspopup_launch("net.stc-popup", b);
107         if (ret < 0) {
108                 g_timeout_add_seconds(STC_SYSPOPUP_TIMER, __stc_syspopup_timer_cb, b);
109                 STC_LOGE("Popup launch failed...retry %d", ret);
110                 return STC_ERROR_NONE;
111         }
112
113         bundle_free(b);
114         __STC_LOG_FUNC_EXIT__;
115         return STC_ERROR_NONE;
116 }
117
118 API stc_plugin_popup_s stc_plugin_popup = {
119         .initialize_plugin =
120                 stc_plugin_popup_initialize,
121         .deinitialize_plugin =
122                 stc_plugin_popup_deinitialize,
123         .show_popup = stc_plugin_popup_show
124 };