-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanelCommandFileAttributesEdit.cs
More file actions
225 lines (205 loc) · 9.73 KB
/
Copy pathPanelCommandFileAttributesEdit.cs
File metadata and controls
225 lines (205 loc) · 9.73 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using netCommander.FileSystemEx;
namespace netCommander
{
class PanelCommandFileAttributesEdit : PanelCommandBase
{
public PanelCommandFileAttributesEdit()
{
ToolStripMenuItem menu = new ToolStripMenuItem();
menu.ShortcutKeys = Keys.Control | Keys.A;
menu.ShowShortcutKeys = true;
menu.Text = "File attributes";
CommandMenu = menu;
}
protected override void internal_command_proc()
{
QueryPanelInfoEventArgs e = new QueryPanelInfoEventArgs();
OnQueryCurrentPanel(e);
if (e.FocusedIndex == -1)
{
return;
}
bool group_mode = false;
int[] sel_indices = e.SelectedIndices;
//we not need cache selection
//becouse e.ItemsCollection must not change (DirectoryList not sort on attributes)
group_mode = (sel_indices.Length > 1);
DirectoryList dl=(DirectoryList)e.ItemCollection;
WIN32_FIND_DATA f_data = new WIN32_FIND_DATA();
string target_file = string.Empty;
if (!group_mode)
{
if (sel_indices.Length == 0)
{
target_file = Path.Combine(dl.DirectoryPath, dl.GetItemDisplayNameLong(e.FocusedIndex));
}
else
{
target_file = Path.Combine(dl.DirectoryPath, dl.GetItemDisplayNameLong(sel_indices[0]));
}
if (target_file.EndsWith(".."))
{
target_file = dl.DirectoryPath;
}
if (!Wrapper.GetFileInfo(target_file, ref f_data))
{
return;
}
}
FileAttributesEditDialog dialog = new FileAttributesEditDialog();
if (!group_mode)
{
dialog.buttonClear.Enabled = false;
set_attributes_to_dialog(dialog, f_data.dwFileAttributes);
dialog.Text = String.Format("Attributes of [{0}]", target_file);
}
else
{
dialog.buttonClear.Enabled = true;
dialog.buttonOK.Text = "Add";
dialog.Text = string.Format("Set or clear attributes [{0} entries]", sel_indices.Length);
}
set_readonly_attributes(dialog);
DialogResult d_res = dialog.ShowDialog();
switch (d_res)
{
case DialogResult.OK:
//group mode = add attributes
//single mode = set attributes
if (!group_mode)
{
try
{
set_attributes_file(target_file, get_attributes_from_dialog(dialog));
}
catch (Exception ex)
{
Messages.ShowException(ex);
}
}
else
{
//group mode
FileAttributes add_fa = get_attributes_from_dialog(dialog);
foreach(int one_index in sel_indices)
{
target_file = Path.Combine(dl.DirectoryPath, dl.GetItemDisplayNameLong(one_index));
try
{
add_attributes_file(target_file, add_fa);
OnItemProcessDone(new ItemEventArs(one_index));
}
catch (Exception ex)
{
Messages.ShowException(ex);
}
}
}
break;
case DialogResult.Yes:
//group mode only = clear attributes
FileAttributes clear_fa = get_attributes_from_dialog(dialog);
foreach (int one_index in sel_indices)
{
target_file = Path.Combine(dl.DirectoryPath, dl.GetItemDisplayNameLong(one_index));
try
{
clear_attributes_file(target_file, clear_fa);
OnItemProcessDone(new ItemEventArs(one_index));
}
catch (Exception ex)
{
Messages.ShowException(ex);
}
}
break;
}
}
private void clear_attributes_file(string file_name, FileAttributes fa)
{
FileSystemInfo fsi = IOhelper.GetFileSystemInfo(file_name);
if (fsi == null)
{
throw new ApplicationException(string.Format("File '{0}' not found"));
}
FileAttributes existing_fa = fsi.Attributes;
int existing_fa_int = (int)existing_fa;
int needed_fa_int;
int fa_int = (int)fa;
needed_fa_int = ~((~existing_fa_int) | fa_int);
FileAttributes needed_fa = (FileAttributes)needed_fa_int;
fsi.Attributes = needed_fa;
}
private void add_attributes_file(string file_name, FileAttributes fa)
{
FileSystemInfo fsi = IOhelper.GetFileSystemInfo(file_name);
if (fsi == null)
{
throw new ApplicationException(string.Format("File '{0}' not found"));
}
FileAttributes existing_fa = fsi.Attributes;
FileAttributes needed_fa = fa | existing_fa;
fsi.Attributes = needed_fa;
}
private void set_attributes_file(string file_name, FileAttributes fa)
{
FileSystemInfo fsi = IOhelper.GetFileSystemInfo(file_name);
if (fsi == null)
{
throw new ApplicationException(string.Format("File '{0}' not found.", file_name));
}
fsi.Attributes = fa;
}
private void set_readonly_attributes(FileAttributesEditDialog dial)
{
dial.checkBoxCompressed.Enabled = false;
dial.checkBoxDirectory.Enabled = false;
dial.checkBoxEncrypted.Enabled = false;
dial.checkBoxReparsePoint.Enabled = false;
dial.checkBoxSparseFile.Enabled = false;
}
private void set_attributes_to_dialog(FileAttributesEditDialog dial, FileAttributes fa)
{
dial.checkBoxArchive.Checked = ((fa & FileAttributes.Archive) == FileAttributes.Archive);
dial.checkBoxCompressed.Checked = ((fa & FileAttributes.Compressed) == FileAttributes.Compressed);
dial.checkBoxDirectory.Checked = ((fa & FileAttributes.Directory) == FileAttributes.Directory);
dial.checkBoxEncrypted.Checked = ((fa & FileAttributes.Encrypted) == FileAttributes.Encrypted);
dial.checkBoxHidden.Checked = ((fa & FileAttributes.Hidden) == FileAttributes.Hidden);
dial.checkBoxNormal.Checked = ((fa & FileAttributes.Normal) == FileAttributes.Normal);
dial.checkBoxNotcontentIndexed.Checked = ((fa & FileAttributes.NotContentIndexed) == FileAttributes.NotContentIndexed);
dial.checkBoxOffline.Checked = ((fa & FileAttributes.Offline) == FileAttributes.Offline);
dial.checkBoxReadonly.Checked = ((fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
dial.checkBoxReparsePoint.Checked = ((fa & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint);
dial.checkBoxSparseFile.Checked = ((fa & FileAttributes.SparseFile) == FileAttributes.SparseFile);
dial.checkBoxSystem.Checked = ((fa & FileAttributes.System) == FileAttributes.System);
dial.checkBoxTemporary.Checked = ((fa & FileAttributes.Temporary) == FileAttributes.Temporary);
}
private FileAttributes get_attributes_from_dialog(FileAttributesEditDialog dial)
{
FileAttributes ret = 0;
//if (dial.checkBoxArchive.Checked)
//{
// ret = ret | FileAttributes.Archive;
//}
ret = dial.checkBoxArchive.Checked ? ret | FileAttributes.Archive : ret;
ret = dial.checkBoxCompressed.Checked ? ret | FileAttributes.Compressed : ret;
ret = dial.checkBoxDirectory.Checked ? ret | FileAttributes.Directory : ret;
ret = dial.checkBoxEncrypted.Checked ? ret | FileAttributes.Encrypted : ret;
ret = dial.checkBoxHidden.Checked ? ret | FileAttributes.Hidden : ret;
ret = dial.checkBoxNormal.Checked ? ret | FileAttributes.Normal : ret;
ret = dial.checkBoxNotcontentIndexed.Checked ? ret | FileAttributes.NotContentIndexed : ret;
ret = dial.checkBoxOffline.Checked ? ret | FileAttributes.Offline : ret;
ret = dial.checkBoxReadonly.Checked ? ret | FileAttributes.ReadOnly : ret;
ret = dial.checkBoxReparsePoint.Checked ? ret | FileAttributes.ReparsePoint : ret;
ret = dial.checkBoxSparseFile.Checked ? ret | FileAttributes.SparseFile : ret;
ret = dial.checkBoxSystem.Checked ? ret | FileAttributes.System : ret;
ret = dial.checkBoxTemporary.Checked ? ret | FileAttributes.Temporary : ret;
return ret;
}
}
}