/*
 * Created on 2004-08-15
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.tooldetect;

import pl.zalewski.mikolaj.symbdev.IWin32ToolConsumer;
import pl.zalewski.mikolaj.symbdev.IWin32ToolDetector;
import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.Win32ToolEntry;

/**
 * @author mikolaj
 *
 * Detects Visual Studio .NET 2003/.NET 2002/6.0
 */
public class VisualStudioToolDetect implements IWin32ToolDetector {

    final String VISUAL_C_KEY_PRE="SOFTWARE\\Microsoft\\";
    final String VISUAL_C_KEY_POST="\\Setup\\VC";
    final String VISUAL_C6_KEY_POST="\\Setup\\Microsoft Visual C++";
    final String VISUAL_C_VALUE="ProductDir";
    final String VISUAL_IDE_KEY_PRE="SOFTWARE\\Microsoft\\";
    final String VISUAL_IDE_VALUE="InstallDir";
    
    final int VISUAL_STUDIO_ID_MIN=Win32ToolEntry.AUTODETECTED_MIN;    
    
    public VisualStudioToolDetect()
    {
        super();
    }

    void findVisualStudio(String version, String versionName, int id, IWin32ToolConsumer consumer)
    {
        String vc=SymbdevPlugin.getHKLMRegValueString(
                VISUAL_C_KEY_PRE+version+VISUAL_C_KEY_POST,
                VISUAL_C_VALUE);

        String ide=SymbdevPlugin.getHKLMRegValueString(
                VISUAL_IDE_KEY_PRE+version,
                VISUAL_IDE_VALUE);

        if (vc==null || ide==null)
            return;
        
        if (ide.endsWith("\\"))
            ide=ide.substring(0, ide.length()-1);
        if (vc.endsWith("\\"))
            vc=vc.substring(0, vc.length()-1);
        
        Win32ToolEntry result=new Win32ToolEntry();
        result.name="Visual"+versionName;
        result.path=ide+";"+vc+"\\bin";
        result.target="wins";
        result.id=VISUAL_STUDIO_ID_MIN+id;
        result.autodetected=true;
        
        consumer.entryDetected(result);
    }

    void findVisualStudio6(String version, String versionName, int id, IWin32ToolConsumer consumer)
    {
        String vc=SymbdevPlugin.getHKLMRegValueString(
                VISUAL_C_KEY_PRE+version+VISUAL_C6_KEY_POST,
                VISUAL_C_VALUE);

        if (vc==null)
            return;
        
        if (vc.endsWith("\\"))
            vc=vc.substring(0, vc.length()-1);
        
        Win32ToolEntry result=new Win32ToolEntry();
        result.name="Visual"+versionName;
        result.path=vc+"\\bin";
        result.target="wins";
        result.id=VISUAL_STUDIO_ID_MIN+id;
        result.autodetected=true;
        
        consumer.entryDetected(result);
    }    
    
    /* (non-Javadoc)
     * @see pl.zalewski.mikolaj.symbdev.IWin32ToolDetector#detect(pl.zalewski.mikolaj.symbdev.IWin32ToolConsumer)
     */
    public void detect(IWin32ToolConsumer consumer)
    {
        findVisualStudio("VisualStudio\\7.1", " Studio .NET 2003", 1, consumer);
        findVisualStudio("VisualStudio\\7.0", " Studio .NET 2002", 2, consumer);
        findVisualStudio6("VisualStudio\\6.0", " Studio 6.0", 3, consumer);
        
        findVisualStudio("VisualStudio\\8.0", " Studio 2005", 99, consumer);        
        findVisualStudio("VCExpress\\8.0", " C++ Express 2005", 100, consumer);        
    }

}
