Merge "power: add power lock expired popup" into tizen
[platform/core/system/system-popup.git] / src / power / power.c
1 /*
2  *  system-popup
3  *
4  * Copyright (c) 2016 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 #include <app_manager.h>
21 #include "popup-common.h"
22
23 #define APP_PID       "_APP_PID_"
24
25 #define DBUS_POWER_PATH "/Org/Tizen/System/DeviceD/Power"
26 #define DBUS_POWER_IFACE "org.tizen.system.deviced.power"
27 #define METHOD_POWER_LOCK_EXPIRED "PowerLockExpired"
28
29 static const struct popup_ops power_lock_expired_ops;
30 static bool signal_broadcasted;
31
32 enum button_selected {
33         ALLOW_APP,
34         CLOSE_APP,
35 };
36
37 static void send_result_dbus_signal(int result)
38 {
39         int ret;
40         char buf[8];
41         char *param[1];
42
43         if (signal_broadcasted)
44                 return;
45
46         signal_broadcasted = true;
47
48         snprintf(buf, sizeof(buf), "%d", result);
49         param[0] = buf;
50         ret = broadcast_dbus_signal(DBUS_POWER_PATH,
51                         DBUS_POWER_IFACE,
52                         METHOD_POWER_LOCK_EXPIRED,
53                         "i", param);
54         if (ret < 0)
55                 _E("FAIL: broadcast_dbus_signal(%d)", ret);
56 }
57
58 static int power_lock_expired_get_content(const struct popup_ops *ops, char *content, unsigned int len)
59 {
60         char *text, *pid_str, *name;
61         struct object_ops *obj;
62         int ret;
63         int pid;
64
65         if (!ops || !content)
66                 return -EINVAL;
67
68         ret = get_object_by_ops(ops, &obj);
69         if (ret < 0) {
70                 _E("Failed to get object (%d)", ret);
71                 return -ENOENT;
72         }
73
74         pid_str = (char *)bundle_get_val(obj->b, APP_PID);
75         if (!pid_str) {
76                 _E("Failed to get app pid");
77                 return -ENOENT;
78         }
79
80         pid = atoi(pid_str);
81         ret = app_manager_get_app_id(pid, &name);
82         if (ret != APP_MANAGER_ERROR_NONE) {
83                 _E("Failed to get app name(%d)", ret);
84                 return -ENOENT;
85         }
86
87         text = _("\"%s\" is using too much battery power. Close \"%s\" ?");
88
89         snprintf(content, len, text, name, name);
90         free(name);
91
92         return 0;
93 }
94
95 static void power_lock_expired_allow_app(const struct popup_ops *ops)
96 {
97         _I("Allow is selected");
98
99         unload_simple_popup(ops);
100
101         send_result_dbus_signal(ALLOW_APP);
102
103         terminate_if_no_popup();
104 }
105
106 static void power_lock_expired_close_app(const struct popup_ops *ops)
107 {
108         _I("Close is selected");
109
110         unload_simple_popup(ops);
111
112         send_result_dbus_signal(CLOSE_APP);
113
114         terminate_if_no_popup();
115 }
116
117 static void power_lock_expired_terminate(const struct popup_ops *ops)
118 {
119         send_result_dbus_signal(CLOSE_APP);
120         signal_broadcasted = false;
121 }
122
123 static const struct popup_ops power_lock_expired_ops = {
124         .name                   = "power_lock_expired",
125         .show                   = load_simple_popup,
126         .get_content    = power_lock_expired_get_content,
127         .left_text              = "IDS_COM_SK_CANCEL",
128         .left                   = power_lock_expired_allow_app,
129         .right_text             = "IDS_COM_SK_OK",
130         .right                  = power_lock_expired_close_app,
131         .terminate              = power_lock_expired_terminate,
132 };
133
134 /* Constructor to register Power popup */
135 static __attribute__ ((constructor)) void power_register_popup(void)
136 {
137         register_popup(&power_lock_expired_ops);
138 }