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 gio.DBusObjectT;
26 
27 public  import gio.DBusInterface;
28 public  import gio.DBusInterfaceIF;
29 public  import glib.ListG;
30 public  import glib.Str;
31 public  import gobject.ObjectG;
32 public  import gobject.Signals;
33 public  import gtkc.gio;
34 public  import gtkc.giotypes;
35 public  import std.algorithm;
36 
37 
38 /**
39  * The #GDBusObject type is the base type for D-Bus objects on both
40  * the service side (see #GDBusObjectSkeleton) and the client side
41  * (see #GDBusObjectProxy). It is essentially just a container of
42  * interfaces.
43  */
44 public template DBusObjectT(TStruct)
45 {
46 	/** Get the main Gtk struct */
47 	public GDBusObject* getDBusObjectStruct(bool transferOwnership = false)
48 	{
49 		if (transferOwnership)
50 			ownedRef = false;
51 		return cast(GDBusObject*)getStruct();
52 	}
53 
54 
55 	/**
56 	 * Gets the D-Bus interface with name @interface_name associated with
57 	 * @object, if any.
58 	 *
59 	 * Params:
60 	 *     interfaceName = A D-Bus interface name.
61 	 *
62 	 * Returns: %NULL if not found, otherwise a
63 	 *     #GDBusInterface that must be freed with g_object_unref().
64 	 *
65 	 * Since: 2.30
66 	 */
67 	public DBusInterfaceIF getInterface(string interfaceName)
68 	{
69 		auto p = g_dbus_object_get_interface(getDBusObjectStruct(), Str.toStringz(interfaceName));
70 		
71 		if(p is null)
72 		{
73 			return null;
74 		}
75 		
76 		return ObjectG.getDObject!(DBusInterface, DBusInterfaceIF)(cast(GDBusInterface*) p, true);
77 	}
78 
79 	/**
80 	 * Gets the D-Bus interfaces associated with @object.
81 	 *
82 	 * Returns: A list of #GDBusInterface instances.
83 	 *     The returned list must be freed by g_list_free() after each element has been freed
84 	 *     with g_object_unref().
85 	 *
86 	 * Since: 2.30
87 	 */
88 	public ListG getInterfaces()
89 	{
90 		auto p = g_dbus_object_get_interfaces(getDBusObjectStruct());
91 		
92 		if(p is null)
93 		{
94 			return null;
95 		}
96 		
97 		return new ListG(cast(GList*) p, true);
98 	}
99 
100 	/**
101 	 * Gets the object path for @object.
102 	 *
103 	 * Returns: A string owned by @object. Do not free.
104 	 *
105 	 * Since: 2.30
106 	 */
107 	public string getObjectPath()
108 	{
109 		return Str.toString(g_dbus_object_get_object_path(getDBusObjectStruct()));
110 	}
111 
112 	protected class OnInterfaceAddedDelegateWrapper
113 	{
114 		void delegate(DBusInterfaceIF, DBusObjectIF) dlg;
115 		gulong handlerId;
116 		
117 		this(void delegate(DBusInterfaceIF, DBusObjectIF) dlg)
118 		{
119 			this.dlg = dlg;
120 			onInterfaceAddedListeners ~= this;
121 		}
122 		
123 		void remove(OnInterfaceAddedDelegateWrapper source)
124 		{
125 			foreach(index, wrapper; onInterfaceAddedListeners)
126 			{
127 				if (wrapper.handlerId == source.handlerId)
128 				{
129 					onInterfaceAddedListeners[index] = null;
130 					onInterfaceAddedListeners = std.algorithm.remove(onInterfaceAddedListeners, index);
131 					break;
132 				}
133 			}
134 		}
135 	}
136 	OnInterfaceAddedDelegateWrapper[] onInterfaceAddedListeners;
137 
138 	/**
139 	 * Emitted when @interface is added to @object.
140 	 *
141 	 * Params:
142 	 *     iface = The #GDBusInterface that was added.
143 	 *
144 	 * Since: 2.30
145 	 */
146 	gulong addOnInterfaceAdded(void delegate(DBusInterfaceIF, DBusObjectIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
147 	{
148 		auto wrapper = new OnInterfaceAddedDelegateWrapper(dlg);
149 		wrapper.handlerId = Signals.connectData(
150 			this,
151 			"interface-added",
152 			cast(GCallback)&callBackInterfaceAdded,
153 			cast(void*)wrapper,
154 			cast(GClosureNotify)&callBackInterfaceAddedDestroy,
155 			connectFlags);
156 		return wrapper.handlerId;
157 	}
158 	
159 	extern(C) static void callBackInterfaceAdded(GDBusObject* dbusobjectStruct, GDBusInterface* iface, OnInterfaceAddedDelegateWrapper wrapper)
160 	{
161 		wrapper.dlg(ObjectG.getDObject!(DBusInterface, DBusInterfaceIF)(iface), wrapper.outer);
162 	}
163 	
164 	extern(C) static void callBackInterfaceAddedDestroy(OnInterfaceAddedDelegateWrapper wrapper, GClosure* closure)
165 	{
166 		wrapper.remove(wrapper);
167 	}
168 
169 	protected class OnInterfaceRemovedDelegateWrapper
170 	{
171 		void delegate(DBusInterfaceIF, DBusObjectIF) dlg;
172 		gulong handlerId;
173 		
174 		this(void delegate(DBusInterfaceIF, DBusObjectIF) dlg)
175 		{
176 			this.dlg = dlg;
177 			onInterfaceRemovedListeners ~= this;
178 		}
179 		
180 		void remove(OnInterfaceRemovedDelegateWrapper source)
181 		{
182 			foreach(index, wrapper; onInterfaceRemovedListeners)
183 			{
184 				if (wrapper.handlerId == source.handlerId)
185 				{
186 					onInterfaceRemovedListeners[index] = null;
187 					onInterfaceRemovedListeners = std.algorithm.remove(onInterfaceRemovedListeners, index);
188 					break;
189 				}
190 			}
191 		}
192 	}
193 	OnInterfaceRemovedDelegateWrapper[] onInterfaceRemovedListeners;
194 
195 	/**
196 	 * Emitted when @interface is removed from @object.
197 	 *
198 	 * Params:
199 	 *     iface = The #GDBusInterface that was removed.
200 	 *
201 	 * Since: 2.30
202 	 */
203 	gulong addOnInterfaceRemoved(void delegate(DBusInterfaceIF, DBusObjectIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
204 	{
205 		auto wrapper = new OnInterfaceRemovedDelegateWrapper(dlg);
206 		wrapper.handlerId = Signals.connectData(
207 			this,
208 			"interface-removed",
209 			cast(GCallback)&callBackInterfaceRemoved,
210 			cast(void*)wrapper,
211 			cast(GClosureNotify)&callBackInterfaceRemovedDestroy,
212 			connectFlags);
213 		return wrapper.handlerId;
214 	}
215 	
216 	extern(C) static void callBackInterfaceRemoved(GDBusObject* dbusobjectStruct, GDBusInterface* iface, OnInterfaceRemovedDelegateWrapper wrapper)
217 	{
218 		wrapper.dlg(ObjectG.getDObject!(DBusInterface, DBusInterfaceIF)(iface), wrapper.outer);
219 	}
220 	
221 	extern(C) static void callBackInterfaceRemovedDestroy(OnInterfaceRemovedDelegateWrapper wrapper, GClosure* closure)
222 	{
223 		wrapper.remove(wrapper);
224 	}
225 }