-
Notifications
You must be signed in to change notification settings - Fork 77
/
DlgEasEditor.cpp
503 lines (418 loc) · 15.2 KB
/
DlgEasEditor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*****************************************************************************/
/* DlgEasEditor.cpp Copyright (c) Ladislav Zezula 2008 */
/*---------------------------------------------------------------------------*/
/* Description : */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 25.07.08 1.00 Lad Created */
/*****************************************************************************/
#include "FileTest.h"
#include "resource.h"
//-----------------------------------------------------------------------------
// Local variables
static TListViewColumns Columns[] =
{
{IDS_EA_NAME, 120},
{IDS_EA_SIZE, 80},
{IDS_EA_VALUE, -1},
{0, 0}
};
//-----------------------------------------------------------------------------
// Local functions
static ULONG CopyEaItem(PFILE_FULL_EA_INFORMATION pTrg, PFILE_FULL_EA_INFORMATION pSrc, bool bSetNextOffset)
{
// Things checked by nt!IoCheckEaBufferValidity:
// 1) There must be zero after the EA name
// 2) NextEntryOffset must be aligned to DWORD boundary
ULONG EntryLength = FIELD_OFFSET(FILE_FULL_EA_INFORMATION, EaName) + pSrc->EaNameLength + sizeof(CHAR) + pSrc->EaValueLength;
// Copy the entire entry
memcpy(pTrg, pSrc, EntryLength);
pTrg->EaName[pTrg->EaNameLength] = 0;
pTrg->NextEntryOffset = 0;
// Set the offset of the next entry, if needed
if(bSetNextOffset)
pTrg->NextEntryOffset = ALIGN_TO_SIZE(EntryLength, sizeof(DWORD));
return EntryLength;
}
static BOOL UpdateDialogButtons(HWND hDlg)
{
HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
HWND hButton;
int nSelected = ListView_GetSelectedCount(hListView);
int nItems = ListView_GetItemCount(hListView);
int nItem = ListView_GetNextItem(hListView, -1, LVNI_SELECTED);
// "Insert will be always enabled"
hButton = GetDlgItem(hDlg, IDC_INSERT);
EnableWindow(hButton, TRUE);
// Enable/disable "Edit"
hButton = GetDlgItem(hDlg, IDC_EDIT);
EnableWindow(hButton, (nSelected == 1));
// Enable/disable "Delete"
hButton = GetDlgItem(hDlg, IDC_DELETE);
EnableWindow(hButton, (nSelected == 1));
// Enable/disable "Up"
hButton = GetDlgItem(hDlg, IDC_MOVE_UP);
EnableWindow(hButton, (nItem > 0));
// Enable/disable "Down"
hButton = GetDlgItem(hDlg, IDC_MOVE_DOWN);
EnableWindow(hButton, (0 <= nItem && nItem < (nItems - 1)));
return TRUE;
}
static int SetListViewEaEntry(
HWND hListView,
int nIndex,
PFILE_FULL_EA_INFORMATION pEaItem,
BOOL bInsertNew)
{
LVITEM lvi;
LPTSTR szEaName;
LPTSTR szEaValue;
LPTSTR szTemp;
PBYTE pbEaValue = (PBYTE)pEaItem->EaName + pEaItem->EaNameLength + 1;
TCHAR szEaSize[20];
size_t cchEaValue;
int i;
// -1 means insert to the end of the list
if(nIndex == -1)
nIndex = ListView_GetItemCount(hListView);
// Allocate buffer for EA name and convert the name
szEaName = new TCHAR[pEaItem->EaNameLength + 1];
for(i = 0; i < pEaItem->EaNameLength; i++)
szEaName[i] = pEaItem->EaName[i];
szEaName[i] = 0;
// Allocate buffer for EA value and convert the value
cchEaValue = (pEaItem->EaValueLength * 3) + 1;
szEaValue = szTemp = new TCHAR[cchEaValue];
if(szEaValue != NULL)
{
LPTSTR szEaValueEnd = szEaValue + cchEaValue;
for(i = 0; i < pEaItem->EaValueLength; i++)
StringCchPrintfEx(szTemp, (szEaValueEnd - szTemp), &szTemp, NULL, 0, _T("%02lX "), pbEaValue[i]);
szTemp[0] = 0;
// Set the main item
ZeroMemory(&lvi, sizeof(LVITEM));
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iItem = nIndex;
lvi.pszText = szEaName;
lvi.lParam = (LPARAM)pEaItem;
// Insert or set the item
if(bInsertNew)
nIndex = ListView_InsertItem(hListView, &lvi);
else
ListView_SetItem(hListView, &lvi);
// Set the sub item text
StringCchPrintf(szEaSize, _countof(szEaSize), _T("0x%04X"), pEaItem->EaValueLength);
ListView_SetItemText(hListView, nIndex, 1, szEaSize);
// Set the sub item text
ListView_SetItemText(hListView, nIndex, 2, szEaValue);
delete [] szEaValue;
}
delete [] szEaName;
return TRUE;
}
//-----------------------------------------------------------------------------
// Dialog handlers
static int OnInitDialog(HWND hDlg, LPARAM lParam)
{
TOpenPacket * pOP = (TOpenPacket *)lParam;
HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
// Save the user data
SetDialogData(hDlg, lParam);
CenterWindowToParent(hDlg);
// Initialize the listview
ListView_CreateColumns(hListView, Columns);
ListView_SetExtendedListViewStyle(hListView, LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
// Save the pointer to TFileTestData
if(lParam != 0)
ExtendedAttributesToListView(hDlg, pOP->pvFileEa);
// Initialize the listview
UpdateDialogButtons(hDlg);
return TRUE;
}
void MoveItem(HWND hDlg, BOOL bUp)
{
LVITEM lvi1;
LVITEM lvi2;
TCHAR szText1[MAX_PATH + 1] = _T("");
TCHAR szText2[MAX_PATH + 1] = _T("");
HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
int nItems = ListView_GetItemCount(hListView);
int nItem1 = 0;
int nItem2 = 0;
if(bUp == FALSE)
{
nItem1 = ListView_GetNextItem(hListView, -1, LVNI_FOCUSED);
nItem2 = nItem1 + 1;
if(nItem1 == nItems - 1)
return;
}
else
{
nItem1 = ListView_GetNextItem(hListView, -1, LVNI_FOCUSED) - 1;
nItem2 = nItem1 + 1;
if(nItem1 < 0)
return;
}
// Disable redrawing during item acrobatics
EnableRedraw(hListView, FALSE);
// Retrieve the both items
ZeroMemory(&lvi1, sizeof(LVITEM));
lvi1.mask = LVIF_IMAGE | LVIF_STATE | LVIF_TEXT | LVIF_PARAM;
lvi1.stateMask = (UINT)-1;
lvi1.iItem = nItem1;
lvi1.pszText = szText1;
lvi1.cchTextMax = _countof(szText1);
ListView_GetItem(hListView, &lvi1);
ZeroMemory(&lvi2, sizeof(LVITEM));
lvi2.mask = LVIF_IMAGE | LVIF_STATE | LVIF_TEXT | LVIF_PARAM;
lvi2.stateMask = (UINT)-1;
lvi2.iItem = nItem2;
lvi2.pszText = szText2;
lvi2.cchTextMax = _countof(szText2);
ListView_GetItem(hListView, &lvi2);
// Switch the main items
lvi2.iItem = nItem1;
ListView_SetItem(hListView, &lvi2);
lvi1.iItem = nItem2;
ListView_SetItem(hListView, &lvi1);
// Switch the texts for the subitems
ListView_GetItemText(hListView, nItem1, 1, szText1, _countof(szText1));
ListView_GetItemText(hListView, nItem2, 1, szText2, _countof(szText2));
ListView_SetItemText(hListView, nItem1, 1, szText2);
ListView_SetItemText(hListView, nItem2, 1, szText1);
// Enable redrawing and redraw the window
EnableRedraw(hListView);
}
static int OnInsertEa(HWND hDlg)
{
PFILE_FULL_EA_INFORMATION pEaItem = NULL;
HWND hListView;
// Call the dialog for creating new EA
if(EaEditorDialog(hDlg, &pEaItem) != IDOK)
return TRUE;
// Insert listview
hListView = GetDlgItem(hDlg, IDC_EA_LIST);
SetListViewEaEntry(hListView, -1, pEaItem, TRUE);
UpdateDialogButtons(hDlg);
return TRUE;
}
static int OnEditEa(HWND hDlg)
{
PFILE_FULL_EA_INFORMATION pEaItem = NULL;
HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
int nSelected = ListView_GetNextItem(hListView, -1, LVNI_SELECTED);
// If no item selected, do nothing
if(nSelected == -1)
return 0;
// Call the dialog for editing EA
pEaItem = (PFILE_FULL_EA_INFORMATION)ListView_GetItemParam(hListView, nSelected);
if(EaEditorDialog(hDlg, &pEaItem) != IDOK)
return TRUE;
SetListViewEaEntry(hListView, nSelected, pEaItem, FALSE);
UpdateDialogButtons(hDlg);
return TRUE;
}
static int OnDeleteEa(HWND hDlg)
{
HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
int nSelected = ListView_GetNextItem(hListView, -1, LVNI_FOCUSED);
// If no item selected, do nothing
if(nSelected != -1)
ListView_DeleteItem(hListView, nSelected);
UpdateDialogButtons(hDlg);
return TRUE;
}
static int OnDoubleClick(HWND hDlg)
{
HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
int nSelected = ListView_GetNextItem(hListView, -1, LVNI_SELECTED);
if(nSelected == -1)
OnInsertEa(hDlg);
else
OnEditEa(hDlg);
return TRUE;
}
static int OnLVKeyDown(HWND hDlg, NMLVKEYDOWN * pLVKeyDown)
{
switch(pLVKeyDown->wVKey)
{
case VK_INSERT:
return OnInsertEa(hDlg);
case VK_DELETE:
return OnDeleteEa(hDlg);
}
return FALSE;
}
static BOOL OnSaveDialog(HWND hDlg)
{
TOpenPacket * pOP = (TOpenPacket *)GetDialogData(hDlg);
if(pOP != NULL)
{
// Delete the existing EAs
pOP->Free();
// Create new extended attributes
ListViewToExtendedAttributes(hDlg, *pOP);
}
return TRUE;
}
static int OnDeleteItem(HWND /* hDlg */, LPNMLISTVIEW pNMListView)
{
PFILE_FULL_EA_INFORMATION pEaItem = (PFILE_FULL_EA_INFORMATION)pNMListView->lParam;
if(pEaItem != NULL)
delete [] pEaItem;
return TRUE;
}
static int OnCommand(HWND hDlg, UINT nNotify, UINT nIDCtrl)
{
if(nNotify == BN_CLICKED)
{
switch(nIDCtrl)
{
case IDC_MOVE_UP:
MoveItem(hDlg, TRUE);
return TRUE;
case IDC_MOVE_DOWN:
MoveItem(hDlg, FALSE);
return TRUE;
case IDC_INSERT:
OnInsertEa(hDlg);
return TRUE;
case IDC_EDIT:
OnEditEa(hDlg);
return TRUE;
case IDC_DELETE:
OnDeleteEa(hDlg);
return TRUE;
case IDOK:
OnSaveDialog(hDlg);
// No break here !!!
case IDCANCEL:
EndDialog(hDlg, nIDCtrl);
return TRUE;
}
}
return FALSE;
}
static int OnNotify(HWND hDlg, NMHDR * pNMHDR)
{
switch(pNMHDR->code)
{
case NM_DBLCLK:
if(pNMHDR->idFrom == IDC_EA_LIST)
return OnDoubleClick(hDlg);
break;
case LVN_KEYDOWN:
if(pNMHDR->idFrom == IDC_EA_LIST)
return OnLVKeyDown(hDlg, (NMLVKEYDOWN *)pNMHDR);
break;
case LVN_ITEMCHANGED:
if(pNMHDR->idFrom == IDC_EA_LIST)
return UpdateDialogButtons(hDlg);
break;
case LVN_DELETEITEM:
if(pNMHDR->idFrom == IDC_EA_LIST)
return OnDeleteItem(hDlg, (LPNMLISTVIEW)pNMHDR);
break;
}
return FALSE;
}
//-----------------------------------------------------------------------------
// Public functions
void ExtendedAttributesToListView(HWND hDlg, PFILE_FULL_EA_INFORMATION pFileEa)
{
PFILE_FULL_EA_INFORMATION pEaItemCopy;
PFILE_FULL_EA_INFORMATION pEaItem;
ULONG dwEaItemSize = 0;
HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
ListView_DeleteAllItems(hListView);
// Load the listview with the extended attributes
if(pFileEa != NULL)
{
pEaItem = pFileEa;
for(;;)
{
// Create the EA copy and insert it to the list
dwEaItemSize = GetEaEntrySize(pEaItem);
pEaItemCopy = (PFILE_FULL_EA_INFORMATION)(new char [dwEaItemSize]);
CopyMemory(pEaItemCopy, pEaItem, dwEaItemSize);
pEaItemCopy->NextEntryOffset = dwEaItemSize;
SetListViewEaEntry(hListView, -1, pEaItemCopy, TRUE);
// Move to the next item
if(pEaItem->NextEntryOffset == 0)
break;
pEaItem = (PFILE_FULL_EA_INFORMATION)((PBYTE)pEaItem + pEaItem->NextEntryOffset);
}
}
}
DWORD ListViewToExtendedAttributes(HWND hDlg, TOpenPacket & OpenPacket)
{
PFILE_FULL_EA_INFORMATION pFileEaPtr;
PFILE_FULL_EA_INFORMATION pEaItem;
PFILE_FULL_EA_INFORMATION pvFileEa = NULL;
DWORD cbFileEa = 0;
HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
int nItemCount = ListView_GetItemCount(hListView);
int nIndex;
// Calculate the buffer necessary to set the EA.
for(nIndex = 0; nIndex < nItemCount; nIndex++)
{
pEaItem = (PFILE_FULL_EA_INFORMATION)ListView_GetItemParam(hListView, nIndex);
if(pEaItem != NULL)
{
// The size of the previous EA item must be aligned to 4 bytes
cbFileEa = ALIGN_TO_SIZE(cbFileEa, sizeof(DWORD));
cbFileEa = cbFileEa + FIELD_OFFSET(FILE_FULL_EA_INFORMATION, EaName) + pEaItem->EaNameLength + 1 + pEaItem->EaValueLength;
}
}
// Allocate buffer for the complete EA list and copy the EAs to that buffer
pFileEaPtr = pvFileEa = (PFILE_FULL_EA_INFORMATION)HeapAlloc(g_hHeap, HEAP_ZERO_MEMORY, cbFileEa);
if(pvFileEa != NULL)
{
for(nIndex = 0; nIndex < nItemCount; nIndex++)
{
pEaItem = (PFILE_FULL_EA_INFORMATION)ListView_GetItemParam(hListView, nIndex);
if(pEaItem != NULL)
{
// Copy the EA item to the list. If this is the last item, we want NextEntryOffset to be zero
CopyEaItem(pFileEaPtr, pEaItem, (nIndex < (nItemCount - 1)));
// If this is the last item, we have to set zero as NextEntryOffset
pFileEaPtr = (PFILE_FULL_EA_INFORMATION)((LPBYTE)pFileEaPtr + pFileEaPtr->NextEntryOffset);
}
}
// Give the EAs tothe caller
OpenPacket.pvFileEa = pvFileEa;
OpenPacket.cbFileEa = cbFileEa;
return ERROR_SUCCESS;
}
else
{
OpenPacket.pvFileEa = NULL;
OpenPacket.cbFileEa = 0;
return ERROR_NOT_ENOUGH_MEMORY;
}
}
// Common dialog proc, shared by Page08Ea.cpp
// - Returns nonzero = message has been handled
// - Returns zero = message has NOT been handled
INT_PTR CALLBACK ExtendedAttributesEditorProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
return OnInitDialog(hDlg, lParam);
case WM_COMMAND:
return OnCommand(hDlg, HIWORD(wParam), LOWORD(wParam));
case WM_NOTIFY:
return OnNotify(hDlg, (NMHDR *)lParam);
}
return FALSE;
}
INT_PTR ExtendedAtributesEditorDialog(HWND hParent, TOpenPacket * pOP)
{
return DialogBoxParam(g_hInst,
MAKEINTRESOURCE(IDD_EAS_EDITOR),
hParent,
ExtendedAttributesEditorProc,
(LPARAM)pOP);
}