2 * MStarDict - International dictionary for Maemo.
3 * Copyright (C) 2010 Roman Moravcik
5 * base on code of stardict:
6 * Copyright (C) 2003-2007 Hu Zheng <huzheng_001@163.com>
8 * based on code of sdcv:
9 * Copyright (C) 2005-2006 Evgeniy <dushistov@mail.ru>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <glib/gi18n.h>
33 #include "lib/getuint32.h"
35 #include "libwrapper.hpp"
36 #include "mstardict.hpp"
39 string parse_data(const gchar *data,
46 guint32 data_size, sec_size = 0;
47 const gchar *p = data;
48 data_size = get_uint32(data);
51 size_t nPlugins = pMStarDict->oPlugins->ParseDataPlugins.nplugins();
52 unsigned int parsed_size;
53 ParseResult parse_result;
55 while (guint32(p - data) < data_size) {
56 for (iPlugin = 0; iPlugin < nPlugins; iPlugin++) {
58 if (pMStarDict->oPlugins->ParseDataPlugins.parse(iPlugin, p, &parsed_size, parse_result, oword)) {
63 if (iPlugin != nPlugins) {
64 for (std::list<ParseResultItem>::iterator it = parse_result.item_list.begin(); it != parse_result.item_list.end(); ++it) {
66 case ParseResultItemType_mark:
67 g_debug("ParseResultItemType_mark");
68 mark += it->mark->pango;
70 case ParseResultItemType_link:
71 mark += it->mark->pango;
73 case ParseResultItemType_res:
75 g_debug("ParseResultItemType_res");
77 if (it->res->type == "image") {
78 } else if (it->res->type == "sound") {
79 } else if (it->res->type == "video") {
83 mark += "<span foreground=\"red\">";
84 gchar *m_str = g_markup_escape_text(it->res->key.c_str(), -1);
91 case ParseResultItemType_widget:
92 g_debug("ParseResultItemType_widget");
95 g_debug("ParseResultItemType_default");
105 case 'l': //need more work...
107 sec_size = strlen(p);
109 gchar *m_str = g_markup_escape_text(p, sec_size);
117 sec_size = strlen(p);
125 sec_size = strlen(p) + 1;
126 mark += _("XDXF data parsing plug-in is not found!");
130 sec_size = strlen(p) + 1;
131 mark += _("PowerWord data parsing plug-in is not found!");
135 sec_size = strlen(p) + 1;
136 mark += _("Wiki data parsing plug-in is not found!");
140 sec_size = strlen(p) + 1;
141 mark += _("HTML data parsing plug-in is not found!");
145 sec_size = strlen(p) + 1;
146 mark += _("WordNet data parsing plug-in is not found!");
150 sec_size = strlen(p);
152 mark += "[<span foreground=\"blue\">";
153 gchar *m_str = g_markup_escape_text(p, sec_size);
162 sec_size = strlen(p);
164 mark += "[<span foreground=\"red\">";
165 gchar *m_str = g_markup_escape_text(p, sec_size);
174 sec_size = g_ntohl(get_uint32(p));
175 //enbale sound button.
176 sec_size += sizeof(guint32);
181 sec_size = g_ntohl(get_uint32(p));
183 GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
184 gdk_pixbuf_loader_write(loader, (const guchar *) (p + sizeof(guint32)), sec_size, NULL);
185 gdk_pixbuf_loader_close(loader, NULL);
186 GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
188 // append_and_mark_orig_word(mark, real_oword, LinksPosList());
190 // append_pixbuf(pixbuf);
192 mark += _("<span foreground=\"red\">[Load image error!]</span>");
194 g_object_unref(loader);
196 mark += _("<span foreground=\"red\">[Missing Image]</span>");
198 sec_size += sizeof(guint32);
202 if (g_ascii_isupper(*p)) {
204 sec_size = g_ntohl(get_uint32(p));
205 sec_size += sizeof(guint32);
208 sec_size = strlen(p) + 1;
210 mark += _("Unknown data type");
219 Library::ListWords(CurrentIndex *iIndex)
221 CurrentIndex *iCurrent = (CurrentIndex *) g_memdup(iIndex,
222 sizeof(CurrentIndex) *
223 query_dictmask.size());
225 oStarDict->ResultsListClear();
228 const gchar *poCurrentWord = poGetCurrentWord(iIndex, query_dictmask, 0);
230 oStarDict->ResultsListInsertLast(poCurrentWord);
233 while (iWordCount < 30 && (poCurrentWord = poGetNextWord(NULL, iIndex, query_dictmask, 0))) {
234 oStarDict->ResultsListInsertLast(poCurrentWord);
238 oStarDict->ResultsReScroll();
245 Library::BuildResultData(std::vector < InstantDictIndex > &dictmask,
247 CurrentIndex *iIndex,
252 bool bFound = false, bLookupWord = false, bLookupSynonymWord = false;
253 gint nWord = 0, count = 0, i = 0, j = 0;
255 struct SearchResult *result;
257 iRealLib = dictmask[iLib].index;
259 bLookupWord = LookupWord(sWord, iIndex[iLib].idx, iIndex[iLib].idx_suggest, iRealLib, 0);
262 LookupSimilarWord(sWord, iIndex[iLib].idx, iIndex[iLib].idx_suggest, iRealLib, 0);
265 SimpleLookupWord(sWord, iIndex[iLib].idx, iIndex[iLib].idx_suggest, iRealLib, 0);
268 LookupSynonymWord(sWord, iIndex[iLib].synidx, iIndex[iLib].synidx_suggest, iRealLib, 0);
269 if (!bLookupSynonymWord)
271 LookupSynonymSimilarWord(sWord, iIndex[iLib].synidx,
272 iIndex[iLib].synidx_suggest, iRealLib, 0);
273 if (!bLookupSynonymWord)
275 SimpleLookupSynonymWord(sWord, iIndex[iLib].synidx,
276 iIndex[iLib].synidx_suggest, iRealLib, 0);
278 if (bLookupWord || bLookupSynonymWord) {
282 if (bLookupSynonymWord)
283 nWord += GetOrigWordCount(iIndex[iLib].synidx, iRealLib, false);
286 count = GetOrigWordCount(iIndex[iLib].idx, iRealLib, true);
287 for (i = 0; i < count; i++) {
288 result = (SearchResult *) g_malloc(sizeof(struct SearchResult));
289 result->bookname = g_strdup(dict_name(iLib).c_str());
290 result->exp = g_strdup(poGetOrigWord(iIndex[iLib].idx, iRealLib));
291 result->def = g_strdup(parse_data(poGetOrigWordData(iIndex[iLib].idx + i, iRealLib), poGetOrigWord(iIndex[iLib].idx, iRealLib)).c_str());
292 *result_data = g_list_append(*result_data, result);
298 for (j = 0; i < nWord; i++, j++) {
299 iWordIdx = poGetOrigSynonymWordIdx(iIndex[iLib].synidx + j, iRealLib);
300 result = (SearchResult *) g_malloc(sizeof(struct SearchResult));
301 result->bookname = g_strdup(dict_name(iLib).c_str());
302 result->exp = g_strdup(poGetOrigWord(iWordIdx, iRealLib));
303 result->def = g_strdup(parse_data(poGetOrigWordData(iWordIdx, iRealLib), poGetOrigWord(iWordIdx, iRealLib)).c_str());
304 *result_data = g_list_append(*result_data, result);
314 Library::FreeResultData(GList *result_data)
319 Library::SimpleLookup(const gchar *sWord,
320 CurrentIndex *piIndex)
322 CurrentIndex *iIndex;
323 GList *results = NULL;
327 iIndex = (CurrentIndex *) g_malloc(sizeof(CurrentIndex) * query_dictmask.size());
331 for (size_t iLib = 0; iLib < query_dictmask.size(); iLib++) {
332 if (BuildResultData(query_dictmask, sWord, iIndex, iLib, &results))
336 FreeResultData(results);
345 Library::LookupWithFuzzy(const gchar *sWord)
347 static const int MAX_FUZZY_MATCH_ITEM = 100;
348 gchar *fuzzy_reslist[MAX_FUZZY_MATCH_ITEM];
351 oStarDict->ResultsListClear();
353 bFound = Libs::LookupWithFuzzy(sWord, fuzzy_reslist, MAX_FUZZY_MATCH_ITEM, query_dictmask);
355 SimpleLookup(fuzzy_reslist[0], iCurrentIndex);
357 for (int i = 0; i < MAX_FUZZY_MATCH_ITEM && fuzzy_reslist[i]; i++) {
358 oStarDict->ResultsListInsertLast(fuzzy_reslist[i]);
359 g_free(fuzzy_reslist[i]);
361 oStarDict->ResultsReScroll();
368 Library::LookupWithRule(const gchar *sWord)
370 gint iMatchCount = 0;
372 gchar **ppMatchWord =
373 (gchar **) g_malloc(sizeof(gchar *) * (MAX_MATCH_ITEM_PER_LIB) * query_dictmask.size());
375 oStarDict->ResultsListClear();
377 iMatchCount = Libs::LookupWithRule(sWord, ppMatchWord, query_dictmask);
379 for (gint i = 0; i < iMatchCount; i++)
380 oStarDict->ResultsListInsertLast(ppMatchWord[i]);
382 SimpleLookup(ppMatchWord[0], iCurrentIndex);
383 oStarDict->ResultsReScroll();
385 for (gint i = 0; i < iMatchCount; i++)
386 g_free(ppMatchWord[i]);
395 Library::LookupWithRegex(const gchar *sWord)
397 gint iMatchCount = 0;
399 gchar **ppMatchWord =
400 (gchar **) g_malloc(sizeof(gchar *) * (MAX_MATCH_ITEM_PER_LIB) * query_dictmask.size());
402 oStarDict->ResultsListClear();
404 iMatchCount = Libs::LookupWithRegex(sWord, ppMatchWord, query_dictmask);
406 for (gint i = 0; i < iMatchCount; i++)
407 oStarDict->ResultsListInsertLast(ppMatchWord[i]);
409 SimpleLookup(ppMatchWord[0], iCurrentIndex);
410 oStarDict->ResultsReScroll();
412 for (gint i = 0; i < iMatchCount; i++)
413 g_free(ppMatchWord[i]);
422 LookupProgressDialogUpdate(gpointer data,
425 GtkWidget *dialog = GTK_WIDGET(data);
428 progress = GTK_WIDGET(g_object_get_data(G_OBJECT(dialog), "progress_bar"));
429 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), fraction);
431 while (gtk_events_pending())
432 gtk_main_iteration();
436 Library::LookupData(const gchar *sWord)
442 std::vector < std::vector < gchar * > > reslist(query_dictmask.size());
445 oStarDict->ResultsListClear();
446 oStarDict->ShowProgressIndicator(true);
447 dialog = oStarDict->CreateLookupProgressDialog(&cancel);
449 bFound = Libs::LookupData(sWord, &reslist[0], LookupProgressDialogUpdate, (gpointer) dialog, &cancel, query_dictmask);
451 for (size_t iLib = 0; iLib < query_dictmask.size(); iLib++) {
452 if (!reslist[iLib].empty()) {
453 SimpleLookup(reslist[iLib][0], iCurrentIndex);
455 for (std::vector < gchar *>::iterator i = reslist[iLib].begin();
456 i != reslist[iLib].end(); ++i) {
457 oStarDict->ResultsListInsertLast(*i);
462 oStarDict->ResultsReScroll();
464 oStarDict->ShowProgressIndicator(false);
465 oStarDict->DestroyLookupProgressDialog(dialog);
469 Library::Library(MStarDict *mStarDict):Libs(NULL, FALSE, 0, 0)
471 oStarDict = mStarDict;
472 iCurrentIndex = NULL;
478 g_free(iCurrentIndex);