Build idl files as part of the normal build process.
[wine/testsucceed.git] / programs / uninstaller / main.c
blob74e6f05956b4389ee91daa0e32b2b008ede26b03
1 /*
2 * Q&D Uninstaller (main.c)
4 * Copyright 2000 Andreas Mohr <andi@lisas.de>
5 * Copyright 2004 Hannu Valtonen <Hannu.Valtonen@hut.fi>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * ToDo:
22 * - add search box for locating entries quickly
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <time.h>
30 #include <windows.h>
31 #include "main.h"
32 #include "regstr.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(uninstaller);
37 /* Work around a Wine bug which defines handles as UINT rather than LPVOID */
38 #ifdef WINE_STRICT
39 #define NULL_HANDLE NULL
40 #else
41 #define NULL_HANDLE 0
42 #endif
44 /* use multi-select listbox */
45 #undef USE_MULTIPLESEL
47 /* Delete uninstall registry key after execution.
48 * This is probably a bad idea, because it's the
49 * uninstall program that is supposed to do that.
51 #undef DEL_REG_KEY
53 char appname[18];
55 static char about_string[] =
56 "Wine Application Uninstaller (C) 2004 by Andreas Mohr <andi@lisas.de> and Hannu Valtonen <Hannu.Valtonen@hut.fi>";
57 static char program_description[] =
58 "Please select the application you wish to uninstall:";
60 typedef struct {
61 char *key;
62 WCHAR *descr;
63 char *command;
64 int active;
65 } uninst_entry;
67 uninst_entry *entries = NULL;
69 int numentries = 0;
70 int list_need_update = 1;
71 int oldsel = -1;
73 struct {
74 DWORD style;
75 LPCSTR text;
76 HWND hwnd;
77 } button[] =
79 { BS_PUSHBUTTON, "Add/Remove", 0 },
80 { BS_PUSHBUTTON, "About", 0 },
81 { BS_PUSHBUTTON, "Exit", 0 }
84 #define NUM (sizeof(button)/sizeof(button[0]))
86 int FetchUninstallInformation(void);
87 void UninstallProgram(void);
89 void ListUninstallPrograms(void)
91 int i, len;
92 char *descr;
94 if (! FetchUninstallInformation())
95 return;
97 for (i=0; i < numentries; i++)
99 len = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, NULL, 0, NULL, NULL);
100 descr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
101 WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, descr, len, NULL, NULL);
102 printf("%s|||%s\n", entries[i].key, descr);
103 HeapFree(GetProcessHeap(), 0, descr);
108 void RemoveSpecificProgram(char *name)
110 int i;
112 if (! FetchUninstallInformation())
113 return;
115 for (i=0; i < numentries; i++)
117 if (strcmp(entries[i].key, name) == 0)
119 entries[i].active++;
120 break;
124 if (i < numentries)
125 UninstallProgram();
126 else
128 fprintf(stderr, "Error: could not match application [%s]\n", name);
132 int main( int argc, char *argv[])
134 MSG msg;
135 WNDCLASS wc;
136 HWND hWnd;
137 LPSTR token = NULL;
138 int i = 1;
139 HINSTANCE hInst = NULL;
141 while( i<argc )
143 token = argv[i++];
145 /* Handle requests just to list the applications */
146 if( !lstrcmpA( token, "--list" ) )
148 ListUninstallPrograms();
149 return 0;
151 else if( !lstrcmpA( token, "--remove" ) )
153 if( i >= argc )
155 WINE_ERR( "The remove option requires a parameter.\n");
156 return 1;
159 RemoveSpecificProgram( argv[i++] );
160 return 0;
162 else
164 WINE_ERR( "unknown option %s\n",token);
165 return 1;
169 LoadString( hInst, IDS_APPNAME, appname, sizeof(appname));
171 wc.style = 0;
172 wc.lpfnWndProc = MainProc;
173 wc.cbClsExtra = 0;
174 wc.cbWndExtra = 0;
175 wc.hInstance = hInst;
176 wc.hIcon = LoadIcon( hInst, appname );
177 wc.hCursor = LoadCursor( NULL_HANDLE, IDI_APPLICATION );
178 wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
179 wc.lpszMenuName = NULL;
180 wc.lpszClassName = appname;
182 if (!RegisterClass(&wc)) exit(1);
183 hWnd = CreateWindow( appname, "Wine Application Uninstaller",
184 WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
185 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
186 NULL_HANDLE, NULL_HANDLE, hInst, NULL );
188 if (!hWnd) exit(1);
190 ShowWindow( hWnd, SW_SHOW );
191 UpdateWindow( hWnd );
193 while( GetMessage(&msg, NULL_HANDLE, 0, 0) ) {
194 TranslateMessage( &msg );
195 DispatchMessage( &msg );
197 return msg.wParam;
200 int cmp_by_name(const void *a, const void *b)
202 return lstrcmpiW(((uninst_entry *)a)->descr, ((uninst_entry *)b)->descr);
205 int FetchUninstallInformation(void)
207 HKEY hkeyUninst, hkeyApp;
208 int i;
209 DWORD sizeOfSubKeyName, displen, uninstlen;
210 char subKeyName[256];
211 char key_app[1024];
212 char *p;
214 numentries = 0;
215 oldsel = -1;
216 if ( RegOpenKeyExA(HKEY_LOCAL_MACHINE, REGSTR_PATH_UNINSTALL,
217 0, KEY_READ, &hkeyUninst) != ERROR_SUCCESS )
219 MessageBox(0, "Uninstall registry key not available (yet), nothing to do !", appname, MB_OK);
220 return 0;
223 if (!entries)
224 entries = HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry));
226 strcpy(key_app, REGSTR_PATH_UNINSTALL);
227 strcat(key_app, "\\");
228 p = key_app+strlen(REGSTR_PATH_UNINSTALL)+1;
230 sizeOfSubKeyName = 255;
231 for ( i=0;
232 RegEnumKeyExA( hkeyUninst, i, subKeyName, &sizeOfSubKeyName,
233 NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS;
234 ++i )
236 static const WCHAR DisplayNameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
238 strcpy(p, subKeyName);
239 RegOpenKeyExA(HKEY_LOCAL_MACHINE, key_app, 0, KEY_READ, &hkeyApp);
241 if ( (RegQueryValueExW(hkeyApp, DisplayNameW,
242 0, 0, NULL, &displen) == ERROR_SUCCESS)
243 && (RegQueryValueExA(hkeyApp, REGSTR_VAL_UNINSTALLER_COMMANDLINE,
244 0, 0, NULL, &uninstlen) == ERROR_SUCCESS) )
246 numentries++;
247 entries = HeapReAlloc(GetProcessHeap(), 0, entries, numentries*sizeof(uninst_entry));
248 entries[numentries-1].key =
249 HeapAlloc(GetProcessHeap(), 0, strlen(subKeyName)+1);
250 strcpy(entries[numentries-1].key, subKeyName);
251 entries[numentries-1].descr =
252 HeapAlloc(GetProcessHeap(), 0, displen);
253 RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0,
254 (LPBYTE)entries[numentries-1].descr, &displen);
255 entries[numentries-1].command =
256 HeapAlloc(GetProcessHeap(), 0, uninstlen);
257 entries[numentries-1].active = 0;
258 RegQueryValueExA(hkeyApp, REGSTR_VAL_UNINSTALLER_COMMANDLINE, 0, 0,
259 entries[numentries-1].command, &uninstlen);
260 WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
261 numentries, entries[numentries-1].key,
262 wine_dbgstr_w(entries[numentries-1].descr),
263 entries[numentries-1].command);
265 RegCloseKey(hkeyApp);
267 sizeOfSubKeyName = 255;
269 qsort(entries, numentries, sizeof(uninst_entry), cmp_by_name);
270 RegCloseKey(hkeyUninst);
271 return 1;
274 void UninstallProgram(void)
276 int i;
277 char errormsg[1024];
278 BOOL res;
279 STARTUPINFO si;
280 PROCESS_INFORMATION info;
281 DWORD exit_code;
282 #ifdef DEL_REG_KEY
283 HKEY hkey;
284 #endif
286 for (i=0; i < numentries; i++)
288 if (!(entries[i].active)) /* don't uninstall this one */
289 continue;
290 WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries[i].descr));
291 memset(&si, 0, sizeof(STARTUPINFO));
292 si.cb = sizeof(STARTUPINFO);
293 si.wShowWindow = SW_NORMAL;
294 res = CreateProcess(NULL, entries[i].command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
295 if (res == TRUE)
296 { /* wait for the process to exit */
297 WaitForSingleObject(info.hProcess, INFINITE);
298 res = GetExitCodeProcess(info.hProcess, &exit_code);
299 WINE_TRACE("%d: %08lx\n", res, exit_code);
300 #ifdef DEL_REG_KEY
301 /* delete the application's uninstall entry */
302 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGSTR_PATH_UNINSTALL,
303 0, KEY_READ, &hkey) == ERROR_SUCCESS)
305 RegDeleteKey(hkey, entries[i].key);
306 RegCloseKey(hkey);
308 #endif
310 else
312 sprintf(errormsg, "Execution of uninstall command '%s' failed, perhaps due to missing executable.", entries[i].command);
313 MessageBox(0, errormsg, appname, MB_OK);
316 WINE_TRACE("finished uninstall phase.\n");
317 list_need_update = 1;
320 LRESULT WINAPI MainProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
322 HFONT static_font, listbox_font;
323 HDC hdc;
324 PAINTSTRUCT ps;
325 TEXTMETRIC tm;
326 int cxChar, cyChar, i, y, bx, maxx, maxy, wx, wy;
327 static HWND hwndList = 0, static_text = 0;
328 DWORD style;
329 RECT rect;
331 switch( msg ) {
332 case WM_CREATE:
334 hdc = GetDC(hWnd);
335 GetTextMetrics(hdc, &tm);
336 static_font = CreateFont(tm.tmHeight + tm.tmExternalLeading, 0, 0, 0, 600, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
337 listbox_font = CreateFont(tm.tmHeight + tm.tmExternalLeading, 0, 0, 0, 0, TRUE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
338 cxChar = tm.tmAveCharWidth;
339 cyChar = tm.tmHeight + tm.tmExternalLeading;
340 ReleaseDC(hWnd, hdc);
341 /* FIXME: implement sorting and use LBS_SORT here ! */
342 style = (WS_CHILD|WS_VISIBLE|LBS_STANDARD) & ~LBS_SORT;
343 #ifdef USE_MULTIPLESEL
344 style |= LBS_MULTIPLESEL;
345 #endif
346 bx = maxx = cxChar * 3;
347 y = maxy = cyChar * 1;
348 static_text = CreateWindow("static", program_description,
349 WS_CHILD|WS_VISIBLE|SS_LEFT,
350 maxx, maxy,
351 cxChar * sizeof(program_description), cyChar * 1,
352 hWnd, (HMENU)1,
353 ((LPCREATESTRUCT)lParam)->hInstance, NULL);
354 SendMessage(static_text, WM_SETFONT, (WPARAM)static_font, MAKELPARAM(FALSE, 0));
355 maxy += cyChar * 2; /*static text + distance */
356 hwndList = CreateWindow("listbox", NULL,
357 style,
358 maxx, maxy,
359 cxChar * 50 + GetSystemMetrics(SM_CXVSCROLL), cyChar * 10,
360 hWnd, (HMENU) 1,
361 (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
362 SendMessage(hwndList, WM_SETFONT, (WPARAM)listbox_font, MAKELPARAM(FALSE, 0));
363 GetWindowRect(hwndList, &rect);
364 maxx += (rect.right - rect.left)*1.1;
365 maxy += (rect.bottom - rect.top)*1.1;
366 wx = 20*cxChar;
367 wy = 7*cyChar/4;
368 y = cyChar * 3;
369 for (i=0; i < NUM; i++)
371 button[i].hwnd = CreateWindow("button", button[i].text,
372 WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
373 maxx, y,
374 wx, wy,
375 hWnd, (HMENU)i,
376 ((LPCREATESTRUCT)lParam)->hInstance, NULL);
377 if (!button[i].hwnd)
378 PostQuitMessage(0);
379 y += 2*cyChar;
381 maxx += wx + cxChar * 4;
382 maxy += cyChar * 2; /* window border */
383 SetWindowPos( hWnd, 0,
384 0, 0, maxx, maxy,
385 SWP_NOMOVE);
386 return 0;
389 case WM_PAINT:
391 hdc = BeginPaint( hWnd, &ps );
392 if (list_need_update)
394 int prevsel;
395 prevsel = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
396 if (!(FetchUninstallInformation()))
398 PostQuitMessage(0);
399 return 0;
401 SendMessage(hwndList, LB_RESETCONTENT, 0, 0);
402 SendMessage(hwndList, WM_SETREDRAW, FALSE, 0);
403 for (i=0; i < numentries; i++)
405 WINE_TRACE("adding %s\n", wine_dbgstr_w(entries[i].descr));
406 SendMessageW(hwndList, LB_ADDSTRING, 0, (LPARAM)entries[i].descr);
408 WINE_TRACE("setting prevsel %d\n", prevsel);
409 if (prevsel != -1)
410 SendMessage(hwndList, LB_SETCURSEL, prevsel, 0 );
411 SendMessage(hwndList, WM_SETREDRAW, TRUE, 0);
412 list_need_update = 0;
414 EndPaint( hWnd, &ps );
415 return 0;
418 case WM_DESTROY:
419 PostQuitMessage( 0 );
420 return 0;
422 case WM_COMMAND:
423 if ((HWND)lParam == hwndList)
425 if (HIWORD(wParam) == LBN_SELCHANGE)
427 int sel = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
429 #ifndef USE_MULTIPLESEL
430 if (oldsel != -1)
432 entries[oldsel].active ^= 1; /* toggle */
433 WINE_TRACE("toggling %d old %s\n", entries[oldsel].active,
434 wine_dbgstr_w(entries[oldsel].descr));
436 #endif
437 entries[sel].active ^= 1; /* toggle */
438 WINE_TRACE("toggling %d %s\n", entries[sel].active,
439 wine_dbgstr_w(entries[oldsel].descr));
440 oldsel = sel;
443 else
444 if ((HWND)lParam == button[0].hwnd) /* Uninstall button */
446 UninstallProgram();
448 InvalidateRect(hWnd, NULL, TRUE);
449 UpdateWindow(hWnd);
452 else
453 if ((HWND)lParam == button[1].hwnd) /* About button */
454 MessageBox(0, about_string, "About Wine Application Uninstaller", MB_OK);
455 else
456 if ((HWND)lParam == button[2].hwnd) /* Exit button */
457 PostQuitMessage(0);
458 return 0;
461 return( DefWindowProc( hWnd, msg, wParam, lParam ));