1 /* gbp-git-dependency-updater.c
2  *
3  * Copyright 2018-2019 Christian Hergert <chergert@redhat.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "gbp-git-dependency-updater"
22 
23 #include "config.h"
24 
25 #include <glib/gi18n.h>
26 
27 #include "daemon/ipc-git-repository.h"
28 
29 #include "gbp-git-dependency-updater.h"
30 #include "gbp-git-progress.h"
31 #include "gbp-git-vcs.h"
32 
33 struct _GbpGitDependencyUpdater
34 {
35   IdeObject parent_instance;
36 };
37 
38 static void
gbp_git_dependency_updater_update_cb(GObject * object,GAsyncResult * result,gpointer user_data)39 gbp_git_dependency_updater_update_cb (GObject      *object,
40                                       GAsyncResult *result,
41                                       gpointer      user_data)
42 {
43   IpcGitRepository *repository = (IpcGitRepository *)object;
44   g_autoptr(IdeTask) task = user_data;
45   g_autoptr(GError) error = NULL;
46 
47   IDE_ENTRY;
48 
49   g_assert (IPC_IS_GIT_REPOSITORY (repository));
50   g_assert (G_IS_ASYNC_RESULT (result));
51   g_assert (IDE_IS_TASK (task));
52 
53   if (!ipc_git_repository_call_update_submodules_finish (repository, result, &error))
54     ide_task_return_error (task, g_steal_pointer (&error));
55   else
56     ide_task_return_boolean (task, TRUE);
57 
58   IDE_EXIT;
59 }
60 
61 static void
gbp_git_dependency_updater_update_async(IdeDependencyUpdater * self,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)62 gbp_git_dependency_updater_update_async (IdeDependencyUpdater *self,
63                                          GCancellable         *cancellable,
64                                          GAsyncReadyCallback   callback,
65                                          gpointer              user_data)
66 {
67   g_autoptr(IpcGitProgress) progress = NULL;
68   g_autoptr(IdeTask) task = NULL;
69   g_autoptr(IdeVcs) vcs = NULL;
70   g_autoptr(IdeNotification) notif = NULL;
71   g_autoptr(GError) error = NULL;
72   IpcGitRepository *repository;
73   GDBusConnection *connection;
74   IdeContext *context;
75 
76   IDE_ENTRY;
77 
78   g_assert (GBP_IS_GIT_DEPENDENCY_UPDATER (self));
79   g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
80 
81   task = ide_task_new (self, cancellable, callback, user_data);
82   ide_task_set_source_tag (task, gbp_git_dependency_updater_update_async);
83 
84   context = ide_object_get_context (IDE_OBJECT (self));
85   vcs = ide_object_get_child_typed (IDE_OBJECT (context), IDE_TYPE_VCS);
86 
87   if (!GBP_IS_GIT_VCS (vcs))
88     {
89       ide_task_return_new_error (task,
90                                  G_IO_ERROR,
91                                  G_IO_ERROR_FAILED,
92                                  _("Git version control is not in use"));
93       IDE_EXIT;
94     }
95 
96   repository = gbp_git_vcs_get_repository (GBP_GIT_VCS (vcs));
97   connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (repository));
98 
99   notif = g_object_new (IDE_TYPE_NOTIFICATION,
100                         "title", _("Updating Git Submodules"),
101                         NULL);
102 
103   if (!(progress = gbp_git_progress_new (connection, notif, cancellable, &error)))
104     {
105       ide_task_return_error (task, g_steal_pointer (&error));
106       IDE_EXIT;
107     }
108 
109   ide_task_set_task_data (task, g_object_ref (progress), g_object_unref);
110   gbp_git_progress_set_withdraw (GBP_GIT_PROGRESS (progress), TRUE);
111   ide_notification_attach (notif, IDE_OBJECT (context));
112 
113   ipc_git_repository_call_update_submodules (repository,
114                                              TRUE,
115                                              g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (progress)),
116                                              cancellable,
117                                              gbp_git_dependency_updater_update_cb,
118                                              g_steal_pointer (&task));
119 
120   IDE_EXIT;
121 }
122 
123 static gboolean
gbp_git_dependency_updater_update_finish(IdeDependencyUpdater * self,GAsyncResult * result,GError ** error)124 gbp_git_dependency_updater_update_finish (IdeDependencyUpdater  *self,
125                                           GAsyncResult          *result,
126                                           GError               **error)
127 {
128   g_assert (GBP_IS_GIT_DEPENDENCY_UPDATER (self));
129   g_assert (IDE_IS_TASK (result));
130 
131   return ide_task_propagate_boolean (IDE_TASK (result), error);
132 }
133 
134 static void
dependency_updater_iface_init(IdeDependencyUpdaterInterface * iface)135 dependency_updater_iface_init (IdeDependencyUpdaterInterface *iface)
136 {
137   iface->update_async = gbp_git_dependency_updater_update_async;
138   iface->update_finish = gbp_git_dependency_updater_update_finish;
139 }
140 
G_DEFINE_FINAL_TYPE_WITH_CODE(GbpGitDependencyUpdater,gbp_git_dependency_updater,IDE_TYPE_OBJECT,G_IMPLEMENT_INTERFACE (IDE_TYPE_DEPENDENCY_UPDATER,dependency_updater_iface_init))141 G_DEFINE_FINAL_TYPE_WITH_CODE (GbpGitDependencyUpdater, gbp_git_dependency_updater, IDE_TYPE_OBJECT,
142                          G_IMPLEMENT_INTERFACE (IDE_TYPE_DEPENDENCY_UPDATER,
143                                                 dependency_updater_iface_init))
144 
145 static void
146 gbp_git_dependency_updater_class_init (GbpGitDependencyUpdaterClass *klass)
147 {
148 }
149 
150 static void
gbp_git_dependency_updater_init(GbpGitDependencyUpdater * self)151 gbp_git_dependency_updater_init (GbpGitDependencyUpdater *self)
152 {
153 }
154