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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Migrating from EggRecent to GtkRecentChooser: GTK+ 2 Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="GTK+ 2 Reference Manual">
<link rel="up" href="migrating.html" title="Part IV. Migrating from Previous Versions of GTK+">
<link rel="prev" href="setting-the-page-flow.html" title="Setting the page flow">
<link rel="next" href="gtkrecent-chooser.html" title="Displaying the Recently Used Documents">
<meta name="generator" content="GTK-Doc V1.33.0 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="migrating.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="setting-the-page-flow.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="gtkrecent-chooser.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="gtk-migrating-GtkRecentChooser"></a>Migrating from EggRecent to GtkRecentChooser</h2></div>
<div><div class="author">
<h3 class="author"><span class="firstname">Emmanuele</span></h3>
<div class="affiliation"><div class="address"><p><br>
<code class="email"><<a class="email" href="mailto:ebassigmail.com">ebassi<em class="parameter"><code>gmail.com</code></em></a>></code><br>
</p></div></div>
</div></div>
</div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="gtk-migrating-GtkRecentChooser.html#gtkrecent-manager">Managing the Recently Used Documents</a></span></dt>
<dt><span class="section"><a href="gtkrecent-chooser.html">Displaying the Recently Used Documents</a></span></dt>
<dt><span class="section"><a href="gtkrecent-advanced.html">Advanced usage</a></span></dt>
</dl></div>
<p>
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:
</p>
<p>
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
Better performances while reading and writing the list of recently used
files
</p></li>
<li class="listitem"><p>
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
</p></li>
<li class="listitem"><p>
Improved the ability to sort and filter the documents, also using
custom sorting and filtering functions
</p></li>
<li class="listitem"><p>
New widgets for displaying the list, and better integration with
current <a class="link" href="GtkFileChooser.html" title="GtkFileChooser"><span class="type">GtkFileChooser</span></a> and <a class="link" href="GtkUIManager.html" title="GtkUIManager"><span class="type">GtkUIManager</span></a> widgets
</p></li>
</ul></div>
<p>
</p>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="gtkrecent-manager"></a>Managing the Recently Used Documents</h2></div></div></div>
<p>
<a class="link" href="GtkRecentManager.html" title="GtkRecentManager"><span class="type">GtkRecentManager</span></a> is used to manage the Recently Used Documents. To
create a new <a class="link" href="GtkRecentManager.html" title="GtkRecentManager"><span class="type">GtkRecentManager</span></a>, you simply call <a class="link" href="GtkRecentManager.html#gtk-recent-manager-new" title="gtk_recent_manager_new ()"><code class="function">gtk_recent_manager_new()</code></a>.
Like the <span class="structname">EggRecentModel</span> inside EggRecent, the
<a class="link" href="GtkRecentManager.html" title="GtkRecentManager"><span class="type">GtkRecentManager</span></a> loads the list of the recent documents and notifies
you of changes inside the list.
</p>
<p>
Usually, instead of creating a new <a class="link" href="GtkRecentManager.html" title="GtkRecentManager"><span class="type">GtkRecentManager</span></a> each time you
need it, you'll want to use the <a class="link" href="GtkRecentManager.html#gtk-recent-manager-get-default" title="gtk_recent_manager_get_default ()"><code class="function">gtk_recent_manager_get_default()</code></a>
function.
</p>
<p>
To add a document to the list, you can use <a class="link" href="GtkRecentManager.html#gtk-recent-manager-add-item" title="gtk_recent_manager_add_item ()"><code class="function">gtk_recent_manager_add_item()</code></a>,
like:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">GtkRecentManager</span> <span class="o">*</span><span class="n">manager</span><span class="p">;</span>
<span class="n">manager</span> <span class="o">=</span> <span class="n">gtk_recent_manager_new</span> <span class="p">();</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">gtk_recent_manager_add_item</span> <span class="p">(</span><span class="n">manager</span><span class="p">,</span> <span class="n">document_uri</span><span class="p">))</span>
<span class="p">{</span>
<span class="cm">/* warn about the error */</span>
<span class="p">}</span>
<span class="n">g_object_unref</span> <span class="p">(</span><span class="n">manager</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
The <a class="link" href="GtkRecentManager.html#gtk-recent-manager-add-item" title="gtk_recent_manager_add_item ()"><code class="function">gtk_recent_manager_add_item()</code></a> 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 <a class="link" href="GtkRecentManager.html#GtkRecentData" title="struct GtkRecentData"><span class="type">GtkRecentData</span></a> structure
and pass it to the <a class="link" href="GtkRecentManager.html#gtk-recent-manager-add-full" title="gtk_recent_manager_add_full ()"><code class="function">gtk_recent_manager_add_full()</code></a> function instead:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>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</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">GtkRecentManager</span> <span class="o">*</span><span class="n">manager</span><span class="p">;</span>
<span class="n">GtkRecentData</span> <span class="o">*</span><span class="n">recent_data</span><span class="p">;</span>
<span class="n">manager</span> <span class="o">=</span> <span class="n">gtk_recent_manager_new</span> <span class="p">();</span>
<span class="n">recent_data</span> <span class="o">=</span> <span class="n">g_new0</span> <span class="p">(</span><span class="n">GtkRecentData</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="cm">/* the user visible name of the document (maybe its title); should</span>
<span class="cm"> * be preferred when displaying the item into the list</span>
<span class="cm"> */</span>
<span class="n">recent_data</span><span class="o">-></span><span class="n">display_name</span> <span class="o">=</span> <span class="n">document_name</span><span class="p">;</span>
<span class="cm">/* the MIME type is mandatory */</span>
<span class="n">recent_data</span><span class="o">-></span><span class="n">mime_type</span> <span class="o">=</span> <span class="n">document_mime_type</span><span class="p">;</span>
<span class="cm">/* the name of the application that is registering the document</span>
<span class="cm"> * (also mandatory); usually, the same name you used with</span>
<span class="cm"> * the g_set_application_name () function.</span>
<span class="cm"> */</span>
<span class="n">recent_data</span><span class="o">-&</span><span class="n">app_name</span> <span class="o">=</span> <span class="n">APP_NAME</span><span class="p">;</span>
<span class="cm">/* the command to open a file; the %u string will be automagically</span>
<span class="cm"> * expanded to the document's URI when getting the application's</span>
<span class="cm"> * command line from the GtkRecentInfo object with</span>
<span class="cm"> * gtk_recent_info_get_application_info ()</span>
<span class="cm"> */</span>
<span class="n">recent_data</span><span class="o">-&</span><span class="n">app_exec</span> <span class="o">=</span> <span class="n">g_strjoin</span> <span class="p">(</span><span class="s">" "</span><span class="p">,</span> <span class="n">g_get_prgname</span> <span class="p">(),</span> <span class="s">"--open-file"</span><span class="p">,</span> <span class="s">"%u"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">gtk_recent_manager_add_full</span> <span class="p">(</span><span class="n">manager</span><span class="p">,</span> <span class="n">document_uri</span><span class="p">,</span> <span class="n">recent_data</span><span class="p">))</span>
<span class="p">{</span>
<span class="cm">/* warn about the error */</span>
<span class="p">}</span>
<span class="n">g_free</span> <span class="p">(</span><span class="n">recent_data</span><span class="o">-></span><span class="n">app_exec</span><span class="p">);</span>
<span class="n">g_free</span> <span class="p">(</span><span class="n">recent_data</span><span class="p">);</span>
<span class="n">g_object_unref</span> <span class="p">(</span><span class="n">manager</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
<p>
Getting the list of items is also similar to
<span class="structname">EggRecentModel</span>; 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:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">GtkRecentManager</span> <span class="o">*</span><span class="n">manager</span><span class="p">;</span>
<span class="n">GList</span> <span class="o">*</span><span class="n">recent_items</span><span class="p">,</span> <span class="o">*</span><span class="n">l</span><span class="p">;</span>
<span class="n">manager</span> <span class="o">=</span> <span class="n">gtk_recent_manager_get_default</span><span class="p">();</span>
<span class="n">recent_items</span> <span class="o">=</span> <span class="n">gtk_recent_manager_get_items</span> <span class="p">(</span><span class="n">manager</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="n">l</span> <span class="o">=</span> <span class="n">recent_items</span><span class="p">;</span> <span class="n">l</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">l</span> <span class="o">=</span> <span class="n">l</span><span class="o">-></span><span class="n">next</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">GtkRecentInfo</span> <span class="o">*</span><span class="n">recent_info</span> <span class="o">=</span> <span class="n">l</span><span class="o">-></span><span class="n">data</span><span class="p">;</span>
<span class="n">do_something_with_the_item</span> <span class="p">(</span><span class="n">recent_info</span><span class="p">);</span>
<span class="p">}</span>
<span class="cm">/* free everything and the list */</span>
<span class="n">g_list_foreach</span> <span class="p">(</span><span class="n">recent_items</span><span class="p">,</span> <span class="p">(</span><span class="n">GFunc</span><span class="p">)</span> <span class="n">gtk_recent_info_unref</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="n">g_list_free</span> <span class="p">(</span><span class="n">recent_items</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
You can also look up a single item:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">GtkRecentInfo</span> <span class="o">*</span><span class="n">recent_info</span><span class="p">;</span>
<span class="n">GError</span> <span class="o">*</span><span class="n">error</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="n">recent_info</span> <span class="o">=</span> <span class="n">gtk_recent_manager_lookup_item</span> <span class="p">(</span><span class="n">manager</span><span class="p">,</span> <span class="n">document_uri</span><span class="p">,</span> <span class="o">&</span><span class="n">error</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">error</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">display_error</span> <span class="p">(</span><span class="n">error</span><span class="p">);</span>
<span class="n">g_error_free</span> <span class="p">(</span><span class="n">error</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="p">{</span>
<span class="n">do_something_with_the_item</span> <span class="p">(</span><span class="n">recent_info</span><span class="p">);</span>
<span class="n">gtk_recent_info_unref</span> <span class="p">(</span><span class="n">recent_info</span><span class="p">);</span>
<span class="p">}</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
The <a class="link" href="GtkRecentManager.html#GtkRecentInfo" title="GtkRecentInfo"><span class="type">GtkRecentInfo</span></a> 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.
</p>
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.33.0</div>
</body>
</html>
|