pulled latest translations from Transifex
[TortoiseGit.git] / src / SshAskPass / SshAskPass.cpp
blob3da8d28b3442156646088e735f293d8f947fada1
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // SshAskPass.cpp : Defines the entry point for the application.
23 #include "stdafx.h"
24 #include "SshAskPass.h"
25 #include <stdio.h>
26 #include "propsys.h"
27 #include "PropKey.h"
29 #define MAX_LOADSTRING 100
31 // Global Variables:
32 HINSTANCE hInst; // current instance
33 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
34 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
36 // Forward declarations of functions included in this code module:
37 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
39 TCHAR g_Promptphrase[] = _T("Enter your OpenSSH passphrase:");
40 TCHAR *g_Prompt = NULL;
42 TCHAR g_PassWord[MAX_LOADSTRING];
44 int APIENTRY _tWinMain(HINSTANCE hInstance,
45 HINSTANCE hPrevInstance,
46 LPTSTR lpCmdLine,
47 int nCmdShow)
49 UNREFERENCED_PARAMETER(hPrevInstance);
51 SetDllDirectory(L"");
53 if( _tcslen(lpCmdLine) == 0 )
55 g_Prompt = g_Promptphrase;
57 else
59 g_Prompt = lpCmdLine;
62 TCHAR *yesno=_T("(yes/no)");
63 size_t lens = _tcslen(yesno);
64 TCHAR *p = lpCmdLine;
65 BOOL bYesNo=FALSE;
67 while(*p)
69 if (_tcsncicmp(p, yesno, lens) == 0)
71 bYesNo = TRUE;
72 break;
74 p++;
77 if(bYesNo)
79 if (::MessageBox(NULL, lpCmdLine, _T("TortoiseGit - git CLI stdin wrapper"), MB_YESNO|MB_ICONQUESTION) == IDYES)
81 _tprintf(_T("yes"));
83 else
85 _tprintf(_T("no"));
87 return 0;
89 else
91 if(DialogBox(hInst, MAKEINTRESOURCE(IDD_ASK_PASSWORD), NULL, About) == IDOK)
93 _tprintf(_T("%s\n"), g_PassWord);
94 return 0;
96 _tprintf(_T("\n"));
97 return -1;
99 return (int) 0;
102 void MarkWindowAsUnpinnable(HWND hWnd)
104 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
106 HMODULE hShell = LoadLibrary(_T("Shell32.dll"));
108 if (hShell) {
109 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
110 if (pfnSHGPSFW) {
111 IPropertyStore *pps;
112 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
113 if (SUCCEEDED(hr)) {
114 PROPVARIANT var;
115 var.vt = VT_BOOL;
116 var.boolVal = VARIANT_TRUE;
117 hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
118 pps->Release();
121 FreeLibrary(hShell);
125 // Message handler for password box.
126 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
128 UNREFERENCED_PARAMETER(lParam);
129 switch (message)
131 case WM_INITDIALOG:
133 MarkWindowAsUnpinnable(hDlg);
134 RECT rect;
135 ::GetWindowRect(hDlg,&rect);
136 DWORD dwWidth = GetSystemMetrics(SM_CXSCREEN);
137 DWORD dwHeight = GetSystemMetrics(SM_CYSCREEN);
139 DWORD x,y;
140 x=(dwWidth - (rect.right-rect.left))/2;
141 y=(dwHeight - (rect.bottom-rect.top))/2;
143 ::MoveWindow(hDlg,x,y,rect.right-rect.left,rect.bottom-rect.top,TRUE);
144 HWND title=::GetDlgItem(hDlg,IDC_STATIC_TITLE);
145 ::SetWindowText(title,g_Prompt);
147 TCHAR *pass =_T("pass");
148 size_t passlens = _tcslen(pass);
149 TCHAR *p = g_Prompt;
150 bool password = false;
151 while (*p)
153 if (_tcsncicmp(p, pass, passlens) == 0)
155 password = true;
156 break;
158 p++;
160 if (!password)
161 SendMessage(::GetDlgItem(hDlg, IDC_PASSWORD), EM_SETPASSWORDCHAR, 0, 0);
163 return (INT_PTR)TRUE;
165 case WM_COMMAND:
167 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
169 if( LOWORD(wParam) == IDOK )
171 HWND password = ::GetDlgItem(hDlg,IDC_PASSWORD);
172 ::GetWindowText(password,g_PassWord,MAX_LOADSTRING);
174 EndDialog(hDlg, LOWORD(wParam));
175 return (INT_PTR)TRUE;
177 break;
179 return (INT_PTR)FALSE;