Doc: Fix broken link
[TortoiseGit.git] / src / SshAskPass / SshAskPass.cpp
blob2d6ad7a2edad8039b88911eb93b1b3b86680bf82
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2016 - 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>
27 #include "UnicodeUtils.h"
29 #include <commctrl.h>
30 #pragma comment(lib, "comctl32.lib")
31 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
33 #define MAX_LOADSTRING 100
35 // Global Variables:
36 HINSTANCE hInst; // current instance
38 const TCHAR g_Promptphrase[] = L"Enter your OpenSSH passphrase:";
39 const TCHAR* g_Prompt = g_Promptphrase;
41 TCHAR g_PassWord[MAX_LOADSTRING];
43 // Forward declarations of functions included in this code module:
44 INT_PTR CALLBACK PasswdDlg(HWND, UINT, WPARAM, LPARAM);
46 int APIENTRY _tWinMain(HINSTANCE /*hInstance*/,
47 HINSTANCE /*hPrevInstance*/,
48 LPTSTR lpCmdLine,
49 int /*nCmdShow*/)
51 SetDllDirectory(L"");
53 InitCommonControls();
55 size_t cmdlineLen = wcslen(lpCmdLine);
56 if (lpCmdLine[0] == '"' && cmdlineLen > 1 && lpCmdLine[cmdlineLen - 1] == '"')
58 lpCmdLine[cmdlineLen - 1] = L'\0';
59 ++lpCmdLine;
61 if (lpCmdLine[0] != L'\0')
62 g_Prompt = lpCmdLine;
64 if (StrStrI(lpCmdLine, L"(yes/no)"))
66 if (::MessageBox(nullptr, g_Prompt, L"TortoiseGit - git CLI stdin wrapper", MB_YESNO | MB_ICONQUESTION) == IDYES)
67 wprintf(L"yes");
68 else
69 wprintf(L"no");
70 return 0;
73 if (StrStrI(lpCmdLine, L"Should I try again?"))
75 if (::MessageBox(nullptr, g_Prompt, L"TortoiseGit - git CLI yes/no wrapper", MB_YESNO | MB_ICONQUESTION) == IDYES)
76 return 0;
78 return 1;
81 if (DialogBox(hInst, MAKEINTRESOURCE(IDD_ASK_PASSWORD), nullptr, PasswdDlg) == IDOK)
83 printf("%s\n", CUnicodeUtils::StdGetUTF8(g_PassWord).c_str());
84 return 0;
86 wprintf(L"\n");
87 return -1;
90 void MarkWindowAsUnpinnable(HWND hWnd)
92 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
94 HMODULE hShell = AtlLoadSystemLibraryUsingFullPath(L"Shell32.dll");
96 if (hShell) {
97 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
98 if (pfnSHGPSFW) {
99 IPropertyStore *pps;
100 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
101 if (SUCCEEDED(hr)) {
102 PROPVARIANT var;
103 var.vt = VT_BOOL;
104 var.boolVal = VARIANT_TRUE;
105 pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
106 pps->Release();
109 FreeLibrary(hShell);
113 // Message handler for password box.
114 INT_PTR CALLBACK PasswdDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM /*lParam*/)
116 switch (message)
118 case WM_INITDIALOG:
120 MarkWindowAsUnpinnable(hDlg);
121 RECT rect;
122 ::GetWindowRect(hDlg,&rect);
123 DWORD dwWidth = GetSystemMetrics(SM_CXSCREEN);
124 DWORD dwHeight = GetSystemMetrics(SM_CYSCREEN);
126 DWORD x,y;
127 x=(dwWidth - (rect.right-rect.left))/2;
128 y=(dwHeight - (rect.bottom-rect.top))/2;
130 ::MoveWindow(hDlg,x,y,rect.right-rect.left,rect.bottom-rect.top,TRUE);
131 HWND title = ::GetDlgItem(hDlg, IDC_STATIC_TITLE);
132 ::SetWindowText(title, g_Prompt);
133 if (!StrStrI(g_Prompt, L"pass"))
134 SendMessage(::GetDlgItem(hDlg, IDC_PASSWORD), EM_SETPASSWORDCHAR, 0, 0);
135 ::FlashWindow(hDlg, TRUE);
137 return (INT_PTR)TRUE;
139 case WM_COMMAND:
141 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
143 if( LOWORD(wParam) == IDOK )
145 HWND password = ::GetDlgItem(hDlg,IDC_PASSWORD);
146 ::GetWindowText(password,g_PassWord,MAX_LOADSTRING);
148 EndDialog(hDlg, LOWORD(wParam));
149 return (INT_PTR)TRUE;
151 break;
153 return (INT_PTR)FALSE;