summaryrefslogtreecommitdiff
path: root/meta2/edit_meta.cpp
blob: 839a15a7d6edac78b3dd8a86b96d21a461d81a84 (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
#include "common.h"
#include "edit_meta.h"
#include "resource.h"
#include "core_functions.h"
#include "icons.h"
#include "api.h"

TCHAR *GetIdString(HANDLE hContact, char *proto) {
	if(proto) {
		char *field = (char *)CallProtoService(proto,PS_GETCAPS,PFLAG_UNIQUEIDSETTING,0);
		DBVARIANT dbv;

		if(!DBGetContactSetting(hContact,proto,field,&dbv)) {
			TCHAR *buff = 0;
			switch(dbv.type)
			{
				case DBVT_ASCIIZ:
					return mir_a2t(dbv.pszVal);
				case DBVT_UTF8:
#ifdef _UNICODE
					return mir_utf8decodeW(dbv.pszVal);
#else
					return mir_utf8decode(dbv.pszVal, 0);
#endif
				case DBVT_WCHAR:
					return mir_u2t(dbv.pwszVal);
				case DBVT_BYTE:
					buff = (TCHAR *)mir_alloc(4 * sizeof(TCHAR));
					mir_sntprintf(buff, 4, _T("%d"),dbv.bVal);
					return buff;
				case DBVT_WORD:
					buff = (TCHAR *)mir_alloc(16 * sizeof(TCHAR));
					mir_sntprintf(buff, 16, _T("%d"),dbv.wVal);
					return buff;
				case DBVT_DWORD:
					buff = (TCHAR *)mir_alloc(32 * sizeof(TCHAR));
					mir_sntprintf(buff, 32, _T("%d"),dbv.dVal);
					return buff;
				default:
					return 0;
			}
			DBFreeVariant(&dbv);
		}
	}
	return 0;
}

HANDLE hMeta;
int def;
SubcontactList subs;
bool changed;

void FillList(HWND hwndLst) {
	LVITEM LvItem = {0};
	LvItem.mask=LVIF_TEXT;   // Text Style
	LvItem.cchTextMax = 256; // Max size of test

	int row = 0;
	char *proto;
	TCHAR *proto_t;

	SendMessage(hwndLst, LVM_DELETEALLITEMS, 0, 0);

	for(SubcontactList::Iterator i = subs.start(); i.has_val(); i.next()) {
		LvItem.iItem = row;

		LvItem.iSubItem = 0; // clist display name
		LvItem.pszText = ContactName(i.val().handle());
		SendMessage(hwndLst, LVM_INSERTITEM, (WPARAM)0, (LPARAM)&LvItem);
		
		proto = ContactProto(i.val().handle());

		LvItem.iSubItem = 1; // id
		LvItem.pszText = GetIdString(i.val().handle(), proto);
		SendMessage(hwndLst, LVM_SETITEM, (WPARAM)0, (LPARAM)&LvItem);
		mir_free(LvItem.pszText);

#ifdef _UNICODE
		proto_t = mir_a2u(proto);
#else
		proto_t = proto;
#endif
		LvItem.iSubItem = 2; // protocol
		LvItem.pszText = proto_t;
		SendMessage(hwndLst, LVM_SETITEM, (WPARAM)0, (LPARAM)&LvItem);
#ifdef _UNICODE
		mir_free(proto_t);
#endif;

		LvItem.iSubItem = 3; // default
		LvItem.pszText = (row == def ? TranslateT("TRUE") : TranslateT("FALSE"));
		SendMessage(hwndLst, LVM_SETITEM, (WPARAM)0, (LPARAM)&LvItem);

		row++;
	}
}

void SetListSelection(HWND hList, int sel) {
	LVITEM LvItem;

	ZeroMemory(&LvItem, sizeof(LvItem));
	LvItem.iItem = sel;
	LvItem.mask = LVIF_STATE;
	LvItem.stateMask = LVIS_SELECTED|LVIS_FOCUSED;
	LvItem.state = LVIS_SELECTED|LVIS_FOCUSED;

	SendMessage(hList, LVM_SETITEMSTATE, (WPARAM)sel, (LPARAM)&LvItem);

}

void ApplyChanges() {
	if(def >= 0 && def < metaMap[hMeta].size())
		MetaAPI_SetDefaultContactNum((WPARAM)hMeta, (LPARAM)def);
	else
		MetaAPI_SetDefaultContactNum((WPARAM)hMeta, (LPARAM)0);

	unsigned long index;
	for(SubcontactList::Iterator i = metaMap[hMeta].start(); i.has_val(); i.next()) {
		if(!subs.get_index(i.val(), index)) {
			Meta_Remove(i.val().handle());
		}
	}
}

#define WMU_SET_BUTTONS			(WM_USER + 0x100)
INT_PTR CALLBACK Meta_EditDialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch(msg) {
		case WM_INITDIALOG:
			TranslateDialogDefault( hwndDlg );
			SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIconEx(I_EDIT));

			SetDlgItemText(hwndDlg, IDC_ED_NAME, ContactName(hMeta));

			{
				HWND hwndLst = GetDlgItem(hwndDlg, IDC_LST_CONTACTS);
				SendMessage(hwndLst,LVM_SETEXTENDEDLISTVIEWSTYLE, 0,LVS_EX_FULLROWSELECT); // Set style

				LVCOLUMN LvCol = {0};
				LvCol.mask=LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;

				LvCol.pszText=TranslateT("Contact");
				LvCol.cx=100;
				SendMessage(hwndLst,LVM_INSERTCOLUMN,0,(LPARAM)&LvCol);

				LvCol.pszText=TranslateT("Id");
				LvCol.cx=130;
				SendMessage(hwndLst,LVM_INSERTCOLUMN,1,(LPARAM)&LvCol);
				LvCol.pszText=TranslateT("Protocol");
				LvCol.cx=100;
				SendMessage(hwndLst,LVM_INSERTCOLUMN,2,(LPARAM)&LvCol);
				LvCol.pszText=TranslateT("Default");
				LvCol.cx=60;
				SendMessage(hwndLst,LVM_INSERTCOLUMN,3,(LPARAM)&LvCol);

				FillList(hwndLst);
				SendMessage(hwndDlg, WMU_SET_BUTTONS, 0, 0);
			}

			break;
		case WMU_SET_BUTTONS:
			{
				HWND hwnd = GetDlgItem(hwndDlg, IDOK);
				EnableWindow(hwnd, changed);
				hwnd = GetDlgItem(hwndDlg, IDC_VALIDATE);
				EnableWindow(hwnd, changed);

				int index = SendDlgItemMessage(hwndDlg, IDC_LST_CONTACTS, LVM_GETNEXTITEM,-1,LVNI_FOCUSED|LVNI_SELECTED); // return item selected
				hwnd = GetDlgItem(hwndDlg, IDC_BTN_SETDEFAULT);
				EnableWindow(hwnd, index != -1 && index != def);
				hwnd = GetDlgItem(hwndDlg, IDC_BTN_REM);
				EnableWindow(hwnd, index != -1);
 
			}
			return TRUE;
		case WM_COMMAND:
			if(HIWORD(wParam) == BN_CLICKED) {
				switch(LOWORD(wParam))
				{
					case IDC_BTN_SETDEFAULT:
						{
							int index = SendDlgItemMessage(hwndDlg, IDC_LST_CONTACTS, LVM_GETNEXTITEM,-1,LVNI_FOCUSED|LVNI_SELECTED); // return item selected
							def = index;
							changed = true;
							FillList(GetDlgItem(hwndDlg, IDC_LST_CONTACTS));
							SetListSelection(GetDlgItem(hwndDlg, IDC_LST_CONTACTS), index);
							SendMessage(hwndDlg, WMU_SET_BUTTONS, 0, 0);
						}
						break;
					case IDC_BTN_REM:
						{
							int index = SendDlgItemMessage(hwndDlg, IDC_LST_CONTACTS, LVM_GETNEXTITEM,-1,LVNI_FOCUSED|LVNI_SELECTED); // return item selected
							if(index >= 0 && index < subs.size()) {
								subs.remove(subs[index].handle());
								if(def >= index && def > 0) def--;
								changed = true;
								FillList(GetDlgItem(hwndDlg, IDC_LST_CONTACTS));
								SendMessage(hwndDlg, WMU_SET_BUTTONS, 0, 0);
							}
						}
						break;
					case IDOK:
						EndDialog(hwndDlg, IDOK);
						break;
					case IDCANCEL:
						EndDialog(hwndDlg, IDCANCEL);
						break;
					case IDC_VALIDATE:
						ApplyChanges();
						break;
				}
				return TRUE;
			}
			break;
		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) {
						SendMessage(hwndDlg, WMU_SET_BUTTONS, 0, 0);
					}
					break;
			}
			break;

	}
	return FALSE;
}

void EditMeta(HANDLE hM) {
	hMeta = hM;
	subs.add_all(metaMap[hMeta]);
	def = (int)DBGetContactSettingByte(hMeta, MODULE, "Default", -1);
	changed = false;

	HWND clui = (HWND)CallService(MS_CLUI_GETHWND,0,0);
	if(DialogBox(hInst, MAKEINTRESOURCE(IDD_METAEDIT), clui, Meta_EditDialogProc) == IDOK) {

		// apply changes

		ApplyChanges();
	}
	subs.clear();
}