1 /* ide-pipeline-addin.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 "ide-pipeline-addin"
22 
23 #include "config.h"
24 
25 #include "ide-pipeline.h"
26 #include "ide-pipeline-addin.h"
27 
G_DEFINE_INTERFACE(IdePipelineAddin,ide_pipeline_addin,IDE_TYPE_OBJECT)28 G_DEFINE_INTERFACE (IdePipelineAddin, ide_pipeline_addin, IDE_TYPE_OBJECT)
29 
30 static void
31 ide_pipeline_addin_default_init (IdePipelineAddinInterface *iface)
32 {
33 }
34 
35 /**
36  * ide_pipeline_addin_prepare:
37  * @self: a #IdePipelineAddin
38  * @pipeline: an #IdePipeline
39  *
40  * This function is called before prepare so that plugins may setup
41  * signals on the pipeline that may allow them to affect how other
42  * plugins interact.
43  *
44  * For example, if you need to connect to pipeline::launcher-created,
45  * you might want to do that here.
46  *
47  * Since: 3.34
48  */
49 void
ide_pipeline_addin_prepare(IdePipelineAddin * self,IdePipeline * pipeline)50 ide_pipeline_addin_prepare (IdePipelineAddin *self,
51                             IdePipeline      *pipeline)
52 {
53   g_return_if_fail (IDE_IS_PIPELINE_ADDIN (self));
54   g_return_if_fail (IDE_IS_PIPELINE (pipeline));
55 
56   if (IDE_PIPELINE_ADDIN_GET_IFACE (self)->prepare)
57     IDE_PIPELINE_ADDIN_GET_IFACE (self)->prepare (self, pipeline);
58 }
59 
60 void
ide_pipeline_addin_load(IdePipelineAddin * self,IdePipeline * pipeline)61 ide_pipeline_addin_load (IdePipelineAddin *self,
62                          IdePipeline      *pipeline)
63 {
64   g_return_if_fail (IDE_IS_PIPELINE_ADDIN (self));
65   g_return_if_fail (IDE_IS_PIPELINE (pipeline));
66 
67   if (IDE_PIPELINE_ADDIN_GET_IFACE (self)->load)
68     IDE_PIPELINE_ADDIN_GET_IFACE (self)->load (self, pipeline);
69 }
70 
71 void
ide_pipeline_addin_unload(IdePipelineAddin * self,IdePipeline * pipeline)72 ide_pipeline_addin_unload (IdePipelineAddin *self,
73                            IdePipeline      *pipeline)
74 {
75   GArray *ar;
76 
77   g_return_if_fail (IDE_IS_PIPELINE_ADDIN (self));
78   g_return_if_fail (IDE_IS_PIPELINE (pipeline));
79 
80   if (IDE_PIPELINE_ADDIN_GET_IFACE (self)->unload)
81     IDE_PIPELINE_ADDIN_GET_IFACE (self)->unload (self, pipeline);
82 
83   /* Unload any stages that are tracked by the addin */
84   ar = g_object_get_data (G_OBJECT (self), "IDE_PIPELINE_ADDIN_STAGES");
85 
86   if G_LIKELY (ar != NULL)
87     {
88       for (guint i = 0; i < ar->len; i++)
89         {
90           guint stage_id = g_array_index (ar, guint, i);
91 
92           ide_pipeline_detach (pipeline, stage_id);
93         }
94     }
95 }
96 
97 /**
98  * ide_pipeline_addin_track:
99  * @self: An #IdePipelineAddin
100  * @stage_id: a stage id returned from ide_pipeline_attach()
101  *
102  * This function will track the stage_id that was returned from
103  * ide_pipeline_attach() or similar functions. Doing so results in
104  * the stage being automatically disconnected when the addin is unloaded.
105  *
106  * This means that many #IdePipelineAddin implementations do not need
107  * an unload vfunc if they track all registered stages.
108  *
109  * You should not mix this function with manual pipeline disconnections.
110  * While it should work, that is not yet guaranteed.
111  *
112  * Since: 3.32
113  */
114 void
ide_pipeline_addin_track(IdePipelineAddin * self,guint stage_id)115 ide_pipeline_addin_track (IdePipelineAddin *self,
116                                 guint                  stage_id)
117 {
118   GArray *ar;
119 
120   g_return_if_fail (IDE_IS_PIPELINE_ADDIN (self));
121   g_return_if_fail (stage_id > 0);
122 
123   ar = g_object_get_data (G_OBJECT (self), "IDE_PIPELINE_ADDIN_STAGES");
124 
125   if (ar == NULL)
126     {
127       ar = g_array_new (FALSE, FALSE, sizeof (guint));
128       g_object_set_data_full (G_OBJECT (self), "IDE_PIPELINE_ADDIN_STAGES",
129                               ar, (GDestroyNotify)g_array_unref);
130     }
131 
132   g_array_append_val (ar, stage_id);
133 }
134