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

« back to all changes in this revision

Viewing changes to Do.Addins/src/Do.Universe/RunInTerminalAction.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
 
/* RunInTerminalAction.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.Diagnostics;
23
 
using System.Collections.Generic;
24
 
using Mono.Unix;
25
 
 
26
 
using Do.Addins;
27
 
 
28
 
namespace Do.Universe
29
 
{
30
 
        /// <summary>
31
 
        /// Runs text commands in a terminal.
32
 
        /// </summary>
33
 
        public class RunInTerminalAction : AbstractAction
34
 
        {
35
 
 
36
 
                static string last_command_found;
37
 
                static Dictionary<string, string> terminals;
38
 
 
39
 
                static RunInTerminalAction ()
40
 
                {
41
 
                        terminals = new Dictionary<string, string> ();
42
 
                        terminals["gnome-terminal"] = "-x";
43
 
                        terminals["xterm"] = "-e";
44
 
                        terminals["konsole"] = "-e";
45
 
                        terminals["xfce4-terminal"] = "-x";
46
 
                }
47
 
 
48
 
                public static bool CommandLineIsFoundOnPath (string command_line)
49
 
                {
50
 
                        string path, command, command_file;
51
 
                        int space_position;
52
 
                        
53
 
                        if (command_line == null) return false;
54
 
                        
55
 
                        command = command_line.Trim ();                 
56
 
                        space_position = command.IndexOf (" ");
57
 
                        if (space_position > 0) {
58
 
                                command = command.Substring (0, space_position);
59
 
                        }
60
 
 
61
 
                        // If this command is the same as the last, yes.
62
 
                        if (command == last_command_found) return true;
63
 
                        
64
 
                        // If the command is found, fine.
65
 
                        if (System.IO.File.Exists (command)) {
66
 
                                last_command_found = command;
67
 
                                return true;
68
 
                        }
69
 
                        
70
 
                        // Otherwise, try to find the command file in path.
71
 
                        path = System.Environment.GetEnvironmentVariable ("PATH");
72
 
                        if (path != null) {
73
 
                                foreach (string part in path.Split (':')) {
74
 
                                        command_file = System.IO.Path.Combine (part, command);
75
 
                                        if (System.IO.File.Exists (command_file)) {
76
 
                                                last_command_found = command;
77
 
                                                return true;
78
 
                                        }
79
 
                                }
80
 
                        }
81
 
                        return false;
82
 
                }
83
 
 
84
 
                static bool GetTerminalSettings (out string program, out string args)
85
 
                {
86
 
                        GConf.Client client;
87
 
 
88
 
                        client = new GConf.Client();
89
 
                        try {
90
 
                                program = client.Get ("/desktop/gnome/applications/terminal/exec") as string;
91
 
                                args = client.Get ("/desktop/gnome/applications/terminal/exec_arg") as string;
92
 
                                if (!CommandLineIsFoundOnPath (program))
93
 
                                        program = args = null;
94
 
                        } catch {
95
 
                                program = args = null;
96
 
                        }
97
 
                        
98
 
                        // No settings found or the program cannot be found. Try to find a
99
 
                        // suitable terminal manually.
100
 
                        if (string.IsNullOrEmpty (program)) {
101
 
                                foreach (string terminal in terminals.Keys) {
102
 
                                        if (CommandLineIsFoundOnPath (terminal)) {
103
 
                                                program = terminal;
104
 
                                                args = terminals[terminal];
105
 
                                                break;
106
 
                                        }
107
 
                                }
108
 
                        }
109
 
                        return program != null;
110
 
                }
111
 
                
112
 
                public static void RunCommandlineInTerminal (string commandline)
113
 
                {
114
 
                        string command, args;
115
 
                        string terminal, terminal_args;
116
 
                        Process proc;
117
 
                        
118
 
                        // Split commandline into command and arguments.
119
 
                        if (commandline.Contains (" ")) {
120
 
                                command = commandline.Substring (0, commandline.IndexOf (" "));
121
 
                                args = commandline.Substring (commandline.IndexOf (" ")+1);
122
 
                        } else {
123
 
                                command = commandline;
124
 
                                args = "";
125
 
                        }
126
 
                        
127
 
                        proc = new Process ();
128
 
                        // Get settings for running command in a terminal.
129
 
                        if (GetTerminalSettings (out terminal, out terminal_args)) {
130
 
                                proc.StartInfo.FileName = terminal;
131
 
                                proc.StartInfo.Arguments = string.Format ("{0} {1}", terminal_args, commandline);
132
 
                        } else {
133
 
                                // No settings found - just run command as a process.
134
 
                                proc.StartInfo.FileName = command;
135
 
                                proc.StartInfo.Arguments = args;
136
 
                        }
137
 
                        Console.WriteLine (proc.StartInfo.FileName + " " + proc.StartInfo.Arguments);
138
 
                        try {
139
 
                                proc.Start ();
140
 
                        } catch (Exception e) {
141
 
                                Console.Error.WriteLine ("Failed to run command in terminal \"{0}\": ", e.Message);
142
 
                        }
143
 
                }
144
 
 
145
 
                public override string Name
146
 
                {
147
 
                        get { return Catalog.GetString ("Run in Terminal"); }
148
 
                }
149
 
                
150
 
                public override string Description
151
 
                {
152
 
                        get { return Catalog.GetString ("Run a command in a terminal."); }
153
 
                }
154
 
                
155
 
                public override string Icon
156
 
                {
157
 
                        get { return "terminal"; }
158
 
                }
159
 
                
160
 
                public override Type[] SupportedItemTypes
161
 
                {
162
 
                        get {
163
 
                                return new Type[] {
164
 
                                        typeof (ITextItem),
165
 
                                        typeof (FileItem),
166
 
                                };
167
 
                        }
168
 
                }
169
 
                
170
 
                public override bool SupportsItem (IItem item)
171
 
                {
172
 
                        if (item is ITextItem) {
173
 
                                return CommandLineIsFoundOnPath ((item as ITextItem).Text);
174
 
                        } else if (item is FileItem) {
175
 
                                return FileItem.IsExecutable (item as FileItem);
176
 
                        }
177
 
                        return false;
178
 
                }
179
 
 
180
 
                public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
181
 
                {
182
 
                        string commandline;
183
 
 
184
 
                        foreach (IItem item in items) {
185
 
                                commandline = null;
186
 
                                if (item is ITextItem) {
187
 
                                        commandline = (item as ITextItem).Text;
188
 
                                }
189
 
                                else if (item is FileItem) {
190
 
                                        // Format the filename so the terminal doesn't choke on it.
191
 
                                        commandline = (item as FileItem).Path.Replace (" ", "\\ ");
192
 
                                }
193
 
                                if (commandline == null) continue;
194
 
                                RunCommandlineInTerminal (commandline);
195
 
                        }
196
 
                        return null;
197
 
                }
198
 
 
199
 
        }
200
 
}