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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
#include "stdafx.h"
void FreeModuleSettingLL(ModuleSettingLL *msll)
{
if (msll == NULL)
return;
ModSetLinkLinkItem *item = msll->first;
ModSetLinkLinkItem *temp;
while (item) {
mir_free(item->name);
temp = item;
item = (ModSetLinkLinkItem *)item->next;
mir_free(temp);
}
msll->first = 0;
msll->last = 0;
}
int enumModulesSettingsProc(const char *setting, DWORD, LPARAM lParam)
{
ModuleSettingLL *msll = (ModuleSettingLL *)lParam;
if (!msll->first) {
msll->first = (ModSetLinkLinkItem *)mir_alloc(sizeof(ModSetLinkLinkItem));
if (!msll->first)
return 1;
msll->first->name = mir_strdup(setting);
msll->first->next = 0;
msll->last = msll->first;
}
else {
ModSetLinkLinkItem *item = (ModSetLinkLinkItem *)mir_alloc(sizeof(ModSetLinkLinkItem));
if (!item)
return 1;
msll->last->next = item;
msll->last = item;
item->name = mir_strdup(setting);
item->next = 0;
}
return 0;
}
int EnumModules(ModuleSettingLL *msll) // 1 = success, 0 = fail
{
msll->first = 0;
msll->last = 0;
if (db_enum_modules(enumModulesSettingsProc, msll)) {
msg(TranslateT("Error loading module list"));
return 0;
}
return 1;
}
int enumSettingsProc(const char *setting, LPARAM lParam)
{
return enumModulesSettingsProc(setting, 0, lParam);
}
int EnumSettings(MCONTACT hContact, const char *module, ModuleSettingLL *msll)
{
// enum all setting the contact has for the module
msll->first = 0;
msll->last = 0;
if (db_enum_settings(hContact, enumSettingsProc, module, msll)) {
msg(TranslateT("Error loading setting list"));
return 0;
}
return 1;
}
int CheckIfModuleIsEmptyProc(const char *, LPARAM)
{
return 1;
}
int IsModuleEmpty(MCONTACT hContact, const char *module)
{
return 0 > db_enum_settings(hContact, CheckIfModuleIsEmptyProc, module);
}
static int stringCompare(const char *p1, const char *p2)
{
return mir_strcmp(p1, p2);
}
LIST<char> m_lResidentSettings(10, stringCompare);
LIST<char> m_lResidentModules(5, stringCompare);
int enumResidentProc(const char *setting, DWORD, LPARAM)
{
m_lResidentSettings.insert(mir_strdup(setting));
char str[FLD_SIZE];
const char *end = strstr(setting, "/");
if (end && (end - setting) < _countof(str)) {
mir_strncpy(str, setting, end - setting + 1);
if (m_lResidentModules.getIndex(str) == -1)
m_lResidentModules.insert(mir_strdup(str));
}
return 0;
}
int LoadResidentSettings()
{
if (g_db)
return !g_db->EnumResidentSettings(enumResidentProc, 0);
return 0;
}
void FreeResidentSettings()
{
for (int i = 0; i < m_lResidentSettings.getCount(); i++) {
mir_free(m_lResidentSettings[i]);
}
m_lResidentSettings.destroy();
for (int i = 0; i < m_lResidentModules.getCount(); i++) {
mir_free(m_lResidentModules[i]);
}
m_lResidentModules.destroy();
}
int IsResidentSetting(const char *module, const char *setting)
{
if (!m_lResidentSettings.getCount()) return 0;
if (m_lResidentModules.getIndex((char*)module) == -1) return 0;
if (!setting) return 1;
char str[2*FLD_SIZE];
mir_strncpy(str, module, _countof(str)-1);
mir_strcat(str, "/");
mir_strncat(str, setting, _countof(str));
return m_lResidentSettings.getIndex(str) != -1;
}
int EnumResidentSettings(const char *module, ModuleSettingLL *msll)
{
msll->first = 0;
msll->last = 0;
if (!module) return 0;
if (!m_lResidentSettings.getCount()) return 0;
if (m_lResidentModules.getIndex((char*)module) == -1) return 0;
int len = (int)mir_strlen(module);
int cnt = 0;
for (int i = 0; i < m_lResidentSettings.getCount(); i++) {
if (strncmp(module, m_lResidentSettings[i], len))
continue;
if (m_lResidentSettings[i][len] != '/' || m_lResidentSettings[i][len+1] == 0) continue;
enumModulesSettingsProc(&m_lResidentSettings[i][len+1], 0, (LPARAM)msll);
cnt++;
}
return cnt;
}
int EnumResidentModules(ModuleSettingLL *msll)
{
msll->first = 0;
msll->last = 0;
if (!m_lResidentModules.getCount()) return 0;
int cnt = 0;
for (int i = 0; i < m_lResidentModules.getCount(); i++) {
enumModulesSettingsProc(m_lResidentModules[i], 0, (LPARAM)msll);
cnt++;
}
return cnt;
}
static int fixing = 0;
// previously saved in DB resident settings are unaccessible.
// so let's find them and delete from DB
int fixResidentSettings()
{
if (!m_lResidentSettings.getCount() || fixing) return 0;
ModuleSettingLL ModuleList, SettingList;
ModSetLinkLinkItem *module, *setting;
MCONTACT hContact = 0;
int NULLContactDone = 0;
char str[2*FLD_SIZE];
int cnt = 0;
if (!EnumModules(&ModuleList)) return 0;
fixing = 1;
while (hwnd2mainWindow) {
if (!hContact) {
if (NULLContactDone)
break;
else {
NULLContactDone = 1;
hContact = db_find_first();
}
}
else
hContact = db_find_next(hContact);
for (module = ModuleList.first; module; module = module->next) {
if (IsModuleEmpty(hContact, module->name) || m_lResidentModules.getIndex(module->name) == -1)
continue;
if (!EnumSettings(hContact, module->name, &SettingList))
continue;
for (setting = SettingList.first; setting; setting = setting->next) {
mir_strncpy(str, module->name, _countof(str)-1);
mir_strcat(str, "/");
mir_strncat(str, setting->name, _countof(str));
int idx = m_lResidentSettings.getIndex(str);
if (idx == -1)
continue;
g_db->SetSettingResident(0, str);
db_unset(hContact, module->name, setting->name);
g_db->SetSettingResident(1, str);
cnt++;
} // for(setting)
FreeModuleSettingLL(&SettingList);
} // for(module)
}
FreeModuleSettingLL(&ModuleList);
fixing = 0;
return cnt;
}
|