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

Revision 2044, 4.1 KB checked in by lowjoel, 4 years ago (diff)

Implement the ICliConfigurer interface to allow configuration of objects from the command line, which helps to solve the compile problems with the change of erasure targets to plugins.

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