aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/newview/lggautocorrect.cpp
diff options
context:
space:
mode:
authorPatrick Sapinski2010-08-24 03:04:20 -0400
committerMcCabe Maxsted2010-08-30 17:04:59 -0700
commitded1245db74ae4c97d174c5779f8572ee2f032fa (patch)
treef42c33f233ed68e715a0ac4c74ddc043f43a68da /linden/indra/newview/lggautocorrect.cpp
parentAdded debug setting UseLegacyChatLogsFolder for saving chat.txt and IM logs i... (diff)
downloadmeta-impy-ded1245db74ae4c97d174c5779f8572ee2f032fa.zip
meta-impy-ded1245db74ae4c97d174c5779f8572ee2f032fa.tar.gz
meta-impy-ded1245db74ae4c97d174c5779f8572ee2f032fa.tar.bz2
meta-impy-ded1245db74ae4c97d174c5779f8572ee2f032fa.tar.xz
added spellcheck + translation from Emerald Viewer. references to modularsystems.sl should be changed!
Diffstat (limited to '')
-rw-r--r--linden/indra/newview/lggautocorrect.cpp400
1 files changed, 400 insertions, 0 deletions
diff --git a/linden/indra/newview/lggautocorrect.cpp b/linden/indra/newview/lggautocorrect.cpp
new file mode 100644
index 0000000..41340fe
--- /dev/null
+++ b/linden/indra/newview/lggautocorrect.cpp
@@ -0,0 +1,400 @@
1/* Copyright (C) 2010 LordGregGreg Back
2
3 This is free software; you can redistribute it and/or modify it
4 under the terms of the GNU Lesser General Public License as
5 published by the Free Software Foundation; either version 2.1 of
6 the License, or (at your option) any later version.
7
8 This is distributed in the hope that it will be useful, but WITHOUT
9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
11 License for more details.
12
13 You should have received a copy of the GNU Lesser General Public
14 License along with the viewer; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
16 02111-1307, USA. */
17
18#include "llviewerprecompiledheaders.h"
19#include "lggautocorrect.h"
20#include "llsdserialize.h"
21#include "llboost.h"
22#include "llcontrol.h"
23#include "llviewercontrol.h"
24#include "llnotifications.h"
25
26LGGAutoCorrect* LGGAutoCorrect::sInstance;
27
28LGGAutoCorrect::LGGAutoCorrect()
29{
30 sInstance = this;
31 sInstance->loadFromDisk();
32}
33
34LGGAutoCorrect::~LGGAutoCorrect()
35{
36 sInstance = NULL;
37}
38
39LGGAutoCorrect* LGGAutoCorrect::getInstance()
40{
41 if(sInstance)return sInstance;
42 else
43 {
44 sInstance = new LGGAutoCorrect();
45 return sInstance;
46 }
47}
48void LGGAutoCorrect::save()
49{
50 saveToDisk(mAutoCorrects);
51}
52std::string LGGAutoCorrect::getFileName()
53{
54 std::string path=gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, "");
55
56 if (!path.empty())
57 {
58 path = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, "settings_autocorrect.xml");
59 }
60 return path;
61}
62std::string LGGAutoCorrect::getDefaultFileName()
63{
64 std::string path=gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, "");
65
66 if (!path.empty())
67 {
68 path = gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, "settings_autocorrect.xml");
69 }
70 return path;
71}
72LLSD LGGAutoCorrect::exportList(std::string listName)
73{
74 LLSD toReturn;
75 if(mAutoCorrects.has(listName))
76 {
77 toReturn["listName"]=listName;
78 toReturn["data"]=mAutoCorrects[listName]["data"];
79 toReturn["author"]=mAutoCorrects[listName]["author"];
80 toReturn["wordStyle"]=mAutoCorrects[listName]["wordStyle"];
81 toReturn["priority"]=mAutoCorrects[listName]["priority"];
82 }
83 return toReturn;
84}
85BOOL LGGAutoCorrect::addCorrectionList(LLSD newList)
86{
87 if(newList.has("listName"))
88 {
89 std::string name = newList["listName"];
90 //if(!mAutoCorrects.has(name)){
91 LLSD newPart;
92 newPart["data"]=newList["data"];
93 newPart["enabled"]=TRUE;
94 newPart["announce"]=FALSE;
95 newPart["author"]=newList["author"];
96 newPart["wordStyle"]=newList["wordStyle"];
97 newPart["priority"]=newList["priority"].asInteger();
98 llinfos << "adding new list with settings priority "<<newPart["priority"].asInteger() <<llendl;
99 mAutoCorrects[name]=newPart;
100
101 return TRUE;
102
103 }
104 return FALSE;
105}
106BOOL LGGAutoCorrect::removeCorrectionList(std::string listName)
107{
108 if(mAutoCorrects.has(listName))
109 {
110 mAutoCorrects.erase(listName);
111 return TRUE;
112 }
113 return FALSE;
114}
115BOOL LGGAutoCorrect::setListEnabled(std::string listName, BOOL enabled)
116{
117 if(mAutoCorrects.has(listName))
118 {
119 mAutoCorrects[listName]["enabled"]=enabled;
120 return TRUE;
121 }
122
123 return FALSE;
124}
125BOOL LGGAutoCorrect::setListAnnounceeState(std::string listName, BOOL announce)
126{
127
128
129 if(mAutoCorrects.has(listName))
130 {
131 mAutoCorrects[listName]["announce"]=announce;
132 return TRUE;
133 }
134 return FALSE;
135}
136BOOL LGGAutoCorrect::setListStyle(std::string listName, BOOL announce)
137{
138 if(mAutoCorrects.has(listName))
139 {
140 mAutoCorrects[listName]["wordStyle"]=announce;
141 return TRUE;
142 }
143 return FALSE;
144}
145BOOL LGGAutoCorrect::setListPriority(std::string listName, int priority)
146{
147 if(mAutoCorrects.has(listName))
148 {
149 mAutoCorrects[listName]["priority"]=priority;
150 return TRUE;
151 }
152 return FALSE;
153}
154LLSD LGGAutoCorrect::getAutoCorrects()
155{
156 //loadFromDisk();
157 return mAutoCorrects;
158}
159void LGGAutoCorrect::loadFromDisk()
160{
161 std::string filename=getFileName();
162 if (filename.empty())
163 {
164 llinfos << "no valid user directory." << llendl;
165 }
166 if(!gDirUtilp->fileExists(filename))
167 {
168 std::string defaultName = getDefaultFileName();
169 llinfos << " user settings file doesnt exist, going to try and read default one from "<<defaultName.c_str()<< llendl;
170
171 if(gDirUtilp->fileExists(defaultName))
172 {
173 LLSD blankllsd;
174 llifstream file;
175 file.open(defaultName.c_str());
176 if (file.is_open())
177 {
178 LLSDSerialize::fromXMLDocument(blankllsd, file);
179 }
180 file.close();
181 saveToDisk(blankllsd);
182 }else
183 saveToDisk(getExampleLLSD());
184 }
185 else
186 {
187 llifstream file;
188 file.open(filename.c_str());
189 if (file.is_open())
190 {
191 LLSDSerialize::fromXML(mAutoCorrects, file);
192 }
193 file.close();
194 }
195}
196void LGGAutoCorrect::saveToDisk(LLSD newSettings)
197{
198 mAutoCorrects=newSettings;
199 std::string filename=getFileName();
200 llofstream file;
201 file.open(filename.c_str());
202 LLSDSerialize::toPrettyXML(mAutoCorrects, file);
203 file.close();
204}
205void LGGAutoCorrect::runTest()
206{
207 std::string startS("He just abandonned all his abilties");
208 std::string endS = replaceWords(startS);
209 llinfos << "!!! Test of autoreplace; start with "<<startS.c_str() << " end with " << endS.c_str()<<llendl;
210
211
212}
213BOOL LGGAutoCorrect::saveListToDisk(std::string listName, std::string fileName)
214{
215 if(mAutoCorrects.has(listName))
216 {
217 llofstream file;
218 file.open(fileName.c_str());
219 LLSDSerialize::toPrettyXML(exportList(listName), file);
220 file.close();
221 return TRUE;
222 }
223 return FALSE;
224}
225LLSD LGGAutoCorrect::getAutoCorrectEntries(std::string listName)
226{
227 LLSD toReturn;
228 if(mAutoCorrects.has(listName))
229 {
230 toReturn=mAutoCorrects[listName];
231 }
232 return toReturn;
233}
234std::string LGGAutoCorrect::replaceWord(std::string currentWord)
235{
236 static BOOL *doAnything = rebind_llcontrol<BOOL>("EmeraldEnableAutoCorrect", &gSavedSettings, true);
237 if(!(*doAnything))return currentWord;
238
239 //loop through priorities
240 for(int currentPriority = 10;currentPriority>=0;currentPriority--)
241 {
242 LLSD::map_const_iterator loc_it = mAutoCorrects.beginMap();
243 LLSD::map_const_iterator loc_end = mAutoCorrects.endMap();
244 for ( ; loc_it != loc_end; ++loc_it)
245 {
246 const std::string& location = (*loc_it).first;
247 //llinfos << "location is "<<location.c_str() << " word is "<<currentWord.c_str()<<llendl;
248 const LLSD& loc_map = (*loc_it).second;
249 if(loc_map["priority"].asInteger()==currentPriority)
250 {
251 if(!loc_map["wordStyle"].asBoolean())
252 {
253 //this means look for partial matches instead of a full word
254 if(loc_map["enabled"].asBoolean())
255 {
256 LLSD::map_const_iterator inner_it = loc_map["data"].beginMap();
257 LLSD::map_const_iterator inner_end = loc_map["data"].endMap();
258 for ( ; inner_it != inner_end; ++inner_it)
259 {
260 const std::string& wrong = (*inner_it).first;
261 const std::string& right = (*inner_it).second;
262 int location = currentWord.find(wrong);
263 if(location!=std::string::npos)
264 {
265 currentWord=currentWord.replace(location,wrong.length(),right);
266 }
267 }
268 }
269
270 }else
271 if((loc_map["data"].has(currentWord))&&(loc_map["enabled"].asBoolean()))
272 {
273 std::string replacement = loc_map["data"][currentWord];
274 if(loc_map["announce"].asBoolean())
275 {
276 LLSD args;
277 //"[Before]" has been auto replaced by "[Replacement]"
278 // based on your [ListName] list.
279 args["BEFORE"] = currentWord;
280 args["LISTNAME"]=location;
281 args["REPLACEMENT"]=replacement;
282 LLNotifications::getInstance()->add("EmeraldAutoReplace",args);
283 }
284 gSavedSettings.setS32("EmeraldAutoCorrectCount",gSavedSettings.getS32("EmeraldAutoCorrectCount")+1);
285 llinfos << "found a word in list " << location.c_str() << " and it will replace " << currentWord.c_str() << " => " << replacement.c_str() << llendl;
286 return replacement;
287 }
288 }
289 }
290 }
291 return currentWord;
292}
293std::string LGGAutoCorrect::replaceWords(std::string words)
294{
295 static BOOL *doAnything = rebind_llcontrol<BOOL>("EmeraldEnableAutoCorrect", &gSavedSettings, true);
296 if(!(*doAnything))return words;
297 //TODO update this function to use the "wordStyle" thing,
298 //but so far this function is never used, so later
299
300 boost_tokenizer tokens(words, boost::char_separator<char>(" "));
301 for (boost_tokenizer::iterator token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter)
302 {
303 std::string currentWord(*token_iter);
304 LLSD::map_const_iterator loc_it = mAutoCorrects.beginMap();
305 LLSD::map_const_iterator loc_end = mAutoCorrects.endMap();
306 for ( ; loc_it != loc_end; ++loc_it)
307 {
308 const std::string& location = (*loc_it).first;
309 //llinfos << "location is "<<location.c_str() << " word is "<<currentWord.c_str()<<llendl;
310 const LLSD& loc_map = (*loc_it).second;
311 if((loc_map["data"].has(currentWord))&&(loc_map["enabled"].asBoolean()))
312 {
313 std::string replacement = loc_map["data"][currentWord];
314 if(loc_map["announce"].asBoolean())
315 {
316 LLSD args;
317 //"[Before]" has been auto replaced by "[Replacement]"
318 // based on your [ListName] list.
319 args["BEFORE"] = currentWord;
320 args["LISTNAME"]=location;
321 args["REPLACEMENT"]=replacement;
322 LLNotifications::getInstance()->add("EmeraldAutoReplace",args);
323 }
324 llinfos << "found a word in list " << location.c_str() << " and it will replace " << currentWord.c_str() << " => " << replacement.c_str() << llendl;
325 int wordStart = words.find(currentWord);
326 words.replace(wordStart,currentWord.length(),replacement);
327 return replaceWords(words);//lol recursion!
328 }
329 }
330 }
331 return words;
332}
333BOOL LGGAutoCorrect::addEntryToList(std::string wrong, std::string right, std::string listName)
334{
335 // *HACK: Make sure the "Custom" list exists, because the design of this
336 // system prevents us from updating it by changing the original file...
337 if(mAutoCorrects.has(listName))
338 {
339 mAutoCorrects[listName]["data"][wrong]=right;
340 return TRUE;
341 }
342 else if(listName == "Custom")
343 {
344 mAutoCorrects[listName]["announce"] = 0;
345 mAutoCorrects[listName]["author"] = "You";
346 mAutoCorrects[listName]["data"][wrong] = right;
347 mAutoCorrects[listName]["enabled"] = 1;
348 mAutoCorrects[listName]["priority"] = 10;
349 mAutoCorrects[listName]["wordStyle"] = 1;
350 return TRUE;
351 }
352
353 return FALSE;
354}
355BOOL LGGAutoCorrect::removeEntryFromList(std::string wrong, std::string listName)
356{
357 if(mAutoCorrects.has(listName))
358 {
359 if(mAutoCorrects[listName]["data"].has(wrong))
360 {
361 mAutoCorrects[listName]["data"].erase(wrong);
362 return TRUE;
363 }
364 }
365 return FALSE;
366}
367
368LLSD LGGAutoCorrect::getExampleLLSD()
369{
370 LLSD toReturn;
371
372 LLSD listone;
373 LLSD listtwo;
374
375 LLSD itemOne;
376 itemOne["wrong"]="wrong1";
377 itemOne["right"]="right1";
378 listone[0]=itemOne;
379
380 LLSD itemTwo;
381 itemTwo["wrong"]="wrong2";
382 itemTwo["right"]="right2";
383 listone[1]=itemTwo;
384
385 toReturn["listOne"]=listone;
386
387
388 itemOne["wrong"]="secondwrong1";
389 itemOne["right"]="secondright1";
390 listone[0]=itemOne;
391
392 itemTwo["wrong"]="secondwrong2";
393 itemTwo["right"]="secondright2";
394 listone[1]=itemTwo;
395
396 toReturn["listTwo"]=listone;
397
398 return toReturn;
399}
400