OSDN Git Service

Fix Issue 22 Error deleting file from context menu if filename contains spaces
[tortoisegit/TortoiseGitJp.git] / src / TortoisePlink / LoginDialog.cpp
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003 - Tim Kemp and Stefan Kueng\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software\r
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
18 \r
19 #include "LoginDialog.h"\r
20 #include "TortoisePlinkRes.h"\r
21 #include <string>\r
22 \r
23 HINSTANCE g_hmodThisDll;\r
24 HWND g_hwndMain;\r
25 \r
26 class LoginDialog\r
27 {\r
28 public:\r
29    LoginDialog(const std::string& prompt);\r
30    \r
31    static bool DoLoginDialog(std::string& password, const std::string& prompt);\r
32 \r
33 private:\r
34    bool myOK;\r
35    HWND _hdlg;\r
36 \r
37    std::string  myPassword;\r
38    std::string  myPrompt;\r
39    \r
40    void CreateModule(void);\r
41    void RetrieveValues();\r
42    \r
43    std::string GetPassword();\r
44 \r
45    friend BOOL CALLBACK LoginDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);\r
46 };\r
47 \r
48 \r
49 BOOL DoLoginDialog(char* password, int maxlen, const char* prompt)\r
50 {\r
51    g_hmodThisDll = GetModuleHandle(0);\r
52    g_hwndMain = GetParentHwnd();\r
53    std::string passwordstr;\r
54    BOOL res = LoginDialog::DoLoginDialog(passwordstr, prompt);\r
55    if (res)\r
56       strncpy(password, passwordstr.c_str(), maxlen);\r
57    return res;\r
58 }\r
59 \r
60 \r
61 BOOL CALLBACK LoginDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)\r
62 {\r
63    if (uMsg == WM_INITDIALOG)\r
64    {\r
65       LoginDialog* pDlg = (LoginDialog*) lParam;\r
66       pDlg->_hdlg = hwndDlg;\r
67       SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);\r
68       // Set prompt text\r
69       SendDlgItemMessage(hwndDlg, IDC_LOGIN_PROMPT, WM_SETTEXT,\r
70                          pDlg->myPrompt.length(), (LPARAM) pDlg->myPrompt.c_str());\r
71       // Make sure edit control has the focus\r
72       //SendDlgItemMessage(hwndDlg, IDC_LOGIN_PASSWORD, WM_SETFOCUS, 0, 0);\r
73       if (GetDlgCtrlID((HWND) wParam) != IDC_LOGIN_PASSWORD)\r
74       { \r
75          SetFocus(GetDlgItem(hwndDlg, IDC_LOGIN_PASSWORD));\r
76          return FALSE; \r
77       } \r
78       return TRUE; \r
79    }\r
80    else if (uMsg == WM_COMMAND && LOWORD(wParam) == IDCANCEL && HIWORD(wParam) == BN_CLICKED)\r
81    {\r
82       LoginDialog* pDlg = (LoginDialog*) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);\r
83       pDlg->myOK = false;\r
84       EndDialog(hwndDlg, IDCANCEL);\r
85       return 1;\r
86    }\r
87    else if (uMsg == WM_COMMAND && LOWORD(wParam) == IDOK && HIWORD(wParam) == BN_CLICKED)\r
88    {\r
89       LoginDialog* pDlg = (LoginDialog*) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);\r
90       pDlg->myOK = true;\r
91       pDlg->RetrieveValues();\r
92       EndDialog(hwndDlg, IDOK);\r
93       return 1;\r
94    }\r
95    return 0;\r
96 }\r
97 \r
98 LoginDialog::LoginDialog(const std::string& prompt)\r
99 {\r
100    myPrompt = prompt;\r
101 }\r
102 \r
103 void LoginDialog::CreateModule(void)\r
104 {\r
105    DialogBoxParam(g_hmodThisDll, MAKEINTRESOURCE(IDD_LOGIN), g_hwndMain,\r
106                   (DLGPROC)(LoginDialogProc), (long)this);\r
107 }\r
108 \r
109 \r
110 bool LoginDialog::DoLoginDialog(std::string& password, const std::string& prompt)\r
111 {\r
112    LoginDialog *pDlg = new LoginDialog(prompt);\r
113 \r
114    pDlg->CreateModule();\r
115 \r
116    bool ret = pDlg->myOK;\r
117    password = pDlg->myPassword;\r
118    \r
119    delete pDlg;\r
120 \r
121    return ret;\r
122 }\r
123 \r
124 \r
125 std::string LoginDialog::GetPassword()\r
126 {\r
127    char szTxt[256];\r
128    SendDlgItemMessage(_hdlg, IDC_LOGIN_PASSWORD, WM_GETTEXT, sizeof(szTxt), (LPARAM)szTxt);\r
129    std::string strText = szTxt;\r
130    return strText;\r
131 }\r
132 \r
133 void LoginDialog::RetrieveValues()\r
134 {\r
135    myPassword = GetPassword();\r
136 }\r
137 \r
138 \r
139 BOOL IsWinNT()\r
140 {\r
141    OSVERSIONINFO vi;\r
142    vi.dwOSVersionInfoSize = sizeof(vi);\r
143    if (GetVersionEx(&vi))\r
144    {\r
145       if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT)\r
146       {\r
147          return TRUE;\r
148       }\r
149    }\r
150    return FALSE;\r
151 }\r
152 \r
153 HWND GetParentHwnd()\r
154 {\r
155    if (IsWinNT())\r
156    {\r
157       return GetDesktopWindow();\r
158    }\r
159    else\r
160    {\r
161       return GetForegroundWindow();\r
162    }\r
163 }\r