summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-program-choosing.c
blob: e3f01ad3879ae3fff4304eadd5c978e166216e74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-program-choosing.c - functions for selecting and activating
 				 programs for opening/viewing particular files.

   Copyright (C) 2000 Eazel, Inc.

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: John Sullivan <sullivan@eazel.com>
*/

#include <config.h>
#include "nautilus-program-choosing.h"

#include "nautilus-glib-extensions.h"
#include "nautilus-mime-actions.h"
#include "nautilus-program-chooser.h"
#include "nautilus-stock-dialogs.h"
#include "nautilus-string.h"
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-uidefs.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <stdlib.h>

static gboolean
any_programs_available_for_file (GnomeVFSMimeActionType action_type, NautilusFile *file)
{
	gboolean result;
	char *uri;
	NautilusDirectory *directory;
	
	uri = nautilus_file_get_uri (file);
	directory = nautilus_directory_get (uri);

	if (action_type == GNOME_VFS_MIME_ACTION_TYPE_COMPONENT) {
		result = nautilus_mime_has_any_components_for_uri (directory, file);
	} else {
		g_assert (action_type == GNOME_VFS_MIME_ACTION_TYPE_APPLICATION);
		result = nautilus_mime_has_any_applications_for_uri (directory, file);
	}

	nautilus_directory_unref (directory);
	g_free (uri);

	return result;
}

/**
 * set_up_program_chooser:
 * 
 * Create but don't yet run a program-choosing dialog.
 * The caller should run the dialog and destroy it.
 * 
 * @file: Which NautilusFile programs are being chosen for.
 * @type: Which type of program is being chosen.
 * @parent: Optional window to parent the dialog on.
 * 
 * Return value: The program-choosing dialog, ready to be run.
 */
static GnomeDialog *
set_up_program_chooser (NautilusFile *file, 
			GnomeVFSMimeActionType type, 
			GtkWindow *parent)
{
	GnomeDialog *dialog;

	g_assert (NAUTILUS_IS_FILE (file));

	dialog = nautilus_program_chooser_new (type, file);
	if (parent != NULL) {
		gnome_dialog_set_parent (dialog, parent);
	}

	/* Don't destroy on close because callers will need 
	 * to extract some information from the dialog after 
	 * it closes.
	 */
	gnome_dialog_close_hides (dialog, TRUE);

	return dialog;	
}


/**
 * nautilus_choose_component_for_file:
 * 
 * Lets user choose a component with which to view a given file.
 * 
 * @file: The NautilusFile to be viewed.
 * @parent_window: If supplied, the component-choosing dialog is parented
 * on this window.
 * @callback: Callback called when choice has been made.
 * @callback_data: Parameter passed back when callback is called.
 */
void
nautilus_choose_component_for_file (NautilusFile *file,
				    GtkWindow *parent_window,
				    NautilusComponentChoiceCallback callback,
				    gpointer callback_data)
{
	NautilusViewIdentifier *identifier;
	GnomeDialog *dialog;
	gboolean any_choices;
	GnomeVFSMimeActionType action_type;

	g_return_if_fail (NAUTILUS_IS_FILE (file));
	g_return_if_fail (callback != NULL);

	action_type = GNOME_VFS_MIME_ACTION_TYPE_COMPONENT;

	/* The API uses a callback so we can do this non-modally in the future,
	 * but for now we just use a modal dialog.
	 */

	any_choices = any_programs_available_for_file (action_type, file);

	identifier = NULL;
	dialog = NULL;

	if (any_choices) {
		dialog = set_up_program_chooser 
			(file, action_type, parent_window);

		if (gnome_dialog_run (dialog) == GNOME_OK) {
			identifier = nautilus_program_chooser_get_component (dialog);
		}
	} else {
		nautilus_program_chooser_show_no_choices_message (action_type, file, parent_window);
	}
	 

	/* Call callback even if identifier is NULL, so caller can
	 * free callback_data if necessary and present some cancel
	 * UI if desired.
	 */
	(* callback) (identifier, callback_data);

	if (any_choices) {
		/* Destroy only after callback, since view identifier will
		 * be destroyed too.
		 */
		g_assert (dialog != NULL);
		gtk_widget_destroy (GTK_WIDGET (dialog));
	}
}				    

/**
 * nautilus_choose_application_for_file:
 * 
 * Lets user choose an application with which to open a given file.
 * 
 * @file: The NautilusFile to be viewed.
 * @parent_window: If supplied, the application-choosing dialog is parented
 * on this window.
 * @callback: Callback called when choice has been made.
 * @callback_data: Parameter passed back when callback is called.
 */
void
nautilus_choose_application_for_file (NautilusFile *file,
				      GtkWindow *parent_window,
				      NautilusApplicationChoiceCallback callback,
				      gpointer callback_data)
{
	GnomeDialog *dialog;
	GnomeVFSMimeApplication *application;
	gboolean any_choices;
	GnomeVFSMimeActionType action_type;

	g_return_if_fail (NAUTILUS_IS_FILE (file));
	g_return_if_fail (callback != NULL);

	action_type = GNOME_VFS_MIME_ACTION_TYPE_APPLICATION;

	/* The API uses a callback so we can do this non-modally in the future,
	 * but for now we just use a modal dialog.
	 */
	any_choices = any_programs_available_for_file (action_type, file);

	application = NULL;
	dialog = NULL;

	if (any_choices) {
		dialog = set_up_program_chooser 
			(file, action_type, parent_window);

		if (gnome_dialog_run (dialog) == GNOME_OK) {
			application = nautilus_program_chooser_get_application (dialog);
		}
	} else {
		nautilus_program_chooser_show_no_choices_message (action_type, file, parent_window);
	}	 

	/* Call callback even if identifier is NULL, so caller can
	 * free callback_data if necessary and present some cancel
	 * UI if desired.
	 */
	(* callback) (application, callback_data);

	if (any_choices) {
		/* Destroy only after callback, since application struct will
		 * be destroyed too.
		 */
		g_assert (dialog != NULL);
		gtk_widget_destroy (GTK_WIDGET (dialog));
	}
}				    

/**
 * nautilus_launch_application_parented:
 * 
 * Fork off a process to launch an application with a given uri as
 * a parameter. Provide a parent window for error dialogs.
 * 
 * @application: The application to be launched.
 * @uri: Passed as a parameter to the application.
 * @parent: A window to use as the parent for any error dialogs.
 * 
 */
void
nautilus_launch_application_parented (GnomeVFSMimeApplication *application, 
				      const char *uri, 
				      GtkWindow *parent)
{
	GnomeDialog *dialog;
	char *command_string;
	char *parameter;
	char *prompt;

	/* If the program can open URIs, always use a URI. This
	 * prevents any possible ambiguity for cases where a path
	 * would looks like a URI.
	 */
	if (application->can_open_uris) {
		parameter = g_strdup (uri);
	} else {
		parameter = gnome_vfs_get_local_path_from_uri (uri);
		if (parameter == NULL) {
			/* This application can't deal with this URI,
			 * because it can only handle local
			 * files. Tell user. Some day we could offer
			 * to copy it locally for the user, if we knew
			 * where to put it, and who would delete it
			 * when done.
			 */
			prompt = g_strdup_printf (_("Sorry, %s can only open local files, and "
						    "\"%s\" is remote. If you want to open it "
						    "with %s, make a local copy first."), 
						  application->name, uri, application->name);
			dialog = nautilus_error_dialog (prompt, _("Can't open remote file"), parent);
			g_free (prompt);
			return;
		}
	}
	
	if (application->requires_terminal) {
		command_string = g_strconcat ("gnome-terminal -x ", application->command, NULL);
	} else {
		command_string = g_strdup (application->command);
	}
	
	nautilus_launch_application_from_command (command_string, parameter);
	
	g_free (parameter);
	g_free (command_string);
}

/**
 * nautilus_launch_application:
 * 
 * Fork off a process to launch an application with a given uri as
 * a parameter.
 * 
 * @application: The application to be launched.
 * @uri: Passed as a parameter to the application.
 */
void
nautilus_launch_application (GnomeVFSMimeApplication *application, const char *uri)
{
	nautilus_launch_application_parented (application, uri, NULL);
}

/**
 * nautilus_launch_application_from_command:
 * 
 * Fork off a process to launch an application with a given uri as
 * a parameter.
 * 
 * @command_string: The application to be launched, with any desired
 * command-line options.
 * @parameter: Passed as a parameter to the application as is.
 */
void
nautilus_launch_application_from_command (const char *command_string, const char *parameter)
{
	char *full_command, *quoted_parameter;

	if (parameter != NULL) {
		quoted_parameter = nautilus_shell_quote (parameter);
		full_command = g_strconcat (command_string, " ", quoted_parameter, " &", NULL);
		g_free (quoted_parameter);
	} else {
		full_command = g_strconcat (command_string, " &", NULL);
	}

	system (full_command);

	g_free (full_command);
}