| 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.ExtensionPoints; |
|---|
| 36 | |
|---|
| 37 | namespace 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 string Help() |
|---|
| 87 | { |
|---|
| 88 | return S._(@"dir Erases files and folders in the directory |
|---|
| 89 | arguments: dir=<directory>[,-excludeMask][,+includeMask][,deleteIfEmpty] |
|---|
| 90 | excludeMask A wildcard expression for files and folders to |
|---|
| 91 | exclude. |
|---|
| 92 | includeMask A wildcard expression for files and folders to |
|---|
| 93 | include. |
|---|
| 94 | The include mask is applied before the exclude mask. |
|---|
| 95 | deleteIfEmpty Deletes the folder at the end of the erasure if it is |
|---|
| 96 | empty."); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public bool ProcessArgument(string argument) |
|---|
| 100 | { |
|---|
| 101 | //The directory target, taking a list of + and - wildcard expressions. |
|---|
| 102 | Regex regex = new Regex("dir=(?<directoryName>.*)(?<directoryParams>(?<directoryExcludeMask>,-[^,]+)|(?<directoryIncludeMask>,\\+[^,]+)|(?<directoryDeleteIfEmpty>,deleteIfEmpty(=(?<directoryDeleteIfEmptyValue>true|false))?))*", |
|---|
| 103 | RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.RightToLeft); |
|---|
| 104 | Match match = regex.Match(argument); |
|---|
| 105 | |
|---|
| 106 | string[] trueValues = new string[] { "yes", "true" }; |
|---|
| 107 | if (match.Groups["directoryName"].Success) |
|---|
| 108 | { |
|---|
| 109 | folderPath.Text = match.Groups["directoryName"].Value; |
|---|
| 110 | if (!match.Groups["directoryDeleteIfEmpty"].Success) |
|---|
| 111 | folderDelete.Checked = false; |
|---|
| 112 | else if (!match.Groups["directoryDeleteIfEmptyValue"].Success) |
|---|
| 113 | folderDelete.Checked = true; |
|---|
| 114 | else |
|---|
| 115 | folderDelete.Checked = |
|---|
| 116 | trueValues.Contains(match.Groups["directoryDeleteIfEmptyValue"].Value); |
|---|
| 117 | |
|---|
| 118 | if (match.Groups["directoryExcludeMask"].Success) |
|---|
| 119 | folderExclude.Text += match.Groups["directoryExcludeMask"].Value.Remove(0, 2) + ' '; |
|---|
| 120 | if (match.Groups["directoryIncludeMask"].Success) |
|---|
| 121 | folderInclude.Text += match.Groups["directoryIncludeMask"].Value.Remove(0, 2) + ' '; |
|---|
| 122 | |
|---|
| 123 | return true; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | try |
|---|
| 127 | { |
|---|
| 128 | if (Directory.Exists(argument)) |
|---|
| 129 | { |
|---|
| 130 | folderPath.Text = argument; |
|---|
| 131 | folderDelete.Checked = false; |
|---|
| 132 | folderInclude.Text = folderExclude.Text = string.Empty; |
|---|
| 133 | return true; |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | catch (NotSupportedException) |
|---|
| 137 | { |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | return false; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | #endregion |
|---|
| 144 | |
|---|
| 145 | private void folderBrowse_Click(object sender, EventArgs e) |
|---|
| 146 | { |
|---|
| 147 | try |
|---|
| 148 | { |
|---|
| 149 | folderDialog.SelectedPath = folderPath.Text; |
|---|
| 150 | if (folderDialog.ShowDialog() == DialogResult.OK) |
|---|
| 151 | folderPath.Text = folderDialog.SelectedPath; |
|---|
| 152 | } |
|---|
| 153 | catch (NotSupportedException) |
|---|
| 154 | { |
|---|
| 155 | MessageBox.Show(this, S._("The path you selected is invalid."), S._("Eraser"), |
|---|
| 156 | MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, |
|---|
| 157 | Localisation.IsRightToLeft(this) ? |
|---|
| 158 | MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | } |
|---|