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 gtk.RecentManager;
26 
27 private import glib.ConstructionException;
28 private import glib.ErrorG;
29 private import glib.GException;
30 private import glib.ListG;
31 private import glib.Str;
32 private import gobject.ObjectG;
33 private import gobject.Signals;
34 private import gtk.RecentInfo;
35 private import gtkc.gtk;
36 public  import gtkc.gtktypes;
37 private import std.algorithm;
38 
39 
40 /**
41  * #GtkRecentManager provides a facility for adding, removing and
42  * looking up recently used files. Each recently used file is
43  * identified by its URI, and has meta-data associated to it, like
44  * the names and command lines of the applications that have
45  * registered it, the number of time each application has registered
46  * the same file, the mime type of the file and whether the file
47  * should be displayed only by the applications that have
48  * registered it.
49  * 
50  * The recently used files list is per user.
51  * 
52  * The #GtkRecentManager acts like a database of all the recently
53  * used files. You can create new #GtkRecentManager objects, but
54  * it is more efficient to use the default manager created by GTK+.
55  * 
56  * Adding a new recently used file is as simple as:
57  * 
58  * |[<!-- language="C" -->
59  * GtkRecentManager *manager;
60  * 
61  * manager = gtk_recent_manager_get_default ();
62  * gtk_recent_manager_add_item (manager, file_uri);
63  * ]|
64  * 
65  * The #GtkRecentManager will try to gather all the needed information
66  * from the file itself through GIO.
67  * 
68  * Looking up the meta-data associated with a recently used file
69  * given its URI requires calling gtk_recent_manager_lookup_item():
70  * 
71  * |[<!-- language="C" -->
72  * GtkRecentManager *manager;
73  * GtkRecentInfo *info;
74  * GError *error = NULL;
75  * 
76  * manager = gtk_recent_manager_get_default ();
77  * info = gtk_recent_manager_lookup_item (manager, file_uri, &error);
78  * if (error)
79  * {
80  * g_warning ("Could not find the file: %s", error->message);
81  * g_error_free (error);
82  * }
83  * else
84  * {
85  * // Use the info object
86  * gtk_recent_info_unref (info);
87  * }
88  * ]|
89  * 
90  * In order to retrieve the list of recently used files, you can use
91  * gtk_recent_manager_get_items(), which returns a list of #GtkRecentInfo-structs.
92  * 
93  * A #GtkRecentManager is the model used to populate the contents of
94  * one, or more #GtkRecentChooser implementations.
95  * 
96  * Note that the maximum age of the recently used files list is
97  * controllable through the #GtkSettings:gtk-recent-files-max-age
98  * property.
99  * 
100  * Recently used files are supported since GTK+ 2.10.
101  *
102  * Since: 2.10
103  */
104 public class RecentManager : ObjectG
105 {
106 	/** the main Gtk struct */
107 	protected GtkRecentManager* gtkRecentManager;
108 
109 	/** Get the main Gtk struct */
110 	public GtkRecentManager* getRecentManagerStruct(bool transferOwnership = false)
111 	{
112 		if (transferOwnership)
113 			ownedRef = false;
114 		return gtkRecentManager;
115 	}
116 
117 	/** the main Gtk struct as a void* */
118 	protected override void* getStruct()
119 	{
120 		return cast(void*)gtkRecentManager;
121 	}
122 
123 	protected override void setStruct(GObject* obj)
124 	{
125 		gtkRecentManager = cast(GtkRecentManager*)obj;
126 		super.setStruct(obj);
127 	}
128 
129 	/**
130 	 * Sets our main struct and passes it to the parent class.
131 	 */
132 	public this (GtkRecentManager* gtkRecentManager, bool ownedRef = false)
133 	{
134 		this.gtkRecentManager = gtkRecentManager;
135 		super(cast(GObject*)gtkRecentManager, ownedRef);
136 	}
137 
138 
139 	/** */
140 	public static GType getType()
141 	{
142 		return gtk_recent_manager_get_type();
143 	}
144 
145 	/**
146 	 * Creates a new recent manager object. Recent manager objects are used to
147 	 * handle the list of recently used resources. A #GtkRecentManager object
148 	 * monitors the recently used resources list, and emits the “changed” signal
149 	 * each time something inside the list changes.
150 	 *
151 	 * #GtkRecentManager objects are expensive: be sure to create them only when
152 	 * needed. You should use gtk_recent_manager_get_default() instead.
153 	 *
154 	 * Returns: A newly created #GtkRecentManager object
155 	 *
156 	 * Since: 2.10
157 	 *
158 	 * Throws: ConstructionException GTK+ fails to create the object.
159 	 */
160 	public this()
161 	{
162 		auto p = gtk_recent_manager_new();
163 		
164 		if(p is null)
165 		{
166 			throw new ConstructionException("null returned by new");
167 		}
168 		
169 		this(cast(GtkRecentManager*) p, true);
170 	}
171 
172 	/**
173 	 * Gets a unique instance of #GtkRecentManager, that you can share
174 	 * in your application without caring about memory management.
175 	 *
176 	 * Returns: A unique #GtkRecentManager. Do not ref or
177 	 *     unref it.
178 	 *
179 	 * Since: 2.10
180 	 */
181 	public static RecentManager getDefault()
182 	{
183 		auto p = gtk_recent_manager_get_default();
184 		
185 		if(p is null)
186 		{
187 			return null;
188 		}
189 		
190 		return ObjectG.getDObject!(RecentManager)(cast(GtkRecentManager*) p);
191 	}
192 
193 	/**
194 	 * Adds a new resource, pointed by @uri, into the recently used
195 	 * resources list, using the metadata specified inside the
196 	 * #GtkRecentData-struct passed in @recent_data.
197 	 *
198 	 * The passed URI will be used to identify this resource inside the
199 	 * list.
200 	 *
201 	 * In order to register the new recently used resource, metadata about
202 	 * the resource must be passed as well as the URI; the metadata is
203 	 * stored in a #GtkRecentData-struct, which must contain the MIME
204 	 * type of the resource pointed by the URI; the name of the application
205 	 * that is registering the item, and a command line to be used when
206 	 * launching the item.
207 	 *
208 	 * Optionally, a #GtkRecentData-struct might contain a UTF-8 string
209 	 * to be used when viewing the item instead of the last component of
210 	 * the URI; a short description of the item; whether the item should
211 	 * be considered private - that is, should be displayed only by the
212 	 * applications that have registered it.
213 	 *
214 	 * Params:
215 	 *     uri = a valid URI
216 	 *     recentData = metadata of the resource
217 	 *
218 	 * Returns: %TRUE if the new item was successfully added to the
219 	 *     recently used resources list, %FALSE otherwise
220 	 *
221 	 * Since: 2.10
222 	 */
223 	public bool addFull(string uri, GtkRecentData* recentData)
224 	{
225 		return gtk_recent_manager_add_full(gtkRecentManager, Str.toStringz(uri), recentData) != 0;
226 	}
227 
228 	/**
229 	 * Adds a new resource, pointed by @uri, into the recently used
230 	 * resources list.
231 	 *
232 	 * This function automatically retrieves some of the needed
233 	 * metadata and setting other metadata to common default values;
234 	 * it then feeds the data to gtk_recent_manager_add_full().
235 	 *
236 	 * See gtk_recent_manager_add_full() if you want to explicitly
237 	 * define the metadata for the resource pointed by @uri.
238 	 *
239 	 * Params:
240 	 *     uri = a valid URI
241 	 *
242 	 * Returns: %TRUE if the new item was successfully added
243 	 *     to the recently used resources list
244 	 *
245 	 * Since: 2.10
246 	 */
247 	public bool addItem(string uri)
248 	{
249 		return gtk_recent_manager_add_item(gtkRecentManager, Str.toStringz(uri)) != 0;
250 	}
251 
252 	/**
253 	 * Gets the list of recently used resources.
254 	 *
255 	 * Returns: a list of
256 	 *     newly allocated #GtkRecentInfo objects. Use
257 	 *     gtk_recent_info_unref() on each item inside the list, and then
258 	 *     free the list itself using g_list_free().
259 	 *
260 	 * Since: 2.10
261 	 */
262 	public ListG getItems()
263 	{
264 		auto p = gtk_recent_manager_get_items(gtkRecentManager);
265 		
266 		if(p is null)
267 		{
268 			return null;
269 		}
270 		
271 		return new ListG(cast(GList*) p, true);
272 	}
273 
274 	/**
275 	 * Checks whether there is a recently used resource registered
276 	 * with @uri inside the recent manager.
277 	 *
278 	 * Params:
279 	 *     uri = a URI
280 	 *
281 	 * Returns: %TRUE if the resource was found, %FALSE otherwise
282 	 *
283 	 * Since: 2.10
284 	 */
285 	public bool hasItem(string uri)
286 	{
287 		return gtk_recent_manager_has_item(gtkRecentManager, Str.toStringz(uri)) != 0;
288 	}
289 
290 	/**
291 	 * Searches for a URI inside the recently used resources list, and
292 	 * returns a #GtkRecentInfo-struct containing informations about the resource
293 	 * like its MIME type, or its display name.
294 	 *
295 	 * Params:
296 	 *     uri = a URI
297 	 *
298 	 * Returns: a #GtkRecentInfo-struct containing information
299 	 *     about the resource pointed by @uri, or %NULL if the URI was
300 	 *     not registered in the recently used resources list. Free with
301 	 *     gtk_recent_info_unref().
302 	 *
303 	 * Since: 2.10
304 	 *
305 	 * Throws: GException on failure.
306 	 */
307 	public RecentInfo lookupItem(string uri)
308 	{
309 		GError* err = null;
310 		
311 		auto p = gtk_recent_manager_lookup_item(gtkRecentManager, Str.toStringz(uri), &err);
312 		
313 		if (err !is null)
314 		{
315 			throw new GException( new ErrorG(err) );
316 		}
317 		
318 		if(p is null)
319 		{
320 			return null;
321 		}
322 		
323 		return ObjectG.getDObject!(RecentInfo)(cast(GtkRecentInfo*) p, true);
324 	}
325 
326 	/**
327 	 * Changes the location of a recently used resource from @uri to @new_uri.
328 	 *
329 	 * Please note that this function will not affect the resource pointed
330 	 * by the URIs, but only the URI used in the recently used resources list.
331 	 *
332 	 * Params:
333 	 *     uri = the URI of a recently used resource
334 	 *     newUri = the new URI of the recently used resource, or
335 	 *         %NULL to remove the item pointed by @uri in the list
336 	 *
337 	 * Returns: %TRUE on success
338 	 *
339 	 * Since: 2.10
340 	 *
341 	 * Throws: GException on failure.
342 	 */
343 	public bool moveItem(string uri, string newUri)
344 	{
345 		GError* err = null;
346 		
347 		auto p = gtk_recent_manager_move_item(gtkRecentManager, Str.toStringz(uri), Str.toStringz(newUri), &err) != 0;
348 		
349 		if (err !is null)
350 		{
351 			throw new GException( new ErrorG(err) );
352 		}
353 		
354 		return p;
355 	}
356 
357 	/**
358 	 * Purges every item from the recently used resources list.
359 	 *
360 	 * Returns: the number of items that have been removed from the
361 	 *     recently used resources list
362 	 *
363 	 * Since: 2.10
364 	 *
365 	 * Throws: GException on failure.
366 	 */
367 	public int purgeItems()
368 	{
369 		GError* err = null;
370 		
371 		auto p = gtk_recent_manager_purge_items(gtkRecentManager, &err);
372 		
373 		if (err !is null)
374 		{
375 			throw new GException( new ErrorG(err) );
376 		}
377 		
378 		return p;
379 	}
380 
381 	/**
382 	 * Removes a resource pointed by @uri from the recently used resources
383 	 * list handled by a recent manager.
384 	 *
385 	 * Params:
386 	 *     uri = the URI of the item you wish to remove
387 	 *
388 	 * Returns: %TRUE if the item pointed by @uri has been successfully
389 	 *     removed by the recently used resources list, and %FALSE otherwise
390 	 *
391 	 * Since: 2.10
392 	 *
393 	 * Throws: GException on failure.
394 	 */
395 	public bool removeItem(string uri)
396 	{
397 		GError* err = null;
398 		
399 		auto p = gtk_recent_manager_remove_item(gtkRecentManager, Str.toStringz(uri), &err) != 0;
400 		
401 		if (err !is null)
402 		{
403 			throw new GException( new ErrorG(err) );
404 		}
405 		
406 		return p;
407 	}
408 
409 	protected class OnChangedDelegateWrapper
410 	{
411 		static OnChangedDelegateWrapper[] listeners;
412 		void delegate(RecentManager) dlg;
413 		gulong handlerId;
414 		
415 		this(void delegate(RecentManager) dlg)
416 		{
417 			this.dlg = dlg;
418 			this.listeners ~= this;
419 		}
420 		
421 		void remove(OnChangedDelegateWrapper source)
422 		{
423 			foreach(index, wrapper; listeners)
424 			{
425 				if (wrapper.handlerId == source.handlerId)
426 				{
427 					listeners[index] = null;
428 					listeners = std.algorithm.remove(listeners, index);
429 					break;
430 				}
431 			}
432 		}
433 	}
434 
435 	/**
436 	 * Emitted when the current recently used resources manager changes
437 	 * its contents, either by calling gtk_recent_manager_add_item() or
438 	 * by another application.
439 	 *
440 	 * Since: 2.10
441 	 */
442 	gulong addOnChanged(void delegate(RecentManager) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
443 	{
444 		auto wrapper = new OnChangedDelegateWrapper(dlg);
445 		wrapper.handlerId = Signals.connectData(
446 			this,
447 			"changed",
448 			cast(GCallback)&callBackChanged,
449 			cast(void*)wrapper,
450 			cast(GClosureNotify)&callBackChangedDestroy,
451 			connectFlags);
452 		return wrapper.handlerId;
453 	}
454 	
455 	extern(C) static void callBackChanged(GtkRecentManager* recentmanagerStruct, OnChangedDelegateWrapper wrapper)
456 	{
457 		wrapper.dlg(wrapper.outer);
458 	}
459 	
460 	extern(C) static void callBackChangedDestroy(OnChangedDelegateWrapper wrapper, GClosure* closure)
461 	{
462 		wrapper.remove(wrapper);
463 	}
464 }