summaryrefslogtreecommitdiff
path: root/src/nautilus-file-operations-admin.c
blob: 4eff668653b80f7607895ffb39c011252f70e6ed (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <config.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>

#include "nautilus-file-operations-admin.h"
#include "nautilus-file-operations-private.h"
#include "nautilus-file-operations-dbus-data.h"
#include "nautilus-ui-utilities.h"
#include "nautilus-file-utilities.h"
#include "nautilus-file.h"

typedef enum
{
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_INVALID,
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_COPY_TO_DIR,
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_MOVE_TO_DIR,
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_MOVE_FROM_DIR,
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_COPY_FILE,
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_MOVE_FILE,
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_DELETE_FILE,
} NautilusAdminFileOpPermissionDlgType;

typedef enum
{
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_INVALID,
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_YES,
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_SKIP,
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_SKIP_ALL,
    NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_CANCEL,
} NautilusAdminFileOpPermissionDlgResponse;


typedef void (*FileOpAutoAdminMountFinishedCallback) (gboolean success,
                                                      gpointer callback_data,
                                                      GError  *error);

typedef struct
{
    FileOpAutoAdminMountFinishedCallback mount_finished_cb;
    gpointer mount_finished_cb_data;
} AdminMountCbData;

typedef struct
{
    gboolean completed;
    gboolean success;
    GMutex mutex;
    GCond cond;
} FileSubopAutoAdminMountData;

typedef struct
{
    NautilusAdminFileOpPermissionDlgType dlg_type;
    GtkWindow *parent_window;
    GFile *file;
    gboolean completed;
    int response;
    GMutex mutex;
    GCond cond;
} AdminOpPermissionDialogData;

static gboolean admin_vfs_mounted = FALSE;

static GFile *
get_as_admin_file (GFile *file)
{
    g_autofree gchar *uri_path = NULL;
    g_autofree gchar *uri = NULL;
    g_autofree char *admin_uri = NULL;
    gboolean uri_op_success;

    if (file == NULL)
    {
        return NULL;
    }

    uri = g_file_get_uri (file);
    uri_op_success = g_uri_split (uri, G_URI_FLAGS_NONE,
                                  NULL, NULL, NULL, NULL,
                                  &uri_path,
                                  NULL, NULL, NULL);

    g_assert (uri_op_success);

    admin_uri = g_strconcat ("admin://", uri_path, NULL);

    return g_file_new_for_uri (admin_uri);
}

static void
file_op_async_auto_admin_vfs_mount_cb (GObject      *source_object,
                                       GAsyncResult *res,
                                       gpointer      user_data)
{
    /*This is only called in thread-default context of main (aka UI) thread*/

    g_autoptr (GError) error = NULL;
    AdminMountCbData *cb_data = user_data;
    gboolean mount_success;

    mount_success = g_file_mount_enclosing_volume_finish (G_FILE (source_object), res, &error);

    if (mount_success || g_error_matches (error, G_IO_ERROR, G_IO_ERROR_ALREADY_MOUNTED))
    {
        admin_vfs_mounted = TRUE;
        mount_success = TRUE;
    }

    cb_data->mount_finished_cb (mount_success, cb_data->mount_finished_cb_data, error);

    g_slice_free (AdminMountCbData, cb_data);
}

static void
mount_admin_vfs_async (FileOpAutoAdminMountFinishedCallback mount_finished_cb,
                       gpointer                             mount_finished_cb_data)
{
    g_autoptr (GFile) admin_root = NULL;
    AdminMountCbData *cb_data;

    cb_data = g_slice_new0 (AdminMountCbData);
    cb_data->mount_finished_cb = mount_finished_cb;
    cb_data->mount_finished_cb_data = mount_finished_cb_data;

    admin_root = g_file_new_for_uri ("admin:///");

    g_file_mount_enclosing_volume (admin_root,
                                   G_MOUNT_MOUNT_NONE,
                                   NULL,
                                   NULL,
                                   file_op_async_auto_admin_vfs_mount_cb,
                                   cb_data);
}

static void
mount_admin_vfs_sync_finished (gboolean  success,
                               gpointer  callback_data,
                               GError   *error)
{
    FileSubopAutoAdminMountData *data = callback_data;

    g_mutex_lock (&data->mutex);

    data->success = success;
    data->completed = TRUE;

    g_cond_signal (&data->cond);
    g_mutex_unlock (&data->mutex);
}

static gboolean
mount_admin_vfs_sync (void)
{
    FileSubopAutoAdminMountData data = { 0 };

    data.completed = FALSE;
    data.success = FALSE;

    g_mutex_init (&data.mutex);
    g_cond_init (&data.cond);

    g_mutex_lock (&data.mutex);

    mount_admin_vfs_async (mount_admin_vfs_sync_finished,
                           &data);

    while (!data.completed)
    {
        g_cond_wait (&data.cond, &data.mutex);
    }

    g_mutex_unlock (&data.mutex);
    g_mutex_clear (&data.mutex);
    g_cond_clear (&data.cond);

    return data.success;
}

static int
do_admin_file_op_permission_dialog (GtkWindow                            *parent_window,
                                    NautilusAdminFileOpPermissionDlgType  dlg_type,
                                    GFile                                *file)
{
    GtkWidget *dialog;
    int response;
    GtkWidget *button;

    if (dlg_type == NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_COPY_TO_DIR ||
        dlg_type == NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_MOVE_TO_DIR ||
        dlg_type == NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_MOVE_FROM_DIR)
    {
        dialog = gtk_message_dialog_new (parent_window,
                                         0,
                                         GTK_MESSAGE_OTHER,
                                         GTK_BUTTONS_NONE,
                                         NULL);

        g_object_set (dialog,
                      "text", _("Destination directory access denied"),
                      "secondary-text", _("You'll need to provide administrator permissions to paste files into this directory."),
                      NULL);

        gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Cancel"),
                               NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_CANCEL);
        button = gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Continue"),
                                        NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_YES);
        gtk_style_context_add_class (gtk_widget_get_style_context (button),
                                     "suggested-action");

        gtk_dialog_set_default_response (GTK_DIALOG (dialog), NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_CANCEL);
    }
    else
    {
        g_autofree gchar *secondary_text = NULL;
        g_autofree gchar *basename = NULL;

        basename = get_basename (file);

        switch (dlg_type)
        {
            case NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_COPY_FILE:
            {
                secondary_text = g_strdup_printf (_("You'll need to provide administrator permissions to copy“%s”."), basename);
            }
            break;

            case NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_MOVE_FILE:
            {
                secondary_text = g_strdup_printf (_("You'll need to provide administrator permissions to move “%s”."), basename);
            }
            break;

            case NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_DELETE_FILE:
            {
                secondary_text = g_strdup_printf (_("You'll need to provide administrator permissions to permanently delete “%s”."), basename);
            }
            break;

            default:
            {
                g_return_val_if_reached (NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_CANCEL);
            }
        }

        dialog = gtk_message_dialog_new (parent_window,
                                         0,
                                         GTK_MESSAGE_QUESTION,
                                         GTK_BUTTONS_NONE,
                                         NULL);

        g_object_set (dialog,
                      "text", _("Insufficient Access"),
                      "secondary-text", secondary_text,
                      NULL);

        /*TODO: Confirm correct pnemonic keys */

        gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Cancel"), NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_CANCEL);
        gtk_dialog_add_button (GTK_DIALOG (dialog), _("Skip _All"), NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_SKIP_ALL);
        gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Skip"), NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_SKIP);
        button = gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Continue"), NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_YES);
        gtk_style_context_add_class (gtk_widget_get_style_context (button),
                                     "suggested-action");

        gtk_dialog_set_default_response (GTK_DIALOG (dialog), NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_CANCEL);
    }

    response = gtk_dialog_run (GTK_DIALOG (dialog));

    gtk_widget_destroy (dialog);

    return response;
}

static gboolean
do_admin_file_op_permission_dialog2 (gpointer callback_data)
{
    AdminOpPermissionDialogData *data = callback_data;
    int response;

    response = do_admin_file_op_permission_dialog (data->parent_window, data->dlg_type, data->file);

    g_mutex_lock (&data->mutex);

    data->completed = TRUE;
    data->response = response;

    g_cond_signal (&data->cond);
    g_mutex_unlock (&data->mutex);

    return FALSE;
}

static int
ask_permission_for_admin_file_subop_in_main_thread (GtkWindow                            *parent_window,
                                                    NautilusAdminFileOpPermissionDlgType  prompt_type,
                                                    GFile                                *file)
{
    AdminOpPermissionDialogData data = { 0 };

    data.dlg_type = prompt_type;
    data.parent_window = parent_window;
    data.file = file;
    data.completed = FALSE;

    g_mutex_init (&data.mutex);
    g_cond_init (&data.cond);

    g_mutex_lock (&data.mutex);

    g_main_context_invoke (NULL,
                           do_admin_file_op_permission_dialog2,
                           &data);

    while (!data.completed)
    {
        g_cond_wait (&data.cond, &data.mutex);
    }

    g_mutex_unlock (&data.mutex);
    g_mutex_clear (&data.mutex);
    g_cond_clear (&data.cond);

    return data.response;
}

static GFile *
get_admin_file_with_permission (CommonJob *job,
                                GFile     *file)
{
    if (!job->admin_permission_granted && !job->admin_permission_denied)
    {
        int dlg_user_response;
        NautilusAdminFileOpPermissionDlgType prompt_type;

        switch (job->kind)
        {
            case OP_KIND_COPY:
            {
                prompt_type = NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_COPY_FILE;
            }
            break;

            case OP_KIND_MOVE:
            {
                prompt_type = NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_MOVE_FILE;
            }
            break;

            case OP_KIND_DELETE:
            {
                prompt_type = NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_DELETE_FILE;
            }
            break;

            default:
            {
                g_return_val_if_reached (NULL);
            }
        }

        dlg_user_response = ask_permission_for_admin_file_subop_in_main_thread (
            job->parent_window,
            prompt_type,
            file);

        switch (dlg_user_response)
        {
            case NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_YES:
            {
                job->admin_permission_granted = TRUE;
            }
            break;

            case NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_SKIP:
            {
            }
            break;

            case NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_SKIP_ALL:
            {
                job->admin_permission_denied = TRUE;
            }
            break;

            case NAUTILUS_ADMIN_FILE_OP_PERMISSION_DLG_RESPONSE_CANCEL:
            {
                g_cancellable_cancel (job->cancellable);
            }
            break;

            default:
            {
                g_assert_not_reached ();
            }
        }
    }

    if (!job->admin_permission_granted)
    {
        return NULL;
    }

    if (!admin_vfs_mounted)
    {
        if (!mount_admin_vfs_sync ())
        {
            return NULL;
        }
    }

    return get_as_admin_file (file);
}

/* Tailored for use in do-while blocks wrapping a GIO call. */
gboolean
retry_with_admin_uri (GFile     **try_file,
                      CommonJob  *job,
                      GError    **error)
{
    g_autoptr (GFile) admin_file = NULL;

    g_return_val_if_fail (try_file != NULL && G_IS_FILE (*try_file), FALSE);

    /* If an admin:// uri has already been tried, there is no need to try again.
     * If it's not a permission error, or the file is not native, the admin
     * backend is not going to help us. */
    if (g_file_has_uri_scheme (*try_file, "admin") ||
        !g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED) ||
        !g_file_is_native (*try_file) ||
        g_cancellable_is_cancelled (job->cancellable))
    {
        return FALSE;
    }

    admin_file = get_admin_file_with_permission (job, *try_file);
    if (admin_file == NULL)
    {
        return FALSE;
    }

    /* Try again once with admin backend. */
    g_set_object (try_file, admin_file);
    g_clear_error (error);
    return TRUE;
}