source: trunk/eraser/Eraser.DefaultPlugins/ErasureTargets/SecureMoveErasureTargetConfigurer.cs @ 2230

Revision 2230, 4.4 KB checked in by lowjoel, 4 years ago (diff)

Implemented secure move from the command line: it can be invoked using the move=<source>|<target> notation, where the move command is optional (pipes are fine)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using System.IO;
31using System.Text.RegularExpressions;
32
33using Eraser.Manager;
34using Eraser.Util;
35
36namespace Eraser.DefaultPlugins
37{
38    public partial class SecureMoveErasureTargetConfigurer : UserControl, IErasureTargetConfigurer
39    {
40        public SecureMoveErasureTargetConfigurer()
41        {
42            InitializeComponent();
43            Theming.ApplyTheme(this);
44        }
45
46        #region IConfigurer<ErasureTarget> Members
47
48        public void LoadFrom(ErasureTarget target)
49        {
50            SecureMoveErasureTarget secureMove = target as SecureMoveErasureTarget;
51            if (secureMove == null)
52                throw new ArgumentException("The provided erasure target type is not " +
53                    "supported by this configurer.");
54
55            fromTxt.Text = secureMove.Path;
56            toTxt.Text = secureMove.Destination;
57
58            moveFolderRadio.Checked =
59                File.Exists(secureMove.Path) || Directory.Exists(secureMove.Path) &&
60                (File.GetAttributes(secureMove.Path) & FileAttributes.Directory) != 0;
61        }
62
63        public bool SaveTo(ErasureTarget target)
64        {
65            SecureMoveErasureTarget secureMove = target as SecureMoveErasureTarget;
66            if (secureMove == null)
67                throw new ArgumentException("The provided erasure target type is not " +
68                    "supported by this configurer.");
69
70            secureMove.Path = fromTxt.Text;
71            secureMove.Destination = toTxt.Text;
72            return true;
73        }
74
75        #endregion
76
77        #region ICliConfigurer<ErasureTarget> Members
78
79        public void Help()
80        {
81            throw new NotImplementedException();
82        }
83
84        public bool ProcessArgument(string argument)
85        {
86            //The secure move source and target, which are separated by a pipe.
87            Regex regex = new Regex("(move=)?(?<source>.*)\\|(?<target>.*)",
88                RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
89            Match match = regex.Match(argument);
90
91            if (match.Groups["source"].Success && match.Groups["target"].Success)
92            {
93                //Get the source and destination paths
94                fromTxt.Text = match.Groups["source"].Value;
95                toTxt.Text = match.Groups["target"].Value;
96
97                //Check the folder radio button if the source is a folder.
98                moveFileRadio.Checked = !(
99                    moveFolderRadio.Checked = Directory.Exists(fromTxt.Text));
100                return true;
101            }
102
103            return false;
104        }
105
106        #endregion
107
108        private void fromSelectButton_Click(object sender, EventArgs e)
109        {
110            if (moveFolderRadio.Checked)
111                fromTxt.Text = SelectFolder(fromTxt.Text, S._("Select the Source folder"));
112            else
113                fromTxt.Text = SelectFile(fromTxt.Text, S._("Select the Source file"));
114        }
115
116        private void toSelectButton_Click(object sender, EventArgs e)
117        {
118            if (moveFolderRadio.Checked)
119                toTxt.Text = SelectFolder(toTxt.Text, S._("Move Source folder to:"));
120            else
121                toTxt.Text = SaveFile(toTxt.Text, S._("Save Source file to"));
122        }
123
124        private string SelectFile(string currentPath, string description)
125        {
126            openFileDialog.FileName = currentPath;
127            openFileDialog.Title = description;
128            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
129            {
130                return openFileDialog.FileName;
131            }
132
133            return string.Empty;
134        }
135
136        private string SaveFile(string currentPath, string description)
137        {
138            saveFileDialog.FileName = currentPath;
139            saveFileDialog.Title = description;
140            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
141            {
142                return saveFileDialog.FileName;
143            }
144
145            return string.Empty;
146        }
147
148        private string SelectFolder(string currentPath, string description)
149        {
150            folderDialog.SelectedPath = currentPath;
151            folderDialog.Description = description;
152            if (folderDialog.ShowDialog(this) == DialogResult.OK)
153            {
154                return folderDialog.SelectedPath;
155            }
156
157            return string.Empty;
158        }
159    }
160}
Note: See TracBrowser for help on using the repository browser.