[Request]Update Flora license version
[apps/core/preloaded/libslp-memo.git] / src / memo_dbif.c
1 /*
2 *
3 * Copyright 2012  Samsung Electronics Co., Ltd
4 *
5 * Licensed under the Flora License, Version 1.1 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *    http://floralicense.org/license/
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <limits.h>
23 #include <fcntl.h>
24 #include <vconf.h>
25
26 #include "memo-log.h"
27 #include "memo-db.h"
28 #include "db.h"
29
30 #ifndef MEMOAPI
31 #define MEMOAPI __attribute__ ((visibility("default")))
32 #endif
33
34 #define DB_PREFIX_PATH  "/opt/dbspace"
35 #define DBNAME ".memo.db"
36
37 static void (*g_data_monitor) (void *) = NULL;
38 static int trans_count = 0;
39 static DBHandle *db;
40 static int ref_count = 0;
41
42 /******************************
43 * External API
44 *******************************/
45 /**
46  * @fn            int memo_init(char *dbfile)
47  * @brief        initialize memo db library
48  * @param[in]    dbfile    db file path
49  * @return        Return 0 (Success) or -1 (Failed)
50  */
51 MEMOAPI int memo_init(char *dbfile)
52 {
53     char *name = NULL;
54     char defname[PATH_MAX];
55
56     if(db) {
57         ref_count++;
58         return 0;
59     }
60
61     if(dbfile)
62         name = dbfile;
63
64     if(name == NULL) {
65         snprintf(defname, sizeof(defname), "%s/%s",
66                 DB_PREFIX_PATH, DBNAME);
67         name = defname;
68     }
69
70     DBG("DB name : %s", name);
71     db = db_init(name);
72     retv_if(db == NULL, -1);
73     ref_count++;
74     return 0;
75 }
76
77 /**
78  * @fn            void memo_fini(void)
79  * @brief        terminate memo db library
80  * @return        None
81  */
82 MEMOAPI void memo_fini(void)
83 {
84     ref_count--;
85     if (ref_count == 0) {
86         db_fini(db);
87         db = NULL;
88     }
89 }
90
91 /**
92  * @fn            struct memo_data* memo_create_data()
93  * @brief        create memo data struct
94  * @return        The pointer of created memo data
95  */
96 MEMOAPI struct memo_data* memo_create_data()
97 {
98     return (struct memo_data *)calloc(1, sizeof(struct memo_data));
99 }
100
101 /**
102  * @fn            struct memo_data* memo_get_data(int id)
103  * @brief        Get memo data with specific id
104  * @param[in]    id    db id
105  * @return        The pointer of memo data struct
106  */
107 MEMOAPI struct memo_data* memo_get_data(int id)
108 {
109     int rc;
110     struct memo_data *md;
111
112     retvm_if(db == NULL, NULL, "DB Handle is null, need memo_init");
113     retvm_if(id < 1, NULL, "Invalid memo data id : %d", id);
114
115     md = memo_create_data();
116     retv_if(md == NULL, md);
117
118     rc = get_data(db, id, md);
119     if(rc) {
120         memo_free_data(md);
121         return NULL;
122     }
123
124     return md;
125 }
126
127 /**
128  * @fn            void memo_free_data(struct memo_data *md)
129  * @brief        deallocate memo data
130  * @param[in]    md    memo data
131  * @return        None
132  */
133 MEMOAPI void memo_free_data(struct memo_data *md)
134 {
135     ret_if(md == NULL);
136
137     if(md->content)
138         free(md->content);
139
140     if(md->comment)
141         free(md->comment);
142
143     if(md->doodle_path)
144         free(md->doodle_path);
145
146     free(md);
147 }
148
149 /**
150  * @fn            int memo_add_data(struct memo_data *md)
151  * @brief        insert memo data
152  * @param[in]    md    memo data struct
153  * @return        Return id (Success) or -1 (Failed)
154  */
155 MEMOAPI int memo_add_data(struct memo_data *md)
156 {
157     retvm_if(db == NULL, -1, "DB Handle is null, need memo_init");
158     return insert_data(db, md);
159 }
160
161 /**
162  * @fn            int memo_mod_data(struct memo_data *md)
163  * @brief        Update data in DB
164  * @param[in]    md    The pointer of memo data
165  * @return        Return 0 (Success) or -1 (Failed)
166  */
167 MEMOAPI int memo_mod_data(struct memo_data *md)
168 {
169     retvm_if(db == NULL, -1, "DB Handle is null, need memo_init");
170     retvm_if(md == NULL, -1, "Update data is null");
171     retvm_if(md->id < 1, -1, "Invalid memo data ID");
172     return update_data(db, md);
173 }
174
175 /**
176  * @fn            int memo_del_data(int id)
177  * @brief        remove data of specific id from DB
178  * @param[in]    id    db id
179  * @return        Return 0 (Success) or -1 (Failed)
180  */
181 MEMOAPI int memo_del_data(int id)
182 {
183     char buf[128] = {0};
184     retvm_if(db == NULL, -1, "DB Handle is null, need memo_init");
185     retvm_if(id < 1, -1, "Invalid memo data ID");
186     memo_data_t *md = memo_get_data(id);
187     /* delete doodle */
188     if(md->has_doodle == 1) {
189             snprintf(buf, 128, "/opt/usr/apps/org.tizen.memo/data/doodle/%d.png", id);
190             if(remove(buf) != 0) {
191                 ERR("Remove file error\n");
192                 memo_free_data(md);
193                 return -1;
194             }
195     }
196         memo_free_data(md);
197     return remove_data(db, id);
198 }
199
200 /**
201  * @fn            struct memo_data_list* memo_get_all_data_list()
202  * @brief        Get the all data list
203  * @return        the header of struct memo_data_list linked list
204  */
205 MEMOAPI struct memo_data_list* memo_get_all_data_list(void)
206 {
207     struct memo_data_list *mdl;
208
209     retvm_if(db == NULL, NULL, "DB Handle is null, need memo_init");
210
211     mdl = get_all_data_list(db);
212     return mdl;
213 }
214
215 /**
216  * @fn            void memo_free_data_list(struct memo_data_list *mdl)
217  * @brief        deallocate memo data list
218  * @param[in]    mdl        the pointer of memo_data_list struct
219  * @return        None
220  */
221 MEMOAPI void memo_free_data_list(struct memo_data_list *mdl)
222 {
223     struct memo_data_list *t, *d;
224
225     t = mdl;
226     while(t) {
227         d = t;
228         t = t->next;
229         if(d->md.content) free(d->md.content);
230         free(d);
231     }
232 }
233
234 /**
235  * @fn            time_t memo_get_modified_time(int id)
236  * @brief        Get modified time
237  * @param[in]    id    db id
238  * @return        modified time
239  */
240 MEMOAPI time_t memo_get_modified_time(int id)
241 {
242     retvm_if(db == NULL, -1, "DB Handle is null, need memo_init");
243
244     return get_modtime(db, id);
245 }
246
247 /**
248  * @fn            int memo_get_count(int *count)
249  * @brief        Get number of memo
250  * @return        0 on success
251                  or -1 on fail
252  *
253  * Requested by widget memo
254  * Added by jy.Lee (jaeyong911.lee@samsung.com)
255  */
256 MEMOAPI int memo_get_count(int *count)
257 {
258     int rc;
259     retvm_if(db == NULL, -1, "DB Handle is null, need memo_init");
260     retvm_if(count == NULL, -1, "count pointer is null");
261
262     rc = get_data_count(db, count);
263     if(rc) {
264         return -1;
265     }
266
267     return 0;
268 }
269
270 MEMOAPI struct memo_operation_list* memo_get_operation_list(time_t stamp)
271 {
272     struct memo_operation_list *mol;
273
274     retvm_if(db == NULL, NULL, "DB Handle is null, need memo_init");
275
276     mol = get_operation_list(db, stamp);
277     return mol;
278 }
279
280 MEMOAPI void memo_free_operation_list(struct memo_operation_list *mol)
281 {
282     struct memo_operation_list *t, *d;
283
284     t = mol;
285     while(t) {
286         d = t;
287         t = t->next;
288         free(d);
289     }
290 }
291
292 static void _on_data_change(keynode_t *node, void *user_data)
293 {
294     if (g_data_monitor != NULL) {
295         g_data_monitor(user_data);
296     }
297 }
298
299 MEMOAPI int memo_subscribe_change(void (*cb)(void *), void *user_data)
300 {
301     g_data_monitor = cb;
302     vconf_notify_key_changed(VCONFKEY_MEMO_DATA_CHANGE, _on_data_change, user_data);
303     return 0;
304 }
305
306 MEMOAPI int memo_unsubscribe_change(void (*cb)(void *))
307 {
308     vconf_ignore_key_changed(VCONFKEY_MEMO_DATA_CHANGE, _on_data_change);
309     g_data_monitor = NULL;
310     return 0;
311 }
312
313 MEMOAPI void memo_begin_trans(void)
314 {
315     trans_count++;
316 }
317
318 MEMOAPI void memo_end_trans(void)
319 {
320     int value = 0;
321     trans_count--;
322     if (trans_count == 0) {
323         if(vconf_get_int(VCONFKEY_MEMO_DATA_CHANGE, &value)) {
324             LOGD("vconf_get_int FAIL\n");
325         } else {
326             if (value == 0) {
327                 vconf_set_int(VCONFKEY_MEMO_DATA_CHANGE, 1);
328             } else {
329                 vconf_set_int(VCONFKEY_MEMO_DATA_CHANGE, 0);
330             }
331         }
332     }
333 }
334
335 MEMOAPI int memo_get_indexes(int *aIndex, int len, MEMO_SORT_TYPE sort)
336 {
337     return get_indexes(db, aIndex, len, sort);
338 }
339
340 MEMOAPI int memo_search_data(const char *search_str, int limit, int offset, MEMO_SORT_TYPE sort,
341     memo_data_iterate_cb_t cb, void *user_data)
342 {
343     return search_data(db, search_str, limit, offset, sort, cb, user_data);
344 }
345
346 MEMOAPI int memo_all_data(memo_data_iterate_cb_t cb, void *user_data)
347 {
348     return all_data(db, cb, user_data);
349 }
350