1
/* RunInTerminalAction.cs
3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this
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.
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.
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/>.
22
using System.Diagnostics;
23
using System.Collections.Generic;
31
/// Runs text commands in a terminal.
33
public class RunInTerminalAction : AbstractAction
36
static string last_command_found;
37
static Dictionary<string, string> terminals;
39
static RunInTerminalAction ()
41
terminals = new Dictionary<string, string> ();
42
terminals["gnome-terminal"] = "-x";
43
terminals["xterm"] = "-e";
44
terminals["konsole"] = "-e";
45
terminals["xfce4-terminal"] = "-x";
48
public static bool CommandLineIsFoundOnPath (string command_line)
50
string path, command, command_file;
53
if (command_line == null) return false;
55
command = command_line.Trim ();
56
space_position = command.IndexOf (" ");
57
if (space_position > 0) {
58
command = command.Substring (0, space_position);
61
// If this command is the same as the last, yes.
62
if (command == last_command_found) return true;
64
// If the command is found, fine.
65
if (System.IO.File.Exists (command)) {
66
last_command_found = command;
70
// Otherwise, try to find the command file in path.
71
path = System.Environment.GetEnvironmentVariable ("PATH");
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;
84
static bool GetTerminalSettings (out string program, out string args)
88
client = new GConf.Client();
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;
95
program = args = null;
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)) {
104
args = terminals[terminal];
109
return program != null;
112
public static void RunCommandlineInTerminal (string commandline)
114
string command, args;
115
string terminal, terminal_args;
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);
123
command = commandline;
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);
133
// No settings found - just run command as a process.
134
proc.StartInfo.FileName = command;
135
proc.StartInfo.Arguments = args;
137
Console.WriteLine (proc.StartInfo.FileName + " " + proc.StartInfo.Arguments);
140
} catch (Exception e) {
141
Console.Error.WriteLine ("Failed to run command in terminal \"{0}\": ", e.Message);
145
public override string Name
147
get { return Catalog.GetString ("Run in Terminal"); }
150
public override string Description
152
get { return Catalog.GetString ("Run a command in a terminal."); }
155
public override string Icon
157
get { return "terminal"; }
160
public override Type[] SupportedItemTypes
170
public override bool SupportsItem (IItem item)
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);
180
public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
184
foreach (IItem item in items) {
186
if (item is ITextItem) {
187
commandline = (item as ITextItem).Text;
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 (" ", "\\ ");
193
if (commandline == null) continue;
194
RunCommandlineInTerminal (commandline);