Windows: Explorer Shell Set Unix Mode bits
[openafs.git] / src / WINNT / client_exp / PropFile.cpp
1 // PropFile.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "PropFile.h"
6
7 extern "C" {
8 #include <afs/param.h>
9 #include <afs/stds.h>
10 }
11
12 #include "WINNT\talocale.h"
13 #include "afs_shl_ext.h"
14 #include "set_afs_acl.h"
15 #include "clear_acl_dlg.h"
16 #include "add_acl_entry_dlg.h"
17 #include "copy_acl_dlg.h"
18 #include "make_mount_point_dlg.h"
19 #include "make_symbolic_link_dlg.h"
20 #include "gui2fs.h"
21 #include "msgs.h"
22
23
24 BOOL CPropFile::PropPageProc( HWND hwnd, UINT uMessage, WPARAM wParam, LPARAM lParam )
25 {
26     switch(uMessage)
27     {
28     case WM_INITDIALOG:
29         {
30             CPropFile * sheetpage = (CPropFile*) ((LPPROPSHEETPAGE) lParam)->lParam;
31             SetWindowLongPtr (hwnd, GWLP_USERDATA, (LONG_PTR) sheetpage);
32             sheetpage->SetHwnd(hwnd);
33             AfxSetResourceHandle(m_hInst);
34
35             CString sText;
36
37             if (filenames.GetCount() <= 0) {
38
39                 ShowWindow(GetDlgItem(m_hwnd, IDC_REMOVEMOUNTPOINT), SW_HIDE);
40                 ShowWindow(GetDlgItem(m_hwnd, IDC_SYMLINK_LABEL), SW_HIDE);
41                 ShowWindow(GetDlgItem(m_hwnd, IDC_MOUNTPOINT_LABEL), SW_HIDE);
42                 EnableUnixMode(FALSE);
43
44             }
45             else if(filenames.GetCount() > 1) {
46                 // multiple items selected
47                 LoadString(sText, IDS_PROP_MULTIPLEITEMS);
48                 SetDlgItemText(hwnd, IDC_PROP_TYPE, sText);
49
50                 ShowWindow(GetDlgItem(m_hwnd, IDC_REMOVEMOUNTPOINT), SW_HIDE);
51                 ShowWindow(GetDlgItem(m_hwnd, IDC_SYMLINK_LABEL), SW_HIDE);
52                 ShowWindow(GetDlgItem(m_hwnd, IDC_MOUNTPOINT_LABEL), SW_HIDE);
53                 EnableUnixMode(FALSE);
54
55                 sText = GetCellName(filenames.GetAt(0), FALSE);
56                 m_cellName = sText;
57                 SetDlgItemText(hwnd, IDC_PROP_CELL, sText);
58
59             } else {
60                 if (m_bIsMountpoint)
61                     LoadString(sText, IDS_PROP_TYPEMOUNTPOINT);
62                 else if (m_bIsSymlink)
63                     LoadString(sText, IDS_PROP_TYPESYMLINK);
64                 else if (m_bIsDir)
65                     LoadString(sText, IDS_PROP_TYPEDIRECTORY);
66                 else
67                     LoadString(sText, IDS_PROP_TYPEFILE);
68
69                 SetDlgItemText(hwnd, IDC_PROP_TYPE, sText);
70
71                 if (m_bIsMountpoint) {
72                     ShowWindow(GetDlgItem(m_hwnd, IDC_REMOVEMOUNTPOINT), SW_SHOW);
73                 }
74                 if (!m_bIsSymlink) {
75                     ShowWindow(GetDlgItem(m_hwnd, IDC_SYMLINK_LABEL), SW_HIDE);
76                 }
77                 if (!m_bIsMountpoint) {
78                     ShowWindow(GetDlgItem(m_hwnd, IDC_MOUNTPOINT_LABEL), SW_HIDE);
79                 }
80
81                 if (!m_bIsMountpoint && !m_bIsSymlink) {
82                     ShowWindow(GetDlgItem(m_hwnd, IDC_EDIT), SW_HIDE);
83                 }
84
85                 if (m_bIsSymlink) {
86                     ShowWindow(GetDlgItem(m_hwnd, IDC_REMOVESYMLINK), SW_SHOW);
87                 }
88
89                 CString user, group, other, suid;
90                 EnableUnixMode(TRUE);
91                 GetUnixModeBits(filenames.GetAt(0), user, group, other, suid);
92                 ShowUnixMode(user, group, other, suid);
93
94                 SetDlgItemText(hwnd, IDC_PROP_FILENAME, filenames.GetAt(0));
95                 if (!GetFID(filenames.GetAt(0), sText, TRUE))
96                     sText = _T("(unknown)");
97                 SetDlgItemText(hwnd, IDC_PROP_FID, sText);
98                 sText = GetOwner(filenames.GetAt(0));
99                 SetDlgItemText(hwnd, IDC_PROP_OWNER, sText);
100                 sText = GetGroup(filenames.GetAt(0));
101                 SetDlgItemText(hwnd, IDC_PROP_GROUP, sText);
102
103                 if (m_bIsMountpoint){
104                     sText = GetMountpoint(filenames.GetAt(0));
105                     sText = sText.Mid(sText.Find('\t')+1);
106                     SetDlgItemText(hwnd, IDC_PROP_SMINFO, sText);
107                 }
108                 if (m_bIsSymlink){
109                     sText = GetSymlink(filenames.GetAt(0));
110                     sText = sText.Mid(sText.Find('\t')+1);
111                     SetDlgItemText(hwnd, IDC_PROP_SMINFO, sText);
112                 }
113
114                 sText = GetCellName(filenames.GetAt(0));
115                 m_cellName = sText;
116                 SetDlgItemText(hwnd, IDC_PROP_CELL, sText);
117             }
118             return TRUE;
119         }
120         break;
121     case WM_NOTIFY:
122         {
123             LPNMHDR point = (LPNMHDR)lParam;
124             int code = point->code;
125             BOOL bRet = FALSE;
126             switch (code)
127             {
128             case PSN_APPLY:
129                 {
130                     CString user, group, other, suid;
131                     MakeUnixModeString(user, group, other, suid);
132                     SetUnixModeBits(filenames, user, group, other, suid);
133                     // Return PSNRET_NOERROR to allow the sheet to close if the user clicked OK.
134                     SetWindowLongPtr(m_hwnd, DWLP_MSGRESULT, PSNRET_NOERROR);
135                 }
136                 break;
137             }
138             SetWindowLongPtr(m_hwnd, DWLP_MSGRESULT, FALSE);
139             return bRet;
140         }
141         break;
142     case WM_COMMAND:
143         switch (HIWORD(wParam))
144         {
145         case BN_CLICKED:
146             switch (LOWORD(wParam))
147             {
148             case IDC_FLUSH:
149                 Flush(filenames);
150                 return TRUE;
151             case IDC_REMOVESYMLINK:
152                 {
153                     int nChoice = ShowMessageBox(IDS_REALLY_REMOVE_SYMLINK, MB_ICONQUESTION | MB_YESNO, IDS_REALLY_REMOVE_SYMLINK);
154                     if (nChoice == IDYES)
155                         RemoveSymlink(filenames.GetAt(0));
156                     return TRUE;
157                 }
158             case IDC_REMOVEMOUNTPOINT:
159                 {
160                     int nChoice = ShowMessageBox(IDS_REALLY_DEL_MOUNT_POINTS, MB_ICONQUESTION | MB_YESNO, IDS_REALLY_DEL_MOUNT_POINTS);
161                     if (nChoice == IDYES)
162                         RemoveMount(filenames);
163                     return TRUE;
164                 }
165             case IDC_EDIT:
166                 {
167                     if (m_bIsMountpoint){
168                         CMakeMountPointDlg dlg;
169                         dlg.SetDir(filenames.GetAt(0));
170                         dlg.SetCell(m_cellName);
171                         dlg.SetVol(m_volName);
172                         dlg.DoModal();
173                     }
174                     if (m_bIsSymlink){
175                         CMakeSymbolicLinkDlg dlg;
176                         CStringA msg(filenames.GetAt(0));
177                         int i;
178                         if ((i=msg.ReverseFind('\\'))>0)
179                             msg=msg.Left(i+1);
180                         else if ((i=msg.ReverseFind(':'))>0)
181                             msg=msg.Left(i+1)+"\\";
182                         dlg.Setbase(msg);
183                         dlg.m_strDir = filenames.GetAt(0);
184                         dlg.m_strName = GetSymlink(filenames.GetAt(0));
185                         dlg.DoModal();
186                     }
187                 }
188             case IDC_ATTR_USER_READ:
189             case IDC_ATTR_USER_WRITE:
190             case IDC_ATTR_USER_EXECUTE:
191             case IDC_ATTR_GROUP_READ:
192             case IDC_ATTR_GROUP_WRITE:
193             case IDC_ATTR_GROUP_EXECUTE:
194             case IDC_ATTR_OTHER_READ:
195             case IDC_ATTR_OTHER_WRITE:
196             case IDC_ATTR_OTHER_EXECUTE:
197                 SendMessage(GetParent(m_hwnd), PSM_CHANGED, (WPARAM)m_hwnd, 0);     // enable the "apply" button
198                 return TRUE;
199             }
200             break;
201         }
202         break;
203     }
204
205     return FALSE;
206 }
207
208 void CPropFile::ShowUnixMode(const CString& strUserRights, const CString& strGroupRights, const CString& strOtherRights, const CString& strSuidRights)
209 {
210     SendDlgItemMessage(m_hwnd, IDC_ATTR_USER_READ, BM_SETCHECK, (strUserRights.Find(_T("r")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
211     SendDlgItemMessage(m_hwnd, IDC_ATTR_USER_WRITE, BM_SETCHECK, (strUserRights.Find(_T("w")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
212     SendDlgItemMessage(m_hwnd, IDC_ATTR_USER_EXECUTE, BM_SETCHECK, (strUserRights.Find(_T("x")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
213
214     SendDlgItemMessage(m_hwnd, IDC_ATTR_GROUP_READ, BM_SETCHECK, (strGroupRights.Find(_T("r")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
215     SendDlgItemMessage(m_hwnd, IDC_ATTR_GROUP_WRITE, BM_SETCHECK, (strGroupRights.Find(_T("w")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
216     SendDlgItemMessage(m_hwnd, IDC_ATTR_GROUP_EXECUTE, BM_SETCHECK, (strGroupRights.Find(_T("x")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
217
218     SendDlgItemMessage(m_hwnd, IDC_ATTR_OTHER_READ, BM_SETCHECK, (strOtherRights.Find(_T("r")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
219     SendDlgItemMessage(m_hwnd, IDC_ATTR_OTHER_WRITE, BM_SETCHECK, (strOtherRights.Find(_T("w")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
220     SendDlgItemMessage(m_hwnd, IDC_ATTR_OTHER_EXECUTE, BM_SETCHECK, (strOtherRights.Find(_T("x")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
221
222     SendDlgItemMessage(m_hwnd, IDC_ATTR_SUID_UID, BM_SETCHECK, (strSuidRights.Find(_T("s")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
223     SendDlgItemMessage(m_hwnd, IDC_ATTR_SUID_GID, BM_SETCHECK, (strSuidRights.Find(_T("g")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
224     SendDlgItemMessage(m_hwnd, IDC_ATTR_SUID_VTX, BM_SETCHECK, (strSuidRights.Find(_T("v")) == -1) ? BST_UNCHECKED : BST_CHECKED, 0);
225 }
226
227 void CPropFile::EnableUnixMode(BOOL bEnable)
228 {
229     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_USER_READ), bEnable);
230     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_USER_WRITE), bEnable);
231     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_USER_EXECUTE), bEnable);
232
233     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_GROUP_READ), bEnable);
234     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_GROUP_WRITE), bEnable);
235     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_GROUP_EXECUTE), bEnable);
236
237     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_OTHER_READ), bEnable);
238     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_OTHER_WRITE), bEnable);
239     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_OTHER_EXECUTE), bEnable);
240
241     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_SUID_UID), bEnable);
242     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_SUID_GID), bEnable);
243     EnableWindow(GetDlgItem(m_hwnd, IDC_ATTR_SUID_VTX), bEnable);
244 }
245
246 void CPropFile::MakeUnixModeString(CString& userRights, CString& groupRights, CString& otherRights, CString& suidRights)
247 {
248     userRights.Empty();
249     groupRights.Empty();
250     otherRights.Empty();
251     suidRights.Empty();
252
253     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_USER_READ, BM_GETCHECK, 0,0) == BST_CHECKED)
254         userRights += _T("r");
255     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_USER_WRITE, BM_GETCHECK, 0,0) == BST_CHECKED)
256         userRights += _T("w");
257     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_USER_EXECUTE, BM_GETCHECK, 0,0) == BST_CHECKED)
258         userRights += _T("x");
259
260     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_GROUP_READ, BM_GETCHECK, 0,0) == BST_CHECKED)
261         groupRights += _T("r");
262     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_GROUP_WRITE, BM_GETCHECK, 0,0) == BST_CHECKED)
263         groupRights += _T("w");
264     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_GROUP_EXECUTE, BM_GETCHECK, 0,0) == BST_CHECKED)
265         groupRights += _T("x");
266
267     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_OTHER_READ, BM_GETCHECK, 0,0) == BST_CHECKED)
268         otherRights += _T("r");
269     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_OTHER_WRITE, BM_GETCHECK, 0,0) == BST_CHECKED)
270         otherRights += _T("w");
271     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_OTHER_EXECUTE, BM_GETCHECK, 0,0) == BST_CHECKED)
272         otherRights += _T("x");
273
274     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_SUID_UID, BM_GETCHECK, 0,0) == BST_CHECKED)
275         suidRights += _T("s");
276     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_SUID_GID, BM_GETCHECK, 0,0) == BST_CHECKED)
277         suidRights += _T("g");
278     if (SendDlgItemMessage(m_hwnd, IDC_ATTR_SUID_VTX, BM_GETCHECK, 0,0) == BST_CHECKED)
279         suidRights += _T("v");
280 }