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