small cosmetic improvements
[swftools.git] / installer / installer.c
index 439fc2f..d22efd5 100644 (file)
@@ -44,10 +44,13 @@ static char path_programfiles[MAX_PATH] = "\0";
 static char pathBuf[MAX_PATH];
 static int do_abort = 0;
 
+static char* pdf2swf_dir;
 static char* pdf2swf_path;
 
 static char registry_path[1024];
 
+static char elevated = 0;
+
 static char*install_path = "c:\\swftools\\";
 #define SOFTWARE_DOMAIN "quiss.org"
 #define SOFTWARE_NAME "SWFTools"
@@ -254,6 +257,21 @@ static char* getRegistryEntry(char*path)
     }
 }
 
+static int has_full_access = 0;
+static char hasFullAccess()
+{
+    /* find out whether we can write keys in HKEY_LOCAL_MACHINE */
+    HKEY hKey;
+    int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", 0,
+                           KEY_CREATE_SUB_KEY, &hKey);
+    if(!ret) {
+        RegCloseKey(hKey);
+        return 1;
+    } else {
+        return 0;
+    }
+}
+
 void processMessages()
 {
     MSG msg;
@@ -360,7 +378,6 @@ BOOL CALLBACK PropertySheetFunc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM
     if(message == WM_INITDIALOG) {
        SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path);
 
-        config_forAllUsers = 0;
         SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
         SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
     }
@@ -408,6 +425,23 @@ BOOL CALLBACK PropertySheetFunc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM
            return 0;
        }
     }
+    if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_SETACTIVE)) {
+        if(!elevated && !has_full_access) {
+            OSVERSIONINFO winverinfo;
+            memset(&winverinfo, 0, sizeof(OSVERSIONINFO));
+            winverinfo.dwOSVersionInfoSize = sizeof(winverinfo);
+            if (GetVersionEx(&winverinfo) && winverinfo.dwMajorVersion >= 5) {
+                /* we're on Vista, were asked to install for all users, but don't have
+                   priviledges to do so. Ask to spawn the process elevated. */
+                char exename[MAX_PATH];
+                GetModuleFileName(NULL, exename, sizeof(exename));
+                if((int)ShellExecute(0, "runas", exename, "elevated", NULL, SW_SHOWNORMAL)>32) {
+                    /* that worked- the second process will do the work */
+                    exit(0);
+                }
+            }
+        }
+    }
     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
 }
 HWND statuswnd;
@@ -483,7 +517,8 @@ BOOL CALLBACK PropertySheetFunc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM
            print_space(buf, "Space available: ", available.QuadPart);
        } else {
            sprintf(buf, "Space available: [Error %d]", GetLastError());
-           if(GetLastError() == ERROR_FILE_NOT_FOUND && install_path[0] && install_path[1]==':') {
+           if((GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_PATH_NOT_FOUND)
+                && install_path[0] && install_path[1]==':') {
                /* installation directory does not yet exist */
                char path[3]={'c',':',0};
                path[0] = install_path[0];
@@ -499,7 +534,7 @@ BOOL CALLBACK PropertySheetFunc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM
        SetDlgItemText(hwnd, IDC_SPACE2, "");
        PropSheet_SetWizButtons(dialog, 0);
        SendMessage(dialog, PSM_CANCELTOCLOSE, 0, 0); //makes wine display a warning
-       SetDlgItemText(hwnd, IDC_TITLE, "Installing files...");
+       SetDlgItemText(hwnd, IDC_TITLE, "Installing...");
        statuswnd = hwnd;
        status_t status;
        status.status = PropertyArchiveStatus;
@@ -523,16 +558,21 @@ static HRESULT (WINAPI *f_SHGetSpecialFolderPath)(HWND hwnd, LPTSTR lpszPath, in
 
 BOOL CALLBACK PropertySheetFunc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
     if(message == WM_INITDIALOG) {
-       pdf2swf_path = concatPaths(install_path, "pdf2swf_gui.exe");
+       pdf2swf_dir = install_path; //concatPaths(install_path, "gpdf2swf");
+       pdf2swf_path = concatPaths(pdf2swf_dir, "gpdf2swf.exe");
        FILE*fi = fopen(pdf2swf_path, "rb");
        if(fi) {
            printf("a GUI program exists, creating desktop/startmenu links\n");
            config_createLinks = 1;
            fclose(fi);
+       } else {
+           config_createLinks = 0;
+           config_createStartmenu = 0;
+           config_createDesktop = 0;
        }
        if(!config_createLinks) {
-           SendDlgItemMessage(hwnd, IDC_STARTMENU, SW_HIDE, 0, 0);
-           SendDlgItemMessage(hwnd, IDC_DESKTOP, SW_HIDE, 0, 0);
+           SendDlgItemMessage(hwnd, IDC_STARTMENU, BN_DISABLE, 0, 0);
+           SendDlgItemMessage(hwnd, IDC_DESKTOP, BN_DISABLE, 0, 0);
        }
 
        SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
@@ -541,13 +581,17 @@ BOOL CALLBACK PropertySheetFunc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM
     if(message == WM_COMMAND) {
        if((wParam&0xffff) == IDC_STARTMENU) {
            config_createStartmenu = SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_GETCHECK, 0, 0);
-           config_createStartmenu^=1;
+           if(config_createLinks) {
+               config_createStartmenu^=1;
+           }
            SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
            return 0;
        }
        if((wParam&0xffff) == IDC_DESKTOP) {
            config_createDesktop = SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_GETCHECK, 0, 0);
-           config_createDesktop^=1;
+           if(config_createLinks) {
+               config_createDesktop^=1;
+           }
            SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createDesktop, 0);
            return 0;
        }
@@ -581,7 +625,7 @@ BOOL CALLBACK PropertySheetFunc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM
            if(config_createDesktop && path_desktop[0]) {
                char* linkName = concatPaths(path_desktop, "pdf2swf.lnk");
                 printf("Creating desktop link %s -> %s\n", linkName, pdf2swf_path);
-               if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
+               if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, pdf2swf_dir)) {
                    MessageBox(0, "Couldn't create desktop shortcut", INSTALLER_NAME, MB_OK);
                    return 1;
                }
@@ -589,8 +633,9 @@ BOOL CALLBACK PropertySheetFunc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM
            if(config_createStartmenu && path_startmenu[0]) {
                char* group = concatPaths(path_startmenu, "pdf2swf");
                CreateDirectory(group, 0);
+                addDir(group);
                char* linkName = concatPaths(group, "pdf2swf.lnk");
-               if(!CreateShortcut(pdf2swf_path, "pdf2swf", concatPaths(group, "pdf2swf.lnk"), 0, 0, 0, install_path) ||
+               if(!CreateShortcut(pdf2swf_path, "pdf2swf", concatPaths(group, "pdf2swf.lnk"), 0, 0, 0, pdf2swf_dir) ||
                   !CreateShortcut(uninstall_path, "uninstall", concatPaths(group, "uninstall.lnk"), 0, 0, 0, install_path)) {
                    MessageBox(0, "Couldn't create start menu entry", INSTALLER_NAME, MB_OK);
                    return 1;
@@ -623,9 +668,9 @@ void findfiles(char*path, int*pos, char*data, int len, char del)
        char*f = concatPaths(path, findFileData.cFileName);
        if(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            findfiles(f, pos, data, len, del);
-           if(del) {
-               RemoveDirectory(f);
-           }
+            /* always try to remove directories- if they are empty, this
+               will work, and they won't prevent superdirectory deletion later */
+            RemoveDirectory(f);
        } else {
            int l = strlen(f);
 
@@ -662,7 +707,7 @@ static char*extrafiles = 0;
 BOOL CALLBACK PropertySheetFunc5(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
     HWND dialog = GetParent(hwnd);
     if(message == WM_INITDIALOG) {
-       SetDlgItemText(hwnd, IDC_INFO, "Ready to deinstall");
+       SetDlgItemText(hwnd, IDC_INFO, "Ready to uninstall");
     }
     if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZNEXT)) {
 
@@ -768,7 +813,22 @@ void runPropertySheet(HWND parent)
        {PropertySheetFunc7, IDD_DEINSTALLED},
 #endif
     };
+
     int num = sizeof(wpage)/sizeof(wpage[0]);
+
+#ifndef DEINSTALL
+    if(elevated) {
+        /* remove license.
+           TODO: remove installdir querying, too (pass installdir
+                 to second process) */
+        int t;
+        for(t=1;t<num;t++) {
+            wpage[t-1] = wpage[t];
+        }
+        num --;
+    }
+#endif
+
     HPROPSHEETPAGE pages[num];
     int t;
     for(t=0;t<num;t++) {
@@ -797,17 +857,29 @@ void runPropertySheet(HWND parent)
 static void remove_self()
 {
     char exename[MAX_PATH];
-    char batname[MAX_PATH];
+    char batdir[MAX_PATH];
+    char batfile[MAX_PATH];
+    char*batname;
     FILE *fp;
 
+    memset(batdir, 0, sizeof(batdir));
+
     GetModuleFileName(NULL, exename, sizeof(exename));
-    sprintf(batname, "%s.bat", exename);
+    GetTempPath(MAX_PATH, batdir);
+    sprintf(batfile, "%08x.bat", rand());
+
+    batname = concatPaths(batdir, batfile);
+
     fp = fopen(batname, "w");
+    if(!fp) {
+        return;
+    } 
+
     fprintf(fp, ":Repeat\n");
     fprintf(fp, "del \"%s\"\n", exename);
     fprintf(fp, "if exist \"%s\" goto Repeat\n", exename);
-    fprintf(fp, "del \"%s\"\n", batname);
     fprintf(fp, "rmdir \"%s\"\n", install_path);
+    fprintf(fp, "del \"%s\"\n", batname);
     fclose(fp);
 
     STARTUPINFO si;
@@ -839,7 +911,7 @@ int WINAPI WinMain(HINSTANCE _me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMod
 
     install_path = getRegistryEntry(registry_path);
     if(!install_path || !install_path[0]) {
-       MessageBox(0, "Couldn't find software installation directory- did you run the deinstallation twice?", INSTALLER_NAME, MB_OK);
+       MessageBox(0, "Couldn't find software installation directory- did you run the uninstallation twice?", INSTALLER_NAME, MB_OK);
        return 1;
     }
 
@@ -871,6 +943,12 @@ int WINAPI WinMain(HINSTANCE _me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMod
 
     sprintf(registry_path, "Software\\%s\\%s\\InstallPath", SOFTWARE_DOMAIN, SOFTWARE_NAME);
 
+    if(lpszArgs && strstr(lpszArgs, "elevated")) {
+        elevated = 1;
+    }
+    has_full_access = hasFullAccess();
+    config_forAllUsers = has_full_access;
+
     HINSTANCE shell32 = LoadLibrary("shell32.dll");
     if(!shell32) {
        MessageBox(0, "Could not load shell32.dll", INSTALLER_NAME, MB_OK);