| 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 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.ComponentModel; |
|---|
| 25 | using System.Drawing; |
|---|
| 26 | using System.Data; |
|---|
| 27 | using System.Linq; |
|---|
| 28 | using System.Text; |
|---|
| 29 | using System.Windows.Forms; |
|---|
| 30 | |
|---|
| 31 | using System.IO; |
|---|
| 32 | using System.Text.RegularExpressions; |
|---|
| 33 | |
|---|
| 34 | using Eraser.Util; |
|---|
| 35 | using Eraser.Plugins; |
|---|
| 36 | using Eraser.Plugins.ExtensionPoints; |
|---|
| 37 | |
|---|
| 38 | namespace Eraser.DefaultPlugins |
|---|
| 39 | { |
|---|
| 40 | public partial class FolderErasureTargetConfigurer : UserControl, |
|---|
| 41 | IErasureTargetConfigurer, IDragAndDropConfigurerFactory<IErasureTarget> |
|---|
| 42 | { |
|---|
| 43 | public FolderErasureTargetConfigurer() |
|---|
| 44 | { |
|---|
| 45 | InitializeComponent(); |
|---|
| 46 | Theming.ApplyTheme(this); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | #region IConfigurer<ErasureTarget> Members |
|---|
| 50 | |
|---|
| 51 | public void LoadFrom(IErasureTarget target) |
|---|
| 52 | { |
|---|
| 53 | FolderErasureTarget folder = target as FolderErasureTarget; |
|---|
| 54 | if (folder == null) |
|---|
| 55 | throw new ArgumentException("The provided erasure target type is not " + |
|---|
| 56 | "supported by this configurer."); |
|---|
| 57 | |
|---|
| 58 | folderPath.Text = folder.Path; |
|---|
| 59 | folderInclude.Text = folder.IncludeMask; |
|---|
| 60 | folderExclude.Text = folder.ExcludeMask; |
|---|
| 61 | folderDelete.Checked = folder.DeleteIfEmpty; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | public bool SaveTo(IErasureTarget target) |
|---|
| 65 | { |
|---|
| 66 | FolderErasureTarget folder = target as FolderErasureTarget; |
|---|
| 67 | if (folder == null) |
|---|
| 68 | throw new ArgumentException("The provided erasure target type is not " + |
|---|
| 69 | "supported by this configurer."); |
|---|
| 70 | |
|---|
| 71 | if (folderPath.Text.Length == 0) |
|---|
| 72 | { |
|---|
| 73 | errorProvider.SetError(folderPath, S._("Invalid folder path")); |
|---|
| 74 | return false; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | folder.Path = folderPath.Text; |
|---|
| 78 | folder.IncludeMask = folderInclude.Text; |
|---|
| 79 | folder.ExcludeMask = folderExclude.Text; |
|---|
| 80 | folder.DeleteIfEmpty = folderDelete.Checked; |
|---|
| 81 | return true; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | #endregion |
|---|
| 85 | |
|---|
| 86 | #region ICliConfigurer<ErasureTarget> Members |
|---|
| 87 | |
|---|
| 88 | public string Help() |
|---|
| 89 | { |
|---|
| 90 | return S._(@"dir Erases files and folders in the directory |
|---|
| 91 | arguments: dir=<directory>[,-excludeMask][,+includeMask][,deleteIfEmpty] |
|---|
| 92 | excludeMask A wildcard expression for files and folders to |
|---|
| 93 | exclude. |
|---|
| 94 | includeMask A wildcard expression for files and folders to |
|---|
| 95 | include. |
|---|
| 96 | The include mask is applied before the exclude mask. |
|---|
| 97 | deleteIfEmpty Deletes the folder at the end of the erasure if it is |
|---|
| 98 | empty."); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | public bool ProcessArgument(string argument) |
|---|
| 102 | { |
|---|
| 103 | //The directory target, taking a list of + and - wildcard expressions. |
|---|
| 104 | Regex regex = new Regex("dir=(?<directoryName>.*)(?<directoryParams>(?<directoryExcludeMask>,-[^,]+)|(?<directoryIncludeMask>,\\+[^,]+)|(?<directoryDeleteIfEmpty>,deleteIfEmpty(=(?<directoryDeleteIfEmptyValue>true|false))?))*", |
|---|
| 105 | RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft); |
|---|
| 106 | Match match = regex.Match(argument); |
|---|
| 107 | |
|---|
| 108 | string[] trueValues = new string[] { "yes", "true" }; |
|---|
| 109 | if (match.Groups["directoryName"].Success) |
|---|
| 110 | { |
|---|
| 111 | folderPath.Text = match.Groups["directoryName"].Value; |
|---|
| 112 | if (!match.Groups["directoryDeleteIfEmpty"].Success) |
|---|
| 113 | folderDelete.Checked = false; |
|---|
| 114 | else if (!match.Groups["directoryDeleteIfEmptyValue"].Success) |
|---|
| 115 | folderDelete.Checked = true; |
|---|
| 116 | else |
|---|
| 117 | folderDelete.Checked = |
|---|
| 118 | trueValues.Contains(match.Groups["directoryDeleteIfEmptyValue"].Value); |
|---|
| 119 | |
|---|
| 120 | if (match.Groups["directoryExcludeMask"].Success) |
|---|
| 121 | folderExclude.Text += match.Groups["directoryExcludeMask"].Value.Remove(0, 2) + ' '; |
|---|
| 122 | if (match.Groups["directoryIncludeMask"].Success) |
|---|
| 123 | folderInclude.Text += match.Groups["directoryIncludeMask"].Value.Remove(0, 2) + ' '; |
|---|
| 124 | |
|---|
| 125 | return true; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | try |
|---|
| 129 | { |
|---|
| 130 | if (Directory.Exists(argument)) |
|---|
| 131 | { |
|---|
| 132 | folderPath.Text = argument; |
|---|
| 133 | folderDelete.Checked = false; |
|---|
| 134 | folderInclude.Text = folderExclude.Text = string.Empty; |
|---|
| 135 | return true; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | catch (NotSupportedException) |
|---|
| 139 | { |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | return false; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | #endregion |
|---|
| 146 | |
|---|
| 147 | #region IDragAndDropConfigurer<IErasureTarget> Members |
|---|
| 148 | |
|---|
| 149 | public ICollection<IErasureTarget> ProcessArgument(DragEventArgs e) |
|---|
| 150 | { |
|---|
| 151 | List<string> files = e.Data.GetDataPresent(DataFormats.FileDrop) ? |
|---|
| 152 | new List<string>((string[])e.Data.GetData(DataFormats.FileDrop, false)) : |
|---|
| 153 | new List<string>(); |
|---|
| 154 | |
|---|
| 155 | List<IErasureTarget> result = new List<IErasureTarget>(); |
|---|
| 156 | foreach (string file in files) |
|---|
| 157 | { |
|---|
| 158 | if (File.Exists(file)) |
|---|
| 159 | { |
|---|
| 160 | FileErasureTarget target = new FileErasureTarget(); |
|---|
| 161 | target.Path = file; |
|---|
| 162 | result.Add(target); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | return result; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | #endregion |
|---|
| 170 | |
|---|
| 171 | private void folderBrowse_Click(object sender, EventArgs e) |
|---|
| 172 | { |
|---|
| 173 | try |
|---|
| 174 | { |
|---|
| 175 | folderDialog.SelectedPath = folderPath.Text; |
|---|
| 176 | if (folderDialog.ShowDialog() == DialogResult.OK) |
|---|
| 177 | folderPath.Text = folderDialog.SelectedPath; |
|---|
| 178 | } |
|---|
| 179 | catch (NotSupportedException) |
|---|
| 180 | { |
|---|
| 181 | MessageBox.Show(this, S._("The path you selected is invalid."), S._("Eraser"), |
|---|
| 182 | MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, |
|---|
| 183 | Localisation.IsRightToLeft(this) ? |
|---|
| 184 | MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | } |
|---|