No issues found
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
3 /* eel-gnome-extensions.c - implementation of new functions that operate on
4 gnome classes. Perhaps some of these should be
5 rolled into gnome someday.
6
7 Copyright (C) 1999, 2000, 2001 Eazel, Inc.
8
9 The Gnome Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 The Gnome Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public
20 License along with the Gnome Library; see the file COPYING.LIB. If not,
21 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
23
24 Authors: Darin Adler <darin@eazel.com>
25 */
26
27 #include <config.h>
28
29 #define GNOME_DESKTOP_USE_UNSTABLE_API
30
31 #include "eel-gnome-extensions.h"
32
33 #include <gtk/gtk.h>
34 #include <libgnome-desktop/gnome-desktop-utils.h>
35
36 /* Return a command string containing the path to a terminal on this system. */
37
38 static char *
39 try_terminal_command (const char *program,
40 const char *args)
41 {
42 char *program_in_path, *quoted, *result;
43
44 if (program == NULL) {
45 return NULL;
46 }
47
48 program_in_path = g_find_program_in_path (program);
49 if (program_in_path == NULL) {
50 return NULL;
51 }
52
53 quoted = g_shell_quote (program_in_path);
54 g_free (program_in_path);
55 if (args == NULL || args[0] == '\0') {
56 return quoted;
57 }
58 result = g_strconcat (quoted, " ", args, NULL);
59 g_free (quoted);
60 return result;
61 }
62
63 static char *
64 try_terminal_command_argv (int argc,
65 char **argv)
66 {
67 GString *string;
68 int i;
69 char *quoted, *result;
70
71 if (argc == 0) {
72 return NULL;
73 }
74
75 if (argc == 1) {
76 return try_terminal_command (argv[0], NULL);
77 }
78
79 string = g_string_new (argv[1]);
80 for (i = 2; i < argc; i++) {
81 quoted = g_shell_quote (argv[i]);
82 g_string_append_c (string, ' ');
83 g_string_append (string, quoted);
84 g_free (quoted);
85 }
86 result = try_terminal_command (argv[0], string->str);
87 g_string_free (string, TRUE);
88
89 return result;
90 }
91
92 static char *
93 get_terminal_command_prefix (gboolean for_command)
94 {
95 int argc;
96 char **argv;
97 char *command;
98 guint i;
99 static const char *const commands[][3] = {
100 { "gnome-terminal", "-x", "" },
101 { "dtterm", "-e", "-ls" },
102 { "nxterm", "-e", "-ls" },
103 { "color-xterm", "-e", "-ls" },
104 { "rxvt", "-e", "-ls" },
105 { "xterm", "-e", "-ls" },
106 };
107
108 /* Try the terminal from preferences. Use without any
109 * arguments if we are just doing a standalone terminal.
110 */
111 argc = 0;
112 argv = g_new0 (char *, 1);
113 gnome_desktop_prepend_terminal_to_vector (&argc, &argv);
114
115 command = NULL;
116 if (argc != 0) {
117 if (for_command) {
118 command = try_terminal_command_argv (argc, argv);
119 } else {
120 /* Strip off the arguments in a lame attempt
121 * to make it be an interactive shell.
122 */
123 command = try_terminal_command (argv[0], NULL);
124 }
125 }
126
127 while (argc != 0) {
128 g_free (argv[--argc]);
129 }
130 g_free (argv);
131
132 if (command != NULL) {
133 return command;
134 }
135
136 /* Try well-known terminal applications in same order that gmc did. */
137 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
138 command = try_terminal_command (commands[i][0],
139 commands[i][for_command ? 1 : 2]);
140 if (command != NULL) {
141 break;
142 }
143 }
144
145 return command;
146 }
147
148 static char *
149 eel_gnome_make_terminal_command (const char *command)
150 {
151 char *prefix, *quoted, *terminal_command;
152
153 if (command == NULL) {
154 return get_terminal_command_prefix (FALSE);
155 }
156 prefix = get_terminal_command_prefix (TRUE);
157 quoted = g_shell_quote (command);
158 terminal_command = g_strconcat (prefix, " /bin/sh -c ", quoted, NULL);
159 g_free (prefix);
160 g_free (quoted);
161 return terminal_command;
162 }
163
164 void
165 eel_gnome_open_terminal_on_screen (const char *command,
166 GdkScreen *screen)
167 {
168 char *command_line;
169 GAppInfo *app;
170 GdkAppLaunchContext *ctx;
171 GError *error = NULL;
172 GdkDisplay *display;
173
174 command_line = eel_gnome_make_terminal_command (command);
175 if (command_line == NULL) {
176 g_message ("Could not start a terminal");
177 return;
178 }
179
180 app = g_app_info_create_from_commandline (command_line, NULL, 0, &error);
181
182 if (app != NULL && screen != NULL) {
183 display = gdk_screen_get_display (screen);
184 ctx = gdk_display_get_app_launch_context (display);
185 gdk_app_launch_context_set_screen (ctx, screen);
186
187 g_app_info_launch (app, NULL, G_APP_LAUNCH_CONTEXT (ctx), &error);
188
189 g_object_unref (app);
190 g_object_unref (ctx);
191 }
192
193 if (error != NULL) {
194 g_message ("Could not start application on terminal: %s", error->message);
195
196 g_error_free (error);
197 }
198
199 g_free (command_line);
200 }