gbp-flatpak-transfer.c 23.5 KB
Newer Older
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
/* gbp-flatpak-transfer.c
 *
 * Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define G_LOG_DOMAIN "gbp-flatpak-transfer"

#include <flatpak.h>
#include <glib/gi18n.h>

#include "gbp-flatpak-transfer.h"

struct _GbpFlatpakTransfer
{
  IdeObject parent_instance;

  gchar   *id;
  gchar   *arch;
  gchar   *branch;

  guint    force_update : 1;

  GMutex   mutex;
  gchar   *status;
  gdouble  progress;
};

enum {
  PROP_0,
  PROP_ID,
  PROP_ARCH,
  PROP_BRANCH,
  PROP_FORCE_UPDATE,
  PROP_TITLE,
  PROP_ICON_NAME,
  PROP_PROGRESS,
  PROP_STATUS,
  N_PROPS
};

54
55
56
57
58
59
60
61
typedef struct
{
  const gchar *name;
  const gchar *url;
} BuiltinFlatpakRepo;

static void transfer_iface_init                     (IdeTransferInterface *iface);
static void gbp_flatpak_transfer_install_repos_tick (GTask                *task);
62
63
64
65
66

G_DEFINE_TYPE_WITH_CODE (GbpFlatpakTransfer, gbp_flatpak_transfer, IDE_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (IDE_TYPE_TRANSFER, transfer_iface_init))

static GParamSpec *properties [N_PROPS];
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
static BuiltinFlatpakRepo builtin_flatpak_repos[] = {
  { "gnome",         "https://sdk.gnome.org/gnome.flatpakrepo" },
  { "gnome-nightly", "https://sdk.gnome.org/gnome-nightly.flatpakrepo" },
};

static void
take_status (GbpFlatpakTransfer *self,
             gchar              *status)
{
  g_mutex_lock (&self->mutex);
  g_free (self->status);
  self->status = status;
  g_mutex_unlock (&self->mutex);

  ide_object_notify_in_main (self, properties[PROP_STATUS]);
}

static void
gbp_flatpak_transfer_wait_check_cb (GObject      *object,
                                    GAsyncResult *result,
                                    gpointer      user_data)
{
  IdeSubprocess *subprocess = (IdeSubprocess *)object;
  g_autoptr(GTask) task = user_data;
  g_autoptr(GError) error = NULL;

  IDE_ENTRY;

  g_assert (IDE_IS_SUBPROCESS (subprocess));
  g_assert (G_IS_ASYNC_RESULT (result));

  if (!ide_subprocess_wait_check_finish (subprocess, result, &error))
    g_task_return_error (task, g_steal_pointer (&error));
  else
    gbp_flatpak_transfer_install_repos_tick (task);

  IDE_EXIT;
}

static void
gbp_flatpak_transfer_install_repos_tick (GTask *task)
{
  GbpFlatpakTransfer *self;
  gint *pos;

  IDE_ENTRY;

  g_assert (G_IS_TASK (task));

  self = g_task_get_source_object (task);
  pos = g_task_get_task_data (task);

  if (*pos < G_N_ELEMENTS (builtin_flatpak_repos))
    {
      g_autoptr(IdeSubprocessLauncher) launcher = NULL;
      g_autoptr(IdeSubprocess) subprocess = NULL;
      g_autoptr(GError) error = NULL;
      const gchar *name = builtin_flatpak_repos[*pos].name;
      const gchar *url = builtin_flatpak_repos[*pos].url;
      GCancellable *cancellable = g_task_get_cancellable (task);

      g_mutex_lock (&self->mutex);
      g_free (self->status);
      /* translators: message is used to notify to the user we are checking for a flatpak repository */
      self->status = g_strdup_printf (_("Checking for %s repository"), name);
      g_mutex_unlock (&self->mutex);

      launcher = ide_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE |
                                              G_SUBPROCESS_FLAGS_STDERR_PIPE);
      ide_subprocess_launcher_set_run_on_host (launcher, TRUE);
      ide_subprocess_launcher_set_clear_env (launcher, FALSE);
      ide_subprocess_launcher_push_argv (launcher, "flatpak");
      ide_subprocess_launcher_push_argv (launcher, "remote-add");
      ide_subprocess_launcher_push_argv (launcher, "--user");
      ide_subprocess_launcher_push_argv (launcher, "--if-not-exists");
      ide_subprocess_launcher_push_argv (launcher, "--from");
      ide_subprocess_launcher_push_argv (launcher, name);
      ide_subprocess_launcher_push_argv (launcher, url);

      subprocess = ide_subprocess_launcher_spawn (launcher, cancellable, &error);

      if (subprocess == NULL)
        {
          g_task_return_error (task, g_steal_pointer (&error));
          IDE_EXIT;
        }

      ide_subprocess_wait_check_async (subprocess,
                                       cancellable,
                                       gbp_flatpak_transfer_wait_check_cb,
                                       g_object_ref (task));

      (*pos)++;

      IDE_EXIT;
    }

  g_task_return_boolean (task, TRUE);

  IDE_EXIT;
}

static void
gbp_flatpak_transfer_install_repos_async (GbpFlatpakTransfer  *self,
                                          GCancellable        *cancellable,
                                          GAsyncReadyCallback  callback,
                                          gpointer             user_data)
{
  g_autoptr(GTask) task = NULL;

  IDE_ENTRY;

  g_assert (GBP_IS_FLATPAK_TRANSFER (self));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  task = g_task_new (self, cancellable, callback, user_data);
  g_task_set_source_tag (task, gbp_flatpak_transfer_install_repos_async);
  g_task_set_task_data (task, g_new0 (guint, 1), g_free);

  gbp_flatpak_transfer_install_repos_tick (task);

  IDE_EXIT;
}

static gboolean
gbp_flatpak_transfer_install_repos_finish (GbpFlatpakTransfer  *self,
                                           GAsyncResult        *result,
                                           GError             **error)
{
  gboolean ret;

  IDE_ENTRY;

  g_assert (GBP_IS_FLATPAK_TRANSFER (self));
  g_assert (G_IS_TASK (result));

  ret = g_task_propagate_boolean (G_TASK (result), error);

  IDE_RETURN (ret);
}
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

static void
progress_callback (const gchar *status,
                   guint        progress,
                   gboolean     estimating,
                   gpointer     user_data)
{
  GbpFlatpakTransfer *self = user_data;

  g_assert (GBP_IS_FLATPAK_TRANSFER (self));

  g_mutex_lock (&self->mutex);
  g_free (self->status);
  self->status = g_strdup (status);
  self->progress = progress / 100.0;
  g_mutex_unlock (&self->mutex);

  ide_object_notify_in_main (self, properties[PROP_PROGRESS]);
  ide_object_notify_in_main (self, properties[PROP_STATUS]);
}

static gboolean
update_installation (GbpFlatpakTransfer   *self,
                     FlatpakInstallation  *installation,
                     GCancellable         *cancellable,
                     GError              **error)
{
  g_autoptr(FlatpakInstalledRef) ref = NULL;

  g_assert (GBP_IS_FLATPAK_TRANSFER (self));
  g_assert (FLATPAK_IS_INSTALLATION (installation));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

240
241
  take_status (self, g_strdup_printf (_("Updating “%s”"), self->id));

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
  ref = flatpak_installation_update (installation,
                                     FLATPAK_UPDATE_FLAGS_NONE,
                                     FLATPAK_REF_KIND_RUNTIME,
                                     self->id,
                                     self->arch,
                                     self->branch,
                                     progress_callback,
                                     self,
                                     cancellable,
                                     error);

  return ref != NULL;
}

static gboolean
install_from_remote (GbpFlatpakTransfer   *self,
                     FlatpakInstallation  *installation,
                     FlatpakRemote        *remote,
                     GCancellable         *cancellable,
                     GError              **error)
{
  g_autoptr(FlatpakInstalledRef) ref = NULL;

  IDE_ENTRY;

  g_assert (GBP_IS_FLATPAK_TRANSFER (self));
  g_assert (FLATPAK_IS_INSTALLATION (installation));
  g_assert (FLATPAK_IS_REMOTE (remote));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  g_debug ("Installing %s/%s/%s from remote %s",
           self->id, self->arch, self->branch,
           flatpak_remote_get_name (remote));

276
277
278
279
  take_status (self, g_strdup_printf (_("Installing “%s” from “%s”"),
                                      self->id,
                                      flatpak_remote_get_name (remote)));

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
  ref = flatpak_installation_install (installation,
                                      flatpak_remote_get_name (remote),
                                      FLATPAK_REF_KIND_RUNTIME,
                                      self->id,
                                      self->arch,
                                      self->branch,
                                      progress_callback,
                                      self,
                                      cancellable,
                                      error);

  IDE_TRACE_MSG ("ref = %p", ref);

  if (ref != NULL)
    g_debug ("%s/%s/%s was installed from remote %s",
             self->id, self->arch, self->branch,
             flatpak_remote_get_name (remote));

  IDE_RETURN (ref != NULL);
}

static void
gbp_flatpak_transfer_execute_worker (GTask        *task,
                                     gpointer      source_object,
                                     gpointer      task_data,
                                     GCancellable *cancellable)
{
  GbpFlatpakTransfer *self = source_object;
  FlatpakInstallation *installations[2] = { NULL };
  g_autoptr(FlatpakInstallation) user = NULL;
  g_autoptr(FlatpakInstallation) system = NULL;

  IDE_ENTRY;

  g_assert (GBP_IS_FLATPAK_TRANSFER (self));
  g_assert (G_IS_TASK (task));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  /*
   * Load the installations.
   */

  installations[0] = user = flatpak_installation_new_user (cancellable, NULL);
  installations[1] = system = flatpak_installation_new_system (cancellable, NULL);

325
326
327
  IDE_TRACE_MSG (" installations[0] = %s", G_OBJECT_TYPE_NAME (installations[0]));
  IDE_TRACE_MSG (" installations[1] = %s", G_OBJECT_TYPE_NAME (installations[1]));

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  /*
   * Locate the id within a previous installation;
   */

  for (guint i = 0; i < G_N_ELEMENTS (installations); i++)
    {
      FlatpakInstallation *installation = installations[i];
      g_autoptr(GError) error = NULL;
      g_autoptr(GPtrArray) refs = NULL;

      if (installation == NULL)
        continue;

      refs = flatpak_installation_list_installed_refs (installation, cancellable, &error);

      if (error != NULL)
        {
          g_task_return_error (task, g_steal_pointer (&error));
          IDE_EXIT;
        }

349
350
351
      IDE_TRACE_MSG ("Looking for %s/%s/%s in installation[%d]",
                     self->id, self->arch, self->branch, i);

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
      for (guint j = 0; j < refs->len; j++)
        {
          FlatpakInstalledRef *ref = g_ptr_array_index (refs, j);
          const gchar *id;
          const gchar *arch;
          const gchar *branch;

          g_assert (FLATPAK_IS_INSTALLED_REF (ref));

          id = flatpak_ref_get_name (FLATPAK_REF (ref));
          arch = flatpak_ref_get_arch (FLATPAK_REF (ref));
          branch = flatpak_ref_get_branch (FLATPAK_REF (ref));

          IDE_TRACE_MSG ("Found %s/%s/%s installed in installation[%u]",
                         id, arch, branch, i);

          if (g_strcmp0 (self->id, id) == 0 &&
              g_strcmp0 (self->branch, branch) == 0 &&
              g_strcmp0 (self->arch, arch) == 0)
            {
              if (!self->force_update)
                {
                  IDE_TRACE_MSG ("Force update unset, considering transfer complete");
                  g_task_return_boolean (task, TRUE);
                  IDE_EXIT;
                }

              if (!update_installation (self, installation, cancellable, &error))
                g_task_return_error (task, g_steal_pointer (&error));
              else
                g_task_return_boolean (task, TRUE);

              IDE_EXIT;
            }
        }
    }

  /*
   * We didn't locate the id under a previous installation, so we need to
   * locate a remote that has the matching ref and install it from that.
   */
  g_debug ("%s was not found, locating within remote", self->id);

  for (guint i = 0; i < G_N_ELEMENTS (installations); i++)
    {
      FlatpakInstallation *installation = installations[i];
      g_autoptr(GPtrArray) remotes = NULL;
      g_autoptr(GError) error = NULL;

      if (installation == NULL)
        continue;

      remotes = flatpak_installation_list_remotes (installation, cancellable, &error);

      if (error != NULL)
        {
          g_task_return_error (task, g_steal_pointer (&error));
          IDE_EXIT;
        }

      for (guint j = 0; j < remotes->len; j++)
        {
          FlatpakRemote *remote = g_ptr_array_index (remotes, j);
          g_autoptr(GPtrArray) refs = NULL;

          g_assert (FLATPAK_IS_REMOTE (remote));

          refs = flatpak_installation_list_remote_refs_sync (installation,
                                                             flatpak_remote_get_name (remote),
                                                             cancellable,
                                                             &error);

          if (error != NULL)
            {
              g_task_return_error (task, g_steal_pointer (&error));
              IDE_EXIT;
            }

          for (guint k = 0; k < refs->len; k++)
            {
              FlatpakRemoteRef *ref = g_ptr_array_index (refs, k);
              const gchar *id;
              const gchar *arch;
              const gchar *branch;

              g_assert (FLATPAK_IS_REMOTE_REF (ref));

              id = flatpak_ref_get_name (FLATPAK_REF (ref));
              arch = flatpak_ref_get_arch (FLATPAK_REF (ref));
              branch = flatpak_ref_get_branch (FLATPAK_REF (ref));

443
444
445
446
              IDE_TRACE_MSG ("Found %s/%s/%s within remote of installation[%d][%s]",
                             id, arch, branch, i,
                             flatpak_remote_get_name (remote));

447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
              if (g_strcmp0 (self->id, id) == 0 &&
                  g_strcmp0 (self->branch, branch) == 0 &&
                  g_strcmp0 (self->arch, arch) == 0)
                {
                  if (install_from_remote (self, installation, remote, cancellable, &error))
                    g_task_return_boolean (task, TRUE);
                  else
                    g_task_return_error (task, g_steal_pointer (&error));

                  IDE_EXIT;
                }
            }
        }
    }

  g_task_return_new_error (task,
                           G_IO_ERROR,
                           G_IO_ERROR_NOT_FOUND,
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
                           /* Translators: %s is the id of the runtime such as org.gnome.Sdk/x86_64/master */
                           _("Failed to locate %s/%s/%s"),
                           self->id, self->arch, self->branch);

  IDE_EXIT;
}

static void
gbp_flatpak_transfer_install_repos_cb (GObject      *object,
                                       GAsyncResult *result,
                                       gpointer      user_data)
{
  GbpFlatpakTransfer *self = (GbpFlatpakTransfer *)object;
  g_autoptr(GTask) task = user_data;
  g_autoptr(GError) error = NULL;

  IDE_ENTRY;

  g_assert (GBP_IS_FLATPAK_TRANSFER (self));
  g_assert (G_IS_ASYNC_RESULT (result));
  g_assert (G_IS_TASK (task));

  if (!gbp_flatpak_transfer_install_repos_finish (self, result, &error))
    g_task_return_error (task, g_steal_pointer (&error));
  else
    g_task_run_in_thread (task, gbp_flatpak_transfer_execute_worker);
491
492
493
494
495
496
497
498
499
500
501
502
503

  IDE_EXIT;
}

static void
gbp_flatpak_transfer_execute_async (IdeTransfer         *transfer,
                                    GCancellable        *cancellable,
                                    GAsyncReadyCallback  callback,
                                    gpointer             user_data)
{
  GbpFlatpakTransfer *self = (GbpFlatpakTransfer *)transfer;
  g_autoptr(GTask) task = NULL;

504
505
  IDE_ENTRY;

506
507
508
509
510
  g_assert (GBP_IS_FLATPAK_TRANSFER (self));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  task = g_task_new (self, cancellable, callback, user_data);
  g_task_set_source_tag (task, gbp_flatpak_transfer_execute_async);
511
512
513
514
515
516
517

  gbp_flatpak_transfer_install_repos_async (self,
                                            cancellable,
                                            gbp_flatpak_transfer_install_repos_cb,
                                            g_steal_pointer (&task));

  IDE_EXIT;
518
519
520
521
522
523
524
}

static gboolean
gbp_flatpak_transfer_execute_finish (IdeTransfer   *transfer,
                                     GAsyncResult  *result,
                                     GError       **error)
{
525
526
527
528
  gboolean ret;

  IDE_ENTRY;

529
530
531
  g_assert (GBP_IS_FLATPAK_TRANSFER (transfer));
  g_assert (G_IS_TASK (result));

532
533
534
  ret = g_task_propagate_boolean (G_TASK (result), error);

  IDE_RETURN (ret);
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
}

static void
transfer_iface_init (IdeTransferInterface *iface)
{
  iface->execute_async = gbp_flatpak_transfer_execute_async;
  iface->execute_finish = gbp_flatpak_transfer_execute_finish;
}

static void
gbp_flatpak_transfer_finalize (GObject *object)
{
  GbpFlatpakTransfer *self = (GbpFlatpakTransfer *)object;

  g_clear_pointer (&self->id, g_free);
  g_clear_pointer (&self->arch, g_free);
  g_clear_pointer (&self->branch, g_free);
  g_mutex_clear (&self->mutex);

  G_OBJECT_CLASS (gbp_flatpak_transfer_parent_class)->finalize (object);
}

static void
gbp_flatpak_transfer_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  GbpFlatpakTransfer *self = GBP_FLATPAK_TRANSFER (object);

  switch (prop_id)
    {
    case PROP_STATUS:
      g_mutex_lock (&self->mutex);
      g_value_set_string (value, self->status);
      g_mutex_unlock (&self->mutex);
      break;

    case PROP_TITLE:
574
575
      g_value_take_string (value, g_strdup_printf (_("Installing %s %s for %s"),
                                                   self->id, self->branch, self->arch));
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
      break;

    case PROP_ICON_NAME:
      g_value_set_string (value, "folder-download-symbolic");
      break;

    case PROP_PROGRESS:
      g_mutex_lock (&self->mutex);
      g_value_set_double (value, self->progress);
      g_mutex_unlock (&self->mutex);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gbp_flatpak_transfer_set_property (GObject      *object,
                                   guint         prop_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  GbpFlatpakTransfer *self = GBP_FLATPAK_TRANSFER (object);

  switch (prop_id)
    {
    case PROP_ID:
Matthew Leeds's avatar
Matthew Leeds committed
604
      g_free (self->id);
605
606
607
608
      self->id = g_value_dup_string (value);
      break;

    case PROP_ARCH:
Matthew Leeds's avatar
Matthew Leeds committed
609
      g_free (self->arch);
610
611
612
613
614
615
      self->arch = g_value_dup_string (value);
      if (self->arch == NULL)
        self->arch = g_strdup (flatpak_get_default_arch ());
      break;

    case PROP_BRANCH:
Matthew Leeds's avatar
Matthew Leeds committed
616
      g_free (self->branch);
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
      self->branch = g_value_dup_string (value);
      if (self->branch == NULL)
        self->branch = g_strdup ("stable");
      break;

    case PROP_FORCE_UPDATE:
      self->force_update = g_value_get_boolean (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gbp_flatpak_transfer_class_init (GbpFlatpakTransferClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gbp_flatpak_transfer_finalize;
  object_class->get_property = gbp_flatpak_transfer_get_property;
  object_class->set_property = gbp_flatpak_transfer_set_property;

  properties [PROP_ID] =
    g_param_spec_string ("id", NULL, NULL, NULL,
                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  properties [PROP_ARCH] =
    g_param_spec_string ("arch", NULL, NULL, NULL,
                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  properties [PROP_BRANCH] =
    g_param_spec_string ("branch", NULL, NULL, NULL,
                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  
  properties [PROP_FORCE_UPDATE] =
    g_param_spec_boolean ("force-update", NULL, NULL, FALSE,
                          G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  properties [PROP_STATUS] =
    g_param_spec_string ("status", NULL, NULL, NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  properties [PROP_TITLE] =
    g_param_spec_string ("title", NULL, NULL, NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  properties [PROP_ICON_NAME] =
    g_param_spec_string ("icon-name", NULL, NULL, NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  properties [PROP_PROGRESS] =
    g_param_spec_double ("progress", NULL, NULL, 0.0, 100.0, 0.0,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, N_PROPS, properties);
}

static void
gbp_flatpak_transfer_init (GbpFlatpakTransfer *self)
{
  g_mutex_init (&self->mutex);

  self->arch = g_strdup (flatpak_get_default_arch ());
  self->branch = g_strdup ("master");
}

GbpFlatpakTransfer *
gbp_flatpak_transfer_new (IdeContext  *context,
                          const gchar *id,
                          const gchar *arch,
                          const gchar *branch,
                          gboolean     force_update)
{
  g_return_val_if_fail (IDE_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (id != NULL, NULL);

  if (arch == NULL)
    arch = flatpak_get_default_arch ();

  if (branch == NULL)
    branch = "stable";

  return g_object_new (GBP_TYPE_FLATPAK_TRANSFER,
                       "context", context,
                       "id", id,
                       "arch", arch,
                       "branch", branch,
                       "force-update", force_update,
                       NULL);
}

gboolean
gbp_flatpak_transfer_is_installed (GbpFlatpakTransfer *self,
                                   GCancellable       *cancellable)
{
  FlatpakInstallation *installations[2] = { NULL };
  g_autoptr(FlatpakInstallation) user = NULL;
  g_autoptr(FlatpakInstallation) system = NULL;

  IDE_ENTRY;

  g_return_val_if_fail (GBP_IS_FLATPAK_TRANSFER (self), FALSE);

  installations[0] = user = flatpak_installation_new_user (cancellable, NULL);
  installations[1] = system = flatpak_installation_new_system (cancellable, NULL);

  for (guint i = 0; i < G_N_ELEMENTS (installations); i++)
    {
      FlatpakInstallation *installation = installations[i];
      g_autoptr(GError) error = NULL;
      g_autoptr(GPtrArray) refs = NULL;

      if (installation == NULL)
        continue;

      refs = flatpak_installation_list_installed_refs (installation, cancellable, &error);

      if (refs == NULL)
        continue;

      for (guint j = 0; j < refs->len; j++)
        {
          FlatpakInstalledRef *ref = g_ptr_array_index (refs, j);
          const gchar *id;
          const gchar *arch;
          const gchar *branch;

          g_assert (FLATPAK_IS_INSTALLED_REF (ref));

          id = flatpak_ref_get_name (FLATPAK_REF (ref));
          arch = flatpak_ref_get_arch (FLATPAK_REF (ref));
          branch = flatpak_ref_get_branch (FLATPAK_REF (ref));

          if (g_strcmp0 (self->id, id) == 0 &&
              g_strcmp0 (self->branch, branch) == 0 &&
              g_strcmp0 (self->arch, arch) == 0)
            return TRUE;
        }
    }

  return FALSE;
}