#include <gtk/gtk.h>

typedef struct
{
    GMainLoop *loop;
    gboolean res;
} MountData;

static void mount_cb(GObject *src, GAsyncResult *res, gpointer user_data)
{
    MountData *data = user_data;

    data->res = g_volume_mount_finish(G_VOLUME(src), res, NULL);
    g_main_loop_quit(data->loop);
}

static void on_click(GtkButton *btn, gpointer _unused)
{
    GVolumeMonitor *mon = g_volume_monitor_get();
    GList *vols = g_volume_monitor_get_volumes(mon), *l;
    char *name;
    GMountOperation *op;
    MountData data;

    for (l = vols; l; l = l->next)
    {
        name = g_volume_get_name(l->data);
        op = gtk_mount_operation_new(NULL);
        data.loop = g_main_loop_new(NULL, TRUE);
        g_volume_mount(l->data, 0, op, NULL, mount_cb, &data);
        g_main_loop_run(data.loop);
        g_main_loop_unref(data.loop);
        g_warning("found volume: %s, mount res: %s", name, data.res ? "true":"false");
        g_free(name);
        g_object_unref(op);
        g_object_unref(l->data);
    }
    g_list_free(vols);
    g_object_unref(mon);
}

int main(int argc, char *argv[])
{
    GtkWidget *window, *button;

    gtk_init(&argc, &argv);

    window = gtk_dialog_new_with_buttons("Test", NULL, 0, "OK", GTK_RESPONSE_ACCEPT, NULL);
    button = gtk_button_new_with_label("test");
    g_signal_connect(button, "clicked", G_CALLBACK(on_click), NULL);
    gtk_container_add(GTK_CONTAINER (GTK_DIALOG(window)->vbox), button);
    gtk_widget_show_all(window);

    gtk_dialog_run(GTK_DIALOG(window));

    return 0;
}