Add notification-ex feature
[platform/core/appfw/data-provider-master.git] / src / main.cc
1 /*
2  * Copyright 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 <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <sys/signalfd.h>
24 #include <signal.h>
25 #include <ctype.h>
26
27 #include <systemd/sd-daemon.h>
28 #include <glib.h>
29 #include <glib-object.h>
30 #include <aul.h>
31 #include <vconf.h>
32 #include <dlog.h>
33 #include <locale.h>
34
35 #include "debug.h"
36 #include "service_common.h"
37 #include "shortcut_service.h"
38 #include "notification_service.h"
39 #include "notification_ex_service.h"
40 #include "badge_service.h"
41
42 static GMainLoop *main_loop;
43 static GIOChannel *channel;
44 static guint source;
45
46 static void lang_key_changed_cb(keynode_t *node, void *first)
47 {
48         char *lang;
49         char *r;
50
51         lang = vconf_get_str(VCONFKEY_LANGSET);
52         if (lang) {
53                 setenv("LANG", lang, 1);
54                 setenv("LC_MESSAGES", lang, 1);
55                 r = setlocale(LC_ALL, "");
56                 if (r == NULL) {
57                         r = setlocale(LC_ALL, lang);
58                         if (r != NULL)
59                                 DBG("setlocale = %s", r);
60                 }
61                 DBG("setlocale [%s]", r);
62                 free(lang);
63         }
64 }
65
66 static inline int app_create(void)
67 {
68         int ret;
69
70         ret = vconf_notify_key_changed(VCONFKEY_LANGSET, lang_key_changed_cb, NULL);
71         if (ret < 0)
72                 DBG("VCONFKEY_LANGSET notify key chenaged [%d]", ret);
73
74         lang_key_changed_cb(NULL, NULL);
75
76         ret = notification_ex_service_init();
77         if (ret < 0)
78                 WARN("notification_ex [%d]", ret);
79
80         service_common_set_connection(
81                 notification_ex_service_get_gdbus_connection());
82
83         ret = shortcut_service_init();
84         if (ret < 0)
85                 WARN("shortcut [%d]", ret);
86
87         ret = notification_service_init();
88         if (ret < 0)
89                 WARN("notification [%d]", ret);
90
91         ret = badge_service_init();
92         if (ret < 0)
93                 WARN("badge [%d]", ret);
94
95         service_common_init();
96         INFO("Successfully initialized");
97
98         return 0;
99 }
100
101 static inline int app_terminate(void)
102 {
103         int ret;
104
105         ret = badge_service_fini();
106         if (ret < 0)
107                 DBG("badge [%d]", ret);
108
109         ret = notification_service_fini();
110         if (ret < 0)
111                 DBG("notification [%d]", ret);
112
113         ret = shortcut_service_fini();
114         if (ret < 0)
115                 DBG("shortcut [%d]", ret);
116
117         return 0;
118 }
119
120 static gboolean __signal_handler(GIOChannel *channel,
121                 GIOCondition cond, gpointer data)
122 {
123         struct signalfd_siginfo fdsi;
124         ssize_t size;
125         int sfd;
126
127         sfd = g_io_channel_unix_get_fd(channel);
128
129         size = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
130         if (size != sizeof(struct signalfd_siginfo)) {
131                 ERR("signal read failed [%d]", errno);
132                 return TRUE;
133         }
134
135         if (fdsi.ssi_signo == SIGTERM) {
136                 ERR("Terminated(SIGTERM)");
137                 g_main_loop_quit(main_loop);
138         } else {
139                 ERR("Unknown SIG[%d] received", fdsi.ssi_signo);
140         }
141
142         return TRUE;
143 }
144
145 static int __init_signal_handler(void)
146 {
147         sigset_t mask;
148         int sfd;
149         int ret;
150
151         ret = sigemptyset(&mask);
152         if (ret < 0) {
153                 ERR("sigemptyset : %d", errno);
154                 return -1;
155         }
156
157         ret = sigaddset(&mask, SIGTERM);
158         if (ret < 0) {
159                 ERR("sigaddset: %d", errno);
160                 return -1;
161         }
162
163         ret = sigprocmask(SIG_BLOCK, &mask, NULL);
164         if (ret < 0) {
165                 ERR("sigprocmask: %d", errno);
166                 return -1;
167         }
168
169         sfd = signalfd(-1, &mask, SFD_NONBLOCK);
170         if (sfd < 0) {
171                 ERR("signalfd: %d", errno);
172                 return -1;
173         }
174
175         channel = g_io_channel_unix_new(sfd);
176         g_io_channel_set_close_on_unref(channel, TRUE);
177         g_io_channel_set_encoding(channel, NULL, NULL);
178         g_io_channel_set_buffered(channel, FALSE);
179
180         source = g_io_add_watch(channel, G_IO_IN, __signal_handler, NULL);
181
182         INFO("source[%u]", source);
183
184         return 0;
185 }
186
187 void __finish(void)
188 {
189         if (source > 0)
190                 g_source_remove(source);
191         if (channel)
192                 g_io_channel_unref(channel);
193
194         app_terminate();
195 }
196
197 int main(int argc, char *argv[])
198 {
199         int restart_count = 0;
200
201         main_loop = g_main_loop_new(NULL, FALSE);
202         if (!main_loop)
203                 return -1;
204
205 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
206         g_type_init();
207 #endif
208
209         vconf_get_int(VCONFKEY_MASTER_RESTART_COUNT, &restart_count);
210
211         app_create();
212         sd_notify(0, "READY=1");
213
214         restart_count++;
215         vconf_set_int(VCONFKEY_MASTER_RESTART_COUNT, restart_count);
216
217         if (__init_signal_handler() < 0)
218                 ERR("Failed to init signal handler");
219
220         g_main_loop_run(main_loop);
221
222         __finish();
223
224         g_main_loop_unref(main_loop);
225
226         return 0;
227 }
228
229 /* End of a file */