~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to Text/src/AppendTextAction.cs

  • Committer: Christopher Halse Rogers
  • Date: 2008-08-24 08:44:24 UTC
  • mfrom: (244.1.2 do-plugins)
  • Revision ID: raof@ubuntu.com-20080824084424-8gp5ff6v9nku9z21
Merge in the tip of 0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* AppendTextAction.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
 
 
24
using Do.Universe;
 
25
using Mono.Unix;
 
26
 
 
27
namespace Text {        
 
28
        public class AppendTextAction : IAction {
 
29
 
 
30
                public string Name { get { return Catalog.GetString ("Append to..."); } }
 
31
                public string Description { get { return Catalog.GetString ("Appends text to a selected file."); } }
 
32
                public string Icon { get { return "text-editor"; } }
 
33
 
 
34
                public Type[] SupportedItemTypes {
 
35
                        get {
 
36
                                return new Type[] { typeof (ITextItem) };
 
37
                        }
 
38
                }
 
39
                
 
40
                public Type[] SupportedModifierItemTypes {
 
41
                        get {
 
42
                                return new Type[] { typeof (FileItem), typeof(ITextItem) };
 
43
                        }
 
44
                }
 
45
                
 
46
                public bool ModifierItemsOptional {
 
47
                        get { return false; }
 
48
                }
 
49
                
 
50
 
 
51
                public bool SupportsItem (IItem item)
 
52
                {
 
53
                        return true;
 
54
                }
 
55
                
 
56
                public bool SupportsModifierItemForItems (IItem[] items, IItem modItem)
 
57
                {
 
58
                        if (modItem is FileItem) {
 
59
                                string mime = (modItem as FileItem).MimeType;
 
60
                                return mime == "x-directory/normal" || mime.StartsWith ("text/");
 
61
                        }
 
62
                        return modItem is ITextItem;
 
63
                }
 
64
                
 
65
                public IItem[] DynamicModifierItemsForItem (IItem item)
 
66
                {
 
67
                        return null;
 
68
                }
 
69
 
 
70
                public IItem[] Perform (IItem[] items, IItem[] modItems)
 
71
                {
 
72
                        if (modItems [0] is FileItem) {
 
73
                                string text = (items [0] as ITextItem).Text;
 
74
                                string mime = (modItems [0] as FileItem).MimeType;
 
75
                                string file = (modItems [0] as FileItem).Path;
 
76
                                
 
77
                                if (mime == "x-directory/normal") return null;
 
78
                                
 
79
                                using (StreamWriter w = File.AppendText (file)) {
 
80
                                        w.WriteLine (text);
 
81
                                        w.Close ();
 
82
                                }
 
83
 
 
84
                                return null;
 
85
                        }
 
86
                        else {
 
87
                                string text = (items [0] as ITextItem).Text;
 
88
                                string text2 = (modItems [0] as ITextItem).Text;
 
89
                                return new IItem[] { new TextItem (text + text2) };
 
90
                        }
 
91
                }
 
92
        }
 
93
}
 
94