1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
<?xml version="1.0"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
]>
<chapter id="gtk-migrating-GtkRecentChooser">
<chapterinfo>
<author>
<firstname>Emmanuele</firstname>
<lastname>Bassi</lastname>
<affiliation>
<address>
<email>ebassi@gmail.com</email>
</address>
</affiliation>
</author>
</chapterinfo>
<title>Migrating from EggRecent to GtkRecentChooser</title>
<para>
Since version 2.10, GTK+ provides a way of handling the recently used
documents. It is similar to the code that has lived inside the libegg
library and has been incorporated by many applications. The GTK+ version
aims to completely replace that code, and offers many distinctive features
that improve the registration and visualization of the recently used
documents, such as:
</para>
<para>
<itemizedlist>
<listitem><para>
Better performances while reading and writing the list of recently used
files
</para></listitem>
<listitem><para>
More meta-data available for each recent document, like the
applications that have registered a document inside the list, the last
time and the number of times the same application did register a
document inside the list, an optional user readable name and
description of the document
</para></listitem>
<listitem><para>
Improved the ability to sort and filter the documents, also using
custom sorting and filtering functions
</para></listitem>
<listitem><para>
New widgets for displaying the list, and better integration with
current #GtkFileChooser and #GtkUIManager widgets
</para></listitem>
</itemizedlist>
</para>
<section id="gtkrecent-manager">
<title>Managing the Recently Used Documents</title>
<para>
#GtkRecentManager is used to manage the Recently Used Documents. To
create a new #GtkRecentManager, you simply call gtk_recent_manager_new().
Like the <structname>EggRecentModel</structname> inside EggRecent, the
#GtkRecentManager loads the list of the recent documents and notifies
you of changes inside the list.
</para>
<para>
Usually, instead of creating a new #GtkRecentManager each time you
need it, you'll want to use the gtk_recent_manager_get_default()
function.
</para>
<para>
To add a document to the list, you can use gtk_recent_manager_add_item(),
like:
<informalexample><programlisting>
GtkRecentManager *manager;
manager = gtk_recent_manager_new (<!-- -->);
if (!gtk_recent_manager_add_item (manager, document_uri))
{
/* warn about the error */
}
g_object_unref (manager);
</programlisting></informalexample>
The gtk_recent_manager_add_item() function will try and guess some of the
meta-data associated to a URI. If you know some of meta-data about the
document yourself, set the desired fields of a #GtkRecentData structure
and pass it to the gtk_recent_manager_add_full() function instead:
<informalexample><programlisting>
GtkRecentManager *manager;
GtkRecentData *recent_data;
manager = gtk_recent_manager_new (<!-- -->);
recent_data = g_new0 (GtkRecentData, 1);
/* the user visible name of the document (maybe its title); should
* be preferred when displaying the item into the list
*/
recent_data->display_name = document_name;
/* the MIME type is mandatory */
recent_data->mime_type = document_mime_type;
/* the name of the application that is registering the document
* (also mandatory); usually, the same name you used with
* the g_set_application_name (<!-- -->) function.
*/
recent_data-&app_name = APP_NAME;
/* the command to open a file; the %u string will be automagically
* expanded to the document's URI when getting the application's
* command line from the GtkRecentInfo object with
* gtk_recent_info_get_application_info (<!-- -->)
*/
recent_data-&app_exec = g_strjoin (" ", g_get_prgname (<!-- -->), "--open-file", "%u", NULL);
if (!gtk_recent_manager_add_full (manager, document_uri, recent_data))
{
/* warn about the error */
}
g_free (recent_data->app_exec);
g_free (recent_data);
g_object_unref (manager);
</programlisting></informalexample>
</para>
<para>
Getting the list of items is also similar to
<structname>EggRecentModel</structname>; the GtkRecentInfo data is
allocated at look up time in order not to waste memory keeping it
around, so you must remember to free the data inside the list and then
the list itself when you are done using it:
<informalexample><programlisting>
GtkRecentManager *manager;
GList *recent_items, *l;
manager = gtk_recent_manager_get_default(<!-- -->);
recent_items = gtk_recent_manager_get_items (manager);
for (l = recent_items; l != NULL; l = l->next)
{
GtkRecentInfo *recent_info = l->data;
do_something_with_the_item (recent_info);
}
/* free everything and the list */
g_list_foreach (recent_items, (GFunc) gtk_recent_info_unref, NULL);
g_list_free (recent_items);
</programlisting></informalexample>
You can also look up a single item:
<informalexample><programlisting>
GtkRecentInfo *recent_info;
GError *error = NULL;
recent_info = gtk_recent_manager_lookup_item (manager, document_uri, &error);
if (error)
{
display_error (error);
g_error_free (error);
}
else
{
do_something_with_the_item (recent_info);
gtk_recent_info_unref (recent_info);
}
</programlisting></informalexample>
The #GtkRecentInfo is a reference counted boxed type, and it holds all
the meta-data of a recently used document, like its display name, its
description, the list of each application that has registered the
document or the list of groups to which the document belong.
</para>
</section> <!-- gtkrecent-manager -->
<section id="gtkrecent-chooser">
<title>Displaying the Recently Used Documents</title>
<para>
Displaying the Recently Used Documents list is handled by any widget
implementing the #GtkRecentChooser interface. These widgets also handle
the sorting and filtering of the list; they will create their own
#GtkRecentManager objects by default:
<informalexample><programlisting>
GtkWidget *chooser;
gint response;
/* create a new dialog with the recently used documents list shown
* using a GtkTreeView widget
*/
chooser = gtk_recent_chooser_dialog_new ("Recent Documents",
parent_window,
GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_OK,
NULL);
/* set the sorting order to "most recently used first" */
gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (chooser), GTK_RECENT_SORT_MRU);
response = gtk_dialog_run (GTK_DIALOG (chooser));
if (response == GTK_RESPONSE_OK)
{
GtkRecentInfo *info;
info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (chooser));
do_something_with_the_item (info);
gtk_recent_info_unref (info);
}
gtk_widget_destroy (chooser);
</programlisting></informalexample>
</para>
</section> <!-- gtkrecent-chooser -->
<section id="gtkrecent-advanced">
<title>Advanced usage</title>
<para>
The #GtkRecentChooser widgets might display items sorted and filtered,
either with already supplied or custom sorting and filtering functions.
The biggest difference from the <structname>EggRecentView</structname>
widgets in EggRecent is that the #GtkRecentChooser widgets will use
their own copy of the list and will apply the sorting and filtering
functions only on the copy; this allows the creation of many viewers
with a single controller, like using many #GtkTreeView with a single
#GtkTreeModel instance.
</para>
<para>
Available sorting methods are:
<informalexample><programlisting>
/* no sorting */
gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (chooser), GTK_RECENT_SORT_NONE);
/* most recently used first */
gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (chooser), GTK_RECENT_SORT_MRU);
/* most recently used last */
gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (chooser), GTK_RECENT_SORT_LRU);
</programlisting></informalexample>
You can create your own sorting function, and the use the
GTK_RECENT_SORT_CUSTOM method:
<informalexample><programlisting>
/* custom sorting function, based on the registration count
* (most used first)
*/
static void
sort_by_usage_count (GtkRecentInfo *a,
GtkRecentInfo *b,
gpointer data)
{
gint count_a, count_b;
count_a = count_b = 0;
if (gtk_recent_info_has_application (a, APP_NAME))
gtk_recent_info_get_application_info (a, APP_NAME, NULL, &count_a, NULL);
if (gtk_recent_info_has_application (b, APP_NAME))
gtk_recent_info_get_application_info (b, APP_NAME, NULL, &count_b, NULL);
return count_a < count_b;
}
...
/* set custom sorting and set the custom sorting function */
gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (chooser),
GTK_RECENT_SORT_CUSTOM);
gtk_recent_chooser_set_sort_func (GTK_RECENT_CHOOSER,
sort_by_usage_count,
NULL, /* sort function data */
NULL /* destroy notify for the data */);
</programlisting></informalexample>
</para>
<para>
Filtering is done using the #GtkRecentFilter object, similar to the
#GtkFileFilter object used by the #GtkFileChooser widgets. The
#GtkRecentFilter object has a set of pre-defined options based on the
meta-data exposed by the #GtkRecentInfo object. It also allows custom
filtering function:
<informalexample><programlisting>
GtkRecentFilter *filter;
filter = gtk_recent_filter_new (<!-- -->);
/* set the user visible name of the filter */
gtk_recent_filter_set_name (filter, "Since Last Month");
/* set the maximum age of a recently used document */
gtk_recent_filter_set_age (filter, 31);
/* the chooser takes the ownership of the object */
gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (chooser), filter);
/* set the currently used filter */
gtk_recent_chooser_set_filter (GTK_RECENT_CHOOSER (chooser), filter);
filter = gtk_recent_filter_new (<!-- -->);
gtk_recent_filter_set_name (filter, "Every text file");
gtk_recent_filter_set_mime_type (filter, "text/plain");
gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (chooser), filter);
</programlisting></informalexample>
The #GtkRecentChooserWidget and #GtkRecentChooserDialog widgets allow
multiple filters and the selection of an appropriate one; the
#GtkRecentChooserMenu widget allows just a single filter object.
</para>
</section> <!-- gtkrecent-advanced -->
</chapter>
<!--
Local variables:
mode: sgml
sgml-parent-document: ("gtk-docs.sgml" "book" "part" "chapter")
End:
-->
|