d548b5d5196b26ec2d88b6e6612397bf2f46a5e9
[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         return NULL;\r
475 }\r
476 \r
477 int _favorites_get_unixtime_from_datetime(char *datetime)\r
478 {\r
479         int nError;\r
480         sqlite3_stmt *stmt;\r
481 \r
482         if(datetime == NULL ) {\r
483                 FAVORITES_LOGE("datetime is NULL\n");\r
484                 return -1;\r
485         }\r
486 \r
487         FAVORITES_LOGE("datetime: %s\n", datetime);\r
488 \r
489         if (_favorites_open_bookmark_db() < 0) {\r
490                 FAVORITES_LOGE("db_util_open is failed\n");\r
491                 return -1;\r
492         }\r
493 \r
494         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
495                                "SELECT strftime('%s', ?)",\r
496                                -1, &stmt, NULL);\r
497         if (nError != SQLITE_OK) {\r
498                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
499                         sqlite3_errmsg(gl_internet_bookmark_db));\r
500                 _favorites_finalize_bookmark_db(stmt);\r
501                 return -1;\r
502         }\r
503 \r
504         if (sqlite3_bind_text(stmt, 1, datetime, -1, NULL) != SQLITE_OK) {\r
505                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
506                 _favorites_finalize_bookmark_db(stmt);\r
507                 return -1;\r
508         }\r
509 \r
510         nError = sqlite3_step(stmt);\r
511         if (nError == SQLITE_ROW) {\r
512                 int unixtime = sqlite3_column_int(stmt, 0);\r
513                 _favorites_finalize_bookmark_db(stmt);\r
514                 return unixtime;\r
515         } else if (nError == SQLITE_DONE) {\r
516                 _favorites_finalize_bookmark_db(stmt);\r
517                 return 0;\r
518         }\r
519         _favorites_close_bookmark_db();\r
520         return -1;\r
521 }\r
522 \r
523 int _add_bookmark(const char *title, const char *address, int parent_id, int *saved_bookmark_id)\r
524 {\r
525         FAVORITES_LOGD("");\r
526         if (title == NULL || strlen(title) <= 0) {\r
527                 FAVORITES_LOGD("Invalid param: Title is empty");\r
528                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
529         }\r
530         if (address == NULL || strlen(address) <= 0) {\r
531                 FAVORITES_LOGD("Invalid param: Address is empty");\r
532                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
533         }\r
534         if (memcmp(address, "file://", 7) == 0) {\r
535                 FAVORITES_LOGD("Not Allow file://");\r
536                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
537         }\r
538 #if defined(BROWSER_BOOKMARK_SYNC)\r
539         if (parent_id != _get_root_folder_id()) {\r
540                 /* check whether folder is exist or not. */\r
541                 if (_get_exists_id(parent_id) <= 0) {\r
542                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
543                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
544                 }\r
545         }\r
546         FAVORITES_LOGD("[%s][%s][%d]", title, address, parent_id);\r
547         int id = -1;\r
548         int *ids = NULL;\r
549         int ids_count = -1;\r
550         bp_bookmark_adaptor_get_duplicated_url_ids_p(&ids, &ids_count, 1,\r
551                 -1, -1, 0, 0, address, 0);\r
552         if (ids_count > 0)\r
553                 id = ids[0];\r
554         free(ids);\r
555 \r
556         if (ids_count > 0) {\r
557                 FAVORITES_LOGE("same URI is already exist");\r
558                 return FAVORITES_ERROR_ITEM_ALREADY_EXIST;\r
559         }\r
560 \r
561         bp_bookmark_info_fmt info;\r
562         memset(&info, 0x00, sizeof(bp_bookmark_info_fmt));\r
563         info.type = 0;\r
564         info.parent = parent_id;\r
565         info.sequence = -1;\r
566         info.access_count = -1;\r
567         info.editable = 1;\r
568         time_t seconds = 0;\r
569         info.date_created = time(&seconds);\r
570         info.date_modified = time(&seconds);\r
571         if (address != NULL && strlen(address) > 0)\r
572                 info.url = strdup(address);\r
573         if (title != NULL && strlen(title) > 0)\r
574                 info.title = strdup(title);\r
575 \r
576         int ret = bp_bookmark_adaptor_easy_create(&id, &info);\r
577         if (ret == 0) {\r
578                 ret = bp_bookmark_adaptor_set_sequence(id, -1); // max sequence\r
579                 bp_bookmark_adaptor_easy_free(&info);\r
580                 if (ret == 0) {\r
581                         *saved_bookmark_id = id;\r
582                         FAVORITES_LOGD("bp_bookmark_adaptor_easy_create is success(id:%d)", *saved_bookmark_id);\r
583                         bp_bookmark_adaptor_publish_notification();\r
584                         return FAVORITES_ERROR_NONE;\r
585                 }\r
586         }\r
587         int errcode = bp_bookmark_adaptor_get_errorcode();\r
588         bp_bookmark_adaptor_easy_free(&info);\r
589         FAVORITES_LOGE("bp_bookmark_adaptor_easy_create is failed[%d]", errcode);\r
590         return FAVORITES_ERROR_DB_FAILED;\r
591 #else\r
592         int nError;\r
593         sqlite3_stmt *stmt;\r
594 #if defined(ROOT_IS_ZERO)\r
595         if (parent_id != _get_root_folder_id()) {\r
596                 /* check whether folder is exist or not. */\r
597                 if (_get_exists_id(parent_id) <= 0) {\r
598                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
599                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
600                 }\r
601         }\r
602 #else\r
603         if (parent_id > 0) {\r
604                 /* check whether folder is exist or not. */\r
605                 if (_get_exists_id(parent_id) <= 0) {\r
606                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
607                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
608                 }\r
609         } else {\r
610                 parent_id = _get_root_folder_id();\r
611         }\r
612 #endif\r
613         FAVORITES_LOGD("[%s][%s][%d]", title, address, parent_id);\r
614 \r
615         int uid = _get_exists_bookmark(address);\r
616         /* ignore this call.. already exist. */\r
617         if (uid > 0) {\r
618                 FAVORITES_LOGE("Bookmark is already exist. cancel the add operation.");\r
619                 return FAVORITES_ERROR_ITEM_ALREADY_EXIST;\r
620         }\r
621 \r
622         int lastIndex = _favorites_get_bookmark_lastindex(parent_id);\r
623         if (lastIndex < 0) {\r
624                 FAVORITES_LOGE("Database::getLastIndex() is failed.\n");\r
625                 return FAVORITES_ERROR_DB_FAILED;\r
626         }\r
627 \r
628         if (_favorites_open_bookmark_db() < 0) {\r
629                 FAVORITES_LOGE("db_util_open is failed\n");\r
630                 return FAVORITES_ERROR_DB_FAILED;\r
631         }\r
632 \r
633         nError =\r
634             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
635                                         "INSERT INTO bookmarks (type, parent, address, title, creationdate,\\r
636                                         updatedate, editable, sequence)\\r
637                                         VALUES (0, ?, ?, ?, DATETIME('now'), DATETIME('now'), 1, ?)",\r
638                                -1, &stmt, NULL);\r
639         if (nError != SQLITE_OK) {\r
640                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
641                         sqlite3_errmsg(gl_internet_bookmark_db));\r
642                 _favorites_finalize_bookmark_db(stmt);\r
643                 return FAVORITES_ERROR_DB_FAILED;\r
644         }\r
645 \r
646         /*parent */\r
647         if (sqlite3_bind_int(stmt, 1, parent_id) != SQLITE_OK) {\r
648                 FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
649                 _favorites_finalize_bookmark_db(stmt);\r
650                 return FAVORITES_ERROR_DB_FAILED;\r
651         }\r
652         /*address */\r
653         if (sqlite3_bind_text(stmt, 2, address, -1, NULL) != SQLITE_OK) {\r
654                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
655                 _favorites_finalize_bookmark_db(stmt);\r
656                 return FAVORITES_ERROR_DB_FAILED;\r
657         }\r
658         /*title */\r
659         if (sqlite3_bind_text(stmt, 3, title, -1, NULL) != SQLITE_OK) {\r
660                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
661                 _favorites_finalize_bookmark_db(stmt);\r
662                 return FAVORITES_ERROR_DB_FAILED;\r
663         }\r
664         /* order */\r
665         if (lastIndex >= 0) {\r
666                 if (sqlite3_bind_int(stmt, 4, (lastIndex + 1)) != SQLITE_OK) {\r
667                         FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
668                         _favorites_finalize_bookmark_db(stmt);\r
669                         return FAVORITES_ERROR_DB_FAILED;\r
670                 }\r
671         }\r
672         nError = sqlite3_step(stmt);\r
673         if (nError == SQLITE_OK || nError == SQLITE_DONE) {\r
674                 _favorites_finalize_bookmark_db(stmt);\r
675                 int id = _get_bookmark_id(address);\r
676                 if (id > 0)\r
677                         *saved_bookmark_id = id;\r
678                 return FAVORITES_ERROR_NONE;\r
679         }\r
680         FAVORITES_LOGE("sqlite3_step is failed");\r
681         _favorites_close_bookmark_db();\r
682         return FAVORITES_ERROR_DB_FAILED;\r
683 #endif\r
684 }\r
685 \r
686 int _add_folder(const char *title, int parent_id, int *saved_folder_id)\r
687 {\r
688         FAVORITES_LOGD("");\r
689         if (title == NULL || strlen(title) <= 0) {\r
690                 FAVORITES_LOGD("Invalid param: Title is empty");\r
691                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
692         }\r
693 #if defined(BROWSER_BOOKMARK_SYNC)\r
694         if (parent_id != _get_root_folder_id()) {\r
695                 /* check whether folder is exist or not. */\r
696                 if (_get_exists_id(parent_id) <= 0) {\r
697                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
698                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
699                 }\r
700         }\r
701         int id = -1;\r
702         int *ids = NULL;\r
703         int ids_count = -1;\r
704         bp_bookmark_adaptor_get_duplicated_title_ids_p(&ids, &ids_count, 1,\r
705                 -1, parent_id, 1, 0, title, 0);\r
706         if (ids_count > 0)\r
707                 id = ids[0];\r
708         free(ids);\r
709 \r
710         if (ids_count > 0) {\r
711                 FAVORITES_LOGE("same title with same parent is already exist");\r
712                 return FAVORITES_ERROR_ITEM_ALREADY_EXIST;\r
713         }\r
714 \r
715         bp_bookmark_info_fmt info;\r
716         memset(&info, 0x00, sizeof(bp_bookmark_info_fmt));\r
717         info.type = 1;\r
718         info.parent = parent_id;\r
719         info.sequence = -1;\r
720         info.access_count = -1;\r
721         info.editable = 1;\r
722         if (title != NULL && strlen(title) > 0)\r
723                 info.title = strdup(title);\r
724         int ret = bp_bookmark_adaptor_easy_create(&id, &info);\r
725         if (ret == 0) {\r
726                 ret = bp_bookmark_adaptor_set_sequence(id, -1); // max sequence\r
727                 bp_bookmark_adaptor_easy_free(&info);\r
728                 if (ret == 0) {\r
729                         *saved_folder_id = id;\r
730                         FAVORITES_LOGD("bmsvc_add_bookmark is success(id:%d)", *saved_folder_id);\r
731                         bp_bookmark_adaptor_publish_notification();\r
732                         return FAVORITES_ERROR_NONE;\r
733                 }\r
734         }\r
735         int errcode = bp_bookmark_adaptor_get_errorcode();\r
736         bp_bookmark_adaptor_easy_free(&info);\r
737         FAVORITES_LOGE("bp_bookmark_adaptor_easy_create is failed[%d]", errcode);\r
738         return FAVORITES_ERROR_DB_FAILED;\r
739 #else\r
740 #if defined(ROOT_IS_ZERO)\r
741         if (parent_id != _get_root_folder_id()) {\r
742                 /* check whether folder is exist or not. */\r
743                 if (_get_exists_id(parent_id) <= 0) {\r
744                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
745                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
746                 }\r
747         }\r
748 #else\r
749         if (parent_id > 0) {\r
750                 /* check whether folder is exist or not. */\r
751                 if (_get_exists_id(parent_id) <= 0) {\r
752                         FAVORITES_LOGE("Not found parent folder (%d)\n", parent_id);\r
753                         return FAVORITES_ERROR_NO_SUCH_FILE;\r
754                 }\r
755         } else {\r
756                 parent_id = _get_root_folder_id();\r
757         }\r
758 #endif\r
759         FAVORITES_LOGD("[%s][%d]", title, parent_id);\r
760 \r
761         int uid = _get_exists_folder(title, parent_id);\r
762         /* ignore this call.. already exist. */\r
763         if (uid > 0) {\r
764                 FAVORITES_LOGE("Folder is already exist. cancel the add operation.");\r
765                 return FAVORITES_ERROR_ITEM_ALREADY_EXIST;\r
766         }\r
767 \r
768         int lastIndex = _favorites_get_bookmark_lastindex(parent_id);\r
769         if (lastIndex < 0) {\r
770                 FAVORITES_LOGE("Database::getLastIndex() is failed.\n");\r
771                 return FAVORITES_ERROR_DB_FAILED;\r
772         }\r
773         if (_favorites_open_bookmark_db() < 0) {\r
774                 FAVORITES_LOGE("db_util_open is failed\n");\r
775                 return FAVORITES_ERROR_DB_FAILED;\r
776         }\r
777 \r
778         int nError;\r
779         sqlite3_stmt *stmt;\r
780 \r
781         nError =\r
782             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
783                                         "INSERT INTO bookmarks (type, parent, title, creationdate,\\r
784                                         updatedate, sequence, editable)\\r
785                                         VALUES (1, ?, ?, DATETIME('now'), DATETIME('now'), ?, 1)",\r
786                                -1, &stmt, NULL);\r
787         if (nError != SQLITE_OK) {\r
788                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
789                         sqlite3_errmsg(gl_internet_bookmark_db));\r
790                 _favorites_finalize_bookmark_db(stmt);\r
791                 return FAVORITES_ERROR_DB_FAILED;\r
792         }\r
793 \r
794         /*parent */\r
795         if (sqlite3_bind_int(stmt, 1, parent_id) != SQLITE_OK) {\r
796                 FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
797                 _favorites_finalize_bookmark_db(stmt);\r
798                 return FAVORITES_ERROR_DB_FAILED;\r
799         }\r
800         /*title */\r
801         if (sqlite3_bind_text(stmt, 2, title, -1, NULL) != SQLITE_OK) {\r
802                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
803                 _favorites_finalize_bookmark_db(stmt);\r
804                 return FAVORITES_ERROR_DB_FAILED;\r
805         }\r
806         /* order */\r
807         if (lastIndex >= 0) {\r
808                 if (sqlite3_bind_int(stmt, 3, (lastIndex + 1)) != SQLITE_OK) {\r
809                         FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
810                         _favorites_finalize_bookmark_db(stmt);\r
811                         return FAVORITES_ERROR_DB_FAILED;\r
812                 }\r
813         }\r
814 \r
815         nError = sqlite3_step(stmt);\r
816         if (nError == SQLITE_OK || nError == SQLITE_DONE) {\r
817                 _favorites_finalize_bookmark_db(stmt);\r
818                 int id = _get_folder_id(title, parent_id);\r
819                 if (id > 0)\r
820                         *saved_folder_id = id;\r
821                 return FAVORITES_ERROR_NONE;\r
822         }\r
823         FAVORITES_LOGE("sqlite3_step is failed");\r
824         _favorites_close_bookmark_db();\r
825         return FAVORITES_ERROR_DB_FAILED;\r
826 #endif\r
827 }\r
828 \r
829 \r
830 int _get_root_folder_id(void)\r
831 {\r
832 #if defined(BROWSER_BOOKMARK_SYNC)\r
833         FAVORITES_LOGE("SYNC");\r
834         int root_id = -1;\r
835         bp_bookmark_adaptor_get_root(&root_id);\r
836         return root_id;\r
837 #else\r
838 #if defined(ROOT_IS_ZERO)\r
839         return 0;\r
840 #else\r
841         FAVORITES_LOGE("NOT SYNC");\r
842         return 1;\r
843 #endif\r
844 #endif\r
845 }\r
846 \r
847 int _get_exists_id(int id)\r
848 {\r
849         FAVORITES_LOGE("");\r
850 #if defined(BROWSER_BOOKMARK_SYNC)\r
851         int value = -1;\r
852         int ret = -1;\r
853         int errorcode = -1;\r
854         ret = bp_bookmark_adaptor_get_type(id, &value);\r
855         if (ret < 0) {\r
856                 errorcode = bp_bookmark_adaptor_get_errorcode();\r
857                 if (errorcode == BP_BOOKMARK_ERROR_NO_DATA)\r
858                         return 0;\r
859                 else\r
860                         return -1;\r
861         }\r
862         return 1;\r
863 #else\r
864         int nError;\r
865         sqlite3_stmt *stmt;\r
866 \r
867         if (id < 0)\r
868                 return -1;\r
869 \r
870         if (_favorites_open_bookmark_db() < 0) {\r
871                 FAVORITES_LOGE("db_util_open is failed\n");\r
872                 return -1;\r
873         }\r
874 \r
875         /* same title  */\r
876         nError =\r
877             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
878                                "select * from bookmarks where id=?",\r
879                                -1, &stmt, NULL);\r
880 \r
881         if (nError != SQLITE_OK) {\r
882                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
883                         sqlite3_errmsg(gl_internet_bookmark_db));\r
884                 _favorites_finalize_bookmark_db(stmt);\r
885                 return -1;\r
886         }\r
887         if (sqlite3_bind_int(stmt, 1, id) != SQLITE_OK) {\r
888                 FAVORITES_LOGE("sqlite3_bind_int is failed.");\r
889                 _favorites_finalize_bookmark_db(stmt);\r
890                 return -1;\r
891         }\r
892         nError = sqlite3_step(stmt);\r
893         if (nError == SQLITE_ROW) {\r
894                 /* The given foldername is exist on the bookmark table */\r
895                 _favorites_finalize_bookmark_db(stmt);\r
896                 return 1;\r
897         } else if (nError == SQLITE_DONE) {\r
898                 _favorites_finalize_bookmark_db(stmt);\r
899                 return 0;\r
900         }\r
901 \r
902         _favorites_close_bookmark_db();\r
903         return -1;\r
904 #endif\r
905 }\r
906 \r
907 int _get_exists_bookmark(const char *address)\r
908 {\r
909         FAVORITES_LOGD("");\r
910         int nError;\r
911         sqlite3_stmt *stmt;\r
912 \r
913         if (_favorites_open_bookmark_db() < 0) {\r
914                 FAVORITES_LOGE("db_util_open is failed\n");\r
915                 return -1;\r
916         }\r
917         /* same title  */\r
918         nError =\r
919             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
920                                "select * from bookmarks where address=? and type=0",\r
921                                -1, &stmt, NULL);\r
922 \r
923         if (nError != SQLITE_OK) {\r
924                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
925                         sqlite3_errmsg(gl_internet_bookmark_db));\r
926                 _favorites_finalize_bookmark_db(stmt);\r
927                 return -1;\r
928         }\r
929 \r
930         if (sqlite3_bind_text(stmt, 1, address, -1, NULL) != SQLITE_OK) {\r
931                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
932                 _favorites_finalize_bookmark_db(stmt);\r
933                 return -1;\r
934         }\r
935 \r
936         nError = sqlite3_step(stmt);\r
937         if (nError == SQLITE_ROW) {\r
938                 _favorites_finalize_bookmark_db(stmt);\r
939                 return 1;\r
940         } else if (nError == SQLITE_DONE) {\r
941                 //FAVORITES_LOGE("bookmark doesn't exists");\r
942                 _favorites_finalize_bookmark_db(stmt);\r
943                 return 0;\r
944         }\r
945         _favorites_close_bookmark_db();\r
946         return -1;\r
947 }\r
948 \r
949 int _get_exists_folder(const char *title, int parent_id)\r
950 {\r
951         FAVORITES_LOGD("");\r
952         int nError;\r
953         sqlite3_stmt *stmt;\r
954 \r
955         if (_favorites_open_bookmark_db() < 0) {\r
956                 FAVORITES_LOGE("db_util_open is failed\n");\r
957                 return -1;\r
958         }\r
959         /* same title  */\r
960         nError =\r
961             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
962                                "select * from bookmarks where title=? and parent=? and type=1",\r
963                                -1, &stmt, NULL);\r
964 \r
965         if (nError != SQLITE_OK) {\r
966                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
967                         sqlite3_errmsg(gl_internet_bookmark_db));\r
968                 _favorites_finalize_bookmark_db(stmt);\r
969                 return -1;\r
970         }\r
971 \r
972         if (sqlite3_bind_text(stmt, 1, title, -1, NULL) != SQLITE_OK) {\r
973                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
974                 _favorites_finalize_bookmark_db(stmt);\r
975                 return -1;\r
976         }\r
977 \r
978         if (sqlite3_bind_int(stmt, 2, parent_id) != SQLITE_OK) {\r
979                 FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
980                 _favorites_finalize_bookmark_db(stmt);\r
981                 return -1;\r
982         }\r
983 \r
984         nError = sqlite3_step(stmt);\r
985         if (nError == SQLITE_ROW) {\r
986                 _favorites_finalize_bookmark_db(stmt);\r
987                 return 1;\r
988         } else if (nError == SQLITE_DONE) {\r
989                 //FAVORITES_LOGE("folder doesn't exists");\r
990                 _favorites_finalize_bookmark_db(stmt);\r
991                 return 0;\r
992         }\r
993         _favorites_close_bookmark_db();\r
994         return -1;\r
995 }\r
996 \r
997 int _get_bookmark_id(const char *address)\r
998 {\r
999         FAVORITES_LOGE("");\r
1000 #if defined(BROWSER_BOOKMARK_SYNC)\r
1001         int id = -1;\r
1002         int *ids = NULL;\r
1003         int ids_count = -1;\r
1004         bp_bookmark_adaptor_get_duplicated_url_ids_p(&ids, &ids_count, 1,\r
1005                 -1, -1, 0, 0, address, 0);\r
1006         if (ids_count > 0)\r
1007                 id = ids[0];\r
1008         free(ids);\r
1009 \r
1010         if (ids_count > 0) {\r
1011                 return id;\r
1012         }\r
1013         FAVORITES_LOGE("There is no such bookmark url exists.");\r
1014         return -1;\r
1015 #else\r
1016         int nError;\r
1017         sqlite3_stmt *stmt;\r
1018 \r
1019         if (_favorites_open_bookmark_db() < 0) {\r
1020                 FAVORITES_LOGE("db_util_open is failed\n");\r
1021                 return -1;\r
1022         }\r
1023         /* same title  */\r
1024         nError =\r
1025             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1026                                "select id from bookmarks where address=? and type=0",\r
1027                                -1, &stmt, NULL);\r
1028 \r
1029         if (nError != SQLITE_OK) {\r
1030                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1031                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1032                 _favorites_finalize_bookmark_db(stmt);\r
1033                 return -1;\r
1034         }\r
1035 \r
1036         if (sqlite3_bind_text(stmt, 1, address, -1, NULL) != SQLITE_OK) {\r
1037                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
1038                 _favorites_finalize_bookmark_db(stmt);\r
1039                 return -1;\r
1040         }\r
1041 \r
1042         nError = sqlite3_step(stmt);\r
1043         if (nError == SQLITE_ROW) {\r
1044                 int id = sqlite3_column_int(stmt, 0);\r
1045                 _favorites_finalize_bookmark_db(stmt);\r
1046                 return id;\r
1047         } else if (nError == SQLITE_DONE) {\r
1048                 //FAVORITES_LOGE("bookmark id doesn't exists");\r
1049                 _favorites_finalize_bookmark_db(stmt);\r
1050                 return 0;\r
1051         }\r
1052         FAVORITES_LOGE("sqlite3_step is failed(%s).\n",\r
1053                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1054         _favorites_close_bookmark_db();\r
1055         return -1;\r
1056 #endif\r
1057 }\r
1058 \r
1059 int _get_folder_id(const char *title, const int parent)\r
1060 {\r
1061         FAVORITES_LOGE("");\r
1062 #if defined(BROWSER_BOOKMARK_SYNC)\r
1063         int id = -1;\r
1064         int *ids = NULL;\r
1065         int ids_count = -1;\r
1066         bp_bookmark_adaptor_get_duplicated_title_ids_p(&ids, &ids_count, 1,\r
1067                 -1, parent, 1, 0, title, 0);\r
1068         if (ids_count > 0)\r
1069                 id = ids[0];\r
1070         free(ids);\r
1071 \r
1072         if (ids_count > 0) {\r
1073                 return id;\r
1074         }\r
1075         FAVORITES_LOGE("There is no such folder exists.");\r
1076         return -1;\r
1077 #else\r
1078         int nError;\r
1079         sqlite3_stmt *stmt;\r
1080 \r
1081         if (_favorites_open_bookmark_db() < 0) {\r
1082                 FAVORITES_LOGE("db_util_open is failed\n");\r
1083                 return -1;\r
1084         }\r
1085         /* same title  */\r
1086         nError =\r
1087             sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1088                                "SELECT id FROM bookmarks WHERE title=? AND parent=? AND type=1",\r
1089                                -1, &stmt, NULL);\r
1090 \r
1091         if (nError != SQLITE_OK) {\r
1092                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1093                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1094                 _favorites_finalize_bookmark_db(stmt);\r
1095                 return -1;\r
1096         }\r
1097 \r
1098         if (sqlite3_bind_text(stmt, 1, title, -1, NULL) != SQLITE_OK) {\r
1099                 FAVORITES_LOGE("sqlite3_bind_text is failed.\n");\r
1100                 _favorites_finalize_bookmark_db(stmt);\r
1101                 return -1;\r
1102         }\r
1103         if (sqlite3_bind_int(stmt, 2, parent) != SQLITE_OK) {\r
1104                 FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
1105                 _favorites_finalize_bookmark_db(stmt);\r
1106                 return -1;\r
1107         }\r
1108 \r
1109         nError = sqlite3_step(stmt);\r
1110         if (nError == SQLITE_ROW) {\r
1111                 int id = sqlite3_column_int(stmt, 0);\r
1112                 _favorites_finalize_bookmark_db(stmt);\r
1113                 return id;\r
1114         } else if (nError == SQLITE_DONE) {\r
1115                 //FAVORITES_LOGE("folder id doesn't exists");\r
1116                 _favorites_finalize_bookmark_db(stmt);\r
1117                 return 0;\r
1118         }\r
1119         FAVORITES_LOGE("sqlite3_step is failed(%s).\n",\r
1120                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1121         _favorites_close_bookmark_db();\r
1122         return -1;\r
1123 #endif\r
1124 }\r
1125 \r
1126 int _get_count_by_folder(int parent_id)\r
1127 {\r
1128         int nError;\r
1129         sqlite3_stmt *stmt;\r
1130         FAVORITES_LOGE("");\r
1131         \r
1132         if (_favorites_open_bookmark_db() < 0) {\r
1133                 FAVORITES_LOGE("db_util_open is failed\n");\r
1134                 return -1;\r
1135         }\r
1136 \r
1137         /*bookmark */\r
1138         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1139                                "select count(*) from bookmarks where parent=?",\r
1140                                -1, &stmt, NULL);\r
1141         if (nError != SQLITE_OK) {\r
1142                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1143                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1144                 _favorites_finalize_bookmark_db(stmt);\r
1145                 return -1;\r
1146         }\r
1147 \r
1148         if (sqlite3_bind_int(stmt, 1, parent_id) != SQLITE_OK) {\r
1149                 FAVORITES_LOGE("sqlite3_bind_int is failed");\r
1150                 _favorites_finalize_bookmark_db(stmt);\r
1151                 return -1;\r
1152         }\r
1153 \r
1154         nError = sqlite3_step(stmt);\r
1155         if (nError == SQLITE_ROW) {\r
1156                 int count = sqlite3_column_int(stmt, 0);\r
1157                 _favorites_finalize_bookmark_db(stmt);\r
1158                 FAVORITES_LOGE("count: %d", count);\r
1159                 return count;\r
1160         } else if (nError == SQLITE_DONE) {\r
1161                 FAVORITES_LOGE("No rows");\r
1162                 _favorites_finalize_bookmark_db(stmt);\r
1163                 return -1;\r
1164         }\r
1165         FAVORITES_LOGE("sqlite3_step is failed");\r
1166         _favorites_close_bookmark_db();\r
1167         return -1;\r
1168 }\r
1169 \r
1170 \r
1171 int _get_list_by_folder(int parent_id, bookmark_list_h *list)\r
1172 {\r
1173         FAVORITES_LOGD("");\r
1174 #if defined(BROWSER_BOOKMARK_SYNC)\r
1175         int *ids = NULL;\r
1176         int ids_count = -1;\r
1177         bp_bookmark_info_fmt info;\r
1178         if (bp_bookmark_adaptor_get_sequence_child_ids_p(&ids, &ids_count, -1, 0, parent_id, -1, 0) < 0) {\r
1179                 FAVORITES_LOGE("bp_bookmark_adaptor_get_sequence_child_ids_p is failed");\r
1180                 return FAVORITES_ERROR_DB_FAILED;\r
1181         }\r
1182 \r
1183         FAVORITES_LOGD("ids_count: %d", ids_count);\r
1184         if (ids_count < 0) {\r
1185                 FAVORITES_LOGE("bookmark list is empty");\r
1186                 return FAVORITES_ERROR_DB_FAILED;\r
1187         }\r
1188 \r
1189         *list = (bookmark_list_h) calloc(1, sizeof(bookmark_list_s));\r
1190         (*list)->count = 0;\r
1191         (*list)->item =\r
1192             (bookmark_entry_internal_h) calloc(ids_count, sizeof(bookmark_entry_internal_s));\r
1193         int i = 0;\r
1194         for(i = 0; i < ids_count; i++) {\r
1195                 (*list)->item[i].id = ids[i];\r
1196                 if (bp_bookmark_adaptor_get_info(ids[i], (BP_BOOKMARK_O_TYPE |\r
1197                                 BP_BOOKMARK_O_PARENT | BP_BOOKMARK_O_SEQUENCE |\r
1198                                 BP_BOOKMARK_O_IS_EDITABLE | BP_BOOKMARK_O_URL |\r
1199                                 BP_BOOKMARK_O_TITLE | BP_BOOKMARK_O_DATE_CREATED |\r
1200                                 BP_BOOKMARK_O_DATE_MODIFIED), &info) == 0) {\r
1201                         (*list)->item[i].is_folder = info.type;\r
1202                         (*list)->item[i].folder_id = info.parent;\r
1203                         (*list)->item[i].orderIndex = info.sequence;\r
1204                         (*list)->item[i].editable = info.editable;\r
1205 \r
1206                         if (info.url != NULL && strlen(info.url) > 0)\r
1207                                 (*list)->item[i].address = strdup(info.url);\r
1208                         if (info.title != NULL && strlen(info.title) > 0)\r
1209                                 (*list)->item[i].title = strdup(info.title);\r
1210                         FAVORITES_LOGD("Title: %s", (*list)->item[i].title);\r
1211 \r
1212                         (*list)->item[i].creationdate = NULL;\r
1213                         (*list)->item[i].creationdate = (char *)calloc(128, sizeof(char));\r
1214                         strftime((*list)->item[i].creationdate,128,"%Y-%m-%d %H:%M:%S",localtime((time_t *)&(info.date_created)));\r
1215 \r
1216                         (*list)->item[i].updatedate = NULL;\r
1217                         (*list)->item[i].updatedate = (char *)calloc(128, sizeof(char));\r
1218                         strftime((*list)->item[i].updatedate,128,"%Y-%m-%d %H:%M:%S",localtime((time_t *)&(info.date_modified)));\r
1219                 }\r
1220                 bp_bookmark_adaptor_easy_free(&info);\r
1221         }\r
1222         (*list)->count = i;\r
1223         FAVORITES_LOGE("bookmark list count : %d", (*list)->count);\r
1224         free(ids);\r
1225         return FAVORITES_ERROR_NONE;\r
1226 #else\r
1227         int nError;\r
1228         sqlite3_stmt *stmt;\r
1229 \r
1230         int item_count = _get_count_by_folder(parent_id);\r
1231         if (item_count < 0) {\r
1232                 FAVORITES_LOGD("_get_count_by_folder is failed");\r
1233                 return FAVORITES_ERROR_DB_FAILED;\r
1234         }\r
1235 \r
1236         if (_favorites_open_bookmark_db() < 0) {\r
1237                 FAVORITES_LOGE("db_util_open is failed\n");\r
1238                 return FAVORITES_ERROR_DB_FAILED;\r
1239         }\r
1240         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1241                                "select id, type, parent, address, title, editable,\\r
1242                                creationdate, updatedate, sequence \\r
1243                                from bookmarks where parent=? order by sequence",\r
1244                                -1, &stmt, NULL);\r
1245         if (nError != SQLITE_OK) {\r
1246                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1247                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1248                 _favorites_finalize_bookmark_db(stmt);\r
1249                 return FAVORITES_ERROR_DB_FAILED;\r
1250         }\r
1251 \r
1252         if (sqlite3_bind_int(stmt, 1, parent_id) != SQLITE_OK) {\r
1253                 FAVORITES_LOGE("sqlite3_bind_int is failed.\n");\r
1254                 _favorites_finalize_bookmark_db(stmt);\r
1255                 return -1;\r
1256         }\r
1257 \r
1258         *list = (bookmark_list_h) calloc(1, sizeof(bookmark_list_s));\r
1259         (*list)->count = 0;\r
1260         (*list)->item =\r
1261             (bookmark_entry_internal_h) calloc(item_count, sizeof(bookmark_entry_internal_s));\r
1262         int i = 0;\r
1263         while ((nError = sqlite3_step(stmt)) == SQLITE_ROW && (i < item_count)) {\r
1264                 (*list)->item[i].id = sqlite3_column_int(stmt, 0);\r
1265                 (*list)->item[i].is_folder = sqlite3_column_int(stmt, 1);\r
1266                 (*list)->item[i].folder_id = sqlite3_column_int(stmt, 2);\r
1267 \r
1268                 (*list)->item[i].address = NULL;\r
1269                 if (!(*list)->item[i].is_folder) {\r
1270                         const char *url = (const char *)(sqlite3_column_text(stmt, 3));\r
1271                         if (url) {\r
1272                                 int length = strlen(url);\r
1273                                 if (length > 0) {\r
1274                                         (*list)->item[i].address = (char *)calloc(length + 1, sizeof(char));\r
1275                                         memcpy((*list)->item[i].address, url, length);\r
1276                                         FAVORITES_LOGE ("url:%s\n", url);\r
1277                                 }\r
1278                         }\r
1279                 }\r
1280 \r
1281                 const char *title = (const char *)(sqlite3_column_text(stmt, 4));\r
1282                 (*list)->item[i].title = NULL;\r
1283                 if (title) {\r
1284                         int length = strlen(title);\r
1285                         if (length > 0) {\r
1286                                 (*list)->item[i].title = (char *)calloc(length + 1, sizeof(char));\r
1287                                 memcpy((*list)->item[i].title, title, length);\r
1288                         }\r
1289                 }\r
1290                 (*list)->item[i].editable = sqlite3_column_int(stmt, 5);\r
1291 \r
1292                 const char *creationdate = (const char *)(sqlite3_column_text(stmt, 6));\r
1293                 (*list)->item[i].creationdate = NULL;\r
1294                 if (creationdate) {\r
1295                         int length = strlen(creationdate);\r
1296                         if (length > 0) {\r
1297                                 (*list)->item[i].creationdate = (char *)calloc(length + 1, sizeof(char));\r
1298                                 memcpy((*list)->item[i].creationdate, creationdate, length);\r
1299                         }\r
1300                 }\r
1301                 const char *updatedate = (const char *)(sqlite3_column_text(stmt, 7));\r
1302                 (*list)->item[i].updatedate = NULL;\r
1303                 if (updatedate) {\r
1304                         int length = strlen(updatedate);\r
1305                         if (length > 0) {\r
1306                                 (*list)->item[i].updatedate = (char *)calloc(length + 1, sizeof(char));\r
1307                                 memcpy((*list)->item[i].updatedate, updatedate, length);\r
1308                         }\r
1309                 }\r
1310 \r
1311                 (*list)->item[i].orderIndex = sqlite3_column_int(stmt, 8);\r
1312                 i++;\r
1313         }\r
1314 \r
1315         (*list)->count = i;\r
1316         FAVORITES_LOGE("bookmark list count : %d", (*list)->count);\r
1317 \r
1318         if ((nError != SQLITE_ROW) && (nError != SQLITE_DONE)) {\r
1319                 FAVORITES_LOGE("sqlite3_step is failed(%s).\n",\r
1320                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1321                 _favorites_finalize_bookmark_db(stmt);\r
1322                 return FAVORITES_ERROR_DB_FAILED;\r
1323         }\r
1324 \r
1325         _favorites_finalize_bookmark_db(stmt);\r
1326         return FAVORITES_ERROR_NONE;\r
1327 #endif\r
1328 }\r
1329 \r
1330 int _set_full_tree_to_html_recur(int parent_id, FILE *fp, int depth)\r
1331 {\r
1332         FAVORITES_LOGD("depth: %d", depth);\r
1333         if (parent_id < 0)\r
1334                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
1335         if (fp == NULL)\r
1336                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
1337         if (depth < 0)\r
1338                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
1339 \r
1340         bookmark_list_h child_list = NULL;\r
1341 \r
1342         int ret = _get_list_by_folder(parent_id, &child_list);\r
1343 \r
1344         if (ret != FAVORITES_ERROR_NONE)\r
1345                 return FAVORITES_ERROR_DB_FAILED;\r
1346 \r
1347         int i = 0;\r
1348         int j = 0;\r
1349         FAVORITES_LOGD("list->count: %d", child_list->count);\r
1350         for (i=0; i < (child_list->count); i++) {\r
1351                 FAVORITES_LOGD("title: %s", child_list->item[i].title);\r
1352                 if (child_list->item[i].is_folder) {\r
1353                         for (j=0 ; j < depth ; j++) {\r
1354                                 fputs("\t", fp);\r
1355                         }\r
1356                         int folder_adddate_unixtime = 0;\r
1357                         folder_adddate_unixtime =\r
1358                                 _favorites_get_unixtime_from_datetime(\r
1359                                         child_list->item[i].creationdate);\r
1360                         fprintf(fp, "<DT><H3 FOLDED ADD_DATE=\"%d\">%s</H3>\n",\r
1361                                         folder_adddate_unixtime, child_list->item[i].title);\r
1362                         for (j=0 ; j < depth ; j++) {\r
1363                                 fputs("\t", fp);\r
1364                         }\r
1365                         fputs("<DL><p>\n", fp);\r
1366                         ret = _set_full_tree_to_html_recur(child_list->item[i].id, fp, depth+1);\r
1367                         if (ret != FAVORITES_ERROR_NONE) {\r
1368                                 _favorites_free_bookmark_list(child_list);\r
1369                                 return ret;\r
1370                         }\r
1371                         for (j=0 ; j < depth ; j++) {\r
1372                                 fputs("\t", fp);\r
1373                         }\r
1374                         fputs("</DL><p>\n", fp);\r
1375                 }else {\r
1376                         int bookmark_adddate_unixtime =\r
1377                                 _favorites_get_unixtime_from_datetime(\r
1378                                 child_list->item[i].creationdate);\r
1379 \r
1380                         if(bookmark_adddate_unixtime<0)\r
1381                                 bookmark_adddate_unixtime=0;\r
1382 \r
1383                         int bookmark_updatedate_unixtime =\r
1384                                 _favorites_get_unixtime_from_datetime(\r
1385                                 child_list->item[i].updatedate);\r
1386 \r
1387                         if(bookmark_updatedate_unixtime<0)\r
1388                                 bookmark_updatedate_unixtime=0;\r
1389 \r
1390                         for (j=0 ; j < depth ; j++) {\r
1391                                 fputs("\t", fp);\r
1392                         }\r
1393                         fprintf(fp,"<DT><A HREF=\"%s\" ",\r
1394                                         child_list->item[i].address);\r
1395                         fprintf(fp,"ADD_DATE=\"%d\" ",\r
1396                                         bookmark_adddate_unixtime);\r
1397                         fprintf(fp,"LAST_VISIT=\"%d\" ",\r
1398                                         bookmark_updatedate_unixtime);\r
1399                         fprintf(fp,"LAST_MODIFIED=\"%d\">",\r
1400                                         bookmark_updatedate_unixtime);\r
1401                         fprintf(fp,"%s</A>\n", child_list->item[i].title );\r
1402                 }\r
1403         }\r
1404 \r
1405         _favorites_free_bookmark_list(child_list);\r
1406         return FAVORITES_ERROR_NONE;\r
1407 }\r
1408 \r
1409 /*************************************************************\r
1410  *      APIs for Internet favorites\r
1411  *************************************************************/\r
1412 int favorites_bookmark_get_root_folder_id(int *root_id)\r
1413 {\r
1414         FAVORITES_LOGE("");\r
1415         if (!root_id)\r
1416                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
1417 \r
1418         *root_id = _get_root_folder_id();\r
1419         FAVORITES_LOGE("root_id: %d", *root_id);\r
1420         return FAVORITES_ERROR_NONE;\r
1421 }\r
1422 \r
1423 int favorites_bookmark_add_bookmark(const char *url, const char *title, const char *foldername, int *bookmark_id)\r
1424 {\r
1425         FAVORITES_LOGE("");\r
1426         int ret = 0;\r
1427         int root_id = 1;\r
1428         int folder_id = 1;\r
1429 \r
1430         if (!foldername || (strlen(foldername) <= 0)) {\r
1431                 FAVORITES_LOGE("foldername is empty. id is now root.\n");\r
1432                 folder_id = _get_root_folder_id();\r
1433         } else {\r
1434                 FAVORITES_LOGE("adding folder\n");\r
1435                 ret = favorites_bookmark_add(foldername, url, root_id, 1, &folder_id);\r
1436                 if (ret != FAVORITES_ERROR_NONE) {\r
1437                         if (ret == FAVORITES_ERROR_ITEM_ALREADY_EXIST) {\r
1438                                 folder_id = _get_folder_id(foldername, _get_root_folder_id());\r
1439                         } else\r
1440                                 return ret;\r
1441                 }\r
1442         }\r
1443         return favorites_bookmark_add(title, url, folder_id, 0, bookmark_id);\r
1444 }\r
1445 \r
1446 int favorites_bookmark_add(const char *title, const char *url, int parent_id, int type, int *saved_id)\r
1447 {\r
1448         FAVORITES_LOGD("");\r
1449         int ret = 0;\r
1450         if (type == 0) {\r
1451                 FAVORITES_LOGD("type is bookmark");\r
1452                 ret = _add_bookmark(title, url, parent_id, saved_id);\r
1453         } else {\r
1454                 FAVORITES_LOGD("type is folder");\r
1455                 ret = _add_folder(title, parent_id, saved_id);\r
1456         }\r
1457 \r
1458         return ret;\r
1459 }\r
1460 \r
1461 int favorites_bookmark_get_count(int *count)\r
1462 {\r
1463         FAVORITES_LOGD("");\r
1464 #if defined(BROWSER_BOOKMARK_SYNC)\r
1465         int ids_count = 0;\r
1466         int *ids = NULL;\r
1467         bp_bookmark_adaptor_get_full_ids_p(&ids, &ids_count);\r
1468         *count = ids_count;\r
1469         free(ids);\r
1470 \r
1471         if (*count <0) {\r
1472                 FAVORITES_LOGE("bp_bookmark_adaptor_get_full_ids_p is failed\n");\r
1473                 return FAVORITES_ERROR_DB_FAILED;\r
1474         }\r
1475         return FAVORITES_ERROR_NONE;\r
1476 #else\r
1477         int nError;\r
1478         sqlite3_stmt *stmt;\r
1479 \r
1480         FAVORITES_NULL_ARG_CHECK(count);\r
1481         \r
1482         if (_favorites_open_bookmark_db() < 0) {\r
1483                 FAVORITES_LOGE("db_util_open is failed\n");\r
1484                 return FAVORITES_ERROR_DB_FAILED;\r
1485         }\r
1486 \r
1487         /* folder + bookmark */\r
1488 #if defined(ROOT_IS_ZERO)\r
1489         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1490                                "select count(*) from bookmarks",\r
1491                                -1, &stmt, NULL);\r
1492 #else\r
1493         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1494                                "select count(*) from bookmarks where parent != 0",\r
1495                                -1, &stmt, NULL);\r
1496 #endif\r
1497         if (nError != SQLITE_OK) {\r
1498                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1499                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1500                 _favorites_finalize_bookmark_db(stmt);\r
1501                 return FAVORITES_ERROR_DB_FAILED;\r
1502         }\r
1503 \r
1504         nError = sqlite3_step(stmt);\r
1505         if (nError == SQLITE_ROW) {\r
1506                 *count = sqlite3_column_int(stmt, 0);\r
1507                 _favorites_finalize_bookmark_db(stmt);\r
1508                 return FAVORITES_ERROR_NONE;\r
1509         } else if (nError == SQLITE_DONE) {\r
1510                 _favorites_finalize_bookmark_db(stmt);\r
1511                 return FAVORITES_ERROR_NONE;\r
1512         }\r
1513         _favorites_close_bookmark_db();\r
1514         return FAVORITES_ERROR_DB_FAILED;\r
1515 #endif\r
1516 }\r
1517 \r
1518 int favorites_bookmark_foreach(favorites_bookmark_foreach_cb callback,void *user_data)\r
1519 {\r
1520         FAVORITES_NULL_ARG_CHECK(callback);\r
1521 #if defined(BROWSER_BOOKMARK_SYNC)\r
1522         int ids_count = 0;\r
1523         int *ids = NULL;\r
1524         int func_ret = 0;\r
1525         int is_folder = -1;\r
1526         int editable = -1;\r
1527 \r
1528         if (bp_bookmark_adaptor_get_full_ids_p(&ids, &ids_count) < 0) {\r
1529                 FAVORITES_LOGE ("bp_bookmark_adaptor_get_full_ids_p is failed.\n");\r
1530                 return FAVORITES_ERROR_DB_FAILED;\r
1531         }\r
1532 \r
1533         FAVORITES_LOGD("bp_bookmark_adaptor_get_full_ids_p count : %d", ids_count);\r
1534         if (ids_count > 0) {\r
1535                 int i = 0;\r
1536                 for (i = 0; i < ids_count; i++) {\r
1537                         FAVORITES_LOGD("I[%d] id : %d", i, ids[i]);\r
1538                         favorites_bookmark_entry_s result;\r
1539                         memset(&result, 0x00, sizeof(favorites_bookmark_entry_s));\r
1540                         result.id = ids[i];\r
1541                         bp_bookmark_adaptor_get_type(result.id, &is_folder);\r
1542                         if (is_folder > 0)\r
1543                                 result.is_folder = 1;\r
1544                         else\r
1545                                 result.is_folder = 0;\r
1546                         FAVORITES_LOGD("is_folder : %d", result.is_folder);\r
1547                         bp_bookmark_adaptor_get_parent_id(result.id, &(result.folder_id));\r
1548                         FAVORITES_LOGD("folder_id : %d", result.folder_id);\r
1549                         bp_bookmark_adaptor_get_url(result.id, &(result.address));\r
1550                         FAVORITES_LOGD("address : %s", result.address);\r
1551                         bp_bookmark_adaptor_get_title(result.id, &(result.title));\r
1552                         FAVORITES_LOGD("title : %s", result.title);\r
1553                         bp_bookmark_adaptor_get_is_editable(result.id, &editable);\r
1554                         if (editable > 0)\r
1555                                 result.editable = 1;\r
1556                         else\r
1557                                 result.editable = 0;\r
1558                         bp_bookmark_adaptor_get_sequence(result.id, &(result.order_index));\r
1559                         FAVORITES_LOGD("order_index : %d", result.order_index);\r
1560 \r
1561                         result.creation_date = NULL;\r
1562                         int recv_int = -1;\r
1563                         bp_bookmark_adaptor_get_date_created(result.id, &recv_int);\r
1564                         result.creation_date = (char *)calloc(128, sizeof(char));\r
1565                         strftime(result.creation_date,128,"%Y-%m-%d %H:%M:%S",localtime((time_t *)&recv_int));\r
1566                         FAVORITES_LOGD("Creationdata:%s", result.creation_date);\r
1567 \r
1568                         result.update_date = NULL;\r
1569                         bp_bookmark_adaptor_get_date_modified(result.id, &recv_int);\r
1570                         result.update_date = (char *)calloc(128, sizeof(char));\r
1571                         strftime(result.update_date,128,"%Y-%m-%d %H:%M:%S",localtime((time_t *)&recv_int));\r
1572                         FAVORITES_LOGD("update_date:%s", result.update_date);\r
1573 \r
1574                         result.visit_date = NULL;\r
1575                         bp_bookmark_adaptor_get_date_visited(result.id, &recv_int);\r
1576                         result.visit_date = (char *)calloc(128, sizeof(char));\r
1577                         strftime(result.visit_date,128,"%Y-%m-%d %H:%M:%S",localtime((time_t *)&recv_int));\r
1578                         FAVORITES_LOGD("visit_date:%s", result.visit_date);\r
1579 \r
1580                         func_ret = callback(&result, user_data);\r
1581                         _favorites_free_bookmark_entry(&result);\r
1582                         if(func_ret == 0) \r
1583                                 break;\r
1584                 }\r
1585         }\r
1586         free(ids);\r
1587         FAVORITES_LOGE ("There are no more bookmarks.\n");\r
1588         return FAVORITES_ERROR_NONE;\r
1589 #else\r
1590         int nError;\r
1591         int func_ret = 0;\r
1592         sqlite3_stmt *stmt;\r
1593 \r
1594         if (_favorites_open_bookmark_db() < 0) {\r
1595                 FAVORITES_LOGE("db_util_open is failed\n");\r
1596                 return FAVORITES_ERROR_DB_FAILED;\r
1597         }\r
1598 #if defined(ROOT_IS_ZERO)\r
1599         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1600                                "select id, type, parent, address, title, editable,\\r
1601                                creationdate, updatedate, sequence \\r
1602                                from bookmarks order by sequence",\r
1603                                -1, &stmt, NULL);\r
1604 #else\r
1605         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1606                                "select id, type, parent, address, title, editable,\\r
1607                                creationdate, updatedate, sequence \\r
1608                                from bookmarks where parent != 0 order by sequence",\r
1609                                -1, &stmt, NULL);\r
1610 #endif\r
1611         if (nError != SQLITE_OK) {\r
1612                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1613                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1614                 _favorites_finalize_bookmark_db(stmt);\r
1615                 return FAVORITES_ERROR_DB_FAILED;\r
1616         }\r
1617 \r
1618         while ((nError = sqlite3_step(stmt)) == SQLITE_ROW) {\r
1619                 favorites_bookmark_entry_s result;\r
1620                 memset(&result, 0x00, sizeof(favorites_bookmark_entry_s));\r
1621                 result.id = sqlite3_column_int(stmt, 0);\r
1622                 result.is_folder = sqlite3_column_int(stmt, 1);\r
1623                 result.folder_id = sqlite3_column_int(stmt, 2);\r
1624 \r
1625                 result.address = NULL;\r
1626                 if (!result.is_folder) {\r
1627                         const char *url = (const char *)(sqlite3_column_text(stmt, 3));\r
1628                         if (url) {\r
1629                                 int length = strlen(url);\r
1630                                 if (length > 0) {\r
1631                                         result.address = (char *)calloc(length + 1, sizeof(char));\r
1632                                         memcpy(result.address, url, length);\r
1633                                         FAVORITES_LOGE ("url:%s\n", url);\r
1634                                 }\r
1635                         }\r
1636                 }\r
1637 \r
1638                 const char *title = (const char *)(sqlite3_column_text(stmt, 4));\r
1639                 result.title = NULL;\r
1640                 if (title) {\r
1641                         int length = strlen(title);\r
1642                         if (length > 0) {\r
1643                                 result.title = (char *)calloc(length + 1, sizeof(char));\r
1644                                 memcpy(result.title, title, length);\r
1645                                 FAVORITES_LOGE ("title:%s\n", title);\r
1646                         }\r
1647                 }\r
1648                 result.editable = sqlite3_column_int(stmt, 5);\r
1649 \r
1650                 const char *creation_date = (const char *)(sqlite3_column_text(stmt, 6));\r
1651                 result.creation_date = NULL;\r
1652                 if (creation_date) {\r
1653                         int length = strlen(creation_date);\r
1654                         if (length > 0) {\r
1655                                 result.creation_date = (char *)calloc(length + 1, sizeof(char));\r
1656                                 memcpy(result.creation_date, creation_date, length);\r
1657                         }\r
1658                 }\r
1659                 const char *update_date = (const char *)(sqlite3_column_text(stmt, 7));\r
1660                 result.update_date = NULL;\r
1661                 if (update_date) {\r
1662                         int length = strlen(update_date);\r
1663                         if (length > 0) {\r
1664                                 result.update_date = (char *)calloc(length + 1, sizeof(char));\r
1665                                 memcpy(result.update_date, update_date, length);\r
1666                         }\r
1667                 }\r
1668 \r
1669                 result.order_index = sqlite3_column_int(stmt, 8);\r
1670 \r
1671                 func_ret = callback(&result, user_data);\r
1672                 _favorites_free_bookmark_entry(&result);\r
1673                 if(func_ret == 0) \r
1674                         break;\r
1675         }\r
1676 \r
1677         FAVORITES_LOGE ("There are no more bookmarks.\n");\r
1678         _favorites_finalize_bookmark_db(stmt);\r
1679         return FAVORITES_ERROR_NONE;\r
1680 #endif\r
1681 }\r
1682 \r
1683 int favorites_bookmark_export_list(const char *file_path)\r
1684 {\r
1685         FAVORITES_NULL_ARG_CHECK(file_path);\r
1686 \r
1687         int root_id = -1;\r
1688         favorites_bookmark_get_root_folder_id(&root_id);\r
1689 \r
1690         FILE *fp = NULL;\r
1691         FAVORITES_LOGE("file_path: %s", file_path);\r
1692         fp = fopen(file_path, "w");\r
1693         if (fp == NULL) {\r
1694                 FAVORITES_LOGE("file opening is failed.");\r
1695                 return FAVORITES_ERROR_INVALID_PARAMETER;\r
1696         }\r
1697         fputs("<!DOCTYPE NETSCAPE-Bookmark-file-1>\n", fp);\r
1698         fputs("<!-- This is an automatically generated file.\n", fp);\r
1699         fputs("It will be read and overwritten.\n", fp);\r
1700         fputs("Do Not Edit! -->\n", fp);\r
1701         fputs("<META HTTP-EQUIV=\"Content-Type\" ", fp);\r
1702         fputs("CONTENT=\"text/html; charset=UTF-8\">\n", fp);\r
1703         fputs("<TITLE>Bookmarks</TITLE>\n", fp);\r
1704         fputs("<H1>Bookmarks</H1>\n", fp);\r
1705         fputs("<DL><p>\n", fp);\r
1706 \r
1707         int ret = -1;\r
1708         ret = _set_full_tree_to_html_recur(root_id, fp, 1);\r
1709         if (ret != FAVORITES_ERROR_NONE) {\r
1710                 fclose(fp);\r
1711                 return FAVORITES_ERROR_DB_FAILED;\r
1712         }\r
1713 \r
1714         fputs("</DL><p>\n", fp);\r
1715         fclose(fp);\r
1716         return FAVORITES_ERROR_NONE;\r
1717 }\r
1718 \r
1719 int favorites_bookmark_get_favicon(int id, Evas *evas, Evas_Object **icon)\r
1720 {\r
1721         FAVORITES_LOGD("");\r
1722         FAVORITES_INVALID_ARG_CHECK(id<0);\r
1723         FAVORITES_NULL_ARG_CHECK(evas);\r
1724         FAVORITES_NULL_ARG_CHECK(icon);\r
1725 #if defined(BROWSER_BOOKMARK_SYNC)\r
1726         void *favicon_data_temp=NULL;\r
1727         favicon_entry_h favicon;\r
1728         favicon = (favicon_entry_h) calloc(1, sizeof(favicon_entry_s));\r
1729         int ret = bp_bookmark_adaptor_get_favicon(id, (unsigned char **)&favicon_data_temp, &(favicon->length));\r
1730         if (ret == 0) {\r
1731                 if (bp_bookmark_adaptor_get_favicon_width(id, &(favicon->w)) == 0) {\r
1732                         if (bp_bookmark_adaptor_get_favicon_height(id, &(favicon->h)) == 0) {\r
1733                                 FAVORITES_LOGD("favicon is successfully loaded.");\r
1734                         } else {\r
1735                                 FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon_height is failed");\r
1736                                 *icon = NULL;\r
1737                                 return FAVORITES_ERROR_DB_FAILED;\r
1738                         }\r
1739                 } else {\r
1740                         FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon_width is failed");\r
1741                         *icon = NULL;\r
1742                         return FAVORITES_ERROR_DB_FAILED;\r
1743                 }\r
1744         } else {\r
1745                 FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon is failed");\r
1746                 *icon = NULL;\r
1747                 return FAVORITES_ERROR_DB_FAILED;\r
1748         }\r
1749         FAVORITES_LOGD("len: %d, w:%d, h:%d", favicon->length, favicon->w, favicon->h);\r
1750         if (favicon->length > 0){\r
1751                 *icon = evas_object_image_filled_add(evas);\r
1752                 evas_object_image_colorspace_set(*icon, EVAS_COLORSPACE_ARGB8888);\r
1753                 evas_object_image_size_set(*icon, favicon->w, favicon->h);\r
1754                 evas_object_image_fill_set(*icon, 0, 0, favicon->w, favicon->h);\r
1755                 evas_object_image_filled_set(*icon, EINA_TRUE);\r
1756                 evas_object_image_alpha_set(*icon,EINA_TRUE);\r
1757                 evas_object_image_data_set(*icon, favicon_data_temp);\r
1758                 return FAVORITES_ERROR_NONE;\r
1759         }\r
1760         *icon = NULL;\r
1761         FAVORITES_LOGE("favicon length is 0");\r
1762         return FAVORITES_ERROR_DB_FAILED;\r
1763 #else\r
1764         void *favicon_data_temp=NULL;\r
1765         favicon_entry_h favicon;\r
1766         sqlite3_stmt *stmt;\r
1767         char    query[1024];\r
1768         int nError;\r
1769 \r
1770         memset(&query, 0x00, sizeof(char)*1024);\r
1771         sprintf(query, "select favicon, favicon_length, favicon_w, favicon_h from bookmarks\\r
1772                         where id=%d"\r
1773                         , id);\r
1774         FAVORITES_LOGE("query: %s", query);\r
1775 \r
1776         if (_favorites_open_bookmark_db() < 0) {\r
1777                 FAVORITES_LOGE("db_util_open is failed\n");\r
1778                 return FAVORITES_ERROR_DB_FAILED;\r
1779         }\r
1780 \r
1781         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1782                         query, -1, &stmt, NULL);\r
1783         if (nError != SQLITE_OK) {\r
1784                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1785                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1786                 _favorites_finalize_bookmark_db(stmt);\r
1787                 return FAVORITES_ERROR_DB_FAILED;\r
1788         }\r
1789 \r
1790         nError = sqlite3_step(stmt);\r
1791         if (nError == SQLITE_ROW) {\r
1792                 favicon = (favicon_entry_h) calloc(1, sizeof(favicon_entry_s));\r
1793                 /* loading favicon from bookmark db */\r
1794                 favicon_data_temp = (void *)sqlite3_column_blob(stmt,0);\r
1795                 favicon->length = sqlite3_column_int(stmt,1);\r
1796                 favicon->w = sqlite3_column_int(stmt,2);\r
1797                 favicon->h = sqlite3_column_int(stmt,3);\r
1798 \r
1799                 if (favicon->length > 0){\r
1800                         favicon->data = calloc(1, favicon->length);\r
1801                         memcpy(favicon->data, favicon_data_temp, favicon->length);\r
1802                         /* transforming to evas object */\r
1803                         *icon = evas_object_image_filled_add(evas);\r
1804                         evas_object_image_colorspace_set(*icon,\r
1805                                                         EVAS_COLORSPACE_ARGB8888);\r
1806                         evas_object_image_size_set(*icon, favicon->w, favicon->h);\r
1807                         evas_object_image_fill_set(*icon, 0, 0, favicon->w,\r
1808                                                                         favicon->h);\r
1809                         evas_object_image_filled_set(*icon, EINA_TRUE);\r
1810                         evas_object_image_alpha_set(*icon,EINA_TRUE);\r
1811                         evas_object_image_data_set(*icon, favicon->data);\r
1812                 }\r
1813                 free(favicon);\r
1814                 _favorites_finalize_bookmark_db(stmt);\r
1815                 return FAVORITES_ERROR_NONE;\r
1816         }\r
1817 \r
1818         _favorites_close_bookmark_db();\r
1819         return FAVORITES_ERROR_NONE;\r
1820 #endif\r
1821 }\r
1822 \r
1823 int favorites_bookmark_set_favicon(int bookmark_id, Evas_Object *icon)\r
1824 {\r
1825         FAVORITES_LOGD("");\r
1826         FAVORITES_INVALID_ARG_CHECK(bookmark_id<0);\r
1827         FAVORITES_NULL_ARG_CHECK(icon);\r
1828 #if defined(BROWSER_BOOKMARK_SYNC)\r
1829         int icon_w = 0;\r
1830         int icon_h = 0;\r
1831         int stride = 0;\r
1832         int icon_length = 0;\r
1833         void *icon_data = (void *)evas_object_image_data_get(icon, EINA_TRUE);\r
1834         evas_object_image_size_get(icon, &icon_w, &icon_h);\r
1835         stride = evas_object_image_stride_get(icon);\r
1836         icon_length = icon_h * stride;\r
1837         FAVORITES_LOGD("favicon size  w:%d, h:%d, stride: %d", icon_w, icon_h, stride);\r
1838         int ret = bp_bookmark_adaptor_set_favicon(bookmark_id,(const unsigned char *)icon_data, icon_length);\r
1839         if (ret == 0) {\r
1840                 if (bp_bookmark_adaptor_set_favicon_width(bookmark_id, icon_w) == 0) {\r
1841                         if (bp_bookmark_adaptor_set_favicon_height(bookmark_id, icon_h) == 0) {\r
1842                                 FAVORITES_LOGE("favicon is successfully saved.");\r
1843                                 bp_bookmark_adaptor_publish_notification();\r
1844                                 return FAVORITES_ERROR_NONE;\r
1845                         } else\r
1846                                 FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon_height is failed");\r
1847                 } else\r
1848                         FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon_width is failed");\r
1849         } else\r
1850                 FAVORITES_LOGE("bp_bookmark_adaptor_set_favicon is failed");\r
1851         FAVORITES_LOGE("set favicon is failed");\r
1852         return FAVORITES_ERROR_DB_FAILED;\r
1853 #else\r
1854         int icon_w = 0;\r
1855         int icon_h = 0;\r
1856         int stride = 0;\r
1857         int icon_length = 0;\r
1858         void *icon_data = (void *)evas_object_image_data_get(icon, EINA_TRUE);\r
1859         evas_object_image_size_get(icon, &icon_w, &icon_h);\r
1860         stride = evas_object_image_stride_get(icon);\r
1861         icon_length = icon_h * stride;\r
1862         FAVORITES_LOGD("favicon size  w:%d, h:%d, stride: %d", icon_w, icon_h, stride);\r
1863         int nError;\r
1864         sqlite3_stmt *stmt;\r
1865 \r
1866         if (_favorites_open_bookmark_db() < 0) {\r
1867                 FAVORITES_LOGE("db_util_open is failed\n");\r
1868                 return FAVORITES_ERROR_DB_FAILED;\r
1869         }\r
1870 \r
1871         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1872                                 "UPDATE bookmarks SET favicon=?,\\r
1873                                 favicon_length=?, favicon_w=?, favicon_h=? \\r
1874                                 WHERE id=?", -1, &stmt, NULL);\r
1875 \r
1876         if (nError != SQLITE_OK) {\r
1877                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1878                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1879                 _favorites_finalize_bookmark_db(stmt);\r
1880                 return FAVORITES_ERROR_DB_FAILED;\r
1881         }\r
1882 \r
1883         // bind\r
1884         if (sqlite3_bind_blob(stmt, 1, icon_data , icon_length, NULL) != SQLITE_OK) {\r
1885                 FAVORITES_LOGE("sqlite3_bind_blob(icon_data) is failed");\r
1886                 _favorites_finalize_bookmark_db(stmt);\r
1887                 return FAVORITES_ERROR_DB_FAILED;\r
1888         }\r
1889         if (sqlite3_bind_int(stmt, 2, icon_length) != SQLITE_OK) {\r
1890                 FAVORITES_LOGE("sqlite3_bind_int(icon_length) is failed");\r
1891                 _favorites_finalize_bookmark_db(stmt);\r
1892                 return FAVORITES_ERROR_DB_FAILED;\r
1893         }\r
1894         if (sqlite3_bind_int(stmt, 3, icon_w) != SQLITE_OK) {\r
1895                 FAVORITES_LOGE("sqlite3_bind_int(icon_w) is failed");\r
1896                 _favorites_finalize_bookmark_db(stmt);\r
1897                 return FAVORITES_ERROR_DB_FAILED;\r
1898         }\r
1899         if (sqlite3_bind_int(stmt, 4, icon_h) != SQLITE_OK) {\r
1900                 FAVORITES_LOGE("sqlite3_bind_int(icon_h) is failed");\r
1901                 _favorites_finalize_bookmark_db(stmt);\r
1902                 return FAVORITES_ERROR_DB_FAILED;\r
1903         }\r
1904         if (sqlite3_bind_int(stmt, 5, bookmark_id) != SQLITE_OK) {\r
1905                 FAVORITES_LOGE("sqlite3_bind_int(bookmark_id) is failed");\r
1906                 _favorites_finalize_bookmark_db(stmt);\r
1907                 return FAVORITES_ERROR_DB_FAILED;\r
1908         }\r
1909 \r
1910         nError = sqlite3_step(stmt);\r
1911         if (nError == SQLITE_ROW || nError == SQLITE_DONE) {\r
1912                 _favorites_finalize_bookmark_db(stmt);\r
1913                 return FAVORITES_ERROR_NONE;\r
1914         }\r
1915 \r
1916         FAVORITES_LOGE("sqlite3_step is failed");\r
1917         _favorites_close_bookmark_db();\r
1918         return FAVORITES_ERROR_DB_FAILED;\r
1919 #endif\r
1920 }\r
1921 \r
1922 int favorites_bookmark_delete_bookmark(int id)\r
1923 {\r
1924         FAVORITES_LOGE("");\r
1925         FAVORITES_INVALID_ARG_CHECK(id<0);\r
1926 #if defined(BROWSER_BOOKMARK_SYNC)\r
1927         if (bp_bookmark_adaptor_delete(id) < 0)\r
1928                 return FAVORITES_ERROR_DB_FAILED;\r
1929         else {\r
1930                 bp_bookmark_adaptor_publish_notification();\r
1931                 return FAVORITES_ERROR_NONE;\r
1932         }\r
1933 #else\r
1934         int nError;\r
1935         sqlite3_stmt *stmt;\r
1936 \r
1937         if (_favorites_open_bookmark_db() < 0) {\r
1938                 FAVORITES_LOGE("db_util_open is failed\n");\r
1939                 return FAVORITES_ERROR_DB_FAILED;\r
1940         }\r
1941 \r
1942 #if defined(ROOT_IS_ZERO)\r
1943         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1944                                 "delete from bookmarks where id=?", -1, &stmt, NULL);\r
1945 #else\r
1946         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
1947                                 "delete from bookmarks where id=? and parent != 0", -1, &stmt, NULL);\r
1948 #endif\r
1949         if (nError != SQLITE_OK) {\r
1950                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
1951                         sqlite3_errmsg(gl_internet_bookmark_db));\r
1952                 _favorites_finalize_bookmark_db(stmt);\r
1953                 return FAVORITES_ERROR_DB_FAILED;\r
1954         }\r
1955         // bind\r
1956         if (sqlite3_bind_int(stmt, 1, id) != SQLITE_OK) {\r
1957                 FAVORITES_LOGE("sqlite3_bind_int is failed");\r
1958                 _favorites_finalize_bookmark_db(stmt);\r
1959                 return FAVORITES_ERROR_DB_FAILED;\r
1960         }\r
1961         nError = sqlite3_step(stmt);\r
1962         if (nError == SQLITE_ROW || nError == SQLITE_DONE) {\r
1963                 _favorites_finalize_bookmark_db(stmt);\r
1964                 return FAVORITES_ERROR_NONE;\r
1965         }\r
1966         FAVORITES_LOGE("sqlite3_step is failed");\r
1967         _favorites_close_bookmark_db();\r
1968         return FAVORITES_ERROR_DB_FAILED;\r
1969 #endif\r
1970 }\r
1971 \r
1972 int favorites_bookmark_delete_all_bookmarks(void)\r
1973 {\r
1974         FAVORITES_LOGD("");\r
1975 #if defined(BROWSER_BOOKMARK_SYNC)\r
1976         int ids_count = 0;\r
1977         int *ids = NULL;\r
1978 \r
1979         if (bp_bookmark_adaptor_get_full_ids_p(&ids, &ids_count) < 0) {\r
1980                 FAVORITES_LOGE ("bp_bookmark_adaptor_get_full_ids_p is failed.\n");\r
1981                 return FAVORITES_ERROR_DB_FAILED;\r
1982         }\r
1983 \r
1984         FAVORITES_LOGD("bp_bookmark_adaptor_get_full_ids_p count : %d", ids_count);\r
1985         if (ids_count > 0) {\r
1986                 int i = 0;\r
1987                 for (i = 0; i < ids_count; i++) {\r
1988                         FAVORITES_LOGD("I[%d] id : %d", i, ids[i]);\r
1989                         if (bp_bookmark_adaptor_delete(ids[i]) < 0) {\r
1990                                 free(ids);\r
1991                                 return FAVORITES_ERROR_DB_FAILED;\r
1992                         } else\r
1993                                 bp_bookmark_adaptor_publish_notification();\r
1994                 }\r
1995         }\r
1996         free(ids);\r
1997         return FAVORITES_ERROR_NONE;\r
1998 #else\r
1999         int nError;\r
2000         sqlite3_stmt *stmt;\r
2001 \r
2002         if (_favorites_open_bookmark_db() < 0) {\r
2003                 FAVORITES_LOGE("db_util_open is failed\n");\r
2004                 return FAVORITES_ERROR_DB_FAILED;\r
2005         }\r
2006 \r
2007 #if defined(ROOT_IS_ZERO)\r
2008         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
2009                                 "delete from bookmarks", -1, &stmt, NULL);\r
2010 #else\r
2011         nError = sqlite3_prepare_v2(gl_internet_bookmark_db,\r
2012                                 "delete from bookmarks where parent !=0", -1, &stmt, NULL);\r
2013 #endif\r
2014         if (nError != SQLITE_OK) {\r
2015                 FAVORITES_LOGE("sqlite3_prepare_v2 is failed(%s).\n",\r
2016                         sqlite3_errmsg(gl_internet_bookmark_db));\r
2017                 _favorites_finalize_bookmark_db(stmt);\r
2018                 return FAVORITES_ERROR_DB_FAILED;\r
2019         }\r
2020         nError = sqlite3_step(stmt);\r
2021         if (nError == SQLITE_ROW || nError == SQLITE_DONE) {\r
2022                 _favorites_finalize_bookmark_db(stmt);\r
2023                 return FAVORITES_ERROR_NONE;\r
2024         }\r
2025         FAVORITES_LOGE("sqlite3_step is failed");\r
2026         _favorites_close_bookmark_db();\r
2027         return FAVORITES_ERROR_DB_FAILED;\r
2028 #endif\r
2029 }\r
2030 \r