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

« back to all changes in this revision

Viewing changes to Do.Addins/src/Do.Universe/FileItemActions.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2008-09-14 10:09:40 UTC
  • mto: (0.1.8 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20080914100940-kyghudg7py14bu2z
Tags: upstream-0.6.0.0
ImportĀ upstreamĀ versionĀ 0.6.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* FileItemActions.cs
2
 
 *
3
 
 * GNOME Do is the legal property of its developers. Please refer to the
4
 
 * COPYRIGHT file distributed with this
5
 
 * source distribution.
6
 
 *
7
 
 * This program is free software: you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation, either version 3 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
 */
20
 
 
21
 
using System;
22
 
using System.IO;
23
 
using System.Collections.Generic;
24
 
 
25
 
using Do.Universe;
26
 
 
27
 
namespace Do.Files {
28
 
 
29
 
        class CopyToAction : IAction {
30
 
 
31
 
                public string Name { get { return "Copy to..."; } } 
32
 
                public string Description { get { return "Copies a file or folder to another location."; } } 
33
 
                public string Icon { get { return "gtk-copy"; } } 
34
 
 
35
 
                public Type [] SupportedItemTypes {
36
 
                        get {
37
 
                                return new Type [] {
38
 
                                        typeof (FileItem),
39
 
                                };
40
 
                        }
41
 
                }
42
 
 
43
 
                public Type [] SupportedModifierItemTypes {
44
 
                        get {
45
 
                                return new Type [] {
46
 
                                        typeof (IFileItem),
47
 
                                };
48
 
                        }
49
 
                }
50
 
 
51
 
                public bool ModifierItemsOptional {
52
 
                        get { return false; }
53
 
                }
54
 
 
55
 
                public bool SupportsItem (IItem item)
56
 
                {
57
 
                        return true;
58
 
                }
59
 
 
60
 
                public bool SupportsModifierItemForItems (IItem [] items, IItem modItem)
61
 
                {
62
 
                        return FileItem.IsDirectory (modItem as IFileItem);
63
 
                }
64
 
 
65
 
                public IItem [] DynamicModifierItemsForItem (IItem item)
66
 
                {
67
 
                        return null;
68
 
                }
69
 
 
70
 
                public IItem [] Perform (IItem [] items, IItem [] modItems)
71
 
                {
72
 
                        IFileItem dest;
73
 
                        List<string> seenPaths;
74
 
 
75
 
                        dest = modItems [0] as IFileItem;
76
 
                        seenPaths = new List<string> ();
77
 
                        foreach (FileItem src in items) {
78
 
                                if (seenPaths.Contains (src.Path)) continue;
79
 
                                try {
80
 
                                        System.Diagnostics.Process.Start ("cp",
81
 
                                                        string.Format ("-r {0} {1}",
82
 
                                                                FileItem.EscapedPath (src), FileItem.EscapedPath (dest)));
83
 
 
84
 
                                        seenPaths.Add (src.Path);
85
 
                                        src.Path = Path.Combine (dest.Path, Path.GetFileName (src.Path));
86
 
                                } catch (Exception e) {
87
 
                                        Console.Error.WriteLine ("CopyToAction could not copy "+
88
 
                                                        src.Path + " to " + dest.Path + ": " + e.Message);
89
 
                                }
90
 
                        }
91
 
                        return null;
92
 
                }
93
 
        }
94
 
 
95
 
        class MoveToAction : IAction {
96
 
 
97
 
                public string Name { get { return "Move to..."; } } 
98
 
                public string Description { get { return "Moves a file or folder to another location."; } } 
99
 
                public string Icon { get { return "forward"; } } 
100
 
 
101
 
                public Type [] SupportedItemTypes {
102
 
                        get {
103
 
                                return new Type [] {
104
 
                                        typeof (FileItem),
105
 
                                };
106
 
                        }
107
 
                }
108
 
 
109
 
                public Type [] SupportedModifierItemTypes {
110
 
                        get {
111
 
                                return new Type [] {
112
 
                                        typeof (IFileItem),
113
 
                                };
114
 
                        }
115
 
                }
116
 
 
117
 
                public bool ModifierItemsOptional {
118
 
                        get { return false; }
119
 
                }
120
 
 
121
 
                public bool SupportsItem (IItem item)
122
 
                {
123
 
                        return true;
124
 
                }
125
 
 
126
 
                public bool SupportsModifierItemForItems (IItem [] items, IItem modItem)
127
 
                {
128
 
                        return items.Length == 1 ||
129
 
                                FileItem.IsDirectory (modItem as IFileItem);
130
 
                }
131
 
 
132
 
                public IItem [] DynamicModifierItemsForItem (IItem item)
133
 
                {
134
 
                        return null;
135
 
                }
136
 
 
137
 
                public IItem [] Perform (IItem [] items, IItem [] modItems)
138
 
                {
139
 
                        IFileItem dest;
140
 
                        List<string> seenPaths;
141
 
 
142
 
                        dest = modItems [0] as IFileItem;
143
 
                        seenPaths = new List<string> ();
144
 
                        foreach (FileItem src in items) {
145
 
                                if (seenPaths.Contains (src.Path)) continue;
146
 
                                try {
147
 
                                        System.Diagnostics.Process.Start ("mv",
148
 
                                                        string.Format ("{0} {1}",
149
 
                                                                FileItem.EscapedPath (src), FileItem.EscapedPath (dest)));
150
 
                                        seenPaths.Add (src.Path);
151
 
 
152
 
                                        if (FileItem.IsDirectory (dest)) {
153
 
                                                src.Path = Path.Combine (dest.Path,
154
 
                                                                Path.GetFileName (src.Path));
155
 
                                        } else {
156
 
                                                src.Path = dest.Path;
157
 
                                        }
158
 
                                } catch (Exception e) {
159
 
                                        Console.Error.WriteLine ("MoveToAction could not move "+
160
 
                                                        src.Path + " to " + dest.Path + ": " + e.Message);
161
 
                                }
162
 
                        }
163
 
                        return null;
164
 
                }
165
 
        }
166
 
 
167
 
        class MoveToTrashAction : AbstractAction {
168
 
 
169
 
                public override string Name { get { return "Move to Trash"; } } 
170
 
                public override string Description { get { return "Moves a file or folder to the trash."; } } 
171
 
                public override string Icon { get { return "user-trash-full"; } } 
172
 
 
173
 
                string Trash {
174
 
                        get { 
175
 
                                return Paths.Combine (
176
 
                                                Paths.ReadXdgUserDir ("XDG_DATA_HOME", ".local/share"),
177
 
                                                "Trash/files");
178
 
                        }
179
 
                }
180
 
 
181
 
                public override Type [] SupportedItemTypes {
182
 
                        get {
183
 
                                return new Type [] {
184
 
                                        typeof (FileItem),
185
 
                                };
186
 
                        }
187
 
                }
188
 
 
189
 
                public override IItem [] Perform (IItem [] items, IItem [] modItems)
190
 
                {
191
 
                        List<string> seenPaths;
192
 
 
193
 
                        seenPaths = new List<string> ();
194
 
                        foreach (FileItem src in items) {
195
 
                                if (seenPaths.Contains (src.Path)) continue;
196
 
                                try {
197
 
                                        System.Diagnostics.Process.Start ("mv",
198
 
                                                        string.Format ("{0} {1}", FileItem.EscapedPath (src), Trash));
199
 
                                        seenPaths.Add (src.Path);
200
 
                                        src.Path = Path.Combine (Trash, Path.GetFileName (src.Path));
201
 
                                } catch (Exception e) {
202
 
                                        Console.Error.WriteLine ("MoveToTrashAction could not move "+
203
 
                                                        src.Path + " to the trash: " + e.Message);
204
 
                                }
205
 
                        }
206
 
                        return null;
207
 
                }
208
 
        }
209
 
 
210
 
        abstract class DeleteAction : AbstractAction {
211
 
 
212
 
                public override string Name { get { return "Delete File"; } } 
213
 
                public override string Description { get { return "Deletes a file or folder."; } } 
214
 
                public override string Icon { get { return "gtk-delete"; } } 
215
 
 
216
 
                public override Type [] SupportedItemTypes {
217
 
                        get {
218
 
                                return new Type [] {
219
 
                                        typeof (IFileItem),
220
 
                                };
221
 
                        }
222
 
                }
223
 
 
224
 
                public override IItem [] Perform (IItem [] items, IItem [] modItems)
225
 
                {
226
 
                        foreach (IFileItem src in items) {
227
 
                                try {
228
 
                                        System.Diagnostics.Process.Start ("rm",
229
 
                                                        string.Format ("-rf {0}", FileItem.EscapedPath (src)));
230
 
                                } catch (Exception e) {
231
 
                                        Console.Error.WriteLine ("DeleteFileAction could not delete "+
232
 
                                                        src.Path + ": " + e.Message);
233
 
                                }
234
 
                        }
235
 
                        return null;
236
 
                }
237
 
        }
238
 
}