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  *              Copyright 2023 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
7  */
8 
9 #include "precomp.h"
10 
11 class CZipPassword : public CDialogImpl<CZipPassword>
12 {
13 private:
14     CStringW m_Filename;
15     CStringA* m_pPassword;
16 public:
CZipPassword(PCWSTR filename,CStringA * Password)17     CZipPassword(PCWSTR filename, CStringA* Password)
18         :m_pPassword(Password)
19     {
20         if (filename != NULL)
21             m_Filename = filename;
22     }
23 
OnInitDialog(UINT nMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)24     LRESULT OnInitDialog(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
25     {
26         CenterWindow(GetParent());
27 
28         /* No filename, so this is the question before starting to extract */
29         if (m_Filename.IsEmpty())
30         {
31             CStringW message(MAKEINTRESOURCEW(IDS_PASSWORD_ZIP_TEXT));
32             ::SetDlgItemTextW(m_hWnd, IDC_MESSAGE, message);
33             ::ShowWindow(GetDlgItem(IDSKIP), SW_HIDE);
34         }
35         else
36         {
37             CStringW message;
38             message.FormatMessage(IDS_PASSWORD_FILE_TEXT, m_Filename.GetString());
39             ::SetDlgItemTextW(m_hWnd, IDC_MESSAGE, message);
40         }
41         return TRUE;
42     }
43 
OnButton(WORD wNotifyCode,WORD wID,HWND hWndCtl,BOOL & bHandled)44     LRESULT OnButton(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
45     {
46         if (wID == IDOK)
47         {
48             HWND item = GetDlgItem(IDC_PASSWORD);
49             int len = ::GetWindowTextLengthA(item);
50             len = ::GetDlgItemTextA(m_hWnd, IDC_PASSWORD, m_pPassword->GetBuffer(len+1), len+1);
51             m_pPassword->ReleaseBuffer(len);
52         }
53         EndDialog(wID);
54         return 0;
55     }
56 
57 public:
58     enum { IDD = IDD_PASSWORD };
59 
60     BEGIN_MSG_MAP(CZipPassword)
61         MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
62         COMMAND_ID_HANDLER(IDOK, OnButton)
63         COMMAND_ID_HANDLER(IDSKIP, OnButton)
64         COMMAND_ID_HANDLER(IDCANCEL, OnButton)
65     END_MSG_MAP()
66 };
67 
_CZipAskPassword(HWND hDlg,PCWSTR filename,CStringA & Password)68 eZipPasswordResponse _CZipAskPassword(HWND hDlg, PCWSTR filename, CStringA& Password)
69 {
70     if (filename)
71         filename = PathFindFileNameW(filename);
72     CZipPassword password(filename, &Password);
73     INT_PTR Result = password.DoModal(hDlg);
74     switch (Result)
75     {
76     case IDOK: return eAccept;
77     case IDSKIP: return eSkip;
78     default:
79     case IDCANCEL: return eAbort;
80     }
81 }
82