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