OSDN Git Service

Update version number to 1.2.1.0
[tortoisegit/TortoiseGitJp.git] / src / SshAskPass / SshAskPass.cpp
1 // SshAskPass.cpp : Defines the entry point for the application.\r
2 //\r
3 \r
4 #include "stdafx.h"\r
5 #include "SshAskPass.h"\r
6 #include <stdio.h>\r
7 \r
8 #define MAX_LOADSTRING 100\r
9 \r
10 // Global Variables:\r
11 HINSTANCE hInst;                                                                // current instance\r
12 TCHAR szTitle[MAX_LOADSTRING];                                  // The title bar text\r
13 TCHAR szWindowClass[MAX_LOADSTRING];                    // the main window class name\r
14 \r
15 // Forward declarations of functions included in this code module:\r
16 ATOM                            MyRegisterClass(HINSTANCE hInstance);\r
17 BOOL                            InitInstance(HINSTANCE, int);\r
18 LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);\r
19 INT_PTR CALLBACK        About(HWND, UINT, WPARAM, LPARAM);\r
20 \r
21 TCHAR g_Promptphrase[] = _T("Entry your OpenSSH passphrase:");\r
22 TCHAR *g_Prompt = NULL;\r
23 \r
24 TCHAR g_PassWord[MAX_LOADSTRING];\r
25 \r
26 int APIENTRY _tWinMain(HINSTANCE hInstance,\r
27                      HINSTANCE hPrevInstance,\r
28                      LPTSTR    lpCmdLine,\r
29                      int       nCmdShow)\r
30 {\r
31         UNREFERENCED_PARAMETER(hPrevInstance);\r
32         UNREFERENCED_PARAMETER(lpCmdLine);\r
33 \r
34         // TODO: Place code here.\r
35         MSG msg;\r
36         HACCEL hAccelTable;\r
37 \r
38 \r
39     if( _tcslen(lpCmdLine) == 0 )\r
40     {\r
41         g_Prompt = g_Promptphrase;\r
42     }\r
43     else\r
44     {\r
45         g_Prompt = lpCmdLine;\r
46     }\r
47     \r
48     _tcslwr(lpCmdLine); //low case\r
49     \r
50     TCHAR *yesno=_T("(yes/no)");\r
51     int lens=_tcslen(yesno);\r
52     TCHAR *p = lpCmdLine;\r
53     BOOL bYesNo=FALSE;\r
54 \r
55     while(*p)\r
56     {\r
57         if(_tcsncmp(p,yesno,lens) == 0)\r
58         {\r
59             bYesNo = TRUE;\r
60         }\r
61         p++;\r
62     }\r
63 \r
64     if(bYesNo)\r
65     {\r
66         if(::MessageBox(NULL,lpCmdLine,_T("OpenSSH"),MB_YESNO|MB_ICONQUESTION) == IDYES)\r
67         {\r
68             _tprintf(_T("yes"));\r
69 \r
70         }else\r
71         {\r
72             _tprintf(_T("no"));\r
73         }\r
74         return 0;\r
75     }\r
76     else\r
77     {\r
78         if(DialogBox(hInst, MAKEINTRESOURCE(IDD_ASK_PASSWORD), NULL, About) == IDOK)\r
79         {\r
80             _tprintf(g_PassWord);\r
81             return 0;\r
82         }\r
83         return -1;\r
84     }\r
85 \r
86 #if 0\r
87         // Initialize global strings\r
88         LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);\r
89         LoadString(hInstance, IDC_SSHASKPASS, szWindowClass, MAX_LOADSTRING);\r
90         MyRegisterClass(hInstance);\r
91 \r
92         // Perform application initialization:\r
93         if (!InitInstance (hInstance, nCmdShow))\r
94         {\r
95                 return FALSE;\r
96         }\r
97 \r
98 \r
99         hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SSHASKPASS));\r
100 \r
101         // Main message loop:\r
102         while (GetMessage(&msg, NULL, 0, 0))\r
103         {\r
104                 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))\r
105                 {\r
106                         TranslateMessage(&msg);\r
107                         DispatchMessage(&msg);\r
108                 }\r
109         }\r
110 #endif\r
111         return (int) 0;\r
112 }\r
113 \r
114 \r
115 \r
116 //\r
117 //  FUNCTION: MyRegisterClass()\r
118 //\r
119 //  PURPOSE: Registers the window class.\r
120 //\r
121 //  COMMENTS:\r
122 //\r
123 //    This function and its usage are only necessary if you want this code\r
124 //    to be compatible with Win32 systems prior to the 'RegisterClassEx'\r
125 //    function that was added to Windows 95. It is important to call this function\r
126 //    so that the application will get 'well formed' small icons associated\r
127 //    with it.\r
128 //\r
129 ATOM MyRegisterClass(HINSTANCE hInstance)\r
130 {\r
131         WNDCLASSEX wcex;\r
132 \r
133         wcex.cbSize = sizeof(WNDCLASSEX);\r
134 \r
135         wcex.style                      = CS_HREDRAW | CS_VREDRAW;\r
136         wcex.lpfnWndProc        = WndProc;\r
137         wcex.cbClsExtra         = 0;\r
138         wcex.cbWndExtra         = 0;\r
139         wcex.hInstance          = hInstance;\r
140         wcex.hIcon                      = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SSHASKPASS));\r
141         wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);\r
142         wcex.hbrBackground      = (HBRUSH)(COLOR_WINDOW+1);\r
143         wcex.lpszMenuName       = MAKEINTRESOURCE(IDC_SSHASKPASS);\r
144         wcex.lpszClassName      = szWindowClass;\r
145         wcex.hIconSm            = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));\r
146 \r
147         return RegisterClassEx(&wcex);\r
148 }\r
149 \r
150 //\r
151 //   FUNCTION: InitInstance(HINSTANCE, int)\r
152 //\r
153 //   PURPOSE: Saves instance handle and creates main window\r
154 //\r
155 //   COMMENTS:\r
156 //\r
157 //        In this function, we save the instance handle in a global variable and\r
158 //        create and display the main program window.\r
159 //\r
160 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)\r
161 {\r
162    HWND hWnd;\r
163 \r
164    hInst = hInstance; // Store instance handle in our global variable\r
165 \r
166    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,\r
167       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);\r
168 \r
169    if (!hWnd)\r
170    {\r
171       return FALSE;\r
172    }\r
173 \r
174    ShowWindow(hWnd, nCmdShow);\r
175    UpdateWindow(hWnd);\r
176 \r
177    return TRUE;\r
178 }\r
179 \r
180 //\r
181 //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)\r
182 //\r
183 //  PURPOSE:  Processes messages for the main window.\r
184 //\r
185 //  WM_COMMAND  - process the application menu\r
186 //  WM_PAINT    - Paint the main window\r
187 //  WM_DESTROY  - post a quit message and return\r
188 //\r
189 //\r
190 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)\r
191 {\r
192         int wmId, wmEvent;\r
193         PAINTSTRUCT ps;\r
194         HDC hdc;\r
195 \r
196         switch (message)\r
197         {\r
198         case WM_COMMAND:\r
199                 wmId    = LOWORD(wParam);\r
200                 wmEvent = HIWORD(wParam);\r
201                 // Parse the menu selections:\r
202                 switch (wmId)\r
203                 {\r
204                 case IDM_ABOUT:\r
205                         DialogBox(hInst, MAKEINTRESOURCE(IDD_ASK_PASSWORD), hWnd, About);\r
206                         break;\r
207                 case IDM_EXIT:\r
208                         DestroyWindow(hWnd);\r
209                         break;\r
210                 default:\r
211                         return DefWindowProc(hWnd, message, wParam, lParam);\r
212                 }\r
213                 break;\r
214         case WM_PAINT:\r
215                 hdc = BeginPaint(hWnd, &ps);\r
216                 // TODO: Add any drawing code here...\r
217                 EndPaint(hWnd, &ps);\r
218                 break;\r
219         case WM_DESTROY:\r
220                 PostQuitMessage(0);\r
221                 break;\r
222         default:\r
223                 return DefWindowProc(hWnd, message, wParam, lParam);\r
224         }\r
225         return 0;\r
226 }\r
227 \r
228 // Message handler for about box.\r
229 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
230 {\r
231         UNREFERENCED_PARAMETER(lParam);\r
232         switch (message)\r
233         {\r
234         case WM_INITDIALOG:\r
235         {   \r
236             RECT rect;\r
237             ::GetWindowRect(hDlg,&rect);\r
238             DWORD dwWidth = GetSystemMetrics(SM_CXSCREEN);\r
239             DWORD dwHeight = GetSystemMetrics(SM_CYSCREEN);\r
240 \r
241             DWORD x,y;\r
242             x=(dwWidth - (rect.right-rect.left))/2;\r
243             y=(dwHeight - (rect.bottom-rect.top))/2;\r
244             \r
245             ::MoveWindow(hDlg,x,y,rect.right-rect.left,rect.bottom-rect.top,TRUE);\r
246             HWND title=::GetDlgItem(hDlg,IDC_STATIC_TITLE);\r
247             if(g_Prompt)\r
248                 ::SetWindowText(title,g_Prompt);\r
249         }\r
250                 return (INT_PTR)TRUE;\r
251 \r
252         case WM_COMMAND:\r
253 \r
254                 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)\r
255                 {\r
256                         if( LOWORD(wParam) == IDOK )\r
257             {\r
258                 HWND password = ::GetDlgItem(hDlg,IDC_PASSWORD);\r
259                 ::GetWindowText(password,g_PassWord,MAX_LOADSTRING);\r
260             }\r
261             EndDialog(hDlg, LOWORD(wParam));\r
262                         return (INT_PTR)TRUE;\r
263                 }\r
264                 break;\r
265         }\r
266         return (INT_PTR)FALSE;\r
267 }\r