core_plugin.cpp 7.22 KB
//*****************************************************************
/*
	VLibras: Automatic contents translator from Portuguese to LIBRAS

	Copyright (c) 2015 Gustavo Sobral, Erickson Silva, Leonardo Araújo
	VLibras-Core group at LAViD, Federal University of Paraiba
*/
//*****************************************************************

/**
 * \file corePlugin.cpp
 * \author Leonardo Araújo
 * \date Maio 2015
 */

#include "core_plugin.h"


/* Variables */

PyTradutor *tradutor;
int app_current_version, dict_current_version;


/* Functions */

int coreInitialize()
{
	tradutor = new PyTradutor();
	return 1;
}


const char* coreExecute()
{
	return tradutor->convertStringToGlosa((const char*) get_text_selection());
}


int coreFinalize()
{
	if (!tradutor)
		return -1;
	delete tradutor;
	return 1;
}


int coreUpdateInstall_player()
{
    system(SCRIPT_UPDATE_VLIBRAS);
    return system("/./opt/vlibras_desktop/script/launcher.sh &");
}
    

int coreUpdateInstall_dict()
{
    dict_current_version = get_installed_version_deb_package(DIC_PACKAGE);
    if (dict_current_version == 0)
        return system(SCRIPT_INSTALL_DICT);    
    return system(SCRIPT_UPDATE_DICT);
}


int coreUpdateCheck()
{
    app_current_version = get_installed_version_deb_package(APP_PACKAGE);
    dict_current_version = get_installed_version_deb_package(DIC_PACKAGE);

    std::string response_api_str = request_current_versions().c_str();
    if (response_api_str.compare("null") == 0)
        return -1;

    if (get_version_from_string(response_api_str, "playerVersion") > app_current_version)
        return 1;
    else
        if (get_version_from_string(response_api_str, "dictionaryVersion") > dict_current_version)
            return 2;
    return 0;
}


int get_installed_version_deb_package(std::string _package_id)
{
    std::string _deb_package_name = "dpkg -s ";
    _deb_package_name.append(_package_id);

    FILE *comm_file_p = popen(_deb_package_name.c_str(), "r");

    char line_buff [MAX_BUFFER_SELECTION];
    std::string version_str = "";
    bool found_flag = false;

    if (comm_file_p == NULL) perror ("Error opening file");
    else
    {
        while ( ! feof (comm_file_p) )
        {
            if ( fgets (line_buff , (int) sizeof(line_buff) , comm_file_p) == NULL ) break;

            version_str = (std::string) line_buff;
            if ( version_str.find("Version:") != std::string::npos )
            {
                found_flag = true;
                break;
            }
        }
        fclose (comm_file_p);
    }

    if (!found_flag) return 0;

    char *line_version_ptr  = strtok ((char*) version_str.c_str(), " ");
    
    if (line_version_ptr  == NULL)
    {
        printf("Version of the deb package is null!\n");
        return 0;
    }

    line_version_ptr  = strtok (NULL, " ");
    version_str = (std::string) line_version_ptr;
    remove_special_chars(&version_str, ".", (char*) "");

    printf("%s installed version: %s\n", _package_id.c_str(), version_str.c_str());

    return atoi(version_str.c_str());

}



int get_version_from_string(std::string json_message, std::string _key)
{
    int position = json_message.find(_key);
    if (position == -1)
        return position;

    std::string version_str_cpy = json_message.substr(position, json_message.find(","));

    remove_special_chars(&version_str_cpy, ".\":,", (char*) "");
    char *tokens_ptr;
    if ( version_str_cpy.find("_") != std::string::npos )
    {
        tokens_ptr  = strtok ((char*) version_str_cpy.c_str(), "_");
        if (tokens_ptr  == NULL)
            printf("[Error] Cannot handler the version of dictionary.\n");
        version_str_cpy = (std::string) tokens_ptr;
    }

    tokens_ptr  = strtok ((char*) version_str_cpy.c_str(), " ");
    if (tokens_ptr  == NULL)
    {
        printf("[Error] Cannot split string with versions.\n");
        return -1;
    }
    tokens_ptr = strtok (NULL, " ");
    version_str_cpy = (std::string) tokens_ptr;    

    return atoi(version_str_cpy.c_str());
}


//@Deprecated
int get_version_from_json(std::string json_message, std::string _key)
{
    
    return -1;

    /*//Code to JsonCpp
    Json::Value parsed_from_str;
    Json::Reader json_reader;
    bool parsing_flag = json_reader.parse(json_message, parsed_from_str);
    
    if (!parsing_flag)
        throw new RuntimeException("Cannot convert the json message to string.");

    std::string version_str = parsed_from_str[_key].asString();
    
    // Check if the version of dictionary contains '_' token.
    if ( version_str.find("_") != std::string::npos )
    {
        char *tokens_ptr  = strtok ((char*) version_str.c_str(), "_");
        if (tokens_ptr  == NULL)
            throw new RuntimeException("Cannot handler the version of dictionary.");            
        version_str = (std::string) tokens_ptr;
    }

    remove_special_chars(&version_str, ".", (char*) "");
    return atoi(version_str.c_str());
    */
}



std::string request_current_versions()
{
    CURL *curl_ptr;
    CURLcode curl_response;
    std::string data_str;

    util::PropertyHandler *property_handler_ptr;
    property_handler_ptr = new util::PropertyHandler((std::string) FILEPATH_CONFIG);

    curl_ptr = curl_easy_init();
    if(curl_ptr)
    {
        std::string url_endpoint = property_handler_ptr->getAttributeValue(ENDPOINT_UPDATE_ID);
        
        std::stringstream ss_convert;
        ss_convert << dict_current_version;
        url_endpoint.append(ss_convert.str());

        if (url_endpoint.size() <= 0)
            throw new RuntimeException("The URL for this Endpoint is not available on the file config.");

        curl_easy_setopt(curl_ptr, CURLOPT_URL, (char*) url_endpoint.c_str());
        curl_easy_setopt(curl_ptr, CURLOPT_WRITEFUNCTION, writeDataCallback);
        curl_easy_setopt(curl_ptr, CURLOPT_WRITEDATA, (char*) &data_str);

        curl_response = curl_easy_perform(curl_ptr);
        
        if(curl_response != CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(curl_response));
            return "null";
        }

        /* always cleanup */
        curl_easy_cleanup(curl_ptr);
        
    }
    return data_str;
}


static size_t writeDataCallback(
    char *contents, size_t size, size_t nmemb, std::string *writer_data)
{
    if (writer_data == NULL)
        return 0;

    writer_data->append(contents, (size * nmemb));
    return (size * nmemb);  
}


char* get_text_selection()
{

	FILE *comm_file_p = popen("xsel -o ", "r");

    char line_buff [MAX_BUFFER_SELECTION];
    std::string line_str = "";

    if (comm_file_p == NULL) perror ("Error opening file");
    else
    {
     while ( ! feof (comm_file_p) )
     {
       if ( fgets (line_buff , (int) sizeof(line_buff) , comm_file_p) == NULL ) break;
       line_str.append((std::string) line_buff);
     }
     fclose (comm_file_p);
    }
    remove_special_chars(&line_str, (std::string) REPLACE_CHARACTERS, (char*) " ");
    
    return (char*) line_str.c_str();

}

void remove_special_chars(std::string *_word, std::string _chars, char* new_char)
{    
    int index = -1; 
    for (int i = 0; i < (int) _chars.size(); i++)
    {
        while ( (index = (int) _word->find(_chars[i])) != (int) std::string::npos)
        {
            _word->replace(index, 1, new_char);
        }
    }
}