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 public  import gtkc.gdktypes;
36 private import gtkc.gtk;
37 public  import gtkc.gtktypes;
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()
111 	{
112 		return gtkRecentManager;
113 	}
114 
115 	/** the main Gtk struct as a void* */
116 	protected override void* getStruct()
117 	{
118 		return cast(void*)gtkRecentManager;
119 	}
120 
121 	protected override void setStruct(GObject* obj)
122 	{
123 		gtkRecentManager = cast(GtkRecentManager*)obj;
124 		super.setStruct(obj);
125 	}
126 
127 	/**
128 	 * Sets our main struct and passes it to the parent class.
129 	 */
130 	public this (GtkRecentManager* gtkRecentManager, bool ownedRef = false)
131 	{
132 		this.gtkRecentManager = gtkRecentManager;
133 		super(cast(GObject*)gtkRecentManager, ownedRef);
134 	}
135 
136 
137 	/** */
138 	public static GType getType()
139 	{
140 		return gtk_recent_manager_get_type();
141 	}
142 
143 	/**
144 	 * Creates a new recent manager object. Recent manager objects are used to
145 	 * handle the list of recently used resources. A #GtkRecentManager object
146 	 * monitors the recently used resources list, and emits the “changed” signal
147 	 * each time something inside the list changes.
148 	 *
149 	 * #GtkRecentManager objects are expensive: be sure to create them only when
150 	 * needed. You should use gtk_recent_manager_get_default() instead.
151 	 *
152 	 * Return: A newly created #GtkRecentManager object
153 	 *
154 	 * Since: 2.10
155 	 *
156 	 * Throws: ConstructionException GTK+ fails to create the object.
157 	 */
158 	public this()
159 	{
160 		auto p = gtk_recent_manager_new();
161 		
162 		if(p is null)
163 		{
164 			throw new ConstructionException("null returned by new");
165 		}
166 		
167 		this(cast(GtkRecentManager*) p, true);
168 	}
169 
170 	/**
171 	 * Gets a unique instance of #GtkRecentManager, that you can share
172 	 * in your application without caring about memory management.
173 	 *
174 	 * Return: A unique #GtkRecentManager. Do not ref or
175 	 *     unref it.
176 	 *
177 	 * Since: 2.10
178 	 */
179 	public static RecentManager getDefault()
180 	{
181 		auto p = gtk_recent_manager_get_default();
182 		
183 		if(p is null)
184 		{
185 			return null;
186 		}
187 		
188 		return ObjectG.getDObject!(RecentManager)(cast(GtkRecentManager*) p);
189 	}
190 
191 	/**
192 	 * Adds a new resource, pointed by @uri, into the recently used
193 	 * resources list, using the metadata specified inside the
194 	 * #GtkRecentData-struct passed in @recent_data.
195 	 *
196 	 * The passed URI will be used to identify this resource inside the
197 	 * list.
198 	 *
199 	 * In order to register the new recently used resource, metadata about
200 	 * the resource must be passed as well as the URI; the metadata is
201 	 * stored in a #GtkRecentData-struct, which must contain the MIME
202 	 * type of the resource pointed by the URI; the name of the application
203 	 * that is registering the item, and a command line to be used when
204 	 * launching the item.
205 	 *
206 	 * Optionally, a #GtkRecentData-struct might contain a UTF-8 string
207 	 * to be used when viewing the item instead of the last component of
208 	 * the URI; a short description of the item; whether the item should
209 	 * be considered private - that is, should be displayed only by the
210 	 * applications that have registered it.
211 	 *
212 	 * Params:
213 	 *     uri = a valid URI
214 	 *     recentData = metadata of the resource
215 	 *
216 	 * Return: %TRUE if the new item was successfully added to the
217 	 *     recently used resources list, %FALSE otherwise
218 	 *
219 	 * Since: 2.10
220 	 */
221 	public bool addFull(string uri, GtkRecentData* recentData)
222 	{
223 		return gtk_recent_manager_add_full(gtkRecentManager, Str.toStringz(uri), recentData) != 0;
224 	}
225 
226 	/**
227 	 * Adds a new resource, pointed by @uri, into the recently used
228 	 * resources list.
229 	 *
230 	 * This function automatically retrieves some of the needed
231 	 * metadata and setting other metadata to common default values;
232 	 * it then feeds the data to gtk_recent_manager_add_full().
233 	 *
234 	 * See gtk_recent_manager_add_full() if you want to explicitly
235 	 * define the metadata for the resource pointed by @uri.
236 	 *
237 	 * Params:
238 	 *     uri = a valid URI
239 	 *
240 	 * Return: %TRUE if the new item was successfully added
241 	 *     to the recently used resources list
242 	 *
243 	 * Since: 2.10
244 	 */
245 	public bool addItem(string uri)
246 	{
247 		return gtk_recent_manager_add_item(gtkRecentManager, Str.toStringz(uri)) != 0;
248 	}
249 
250 	/**
251 	 * Gets the list of recently used resources.
252 	 *
253 	 * Return: a list of
254 	 *     newly allocated #GtkRecentInfo objects. Use
255 	 *     gtk_recent_info_unref() on each item inside the list, and then
256 	 *     free the list itself using g_list_free().
257 	 *
258 	 * Since: 2.10
259 	 */
260 	public ListG getItems()
261 	{
262 		auto p = gtk_recent_manager_get_items(gtkRecentManager);
263 		
264 		if(p is null)
265 		{
266 			return null;
267 		}
268 		
269 		return new ListG(cast(GList*) p);
270 	}
271 
272 	/**
273 	 * Checks whether there is a recently used resource registered
274 	 * with @uri inside the recent manager.
275 	 *
276 	 * Params:
277 	 *     uri = a URI
278 	 *
279 	 * Return: %TRUE if the resource was found, %FALSE otherwise
280 	 *
281 	 * Since: 2.10
282 	 */
283 	public bool hasItem(string uri)
284 	{
285 		return gtk_recent_manager_has_item(gtkRecentManager, Str.toStringz(uri)) != 0;
286 	}
287 
288 	/**
289 	 * Searches for a URI inside the recently used resources list, and
290 	 * returns a #GtkRecentInfo-struct containing informations about the resource
291 	 * like its MIME type, or its display name.
292 	 *
293 	 * Params:
294 	 *     uri = a URI
295 	 *
296 	 * Return: a #GtkRecentInfo-struct containing information
297 	 *     about the resource pointed by @uri, or %NULL if the URI was
298 	 *     not registered in the recently used resources list. Free with
299 	 *     gtk_recent_info_unref().
300 	 *
301 	 * Since: 2.10
302 	 *
303 	 * Throws: GException on failure.
304 	 */
305 	public RecentInfo lookupItem(string uri)
306 	{
307 		GError* err = null;
308 		
309 		auto p = gtk_recent_manager_lookup_item(gtkRecentManager, Str.toStringz(uri), &err);
310 		
311 		if (err !is null)
312 		{
313 			throw new GException( new ErrorG(err) );
314 		}
315 		
316 		if(p is null)
317 		{
318 			return null;
319 		}
320 		
321 		return ObjectG.getDObject!(RecentInfo)(cast(GtkRecentInfo*) p);
322 	}
323 
324 	/**
325 	 * Changes the location of a recently used resource from @uri to @new_uri.
326 	 *
327 	 * Please note that this function will not affect the resource pointed
328 	 * by the URIs, but only the URI used in the recently used resources list.
329 	 *
330 	 * Params:
331 	 *     uri = the URI of a recently used resource
332 	 *     newUri = the new URI of the recently used resource, or
333 	 *         %NULL to remove the item pointed by @uri in the list
334 	 *
335 	 * Return: %TRUE on success
336 	 *
337 	 * Since: 2.10
338 	 *
339 	 * Throws: GException on failure.
340 	 */
341 	public bool moveItem(string uri, string newUri)
342 	{
343 		GError* err = null;
344 		
345 		auto p = gtk_recent_manager_move_item(gtkRecentManager, Str.toStringz(uri), Str.toStringz(newUri), &err) != 0;
346 		
347 		if (err !is null)
348 		{
349 			throw new GException( new ErrorG(err) );
350 		}
351 		
352 		return p;
353 	}
354 
355 	/**
356 	 * Purges every item from the recently used resources list.
357 	 *
358 	 * Return: the number of items that have been removed from the
359 	 *     recently used resources list
360 	 *
361 	 * Since: 2.10
362 	 *
363 	 * Throws: GException on failure.
364 	 */
365 	public int purgeItems()
366 	{
367 		GError* err = null;
368 		
369 		auto p = gtk_recent_manager_purge_items(gtkRecentManager, &err);
370 		
371 		if (err !is null)
372 		{
373 			throw new GException( new ErrorG(err) );
374 		}
375 		
376 		return p;
377 	}
378 
379 	/**
380 	 * Removes a resource pointed by @uri from the recently used resources
381 	 * list handled by a recent manager.
382 	 *
383 	 * Params:
384 	 *     uri = the URI of the item you wish to remove
385 	 *
386 	 * Return: %TRUE if the item pointed by @uri has been successfully
387 	 *     removed by the recently used resources list, and %FALSE otherwise
388 	 *
389 	 * Since: 2.10
390 	 *
391 	 * Throws: GException on failure.
392 	 */
393 	public bool removeItem(string uri)
394 	{
395 		GError* err = null;
396 		
397 		auto p = gtk_recent_manager_remove_item(gtkRecentManager, Str.toStringz(uri), &err) != 0;
398 		
399 		if (err !is null)
400 		{
401 			throw new GException( new ErrorG(err) );
402 		}
403 		
404 		return p;
405 	}
406 
407 	int[string] connectedSignals;
408 
409 	void delegate(RecentManager)[] onChangedListeners;
410 	/**
411 	 * Emitted when the current recently used resources manager changes
412 	 * its contents, either by calling gtk_recent_manager_add_item() or
413 	 * by another application.
414 	 *
415 	 * Since: 2.10
416 	 */
417 	void addOnChanged(void delegate(RecentManager) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
418 	{
419 		if ( "changed" !in connectedSignals )
420 		{
421 			Signals.connectData(
422 				this,
423 				"changed",
424 				cast(GCallback)&callBackChanged,
425 				cast(void*)this,
426 				null,
427 				connectFlags);
428 			connectedSignals["changed"] = 1;
429 		}
430 		onChangedListeners ~= dlg;
431 	}
432 	extern(C) static void callBackChanged(GtkRecentManager* recentmanagerStruct, RecentManager _recentmanager)
433 	{
434 		foreach ( void delegate(RecentManager) dlg; _recentmanager.onChangedListeners )
435 		{
436 			dlg(_recentmanager);
437 		}
438 	}
439 }