summaryrefslogtreecommitdiff
path: root/plugins/ContactsPlus/src/utils.cpp
blob: 546468bd20f83aca4bb17e43f97954a9ecb1792e (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
// ---------------------------------------------------------------------------
//                Contacts+ for Miranda Instant Messenger
//                _______________________________________
//
// Copyright © 2002 Dominus Procellarum
// Copyright © 2004-2008 Joe Kucera
//
// 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 2
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// ---------------------------------------------------------------------------

#include "contacts.h"

/* a strlennull() that likes NULL */
size_t __fastcall strlennull(const char *string)
{
	if (string)
		return strlen(string);

	return 0;
}

int __fastcall strcmpnull(const char *str1, const char *str2)
{
	if (!str1 || !str2) return 1;

	return strcmp(str1, str2);
}

void __fastcall strcpynull(char *str1, const char *str2)
{
	if (!str2)
		str2 = "";

	strcpy(str1, str2);
}

char* __fastcall null_strdup(const char *string)
{
	if (string)
		return strdup(string);

	return NULL;
}

TCHAR* GetContactUID(MCONTACT hContact)
{
	char *szProto = GetContactProto(hContact);
	char *uid = (char*)CallProtoService(szProto, PS_GETCAPS, PFLAG_UNIQUEIDSETTING, 0);
	if (INT_PTR(uid) == CALLSERVICE_NOTFOUND || uid == 0)
		return NULL;

	DBVARIANT vrUid;
	if (db_get_s(hContact, szProto, uid, &vrUid, 0))
		return NULL;

	if (vrUid.type == DBVT_DWORD) {
		TCHAR tmp[100];
		_itot(vrUid.dVal, tmp, 10);
		return mir_tstrdup(tmp);
	}

	if (vrUid.type == DBVT_ASCIIZ) {
		TCHAR *res = mir_a2t(vrUid.pszVal);
		mir_free(vrUid.pszVal);
		return res;
	}

	if (vrUid.type == DBVT_UTF8) {
		TCHAR *res = mir_utf8decodeT(vrUid.pszVal);
		mir_free(vrUid.pszVal);
		return res;
	}

	return NULL;
}

void DialogAddContactExecute(HWND hwndDlg, MCONTACT hNewContact)
{
	ADDCONTACTSTRUCT acs = { 0 };
	acs.hContact = hNewContact;
	acs.handleType = HANDLE_CONTACT;
	CallService(MS_ADDCONTACT_SHOW, (WPARAM)hwndDlg, (LPARAM)&acs);
}


void DrawProtocolIcon(HWND hwndDlg, LPARAM lParam, MCONTACT hContact)
{
	LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lParam;

	if (dis->hwndItem == GetDlgItem(hwndDlg, IDC_PROTOCOL))
	{
		HICON hIcon = LoadContactProtoIcon(hContact);
		if (hIcon)
		{
			DrawIconEx(dis->hDC, dis->rcItem.left, dis->rcItem.top, hIcon, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0, NULL, DI_NORMAL);
			DestroyIcon(hIcon);
		}
	}
}


void UpdateDialogTitle(HWND hwndDlg, MCONTACT hContact, TCHAR *pszTitleStart)
{
	TCHAR newtitle[512];
	lstrcpyn(newtitle, TranslateTS(pszTitleStart), SIZEOF(newtitle));
	
	if (hContact) {
		char *szProto = GetContactProto(hContact);
		if (szProto) {
			ptrT uid(GetContactUID(hContact));
			TCHAR *contactName = pcli->pfnGetContactDisplayName(hContact, 0);

			TCHAR oldTitle[MAX_PATH];
			GetDlgItemText(hwndDlg, IDC_NAME, oldTitle, SIZEOF(oldTitle));

			if (lstrcmp(uid ? uid : contactName, oldTitle))
				SetDlgItemText(hwndDlg, IDC_NAME, uid ? uid : contactName);

			TCHAR *szStatus = pcli->pfnGetStatusModeDescription(szProto == NULL ? ID_STATUS_OFFLINE : db_get_w(hContact, szProto, "Status", ID_STATUS_OFFLINE), 0);
			mir_sntprintf(newtitle, 256, _T("%s %s (%s)"), TranslateTS(pszTitleStart), contactName, szStatus);
		}
	}

	SetWindowText(hwndDlg, newtitle);
}


void UpdateDialogAddButton(HWND hwndDlg, MCONTACT hContact)
{
	int bVisible = db_get_b(hContact, "CList", "NotOnList", 0);
	ShowWindow(GetDlgItem(hwndDlg, IDC_ADD), bVisible ? SW_SHOW : SW_HIDE);
}


HICON InitMButton(HWND hDlg, int idButton, LPCSTR szIcon, char *szTip)
{
	HWND hButton = GetDlgItem(hDlg, idButton);
	HICON hIcon = (HICON)LoadImageA(GetModuleHandle(NULL), szIcon, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0);

	SendMessage(hButton, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
	SendMessage(hButton, BUTTONSETASFLATBTN, TRUE, 0);
	SendMessage(hButton, BUTTONADDTOOLTIP, (WPARAM)szTip, 0);
	return hIcon;
}


HICON LoadContactProtoIcon(MCONTACT hContact)
{
	char *szProto = GetContactProto(hContact);
	if (szProto)
		return (HICON)CallProtoService(szProto, PS_LOADICON, PLI_PROTOCOL | PLIF_SMALL, 0);
	return NULL;
}


void EnableDlgItem(HWND hwndDlg, UINT control, int state)
{
	EnableWindow(GetDlgItem(hwndDlg, control), state);
}


TCHAR* GetWindowTextT(HWND hWnd)
{
	int len = GetWindowTextLength(hWnd) + 1;
	TCHAR* txt = (TCHAR*)mir_alloc(len * sizeof(TCHAR));
	if (txt) {
		txt[0] = 0;
		GetWindowText(hWnd, txt, len);
	}
	return txt;
}

TCHAR* __fastcall strdupT(const TCHAR *string)
{
	if (string)
		return (TCHAR*)wcsdup((TCHAR*)string);
	return NULL;
}