core_plugin.cpp
7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//*****************************************************************
/*
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);
}
}
}