remove resource leak detected by prevent
[platform/core/api/favorites.git] / src / favorites_bookmark.c
1 /*\r
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the License);\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an AS IS BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License. \r
15  */\r
16 \r
17 #include <string.h>\r
18 #include <dlog.h>\r
19 #include <db-util.h>\r
20 #include <favorites.h>\r
21 #include <favorites_private.h>\r
22 #if defined(BROWSER_BOOKMARK_SYNC)\r
23 #include "bookmark-adaptor.h"\r
24 #include "time.h"\r
25 #endif\r
26 \r
27 sqlite3 *gl_internet_bookmark_db = 0;\r
28 \r
29 /* Private Functions */\r
30 void _favorites_close_bookmark_db(void)\r
31 {\r
32         if (gl_internet_bookmark_db) {\r
33                 if (db_util_close(gl_internet_bookmark_db) != DB_UTIL_OK) {\r
34                         FAVORITES_LOGE("db_util_close is failed(%s).\n",\r
35                         sqlite3_errmsg(gl_internet_bookmark_db));\r
36                 }\r
37                 gl_internet_bookmark_db = 0;\r
38         }\r
39 }\r
40 \r
41 void _favorites_finalize_bookmark_db(sqlite3_stmt *stmt)\r
42 {\r
43         if (sqlite3_finalize(stmt) != SQLITE_OK) {\r
44                 FAVORITES_LOGE("sqlite3_finalize is failed");\r
45         }\r
46         _favorites_close_bookmark_db();\r
47 }\r
48 const char *_favorites_get_bookmark_db_name(void)\r
49 {\r
50         return "/opt/usr/dbspace/.internet_bookmark.db";\r
51 }\r
52 int _favorites_open_bookmark_db(void)\r
53 {\r
54         _favorites_close_bookmark_db();\r
55         if (db_util_open\r
56             (_favorites_get_bookmark_db_name(), &gl_internet_bookmark_db,\r
57              DB_UTIL_REGISTER_HOOK_METHOD) != SQLITE_OK) {\r
58                 if (db_util_close(gl_internet_bookmark_db) != DB_UTIL_OK) {\r
59                         FAVORITES_LOGE("db_util_close is failed(%s).\n",\r
60                         sqlite3_errmsg(gl_internet_bookmark_db));\r
61                 }\r
62                 gl_internet_bookmark_db = 0;\r
63                 return -1;\r
64         }\r
65         return gl_internet_bookmark_db ? 0 : -1;\r
66 }\r
67 \r
68 void _favorites_free_bookmark_list(bookmark_list_h m_list)\r
69 {\r
70         FAVORITES_LOGE(" ");\r
71 \r
72         int i = 0;\r
73         if (m_list == NULL)\r
74                 return;\r
75 \r
76         if (m_list->item != NULL) {\r
77                 for (i = 0; i < m_list->count; i++) {\r
78                         if (m_list->item[i].address != NULL)\r
79                                 free(m_list->item[i].address);\r
80                         if (m_list->item[i].title != NULL)\r
81                                 free(m_list->item[i].title);\r
82                         if (m_list->item[i].creationdate != NULL)\r
83                                 free(m_list->item[i].creationdate);\r
84                         if (m_list->item[i].updatedate != NULL)\r
85                                 free(m_list->item[i].updatedate);\r
86                 }\r
87                 free(m_list->item);\r
88         }\r
89         free(m_list);\r
90         m_list = NULL;\r
91 }\r
92 \r
93 int _favorites_free_bookmark_entry(favorites_bookmark_entry_s *entry)\r
94 {\r
95         FAVORITES_NULL_ARG_CHECK(entry);\r
96 \r
97         if (entry->address != NULL)\r
98                 free(entry->address);\r
99         if (entry->title != NULL)\r
100                 free(entry->title);\r
101         if (entry->creation_date != NULL)\r
102                 free(entry->creation_date);\r
103         if (entry->update_date != NULL)\r
104                 free(entry->update_date);\r
105 \r
106         return FAVORITES_ERROR_NONE;\r
107 }\r
108 \r
109 /* search last of sequence(order's index) */\r
110 int _favorites_get_bookmark_lastindex(int locationId)\r
111 {\r
112         int nError;\r
113         sqlite3_stmt *stmt;\r
114 \r
115         if (_favorites_open_bookmark_db() < 0) {\r
116                 FAVORITES_LOGE("db_util_open is failed\n");\r
117                 return -1;\r
118         }\r
119 \r
120         nError =\r
121             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
122                                "select sequence from bookmarks where parent=? order by sequence desc",\r
123                                -1, &stmt, NULL);\r
124         if (nError != SQLITE_OK) {\r
125                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
126                         sqlite3_errmsg(gl_internet_bookmark_db));\r
127                 _favorites_finalize_bookmark_db(stmt);\r
128                 return -1;\r
129         }\r
130         if (sqlite3_bind_int(stmt, 1, locationId) != SQLITE_OK) {\r
131                 FAVORITES_LOGE("sqlite3_bind_int is failed");\r
132                 _favorites_finalize_bookmark_db(stmt);\r
133                 return -1;\r
134         }\r
135 \r
136         if ((nError = sqlite3_step(stmt)) == SQLITE_ROW) {\r
137                 int index = sqlite3_column_int(stmt, 0);\r
138                 _favorites_finalize_bookmark_db(stmt);\r
139                 return index;\r
140         } else if (nError == SQLITE_DONE) {\r
141                 FAVORITES_LOGE("Not found items in This Folder");\r
142                 _favorites_finalize_bookmark_db(stmt);\r
143                 return 0;\r
144         }\r
145         _favorites_close_bookmark_db();\r
146         return -1;\r
147 }\r
148 \r
149 int _favorites_get_bookmark_count_at_folder(int folderId)\r
150 {\r
151         int nError;\r
152         sqlite3_stmt *stmt;\r
153         FAVORITES_LOGE("");\r
154         \r
155         if (_favorites_open_bookmark_db() < 0) {\r
156                 FAVORITES_LOGE("db_util_open is failed\n");\r
157                 return -1;\r
158         }\r
159 \r
160         /*bookmark */\r
161         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
162                                "select count(*) from bookmarks where parent=? and type=0",\r
163                                -1, &stmt, NULL);\r
164         if (nError != SQLITE_OK) {\r
165                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
166                         sqlite3_errmsg(gl_internet_bookmark_db));\r
167                 _favorites_finalize_bookmark_db(stmt);\r
168                 return -1;\r
169         }\r
170 \r
171         if (sqlite3_bind_int(stmt, 1, folderId) != SQLITE_OK) {\r
172                 FAVORITES_LOGE("sqlite3_bind_int is failed");\r
173                 _favorites_finalize_bookmark_db(stmt);\r
174                 return -1;\r
175         }\r
176 \r
177         nError = sqlite3_step(stmt);\r
178         if (nError == SQLITE_ROW) {\r
179                 int count = sqlite3_column_int(stmt, 0);\r
180                 _favorites_finalize_bookmark_db(stmt);\r
181                 FAVORITES_LOGE("count: %d", count);\r
182                 return count;\r
183         } else if (nError == SQLITE_DONE) {\r
184                 _favorites_finalize_bookmark_db(stmt);\r
185                 return 0;\r
186         }\r
187         _favorites_close_bookmark_db();\r
188         return -1;\r
189 }\r
190 \r
191 int _favorites_bookmark_get_folder_count(void)\r
192 {\r
193         int nError;\r
194         sqlite3_stmt *stmt;\r
195         FAVORITES_LOGE("");\r
196         \r
197         if (_favorites_open_bookmark_db() < 0) {\r
198                 FAVORITES_LOGE("db_util_open is failed\n");\r
199                 return -1;\r
200         }\r
201 \r
202         /* folder + bookmark */\r
203         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
204                                "select count(*) from bookmarks where type=1",\r
205                                -1, &stmt, NULL);\r
206         if (nError != SQLITE_OK) {\r
207                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
208                         sqlite3_errmsg(gl_internet_bookmark_db));\r
209                 _favorites_finalize_bookmark_db(stmt);\r
210                 return -1;\r
211         }\r
212 \r
213         nError = sqlite3_step(stmt);\r
214         if (nError == SQLITE_ROW) {\r
215                 int count = sqlite3_column_int(stmt, 0);\r
216                 _favorites_finalize_bookmark_db(stmt);\r
217                 return count;\r
218         } else if (nError == SQLITE_DONE) {\r
219                 _favorites_finalize_bookmark_db(stmt);\r
220                 return 0;\r
221         }\r
222         _favorites_close_bookmark_db();\r
223         return -1;\r
224 }\r
225 \r
226 bookmark_list_h _favorites_get_bookmark_list_at_folder(int folderId)\r
227 {\r
228         bookmark_list_h m_list = NULL;\r
229         int nError;\r
230         sqlite3_stmt *stmt;\r
231         char    query[1024];\r
232 \r
233         FAVORITES_LOGE("folderId: %d", folderId);\r
234         if(folderId<=0){\r
235                 FAVORITES_LOGE("folderId is wrong");\r
236                 return NULL;\r
237         }\r
238 \r
239         memset(&query, 0x00, sizeof(char)*1024);\r
240 \r
241         /* check the total count of items */\r
242         int item_count = 0;\r
243         item_count = _favorites_get_bookmark_count_at_folder(folderId);\r
244 \r
245         if (item_count <= 0)\r
246                 return NULL;\r
247 \r
248         /* Get bookmarks list only under given folder */\r
249         sprintf(query, "select id, type, parent, address, title, editable,\\r
250                                creationdate, updatedate, sequence \\r
251                                from bookmarks where type=0 and parent =%d order by sequence"\r
252                         , folderId);\r
253         FAVORITES_LOGE("query: %s", query);\r
254 \r
255         if (_favorites_open_bookmark_db() < 0) {\r
256                 FAVORITES_LOGE("db_util_open is failed\n");\r
257                 return NULL;\r
258         }\r
259         \r
260         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
261                                query, -1, &stmt, NULL);\r
262         if (nError != SQLITE_OK) {\r
263                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
264                         sqlite3_errmsg(gl_internet_bookmark_db));\r
265                 _favorites_finalize_bookmark_db(stmt);\r
266                 return NULL;\r
267         }\r
268 \r
269         /*  allocation .... Array for Items */\r
270         m_list = (bookmark_list_h) calloc(1, sizeof(bookmark_list_s));\r
271         m_list->item =\r
272             (bookmark_entry_internal_h) calloc(item_count, sizeof(bookmark_entry_internal_s));\r
273         m_list->count = item_count;\r
274         int i = 0;\r
275         while ((nError = sqlite3_step(stmt)) == SQLITE_ROW \r
276                 && (i < item_count)) {\r
277                 m_list->item[i].id = sqlite3_column_int(stmt, 0);\r
278                 m_list->item[i].is_folder = sqlite3_column_int(stmt, 1);\r
279                 m_list->item[i].folder_id = sqlite3_column_int(stmt, 2);\r
280 \r
281                 if (!m_list->item[i].is_folder) {\r
282                         const char *url =\r
283                             (const char *)(sqlite3_column_text(stmt, 3));\r
284                         m_list->item[i].address = NULL;\r
285                         if (url) {\r
286                                 int length = strlen(url);\r
287                                 if (length > 0) {\r
288                                         m_list->item[i].address =\r
289                                             (char *)calloc(length + 1,\r
290                                                            sizeof(char));\r
291                                         memcpy(m_list->item[i].address, url,\r
292                                                length);\r
293                                 }\r
294                         }\r
295                 }\r
296 \r
297                 const char *title =\r
298                     (const char *)(sqlite3_column_text(stmt, 4));\r
299                 m_list->item[i].title = NULL;\r
300                 if (title) {\r
301                         int length = strlen(title);\r
302                         if (length > 0) {\r
303                                 m_list->item[i].title =\r
304                                     (char *)calloc(length + 1, sizeof(char));\r
305                                 memcpy(m_list->item[i].title, title, length);\r
306                         }\r
307                         FAVORITES_LOGE("Bookmark Title:%s\n", m_list->item[i].title);\r
308                 }\r
309                 m_list->item[i].editable = sqlite3_column_int(stmt, 5);\r
310 \r
311                 const char *creationdate =\r
312                     (const char *)(sqlite3_column_text(stmt, 6));\r
313                 m_list->item[i].creationdate = NULL;\r
314                 if (creationdate) {\r
315                         int length = strlen(creationdate);\r
316                         if (length > 0) {\r
317                                 m_list->item[i].creationdate =\r
318                                     (char *)calloc(length + 1, sizeof(char));\r
319                                 memcpy(m_list->item[i].creationdate,\r
320                                        creationdate, length);\r
321                         }\r
322                 }\r
323                 const char *updatedate =\r
324                     (const char *)(sqlite3_column_text(stmt, 7));\r
325                 m_list->item[i].updatedate = NULL;\r
326                 if (updatedate) {\r
327                         int length = strlen(updatedate);\r
328                         if (length > 0) {\r
329                                 m_list->item[i].updatedate =\r
330                                     (char *)calloc(length + 1, sizeof(char));\r
331                                 memcpy(m_list->item[i].updatedate, updatedate,\r
332                                        length);\r
333                         }\r
334                 }\r
335 \r
336                 m_list->item[i].orderIndex = sqlite3_column_int(stmt, 8);\r
337                 i++;\r
338         }\r
339         m_list->count = i;\r
340 \r
341         if (i <= 0) {\r
342                 FAVORITES_LOGE("sqlite3_step is failed");\r
343                 _favorites_close_bookmark_db();\r
344                 _favorites_free_bookmark_list(m_list);\r
345                 return NULL;\r
346         }\r
347         _favorites_finalize_bookmark_db(stmt);\r
348         return m_list;\r
349 }\r
350 \r
351 bookmark_list_h _favorites_bookmark_get_folder_list(void)\r
352 {\r
353         bookmark_list_h m_list = NULL;\r
354         int nError;\r
355         sqlite3_stmt *stmt;\r
356         char    query[1024];\r
357 \r
358         FAVORITES_LOGE("");\r
359 \r
360         memset(&query, 0x00, sizeof(char)*1024);\r
361 \r
362         /* check the total count of items */\r
363         int item_count = 0;\r
364         item_count = _favorites_bookmark_get_folder_count();\r
365 \r
366         if (item_count <= 0)\r
367                 return NULL;\r
368 \r
369         /* Get bookmarks list only under given folder */\r
370         sprintf(query, "select id, type, parent, address, title, editable,\\r
371                                creationdate, updatedate, sequence \\r
372                                from bookmarks where type=1 order by sequence");\r
373         FAVORITES_LOGE("query: %s", query);\r
374 \r
375         if (_favorites_open_bookmark_db() < 0) {\r
376                 FAVORITES_LOGE("db_util_open is failed\n");\r
377                 return NULL;\r
378         }\r
379         \r
380         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
381                                query, -1, &stmt, NULL);\r
382         if (nError != SQLITE_OK) {\r
383                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
384                         sqlite3_errmsg(gl_internet_bookmark_db));\r
385                 _favorites_finalize_bookmark_db(stmt);\r
386                 return NULL;\r
387         }\r
388 \r
389         /*  allocation .... Array for Items */\r
390         m_list = (bookmark_list_h) calloc(1, sizeof(bookmark_list_s));\r
391         m_list->item =\r
392             (bookmark_entry_internal_h) calloc(item_count, sizeof(bookmark_entry_internal_s));\r
393         m_list->count = item_count;\r
394         int i = 0;\r
395         while ((nError = sqlite3_step(stmt)) == SQLITE_ROW \r
396                 && (i < item_count)) {\r
397                 m_list->item[i].id = sqlite3_column_int(stmt, 0);\r
398                 m_list->item[i].is_folder = sqlite3_column_int(stmt, 1);\r
399                 m_list->item[i].folder_id = sqlite3_column_int(stmt, 2);\r
400 \r
401                 if (!m_list->item[i].is_folder) {\r
402                         const char *url =\r
403                             (const char *)(sqlite3_column_text(stmt, 3));\r
404                         m_list->item[i].address = NULL;\r
405                         if (url) {\r
406                                 int length = strlen(url);\r
407                                 if (length > 0) {\r
408                                         m_list->item[i].address =\r
409                                             (char *)calloc(length + 1,\r
410                                                            sizeof(char));\r
411                                         memcpy(m_list->item[i].address, url,\r
412                                                length);\r
413                                 }\r
414                         }\r
415                 }\r
416 \r
417                 const char *title =\r
418                     (const char *)(sqlite3_column_text(stmt, 4));\r
419                 m_list->item[i].title = NULL;\r
420                 if (title) {\r
421                         int length = strlen(title);\r
422                         if (length > 0) {\r
423                                 m_list->item[i].title =\r
424                                     (char *)calloc(length + 1, sizeof(char));\r
425                                 memcpy(m_list->item[i].title, title, length);\r
426                         }\r
427                         FAVORITES_LOGE("Bookmark Title:%s\n", m_list->item[i].title);\r
428                 }\r
429                 m_list->item[i].editable = sqlite3_column_int(stmt, 5);\r
430 \r
431                 const char *creationdate =\r
432                     (const char *)(sqlite3_column_text(stmt, 6));\r
433                 m_list->item[i].creationdate = NULL;\r
434                 if (creationdate) {\r
435                         int length = strlen(creationdate);\r
436                         if (length > 0) {\r
437                                 m_list->item[i].creationdate =\r
438                                     (char *)calloc(length + 1, sizeof(char));\r
439                                 memcpy(m_list->item[i].creationdate,\r
440                                        creationdate, length);\r
441                         }\r
442                 }\r
443                 const char *updatedate =\r
444                     (const char *)(sqlite3_column_text(stmt, 7));\r
445                 m_list->item[i].updatedate = NULL;\r
446                 if (updatedate) {\r
447                         int length = strlen(updatedate);\r
448                         if (length > 0) {\r
449                                 m_list->item[i].updatedate =\r
450                                     (char *)calloc(length + 1, sizeof(char));\r
451                                 memcpy(m_list->item[i].updatedate, updatedate,\r
452                                        length);\r
453                         }\r
454                 }\r
455 \r
456                 m_list->item[i].orderIndex = sqlite3_column_int(stmt, 8);\r
457                 i++;\r
458         }\r
459         m_list->count = i;\r
460 \r
461         if (nError == SQLITE_DONE) {\r
462                 if (m_list->count > 0) {\r
463                         _favorites_finalize_bookmark_db(stmt);\r
464                         return m_list;\r
465                 } else if (m_list->count == 0) {\r
466                         FAVORITES_LOGE("There is no folders");\r
467                         _favorites_finalize_bookmark_db(stmt);\r
468                         _favorites_free_bookmark_list(m_list);\r
469                         return NULL;\r
470                 }\r
471         }\r
472         FAVORITES_LOGE("sqlite3_step is failed");\r
473         _favorites_finalize_bookmark_db(stmt);\r
474         _favorites_free_bookmark_list(m_list);\r
475         return NULL;\r
476 }\r
477 \r
478 int _favorites_get_unixtime_from_datetime(char *datetime)\r
479 {\r
480         int nError;\r
481         sqlite3_stmt *stmt;\r
482 \r
483         if(datetime == NULL ) {\r
484                 FAVORITES_LOGE("datetime is NULL\n");\r
485                 return -1;\r
486         }\r
487 \r
488         FAVORITES_LOGE("datetime: %s\n", datetime);\r
489 \r
490         if (_favorites_open_bookmark_db() < 0) {\r
491                 FAVORITES_LOGE("db_util_open is failed\n");\r
492                 return -1;\r
493         }\r
494 \r
495         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
496                                "SELECT strftime('%s', ?)",\r
497                                -1, &stmt, NULL);\r
498         if (nError != SQLITE_OK) {\r
499                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
500                         sqlite3_errmsg(gl_internet_bookmark_db));\r
501                 _favorites_finalize_bookmark_db(stmt);\r
502                 return -1;\r
503         }\r
504 \r
505         if (sqlite3_bind_text(stmt, 1, datetime, -1, NULL) != SQLITE_OK) {\r
506                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
507                 _favorites_finalize_bookmark_db(stmt);\r
508                 return -1;\r
509         }\r
510 \r
511         nError = sqlite3_step(stmt);\r
512         if (nError == SQLITE_ROW) {\r
513                 int unixtime = sqlite3_column_int(stmt, 0);\r
514                 _favorites_finalize_bookmark_db(stmt);\r
515                 return unixtime;\r
516         } else if (nError == SQLITE_DONE) {\r
517                 _favorites_finalize_bookmark_db(stmt);\r
518                 return 0;\r
519         }\r
520         _favorites_close_bookmark_db();\r
521         return -1;\r
522 }\r
523 \r
524 int _add_bookmark(const char *title, const char *address, int parent_id, int *saved_bookmark_id)\r
525 {\r
526         FAVORITES_LOGD("");\r
527         if (title == NULL || strlen(title) <= 0) {\r
528                 FAVORITES_LOGD("Invalid param: Title is empty");\r
529                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
530         }\r
531         if (address == NULL || strlen(address) <= 0) {\r
532                 FAVORITES_LOGD("Invalid param: Address is empty");\r
533                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
534         }\r
535         if (memcmp(address, "file://", 7) == 0) {\r
536                 FAVORITES_LOGD("Not Allow file://");\r
537                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
538         }\r
539 #if defined(BROWSER_BOOKMARK_SYNC)\r
540         if (parent_id != _get_root_folder_id()) {\r
541                 /* check whether folder is exist or not. */\r
542                 if (_get_exists_id(parent_id) <= 0) {\r
543                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
544                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
545                 }\r
546         }\r
547         FAVORITES_LOGD("[%s][%s][%d]", title, address, parent_id);\r
548         int id = -1;\r
549         int *ids = NULL;\r
550         int ids_count = -1;\r
551         bp_bookmark_adaptor_get_duplicated_url_ids_p(&ids, &ids_count, 1,\r
552                 -1, -1, 0, 0, address, 0);\r
553         if (ids_count > 0)\r
554                 id = ids[0];\r
555         free(ids);\r
556 \r
557         if (ids_count > 0) {\r
558                 FAVORITES_LOGE("same URI is already exist");\r
559                 return FAVORITES_ERROR_ITEM_ALREADY_EXIST;\r
560         }\r
561 \r
562         bp_bookmark_info_fmt info;\r
563         memset(&info, 0x00, sizeof(bp_bookmark_info_fmt));\r
564         info.type = 0;\r
565         info.parent = parent_id;\r
566         info.sequence = -1;\r
567         info.access_count = -1;\r
568         info.editable = 1;\r
569         time_t seconds = 0;\r
570         info.date_created = time(&seconds);\r
571         info.date_modified = time(&seconds);\r
572         if (address != NULL && strlen(address) > 0)\r
573                 info.url = strdup(address);\r
574         if (title != NULL && strlen(title) > 0)\r
575                 info.title = strdup(title);\r
576 \r
577         int ret = bp_bookmark_adaptor_easy_create(&id, &info);\r
578         if (ret == 0) {\r
579                 ret = bp_bookmark_adaptor_set_sequence(id, -1); // max sequence\r
580                 bp_bookmark_adaptor_easy_free(&info);\r
581                 if (ret == 0) {\r
582                         *saved_bookmark_id = id;\r
583                         FAVORITES_LOGD("bp_bookmark_adaptor_easy_create is success(id:%d)", *saved_bookmark_id);\r
584                         bp_bookmark_adaptor_publish_notification();\r
585                         return FAVORITES_ERROR_NONE;\r
586                 }\r
587         }\r
588         int errcode = bp_bookmark_adaptor_get_errorcode();\r
589         bp_bookmark_adaptor_easy_free(&info);\r
590         FAVORITES_LOGE("bp_bookmark_adaptor_easy_create is failed[%d]", errcode);\r
591         return FAVORITES_ERROR_DB_FAILED;\r
592 #else\r
593         int nError;\r
594         sqlite3_stmt *stmt;\r
595 #if defined(ROOT_IS_ZERO)\r
596         if (parent_id != _get_root_folder_id()) {\r
597                 /* check whether folder is exist or not. */\r
598                 if (_get_exists_id(parent_id) <= 0) {\r
599                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
600                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
601                 }\r
602         }\r
603 #else\r
604         if (parent_id > 0) {\r
605                 /* check whether folder is exist or not. */\r
606                 if (_get_exists_id(parent_id) <= 0) {\r
607                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
608                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
609                 }\r
610         } else {\r
611                 parent_id = _get_root_folder_id();\r
612         }\r
613 #endif\r
614         FAVORITES_LOGD("[%s][%s][%d]", title, address, parent_id);\r
615 \r
616         int uid = _get_exists_bookmark(address);\r
617         /* ignore this call.. already exist. */\r
618         if (uid > 0) {\r
619                 FAVORITES_LOGE("Bookmark is already exist. cancel the add operation.");\r
620                 return FAVORITES_ERROR_ITEM_ALREADY_EXIST;\r
621         }\r
622 \r
623         int lastIndex = _favorites_get_bookmark_lastindex(parent_id);\r
624         if (lastIndex < 0) {\r
625                 FAVORITES_LOGE("Database::getLastIndex() is failed.\n");\r
626                 return FAVORITES_ERROR_DB_FAILED;\r
627         }\r
628 \r
629         if (_favorites_open_bookmark_db() < 0) {\r
630                 FAVORITES_LOGE("db_util_open is failed\n");\r
631                 return FAVORITES_ERROR_DB_FAILED;\r
632         }\r
633 \r
634         nError =\r
635             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
636                                         "INSERT INTO bookmarks (type, parent, address, title, creationdate,\\r
637                                         updatedate, editable, sequence)\\r
638                                         VALUES (0, ?, ?, ?, DATETIME('now'), DATETIME('now'), 1, ?)",\r
639                                -1, &stmt, NULL);\r
640         if (nError != SQLITE_OK) {\r
641                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
642                         sqlite3_errmsg(gl_internet_bookmark_db));\r
643                 _favorites_finalize_bookmark_db(stmt);\r
644                 return FAVORITES_ERROR_DB_FAILED;\r
645         }\r
646 \r
647         /*parent */\r
648         if (sqlite3_bind_int(stmt, 1, parent_id) != SQLITE_OK) {\r
649                 FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
650                 _favorites_finalize_bookmark_db(stmt);\r
651                 return FAVORITES_ERROR_DB_FAILED;\r
652         }\r
653         /*address */\r
654         if (sqlite3_bind_text(stmt, 2, address, -1, NULL) != SQLITE_OK) {\r
655                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
656                 _favorites_finalize_bookmark_db(stmt);\r
657                 return FAVORITES_ERROR_DB_FAILED;\r
658         }\r
659         /*title */\r
660         if (sqlite3_bind_text(stmt, 3, title, -1, NULL) != SQLITE_OK) {\r
661                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
662                 _favorites_finalize_bookmark_db(stmt);\r
663                 return FAVORITES_ERROR_DB_FAILED;\r
664         }\r
665         /* order */\r
666         if (lastIndex >= 0) {\r
667                 if (sqlite3_bind_int(stmt, 4, (lastIndex + 1)) != SQLITE_OK) {\r
668                         FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
669                         _favorites_finalize_bookmark_db(stmt);\r
670                         return FAVORITES_ERROR_DB_FAILED;\r
671                 }\r
672         }\r
673         nError = sqlite3_step(stmt);\r
674         if (nError == SQLITE_OK || nError == SQLITE_DONE) {\r
675                 _favorites_finalize_bookmark_db(stmt);\r
676                 int id = _get_bookmark_id(address);\r
677                 if (id > 0)\r
678                         *saved_bookmark_id = id;\r
679                 return FAVORITES_ERROR_NONE;\r
680         }\r
681         FAVORITES_LOGE("sqlite3_step is failed");\r
682         _favorites_close_bookmark_db();\r
683         return FAVORITES_ERROR_DB_FAILED;\r
684 #endif\r
685 }\r
686 \r
687 int _add_folder(const char *title, int parent_id, int *saved_folder_id)\r
688 {\r
689         FAVORITES_LOGD("");\r
690         if (title == NULL || strlen(title) <= 0) {\r
691                 FAVORITES_LOGD("Invalid param: Title is empty");\r
692                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
693         }\r
694 #if defined(BROWSER_BOOKMARK_SYNC)\r
695         if (parent_id != _get_root_folder_id()) {\r
696                 /* check whether folder is exist or not. */\r
697                 if (_get_exists_id(parent_id) <= 0) {\r
698                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
699                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
700                 }\r
701         }\r
702         int id = -1;\r
703         int *ids = NULL;\r
704         int ids_count = -1;\r
705         bp_bookmark_adaptor_get_duplicated_title_ids_p(&ids, &ids_count, 1,\r
706                 -1, parent_id, 1, 0, title, 0);\r
707         if (ids_count > 0)\r
708                 id = ids[0];\r
709         free(ids);\r
710 \r
711         if (ids_count > 0) {\r
712                 FAVORITES_LOGE("same title with same parent is already exist");\r
713                 return FAVORITES_ERROR_ITEM_ALREADY_EXIST;\r
714         }\r
715 \r
716         bp_bookmark_info_fmt info;\r
717         memset(&info, 0x00, sizeof(bp_bookmark_info_fmt));\r
718         info.type = 1;\r
719         info.parent = parent_id;\r
720         info.sequence = -1;\r
721         info.access_count = -1;\r
722         info.editable = 1;\r
723         if (title != NULL && strlen(title) > 0)\r
724                 info.title = strdup(title);\r
725         int ret = bp_bookmark_adaptor_easy_create(&id, &info);\r
726         if (ret == 0) {\r
727                 ret = bp_bookmark_adaptor_set_sequence(id, -1); // max sequence\r
728                 bp_bookmark_adaptor_easy_free(&info);\r
729                 if (ret == 0) {\r
730                         *saved_folder_id = id;\r
731                         FAVORITES_LOGD("bmsvc_add_bookmark is success(id:%d)", *saved_folder_id);\r
732                         bp_bookmark_adaptor_publish_notification();\r
733                         return FAVORITES_ERROR_NONE;\r
734                 }\r
735         }\r
736         int errcode = bp_bookmark_adaptor_get_errorcode();\r
737         bp_bookmark_adaptor_easy_free(&info);\r
738         FAVORITES_LOGE("bp_bookmark_adaptor_easy_create is failed[%d]", errcode);\r
739         return FAVORITES_ERROR_DB_FAILED;\r
740 #else\r
741 #if defined(ROOT_IS_ZERO)\r
742         if (parent_id != _get_root_folder_id()) {\r
743                 /* check whether folder is exist or not. */\r
744                 if (_get_exists_id(parent_id) <= 0) {\r
745                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
746                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
747                 }\r
748         }\r
749 #else\r
750         if (parent_id > 0) {\r
751                 /* check whether folder is exist or not. */\r
752                 if (_get_exists_id(parent_id) <= 0) {\r
753                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
754                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
755                 }\r
756         } else {\r
757                 parent_id = _get_root_folder_id();\r
758         }\r
759 #endif\r
760         FAVORITES_LOGD("[%s][%d]", title, parent_id);\r
761 \r
762         int uid = _get_exists_folder(title, parent_id);\r
763         /* ignore this call.. already exist. */\r
764         if (uid > 0) {\r
765                 FAVORITES_LOGE("Folder is already exist. cancel the add operation.");\r
766                 return FAVORITES_ERROR_ITEM_ALREADY_EXIST;\r
767         }\r
768 \r
769         int lastIndex = _favorites_get_bookmark_lastindex(parent_id);\r
770         if (lastIndex < 0) {\r
771                 FAVORITES_LOGE("Database::getLastIndex() is failed.\n");\r
772                 return FAVORITES_ERROR_DB_FAILED;\r
773         }\r
774         if (_favorites_open_bookmark_db() < 0) {\r
775                 FAVORITES_LOGE("db_util_open is failed\n");\r
776                 return FAVORITES_ERROR_DB_FAILED;\r
777         }\r
778 \r
779         int nError;\r
780         sqlite3_stmt *stmt;\r
781 \r
782         nError =\r
783             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
784                                         "INSERT INTO bookmarks (type, parent, title, creationdate,\\r
785                                         updatedate, sequence, editable)\\r
786                                         VALUES (1, ?, ?, DATETIME('now'), DATETIME('now'), ?, 1)",\r
787                                -1, &stmt, NULL);\r
788         if (nError != SQLITE_OK) {\r
789                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
790                         sqlite3_errmsg(gl_internet_bookmark_db));\r
791                 _favorites_finalize_bookmark_db(stmt);\r
792                 return FAVORITES_ERROR_DB_FAILED;\r
793         }\r
794 \r
795         /*parent */\r
796         if (sqlite3_bind_int(stmt, 1, parent_id) != SQLITE_OK) {\r
797                 FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
798                 _favorites_finalize_bookmark_db(stmt);\r
799                 return FAVORITES_ERROR_DB_FAILED;\r
800         }\r
801         /*title */\r
802         if (sqlite3_bind_text(stmt, 2, title, -1, NULL) != SQLITE_OK) {\r
803                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
804                 _favorites_finalize_bookmark_db(stmt);\r
805                 return FAVORITES_ERROR_DB_FAILED;\r
806         }\r
807         /* order */\r
808         if (lastIndex >= 0) {\r
809                 if (sqlite3_bind_int(stmt, 3, (lastIndex + 1)) != SQLITE_OK) {\r
810                         FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
811                         _favorites_finalize_bookmark_db(stmt);\r
812                         return FAVORITES_ERROR_DB_FAILED;\r
813                 }\r
814         }\r
815 \r
816         nError = sqlite3_step(stmt);\r
817         if (nError == SQLITE_OK || nError == SQLITE_DONE) {\r
818                 _favorites_finalize_bookmark_db(stmt);\r
819                 int id = _get_folder_id(title, parent_id);\r
820                 if (id > 0)\r
821                         *saved_folder_id = id;\r
822                 return FAVORITES_ERROR_NONE;\r
823         }\r
824         FAVORITES_LOGE("sqlite3_step is failed");\r
825         _favorites_close_bookmark_db();\r
826         return FAVORITES_ERROR_DB_FAILED;\r
827 #endif\r
828 }\r
829 \r
830 \r
831 int _get_root_folder_id(void)\r
832 {\r
833 #if defined(BROWSER_BOOKMARK_SYNC)\r
834         FAVORITES_LOGE("SYNC");\r
835         int root_id = -1;\r
836         bp_bookmark_adaptor_get_root(&root_id);\r
837         return root_id;\r
838 #else\r
839 #if defined(ROOT_IS_ZERO)\r
840         return 0;\r
841 #else\r
842         FAVORITES_LOGE("NOT SYNC");\r
843         return 1;\r
844 #endif\r
845 #endif\r
846 }\r
847 \r
848 int _get_exists_id(int id)\r
849 {\r
850         FAVORITES_LOGE("");\r
851 #if defined(BROWSER_BOOKMARK_SYNC)\r
852         int value = -1;\r
853         int ret = -1;\r
854         int errorcode = -1;\r
855         ret = bp_bookmark_adaptor_get_type(id, &value);\r
856         if (ret < 0) {\r
857                 errorcode = bp_bookmark_adaptor_get_errorcode();\r
858                 if (errorcode == BP_BOOKMARK_ERROR_NO_DATA)\r
859                         return 0;\r
860                 else\r
861                         return -1;\r
862         }\r
863         return 1;\r
864 #else\r
865         int nError;\r
866         sqlite3_stmt *stmt;\r
867 \r
868         if (id < 0)\r
869                 return -1;\r
870 \r
871         if (_favorites_open_bookmark_db() < 0) {\r
872                 FAVORITES_LOGE("db_util_open is failed\n");\r
873                 return -1;\r
874         }\r
875 \r
876         /* same title  */\r
877         nError =\r
878             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
879                                "select * from bookmarks where id=?",\r
880                                -1, &stmt, NULL);\r
881 \r
882         if (nError != SQLITE_OK) {\r
883                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
884                         sqlite3_errmsg(gl_internet_bookmark_db));\r
885                 _favorites_finalize_bookmark_db(stmt);\r
886                 return -1;\r
887         }\r
888         if (sqlite3_bind_int(stmt, 1, id) != SQLITE_OK) {\r
889                 FAVORITES_LOGE("sqlite3_bind_int is failed.");\r
890                 _favorites_finalize_bookmark_db(stmt);\r
891                 return -1;\r
892         }\r
893         nError = sqlite3_step(stmt);\r
894         if (nError == SQLITE_ROW) {\r
895                 /* The given foldername is exist on the bookmark table */\r
896                 _favorites_finalize_bookmark_db(stmt);\r
897                 return 1;\r
898         } else if (nError == SQLITE_DONE) {\r
899                 _favorites_finalize_bookmark_db(stmt);\r
900                 return 0;\r
901         }\r
902 \r
903         _favorites_close_bookmark_db();\r
904         return -1;\r
905 #endif\r
906 }\r
907 \r
908 int _get_exists_bookmark(const char *address)\r
909 {\r
910         FAVORITES_LOGD("");\r
911         int nError;\r
912         sqlite3_stmt *stmt;\r
913 \r
914         if (_favorites_open_bookmark_db() < 0) {\r
915                 FAVORITES_LOGE("db_util_open is failed\n");\r
916                 return -1;\r
917         }\r
918         /* same title  */\r
919         nError =\r
920             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
921                                "select * from bookmarks where address=? and type=0",\r
922                                -1, &stmt, NULL);\r
923 \r
924         if (nError != SQLITE_OK) {\r
925                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
926                         sqlite3_errmsg(gl_internet_bookmark_db));\r
927                 _favorites_finalize_bookmark_db(stmt);\r
928                 return -1;\r
929         }\r
930 \r
931         if (sqlite3_bind_text(stmt, 1, address, -1, NULL) != SQLITE_OK) {\r
932                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
933                 _favorites_finalize_bookmark_db(stmt);\r
934                 return -1;\r
935         }\r
936 \r
937         nError = sqlite3_step(stmt);\r
938         if (nError == SQLITE_ROW) {\r
939                 _favorites_finalize_bookmark_db(stmt);\r
940                 return 1;\r
941         } else if (nError == SQLITE_DONE) {\r
942                 //FAVORITES_LOGE("bookmark doesn't exists");\r
943                 _favorites_finalize_bookmark_db(stmt);\r
944                 return 0;\r
945         }\r
946         _favorites_close_bookmark_db();\r
947         return -1;\r
948 }\r
949 \r
950 int _get_exists_folder(const char *title, int parent_id)\r
951 {\r
952         FAVORITES_LOGD("");\r
953         int nError;\r
954         sqlite3_stmt *stmt;\r
955 \r
956         if (_favorites_open_bookmark_db() < 0) {\r
957                 FAVORITES_LOGE("db_util_open is failed\n");\r
958                 return -1;\r
959         }\r
960         /* same title  */\r
961         nError =\r
962             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
963                                "select * from bookmarks where title=? and parent=? and type=1",\r
964                                -1, &stmt, NULL);\r
965 \r
966         if (nError != SQLITE_OK) {\r
967                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
968                         sqlite3_errmsg(gl_internet_bookmark_db));\r
969                 _favorites_finalize_bookmark_db(stmt);\r
970                 return -1;\r
971         }\r
972 \r
973         if (sqlite3_bind_text(stmt, 1, title, -1, NULL) != SQLITE_OK) {\r
974                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
975                 _favorites_finalize_bookmark_db(stmt);\r
976                 return -1;\r
977         }\r
978 \r
979         if (sqlite3_bind_int(stmt, 2, parent_id) != SQLITE_OK) {\r
980                 FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
981                 _favorites_finalize_bookmark_db(stmt);\r
982                 return -1;\r
983         }\r
984 \r
985         nError = sqlite3_step(stmt);\r
986         if (nError == SQLITE_ROW) {\r
987                 _favorites_finalize_bookmark_db(stmt);\r
988                 return 1;\r
989         } else if (nError == SQLITE_DONE) {\r
990                 //FAVORITES_LOGE("folder doesn't exists");\r
991                 _favorites_finalize_bookmark_db(stmt);\r
992                 return 0;\r
993         }\r
994         _favorites_close_bookmark_db();\r
995         return -1;\r
996 }\r
997 \r
998 int _get_bookmark_id(const char *address)\r
999 {\r
1000         FAVORITES_LOGE("");\r
1001 #if defined(BROWSER_BOOKMARK_SYNC)\r
1002         int id = -1;\r
1003         int *ids = NULL;\r
1004         int ids_count = -1;\r
1005         bp_bookmark_adaptor_get_duplicated_url_ids_p(&ids, &ids_count, 1,\r
1006                 -1, -1, 0, 0, address, 0);\r
1007         if (ids_count > 0)\r
1008                 id = ids[0];\r
1009         free(ids);\r
1010 \r
1011         if (ids_count > 0) {\r
1012                 return id;\r
1013         }\r
1014         FAVORITES_LOGE("There is no such bookmark url exists.");\r
1015         return -1;\r
1016 #else\r
1017         int nError;\r
1018         sqlite3_stmt *stmt;\r
1019 \r
1020         if (_favorites_open_bookmark_db() < 0) {\r
1021                 FAVORITES_LOGE("db_util_open is failed\n");\r
1022                 return -1;\r
1023         }\r
1024         /* same title  */\r
1025         nError =\r
1026             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1027                                "select id from bookmarks where address=? and type=0",\r
1028                                -1, &stmt, NULL);\r
1029 \r
1030         if (nError != SQLITE_OK) {\r
1031                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1032                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1033                 _favorites_finalize_bookmark_db(stmt);\r
1034                 return -1;\r
1035         }\r
1036 \r
1037         if (sqlite3_bind_text(stmt, 1, address, -1, NULL) != SQLITE_OK) {\r
1038                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
1039                 _favorites_finalize_bookmark_db(stmt);\r
1040                 return -1;\r
1041         }\r
1042 \r
1043         nError = sqlite3_step(stmt);\r
1044         if (nError == SQLITE_ROW) {\r
1045                 int id = sqlite3_column_int(stmt, 0);\r
1046                 _favorites_finalize_bookmark_db(stmt);\r
1047                 return id;\r
1048         } else if (nError == SQLITE_DONE) {\r
1049                 //FAVORITES_LOGE("bookmark id doesn't exists");\r
1050                 _favorites_finalize_bookmark_db(stmt);\r
1051                 return 0;\r
1052         }\r
1053         FAVORITES_LOGE("sqlite3_step is failed(%s).\n",\r
1054                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1055         _favorites_close_bookmark_db();\r
1056         return -1;\r
1057 #endif\r
1058 }\r
1059 \r
1060 int _get_folder_id(const char *title, const int parent)\r
1061 {\r
1062         FAVORITES_LOGE("");\r
1063 #if defined(BROWSER_BOOKMARK_SYNC)\r
1064         int id = -1;\r
1065         int *ids = NULL;\r
1066         int ids_count = -1;\r
1067         bp_bookmark_adaptor_get_duplicated_title_ids_p(&ids, &ids_count, 1,\r
1068                 -1, parent, 1, 0, title, 0);\r
1069         if (ids_count > 0)\r
1070                 id = ids[0];\r
1071         free(ids);\r
1072 \r
1073         if (ids_count > 0) {\r
1074                 return id;\r
1075         }\r
1076         FAVORITES_LOGE("There is no such folder exists.");\r
1077         return -1;\r
1078 #else\r
1079         int nError;\r
1080         sqlite3_stmt *stmt;\r
1081 \r
1082         if (_favorites_open_bookmark_db() < 0) {\r
1083                 FAVORITES_LOGE("db_util_open is failed\n");\r
1084                 return -1;\r
1085         }\r
1086         /* same title  */\r
1087         nError =\r
1088             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1089                                "SELECT id FROM bookmarks WHERE title=? AND parent=? AND type=1",\r
1090                                -1, &stmt, NULL);\r
1091 \r
1092         if (nError != SQLITE_OK) {\r
1093                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1094                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1095                 _favorites_finalize_bookmark_db(stmt);\r
1096                 return -1;\r
1097         }\r
1098 \r
1099         if (sqlite3_bind_text(stmt, 1, title, -1, NULL) != SQLITE_OK) {\r
1100                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
1101                 _favorites_finalize_bookmark_db(stmt);\r
1102                 return -1;\r
1103         }\r
1104         if (sqlite3_bind_int(stmt, 2, parent) != SQLITE_OK) {\r
1105                 FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
1106                 _favorites_finalize_bookmark_db(stmt);\r
1107                 return -1;\r
1108         }\r
1109 \r
1110         nError = sqlite3_step(stmt);\r
1111         if (nError == SQLITE_ROW) {\r
1112                 int id = sqlite3_column_int(stmt, 0);\r
1113                 _favorites_finalize_bookmark_db(stmt);\r
1114                 return id;\r
1115         } else if (nError == SQLITE_DONE) {\r
1116                 //FAVORITES_LOGE("folder id doesn't exists");\r
1117                 _favorites_finalize_bookmark_db(stmt);\r
1118                 return 0;\r
1119         }\r
1120         FAVORITES_LOGE("sqlite3_step is failed(%s).\n",\r
1121                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1122         _favorites_close_bookmark_db();\r
1123         return -1;\r
1124 #endif\r
1125 }\r
1126 \r
1127 int _get_count_by_folder(int parent_id)\r
1128 {\r
1129         int nError;\r
1130         sqlite3_stmt *stmt;\r
1131         FAVORITES_LOGE("");\r
1132         \r
1133         if (_favorites_open_bookmark_db() < 0) {\r
1134                 FAVORITES_LOGE("db_util_open is failed\n");\r
1135                 return -1;\r
1136         }\r
1137 \r
1138         /*bookmark */\r
1139         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1140                                "select count(*) from bookmarks where parent=?",\r
1141                                -1, &stmt, NULL);\r
1142         if (nError != SQLITE_OK) {\r
1143                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1144                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1145                 _favorites_finalize_bookmark_db(stmt);\r
1146                 return -1;\r
1147         }\r
1148 \r
1149         if (sqlite3_bind_int(stmt, 1, parent_id) != SQLITE_OK) {\r
1150                 FAVORITES_LOGE("sqlite3_bind_int is failed");\r
1151                 _favorites_finalize_bookmark_db(stmt);\r
1152                 return -1;\r
1153         }\r
1154 \r
1155         nError = sqlite3_step(stmt);\r
1156         if (nError == SQLITE_ROW) {\r
1157                 int count = sqlite3_column_int(stmt, 0);\r
1158                 _favorites_finalize_bookmark_db(stmt);\r
1159                 FAVORITES_LOGE("count: %d", count);\r
1160                 return count;\r
1161         } else if (nError == SQLITE_DONE) {\r
1162                 FAVORITES_LOGE("No rows");\r
1163                 _favorites_finalize_bookmark_db(stmt);\r
1164                 return -1;\r
1165         }\r
1166         FAVORITES_LOGE("sqlite3_step is failed");\r
1167         _favorites_close_bookmark_db();\r
1168         return -1;\r
1169 }\r
1170 \r
1171 \r
1172 int _get_list_by_folder(int parent_id, bookmark_list_h *list)\r
1173 {\r
1174         FAVORITES_LOGD("");\r
1175 #if defined(BROWSER_BOOKMARK_SYNC)\r
1176         int *ids = NULL;\r
1177         int ids_count = -1;\r
1178         bp_bookmark_info_fmt info;\r
1179         if (bp_bookmark_adaptor_get_sequence_child_ids_p(&ids, &ids_count, -1, 0, parent_id, -1, 0) < 0) {\r
1180                 FAVORITES_LOGE("bp_bookmark_adaptor_get_sequence_child_ids_p is failed");\r
1181                 return FAVORITES_ERROR_DB_FAILED;\r
1182         }\r
1183 \r
1184         FAVORITES_LOGD("ids_count: %d", ids_count);\r
1185         if (ids_count < 0) {\r
1186                 FAVORITES_LOGE("bookmark list is empty");\r
1187                 return FAVORITES_ERROR_DB_FAILED;\r
1188         }\r
1189 \r
1190         *list = (bookmark_list_h) calloc(1, sizeof(bookmark_list_s));\r
1191         (*list)->count = 0;\r
1192         (*list)->item =\r
1193             (bookmark_entry_internal_h) calloc(ids_count, sizeof(bookmark_entry_internal_s));\r
1194         int i = 0;\r
1195         for(i = 0; i < ids_count; i++) {\r
1196                 (*list)->item[i].id = ids[i];\r
1197                 if (bp_bookmark_adaptor_get_info(ids[i], (BP_BOOKMARK_O_TYPE |\r
1198                                 BP_BOOKMARK_O_PARENT | BP_BOOKMARK_O_SEQUENCE |\r
1199                                 BP_BOOKMARK_O_IS_EDITABLE | BP_BOOKMARK_O_URL |\r
1200                                 BP_BOOKMARK_O_TITLE | BP_BOOKMARK_O_DATE_CREATED |\r
1201                                 BP_BOOKMARK_O_DATE_MODIFIED), &info) == 0) {\r
1202                         (*list)->item[i].is_folder = info.type;\r
1203                         (*list)->item[i].folder_id = info.parent;\r
1204                         (*list)->item[i].orderIndex = info.sequence;\r
1205                         (*list)->item[i].editable = info.editable;\r
1206 \r
1207                         if (info.url != NULL && strlen(info.url) > 0)\r
1208                                 (*list)->item[i].address = strdup(info.url);\r
1209                         if (info.title != NULL && strlen(info.title) > 0)\r
1210                                 (*list)->item[i].title = strdup(info.title);\r
1211                         FAVORITES_LOGD("Title: %s", (*list)->item[i].title);\r
1212 \r
1213                         (*list)->item[i].creationdate = NULL;\r
1214                         (*list)->item[i].creationdate = (char *)calloc(128, sizeof(char));\r
1215                         strftime((*list)->item[i].creationdate,128,"%Y-%m-%d %H:%M:%S",localtime((time_t *)&(info.date_created)));\r
1216 \r
1217                         (*list)->item[i].updatedate = NULL;\r
1218                         (*list)->item[i].updatedate = (char *)calloc(128, sizeof(char));\r
1219                         strftime((*list)->item[i].updatedate,128,"%Y-%m-%d %H:%M:%S",localtime((time_t *)&(info.date_modified)));\r
1220                 }\r
1221                 bp_bookmark_adaptor_easy_free(&info);\r
1222         }\r
1223         (*list)->count = i;\r
1224         FAVORITES_LOGE("bookmark list count : %d", (*list)->count);\r
1225         free(ids);\r
1226         return FAVORITES_ERROR_NONE;\r
1227 #else\r
1228         int nError;\r
1229         sqlite3_stmt *stmt;\r
1230 \r
1231         int item_count = _get_count_by_folder(parent_id);\r
1232         if (item_count < 0) {\r
1233                 FAVORITES_LOGD("_get_count_by_folder is failed");\r
1234                 return FAVORITES_ERROR_DB_FAILED;\r
1235         }\r
1236 \r
1237         if (_favorites_open_bookmark_db() < 0) {\r
1238                 FAVORITES_LOGE("db_util_open is failed\n");\r
1239                 return FAVORITES_ERROR_DB_FAILED;\r
1240         }\r
1241         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1242                                "select id, type, parent, address, title, editable,\\r
1243                                creationdate, updatedate, sequence \\r
1244                                from bookmarks where parent=? order by sequence",\r
1245                                -1, &stmt, NULL);\r
1246         if (nError != SQLITE_OK) {\r
1247                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1248                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1249                 _favorites_finalize_bookmark_db(stmt);\r
1250                 return FAVORITES_ERROR_DB_FAILED;\r
1251         }\r
1252 \r
1253         if (sqlite3_bind_int(stmt, 1, parent_id) != SQLITE_OK) {\r
1254                 FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
1255                 _favorites_finalize_bookmark_db(stmt);\r
1256                 return -1;\r
1257         }\r
1258 \r
1259         *list = (bookmark_list_h) calloc(1, sizeof(bookmark_list_s));\r
1260         (*list)->count = 0;\r
1261         (*list)->item =\r
1262             (bookmark_entry_internal_h) calloc(item_count, sizeof(bookmark_entry_internal_s));\r
1263         int i = 0;\r
1264         while ((nError = sqlite3_step(stmt)) == SQLITE_ROW && (i < item_count)) {\r
1265                 (*list)->item[i].id = sqlite3_column_int(stmt, 0);\r
1266                 (*list)->item[i].is_folder = sqlite3_column_int(stmt, 1);\r
1267                 (*list)->item[i].folder_id = sqlite3_column_int(stmt, 2);\r
1268 \r
1269                 (*list)->item[i].address = NULL;\r
1270                 if (!(*list)->item[i].is_folder) {\r
1271                         const char *url = (const char *)(sqlite3_column_text(stmt, 3));\r
1272                         if (url) {\r
1273                                 int length = strlen(url);\r
1274                                 if (length > 0) {\r
1275                                         (*list)->item[i].address = (char *)calloc(length + 1, sizeof(char));\r
1276                                         memcpy((*list)->item[i].address, url, length);\r
1277                                         FAVORITES_LOGE ("url:%s\n", url);\r
1278                                 }\r
1279                         }\r
1280                 }\r
1281 \r
1282                 const char *title = (const char *)(sqlite3_column_text(stmt, 4));\r
1283                 (*list)->item[i].title = NULL;\r
1284                 if (title) {\r
1285                         int length = strlen(title);\r
1286                         if (length > 0) {\r
1287                                 (*list)->item[i].title = (char *)calloc(length + 1, sizeof(char));\r
1288                                 memcpy((*list)->item[i].title, title, length);\r
1289                         }\r
1290                 }\r
1291                 (*list)->item[i].editable = sqlite3_column_int(stmt, 5);\r
1292 \r
1293                 const char *creationdate = (const char *)(sqlite3_column_text(stmt, 6));\r
1294                 (*list)->item[i].creationdate = NULL;\r
1295                 if (creationdate) {\r
1296                         int length = strlen(creationdate);\r
1297                         if (length > 0) {\r
1298                                 (*list)->item[i].creationdate = (char *)calloc(length + 1, sizeof(char));\r
1299                                 memcpy((*list)->item[i].creationdate, creationdate, length);\r
1300                         }\r
1301                 }\r
1302                 const char *updatedate = (const char *)(sqlite3_column_text(stmt, 7));\r
1303                 (*list)->item[i].updatedate = NULL;\r
1304                 if (updatedate) {\r
1305                         int length = strlen(updatedate);\r
1306                         if (length > 0) {\r
1307                                 (*list)->item[i].updatedate = (char *)calloc(length + 1, sizeof(char));\r
1308                                 memcpy((*list)->item[i].updatedate, updatedate, length);\r
1309                         }\r
1310                 }\r
1311 \r
1312                 (*list)->item[i].orderIndex = sqlite3_column_int(stmt, 8);\r
1313                 i++;\r
1314         }\r
1315 \r
1316         (*list)->count = i;\r
1317         FAVORITES_LOGE("bookmark list count : %d", (*list)->count);\r
1318 \r
1319         if ((nError != SQLITE_ROW) && (nError != SQLITE_DONE)) {\r
1320                 FAVORITES_LOGE("sqlite3_step is failed(%s).\n",\r
1321                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1322                 _favorites_finalize_bookmark_db(stmt);\r
1323                 return FAVORITES_ERROR_DB_FAILED;\r
1324         }\r
1325 \r
1326         _favorites_finalize_bookmark_db(stmt);\r
1327         return FAVORITES_ERROR_NONE;\r
1328 #endif\r
1329 }\r
1330 \r
1331 int _set_full_tree_to_html_recur(int parent_id, FILE *fp, int depth)\r
1332 {\r
1333         FAVORITES_LOGD("depth: %d", depth);\r
1334         if (parent_id < 0)\r
1335                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
1336         if (fp == NULL)\r
1337                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
1338         if (depth < 0)\r
1339                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
1340 \r
1341         bookmark_list_h child_list = NULL;\r
1342 \r
1343         int ret = _get_list_by_folder(parent_id, &child_list);\r
1344 \r
1345         if (ret != FAVORITES_ERROR_NONE) {\r
1346                 _favorites_free_bookmark_list(child_list);\r
1347                 return FAVORITES_ERROR_DB_FAILED;\r
1348         }\r
1349 \r
1350         int i = 0;\r
1351         int j = 0;\r
1352         FAVORITES_LOGD("list->count: %d", child_list->count);\r
1353         for (i=0; i < (child_list->count); i++) {\r
1354                 FAVORITES_LOGD("title: %s", child_list->item[i].title);\r
1355                 if (child_list->item[i].is_folder) {\r
1356                         for (j=0 ; j < depth ; j++) {\r
1357                                 fputs("\t", fp);\r
1358                         }\r
1359                         int folder_adddate_unixtime = 0;\r
1360                         folder_adddate_unixtime =\r
1361                                 _favorites_get_unixtime_from_datetime(\r
1362                                         child_list->item[i].creationdate);\r
1363                         fprintf(fp, "<DT><H3 FOLDED ADD_DATE=\"%d\">%s</H3>\n",\r
1364                                         folder_adddate_unixtime, child_list->item[i].title);\r
1365                         for (j=0 ; j < depth ; j++) {\r
1366                                 fputs("\t", fp);\r
1367                         }\r
1368                         fputs("<DL><p>\n", fp);\r
1369                         ret = _set_full_tree_to_html_recur(child_list->item[i].id, fp, depth+1);\r
1370                         if (ret != FAVORITES_ERROR_NONE) {\r
1371                                 _favorites_free_bookmark_list(child_list);\r
1372                                 return ret;\r
1373                         }\r
1374                         for (j=0 ; j < depth ; j++) {\r
1375                                 fputs("\t", fp);\r
1376                         }\r
1377                         fputs("</DL><p>\n", fp);\r
1378                 }else {\r
1379                         int bookmark_adddate_unixtime =\r
1380                                 _favorites_get_unixtime_from_datetime(\r
1381                                 child_list->item[i].creationdate);\r
1382 \r
1383                         if(bookmark_adddate_unixtime<0)\r
1384                                 bookmark_adddate_unixtime=0;\r
1385 \r
1386                         int bookmark_updatedate_unixtime =\r
1387                                 _favorites_get_unixtime_from_datetime(\r
1388                                 child_list->item[i].updatedate);\r
1389 \r
1390                         if(bookmark_updatedate_unixtime<0)\r
1391                                 bookmark_updatedate_unixtime=0;\r
1392 \r
1393                         for (j=0 ; j < depth ; j++) {\r
1394                                 fputs("\t", fp);\r
1395                         }\r
1396                         fprintf(fp,"<DT><A HREF=\"%s\" ",\r
1397                                         child_list->item[i].address);\r
1398                         fprintf(fp,"ADD_DATE=\"%d\" ",\r
1399                                         bookmark_adddate_unixtime);\r
1400                         fprintf(fp,"LAST_VISIT=\"%d\" ",\r
1401                                         bookmark_updatedate_unixtime);\r
1402                         fprintf(fp,"LAST_MODIFIED=\"%d\">",\r
1403                                         bookmark_updatedate_unixtime);\r
1404                         fprintf(fp,"%s</A>\n", child_list->item[i].title );\r
1405                 }\r
1406         }\r
1407 \r
1408         _favorites_free_bookmark_list(child_list);\r
1409         return FAVORITES_ERROR_NONE;\r
1410 }\r
1411 \r
1412 /*************************************************************\r
1413  *      APIs for Internet favorites\r
1414  *************************************************************/\r
1415 int favorites_bookmark_get_root_folder_id(int *root_id)\r
1416 {\r
1417         FAVORITES_LOGE("");\r
1418         if (!root_id)\r
1419                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
1420 \r
1421         *root_id = _get_root_folder_id();\r
1422         FAVORITES_LOGE("root_id: %d", *root_id);\r
1423         return FAVORITES_ERROR_NONE;\r
1424 }\r
1425 \r
1426 int favorites_bookmark_add_bookmark(const char *url, const char *title, const char *foldername, int *bookmark_id)\r
1427 {\r
1428         FAVORITES_LOGE("");\r
1429         int ret = 0;\r
1430         int root_id = 1;\r
1431         int folder_id = 1;\r
1432 \r
1433         if (!foldername || (strlen(foldername) <= 0)) {\r
1434                 FAVORITES_LOGE("foldername is empty. id is now root.\n");\r
1435                 folder_id = _get_root_folder_id();\r
1436         } else {\r
1437                 FAVORITES_LOGE("adding folder\n");\r
1438                 ret = favorites_bookmark_add(foldername, url, root_id, 1, &folder_id);\r
1439                 if (ret != FAVORITES_ERROR_NONE) {\r
1440                         if (ret == FAVORITES_ERROR_ITEM_ALREADY_EXIST) {\r
1441                                 folder_id = _get_folder_id(foldername, _get_root_folder_id());\r
1442                         } else\r
1443                                 return ret;\r
1444                 }\r
1445         }\r
1446         return favorites_bookmark_add(title, url, folder_id, 0, bookmark_id);\r
1447 }\r
1448 \r
1449 int favorites_bookmark_add(const char *title, const char *url, int parent_id, int type, int *saved_id)\r
1450 {\r
1451         FAVORITES_LOGD("");\r
1452         int ret = 0;\r
1453         if (type == 0) {\r
1454                 FAVORITES_LOGD("type is bookmark");\r
1455                 ret = _add_bookmark(title, url, parent_id, saved_id);\r
1456         } else {\r
1457                 FAVORITES_LOGD("type is folder");\r
1458                 ret = _add_folder(title, parent_id, saved_id);\r
1459         }\r
1460 \r
1461         return ret;\r
1462 }\r
1463 \r
1464 int favorites_bookmark_get_count(int *count)\r
1465 {\r
1466         FAVORITES_LOGD("");\r
1467 #if defined(BROWSER_BOOKMARK_SYNC)\r
1468         int ids_count = 0;\r
1469         int *ids = NULL;\r
1470         bp_bookmark_adaptor_get_full_ids_p(&ids, &ids_count);\r
1471         *count = ids_count;\r
1472         free(ids);\r
1473 \r
1474         if (*count <0) {\r
1475                 FAVORITES_LOGE("bp_bookmark_adaptor_get_full_ids_p is failed\n");\r
1476                 return FAVORITES_ERROR_DB_FAILED;\r
1477         }\r
1478         return FAVORITES_ERROR_NONE;\r
1479 #else\r
1480         int nError;\r
1481         sqlite3_stmt *stmt;\r
1482 \r
1483         FAVORITES_NULL_ARG_CHECK(count);\r
1484         \r
1485         if (_favorites_open_bookmark_db() < 0) {\r
1486                 FAVORITES_LOGE("db_util_open is failed\n");\r
1487                 return FAVORITES_ERROR_DB_FAILED;\r
1488         }\r
1489 \r
1490         /* folder + bookmark */\r
1491 #if defined(ROOT_IS_ZERO)\r
1492         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1493                                "select count(*) from bookmarks",\r
1494                                -1, &stmt, NULL);\r
1495 #else\r
1496         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1497                                "select count(*) from bookmarks where parent != 0",\r
1498                                -1, &stmt, NULL);\r
1499 #endif\r
1500         if (nError != SQLITE_OK) {\r
1501                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1502                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1503                 _favorites_finalize_bookmark_db(stmt);\r
1504                 return FAVORITES_ERROR_DB_FAILED;\r
1505         }\r
1506 \r
1507         nError = sqlite3_step(stmt);\r
1508         if (nError == SQLITE_ROW) {\r
1509                 *count = sqlite3_column_int(stmt, 0);\r
1510                 _favorites_finalize_bookmark_db(stmt);\r
1511                 return FAVORITES_ERROR_NONE;\r
1512         } else if (nError == SQLITE_DONE) {\r
1513                 _favorites_finalize_bookmark_db(stmt);\r
1514                 return FAVORITES_ERROR_NONE;\r
1515         }\r
1516         _favorites_close_bookmark_db();\r
1517         return FAVORITES_ERROR_DB_FAILED;\r
1518 #endif\r
1519 }\r
1520 \r
1521 int favorites_bookmark_foreach(favorites_bookmark_foreach_cb callback,void *user_data)\r
1522 {\r
1523         FAVORITES_NULL_ARG_CHECK(callback);\r
1524 #if defined(BROWSER_BOOKMARK_SYNC)\r
1525         int ids_count = 0;\r
1526         int *ids = NULL;\r
1527         int func_ret = 0;\r
1528         int is_folder = -1;\r
1529         int editable = -1;\r
1530 \r
1531         if (bp_bookmark_adaptor_get_full_ids_p(&ids, &ids_count) < 0) {\r
1532                 FAVORITES_LOGE ("bp_bookmark_adaptor_get_full_ids_p is failed.\n");\r
1533                 return FAVORITES_ERROR_DB_FAILED;\r
1534         }\r
1535 \r
1536         FAVORITES_LOGD("bp_bookmark_adaptor_get_full_ids_p count : %d", ids_count);\r
1537         if (ids_count > 0) {\r
1538                 int i = 0;\r
1539                 for (i = 0; i < ids_count; i++) {\r
1540                         FAVORITES_LOGD("I[%d] id : %d", i, ids[i]);\r
1541                         favorites_bookmark_entry_s result;\r
1542                         memset(&result, 0x00, sizeof(favorites_bookmark_entry_s));\r
1543                         result.id = ids[i];\r
1544                         bp_bookmark_adaptor_get_type(result.id, &is_folder);\r
1545                         if (is_folder > 0)\r
1546                                 result.is_folder = 1;\r
1547                         else\r
1548                                 result.is_folder = 0;\r
1549                         FAVORITES_LOGD("is_folder : %d", result.is_folder);\r
1550                         bp_bookmark_adaptor_get_parent_id(result.id, &(result.folder_id));\r
1551                         FAVORITES_LOGD("folder_id : %d", result.folder_id);\r
1552                         bp_bookmark_adaptor_get_url(result.id, &(result.address));\r
1553                         FAVORITES_LOGD("address : %s", result.address);\r
1554                         bp_bookmark_adaptor_get_title(result.id, &(result.title));\r
1555                         FAVORITES_LOGD("title : %s", result.title);\r
1556                         bp_bookmark_adaptor_get_is_editable(result.id, &editable);\r
1557                         if (editable > 0)\r
1558                                 result.editable = 1;\r
1559                         else\r
1560                                 result.editable = 0;\r
1561                         bp_bookmark_adaptor_get_sequence(result.id, &(result.order_index));\r
1562                         FAVORITES_LOGD("order_index : %d", result.order_index);\r
1563 \r
1564                         result.creation_date = NULL;\r
1565                         int recv_int = -1;\r
1566                         bp_bookmark_adaptor_get_date_created(result.id, &recv_int);\r
1567                         result.creation_date = (char *)calloc(128, sizeof(char));\r
1568                         strftime(result.creation_date,128,"%Y-%m-%d %H:%M:%S",localtime((time_t *)&recv_int));\r
1569                         FAVORITES_LOGD("Creationdata:%s", result.creation_date);\r
1570 \r
1571                         result.update_date = NULL;\r
1572                         bp_bookmark_adaptor_get_date_modified(result.id, &recv_int);\r
1573                         result.update_date = (char *)calloc(128, sizeof(char));\r
1574                         strftime(result.update_date,128,"%Y-%m-%d %H:%M:%S",localtime((time_t *)&recv_int));\r
1575                         FAVORITES_LOGD("update_date:%s", result.update_date);\r
1576 \r
1577                         result.visit_date = NULL;\r
1578                         bp_bookmark_adaptor_get_date_visited(result.id, &recv_int);\r
1579                         result.visit_date = (char *)calloc(128, sizeof(char));\r
1580                         strftime(result.visit_date,128,"%Y-%m-%d %H:%M:%S",localtime((time_t *)&recv_int));\r
1581                         FAVORITES_LOGD("visit_date:%s", result.visit_date);\r
1582 \r
1583                         func_ret = callback(&result, user_data);\r
1584                         _favorites_free_bookmark_entry(&result);\r
1585                         if(func_ret == 0) \r
1586                                 break;\r
1587                 }\r
1588         }\r
1589         free(ids);\r
1590         FAVORITES_LOGE ("There are no more bookmarks.\n");\r
1591         return FAVORITES_ERROR_NONE;\r
1592 #else\r
1593         int nError;\r
1594         int func_ret = 0;\r
1595         sqlite3_stmt *stmt;\r
1596 \r
1597         if (_favorites_open_bookmark_db() < 0) {\r
1598                 FAVORITES_LOGE("db_util_open is failed\n");\r
1599                 return FAVORITES_ERROR_DB_FAILED;\r
1600         }\r
1601 #if defined(ROOT_IS_ZERO)\r
1602         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1603                                "select id, type, parent, address, title, editable,\\r
1604                                creationdate, updatedate, sequence \\r
1605                                from bookmarks order by sequence",\r
1606                                -1, &stmt, NULL);\r
1607 #else\r
1608         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1609                                "select id, type, parent, address, title, editable,\\r
1610                                creationdate, updatedate, sequence \\r
1611                                from bookmarks where parent != 0 order by sequence",\r
1612                                -1, &stmt, NULL);\r
1613 #endif\r
1614         if (nError != SQLITE_OK) {\r
1615                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1616                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1617                 _favorites_finalize_bookmark_db(stmt);\r
1618                 return FAVORITES_ERROR_DB_FAILED;\r
1619         }\r
1620 \r
1621         while ((nError = sqlite3_step(stmt)) == SQLITE_ROW) {\r
1622                 favorites_bookmark_entry_s result;\r
1623                 memset(&result, 0x00, sizeof(favorites_bookmark_entry_s));\r
1624                 result.id = sqlite3_column_int(stmt, 0);\r
1625                 result.is_folder = sqlite3_column_int(stmt, 1);\r
1626                 result.folder_id = sqlite3_column_int(stmt, 2);\r
1627 \r
1628                 result.address = NULL;\r
1629                 if (!result.is_folder) {\r
1630                         const char *url = (const char *)(sqlite3_column_text(stmt, 3));\r
1631                         if (url) {\r
1632                                 int length = strlen(url);\r
1633                                 if (length > 0) {\r
1634                                         result.address = (char *)calloc(length + 1, sizeof(char));\r
1635                                         memcpy(result.address, url, length);\r
1636                                         FAVORITES_LOGE ("url:%s\n", url);\r
1637                                 }\r
1638                         }\r
1639                 }\r
1640 \r
1641                 const char *title = (const char *)(sqlite3_column_text(stmt, 4));\r
1642                 result.title = NULL;\r
1643                 if (title) {\r
1644                         int length = strlen(title);\r
1645                         if (length > 0) {\r
1646                                 result.title = (char *)calloc(length + 1, sizeof(char));\r
1647                                 memcpy(result.title, title, length);\r
1648                                 FAVORITES_LOGE ("title:%s\n", title);\r
1649                         }\r
1650                 }\r
1651                 result.editable = sqlite3_column_int(stmt, 5);\r
1652 \r
1653                 const char *creation_date = (const char *)(sqlite3_column_text(stmt, 6));\r
1654                 result.creation_date = NULL;\r
1655                 if (creation_date) {\r
1656                         int length = strlen(creation_date);\r
1657                         if (length > 0) {\r
1658                                 result.creation_date = (char *)calloc(length + 1, sizeof(char));\r
1659                                 memcpy(result.creation_date, creation_date, length);\r
1660                         }\r
1661                 }\r
1662                 const char *update_date = (const char *)(sqlite3_column_text(stmt, 7));\r
1663                 result.update_date = NULL;\r
1664                 if (update_date) {\r
1665                         int length = strlen(update_date);\r
1666                         if (length > 0) {\r
1667                                 result.update_date = (char *)calloc(length + 1, sizeof(char));\r
1668                                 memcpy(result.update_date, update_date, length);\r
1669                         }\r
1670                 }\r
1671 \r
1672                 result.order_index = sqlite3_column_int(stmt, 8);\r
1673 \r
1674                 func_ret = callback(&result, user_data);\r
1675                 _favorites_free_bookmark_entry(&result);\r
1676                 if(func_ret == 0) \r
1677                         break;\r
1678         }\r
1679 \r
1680         FAVORITES_LOGE ("There are no more bookmarks.\n");\r
1681         _favorites_finalize_bookmark_db(stmt);\r
1682         return FAVORITES_ERROR_NONE;\r
1683 #endif\r
1684 }\r
1685 \r
1686 int favorites_bookmark_export_list(const char *file_path)\r
1687 {\r
1688         FAVORITES_NULL_ARG_CHECK(file_path);\r
1689 \r
1690         int root_id = -1;\r
1691         favorites_bookmark_get_root_folder_id(&root_id);\r
1692 \r
1693         FILE *fp = NULL;\r
1694         FAVORITES_LOGE("file_path: %s", file_path);\r
1695         fp = fopen(file_path, "w");\r
1696         if (fp == NULL) {\r
1697                 FAVORITES_LOGE("file opening is failed.");\r
1698                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
1699         }\r
1700         fputs("<!DOCTYPE NETSCAPE-Bookmark-file-1>\n", fp);\r
1701         fputs("<!-- This is an automatically generated file.\n", fp);\r
1702         fputs("It will be read and overwritten.\n", fp);\r
1703         fputs("Do Not Edit! -->\n", fp);\r
1704         fputs("<META HTTP-EQUIV=\"Content-Type\" ", fp);\r
1705         fputs("CONTENT=\"text/html; charset=UTF-8\">\n", fp);\r
1706         fputs("<TITLE>Bookmarks</TITLE>\n", fp);\r
1707         fputs("<H1>Bookmarks</H1>\n", fp);\r
1708         fputs("<DL><p>\n", fp);\r
1709 \r
1710         int ret = -1;\r
1711         ret = _set_full_tree_to_html_recur(root_id, fp, 1);\r
1712         if (ret != FAVORITES_ERROR_NONE) {\r
1713                 fclose(fp);\r
1714                 return FAVORITES_ERROR_DB_FAILED;\r
1715         }\r
1716 \r
1717         fputs("</DL><p>\n", fp);\r
1718         fclose(fp);\r
1719         return FAVORITES_ERROR_NONE;\r
1720 }\r
1721 \r
1722 int favorites_bookmark_get_favicon(int id, Evas *evas, Evas_Object **icon)\r
1723 {\r
1724         FAVORITES_LOGD("");\r
1725         FAVORITES_INVALID_ARG_CHECK(id<0);\r
1726         FAVORITES_NULL_ARG_CHECK(evas);\r
1727         FAVORITES_NULL_ARG_CHECK(icon);\r
1728 #if defined(BROWSER_BOOKMARK_SYNC)\r
1729         void *favicon_data_temp=NULL;\r
1730         favicon_entry_h favicon;\r
1731         favicon = (favicon_entry_h) calloc(1, sizeof(favicon_entry_s));\r
1732         int ret = bp_bookmark_adaptor_get_favicon(id, (unsigned char **)&favicon_data_temp, &(favicon->length));\r
1733         if (ret == 0) {\r
1734                 if (bp_bookmark_adaptor_get_favicon_width(id, &(favicon->w)) == 0) {\r
1735                         if (bp_bookmark_adaptor_get_favicon_height(id, &(favicon->h)) == 0) {\r
1736                                 FAVORITES_LOGD("favicon is successfully loaded.");\r
1737                         } else {\r
1738                                 FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon_height is failed");\r
1739                                 *icon = NULL;\r
1740                                 return FAVORITES_ERROR_DB_FAILED;\r
1741                         }\r
1742                 } else {\r
1743                         FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon_width is failed");\r
1744                         *icon = NULL;\r
1745                         return FAVORITES_ERROR_DB_FAILED;\r
1746                 }\r
1747         } else {\r
1748                 FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon is failed");\r
1749                 *icon = NULL;\r
1750                 return FAVORITES_ERROR_DB_FAILED;\r
1751         }\r
1752         FAVORITES_LOGD("len: %d, w:%d, h:%d", favicon->length, favicon->w, favicon->h);\r
1753         if (favicon->length > 0){\r
1754                 *icon = evas_object_image_filled_add(evas);\r
1755                 evas_object_image_colorspace_set(*icon, EVAS_COLORSPACE_ARGB8888);\r
1756                 evas_object_image_size_set(*icon, favicon->w, favicon->h);\r
1757                 evas_object_image_fill_set(*icon, 0, 0, favicon->w, favicon->h);\r
1758                 evas_object_image_filled_set(*icon, EINA_TRUE);\r
1759                 evas_object_image_alpha_set(*icon,EINA_TRUE);\r
1760                 evas_object_image_data_set(*icon, favicon_data_temp);\r
1761                 return FAVORITES_ERROR_NONE;\r
1762         }\r
1763         *icon = NULL;\r
1764         FAVORITES_LOGE("favicon length is 0");\r
1765         return FAVORITES_ERROR_DB_FAILED;\r
1766 #else\r
1767         void *favicon_data_temp=NULL;\r
1768         favicon_entry_h favicon;\r
1769         sqlite3_stmt *stmt;\r
1770         char    query[1024];\r
1771         int nError;\r
1772 \r
1773         memset(&query, 0x00, sizeof(char)*1024);\r
1774         sprintf(query, "select favicon, favicon_length, favicon_w, favicon_h from bookmarks\\r
1775                         where id=%d"\r
1776                         , id);\r
1777         FAVORITES_LOGE("query: %s", query);\r
1778 \r
1779         if (_favorites_open_bookmark_db() < 0) {\r
1780                 FAVORITES_LOGE("db_util_open is failed\n");\r
1781                 return FAVORITES_ERROR_DB_FAILED;\r
1782         }\r
1783 \r
1784         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1785                         query, -1, &stmt, NULL);\r
1786         if (nError != SQLITE_OK) {\r
1787                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1788                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1789                 _favorites_finalize_bookmark_db(stmt);\r
1790                 return FAVORITES_ERROR_DB_FAILED;\r
1791         }\r
1792 \r
1793         nError = sqlite3_step(stmt);\r
1794         if (nError == SQLITE_ROW) {\r
1795                 favicon = (favicon_entry_h) calloc(1, sizeof(favicon_entry_s));\r
1796                 /* loading favicon from bookmark db */\r
1797                 favicon_data_temp = (void *)sqlite3_column_blob(stmt,0);\r
1798                 favicon->length = sqlite3_column_int(stmt,1);\r
1799                 favicon->w = sqlite3_column_int(stmt,2);\r
1800                 favicon->h = sqlite3_column_int(stmt,3);\r
1801 \r
1802                 if (favicon->length > 0){\r
1803                         favicon->data = calloc(1, favicon->length);\r
1804                         memcpy(favicon->data, favicon_data_temp, favicon->length);\r
1805                         /* transforming to evas object */\r
1806                         *icon = evas_object_image_filled_add(evas);\r
1807                         evas_object_image_colorspace_set(*icon,\r
1808                                                         EVAS_COLORSPACE_ARGB8888);\r
1809                         evas_object_image_size_set(*icon, favicon->w, favicon->h);\r
1810                         evas_object_image_fill_set(*icon, 0, 0, favicon->w,\r
1811                                                                         favicon->h);\r
1812                         evas_object_image_filled_set(*icon, EINA_TRUE);\r
1813                         evas_object_image_alpha_set(*icon,EINA_TRUE);\r
1814                         evas_object_image_data_set(*icon, favicon->data);\r
1815                 }\r
1816                 free(favicon);\r
1817                 _favorites_finalize_bookmark_db(stmt);\r
1818                 return FAVORITES_ERROR_NONE;\r
1819         }\r
1820 \r
1821         _favorites_close_bookmark_db();\r
1822         return FAVORITES_ERROR_NONE;\r
1823 #endif\r
1824 }\r
1825 \r
1826 int favorites_bookmark_set_favicon(int bookmark_id, Evas_Object *icon)\r
1827 {\r
1828         FAVORITES_LOGD("");\r
1829         FAVORITES_INVALID_ARG_CHECK(bookmark_id<0);\r
1830         FAVORITES_NULL_ARG_CHECK(icon);\r
1831 #if defined(BROWSER_BOOKMARK_SYNC)\r
1832         int icon_w = 0;\r
1833         int icon_h = 0;\r
1834         int stride = 0;\r
1835         int icon_length = 0;\r
1836         void *icon_data = (void *)evas_object_image_data_get(icon, EINA_TRUE);\r
1837         evas_object_image_size_get(icon, &icon_w, &icon_h);\r
1838         stride = evas_object_image_stride_get(icon);\r
1839         icon_length = icon_h * stride;\r
1840         FAVORITES_LOGD("favicon size  w:%d, h:%d, stride: %d", icon_w, icon_h, stride);\r
1841         int ret = bp_bookmark_adaptor_set_favicon(bookmark_id,(const unsigned char *)icon_data, icon_length);\r
1842         if (ret == 0) {\r
1843                 if (bp_bookmark_adaptor_set_favicon_width(bookmark_id, icon_w) == 0) {\r
1844                         if (bp_bookmark_adaptor_set_favicon_height(bookmark_id, icon_h) == 0) {\r
1845                                 FAVORITES_LOGE("favicon is successfully saved.");\r
1846                                 bp_bookmark_adaptor_publish_notification();\r
1847                                 return FAVORITES_ERROR_NONE;\r
1848                         } else\r
1849                                 FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon_height is failed");\r
1850                 } else\r
1851                         FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon_width is failed");\r
1852         } else\r
1853                 FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon is failed");\r
1854         FAVORITES_LOGE("set favicon is failed");\r
1855         return FAVORITES_ERROR_DB_FAILED;\r
1856 #else\r
1857         int icon_w = 0;\r
1858         int icon_h = 0;\r
1859         int stride = 0;\r
1860         int icon_length = 0;\r
1861         void *icon_data = (void *)evas_object_image_data_get(icon, EINA_TRUE);\r
1862         evas_object_image_size_get(icon, &icon_w, &icon_h);\r
1863         stride = evas_object_image_stride_get(icon);\r
1864         icon_length = icon_h * stride;\r
1865         FAVORITES_LOGD("favicon size  w:%d, h:%d, stride: %d", icon_w, icon_h, stride);\r
1866         int nError;\r
1867         sqlite3_stmt *stmt;\r
1868 \r
1869         if (_favorites_open_bookmark_db() < 0) {\r
1870                 FAVORITES_LOGE("db_util_open is failed\n");\r
1871                 return FAVORITES_ERROR_DB_FAILED;\r
1872         }\r
1873 \r
1874         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1875                                 "UPDATE bookmarks SET favicon=?,\\r
1876                                 favicon_length=?, favicon_w=?, favicon_h=? \\r
1877                                 WHERE id=?", -1, &stmt, NULL);\r
1878 \r
1879         if (nError != SQLITE_OK) {\r
1880                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1881                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1882                 _favorites_finalize_bookmark_db(stmt);\r
1883                 return FAVORITES_ERROR_DB_FAILED;\r
1884         }\r
1885 \r
1886         // bind\r
1887         if (sqlite3_bind_blob(stmt, 1, icon_data , icon_length, NULL) != SQLITE_OK) {\r
1888                 FAVORITES_LOGE("sqlite3_bind_blob(icon_data) is failed");\r
1889                 _favorites_finalize_bookmark_db(stmt);\r
1890                 return FAVORITES_ERROR_DB_FAILED;\r
1891         }\r
1892         if (sqlite3_bind_int(stmt, 2, icon_length) != SQLITE_OK) {\r
1893                 FAVORITES_LOGE("sqlite3_bind_int(icon_length) is failed");\r
1894                 _favorites_finalize_bookmark_db(stmt);\r
1895                 return FAVORITES_ERROR_DB_FAILED;\r
1896         }\r
1897         if (sqlite3_bind_int(stmt, 3, icon_w) != SQLITE_OK) {\r
1898                 FAVORITES_LOGE("sqlite3_bind_int(icon_w) is failed");\r
1899                 _favorites_finalize_bookmark_db(stmt);\r
1900                 return FAVORITES_ERROR_DB_FAILED;\r
1901         }\r
1902         if (sqlite3_bind_int(stmt, 4, icon_h) != SQLITE_OK) {\r
1903                 FAVORITES_LOGE("sqlite3_bind_int(icon_h) is failed");\r
1904                 _favorites_finalize_bookmark_db(stmt);\r
1905                 return FAVORITES_ERROR_DB_FAILED;\r
1906         }\r
1907         if (sqlite3_bind_int(stmt, 5, bookmark_id) != SQLITE_OK) {\r
1908                 FAVORITES_LOGE("sqlite3_bind_int(bookmark_id) is failed");\r
1909                 _favorites_finalize_bookmark_db(stmt);\r
1910                 return FAVORITES_ERROR_DB_FAILED;\r
1911         }\r
1912 \r
1913         nError = sqlite3_step(stmt);\r
1914         if (nError == SQLITE_ROW || nError == SQLITE_DONE) {\r
1915                 _favorites_finalize_bookmark_db(stmt);\r
1916                 return FAVORITES_ERROR_NONE;\r
1917         }\r
1918 \r
1919         FAVORITES_LOGE("sqlite3_step is failed");\r
1920         _favorites_close_bookmark_db();\r
1921         return FAVORITES_ERROR_DB_FAILED;\r
1922 #endif\r
1923 }\r
1924 \r
1925 int favorites_bookmark_delete_bookmark(int id)\r
1926 {\r
1927         FAVORITES_LOGE("");\r
1928         FAVORITES_INVALID_ARG_CHECK(id<0);\r
1929 #if defined(BROWSER_BOOKMARK_SYNC)\r
1930         if (bp_bookmark_adaptor_delete(id) < 0)\r
1931                 return FAVORITES_ERROR_DB_FAILED;\r
1932         else {\r
1933                 bp_bookmark_adaptor_publish_notification();\r
1934                 return FAVORITES_ERROR_NONE;\r
1935         }\r
1936 #else\r
1937         int nError;\r
1938         sqlite3_stmt *stmt;\r
1939 \r
1940         if (_favorites_open_bookmark_db() < 0) {\r
1941                 FAVORITES_LOGE("db_util_open is failed\n");\r
1942                 return FAVORITES_ERROR_DB_FAILED;\r
1943         }\r
1944 \r
1945 #if defined(ROOT_IS_ZERO)\r
1946         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1947                                 "delete from bookmarks where id=?", -1, &stmt, NULL);\r
1948 #else\r
1949         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1950                                 "delete from bookmarks where id=? and parent != 0", -1, &stmt, NULL);\r
1951 #endif\r
1952         if (nError != SQLITE_OK) {\r
1953                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1954                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1955                 _favorites_finalize_bookmark_db(stmt);\r
1956                 return FAVORITES_ERROR_DB_FAILED;\r
1957         }\r
1958         // bind\r
1959         if (sqlite3_bind_int(stmt, 1, id) != SQLITE_OK) {\r
1960                 FAVORITES_LOGE("sqlite3_bind_int is failed");\r
1961                 _favorites_finalize_bookmark_db(stmt);\r
1962                 return FAVORITES_ERROR_DB_FAILED;\r
1963         }\r
1964         nError = sqlite3_step(stmt);\r
1965         if (nError == SQLITE_ROW || nError == SQLITE_DONE) {\r
1966                 _favorites_finalize_bookmark_db(stmt);\r
1967                 return FAVORITES_ERROR_NONE;\r
1968         }\r
1969         FAVORITES_LOGE("sqlite3_step is failed");\r
1970         _favorites_close_bookmark_db();\r
1971         return FAVORITES_ERROR_DB_FAILED;\r
1972 #endif\r
1973 }\r
1974 \r
1975 int favorites_bookmark_delete_all_bookmarks(void)\r
1976 {\r
1977         FAVORITES_LOGD("");\r
1978 #if defined(BROWSER_BOOKMARK_SYNC)\r
1979         int ids_count = 0;\r
1980         int *ids = NULL;\r
1981 \r
1982         if (bp_bookmark_adaptor_get_full_ids_p(&ids, &ids_count) < 0) {\r
1983                 FAVORITES_LOGE ("bp_bookmark_adaptor_get_full_ids_p is failed.\n");\r
1984                 return FAVORITES_ERROR_DB_FAILED;\r
1985         }\r
1986 \r
1987         FAVORITES_LOGD("bp_bookmark_adaptor_get_full_ids_p count : %d", ids_count);\r
1988         if (ids_count > 0) {\r
1989                 int i = 0;\r
1990                 for (i = 0; i < ids_count; i++) {\r
1991                         FAVORITES_LOGD("I[%d] id : %d", i, ids[i]);\r
1992                         if (bp_bookmark_adaptor_delete(ids[i]) < 0) {\r
1993                                 free(ids);\r
1994                                 return FAVORITES_ERROR_DB_FAILED;\r
1995                         } else\r
1996                                 bp_bookmark_adaptor_publish_notification();\r
1997                 }\r
1998         }\r
1999         free(ids);\r
2000         return FAVORITES_ERROR_NONE;\r
2001 #else\r
2002         int nError;\r
2003         sqlite3_stmt *stmt;\r
2004 \r
2005         if (_favorites_open_bookmark_db() < 0) {\r
2006                 FAVORITES_LOGE("db_util_open is failed\n");\r
2007                 return FAVORITES_ERROR_DB_FAILED;\r
2008         }\r
2009 \r
2010 #if defined(ROOT_IS_ZERO)\r
2011         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
2012                                 "delete from bookmarks", -1, &stmt, NULL);\r
2013 #else\r
2014         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
2015                                 "delete from bookmarks where parent !=0", -1, &stmt, NULL);\r
2016 #endif\r
2017         if (nError != SQLITE_OK) {\r
2018                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
2019                         sqlite3_errmsg(gl_internet_bookmark_db));\r
2020                 _favorites_finalize_bookmark_db(stmt);\r
2021                 return FAVORITES_ERROR_DB_FAILED;\r
2022         }\r
2023         nError = sqlite3_step(stmt);\r
2024         if (nError == SQLITE_ROW || nError == SQLITE_DONE) {\r
2025                 _favorites_finalize_bookmark_db(stmt);\r
2026                 return FAVORITES_ERROR_NONE;\r
2027         }\r
2028         FAVORITES_LOGE("sqlite3_step is failed");\r
2029         _favorites_close_bookmark_db();\r
2030         return FAVORITES_ERROR_DB_FAILED;\r
2031 #endif\r
2032 }\r
2033 \r