1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module peas.ExtensionSet;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import peas.Engine;
32 private import peas.PluginInfo;
33 private import peas.c.functions;
34 public  import peas.c.types;
35 private import std.algorithm;
36 
37 
38 /**
39  * The #PeasExtensionSet structure contains only private data and should only
40  * be accessed using the provided API.
41  */
42 public class ExtensionSet : ObjectG
43 {
44 	/** the main Gtk struct */
45 	protected PeasExtensionSet* peasExtensionSet;
46 
47 	/** Get the main Gtk struct */
48 	public PeasExtensionSet* getExtensionSetStruct(bool transferOwnership = false)
49 	{
50 		if (transferOwnership)
51 			ownedRef = false;
52 		return peasExtensionSet;
53 	}
54 
55 	/** the main Gtk struct as a void* */
56 	protected override void* getStruct()
57 	{
58 		return cast(void*)peasExtensionSet;
59 	}
60 
61 	protected override void setStruct(GObject* obj)
62 	{
63 		peasExtensionSet = cast(PeasExtensionSet*)obj;
64 		super.setStruct(obj);
65 	}
66 
67 	/**
68 	 * Sets our main struct and passes it to the parent class.
69 	 */
70 	public this (PeasExtensionSet* peasExtensionSet, bool ownedRef = false)
71 	{
72 		this.peasExtensionSet = peasExtensionSet;
73 		super(cast(GObject*)peasExtensionSet, ownedRef);
74 	}
75 
76 
77 	/** */
78 	public static GType getType()
79 	{
80 		return peas_extension_set_get_type();
81 	}
82 
83 	/**
84 	 * Create a new #PeasExtensionSet for the @exten_type extension type.
85 	 *
86 	 * If @engine is %NULL, then the default engine will be used.
87 	 *
88 	 * Since libpeas 1.22, @exten_type can be an Abstract #GType
89 	 * and not just an Interface #GType.
90 	 *
91 	 * See peas_extension_set_new() for more information.
92 	 *
93 	 * Params:
94 	 *     engine = A #PeasEngine, or %NULL.
95 	 *     extenType = the extension #GType.
96 	 *     firstProperty = the name of the first property.
97 	 *     varArgs = the value of the first property, followed optionally by more
98 	 *         name/value pairs, followed by %NULL.
99 	 *
100 	 * Returns: a new instance of #PeasExtensionSet.
101 	 *
102 	 * Throws: ConstructionException GTK+ fails to create the object.
103 	 */
104 	public this(Engine engine, GType extenType, string firstProperty, void* varArgs)
105 	{
106 		auto p = peas_extension_set_new_valist((engine is null) ? null : engine.getEngineStruct(), extenType, Str.toStringz(firstProperty), varArgs);
107 
108 		if(p is null)
109 		{
110 			throw new ConstructionException("null returned by new_valist");
111 		}
112 
113 		this(cast(PeasExtensionSet*) p, true);
114 	}
115 
116 	/**
117 	 * Create a new #PeasExtensionSet for the @exten_type extension type.
118 	 *
119 	 * If @engine is %NULL, then the default engine will be used.
120 	 *
121 	 * Since libpeas 1.22, @exten_type can be an Abstract #GType
122 	 * and not just an Interface #GType.
123 	 *
124 	 * See peas_extension_set_new() for more information.
125 	 *
126 	 * Params:
127 	 *     engine = A #PeasEngine, or %NULL.
128 	 *     extenType = the extension #GType.
129 	 *     parameters = an array of #GParameter.
130 	 *
131 	 * Returns: a new instance of #PeasExtensionSet.
132 	 *
133 	 * Throws: ConstructionException GTK+ fails to create the object.
134 	 */
135 	public this(Engine engine, GType extenType, GParameter[] parameters)
136 	{
137 		auto p = peas_extension_set_newv((engine is null) ? null : engine.getEngineStruct(), extenType, cast(uint)parameters.length, parameters.ptr);
138 
139 		if(p is null)
140 		{
141 			throw new ConstructionException("null returned by newv");
142 		}
143 
144 		this(cast(PeasExtensionSet*) p, true);
145 	}
146 
147 	/**
148 	 * Calls @func for each #PeasExtension.
149 	 *
150 	 * Params:
151 	 *     func = A function call for each extension.
152 	 *     data = Optional data to be passed to the function or %NULL.
153 	 *
154 	 * Since: 1.2
155 	 */
156 	public void foreac(PeasExtensionSetForeachFunc func, void* data)
157 	{
158 		peas_extension_set_foreach(peasExtensionSet, func, data);
159 	}
160 
161 	/**
162 	 * Returns the #PeasExtension object corresponding to @info, or %NULL
163 	 * if the plugin doesn't provide such an extension.
164 	 *
165 	 * Params:
166 	 *     info = a #PeasPluginInfo
167 	 *
168 	 * Returns: a reference to a #PeasExtension or %NULL
169 	 */
170 	public PeasExtension* getExtension(PluginInfo info)
171 	{
172 		return peas_extension_set_get_extension(peasExtensionSet, (info is null) ? null : info.getPluginInfoStruct());
173 	}
174 
175 	protected class OnExtensionAddedDelegateWrapper
176 	{
177 		void delegate(PluginInfo, ObjectG, ExtensionSet) dlg;
178 		gulong handlerId;
179 
180 		this(void delegate(PluginInfo, ObjectG, ExtensionSet) dlg)
181 		{
182 			this.dlg = dlg;
183 			onExtensionAddedListeners ~= this;
184 		}
185 
186 		void remove(OnExtensionAddedDelegateWrapper source)
187 		{
188 			foreach(index, wrapper; onExtensionAddedListeners)
189 			{
190 				if (wrapper.handlerId == source.handlerId)
191 				{
192 					onExtensionAddedListeners[index] = null;
193 					onExtensionAddedListeners = std.algorithm.remove(onExtensionAddedListeners, index);
194 					break;
195 				}
196 			}
197 		}
198 	}
199 	OnExtensionAddedDelegateWrapper[] onExtensionAddedListeners;
200 
201 	/**
202 	 * The extension-added signal is emitted when a new extension has been
203 	 * added to the #PeasExtensionSet. It happens when a new plugin implementing
204 	 * the extension set's extension type is loaded.
205 	 *
206 	 * You should connect to this signal in order to set up the extensions when
207 	 * they are loaded. Note that this signal is not fired for extensions coming
208 	 * from plugins that were already loaded when the #PeasExtensionSet instance
209 	 * was created. You should set those up by yourself.
210 	 *
211 	 * Params:
212 	 *     info = A #PeasPluginInfo.
213 	 *     exten = A #PeasExtension.
214 	 */
215 	gulong addOnExtensionAdded(void delegate(PluginInfo, ObjectG, ExtensionSet) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
216 	{
217 		auto wrapper = new OnExtensionAddedDelegateWrapper(dlg);
218 		wrapper.handlerId = Signals.connectData(
219 			this,
220 			"extension-added",
221 			cast(GCallback)&callBackExtensionAdded,
222 			cast(void*)wrapper,
223 			cast(GClosureNotify)&callBackExtensionAddedDestroy,
224 			connectFlags);
225 		return wrapper.handlerId;
226 	}
227 
228 	extern(C) static void callBackExtensionAdded(PeasExtensionSet* extensionsetStruct, PeasPluginInfo* info, GObject* exten, OnExtensionAddedDelegateWrapper wrapper)
229 	{
230 		wrapper.dlg(ObjectG.getDObject!(PluginInfo)(info), ObjectG.getDObject!(ObjectG)(exten), wrapper.outer);
231 	}
232 
233 	extern(C) static void callBackExtensionAddedDestroy(OnExtensionAddedDelegateWrapper wrapper, GClosure* closure)
234 	{
235 		wrapper.remove(wrapper);
236 	}
237 
238 	protected class OnExtensionRemovedDelegateWrapper
239 	{
240 		void delegate(PluginInfo, ObjectG, ExtensionSet) dlg;
241 		gulong handlerId;
242 
243 		this(void delegate(PluginInfo, ObjectG, ExtensionSet) dlg)
244 		{
245 			this.dlg = dlg;
246 			onExtensionRemovedListeners ~= this;
247 		}
248 
249 		void remove(OnExtensionRemovedDelegateWrapper source)
250 		{
251 			foreach(index, wrapper; onExtensionRemovedListeners)
252 			{
253 				if (wrapper.handlerId == source.handlerId)
254 				{
255 					onExtensionRemovedListeners[index] = null;
256 					onExtensionRemovedListeners = std.algorithm.remove(onExtensionRemovedListeners, index);
257 					break;
258 				}
259 			}
260 		}
261 	}
262 	OnExtensionRemovedDelegateWrapper[] onExtensionRemovedListeners;
263 
264 	/**
265 	 * The extension-removed signal is emitted when a new extension is about to be
266 	 * removed from the #PeasExtensionSet. It happens when a plugin implementing
267 	 * the extension set's extension type is unloaded, or when the
268 	 * #PeasExtensionSet itself is destroyed.
269 	 *
270 	 * You should connect to this signal in order to clean up the extensions
271 	 * when their plugin is unload. Note that this signal is not fired for the
272 	 * #PeasExtension instances still available when the #PeasExtensionSet
273 	 * instance is destroyed. You should clean those up by yourself.
274 	 *
275 	 * Params:
276 	 *     info = A #PeasPluginInfo.
277 	 *     exten = A #PeasExtension.
278 	 */
279 	gulong addOnExtensionRemoved(void delegate(PluginInfo, ObjectG, ExtensionSet) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
280 	{
281 		auto wrapper = new OnExtensionRemovedDelegateWrapper(dlg);
282 		wrapper.handlerId = Signals.connectData(
283 			this,
284 			"extension-removed",
285 			cast(GCallback)&callBackExtensionRemoved,
286 			cast(void*)wrapper,
287 			cast(GClosureNotify)&callBackExtensionRemovedDestroy,
288 			connectFlags);
289 		return wrapper.handlerId;
290 	}
291 
292 	extern(C) static void callBackExtensionRemoved(PeasExtensionSet* extensionsetStruct, PeasPluginInfo* info, GObject* exten, OnExtensionRemovedDelegateWrapper wrapper)
293 	{
294 		wrapper.dlg(ObjectG.getDObject!(PluginInfo)(info), ObjectG.getDObject!(ObjectG)(exten), wrapper.outer);
295 	}
296 
297 	extern(C) static void callBackExtensionRemovedDestroy(OnExtensionRemovedDelegateWrapper wrapper, GClosure* closure)
298 	{
299 		wrapper.remove(wrapper);
300 	}
301 }