1 /* gbp-git-vcs-initializer.c
2  *
3  * Copyright 2016-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-vcs-initializer"
22 
23 #include "gbp-git-client.h"
24 #include "gbp-git-vcs-initializer.h"
25 
26 struct _GbpGitVcsInitializer
27 {
28   IdeObject parent_instance;
29 };
30 
31 static gchar *
32 gbp_git_vcs_initializer_get_title (IdeVcsInitializer *self)
33 {
34   return g_strdup ("Git");
35 }
36 
37 static void
38 create_cb (GObject      *object,
39            GAsyncResult *result,
40            gpointer      user_data)
41 {
42   IpcGitService *service = (IpcGitService *)object;
43   g_autoptr(IdeTask) task = user_data;
44   g_autoptr(GError) error = NULL;
45   g_autofree gchar *git_location = NULL;
46 
47   g_assert (IDE_IS_MAIN_THREAD ());
48   g_assert (IPC_IS_GIT_SERVICE (service));
49   g_assert (G_IS_ASYNC_RESULT (result));
50   g_assert (IDE_IS_TASK (task));
51 
52   if (!ipc_git_service_call_create_finish (service, &git_location, result, &error))
53     ide_task_return_error (task, g_steal_pointer (&error));
54   else
55     ide_task_return_boolean (task, TRUE);
56 }
57 
58 static void
59 get_service_cb (GObject      *object,
60                 GAsyncResult *result,
61                 gpointer      user_data)
62 {
63   GbpGitClient *client = (GbpGitClient *)object;
64   g_autoptr(IpcGitService) service = NULL;
65   g_autoptr(IdeTask) task = user_data;
66   g_autoptr(GError) error = NULL;
67   GCancellable *cancellable;
68   GFile *file;
69 
70   g_assert (IDE_IS_MAIN_THREAD ());
71   g_assert (GBP_IS_GIT_CLIENT (client));
72   g_assert (G_IS_ASYNC_RESULT (result));
73   g_assert (IDE_IS_TASK (task));
74 
75   cancellable = ide_task_get_cancellable (task);
76   file = ide_task_get_task_data (task);
77 
78   if (!(service = gbp_git_client_get_service_finish (client, result, &error)))
79     ide_task_return_error (task, g_steal_pointer (&error));
80   else
81     ipc_git_service_call_create (service,
82                                  g_file_peek_path (file),
83                                  FALSE,
84                                  cancellable,
85                                  create_cb,
86                                  g_steal_pointer (&task));
87 }
88 
89 static void
90 gbp_git_vcs_initializer_initialize_async (IdeVcsInitializer   *initializer,
91                                           GFile               *file,
92                                           GCancellable        *cancellable,
93                                           GAsyncReadyCallback  callback,
94                                           gpointer             user_data)
95 {
96   g_autoptr(IdeTask) task = NULL;
97   GbpGitClient *client;
98   IdeContext *context;
99 
100   g_assert (IDE_IS_MAIN_THREAD ());
101   g_assert (GBP_IS_GIT_VCS_INITIALIZER (initializer));
102   g_assert (G_IS_FILE (file));
103   g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
104 
105   task = ide_task_new (initializer, cancellable, callback, user_data);
106   ide_task_set_source_tag (task, gbp_git_vcs_initializer_initialize_async);
107   ide_task_set_task_data (task, g_file_dup (file), g_object_unref);
108 
109   context = ide_object_get_context (IDE_OBJECT (initializer));
110   client = gbp_git_client_from_context (context);
111 
112   gbp_git_client_get_service_async (client,
113                                     cancellable,
114                                     get_service_cb,
115                                     g_steal_pointer (&task));
116 }
117 
118 static gboolean
119 gbp_git_vcs_initializer_initialize_finish (IdeVcsInitializer  *initializer,
120                                            GAsyncResult       *result,
121                                            GError            **error)
122 {
123   g_assert (IDE_IS_MAIN_THREAD ());
124   g_assert (GBP_IS_GIT_VCS_INITIALIZER (initializer));
125   g_assert (IDE_IS_TASK (result));
126 
127   return ide_task_propagate_boolean (IDE_TASK (result), error);
128 }
129 
130 static void
131 vcs_initializer_iface_init (IdeVcsInitializerInterface *iface)
132 {
133   iface->get_title = gbp_git_vcs_initializer_get_title;
134   iface->initialize_async = gbp_git_vcs_initializer_initialize_async;
135   iface->initialize_finish = gbp_git_vcs_initializer_initialize_finish;
136 }
137 
138 G_DEFINE_FINAL_TYPE_WITH_CODE (GbpGitVcsInitializer, gbp_git_vcs_initializer, IDE_TYPE_OBJECT,
139                          G_IMPLEMENT_INTERFACE (IDE_TYPE_VCS_INITIALIZER, vcs_initializer_iface_init))
140 
141 static void
142 gbp_git_vcs_initializer_class_init (GbpGitVcsInitializerClass *klass)
143 {
144 }
145 
146 static void
147 gbp_git_vcs_initializer_init (GbpGitVcsInitializer *self)
148 {
149 }
150