OSDN Git Service

add TortoisePlink
[tortoisegit/TortoiseGitJp.git] / src / TortoisePlink / LoginDialog.cpp
diff --git a/src/TortoisePlink/LoginDialog.cpp b/src/TortoisePlink/LoginDialog.cpp
new file mode 100644 (file)
index 0000000..f03a3c3
--- /dev/null
@@ -0,0 +1,163 @@
+// TortoiseSVN - a Windows shell extension for easy version control\r
+\r
+// Copyright (C) 2003 - Tim Kemp and Stefan Kueng\r
+\r
+// This program is free software; you can redistribute it and/or\r
+// modify it under the terms of the GNU General Public License\r
+// as published by the Free Software Foundation; either version 2\r
+// of the License, or (at your option) any later version.\r
+\r
+// This program is distributed in the hope that it will be useful,\r
+// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+// GNU General Public License for more details.\r
+\r
+// You should have received a copy of the GNU General Public License\r
+// along with this program; if not, write to the Free Software\r
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
+\r
+#include "LoginDialog.h"\r
+#include "TortoisePlinkRes.h"\r
+#include <string>\r
+\r
+HINSTANCE g_hmodThisDll;\r
+HWND g_hwndMain;\r
+\r
+class LoginDialog\r
+{\r
+public:\r
+   LoginDialog(const std::string& prompt);\r
+   \r
+   static bool DoLoginDialog(std::string& password, const std::string& prompt);\r
+\r
+private:\r
+   bool myOK;\r
+   HWND _hdlg;\r
+\r
+   std::string  myPassword;\r
+   std::string  myPrompt;\r
+   \r
+   void CreateModule(void);\r
+   void RetrieveValues();\r
+   \r
+   std::string GetPassword();\r
+\r
+   friend BOOL CALLBACK LoginDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);\r
+};\r
+\r
+\r
+BOOL DoLoginDialog(char* password, int maxlen, const char* prompt)\r
+{\r
+   g_hmodThisDll = GetModuleHandle(0);\r
+   g_hwndMain = GetParentHwnd();\r
+   std::string passwordstr;\r
+   BOOL res = LoginDialog::DoLoginDialog(passwordstr, prompt);\r
+   if (res)\r
+      strncpy(password, passwordstr.c_str(), maxlen);\r
+   return res;\r
+}\r
+\r
+\r
+BOOL CALLBACK LoginDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)\r
+{\r
+   if (uMsg == WM_INITDIALOG)\r
+   {\r
+      LoginDialog* pDlg = (LoginDialog*) lParam;\r
+      pDlg->_hdlg = hwndDlg;\r
+      SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);\r
+      // Set prompt text\r
+      SendDlgItemMessage(hwndDlg, IDC_LOGIN_PROMPT, WM_SETTEXT,\r
+                         pDlg->myPrompt.length(), (LPARAM) pDlg->myPrompt.c_str());\r
+      // Make sure edit control has the focus\r
+      //SendDlgItemMessage(hwndDlg, IDC_LOGIN_PASSWORD, WM_SETFOCUS, 0, 0);\r
+      if (GetDlgCtrlID((HWND) wParam) != IDC_LOGIN_PASSWORD)\r
+      { \r
+         SetFocus(GetDlgItem(hwndDlg, IDC_LOGIN_PASSWORD));\r
+         return FALSE; \r
+      } \r
+      return TRUE; \r
+   }\r
+   else if (uMsg == WM_COMMAND && LOWORD(wParam) == IDCANCEL && HIWORD(wParam) == BN_CLICKED)\r
+   {\r
+      LoginDialog* pDlg = (LoginDialog*) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);\r
+      pDlg->myOK = false;\r
+      EndDialog(hwndDlg, IDCANCEL);\r
+      return 1;\r
+   }\r
+   else if (uMsg == WM_COMMAND && LOWORD(wParam) == IDOK && HIWORD(wParam) == BN_CLICKED)\r
+   {\r
+      LoginDialog* pDlg = (LoginDialog*) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);\r
+      pDlg->myOK = true;\r
+      pDlg->RetrieveValues();\r
+      EndDialog(hwndDlg, IDOK);\r
+      return 1;\r
+   }\r
+   return 0;\r
+}\r
+\r
+LoginDialog::LoginDialog(const std::string& prompt)\r
+{\r
+   myPrompt = prompt;\r
+}\r
+\r
+void LoginDialog::CreateModule(void)\r
+{\r
+   DialogBoxParam(g_hmodThisDll, MAKEINTRESOURCE(IDD_LOGIN), g_hwndMain,\r
+                  (DLGPROC)(LoginDialogProc), (long)this);\r
+}\r
+\r
+\r
+bool LoginDialog::DoLoginDialog(std::string& password, const std::string& prompt)\r
+{\r
+   LoginDialog *pDlg = new LoginDialog(prompt);\r
+\r
+   pDlg->CreateModule();\r
+\r
+   bool ret = pDlg->myOK;\r
+   password = pDlg->myPassword;\r
+   \r
+   delete pDlg;\r
+\r
+   return ret;\r
+}\r
+\r
+\r
+std::string LoginDialog::GetPassword()\r
+{\r
+   char szTxt[256];\r
+   SendDlgItemMessage(_hdlg, IDC_LOGIN_PASSWORD, WM_GETTEXT, sizeof(szTxt), (LPARAM)szTxt);\r
+   std::string strText = szTxt;\r
+   return strText;\r
+}\r
+\r
+void LoginDialog::RetrieveValues()\r
+{\r
+   myPassword = GetPassword();\r
+}\r
+\r
+\r
+BOOL IsWinNT()\r
+{\r
+   OSVERSIONINFO vi;\r
+   vi.dwOSVersionInfoSize = sizeof(vi);\r
+   if (GetVersionEx(&vi))\r
+   {\r
+      if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT)\r
+      {\r
+         return TRUE;\r
+      }\r
+   }\r
+   return FALSE;\r
+}\r
+\r
+HWND GetParentHwnd()\r
+{\r
+   if (IsWinNT())\r
+   {\r
+      return GetDesktopWindow();\r
+   }\r
+   else\r
+   {\r
+      return GetForegroundWindow();\r
+   }\r
+}\r