src/bookmarks/sql3/src/engine_bookmark.c

00001 /******************************************************************************
00002 This file is part of mdictionary.
00003 
00004 mdictionary is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 mdictionary is distributed in the hope that it will be useful, 
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License 
00015 along with mdictionary; if not, write to the Free Software
00016 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00017 
00018 Copyright 2006 ComArch S.A.
00019 ******************************************************************************/
00020 
00021 // header with data structure and function definition for XDXF engine.
00022 // Also Engine API. 
00023 #include <engine_bookmark.h>
00024 //------------------------------------------------------------------------------
00025 
00026 #ifndef NOLOGS
00027 #include <glib/gstdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #endif
00031 
00032 #define LOGS g_debug
00033 
00034 /*inline void LOGS(gchar* frm, ...) {
00035 #ifndef NOLOGS
00036         //g_printf(frm);
00037 #endif
00038 }*/
00039 
00040 //==============================================================================
00041 //==============================================================================
00042 //==============================================================================
00043 //-------------------------------------- FUNCTION FROM API 0.2 !!!
00044 //==============================================================================
00045 int get_id(void* data,int n,char** argv,char** names) {
00046         int len = strlen(&(argv[0][0]));
00047         memcpy(data,&(argv[0][0]), len);
00048         gchar* tmp = (gchar*)data;
00049         tmp[len] = 0;
00050         return 0;
00051 }
00052 
00053 gboolean bm_engine_add_word(Engine* engine,
00054                          gchar*  word,
00055                          gchar*  translation) {
00056         gint sql_res = 0;
00057         gchar* err = NULL;
00058         gboolean result = TRUE;
00059         gchar* sql = NULL;
00060 
00061         LOGS("Bookmark/%s->%s() called. Param\nEngine at address: %p\n"
00062              "word: %s\ntranslation address: %p\n",
00063              (gchar*)__FILE__,
00064              (gchar*)__FUNCTION__,
00065              engine,
00066              word,
00067              translation
00068             );
00069         g_assert(engine != NULL);
00070         g_assert(word != NULL);
00071         g_assert(translation != NULL);
00072 
00073         // start timer for this function
00074         timer(TIMER_START, (gchar*)(gchar*)__FUNCTION__);
00075 
00076         BookData* data = (BookData*)(engine->engine_data);
00077         sql = sqlite3_mprintf(
00078                 "INSERT INTO translations VALUES(NULL, '%q', '%q');",
00079                 word, translation);
00080         sql_res = sqlite3_exec(data->db, sql, NULL, NULL, &err);
00081         if(err || sql_res!=0) {
00082                 LOGS("Error while adding translation for word %s \nreason:%s\n\n", word, err);
00083                 result = FALSE;
00084         } 
00085         sqlite3_free(sql); sql = NULL;
00086 
00087         timer(TIMER_STOP, (gchar*)(gchar*)__FUNCTION__);
00088         return result;
00089 }
00090 //------------------------------------------------------------------------------
00091 gboolean bm_engine_remove_word(Engine* engine, gchar*  word) {
00092         gint sql_res = 0;
00093         gchar* err = NULL;
00094         gboolean result = TRUE;
00095 
00096         LOGS("Bookmark/%s->%s() called. Param\nEngine at address: %p\n"
00097              "word: %s\n",(gchar*)__FILE__,(gchar*)__FUNCTION__,engine,word);
00098         g_assert(engine != NULL);
00099         g_assert(word != NULL);
00100         timer(TIMER_START, (gchar*)(gchar*)__FUNCTION__);
00101 
00102         BookData* data = (BookData*)(engine->engine_data);
00103         gchar* sql = sqlite3_mprintf(
00104                 "DELETE FROM translations WHERE word LIKE '%q';", 
00105                                 word);
00106         sql_res = sqlite3_exec(data->db, sql, NULL, NULL, &err);
00107         if(err || sql_res!=0) {
00108                 LOGS("Error while deleting \'%s\' <-> reason:\n%s\n",word,err);
00109                 sqlite3_free(sql); sql = NULL;
00110                 return FALSE;
00111         }
00112         sqlite3_free(sql); sql = NULL;
00113 
00114         timer(TIMER_STOP, (gchar*)(gchar*)__FUNCTION__);
00115         return result;
00116 }
00117 //------------------------------------------------------------------------------      
00118 gchar* bm_engine_get_lang_from(Engine* engine) {
00119         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00120         gchar* result = g_strdup("any");
00121         LOGS("Bookmark/%s->%s() return string=%s\n",
00122                 (gchar*)__FILE__,
00123                 (gchar*)__FUNCTION__,
00124                 result
00125                );
00126         return result;
00127 }
00128 //------------------------------------------------------------------------------
00129 gchar* bm_engine_get_lang_to(Engine* engine) {
00130         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00131         gchar* result = g_strdup("any");
00132         LOGS("Bookmark/%s->%s() return string=%s\n",
00133                 (gchar*)__FILE__,
00134                 (gchar*)__FUNCTION__,
00135                 result
00136                );
00137         return result;
00138 }
00139 //------------------------------------------------------------------------------
00140 gchar* bm_engine_get_title(Engine* engine) {
00141         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00142         gchar* result = g_strconcat(g_get_user_name(),"s' bookmarks",NULL);
00143         LOGS("Bookmark/%s->%s() return string=%s\n",
00144                 (gchar*)__FILE__,
00145                 (gchar*)__FUNCTION__,
00146                 result
00147                );
00148         return result;
00149 }
00150 //------------------------------------------------------------------------------
00151 gchar* bm_engine_get_icon_path(Engine* engine) {
00152         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00153         gchar* result = g_strdup("/usr/share/pixmaps/ws_eng_bookmark_icon.png");
00154         LOGS("Bookmark/%s->%s() return string=%s\n",
00155                 (gchar*)__FILE__,
00156                 (gchar*)__FUNCTION__,
00157                 result
00158                );
00159         return result;
00160 }
00161 
00162 //==============================================================================
00163 //==============================================================================
00164 //==============================================================================
00165 //-------------------------------------- FUNCTION TO WRITE (NOT IMPLEMENTED YET)
00166 //==============================================================================
00167 
00168 //------------------------------------------------------------------------------
00169 // searching word by concrete engine
00170 void bm_engine_search_word_translation(Engine* engine, gchar* word)
00171 {
00172         LOGS("Bookmark/%s->%s() called.\n-->PARAM:engine at adress=%p\n"
00173              "-->PARAM:word=\'%s\'\n",
00174              (gchar*)__FILE__,
00175              (gchar*)__FUNCTION__,
00176              engine,
00177              word
00178             );
00179         g_assert(engine != NULL);
00180         g_assert(word != NULL);
00181         // start timer for this function
00182         timer(TIMER_START, (gchar*)(gchar*)__FUNCTION__);
00183         BookData* data = (BookData*)(engine->engine_data);
00184         gchar* tmp = g_utf8_strdown(word,-1);
00185         //sqlite3_vm* query;
00186         gchar* sql = NULL;
00187         gchar* tran = NULL;
00188         sqlite3_stmt *query;//ppStmt
00189         const char *pzTail;
00190 /*
00191         sql = g_strconcat("SELECT * FROM translations WHERE "
00192                           "word_id=(SELECT id FROM words WHERE word=\'",
00193                           tmp,
00194                           "\');",
00195                           NULL);
00196 */
00197 
00198         /*sql = g_strconcat("SELECT * FROM translations WHERE word LIKE \'",
00199                           tmp,
00200                           "\';",
00201                           NULL);*/
00202         
00203         sql = sqlite3_mprintf(
00204                         "SELECT * FROM translations WHERE word LIKE '%q';",
00205                         tmp);   
00206         
00207         g_debug("Query text: %s", sql);
00208         gint sql_res = 0;
00209        // const gchar* end;
00210        //gchar* err = NULL;
00211         sql_res = sqlite3_prepare(
00212                         data->db,/* Database handle */
00213                         (const gchar *)sql,/* SQL statement, UTF-8 encoded */
00214                         -1,/* Length of zSql in bytes. */
00215                         &query,/* OUT: Statement handle */
00216                         &pzTail/* OUT: Pointer to unused portion of zSql */
00217                         );
00218         if (sql_res != SQLITE_OK)
00219         {
00220                  LOGS("Error while compiling query:\n%s\nreason: %d\n",sql, sql_res);
00221                  sqlite3_free(sql); sql = NULL;
00222                  data->cb_search_word_trans(NULL, NULL, 
00223                                         data->cb_search_word_trans_data,
00224                                         ENGINE_NO_ERROR);
00225                  return;
00226         }
00227         gint status = 0;
00228         gboolean first = TRUE;
00229         do { 
00230                 first = TRUE;
00231                 do {
00232                         if(!first) sleep(1);
00233                         first = FALSE;
00234                         status = sqlite3_step(query);
00235                         if(status == SQLITE_ROW) {
00236                                 if (tran != NULL){
00237                                         tran = g_strconcat(tran,"<BR><BR>", 
00238                                                 sqlite3_column_text(query, 2),
00239                                                 NULL);  
00240                                 }else{
00241                                         tran = g_strdup_printf("%s <BR><BR>",
00242                                                 sqlite3_column_text(query, 2));
00243                                 }
00244                                 LOGS("Translation found :\n\"%s\"\n",tran);
00245                                 
00246                         }
00247                 } while((status == SQLITE_BUSY) && (status != SQLITE_ERROR));
00248         } while((status == SQLITE_ROW) && (status != SQLITE_ERROR));
00249         sqlite3_finalize(query);
00250 
00251         
00252         timer(TIMER_STOP,(gchar*)(gchar*)__FUNCTION__);
00253         timer(TIMER_START,"callback for returning word's translation START");
00254         // calling callback for word translation
00255 
00256         data->cb_search_word_trans(tran, word, data->cb_search_word_trans_data,
00257                  ENGINE_NO_ERROR);
00258         sqlite3_free(sql); sql = NULL;
00259         timer(TIMER_STOP,"callback for returning word's translation END");
00260         if(data->auto_free) {
00261                 LOGS("Bookmark/%s->%s() deleting all dynamic data because "
00262                      "AUTO_FREE=TRUE\n",
00263                      (gchar*)__FILE__,
00264                      (gchar*)__FUNCTION__
00265                     );
00266                 g_free(tran);
00267         }
00268         tran = NULL;
00269 }
00270 
00271 //------------------------------------------------------------------------------
00272 void bm_engine_close(Engine* engine)
00273 {
00274         LOGS("Bookmark/%s->%s() called.\n-->PARAM: engine adress=%p\n",
00275                 (gchar*)__FILE__,
00276                 (gchar*)__FUNCTION__,
00277                 engine);
00278         g_assert(engine != NULL);
00279         
00280         BookData* data = (BookData*)(engine->engine_data);
00281         sqlite3_close(data->db);
00282         
00283         LOGS("Bookmark/%s->%s() engine at adress=%p is deleted.\n",
00284                 (gchar*)__FILE__,
00285                 (gchar*)__FUNCTION__,
00286                 engine);
00287         g_free(engine);
00288         engine = NULL;
00289 }
00290 //------------------------------------------------------------------------------
00291 
00292 Engine* bm_engine_create(gchar* location,
00293                       EngineOptimizationFlag auto_cache,
00294                       cb_progress progress_handler,
00295                       gpointer progress_data,
00296                       gdouble seed)
00297 {
00298         LOGS("Bookmark/%s->%s() called.\n"
00299              "-->PARAM:location=\'%s\'\n"
00300              "-->PARAM:auto_cache=%d\n",
00301              (gchar*)__FILE__,
00302              (gchar*)__FUNCTION__,
00303              location,
00304              (guint)auto_cache
00305             );
00306         timer(TIMER_START,(gchar*)(gchar*)__FUNCTION__);        
00307 
00308         gchar* tmp = g_strdup(location);
00309         string_to_path(&tmp);
00310 
00311         Engine* result = (Engine*)g_try_malloc(sizeof(Engine));
00312         result->engine_location = bm_engine_location;
00313         result->engine_is_optimized = bm_engine_is_optimized;
00314         result->engine_optimize = bm_engine_optimize;
00315         result->engine_search_word_list = bm_engine_search_word_list;
00316         result->engine_search_word_translation = 
00317                         bm_engine_search_word_translation;        
00318         result->engine_close = bm_engine_close;
00319         result->engine_status = bm_engine_status;
00320         result->engine_error_message = bm_engine_status_message;
00321         result->engine_set_callback = bm_engine_set_callback;
00322         result->engine_set_progress_seed = bm_engine_set_progress_seed;
00323         result->engine_set_auto_free = bm_engine_set_auto_free;
00324         // 0.2 API:
00325         result->engine_add_word = bm_engine_add_word;
00326         result->engine_remove_word = bm_engine_remove_word;
00327         result->engine_get_lang_from = bm_engine_get_lang_from;
00328         result->engine_get_lang_to = bm_engine_get_lang_to;
00329         result->engine_get_title = bm_engine_get_title;
00330         result->engine_get_icon_path = bm_engine_get_icon_path;
00331 
00332 
00333         BookData* data = (BookData*)g_try_malloc(sizeof(BookData));
00334         result->engine_data = (gpointer)data;
00335 
00336         LOGS("Bookmark/%s->%s() opening file...\'%s\'.\n",
00337              (gchar*)__FILE__,
00338              (gchar*)__FUNCTION__,
00339              location
00340             );
00341         gchar* tmp2 = g_strconcat(tmp,"/ws_bookmarks3",NULL);
00342         //gchar* err;
00343         gint res = sqlite3_open(tmp2,&data->db);
00344         g_free(tmp2); tmp2 = NULL;
00345         if(res != SQLITE_OK) {
00346                 LOGS("Bookmark/%s->%s() opening bookmark file failed.%s\n",
00347                      (gchar*)__FILE__,
00348                      (gchar*)__FUNCTION__,
00349                      sqlite3_errmsg(data->db)
00350                     );
00351                 //g_free(err);
00352                 g_free(data);
00353                 g_free(result);
00354                 result = NULL;
00355         }
00356         else {
00357                 LOGS("Bookmark/%s->%s()opening dictionary file successed.\n",
00358                         (gchar*)__FILE__,
00359                         (gchar*)__FUNCTION__
00360                        );
00361                 data->dict_path = g_strdup(tmp);
00362                 data->cb_progress_caching = progress_handler;
00363                 data->cb_progress_caching_data = progress_data;        
00364                 data->cb_progress_caching_seed = seed;        
00365                 data->cb_progress_word_list = NULL;
00366                 data->cb_progress_word_list_data = NULL;
00367                 data->cb_progress_word_list_seed = 0.01;
00368                 data->cb_progress_word_trans = NULL;
00369                 data->cb_progress_word_trans_data = NULL;
00370                 data->cb_progress_word_trans_seed = 0.01;
00371 
00372                 data->cb_search_word_list = NULL;
00373                 data->cb_search_word_list_data = NULL;
00374 
00375                 data->cb_search_word_trans = NULL;
00376                 data->cb_search_word_trans_data = NULL;
00377 
00378                 data->auto_free = FALSE;
00379                 // there is no cache mechanizm in bookmarks
00380         }
00381         g_free(tmp); tmp = NULL;
00382         
00383         timer(TIMER_STOP,(gchar*)(gchar*)__FUNCTION__);
00384         LOGS("Bookmark/%s->%s() returned Engine at adress=%p\n",
00385              (gchar*)__FILE__,
00386              (gchar*)__FUNCTION__,
00387              result
00388             );
00389         return result;
00390 }
00391 //------------------------------------------------------------------------------
00392 
00393 
00394 
00395 static gboolean is_Bookmark_file(gchar* file) {
00396         LOGS("Bookmark/%s->%s() called.\n\
00397                  -->PARAM:file=\'%s\'\n",
00398                  (gchar*)__FILE__,
00399                  (gchar*)__FUNCTION__,
00400                  file
00401                );
00402         gchar* err = NULL;
00403         sqlite3* tmp = NULL;
00404         gint result = sqlite3_open(file, &tmp);
00405         if(result != SQLITE_OK) {
00406                 LOGS("Wrong file! Not a sqlite database.\n");
00407                 //g_free(err);
00408                 sqlite3_close(tmp);
00409                 return FALSE;
00410         }
00411 
00412         
00413         gchar sql[] = "SELECT COUNT(word) FROM translations WHERE word=\'.\';";  
00414         result = sqlite3_exec(tmp, sql, NULL, NULL, &err);
00415         if(err || result!=0) {
00416                 LOGS("Wrong database! Not a bookmark.\n");
00417                 g_free(err);
00418                 sqlite3_close(tmp);
00419                 return FALSE;
00420         }
00421         sqlite3_close(tmp);
00422         return TRUE;
00423 }
00424 //------------------------------------------------------------------------------
00425 
00426 
00427 void bm_engine_optimize(Engine* engine)
00428 {
00429         LOGS("Bookmark/%s->%s() called for engine at adress=%p\n",
00430              (gchar*)__FILE__,
00431              (gchar*)__FUNCTION__,
00432              engine
00433             );
00434         LOGS("Unsupported optimization mechanizm for this engine!\n");
00435         LOGS("Bookmark/%s->%s()'s work finished.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00436 }
00437 //------------------------------------------------------------------------------
00438 gboolean bm_engine_check(gchar* location) 
00439 {        
00440         LOGS("Bookmark/%s->%s() called.\n-->PARAM:location=\'%s\'\n",
00441              (gchar*)__FILE__,
00442              (gchar*)__FUNCTION__,
00443              location
00444             );
00445         timer(TIMER_START,(gchar*)(gchar*)__FUNCTION__);
00446         gboolean result = TRUE;        
00447         gchar* filepath = g_strdup(location);
00448         gchar* tmp = NULL;
00449         
00450         string_to_path(&filepath);
00451         if (filepath == NULL) {
00452                 result = FALSE;
00453                 LOGS("Bookmark/%s->%s() location \'%s\' is not a proper "
00454                      "path!\n",
00455                      (gchar*)__FILE__,
00456                      (gchar*)__FUNCTION__,
00457                      location
00458                     );
00459         }
00460         else {
00461                 tmp = g_strconcat(filepath,"/ws_bookmarks3",NULL);
00462                 g_free(filepath);
00463                 filepath = tmp;
00464                 tmp = NULL;
00465 
00466                 LOGS("Bookmark/%s->%s() finnal file to check is: %s\n",
00467                      (gchar*)__FILE__,
00468                      (gchar*)__FUNCTION__,
00469                      filepath
00470                     );
00471                 if (!g_file_test(filepath, G_FILE_TEST_IS_REGULAR)) {
00472                         LOGS("Bookmark/%s->%s() file \'%s\' does not exists!\n",
00473                              (gchar*)__FILE__,
00474                              (gchar*)__FUNCTION__,
00475                              filepath
00476                             );
00477                         result = FALSE;
00478                 };
00479         };
00480         if (result != FALSE) {
00481                 result = is_Bookmark_file(filepath);
00482         };
00483 
00484         g_free(filepath);
00485         timer(TIMER_STOP,(gchar*)(gchar*)__FUNCTION__);
00486         LOGS("Bookmark/%s->%s() returned bool statement=%s.\n",
00487              (gchar*)__FILE__,
00488              (gchar*)__FUNCTION__,
00489              PRINT_STATE(result)
00490             );
00491         return result;
00492 }
00493 
00494 //------------------------------------------------------------------------------
00495 gboolean bm_engine_is_optimized(Engine* engine) 
00496 {
00497         LOGS("Bookmark/%s->%s() called.\n-->PARAM: engine adress=%p\n",
00498              (gchar*)__FILE__,
00499              (gchar*)__FUNCTION__,
00500              engine
00501             );
00502         g_assert(engine != NULL);                
00503         gboolean result = FALSE;
00504         LOGS("Bookmark/%s->%s() returned bool statement=%s.\n",
00505              (gchar*)__FILE__,
00506              (gchar*)__FUNCTION__,
00507              PRINT_STATE(result)
00508             );
00509         return result;
00510 }
00511 //------------------------------------------------------------------------------
00512 
00513 void bm_engine_search_word_list(Engine* engine, gchar* pattern)
00514 {
00515         LOGS("Bookmark/%s->%s() called. Searching words list\n"
00516              "-->PARAM:engine at adress=%p\n"
00517              "-->PARAM:pattern=\"%s\"\n",
00518              (gchar*)__FILE__,
00519              (gchar*)__FUNCTION__,
00520              engine,
00521              pattern
00522             );
00523         g_assert(engine != NULL);
00524         g_assert(pattern != NULL);
00525 
00526         timer(TIMER_START,(gchar*)(gchar*)__FUNCTION__);
00527         BookData* data = (BookData*)(engine->engine_data);
00528         if(data->cb_search_word_list == NULL) {
00529                 LOGS("Bookmark/%s->%s() callback for Word List not set. "
00530                      "Searching aborted.\n",
00531                      (gchar*)__FILE__,
00532                      (gchar*)__FUNCTION__
00533                     );
00534                 timer(TIMER_STOP,(gchar*)(gchar*)__FUNCTION__);
00535                 return;
00536         };
00537  
00538 
00539         gchar* tmp = g_utf8_strdown(pattern,-1);
00540         sqlite3_stmt *query;//ppStmt
00541         const char *pzTail;
00542         gchar* sql = NULL;
00543         gchar* tran = NULL;
00544         g_strstrip(tmp);
00545         if ((int)(tmp[0]) == 42 && tmp[1] == '\0') //asterix?
00546         {
00547                 sql = sqlite3_mprintf("SELECT DISTINCT word FROM translations;");
00548         }
00549         else
00550         {
00551                 sql = sqlite3_mprintf(
00552                 "SELECT DISTINCT word FROM translations WHERE word LIKE'%q%%';", tmp);
00553         }
00554         /*sql = g_strconcat("SELECT word FROM translations WHERE "
00555                           "word LIKE \'",tmp,"\%\';",NULL);*/
00556         LOGS("QUERY: %s\n",sql);
00557         gint sql_res = 0;
00558         sql_res = sqlite3_prepare(
00559                         data->db,/* Database handle */
00560                         (const gchar *)sql,/* SQL statement, UTF-8 encoded */
00561                         -1,/* Length of zSql in bytes. */
00562                         &query,/* OUT: Statement handle */
00563                         &pzTail/* OUT: Pointer to unused portion of zSql */
00564                         );
00565         if (sql_res != SQLITE_OK)
00566         {
00567                 LOGS("Error while compiling query:\n%s\nreason:%d\n",sql,
00568                                                                 sql_res);
00569                 sqlite3_free(sql); sql = NULL;
00570                 return;
00571         }
00572         gint status = 0;
00573         gboolean first = TRUE;
00574         GArray* result = g_array_new(TRUE, TRUE, sizeof(gchar*) );
00575         do { 
00576                 first = TRUE;
00577                 do {
00578                         if(!first) sleep(1);
00579                         first = FALSE;;
00580                         /*status = sqlite3_step(
00581                                         query, 
00582                                         &nCol,            
00583                                         &values,  
00584                                         &head 
00585                                         );*/
00586                         status = sqlite3_step(query);
00587                         if(status == SQLITE_ERROR) {
00588                                 LOGS("Error while making next step\n");
00589                                 sqlite3_free(sql); sql = NULL;
00590                                 data->cb_search_word_list(NULL , NULL, 
00591                                 data->cb_search_word_list_data, ENGINE_NO_ERROR);
00592                                 return;
00593                         };
00594                         /*if(status == SQLITE_ROW) {
00595                                 LOGS("Fount new word: \"%s\"\n",&(values[0][0]));
00596                                 gchar* tmp = g_strdup((gchar*)&(values[0][0]));
00597                                 g_array_append_val(result, tmp );        
00598                         }*/
00599                         if(status == SQLITE_ROW) {
00600                                 gchar* tmp = g_strdup(
00601                                                 sqlite3_column_text(query, 0));
00602                                 LOGS("Found new word: \"%s\"\n", tmp);
00603                                 g_array_append_val(result, tmp);
00604                         }
00605                 } while((status == SQLITE_BUSY) && (status != SQLITE_ERROR));
00606         } while((status == SQLITE_ROW) && (status != SQLITE_ERROR));
00607         sqlite3_finalize(query);
00608         timer(TIMER_STOP,(gchar*)(gchar*)__FUNCTION__);
00609 
00610 
00611         timer(TIMER_START,"callback for returning words LIST START");
00612         // calling callback for word translation
00613 
00614         data->cb_search_word_list(result , pattern, 
00615                         data->cb_search_word_list_data, ENGINE_NO_ERROR);
00616                 
00617         timer(TIMER_STOP,"callback for returning word LIST END");
00618         if(data->auto_free) {
00619                 LOGS("Bookmark/%s->%s() deleting all dynamic data because "
00620                      "AUTO_FREE=TRUE\n",
00621                      (gchar*)__FILE__,
00622                      (gchar*)__FUNCTION__
00623                     );
00624                 g_free(tran);
00625         }
00626 }
00627 
00628 //==============================================================================
00629 //==============================================================================
00630 //==============================================================================
00631 //---------------------------------------------------------- COMPLETED FUNCTIONS
00632 //==============================================================================
00633 // global functions
00634 EngineModule engine_global_functions()
00635 {
00636         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00637         EngineModule result;        
00638         result.engine_check             = bm_engine_check;
00639         result.engine_description       = bm_engine_description;
00640         result.engine_format            = bm_engine_format;
00641         result.engine_version           = bm_engine_version;
00642         result.engine_create            = bm_engine_create;
00643         LOGS("Bookmark/%s->%s()returned EngineModule at adress=%p.\n",
00644              (gchar*)__FILE__,
00645              (gchar*)__FUNCTION__,
00646              result
00647             );
00648         return result;
00649 }
00650 //------------------------------------------------------------------------------
00651 // for macro: dict_eng_status_message(error)
00652 gchar* bm_engine_status_message(EngineStatus error) 
00653 {
00654         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00655         switch (error) {
00656                 case ENGINE_NO_ERROR:
00657                         return "No error.";
00658                 case ENGINE_WRONG_FILE:
00659                         return "File which You are trying to use is wrong type.";
00660                 case ENGINE_COULDNT_READ:
00661                         return "Could not read from file.";
00662                 case ENGINE_NO_FILE:
00663                         return "There is no such a file.";
00664                 case ENGINE_OUT_OF_MEMORY:
00665                         return "There were no enough memory for this action.";
00666                 default:
00667                         return "Wrong engine's status identifier!";
00668         }
00669 }
00670 //------------------------------------------------------------------------------
00671 // for macro: dict_eng_module_get_version(module)
00672 gchar* bm_engine_version() 
00673 {
00674         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00675         gchar* result = g_strdup(DIC_ENG_VERSION);
00676         LOGS("Bookmark/%s->%s() return string=%s\n",
00677                 (gchar*)__FILE__,
00678                 (gchar*)__FUNCTION__,
00679                 result
00680                );
00681         return result;
00682 }
00683 //------------------------------------------------------------------------------
00684 // for macro: dict_eng_module_get_format(module)
00685 gchar* bm_engine_format() 
00686 {
00687         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00688         gchar* result = g_strdup(DIC_ENG_FORMAT);
00689         LOGS("Bookmark/%s->%s() return string=%s\n",
00690              (gchar*)__FILE__,
00691              (gchar*)__FUNCTION__,
00692              result
00693             );
00694         return result;
00695 }
00696 //------------------------------------------------------------------------------
00697 // for macro: dict_eng_module_get_description(module)
00698 gchar* bm_engine_description() 
00699 {
00700         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00701         gchar* result = g_strdup(DIC_ENG_DESCRIPTION);
00702         LOGS("Bookmark/%s->%s() return string=%s\n",
00703              (gchar*)__FILE__,
00704              (gchar*)__FUNCTION__,
00705              result
00706             );
00707         return result;
00708 }
00709 //------------------------------------------------------------------------------
00710 // for macro: dict_eng_get_location(engine)
00711 gchar* bm_engine_location(Engine* engine)
00712 {
00713         LOGS("Bookmark/%s->%s() called.\n-->PARAM: engine adress=%p\n",
00714              (gchar*)__FILE__,
00715              (gchar*)__FUNCTION__,
00716              engine
00717             );
00718         g_assert(engine != NULL);
00719         BookData* data = (BookData*)(engine->engine_data);
00720         
00721         gchar* result;
00722         if(data->auto_free) {
00723                 result = data->dict_path;
00724         }
00725         else {
00726                 result = g_strdup(data->dict_path);
00727         }
00728 
00729         LOGS("Bookmark/%s->%s() returned string=%s\n",
00730              (gchar*)__FILE__,
00731              (gchar*)__FUNCTION__,
00732              result
00733             );
00734         return result;
00735 }
00736 //------------------------------------------------------------------------------
00737 // for macro: dict_eng_set_auto_free(engine, state)
00738 void bm_engine_set_auto_free(Engine* engine, gboolean state) 
00739 {
00740         LOGS("Bookmark/%s->%s() called.\n"
00741              "-->PARAM:engine at adress=%p\n"
00742              "-->PARAM:state=%s\n",
00743              (gchar*)__FILE__,
00744              (gchar*)__FUNCTION__,
00745              engine,
00746              PRINT_STATE(state)
00747             );
00748         g_assert(engine != NULL);
00749         BookData* data = (BookData*)(engine->engine_data);
00750         
00751         data->auto_free = state;
00752         LOGS("Bookmark/%s->%s() Current auto_free is %s\n",
00753              (gchar*)__FILE__,
00754              (gchar*)__FUNCTION__,
00755              PRINT_STATE(data->auto_free)
00756             );
00757 }
00758 //------------------------------------------------------------------------------
00759 // for macro: dict_eng_get_last_status(engine)
00760 EngineStatus bm_engine_status(Engine* engine) 
00761 {
00762         LOGS("Bookmark/%s->%s() called.\n"
00763                 "-->PARAM:engine at adress=%p\n",
00764                 (gchar*)__FILE__,
00765                 (gchar*)__FUNCTION__,
00766                engine
00767                );
00768         BookData* data = (BookData*)(engine->engine_data);
00769         LOGS("Bookmark/%s->%s() returned error code: %d\n",
00770              (gchar*)__FILE__,
00771              (gchar*)__FUNCTION__,
00772              (gint)(data->last_error)
00773             );        
00774         return data->last_error;
00775 }
00776 //------------------------------------------------------------------------------
00777 // for macro: dict_eng_set_progress_seed(engine, signal, val)
00778 void bm_engine_set_progress_seed(Engine* engine, gchar* signal, gdouble seed) {
00779         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00780         BookData* data = (BookData*)(engine->engine_data);
00781         if(g_ascii_strcasecmp(signal,ENGINE_PROGRESS_OPTIMIZING_SIGNAL) == 0)  {
00782                 data->cb_progress_caching_seed = seed;
00783                 LOGS("Bookmark/%s->%s() sets new seed=%0.2f for for signal "
00784                      "\"%s\".\n",
00785                      (gchar*)__FILE__,
00786                      (gchar*)__FUNCTION__,
00787                      seed,
00788                      signal
00789                     );        
00790         } 
00791         else {
00792                 LOGS("Bookmark/%s->%s() unsupported signalfor progress: %s.\n",
00793                      (gchar*)__FILE__,
00794                      (gchar*)__FUNCTION__,
00795                      signal
00796                     );
00797         };
00798 }
00799 //------------------------------------------------------------------------------
00800 // for macro: dict_eng_set_callback(engine,signal,c_handler,data)
00801 gpointer bm_engine_set_callback(Engine* engine,
00802                              gchar* signal,
00803                              gpointer c_handler,
00804                              gpointer user_data)
00805 {
00806         LOGS("Bookmark/%s->%s() called.\n",(gchar*)__FILE__,(gchar*)__FUNCTION__);
00807         g_assert(engine != NULL);
00808         g_assert(signal != NULL);
00809         g_assert(c_handler != NULL);
00810         BookData* data = (BookData*)(engine->engine_data);
00811         if(g_ascii_strcasecmp(signal,ENGINE_PROGRESS_OPTIMIZING_SIGNAL) == 0)  {
00812                 gpointer result = data->cb_progress_caching;
00813                 data->cb_progress_caching = c_handler;
00814                 data->cb_progress_caching_data = user_data;
00815                 LOGS("Bookmark/%s->%s() sets handler for signal \"%s\".\n",
00816                         (gchar*)__FILE__,
00817                         (gchar*)__FUNCTION__,
00818                         signal
00819                        );
00820                 LOGS("Bookmark/%s->%s() Function at adress =  %d.\n",
00821                         (gchar*)__FILE__,
00822                         (gchar*)__FUNCTION__,
00823                         (guint)c_handler
00824                        );
00825                 LOGS("Bookmark/%s->%s()     Data at adress =  %d.\n",
00826                         (gchar*)__FILE__,
00827                         (gchar*)__FUNCTION__,
00828                         (guint)user_data
00829                        );
00830                 return result;                
00831         }
00832         else if(g_ascii_strcasecmp(signal, ENGINE_WORD_LIST_SIGNAL) == 0) {
00833                 gpointer result = data->cb_search_word_list;
00834                 data->cb_search_word_list = c_handler;
00835                 data->cb_search_word_list_data = user_data;
00836                 LOGS("Bookmark/%s->%s() sets handler for signal \"%s\".\n",
00837                         (gchar*)__FILE__,
00838                         (gchar*)__FUNCTION__,
00839                         signal
00840                        );
00841                 LOGS("Bookmark/%s->%s() Function at adress =  %d.\n",
00842                         (gchar*)__FILE__,
00843                         (gchar*)__FUNCTION__,
00844                         (guint)c_handler
00845                        );
00846                 LOGS("Bookmark/%s->%s()     Data at adress =  %d.\n",
00847                         (gchar*)__FILE__,
00848                         (gchar*)__FUNCTION__,
00849                         (guint)user_data
00850                        );
00851                 return result;                        
00852         }
00853         else if(g_ascii_strcasecmp(signal,
00854                 ENGINE_WORD_TRANSLATION_SIGNAL) == 0)  {
00855                         gpointer result = data->cb_search_word_trans;
00856                         data->cb_search_word_trans = c_handler;
00857                         data->cb_search_word_trans_data = user_data;
00858                         LOGS("Bookmark/%s->%s() sets handler for signal \"%s\".\n",
00859                                 (gchar*)__FILE__,
00860                                 (gchar*)__FUNCTION__,
00861                                 signal
00862                                );
00863                         LOGS("Bookmark/%s->%s() Function at adress =  %d.\n",
00864                                 (gchar*)__FILE__,
00865                                 (gchar*)__FUNCTION__,
00866                                 (guint)c_handler
00867                                );
00868                         LOGS("Bookmark/%s->%s()     Data at adress =  %d.\n",
00869                                 (gchar*)__FILE__,
00870                                 (gchar*)__FUNCTION__,
00871                                 (guint)user_data
00872                                );
00873                         return result;                        
00874                 }
00875                 else {
00876                         g_warning("Bookmark/%s->%s() unsupported signal: %s.\n",
00877                                   (gchar*)__FILE__,
00878                                   (gchar*)__FUNCTION__,
00879                                   signal
00880                                  );
00881                         return NULL;
00882                 }
00883 }
00884 
00885 
00886 
00887 
00888 
00889 
00890 //==============================================================================
00891 //==============================================================================
00892 //==============================================================================
00893 //---------------------------------------------------------- HELPFULLY FUNCTIONS
00894 //==============================================================================
00895 
00896 //------------------------------------------------------------------------------
00897 static gchar* string_to_path(gchar** string) {
00898         LOGS("Bookmark/%s->%s() called.\n\
00899                  -->PARAM:string=\'%s\'\n",
00900                  (gchar*)__FILE__,
00901                  (gchar*)__FUNCTION__,
00902                  string[0]
00903                );
00904         gchar* arg = string[0];
00905         gchar* new = NULL;
00906         // cleaning from leading and trailing whitespaces
00907         g_strstrip(arg);        
00908          // add current directory if this is not absolute directory
00909         if (!g_path_is_absolute(arg)) {
00910                 gchar* tmp = g_get_current_dir();
00911                 new = g_strconcat(tmp,"/",arg,NULL);
00912                 g_free(arg); arg = new; new = NULL;
00913         };
00914         // this is not a directory
00915         if (!g_file_test(arg, G_FILE_TEST_IS_DIR)) {        
00916                 // if this is wrong filepath, string was wrong
00917                 if (!g_file_test(arg, G_FILE_TEST_IS_REGULAR)) {        
00918                         g_free(arg);
00919                         new = NULL;
00920                 }
00921                 //if this is a file, remove filename
00922                 else
00923                 {   
00924                         new = g_path_get_dirname (arg);
00925                         g_free(arg);
00926                 }
00927         }
00928         // this is a directory
00929         else {   
00930                 // remove suffix "/" if neded...     
00931                 if (g_str_has_suffix(arg,"/") ) {        
00932                         new = g_path_get_dirname (arg);
00933                         g_free(arg);
00934                 }
00935                 else {
00936                         new = arg;
00937                 }
00938         };
00939         // now in new should be proper filepath, if not, string was wrong
00940         if (!g_file_test(new, G_FILE_TEST_IS_DIR))  {        
00941                 // if that directory does not exist, passed string wasn't proper       
00942                 g_free(new);
00943                 new = NULL;
00944         };
00945         // replace string under passed address
00946         string[0] = new;
00947         LOGS("Bookmark/%s->%s() returned string=\'%s\'\n",
00948                 (gchar*)__FILE__,
00949                 (gchar*)__FUNCTION__,
00950                 string[0]
00951                );
00952         return new;
00953 }
00954 //------------------------------------------------------------------------------
00955 static double timer(gboolean start, gchar* message)
00956 {
00957         static GArray* stack = NULL;
00958         static gboolean first_run = TRUE;
00959         static struct timeval actual_time;
00960         static struct timeval last_time;
00961         static struct timeval result;
00962         static double seconds = 0.0;
00963         if(first_run) {
00964                 first_run = FALSE;
00965                 stack = g_array_new(TRUE, TRUE, sizeof(struct timeval));
00966         };        
00967 
00968         if (start) {
00969                 LOGS("Bookmark->%s() start timer for function '%s()'.\n",
00970                         (gchar*)__FUNCTION__,
00971                         message
00972                        );
00973                 g_array_prepend_val(stack, actual_time);
00974                 gettimeofday(&g_array_index(stack, struct timeval, 0),NULL);
00975                 return -1.0;
00976         }
00977         // we just want to end some timer - print some information about 
00978         // working time;
00979         else {          
00980                 gettimeofday(&actual_time,NULL);
00981                 last_time = g_array_index(stack, struct timeval, 0);
00982                 g_array_remove_index(stack, 0);
00983 
00984                 if (actual_time.tv_usec < last_time.tv_usec) {
00985                         int nsec = (last_time.tv_usec - actual_time.tv_usec) / 
00986                                         (1000000 + 1);
00987                         last_time.tv_usec -= 1000000 * nsec;
00988                         last_time.tv_sec += nsec;
00989                 }
00990                 if (actual_time.tv_usec - last_time.tv_usec > 1000000) {
00991                         int nsec = (last_time.tv_usec - actual_time.tv_usec) / 
00992                                         1000000;
00993                         last_time.tv_usec += 1000000 * nsec;
00994                         last_time.tv_sec -= nsec;
00995                 }
00996                 result.tv_sec = actual_time.tv_sec - last_time.tv_sec;
00997                 result.tv_usec = actual_time.tv_usec - last_time.tv_usec;
00998                 seconds = (((double)(result.tv_usec)) / 1e6) +
00999                                 ((double)(result.tv_sec));
01000 
01001                 LOGS("Bookmark->%s() function \'%s()\' was working for: %g "
01002                                 "[s] or %ld [us].\n",
01003                 (gchar*)__FUNCTION__,
01004                 message,
01005                 seconds,
01006                 ((long)(result.tv_sec*1e6)+(result.tv_usec))
01007                        );
01008                 // stack is empty so we delete everything
01009                 if(stack->len == 0)   
01010                 {
01011                         g_array_free(stack, TRUE);
01012                         first_run = TRUE;
01013                 }
01014         }
01015         return seconds;
01016 }
01017 //------------------------------------------------------------------------------
01018 
01019 
01020 

Generated on Wed Jan 2 09:19:42 2008 for WhiteStork Project by  doxygen 1.5.1