~raof/do-plugins/bansheeplugin

« back to all changes in this revision

Viewing changes to File/src/Do/Do.FilesAndFolders/NewFileAction.cs

  • Committer: Alex Launi
  • Date: 2009-01-12 22:08:01 UTC
  • mfrom: (276.29.21 plugins-trunk)
  • Revision ID: alex.launi@gmail.com-20090112220801-te84p7e7av19gt1j
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
using System;
21
21
using System.IO;
22
22
using System.Linq;
23
 
using System.Text;
24
 
using System.Collections;
25
23
using System.Collections.Generic;
26
24
 
27
25
using Mono.Unix;
32
30
namespace Do.FilesAndFolders
33
31
{
34
32
        
35
 
        public class NewFileAction : Act
 
33
        public class NewFileAction : AbstractFileAction
36
34
        {
37
35
 
38
 
                const int MaxPathLength = 256;
39
 
 
40
36
                public override string Name {
41
37
                        get { return Catalog.GetString ("Create New File"); }
42
38
                }
49
45
                        get { return "filenew"; }
50
46
                }
51
47
                
52
 
                public override IEnumerable<Type> SupportedItemTypes {
53
 
                        get {
54
 
                                yield return typeof (ITextItem);
55
 
                                yield return typeof (IFileItem);
56
 
                        }
57
 
                }
58
 
 
59
 
                public override IEnumerable<Type> SupportedModifierItemTypes {
60
 
                        get {
61
 
                                yield return typeof (ITextItem);
62
 
                                yield return typeof (IFileItem);
63
 
                        }
64
 
                }
65
 
 
66
48
                public override bool ModifierItemsOptional {
67
49
                        get { return true; }
68
50
                }
69
51
 
70
52
                /// <summary>
71
 
                /// Supports using an IFileItem as the parent if it is a folder
72
 
                /// that already exists.
73
 
                /// </summary>
74
 
                /// <param name="item">
75
 
                /// A <see cref="IFileItem"/>
76
 
                /// </param>
77
 
                /// <returns>
78
 
                /// A <see cref="System.Boolean"/>
79
 
                /// </returns>
80
 
                bool Supports (IFileItem item)
81
 
                {
82
 
                        return item.Path.Length < MaxPathLength && Directory.Exists (item.Path);
83
 
                }
84
 
 
85
 
                /// <summary>
86
53
                /// Supports using an ITextItem as the folder name if
87
54
                /// it is not the name of a folder that already exists.
88
55
                /// </summary>
92
59
                /// <returns>
93
60
                /// A <see cref="System.Boolean"/>
94
61
                /// </returns>
95
 
                bool Supports (ITextItem item)
 
62
                protected override bool SupportsItem (ITextItem item)
96
63
                {
97
 
                        string path = item.Text.Replace ("~", Plugin.ImportantFolders.UserHome);
 
64
                        string path = GetPath (item);
98
65
                        return path.Length < MaxPathLength && !File.Exists (path) && !Directory.Exists (path);
99
66
                }
100
 
                
101
 
                public override bool SupportsItem (Item item)
 
67
 
 
68
                protected override bool SupportsItem (IFileItem item)
102
69
                {
103
 
                        if (item is ITextItem) return Supports (item as ITextItem);
104
 
                        if (item is IFileItem) return Supports (item as IFileItem);
105
 
                        return false;
 
70
                        return Directory.Exists (GetPath (item));
106
71
                }
107
72
 
108
73
                /// <summary>
119
84
                /// </returns>
120
85
                public override bool SupportsModifierItemForItems (IEnumerable<Item> items, Item modItem)
121
86
                {
122
 
                        Func<Item, bool> isDirectory = item =>
123
 
                                item is IFileItem && Supports (item as IFileItem);
124
 
 
125
 
                        return !isDirectory (items.First ()) || !isDirectory (modItem);
 
87
                        Item first = items.First ();
 
88
                        return
 
89
                                (first is IFileItem && !(modItem is IFileItem)) ||
 
90
                                (first is ITextItem && modItem is IFileItem && SupportsItem (modItem as IFileItem));
126
91
                }
127
92
 
128
93
                /// <summary>
137
102
                /// <returns>
138
103
                /// A <see cref="IEnumerable"/>
139
104
                /// </returns>
140
 
                public override IEnumerable<Item> Perform (IEnumerable<Item> items, IEnumerable<Item> modItems)
 
105
                protected override IEnumerable<Item> Perform (Item source, Item destination)
141
106
                {
142
107
                        IFileItem parent = null;
143
108
                        ITextItem fileName = null;
144
 
                        Item first = items.First (), mod = modItems.FirstOrDefault ();
145
109
                        
146
 
                        if (first is IFileItem) {
147
 
                                parent = first as IFileItem;
 
110
                        if (source is IFileItem) {
 
111
                                parent = source as IFileItem;
148
112
                                // We provide a default folder name if none is provided.
149
 
                                fileName = mod as ITextItem ??
 
113
                                fileName = destination as ITextItem ??
150
114
                                        Plugin.NewTextItem (GetNewFileName (parent.Path));
151
 
                        } else if (first is ITextItem) {
152
 
                                fileName = first as ITextItem;
 
115
                        } else if (source is ITextItem) {
 
116
                                fileName = source as ITextItem;
153
117
                                // We provide a default parent folder if none is provided.
154
 
                                parent = mod as IFileItem ??
 
118
                                parent = destination as IFileItem ??
155
119
                                        Plugin.NewFileItem (Plugin.ImportantFolders.Desktop);
156
120
                        }
157
121
                        
160
124
                        yield return Plugin.NewFileItem (path) as Item;
161
125
                }
162
126
 
 
127
                protected override IEnumerable<Item> Perform (Item source)
 
128
                {
 
129
                        return Perform (source, null);
 
130
                }
 
131
 
163
132
                /// <summary>
164
133
                /// Given a directory, returns a name of a file that does not exist
165
134
                /// in that directory.