aboutsummaryrefslogtreecommitdiff
path: root/find_java2/src/WinLauncher2App.cpp
blob: 392ad9e62edf01aca1945140b7d6dac3b7d199cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#include "stdafx.h"
#include "WinLauncher2App.h"

#include "utils.h"
#include "JavaFinder.h"
#include "FindJava2Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CWinLauncher2App

BEGIN_MESSAGE_MAP(CWinLauncher2App, CWinApp)
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// The one and only CWinLauncher2App object
CWinLauncher2App theApp;

class CLauncherCmdLineInfo : public CCommandLineInfo {
public:
    bool mDoHelp;
    bool mDoForceUi;
    bool mDoJava1_7;
    CString mFilename;

    CLauncherCmdLineInfo() : mDoHelp(false), mDoForceUi(false), mDoJava1_7(false) {}

    virtual void ParseParam(const TCHAR* pszParam, BOOL bFlag, BOOL bLast) {
        // Expected command line:
        // /h | help  : msg box with command line arguments
        // /f | force : force UI selection
        // /7         : require java 1.7
        // path-to-launch

        if (!bFlag) {
            mFilename = pszParam;
        } else if (_tcsnccmp(pszParam, _T("h"), 2) == 0) {
            mDoHelp = true;
        } else if (_tcsnccmp(pszParam, _T("f"), 2) == 0) {
            mDoForceUi = true;
        } else if (_tcsnccmp(pszParam, _T("7"), 2) == 0) {
            mDoJava1_7 = true;
        }
    }
};


CWinLauncher2App::CWinLauncher2App() {
    // support Restart Manager
    m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
}

BOOL CWinLauncher2App::InitInstance() {
    // InitCommonControlsEx() is required on Windows XP if an application
    // manifest specifies use of ComCtl32.dll version 6 or later to enable
    // visual styles.  Otherwise, any window creation will fail.
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);
    // Set this to include all the common control classes you want to use
    // in your application.
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);

    CWinApp::InitInstance();
    AfxEnableControlContainer();

    // Create the shell manager, in case the dialog contains
    // any shell tree view or shell list view controls.
    CShellManager *pShellManager = new CShellManager;

    // Activate "Windows Native" visual manager for enabling themes in MFC controls
    CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

    // Set CWinApp default registry key. Must be consistent with all apps using findjava2.
    SetRegistryKey(_T("Android-FindJava2"));

    // Use VERSIONINFO.FileDescription as the canonical app name
    initUtils(NULL);

    CLauncherCmdLineInfo cmdLine;
    ParseCommandLine(cmdLine);

    if (cmdLine.mDoHelp) {
        const TCHAR *msg =
            _T("WinLauncher2 [/7|/f|/h]\r\n")
            _T("/7 : Requires Java 1.7 instead of 1.6\r\n")
            _T("/f : Force UI\r\n")
            _T("/h : Help\r\n");
            AfxMessageBox(msg);
        return FALSE; // quit without starting MFC app msg loop
    }

    CJavaFinder javaFinder(JAVA_VERS_TO_INT(1, cmdLine.mDoJava1_7 ? 7 : 6));
    CJavaPath javaPath = javaFinder.getRegistryPath();
    if (cmdLine.mDoForceUi || javaPath.isEmpty()) {
        javaPath.clear();

        CFindJava2Dlg dlg;
        dlg.setJavaFinder(&javaFinder);
        m_pMainWnd = &dlg;
        INT_PTR nResponse = dlg.DoModal();

        if (nResponse == IDOK) {
            // Use choice selected by user and save in registry.
            javaPath = dlg.getSelectedPath();
            javaFinder.setRegistryPath(javaPath);
        } else if (nResponse == IDCANCEL) {
            // Canceled by user, exit silently.
        } else if (nResponse == -1) {
            TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
        }
    }

    if (!javaPath.isEmpty()) {
        // TODO actually launch configured app instead of just printing path.
        CString msg(_T("PLACEHOLDER TODO run app using "));
        msg.Append(javaPath.mPath);
        AfxMessageBox(msg);
    }

    // Delete the shell manager created above.
    if (pShellManager != NULL) {
        delete pShellManager;
    }

    // Since the dialog has been closed, return FALSE so that we exit the
    // application, rather than start the application's message pump.
    return FALSE;
}