uninstaller: Disable Uninstall button when no items selected.
[wine] / programs / uninstaller / main.c
1 /*
2  * Uninstaller
3  *
4  * Copyright 2000 Andreas Mohr
5  * Copyright 2004 Hannu Valtonen
6  * Copyright 2005 Jonathan Ernst
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <windows.h>
27 #include <shlwapi.h>
28 #include "resource.h"
29 #include "regstr.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(uninstaller);
33
34 typedef struct {
35     HKEY  root;
36     WCHAR *key;
37     WCHAR *descr;
38     WCHAR *command;
39     int active;
40 } uninst_entry;
41 static uninst_entry *entries = NULL;
42 static unsigned int numentries = 0;
43 static int list_need_update = 1;
44 static int oldsel = -1;
45 static WCHAR *sFilter;
46 static WCHAR sAppName[MAX_STRING_LEN];
47 static WCHAR sAboutTitle[MAX_STRING_LEN];
48 static WCHAR sAbout[MAX_STRING_LEN];
49 static WCHAR sRegistryKeyNotAvailable[MAX_STRING_LEN];
50 static WCHAR sUninstallFailed[MAX_STRING_LEN];
51
52 static int FetchUninstallInformation(void);
53 static void UninstallProgram(void);
54 static void UpdateList(HWND hList);
55 static int cmp_by_name(const void *a, const void *b);
56 static INT_PTR CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
57
58
59 static const WCHAR BackSlashW[] = { '\\', 0 };
60 static const WCHAR DisplayNameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
61 static const WCHAR PathUninstallW[] = {
62         'S','o','f','t','w','a','r','e','\\',
63         'M','i','c','r','o','s','o','f','t','\\',
64         'W','i','n','d','o','w','s','\\',
65         'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
66         'U','n','i','n','s','t','a','l','l',0 };
67 static const WCHAR UninstallCommandlineW[] = {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
68
69
70 /**
71  * Used to output program list when used with --list
72  */
73 static void ListUninstallPrograms(void)
74 {
75     unsigned int i;
76     int lenDescr, lenKey;
77     char *descr;
78     char *key;
79
80     FetchUninstallInformation();
81
82     for (i=0; i < numentries; i++)
83     {
84         lenDescr = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, NULL, 0, NULL, NULL); 
85         lenKey = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, NULL, 0, NULL, NULL); 
86         descr = HeapAlloc(GetProcessHeap(), 0, lenDescr);
87         key = HeapAlloc(GetProcessHeap(), 0, lenKey);
88         WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, descr, lenDescr, NULL, NULL);
89         WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, key, lenKey, NULL, NULL);
90         printf("%s|||%s\n", key, descr);
91         HeapFree(GetProcessHeap(), 0, descr);
92         HeapFree(GetProcessHeap(), 0, key);
93     }
94 }
95
96
97 static void RemoveSpecificProgram(WCHAR *nameW)
98 {
99     unsigned int i;
100     int lenName;
101     char *name;
102
103     FetchUninstallInformation();
104
105     for (i=0; i < numentries; i++)
106     {
107         if (lstrcmpW(entries[i].key, nameW) == 0)
108         {
109             entries[i].active++;
110             break;
111         }
112     }
113
114     if (i < numentries)
115         UninstallProgram();
116     else
117     {
118         lenName = WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, NULL, 0, NULL, NULL); 
119         name = HeapAlloc(GetProcessHeap(), 0, lenName);
120         WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, name, lenName, NULL, NULL);
121         fprintf(stderr, "Error: could not match application [%s]\n", name);
122         HeapFree(GetProcessHeap(), 0, name);
123     }
124 }
125
126
127 int wmain(int argc, WCHAR *argv[])
128 {
129     LPCWSTR token = NULL;
130     HINSTANCE hInst = GetModuleHandleW(0);
131     static const WCHAR listW[] = { '-','-','l','i','s','t',0 };
132     static const WCHAR removeW[] = { '-','-','r','e','m','o','v','e',0 };
133     int i = 1;
134
135     while( i<argc )
136     {
137         token = argv[i++];
138         
139         /* Handle requests just to list the applications */
140         if( !lstrcmpW( token, listW ) )
141         {
142             ListUninstallPrograms();
143             return 0;
144         }
145         else if( !lstrcmpW( token, removeW ) )
146         {
147             if( i >= argc )
148             {
149                 WINE_ERR( "The remove option requires a parameter.\n");
150                 return 1;
151             }
152
153             RemoveSpecificProgram( argv[i++] );
154             return 0;
155         }
156         else 
157         {
158             WINE_ERR( "unknown option %s\n",wine_dbgstr_w(token));
159             return 1;
160         }
161     }
162
163     /* Load MessageBox's strings */
164     LoadStringW(hInst, IDS_APPNAME, sAppName, sizeof(sAppName)/sizeof(WCHAR));
165     LoadStringW(hInst, IDS_ABOUTTITLE, sAboutTitle, sizeof(sAboutTitle)/sizeof(WCHAR));
166     LoadStringW(hInst, IDS_ABOUT, sAbout, sizeof(sAbout)/sizeof(WCHAR));
167     LoadStringW(hInst, IDS_REGISTRYKEYNOTAVAILABLE, sRegistryKeyNotAvailable, sizeof(sRegistryKeyNotAvailable)/sizeof(WCHAR));
168     LoadStringW(hInst, IDS_UNINSTALLFAILED, sUninstallFailed, sizeof(sUninstallFailed)/sizeof(WCHAR));
169
170     return DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_UNINSTALLER), NULL, DlgProc);
171 }
172
173
174 /**
175  * Used to sort entries by name.
176  */
177 static int cmp_by_name(const void *a, const void *b)
178 {
179     return lstrcmpiW(((const uninst_entry *)a)->descr, ((const uninst_entry *)b)->descr);
180 }
181
182
183 /**
184  * Fetch information from the uninstall key.
185  */
186 static int FetchFromRootKey(HKEY root)
187 {
188     HKEY hkeyUninst, hkeyApp;
189     int i;
190     DWORD sizeOfSubKeyName, displen, uninstlen;
191     WCHAR subKeyName[256];
192     WCHAR key_app[1024];
193     WCHAR *p;
194
195     if (RegOpenKeyExW(root, PathUninstallW, 0, KEY_READ, &hkeyUninst) != ERROR_SUCCESS)
196         return 0;
197
198     lstrcpyW(key_app, PathUninstallW);
199     lstrcatW(key_app, BackSlashW);
200     p = key_app+lstrlenW(PathUninstallW)+1;
201
202     sizeOfSubKeyName = 255;
203     for (i=0; RegEnumKeyExW( hkeyUninst, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS; ++i)
204     {
205         lstrcpyW(p, subKeyName);
206         RegOpenKeyExW(root, key_app, 0, KEY_READ, &hkeyApp);
207         if ((RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, NULL, &displen) == ERROR_SUCCESS)
208          && (RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, NULL, &uninstlen) == ERROR_SUCCESS))
209         {
210             numentries++;
211             entries = HeapReAlloc(GetProcessHeap(), 0, entries, numentries*sizeof(uninst_entry));
212             entries[numentries-1].root = root;
213             entries[numentries-1].key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName)+1)*sizeof(WCHAR));
214             lstrcpyW(entries[numentries-1].key, subKeyName);
215             entries[numentries-1].descr = HeapAlloc(GetProcessHeap(), 0, displen);
216             RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, (LPBYTE)entries[numentries-1].descr, &displen);
217             entries[numentries-1].command = HeapAlloc(GetProcessHeap(), 0, uninstlen);
218             entries[numentries-1].active = 0;
219             RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, (LPBYTE)entries[numentries-1].command, &uninstlen);
220             WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
221             numentries, wine_dbgstr_w(entries[numentries-1].key), wine_dbgstr_w(entries[numentries-1].descr), wine_dbgstr_w(entries[numentries-1].command));
222             if(sFilter != NULL && StrStrIW(entries[numentries-1].descr,sFilter)==NULL)
223                 numentries--;
224         }
225         RegCloseKey(hkeyApp);
226         sizeOfSubKeyName = 255;
227     }
228     RegCloseKey(hkeyUninst);
229     return 1;
230
231 }
232
233 static int FetchUninstallInformation(void)
234 {
235     int rc;
236
237     numentries = 0;
238     oldsel = -1;
239     if (!entries)
240         entries = HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry));
241
242     rc = FetchFromRootKey(HKEY_LOCAL_MACHINE);
243     rc |= FetchFromRootKey(HKEY_CURRENT_USER);
244
245     qsort(entries, numentries, sizeof(uninst_entry), cmp_by_name);
246     return rc;
247 }
248
249 static void UninstallProgram(void)
250 {
251     unsigned int i;
252     WCHAR errormsg[1024];
253     BOOL res;
254     STARTUPINFOW si;
255     PROCESS_INFORMATION info;
256     DWORD exit_code;
257     HKEY hkey;
258     for (i=0; i < numentries; i++)
259     {
260         if (!(entries[i].active)) /* don't uninstall this one */
261             continue;
262         WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries[i].descr));
263         memset(&si, 0, sizeof(STARTUPINFOW));
264         si.cb = sizeof(STARTUPINFOW);
265         si.wShowWindow = SW_NORMAL;
266         res = CreateProcessW(NULL, entries[i].command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
267         if (res)
268         {   /* wait for the process to exit */
269             WaitForSingleObject(info.hProcess, INFINITE);
270             res = GetExitCodeProcess(info.hProcess, &exit_code);
271             WINE_TRACE("%d: %08x\n", res, exit_code);
272         }
273         else
274         {
275             wsprintfW(errormsg, sUninstallFailed, entries[i].command);
276             if(MessageBoxW(0, errormsg, sAppName, MB_YESNO | MB_ICONQUESTION)==IDYES)
277             {
278                 /* delete the application's uninstall entry */
279                 RegOpenKeyExW(entries[i].root, PathUninstallW, 0, KEY_READ, &hkey);
280                 RegDeleteKeyW(hkey, entries[i].key);
281                 RegCloseKey(hkey);
282             }
283         }
284     }
285     WINE_TRACE("finished uninstall phase.\n");
286     list_need_update = 1;
287 }
288
289 static void UpdateButtons(HWND hDlg, HWND hList)
290 {
291     EnableWindow(GetDlgItem(hDlg, IDC_UNINSTALL), SendMessageW(hList, LB_GETSELCOUNT, 0, 0) > 0);
292 }
293
294 static INT_PTR CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
295 {
296     TEXTMETRICW tm;
297     HDC hdc;
298     HWND hList = GetDlgItem(hwnd, IDC_LIST);
299     switch(Message)
300     {
301         case WM_INITDIALOG:
302             hdc = GetDC(hwnd);
303             GetTextMetricsW(hdc, &tm);
304             UpdateList(hList);
305             ReleaseDC(hwnd, hdc);
306             UpdateButtons(hwnd, hList);
307             break;
308         case WM_COMMAND:
309             switch(LOWORD(wParam))
310             {
311                 case IDC_FILTER:
312                 {
313                     if (HIWORD(wParam) == EN_CHANGE)
314                     {
315                         int len = GetWindowTextLengthW(GetDlgItem(hwnd, IDC_FILTER));
316                         list_need_update = 1;
317                         if(len > 0)
318                         {
319                             sFilter = (WCHAR*)GlobalAlloc(GPTR, (len + 1)*sizeof(WCHAR));
320                             GetDlgItemTextW(hwnd, IDC_FILTER, sFilter, len + 1);
321                         }
322                         else sFilter = NULL;
323                         UpdateList(hList);
324                         UpdateButtons(hwnd, hList);
325                     }
326                     break;
327                 }
328                 case IDC_UNINSTALL:
329                 {
330                     int count = SendMessageW(hList, LB_GETSELCOUNT, 0, 0);
331                     if(count != 0)
332                     {
333                         UninstallProgram();
334                         UpdateList(hList);
335                         UpdateButtons(hwnd, hList);
336                     }
337                     break;
338                 }
339                 case IDC_LIST:
340                     if (HIWORD(wParam) == LBN_SELCHANGE)
341                     {
342                        int sel = SendMessageW(hList, LB_GETCURSEL, 0, 0);
343                        if (oldsel != -1)
344                        {
345                            entries[oldsel].active ^= 1; /* toggle */
346                            WINE_TRACE("toggling %d old %s\n", entries[oldsel].active,
347                            wine_dbgstr_w(entries[oldsel].descr));
348                        }
349                        entries[sel].active ^= 1; /* toggle */
350                        WINE_TRACE("toggling %d %s\n", entries[sel].active,
351                        wine_dbgstr_w(entries[sel].descr));
352                        oldsel = sel;
353                    }
354                     UpdateButtons(hwnd, hList);
355                     break;
356                 case IDC_ABOUT:
357                     MessageBoxW(0, sAbout, sAboutTitle, MB_OK);
358                     break;
359                 case IDCANCEL:
360                 case IDC_EXIT:
361                     EndDialog(hwnd, 0);
362                     break;
363             }
364             break;
365         default:
366             return FALSE;
367     }
368     return TRUE;
369 }
370
371
372 static void UpdateList(HWND hList)
373 {
374     unsigned int i;
375     if (list_need_update)
376     {
377         int prevsel;
378         prevsel = SendMessageW(hList, LB_GETCURSEL, 0, 0);
379         if (!(FetchUninstallInformation()))
380         {
381             MessageBoxW(0, sRegistryKeyNotAvailable, sAppName, MB_OK);
382             PostQuitMessage(0);
383             return;
384         }
385         SendMessageW(hList, LB_RESETCONTENT, 0, 0);
386         SendMessageW(hList, WM_SETREDRAW, FALSE, 0);
387         for (i=0; i < numentries; i++)
388         {
389             WINE_TRACE("adding %s\n", wine_dbgstr_w(entries[i].descr));
390             SendMessageW(hList, LB_ADDSTRING, 0, (LPARAM)entries[i].descr);
391         }
392         WINE_TRACE("setting prevsel %d\n", prevsel);
393         if (prevsel != -1)
394             SendMessageW(hList, LB_SETCURSEL, prevsel, 0 );
395         SendMessageW(hList, WM_SETREDRAW, TRUE, 0);
396         list_need_update = 0;
397     }
398 }