| [2276] | 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.Text; |
|---|
| 25 | |
|---|
| 26 | using System.Globalization; |
|---|
| 27 | using System.Windows.Forms; |
|---|
| 28 | using System.IO; |
|---|
| 29 | using System.Drawing; |
|---|
| 30 | |
|---|
| 31 | using Eraser.Util; |
|---|
| 32 | using Eraser.Manager; |
|---|
| [2451] | 33 | using Eraser.Plugins; |
|---|
| [2445] | 34 | using Eraser.Plugins.ExtensionPoints; |
|---|
| [2276] | 35 | |
|---|
| 36 | namespace Eraser |
|---|
| 37 | { |
|---|
| 38 | static class TaskDragDropHelper |
|---|
| 39 | { |
|---|
| 40 | /// <summary> |
|---|
| 41 | /// Parses a list of locations dropped on the target. |
|---|
| 42 | /// </summary> |
|---|
| 43 | /// <param name="e">The event argument.</param> |
|---|
| 44 | /// <param name="recycleBin">A reference to a <typeparamref name="System.Boolean"/> value |
|---|
| 45 | /// which holds whether the recycle bin was dropped.</param> |
|---|
| 46 | /// <returns>The list of file paths dropped.</returns> |
|---|
| 47 | public static ICollection<string> GetFiles(DragEventArgs e, out bool recycleBin) |
|---|
| 48 | { |
|---|
| 49 | //Get the file paths first. |
|---|
| 50 | recycleBin = false; |
|---|
| 51 | List<string> files = e.Data.GetDataPresent(DataFormats.FileDrop) ? |
|---|
| 52 | new List<string>((string[])e.Data.GetData(DataFormats.FileDrop, false)) : |
|---|
| 53 | new List<string>(); |
|---|
| 54 | |
|---|
| 55 | //Then try to see if we have shell locations dropped on us. |
|---|
| 56 | if (e.Data.GetDataPresent("Shell IDList Array")) |
|---|
| 57 | { |
|---|
| 58 | MemoryStream stream = (MemoryStream)e.Data.GetData("Shell IDList Array"); |
|---|
| 59 | byte[] buffer = new byte[stream.Length]; |
|---|
| 60 | stream.Read(buffer, 0, buffer.Length); |
|---|
| 61 | ShellCIDA cida = new ShellCIDA(buffer); |
|---|
| 62 | |
|---|
| 63 | if (cida.cidl > 0) |
|---|
| 64 | { |
|---|
| 65 | for (int i = 1; i <= cida.cidl; ++i) |
|---|
| 66 | { |
|---|
| [2451] | 67 | if (cida.aoffset[i].Guid == Shell.KnownFolderIDs.RecycleBin) |
|---|
| [2276] | 68 | { |
|---|
| [2451] | 69 | recycleBin = true; |
|---|
| [2276] | 70 | } |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | return files; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| [2451] | 78 | /// <summary> |
|---|
| 79 | /// Parses a list of locations dropped on the target, converting them to the appropriate |
|---|
| 80 | /// IErasureTarget instance. |
|---|
| 81 | /// </summary> |
|---|
| 82 | /// <param name="e">The event argument.</param> |
|---|
| 83 | /// <returns>A list of erasure targets which will erase all the files and directories |
|---|
| 84 | /// as described by the drag-and-drop operation.</returns> |
|---|
| 85 | public static ICollection<IErasureTarget> GetTargets(DragEventArgs e) |
|---|
| [2276] | 86 | { |
|---|
| [2445] | 87 | ICollection<IErasureTarget> result = new List<IErasureTarget>(); |
|---|
| [2451] | 88 | foreach (IErasureTarget target in Host.Instance.ErasureTargetFactories) |
|---|
| [2276] | 89 | { |
|---|
| [2451] | 90 | //Skip targets not supporting IDragAndDropConfigurer |
|---|
| [2508] | 91 | if (!(target.Configurer is IDragAndDropConfigurerFactory<IErasureTarget>)) |
|---|
| [2276] | 92 | continue; |
|---|
| 93 | |
|---|
| [2508] | 94 | IDragAndDropConfigurerFactory<IErasureTarget> configurer = |
|---|
| 95 | (IDragAndDropConfigurerFactory<IErasureTarget>)target.Configurer; |
|---|
| [2451] | 96 | foreach (IErasureTarget newTarget in configurer.ProcessArgument(e)) |
|---|
| 97 | result.Add(newTarget); |
|---|
| [2276] | 98 | } |
|---|
| 99 | |
|---|
| 100 | return result; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /// <summary> |
|---|
| 104 | /// Handles the drag enter event. |
|---|
| 105 | /// </summary> |
|---|
| 106 | /// <param name="sender">The sender for the event.</param> |
|---|
| 107 | /// <param name="e">Event argument for the drag & drop operation.</param> |
|---|
| 108 | public static void OnDragEnter(Control control, DragEventArgs e, |
|---|
| 109 | string descriptionMessage, ICollection<string> items) |
|---|
| 110 | { |
|---|
| 111 | //Replace the C# {0} with the %1 used by Windows. |
|---|
| 112 | descriptionMessage = descriptionMessage.Replace("{0}", "%1"); |
|---|
| 113 | |
|---|
| 114 | string descriptionInsert = string.Empty; |
|---|
| 115 | string descriptionItemFormat = S._("{0}, "); |
|---|
| 116 | foreach (string item in items) |
|---|
| 117 | { |
|---|
| 118 | if (descriptionInsert.Length < 259 && |
|---|
| 119 | (descriptionInsert.Length < 3 || descriptionInsert.Substring(descriptionInsert.Length - 3) != "...")) |
|---|
| 120 | { |
|---|
| 121 | string append = string.Format(CultureInfo.InvariantCulture, |
|---|
| 122 | descriptionItemFormat, item); |
|---|
| 123 | |
|---|
| 124 | if (descriptionInsert.Length + append.Length > 259) |
|---|
| 125 | descriptionInsert += "....."; |
|---|
| 126 | else |
|---|
| 127 | descriptionInsert += append; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | if (!string.IsNullOrEmpty(descriptionInsert)) |
|---|
| 132 | descriptionInsert = descriptionInsert.Remove(descriptionInsert.Length - 2); |
|---|
| 133 | |
|---|
| 134 | if (e.Data.GetDataPresent("DragImageBits")) |
|---|
| 135 | DropTargetHelper.DragEnter(control, e.Data, new Point(e.X, e.Y), e.Effect, |
|---|
| 136 | descriptionMessage, descriptionInsert); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | public static void OnDrop(DragEventArgs e) |
|---|
| 140 | { |
|---|
| 141 | DropTargetHelper.Drop(e.Data, new Point(e.X, e.Y), e.Effect); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | } |
|---|