Changing Remote File Permissions - Part 2
Page 1 Page 2 Page 3
The following code list is only a portion of the FormPermissions.cs file. You can view the complete source code in the download for this article.
We'll create a control array to hold our permission selections on the permissions form. Note that this is a very simplified means to work around no longer having control arrays built into .NET. You could dynamically create the checkbox controls using a control class, but for the purposes of this article a control array will suffice.
public class FormPermissions : System.Windows.Forms.Form {
/*
* CODE REMOVED FOR CLARITY
*/
// create our control array
public CheckBox[] cbPerms = new CheckBox[9];
public FormPermissions() {
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
// define each item in array
cbPerms[0] = this.owner_read;
cbPerms[1] = this.owner_write;
cbPerms[2] = this.owner_execute;
cbPerms[3] = this.group_read;
cbPerms[4] = this.group_write;
cbPerms[5] = this.group_execute;
cbPerms[6] = this.other_read;
cbPerms[7] = this.other_write;
cbPerms[8] = this.other_execute;
}
The following code segment handles the modal mode for our permissions form.
public string ShowModal(SortedList remoteFiles) {
string retVal = "";
ParsePermissions(remoteFiles);
if (this.ShowDialog() == DialogResult.OK) {
retVal = textBox_permissonMode.Text.Trim();
}
return retVal;
}
private void buttonApply_Click(object sender, System.EventArgs e) {
this.Close();
}
private void buttonCancel_Click(object sender, System.EventArgs e) {
this.Close();
}
The ParsePermissions method does all the work converting the existing file permissions to use with our CheckBox control array. Ensure that each checkbox has its Tag property assigned the values shown in the comment.
private void ParsePermissions(SortedList remoteFiles) {
/* We'll use the following CheckBox.Tag values to calc permissions
* 1st Triplet: R=400, W=200, X=100
* 2nd Triplet: R= 40, W= 20, X= 10
* 3rd Triplet: R= 4, W= 2, X= 1
* If more than one file is selected, set checkbox and gray out.
*/
int counter = 0;
int newPerm = 0;
IEnumerator erf = remoteFiles.GetEnumerator();
foreach (DictionaryEntry dPerms in remoteFiles) {
counter++;
string file = dPerms.Key.ToString();
string perm = dPerms.Value.ToString();
for (int i = 0; i < 9; i++) {
if (perm.Substring(i, 1).ToLower() != "-") {
if (counter > 1) {
cbPerms[i].ThreeState = true;
cbPerms[i].CheckState = CheckState.Indeterminate;
}else{
newPerm += Convert.ToInt16(cbPerms[i].Tag);
}
cbPerms[i].Checked = true;
}else{
if (counter > 1) {
cbPerms[i].ThreeState = true;
cbPerms[i].CheckState = CheckState.Indeterminate;
}
cbPerms[i].Checked = false;
}
}
}
textBox_permissonMode.Text = newPerm.ToString();
}
You will need to change the Click Event delegate for each CheckBox to use the following event handler. You do this by changing the delegates within the region named Windows Form Designer generated code manually.
// CODE REMOVED FOR CLARITY
// change each checkbox click event handler
this.owner_read.Click += new System.EventHandler(this.Permissions_Click);
// CODE REMOVED FOR CLARITY
private void Permissions_Click(object sender, System.EventArgs e) {
CheckBox cb = (CheckBox) sender;
int c = Convert.ToInt16(cb.Tag);
int cp = Convert.ToInt16(textBox_permissonMode.Text);
switch (cb.CheckState) {
case CheckState.Checked:
cp += c;
break;
case CheckState.Unchecked:
cp -= c;
break;
case CheckState.Indeterminate:
break;
}
textBox_permissonMode.Text = cp.ToString();
}
The Permissions_Click event handler simply increments or decrements the permission mode textbox value by the checkbox tag value depending on whether the checkbox is checked or un-checked. It is the value of the permission mode textbox that is used by the IssueCommand method in the permissions option event handler in FormMain.cs.
Compile and run the project, then connect to a Unix server. Select one or more files in the remote files list view and right-click to display the popup menu. Select the permissions option to display the permissions form. When you click Accept, your permission settings are applied to the selected files.
As you can see from the status window, we issue the chmod command with the permissions we selected on the permissions form.
The original example application is provided in the free evaluation version of Secure FTP Factory for .NET.
Page 1 Page 2 Page 3
|