2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "homescreen-efl.h"
26 Ecore_Timer *close_timer;
32 #define CREATE_TABLE_ITEM "CREATE TABLE IF NOT EXISTS item(\
33 id INTEGER PRIMARY KEY NOT NULL,\
44 #define SELECT_ITEM "SELECT * FROM item;"
45 #define INSERT_ITEM "INSERT OR REPLACE INTO item VALUES(?,?,?,?,?,?,?,?,?,?);"
46 #define CLEAR_TABLE "DELETE FROM item;"
47 #define DELETE_ITEM "DELETE FROM item WHERE id = ?;"
49 #define DATABASE_CLOSE_TIME 5
51 bool _db_is_success(int return_code);
54 bool _db_prepare(const char *query, sqlite3_stmt **stmt);
55 bool _db_step(sqlite3_stmt *stmt);
56 bool _db_finalize(sqlite3_stmt *stmt);
58 bool db_create_tables(void)
63 sqlite3_exec(db_info.db, CLEAR_TABLE, NULL, NULL, NULL);
65 if (sqlite3_exec(db_info.db, CREATE_TABLE_ITEM, NULL, NULL, NULL))
68 db_item_t root = {0, APP_ITEM_ROOT, "", -1, -1, 0, 0, 0, 0};
69 if (!db_update_apps(root, true))
77 bool db_get_apps(Eina_List **apps)
79 const char *content_info = NULL;
84 if (!_db_prepare(SELECT_ITEM, &stmt))
87 while (sqlite3_step(stmt) == SQLITE_ROW) {
88 db_item_t *item = (db_item_t *) malloc(sizeof(db_item_t));
89 item->id = sqlite3_column_int(stmt, 0);
90 item->type = sqlite3_column_int(stmt, 1);
91 item->appid = strdup((const char *) sqlite3_column_text(
93 item->first_id = sqlite3_column_int(stmt, 3);
94 item->next_id = sqlite3_column_int(stmt, 4);
96 item->x = sqlite3_column_int(stmt, 5);
97 item->y = sqlite3_column_int(stmt, 6);
98 item->w = sqlite3_column_int(stmt, 7);
99 item->h = sqlite3_column_int(stmt, 8);
102 content_info = (const char *)sqlite3_column_text(stmt, 9);
105 item->content_info = strdup(content_info);
107 item->content_info = NULL;
110 *apps = eina_list_append(*apps, item);
111 dlog_print(DLOG_DEBUG, LOG_TAG, "App: %s [%s]", item->appid, item->content_info );
114 if (!_db_finalize(stmt))
122 void db_free_apps(Eina_List *apps)
125 EINA_LIST_FREE(apps, db_item) {
126 free(db_item->appid);
131 bool db_update_apps(db_item_t item, bool insert)
139 if (!_db_prepare(INSERT_ITEM, &stmt)) {
140 LOGD("_db_prepare() failed");
144 sqlite3_bind_int(stmt, 1, item.id);
145 sqlite3_bind_int(stmt, 2, item.type);
146 sqlite3_bind_text(stmt, 3, item.appid, -1, SQLITE_STATIC);
147 sqlite3_bind_int(stmt, 4, item.first_id);
148 sqlite3_bind_int(stmt, 5, item.next_id);
149 sqlite3_bind_int(stmt, 6, item.x);
150 sqlite3_bind_int(stmt, 7, item.y);
151 sqlite3_bind_int(stmt, 8, item.w);
152 sqlite3_bind_int(stmt, 9, item.h);
153 sqlite3_bind_text(stmt, 10, item.content_info, -1, SQLITE_STATIC);
155 if (!_db_prepare(DELETE_ITEM, &stmt)) {
156 LOGD("_db_prepare() failed");
160 sqlite3_bind_int(stmt, 1, item.id);
167 if (!_db_finalize(stmt))
175 bool _db_is_success(int return_code)
177 switch (return_code) {
179 LOGD("RETURN CODE: SQLITE_OK");
182 LOGD("RETURN CODE: SQLITE_ERROR");
184 case SQLITE_INTERNAL:
185 LOGD("RETURN CODE: SQLITE_INTERNAL");
188 LOGD("RETURN CODE: SQLITE_PERM");
191 LOGD("RETURN CODE: SQLITE_ABORT");
194 LOGD("RETURN CODE: SQLITE_BUSY");
197 LOGD("RETURN CODE: SQLITE_LOCKED");
200 LOGD("RETURN CODE: SQLITE_NOMEM");
202 case SQLITE_READONLY:
203 LOGD("RETURN CODE: SQLITE_READONLY");
205 case SQLITE_INTERRUPT:
206 LOGD("RETURN CODE: SQLITE_INTERRUPT");
209 LOGD("RETURN CODE: SQLITE_IOERR");
212 LOGD("RETURN CODE: SQLITE_CORRUPT");
214 case SQLITE_NOTFOUND:
215 LOGD("RETURN CODE: SQLITE_NOTFOUND");
218 LOGD("RETURN CODE: SQLITE_FULL");
220 case SQLITE_CANTOPEN:
221 LOGD("RETURN CODE: SQLITE_CANTOPEN");
223 case SQLITE_PROTOCOL:
224 LOGD("RETURN CODE: SQLITE_PROTOCOL");
227 LOGD("RETURN CODE: SQLITE_EMPTY");
230 LOGD("RETURN CODE: SQLITE_SCHEMA");
233 LOGD("RETURN CODE: SQLITE_TOOBIG");
235 case SQLITE_CONSTRAINT:
236 LOGD("RETURN CODE: SQLITE_CONSTRAINT");
238 case SQLITE_MISMATCH:
239 LOGD("RETURN CODE: SQLITE_MISMATCH");
242 LOGD("RETURN CODE: SQLITE_MISUSE");
245 LOGD("RETURN CODE: SQLITE_NOLFS");
248 LOGD("RETURN CODE: SQLITE_AUTH");
251 LOGD("RETURN CODE: SQLITE_FORMAT");
254 LOGD("RETURN CODE: SQLITE_RANGE");
257 LOGD("RETURN CODE: SQLITE_NOTADB");
260 LOGD("RETURN CODE: SQLITE_ROW");
263 LOGD("RETURN CODE: SQLITE_DONE");
269 return return_code == SQLITE_OK || return_code == SQLITE_DONE;
277 if (!_db_is_success(sqlite3_open(DATABASE_FILE, &db_info.db)))
280 if (sqlite3_exec(db_info.db, "BEGIN IMMEDIATE TRANSACTION", NULL,
287 Eina_Bool _db_close_cb(void *data)
292 if (sqlite3_exec(db_info.db, "COMMIT TRANSACTION",
294 return ECORE_CALLBACK_DONE;
296 if (_db_is_success(sqlite3_close(db_info.db)))
299 db_info.close_timer = NULL;
300 return ECORE_CALLBACK_DONE;
308 if (db_info.close_timer) {
309 ecore_timer_reset(db_info.close_timer);
311 db_info.close_timer = ecore_timer_add(DATABASE_CLOSE_TIME,
316 bool _db_prepare(const char *query, sqlite3_stmt **stmt)
318 return _db_is_success(sqlite3_prepare_v2(db_info.db, query,
319 strlen(query), stmt, NULL));
322 bool _db_step(sqlite3_stmt *stmt)
324 return _db_is_success(sqlite3_step(stmt));
327 bool _db_finalize(sqlite3_stmt *stmt)
329 return _db_is_success(sqlite3_finalize(stmt));