1 /*
2  * PROJECT:     ReactOS Zip Shell Extension
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Ask the user for a password
5  * COPYRIGHT:   Copyright 2019 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
10 class CZipPassword : public CDialogImpl<CZipPassword>
11 {
12 private:
13     CStringA m_Filename;
14     CStringA* m_pPassword;
15 public:
16     CZipPassword(const char* filename, CStringA* Password)
17         :m_pPassword(Password)
18     {
19         if (filename != NULL)
20             m_Filename = filename;
21     }
22 
23     LRESULT OnInitDialog(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
24     {
25         CenterWindow(GetParent());
26 
27         /* No filename, so this is the question before starting to extract */
28         if (m_Filename.IsEmpty())
29         {
30             CStringA message(MAKEINTRESOURCE(IDS_PASSWORD_ZIP_TEXT));
31             ::SetDlgItemTextA(m_hWnd, IDC_MESSAGE, message);
32             ::ShowWindow(GetDlgItem(IDSKIP), SW_HIDE);
33         }
34         else
35         {
36             CStringA message;
37             message.FormatMessage(IDS_PASSWORD_FILE_TEXT, m_Filename.GetString());
38             ::SetDlgItemTextA(m_hWnd, IDC_MESSAGE, message);
39         }
40         return TRUE;
41     }
42 
43     LRESULT OnButton(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
44     {
45         if (wID == IDOK)
46         {
47             HWND item = GetDlgItem(IDC_PASSWORD);
48             int len = ::GetWindowTextLengthA(item);
49             len = ::GetDlgItemTextA(m_hWnd, IDC_PASSWORD, m_pPassword->GetBuffer(len+1), len+1);
50             m_pPassword->ReleaseBuffer(len);
51         }
52         EndDialog(wID);
53         return 0;
54     }
55 
56 public:
57     enum { IDD = IDD_PASSWORD };
58 
59     BEGIN_MSG_MAP(CZipPassword)
60         MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
61         COMMAND_ID_HANDLER(IDOK, OnButton)
62         COMMAND_ID_HANDLER(IDSKIP, OnButton)
63         COMMAND_ID_HANDLER(IDCANCEL, OnButton)
64     END_MSG_MAP()
65 };
66 
67 eZipPasswordResponse _CZipAskPassword(HWND hDlg, const char* filename, CStringA& Password)
68 {
69     if (filename)
70         filename = PathFindFileNameA(filename);
71     CZipPassword password(filename, &Password);
72     INT_PTR Result = password.DoModal(hDlg);
73     switch (Result)
74     {
75     case IDOK: return eAccept;
76     case IDSKIP: return eSkip;
77     default:
78     case IDCANCEL: return eAbort;
79     }
80 }
81