#include <map>
#include "libwrapper.hpp"
-
+#include "mstardict.hpp"
static std::string xdxf2text(const char *p)
{
p += sec_size;
}
-
return res;
}
-void Library::SimpleLookup(const string &str, TSearchResultList& res_list)
-{
- glong idx, idx_suggest;
- res_list.reserve(ndicts());
- for (gint idict=0; idict<ndicts(); ++idict)
- if (SimpleLookupWord(str.c_str(), idx, idx_suggest, (size_t) idict, 0))
- res_list.push_back(
- TSearchResult(dict_name(idict),
- poGetWord(idx, idict, 0),
- parse_data(poGetOrigWordData(idx, idict))));
+void Library::ListWords(CurrentIndex* iIndex)
+{
+ CurrentIndex *iCurrent = (CurrentIndex*)g_memdup(iIndex, sizeof(CurrentIndex)*query_dictmask.size());
+
+ pMStarDict->ResultsListClear();
+
+ int iWordCount=0;
+ const gchar * poCurrentWord = poGetCurrentWord(iIndex, query_dictmask, 0);
+ if (poCurrentWord) {
+ pMStarDict->ResultsListInsertLast(poCurrentWord);
+ iWordCount++;
+
+ while (iWordCount < 30 && (poCurrentWord = poGetNextWord(NULL, iIndex, query_dictmask, 0))) {
+ pMStarDict->ResultsListInsertLast(poCurrentWord);
+ iWordCount++;
+ }
+ }
+ pMStarDict->ReScroll();
+
+ if (iCurrent)
+ g_free (iCurrent);
+}
+
+bool Library::BuildResultData(std::vector<InstantDictIndex> &dictmask, const char* sWord, CurrentIndex *iIndex, int iLib, TSearchResultList& res_list)
+{
+ int iRealLib;
+ bool bFound = false, bLookupWord = false, bLookupSynonymWord = false;
+ gint nWord=0, count=0, i=0, j=0;
+
+ iRealLib = dictmask[iLib].index;
+
+ bLookupWord = LookupWord(sWord, iIndex[iLib].idx, iIndex[iLib].idx_suggest, iRealLib, 0);
+ if (!bLookupWord)
+ bLookupWord = LookupSimilarWord(sWord, iIndex[iLib].idx, iIndex[iLib].idx_suggest, iRealLib, 0);
+ if (!bLookupWord)
+ bLookupWord = SimpleLookupWord(sWord, iIndex[iLib].idx, iIndex[iLib].idx_suggest, iRealLib, 0);
+
+ bLookupSynonymWord = LookupSynonymWord(sWord, iIndex[iLib].synidx, iIndex[iLib].synidx_suggest, iRealLib, 0);
+ if (!bLookupSynonymWord)
+ bLookupSynonymWord = LookupSynonymSimilarWord(sWord, iIndex[iLib].synidx, iIndex[iLib].synidx_suggest, iRealLib, 0);
+ if (!bLookupSynonymWord)
+ bLookupSynonymWord = SimpleLookupSynonymWord(sWord, iIndex[iLib].synidx, iIndex[iLib].synidx_suggest, iRealLib, 0);
+
+ g_debug ("bookname: %s, iLib: %d, iRealLib: %d, str: %s", dict_name(iLib).c_str(), iLib, iRealLib, sWord);
+
+ if (bLookupWord || bLookupSynonymWord) {
+ if (bLookupWord)
+ nWord++;
+
+ if (bLookupSynonymWord)
+ nWord+=GetOrigWordCount(iIndex[iLib].synidx, iRealLib, false);
+
+ if (bLookupWord) {
+ count = GetOrigWordCount(iIndex[iLib].idx, iRealLib, true);
+ for (i=0;i<count;i++) {
+ res_list.push_back(TSearchResult(dict_name(iLib),
+ poGetWord(iIndex[iLib].idx, iRealLib, 0),
+ parse_data(poGetOrigWordData(iIndex[iLib].idx+i, iRealLib))));
+ }
+ i = 1;
+ } else {
+ i = 0;
+ }
+ for (j = 0; i < nWord; i++, j++) {
+ res_list.push_back(TSearchResult(dict_name(iLib),
+ poGetWord(iIndex[iLib].synidx+j, iRealLib, 0),
+ parse_data(poGetOrigWordData(iIndex[iLib].synidx+j, iRealLib))));
+ }
+
+ bFound = true;
+ }
+
+ return bFound;
+}
+
+bool Library::SimpleLookup(const gchar* sWord, CurrentIndex* piIndex)
+{
+ CurrentIndex *iIndex;
+ TSearchResultList results;
+ bool bFound = false;
+
+ if (!piIndex)
+ iIndex = (CurrentIndex *)g_malloc(sizeof(CurrentIndex) * query_dictmask.size());
+ else
+ iIndex = piIndex;
+
+ for (size_t iLib=0; iLib<query_dictmask.size(); iLib++) {
+ bFound = BuildResultData(query_dictmask, sWord, iIndex, iLib, results);
+ }
+
+ if (!piIndex)
+ g_free(iIndex);
+
+ return bFound;
}
void Library::LookupWithFuzzy(const string &str, TSearchResultList& res_list)
for (gchar **p=fuzzy_res, **end=fuzzy_res+MAXFUZZY;
p!=end && *p; ++p) {
- SimpleLookup(*p, res_list);
+// SimpleLookup(*p, res_list);
g_free(*p);
}
}
void Library::LookupWithRule(const string &str, TSearchResultList& res_list)
{
- std::vector<gchar *> match_res((MAX_MATCH_ITEM_PER_LIB) * ndicts());
+ std::vector<gchar *> match_res((MAX_MATCH_ITEM_PER_LIB) * query_dictmask.size());
gint nfound=Libs::LookupWithRule(str.c_str(), &match_res[0], query_dictmask);
if (!nfound)
return;
for (gint i=0; i<nfound; ++i) {
- SimpleLookup(match_res[i], res_list);
+// SimpleLookup(match_res[i], res_list);
g_free(match_res[i]);
}
}
void Library::LookupData(const string &str, TSearchResultList& res_list)
{
bool cancel = false;
- std::vector<gchar *> drl[ndicts()];
+ std::vector<gchar *> drl[query_dictmask.size()];
if (!Libs::LookupData(str.c_str(), drl, NULL, NULL, &cancel, query_dictmask))
return;
- for (int idict=0; idict<ndicts(); ++idict)
- for (std::vector<gchar *>::size_type j=0; j<drl[idict].size(); ++j) {
- SimpleLookup(drl[idict][j], res_list);
- g_free(drl[idict][j]);
+ for (size_t iLib=0; iLib<query_dictmask.size(); iLib++)
+ for (std::vector<gchar *>::size_type j=0; j<drl[iLib].size(); ++j) {
+// SimpleLookup(drl[iLib][j], res_list);
+ g_free(drl[iLib][j]);
}
}
-bool Library::process_phrase(const char *loc_str, TSearchResultList &res_list)
+Library::Library () : Libs (NULL, FALSE, 0, 0)
{
- if (NULL==loc_str)
- return true;
-
- std::string query;
-
- gsize bytes_read;
- gsize bytes_written;
- GError *err=NULL;
- char *str=NULL;
- if (!utf8_input)
- str=g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, &err);
- else
- str=g_strdup(loc_str);
-
- if (NULL==str) {
- fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str);
- fprintf(stderr, "%s\n", err->message);
- g_error_free(err);
- return false;
- }
-
- if (str[0]=='\0')
- return true;
-
- switch (analyse_query(str, query)) {
- case qtFUZZY:
- g_debug ("FUZZY");
- LookupWithFuzzy(query, res_list);
- break;
- case qtREGEX:
- g_debug ("REGEX");
- LookupWithRule(query, res_list);
- break;
- case qtSIMPLE:
- g_debug ("SIMPLE");
- SimpleLookup(str, res_list);
- if (res_list.empty())
- LookupWithFuzzy(str, res_list);
- break;
- case qtDATA:
- g_debug ("DATA");
- LookupData(query, res_list);
- break;
- default:
- g_debug ("DEFAULT");
- /*nothing*/;
- }
-
- g_free(str);
- return true;
+ iCurrentIndex = NULL;
}
+Library::~Library ()
+{
+ if (iCurrentIndex)
+ g_free (iCurrentIndex);
+}
#include "file.hpp"
#include "mstardict.hpp"
+MStarDict *pMStarDict;
+
enum {
- INDEX_COLUMN = 0,
- BOOKNAME_COLUMN,
DEF_COLUMN,
N_COLUMNS
};
-
MStarDict::MStarDict ()
{
label_widget = NULL;
results_widget = NULL;
results_view = NULL;
+ results_view_scroll = NULL;
/* create list of ressults */
results_list = gtk_list_store_new (N_COLUMNS,
- G_TYPE_INT, /* INDEX_COLUMN */
- G_TYPE_STRING, /* BOOKNAME_COLUMN */
G_TYPE_STRING); /* DEF_COLUMN */
- /* set sorting of resuslts */
- gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (results_list),
- DEF_COLUMN,
- GTK_SORT_ASCENDING);
-
/* initialize stardict library */
- lib = new Library (true, true);
+ oLibs = new Library ();
}
MStarDict::~MStarDict ()
g_object_unref (results_list);
/* deinitialize stardict library */
- delete lib;
+ delete oLibs;
}
gboolean
-MStarDict::on_results_view_selection_changed (GtkTreeSelection *selection,
+MStarDict::onResultsViewSelectionChanged (GtkTreeSelection *selection,
MStarDict *mStarDict)
{
GtkTreeModel *model;
GtkTreeIter iter;
char *bookname, *def, *exp;
- gint selected = 0;
+ const gchar *sWord;
+ bool bFound = false;
if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
/* unselect selected rows */
gtk_tree_selection_unselect_all (selection);
- gtk_tree_model_get (model, &iter, INDEX_COLUMN, &selected, -1);
+
+ gtk_tree_model_get (model, &iter, DEF_COLUMN, &sWord, -1);
+
+ /* clear previous search results */
+ mStarDict->results.clear();
+
+ for (size_t iLib=0; iLib<mStarDict->oLibs->query_dictmask.size(); iLib++) {
+ bFound = mStarDict->oLibs->BuildResultData(mStarDict->oLibs->query_dictmask,
+ sWord,
+ mStarDict->oLibs->iCurrentIndex,
+ iLib,
+ mStarDict->results);
+ }
bookname = g_markup_printf_escaped ("<span color=\"dimgray\" size=\"x-small\">%s</span>",
- mStarDict->result_list[selected].bookname.c_str());
+ mStarDict->results[0].bookname.c_str());
def = g_markup_printf_escaped ("<span color=\"darkred\" weight=\"heavy\" size=\"large\">%s</span>",
- mStarDict->result_list[selected].def.c_str());
- exp = g_strdup (mStarDict->result_list[selected].exp.c_str());
+ mStarDict->results[0].def.c_str());
+ exp = g_strdup (mStarDict->results[0].exp.c_str());
/* create translation window */
- mStarDict->create_translation_window (bookname, def, exp);
+ mStarDict->CreateTranslationWindow (bookname, def, exp);
g_free (bookname);
g_free (def);
}
gboolean
-MStarDict::on_search_entry_changed (GtkEditable *editable,
+MStarDict::onSearchEntryChanged (GtkEditable *editable,
MStarDict *mStarDict)
{
GtkTreeSelection *selection;
- const gchar *search;
- GtkTreeIter iter;
+ const gchar *sWord;
+ bool bFound = false;
+ std::string query;
- search = gtk_entry_get_text (GTK_ENTRY (editable));
+ sWord = gtk_entry_get_text (GTK_ENTRY (editable));
- if (strcmp (search, "") == 0) {
+ if (strcmp (sWord, "") == 0) {
gtk_widget_show (mStarDict->label_widget);
gtk_widget_hide (mStarDict->results_widget);
} else {
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (mStarDict->results_view));
gtk_tree_selection_set_mode (selection, GTK_SELECTION_NONE);
- /* clear previous search results */
- mStarDict->result_list.clear();
- gtk_list_store_clear (mStarDict->results_list);
-
/* unselect rows */
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (mStarDict->results_view));
gtk_tree_selection_unselect_all (selection);
- /* fill list with new results */
- mStarDict->lib->process_phrase(search, mStarDict->result_list);
- if (!mStarDict->result_list.empty()) {
- for (size_t i = 0; i < mStarDict->result_list.size(); ++i) {
- gtk_list_store_append (mStarDict->results_list, &iter);
- gtk_list_store_set (mStarDict->results_list,
- &iter,
- INDEX_COLUMN, i,
- BOOKNAME_COLUMN, mStarDict->result_list[i].bookname.c_str(),
- DEF_COLUMN, mStarDict->result_list[i].def.c_str(),
- -1);
+ switch (analyse_query(sWord, query)) {
+ case qtFUZZY:
+ g_debug ("FUZZY");
+// oLibs->LookupWithFuzzy(query, res_list);
+ break;
+ case qtREGEX:
+ g_debug ("REGEX");
+// oLibs->LookupWithRule(query, res_list);
+ break;
+ case qtSIMPLE:
+ g_debug ("SIMPLE");
+ bFound = mStarDict->oLibs->SimpleLookup(sWord, mStarDict->oLibs->iCurrentIndex);
+ if (!bFound) {
+ /* suggested */
+ }
+ mStarDict->oLibs->ListWords(mStarDict->oLibs->iCurrentIndex);
+ break;
+ case qtDATA:
+ g_debug ("DATA");
+// oLibs->LookupData(query, res_list);
+ break;
+ default:
+ g_debug ("DEFAULT");
+ /*nothing*/;
}
- }
/* unselect selected rows */
gtk_tree_selection_unselect_all (selection);
};
void
-MStarDict::load_dictionaries ()
+MStarDict::LoadDictionaries ()
{
strlist_t dicts_dir_list;
strlist_t order_list;
dicts_dir_list.push_back (std::string ("/home/user/MyDocs/mstardict"));
for_each_file(dicts_dir_list, ".ifo", order_list, disable_list, GetAllDictList(load_list));
- lib->load(load_list);
+ oLibs->load(load_list);
- lib->query_dictmask.clear();
+ oLibs->query_dictmask.clear();
for (std::list<std::string>::iterator i = load_list.begin(); i != load_list.end(); ++i) {
size_t iLib;
- if (lib->find_lib_by_filename(i->c_str(), iLib)) {
+ if (oLibs->find_lib_by_filename(i->c_str(), iLib)) {
InstantDictIndex instance_dict_index;
instance_dict_index.type = InstantDictType_LOCAL;
instance_dict_index.index = iLib;
- lib->query_dictmask.push_back(instance_dict_index);
+ oLibs->query_dictmask.push_back(instance_dict_index);
}
}
+
+ if (oLibs->iCurrentIndex)
+ g_free (oLibs->iCurrentIndex);
+ oLibs->iCurrentIndex = (CurrentIndex *)g_malloc(sizeof(CurrentIndex) * oLibs->query_dictmask.size());
+
}
void
-MStarDict::create_translation_window (const gchar *bookname,
+MStarDict::CreateTranslationWindow (const gchar *bookname,
const gchar *def,
const gchar *exp)
{
}
void
-MStarDict::create_main_window ()
+MStarDict::CreateMainWindow ()
{
HildonProgram *program = NULL;
- GtkWidget *window, *alignment, *vbox, *pannable;
+ GtkWidget *window, *alignment, *vbox;
GtkCellRenderer *renderer;
GtkTreeSelection *selection;
- GdkColor style_color;
/* hildon program */
program = hildon_program_get_instance ();
gtk_box_pack_start (GTK_BOX (vbox), results_widget, TRUE, TRUE, 0);
/* pannable for tree view */
- pannable = hildon_pannable_area_new ();
- gtk_container_add (GTK_CONTAINER (results_widget), pannable);
+ results_view_scroll = hildon_pannable_area_new ();
+ gtk_container_add (GTK_CONTAINER (results_widget), results_view_scroll);
/* result tree view */
results_view = hildon_gtk_tree_view_new (HILDON_UI_MODE_EDIT);
gtk_tree_view_set_model (GTK_TREE_VIEW (results_view),
GTK_TREE_MODEL (results_list));
- gtk_container_add (GTK_CONTAINER (pannable), results_view);
+ gtk_container_add (GTK_CONTAINER (results_view_scroll), results_view);
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (results_view));
g_signal_connect (selection, "changed",
- G_CALLBACK (on_results_view_selection_changed), this);
+ G_CALLBACK (onResultsViewSelectionChanged), this);
/* def column */
renderer = gtk_cell_renderer_text_new ();
NULL);
g_object_set (G_OBJECT (renderer), "xpad", 10, NULL);
- /* bookname column */
- renderer = gtk_cell_renderer_text_new ();
- gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (results_view),
- -1,
- "Bookname", renderer,
- "text", BOOKNAME_COLUMN,
- NULL);
-
- if (!gtk_style_lookup_color (GTK_WIDGET (label_widget)->style, "SecondaryTextColor",
- &style_color)) {
- gdk_color_parse ("grey", &style_color);
- }
- g_object_set (G_OBJECT (renderer),
- "xalign", 1.0,
- "width-chars", 10,
- "foreground-gdk", &style_color,
- "foreground-set", TRUE,
- "size", 12000,
- "ellipsize", PANGO_ELLIPSIZE_END,
- "ellipsize-set", TRUE,
- NULL);
-
/* search entry */
search = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
gtk_box_pack_end (GTK_BOX (vbox), search, FALSE, TRUE, 0);
g_signal_connect (search, "changed",
- G_CALLBACK (on_search_entry_changed), this);
+ G_CALLBACK (onSearchEntryChanged), this);
/* window signals */
g_signal_connect (G_OBJECT (window), "destroy",
gtk_widget_grab_focus (GTK_WIDGET (search));
}
+void
+MStarDict::ResultsListClear()
+{
+ gtk_list_store_clear (results_list);
+}
+
+void
+MStarDict::ResultsListInsertLast(const gchar *word)
+{
+ GtkTreeIter iter;
+ gtk_list_store_append (results_list, &iter);
+ gtk_list_store_set (results_list, &iter, DEF_COLUMN, word, -1);
+}
+
+void
+MStarDict::ReScroll()
+{
+ hildon_pannable_area_scroll_to (HILDON_PANNABLE_AREA (results_view_scroll), -1, 0);
+}
+
int
main (int argc, char **argv)
{
/* create main window */
MStarDict mStarDict;
- mStarDict.create_main_window ();
+ pMStarDict = &mStarDict;
+ mStarDict.CreateMainWindow();
/* load all dictionaries */
- mStarDict.load_dictionaries ();
+ mStarDict.LoadDictionaries();
gtk_main ();
return 0;