~ubuntu-branches/ubuntu/precise/gnome-do/precise-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* FileItemActions.cs
 *
 * GNOME Do is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.IO;
using System.Collections.Generic;

using Do.Universe;

namespace Do.Files {

	class CopyToAction : IAction {

		public string Name { get { return "Copy to..."; } } 
		public string Description { get { return "Copies a file or folder to another location."; } } 
		public string Icon { get { return "gtk-copy"; } } 

		public Type [] SupportedItemTypes {
			get {
				return new Type [] {
					typeof (FileItem),
				};
			}
		}

		public Type [] SupportedModifierItemTypes {
			get {
				return new Type [] {
					typeof (IFileItem),
				};
			}
		}

		public bool ModifierItemsOptional {
			get { return false; }
		}

		public bool SupportsItem (IItem item)
		{
			return true;
		}

		public bool SupportsModifierItemForItems (IItem [] items, IItem modItem)
		{
			return FileItem.IsDirectory (modItem as IFileItem);
		}

		public IItem [] DynamicModifierItemsForItem (IItem item)
		{
			return null;
		}

		public IItem [] Perform (IItem [] items, IItem [] modItems)
		{
			IFileItem dest;
			List<string> seenPaths;

			dest = modItems [0] as IFileItem;
			seenPaths = new List<string> ();
			foreach (FileItem src in items) {
				if (seenPaths.Contains (src.Path)) continue;
				try {
					System.Diagnostics.Process.Start ("cp",
							string.Format ("-r {0} {1}",
								FileItem.EscapedPath (src), FileItem.EscapedPath (dest)));

					seenPaths.Add (src.Path);
					src.Path = Path.Combine (dest.Path, Path.GetFileName (src.Path));
				} catch (Exception e) {
					Console.Error.WriteLine ("CopyToAction could not copy "+
							src.Path + " to " + dest.Path + ": " + e.Message);
				}
			}
			return null;
		}
	}

	class MoveToAction : IAction {

		public string Name { get { return "Move to..."; } } 
		public string Description { get { return "Moves a file or folder to another location."; } } 
		public string Icon { get { return "forward"; } } 

		public Type [] SupportedItemTypes {
			get {
				return new Type [] {
					typeof (FileItem),
				};
			}
		}

		public Type [] SupportedModifierItemTypes {
			get {
				return new Type [] {
					typeof (IFileItem),
				};
			}
		}

		public bool ModifierItemsOptional {
			get { return false; }
		}

		public bool SupportsItem (IItem item)
		{
			return true;
		}

		public bool SupportsModifierItemForItems (IItem [] items, IItem modItem)
		{
			return items.Length == 1 ||
				FileItem.IsDirectory (modItem as IFileItem);
		}

		public IItem [] DynamicModifierItemsForItem (IItem item)
		{
			return null;
		}

		public IItem [] Perform (IItem [] items, IItem [] modItems)
		{
			IFileItem dest;
			List<string> seenPaths;

			dest = modItems [0] as IFileItem;
			seenPaths = new List<string> ();
			foreach (FileItem src in items) {
				if (seenPaths.Contains (src.Path)) continue;
				try {
					System.Diagnostics.Process.Start ("mv",
							string.Format ("{0} {1}",
								FileItem.EscapedPath (src), FileItem.EscapedPath (dest)));
					seenPaths.Add (src.Path);

					if (FileItem.IsDirectory (dest)) {
						src.Path = Path.Combine (dest.Path,
								Path.GetFileName (src.Path));
					} else {
						src.Path = dest.Path;
					}
				} catch (Exception e) {
					Console.Error.WriteLine ("MoveToAction could not move "+
							src.Path + " to " + dest.Path + ": " + e.Message);
				}
			}
			return null;
		}
	}

	class MoveToTrashAction : AbstractAction {

		public override string Name { get { return "Move to Trash"; } } 
		public override string Description { get { return "Moves a file or folder to the trash."; } } 
		public override string Icon { get { return "user-trash-full"; } } 

		string Trash {
			get { 
				return Paths.Combine (
						Paths.ReadXdgUserDir ("XDG_DATA_HOME", ".local/share"),
						"Trash/files");
			}
		}

		public override Type [] SupportedItemTypes {
			get {
				return new Type [] {
					typeof (FileItem),
				};
			}
		}

		public override IItem [] Perform (IItem [] items, IItem [] modItems)
		{
			List<string> seenPaths;

			seenPaths = new List<string> ();
			foreach (FileItem src in items) {
				if (seenPaths.Contains (src.Path)) continue;
				try {
					System.Diagnostics.Process.Start ("mv",
							string.Format ("{0} {1}", FileItem.EscapedPath (src), Trash));
					seenPaths.Add (src.Path);
					src.Path = Path.Combine (Trash, Path.GetFileName (src.Path));
				} catch (Exception e) {
					Console.Error.WriteLine ("MoveToTrashAction could not move "+
							src.Path + " to the trash: " + e.Message);
				}
			}
			return null;
		}
	}

	abstract class DeleteAction : AbstractAction {

		public override string Name { get { return "Delete File"; } } 
		public override string Description { get { return "Deletes a file or folder."; } } 
		public override string Icon { get { return "gtk-delete"; } } 

		public override Type [] SupportedItemTypes {
			get {
				return new Type [] {
					typeof (IFileItem),
				};
			}
		}

		public override IItem [] Perform (IItem [] items, IItem [] modItems)
		{
			foreach (IFileItem src in items) {
				try {
					System.Diagnostics.Process.Start ("rm",
							string.Format ("-rf {0}", FileItem.EscapedPath (src)));
				} catch (Exception e) {
					Console.Error.WriteLine ("DeleteFileAction could not delete "+
							src.Path + ": " + e.Message);
				}
			}
			return null;
		}
	}
}