/*
    UninstallMenu.java
    Create a menu item for the uninstaller

    Notes: 
     1. You can probably use this class as is, but be sure to review the
	    code to insure it's doing what you expect.
     2. Add UninstallMenu to the "Customs" | "Installer" tab 
       "After uninstaller created". Move the UninstallerMenu class
       to whatever directory you define for custom installer classes.
     3. You are welcome to adapt this to meet your needs
        as long as you only use it with the JExpress product
 

    Copyright (C) 1998-2010 DeNova
    
*/


import javax.swing.JLabel;
import java.io.File;
import java.awt.BorderLayout;
import com.denova.io.Log;
import com.denova.runtime.UnixMenu;
import com.denova.runtime.WindowsConstants;
import com.denova.runtime.WindowsMenus;
import com.denova.runtime.WindowsRegistry;
import com.denova.runtime.WindowsUtils;
import com.denova.ui.Fonts;
import com.denova.ui.WizardPanel;
import com.denova.util.PropertyList;
import com.denova.JExpress.Installer.CustomUninstaller;
import com.denova.JExpress.Installer.InstallPropertyNames;



public class UninstallMenu extends WizardPanel
                              implements InstallPropertyNames {

    public UninstallMenu(PropertyList properties) {

        // initialize WizardPanel superclass
        super(properties);

        // let them know what's happening
        setLayout(new BorderLayout ());
        JLabel notice = new JLabel("Creating menu item for the uninstaller", JLabel.CENTER);
        notice.setFont(Fonts.Bold);
        add(notice);
    }

    /** 
     * Enter the panel. 
     *
     * Do not call this method; only the wizard should call it.
     */
     public void enter()
     {
        // we don't actually want to interact with the user

        try {

            if (isWindows()) {
                createWindowsMenu();
            }
            
            else if (!isMacOsX())
            {
                createUnixMenu();
            }

        }
        catch(Exception e) {

            // report that something went wrong
            debug("exception while creating uninstaller menu");
            log(e);
        }

        // we don't want any user interaction
        // so move them to the next panel automatically
        showNextPanel ();
    }

    
    private void createUnixMenu()
    throws Exception
    {
        if (UnixMenu.kdeMenusInstalled() ||
            UnixMenu.gnomeMenusInstalled())
        {
            String submenu = getPropertyList().getProperty(Submenu, "");
            String installDir = getPropertyList().getProperty(ApplicationDirectory, "");
            String productName = getPropertyList().getProperty(PackageName, "");
                    
            UnixMenu unixMenu = new UnixMenu(uninstallLog);
            unixMenu.setSubmenu(submenu);
            unixMenu.setInstallDir(installDir);
            unixMenu.setWorkingDir(installDir);
            unixMenu.setLaunchDirName(installDir);
            unixMenu.setLongName("Uninstall " + productName);
            unixMenu.setShortName("uninstall");
            
            if (UnixMenu.kdeMenusInstalled())
            {
                unixMenu.createKdeMenu("uninstall", false);
            }
            
            if (UnixMenu.gnomeMenusInstalled())
            {
                unixMenu.createGnomeMenu("uninstall", false);
            }
        }
    }
    
    
    private void createWindowsMenu()
    throws Exception {

        // clear any errors
        WindowsUtils.clearError();

        String productName = getPropertyList().getProperty(PackageName, "");
        String submenu = getPropertyList().getProperty(Submenu, "");
        String version = getPropertyList().getProperty(PackageVersion, "");

        if (productName. length() > 0)
        {
            boolean ok;

            final String PrimaryKey = 
			 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + productName;

            String uninstallCommand = WindowsRegistry.getStringData(PrimaryKey + " " + version, "UninstallString");
            if (uninstallCommand == null) {
                uninstallCommand = WindowsRegistry.getStringData(PrimaryKey, "UninstallString");
            }

            if (uninstallCommand == null ||
                uninstallCommand. length () <= 0)
            {
                log("unable to get uninstall command from " + PrimaryKey);
            }
            else
            {
				String commandArgs = "";
				int index = -1;
				
				// if the command starts with "", find the ending quote
				if (uninstallCommand. charAt(0) == '"') {
					
					String innerString = uninstallCommand. substring(1);
					index = innerString. indexOf("\"") + 1;
				}
				
				else {
					index = uninstallCommand. indexOf(" ");
				}
				
				if (index > 0) {
					commandArgs = uninstallCommand. substring(index);
					uninstallCommand = uninstallCommand. substring(0, index);
				}
				
				// split the command into the directory and the executable file
				File f = new File(uninstallCommand);
				String execCommand = f. getName ();
				String workingDir = f. getParent ();
				
				String menuName = "Uninstall " + productName;
				String windowsStyle = "";
				String iconFilename = WindowsRegistry.getStringData(PrimaryKey, "DisplayIcon");
				if (iconFilename == null)
				{
				    iconFilename = "";
				}
				
				debug("uninstallCommand " + uninstallCommand);
				debug("commandArgs " + commandArgs);
				debug("workingDir " + workingDir);
				debug("execCommand " + execCommand);
				debug("iconFilename " + iconFilename);
				
                WindowsMenus windowsMenus = new WindowsMenus();
                ok = windowsMenus.addMenuItem(submenu,
        	                         menuName,
        	                         workingDir,
                                     execCommand,
                                     commandArgs,
                                     windowsStyle,
                                     iconFilename);
                debug("added uninstall menu: " + ok);
				
                // remove this menu item during the uninstall
                String applicationDirectory = getPropertyList (). getProperty(ApplicationDirectory, "");
                CustomUninstaller customUninstaller = new CustomUninstaller ();
                customUninstaller.deleteWindowsMenuItem(submenu,
                                                        menuName,
                                                        WindowsConstants. CurrentUser);

                customUninstaller.append(applicationDirectory);

            }
        }
    }

    void debug(String s) {
        if (debugging) {
            log (s);
        }
    }

    static private void log(String s) {
        startLog ();
        uninstallLog. write(s);
    }

    static private void log(Exception e) {
        startLog ();
        uninstallLog. setLogging(true);
        uninstallLog. write(e);
    }

    static private void startLog () {
        if (uninstallLog == null) {
            uninstallLog = new Log("uninstallmenu");
        }
    }

    static private void stopLogging () {
        if (uninstallLog != null) {
            uninstallLog. stopLogging ();
        }
    }

    static Log uninstallLog;
    static final boolean debugging = true;

}  // UninstallMenu