nautilus-mime-actions.c 62.6 KB
Newer Older
1
/* nautilus-mime-actions.c - uri-specific versions of mime action functions
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 *  Copyright (C) 2000, 2001 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,
 *  see <http://www.gnu.org/licenses/>.
 *
 *  Authors: Maciej Stachowiak <mjs@eazel.com>
 */
21 22

#include <config.h>
23

24
#include "nautilus-mime-actions.h"
Alexander Larsson's avatar
Alexander Larsson committed
25

26
#include "nautilus-window-slot.h"
27
#include "nautilus-application.h"
28

Alexander Larsson's avatar
Alexander Larsson committed
29 30 31
#include <eel/eel-glib-extensions.h>
#include <eel/eel-stock-dialogs.h>
#include <eel/eel-string.h>
32
#include <glib.h>
Alexander Larsson's avatar
Alexander Larsson committed
33
#include <glib/gi18n.h>
34
#include <glib/gstdio.h>
Alexander Larsson's avatar
Alexander Larsson committed
35
#include <string.h>
36
#include <gdk/gdkx.h>
Alexander Larsson's avatar
Alexander Larsson committed
37

38 39
#include "nautilus-file-attributes.h"
#include "nautilus-file.h"
40
#include "nautilus-file-utilities.h"
41 42 43 44 45
#include "nautilus-file-operations.h"
#include "nautilus-metadata.h"
#include "nautilus-program-choosing.h"
#include "nautilus-global-preferences.h"
#include "nautilus-signaller.h"
46
#include "nautilus-metadata.h"
47 48
#include "nautilus-ui-utilities.h"

49 50

#define DEBUG_FLAG NAUTILUS_DEBUG_MIME
51
#include "nautilus-debug.h"
Alexander Larsson's avatar
Alexander Larsson committed
52

53 54 55 56 57 58 59
typedef enum
{
    ACTIVATION_ACTION_ASK,
    ACTIVATION_ACTION_OPEN_IN_VIEW,
    ACTIVATION_ACTION_OPEN_IN_APPLICATION,
    ACTIVATION_ACTION_EXTRACT,
    ACTIVATION_ACTION_DO_NOTHING,
Alexander Larsson's avatar
Alexander Larsson committed
60 61
} ActivationAction;

62 63 64 65
typedef struct
{
    NautilusFile *file;
    char *uri;
66 67
} LaunchLocation;

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
typedef struct
{
    NautilusWindowSlot *slot;
    gpointer window;
    GtkWindow *parent_window;
    GCancellable *cancellable;
    GList *locations;
    GList *mountables;
    GList *start_mountables;
    GList *not_mounted;
    NautilusWindowOpenFlags flags;
    char *timed_wait_prompt;
    gboolean timed_wait_active;
    NautilusFileListHandle *files_handle;
    gboolean tried_mounting;
    gboolean user_confirmation;
Alexander Larsson's avatar
Alexander Larsson committed
84 85
} ActivateParameters;

86 87 88 89 90 91 92
typedef struct
{
    ActivateParameters *activation_params;
    GQueue *uris;
    GQueue *unhandled_uris;
} ApplicationLaunchParameters;

93
/* Microsoft mime types at https://blogs.msdn.microsoft.com/vsofficedeveloper/2008/05/08/office-2007-file-format-mime-types-for-http-content-streaming-2/ */
94 95 96 97 98 99 100
struct
{
    char *name;
    char *mimetypes[20];
} mimetype_groups[] =
{
    {
101 102 103
        N_("Anything"),
        { NULL }
    },
104
    {
105 106 107 108 109
        N_("Files"),
        { "application/octet-stream",
          "text/plain",
          NULL}
    },
110
    {
111 112 113 114
        N_("Folders"),
        { "inode/directory",
          NULL}
    },
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    { N_("Documents"),
      { "application/rtf",
        "application/msword",
        "application/vnd.sun.xml.writer",
        "application/vnd.sun.xml.writer.global",
        "application/vnd.sun.xml.writer.template",
        "application/vnd.oasis.opendocument.text",
        "application/vnd.oasis.opendocument.text-template",
        "application/x-abiword",
        "application/x-applix-word",
        "application/x-mswrite",
        "application/docbook+xml",
        "application/x-kword",
        "application/x-kword-crypt",
        "application/x-lyx",
130
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
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
        NULL}},
    { N_("Illustration"),
      { "application/illustrator",
        "application/vnd.corel-draw",
        "application/vnd.stardivision.draw",
        "application/vnd.oasis.opendocument.graphics",
        "application/x-dia-diagram",
        "application/x-karbon",
        "application/x-killustrator",
        "application/x-kivio",
        "application/x-kontour",
        "application/x-wpg",
        NULL}},
    { N_("Music"),
      { "application/ogg",
        "audio/x-vorbis+ogg",
        "audio/ac3",
        "audio/basic",
        "audio/midi",
        "audio/x-flac",
        "audio/mp4",
        "audio/mpeg",
        "audio/x-mpeg",
        "audio/x-ms-asx",
        "audio/x-pn-realaudio",
        NULL}},
    { N_("PDF / PostScript"),
      { "application/pdf",
        "application/postscript",
        "application/x-dvi",
        "image/x-eps",
162
        "image/vnd.djvu+multipage",
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
        NULL}},
    { N_("Picture"),
      { "application/vnd.oasis.opendocument.image",
        "application/x-krita",
        "image/bmp",
        "image/cgm",
        "image/gif",
        "image/jpeg",
        "image/jpeg2000",
        "image/png",
        "image/svg+xml",
        "image/tiff",
        "image/x-compressed-xcf",
        "image/x-pcx",
        "image/x-photo-cd",
        "image/x-psd",
        "image/x-tga",
        "image/x-xcf",
        NULL}},
    { N_("Presentation"),
      { "application/vnd.ms-powerpoint",
        "application/vnd.sun.xml.impress",
        "application/vnd.oasis.opendocument.presentation",
        "application/x-magicpoint",
        "application/x-kpresenter",
188
        "application/vnd.openxmlformats-officedocument.presentationml.presentation",
189 190 191 192 193 194 195 196 197 198 199 200 201 202
        NULL}},
    { N_("Spreadsheet"),
      { "application/vnd.lotus-1-2-3",
        "application/vnd.ms-excel",
        "application/vnd.stardivision.calc",
        "application/vnd.sun.xml.calc",
        "application/vnd.oasis.opendocument.spreadsheet",
        "application/x-applix-spreadsheet",
        "application/x-gnumeric",
        "application/x-kspread",
        "application/x-kspread-crypt",
        "application/x-quattropro",
        "application/x-sc",
        "application/x-siag",
203
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        NULL}},
    { N_("Text File"),
      { "text/plain",
        NULL}},
    { N_("Video"),
      { "video/mp4",
        "video/3gpp",
        "video/mpeg",
        "video/quicktime",
        "video/vivo",
        "video/x-avi",
        "video/x-mng",
        "video/x-ms-asf",
        "video/x-ms-wmv",
        "video/x-msvideo",
        "video/x-nsv",
        "video/x-real-video",
        NULL}}
222 223
};

Alexander Larsson's avatar
Alexander Larsson committed
224 225 226 227
/* Number of seconds until cancel dialog shows up */
#define DELAY_UNTIL_CANCEL_MSECS 5000

#define SILENT_WINDOW_OPEN_LIMIT 5
228
#define SILENT_OPEN_LIMIT 5
Alexander Larsson's avatar
Alexander Larsson committed
229 230 231 232 233 234 235 236

/* This number controls a maximum character count for a URL that is
 * displayed as part of a dialog. It's fairly arbitrary -- big enough
 * to allow most "normal" URIs to display in full, but small enough to
 * prevent the dialog from getting insanely wide.
 */
#define MAX_URI_IN_DIALOG_LENGTH 60

237 238 239 240 241 242 243 244
static void cancel_activate_callback (gpointer callback_data);
static void activate_activation_uris_ready_callback (GList   *files,
                                                     gpointer callback_data);
static void activation_mount_mountables (ActivateParameters *parameters);
static void activation_start_mountables (ActivateParameters *parameters);
static void activate_callback (GList   *files,
                               gpointer callback_data);
static void activation_mount_not_mounted (ActivateParameters *parameters);
Alexander Larsson's avatar
Alexander Larsson committed
245 246


247 248 249
static void
launch_location_free (LaunchLocation *location)
{
250 251 252
    nautilus_file_unref (location->file);
    g_free (location->uri);
    g_free (location);
253 254 255 256 257
}

static void
launch_location_list_free (GList *list)
{
258 259
    g_list_foreach (list, (GFunc) launch_location_free, NULL);
    g_list_free (list);
260 261 262 263 264
}

static GList *
get_file_list_for_launch_locations (GList *locations)
{
265 266
    GList *files, *l;
    LaunchLocation *location;
267

268 269 270 271
    files = NULL;
    for (l = locations; l != NULL; l = l->next)
    {
        location = l->data;
Alexander Larsson's avatar
Alexander Larsson committed
272

273 274 275 276
        files = g_list_prepend (files,
                                nautilus_file_ref (location->file));
    }
    return g_list_reverse (files);
277 278 279 280 281 282
}


static LaunchLocation *
launch_location_from_file (NautilusFile *file)
{
283 284 285 286 287 288
    LaunchLocation *location;
    location = g_new (LaunchLocation, 1);
    location->file = nautilus_file_ref (file);
    location->uri = nautilus_file_get_uri (file);

    return location;
289 290 291 292
}

static void
launch_location_update_from_file (LaunchLocation *location,
293
                                  NautilusFile   *file)
294
{
295 296 297 298
    nautilus_file_unref (location->file);
    g_free (location->uri);
    location->file = nautilus_file_ref (file);
    location->uri = nautilus_file_get_uri (file);
299 300 301 302
}

static void
launch_location_update_from_uri (LaunchLocation *location,
303
                                 const char     *uri)
304
{
305 306 307 308
    nautilus_file_unref (location->file);
    g_free (location->uri);
    location->file = nautilus_file_get_by_uri (uri);
    location->uri = g_strdup (uri);
309 310 311
}

static LaunchLocation *
312 313
find_launch_location_for_file (GList        *list,
                               NautilusFile *file)
314
{
315 316
    LaunchLocation *location;
    GList *l;
317

318 319 320
    for (l = list; l != NULL; l = l->next)
    {
        location = l->data;
321

322 323 324 325 326 327
        if (location->file == file)
        {
            return location;
        }
    }
    return NULL;
328 329 330 331 332
}

static GList *
launch_locations_from_file_list (GList *list)
{
333
    GList *new;
334

335 336 337 338 339 340 341 342 343
    new = NULL;
    while (list)
    {
        new = g_list_prepend (new,
                              launch_location_from_file (list->data));
        list = list->next;
    }
    new = g_list_reverse (new);
    return new;
344
}
Alexander Larsson's avatar
Alexander Larsson committed
345 346

static ApplicationLaunchParameters *
347 348
application_launch_parameters_new (ActivateParameters *activation_params,
                                   GQueue             *uris)
Alexander Larsson's avatar
Alexander Larsson committed
349
{
350
    ApplicationLaunchParameters *result;
Alexander Larsson's avatar
Alexander Larsson committed
351

352
    result = g_new0 (ApplicationLaunchParameters, 1);
353 354 355
    result->activation_params = activation_params;
    result->uris = uris;
    result->unhandled_uris = g_queue_new ();
Alexander Larsson's avatar
Alexander Larsson committed
356

357
    return result;
Alexander Larsson's avatar
Alexander Larsson committed
358 359
}

360
static gboolean
Alexander Larsson's avatar
Alexander Larsson committed
361
nautilus_mime_actions_check_if_required_attributes_ready (NautilusFile *file)
362
{
363 364
    NautilusFileAttributes attributes;
    gboolean ready;
365

366 367
    attributes = nautilus_mime_actions_get_required_file_attributes ();
    ready = nautilus_file_check_if_ready (file, attributes);
368

369
    return ready;
370 371
}

372
NautilusFileAttributes
Alexander Larsson's avatar
Alexander Larsson committed
373
nautilus_mime_actions_get_required_file_attributes (void)
374
{
375 376
    return NAUTILUS_FILE_ATTRIBUTE_INFO |
           NAUTILUS_FILE_ATTRIBUTE_LINK_INFO;
377
}
378

Alexander Larsson's avatar
Alexander Larsson committed
379
GAppInfo *
380
nautilus_mime_get_default_application_for_file (NautilusFile *file)
381
{
382 383 384
    GAppInfo *app;
    char *mime_type;
    char *uri_scheme;
385

386 387 388 389
    if (!nautilus_mime_actions_check_if_required_attributes_ready (file))
    {
        return NULL;
    }
Alexander Larsson's avatar
Alexander Larsson committed
390

391 392 393 394 395 396 397 398 399 400 401 402 403 404
    mime_type = nautilus_file_get_mime_type (file);
    app = g_app_info_get_default_for_type (mime_type,
                                           !nautilus_file_is_local_or_fuse (file));
    g_free (mime_type);

    if (app == NULL)
    {
        uri_scheme = nautilus_file_get_uri_scheme (file);
        if (uri_scheme != NULL)
        {
            app = g_app_info_get_default_for_uri_scheme (uri_scheme);
            g_free (uri_scheme);
        }
    }
Alexander Larsson's avatar
Alexander Larsson committed
405

406
    return app;
407
}
408

409 410
static int
file_compare_by_mime_type (NautilusFile *file_a,
411 412 413 414 415 416 417 418 419 420 421 422 423 424
                           NautilusFile *file_b)
{
    char *mime_type_a, *mime_type_b;
    int ret;

    mime_type_a = nautilus_file_get_mime_type (file_a);
    mime_type_b = nautilus_file_get_mime_type (file_b);

    ret = strcmp (mime_type_a, mime_type_b);

    g_free (mime_type_a);
    g_free (mime_type_b);

    return ret;
425 426 427 428
}

static int
file_compare_by_parent_uri (NautilusFile *file_a,
429 430 431 432
                            NautilusFile *file_b)
{
    char *parent_uri_a, *parent_uri_b;
    int ret;
433

434 435
    parent_uri_a = nautilus_file_get_parent_uri (file_a);
    parent_uri_b = nautilus_file_get_parent_uri (file_b);
436

437
    ret = strcmp (parent_uri_a, parent_uri_b);
438

439 440
    g_free (parent_uri_a);
    g_free (parent_uri_b);
441

442
    return ret;
443 444
}

Alexander Larsson's avatar
Alexander Larsson committed
445
GAppInfo *
446 447
nautilus_mime_get_default_application_for_files (GList *files)
{
448 449 450
    GList *l, *sorted_files;
    NautilusFile *file;
    GAppInfo *app, *one_app;
451

452
    g_assert (files != NULL);
453

454
    sorted_files = g_list_sort (g_list_copy (files), (GCompareFunc) file_compare_by_mime_type);
455

456 457 458 459
    app = NULL;
    for (l = sorted_files; l != NULL; l = l->next)
    {
        file = l->data;
460

461 462 463 464 465 466
        if (l->prev &&
            file_compare_by_mime_type (file, l->prev->data) == 0 &&
            file_compare_by_parent_uri (file, l->prev->data) == 0)
        {
            continue;
        }
467

468 469 470 471 472 473 474 475 476 477 478 479 480 481
        one_app = nautilus_mime_get_default_application_for_file (file);
        if (one_app == NULL || (app != NULL && !g_app_info_equal (app, one_app)))
        {
            if (app)
            {
                g_object_unref (app);
            }
            if (one_app)
            {
                g_object_unref (one_app);
            }
            app = NULL;
            break;
        }
482

483 484 485 486 487 488 489 490 491
        if (app == NULL)
        {
            app = one_app;
        }
        else
        {
            g_object_unref (one_app);
        }
    }
492

493
    g_list_free (sorted_files);
494

495
    return app;
496 497
}

Alexander Larsson's avatar
Alexander Larsson committed
498
static void
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
trash_or_delete_files (GtkWindow   *parent_window,
                       const GList *files,
                       gboolean     delete_if_all_already_in_trash)
{
    GList *locations;
    const GList *node;

    locations = NULL;
    for (node = files; node != NULL; node = node->next)
    {
        locations = g_list_prepend (locations,
                                    nautilus_file_get_location ((NautilusFile *) node->data));
    }

    locations = g_list_reverse (locations);

    nautilus_file_operations_trash_or_delete (locations,
                                              parent_window,
                                              NULL, NULL);
    g_list_free_full (locations, g_object_unref);
Alexander Larsson's avatar
Alexander Larsson committed
519 520 521
}

static void
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
report_broken_symbolic_link (GtkWindow    *parent_window,
                             NautilusFile *file)
{
    char *target_path;
    char *display_name;
    char *prompt;
    char *detail;
    GtkDialog *dialog;
    GList file_as_list;
    int response;
    gboolean can_trash;

    g_assert (nautilus_file_is_broken_symbolic_link (file));

    display_name = nautilus_file_get_display_name (file);
    can_trash = nautilus_file_can_trash (file) && !nautilus_file_is_in_trash (file);

    if (can_trash)
    {
        prompt = g_strdup_printf (_("The link “%s” is broken. Move it to Trash?"), display_name);
    }
    else
    {
        prompt = g_strdup_printf (_("The link “%s” is broken."), display_name);
    }
    g_free (display_name);

    target_path = nautilus_file_get_symbolic_link_target_path (file);
    if (target_path == NULL)
    {
        detail = g_strdup (_("This link cannot be used because it has no target."));
    }
    else
    {
        detail = g_strdup_printf (_("This link cannot be used because its target "
557
                                    "“%s” doesn’t exist."), target_path);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    }

    if (!can_trash)
    {
        eel_run_simple_dialog (GTK_WIDGET (parent_window), FALSE, GTK_MESSAGE_WARNING,
                               prompt, detail, _("_Cancel"), NULL);
        goto out;
    }

    dialog = eel_show_yes_no_dialog (prompt, detail, _("Mo_ve to Trash"), _("_Cancel"),
                                     parent_window);

    gtk_dialog_set_default_response (dialog, GTK_RESPONSE_CANCEL);

    /* Make this modal to avoid problems with reffing the view & file
     * to keep them around in case the view changes, which would then
     * cause the old view not to be destroyed, which would cause its
     * merged Bonobo items not to be un-merged. Maybe we need to unmerge
     * explicitly when disconnecting views instead of relying on the
     * unmerge in Destroy. But since BonoboUIHandler is probably going
     * to change wildly, I don't want to mess with this now.
     */

    response = gtk_dialog_run (dialog);
    gtk_widget_destroy (GTK_WIDGET (dialog));

    if (response == GTK_RESPONSE_YES)
    {
        file_as_list.data = file;
        file_as_list.next = NULL;
        file_as_list.prev = NULL;
        trash_or_delete_files (parent_window, &file_as_list, TRUE);
    }
Alexander Larsson's avatar
Alexander Larsson committed
591 592

out:
593 594 595
    g_free (prompt);
    g_free (target_path);
    g_free (detail);
Alexander Larsson's avatar
Alexander Larsson committed
596 597 598 599 600
}

static ActivationAction
get_activation_action (NautilusFile *file)
{
601 602
    ActivationAction action;
    char *activation_uri;
603
    gboolean handles_extract = FALSE;
604
    g_autoptr (GAppInfo) app_info = NULL;
605
    const gchar *app_id;
606 607

    app_info = nautilus_mime_get_default_application_for_file (file);
608 609 610 611 612
    if (app_info != NULL)
    {
        app_id = g_app_info_get_id (app_info);
        handles_extract = g_strcmp0 (app_id, NAUTILUS_DESKTOP_ID) == 0;
    }
613
    if (handles_extract && nautilus_file_is_archive (file))
614 615 616 617 618 619 620 621 622 623
    {
        return ACTIVATION_ACTION_EXTRACT;
    }

    activation_uri = nautilus_file_get_activation_uri (file);
    if (activation_uri == NULL)
    {
        activation_uri = nautilus_file_get_uri (file);
    }

624
    if (nautilus_file_opens_in_view (file))
625
    {
626
        action = ACTIVATION_ACTION_OPEN_IN_VIEW;
627
    }
628
    else
629
    {
630
        action = ACTIVATION_ACTION_OPEN_IN_APPLICATION;
631 632
    }
    g_free (activation_uri);
633

634
    return action;
Alexander Larsson's avatar
Alexander Larsson committed
635 636
}

637 638 639
gboolean
nautilus_mime_file_extracts (NautilusFile *file)
{
640
    return get_activation_action (file) == ACTIVATION_ACTION_EXTRACT;
641 642
}

Alexander Larsson's avatar
Alexander Larsson committed
643 644 645
gboolean
nautilus_mime_file_opens_in_external_app (NautilusFile *file)
{
646 647 648 649 650
    ActivationAction activation_action;

    activation_action = get_activation_action (file);

    return (activation_action == ACTIVATION_ACTION_OPEN_IN_APPLICATION);
Alexander Larsson's avatar
Alexander Larsson committed
651 652 653 654 655
}

static gboolean
file_was_cancelled (NautilusFile *file)
{
656 657 658 659 660 661 662
    GError *error;

    error = nautilus_file_get_file_info_error (file);
    return
        error != NULL &&
        error->domain == G_IO_ERROR &&
        error->code == G_IO_ERROR_CANCELLED;
Alexander Larsson's avatar
Alexander Larsson committed
663 664 665 666 667
}

static gboolean
file_was_not_mounted (NautilusFile *file)
{
668 669 670 671 672 673 674
    GError *error;

    error = nautilus_file_get_file_info_error (file);
    return
        error != NULL &&
        error->domain == G_IO_ERROR &&
        error->code == G_IO_ERROR_NOT_MOUNTED;
Alexander Larsson's avatar
Alexander Larsson committed
675 676 677 678 679
}

static void
activation_parameters_free (ActivateParameters *parameters)
{
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
    if (parameters->timed_wait_active)
    {
        eel_timed_wait_stop (cancel_activate_callback, parameters);
    }

    if (parameters->slot)
    {
        g_object_remove_weak_pointer (G_OBJECT (parameters->slot), (gpointer *) &parameters->slot);
    }
    if (parameters->parent_window)
    {
        g_object_remove_weak_pointer (G_OBJECT (parameters->parent_window), (gpointer *) &parameters->parent_window);
    }
    g_object_unref (parameters->cancellable);
    launch_location_list_free (parameters->locations);
    nautilus_file_list_free (parameters->mountables);
    nautilus_file_list_free (parameters->start_mountables);
    nautilus_file_list_free (parameters->not_mounted);
    g_free (parameters->timed_wait_prompt);
    g_assert (parameters->files_handle == NULL);
    g_free (parameters);
Alexander Larsson's avatar
Alexander Larsson committed
701 702
}

703 704 705 706 707 708 709 710 711 712
static void
application_launch_parameters_free (ApplicationLaunchParameters *parameters)
{
    g_queue_free (parameters->unhandled_uris);
    g_queue_free (parameters->uris);
    activation_parameters_free (parameters->activation_params);

    g_free (parameters);
}

Alexander Larsson's avatar
Alexander Larsson committed
713 714 715
static void
cancel_activate_callback (gpointer callback_data)
{
716
    ActivateParameters *parameters = callback_data;
717

718
    parameters->timed_wait_active = FALSE;
719

720 721 722 723 724 725 726 727
    g_cancellable_cancel (parameters->cancellable);

    if (parameters->files_handle)
    {
        nautilus_file_list_cancel_call_when_ready (parameters->files_handle);
        parameters->files_handle = NULL;
        activation_parameters_free (parameters);
    }
Alexander Larsson's avatar
Alexander Larsson committed
728 729 730 731 732
}

static void
activation_start_timed_cancel (ActivateParameters *parameters)
{
733 734 735 736 737 738 739
    parameters->timed_wait_active = TRUE;
    eel_timed_wait_start_with_duration
        (DELAY_UNTIL_CANCEL_MSECS,
        cancel_activate_callback,
        parameters,
        parameters->timed_wait_prompt,
        parameters->parent_window);
Alexander Larsson's avatar
Alexander Larsson committed
740 741
}

742 743 744
static void
pause_activation_timed_cancel (ActivateParameters *parameters)
{
745 746 747 748 749
    if (parameters->timed_wait_active)
    {
        eel_timed_wait_stop (cancel_activate_callback, parameters);
        parameters->timed_wait_active = FALSE;
    }
750 751 752 753 754
}

static void
unpause_activation_timed_cancel (ActivateParameters *parameters)
{
755 756 757 758
    if (!parameters->timed_wait_active)
    {
        activation_start_timed_cancel (parameters);
    }
759 760 761
}


Alexander Larsson's avatar
Alexander Larsson committed
762
static void
763 764 765
activate_mount_op_active (GtkMountOperation  *operation,
                          GParamSpec         *pspec,
                          ActivateParameters *parameters)
Alexander Larsson's avatar
Alexander Larsson committed
766
{
767
    gboolean is_active;
768

769
    g_object_get (operation, "is-showing", &is_active, NULL);
770

771 772 773 774 775 776 777 778
    if (is_active)
    {
        pause_activation_timed_cancel (parameters);
    }
    else
    {
        unpause_activation_timed_cancel (parameters);
    }
Alexander Larsson's avatar
Alexander Larsson committed
779 780 781
}

static gboolean
782
confirm_multiple_windows (GtkWindow *parent_window,
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
                          int        count,
                          gboolean   use_tabs)
{
    GtkDialog *dialog;
    char *prompt;
    char *detail;
    int response;

    if (count <= SILENT_WINDOW_OPEN_LIMIT)
    {
        return TRUE;
    }

    prompt = _("Are you sure you want to open all files?");
    if (use_tabs)
    {
        detail = g_strdup_printf (ngettext ("This will open %d separate tab.",
                                            "This will open %d separate tabs.", count), count);
    }
    else
    {
        detail = g_strdup_printf (ngettext ("This will open %d separate window.",
                                            "This will open %d separate windows.", count), count);
    }
    dialog = eel_show_yes_no_dialog (prompt, detail,
                                     _("_OK"), _("_Cancel"),
                                     parent_window);
    g_free (detail);

    response = gtk_dialog_run (dialog);
    gtk_widget_destroy (GTK_WIDGET (dialog));

    return response == GTK_RESPONSE_YES;
}

typedef struct
{
    NautilusWindowSlot *slot;
    GtkWindow *parent_window;
    NautilusFile *file;
    GList *files;
    NautilusWindowOpenFlags flags;
    gboolean user_confirmation;
    char *uri;
    GDBusProxy *proxy;
    GtkWidget *dialog;
829 830 831 832 833
} ActivateParametersInstall;

static void
activate_parameters_install_free (ActivateParametersInstall *parameters_install)
{
834 835 836 837 838 839 840 841
    if (parameters_install->slot)
    {
        g_object_remove_weak_pointer (G_OBJECT (parameters_install->slot), (gpointer *) &parameters_install->slot);
    }
    if (parameters_install->parent_window)
    {
        g_object_remove_weak_pointer (G_OBJECT (parameters_install->parent_window), (gpointer *) &parameters_install->parent_window);
    }
842

843 844 845 846
    if (parameters_install->proxy != NULL)
    {
        g_object_unref (parameters_install->proxy);
    }
847

848 849 850 851
    nautilus_file_unref (parameters_install->file);
    nautilus_file_list_free (parameters_install->files);
    g_free (parameters_install->uri);
    g_free (parameters_install);
852 853
}

854
static char *
855 856
get_application_no_mime_type_handler_message (NautilusFile *file,
                                              char         *uri)
857
{
858 859 860
    char *uri_for_display;
    char *name;
    char *error_message;
861

862
    name = nautilus_file_get_display_name (file);
863

864 865 866 867 868 869 870 871
    /* Truncate the URI so it doesn't get insanely wide. Note that even
     * though the dialog uses wrapped text, if the URI doesn't contain
     * white space then the text-wrapping code is too stupid to wrap it.
     */
    uri_for_display = eel_str_middle_truncate (name, MAX_URI_IN_DIALOG_LENGTH);
    error_message = g_strdup_printf (_("Could not display “%s”."), uri_for_display);
    g_free (uri_for_display);
    g_free (name);
872

873
    return error_message;
874 875
}

876
static void
877
open_with_response_cb (GtkDialog *dialog,
878 879 880 881 882 883 884 885 886 887 888 889 890 891
                       gint       response_id,
                       gpointer   user_data)
{
    GtkWindow *parent_window;
    NautilusFile *file;
    GList files;
    GAppInfo *info;
    ActivateParametersInstall *parameters = user_data;

    if (response_id != GTK_RESPONSE_OK)
    {
        gtk_widget_destroy (GTK_WIDGET (dialog));
        return;
    }
892

893 894 895
    parent_window = parameters->parent_window;
    file = g_object_get_data (G_OBJECT (dialog), "mime-action:file");
    info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (dialog));
896

897
    gtk_widget_destroy (GTK_WIDGET (dialog));
898

899
    g_signal_emit_by_name (nautilus_signaller_get_current (), "mime-data-changed");
900

901 902 903 904
    files.next = NULL;
    files.prev = NULL;
    files.data = file;
    nautilus_launch_application (info, &files, parent_window);
905

906
    g_object_unref (info);
907

908
    activate_parameters_install_free (parameters);
909 910 911
}

static void
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
choose_program (GtkDialog *message_dialog,
                int        response,
                gpointer   callback_data)
{
    GtkWidget *dialog;
    NautilusFile *file;
    GFile *location;
    ActivateParametersInstall *parameters = callback_data;

    if (response != GTK_RESPONSE_ACCEPT)
    {
        gtk_widget_destroy (GTK_WIDGET (message_dialog));
        activate_parameters_install_free (parameters);
        return;
    }
927

928
    file = g_object_get_data (G_OBJECT (message_dialog), "mime-action:file");
929

930
    g_assert (NAUTILUS_IS_FILE (file));
931

932 933
    location = nautilus_file_get_location (file);
    nautilus_file_ref (file);
934

935 936
    /* Destroy the message dialog after ref:ing the file */
    gtk_widget_destroy (GTK_WIDGET (message_dialog));
937

938 939 940 941 942 943 944
    dialog = gtk_app_chooser_dialog_new (parameters->parent_window,
                                         GTK_DIALOG_MODAL,
                                         location);
    g_object_set_data_full (G_OBJECT (dialog),
                            "mime-action:file",
                            nautilus_file_ref (file),
                            (GDestroyNotify) nautilus_file_unref);
945

946
    gtk_widget_show (dialog);
947

948 949 950 951
    g_signal_connect (dialog,
                      "response",
                      G_CALLBACK (open_with_response_cb),
                      parameters);
952

953 954
    g_object_unref (location);
    nautilus_file_unref (file);
955 956
}

957 958 959
static void
show_unhandled_type_error (ActivateParametersInstall *parameters)
{
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
    GtkWidget *dialog;

    char *mime_type = nautilus_file_get_mime_type (parameters->file);
    char *error_message = get_application_no_mime_type_handler_message (parameters->file, parameters->uri);
    if (g_content_type_is_unknown (mime_type))
    {
        dialog = gtk_message_dialog_new (parameters->parent_window,
                                         GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
                                         GTK_MESSAGE_ERROR,
                                         0,
                                         "%s", error_message);
        gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                  _("The file is of an unknown type"));
    }
    else
    {
        char *text;
        text = g_strdup_printf (_("There is no application installed for “%s” files"), g_content_type_get_description (mime_type));

        dialog = gtk_message_dialog_new (parameters->parent_window,
                                         GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
                                         GTK_MESSAGE_ERROR,
                                         0,
                                         "%s", error_message);
        gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                  "%s", text);

        g_free (text);
    }
989

990
    gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Select Application"), GTK_RESPONSE_ACCEPT);
991

992
    gtk_dialog_add_button (GTK_DIALOG (dialog), _("_OK"), GTK_RESPONSE_OK);
993

994
    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
995

996 997 998 999
    g_object_set_data_full (G_OBJECT (dialog),
                            "mime-action:file",
                            nautilus_file_ref (parameters->file),
                            (GDestroyNotify) nautilus_file_unref);
1000

1001
    gtk_widget_show (GTK_WIDGET (dialog));
1002

1003 1004
    g_signal_connect (dialog, "response",
                      G_CALLBACK (choose_program), parameters);
1005

1006 1007
    g_free (error_message);
    g_free (mime_type);
1008 1009
}

1010
static void
1011
search_for_application_dbus_call_notify_cb (GDBusProxy   *proxy,
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
                                            GAsyncResult *result,
                                            gpointer      user_data)
{
    ActivateParametersInstall *parameters_install = user_data;
    GVariant *variant;
    GError *error = NULL;

    variant = g_dbus_proxy_call_finish (proxy, result, &error);
    if (variant == NULL)
    {
        if (!g_dbus_error_is_remote_error (error) ||
            g_strcmp0 (g_dbus_error_get_remote_error (error), "org.freedesktop.PackageKit.Modify.Failed") == 0)
        {
            char *message;

            message = g_strdup_printf ("%s\n%s",
                                       _("There was an internal error trying to search for applications:"),
                                       error->message);
1030 1031 1032 1033
            show_dialog (_("Unable to search for application"),
                         message,
                         parameters_install->parent_window,
                         GTK_MESSAGE_ERROR);
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
            g_free (message);
        }
        else
        {
            g_warning ("Error while trying to search for applications: %s",
                       error->message);
        }

        g_error_free (error);
        activate_parameters_install_free (parameters_install);
        return;
    }

    g_variant_unref (variant);

    /* activate the file again */
    nautilus_mime_activate_files (parameters_install->parent_window,
                                  parameters_install->slot,
                                  parameters_install->files,
                                  parameters_install->flags,
                                  parameters_install->user_confirmation);

    activate_parameters_install_free (parameters_install);
1057 1058 1059
}

static void
1060 1061
search_for_application_mime_type (ActivateParametersInstall *parameters_install,
                                  const gchar               *mime_type)
1062
{
1063 1064 1065
    GdkWindow *window;
    guint xid = 0;
    const char *mime_types[2];
1066

1067
    g_assert (parameters_install->proxy != NULL);
1068

1069 1070 1071 1072 1073 1074
    /* get XID from parent window */
    window = gtk_widget_get_window (GTK_WIDGET (parameters_install->parent_window));
    if (window != NULL)
    {
        xid = GDK_WINDOW_XID (window);
    }
1075

1076 1077
    mime_types[0] = mime_type;
    mime_types[1] = NULL;
1078

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    g_dbus_proxy_call (parameters_install->proxy,
                       "InstallMimeTypes",
                       g_variant_new ("(u^ass)",
                                      xid,
                                      mime_types,
                                      "hide-confirm-search"),
                       G_DBUS_CALL_FLAGS_NONE,
                       G_MAXINT /* no timeout */,
                       NULL /* cancellable */,
                       (GAsyncReadyCallback) search_for_application_dbus_call_notify_cb,
                       parameters_install);
1090

1091
    DEBUG ("InstallMimeType method invoked for %s", mime_type);
1092 1093 1094
}

static void
1095 1096
application_unhandled_file_install (GtkDialog                 *dialog,
                                    gint                       response_id,
1097
                                    ActivateParametersInstall *parameters_install)
1098
{
1099
    char *mime_type;
1100

1101 1102
    gtk_widget_destroy (GTK_WIDGET (dialog));
    parameters_install->dialog = NULL;
1103

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
    if (response_id == GTK_RESPONSE_YES)
    {
        mime_type = nautilus_file_get_mime_type (parameters_install->file);
        search_for_application_mime_type (parameters_install, mime_type);
        g_free (mime_type);
    }
    else
    {
        /* free as we're not going to get the async dbus callback */
        activate_parameters_install_free (parameters_install);
    }
1115 1116
}

1117 1118 1119
static gboolean
delete_cb (GtkDialog *dialog)
{
1120 1121
    gtk_dialog_response (dialog, GTK_RESPONSE_DELETE_EVENT);
    return TRUE;
1122 1123
}

1124
static void
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
pk_proxy_appeared_cb (GObject      *source,
                      GAsyncResult *res,
                      gpointer      user_data)
{
    ActivateParametersInstall *parameters_install = user_data;
    char *mime_type, *name_owner;
    char *error_message;
    GtkWidget *dialog;
    GDBusProxy *proxy;
    GError *error = NULL;

    proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
    name_owner = g_dbus_proxy_get_name_owner (proxy);

    if (error != NULL || name_owner == NULL)
    {
        g_warning ("Couldn't call Modify on the PackageKit interface: %s",
                   error != NULL ? error->message : "no owner for PackageKit");
        g_clear_error (&error);

        /* show an unhelpful dialog */
        show_unhandled_type_error (parameters_install);

        return;
    }

    g_free (name_owner);

    mime_type = nautilus_file_get_mime_type (parameters_install->file);
    error_message = get_application_no_mime_type_handler_message (parameters_install->file,
                                                                  parameters_install->uri);
    /* use a custom dialog to prompt the user to install new software */
    dialog = gtk_message_dialog_new (parameters_install->parent_window, 0,
                                     GTK_MESSAGE_ERROR,
                                     GTK_BUTTONS_YES_NO,
                                     "%s", error_message);
    gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                              _("There is no application installed for “%s” files.\n"
                                                "Do you want to search for an application to open this file?"),
                                              g_content_type_get_description (mime_type));
    gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);

    parameters_install->dialog = dialog;
    parameters_install->proxy = proxy;

    g_signal_connect (dialog, "response",
                      G_CALLBACK (application_unhandled_file_install),
                      parameters_install);
    g_signal_connect (dialog, "delete-event",
                      G_CALLBACK (delete_cb), NULL);
    gtk_widget_show_all (dialog);
    g_free (mime_type);
1177 1178 1179
}

static void
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
application_unhandled_uri (ActivateParameters *parameters,
                           char               *uri)
{
    gboolean show_install_mime;
    char *mime_type;
    NautilusFile *file;
    ActivateParametersInstall *parameters_install;

    file = nautilus_file_get_by_uri (uri);

    mime_type = nautilus_file_get_mime_type (file);

    /* copy the parts of parameters we are interested in as the orignal will be unref'd */
    parameters_install = g_new0 (ActivateParametersInstall, 1);
    parameters_install->slot = parameters->slot;
    g_object_add_weak_pointer (G_OBJECT (parameters_install->slot), (gpointer *) &parameters_install->slot);
    if (parameters->parent_window)
    {
        parameters_install->parent_window = parameters->parent_window;
        g_object_add_weak_pointer (G_OBJECT (parameters_install->parent_window), (gpointer *) &parameters_install->parent_window);
    }
    parameters_install->file = file;
    parameters_install->files = get_file_list_for_launch_locations (parameters->locations);
    parameters_install->flags = parameters->flags;
    parameters_install->user_confirmation = parameters->user_confirmation;
    parameters_install->uri = g_strdup (uri);
1206 1207

#ifdef ENABLE_PACKAGEKIT
1208 1209
    /* allow an admin to disable the PackageKit search functionality */
    show_install_mime = g_settings_get_boolean (nautilus_preferences, NAUTILUS_PREFERENCES_INSTALL_MIME_ACTIVATION);
1210
#else
1211 1212
    /* we have no install functionality */
    show_install_mime = FALSE;
1213
#endif
1214 1215 1216 1217 1218
    /* There is no use trying to look for handlers of application/octet-stream */
    if (g_content_type_is_unknown (mime_type))
    {
        show_install_mime = FALSE;
    }
1219

1220
    g_free (mime_type);
1221

1222 1223 1224 1225
    if (!show_install_mime)
    {
        goto out;
    }
1226

1227 1228 1229 1230 1231 1232 1233 1234 1235
    g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
                              G_DBUS_PROXY_FLAGS_NONE,
                              NULL,
                              "org.freedesktop.PackageKit",
                              "/org/freedesktop/PackageKit",
                              "org.freedesktop.PackageKit.Modify",
                              NULL,
                              pk_proxy_appeared_cb,
                              parameters_install);
1236

1237
    return;
1238 1239

out:
1240 1241
    /* show an unhelpful dialog */
    show_unhandled_type_error (parameters_install);
1242 1243
}

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
static void
on_launch_default_for_uri (GObject      *source_object,
                           GAsyncResult *res,
                           gpointer      user_data)
{
    ApplicationLaunchParameters *params;
    ActivateParameters *activation_params;
    char *uri;
    gboolean sandboxed;
    GError *error = NULL;

    params = user_data;
    activation_params = params->activation_params;
    uri = g_queue_pop_head (params->uris);
    sandboxed = g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS);

    nautilus_launch_default_for_uri_finish (res, &error);
1261
    if (!sandboxed && error != NULL && error->code != G_IO_ERROR_CANCELLED)
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
    {
        g_queue_push_tail (params->unhandled_uris, uri);
    }

    if (!g_queue_is_empty (params->uris))
    {
        nautilus_launch_default_for_uri_async (g_queue_peek_head (params->uris),
                                               activation_params->parent_window,
                                               activation_params->cancellable,
                                               on_launch_default_for_uri,
                                               params);
    }
    else
    {
        gboolean should_close;
        NautilusWindow *window;

        should_close = activation_params->flags &
                       NAUTILUS_WINDOW_OPEN_FLAG_CLOSE_BEHIND;
        window = nautilus_window_slot_get_window (activation_params->slot);

        if (should_close && window != NULL)
        {
            nautilus_window_close (window);
        }
        else
        {
            while ((uri = g_queue_pop_head (params->unhandled_uris)) != NULL)
            {
                application_unhandled_uri (activation_params, uri);
            }
        }

        application_launch_parameters_free (params);
    }
}

Alexander Larsson's avatar
Alexander Larsson committed
1299 1300 1301
static void
activate_files (ActivateParameters *parameters)
{
1302
    NautilusFile *file;
1303 1304 1305
    NautilusWindow *window;
    NautilusWindowOpenFlags flags;
    int count;
1306
    g_autofree char *old_working_dir = NULL;
1307
    gboolean closed_window;
1308 1309 1310 1311
    g_autoptr (GQueue) open_in_app_uris = NULL;
    g_autoptr (GQueue) open_in_view_files = NULL;
    GList *l;
    ActivationAction action;
1312

1313 1314
    open_in_view_files = g_queue_new ();
    open_in_app_uris = g_queue_new ();
1315 1316 1317

    for (l = parameters->locations; l != NULL; l = l->next)
    {
1318 1319
        LaunchLocation *location;

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
        location = l->data;
        file = location->file;

        if (file_was_cancelled (file))
        {
            continue;
        }

        action = get_activation_action (file);
        switch (action)
        {
            case ACTIVATION_ACTION_OPEN_IN_VIEW:
1332 1333 1334 1335
            {
                g_queue_push_tail (open_in_view_files, file);
            }
            break;
1336 1337

            case ACTIVATION_ACTION_OPEN_IN_APPLICATION:
1338 1339 1340 1341
            {
                g_queue_push_tail (open_in_app_uris, location->uri);
            }
            break;
1342 1343

            case ACTIVATION_ACTION_DO_NOTHING:
1344 1345 1346
            {
            }
            break;
1347 1348

            case ACTIVATION_ACTION_EXTRACT:
1349
            {
1350 1351
                /* Extraction of files should be handled in the view */
                g_assert_not_reached ();
1352 1353
            }
            break;
1354

1355
            default:
1356 1357 1358 1359
            {
                g_assert_not_reached ();
            }
            break;
1360 1361 1362
        }
    }

1363
    count = g_queue_get_length (open_in_view_files);
1364 1365 1366 1367 1368 1369

    flags = parameters->flags;
    if (count > 1)
    {
        if ((parameters->flags & NAUTILUS_WINDOW_OPEN_FLAG_NEW_WINDOW) == 0)
        {
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
            /* if CLOSE_BEHIND is set and we have a directory to be activated, we
             * will first have to open a new window and after that we can open the
             * rest of files in tabs */
            if ((parameters->flags & NAUTILUS_WINDOW_OPEN_FLAG_CLOSE_BEHIND) != 0)
            {
                flags |= NAUTILUS_WINDOW_OPEN_FLAG_NEW_WINDOW;
            }
            else
            {
                flags |= NAUTILUS_WINDOW_OPEN_FLAG_NEW_TAB;
            }
1381 1382 1383 1384 1385 1386
        }
        else
        {
            flags |= NAUTILUS_WINDOW_OPEN_FLAG_NEW_WINDOW;
        }
    }
1387 1388
    else
    {
1389 1390 1391 1392 1393 1394
        /* if we want to close the window and activate a single directory, then we will need
         * the NEW_WINDOW flag set */
        if ((parameters->flags & NAUTILUS_WINDOW_OPEN_FLAG_CLOSE_BEHIND) != 0)
        {
            flags |= NAUTILUS_WINDOW_OPEN_FLAG_NEW_WINDOW;
        }
1395
    }
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410

    if (parameters->slot != NULL &&
        (!parameters->user_confirmation ||
         confirm_multiple_windows (parameters->parent_window, count,
                                   (flags & NAUTILUS_WINDOW_OPEN_FLAG_NEW_TAB) != 0)))
    {
        if ((flags & NAUTILUS_WINDOW_OPEN_FLAG_NEW_TAB) != 0 &&
            g_settings_get_enum (nautilus_preferences, NAUTILUS_PREFERENCES_NEW_TAB_POSITION) ==
            NAUTILUS_NEW_TAB_POSITION_AFTER_CURRENT_TAB)
        {
            /* When inserting N tabs after the current one,
             * we first open tab N, then tab N-1, ..., then tab 0.
             * Each of them is appended to the current tab, i.e.
             * prepended to the list of tabs to open.
             */
1411
            g_queue_reverse (open_in_view_files);
1412 1413
        }

1414
        closed_window = FALSE;
1415

1416
        for (l = g_queue_peek_head_link (open_in_view_files); l != NULL; l = l->next)
1417
        {
1418 1419 1420
            g_autofree char *uri = NULL;
            g_autoptr (GFile) location = NULL;
            g_autoptr (GFile) location_with_permissions = NULL;
1421 1422 1423 1424
            /* The ui should ask for navigation or object windows
             * depending on what the current one is */
            file = NAUTILUS_FILE (l->data);
            uri = nautilus_file_get_activation_uri (file);
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
            location = g_file_new_for_uri (uri);
            if (g_file_is_native (location) &&
                (nautilus_file_is_in_admin (file) ||
                 !nautilus_file_can_read (file) ||
                 !nautilus_file_can_execute (file)))
            {
                g_autofree gchar *file_path = NULL;

                g_free (uri);

                file_path = g_file_get_path (location);
                uri = g_strconcat ("admin://", file_path, NULL);
            }

            location_with_permissions = g_file_new_for_uri (uri);
1440 1441 1442 1443 1444
            /* FIXME: we need to pass the parent_window, but we only use it for the current active window,
             * which nautilus-application should take care of. However is not working and creating regressions
             * in some cases. Until we figure out what's going on, continue to use the parameters->slot
             * to make splicit the window we want to use for activating the files */
            nautilus_application_open_location_full (NAUTILUS_APPLICATION (g_application_get_default ()),
1445
                                                     location_with_permissions, flags, NULL, NULL, parameters->slot);
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461

            /* close only the window from which the action was launched and then open
             * tabs/windows (depending on parameters->flags) */
            if (!closed_window && (flags & NAUTILUS_WINDOW_OPEN_FLAG_CLOSE_BEHIND) != 0)
            {
                flags &= (~NAUTILUS_WINDOW_OPEN_FLAG_CLOSE_BEHIND);

                /* if NEW_WINDOW is set, we want all files in new windows, not in tabs */
                if ((parameters->flags & NAUTILUS_WINDOW_OPEN_FLAG_NEW_WINDOW) == 0)
                {
                    flags &= (~NAUTILUS_WINDOW_OPEN_FLAG_NEW_WINDOW);
                    flags |= NAUTILUS_WINDOW_OPEN_FLAG_NEW_TAB;
                }

                closed_window = TRUE;
            }
1462 1463 1464
        }
    }

1465
    if (g_queue_is_empty (open_in_app_uris))
1466
    {
1467 1468
        window = NULL;
        if (parameters->slot != NULL)
1469
        {
1470
            window = nautilus_window_slot_get_window (parameters->slot);
1471 1472 1473 1474 1475 1476 1477
        }

        if ((parameters->flags & NAUTILUS_WINDOW_OPEN_FLAG_CLOSE_BEHIND) != 0 &&
            window != NULL)
        {
            nautilus_window_close (window);
        }
1478 1479

        activation_parameters_free (parameters);
1480
    }
1481 1482 1483 1484
    else
    {
        const char *uri;
        ApplicationLaunchParameters *params;
1485

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
        uri = g_queue_peek_head (open_in_app_uris);
        params = application_launch_parameters_new (parameters,
                                                    g_queue_copy (open_in_app_uris));

        nautilus_launch_default_for_uri_async (uri,
                                               parameters->parent_window,
                                               parameters->cancellable,
                                               on_launch_default_for_uri,
                                               params);
    }
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
}

static void
activation_mount_not_mounted_callback (GObject      *source_object,
                                       GAsyncResult *res,
                                       gpointer      user_data)
{
    ActivateParameters *parameters = user_data;
    GError *error;
    NautilusFile *file;
    LaunchLocation *loc;

    file = parameters->not_mounted->data;

    error = NULL;
    if (!g_file_mount_enclosing_volume_finish (G_FILE (source_object), res, &error))
    {
        if (error->domain != G_IO_ERROR ||
            (error->code != G_IO_ERROR_CANCELLED &&
             error->code != G_IO_ERROR_FAILED_HANDLED &&
             error->code != G_IO_ERROR_ALREADY_MOUNTED))
        {
1518 1519 1520 1521
            show_dialog (_("Unable to access location"),
                         error->message,
                         parameters->parent_window,
                         GTK_MESSAGE_ERROR);
1522 1523 1524 1525 1526 1527 1528 1529 1530
        }

        if (error->domain != G_IO_ERROR ||
            error->code != G_IO_ERROR_ALREADY_MOUNTED)
        {
            loc = find_launch_location_for_file (parameters->locations,
                                                 file);
            if (loc)
            {
1531
                parameters->locations = g_list_remove (parameters->locations, loc);
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
                launch_location_free (loc);
            }
        }

        g_error_free (error);
    }

    parameters->not_mounted = g_list_delete_link (parameters->not_mounted,
                                                  parameters->not_mounted);
    nautilus_file_unref (file);

    activation_mount_not_mounted (parameters);
Alexander Larsson's avatar
Alexander Larsson committed
1544 1545 1546 1547 1548
}

static void
activation_mount_not_mounted (ActivateParameters *parameters)
{
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
    NautilusFile *file;
    GFile *location;
    LaunchLocation *loc;
    GMountOperation *mount_op;
    GList *l, *next, *files;

    if (parameters->not_mounted != NULL)
    {
        file = parameters->not_mounted->data;
        mount_op = gtk_mount_operation_new (parameters->parent_window);
        g_mount_operation_set_password_save (mount_op, G_PASSWORD_SAVE_FOR_SESSION);
        g_signal_connect (mount_op, "notify::is-showing",
                          G_CALLBACK (activate_mount_op_active), parameters);
        location = nautilus_file_get_location (file);
        g_file_mount_enclosing_volume (location, 0, mount_op, parameters->cancellable,
                                       activation_mount_not_mounted_callback, parameters);
        g_object_unref (location);
        /* unref mount_op here - g_file_mount_enclosing_volume() does ref for itself */
        g_object_unref (mount_op);
        return;
    }

    parameters->tried_mounting = TRUE;

    if (parameters->locations == NULL)
    {
        activation_parameters_free (parameters);
        return;
    }

    /*  once the mount is finished, refresh all attributes        */
    /*  - fixes new windows not appearing after successful mount  */
    for (l = parameters->locations; l != NULL; l = next)
    {
        loc = l->data;
        next = l->next;
        nautilus_file_invalidate_all_attributes (loc->file);
    }

    files = get_file_list_for_launch_locations (parameters->locations);
    nautilus_file_list_call_when_ready
        (files,
        nautilus_mime_actions_get_required_file_attributes (),
        &parameters->files_handle,
        activate_callback, parameters);
    nautilus_file_list_free (files);
Alexander Larsson's avatar
Alexander Larsson committed
1595 1596 1597 1598
}


static void
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
activate_callback (GList    *files,
                   gpointer  callback_data)
{
    ActivateParameters *parameters = callback_data;
    GList *l, *next;
    NautilusFile *file;
    LaunchLocation *location;

    parameters->files_handle = NULL;

    for (l = parameters->locations; l != NULL; l = next)
    {
        location = l->data;
        file = location->file;
        next = l->next;

        if (file_was_cancelled (file))
        {
            launch_location_free (location);
            parameters->locations = g_list_delete_link (parameters->locations, l);
            continue;
        }

        if (file_was_not_mounted (file))
        {
            if (parameters->tried_mounting)
            {
                launch_location_free (location);
                parameters->locations = g_list_delete_link (parameters->locations, l);
            }
            else
            {
                parameters->not_mounted = g_list_prepend (parameters->not_mounted,
                                                          nautilus_file_ref (file));
            }
            continue;
        }
    }


    if (parameters->not_mounted != NULL)
    {
        activation_mount_not_mounted (parameters);
    }
    else
    {
        activate_files (parameters);
    }
Alexander Larsson's avatar
Alexander Larsson committed
1647 1648 1649
}

static void
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
activate_activation_uris_ready_callback (GList    *files_ignore,
                                         gpointer  callback_data)
{
    ActivateParameters *parameters = callback_data;
    GList *l, *next, *files;
    NautilusFile *file;
    LaunchLocation *location;

    parameters->files_handle = NULL;

    for (l = parameters->locations; l != NULL; l = next)
    {
        location = l->data;
        file = location->file;
        next = l->next;

        if (file_was_cancelled (file))
        {
            launch_location_free (location);
            parameters->locations = g_list_delete_link (parameters->locations, l);
            continue;
        }

        if (nautilus_file_is_broken_symbolic_link (file))
        {
            launch_location_free (location);
            parameters->locations = g_list_delete_link (parameters->locations, l);
            pause_activation_timed_cancel (parameters);
            report_broken_symbolic_link (parameters->parent_window, file);
            unpause_activation_timed_cancel (parameters);
            continue;
        }

        if (nautilus_file_get_file_type (file) == G_FILE_TYPE_MOUNTABLE &&
            !nautilus_file_has_activation_uri (file))
        {
            /* Don't launch these... There is nothing we
             *  can do */
            launch_location_free (location);
            parameters->locations = g_list_delete_link (parameters->locations, l);
            continue;
        }
    }

    if (parameters->locations == NULL)
    {
        activation_parameters_free (parameters);
        return;
    }

    /* Convert the files to the actual activation uri files */
    for (l = parameters->locations; l != NULL; l = l->next)
    {
        char *uri;
        location = l->data;

        /* We want the file for the activation URI since we care
         * about the attributes for that, not for the original file.
         */
        uri = nautilus_file_get_activation_uri (location->file);
        if (uri != NULL)
        {
            launch_location_update_from_uri (location, uri);
        }
        g_free (uri);
    }


    /* get the parameters for the actual files */
    files = get_file_list_for_launch_locations (parameters->locations);
    nautilus_file_list_call_when_ready
        (files,
        nautilus_mime_actions_get_required_file_attributes (),
        &parameters->files_handle,
        activate_callback, parameters);
    nautilus_file_list_free (files);
Alexander Larsson's avatar
Alexander Larsson committed
1726 1727 1728 1729 1730
}

static void
activation_get_activation_uris (ActivateParameters *parameters)
{
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
    GList *l, *files;
    NautilusFile *file;
    LaunchLocation *location;

    /* link target info might be stale, re-read it */
    for (l = parameters->locations; l != NULL; l = l->next)
    {
        location = l->data;
        file = location->file;

        if (file_was_cancelled (file))
        {
            launch_location_free (location);
            parameters->locations = g_list_delete_link (parameters->locations, l);
            continue;
        }
    }
Alexander Larsson's avatar
Alexander Larsson committed
1748

1749 1750 1751 1752 1753
    if (parameters->locations == NULL)
    {
        activation_parameters_free (parameters);
        return;
    }
1754

1755 1756 1757 1758 1759 1760
    files = get_file_list_for_launch_locations (parameters->locations);
    nautilus_file_list_call_when_ready
        (files, nautilus_mime_actions_get_required_file_attributes (),
        &parameters->files_handle,
        activate_activation_uris_ready_callback, parameters);
    nautilus_file_list_free (files);
Alexander Larsson's avatar
Alexander Larsson committed
1761 1762 1763
}

static void
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803