Added extracting from bz2 archives
authormaritus <lucas.pawlik@gmail.com>
Wed, 21 Feb 2007 10:51:28 +0000 (10:51 +0000)
committermaritus <lucas.pawlik@gmail.com>
Wed, 21 Feb 2007 10:51:28 +0000 (10:51 +0000)
git-svn-id: file:///svnroot/mdictionary/trunk@82 5bde0345-f819-0410-ac75-e5045f9217cc

src/dbus_wrapper/src/ws_dbus.c
src/manager/include/untar.h [new file with mode: 0644]
src/manager/makefile
src/manager/src/untar.c [new file with mode: 0644]
src/manager/src/ws_manager.c

index 341e1b3..bcfaf50 100644 (file)
@@ -171,6 +171,8 @@ WSDBusData * ws_dbus_create (gchar *name, gchar *version)
                              "add_bookmark",
                              "remove_bookmark",
                              "send_message",
+                            "extract_dictionary",
+                            "return_extracted_dict",                           
                              NULL};
 
         guint array_size = 0;
@@ -448,7 +450,7 @@ WSDBusStatus ws_dbus_client_find_word (WSDBusData * ws_dbus_data, gchar * word)
                 return WS_DBUS_STATUS_ERROR;
         };
 
-         osso_rpc_free_val (retval);
+        osso_rpc_free_val (retval);
         g_free (retval);
         return WS_DBUS_STATUS_OK;
 };
@@ -525,12 +527,12 @@ WSDBusStatus ws_dbus_client_search_in_history (WSDBusData* ws_dbus_data,
         if (result != OSSO_OK)
         {
                 g_debug ("Error message: %s\n", retval->value.s);
-                 osso_rpc_free_val (retval);
+                osso_rpc_free_val (retval);
                 g_free (retval);
                 return WS_DBUS_STATUS_ERROR;
         };
 
-         osso_rpc_free_val (retval);
+        osso_rpc_free_val (retval);
         g_free (retval);
         return WS_DBUS_STATUS_OK;
 };
@@ -570,7 +572,7 @@ WSDBusStatus ws_dbus_server_return_words  (WSDBusData * ws_dbus_data,
                 return WS_DBUS_STATUS_ERROR;
         };
 
-         osso_rpc_free_val (retval);
+        osso_rpc_free_val (retval);
         g_free (retval);
         return WS_DBUS_STATUS_OK;
 };
@@ -841,7 +843,7 @@ WSDBusStatus ws_dbus_update_progressbar (WSDBusData * ws_dbus_data,
                 return WS_DBUS_STATUS_ERROR;
         };
 
-         osso_rpc_free_val (retval);
+        osso_rpc_free_val (retval);
         g_free (retval);
         return WS_DBUS_STATUS_OK;
 };
@@ -970,3 +972,86 @@ WSDBusStatus ws_dbus_send_message (WSDBusData * ws_dbus_data, gchar * word)
         g_free (retval);
         return WS_DBUS_STATUS_OK;
 };
+
+WSDBusStatus ws_dbus_extract_dictionary(WSDBusData * ws_dbus_data, 
+                                       gchar* path)
+
+{
+        osso_return_t result;
+        osso_rpc_t *retval;
+
+        retval = g_try_malloc (sizeof (osso_rpc_t));
+
+        if (retval == NULL) 
+        {
+                g_debug("DBUS: Error in function %s:",__FUNCTION__);
+                g_debug(" Couldn't allocate memory for message's return value\n");
+        };
+
+        result = osso_rpc_run(ws_dbus_data->context, 
+                               ws_dbus_data->remote_service, 
+                               ws_dbus_data->remote_object, 
+                               ws_dbus_data->remote_iface, 
+                               "extract_dictionary", 
+                               retval,
+                               DBUS_TYPE_STRING,
+                               path,
+                               DBUS_TYPE_INVALID);
+
+        g_debug ("\nDBUS: %s: ", __FUNCTION__);
+
+        ws_dbus_libosso_errors (result);
+
+        if (result != OSSO_OK)
+        {
+                g_debug ("Error message: %s\n", retval->value.s);
+                osso_rpc_free_val (retval);
+                g_free (retval);
+                return WS_DBUS_STATUS_ERROR;
+        };
+
+        osso_rpc_free_val (retval);
+        g_free (retval);
+        return WS_DBUS_STATUS_OK;
+};
+
+WSDBusStatus ws_dbus_server_return_extracted_dict(WSDBusData* ws_dbus_data, 
+                                       gchar* path)
+{
+       osso_return_t result;
+        osso_rpc_t *retval;
+
+        retval = g_try_malloc (sizeof (osso_rpc_t));
+
+        if (retval == NULL) 
+        {
+                g_debug("DBUS: Error in function %s:", __FUNCTION__);
+                g_debug(" Couldn't allocate memory for message's return value\n");
+        };
+
+        result = osso_rpc_run(ws_dbus_data->context, 
+                              ws_dbus_data->remote_service, 
+                              ws_dbus_data->remote_object, 
+                              ws_dbus_data->remote_iface, 
+                              "return_extracted_dict", 
+                              retval,
+                              DBUS_TYPE_STRING,
+                              path,
+                              DBUS_TYPE_INVALID);
+
+        g_debug ("\nDBUS: %s: ", __FUNCTION__);
+
+        ws_dbus_libosso_errors (result);
+
+        if (result != OSSO_OK)
+        {
+                g_debug ("Error message: %s\n", retval->value.s);
+                 osso_rpc_free_val (retval);
+                g_free (retval);
+                return WS_DBUS_STATUS_ERROR;
+        };
+
+        osso_rpc_free_val (retval);
+        g_free (retval);
+        return WS_DBUS_STATUS_OK;      
+};
diff --git a/src/manager/include/untar.h b/src/manager/include/untar.h
new file mode 100644 (file)
index 0000000..aaa1f76
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef _WS_UNTAR
+#define _WS_UNTAR
+
+#include <glib.h>
+/*
+Usage example:
+
+decompress_file ("comn_dictd04_wn.tar.bz2", "./"); 
+
+The above example extracts a given archive, to the current directory
+*/
+
+gint decompress_file(gchar *in_file, gchar *out_path);
+
+
+#endif /*_WS_UNTAR*/
index b16cd8b..2c89ef1 100644 (file)
@@ -1,8 +1,8 @@
 CC = gcc
 DEBUG = -g -Wall 
-INCLUDE = include
+INCLUDE = ./include
 INCLUDE_DBUS_WRAPPER = ../../include
-LIBS = `pkg-config --libs glib-2.0 gmodule-2.0 libosso gconf-2.0 ` -lpthread
+LIBS = `pkg-config --libs glib-2.0 gmodule-2.0 libosso gconf-2.0 ` -lpthread -lbz2
 SQLITE=-DSQLITE=0
 FLAGS = `pkg-config --cflags glib-2.0 gmodule-2.0 libosso gconf-2.0 ` -I${INCLUDE} -I${INCLUDE_DBUS_WRAPPER} ${SQLITE}
 SOURCE = src
@@ -11,10 +11,9 @@ MAIN_BINARIES = ../../bin
 
 EXECUTABLE = ${BINARY}/WhiteStorkManager
 
-all: ${BINARY}/ws_manager.o ${BINARY}/whitestork.o ${MAIN_BINARIES}/ws_dbus.o ${BINARY}/pc-instances.o
+all: ${BINARY}/ws_manager.o ${BINARY}/whitestork.o ${MAIN_BINARIES}/ws_dbus.o ${BINARY}/pc-instances.o ${BINARY}/untar.o
                @echo Compiling with ${SQLITE} flag
-
-               @${CC} ${DEBUG} ${BINARY}/ws_manager.o ${BINARY}/whitestork.o ${MAIN_BINARIES}/ws_dbus.o ${BINARY}/pc-instances.o -o ${EXECUTABLE} ${LIBS} ${FLAGS}
+               @${CC} ${DEBUG} ${BINARY}/ws_manager.o ${BINARY}/whitestork.o ${MAIN_BINARIES}/ws_dbus.o ${BINARY}/pc-instances.o ${BINARY}/untar.o -o ${EXECUTABLE} ${LIBS} ${FLAGS}
                @cp ${EXECUTABLE} ${MAIN_BINARIES}
 
 ${BINARY}/pc-instances.o: ${SOURCE}/pc-instances.c
@@ -24,8 +23,9 @@ ${BINARY}/whitestork.o: ${SOURCE}/whitestork.c
                @${CC} ${DEBUG} -c ${SOURCE}/whitestork.c -o ${BINARY}/whitestork.o ${FLAGS}
 
 ${BINARY}/ws_manager.o: ${SOURCE}/ws_manager.c
-               @echo ${FLAGS}
                @${CC} ${DEBUG} -c ${SOURCE}/ws_manager.c -o ${BINARY}/ws_manager.o ${FLAGS}
+${BINARY}/untar.o: ${SOURCE}/untar.c
+               @${CC} ${DEBUG} -c ${SOURCE}/untar.c -o ${BINARY}/untar.o ${FLAGS} 
 
 ${MAIN_BINARIES}/ws_dbus.o:
                @cd ../dbus_wrapper && make
diff --git a/src/manager/src/untar.c b/src/manager/src/untar.c
new file mode 100644 (file)
index 0000000..e9d615a
--- /dev/null
@@ -0,0 +1,221 @@
+#include <untar.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <bzlib.h>
+#include <sys/stat.h>
+#include <glib-2.0/glib.h>
+
+#define BLOCK_SIZE 512
+#define BUFFER_SIZE BLOCK_SIZE*32
+#define ASCII_NR(NR) (NR + 0x30)
+
+typedef struct _TarHeader
+{
+       gchar name[100];
+       gchar mode[8];
+       gchar uid[8];
+       gchar gid[8];
+       gchar size[12];
+       gchar mtime[12];
+       gchar chksum[8];
+       gchar typeflag;
+       gchar linkname[100];
+       gchar magic[6];
+       gchar version[2];
+       gchar uname[32];
+       gchar gname[32];
+       gchar devmajor[8];
+       gchar devminor[8];
+       gchar prefix[155];
+       gchar padding[12];
+       gchar *gnu_longname;
+       gchar *gnu_longlink;
+} TarHeader;
+
+typedef enum _RecordType
+{
+       File = ASCII_NR (0),
+       ARCHLINK = ASCII_NR (1),
+       SYMLINK = ASCII_NR (2),
+       CHARDEV = ASCII_NR (3),
+       BLOCKDEV = ASCII_NR (4),
+       Dir = ASCII_NR (5),
+       FIFO = ASCII_NR (6),
+       RESERVED = ASCII_NR (7)
+} RecordType;
+
+static gint unpack_file_contents(BZFILE *bz2_file, 
+                               TarHeader *header,
+                               gchar *out_name);
+
+
+static gint unpack_file_contents(BZFILE *bz2_file, 
+                               TarHeader *header,
+                               gchar *out_name)
+{
+       gchar* buffer;
+       guint i;
+       FILE *file_out;
+       gulong length;
+       guint result;
+       gint bzerror;
+       
+       file_out = fopen(out_name, "w");
+       
+       if (file_out == NULL) return -1;
+       
+       sscanf(header->size, "%12lo", &length);
+       printf ("File size: %ld\n", length);
+       
+       for (i=0; i < (length / BLOCK_SIZE); ++i)
+       {
+               buffer = (gchar*) g_try_malloc (BLOCK_SIZE * sizeof(gchar));
+               
+               result = BZ2_bzRead ( &bzerror, bz2_file, buffer, BLOCK_SIZE *
+                               sizeof (gchar));
+               
+               fwrite (buffer, BLOCK_SIZE * sizeof(gchar), 1, file_out);
+               g_free (buffer);
+       };
+               
+       i = length % BLOCK_SIZE;
+                               
+       if (i != 0)
+       {
+               buffer = (gchar*) g_try_malloc (BLOCK_SIZE * sizeof(gchar));
+               
+               if (buffer == NULL)
+               {
+                       printf ("Memory not allocated");
+                       return -1;
+               };
+               
+               result = BZ2_bzRead(&bzerror, bz2_file, buffer, 
+                               BLOCK_SIZE * sizeof(gchar));
+               
+               if (bzerror == BZ_OK)
+               {
+                       fwrite (buffer, i * sizeof(gchar), 1, file_out);
+               }
+               
+               else 
+               {
+                       printf ("bzerror = %d", bzerror);
+                       return -1;
+               };
+               
+               g_free(buffer);
+               
+       };
+                               
+       fclose(file_out);
+       
+       return 0;
+};
+
+gint decompress_file (gchar *in_file, gchar *out_path)
+{
+       FILE *file;
+       BZFILE  *bz2_file;
+       guint result;
+       TarHeader* header;
+       gint bzerror;
+       
+       header = (TarHeader*) g_try_malloc (BLOCK_SIZE * sizeof(gchar));
+       
+       if (header == NULL) printf ("\nCould not allocate memory\n");
+       
+       file = fopen (in_file, "rb");
+       
+       if (file != NULL)
+
+       {
+               bz2_file = BZ2_bzReadOpen (&bzerror, file, 0, 0, NULL, 0);
+
+               if ( bzerror != BZ_OK ) 
+               {
+                       BZ2_bzReadClose ( &bzerror, bz2_file );
+                       printf ("There was an error while reading the compressed file\n");
+               }
+               
+               bzerror = BZ_OK;
+               while (1) 
+               {
+                       
+                       result = BZ2_bzRead ( &bzerror, bz2_file, header, 
+                                              BLOCK_SIZE * sizeof (char));
+                       
+//                     printf ("Jestem\n");
+                       
+                       if ( bzerror == BZ_OK )
+                       {
+                               if (strlen (header->name) == 0)
+                               {
+                                       printf ("\nFilename length is 0, exitting\n");
+                                       break;
+                               };
+                               
+                               printf ("Name: %s\n", header->name);
+                               printf ("Prefix: %s\n", header->prefix);
+                               
+
+                               gchar *temp = g_strconcat (out_path,
+                                                       header->name, NULL);
+                               
+                               switch (header->typeflag)
+                               {
+                                       case File:
+                                               printf ("File\n");
+                                               unpack_file_contents (bz2_file, 
+                                                       header, temp);
+                                               break;
+                                               
+                                       case Dir:
+                                               
+                                               printf ("Dir %s\n", temp);
+                                               if (mkdir (temp, S_IRUSR|
+                                                       S_IWUSR|S_IXUSR) != 0)
+                                               {
+                                                       printf ("Couldn't create directory %s", 
+                                                              temp);
+                                               };
+                                               
+                                               
+                                               break;
+                                               
+                                       default:
+                                               printf ("Only files and dirs can be unpacked");
+                               }
+                               
+                               g_free (temp);
+                               
+                       }
+                               
+                       else if ( bzerror != BZ_STREAM_END )
+                       {
+                               printf ("\nBZ2 READ_CLOSE(stream end) %d\n", bzerror);
+                               BZ2_bzReadClose ( &bzerror, bz2_file );
+                               break;
+                       }
+                       
+                       else 
+                       {
+                               printf ("\nExitting, error nr %d\n", bzerror);
+                               BZ2_bzReadClose ( &bzerror, bz2_file );
+                               break;
+                       };
+                       
+               };
+               
+       }
+       else 
+       {
+               printf ("There was an error while trying to read the archive file");
+               return -100;
+       };
+       
+       return 100;
+       
+       fclose (file);
+};
index 246f63d..cb27331 100644 (file)
@@ -24,14 +24,13 @@ extern "C" {
 
 #include <unistd.h>
 #include <glib/gstdio.h>
-//#include <glib.h>
 #include <gconf/gconf-client.h>
 #include <gconf/gconf.h>
 #include <pthread.h>
 #include <gmodule.h>
 #include <string.h>
-#include "ws_manager.h"
-
+#include <ws_manager.h>
+#include <untar.h>
 #define GCONF_KEY "/apps/maemo/WhiteStork/Dictionaries"
 #define GCONF_LIB_PATH "/apps/maemo/WhiteStork/Engines"
                         
@@ -86,6 +85,8 @@ WSMngSearchData* ws_manager_create()
        return NULL;
 }
 
+static void ws_mng_extract_dictionary(GError *error, GArray *word,
+                                   gpointer user_data);
 /**
  * Public function, see ws_manager.h
  */
@@ -117,10 +118,16 @@ static void ws_mng_on_found_translation(gchar* translation,
 {
         g_debug("->%s", __FUNCTION__);
         WSMngSearchData *data = (WSMngSearchData *) user_data;
+       
+       
         //decrement returned results count
         //data->returned_results--;
        data->returned_trans_results--;
+       
         //if there is returned translation        
+       g_printf("\ndla slowa %s\n",  pattern);
+       g_printf("ile slownikow %d\n", data->returned_trans_results);
+       g_printf("moje tlmumaczenie %s\n", data->trans);
         if (translation != NULL)
         {
                 if (data->trans == NULL)
@@ -131,11 +138,11 @@ static void ws_mng_on_found_translation(gchar* translation,
                                           "<PATTERN_CLOSED><TRANSLATION_OPEN>",
                                           translation,
                                           "<TRANSLATION_CLOSED>",
-                                          NULL);
-                }
+                                          NULL);               
+               }
                 else //if thre was stored trnasloation 
                 {
-                        if (data->returned_trans_results >= 0)
+                        if (data->returned_trans_results > 0)
                         {
                                 //copy stored trnasloation to temporery variable
                                 gchar* tmp = g_strconcat(data->trans,
@@ -170,8 +177,8 @@ static void ws_mng_on_found_translation(gchar* translation,
                 {
                         data->search_in_history = FALSE;
                        data->is_seraching_trans = FALSE;
-                               //send translation to gui
-                               ws_dbus_server_return_translations(data->dbus_data,
+                               //send translation to gui                       
+                       ws_dbus_server_return_translations(data->dbus_data,
                                                        data->trans);
                        g_free(data->trans); data->trans = NULL;        
                 }
@@ -338,7 +345,7 @@ static void ws_mng_on_search_word(GError *error, GArray *word, gpointer user_dat
                         NULL, 
                         ws_mng_search_word, 
                         data);   //create thread and run search 
-        g_debug("<-%s", __FUNCTION__);
+       g_debug("<-%s", __FUNCTION__);
 }
 
 
@@ -617,7 +624,7 @@ static void ws_mng_load_dict(GArray* dict_directory, WSMngSearchData* data)
  *@param word pattern to serch for translation
  *@param user_data data needed to send results to UI
  */
-static void ws_mng_on_search_in_history (GError *error, GArray *word, 
+static void ws_mng_on_search_in_history(GError *error, GArray *word, 
                                   gpointer user_data)
 {
         g_debug("->%s", __FUNCTION__);
@@ -965,6 +972,10 @@ void ws_mng_init_dbus (WSMngSearchData *data)
                       "remove_bookmark",
                       ws_mng_remove_bookmark,
                       data);//set callback for remove bookmarks signal
+       ws_dbus_set_cb(data->dbus_data,
+                       "extract_dictionary",
+                       ws_mng_extract_dictionary,
+                       data);
 
 
         g_debug("<-%s", __FUNCTION__);
@@ -1336,6 +1347,32 @@ void ws_mng_close (WSMngSearchData *data)
         g_debug("<-%s", __FUNCTION__);
 }
 
+static void ws_mng_extract_dictionary(GError *error, GArray* param,
+                                   gpointer user_data)
+{
+       g_debug("->%s", __FUNCTION__);
+       WSMngSearchData *data = (WSMngSearchData *) user_data;
+        osso_rpc_t *osso_data;
+        //get the data sended by dbus
+        osso_data = &g_array_index (param, osso_rpc_t, 0); 
+        //get the path sended by dbus
+        gchar* path = g_strdup(osso_data->value.s);
+       gint result = -1;
+       if (path != NULL)
+       {
+               result = decompress_file(path,"./");
+       }
+       if (result > 0)
+       {
+               ws_dbus_server_return_extracted_dict(data->dbus_data, 
+                                                       path);
+       }else
+       {
+               ws_dbus_server_return_extracted_dict(data->dbus_data, 
+                                                       NULL);
+       }
+       g_debug("<-%s", __FUNCTION__);
+}
 #ifdef __cplusplus
 }
 #endif