Updated changelog
[TortoiseGit.git] / src / TortoisePlink / LoginDialog.cpp
blob94ea92b4661d4d97560fa136c2b702ff37058fe9
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2003 - Tim Kemp and Stefan Kueng
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
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include "LoginDialog.h"
20 #include "TortoisePlinkRes.h"
21 #include <string>
23 HINSTANCE g_hmodThisDll;
24 HWND g_hwndMain;
26 class LoginDialog
28 public:
29 LoginDialog(const std::string& prompt);
31 static bool DoLoginDialog(std::string& password, const std::string& prompt);
33 private:
34 bool myOK;
35 HWND _hdlg;
37 std::string myPassword;
38 std::string myPrompt;
40 void CreateModule(void);
41 void RetrieveValues();
43 std::string GetPassword();
45 friend BOOL CALLBACK LoginDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
49 BOOL DoLoginDialog(char* password, int maxlen, const char* prompt)
51 g_hmodThisDll = GetModuleHandle(0);
52 g_hwndMain = GetParentHwnd();
53 std::string passwordstr;
54 BOOL res = LoginDialog::DoLoginDialog(passwordstr, prompt);
55 if (res)
56 strncpy(password, passwordstr.c_str(), maxlen);
57 return res;
61 BOOL CALLBACK LoginDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
63 if (uMsg == WM_INITDIALOG)
65 LoginDialog* pDlg = (LoginDialog*) lParam;
66 pDlg->_hdlg = hwndDlg;
67 SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
68 // Set prompt text
69 SendDlgItemMessage(hwndDlg, IDC_LOGIN_PROMPT, WM_SETTEXT,
70 pDlg->myPrompt.length(), (LPARAM) pDlg->myPrompt.c_str());
71 // Make sure edit control has the focus
72 //SendDlgItemMessage(hwndDlg, IDC_LOGIN_PASSWORD, WM_SETFOCUS, 0, 0);
73 if (GetDlgCtrlID((HWND) wParam) != IDC_LOGIN_PASSWORD)
75 SetFocus(GetDlgItem(hwndDlg, IDC_LOGIN_PASSWORD));
76 return FALSE;
78 return TRUE;
80 else if (uMsg == WM_COMMAND && LOWORD(wParam) == IDCANCEL && HIWORD(wParam) == BN_CLICKED)
82 LoginDialog* pDlg = (LoginDialog*) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
83 pDlg->myOK = false;
84 EndDialog(hwndDlg, IDCANCEL);
85 return 1;
87 else if (uMsg == WM_COMMAND && LOWORD(wParam) == IDOK && HIWORD(wParam) == BN_CLICKED)
89 LoginDialog* pDlg = (LoginDialog*) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
90 pDlg->myOK = true;
91 pDlg->RetrieveValues();
92 EndDialog(hwndDlg, IDOK);
93 return 1;
95 return 0;
98 LoginDialog::LoginDialog(const std::string& prompt)
100 myPrompt = prompt;
103 void LoginDialog::CreateModule(void)
105 DialogBoxParam(g_hmodThisDll, MAKEINTRESOURCE(IDD_LOGIN), g_hwndMain,
106 (DLGPROC)(LoginDialogProc), (LONG_PTR)this);
110 bool LoginDialog::DoLoginDialog(std::string& password, const std::string& prompt)
112 LoginDialog *pDlg = new LoginDialog(prompt);
114 pDlg->CreateModule();
116 bool ret = pDlg->myOK;
117 password = pDlg->myPassword;
119 delete pDlg;
121 return ret;
125 std::string LoginDialog::GetPassword()
127 char szTxt[256];
128 SendDlgItemMessage(_hdlg, IDC_LOGIN_PASSWORD, WM_GETTEXT, sizeof(szTxt), (LPARAM)szTxt);
129 std::string strText = szTxt;
130 return strText;
133 void LoginDialog::RetrieveValues()
135 myPassword = GetPassword();
139 BOOL IsWinNT()
141 OSVERSIONINFO vi;
142 vi.dwOSVersionInfoSize = sizeof(vi);
143 if (GetVersionEx(&vi))
145 if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT)
147 return TRUE;
150 return FALSE;
153 HWND GetParentHwnd()
155 if (IsWinNT())
157 return GetDesktopWindow();
159 else
161 return GetForegroundWindow();