Various changes to preferences object, file loading, and error logging.
[jben.git] / jben.cpp
blob31cdb60b1a6651bc8840ede5ebb3e9816081d9d4
1 /*
2 Project: J-Ben
3 Author: Paul Goins
4 Website: http://www.vultaire.net/software/jben/
5 License: GNU General Public License (GPL) version 2
6 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
8 File: jben.cpp
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>
24 #include "jben.h"
25 #include "wdict.h"
26 #include "kdict.h"
27 #include "encoding_convert.h"
28 #include "errorlog.h"
30 #include <cstdlib>
31 #ifndef __WXMSW__
32 #include <locale.h>
33 #endif
35 JBen *jben;
37 /* The application entry point */
38 IMPLEMENT_APP(JBen)
40 void ErrorLogDisplayFunc(ELType t, const string& message, void *srcObj) {
41 switch(t) {
42 case EL_Error:
43 #ifdef DEBUG
44 cout << "[Error] " << message << endl;
45 #endif
46 wxMessageBox(utfconv_mw(message), _T("Error"),
47 wxOK | wxICON_ERROR, (wxWindow*)srcObj);
48 break;
49 case EL_Warning:
50 #ifdef DEBUG
51 cout << "[Warning] " << message << endl;
52 #endif
53 wxMessageBox(utfconv_mw(message), _T("Warning"),
54 wxOK | wxICON_ERROR, (wxWindow*)srcObj);
55 break;
56 case EL_Info:
57 #ifdef DEBUG
58 cout << "[Info] " << message << endl;
59 #endif
60 wxMessageBox(utfconv_mw(message), _T("Info"),
61 wxOK | wxICON_ERROR, (wxWindow*)srcObj);
62 break;
63 case EL_Silent:
64 #ifdef DEBUG
65 cout << "[Silent] " << message << endl;
66 #endif
67 /* do nothing */
68 break;
72 bool JBen::OnInit() {
73 jben = this;
74 kanjiList = (KanjiList *)NULL;
75 vocabList = (VocabList *)NULL;
76 gui = (FrameMainGUI *)NULL;
78 /* the below -might- help on win32 systems, but for now is unused. */
79 #if 0
80 #ifndef __WXMSW__
81 setlocale(LC_ALL, "");
82 #endif
83 #endif
85 /* Start our random number generator */
86 srand(time(NULL));
87 for(int i=0;i<50;i++) rand(); /* On some platforms I've seen rand() behave
88 fairly predictably for the first iteration
89 or so. That's why I spin off a few
90 iterations of rand() before really using
91 it. */
93 /* Various initialization */
94 InitUTFConv();
95 el.RegDisplayFunc(ErrorLogDisplayFunc);
97 /* Dictionary loading, etc., depends on our config file. */
98 Preferences *prefs = Preferences::Get();
99 if(!prefs) {
100 /* This -should- never occur now. */
101 fprintf(stderr, "Could not create preferences object. FATAL ERROR!\n\n");
102 return false;
105 const KDict* kd = KDict::Get();
106 const WDict* wd = WDict::Get();
108 if(wd->MainDataLoaded()) {
109 vocabList = new VocabList();
110 vocabList->AddList(
111 utfconv_mw(prefs->GetSetting("vocablist")));
113 else vocabList = NULL;
114 if(kd->MainDataLoaded()) {
115 kanjiList = new KanjiList(kd->GetHashTable());
116 kanjiList->AddFromString(
117 utfconv_mw(prefs->GetSetting("kanjilist")));
119 else kanjiList = NULL;
121 gui = new FrameMainGUI();
122 gui->Show(true);
123 SetTopWindow(gui);
125 return true;
128 int JBen::OnExit() {
129 #ifdef DEBUG
130 printf("JBen::OnExit being processed...\n");
131 #endif
132 KDict::Destroy();
133 WDict::Destroy();
134 Preferences::Destroy();
135 if(kanjiList) delete kanjiList;
136 if(vocabList) delete vocabList;
137 DestroyUTFConv();
138 #ifdef DEBUG
139 printf("Terminating program.\n");
140 #endif
141 return 0;