summaryrefslogtreecommitdiff
path: root/plugins/Sessions/Src/Utils.cpp
blob: c0f2e5ea9036498ad4ada2378fee07800cd31d73 (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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
Sessions Management plugin for Miranda IM

Copyright (C) 2007-2008 Danil Mozhar

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

void AddSessionMark(MCONTACT hContact, int mode, char bit)
{
	if (mode == 0) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "LastSessionsMarks"));
		if (szValue) {
			char temp_1 = szValue[0];
			for (int i = 0; i < g_ses_limit; i++) {
				char temp_2 = szValue[i + 1];
				szValue[i + 1] = temp_1;
				temp_1 = temp_2;
			}
			for (int i = g_ses_limit; i < 10; i++)
				szValue[i] = '0';
			szValue[0] = bit;
			db_set_s(hContact, MODNAME, "LastSessionsMarks", szValue);
		}
		else if (bit == '1')
			db_set_s(hContact, MODNAME, "LastSessionsMarks", "10000000000");
	}
	else if (mode == 1) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "UserSessionsMarks"));
		if (szValue) {
			char *pszBuffer;
			if (mir_strlen(szValue) < g_ses_count) {
				pszBuffer = (char*)mir_alloc(g_ses_count + 1);
				memset(pszBuffer, 0, (g_ses_count + 1));
				mir_strcpy(pszBuffer, szValue);
			}
			else pszBuffer = szValue.detach();

			char temp_1 = pszBuffer[0];
			for (int i = 0; i < g_ses_count; i++) {
				char temp_2 = pszBuffer[i + 1];
				pszBuffer[i + 1] = temp_1;
				temp_1 = temp_2;
			}
			pszBuffer[0] = bit;
			db_set_s(hContact, MODNAME, "UserSessionsMarks", pszBuffer);

			mir_free(pszBuffer);
		}
		else if (bit == '1')
			db_set_s(hContact, MODNAME, "UserSessionsMarks", "10000000000");
		else
			db_set_s(hContact, MODNAME, "UserSessionsMarks", "00000000000");
	}
}

void RemoveSessionMark(MCONTACT hContact, int mode, int marknum)
{
	if (mode == 0) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "LastSessionsMarks"));
		if (szValue) {
			for (int i = marknum; i < g_ses_limit; i++)
				szValue[i] = szValue[i + 1];

			for (int i = g_ses_limit; i < 10; i++)
				szValue[i] = '0';

			db_set_s(hContact, MODNAME, "LastSessionsMarks", szValue);
		}
	}
	else if (mode == 1) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "UserSessionsMarks"));
		if (szValue) {
			for (int i = marknum; i < g_ses_limit; i++)
				szValue[i] = szValue[i + 1];

			db_set_s(hContact, MODNAME, "UserSessionsMarks", szValue);
		}
	}
}

void SetSessionMark(MCONTACT hContact, int mode, char bit, unsigned int marknum)
{
	if (mode == 0) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "LastSessionsMarks"));
		if (szValue) {
			szValue[marknum] = bit;
			db_set_s(hContact, MODNAME, "LastSessionsMarks", szValue);
		}
	}
	else if (mode == 1) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "UserSessionsMarks"));
		if (szValue) {
			szValue[marknum] = bit;
			db_set_s(hContact, MODNAME, "UserSessionsMarks", szValue);
		}
	}
}

bool LoadContactsFromMask(MCONTACT hContact, int mode, int count)
{
	ptrA szValue;
	if (mode == 0)
		szValue = db_get_sa(hContact, MODNAME, "LastSessionsMarks");
	else if (mode == 1)
		szValue = db_get_sa(hContact, MODNAME, "UserSessionsMarks");

	if (szValue == NULL)
		return false;

	return szValue[count] == '1';
}

void AddInSessionOrder(MCONTACT hContact, int mode, int ordernum, int writemode)
{
	char szFormNumBuf[100];
	mir_snprintf(szFormNumBuf, "%02u", ordernum);

	if (mode == 0) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "LastSessionsMarks"));
		if (szValue) {
			int len = (int)mir_strlen(szValue);
			if (!len)
				len = 20;

			char *temp2 = (char*)_alloca(len - 1);
			strncpy(temp2, szValue, len - 2);
			temp2[len - 2] = '\0';

			char *temp = (char*)_alloca(len + 1);
			mir_snprintf(temp, len + 1, "%02u%s", ordernum, temp2);

			for (int i = (g_ses_limit * 2); i < 20; i++)
				temp[i] = '0';

			db_set_s(hContact, MODNAME, "LastSessionsOrder", temp);
		}
		else if (writemode == 1) {
			mir_snprintf(szFormNumBuf, "%02u%s", ordernum, "000000000000000000");
			db_set_s(hContact, MODNAME, "LastSessionsOrder", szFormNumBuf);
		}
	}
	else if (mode == 1) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "UserSessionsOrder"));
		if (szValue) {
			char *pszBuffer;
			if (mir_strlen(szValue) < (g_ses_count * 2)) {
				pszBuffer = (char*)mir_alloc((g_ses_count * 2) + 1);
				memset(pszBuffer, 0, ((g_ses_count * 2) + 1));
				mir_strcpy(pszBuffer, szValue);
			}
			else pszBuffer = mir_strdup(szValue);

			int len = (int)mir_strlen(pszBuffer);
			len = (len == 0) ? 20 : len + 2;
			char *temp = (char*)_alloca(len + 1);
			mir_snprintf(temp, len + 1, "%02u%s", ordernum, szValue);

			db_set_s(hContact, MODNAME, "UserSessionsOrder", temp);
			mir_free(pszBuffer);
		}
		else if (writemode == 1)
			db_set_s(hContact, MODNAME, "UserSessionsOrder", szFormNumBuf);
		else
			db_set_s(hContact, MODNAME, "UserSessionsOrder", "00");
	}
}

int GetInSessionOrder(MCONTACT hContact, int mode, int count)
{
	char szTemp[3];
	if (mode == 0) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "LastSessionsOrder"));
		if (szValue) {
			strncpy_s(szTemp, &szValue[count * 2], 2);
			return atoi(szTemp);
		}
	}
	else if (mode == 1) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "UserSessionsOrder"));
		if (szValue) {
			strncpy_s(szTemp, &szValue[count * 2], 2);
			return atoi(szTemp);
		}
	}
	return 0;
}

void SetInSessionOrder(MCONTACT hContact, int mode, int count, unsigned int ordernum)
{
	char szTemp[3];
	mir_snprintf(szTemp, "%02u", ordernum);

	if (mode == 0) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "LastSessionsOrder"));
		if (szValue) {
			szValue[count * 2] = szTemp[0];
			szValue[count * 2 + 1] = szTemp[1];
			db_set_s(hContact, MODNAME, "LastSessionsOrder", szValue);
		}
	}
	else if (mode == 1) {
		ptrA szValue(db_get_sa(hContact, MODNAME, "UserSessionsOrder"));
		if (szValue) {
			szValue[count * 2] = szTemp[0];
			szValue[count * 2 + 1] = szTemp[1];
			db_set_s(hContact, MODNAME, "UserSessionsOrder", szValue);
		}
	}
}

BOOL ResaveSettings(char *szName, int iFirst, int iLimit, wchar_t *szBuffer)
{
	for (int i = iFirst; i < iLimit; i++) {
		if (szBuffer == NULL)
			break;

		char szNameBuf[256];
		mir_snprintf(szNameBuf, "%s_%u", szName, i);

		wchar_t *ptszTemp = db_get_wsa(NULL, MODNAME, szNameBuf);
		db_set_ws(NULL, MODNAME, szNameBuf, szBuffer);
		mir_free(szBuffer);

		BYTE marked = IsMarkedUserDefSession(i);
		MarkUserDefSession(i, (BYTE)((i == iFirst) ? IsMarkedUserDefSession(iFirst - 1) : marked));

		if (ptszTemp == NULL) // read failed
			return 0;

		szBuffer = ptszTemp;
	}

	mir_free(szBuffer);
	return 1;
}

int AddToCurSession(MCONTACT wparam, LPARAM)
{
	if (CheckForDuplicate(session_list, wparam) == -1) {
		for (int i = 0;; i++) {
			if (session_list[i] == 0) {
				session_list[i] = wparam;
				break;
			}
		}
	}
	return 0;
}

int DelFromCurSession(MCONTACT wparam, LPARAM)
{
	for (int i = 0; session_list[i] != 0; i++) {
		if (session_list[i] == wparam) {
			while (session_list[i + 1] != 0) {
				session_list[i] = session_list[i + 1];
				i++;
			}
			session_list[i] = 0;
		}
	}
	return 0;
}

int CheckForDuplicate(MCONTACT contact_list[], MCONTACT lparam)
{
	MCONTACT s_list[255] = { 0 };
	memcpy(s_list, contact_list, sizeof(s_list));
	for (int i = 0;; i++) {
		if (s_list[i] == lparam)
			return i;
		if (s_list[i] == 0)
			return -1;
	}
}

int LoadSessionToCombobox(HWND hdlg, BOOL mode, int iLimit, char* pszSetting, int iFirstNum)
{
	int ses_count = iFirstNum, index;
	char szBuffer[256] = { 0 };
	if (session_list_recovered[0] && !iFirstNum && !mode) {
		index = SendDlgItemMessage(hdlg, IDC_LIST, CB_ADDSTRING, 0, (LPARAM)TranslateT("Session Before Last Crash"));
		SendDlgItemMessage(hdlg, IDC_LIST, CB_SETITEMDATA, (WPARAM)index, 256);
	}

	for (int i = 0; i < iLimit; i++) {
		mir_snprintf(szBuffer, "%s_%u", pszSetting, i);
		wchar_t *pszBuffer = db_get_wsa(NULL, MODNAME, szBuffer);
		if (pszBuffer) {
			if (!IsMarkedUserDefSession(i + iFirstNum) || mode == 1) {
				index = SendDlgItemMessage(hdlg, IDC_LIST, CB_ADDSTRING, 0, (LPARAM)pszBuffer);
				SendDlgItemMessage(hdlg, IDC_LIST, CB_SETITEMDATA, (WPARAM)index, i + iFirstNum);
			}
			else {
				SendDlgItemMessage(hdlg, IDC_LIST, CB_INSERTSTRING, 0, (LPARAM)pszBuffer);
				SendDlgItemMessage(hdlg, IDC_LIST, CB_SETITEMDATA, 0, i + iFirstNum);
			}
			mir_free(pszBuffer);
		}
		else {
			if (i == 0) ses_count = iLimit - iFirstNum;
			break;
		}
	}

	return ses_count;
}

int FillFavoritesMenu(HMENU hMenu, int iLimit)
{
	int iItems = 0;

	for (int i = 0; i < iLimit; i++) {
		if (IsMarkedUserDefSession(i)) {
			char szBuffer[256];
			mir_snprintf(szBuffer, "%s_%u", "UserSessionDsc", i);
			wchar_t *pszBuffer = db_get_wsa(NULL, MODNAME, szBuffer);
			if (pszBuffer) {
				AppendMenu(hMenu, MF_STRING, i + 1, pszBuffer);
				iItems++;
				mir_free(pszBuffer);
			}
		}
	}
	return iItems;
}

void OffsetWindow(HWND parent, HWND hwnd, int dx, int dy)
{
	POINT pt;
	RECT rc;
	GetWindowRect(hwnd, &rc);
	pt.x = rc.left;
	pt.y = rc.top;
	ScreenToClient(parent, &pt);
	rc.left = pt.x;
	rc.top = pt.y;

	OffsetRect(&rc, dx, dy);
	SetWindowPos(hwnd, NULL, rc.left, rc.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}

int CheckContactVisibility(MCONTACT hContact)
{
	return !db_get_b(hContact, "CList", "Hidden", 0);
}

void RenameUserDefSession(int ses_count, wchar_t* ptszNewName)
{
	char szSession[256];
	mir_snprintf(szSession, "%s_%u", "UserSessionDsc", ses_count);
	db_set_ws(NULL, MODNAME, szSession, ptszNewName);
}

int MarkUserDefSession(int ses_count, BYTE bCheck)
{
	char szSessionName[256];
	mir_snprintf(szSessionName, "%s_%u", "FavUserSession", ses_count);
	db_set_b(NULL, MODNAME, szSessionName, bCheck);
	return 0;
}

BYTE IsMarkedUserDefSession(int ses_count)
{
	char szSessionName[256];
	mir_snprintf(szSessionName, "%s_%u", "FavUserSession", ses_count);
	return db_get_b(NULL, MODNAME, szSessionName, 0);
}

void SavePosition(HWND hwnd, char *wndName)
{
	RECT rc;
	GetWindowRect(hwnd, &rc);
	char buffer[512];
	mir_snprintf(buffer, "%sPosX", wndName);
	db_set_dw(0, MODNAME, buffer, rc.left);
	mir_snprintf(buffer, "%sPosY", wndName);
	db_set_dw(0, MODNAME, buffer, rc.top);
}

void LoadPosition(HWND hWnd, char *wndName)
{
	char buffer[512];
	mir_snprintf(buffer, "%sPosX", wndName);
	int x = db_get_dw(0, MODNAME, buffer, ((GetSystemMetrics(SM_CXSCREEN)) / 2) - 130);
	mir_snprintf(buffer, "%sPosY", wndName);
	int y = db_get_dw(0, MODNAME, buffer, ((GetSystemMetrics(SM_CYSCREEN)) / 2) - 80);
	SetWindowPos(hWnd, NULL, x, y, 0, 0, SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOSIZE);
}