183
188
return app_infos;
185
190
/******************************************************************************/
191
/* Takes a null-terminated array of strings at least 2 items long. The first
192
* item is always the application to execute, including if it is a script or
193
* .desktop file. The second is the files to open. Either ARG_FILE or ARG_APP
194
* can be NULL, but not both. If ARG_FILE is NULL, just launch ARG_APP. If
195
* ARG_APP is NULL, open ARG_FILE with the default mime handler. */
187
vfs_launch_application (LaunchInfo *launch_info) {
188
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
191
GError *error = NULL;
193
gchar *working_dir = NULL;
195
if (launch_info->file) {
196
working_dir = (vfs_file_is_directory (launch_info->file)) ?
197
g_strdup (launch_info->file) :
198
g_path_get_dirname (launch_info->file);
201
working_dir = g_strdup (g_get_home_dir ());
204
g_strdelimit (launch_info->command, " ", '\1');
206
/* only add the file name as an argument if one was specified */
207
gchar *arg = (launch_info->file) ?
208
g_strconcat (launch_info->command, "\1", launch_info->file, NULL) :
209
g_strdup (launch_info->command);
211
gchar** args = g_strsplit (arg, "\1", 0);
213
/* need to do this to convert the '\2's back into spaces. */
214
g_strdelimit (args[0], "\2", ' ');
216
/* remove trailing spaces from all args */
219
g_strchomp (args[i]);
223
g_spawn_async_with_pipes (working_dir,
235
ret = utils_gerror_ok (&error, TRUE) ? ret : FALSE;
197
vfs_launch_application (const gchar *const *args) {
199
/* Can't have both arg[ARG_APP] and args[ARG_FILE} NULL */
200
g_return_val_if_fail ((args[ARG_APP] != NULL || args[ARG_FILE] != NULL), FALSE);
203
gboolean ret = FALSE;
204
GError *error = NULL;
206
gchar *command_line = NULL;
208
/* Put the file(s) in a list (only of there is a file) for g_app_info_launch() */
209
for (i = ARG_FILE; args[i]; i++) {
210
files = g_list_append (files, (gpointer)g_file_new_for_path (args[i]));
213
gchar *uri = args[ARG_FILE] ? g_filename_to_uri(args[ARG_FILE], NULL, NULL) : g_filename_to_uri(args[ARG_APP], NULL, NULL);
214
GtkRecentManager *recent = gtk_recent_manager_new ();
215
gtk_recent_manager_add_item (recent, uri);
216
g_object_unref (G_OBJECT(recent));
219
/* Set the current working dir. Do we use ARG_APP or ARG_FILE to set
220
* cwd? If ARG_FILE == NULL, use ARG_APP, otherwise use ARG_FILE */
221
const gchar *cwd_root = args[ARG_FILE] == NULL ? args[ARG_APP] : args[ARG_FILE];
222
gchar *working_dir = vfs_file_is_directory (cwd_root) ? g_strdup (cwd_root) : g_path_get_dirname (cwd_root);
223
g_chdir (working_dir);
239
224
g_free (working_dir);
226
/* No agrs[ARG_APP] was NULL, open file with default mime handler */
227
if (args[ARG_APP] == NULL) {
228
GFile *file = g_file_new_for_path (args[ARG_FILE]);
229
gchar *uri = g_file_get_uri (file);
230
ret = g_app_info_launch_default_for_uri (uri, NULL, &error);
231
utils_gerror_ok (&error, TRUE);
233
g_object_unref (file);
236
GAppInfo *app_info = NULL;
237
if (vfs_file_is_desktop (args[ARG_APP])) {
238
app_info = G_APP_INFO (g_desktop_app_info_new_from_filename (args[ARG_APP]));
241
command_line = vfs_file_is_executable (args[ARG_APP]) ?
242
utils_escape_str (args[ARG_APP], " ", "\\ ") : g_strdup (args[ARG_APP]);
243
app_info = g_app_info_create_from_commandline (command_line, NULL, 0, &error);
244
utils_gerror_ok (&error, TRUE);
248
ret = g_app_info_launch (app_info, files, NULL, &error);
249
utils_gerror_ok (&error, TRUE);
250
g_object_unref (app_info);
253
gchar *msg = g_strdup_printf (_("Could not open \"%s\".\n"), args[ARG_FILE]);
254
utils_show_dialog (_("Error"), msg, GTK_MESSAGE_ERROR);
258
g_list_foreach (files, (GFunc)g_object_unref, NULL);
260
g_free (command_line);
262
g_chdir (g_get_home_dir());
243
266
/******************************************************************************/
245
268
vfs_file_do_default_action (const gchar *file_name) {
246
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
248
LaunchInfo *launch_info = g_new0 (LaunchInfo, 1);
250
/*Try launching file as desktop file first*/
251
if (vfs_file_is_desktop (file_name)) {
252
GDesktopAppInfo *app_info = g_desktop_app_info_new_from_filename (file_name);
253
launch_info->command = g_strdup (g_app_info_get_executable (G_APP_INFO (app_info)));
254
launch_info->file = NULL;
255
g_object_unref (app_info);
257
/* run it if its an executable */
258
else if (vfs_file_is_executable (file_name)) {
259
launch_info->command = g_strdup (file_name);
260
/* need to do this in case the path or filename has spaces in it since
261
* the spaces are converted to '\1' in vfs_launch_application.
262
* vfs_launch_application will convert the '\2' back to a space. */
263
g_strdelimit (launch_info->command, " ", '\2');
264
launch_info->file = NULL;
269
gchar **args = g_strv_new (ARGS_SIZE);
270
gchar *_file_name = g_strdup (file_name);
272
if (vfs_file_is_desktop (_file_name) ||
273
vfs_file_is_executable (_file_name)) {
274
args[ARG_APP] = _file_name;
266
276
/* open it with the default mime app */
268
launch_info->command = vfs_get_default_mime_application (file_name);
269
launch_info->file = g_strdup (file_name);
272
gboolean ret = FALSE;
273
if (launch_info->command) {
274
ret = vfs_launch_application (launch_info);
277
gchar *msg = g_strdup_printf ("Could not display \"%s\".\n"
278
"There is no application installed for this file type.", file_name);
279
utils_show_dialog ("Error", msg, GTK_MESSAGE_ERROR);
283
g_free (launch_info->command);
284
g_free (launch_info->file);
285
g_free (launch_info);
278
args[ARG_FILE] = _file_name;
280
gboolean ret = vfs_launch_application ((const gchar*const*)args);
289
284
/******************************************************************************/