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

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

Issue 11013110: Cleanup (Closed)
Patch Set: Minor CallEngine refactoring Created July 26, 2013, 2:12 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
1 #include "PluginStdAfx.h" 1 #include "PluginStdAfx.h"
2 2
3 #include <Wbemidl.h> 3 #include <Wbemidl.h>
4 #include <time.h> 4 #include <time.h>
5 #include "PluginIniFileW.h"
6 #include "PluginIniFile.h"
7 #include "PluginSettings.h" 5 #include "PluginSettings.h"
8 #include "PluginClient.h" 6 #include "PluginClient.h"
9 #include "PluginChecksum.h"
10 #include "PluginSystem.h" 7 #include "PluginSystem.h"
11 #ifdef SUPPORT_FILTER 8 #ifdef SUPPORT_FILTER
12 #include "PluginFilter.h" 9 #include "PluginFilter.h"
13 #endif 10 #endif
14 #include "PluginMutex.h" 11 #include "PluginMutex.h"
15 #include "PluginHttpRequest.h"
16 #include "../shared/Utils.h" 12 #include "../shared/Utils.h"
17 #include <memory> 13 #include <memory>
18 14
19 15
20 // IE functions 16 // IE functions
21 #pragma comment(lib, "iepmapi.lib") 17 #pragma comment(lib, "iepmapi.lib")
22 18
23 #include <knownfolders.h> 19 #include <knownfolders.h>
24 20
25 namespace 21 namespace
26 { 22 {
27 std::wstring CreateDomainWhitelistingFilter(CString domain) 23 std::wstring CreateDomainWhitelistingFilter(const CString domain)
28 { 24 {
29 return L"@@||" + domain + L"^$document"; 25 return std::wstring(L"@@||") + domain.GetString() + std::wstring(L"^$documen t");
30 } 26 }
31 } 27 }
32 28
33 class TSettings 29 class TSettings
34 { 30 {
35 DWORD processorId; 31 DWORD processorId;
36 32
37 char sPluginId[44]; 33 char sPluginId[44];
38 }; 34 };
39 35
(...skipping 19 matching lines...) Expand all
59 class CPluginSettingsWhitelistLock : public CPluginMutex 55 class CPluginSettingsWhitelistLock : public CPluginMutex
60 { 56 {
61 public: 57 public:
62 CPluginSettingsWhitelistLock() : CPluginMutex("SettingsFileWhitelist", PLUGIN_ ERROR_MUTEX_SETTINGS_FILE_WHITELIST) {} 58 CPluginSettingsWhitelistLock() : CPluginMutex("SettingsFileWhitelist", PLUGIN_ ERROR_MUTEX_SETTINGS_FILE_WHITELIST) {}
63 ~CPluginSettingsWhitelistLock() {} 59 ~CPluginSettingsWhitelistLock() {}
64 }; 60 };
65 61
66 #endif 62 #endif
67 63
68 CPluginSettings* CPluginSettings::s_instance = NULL; 64 CPluginSettings* CPluginSettings::s_instance = NULL;
69 bool CPluginSettings::s_isLightOnly = false;
70 65
71 CComAutoCriticalSection CPluginSettings::s_criticalSectionLocal; 66 CComAutoCriticalSection CPluginSettings::s_criticalSectionLocal;
72 #ifdef SUPPORT_WHITELIST
73 CComAutoCriticalSection CPluginSettings::s_criticalSectionDomainHistory;
74 #endif
75 67
76 68
77 CPluginSettings::CPluginSettings() : 69 CPluginSettings::CPluginSettings() : m_dwWorkingThreadId(0), m_isPluginEnabledTa b(true)
78 m_settingsVersion("1"), m_isDirty(false), m_isFirstRun(false), m_isFirstRunUpd ate(false), m_dwMainProcessId(0), m_dwMainThreadId(0), m_dwWorkingThreadId(0),
79 m_isDirtyTab(false), m_isPluginEnabledTab(true), m_tabNumber("1")
80 { 70 {
81
82 CPluginSettings *lightInstance = s_instance;
83 s_instance = NULL; 71 s_instance = NULL;
84 72
85 m_settingsFile = std::auto_ptr<CPluginIniFileW>(new CPluginIniFileW(GetDataPat h(SETTINGS_INI_FILE), false));
86 m_settingsFileTab = std::auto_ptr<CPluginIniFileW>(new CPluginIniFileW(GetData Path(SETTINGS_INI_FILE_TAB), true));
87
88 m_WindowsBuildNumber = 0; 73 m_WindowsBuildNumber = 0;
89 74
90 Clear();
91 ClearTab();
92 #ifdef SUPPORT_WHITELIST 75 #ifdef SUPPORT_WHITELIST
93 ClearWhitelist(); 76 ClearWhitelist();
94 #endif 77 #endif
95
96 // Check existence of settings file
97 bool isFileExisting = false;
98 {
99 CPluginSettingsLock lock;
100 if (lock.IsLocked())
101 {
102 std::ifstream is;
103 is.open(GetDataPath(SETTINGS_INI_FILE), std::ios_base::in);
104 if (!is.is_open())
105 {
106 TCHAR pf[MAX_PATH];
107 SHGetSpecialFolderPath(
108 0,
109 pf,
110 CSIDL_PROGRAM_FILESX86,
111 FALSE );
112 is.open(GetDataPath(SETTINGS_INI_FILE), std::ios_base::in);
113 if (!is.is_open())
114 {
115 m_isDirty = true;
116 }
117 else
118 {
119 is.close();
120 isFileExisting = true;
121
122 }
123
124 }
125 else
126 {
127 is.close();
128
129 isFileExisting = true;
130 }
131 }
132 }
133
134 // Read or convert file
135 if (isFileExisting)
136 {
137 Read(false);
138 }
139 else
140 {
141 m_isDirty = true;
142 }
143
144 if (s_isLightOnly)
145 {
146 this->SetMainProcessId(lightInstance->m_dwMainProcessId);
147 this->SetMainThreadId(lightInstance->m_dwMainThreadId);
148 this->SetMainUiThreadId(lightInstance->m_dwMainUiThreadId);
149 this->SetWorkingThreadId(lightInstance->m_dwWorkingThreadId);
150 }
151 Write();
152 } 78 }
153 79
154 80
155 CPluginSettings::~CPluginSettings() 81 CPluginSettings::~CPluginSettings()
156 { 82 {
157 s_instance = NULL; 83 s_instance = NULL;
158 } 84 }
159 85
160 86
161 CPluginSettings* CPluginSettings::GetInstance() 87 CPluginSettings* CPluginSettings::GetInstance()
162 { 88 {
163 CPluginSettings* instance = NULL; 89 CPluginSettings* instance = NULL;
164 90
165 s_criticalSectionLocal.Lock(); 91 s_criticalSectionLocal.Lock();
166 { 92 {
167 if ((!s_instance) || (s_isLightOnly)) 93 if (!s_instance)
168 { 94 {
169 s_instance = new CPluginSettings(); 95 s_instance = new CPluginSettings();
170 #ifdef USE_CONSOLE 96 #ifdef USE_CONSOLE
171 CONSOLE("Fetching Available Subscription\n"); 97 CONSOLE("Fetching Available Subscription\n");
172 #endif 98 #endif
173 CPluginSettings::GetInstance()->m_subscriptions = CPluginClient::GetInstan ce()->FetchAvailableSubscriptions(); 99 CPluginSettings::GetInstance()->m_subscriptions = CPluginClient::GetInstan ce()->FetchAvailableSubscriptions();
174 s_isLightOnly = false;
175 } 100 }
176 101
177 instance = s_instance; 102 instance = s_instance;
178 } 103 }
179 s_criticalSectionLocal.Unlock(); 104 s_criticalSectionLocal.Unlock();
180 105
181 return instance; 106 return instance;
182 } 107 }
183 108
184 109
185 bool CPluginSettings::HasInstance() 110 bool CPluginSettings::HasInstance()
186 { 111 {
187 bool hasInstance = true; 112 bool hasInstance = true;
188 113
189 s_criticalSectionLocal.Lock(); 114 s_criticalSectionLocal.Lock();
190 { 115 {
191 hasInstance = s_instance != NULL; 116 hasInstance = s_instance != NULL;
192 } 117 }
193 s_criticalSectionLocal.Unlock(); 118 s_criticalSectionLocal.Unlock();
194 119
195 return hasInstance; 120 return hasInstance;
196 } 121 }
197 122
198 123
199 bool CPluginSettings::Read(bool bDebug)
200 {
201 bool isRead = true;
202
203 DEBUG_SETTINGS(L"Settings::Read")
204 {
205 if (bDebug)
206 {
207 DEBUG_GENERAL(L"*** Loading settings:" + m_settingsFile->GetFilePath());
208 }
209
210 CPluginSettingsLock lock;
211 if (lock.IsLocked())
212 {
213 isRead = m_settingsFile->Read();
214 if (isRead)
215 {
216 if (m_settingsFile->IsValidChecksum())
217 {
218 m_properties = m_settingsFile->GetSectionData("Settings");
219 }
220 else
221 {
222 DEBUG_SETTINGS("Settings:Invalid checksum - Deleting file")
223
224 Clear();
225
226 DEBUG_ERROR_LOG(m_settingsFile->GetLastError(), PLUGIN_ERROR_SETTINGS, PLUGIN_ERROR_SETTINGS_FILE_READ_CHECKSUM, "Settings::Read - Checksum")
227 isRead = false;
228 m_isDirty = true;
229 }
230 }
231 else if (m_settingsFile->GetLastError() == ERROR_FILE_NOT_FOUND)
232 {
233 DEBUG_ERROR_LOG(m_settingsFile->GetLastError(), PLUGIN_ERROR_SETTINGS, P LUGIN_ERROR_SETTINGS_FILE_READ, "Settings::Read")
234 m_isDirty = true;
235 }
236 else
237 {
238 DEBUG_ERROR_LOG(m_settingsFile->GetLastError(), PLUGIN_ERROR_SETTINGS, P LUGIN_ERROR_SETTINGS_FILE_READ, "Settings::Read")
239 }
240 }
241 else
242 {
243 isRead = false;
244 }
245 }
246
247 // Write file in case it is dirty
248 if (isRead)
249 {
250 isRead = Write();
251 }
252
253 return isRead;
254 }
255
256
257 void CPluginSettings::Clear()
258 {
259 // Default settings
260 s_criticalSectionLocal.Lock();
261 {
262 m_properties.clear();
263
264 m_properties[SETTING_PLUGIN_VERSION] = IEPLUGIN_VERSION;
265 m_properties[SETTING_LANGUAGE] = "en";
266 }
267 s_criticalSectionLocal.Unlock();
268 }
269 124
270 CString CPluginSettings::GetDataPath(const CString& filename) 125 CString CPluginSettings::GetDataPath(const CString& filename)
271 { 126 {
272 std::wstring path = ::GetAppDataPath() + L"\\" + static_cast<LPCWSTR>(filename ); 127 std::wstring path = ::GetAppDataPath() + L"\\" + static_cast<LPCWSTR>(filename );
273 return CString(path.c_str()); 128 return CString(path.c_str());
274 } 129 }
275 130
276 CString CPluginSettings::GetSystemLanguage() 131 CString CPluginSettings::GetSystemLanguage()
277 { 132 {
278 CString language; 133 CString language;
279 CString country; 134 CString country;
280 135
281 DWORD bufSize = 256; 136 DWORD bufSize = 256;
282 int ccBuf = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME, langu age.GetBufferSetLength(bufSize), bufSize); 137 int ccBuf = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME, langu age.GetBufferSetLength(bufSize), bufSize);
283 ccBuf = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO3166CTRYNAME, country. GetBufferSetLength(bufSize), bufSize); 138 ccBuf = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO3166CTRYNAME, country. GetBufferSetLength(bufSize), bufSize);
284 139
285 if ((country.IsEmpty()) || (language.IsEmpty())) 140 if ((country.IsEmpty()) || (language.IsEmpty()))
286 { 141 {
287 return CString(); 142 return CString();
288 } 143 }
289 CString lang; 144 CString lang;
290 lang.Append(language); 145 lang.Append(language);
291 lang.Append(L"-"); 146 lang.Append(L"-");
292 lang.Append(country); 147 lang.Append(country);
293 148
294 return lang; 149 return lang;
295 150
296 } 151 }
297 152
298 CString CPluginSettings::GetTempPath(const CString& filename)
299 {
300 CString tempPath;
301
302 LPWSTR pwszCacheDir = NULL;
303
304 HRESULT hr = ::IEGetWriteableFolderPath(FOLDERID_InternetCache, &pwszCacheDir) ;
305 if (SUCCEEDED(hr))
306 {
307 tempPath = pwszCacheDir;
308 }
309 // Not implemented in IE6
310 else if (hr == E_NOTIMPL)
311 {
312 TCHAR path[MAX_PATH] = _T("");
313
314 if (::SHGetSpecialFolderPath(NULL, path, CSIDL_INTERNET_CACHE, TRUE))
315 {
316 tempPath = path;
317 }
318 else
319 {
320 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_SYSINFO, PLUGIN_ERROR_SYSIN FO_GET_SPECIAL_FOLDER_TEMP, "Settings::GetTempPath failed");
321 }
322 }
323 // Other error
324 else
325 {
326 DEBUG_ERROR_LOG(hr, PLUGIN_ERROR_SYSINFO, PLUGIN_ERROR_SYSINFO_TEMP_PATH, "S ettings::GetTempPath failed");
327 }
328
329 ::CoTaskMemFree(pwszCacheDir);
330
331 return tempPath + "\\" + filename;
332 }
333
334 CString CPluginSettings::GetTempFile(const CString& prefix, const CString& exten sion)
335 {
336 TCHAR nameBuffer[MAX_PATH] = _T("");
337
338 CString tempPath;
339
340 DWORD dwRetVal = ::GetTempFileName(GetTempPath(), prefix, 0, nameBuffer);
341 if (dwRetVal == 0)
342 {
343 DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_SYSINFO, PLUGIN_ERROR_SYSINFO _TEMP_FILE, "Settings::GetTempFileName failed");
344
345 tempPath = GetDataPath();
346 }
347 else
348 {
349 tempPath = nameBuffer;
350 if (!extension.IsEmpty())
351 {
352 int pos = tempPath.ReverseFind(_T('.'));
353 if (pos >= 0)
354 {
355 tempPath = tempPath.Left(pos+1) + extension;
356 }
357 }
358 }
359
360 return tempPath;
361 }
362
363
364 bool CPluginSettings::Has(const CString& key) const
365 {
366 bool hasKey;
367
368 s_criticalSectionLocal.Lock();
369 {
370 hasKey = m_properties.find(key) != m_properties.end();
371 }
372 s_criticalSectionLocal.Unlock();
373
374 return hasKey;
375 }
376
377
378 void CPluginSettings::Remove(const CString& key)
379 {
380 s_criticalSectionLocal.Lock();
381 {
382 TProperties::iterator it = m_properties.find(key);
383 if (it != m_properties.end())
384 {
385 m_properties.erase(it);
386 m_isDirty = true;
387 }
388 }
389 s_criticalSectionLocal.Unlock();
390 }
391
392
393 CString CPluginSettings::GetString(const CString& key, const CString& defaultVal ue) const
394 {
395 CString val = defaultValue;
396
397 s_criticalSectionLocal.Lock();
398 {
399 TProperties::const_iterator it = m_properties.find(key);
400 if (it != m_properties.end())
401 {
402 val = it->second;
403 }
404 }
405 s_criticalSectionLocal.Unlock();
406
407 DEBUG_SETTINGS("Settings::GetString key:" + key + " value:" + val)
408
409 return val;
410 }
411
412
413 void CPluginSettings::SetString(const CString& key, const CString& value)
414 {
415 if (value.IsEmpty()) return;
416
417 DEBUG_SETTINGS("Settings::SetString key:" + key + " value:" + value)
418
419 s_criticalSectionLocal.Lock();
420 {
421 TProperties::iterator it = m_properties.find(key);
422 if (it != m_properties.end() && it->second != value)
423 {
424 it->second = value;
425 m_isDirty = true;
426 }
427 else if (it == m_properties.end())
428 {
429 m_properties[key] = value;
430 m_isDirty = true;
431 }
432 }
433 s_criticalSectionLocal.Unlock();
434 }
435
436
437 int CPluginSettings::GetValue(const CString& key, int defaultValue) const
438 {
439 int val = defaultValue;
440
441 CString sValue;
442 sValue.Format(L"%d", defaultValue);
443
444 s_criticalSectionLocal.Lock();
445 {
446 TProperties::const_iterator it = m_properties.find(key);
447 if (it != m_properties.end())
448 {
449 sValue = it->second;
450 val = _wtoi(it->second);
451 }
452 }
453 s_criticalSectionLocal.Unlock();
454
455 DEBUG_SETTINGS("Settings::GetValue key:" + key + " value:" + sValue)
456
457 return val;
458 }
459
460
461 void CPluginSettings::SetValue(const CString& key, int value)
462 {
463 CString sValue;
464 sValue.Format(L"%d", value);
465
466 DEBUG_SETTINGS("Settings::SetValue key:" + key + " value:" + sValue)
467
468 s_criticalSectionLocal.Lock();
469 {
470 TProperties::iterator it = m_properties.find(key);
471 if (it != m_properties.end() && it->second != sValue)
472 {
473 it->second = sValue;
474 m_isDirty = true;
475 }
476 else if (it == m_properties.end())
477 {
478 m_properties[key] = sValue;
479 m_isDirty = true;
480 }
481 }
482 s_criticalSectionLocal.Unlock();
483 }
484
485
486 bool CPluginSettings::GetBool(const CString& key, bool defaultValue) const
487 {
488 bool value = defaultValue;
489
490 s_criticalSectionLocal.Lock();
491 {
492 TProperties::const_iterator it = m_properties.find(key);
493 if (it != m_properties.end())
494 {
495 if (it->second == "true") value = true;
496 if (it->second == "false") value = false;
497 }
498 }
499 s_criticalSectionLocal.Unlock();
500
501 DEBUG_SETTINGS("Settings::GetBool key:" + key + " value:" + (value ? "true":"f alse"))
502
503 return value;
504 }
505
506
507 void CPluginSettings::SetBool(const CString& key, bool value)
508 {
509 SetString(key, value ? "true":"false");
510 }
511
512 153
513 bool CPluginSettings::IsPluginEnabled() const 154 bool CPluginSettings::IsPluginEnabled() const
514 { 155 {
515 return m_isPluginEnabledTab; 156 return m_isPluginEnabledTab;
516 } 157 }
517 158
518 159
519 std::map<CString, CString> CPluginSettings::GetFilterLanguageTitleList() const 160 std::map<CString, CString> CPluginSettings::GetFilterLanguageTitleList() const
520 { 161 {
521 std::map<CString, CString> filterList; 162 std::map<CString, CString> filterList;
522 for (size_t i = 0; i < m_subscriptions.size(); i ++) 163 for (size_t i = 0; i < m_subscriptions.size(); i ++)
523 { 164 {
524 SubscriptionDescription it = m_subscriptions[i]; 165 SubscriptionDescription it = m_subscriptions[i];
525 filterList.insert(std::make_pair(CString(it.url.c_str()), CString(it.title.c _str()))); 166 filterList.insert(std::make_pair(CString(it.url.c_str()), CString(it.title.c _str())));
526 } 167 }
527 return filterList; 168 return filterList;
528 } 169 }
529 170
530
531 bool CPluginSettings::Write(bool isDebug)
532 {
533 bool isWritten = true;
534
535 if (!m_isDirty)
536 {
537 return isWritten;
538 }
539
540 if (isDebug)
541 {
542 DEBUG_GENERAL(L"*** Writing changed settings")
543 }
544
545 CPluginSettingsLock lock;
546 if (lock.IsLocked())
547 {
548 m_settingsFile->Clear();
549
550 // Properties
551 CPluginIniFileW::TSectionData settings;
552
553 s_criticalSectionLocal.Lock();
554 {
555 for (TProperties::iterator it = m_properties.begin(); it != m_properties.e nd(); ++it)
556 {
557 settings[it->first] = it->second;
558 }
559 }
560 s_criticalSectionLocal.Unlock();
561
562 m_settingsFile->UpdateSection("Settings", settings);
563
564 // Write file
565 isWritten = m_settingsFile->Write();
566 if (!isWritten)
567 {
568 DEBUG_ERROR_LOG(m_settingsFile->GetLastError(), PLUGIN_ERROR_SETTINGS, PLU GIN_ERROR_SETTINGS_FILE_WRITE, "Settings::Write")
569 }
570
571 m_isDirty = false;
572
573 IncrementTabVersion(SETTING_TAB_SETTINGS_VERSION);
574 }
575 else
576 {
577 isWritten = false;
578 }
579
580 return isWritten;
581 }
582
583
584 bool CPluginSettings::IsMainProcess(DWORD dwProcessId) const
585 {
586 if (dwProcessId == 0)
587 {
588 dwProcessId = ::GetCurrentProcessId();
589 }
590 return m_dwMainProcessId == dwProcessId;
591 }
592
593 void CPluginSettings::SetMainProcessId()
594 {
595 m_dwMainProcessId = ::GetCurrentProcessId();
596 }
597
598 void CPluginSettings::SetMainProcessId(DWORD id)
599 {
600 m_dwMainProcessId = id;
601 }
602
603
604 bool CPluginSettings::IsMainUiThread(DWORD dwThreadId) const
605 {
606 if (dwThreadId == 0)
607 {
608 dwThreadId = ::GetCurrentThreadId();
609 }
610 return m_dwMainUiThreadId == dwThreadId;
611 }
612
613 void CPluginSettings::SetMainUiThreadId()
614 {
615 m_dwMainUiThreadId = ::GetCurrentThreadId();
616 }
617
618 void CPluginSettings::SetMainUiThreadId(DWORD id)
619 {
620 m_dwMainUiThreadId = id;
621 }
622 bool CPluginSettings::IsMainThread(DWORD dwThreadId) const
623 {
624 if (dwThreadId == 0)
625 {
626 dwThreadId = ::GetCurrentThreadId();
627 }
628 return m_dwMainThreadId == dwThreadId;
629 }
630
631 void CPluginSettings::SetMainThreadId()
632 {
633 m_dwMainThreadId = ::GetCurrentThreadId();
634 }
635
636 void CPluginSettings::SetMainThreadId(DWORD id)
637 {
638 m_dwMainThreadId = id;
639 }
640
641 bool CPluginSettings::IsWorkingThread(DWORD dwThreadId) const 171 bool CPluginSettings::IsWorkingThread(DWORD dwThreadId) const
642 { 172 {
643 if (dwThreadId == 0) 173 if (dwThreadId == 0)
644 { 174 {
645 dwThreadId = ::GetCurrentThreadId(); 175 dwThreadId = ::GetCurrentThreadId();
646 } 176 }
647 return m_dwWorkingThreadId == dwThreadId; 177 return m_dwWorkingThreadId == dwThreadId;
648 } 178 }
649 179
650 void CPluginSettings::SetWorkingThreadId() 180 void CPluginSettings::SetWorkingThreadId()
651 { 181 {
652 m_dwWorkingThreadId = ::GetCurrentThreadId(); 182 m_dwWorkingThreadId = ::GetCurrentThreadId();
653 } 183 }
654 184
655 void CPluginSettings::SetWorkingThreadId(DWORD id) 185 void CPluginSettings::SetWorkingThreadId(DWORD id)
656 { 186 {
657 m_dwWorkingThreadId = id; 187 m_dwWorkingThreadId = id;
658 } 188 }
659 189
660 void CPluginSettings::SetFirstRun() 190 void CPluginSettings::SetFirstRun()
661 { 191 {
662 m_isFirstRun = true; 192 m_isFirstRun = true;
663 } 193 }
664 194
665 bool CPluginSettings::IsFirstRun() const 195 bool CPluginSettings::IsFirstRun() const
666 { 196 {
667 return m_isFirstRun; 197 return m_isFirstRun;
668 } 198 }
669 199
670 void CPluginSettings::SetFirstRunUpdate()
671 {
672 m_isFirstRunUpdate = true;
673 }
674
675 bool CPluginSettings::IsFirstRunUpdate() const
676 {
677 return m_isFirstRunUpdate;
678 }
679
680 bool CPluginSettings::IsFirstRunAny() const
681 {
682 return m_isFirstRun || m_isFirstRunUpdate;
683 }
684
685 // ============================================================================
686 // Tab settings
687 // ============================================================================
688
689 void CPluginSettings::ClearTab()
690 {
691 s_criticalSectionLocal.Lock();
692 {
693 m_isPluginEnabledTab = true;
694
695 m_errorsTab.clear();
696
697 m_propertiesTab.clear();
698
699 m_propertiesTab[SETTING_TAB_PLUGIN_ENABLED] = "true";
700 }
701 s_criticalSectionLocal.Unlock();
702 }
703
704
705 bool CPluginSettings::ReadTab(bool bDebug)
706 {
707 bool isRead = true;
708
709 DEBUG_SETTINGS(L"SettingsTab::Read tab")
710
711 if (bDebug)
712 {
713 DEBUG_GENERAL(L"*** Loading tab settings:" + m_settingsFileTab->GetFilePat h());
714 }
715
716 isRead = m_settingsFileTab->Read();
717 if (isRead)
718 {
719 ClearTab();
720
721 if (m_settingsFileTab->IsValidChecksum())
722 {
723 s_criticalSectionLocal.Lock();
724 {
725 m_propertiesTab = m_settingsFileTab->GetSectionData("Settings");
726
727 m_errorsTab = m_settingsFileTab->GetSectionData("Errors");
728
729 TProperties::iterator it = m_propertiesTab.find(SETTING_TAB_PLUGIN_ENA BLED);
730 if (it != m_propertiesTab.end())
731 {
732 m_isPluginEnabledTab = it->second != "false";
733 }
734 }
735 s_criticalSectionLocal.Unlock();
736 }
737 else
738 {
739 DEBUG_SETTINGS("SettingsTab:Invalid checksum - Deleting file")
740
741 DEBUG_ERROR_LOG(m_settingsFileTab->GetLastError(), PLUGIN_ERROR_SETTIN GS_TAB, PLUGIN_ERROR_SETTINGS_FILE_READ_CHECKSUM, "SettingsTab::Read - Checksum" )
742 isRead = false;
743 m_isDirtyTab = true;
744 }
745 }
746 else if (m_settingsFileTab->GetLastError() == ERROR_FILE_NOT_FOUND)
747 {
748 m_isDirtyTab = true;
749 }
750 else
751 {
752 DEBUG_ERROR_LOG(m_settingsFileTab->GetLastError(), PLUGIN_ERROR_SETTINGS_T AB, PLUGIN_ERROR_SETTINGS_FILE_READ, "SettingsTab::Read")
753 }
754
755
756 // Write file in case it is dirty or does not exist
757 WriteTab();
758
759 return isRead;
760 }
761
762 bool CPluginSettings::WriteTab(bool isDebug)
763 {
764 bool isWritten = true;
765
766 if (!m_isDirtyTab)
767 {
768 return isWritten;
769 }
770
771 if (isDebug)
772 {
773 DEBUG_GENERAL(L"*** Writing changed tab settings")
774 }
775
776 m_settingsFileTab->Clear();
777
778 // Properties & errors
779 CPluginIniFileW::TSectionData settings;
780 CPluginIniFileW::TSectionData errors;
781
782 s_criticalSectionLocal.Lock();
783 {
784 for (TProperties::iterator it = m_propertiesTab.begin(); it != m_propertiesT ab.end(); ++it)
785 {
786 settings[it->first] = it->second;
787 }
788
789 for (TProperties::iterator it = m_errorsTab.begin(); it != m_errorsTab.end() ; ++it)
790 {
791 errors[it->first] = it->second;
792 }
793 }
794 s_criticalSectionLocal.Unlock();
795
796 m_settingsFileTab->UpdateSection("Settings", settings);
797 m_settingsFileTab->UpdateSection("Errors", errors);
798
799 // Write file
800 isWritten = m_settingsFileTab->Write();
801 if (!isWritten)
802 {
803 DEBUG_ERROR_LOG(m_settingsFileTab->GetLastError(), PLUGIN_ERROR_SETTINGS_TAB , PLUGIN_ERROR_SETTINGS_FILE_WRITE, "SettingsTab::Write")
804 }
805
806 m_isDirtyTab = !isWritten;
807
808 return isWritten;
809 }
810
811
812 void CPluginSettings::EraseTab()
813 {
814 ClearTab();
815
816 m_isDirtyTab = true;
817
818 WriteTab();
819 }
820
821
822 bool CPluginSettings::IncrementTabCount()
823 {
824 int tabCount = 1;
825
826
827 if (s_isLightOnly)
828 {
829 return false;
830 }
831
832 CPluginSettingsTabLock lock;
833 if (lock.IsLocked())
834 {
835 SYSTEMTIME systemTime;
836 ::GetSystemTime(&systemTime);
837
838 CString today;
839 today.Format(L"%d-%d-%d", systemTime.wYear, systemTime.wMonth, systemTime.wD ay);
840
841 ReadTab(false);
842
843 s_criticalSectionLocal.Lock();
844 {
845 TProperties::iterator it = m_propertiesTab.find(SETTING_TAB_COUNT);
846 if (it != m_propertiesTab.end())
847 {
848 tabCount = _wtoi(it->second) + 1;
849 }
850
851 it = m_propertiesTab.find(SETTING_TAB_START_TIME);
852
853 //Is this a first IE instance?
854 HWND ieWnd = FindWindow(L"IEFrame", NULL);
855 if (ieWnd != NULL)
856 {
857 ieWnd = FindWindowEx(NULL, ieWnd, L"IEFrame", NULL);
858
859 }
860 if ((it != m_propertiesTab.end() && it->second != today))
861 {
862 tabCount = 1;
863 }
864 m_tabNumber.Format(L"%d", tabCount);
865
866 m_propertiesTab[SETTING_TAB_COUNT] = m_tabNumber;
867 m_propertiesTab[SETTING_TAB_START_TIME] = today;
868
869 // Main tab?
870 if (tabCount == 1)
871 {
872 m_propertiesTab[SETTING_TAB_DICTIONARY_VERSION] = "1";
873 m_propertiesTab[SETTING_TAB_SETTINGS_VERSION] = "1";
874 #ifdef SUPPORT_WHITELIST
875 m_propertiesTab[SETTING_TAB_WHITELIST_VERSION] = "1";
876 #endif
877 #ifdef SUPPORT_FILTER
878 m_propertiesTab[SETTING_TAB_FILTER_VERSION] = "1";
879 #endif
880 #ifdef SUPPORT_CONFIG
881 m_propertiesTab[SETTING_TAB_CONFIG_VERSION] = "1";
882 #endif
883 }
884 }
885 s_criticalSectionLocal.Unlock();
886
887 m_isDirtyTab = true;
888
889 WriteTab(false);
890 }
891
892 return tabCount == 1;
893 }
894
895
896 CString CPluginSettings::GetTabNumber() const
897 {
898 CString tabNumber;
899
900 s_criticalSectionLocal.Lock();
901 {
902 tabNumber = m_tabNumber;
903 }
904 s_criticalSectionLocal.Unlock();
905
906 return tabNumber;
907 }
908
909
910 bool CPluginSettings::DecrementTabCount()
911 {
912 int tabCount = 0;
913
914 CPluginSettingsTabLock lock;
915 if (lock.IsLocked())
916 {
917 ReadTab(false);
918
919 s_criticalSectionLocal.Lock();
920 {
921 TProperties::iterator it = m_propertiesTab.find(SETTING_TAB_COUNT);
922 if (it != m_propertiesTab.end())
923 {
924 tabCount = max(_wtoi(it->second) - 1, 0);
925
926 if (tabCount > 0)
927 {
928 m_tabNumber.Format(L"%d", tabCount);
929
930 m_propertiesTab[SETTING_TAB_COUNT] = m_tabNumber;
931 }
932 else
933 {
934 it = m_propertiesTab.find(SETTING_TAB_START_TIME);
935 if (it != m_propertiesTab.end())
936 {
937 m_propertiesTab.erase(it);
938 }
939
940 it = m_propertiesTab.find(SETTING_TAB_COUNT);
941 if (it != m_propertiesTab.end())
942 {
943 m_propertiesTab.erase(it);
944 }
945 }
946
947 m_isDirtyTab = true;
948 }
949 }
950 s_criticalSectionLocal.Unlock();
951
952 WriteTab(false);
953 }
954
955 return tabCount == 0;
956 }
957
958
959 void CPluginSettings::TogglePluginEnabled() 200 void CPluginSettings::TogglePluginEnabled()
960 { 201 {
961 CPluginSettingsTabLock lock; 202 CPluginSettingsTabLock lock;
962 if (lock.IsLocked()) 203 if (lock.IsLocked())
963 { 204 {
964 ReadTab(false);
965
966 s_criticalSectionLocal.Lock(); 205 s_criticalSectionLocal.Lock();
967 { 206 {
207 //TODO: Query if plugin is enabled from the AdblockPlusEngine
968 m_isPluginEnabledTab = m_isPluginEnabledTab ? false : true; 208 m_isPluginEnabledTab = m_isPluginEnabledTab ? false : true;
969 m_propertiesTab[SETTING_TAB_PLUGIN_ENABLED] = m_isPluginEnabledTab ? "true " : "false"; 209 //TODO: Set plugin enabled/disabled in AdblockPlusEngine
970 m_isDirtyTab = true;
971 } 210 }
972 s_criticalSectionLocal.Unlock(); 211 s_criticalSectionLocal.Unlock();
973
974 WriteTab(false);
975 } 212 }
976 } 213 }
977 void CPluginSettings::SetPluginDisabled() 214 void CPluginSettings::SetPluginDisabled()
978 { 215 {
979 CPluginSettingsTabLock lock; 216 CPluginSettingsTabLock lock;
980 if (lock.IsLocked()) 217 if (lock.IsLocked())
981 { 218 {
982 ReadTab(false);
983
984 s_criticalSectionLocal.Lock(); 219 s_criticalSectionLocal.Lock();
985 { 220 {
986 m_isPluginEnabledTab = false; 221 m_isPluginEnabledTab = false;
987 m_propertiesTab[SETTING_TAB_PLUGIN_ENABLED] = "false"; 222 //TODO: Set plugin disabled in AdblockPlusEngine
988 m_isDirtyTab = true;
989 } 223 }
990 s_criticalSectionLocal.Unlock(); 224 s_criticalSectionLocal.Unlock();
991
992 WriteTab(false);
993 } 225 }
994 } 226 }
995 void CPluginSettings::SetPluginEnabled() 227 void CPluginSettings::SetPluginEnabled()
996 { 228 {
997 CPluginSettingsTabLock lock; 229 CPluginSettingsTabLock lock;
998 if (lock.IsLocked()) 230 if (lock.IsLocked())
999 { 231 {
1000 ReadTab(false);
1001
1002 s_criticalSectionLocal.Lock(); 232 s_criticalSectionLocal.Lock();
1003 { 233 {
1004 m_isPluginEnabledTab = true; 234 m_isPluginEnabledTab = true;
1005 m_propertiesTab[SETTING_TAB_PLUGIN_ENABLED] = "true"; 235 //TODO: Set plugin enabled in AdblockPlusEngine
1006 m_isDirtyTab = true;
1007 } 236 }
1008 s_criticalSectionLocal.Unlock(); 237 s_criticalSectionLocal.Unlock();
1009
1010 WriteTab(false);
1011 } 238 }
1012 } 239 }
1013 bool CPluginSettings::GetPluginEnabled() const 240 bool CPluginSettings::GetPluginEnabled() const
1014 { 241 {
242 //TODO: Query AdblockPlusEngine
1015 return m_isPluginEnabledTab; 243 return m_isPluginEnabledTab;
1016 } 244 }
1017 245
246 bool CPluginSettings::GetStatusBarAsked()
247 {
248 return CPluginClient::GetInstance()->GetPref(L"statusbarasked", false);
249 }
250
251 void CPluginSettings::SetStatusBarAsked()
252 {
253 CPluginClient::GetInstance()->SetPref(L"statusbarasked", true);
254 }
255
1018 256
1019 void CPluginSettings::AddError(const CString& error, const CString& errorCode) 257 void CPluginSettings::AddError(const CString& error, const CString& errorCode)
1020 { 258 {
1021 DEBUG_SETTINGS(L"SettingsTab::AddError error:" + error + " code:" + errorCode) 259 DEBUG_SETTINGS(L"SettingsTab::AddError error:" + error + " code:" + errorCode)
1022
1023 CPluginSettingsTabLock lock;
1024 if (lock.IsLocked())
1025 {
1026 ReadTab(false);
1027
1028 s_criticalSectionLocal.Lock();
1029 {
1030 if (m_errorsTab.find(error) == m_errorsTab.end())
1031 {
1032 m_errorsTab[error] = errorCode;
1033 m_isDirtyTab = true;
1034 }
1035 }
1036 s_criticalSectionLocal.Unlock();
1037
1038 WriteTab(false);
1039 }
1040 } 260 }
1041 261
1042 262
1043 CString CPluginSettings::GetErrorList() const
1044 {
1045 CString errors;
1046
1047 s_criticalSectionLocal.Lock();
1048 {
1049 for (TProperties::const_iterator it = m_errorsTab.begin(); it != m_errorsTab .end(); ++it)
1050 {
1051 if (!errors.IsEmpty())
1052 {
1053 errors += ',';
1054 }
1055
1056 errors += it->first + '.' + it->second;
1057 }
1058 }
1059 s_criticalSectionLocal.Unlock();
1060
1061 return errors;
1062 }
1063
1064
1065 void CPluginSettings::RemoveErrors()
1066 {
1067 CPluginSettingsTabLock lock;
1068 if (lock.IsLocked())
1069 {
1070 ReadTab(false);
1071
1072 s_criticalSectionLocal.Lock();
1073 {
1074 if (m_errorsTab.size() > 0)
1075 {
1076 m_isDirtyTab = true;
1077 }
1078 m_errorsTab.clear();
1079 }
1080 s_criticalSectionLocal.Unlock();
1081
1082 WriteTab(false);
1083 }
1084 }
1085
1086
1087 bool CPluginSettings::GetForceConfigurationUpdateOnStart() const
1088 {
1089 bool isUpdating = false;
1090
1091 CPluginSettingsTabLock lock;
1092 if (lock.IsLocked())
1093 {
1094 s_criticalSectionLocal.Lock();
1095 {
1096 isUpdating = m_propertiesTab.find(SETTING_TAB_UPDATE_ON_START) != m_proper tiesTab.end();
1097 }
1098 s_criticalSectionLocal.Unlock();
1099 }
1100
1101 return isUpdating;
1102 }
1103
1104
1105 void CPluginSettings::ForceConfigurationUpdateOnStart(bool isUpdating)
1106 {
1107 CPluginSettingsTabLock lock;
1108 if (lock.IsLocked())
1109 {
1110 ReadTab(false);
1111
1112 s_criticalSectionLocal.Lock();
1113 {
1114 TProperties::iterator it = m_propertiesTab.find(SETTING_TAB_UPDATE_ON_STAR T);
1115
1116 if (isUpdating && it == m_propertiesTab.end())
1117 {
1118 m_propertiesTab[SETTING_TAB_UPDATE_ON_START] = "true";
1119 m_propertiesTab[SETTING_TAB_UPDATE_ON_START_REMOVE] = "false";
1120
1121 m_isDirtyTab = true;
1122 }
1123 else if (!isUpdating)
1124 {
1125 // OK to remove?
1126 TProperties::iterator itRemove = m_propertiesTab.find(SETTING_TAB_UPDATE _ON_START_REMOVE);
1127
1128 if (itRemove == m_propertiesTab.end() || itRemove->second == "true")
1129 {
1130 if (it != m_propertiesTab.end())
1131 {
1132 m_propertiesTab.erase(it);
1133 }
1134
1135 if (itRemove != m_propertiesTab.end())
1136 {
1137 m_propertiesTab.erase(itRemove);
1138 }
1139
1140 m_isDirtyTab = true;
1141 }
1142 }
1143 }
1144 s_criticalSectionLocal.Unlock();
1145
1146 WriteTab(false);
1147 }
1148 }
1149
1150 void CPluginSettings::RemoveForceConfigurationUpdateOnStart()
1151 {
1152 CPluginSettingsTabLock lock;
1153 if (lock.IsLocked())
1154 {
1155 ReadTab(false);
1156
1157 s_criticalSectionLocal.Lock();
1158 {
1159 // OK to remove?
1160 TProperties::iterator itRemove = m_propertiesTab.find(SETTING_TAB_UPDATE_O N_START_REMOVE);
1161
1162 if (itRemove != m_propertiesTab.end())
1163 {
1164 m_propertiesTab.erase(itRemove);
1165 m_isDirtyTab = true;
1166 }
1167 }
1168 s_criticalSectionLocal.Unlock();
1169
1170 WriteTab(false);
1171 }
1172 }
1173
1174 void CPluginSettings::RefreshTab()
1175 {
1176 CPluginSettingsTabLock lock;
1177 if (lock.IsLocked())
1178 {
1179 ReadTab();
1180 }
1181 }
1182
1183
1184 int CPluginSettings::GetTabVersion(const CString& key) const
1185 {
1186 int version = 0;
1187
1188 s_criticalSectionLocal.Lock();
1189 {
1190 TProperties::const_iterator it = m_propertiesTab.find(key);
1191 if (it != m_propertiesTab.end())
1192 {
1193 version = _wtoi(it->second);
1194 }
1195 }
1196 s_criticalSectionLocal.Unlock();
1197
1198 return version;
1199 }
1200
1201 void CPluginSettings::IncrementTabVersion(const CString& key)
1202 {
1203 CPluginSettingsTabLock lock;
1204 if (lock.IsLocked())
1205 {
1206 ReadTab(false);
1207
1208 s_criticalSectionLocal.Lock();
1209 {
1210 int version = 1;
1211
1212 TProperties::iterator it = m_propertiesTab.find(key);
1213 if (it != m_propertiesTab.end())
1214 {
1215 version = _wtoi(it->second) + 1;
1216 }
1217
1218 CString versionString;
1219 versionString.Format(L"%d", version);
1220
1221 m_propertiesTab[key] = versionString;
1222 }
1223 s_criticalSectionLocal.Unlock();
1224
1225 m_isDirtyTab = true;
1226
1227 WriteTab(false);
1228 }
1229 }
1230
1231
1232 // ============================================================================ 263 // ============================================================================
1233 // Whitelist settings 264 // Whitelist settings
1234 // ============================================================================ 265 // ============================================================================
1235 266
1236 #ifdef SUPPORT_WHITELIST 267 #ifdef SUPPORT_WHITELIST
1237 268
1238 void CPluginSettings::ClearWhitelist() 269 void CPluginSettings::ClearWhitelist()
1239 { 270 {
1240 s_criticalSectionLocal.Lock(); 271 s_criticalSectionLocal.Lock();
1241 { 272 {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
1334 365
1335 m_WindowsBuildNumber = osvi.dwBuildNumber; 366 m_WindowsBuildNumber = osvi.dwBuildNumber;
1336 } 367 }
1337 368
1338 return m_WindowsBuildNumber; 369 return m_WindowsBuildNumber;
1339 } 370 }
1340 371
1341 void CPluginSettings::SetSubscription(const std::wstring& url) 372 void CPluginSettings::SetSubscription(const std::wstring& url)
1342 { 373 {
1343 CPluginClient::GetInstance()->SetSubscription(url); 374 CPluginClient::GetInstance()->SetSubscription(url);
1344 RefreshFilterlist();
1345 RefreshWhitelist(); 375 RefreshWhitelist();
1346 } 376 }
1347 377
1348 CString CPluginSettings::GetSubscription() 378 CString CPluginSettings::GetSubscription()
1349 { 379 {
1350 std::vector<SubscriptionDescription> subscriptions = CPluginClient::GetInstanc e()->GetListedSubscriptions(); 380 std::vector<SubscriptionDescription> subscriptions = CPluginClient::GetInstanc e()->GetListedSubscriptions();
1351 if (subscriptions.size() > 0) 381 if (subscriptions.size() > 0)
1352 return CString(subscriptions.front().url.c_str()); 382 return CString(subscriptions.front().url.c_str());
1353 else 383 else
1354 return CString(L""); 384 return CString(L"");
1355 } 385 }
1356 386
1357 387
1358 void CPluginSettings::RefreshFilterlist()
1359 {
1360 CPluginClient::GetInstance()->UpdateAllSubscriptions();
1361 }
1362 388
1363 #endif // SUPPORT_WHITELIST 389 #endif // SUPPORT_WHITELIST
OLDNEW

Powered by Google App Engine
This is Rietveld