Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/plugin/PluginDictionary.cpp

Issue 10897028: Create a shared dictionary class for plugin and engine (Closed)
Patch Set: Created June 7, 2013, 12:42 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #include "PluginStdAfx.h"
2
3 #include "PluginDictionary.h"
4 #include "PluginClient.h"
5 #include "PluginSettings.h"
6 #include "PluginMutex.h"
7
8
9 class CPluginDictionaryLock : public CPluginMutex
10 {
11 public:
12 CPluginDictionaryLock() : CPluginMutex("DictionaryFile", PLUGIN_ERROR_MUTEX_DI CTIONARY_FILE) {}
13 ~CPluginDictionaryLock() {}
14 };
15
16
17 CPluginDictionary* CPluginDictionary::s_instance = NULL;
18
19 CComAutoCriticalSection CPluginDictionary::s_criticalSectionDictionary;
20
21
22 CPluginDictionary::CPluginDictionary(bool forceCreate) : m_dictionaryLanguage("e n")
23 {
24 DEBUG_GENERAL("*** Initializing dictionary")
25
26 m_dictionaryConversions[L"UPDATE"] = L"MENU_UPDATE";
27 m_dictionaryConversions[L"ABOUT"] = L"MENU_ABOUT";
28 m_dictionaryConversions[L"FAQ"] = L"MENU_FAQ";
29 m_dictionaryConversions[L"FEEDBACK"] = L"MENU_FEEDBACK";
30 m_dictionaryConversions[L"INVITE_FRIENDS"] = L"MENU_INVITE_FRIENDS";
31 m_dictionaryConversions[L"UPGRADE"] = L"MENU_UPGRADE";
32 m_dictionaryConversions[L"SETTINGS"] = L"MENU_SETTINGS";
33 m_dictionaryConversions[L"ENABLE"] = L"MENU_ENABLE";
34 m_dictionaryConversions[L"DISABLE"] = L"MENU_DISABLE";
35 m_dictionaryConversions[L"DISABLE_ON"] = L"MENU_DISABLE_ON";
36
37 m_dictionaryConversions[L"YES"] = L"GENERAL_YES";
38 m_dictionaryConversions[L"NO"] = L"GENERAL_NO";
39 m_dictionaryConversions[L"CANCEL"] = L"GENERAL_CANCEL";
40
41 CString lang = CPluginSettings::GetSystemLanguage();
42
43 bool isExisting = true;
44 {
45 CPluginDictionaryLock lock;
46 if (lock.IsLocked())
47 {
48 std::ifstream is;
49 is.open(CPluginSettings::GetDataPath(DICTIONARY_INI_FILE), std::ios_base:: in);
50 if (!is.is_open())
51 {
52 DEBUG_DICTIONARY("Dictionary::Constructor - Exists:no")
53 isExisting = false;
54 }
55 else
56 {
57 DEBUG_DICTIONARY("Dictionary::Constructor - Exists:yes")
58 is.close();
59 }
60 }
61 }
62
63 if (!isExisting || forceCreate)
64 {
65 Create(forceCreate);
66 }
67 }
68
69
70 CPluginDictionary::~CPluginDictionary()
71 {
72 s_criticalSectionDictionary.Lock();
73 {
74 s_instance = NULL;
75 }
76 s_criticalSectionDictionary.Unlock();
77 }
78
79
80 CPluginDictionary* CPluginDictionary::GetInstance(bool forceCreate)
81 {
82 CPluginDictionary* instance = NULL;
83
84 s_criticalSectionDictionary.Lock();
85 {
86 if (!s_instance)
87 {
88 DEBUG_DICTIONARY("Dictionary::GetInstance - creating")
89 s_instance = new CPluginDictionary(forceCreate);
90 }
91
92 instance = s_instance;
93 }
94 s_criticalSectionDictionary.Unlock();
95
96 return instance;
97 }
98
99
100 bool CPluginDictionary::IsLanguageSupported(const CString& lang)
101 {
102 bool hasLanguage = false;
103
104 CPluginDictionaryLock lock;
105 if (lock.IsLocked())
106 {
107 CPluginIniFileW iniFile(CPluginSettings::GetDataPath(DICTIONARY_INI_FILE));
108
109 iniFile.Read();
110
111 hasLanguage = iniFile.HasSection(lang);
112 }
113
114 return hasLanguage;
115 }
116
117
118 void CPluginDictionary::SetLanguage(const CString& lang)
119 {
120 DEBUG_GENERAL(L"*** Loading dictionary:" + CPluginSettings::GetDataPath(DICTIO NARY_INI_FILE))
121
122 CPluginDictionaryLock lock;
123 if (lock.IsLocked())
124 {
125 CPluginIniFileW iniFile(CPluginSettings::GetDataPath(DICTIONARY_INI_FILE));
126
127 if (!iniFile.Read())
128 {
129 DEBUG_ERROR_LOG(iniFile.GetLastError(), PLUGIN_ERROR_DICTIONARY, PLUGIN_ER ROR_DICTIONARY_READ_FILE, "Dictionary::SetLanguage - Read")
130 return;
131 }
132
133 s_criticalSectionDictionary.Lock();
134 {
135 if (iniFile.HasSection(lang))
136 {
137 m_dictionaryLanguage = lang;
138
139 m_dictionary = iniFile.GetSectionData(lang);
140
141 CString dicEl;
142 dicEl.Format(L"%d", m_dictionary.size());
143
144 DEBUG_GENERAL(L"*** Using dictionary section [" + lang + "] - " + dicEl + L" elements")
145 }
146 else
147 {
148 m_dictionary = iniFile.GetSectionData(L"en");
149 m_dictionaryLanguage = L"en";
150
151 CString dicEl;
152 dicEl.Format(L"%d", m_dictionary.size());
153
154 DEBUG_GENERAL(L"*** Using dictionary section [en] instead of [" + lang + "] - " + dicEl + L" elements")
155 }
156
157 // Dictionary conversions
158 for (std::map<CString,CString>::iterator it = m_dictionaryConversions.begi n(); it != m_dictionaryConversions.end(); ++it)
159 {
160 CPluginIniFileW::TSectionData::iterator itDic = m_dictionary.find(it->fi rst);
161 if (itDic != m_dictionary.end())
162 {
163 m_dictionary[it->second] = itDic->second;
164 }
165 }
166
167 #ifdef ENABLE_DEBUG_DICTIONARY
168 for (CPluginIniFileW::TSectionData::iterator it = m_dictionary.begin(); it != m_dictionary.end(); ++it)
169 {
170 DEBUG_DICTIONARY("- " + it->first + " -> " + it->second)
171 }
172 #endif
173 }
174 s_criticalSectionDictionary.Unlock();
175 }
176 }
177
178
179 CString CPluginDictionary::Lookup(const CString& key)
180 {
181 CString value = key;
182
183 s_criticalSectionDictionary.Lock();
184 {
185 CPluginIniFileW::TSectionData::iterator it = m_dictionary.find(key);
186 if (it != m_dictionary.end())
187 {
188 value = it->second;
189 }
190 #ifdef DEBUG_ERROR_LOG
191 else
192 {
193 DWORD dwErrorCode = 0;
194
195 dwErrorCode |= m_dictionaryLanguage.GetAt(0) << 24;
196 dwErrorCode |= m_dictionaryLanguage.GetAt(1) << 16;
197 dwErrorCode |= key.GetAt(0) << 8;
198 dwErrorCode |= key.GetAt(1);
199
200 DEBUG_ERROR_LOG(dwErrorCode, PLUGIN_ERROR_DICTIONARY, PLUGIN_ERROR_DICTION ARY_LOOKUP, L"Dictionary::Lookup - " + key)
201 }
202 #endif
203 }
204 s_criticalSectionDictionary.Unlock();
205
206 DEBUG_DICTIONARY("Dictionary::Lookup key:" + key + " value:" + value)
207
208 return value;
209 }
210
211
212 void CPluginDictionary::Create(bool forceCreate)
213 {
214 DEBUG_GENERAL(L"*** Creating dictionary:" + CPluginSettings::GetDataPath(DICTI ONARY_INI_FILE))
215
216 CPluginDictionaryLock lock;
217 if (lock.IsLocked())
218 {
219 CPluginIniFileW iniFile(CPluginSettings::GetDataPath(DICTIONARY_INI_FILE));
220
221 CPluginSettings* settings = CPluginSettings::GetInstance();
222
223 if (forceCreate)
224 {
225 }
226 else if (iniFile.Exists() || !settings->IsMainProcess())
227 {
228 return;
229 }
230
231 int dictionaryVersion = 1;
232
233 s_criticalSectionDictionary.Lock();
234 {
235 m_dictionary.clear();
236
237 #if (defined PRODUCT_ADBLOCKPLUS)
238
239 // Popup menu
240 m_dictionary["MENU_UPDATE"] = "Update Adblock Plus to newest version";
241 m_dictionary["MENU_ABOUT"] = "About Adblock Plus";
242 m_dictionary["MENU_ACTIVATE"] = "Activate Adblock Plus";
243 m_dictionary["MENU_FAQ"] = "Frequently Asked Questions";
244 m_dictionary["MENU_FEEDBACK"] = "Feedback";
245 m_dictionary["MENU_INVITE_FRIENDS"] = "Invite friends";
246 m_dictionary["MENU_SETTINGS"] = "Settings";
247 m_dictionary["MENU_ENABLE"] = "Enable Adblock Plus";
248 m_dictionary["MENU_DISABLE"] = "Disable Adblock Plus";
249 m_dictionary["MENU_DISABLE_ON"] = "Disable Adblock Plus on...";
250
251 // Update dialog
252 m_dictionary["UPDATE_TITLE"] = "Update Adblock Plus";
253 m_dictionary["UPDATE_NEW_VERSION_EXISTS"] = "A new version of Adblock Plus is available";
254 m_dictionary["UPDATE_DO_YOU_WISH_TO_DOWNLOAD"] = "Do you wish to download it now?";
255
256 // Download update dialog
257 m_dictionary["DOWNLOAD_UPDATE_TITLE"] = "Download Adblock Plus";
258 m_dictionary["DOWNLOAD_UPDATE_BUTTON"] = "Update";
259 m_dictionary["DOWNLOAD_PLEASE_WAIT"] = "Please wait...";
260 m_dictionary["DOWNLOAD_UPDATE_ERROR_TEXT"] = "Error downloading installer" ;
261 m_dictionary["DOWNLOAD_UPDATE_SUCCESS_TEXT"] = "If you choose to update Ad block Plus, your Internet Explorer will close before installation";
262
263 // Various dialogs
264 m_dictionary["ERROR_STATUS_BAR_DISABLED"] = "The plugin menu is located in the statusbar, would you like to enable Internet Explorer's statusbar?";
265 m_dictionary["ERROR_STATUS_BAR_DISABLED_TITLE"] = "Enable status bar?";
266 m_dictionary["ERROR_CAN_NOT_ENABLE_STATUS_BAR"] = "The plugin menu is loca ted in the statusbar. Please enable it under View->Toolbars->Status bar";
267 m_dictionary["ERROR_CAN_NOT_ENABLE_STATUS_BAR_TITLE"] = "Please enable sta tus bar";
268
269 #endif
270 // General texts
271 m_dictionary["GENERAL_YES"] = "Yes";
272 m_dictionary["GENERAL_NO"] = "No";
273 m_dictionary["GENERAL_CANCEL"] = "Cancel";
274 m_dictionary["GENERAL_CLOSE"] = "Close";
275
276 iniFile.UpdateSection("en", m_dictionary);
277 }
278 s_criticalSectionDictionary.Unlock();
279
280 if (iniFile.Write())
281 {
282 CPluginSettings* settings = CPluginSettings::GetInstance();
283
284 settings->SetValue(SETTING_DICTIONARY_VERSION, dictionaryVersion);
285 settings->Write();
286 }
287 else
288 {
289 DEBUG_ERROR_LOG(iniFile.GetLastError(), PLUGIN_ERROR_DICTIONARY, PLUGIN_ER ROR_DICTIONARY_CREATE_FILE, L"Dictionary::Create - Write")
290 }
291 #ifdef PRODUCT_ADBLOCKPLUS
292 // Delete old
293 ::DeleteFile(CPluginSettings::GetDataPath(L"dictionary.ini"));
294 #endif
295 }
296 }
OLDNEW

Powered by Google App Engine
This is Rietveld