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.AppInfoMonitor;
26 
27 private import gobject.ObjectG;
28 private import gobject.Signals;
29 private import gtkc.gio;
30 public  import gtkc.giotypes;
31 private import std.algorithm;
32 
33 
34 /**
35  * #GAppInfoMonitor is a very simple object used for monitoring the app
36  * info database for changes (ie: newly installed or removed
37  * applications).
38  * 
39  * Call g_app_info_monitor_get() to get a #GAppInfoMonitor and connect
40  * to the "changed" signal.
41  * 
42  * In the usual case, applications should try to make note of the change
43  * (doing things like invalidating caches) but not act on it.  In
44  * particular, applications should avoid making calls to #GAppInfo APIs
45  * in response to the change signal, deferring these until the time that
46  * the data is actually required.  The exception to this case is when
47  * application information is actually being displayed on the screen
48  * (eg: during a search or when the list of all applications is shown).
49  * The reason for this is that changes to the list of installed
50  * applications often come in groups (like during system updates) and
51  * rescanning the list on every change is pointless and expensive.
52  *
53  * Since: 2.40
54  */
55 public class AppInfoMonitor : ObjectG
56 {
57 	/** the main Gtk struct */
58 	protected GAppInfoMonitor* gAppInfoMonitor;
59 
60 	/** Get the main Gtk struct */
61 	public GAppInfoMonitor* getAppInfoMonitorStruct(bool transferOwnership = false)
62 	{
63 		if (transferOwnership)
64 			ownedRef = false;
65 		return gAppInfoMonitor;
66 	}
67 
68 	/** the main Gtk struct as a void* */
69 	protected override void* getStruct()
70 	{
71 		return cast(void*)gAppInfoMonitor;
72 	}
73 
74 	protected override void setStruct(GObject* obj)
75 	{
76 		gAppInfoMonitor = cast(GAppInfoMonitor*)obj;
77 		super.setStruct(obj);
78 	}
79 
80 	/**
81 	 * Sets our main struct and passes it to the parent class.
82 	 */
83 	public this (GAppInfoMonitor* gAppInfoMonitor, bool ownedRef = false)
84 	{
85 		this.gAppInfoMonitor = gAppInfoMonitor;
86 		super(cast(GObject*)gAppInfoMonitor, ownedRef);
87 	}
88 
89 
90 	/** */
91 	public static GType getType()
92 	{
93 		return g_app_info_monitor_get_type();
94 	}
95 
96 	/**
97 	 * Gets the #GAppInfoMonitor for the current thread-default main
98 	 * context.
99 	 *
100 	 * The #GAppInfoMonitor will emit a "changed" signal in the
101 	 * thread-default main context whenever the list of installed
102 	 * applications (as reported by g_app_info_get_all()) may have changed.
103 	 *
104 	 * You must only call g_object_unref() on the return value from under
105 	 * the same main context as you created it.
106 	 *
107 	 * Returns: a reference to a #GAppInfoMonitor
108 	 *
109 	 * Since: 2.40
110 	 */
111 	public static AppInfoMonitor get()
112 	{
113 		auto p = g_app_info_monitor_get();
114 		
115 		if(p is null)
116 		{
117 			return null;
118 		}
119 		
120 		return ObjectG.getDObject!(AppInfoMonitor)(cast(GAppInfoMonitor*) p, true);
121 	}
122 
123 	protected class OnChangedDelegateWrapper
124 	{
125 		void delegate(AppInfoMonitor) dlg;
126 		gulong handlerId;
127 		
128 		this(void delegate(AppInfoMonitor) dlg)
129 		{
130 			this.dlg = dlg;
131 			onChangedListeners ~= this;
132 		}
133 		
134 		void remove(OnChangedDelegateWrapper source)
135 		{
136 			foreach(index, wrapper; onChangedListeners)
137 			{
138 				if (wrapper.handlerId == source.handlerId)
139 				{
140 					onChangedListeners[index] = null;
141 					onChangedListeners = std.algorithm.remove(onChangedListeners, index);
142 					break;
143 				}
144 			}
145 		}
146 	}
147 	OnChangedDelegateWrapper[] onChangedListeners;
148 
149 	/**
150 	 * Signal emitted when the app info database for changes (ie: newly installed
151 	 * or removed applications).
152 	 */
153 	gulong addOnChanged(void delegate(AppInfoMonitor) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
154 	{
155 		auto wrapper = new OnChangedDelegateWrapper(dlg);
156 		wrapper.handlerId = Signals.connectData(
157 			this,
158 			"changed",
159 			cast(GCallback)&callBackChanged,
160 			cast(void*)wrapper,
161 			cast(GClosureNotify)&callBackChangedDestroy,
162 			connectFlags);
163 		return wrapper.handlerId;
164 	}
165 	
166 	extern(C) static void callBackChanged(GAppInfoMonitor* appinfomonitorStruct, OnChangedDelegateWrapper wrapper)
167 	{
168 		wrapper.dlg(wrapper.outer);
169 	}
170 	
171 	extern(C) static void callBackChangedDestroy(OnChangedDelegateWrapper wrapper, GClosure* closure)
172 	{
173 		wrapper.remove(wrapper);
174 	}
175 }