static void
autorun (GMount *mount)
{
    g_autoptr (GFile) root = NULL;
    g_autoptr (GFile) program_to_spawn = NULL;
    g_autoptr (GFile) program_parameter_file = NULL;
    g_autofree char *error_string = NULL;
    g_autofree char *path_to_spawn = NULL;
    g_autofree char *cwd_for_program = NULL;
    g_autofree char *program_parameter = NULL;

    root = g_mount_get_root (mount);

    /* Careful here, according to
     *
     *  http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
     *
     * the ordering does matter.
     */

    if (_check_file (root, ".autorun", TRUE))
    {
        program_to_spawn = g_file_get_child (root, ".autorun");
    }
    else if (_check_file (root, "autorun", TRUE))
    {
        program_to_spawn = g_file_get_child (root, "autorun");
    }
    else if (_check_file (root, "autorun.sh", TRUE))
    {
        program_to_spawn = g_file_new_for_path ("/bin/sh");
        program_parameter_file = g_file_get_child (root, "autorun.sh");
    }

    if (program_to_spawn != NULL)
    {
        path_to_spawn = g_file_get_path (program_to_spawn);
    }
    if (program_parameter_file != NULL)
    {
        program_parameter = g_file_get_path (program_parameter_file);
    }

    cwd_for_program = g_file_get_path (root);

    if (path_to_spawn != NULL && cwd_for_program != NULL)
    {
        if (chdir (cwd_for_program) == 0)
        {
            execl (path_to_spawn, path_to_spawn, program_parameter, NULL);
            error_string = g_strdup_printf (_("Unable to start the program:\n%s"), strerror (errno));
            goto out;
        }
        error_string = g_strdup_printf (_("Unable to start the program:\n%s"), strerror (errno));
        goto out;
    }
    error_string = g_strdup_printf (_("Unable to locate the program"));

out:
    if (error_string != NULL)
    {
        GtkWidget *dialog;
        dialog = gtk_message_dialog_new_with_markup (NULL,         /* TODO: parent window? */
                                                     0,
                                                     GTK_MESSAGE_ERROR,
                                                     GTK_BUTTONS_OK,
                                                     _("Oops! There was a problem running this software."));
        gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", error_string);
        /* This is required because we don't show dialogs in the
         *  window picker and if the window pops under another window
         *  there is no way to get it back. */
        gtk_window_set_keep_above (GTK_WINDOW (dialog), TRUE);

        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);
    }
}
示例#2
0
static void
autorun (GMount *mount)
{
    char *error_string;
    GFile *root;
    GFile *program_to_spawn;
    char *path_to_spawn;
    char *cwd_for_program;

    root = g_mount_get_root (mount);

    /* Careful here, according to
     *
     *  http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
     *
     * the ordering does matter.
     */

    program_to_spawn = NULL;
    path_to_spawn = NULL;

    if (_check_file (root, ".autorun", TRUE))
    {
        program_to_spawn = g_file_get_child (root, ".autorun");
    }
    else if (_check_file (root, "autorun", TRUE))
    {
        program_to_spawn = g_file_get_child (root, "autorun");
    }
    else if (_check_file (root, "autorun.sh", TRUE))
    {
        program_to_spawn = g_file_get_child (root, "autorun.sh");
    }
    else if (_check_file (root, "autorun.exe", TRUE))
    {
        /* TODO */
    }
    else if (_check_file (root, "AUTORUN.EXE", TRUE))
    {
        /* TODO */
    }
    else if (_check_file (root, "autorun.inf", FALSE))
    {
        /* TODO */
    }
    else if (_check_file (root, "AUTORUN.INF", FALSE))
    {
        /* TODO */
    }

    if (program_to_spawn != NULL)
    {
        path_to_spawn = g_file_get_path (program_to_spawn);
    }

    cwd_for_program = g_file_get_path (root);

    error_string = NULL;
    if (path_to_spawn != NULL && cwd_for_program != NULL)
    {
        if (chdir (cwd_for_program) == 0)
        {
            execl (path_to_spawn, path_to_spawn, NULL);
            error_string = g_strdup_printf (_("Error starting autorun program: %s"), strerror (errno));
            goto out;
        }
        error_string = g_strdup_printf (_("Error starting autorun program: %s"), strerror (errno));
        goto out;
    }
    error_string = g_strdup_printf (_("Cannot find the autorun program"));

out:
    if (program_to_spawn != NULL)
    {
        g_object_unref (program_to_spawn);
    }
    if (root != NULL)
    {
        g_object_unref (root);
    }
    g_free (path_to_spawn);
    g_free (cwd_for_program);

    if (error_string != NULL)
    {
        GtkWidget *dialog;
        dialog = gtk_message_dialog_new_with_markup (NULL, /* TODO: parent window? */
                 0,
                 GTK_MESSAGE_ERROR,
                 GTK_BUTTONS_OK,
                 _("<big><b>Error autorunning software</b></big>"));
        gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", error_string);
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);
        g_free (error_string);
    }
}