Fix the prevent issue (CID 43204)
[framework/connectivity/bluetooth-share.git] / bt-share / src / bt-share-common.c
1 /*
2  * bluetooth-share
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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 <pmapi.h>
21 #include <glib.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <vconf.h>
25 #include <Ecore_File.h>
26
27 #include "vconf-keys.h"
28 #include "applog.h"
29 #include "bt-share-common.h"
30
31 int _bt_share_block_sleep(gboolean is_block)
32 {
33         static int block_sleep_count = 0;
34         int result = -1;
35
36         DBG("is_block [%d], block_sleep_count[%d]\n", is_block,
37                      block_sleep_count);
38
39         if (is_block) {
40                 if (block_sleep_count < 0) {
41                         DBG("block_sleep_count[%d] is invalid. It is set to 0.\n",
42                                      block_sleep_count);
43                         block_sleep_count = 0;
44                 } else if (block_sleep_count == 0) {
45                         result = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
46                         if (result != 0) {
47                                 DBG("LCD Lock is failed with result code [%d]\n", result);
48                         }
49                 } else {
50                         result = 0;
51                 }
52
53                 if (result == 0) {
54                         block_sleep_count++;
55                 }
56         } else {
57                 if (block_sleep_count <= 0) {
58                         DBG("block_sleep_count[%d] is invalid. It is set to 0.\n",
59                                      block_sleep_count);
60                         block_sleep_count = 0;
61                 } else if (block_sleep_count == 1) {
62                         result = pm_unlock_state(LCD_OFF, PM_RESET_TIMER);
63                         if (result != 0) {
64                                 DBG("LCD Unlock is failed with result code [%d]\n",
65                                              result);
66                         }
67                 } else {
68                         result = 0;
69                 }
70
71                 if (result == 0) {
72                         block_sleep_count--;
73                 }
74         }
75
76         DBG("result [%d]\n", result);
77         return result;
78 }
79
80
81
82 int _bt_set_transfer_indicator(gboolean state)
83 {
84         int bt_device_state;
85         static int block_cnt = 0;
86         int ret;
87
88         ret = vconf_get_int(VCONFKEY_BT_STATUS, (void *)&bt_device_state);
89         if (ret != 0) {
90                 DBG("Get vconf failed\n");
91                 return -1;
92         }
93
94         if(state == TRUE) {
95                 block_cnt++;
96                 if(bt_device_state & BT_STATUS_TRANSFER)
97                         return 0;
98                 bt_device_state |= BT_STATUS_TRANSFER;
99         } else {
100                 if(block_cnt > 0)
101                         block_cnt--;
102                 if(block_cnt != 0)
103                         return 0;
104                 bt_device_state ^= BT_STATUS_TRANSFER;
105         }
106
107         ret = vconf_set_int(VCONFKEY_BT_STATUS, bt_device_state);
108         if (ret != 0) {
109                 DBG("Set vconf failed\n");
110                 return -1;
111         }
112         return 0;
113 }
114
115 static char *__bt_share_get_transfer_file_name(int file_type)
116 {
117         int count = 0;
118         char *appendix;
119         char *file;
120         char *file_format;
121         char *file_name;
122
123         if (file_type == BT_HTTP_FILE) {
124                 file_name = HTML_FILE_NAME;
125                 file_format = HTML_FILE_FORMAT;
126         } else {
127                 file_name = TXT_FILE_NAME;
128                 file_format = TXT_FILE_FORMAT;
129         }
130
131         file = g_strdup(file_name);
132
133         /* While the file exists, increase the file name */
134         while (access(file, F_OK) == 0) {
135                 g_free(file);
136
137                 appendix = g_strdup_printf("_%d", count);
138                 file = g_strdup_printf(file_format, appendix);
139                 g_free(appendix);
140
141                 count++;
142         }
143
144         return file;
145 }
146
147 void _bt_remove_tmp_file(char *file_path)
148 {
149         if (g_str_has_prefix(file_path, BT_TMP_FILE) == TRUE) {
150                 DBG("Remove the file: %s", file_path);
151                 ecore_file_remove(file_path);
152         }
153 }
154
155 char *_bt_share_create_transfer_file(char *text)
156 {
157         int fd;
158         int file_type;
159         char *file = NULL;
160         char *content;
161         char *url_sheme;
162         ssize_t write_size;
163
164         retv_if(text == NULL, NULL);
165
166         url_sheme = g_uri_parse_scheme(text);
167
168         if (url_sheme) {
169                 /* HTTP file generate */
170                 g_free(url_sheme);
171                 file_type = BT_HTTP_FILE;
172         } else {
173                 /* TXT file generate */
174                 file_type = BT_TXT_FILE;
175         }
176
177         file = __bt_share_get_transfer_file_name(file_type);
178         retv_if(file == NULL, NULL);
179
180         fd = open(file, O_RDWR | O_CREAT, 0755);
181
182         if (fd < 0) {
183                 ERR("Fail to open the file");
184                 goto fail;
185         }
186
187         if (file_type == BT_HTTP_FILE) {
188                 content = g_strdup_printf(HTML_FORMAT, text, text);
189         } else {
190                 content = g_strdup(text);
191         }
192
193         DBG("content: \n%s", content);
194
195         write_size = write(fd, content, strlen(content));
196         g_free(content);
197         close(fd);
198
199         if (write_size < 0) {
200                 ERR("Fail to write in file");
201                 goto fail;
202         }
203
204         return file;
205 fail:
206         g_free(file);
207         return NULL;
208 }
209