Apply consistent log messages.
[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 #define APP_COMM      "_APP_COMM_"
25 #define REQ_ID        "_REQUEST_ID_"
26
27 #define DBUS_POWER_BUS   "org.tizen.system.deviced"
28 #define DBUS_POWER_PATH  "/Org/Tizen/System/DeviceD/Display"
29 #define DBUS_POWER_IFACE "org.tizen.system.deviced.display"
30 #define METHOD_LOCK_TIMEOUT_INPUT "LockTimeoutInput"
31
32 static const struct popup_ops power_lock_expired_ops;
33 static bool result_sended;
34 static char *request_id;
35
36 enum button_selected {
37         ALLOW_APP,
38         RESTRICT_APP,
39         CLOSE_APP,
40 };
41
42 static void send_result(int result)
43 {
44         int ret;
45         char buf[8];
46         char *param[2];
47
48         if (result_sended)
49                 return;
50
51         result_sended = true;
52
53         param[0] = request_id;
54         snprintf(buf, sizeof(buf), "%d", result);
55         param[1] = buf;
56         ret = popup_dbus_method_sync(
57                         DBUS_POWER_BUS,
58                         DBUS_POWER_PATH,
59                         DBUS_POWER_IFACE,
60                         METHOD_LOCK_TIMEOUT_INPUT,
61                         "si", param);
62         if (ret < 0)
63                 _E("Failed to deliver the result: %d", ret);
64 }
65
66 static int power_lock_expired_get_content(const struct popup_ops *ops, char *content, unsigned int len)
67 {
68         char *text, *pid_str, *name;
69         struct object_ops *obj;
70         int ret;
71         int pid;
72
73         if (!ops || !content)
74                 return -EINVAL;
75
76         ret = get_object_by_ops(ops, &obj);
77         if (ret < 0) {
78                 _E("Failed to get object: %d", ret);
79                 return -ENOENT;
80         }
81
82         pid_str = (char *)bundle_get_val(obj->b, APP_PID);
83         if (!pid_str) {
84                 _E("Failed to get app pid.");
85                 return -ENOENT;
86         }
87
88         request_id = (char *)bundle_get_val(obj->b, REQ_ID);
89         if (!request_id) {
90                 _E("Failed to get request id.");
91                 return -ENOENT;
92         }
93
94         pid = atoi(pid_str);
95         ret = app_manager_get_app_id(pid, &name);
96         if (ret != APP_MANAGER_ERROR_NONE) {
97                 name = (char *)bundle_get_val(obj->b, APP_COMM);
98                 if (!name) {
99                         _E("Failed to get app command.");
100                 return -ENOENT;
101         }
102         }
103
104         text = _("\"%s\" is using too much battery power. Close \"%s\" ?");
105
106         snprintf(content, len, text, name, name);
107         free(name);
108
109         return 0;
110 }
111
112 static void power_lock_expired_allow_app(const struct popup_ops *ops)
113 {
114         _I("Allow is selected.");
115
116         unload_simple_popup(ops);
117
118         send_result(ALLOW_APP);
119
120         terminate_if_no_popup();
121 }
122
123 static void power_lock_expired_close_app(const struct popup_ops *ops)
124 {
125         _I("Close is selected.");
126
127         unload_simple_popup(ops);
128
129         send_result(CLOSE_APP);
130
131         terminate_if_no_popup();
132 }
133
134 static void power_lock_expired_terminate(const struct popup_ops *ops)
135 {
136         send_result(CLOSE_APP);
137         result_sended = false;
138 }
139
140 static const struct popup_ops power_lock_expired_ops = {
141         .name                   = "power_lock_expired",
142         .pattern                = FEEDBACK_PATTERN_LOWBATT,
143         .show                   = load_simple_popup,
144         .get_content    = power_lock_expired_get_content,
145         .left_text              = "IDS_COM_SK_CANCEL",
146         .left                   = power_lock_expired_allow_app,
147         .right_text             = "IDS_COM_SK_OK",
148         .right                  = power_lock_expired_close_app,
149         .terminate              = power_lock_expired_terminate,
150 };
151
152 /* Constructor to register Power popup */
153 static __attribute__ ((constructor)) void power_register_popup(void)
154 {
155         register_popup(&power_lock_expired_ops);
156 }