appwiz: Use the correct types for callbacks.
[wine] / dlls / appwiz.cpl / appwiz.c
1 /*
2  * Add/Remove Programs applet
3  * Partially based on Wine Uninstaller
4  *
5  * Copyright 2000 Andreas Mohr
6  * Copyright 2004 Hannu Valtonen
7  * Copyright 2005 Jonathan Ernst
8  * Copyright 2001-2002, 2008 Owen Rudge
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  *
24  */
25
26 #define NONAMELESSUNION
27
28 #include "config.h"
29 #include "wine/port.h"
30
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <windef.h>
36 #include <winbase.h>
37 #include <winuser.h>
38 #include <wingdi.h>
39 #include <winreg.h>
40 #include <shellapi.h>
41 #include <commctrl.h>
42 #include <commdlg.h>
43 #include <cpl.h>
44
45 #include "wine/unicode.h"
46 #include "wine/list.h"
47 #include "wine/debug.h"
48 #include "appwiz.h"
49 #include "res.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl);
52
53 /* define a maximum length for various buffers we use */
54 #define MAX_STRING_LEN    1024
55
56 typedef struct APPINFO
57 {
58     struct list entry;
59     int id;
60
61     LPWSTR title;
62     LPWSTR path;
63     LPWSTR path_modify;
64
65     LPWSTR icon;
66     int iconIdx;
67
68     LPWSTR publisher;
69     LPWSTR version;
70
71     HKEY regroot;
72     WCHAR regkey[MAX_STRING_LEN];
73 } APPINFO;
74
75 static struct list app_list = LIST_INIT( app_list );
76 HINSTANCE hInst;
77
78 static WCHAR btnRemove[MAX_STRING_LEN];
79 static WCHAR btnModifyRemove[MAX_STRING_LEN];
80
81 static const WCHAR openW[] = {'o','p','e','n',0};
82
83 /* names of registry keys */
84 static const WCHAR BackSlashW[] = { '\\', 0 };
85 static const WCHAR DisplayNameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
86 static const WCHAR DisplayIconW[] = {'D','i','s','p','l','a','y','I','c','o','n',0};
87 static const WCHAR DisplayVersionW[] = {'D','i','s','p','l','a','y','V','e','r',
88     's','i','o','n',0};
89 static const WCHAR PublisherW[] = {'P','u','b','l','i','s','h','e','r',0};
90 static const WCHAR ContactW[] = {'C','o','n','t','a','c','t',0};
91 static const WCHAR HelpLinkW[] = {'H','e','l','p','L','i','n','k',0};
92 static const WCHAR HelpTelephoneW[] = {'H','e','l','p','T','e','l','e','p','h',
93     'o','n','e',0};
94 static const WCHAR ModifyPathW[] = {'M','o','d','i','f','y','P','a','t','h',0};
95 static const WCHAR NoModifyW[] = {'N','o','M','o','d','i','f','y',0};
96 static const WCHAR ReadmeW[] = {'R','e','a','d','m','e',0};
97 static const WCHAR URLUpdateInfoW[] = {'U','R','L','U','p','d','a','t','e','I',
98     'n','f','o',0};
99 static const WCHAR CommentsW[] = {'C','o','m','m','e','n','t','s',0};
100 static const WCHAR UninstallCommandlineW[] = {'U','n','i','n','s','t','a','l','l',
101     'S','t','r','i','n','g',0};
102 static const WCHAR WindowsInstallerW[] = {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
103 static const WCHAR SystemComponentW[] = {'S','y','s','t','e','m','C','o','m','p','o','n','e','n','t',0};
104
105 static const WCHAR PathUninstallW[] = {
106         'S','o','f','t','w','a','r','e','\\',
107         'M','i','c','r','o','s','o','f','t','\\',
108         'W','i','n','d','o','w','s','\\',
109         'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
110         'U','n','i','n','s','t','a','l','l',0 };
111
112 /******************************************************************************
113  * Name       : DllMain
114  * Description: Entry point for DLL file
115  */
116 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
117                     LPVOID lpvReserved)
118 {
119     TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
120
121     switch (fdwReason)
122     {
123         case DLL_PROCESS_ATTACH:
124             hInst = hinstDLL;
125             break;
126     }
127     return TRUE;
128 }
129
130 /******************************************************************************
131  * Name       : FreeAppInfo
132  * Description: Frees memory used by an AppInfo structure, and any children.
133  */
134 static void FreeAppInfo(APPINFO *info)
135 {
136     HeapFree(GetProcessHeap(), 0, info->title);
137     HeapFree(GetProcessHeap(), 0, info->path);
138     HeapFree(GetProcessHeap(), 0, info->path_modify);
139     HeapFree(GetProcessHeap(), 0, info->icon);
140     HeapFree(GetProcessHeap(), 0, info->publisher);
141     HeapFree(GetProcessHeap(), 0, info->version);
142     HeapFree(GetProcessHeap(), 0, info);
143 }
144
145 /******************************************************************************
146  * Name       : ReadApplicationsFromRegistry
147  * Description: Creates a linked list of uninstallable applications from the
148  *              registry.
149  * Parameters : root    - Which registry root to read from
150  * Returns    : TRUE if successful, FALSE otherwise
151  */
152 static BOOL ReadApplicationsFromRegistry(HKEY root)
153 {
154     HKEY hkeyApp;
155     int i, id = 0;
156     DWORD sizeOfSubKeyName, displen, uninstlen;
157     DWORD dwNoModify, dwType, value, size;
158     WCHAR subKeyName[256];
159     WCHAR *command;
160     APPINFO *info = NULL;
161     LPWSTR iconPtr;
162
163     sizeOfSubKeyName = sizeof(subKeyName) / sizeof(subKeyName[0]);
164
165     for (i = 0; RegEnumKeyExW(root, i, subKeyName, &sizeOfSubKeyName, NULL,
166         NULL, NULL, NULL) != ERROR_NO_MORE_ITEMS; ++i)
167     {
168         RegOpenKeyExW(root, subKeyName, 0, KEY_READ, &hkeyApp);
169         size = sizeof(value);
170         if (!RegQueryValueExW(hkeyApp, SystemComponentW, NULL, &dwType, (LPBYTE)&value, &size)
171             && dwType == REG_DWORD && value == 1)
172         {
173             RegCloseKey(hkeyApp);
174             sizeOfSubKeyName = sizeof(subKeyName) / sizeof(subKeyName[0]);
175             continue;
176         }
177         displen = 0;
178         uninstlen = 0;
179         if (!RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, NULL, &displen))
180         {
181             size = sizeof(value);
182             if (!RegQueryValueExW(hkeyApp, WindowsInstallerW, NULL, &dwType, (LPBYTE)&value, &size)
183                 && dwType == REG_DWORD && value == 1)
184             {
185                 static const WCHAR fmtW[] = {'m','s','i','e','x','e','c',' ','/','x','%','s',0};
186                 int len = lstrlenW(fmtW) + lstrlenW(subKeyName);
187
188                 if (!(command = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) goto err;
189                 wsprintfW(command, fmtW, subKeyName);
190             }
191             else if (!RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, NULL, &uninstlen))
192             {
193                 if (!(command = HeapAlloc(GetProcessHeap(), 0, uninstlen))) goto err;
194                 RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, (LPBYTE)command, &uninstlen);
195             }
196             else
197             {
198                 RegCloseKey(hkeyApp);
199                 sizeOfSubKeyName = sizeof(subKeyName) / sizeof(subKeyName[0]);
200                 continue;
201             }
202
203             info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct APPINFO));
204             if (!info) goto err;
205
206             info->title = HeapAlloc(GetProcessHeap(), 0, displen);
207
208             if (!info->title)
209                 goto err;
210
211             RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, (LPBYTE)info->title,
212                 &displen);
213
214             /* now get DisplayIcon */
215             displen = 0;
216             RegQueryValueExW(hkeyApp, DisplayIconW, 0, 0, NULL, &displen);
217
218             if (displen == 0)
219                 info->icon = 0;
220             else
221             {
222                 info->icon = HeapAlloc(GetProcessHeap(), 0, displen);
223
224                 if (!info->icon)
225                     goto err;
226
227                 RegQueryValueExW(hkeyApp, DisplayIconW, 0, 0, (LPBYTE)info->icon,
228                     &displen);
229
230                 /* separate the index from the icon name, if supplied */
231                 iconPtr = strchrW(info->icon, ',');
232
233                 if (iconPtr)
234                 {
235                     *iconPtr++ = 0;
236                     info->iconIdx = atoiW(iconPtr);
237                 }
238             }
239
240             /* publisher, version */
241             if (RegQueryValueExW(hkeyApp, PublisherW, 0, 0, NULL, &displen) ==
242                 ERROR_SUCCESS)
243             {
244                 info->publisher = HeapAlloc(GetProcessHeap(), 0, displen);
245
246                 if (!info->publisher)
247                     goto err;
248
249                 RegQueryValueExW(hkeyApp, PublisherW, 0, 0, (LPBYTE)info->publisher,
250                     &displen);
251             }
252
253             if (RegQueryValueExW(hkeyApp, DisplayVersionW, 0, 0, NULL, &displen) ==
254                 ERROR_SUCCESS)
255             {
256                 info->version = HeapAlloc(GetProcessHeap(), 0, displen);
257
258                 if (!info->version)
259                     goto err;
260
261                 RegQueryValueExW(hkeyApp, DisplayVersionW, 0, 0, (LPBYTE)info->version,
262                     &displen);
263             }
264
265             /* Check if NoModify is set */
266             dwType = REG_DWORD;
267             dwNoModify = 0;
268             displen = sizeof(DWORD);
269
270             if (RegQueryValueExW(hkeyApp, NoModifyW, NULL, &dwType, (LPBYTE)&dwNoModify, &displen)
271                 != ERROR_SUCCESS)
272             {
273                 dwNoModify = 0;
274             }
275
276             /* Some installers incorrectly create a REG_SZ instead of a REG_DWORD */
277             if (dwType == REG_SZ)
278                 dwNoModify = (*(BYTE *)&dwNoModify == '1');
279
280             /* Fetch the modify path */
281             if (!dwNoModify)
282             {
283                 size = sizeof(value);
284                 if (!RegQueryValueExW(hkeyApp, WindowsInstallerW, NULL, &dwType, (LPBYTE)&value, &size)
285                     && dwType == REG_DWORD && value == 1)
286                 {
287                     static const WCHAR fmtW[] = {'m','s','i','e','x','e','c',' ','/','i','%','s',0};
288                     int len = lstrlenW(fmtW) + lstrlenW(subKeyName);
289
290                     if (!(info->path_modify = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) goto err;
291                     wsprintfW(info->path_modify, fmtW, subKeyName);
292                 }
293                 else if (!RegQueryValueExW(hkeyApp, ModifyPathW, 0, 0, NULL, &displen))
294                 {
295                     if (!(info->path_modify = HeapAlloc(GetProcessHeap(), 0, displen))) goto err;
296                     RegQueryValueExW(hkeyApp, ModifyPathW, 0, 0, (LPBYTE)info->path_modify, &displen);
297                 }
298             }
299
300             /* registry key */
301             info->regroot = root;
302             lstrcpyW(info->regkey, subKeyName);
303             info->path = command;
304
305             info->id = id++;
306             list_add_tail( &app_list, &info->entry );
307         }
308
309         RegCloseKey(hkeyApp);
310         sizeOfSubKeyName = sizeof(subKeyName) / sizeof(subKeyName[0]);
311     }
312
313     return TRUE;
314 err:
315     RegCloseKey(hkeyApp);
316     if (info) FreeAppInfo(info);
317     return FALSE;
318 }
319
320
321 /******************************************************************************
322  * Name       : AddApplicationsToList
323  * Description: Populates the list box with applications.
324  * Parameters : hWnd    - Handle of the dialog box
325  */
326 static void AddApplicationsToList(HWND hWnd, HIMAGELIST hList)
327 {
328     APPINFO *iter;
329     LVITEMW lvItem;
330     HICON hIcon;
331     int index;
332
333     LIST_FOR_EACH_ENTRY( iter, &app_list, APPINFO, entry )
334     {
335         if (!iter->title[0]) continue;
336
337         /* get the icon */
338         index = 0;
339
340         if (iter->icon)
341         {
342             if (ExtractIconExW(iter->icon, iter->iconIdx, NULL, &hIcon, 1) == 1)
343             {
344                 index = ImageList_AddIcon(hList, hIcon);
345                 DestroyIcon(hIcon);
346             }
347         }
348
349         lvItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
350         lvItem.iItem = iter->id;
351         lvItem.iSubItem = 0;
352         lvItem.pszText = iter->title;
353         lvItem.iImage = index;
354         lvItem.lParam = iter->id;
355
356         index = ListView_InsertItemW(hWnd, &lvItem);
357
358         /* now add the subitems (columns) */
359         ListView_SetItemTextW(hWnd, index, 1, iter->publisher);
360         ListView_SetItemTextW(hWnd, index, 2, iter->version);
361     }
362 }
363
364 /******************************************************************************
365  * Name       : RemoveItemsFromList
366  * Description: Clears the application list box.
367  * Parameters : hWnd    - Handle of the dialog box
368  */
369 static void RemoveItemsFromList(HWND hWnd)
370 {
371     SendDlgItemMessageW(hWnd, IDL_PROGRAMS, LVM_DELETEALLITEMS, 0, 0);
372 }
373
374 /******************************************************************************
375  * Name       : EmptyList
376  * Description: Frees memory used by the application linked list.
377  */
378 static inline void EmptyList(void)
379 {
380     APPINFO *info, *next;
381     LIST_FOR_EACH_ENTRY_SAFE( info, next, &app_list, APPINFO, entry )
382     {
383         list_remove( &info->entry );
384         FreeAppInfo( info );
385     }
386 }
387
388 /******************************************************************************
389  * Name       : UpdateButtons
390  * Description: Enables/disables the Add/Remove button depending on current
391  *              selection in list box.
392  * Parameters : hWnd    - Handle of the dialog box
393  */
394 static void UpdateButtons(HWND hWnd)
395 {
396     APPINFO *iter;
397     LVITEMW lvItem;
398     LRESULT selitem = SendDlgItemMessageW(hWnd, IDL_PROGRAMS, LVM_GETNEXTITEM, -1,
399        LVNI_FOCUSED | LVNI_SELECTED);
400     BOOL enable_modify = FALSE;
401
402     if (selitem != -1)
403     {
404         lvItem.iItem = selitem;
405         lvItem.mask = LVIF_PARAM;
406
407         if (SendDlgItemMessageW(hWnd, IDL_PROGRAMS, LVM_GETITEMW, 0, (LPARAM) &lvItem))
408         {
409             LIST_FOR_EACH_ENTRY( iter, &app_list, APPINFO, entry )
410             {
411                 if (iter->id == lvItem.lParam)
412                 {
413                     /* Decide whether to display Modify/Remove as one button or two */
414                     enable_modify = (iter->path_modify != NULL);
415
416                     /* Update title as appropriate */
417                     if (iter->path_modify == NULL)
418                         SetWindowTextW(GetDlgItem(hWnd, IDC_ADDREMOVE), btnModifyRemove);
419                     else
420                         SetWindowTextW(GetDlgItem(hWnd, IDC_ADDREMOVE), btnRemove);
421
422                     break;
423                 }
424             }
425         }
426     }
427
428     /* Enable/disable other buttons if necessary */
429     EnableWindow(GetDlgItem(hWnd, IDC_ADDREMOVE), (selitem != -1));
430     EnableWindow(GetDlgItem(hWnd, IDC_SUPPORT_INFO), (selitem != -1));
431     EnableWindow(GetDlgItem(hWnd, IDC_MODIFY), enable_modify);
432 }
433
434 /******************************************************************************
435  * Name       : InstallProgram
436  * Description: Search for potential Installer and execute it.
437  * Parameters : hWnd    - Handle of the dialog box
438  */
439 static void InstallProgram(HWND hWnd)
440 {
441     static const WCHAR filters[] = {'%','s','%','c','*','i','n','s','t','a','l','*','.','e','x','e',';','*','s','e','t','u','p','*','.','e','x','e',';','*','.','m','s','i','%','c','%','s','%','c','*','.','e','x','e','%','c','%','s','%','c','*','.','*','%','c',0}
442 ;
443     OPENFILENAMEW ofn;
444     WCHAR titleW[MAX_STRING_LEN];
445     WCHAR filter_installs[MAX_STRING_LEN];
446     WCHAR filter_programs[MAX_STRING_LEN];
447     WCHAR filter_all[MAX_STRING_LEN];
448     WCHAR FilterBufferW[MAX_PATH];
449     WCHAR FileNameBufferW[MAX_PATH];
450
451     LoadStringW(hInst, IDS_CPL_TITLE, titleW, sizeof(titleW)/sizeof(WCHAR));
452     LoadStringW(hInst, IDS_FILTER_INSTALLS, filter_installs, sizeof(filter_installs)/sizeof(WCHAR));
453     LoadStringW(hInst, IDS_FILTER_PROGRAMS, filter_programs, sizeof(filter_programs)/sizeof(WCHAR));
454     LoadStringW(hInst, IDS_FILTER_ALL, filter_all, sizeof(filter_all)/sizeof(WCHAR));
455
456     snprintfW( FilterBufferW, MAX_PATH, filters, filter_installs, 0, 0,
457                filter_programs, 0, 0, filter_all, 0, 0 );
458     memset(&ofn, 0, sizeof(OPENFILENAMEW));
459     ofn.lStructSize = sizeof(OPENFILENAMEW);
460     ofn.hwndOwner = hWnd;
461     ofn.hInstance = hInst;
462     ofn.lpstrFilter = FilterBufferW;
463     ofn.nFilterIndex = 0;
464     ofn.lpstrFile = FileNameBufferW;
465     ofn.nMaxFile = MAX_PATH;
466     ofn.lpstrFileTitle = NULL;
467     ofn.nMaxFileTitle = 0;
468     ofn.lpstrTitle = titleW;
469     ofn.Flags = OFN_HIDEREADONLY | OFN_ENABLESIZING;
470     FileNameBufferW[0] = 0;
471
472     if (GetOpenFileNameW(&ofn))
473     {
474         SHELLEXECUTEINFOW sei;
475         memset(&sei, 0, sizeof(sei));
476         sei.cbSize = sizeof(sei);
477         sei.lpVerb = openW;
478         sei.nShow = SW_SHOWDEFAULT;
479         sei.fMask = SEE_MASK_NO_CONSOLE;
480         sei.lpFile = ofn.lpstrFile;
481
482         ShellExecuteExW(&sei);
483     }
484 }
485
486 /******************************************************************************
487  * Name       : UninstallProgram
488  * Description: Executes the specified program's installer.
489  * Parameters : id      - the internal ID of the installer to remove
490  * Parameters : button  - ID of button pressed (Modify or Remove)
491  */
492 static void UninstallProgram(int id, DWORD button)
493 {
494     APPINFO *iter;
495     STARTUPINFOW si;
496     PROCESS_INFORMATION info;
497     WCHAR errormsg[MAX_STRING_LEN];
498     WCHAR sUninstallFailed[MAX_STRING_LEN];
499     HKEY hkey;
500     BOOL res;
501
502     LoadStringW(hInst, IDS_UNINSTALL_FAILED, sUninstallFailed,
503         sizeof(sUninstallFailed) / sizeof(sUninstallFailed[0]));
504
505     LIST_FOR_EACH_ENTRY( iter, &app_list, APPINFO, entry )
506     {
507         if (iter->id == id)
508         {
509             TRACE("Uninstalling %s (%s)\n", wine_dbgstr_w(iter->title),
510                 wine_dbgstr_w(iter->path));
511
512             memset(&si, 0, sizeof(STARTUPINFOW));
513             si.cb = sizeof(STARTUPINFOW);
514             si.wShowWindow = SW_NORMAL;
515
516             res = CreateProcessW(NULL, (button == IDC_MODIFY) ? iter->path_modify : iter->path,
517                 NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
518
519             if (res)
520             {
521                 CloseHandle(info.hThread);
522
523                 /* wait for the process to exit */
524                 WaitForSingleObject(info.hProcess, INFINITE);
525                 CloseHandle(info.hProcess);
526             }
527             else
528             {
529                 wsprintfW(errormsg, sUninstallFailed, iter->path);
530
531                 if (MessageBoxW(0, errormsg, iter->title, MB_YESNO |
532                     MB_ICONQUESTION) == IDYES)
533                 {
534                     /* delete the application's uninstall entry */
535                     RegOpenKeyExW(iter->regroot, PathUninstallW, 0, KEY_READ, &hkey);
536                     RegDeleteKeyW(hkey, iter->regkey);
537                     RegCloseKey(hkey);
538                 }
539             }
540
541             break;
542         }
543     }
544 }
545
546 /**********************************************************************************
547  * Name       : SetInfoDialogText
548  * Description: Sets the text of a label in a window, based upon a registry entry
549  *              or string passed to the function.
550  * Parameters : hKey         - registry entry to read from, NULL if not reading
551  *                             from registry
552  *              lpKeyName    - key to read from, or string to check if hKey is NULL
553  *              lpAltMessage - alternative message if entry not found
554  *              hWnd         - handle of dialog box
555  *              iDlgItem     - ID of label in dialog box
556  */
557 static void SetInfoDialogText(HKEY hKey, LPCWSTR lpKeyName, LPCWSTR lpAltMessage,
558   HWND hWnd, int iDlgItem)
559 {
560     WCHAR buf[MAX_STRING_LEN];
561     DWORD buflen;
562     HWND hWndDlgItem;
563
564     hWndDlgItem = GetDlgItem(hWnd, iDlgItem);
565
566     /* if hKey is null, lpKeyName contains the string we want to check */
567     if (hKey == NULL)
568     {
569         if ((lpKeyName) && (lstrlenW(lpKeyName) > 0))
570             SetWindowTextW(hWndDlgItem, lpKeyName);
571         else
572             SetWindowTextW(hWndDlgItem, lpAltMessage);
573     }
574     else
575     {
576         buflen = MAX_STRING_LEN;
577
578         if ((RegQueryValueExW(hKey, lpKeyName, 0, 0, (LPBYTE) buf, &buflen) ==
579            ERROR_SUCCESS) && (lstrlenW(buf) > 0))
580             SetWindowTextW(hWndDlgItem, buf);
581         else
582             SetWindowTextW(hWndDlgItem, lpAltMessage);
583     }
584 }
585
586 /******************************************************************************
587  * Name       : SupportInfoDlgProc
588  * Description: Callback procedure for support info dialog
589  * Parameters : hWnd    - hWnd of the window
590  *              msg     - reason for calling function
591  *              wParam  - additional parameter
592  *              lParam  - additional parameter
593  * Returns    : Depends on the message
594  */
595 static INT_PTR CALLBACK SupportInfoDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
596 {
597     APPINFO *iter;
598     HKEY hkey;
599     WCHAR oldtitle[MAX_STRING_LEN];
600     WCHAR buf[MAX_STRING_LEN];
601     WCHAR key[MAX_STRING_LEN];
602     WCHAR notfound[MAX_STRING_LEN];
603
604     switch(msg)
605     {
606         case WM_INITDIALOG:
607             LIST_FOR_EACH_ENTRY( iter, &app_list, APPINFO, entry )
608             {
609                 if (iter->id == (int) lParam)
610                 {
611                     lstrcpyW(key, PathUninstallW);
612                     lstrcatW(key, BackSlashW);
613                     lstrcatW(key, iter->regkey);
614
615                     /* check the application's registry entries */
616                     RegOpenKeyExW(iter->regroot, key, 0, KEY_READ, &hkey);
617
618                     /* Load our "not specified" string */
619                     LoadStringW(hInst, IDS_NOT_SPECIFIED, notfound,
620                         sizeof(notfound) / sizeof(notfound[0]));
621
622                     /* Update the data for items already read into the structure */
623                     SetInfoDialogText(NULL, iter->publisher, notfound, hWnd,
624                         IDC_INFO_PUBLISHER);
625                     SetInfoDialogText(NULL, iter->version, notfound, hWnd,
626                         IDC_INFO_VERSION);
627
628                     /* And now update the data for those items in the registry */
629                     SetInfoDialogText(hkey, ContactW, notfound, hWnd,
630                         IDC_INFO_CONTACT);
631                     SetInfoDialogText(hkey, HelpLinkW, notfound, hWnd,
632                         IDC_INFO_SUPPORT);
633                     SetInfoDialogText(hkey, HelpTelephoneW, notfound, hWnd,
634                         IDC_INFO_PHONE);
635                     SetInfoDialogText(hkey, ReadmeW, notfound, hWnd,
636                         IDC_INFO_README);
637                     SetInfoDialogText(hkey, URLUpdateInfoW, notfound, hWnd,
638                         IDC_INFO_UPDATES);
639                     SetInfoDialogText(hkey, CommentsW, notfound, hWnd,
640                         IDC_INFO_COMMENTS);
641
642                     /* Update the main label with the app name */
643                     if (GetWindowTextW(GetDlgItem(hWnd, IDC_INFO_LABEL), oldtitle,
644                         MAX_STRING_LEN) != 0)
645                     {
646                         wsprintfW(buf, oldtitle, iter->title);
647                         SetWindowTextW(GetDlgItem(hWnd, IDC_INFO_LABEL), buf);
648                     }
649
650                     RegCloseKey(hkey);
651
652                     break;
653                 }
654             }
655
656             return TRUE;
657
658         case WM_DESTROY:
659             return 0;
660
661         case WM_COMMAND:
662             switch (LOWORD(wParam))
663             {
664                 case IDOK:
665                     EndDialog(hWnd, TRUE);
666                     break;
667
668             }
669
670             return TRUE;
671     }
672
673     return FALSE;
674 }
675
676 /******************************************************************************
677  * Name       : SupportInfo
678  * Description: Displays the Support Information dialog
679  * Parameters : hWnd    - Handle of the main dialog
680  *              id      - ID of the application to display information for
681  */
682 static void SupportInfo(HWND hWnd, int id)
683 {
684     DialogBoxParamW(hInst, MAKEINTRESOURCEW(IDD_INFO), hWnd, SupportInfoDlgProc, id);
685 }
686
687 /* Definition of column headers for AddListViewColumns function */
688 typedef struct AppWizColumn {
689    int width;
690    int fmt;
691    int title;
692 } AppWizColumn;
693
694 static const AppWizColumn columns[] = {
695     {200, LVCFMT_LEFT, IDS_COLUMN_NAME},
696     {150, LVCFMT_LEFT, IDS_COLUMN_PUBLISHER},
697     {100, LVCFMT_LEFT, IDS_COLUMN_VERSION},
698 };
699
700 /******************************************************************************
701  * Name       : AddListViewColumns
702  * Description: Adds column headers to the list view control.
703  * Parameters : hWnd    - Handle of the list view control.
704  * Returns    : TRUE if completed successfully, FALSE otherwise.
705  */
706 static BOOL AddListViewColumns(HWND hWnd)
707 {
708     WCHAR buf[MAX_STRING_LEN];
709     LVCOLUMNW lvc;
710     UINT i;
711
712     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
713
714     /* Add the columns */
715     for (i = 0; i < sizeof(columns) / sizeof(columns[0]); i++)
716     {
717         lvc.iSubItem = i;
718         lvc.pszText = buf;
719
720         /* set width and format */
721         lvc.cx = columns[i].width;
722         lvc.fmt = columns[i].fmt;
723
724         LoadStringW(hInst, columns[i].title, buf, sizeof(buf) / sizeof(buf[0]));
725
726         if (ListView_InsertColumnW(hWnd, i, &lvc) == -1)
727             return FALSE;
728     }
729
730     return TRUE;
731 }
732
733 /******************************************************************************
734  * Name       : AddListViewImageList
735  * Description: Creates an ImageList for the list view control.
736  * Parameters : hWnd    - Handle of the list view control.
737  * Returns    : Handle of the image list.
738  */
739 static HIMAGELIST AddListViewImageList(HWND hWnd)
740 {
741     HIMAGELIST hSmall;
742     HICON hDefaultIcon;
743
744     hSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
745                               ILC_COLOR32 | ILC_MASK, 1, 1);
746
747     /* Add default icon to image list */
748     hDefaultIcon = LoadIconW(hInst, MAKEINTRESOURCEW(ICO_MAIN));
749     ImageList_AddIcon(hSmall, hDefaultIcon);
750     DestroyIcon(hDefaultIcon);
751
752     SendMessageW(hWnd, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)hSmall);
753
754     return hSmall;
755 }
756
757 /******************************************************************************
758  * Name       : ResetApplicationList
759  * Description: Empties the app list, if need be, and recreates it.
760  * Parameters : bFirstRun  - TRUE if this is the first time this is run, FALSE otherwise
761  *              hWnd       - handle of the dialog box
762  *              hImageList - handle of the image list
763  * Returns    : New handle of the image list.
764  */
765 static HIMAGELIST ResetApplicationList(BOOL bFirstRun, HWND hWnd, HIMAGELIST hImageList)
766 {
767     static const BOOL is_64bit = sizeof(void *) > sizeof(int);
768     HWND hWndListView;
769     HKEY hkey;
770
771     hWndListView = GetDlgItem(hWnd, IDL_PROGRAMS);
772
773     /* if first run, create the image list and add the listview columns */
774     if (bFirstRun)
775     {
776         if (!AddListViewColumns(hWndListView))
777             return NULL;
778     }
779     else /* we need to remove the existing things first */
780     {
781         RemoveItemsFromList(hWnd);
782         ImageList_Destroy(hImageList);
783
784         /* reset the list, since it's probably changed if the uninstallation was
785            successful */
786         EmptyList();
787     }
788
789     /* now create the image list and add the applications to the listview */
790     hImageList = AddListViewImageList(hWndListView);
791
792     if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ, &hkey))
793     {
794         ReadApplicationsFromRegistry(hkey);
795         RegCloseKey(hkey);
796     }
797     if (is_64bit &&
798         !RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ|KEY_WOW64_32KEY, &hkey))
799     {
800         ReadApplicationsFromRegistry(hkey);
801         RegCloseKey(hkey);
802     }
803     if (!RegOpenKeyExW(HKEY_CURRENT_USER, PathUninstallW, 0, KEY_READ, &hkey))
804     {
805         ReadApplicationsFromRegistry(hkey);
806         RegCloseKey(hkey);
807     }
808
809     AddApplicationsToList(hWndListView, hImageList);
810     UpdateButtons(hWnd);
811
812     return(hImageList);
813 }
814
815 /******************************************************************************
816  * Name       : MainDlgProc
817  * Description: Callback procedure for main tab
818  * Parameters : hWnd    - hWnd of the window
819  *              msg     - reason for calling function
820  *              wParam  - additional parameter
821  *              lParam  - additional parameter
822  * Returns    : Depends on the message
823  */
824 static INT_PTR CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
825 {
826     int selitem;
827     static HIMAGELIST hImageList;
828     LPNMHDR nmh;
829     LVITEMW lvItem;
830
831     switch(msg)
832     {
833         case WM_INITDIALOG:
834             hImageList = ResetApplicationList(TRUE, hWnd, hImageList);
835
836             if (!hImageList)
837                 return FALSE;
838
839             return TRUE;
840
841         case WM_DESTROY:
842             RemoveItemsFromList(hWnd);
843             ImageList_Destroy(hImageList);
844
845             EmptyList();
846
847             return 0;
848
849         case WM_NOTIFY:
850             nmh = (LPNMHDR) lParam;
851
852             switch (nmh->idFrom)
853             {
854                 case IDL_PROGRAMS:
855                     switch (nmh->code)
856                     {
857                         case LVN_ITEMCHANGED:
858                             UpdateButtons(hWnd);
859                             break;
860                     }
861                     break;
862             }
863
864             return TRUE;
865
866         case WM_COMMAND:
867             switch (LOWORD(wParam))
868             {
869                 case IDC_INSTALL:
870                     InstallProgram(hWnd);
871                     break;
872
873                 case IDC_ADDREMOVE:
874                 case IDC_MODIFY:
875                     selitem = SendDlgItemMessageW(hWnd, IDL_PROGRAMS,
876                         LVM_GETNEXTITEM, -1, LVNI_FOCUSED|LVNI_SELECTED);
877
878                     if (selitem != -1)
879                     {
880                         lvItem.iItem = selitem;
881                         lvItem.mask = LVIF_PARAM;
882
883                         if (SendDlgItemMessageW(hWnd, IDL_PROGRAMS, LVM_GETITEMW,
884                           0, (LPARAM) &lvItem))
885                             UninstallProgram(lvItem.lParam, LOWORD(wParam));
886                     }
887
888                     hImageList = ResetApplicationList(FALSE, hWnd, hImageList);
889
890                     break;
891
892                 case IDC_SUPPORT_INFO:
893                     selitem = SendDlgItemMessageW(hWnd, IDL_PROGRAMS,
894                         LVM_GETNEXTITEM, -1, LVNI_FOCUSED | LVNI_SELECTED);
895
896                     if (selitem != -1)
897                     {
898                         lvItem.iItem = selitem;
899                         lvItem.mask = LVIF_PARAM;
900
901                         if (SendDlgItemMessageW(hWnd, IDL_PROGRAMS, LVM_GETITEMW,
902                           0, (LPARAM) &lvItem))
903                             SupportInfo(hWnd, lvItem.lParam);
904                     }
905
906                     break;
907             }
908
909             return TRUE;
910     }
911
912     return FALSE;
913 }
914
915 static int CALLBACK propsheet_callback( HWND hwnd, UINT msg, LPARAM lparam )
916 {
917     switch (msg)
918     {
919     case PSCB_INITIALIZED:
920         SendMessageW( hwnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW( hInst, MAKEINTRESOURCEW(ICO_MAIN) ));
921         break;
922     }
923     return 0;
924 }
925
926 /******************************************************************************
927  * Name       : StartApplet
928  * Description: Main routine for applet
929  * Parameters : hWnd    - hWnd of the Control Panel
930  */
931 static void StartApplet(HWND hWnd)
932 {
933     PROPSHEETPAGEW psp;
934     PROPSHEETHEADERW psh;
935     WCHAR tab_title[MAX_STRING_LEN], app_title[MAX_STRING_LEN];
936
937     /* Load the strings we will use */
938     LoadStringW(hInst, IDS_TAB1_TITLE, tab_title, sizeof(tab_title) / sizeof(tab_title[0]));
939     LoadStringW(hInst, IDS_CPL_TITLE, app_title, sizeof(app_title) / sizeof(app_title[0]));
940     LoadStringW(hInst, IDS_REMOVE, btnRemove, sizeof(btnRemove) / sizeof(btnRemove[0]));
941     LoadStringW(hInst, IDS_MODIFY_REMOVE, btnModifyRemove, sizeof(btnModifyRemove) / sizeof(btnModifyRemove[0]));
942
943     /* Fill out the PROPSHEETPAGE */
944     psp.dwSize = sizeof (PROPSHEETPAGEW);
945     psp.dwFlags = PSP_USETITLE;
946     psp.hInstance = hInst;
947     psp.u.pszTemplate = MAKEINTRESOURCEW (IDD_MAIN);
948     psp.u2.pszIcon = NULL;
949     psp.pfnDlgProc = MainDlgProc;
950     psp.pszTitle = tab_title;
951     psp.lParam = 0;
952
953     /* Fill out the PROPSHEETHEADER */
954     psh.dwSize = sizeof (PROPSHEETHEADERW);
955     psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEICONID | PSH_USECALLBACK;
956     psh.hwndParent = hWnd;
957     psh.hInstance = hInst;
958     psh.u.pszIcon = MAKEINTRESOURCEW(ICO_MAIN);
959     psh.pszCaption = app_title;
960     psh.nPages = 1;
961     psh.u3.ppsp = &psp;
962     psh.pfnCallback = propsheet_callback;
963     psh.u2.nStartPage = 0;
964
965     /* Display the property sheet */
966     PropertySheetW (&psh);
967 }
968
969 static LONG start_params(const WCHAR *params)
970 {
971     static const WCHAR install_geckoW[] = {'i','n','s','t','a','l','l','_','g','e','c','k','o',0};
972     static const WCHAR install_monoW[] = {'i','n','s','t','a','l','l','_','m','o','n','o',0};
973
974     if(!params)
975         return FALSE;
976
977     if(!strcmpW(params, install_geckoW)) {
978         install_addon(ADDON_GECKO);
979         return TRUE;
980     }
981
982     if(!strcmpW(params, install_monoW)) {
983         install_addon(ADDON_MONO);
984         return TRUE;
985     }
986
987     WARN("unknown param %s\n", debugstr_w(params));
988     return FALSE;
989 }
990
991 /******************************************************************************
992  * Name       : CPlApplet
993  * Description: Entry point for Control Panel applets
994  * Parameters : hwndCPL - hWnd of the Control Panel
995  *              message - reason for calling function
996  *              lParam1 - additional parameter
997  *              lParam2 - additional parameter
998  * Returns    : Depends on the message
999  */
1000 LONG CALLBACK CPlApplet(HWND hwndCPL, UINT message, LPARAM lParam1, LPARAM lParam2)
1001 {
1002     INITCOMMONCONTROLSEX iccEx;
1003
1004     switch (message)
1005     {
1006         case CPL_INIT:
1007             iccEx.dwSize = sizeof(iccEx);
1008             iccEx.dwICC = ICC_LISTVIEW_CLASSES | ICC_TAB_CLASSES | ICC_LINK_CLASS;
1009
1010             InitCommonControlsEx(&iccEx);
1011
1012             return TRUE;
1013
1014         case CPL_GETCOUNT:
1015             return 1;
1016
1017         case CPL_STARTWPARMSW:
1018             return start_params((const WCHAR *)lParam2);
1019
1020         case CPL_INQUIRE:
1021         {
1022             CPLINFO *appletInfo = (CPLINFO *) lParam2;
1023
1024             appletInfo->idIcon = ICO_MAIN;
1025             appletInfo->idName = IDS_CPL_TITLE;
1026             appletInfo->idInfo = IDS_CPL_DESC;
1027             appletInfo->lData = 0;
1028
1029             break;
1030         }
1031
1032         case CPL_DBLCLK:
1033             StartApplet(hwndCPL);
1034             break;
1035     }
1036
1037     return FALSE;
1038 }