Debian Codegraph @

CodeLite/cl_config.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : cl_config.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "cl_config.h"
27
28 #include "FontUtils.hpp"
29 #include "cl_defs.h"
30 #include "cl_standard_paths.h"
31 #include "file_logger.h"
32 #include "fileutils.h"
33 #include "wx/filename.h"
34
35 #include <algorithm>
36 #include <wx/filefn.h>
37 #include <wx/log.h>
38 #include <wx/stdpaths.h>
39
40 #define ADD_OBJ_IF_NOT_EXISTS(parent, objName) \
41 if(!parent.hasNamedObject(objName)) { \
42 JSONItem obj = JSONItem::createObject(objName); \
43 parent.append(obj); \
44 }
45
46 #define ADD_ARR_IF_NOT_EXISTS(parent, arrName) \
47 if(!parent.hasNamedObject(arrName)) { \
48 JSONItem arr = JSONItem::createArray(arrName); \
49 parent.append(arr); \
50 }
51
52 namespace
53 {
54 void truncate_array(wxArrayString& arr, size_t maxSize)
55 {
56 while(arr.GetCount() > maxSize && arr.GetCount() > 0) {
57 arr.RemoveAt(arr.GetCount() - 1);
58 }
59 }
60 } // namespace
61
62 clConfig::clConfig(const wxString& filename)
63 {
64 if(wxFileName(filename).IsAbsolute()) {
65 m_filename = filename;
66 } else {
67 m_filename = clStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "config" +
68 wxFileName::GetPathSeparator() + filename;
69 }
70
71 if(m_filename.FileExists()) {
72 m_root = new JSON(m_filename);
73
74 } else {
75 if(!m_filename.DirExists()) {
76 m_filename.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
77 }
78 m_root = new JSON(cJSON_Object);
79 }
80
81 // Load the "Recent Items" cache
82 {
83 wxArrayString recentFiles;
84 JSONItem e = m_root->toElement();
85 if(e.hasNamedObject("RecentWorkspaces")) {
86 recentFiles = e.namedObject("RecentWorkspaces").toArrayString();
87 m_cacheRecentItems.insert(std::make_pair("RecentWorkspaces", recentFiles));
88 }
89 }
90
91 {
92 wxArrayString recentFiles;
93 JSONItem e = m_root->toElement();
94 if(e.hasNamedObject("RecentFiles")) {
95 recentFiles = e.namedObject("RecentFiles").toArrayString();
96 m_cacheRecentItems.insert(std::make_pair("RecentFiles", recentFiles));
97 }
98 }
99 }
100
101 clConfig::~clConfig() { wxDELETE(m_root); }
102
103 clConfig& clConfig::Get()
104 {
105 static clConfig config;
106 return config;
107 }
108
109 bool clConfig::GetOutputTabOrder(wxArrayString& tabs, int& selected)
110 {
111 if(m_root->toElement().hasNamedObject("outputTabOrder")) {
112 JSONItem element = m_root->toElement().namedObject("outputTabOrder");
113 tabs = element.namedObject("tabs").toArrayString();
114 selected = element.namedObject("selected").toInt();
115 return true;
116 }
117 return false;
118 }
119
120 void clConfig::SetOutputTabOrder(const wxArrayString& tabs, int selected)
121 {
122 DoDeleteProperty("outputTabOrder");
123
124 // first time
125 JSONItem e = JSONItem::createObject("outputTabOrder");
126 e.addProperty("tabs", tabs);
127 e.addProperty("selected", selected);
128 m_root->toElement().append(e);
129 m_root->save(m_filename);
130 }
131
132 bool clConfig::GetWorkspaceTabOrder(wxArrayString& tabs, int& selected)
133 {
134 if(m_root->toElement().hasNamedObject("workspaceTabOrder")) {
135 JSONItem element = m_root->toElement().namedObject("workspaceTabOrder");
136 tabs = element.namedObject("tabs").toArrayString();
137 selected = element.namedObject("selected").toInt();
138 return true;
139 }
140 return false;
141 }
142
143 void clConfig::SetWorkspaceTabOrder(const wxArrayString& tabs, int selected)
144 {
145 DoDeleteProperty("workspaceTabOrder");
146
147 // first time
148 JSONItem e = JSONItem::createObject("workspaceTabOrder");
149 e.addProperty("tabs", tabs);
150 e.addProperty("selected", selected);
151 m_root->toElement().append(e);
152
153 m_root->save(m_filename);
154 }
155
156 void clConfig::DoDeleteProperty(const wxString& property)
157 {
158 if(m_root->toElement().hasNamedObject(property)) {
159 m_root->toElement().removeProperty(property);
160 }
161 }
162
163 bool clConfig::ReadItem(clConfigItem* item, const wxString& differentName)
164 {
165 wxString nameToUse = differentName.IsEmpty() ? item->GetName() : differentName;
166 if(m_root->toElement().hasNamedObject(nameToUse)) {
167 item->FromJSON(m_root->toElement().namedObject(nameToUse));
168 return true;
169 }
170 return false;
171 }
172
173 bool clConfig::Write(const wxString& name, std::function<JSONItem()> serialiser_func, const wxFileName& configFile)
174 {
175 auto item = serialiser_func();
176 if(configFile.IsOk()) {
177 // write to a separate file
178 configFile.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
179 return FileUtils::WriteFileContent(configFile, item.format());
180 } else {
181 // add it to the global configuration file
182 DoDeleteProperty(name);
183 item.setName(name);
184 m_root->toElement().append(item);
185 return true;
186 }
187 }
188
189 void clConfig::Read(const wxString& name, std::function<void(const JSONItem& item)> deserialiser_func,
190 const wxFileName& configFile)
191 {
192 if(configFile.IsOk() && configFile.FileExists()) {
193 JSON root(configFile);
194 if(!root.isOk()) {
195 return;
196 }
197 deserialiser_func(root.toElement());
198 } else {
199 auto root = m_root->toElement();
200 if(root.hasNamedObject(name)) {
201 deserialiser_func(root[name]);
202 }
203 }
204 }
205
206 void clConfig::WriteItem(const clConfigItem* item, const wxString& differentName)
207 {
208 wxString nameToUse = differentName.IsEmpty() ? item->GetName() : differentName;
209 DoDeleteProperty(nameToUse);
210 m_root->toElement().append(item->ToJSON());
211 m_root->save(m_filename);
212 }
213
214 void clConfig::Reload()
215 {
216 if(m_filename.FileExists() == false)
217 return;
218
219 delete m_root;
220 m_root = new JSON(m_filename);
221 }
222
223 wxArrayString clConfig::MergeArrays(const wxArrayString& arr1, const wxArrayString& arr2) const
224 {
225 // we use set to keep the records sorted
226 std::set<wxString> visited;
227
228 for(const wxString& element : arr1) {
229 if(visited.count(element))
230 continue;
231 visited.insert(element);
232 }
233
234 wxArrayString merged_array;
235 merged_array.reserve(visited.size());
236 for(const wxString& element : visited) {
237 merged_array.Add(element);
238 }
239 return merged_array;
240 }
241
242 wxStringMap_t clConfig::MergeStringMaps(const wxStringMap_t& map1, const wxStringMap_t& map2) const
243 {
244 wxStringMap_t output;
245 output.reserve(map1.size() + map2.size());
246
247 output.insert(map1.begin(), map1.end());
248 output.insert(map2.begin(), map2.end());
249 return output;
250 }
251
252 void clConfig::Save()
253 {
254 if(m_root) {
255 LOG_IF_TRACE { clDEBUG1() << "Config file:" << m_filename << "saved!" << endl; }
256 m_root->save(m_filename);
257 }
258 }
259
260 void clConfig::Save(const wxFileName& fn)
261 {
262 if(m_root)
263 m_root->save(fn);
264 }
265
266 JSONItem clConfig::GetGeneralSetting()
267 {
268 if(!m_root->toElement().hasNamedObject("General")) {
269 JSONItem general = JSONItem::createObject("General");
270 m_root->toElement().append(general);
271 }
272 return m_root->toElement().namedObject("General");
273 }
274
275 void clConfig::Write(const wxString& name, bool value)
276 {
277 JSONItem general = GetGeneralSetting();
278 if(general.hasNamedObject(name)) {
279 general.removeProperty(name);
280 }
281
282 general.addProperty(name, value);
283 Save();
284 }
285
286 bool clConfig::Read(const wxString& name, bool defaultValue)
287 {
288 #if CL_USE_NATIVEBOOK
289 if(name == "UseCustomBaseColour")
290 return false;
291 #endif
292 JSONItem general = GetGeneralSetting();
293 if(general.namedObject(name).isBool()) {
294 return general.namedObject(name).toBool();
295 }
296 return defaultValue;
297 }
298
299 void clConfig::Write(const wxString& name, int value)
300 {
301 JSONItem general = GetGeneralSetting();
302 if(general.hasNamedObject(name)) {
303 general.removeProperty(name);
304 }
305
306 general.addProperty(name, value);
307 Save();
308 }
309
310 int clConfig::Read(const wxString& name, int defaultValue)
311 {
312 JSONItem general = GetGeneralSetting();
313 return general.namedObject(name).toInt(defaultValue);
314 }
315
316 void clConfig::Write(const wxString& name, const wxString& value)
317 {
318 JSONItem general = GetGeneralSetting();
319 if(general.hasNamedObject(name)) {
320 general.removeProperty(name);
321 }
322
323 general.addProperty(name, value);
324 Save();
325 }
326
327 wxString clConfig::Read(const wxString& name, const wxString& defaultValue)
328 {
329 JSONItem general = GetGeneralSetting();
330 if(general.namedObject(name).isString()) {
331 return general.namedObject(name).toString();
332 }
333
334 return defaultValue;
335 }
336
337 int clConfig::GetAnnoyingDlgAnswer(const wxString& name, int defaultValue)
338 {
339 if(m_root->toElement().hasNamedObject("AnnoyingDialogsAnswers")) {
340
341 JSONItem element = m_root->toElement().namedObject("AnnoyingDialogsAnswers");
342 if(element.hasNamedObject(name)) {
343 return element.namedObject(name).toInt(defaultValue);
344 }
345 }
346 return defaultValue;
347 }
348
349 void clConfig::SetAnnoyingDlgAnswer(const wxString& name, int value)
350 {
351 if(!m_root->toElement().hasNamedObject("AnnoyingDialogsAnswers")) {
352 JSONItem element = JSONItem::createObject("AnnoyingDialogsAnswers");
353 m_root->toElement().append(element);
354 }
355
356 JSONItem element = m_root->toElement().namedObject("AnnoyingDialogsAnswers");
357 if(element.hasNamedObject(name)) {
358 element.removeProperty(name);
359 }
360 element.addProperty(name, value);
361 Save();
362 }
363
364 void clConfig::ClearAnnoyingDlgAnswers()
365 {
366 DoDeleteProperty("AnnoyingDialogsAnswers");
367 Save();
368 Reload();
369 }
370
371 void clConfig::SetQuickFindSearchItems(const wxArrayString& items)
372 {
373 ADD_OBJ_IF_NOT_EXISTS(m_root->toElement(), "QuickFindBar");
374 JSONItem quickFindBar = m_root->toElement().namedObject("QuickFindBar");
375 if(quickFindBar.hasNamedObject("SearchHistory")) {
376 quickFindBar.removeProperty("SearchHistory");
377 }
378
379 wxArrayString items_to_save = items;
380 truncate_array(items_to_save, 20);
381
382 quickFindBar.addProperty("SearchHistory", items_to_save);
383 Save();
384 }
385
386 void clConfig::SetQuickFindReplaceItems(const wxArrayString& items)
387 {
388 ADD_OBJ_IF_NOT_EXISTS(m_root->toElement(), "QuickFindBar");
389 JSONItem quickFindBar = m_root->toElement().namedObject("QuickFindBar");
390 if(quickFindBar.hasNamedObject("ReplaceHistory")) {
391 quickFindBar.removeProperty("ReplaceHistory");
392 }
393
394 wxArrayString items_to_save = items;
395 truncate_array(items_to_save, 20);
396
397 quickFindBar.addProperty("ReplaceHistory", items_to_save);
398 Save();
399 }
400
401 void clConfig::AddQuickFindReplaceItem(const wxString& str)
402 {
403 ADD_OBJ_IF_NOT_EXISTS(m_root->toElement(), "QuickFindBar");
404
405 JSONItem quickFindBar = m_root->toElement().namedObject("QuickFindBar");
406 ADD_ARR_IF_NOT_EXISTS(quickFindBar, "ReplaceHistory");
407
408 JSONItem arr = quickFindBar.namedObject("ReplaceHistory");
409 wxArrayString items = arr.toArrayString();
410
411 // Update the array
412 int where = items.Index(str);
413 if(where != wxNOT_FOUND) {
414 items.RemoveAt(where);
415 items.Insert(str, 0);
416
417 } else {
418 // remove overflow items if needed
419 if(items.GetCount() > 20) {
420 // remove last item
421 items.RemoveAt(items.GetCount() - 1);
422 }
423 items.Insert(str, 0);
424 }
425
426 quickFindBar.removeProperty("ReplaceHistory");
427 quickFindBar.addProperty("ReplaceHistory", items);
428 Save();
429 }
430
431 void clConfig::AddQuickFindSearchItem(const wxString& str)
432 {
433 ADD_OBJ_IF_NOT_EXISTS(m_root->toElement(), "QuickFindBar");
434
435 JSONItem quickFindBar = m_root->toElement().namedObject("QuickFindBar");
436 ADD_ARR_IF_NOT_EXISTS(quickFindBar, "SearchHistory");
437
438 JSONItem arr = quickFindBar.namedObject("SearchHistory");
439 wxArrayString items = arr.toArrayString();
440
441 // Update the array
442 int where = items.Index(str);
443 if(where != wxNOT_FOUND) {
444 items.RemoveAt(where);
445 }
446 items.Insert(str, 0);
447
448 // Reudce to size to max of 20
449 while(items.size() > 20) {
450 items.RemoveAt(items.size() - 1);
451 }
452
453 // Update the array
454 quickFindBar.removeProperty("SearchHistory");
455 quickFindBar.addProperty("SearchHistory", items);
456 Save();
457 }
458
459 wxArrayString clConfig::GetQuickFindReplaceItems() const
460 {
461 ADD_OBJ_IF_NOT_EXISTS(m_root->toElement(), "QuickFindBar");
462 JSONItem quickFindBar = m_root->toElement().namedObject("QuickFindBar");
463 ADD_ARR_IF_NOT_EXISTS(quickFindBar, "ReplaceHistory");
464 return quickFindBar.namedObject("ReplaceHistory").toArrayString();
465 }
466
467 wxArrayString clConfig::GetQuickFindSearchItems() const
468 {
469 ADD_OBJ_IF_NOT_EXISTS(m_root->toElement(), "QuickFindBar");
470 JSONItem quickFindBar = m_root->toElement().namedObject("QuickFindBar");
471 ADD_ARR_IF_NOT_EXISTS(quickFindBar, "SearchHistory");
472 return quickFindBar.namedObject("SearchHistory").toArrayString();
473 }
474
475 wxArrayString clConfig::Read(const wxString& name, const wxArrayString& defaultValue)
476 {
477 JSONItem general = GetGeneralSetting();
478 if(general.hasNamedObject(name)) {
479 return general.namedObject(name).toArrayString();
480 }
481 return defaultValue;
482 }
483
484 void clConfig::Write(const wxString& name, const wxArrayString& value)
485 {
486 JSONItem general = GetGeneralSetting();
487 if(general.hasNamedObject(name)) {
488 general.removeProperty(name);
489 }
490
491 general.addProperty(name, value);
492 Save();
493 }
494
495 void clConfig::DoAddRecentItem(const wxString& propName, const wxString& filename)
496 {
497 wxArrayString recentItems = DoGetRecentItems(propName);
498
499 // Prepend the item
500 if(recentItems.Index(filename) != wxNOT_FOUND) {
501 recentItems.Remove(filename);
502 }
503
504 if(!wxFileName(filename).FileExists()) {
505 // Don't add non existing file
506 return;
507 }
508
509 recentItems.Insert(filename, 0);
510
511 // Make sure the list does not go over 15 items
512 while(recentItems.size() >= 15) {
513 recentItems.RemoveAt(recentItems.size() - 1);
514 }
515
516 // Remove non existing items
517 wxArrayString existingFiles;
518 for(size_t i = 0; i < recentItems.size(); ++i) {
519 if(wxFileName(recentItems.Item(i)).FileExists()) {
520 existingFiles.Add(recentItems.Item(i));
521 }
522 }
523 recentItems.swap(existingFiles);
524
525 // Remove old node if exists
526 JSONItem e = m_root->toElement();
527 if(e.hasNamedObject(propName)) {
528 e.removeProperty(propName);
529 }
530
531 // append new property
532 e.addProperty(propName, recentItems);
533
534 // update the cache
535 if(m_cacheRecentItems.count(propName)) {
536 m_cacheRecentItems.erase(propName);
537 }
538
539 m_cacheRecentItems.insert(std::make_pair(propName, recentItems));
540 m_root->save(m_filename);
541 }
542
543 void clConfig::DoClearRecentItems(const wxString& propName)
544 {
545 JSONItem e = m_root->toElement();
546 if(e.hasNamedObject(propName)) {
547 e.removeProperty(propName);
548 }
549 m_root->save(m_filename);
550 // update the cache
551 if(m_cacheRecentItems.count(propName)) {
552 m_cacheRecentItems.erase(propName);
553 }
554 }
555
556 wxArrayString clConfig::DoGetRecentItems(const wxString& propName) const
557 {
558 wxArrayString recentItems;
559
560 // Try the cache first
561 if(m_cacheRecentItems.count(propName)) {
562 recentItems = m_cacheRecentItems.find(propName)->second;
563
564 } else {
565 JSONItem e = m_root->toElement();
566 if(e.hasNamedObject(propName)) {
567 recentItems = e.namedObject(propName).toArrayString();
568 }
569 }
570 return recentItems;
571 }
572 #if wxUSE_GUI
573 wxFont clConfig::Read(const wxString& name, const wxFont& defaultValue)
574 {
575 JSONItem general = GetGeneralSetting();
576 if(!general.hasNamedObject(name))
577 return defaultValue;
578
579 // Unserialize the font
580 wxFont font;
581 JSONItem f = general.namedObject(name);
582 if(!f.hasNamedObject("fontDesc"))
583 return defaultValue;
584
585 font.SetNativeFontInfo(FontUtils::GetFontInfo(f.namedObject("fontDesc").toString()));
586 return font;
587 }
588
589 void clConfig::Write(const wxString& name, const wxFont& value)
590 {
591 JSONItem font = JSONItem::createObject(name);
592 font.addProperty("fontDesc", FontUtils::GetFontInfo(value));
593
594 JSONItem general = GetGeneralSetting();
595 if(general.hasNamedObject(name)) {
596 general.removeProperty(name);
597 }
598 general.append(font);
599 Save();
600 }
601
602 wxColour clConfig::Read(const wxString& name, const wxColour& defaultValue)
603 {
604 wxString strValue;
605 strValue = Read(name, wxString());
606 if(strValue.IsEmpty()) {
607 return defaultValue;
608 }
609 wxColour col(strValue);
610 return col;
611 }
612
613 void clConfig::Write(const wxString& name, const wxColour& value)
614 {
615 wxString strValue = value.GetAsString(wxC2S_HTML_SYNTAX);
616 Write(name, strValue);
617 Save();
618 }
619
620 #endif