Fixed issue #2495: "Show Reflog" dialog shows empty action for "push" entries
[TortoiseGit.git] / src / SshAskPass / SshAskPass.cpp
blobbc1a16887685303861ac5c6e6702ac8eed50b4a1
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - 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 "resource.h"
25 #include <propsys.h>
26 #include <PropKey.h>
28 #include <commctrl.h>
29 #pragma comment(lib, "comctl32.lib")
30 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
32 #define MAX_LOADSTRING 100
34 // Global Variables:
35 HINSTANCE hInst; // current instance
37 const TCHAR g_Promptphrase[] = _T("Enter your OpenSSH passphrase:");
38 const TCHAR *g_Prompt = g_Promptphrase;
40 TCHAR g_PassWord[MAX_LOADSTRING];
42 // Forward declarations of functions included in this code module:
43 INT_PTR CALLBACK PasswdDlg(HWND, UINT, WPARAM, LPARAM);
45 int APIENTRY _tWinMain(HINSTANCE /*hInstance*/,
46 HINSTANCE /*hPrevInstance*/,
47 LPTSTR lpCmdLine,
48 int /*nCmdShow*/)
50 SetDllDirectory(L"");
52 InitCommonControls();
54 size_t cmdlineLen =_tcslen(lpCmdLine);
55 if (lpCmdLine[0] == '"' && cmdlineLen > 1 && lpCmdLine[cmdlineLen - 1] == '"')
57 lpCmdLine[cmdlineLen - 1] = 0;
58 ++lpCmdLine;
60 if (lpCmdLine[0] != 0)
61 g_Prompt = lpCmdLine;
63 const TCHAR *yesno=_T("(yes/no)");
64 const size_t lens = _tcslen(yesno);
65 const TCHAR *p = lpCmdLine;
66 BOOL bYesNo=FALSE;
68 while(*p)
70 if (_tcsncicmp(p, yesno, lens) == 0)
72 bYesNo = TRUE;
73 break;
75 ++p;
78 if(bYesNo)
80 if (::MessageBox(NULL, g_Prompt, _T("TortoiseGit - git CLI stdin wrapper"), MB_YESNO|MB_ICONQUESTION) == IDYES)
82 _tprintf(_T("yes"));
84 else
86 _tprintf(_T("no"));
88 return 0;
90 else
92 if (DialogBox(hInst, MAKEINTRESOURCE(IDD_ASK_PASSWORD), nullptr, PasswdDlg) == IDOK)
94 _tprintf(_T("%s\n"), g_PassWord);
95 return 0;
97 _tprintf(_T("\n"));
98 return -1;
102 void MarkWindowAsUnpinnable(HWND hWnd)
104 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
106 HMODULE hShell = AtlLoadSystemLibraryUsingFullPath(_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 pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
118 pps->Release();
121 FreeLibrary(hShell);
125 // Message handler for password box.
126 INT_PTR CALLBACK PasswdDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM /*lParam*/)
128 switch (message)
130 case WM_INITDIALOG:
132 MarkWindowAsUnpinnable(hDlg);
133 RECT rect;
134 ::GetWindowRect(hDlg,&rect);
135 DWORD dwWidth = GetSystemMetrics(SM_CXSCREEN);
136 DWORD dwHeight = GetSystemMetrics(SM_CYSCREEN);
138 DWORD x,y;
139 x=(dwWidth - (rect.right-rect.left))/2;
140 y=(dwHeight - (rect.bottom-rect.top))/2;
142 ::MoveWindow(hDlg,x,y,rect.right-rect.left,rect.bottom-rect.top,TRUE);
143 HWND title=::GetDlgItem(hDlg,IDC_STATIC_TITLE);
144 ::SetWindowText(title,g_Prompt);
146 const TCHAR *pass =_T("pass");
147 const size_t passlens = _tcslen(pass);
148 const TCHAR *p = g_Prompt;
149 bool password = false;
150 while (*p)
152 if (_tcsncicmp(p, pass, passlens) == 0)
154 password = true;
155 break;
157 ++p;
159 if (!password)
160 SendMessage(::GetDlgItem(hDlg, IDC_PASSWORD), EM_SETPASSWORDCHAR, 0, 0);
161 ::FlashWindow(hDlg, TRUE);
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;