Remove trivial unnecessary build dependency
[apps/core/preloaded/lockscreen.git] / src / battery_ctrl.c
1 /*
2  * Copyright (c) 2009-2014 Samsung Electronics Co., Ltd All Rights Reserved
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 <device/battery.h>
18 #include <device/callback.h>
19
20 #include "lockscreen.h"
21 #include "log.h"
22 #include "battery_ctrl.h"
23 #include "battery.h"
24 #include "main_view.h"
25
26 #include <Ecore.h>
27
28 static Ecore_Event_Handler *handler;
29 static Evas_Object *main_view;
30
31
32 // FIXME why this is needed?
33 static char *_replaceString(char *strInput, const char *strTarget, const char *strChange)
34 {
35         char* strResult; char* strTemp;
36         int i = 0;
37         int nCount = 0;
38         int nTargetLength = strlen(strTarget);
39
40         if (nTargetLength < 1) {
41                 ERR("there is no target to chnage");
42                 return NULL;
43         }
44
45         int nChangeLength = strlen(strChange);
46
47         if (nChangeLength != nTargetLength) {
48                 for (i = 0; strInput[i] != '\0';) {
49                         if (memcmp(&strInput[i], strTarget, nTargetLength) == 0) {
50                                 nCount++;               //consider same string exist
51                                 i += nTargetLength;
52                         } else {
53                                 i++;
54                         }
55                 }
56         } else {
57                 i = strlen(strInput);
58         }
59
60         strResult = (char *) malloc(i + 1 + nCount * (nChangeLength - nTargetLength));
61
62         if (!strResult) {
63                 ERR("fail malloc!!");
64                 return NULL;
65         }
66
67         strTemp = strResult;
68         while (*strInput) {
69                 if (memcmp(strInput, strTarget, nTargetLength) == 0) {
70                         memcpy(strTemp, strChange, nChangeLength);
71                         strTemp += nChangeLength;       //move changed length
72                         strInput  += nTargetLength;     // move target length
73                 } else {
74                         *strTemp++ = *strInput++;               // original str cpy
75                 }
76         }
77
78         *strTemp = '\0';
79
80         return strResult;
81 }
82
83 static char *_text_from_percentage(int capacity)
84 {
85         char buff[64];
86         char *newString = NULL;
87         newString = _replaceString(_("IDS_LCKSCN_BODY_CHARGING_C_PDP"), "%d%", "%d%%");
88
89         if (newString != NULL) {
90                 snprintf(buff, sizeof(buff), newString , capacity);
91                 free(newString) ;
92         } else {
93                 snprintf(buff, sizeof(buff), _("IDS_LCKSCN_BODY_CHARGING_C_PDP") , capacity);
94         }
95         return strdup(buff);
96 }
97
98 static int _battery_update(void)
99 {
100         if (lockscreen_battery_is_charging()) {
101                 if (lockscreen_battery_level_get() == 100) {
102                         lockscreen_main_view_battery_status_text_set(main_view, _("IDS_SM_POP_FULLY_CHARGED"));
103                 } else {
104                         char *buff = _text_from_percentage(lockscreen_battery_level_get());
105                         lockscreen_main_view_battery_status_text_set(main_view, buff);
106                         free(buff);
107                 }
108         } else {
109                 if (lockscreen_battery_level_get() == 100 && lockscreen_battery_is_connected()) {
110                         lockscreen_main_view_battery_status_text_set(main_view, _("IDS_SM_POP_FULLY_CHARGED"));
111                 } else {
112                         lockscreen_main_view_battery_status_text_set(main_view, NULL);
113                 }
114         }
115         return 0;
116 }
117
118 static Eina_Bool _data_battery_update(void *data, int event, void *event_info)
119 {
120         _battery_update();
121         return EINA_TRUE;
122 }
123
124 int lock_battery_ctrl_init(Evas_Object *view)
125 {
126         if (lockscreen_battery_init()) {
127                 FAT("lockscreen_battery_init failed. Battery related information will not be available");
128                 return 1;
129         }
130         handler = ecore_event_handler_add(LOCKSCREEN_EVENT_BATTERY_CHANGED, _data_battery_update, NULL);
131         if (!handler)
132                 FAT("ecore_event_handler_add failed on LOCKSCREEN_DATA_MODEL_EVENT_BATTERY_CHANGED event");
133         main_view = view;
134         _battery_update();
135         return 0;
136 }
137
138 void lock_battery_ctrl_fini(void)
139 {
140         ecore_event_handler_del(handler);
141 }