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

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