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.AppLaunchContext;
26 
27 private import gio.AppInfo;
28 private import gio.AppInfoIF;
29 private import gio.FileIF;
30 private import glib.ConstructionException;
31 private import glib.ListG;
32 private import glib.Str;
33 private import glib.Variant;
34 private import gobject.ObjectG;
35 private import gobject.Signals;
36 private import gtkc.gio;
37 public  import gtkc.giotypes;
38 private import std.algorithm;
39 
40 
41 /**
42  * Integrating the launch with the launching application. This is used to
43  * handle for instance startup notification and launching the new application
44  * on the same screen as the launching window.
45  */
46 public class AppLaunchContext : ObjectG
47 {
48 	/** the main Gtk struct */
49 	protected GAppLaunchContext* gAppLaunchContext;
50 
51 	/** Get the main Gtk struct */
52 	public GAppLaunchContext* getAppLaunchContextStruct(bool transferOwnership = false)
53 	{
54 		if (transferOwnership)
55 			ownedRef = false;
56 		return gAppLaunchContext;
57 	}
58 
59 	/** the main Gtk struct as a void* */
60 	protected override void* getStruct()
61 	{
62 		return cast(void*)gAppLaunchContext;
63 	}
64 
65 	protected override void setStruct(GObject* obj)
66 	{
67 		gAppLaunchContext = cast(GAppLaunchContext*)obj;
68 		super.setStruct(obj);
69 	}
70 
71 	/**
72 	 * Sets our main struct and passes it to the parent class.
73 	 */
74 	public this (GAppLaunchContext* gAppLaunchContext, bool ownedRef = false)
75 	{
76 		this.gAppLaunchContext = gAppLaunchContext;
77 		super(cast(GObject*)gAppLaunchContext, ownedRef);
78 	}
79 
80 
81 	/** */
82 	public static GType getType()
83 	{
84 		return g_app_launch_context_get_type();
85 	}
86 
87 	/**
88 	 * Creates a new application launch context. This is not normally used,
89 	 * instead you instantiate a subclass of this, such as #GdkAppLaunchContext.
90 	 *
91 	 * Returns: a #GAppLaunchContext.
92 	 *
93 	 * Throws: ConstructionException GTK+ fails to create the object.
94 	 */
95 	public this()
96 	{
97 		auto p = g_app_launch_context_new();
98 		
99 		if(p is null)
100 		{
101 			throw new ConstructionException("null returned by new");
102 		}
103 		
104 		this(cast(GAppLaunchContext*) p, true);
105 	}
106 
107 	/**
108 	 * Gets the display string for the @context. This is used to ensure new
109 	 * applications are started on the same display as the launching
110 	 * application, by setting the `DISPLAY` environment variable.
111 	 *
112 	 * Params:
113 	 *     info = a #GAppInfo
114 	 *     files = a #GList of #GFile objects
115 	 *
116 	 * Returns: a display string for the display.
117 	 */
118 	public string getDisplay(AppInfoIF info, ListG files)
119 	{
120 		auto retStr = g_app_launch_context_get_display(gAppLaunchContext, (info is null) ? null : info.getAppInfoStruct(), (files is null) ? null : files.getListGStruct());
121 		
122 		scope(exit) Str.freeString(retStr);
123 		return Str.toString(retStr);
124 	}
125 
126 	/**
127 	 * Gets the complete environment variable list to be passed to
128 	 * the child process when @context is used to launch an application.
129 	 * This is a %NULL-terminated array of strings, where each string has
130 	 * the form `KEY=VALUE`.
131 	 *
132 	 * Returns: the
133 	 *     child's environment
134 	 *
135 	 * Since: 2.32
136 	 */
137 	public string[] getEnvironment()
138 	{
139 		auto retStr = g_app_launch_context_get_environment(gAppLaunchContext);
140 		
141 		scope(exit) Str.freeStringArray(retStr);
142 		return Str.toStringArray(retStr);
143 	}
144 
145 	/**
146 	 * Initiates startup notification for the application and returns the
147 	 * `DESKTOP_STARTUP_ID` for the launched operation, if supported.
148 	 *
149 	 * Startup notification IDs are defined in the
150 	 * [FreeDesktop.Org Startup Notifications standard](http://standards.freedesktop.org/startup-notification-spec/startup-notification-latest.txt").
151 	 *
152 	 * Params:
153 	 *     info = a #GAppInfo
154 	 *     files = a #GList of of #GFile objects
155 	 *
156 	 * Returns: a startup notification ID for the application, or %NULL if
157 	 *     not supported.
158 	 */
159 	public string getStartupNotifyId(AppInfoIF info, ListG files)
160 	{
161 		auto retStr = g_app_launch_context_get_startup_notify_id(gAppLaunchContext, (info is null) ? null : info.getAppInfoStruct(), (files is null) ? null : files.getListGStruct());
162 		
163 		scope(exit) Str.freeString(retStr);
164 		return Str.toString(retStr);
165 	}
166 
167 	/**
168 	 * Called when an application has failed to launch, so that it can cancel
169 	 * the application startup notification started in g_app_launch_context_get_startup_notify_id().
170 	 *
171 	 * Params:
172 	 *     startupNotifyId = the startup notification id that was returned by g_app_launch_context_get_startup_notify_id().
173 	 */
174 	public void launchFailed(string startupNotifyId)
175 	{
176 		g_app_launch_context_launch_failed(gAppLaunchContext, Str.toStringz(startupNotifyId));
177 	}
178 
179 	/**
180 	 * Arranges for @variable to be set to @value in the child's
181 	 * environment when @context is used to launch an application.
182 	 *
183 	 * Params:
184 	 *     variable = the environment variable to set
185 	 *     value = the value for to set the variable to.
186 	 *
187 	 * Since: 2.32
188 	 */
189 	public void setenv(string variable, string value)
190 	{
191 		g_app_launch_context_setenv(gAppLaunchContext, Str.toStringz(variable), Str.toStringz(value));
192 	}
193 
194 	/**
195 	 * Arranges for @variable to be unset in the child's environment
196 	 * when @context is used to launch an application.
197 	 *
198 	 * Params:
199 	 *     variable = the environment variable to remove
200 	 *
201 	 * Since: 2.32
202 	 */
203 	public void unsetenv(string variable)
204 	{
205 		g_app_launch_context_unsetenv(gAppLaunchContext, Str.toStringz(variable));
206 	}
207 
208 	protected class OnLaunchFailedDelegateWrapper
209 	{
210 		static OnLaunchFailedDelegateWrapper[] listeners;
211 		void delegate(string, AppLaunchContext) dlg;
212 		gulong handlerId;
213 		
214 		this(void delegate(string, AppLaunchContext) dlg)
215 		{
216 			this.dlg = dlg;
217 			this.listeners ~= this;
218 		}
219 		
220 		void remove(OnLaunchFailedDelegateWrapper source)
221 		{
222 			foreach(index, wrapper; listeners)
223 			{
224 				if (wrapper.handlerId == source.handlerId)
225 				{
226 					listeners[index] = null;
227 					listeners = std.algorithm.remove(listeners, index);
228 					break;
229 				}
230 			}
231 		}
232 	}
233 
234 	/**
235 	 * The ::launch-failed signal is emitted when a #GAppInfo launch
236 	 * fails. The startup notification id is provided, so that the launcher
237 	 * can cancel the startup notification.
238 	 *
239 	 * Params:
240 	 *     startupNotifyId = the startup notification id for the failed launch
241 	 *
242 	 * Since: 2.36
243 	 */
244 	gulong addOnLaunchFailed(void delegate(string, AppLaunchContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
245 	{
246 		auto wrapper = new OnLaunchFailedDelegateWrapper(dlg);
247 		wrapper.handlerId = Signals.connectData(
248 			this,
249 			"launch-failed",
250 			cast(GCallback)&callBackLaunchFailed,
251 			cast(void*)wrapper,
252 			cast(GClosureNotify)&callBackLaunchFailedDestroy,
253 			connectFlags);
254 		return wrapper.handlerId;
255 	}
256 	
257 	extern(C) static void callBackLaunchFailed(GAppLaunchContext* applaunchcontextStruct, char* startupNotifyId, OnLaunchFailedDelegateWrapper wrapper)
258 	{
259 		wrapper.dlg(Str.toString(startupNotifyId), wrapper.outer);
260 	}
261 	
262 	extern(C) static void callBackLaunchFailedDestroy(OnLaunchFailedDelegateWrapper wrapper, GClosure* closure)
263 	{
264 		wrapper.remove(wrapper);
265 	}
266 
267 	protected class OnLaunchedDelegateWrapper
268 	{
269 		static OnLaunchedDelegateWrapper[] listeners;
270 		void delegate(AppInfoIF, Variant, AppLaunchContext) dlg;
271 		gulong handlerId;
272 		
273 		this(void delegate(AppInfoIF, Variant, AppLaunchContext) dlg)
274 		{
275 			this.dlg = dlg;
276 			this.listeners ~= this;
277 		}
278 		
279 		void remove(OnLaunchedDelegateWrapper source)
280 		{
281 			foreach(index, wrapper; listeners)
282 			{
283 				if (wrapper.handlerId == source.handlerId)
284 				{
285 					listeners[index] = null;
286 					listeners = std.algorithm.remove(listeners, index);
287 					break;
288 				}
289 			}
290 		}
291 	}
292 
293 	/**
294 	 * The ::launched signal is emitted when a #GAppInfo is successfully
295 	 * launched. The @platform_data is an GVariant dictionary mapping
296 	 * strings to variants (ie a{sv}), which contains additional,
297 	 * platform-specific data about this launch. On UNIX, at least the
298 	 * "pid" and "startup-notification-id" keys will be present.
299 	 *
300 	 * Params:
301 	 *     info = the #GAppInfo that was just launched
302 	 *     platformData = additional platform-specific data for this launch
303 	 *
304 	 * Since: 2.36
305 	 */
306 	gulong addOnLaunched(void delegate(AppInfoIF, Variant, AppLaunchContext) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
307 	{
308 		auto wrapper = new OnLaunchedDelegateWrapper(dlg);
309 		wrapper.handlerId = Signals.connectData(
310 			this,
311 			"launched",
312 			cast(GCallback)&callBackLaunched,
313 			cast(void*)wrapper,
314 			cast(GClosureNotify)&callBackLaunchedDestroy,
315 			connectFlags);
316 		return wrapper.handlerId;
317 	}
318 	
319 	extern(C) static void callBackLaunched(GAppLaunchContext* applaunchcontextStruct, GAppInfo* info, GVariant* platformData, OnLaunchedDelegateWrapper wrapper)
320 	{
321 		wrapper.dlg(ObjectG.getDObject!(AppInfo, AppInfoIF)(info), new Variant(platformData), wrapper.outer);
322 	}
323 	
324 	extern(C) static void callBackLaunchedDestroy(OnLaunchedDelegateWrapper wrapper, GClosure* closure)
325 	{
326 		wrapper.remove(wrapper);
327 	}
328 }