~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to GNOME Terminal/src/RunInTerminalAction.cs

  • Committer: David Siegel
  • Date: 2008-06-02 16:09:11 UTC
  • Revision ID: dave@x-20080602160911-5bc56q3okv27pg7y
Added GNOME Terminal plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* RunInTerminalAction.cs
 
2
 * 
 
3
 * GNOME Do is the legal property of its developers, whose names are too numerous
 
4
 * to list here.  Please refer to the 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.Diagnostics;
 
23
using System.IO;
 
24
 
 
25
using Do.Universe;
 
26
 
 
27
namespace GNOME.Terminal
 
28
{
 
29
        public class RunInTerminalAction : AbstractAction
 
30
        {
 
31
                static string last_command_found;
 
32
                
 
33
                public RunInTerminalAction()
 
34
                {
 
35
                }
 
36
 
 
37
                public override string Name
 
38
                {
 
39
              get { return "Run in Terminal"; }
 
40
            }
 
41
 
 
42
            public override string Description
 
43
                {
 
44
              get { return "Runs a command in GNOME Terminal."; }
 
45
            }
 
46
 
 
47
            public override string Icon
 
48
                {
 
49
              get { return "gnome-terminal"; }
 
50
            }
 
51
 
 
52
            public override Type[] SupportedItemTypes
 
53
                {
 
54
              get {
 
55
                return new Type[] {
 
56
                        typeof (ITextItem),
 
57
                        typeof (IFileItem),
 
58
                };
 
59
              }
 
60
            }
 
61
 
 
62
                public override bool SupportsItem (IItem item)
 
63
                {
 
64
                        if (item is ITextItem) {
 
65
                                return CommandLineFoundOnPath ((item as ITextItem).Text);
 
66
                        } else if (item is IFileItem) {
 
67
                                return FileItem.IsExecutable (item as IFileItem);
 
68
                        }
 
69
                        return false;
 
70
                }
 
71
 
 
72
            public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
 
73
            {
 
74
                        foreach (IItem item in items) {
 
75
                                string cmd = "";
 
76
                                if (item is ITextItem) {
 
77
                                        cmd = (item as ITextItem).Text;
 
78
                                } else if (item is IFileItem) {
 
79
                                        cmd = (item as IFileItem).Path;
 
80
                                }
 
81
 
 
82
                                try {
 
83
                                        Process.Start ("gnome-terminal", "-x " + cmd );
 
84
                                } catch (Exception e) {
 
85
                                        Console.Error.WriteLine ("Could not open gnome-terminal with command {0}: {1}",
 
86
                                                cmd, e.Message );
 
87
                                }
 
88
                        }
 
89
                        return null;
 
90
            }
 
91
 
 
92
                public static bool CommandLineFoundOnPath (string command_line)
 
93
                {
 
94
                        string path, command, command_file;
 
95
                        int space_position;
 
96
                        
 
97
                        if (command_line == null) return false;
 
98
                        
 
99
                        command = command_line.Trim ();                 
 
100
                        space_position = command.IndexOf (" ");
 
101
                        if (space_position > 0) {
 
102
                                command = command.Substring (0, space_position);
 
103
                        }
 
104
 
 
105
                        // If this command is the same as the last, yes.
 
106
                        if (command == last_command_found) return true;
 
107
                        
 
108
                        // If the command is found, fine.
 
109
                        if (System.IO.File.Exists (command)) {
 
110
                                last_command_found = command;
 
111
                                return true;
 
112
                        }
 
113
                        
 
114
                        // Otherwise, try to find the command file in path.
 
115
                        path = System.Environment.GetEnvironmentVariable ("PATH");
 
116
                        if (path != null) {
 
117
                                foreach (string part in path.Split (':')) {
 
118
                                        command_file = System.IO.Path.Combine (part, command);
 
119
                                        if (System.IO.File.Exists (command_file)) {
 
120
                                                last_command_found = command;
 
121
                                                return true;
 
122
                                        }
 
123
                                }
 
124
                        }
 
125
                        return false;
 
126
                }
 
127
        }
 
128
}