-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
UserConfig.cpp
181 lines (142 loc) · 5.72 KB
/
UserConfig.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "stdafx.h"
#include "UserConfig.h"
namespace
{
CPath GetIniFilePath()
{
CPath iniFilePath;
GetModuleFileName(NULL, iniFilePath.m_strPath.GetBuffer(MAX_PATH), MAX_PATH);
iniFilePath.m_strPath.ReleaseBuffer();
iniFilePath.RenameExtension(L".ini");
return iniFilePath;
}
// Receives a path with an optional index, e.g.:
// C:\path\to\awesome.exe,101
// Removes the index from the string and returns it.
int ExtractIconIndex(PWSTR pathWithOptionalIndex)
{
size_t len = wcslen(pathWithOptionalIndex);
for(size_t i = len - 1; i > 0; i--)
{
WCHAR p = pathWithOptionalIndex[i];
if(p == ',')
{
if(i == len - 1)
break;
pathWithOptionalIndex[i] = L'\0';
return _wtoi(pathWithOptionalIndex + i + 1);
}
if(p < L'0' && p > L'9')
break;
}
return 0;
}
CPath RelativeToAbsolutePathExpanded(PWSTR relativePath)
{
CPath moduleFileNamePath;
GetModuleFileName(NULL, moduleFileNamePath.m_strPath.GetBuffer(MAX_PATH), MAX_PATH);
moduleFileNamePath.m_strPath.ReleaseBuffer();
moduleFileNamePath.RemoveFileSpec();
CString relativePathExpanded;
DWORD relativePathExpandedSize = ExpandEnvironmentStrings(relativePath, nullptr, 0);
ExpandEnvironmentStrings(relativePath, relativePathExpanded.GetBuffer(relativePathExpandedSize), relativePathExpandedSize);
relativePathExpanded.ReleaseBuffer();
CPath result;
result.Combine(moduleFileNamePath, relativePathExpanded);
return result;
}
}
UserConfig::UserConfig()
{
LoadFromIniFile();
}
bool UserConfig::LoadFromIniFile()
{
CPath iniFilePath = GetIniFilePath();
WCHAR szBuffer[1025];
m_mouseHotKey.key = GetPrivateProfileInt(L"mouse", L"key", 0, iniFilePath);
m_mouseHotKey.ctrl = GetPrivateProfileInt(L"mouse", L"ctrl", 0, iniFilePath);
m_mouseHotKey.alt = GetPrivateProfileInt(L"mouse", L"alt", 0, iniFilePath);
m_mouseHotKey.shift = GetPrivateProfileInt(L"mouse", L"shift", 0, iniFilePath);
m_keybdHotKey.key = GetPrivateProfileInt(L"keyboard", L"key", 0, iniFilePath);
m_keybdHotKey.ctrl = GetPrivateProfileInt(L"keyboard", L"ctrl", 0, iniFilePath);
m_keybdHotKey.alt = GetPrivateProfileInt(L"keyboard", L"alt", 0, iniFilePath);
m_keybdHotKey.shift = GetPrivateProfileInt(L"keyboard", L"shift", 0, iniFilePath);
m_uiLanguage = static_cast<LANGID>(GetPrivateProfileInt(L"config", L"ui_language", 0, iniFilePath));
m_checkForUpdates = GetPrivateProfileInt(L"config", L"check_for_updates", 1, iniFilePath);
m_autoCopySelection = GetPrivateProfileInt(L"config", L"auto_copy_selection", 0, iniFilePath);
m_hideWndOnStartup = GetPrivateProfileInt(L"config", L"hide_wnd_on_startup", 0, iniFilePath);
m_hideTrayIcon = GetPrivateProfileInt(L"config", L"hide_tray_icon", 0, iniFilePath);
m_textBoxNonResiable = GetPrivateProfileInt(L"config", L"text_box_non_resizable", 0, iniFilePath);
GetPrivateProfileString(L"config", L"font_name", L"", m_fontName.GetBuffer(LF_FACESIZE), LF_FACESIZE, iniFilePath);
m_fontName.ReleaseBuffer();
m_fontSize = GetPrivateProfileInt(L"config", L"font_size", 0, iniFilePath);
m_unicodeSpacesToAscii = GetPrivateProfileInt(L"config", L"unicode_spaces_to_ascii", 0, iniFilePath);
m_textRetrievalMethod = TextRetrievalMethod::default;
GetPrivateProfileString(L"config", L"text_retrieval_method", L"", szBuffer, ARRAYSIZE(szBuffer), iniFilePath);
if(_wcsicmp(szBuffer, L"msaa") == 0)
{
m_textRetrievalMethod = TextRetrievalMethod::msaa;
}
else if(_wcsicmp(szBuffer, L"uia") == 0)
{
m_textRetrievalMethod = TextRetrievalMethod::uia;
}
m_webButtonsIconSize = GetPrivateProfileInt(L"web_buttons", L"icon_size", 16, iniFilePath);
m_webButtonsPerRow = GetPrivateProfileInt(L"web_buttons", L"buttons_per_row", 8, iniFilePath);
for(int i = 1; ; i++)
{
CString iniSectionName;
iniSectionName.Format(L"web_button_%d", i);
GetPrivateProfileString(iniSectionName, L"command", L"", szBuffer, ARRAYSIZE(szBuffer), iniFilePath);
if(*szBuffer == L'\0')
break;
WebButtonInfo webButtonInfo;
webButtonInfo.command = szBuffer;
GetPrivateProfileString(iniSectionName, L"name", L"", szBuffer, ARRAYSIZE(szBuffer), iniFilePath);
webButtonInfo.name = szBuffer;
GetPrivateProfileString(iniSectionName, L"icon", L"", szBuffer, ARRAYSIZE(szBuffer), iniFilePath);
if(*szBuffer != L'\0')
{
webButtonInfo.iconIndex = ExtractIconIndex(szBuffer);
webButtonInfo.iconPath = RelativeToAbsolutePathExpanded(szBuffer);
}
GetPrivateProfileString(iniSectionName, L"key", L"", szBuffer, ARRAYSIZE(szBuffer), iniFilePath);
webButtonInfo.acceleratorKey = *szBuffer;
webButtonInfo.width = GetPrivateProfileInt(iniSectionName, L"width", 0, iniFilePath);
webButtonInfo.height = GetPrivateProfileInt(iniSectionName, L"height", 0, iniFilePath);
m_webButtonInfos.push_back(webButtonInfo);
}
for(int i = 1; ; i++)
{
CString iniKeyName;
iniKeyName.Format(L"%d", i);
GetPrivateProfileString(L"exclude", iniKeyName, L"", szBuffer, ARRAYSIZE(szBuffer), iniFilePath);
if(*szBuffer == L'\0')
break;
m_excludedPrograms.push_back(szBuffer);
}
return true;
}
bool UserConfig::SaveToIniFile() const
{
CPath iniFilePath = GetIniFilePath();
if(!iniFilePath)
return false;
bool succeeded = true;
CString str;
str.Format(L"%d", m_mouseHotKey.key);
if(!WritePrivateProfileString(L"mouse", L"key", str, iniFilePath))
succeeded = false;
str.Format(L"%d", m_mouseHotKey.ctrl ? 1 : 0);
if(!WritePrivateProfileString(L"mouse", L"ctrl", str, iniFilePath))
succeeded = false;
str.Format(L"%d", m_mouseHotKey.alt ? 1 : 0);
if(!WritePrivateProfileString(L"mouse", L"alt", str, iniFilePath))
succeeded = false;
str.Format(L"%d", m_mouseHotKey.shift ? 1 : 0);
if(!WritePrivateProfileString(L"mouse", L"shift", str, iniFilePath))
succeeded = false;
// Don't write the other configuration, as there's no GUI for it.
return succeeded;
}