source: trunk/eraser/Eraser.DefaultPlugins/ErasureTargets/FolderErasureTargetConfigurer.cs @ 2228

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

Based on r2226, allow the file and folder erasure target configurers to detect a path (of their own type) and then accept it as their own. This allows the specification of paths on the command line without explicit handler requirements, so things like these work:

eraser addtask D:\test.txt D:\testing

And a file target and a directory target will be made, respectively, using default settings.

Also fixed an implementation error where the return code of the ProcessArgument? function was not properly set.

  • 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;
30
31using System.Text.RegularExpressions;
32
33using Eraser.Manager;
34using Eraser.Util;
35using System.IO;
36
37namespace Eraser.DefaultPlugins
38{
39    public partial class FolderErasureTargetConfigurer : UserControl, IErasureTargetConfigurer
40    {
41        public FolderErasureTargetConfigurer()
42        {
43            InitializeComponent();
44            Theming.ApplyTheme(this);
45        }
46
47        #region IConfigurer<ErasureTarget> Members
48
49        public void LoadFrom(ErasureTarget target)
50        {
51            FolderErasureTarget folder = target as FolderErasureTarget;
52            if (folder == null)
53                throw new ArgumentException("The provided erasure target type is not " +
54                    "supported by this configurer.");
55
56            folderPath.Text = folder.Path;
57            folderInclude.Text = folder.IncludeMask;
58            folderExclude.Text = folder.ExcludeMask;
59            folderDelete.Checked = folder.DeleteIfEmpty;
60        }
61
62        public bool SaveTo(ErasureTarget target)
63        {
64            FolderErasureTarget folder = target as FolderErasureTarget;
65            if (folder == null)
66                throw new ArgumentException("The provided erasure target type is not " +
67                    "supported by this configurer.");
68
69            if (folderPath.Text.Length == 0)
70            {
71                errorProvider.SetError(folderPath, S._("Invalid folder path"));
72                return false;
73            }
74
75            folder.Path = folderPath.Text;
76            folder.IncludeMask = folderInclude.Text;
77            folder.ExcludeMask = folderExclude.Text;
78            folder.DeleteIfEmpty = folderDelete.Checked;
79            return true;
80        }
81
82        #endregion
83
84        #region ICliConfigurer<ErasureTarget> Members
85
86        public void Help()
87        {
88            throw new NotImplementedException();
89        }
90
91        public bool ProcessArgument(string argument)
92        {
93            //The directory target, taking a list of + and - wildcard expressions.
94            Regex regex = new Regex("dir=(?<directoryName>.*)(?<directoryParams>(?<directoryExcludeMask>,-[^,]+)|(?<directoryIncludeMask>,\\+[^,]+)|(?<directoryDeleteIfEmpty>,deleteIfEmpty(=(?<directoryDeleteIfEmptyValue>true|false))?))*",
95                RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
96            Match match = regex.Match(argument);
97
98            string[] trueValues = new string[] { "yes", "true" };
99            if (match.Groups["directoryName"].Success)
100            {
101                folderPath.Text = match.Groups["directoryName"].Value;
102                if (!match.Groups["directoryDeleteIfEmpty"].Success)
103                    folderDelete.Checked = false;
104                else if (!match.Groups["directoryDeleteIfEmptyValue"].Success)
105                    folderDelete.Checked = true;
106                else
107                    folderDelete.Checked =
108                        trueValues.Contains(match.Groups["directoryDeleteIfEmptyValue"].Value);
109
110                if (match.Groups["directoryExcludeMask"].Success)
111                    folderExclude.Text += match.Groups["directoryExcludeMask"].Value.Remove(0, 2) + ' ';
112                if (match.Groups["directoryIncludeMask"].Success)
113                    folderInclude.Text += match.Groups["directoryIncludeMask"].Value.Remove(0, 2) + ' ';
114
115                return true;
116            }
117            else if (Directory.Exists(argument))
118            {
119                folderPath.Text = argument;
120                folderDelete.Checked = false;
121                folderInclude.Text = folderExclude.Text = string.Empty;
122                return true;
123            }
124
125            return false;
126        }
127
128        #endregion
129
130        private void folderBrowse_Click(object sender, EventArgs e)
131        {
132            try
133            {
134                folderDialog.SelectedPath = folderPath.Text;
135                if (folderDialog.ShowDialog() == DialogResult.OK)
136                    folderPath.Text = folderDialog.SelectedPath;
137            }
138            catch (NotSupportedException)
139            {
140                MessageBox.Show(this, S._("The path you selected is invalid."), S._("Eraser"),
141                    MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
142                    Localisation.IsRightToLeft(this) ?
143                        MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0);
144            }
145        }
146    }
147}
Note: See TracBrowser for help on using the repository browser.