ce712ccb828d0ead753310d63d2338dd98f5ef22
[mstardict] / src / dictmngr.cpp
1 /*
2  *  MStarDict - International dictionary for Maemo.
3  *  Copyright (C) 2010 Roman Moravcik
4  *
5  *  base on code of stardict:
6  *  Copyright (C) 2003-2007 Hu Zheng <huzheng_001@163.com>
7  *
8  *  based on code of sdcv:
9  *  Copyright (C) 2005-2006 Evgeniy <dushistov@mail.ru>
10  *
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.
15  *
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.
20  *
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
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include <list>
31
32 #include <glib.h>
33 #include <glib/gi18n.h>
34
35 #include <gtk/gtk.h>
36 #include <hildon/hildon.h>
37
38 #include "conf.hpp"
39 #include "libwrapper.hpp"
40 #include "mstardict.hpp"
41 #include "dictmngr.hpp"
42
43 enum {
44     BOOKNAME_DICT_INFO_COLUMN,
45     FILENAME_DICT_INFO_COLUMN,
46     N_DICT_INFO_COLUMNS
47 };
48
49 class GetAllDictList {
50   public:
51     GetAllDictList(std::list < std::string > &dict_all_list_):dict_all_list(dict_all_list_) {
52     } void operator() (const std::string & url, bool disable) {
53         dict_all_list.push_back(url);
54     }
55   private:
56     std::list < std::string > &dict_all_list;
57 };
58
59 DictMngr::DictMngr(MStarDict *mStarDict)
60 {
61     oStarDict = mStarDict;
62 }
63
64 DictMngr::~DictMngr()
65 {
66 }
67
68 void
69 DictMngr::CreateDictMngrDialog()
70 {
71     GtkWidget *dialog, *selector;
72     GtkCellRenderer *renderer;
73     HildonTouchSelectorColumn *column;
74     GtkTreeModel *tree_model;
75     GtkTreeIter iter;
76     gboolean iter_valid = TRUE;
77     std::list < std::string > all_dict_list;
78     std::list < std::string > selected_dict_list;
79     GtkListStore *dict_list = NULL;
80
81     dict_list = gtk_list_store_new(N_DICT_INFO_COLUMNS,
82                                    G_TYPE_STRING,       /* bookname */
83                                    G_TYPE_STRING);      /* filename */
84
85     /* create dialog */
86     dialog = gtk_dialog_new();
87     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
88     gtk_window_set_title(GTK_WINDOW(dialog), _("Dictionaries"));
89     gtk_dialog_add_button(GTK_DIALOG(dialog), "OK", GTK_RESPONSE_ACCEPT);
90     gtk_window_set_default_size(GTK_WINDOW(dialog), -1, 400);
91
92     /* dictionary selector */
93     selector = hildon_touch_selector_new();
94     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), selector);
95
96     renderer = gtk_cell_renderer_text_new();
97     g_object_set(G_OBJECT(renderer),
98                  "xpad", 10,
99                  "ellipsize", PANGO_ELLIPSIZE_END,
100                  "ellipsize-set", TRUE,
101                  NULL);
102
103     column =
104         hildon_touch_selector_append_column(HILDON_TOUCH_SELECTOR
105                                             (selector),
106                                             GTK_TREE_MODEL(dict_list),
107                                             renderer, "text", BOOKNAME_DICT_INFO_COLUMN, NULL);
108     hildon_touch_selector_column_set_text_column(column, 0);
109
110     /* fill list with all available dictionaries */
111     GetAllDictionaryList(all_dict_list);
112     for (std::list < std::string >::iterator i = all_dict_list.begin();
113          i != all_dict_list.end(); ++i) {
114         DictInfo dictinfo;
115
116         dictinfo.load_from_ifo_file(i->c_str(), 0);
117         gtk_list_store_append(dict_list, &iter);
118         gtk_list_store_set(dict_list, &iter,
119                            BOOKNAME_DICT_INFO_COLUMN,
120                            dictinfo.bookname.c_str(), FILENAME_DICT_INFO_COLUMN, i->c_str(), -1);
121     }
122     g_object_unref(dict_list);
123
124     /* set selector mode to multiple */
125     hildon_touch_selector_set_column_selection_mode(HILDON_TOUCH_SELECTOR
126                                                     (selector),
127                                                     HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
128     hildon_touch_selector_unselect_all(HILDON_TOUCH_SELECTOR(selector), BOOKNAME_DICT_INFO_COLUMN);
129
130     /* select all load dictionaries */
131     tree_model =
132         hildon_touch_selector_get_model(HILDON_TOUCH_SELECTOR(selector), BOOKNAME_DICT_INFO_COLUMN);
133     for (iter_valid = gtk_tree_model_get_iter_first(tree_model, &iter);
134          iter_valid; iter_valid = gtk_tree_model_iter_next(tree_model, &iter)) {
135         const gchar *bookname;
136
137         gtk_tree_model_get(tree_model, &iter, BOOKNAME_DICT_INFO_COLUMN, &bookname, -1);
138         for (size_t iLib = 0; iLib < oStarDict->oLibs->query_dictmask.size(); iLib++) {
139             if (!strcmp(oStarDict->oLibs->dict_name(iLib).c_str(), bookname)) {
140                 hildon_touch_selector_select_iter(HILDON_TOUCH_SELECTOR
141                                                   (selector),
142                                                   BOOKNAME_DICT_INFO_COLUMN, &iter, FALSE);
143                 break;
144             }
145         }
146     }
147
148     /* show dialog */
149     gtk_widget_show_all(GTK_WIDGET(dialog));
150
151     /* run the dialog */
152     if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
153         GList *selected_dicts = NULL;
154
155         selected_dicts =
156             hildon_touch_selector_get_selected_rows(HILDON_TOUCH_SELECTOR
157                                                     (selector), BOOKNAME_DICT_INFO_COLUMN);
158         if (selected_dicts) {
159             GList *dict = selected_dicts;
160             const gchar *filename;
161
162             while (dict) {
163                 gtk_tree_model_get_iter(GTK_TREE_MODEL(tree_model), &iter,
164                                         (GtkTreePath *) (dict->data));
165                 gtk_tree_model_get(GTK_TREE_MODEL(tree_model), &iter,
166                                    FILENAME_DICT_INFO_COLUMN, &filename, -1);
167                 selected_dict_list.push_back(std::string(filename));
168                 dict = dict->next;
169             }
170             g_list_foreach(selected_dicts, (GFunc) gtk_tree_path_free, NULL);
171             g_list_free(selected_dicts);
172         }
173
174         if (oStarDict->oConf->SetStringList("/apps/maemo/mstardict/dict_list", selected_dict_list)) {
175             /* reload dictionaries */
176             ReLoadDictionaries(selected_dict_list);
177         }
178     }
179     gtk_widget_destroy(GTK_WIDGET(dialog));
180 }
181
182 void
183 DictMngr::GetAllDictionaryList(std::list < std::string > &dict_list)
184 {
185     strlist_t dicts_dir_list;
186     strlist_t order_list;
187     strlist_t disable_list;
188
189     /* dictionary directory */
190     dicts_dir_list.push_back(std::string("/home/user/MyDocs/mstardict"));
191     for_each_file(dicts_dir_list, ".ifo", order_list, disable_list, GetAllDictList(dict_list));
192 }
193
194 void
195 DictMngr::LoadDictionaries()
196 {
197     std::list < std::string > dict_list;
198
199     if (!oStarDict->oConf->GetStringList("/apps/maemo/mstardict/dict_list", dict_list)) {
200         GetAllDictionaryList(dict_list);
201         oStarDict->oConf->SetStringList("/apps/maemo/mstardict/dict_list", dict_list);
202     }
203
204     oStarDict->oLibs->load(dict_list);
205     oStarDict->oLibs->query_dictmask.clear();
206     for (std::list < std::string >::iterator i = dict_list.begin(); i != dict_list.end(); ++i) {
207         size_t iLib;
208         if (oStarDict->oLibs->find_lib_by_filename(i->c_str(), iLib)) {
209             InstantDictIndex instance_dict_index;
210             instance_dict_index.type = InstantDictType_LOCAL;
211             instance_dict_index.index = iLib;
212             oStarDict->oLibs->query_dictmask.push_back(instance_dict_index);
213         }
214     }
215
216     if (oStarDict->oLibs->iCurrentIndex)
217         g_free(oStarDict->oLibs->iCurrentIndex);
218     oStarDict->oLibs->iCurrentIndex =
219         (CurrentIndex *) g_malloc(sizeof(CurrentIndex) * oStarDict->oLibs->query_dictmask.size());
220
221     if (oStarDict->oLibs->query_dictmask.empty())
222         oStarDict->ShowNoDictionary(true);
223 }
224
225 void
226 DictMngr::ReLoadDictionaries(std::list < std::string > &dict_list)
227 {
228     oStarDict->oLibs->reload(dict_list, 0, 0);
229     oStarDict->oLibs->query_dictmask.clear();
230     for (std::list < std::string >::iterator i = dict_list.begin(); i != dict_list.end(); ++i) {
231         size_t iLib;
232         if (oStarDict->oLibs->find_lib_by_filename(i->c_str(), iLib)) {
233             InstantDictIndex instance_dict_index;
234             instance_dict_index.type = InstantDictType_LOCAL;
235             instance_dict_index.index = iLib;
236             oStarDict->oLibs->query_dictmask.push_back(instance_dict_index);
237         }
238     }
239
240     if (oStarDict->oLibs->iCurrentIndex)
241         g_free(oStarDict->oLibs->iCurrentIndex);
242     oStarDict->oLibs->iCurrentIndex =
243         (CurrentIndex *) g_malloc(sizeof(CurrentIndex) * oStarDict->oLibs->query_dictmask.size());
244
245     if (oStarDict->oLibs->query_dictmask.empty())
246         oStarDict->ShowNoDictionary(true);
247 }