shell32/tests: Some tests for IShellWindows.
[wine.git] / dlls / inetcpl.cpl / security.c
blob6d7905785ba2a8b0c391c0df45f3d01a2e6e57b5
1 /*
2 * Internet control panel applet: security propsheet
4 * Copyright 2011 Detlef Riekenberg
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
23 #define CONST_VTABLE
25 #include <stdarg.h>
26 #include <windef.h>
27 #include <winbase.h>
28 #include <winuser.h>
29 #include <prsht.h>
30 #include "commctrl.h"
32 #include "ole2.h"
33 #include "urlmon.h"
34 #include "initguid.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "inetcpl.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(inetcpl);
43 typedef struct secdlg_data_s {
44 HWND hsec; /* security propsheet */
45 HWND hlv; /* listview */
46 HWND htb; /* trackbar */
47 IInternetSecurityManager *sec_mgr;
48 IInternetZoneManager *zone_mgr;
49 DWORD zone_enumerator;
50 DWORD num_zones;
51 ZONEATTRIBUTES *zone_attr;
52 DWORD *zones;
53 DWORD *levels;
54 HIMAGELIST himages;
55 DWORD last_lv_index;
56 DWORD last_level;
57 } secdlg_data;
59 #define NUM_TRACKBAR_POS 5
61 static DWORD url_templates[] = {URLTEMPLATE_CUSTOM,
62 URLTEMPLATE_LOW,
63 URLTEMPLATE_MEDLOW,
64 URLTEMPLATE_MEDIUM,
65 URLTEMPLATE_MEDHIGH,
66 URLTEMPLATE_HIGH};
68 /*********************************************************************
69 * index_from_urltemplate [internal]
72 static DWORD index_from_urltemplate(URLTEMPLATE value)
75 DWORD index = sizeof(url_templates) / sizeof(url_templates[0]);
77 while((index > 0) && (url_templates[index-1] != value))
78 index--;
80 index--; /* table entries are 0 based */
81 if (!index && value)
82 FIXME("URLTEMPLATE 0x%x not supported\n", value);
84 TRACE("URLTEMPLATE 0x%08x=> Level %d\n", value, index);
85 return index;
88 /*********************************************************************
89 * update_security_level [internal]
92 static void update_security_level(secdlg_data *sd, DWORD lv_index, DWORD tb_index)
94 WCHAR name[512];
95 DWORD current_index;
97 TRACE("(%p, lv_index: %u, tb_index: %u)\n", sd, lv_index, tb_index);
99 if ((sd->levels[lv_index] != sd->last_level) || (tb_index > 0)) {
100 /* show or hide the trackbar */
101 if (!sd->levels[lv_index] || !sd->last_level)
102 ShowWindow(sd->htb, sd->levels[lv_index] ? SW_NORMAL : SW_HIDE);
104 current_index = (tb_index > 0) ? tb_index : index_from_urltemplate(sd->levels[lv_index]);
106 name[0] = 0;
107 LoadStringW(hcpl, IDS_SEC_LEVEL0 + current_index, name, sizeof(name)/sizeof(name[0]));
108 TRACE("new level #%d: %s\n", current_index, debugstr_w(name));
109 SetWindowTextW(GetDlgItem(sd->hsec, IDC_SEC_LEVEL), name);
111 name[0] = 0;
112 LoadStringW(hcpl, IDS_SEC_LEVEL0_INFO + (current_index * 0x10), name, sizeof(name)/sizeof(name[0]));
113 TRACE("new level info: %s\n", debugstr_w(name));
114 SetWindowTextW(GetDlgItem(sd->hsec, IDC_SEC_LEVEL_INFO), name);
116 if (current_index)
117 SendMessageW(sd->htb, TBM_SETPOS, TRUE, NUM_TRACKBAR_POS - current_index);
119 sd->last_level = sd->levels[lv_index];
124 /*********************************************************************
125 * update_zone_info [internal]
128 static void update_zone_info(secdlg_data *sd, DWORD lv_index)
130 ZONEATTRIBUTES *za = &sd->zone_attr[lv_index];
131 WCHAR name[MAX_PATH];
132 DWORD len;
134 SetWindowTextW(GetDlgItem(sd->hsec, IDC_SEC_ZONE_INFO), za->szDescription);
136 len = LoadStringW(hcpl, IDS_SEC_SETTINGS, name, sizeof(name)/sizeof(*name));
137 lstrcpynW(&name[len], za->szDisplayName, sizeof(name)/sizeof(*name) - len - 1);
139 TRACE("new title: %s\n", debugstr_w(name));
140 SetWindowTextW(GetDlgItem(sd->hsec, IDC_SEC_GROUP), name);
142 update_security_level(sd, lv_index, 0);
143 sd->last_lv_index = lv_index;
146 /*********************************************************************
147 * add_zone_to_listview [internal]
150 static void add_zone_to_listview(secdlg_data *sd, DWORD *pindex, DWORD zone)
152 DWORD lv_index = *pindex;
153 ZONEATTRIBUTES *za = &sd->zone_attr[lv_index];
154 LVITEMW lvitem;
155 HRESULT hr;
156 INT iconid = 0;
157 HMODULE hdll = NULL;
158 WCHAR * ptr;
159 HICON icon;
161 TRACE("item %d (zone %d)\n", lv_index, zone);
163 sd->zones[lv_index] = zone;
165 memset(&lvitem, 0, sizeof(LVITEMW));
166 memset(za, 0, sizeof(ZONEATTRIBUTES));
167 za->cbSize = sizeof(ZONEATTRIBUTES);
168 hr = IInternetZoneManager_GetZoneAttributes(sd->zone_mgr, zone, za);
169 if (SUCCEEDED(hr)) {
170 TRACE("displayname: %s\n", debugstr_w(za->szDisplayName));
171 TRACE("description: %s\n", debugstr_w(za->szDescription));
172 TRACE("minlevel: 0x%x, recommended: 0x%x, current: 0x%x (flags: 0x%x)\n", za->dwTemplateMinLevel,
173 za->dwTemplateRecommended, za->dwTemplateCurrentLevel, za->dwFlags);
175 if (za->dwFlags & ZAFLAGS_NO_UI ) {
176 TRACE("item %d (zone %d): UI disabled for %s\n", lv_index, zone, debugstr_w(za->szDisplayName));
177 return;
180 sd->levels[lv_index] = za->dwTemplateCurrentLevel;
182 lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
183 lvitem.iItem = lv_index;
184 lvitem.iSubItem = 0;
185 lvitem.pszText = za->szDisplayName;
186 lvitem.lParam = (LPARAM) zone;
188 /* format is "filename.ext#iconid" */
189 ptr = StrChrW(za->szIconPath, '#');
190 if (ptr) {
191 *ptr = 0;
192 ptr++;
193 iconid = StrToIntW(ptr);
194 hdll = LoadLibraryExW(za->szIconPath, NULL, LOAD_LIBRARY_AS_DATAFILE);
195 TRACE("%p: icon #%d from %s\n", hdll, iconid, debugstr_w(za->szIconPath));
197 icon = LoadImageW(hdll, MAKEINTRESOURCEW(iconid), IMAGE_ICON, GetSystemMetrics(SM_CXICON),
198 GetSystemMetrics(SM_CYICON), LR_SHARED);
200 if (!icon) {
201 FIXME("item %d (zone %d): missing icon #%d in %s\n", lv_index, zone, iconid, debugstr_w(za->szIconPath));
204 /* the failure result (NULL) from LoadImageW let ImageList_AddIcon fail
205 with -1, which is reused in ListView_InsertItemW to disable the image */
206 lvitem.iImage = ImageList_AddIcon(sd->himages, icon);
208 else
209 FIXME("item %d (zone %d): malformed szIconPath %s\n", lv_index, zone, debugstr_w(za->szIconPath));
211 if (ListView_InsertItemW(sd->hlv, &lvitem) >= 0) {
212 /* activate first item in the listview */
213 if (! lv_index) {
214 lvitem.state = LVIS_FOCUSED | LVIS_SELECTED;
215 lvitem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
216 SendMessageW(sd->hlv, LVM_SETITEMSTATE, 0, (LPARAM) &lvitem);
217 sd->last_level = ~0;
218 update_zone_info(sd, lv_index);
220 (*pindex)++;
222 FreeLibrary(hdll);
224 else
225 FIXME("item %d (zone %d): GetZoneAttributes failed with 0x%x\n", lv_index, zone, hr);
228 /*********************************************************************
229 * security_cleanup_zones [internal]
232 static void security_cleanup_zones(secdlg_data *sd)
234 if (sd->zone_enumerator) {
235 IInternetZoneManager_DestroyZoneEnumerator(sd->zone_mgr, sd->zone_enumerator);
238 if (sd->zone_mgr) {
239 IInternetZoneManager_Release(sd->zone_mgr);
242 if (sd->sec_mgr) {
243 IInternetSecurityManager_Release(sd->sec_mgr);
247 /*********************************************************************
248 * security_enum_zones [internal]
251 static HRESULT security_enum_zones(secdlg_data * sd)
253 HRESULT hr;
255 hr = CoInternetCreateSecurityManager(NULL, &sd->sec_mgr, 0);
256 if (SUCCEEDED(hr)) {
257 hr = CoInternetCreateZoneManager(NULL, &sd->zone_mgr, 0);
258 if (SUCCEEDED(hr)) {
259 hr = IInternetZoneManager_CreateZoneEnumerator(sd->zone_mgr, &sd->zone_enumerator, &sd->num_zones, 0);
262 return hr;
265 /*********************************************************************
266 * security_on_destroy [internal]
268 * handle WM_NCDESTROY
271 static INT_PTR security_on_destroy(secdlg_data * sd)
273 TRACE("(%p)\n", sd);
275 heap_free(sd->zone_attr);
276 heap_free(sd->zones);
277 if (sd->himages) {
278 SendMessageW(sd->hlv, LVM_SETIMAGELIST, LVSIL_NORMAL, 0);
279 ImageList_Destroy(sd->himages);
282 security_cleanup_zones(sd);
283 SetWindowLongPtrW(sd->hsec, DWLP_USER, 0);
284 heap_free(sd);
285 return TRUE;
288 /*********************************************************************
289 * security_on_initdialog [internal]
291 * handle WM_INITDIALOG
294 static INT_PTR security_on_initdialog(HWND hsec)
296 secdlg_data *sd;
297 HRESULT hr;
298 DWORD current_zone;
299 DWORD lv_index = 0;
300 DWORD i;
302 sd = heap_alloc_zero(sizeof(secdlg_data));
303 SetWindowLongPtrW(hsec, DWLP_USER, (LONG_PTR) sd);
304 if (!sd) {
305 return FALSE;
308 sd->hsec = hsec;
309 sd->hlv = GetDlgItem(hsec, IDC_SEC_LISTVIEW);
310 sd->htb = GetDlgItem(hsec, IDC_SEC_TRACKBAR);
312 EnableWindow(sd->htb, FALSE); /* not changeable yet */
314 TRACE("(%p) (data: %p, listview: %p, trackbar: %p)\n", hsec, sd, sd->hlv, sd->htb);
316 SendMessageW(sd->htb, TBM_SETRANGE, FALSE, MAKELONG(0, NUM_TRACKBAR_POS - 1));
317 SendMessageW(sd->htb, TBM_SETTICFREQ, 1, 0 );
319 /* Create the image lists for the listview */
320 sd->himages = ImageList_Create(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), ILC_COLOR32 | ILC_MASK, 1, 1);
322 TRACE("using imagelist: %p\n", sd->himages);
323 if (!sd->himages) {
324 ERR("ImageList_Create failed!\n");
325 return FALSE;
327 SendMessageW(sd->hlv, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)sd->himages);
329 hr = security_enum_zones(sd);
330 if (FAILED(hr)) {
331 ERR("got 0x%x\n", hr);
332 security_on_destroy(sd);
333 return FALSE;
336 TRACE("found %d zones\n", sd->num_zones);
338 /* remember ZONEATTRIBUTES for a listview entry */
339 sd->zone_attr = heap_alloc(sizeof(ZONEATTRIBUTES) * sd->num_zones);
340 if (!sd->zone_attr) {
341 security_on_destroy(sd);
342 return FALSE;
345 /* remember zone number and current security level for a listview entry */
346 sd->zones = heap_alloc((sizeof(DWORD) + sizeof(DWORD)) * sd->num_zones);
347 if (!sd->zones) {
348 security_on_destroy(sd);
349 return FALSE;
351 sd->levels = &sd->zones[sd->num_zones];
353 /* use the same order as visible with native inetcpl.cpl */
354 add_zone_to_listview(sd, &lv_index, URLZONE_INTERNET);
355 add_zone_to_listview(sd, &lv_index, URLZONE_INTRANET);
356 add_zone_to_listview(sd, &lv_index, URLZONE_TRUSTED);
357 add_zone_to_listview(sd, &lv_index, URLZONE_UNTRUSTED);
359 for (i = 0; i < sd->num_zones; i++)
361 hr = IInternetZoneManager_GetZoneAt(sd->zone_mgr, sd->zone_enumerator, i, &current_zone);
362 if (SUCCEEDED(hr) && (current_zone != (DWORD)URLZONE_INVALID)) {
363 if (!current_zone || (current_zone > URLZONE_UNTRUSTED)) {
364 add_zone_to_listview(sd, &lv_index, current_zone);
368 return TRUE;
371 /*********************************************************************
372 * security_on_notify [internal]
374 * handle WM_NOTIFY
377 static INT_PTR security_on_notify(secdlg_data *sd, WPARAM wparam, LPARAM lparam)
379 NMLISTVIEW *nm;
381 nm = (NMLISTVIEW *) lparam;
382 switch (nm->hdr.code)
384 case LVN_ITEMCHANGED:
385 TRACE("LVN_ITEMCHANGED (0x%lx, 0x%lx) from %p with code: %d (item: %d, uNewState: %u)\n",
386 wparam, lparam, nm->hdr.hwndFrom, nm->hdr.code, nm->iItem, nm->uNewState);
387 if ((nm->uNewState & LVIS_SELECTED) == LVIS_SELECTED) {
388 update_zone_info(sd, nm->iItem);
390 break;
392 case PSN_APPLY:
393 TRACE("PSN_APPLY (0x%lx, 0x%lx) from %p with code: %d\n", wparam, lparam,
394 nm->hdr.hwndFrom, nm->hdr.code);
395 break;
397 default:
398 TRACE("WM_NOTIFY (0x%lx, 0x%lx) from %p with code: %d\n", wparam, lparam,
399 nm->hdr.hwndFrom, nm->hdr.code);
402 return FALSE;
405 /*********************************************************************
406 * security_dlgproc [internal]
409 INT_PTR CALLBACK security_dlgproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
411 secdlg_data *sd;
413 if (msg == WM_INITDIALOG) {
414 return security_on_initdialog(hwnd);
417 sd = (secdlg_data *)GetWindowLongPtrW(hwnd, DWLP_USER);
418 if (sd) {
419 switch (msg)
421 case WM_NOTIFY:
422 return security_on_notify(sd, wparam, lparam);
424 case WM_NCDESTROY:
425 return security_on_destroy(sd);
427 default:
428 /* do not flood the log */
429 if ((msg == WM_SETCURSOR) || (msg == WM_NCHITTEST) ||
430 (msg == WM_MOUSEMOVE) || (msg == WM_MOUSEACTIVATE) || (msg == WM_PARENTNOTIFY))
431 return FALSE;
433 TRACE("(%p, 0x%08x/%03d, 0x%08lx, 0x%08lx)\n", hwnd, msg, msg, wparam, lparam);
436 return FALSE;