summaryrefslogtreecommitdiff
path: root/plugins/MetaContacts/src/edit.cpp
blob: 859ad8aafd2d5eaf4e9ed3495f54d4fc48ee243c (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
MetaContacts Plugin for Miranda IM.

Copyright © 2004 Universite Louis PASTEUR, STRASBOURG.

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.
*/

/** @file edit.c 
*
* Functions for the <b>'Edit'</b> Dialog.
* Contains all the functions and all the structures
* needed to display and control the <b>'Edit'</b> Dialog.
*/

#include "metacontacts.h"

//! Holds the differents changes that have to made
typedef struct tag_CHANGES {
	HCONTACT hMeta;							//!< \c HANDLE of the MetaContact that is edited.
	HCONTACT hDefaultContact;					//!< \c HANDLE of the new default contact
	HCONTACT hOfflineContact;
	int num_deleted,						//!< \c DWORD number of deleted contacts
		num_contacts;						//!< \c DWORD number of contacts
	HCONTACT hDeletedContacts[MAX_CONTACTS];	//!< \c HANDLEs of the subcontacts to be removed from this metacontact
	HCONTACT hContact[MAX_CONTACTS];			//!< \c HANDLEs of the subcontacts, in the order they should be in
	int force_default;
} CHANGES;

CHANGES changes;							//!< \c global CHANGES structure

/** Fills the list of contacts
*
* @param chg : Structure holding all the change info (See CHANGES).
*/

void FillContactList(HWND hWndDlg, CHANGES *chg)
{
	HWND hList = GetDlgItem(hWndDlg, IDC_LST_CONTACTS);
	TCHAR buff[256];

	SendMessage(hList, LVM_DELETEALLITEMS, 0, 0);

	LVITEM LvItem = { 0 };
	LvItem.mask = LVIF_TEXT;   // Text Style
	LvItem.cchTextMax = 256; // Max size of test

	for (int i = 0; i < chg->num_contacts; i++)  {
		LvItem.iItem = i;
	
		TCHAR *ptszCDN = pcli->pfnGetContactDisplayName(chg->hContact[i], GCDNF_TCHAR);
		if (ptszCDN == NULL)
			ptszCDN = TranslateT("(Unknown Contact)");

		LvItem.iSubItem = 0; // clist display name
		LvItem.pszText = ptszCDN;
		ListView_InsertItem(hList, &LvItem);

		LvItem.iSubItem = 1; // id
		char *szProto = GetContactProto(chg->hContact[i]);
		if (szProto) {
			char *szField = (char *)CallProtoService(szProto, PS_GETCAPS, PFLAG_UNIQUEIDSETTING, 0);

			DBVARIANT dbv;
			if ( !db_get(chg->hContact[i], szProto, szField, &dbv)) {
				switch(dbv.type) {
				case DBVT_ASCIIZ:
					_tcsncpy(buff, _A2T(dbv.pszVal), SIZEOF(buff));
					break;
				case DBVT_WCHAR:
					_tcsncpy(buff, dbv.ptszVal, SIZEOF(buff));
					break;
				case DBVT_BYTE:
					_itot(dbv.bVal, buff, 10);
					break;
				case DBVT_WORD:
					_itot(dbv.wVal, buff, 10);
					break;
				case DBVT_DWORD:
					_itot(dbv.dVal, buff, 10);
					break;
				default:
					buff[0] = 0;
				}
				db_free(&dbv);
			}
			else buff[0] = 0;

			LvItem.pszText = buff;
			SendMessage(hList,LVM_SETITEM,0,(LPARAM)&LvItem); // Enter text to SubItems

			LvItem.iSubItem = 2; // protocol
			_tcsncpy(buff, _A2T(szProto), SIZEOF(buff));
			ListView_SetItem(hList, &LvItem);
		}
		else {
			LvItem.pszText = _T("Unknown");
			ListView_SetItem(hList, &LvItem);

			LvItem.iSubItem = 2; // protocol
			ListView_SetItem(hList, &LvItem);
		}
		LvItem.iSubItem = 3; // Default (Yes/No)
		LvItem.pszText = (chg->hContact[i] == chg->hDefaultContact ? TranslateT("Yes") : TranslateT("No"));
		ListView_SetItem(hList, &LvItem);

		LvItem.iSubItem = 4; // Offline (Yes/No)
		LvItem.pszText = (chg->hContact[i] == chg->hOfflineContact ? TranslateT("Yes") : TranslateT("No"));
		ListView_SetItem(hList, &LvItem);
	}
}

void SetListSelection(HWND hList, int sel)
{
	ListView_SetItemState(hList, sel, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
}

/** Scans the \c CHANGES and call the appropriate function for each change.
*
* @param chg : Structure holding all the change info (See CHANGES).
*/

void ApplyChanges(CHANGES *chg)
{
	int i;

	// remove removed contacts
	for (i = 0; i < chg->num_deleted; i++) {
		Meta_Delete((WPARAM)chg->hDeletedContacts[i], 0);
		if (chg->hDeletedContacts[i] == chg->hDefaultContact)
			chg->hDefaultContact = 0;
		if (chg->hDeletedContacts[i] == chg->hOfflineContact)
			chg->hOfflineContact = 0;
	}

	// set contact positions
	for (i = 0; i < chg->num_contacts; i++) {
		if (Meta_GetContactNumber(chg->hContact[i]) != i)
			Meta_SwapContacts(chg->hMeta, Meta_GetContactNumber(chg->hContact[i]), i);
	}

	NotifyEventHooks(hSubcontactsChanged, (WPARAM)chg->hMeta, (LPARAM)chg->hDefaultContact);

	// set default
	if (chg->hDefaultContact)
		db_set_dw(chg->hMeta, META_PROTO, "Default", Meta_GetContactNumber(chg->hDefaultContact));
	else
		db_set_dw(chg->hMeta, META_PROTO, "Default", 0);
	NotifyEventHooks(hEventDefaultChanged, (WPARAM)chg->hMeta, (LPARAM)chg->hDefaultContact);

	// set offline
	if (chg->hOfflineContact)
		db_set_dw(chg->hMeta, META_PROTO, "OfflineSend", Meta_GetContactNumber(chg->hOfflineContact));
	else
		db_set_dw(chg->hMeta, META_PROTO, "OfflineSend", (DWORD)-1);

	// fix nick
	HCONTACT most_online = Meta_GetMostOnline(chg->hMeta);
	Meta_CopyContactNick(chg->hMeta, most_online);

	// fix status
	Meta_FixStatus(chg->hMeta);

	// fix avatar
	most_online = Meta_GetMostOnlineSupporting(chg->hMeta, PFLAGNUM_4, PF4_AVATARS);
	if (most_online) {
		PROTO_AVATAR_INFORMATIONT AI;

		AI.cbSize = sizeof(AI);
		AI.hContact = chg->hMeta;
		AI.format = PA_FORMAT_UNKNOWN;
		_tcscpy(AI.filename, _T("X"));

		if ((int)CallProtoService(META_PROTO, PS_GETAVATARINFOT, 0, (LPARAM)&AI) == GAIR_SUCCESS)
	        db_set_ts(chg->hMeta, "ContactPhoto", "File",AI.filename);
	}

	if (MetaAPI_GetForceState((WPARAM)chg->hMeta, 0) != chg->force_default)
		MetaAPI_ForceDefault((WPARAM)chg->hMeta, 0);
}

LRESULT ProcessCustomDraw (LPARAM lParam)
{
    LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
	 switch(lplvcd->nmcd.dwDrawStage) {
	 case CDDS_PREPAINT : //Before the paint cycle begins
		 //request notifications for individual listview items
		 return CDRF_NOTIFYITEMDRAW;

	 case CDDS_ITEMPREPAINT: //Before an item is drawn
		 if (changes.hContact[(int)lplvcd->nmcd.dwItemSpec] == changes.hDefaultContact) {
			 lplvcd->clrText = RGB(255, 0, 0);
		 }
		 return CDRF_NEWFONT;
	 }

	 return 0;
}

/** Callback function for the <b>'Edit'</b> Dialog.
*
* All the UI is controlled here, from display to functionnalities.
*
* @param hwndDlg :	\c HANDLE to the <b>'Edit'</b> \c Dialog.
* @param uMsg :		Specifies the message received by this dialog.
* @param wParam :	Specifies additional message-specific information. 
* @param lParam :	Specifies additional message-specific information (handle of MetaContact to edit)
*
* @return			\c TRUE if the dialog processed the message, \c FALSE if it did not.
*/

#define WMU_SETTITLE		(WM_USER + 1)

INT_PTR CALLBACK Meta_EditDialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	HWND hwnd;
	int sel, i;

	switch(msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault( hwndDlg );
		SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIconEx(I_EDIT));
		{	
			// Disable the 'Apply' button.
			EnableWindow(GetDlgItem(hwndDlg,IDC_VALIDATE),FALSE);

			hwnd = GetDlgItem(hwndDlg, IDC_LST_CONTACTS);
			ListView_SetExtendedListViewStyle(hwnd, LVS_EX_FULLROWSELECT);

			// Create list columns
			LVCOLUMN LvCol = { 0 };
			LvCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;

			LvCol.pszText = TranslateT("Contact");
			LvCol.cx = 100;
			ListView_InsertColumn(hwnd, 0, &LvCol);

			LvCol.pszText = TranslateT("ID");
			LvCol.cx = 130;
			ListView_InsertColumn(hwnd, 1, &LvCol);

			LvCol.pszText = TranslateT("Protocol");
			LvCol.cx = 100;
			ListView_InsertColumn(hwnd, 2, &LvCol);

			LvCol.pszText = TranslateT("Default");
			LvCol.cx = 60;
			ListView_InsertColumn(hwnd, 3, &LvCol);

			LvCol.pszText = TranslateT("Send Offline");
			LvCol.cx = 85;
			ListView_InsertColumn(hwnd, 4, &LvCol);

			// disable buttons until a selection is made in the list
			EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_REM), FALSE);
			EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_SETDEFAULT), FALSE);
			EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_SETOFFLINE), FALSE);
			EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_UP), FALSE);
			EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DOWN), FALSE);

			int nb_contacts = db_get_dw((HCONTACT)lParam, META_PROTO, "NumContacts", 0);
			int default_contact_number = db_get_dw((HCONTACT)lParam, META_PROTO, "Default", (DWORD)-1);
			int offline_contact_number = db_get_dw((HCONTACT)lParam, META_PROTO, "OfflineSend", (DWORD)-1);

			changes.hMeta = (HCONTACT)lParam;
			changes.num_contacts = nb_contacts;
			changes.num_deleted = 0;
			changes.hDefaultContact = Meta_GetContactHandle((HCONTACT)lParam, default_contact_number);
			changes.hOfflineContact = Meta_GetContactHandle((HCONTACT)lParam, offline_contact_number);
			for (i = 0; i < nb_contacts; i++)
				changes.hContact[i] = Meta_GetContactHandle((HCONTACT)lParam, i);
			changes.force_default = MetaAPI_GetForceState((WPARAM)lParam, 0);

			SendMessage(hwndDlg, WMU_SETTITLE, 0, lParam);

			CheckDlgButton(hwndDlg, IDC_CHK_FORCEDEFAULT, changes.force_default);

			FillContactList(hwndDlg, &changes);
		}
		return TRUE;

	case WMU_SETTITLE:
		{
			TCHAR *ptszCDN = pcli->pfnGetContactDisplayName((HCONTACT)lParam, GCDNF_TCHAR);
			if (ptszCDN == NULL)
				ptszCDN = TranslateT("(Unknown Contact)");

			SetWindowText(GetDlgItem(hwndDlg, IDC_ED_NAME), ptszCDN);
		}
		return TRUE;

	case WM_NOTIFY: // the message that is being sent always
		switch(LOWORD(wParam)) { // hit control
		case IDC_LST_CONTACTS:      // did we hit our ListView contorl?
			if (((LPNMHDR)lParam)->code == NM_CLICK) {
				sel = ListView_GetNextItem(GetDlgItem(hwndDlg, IDC_LST_CONTACTS), -1, LVNI_FOCUSED|LVNI_SELECTED); // return item selected

				// enable buttons
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_REM), sel != -1);
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_SETDEFAULT), sel != -1 && changes.hContact[sel] != changes.hDefaultContact);
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_SETOFFLINE), sel != -1 && changes.hContact[sel] != changes.hOfflineContact);
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_UP), sel > 0);
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DOWN), (sel != -1 && sel < changes.num_contacts - 1));
			}					
		}
		break;

	case WM_COMMAND:
		switch(HIWORD(wParam)) {
		case BN_CLICKED:	// A button ('Remove', 'OK', 'Cancel' or 'Apply', normally) has been clicked
			switch(LOWORD(wParam)) {
			case IDC_VALIDATE: // Apply changes, if there is still one contact attached to the metacontact.
				if (changes.num_contacts == 0) { // Otherwise, delete the metacontact.
					if (IDYES == MessageBox(hwndDlg, TranslateT(szDelMsg), TranslateT("Delete MetaContact?"), MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON1)) {
						Meta_Delete((WPARAM)changes.hMeta, 0);
						DestroyWindow(hwndDlg);
					}
					return TRUE;
				}
				ApplyChanges(&changes);

				// Disable the 'Apply' button.
				EnableWindow(GetDlgItem(hwndDlg, IDC_VALIDATE), FALSE);
				break;

			case IDOK:
				if ( IsWindowEnabled(GetDlgItem(hwndDlg, IDC_VALIDATE))) { // If there are changes that could be made,
					if (changes.num_contacts == 0) { // do the work that would have be done if the 'Apply' button was clicked.
						if (IDYES == MessageBox(hwndDlg, TranslateT(szDelMsg), TranslateT("Delete MetaContact?"), MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON1)) {
							Meta_Delete((WPARAM)changes.hMeta,0);
							DestroyWindow(hwndDlg);
						}
						return TRUE;
					}
					ApplyChanges(&changes);
				}
				EndDialog(hwndDlg, IDOK);
				return TRUE;

			case IDCANCEL:	// Simply close the dialog
				EndDialog(hwndDlg, IDCANCEL);
				return TRUE;

			case IDC_BTN_SETDEFAULT:
				hwnd = GetDlgItem(hwndDlg, IDC_LST_CONTACTS);
				sel = ListView_GetNextItem(hwnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
				InvalidateRect(hwnd, 0, TRUE);
				changes.hDefaultContact = changes.hContact[sel];
				SendMessage(hwndDlg, WMU_SETTITLE, 0, (LPARAM)changes.hContact[sel]);

				FillContactList(hwndDlg, &changes);
				SetListSelection(hwnd, sel);
				EnableWindow(GetDlgItem(hwndDlg,IDC_BTN_SETDEFAULT),FALSE);
				EnableWindow(GetDlgItem(hwndDlg,IDC_VALIDATE),TRUE);
				return TRUE;

			case IDC_BTN_SETOFFLINE:
				hwnd = GetDlgItem(hwndDlg, IDC_LST_CONTACTS);
				sel = ListView_GetNextItem(hwnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
				InvalidateRect(hwnd, 0, TRUE);
				changes.hOfflineContact = changes.hContact[sel];

				FillContactList(hwndDlg, &changes);
				SetListSelection(hwnd, sel);
				EnableWindow(GetDlgItem(hwndDlg,IDC_BTN_SETOFFLINE),FALSE);
				EnableWindow(GetDlgItem(hwndDlg,IDC_VALIDATE),TRUE);
				return TRUE;

			case IDC_BTN_REM:
				hwnd = GetDlgItem(hwndDlg, IDC_LST_CONTACTS);
				sel = ListView_GetNextItem(hwnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
				changes.num_contacts--;
				changes.hDeletedContacts[changes.num_deleted++] = changes.hContact[sel];
				if (changes.hDefaultContact == changes.hContact[sel]) {
					if (changes.num_contacts > 0) {
						changes.hDefaultContact = changes.hContact[0];
						SetWindowText(GetDlgItem(hwndDlg,IDC_ED_DEFAULT), pcli->pfnGetContactDisplayName(changes.hDefaultContact, GCDNF_TCHAR));
					}
					else {
						changes.hDefaultContact = 0;
						SetWindowText(GetDlgItem(hwndDlg,IDC_ED_DEFAULT), _T("None"));
					}
				}

				for (i = sel; i < changes.num_contacts; i++)
					changes.hContact[i] = changes.hContact[i + 1];
				FillContactList(hwndDlg, &changes);

				// disable buttons
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_REM), FALSE);
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_SETDEFAULT), FALSE);
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_SETOFFLINE), FALSE);
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_UP), FALSE);
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DOWN), FALSE);

				// Enable the 'Apply' button.
				EnableWindow(GetDlgItem(hwndDlg,IDC_VALIDATE),TRUE);
				return TRUE;

			case IDC_BTN_UP:
				hwnd = GetDlgItem(hwndDlg, IDC_LST_CONTACTS);
				sel = ListView_GetNextItem(hwnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
				{
					HCONTACT temp = changes.hContact[sel];
					changes.hContact[sel] = changes.hContact[sel - 1];
					changes.hContact[sel-1] = temp;
				}
				FillContactList(hwndDlg, &changes);
				sel--;
				SetListSelection(hwnd, sel);

				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_UP), (sel > 0));
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DOWN), (sel < changes.num_contacts - 1));
				EnableWindow(GetDlgItem(hwndDlg,IDC_VALIDATE),TRUE);
				return TRUE;

			case IDC_BTN_DOWN:
				hwnd = GetDlgItem(hwndDlg, IDC_LST_CONTACTS);
				sel = ListView_GetNextItem(hwnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
				{
					HCONTACT temp = changes.hContact[sel];
					changes.hContact[sel] = changes.hContact[sel + 1];
					changes.hContact[sel + 1] = temp;
				}
				FillContactList(hwndDlg, &changes);
				sel++;
				SetListSelection(hwnd, sel);

				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_UP), (sel > 0));
				EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DOWN), (sel < changes.num_contacts - 1));
				EnableWindow(GetDlgItem(hwndDlg,IDC_VALIDATE),TRUE);
				return TRUE;

			case IDC_CHK_FORCEDEFAULT:
				changes.force_default = IsDlgButtonChecked(hwndDlg, IDC_CHK_FORCEDEFAULT);
				EnableWindow(GetDlgItem(hwndDlg,IDC_VALIDATE),TRUE);
				return TRUE;
			}
		}
		break;

	case WM_CLOSE:
		DestroyWindow(hwndDlg);
		return TRUE;

	case WM_DESTROY:
		Skin_ReleaseIcon((HICON)SendMessage(hwndDlg, WM_SETICON, ICON_BIG, 0));
		EndDialog(hwndDlg, IDCANCEL);
		break;
	}

	return FALSE;
}