summaryrefslogtreecommitdiff
path: root/plugins/NotesAndReminders/src/miscutils.cpp
blob: e592b94699b8a8fac8e84d328423279ee23f74ec (plain)
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
#include "globals.h"

BOOL (WINAPI *MySetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
HANDLE (WINAPI *MyMonitorFromWindow)(HWND,DWORD);
BOOL (WINAPI *MyTzSpecificLocalTimeToSystemTime)(LPTIME_ZONE_INFORMATION,LPSYSTEMTIME,LPSYSTEMTIME);
BOOL (WINAPI *MySystemTimeToTzSpecificLocalTime)(LPTIME_ZONE_INFORMATION,LPSYSTEMTIME,LPSYSTEMTIME);

WORD ConvertHotKeyToControl(WORD HK)
{
	WORD R = 0;
	if ((HK & MOD_CONTROL) == MOD_CONTROL) R = R | HOTKEYF_CONTROL;
	if ((HK & MOD_ALT) == MOD_ALT) R = R | HOTKEYF_ALT;
	if ((HK & MOD_SHIFT) == MOD_SHIFT) R = R | HOTKEYF_SHIFT;
	return R;
}

WORD ConvertControlToHotKey(WORD HK)
{
	WORD R = 0;
	if ((HK & HOTKEYF_CONTROL) == HOTKEYF_CONTROL) R = R | MOD_CONTROL;
	if ((HK & HOTKEYF_ALT) == HOTKEYF_ALT) R = R | MOD_ALT;
	if ((HK & HOTKEYF_SHIFT) == HOTKEYF_SHIFT) R = R | MOD_SHIFT;
	return R;
}

void WriteSettingInt(HANDLE hContact,char *ModuleName,char *SettingName,int Value)
{
	DBCONTACTWRITESETTING cws = {0};
	DBVARIANT dbv = {0};
	dbv.type = DBVT_DWORD;
	dbv.dVal = Value;
	cws.szModule = ModuleName;
	cws.szSetting = SettingName;
	cws.value = dbv;
	CallService(MS_DB_CONTACT_WRITESETTING, (WPARAM)hContact, (DWORD)&cws);
}

int ReadSettingInt(HANDLE hContact,char *ModuleName,char *SettingName,int Default)
{
	DBCONTACTGETSETTING cws = {0};
	DBVARIANT dbv = {0};
	dbv.type = DBVT_DWORD;
	dbv.dVal = Default;
	cws.szModule = ModuleName;
	cws.szSetting = SettingName;
	cws.pValue = &dbv;
	if (CallService(MS_DB_CONTACT_GETSETTING,(DWORD)hContact,(DWORD)&cws)) 
		return Default;
	else 
		return dbv.dVal;
}

void DeleteSetting(HANDLE hContact,char *ModuleName,char *SettingName)
{
	DBCONTACTGETSETTING dbcgs = {0};
	dbcgs.szModule = ModuleName;
	dbcgs.szSetting = SettingName;
	dbcgs.pValue = NULL;
	CallService(MS_DB_CONTACT_DELETESETTING,(DWORD)hContact,(DWORD)&dbcgs);
}

void FreeSettingBlob(WORD pSize,void *pbBlob)
{
	DBVARIANT dbv = {0};
	dbv.type = DBVT_BLOB;
	dbv.cpbVal = pSize;
	dbv.pbVal = (BYTE*)pbBlob;
	CallService(MS_DB_CONTACT_FREEVARIANT,0,(DWORD)&dbv);
}

void WriteSettingBlob(HANDLE hContact,char *ModuleName,char *SettingName,WORD pSize,void *pbBlob)
{
	DBCONTACTWRITESETTING cgs = {0};
	DBVARIANT dbv = {0};
	dbv.type = DBVT_BLOB;
	dbv.cpbVal = pSize;
	dbv.pbVal = (BYTE*)pbBlob;
	cgs.szModule = ModuleName;
	cgs.szSetting = SettingName;
	cgs.value = dbv;
	CallService(MS_DB_CONTACT_WRITESETTING,(DWORD)hContact,(DWORD)&cgs);
}

void ReadSettingBlob(HANDLE hContact, char *ModuleName, char *SettingName, WORD *pSize, void **pbBlob)
{
	DBCONTACTGETSETTING cgs = {0};
	DBVARIANT dbv = {0};
	dbv.type = DBVT_BLOB;
	cgs.szModule = ModuleName;
	cgs.szSetting = SettingName;
	cgs.pValue = &dbv;
	if ( CallService(MS_DB_CONTACT_GETSETTING,(DWORD)hContact,(DWORD)&cgs)) {
		*pSize = 0;
		*pbBlob = NULL;
	}
	else {
		*pSize = LOWORD(dbv.cpbVal);
		*pbBlob = (int*)dbv.pbVal;
	}
}

void WriteSettingIntArray(HANDLE hContact,char *ModuleName,char *SettingName,const int *Value, int Size)
{
	WriteSettingBlob(hContact,ModuleName,SettingName,sizeof(int)*Size,(void*)Value);
}

bool ReadSettingIntArray(HANDLE hContact,char *ModuleName,char *SettingName,int *Value, int Size)
{
	WORD sz = 4096;
	int *pData;
	ReadSettingBlob(hContact, ModuleName, SettingName, &sz, (void**)&pData);
	if (!pData)
		return false;

	bool bResult = false;

	if (sz == sizeof(int)*Size) {
		memcpy(Value, pData, sizeof(int)*Size);
		bResult = true;
	}

	FreeSettingBlob(sz,pData);
	return bResult;
}

void TreeAdd(TREEELEMENT **root, void *Data)
{
	TREEELEMENT *NTE = (TREEELEMENT*)malloc(sizeof(TREEELEMENT));
	if (NTE) {
		NTE->ptrdata = Data;
		NTE->next = *root;
		*root = NTE;
	}
}

void TreeAddSorted(TREEELEMENT **root,void *Data,int (*CompareCb)(TREEELEMENT*,TREEELEMENT*))
{
	if (!*root) {
		// list empty, just add normally
		TreeAdd(root,Data);
		return;
	}

	TREEELEMENT *NTE = (TREEELEMENT*)malloc(sizeof(TREEELEMENT));
	if (!NTE)
		return;

	NTE->ptrdata = Data;
	NTE->next = NULL;

	// insert sorted

	TREEELEMENT *Prev = NULL;
	TREEELEMENT *TTE = *root;

	while (TTE) {
		if (CompareCb(NTE, TTE) < 0) {
			if (Prev) {
				Prev->next = NTE;
				NTE->next = TTE;
			}
			else {
				// first in list
				NTE->next = TTE;
				*root = NTE;
			}
			return;
		}

		Prev = TTE;
		TTE = (TREEELEMENT*)TTE->next;
	}

	// add last
	Prev->next = NTE;
}

void TreeDelete(TREEELEMENT **root,void *iItem)
{
	TREEELEMENT *TTE = *root, *Prev = NULL;
	if (!TTE)
		return;

	while ((TTE) && (TTE->ptrdata != iItem)) {
		Prev = TTE;
		TTE = (TREEELEMENT*)TTE->next;
	}
	if (TTE) {
		if (Prev)
			Prev->next = TTE->next;
		else 
			*root = (TREEELEMENT*)TTE->next;
		SAFE_FREE((void**)&TTE);
	}
}

void *TreeGetAt(TREEELEMENT *root, int iItem)
{
	if (!root)
		return NULL;

	TREEELEMENT *TTE = root;
	int i = 0;
	while ((TTE) && (i != iItem)) {
		TTE = (TREEELEMENT*)TTE->next;
		i++;
	}
	return (!TTE) ? NULL : TTE->ptrdata;
}

int TreeGetCount(TREEELEMENT *root)
{
	if (!root)
		return 0;
	
	int i = 0;
	TREEELEMENT *TTE = root;
	while (TTE) {
		TTE = (TREEELEMENT*)TTE->next;
		i++;
	}
	return i;
}