tizen 2.3 release
[kernel/api/system-resource.git] / src / network / notification.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 /*
21  * @file notification.c
22  *
23  * @desc Notification specific functions
24  *
25  * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
26  *
27  */
28
29 #include <resourced.h>
30
31 #include "edbus-handler.h"
32 #include "notification.h"
33 #include "trace.h"
34 #include "macro.h"
35 #include "roaming.h"
36 #include "datausage-vconf-common.h"
37
38 #define RESTRICTION_ACTIVE "RestrictionActive"
39 #define RESTRICTION_WARNING "RestrictionWarning"
40
41 #define NOTI_KEY                "_SYSPOPUP_CONTENT_"
42 #define NOTI_KEY_LIMIT  "_DATAUSAGE_LIMIT_"
43 #define NOTI_VALUE_DISABLED     "datausage_disabled"
44 #define NOTI_VALUE_WARNING      "datausage_warning"
45
46 #define METHOD_CALL_POPUP "DatausagePopupLaunch"
47
48 static int show_restriction_popup(const char *value)
49 {
50         char buf[MAX_DEC_SIZE(int)];
51         char str_val[32];
52         char *pa[4];
53         int ret, retval, quota_limit = -1;
54
55         if (restriction_check_limit_status(&retval) < 0)
56                 _E("Failed to check limit status");
57
58         if (!retval) {
59                 _E("data usage limit is not set");
60                 return RESOURCED_ERROR_FAIL;
61         }
62
63         if (restriction_read_quota(&quota_limit) < 0)
64                 _E("Failed to read a quota value");
65
66         if (quota_limit <= 0) {
67                 _D("quota_limit is invalid\n");
68                 return RESOURCED_ERROR_FAIL;
69         }
70
71         snprintf(str_val, sizeof(str_val), "%s", value);
72         snprintf(buf, sizeof(buf), "%d", quota_limit);
73
74         pa[0] = NOTI_KEY;
75         pa[1] = str_val;
76         pa[2] = NOTI_KEY_LIMIT;
77         pa[3] = buf;
78
79         ret = dbus_method_async(SYSTEM_POPUP_BUS_NAME, SYSTEM_POPUP_PATH_WATCHDOG, SYSTEM_POPUP_IFACE_WATCHDOG, METHOD_CALL_POPUP, "ssss", pa);
80         if (ret < 0)
81                 _E("no message : failed to setting %d", ret);
82         return ret;
83 }
84
85 void send_restriction_notification(const char *appid)
86 {
87         if (broadcast_edbus_signal(RESOURCED_PATH_NETWORK,
88                                    RESOURCED_INTERFACE_NETWORK,
89                                    RESTRICTION_ACTIVE,
90                                    DBUS_TYPE_STRING,
91                                    (void *)(&appid)) != RESOURCED_ERROR_NONE) {
92                 _E("Failed to send DBUS message.");
93         }
94
95         restriction_set_status(RESTRICTION_STATE_SET);
96
97         _I("Show a network disabled popup");
98         show_restriction_popup(NOTI_VALUE_DISABLED);
99 }
100
101 void send_restriction_warn_notification(const char *appid)
102 {
103         if (broadcast_edbus_signal(RESOURCED_PATH_NETWORK,
104                                    RESOURCED_INTERFACE_NETWORK,
105                                    RESTRICTION_WARNING,
106                                    DBUS_TYPE_STRING,
107                                    (void *)(&appid)) != RESOURCED_ERROR_NONE) {
108                 _E("Failed to send DBUS message.");
109         }
110
111         _I("Show a network warning popup");
112         show_restriction_popup(NOTI_VALUE_WARNING);
113 }
114