]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchooserdefault.c
Avoid gratitious use of g_strdup_printf().
[~andy/gtk] / gtk / gtkrecentchooserdefault.c
1 /* GTK - The GIMP Toolkit
2  * gtkrecentchooserdefault.c
3  * Copyright (C) 2005-2006, Emmanuele Bassi
4  * 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24 #include <time.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #include <gdk/gdkscreen.h>
33
34 #include "gtkstock.h"
35 #include "gtkicontheme.h"
36 #include "gtkiconfactory.h"
37 #include "gtksettings.h"
38 #include "gtktreeview.h"
39 #include "gtkliststore.h"
40 #include "gtkbutton.h"
41 #include "gtkcelllayout.h"
42 #include "gtkcellrendererpixbuf.h"
43 #include "gtkcellrenderertext.h"
44 #include "gtkcheckmenuitem.h"
45 #include "gtkclipboard.h"
46 #include "gtkcombobox.h"
47 #include "gtkentry.h"
48 #include "gtkeventbox.h"
49 #include "gtkexpander.h"
50 #include "gtkframe.h"
51 #include "gtkhbox.h"
52 #include "gtkhpaned.h"
53 #include "gtkimage.h"
54 #include "gtkimagemenuitem.h"
55 #include "gtkintl.h"
56 #include "gtklabel.h"
57 #include "gtkmenuitem.h"
58 #include "gtkmessagedialog.h"
59 #include "gtkscrolledwindow.h"
60 #include "gtkseparatormenuitem.h"
61 #include "gtksizegroup.h"
62 #include "gtktable.h"
63 #include "gtktreemodelsort.h"
64 #include "gtktreemodelfilter.h"
65 #include "gtktreeselection.h"
66 #include "gtktreestore.h"
67 #include "gtktooltips.h"
68 #include "gtktypebuiltins.h"
69 #include "gtkvbox.h"
70
71 #include "gtkrecentmanager.h"
72 #include "gtkrecentfilter.h"
73 #include "gtkrecentchooser.h"
74 #include "gtkrecentchooserprivate.h"
75 #include "gtkrecentchooserutils.h"
76 #include "gtkrecentchooserdefault.h"
77
78 #include "gtkprivate.h"
79 #include "gtkalias.h"
80
81 \f
82
83 struct _GtkRecentChooserDefault
84 {
85   GtkVBox parent_instance;
86   
87   GtkRecentManager *manager;
88   gulong manager_changed_id;
89   guint local_manager : 1;
90   
91   gint icon_size;
92
93   /* RecentChooser properties */
94   gint limit;  
95   GtkRecentSortType sort_type;
96   guint show_private : 1;
97   guint show_not_found : 1;
98   guint select_multiple : 1;
99   guint show_tips : 1;
100   guint show_icons : 1;
101   guint local_only : 1;
102   
103   GSList *filters;
104   GtkRecentFilter *current_filter;
105   GtkWidget *filter_combo_hbox;
106   GtkWidget *filter_combo;
107   
108   GtkRecentSortFunc sort_func;
109   gpointer sort_data;
110   GDestroyNotify sort_data_destroy;
111
112   GtkTooltips *tooltips;
113
114   GtkIconTheme *icon_theme;
115   
116   GtkWidget *recent_view;
117   GtkListStore *recent_store;
118   GtkTreeViewColumn *icon_column;
119   GtkTreeViewColumn *meta_column;
120   GtkCellRenderer *meta_renderer;
121   GtkTreeSelection *selection;
122   
123   GtkWidget *recent_popup_menu;
124   GtkWidget *recent_popup_menu_copy_item;
125   GtkWidget *recent_popup_menu_remove_item;
126   GtkWidget *recent_popup_menu_clear_item;
127   GtkWidget *recent_popup_menu_show_private_item;
128  
129   guint load_id;
130   GList *recent_items;
131   gint n_recent_items;
132   gint loaded_items;
133   guint load_state;
134 };
135
136 typedef struct _GtkRecentChooserDefaultClass
137 {
138   GtkVBoxClass parent_class;
139 } GtkRecentChooserDefaultClass;
140
141 enum {
142   RECENT_URI_COLUMN,
143   RECENT_DISPLAY_NAME_COLUMN,
144   RECENT_INFO_COLUMN,
145     
146   N_RECENT_COLUMNS
147 };
148
149 enum {
150   LOAD_EMPTY,    /* initial state: the model is empty */
151   LOAD_PRELOAD,  /* the model is loading and not inserted in the tree yet */
152   LOAD_LOADING,  /* the model is fully loaded but not inserted */
153   LOAD_FINISHED  /* the model is fully loaded and inserted */
154 };
155
156 enum {
157   TEXT_URI_LIST
158 };
159
160 /* Target types for DnD from the file list */
161 static const GtkTargetEntry recent_list_source_targets[] = {
162   { "text/uri-list", 0, TEXT_URI_LIST }
163 };
164
165 /* Icon size for if we can't get it from the theme */
166 #define FALLBACK_ICON_SIZE  48
167 #define FALLBACK_ITEM_LIMIT 20
168
169 #define NUM_CHARS 40
170 #define NUM_LINES 9
171
172 \f
173
174 /* GObject */
175 static void     _gtk_recent_chooser_default_class_init  (GtkRecentChooserDefaultClass *klass);
176 static void     _gtk_recent_chooser_default_init        (GtkRecentChooserDefault      *impl);
177 static GObject *gtk_recent_chooser_default_constructor  (GType                         type,
178                                                          guint                         n_construct_prop,
179                                                          GObjectConstructParam        *construct_params);
180 static void     gtk_recent_chooser_default_finalize     (GObject                      *object);
181 static void     gtk_recent_chooser_default_dispose      (GObject                      *object);
182 static void     gtk_recent_chooser_default_set_property (GObject                      *object,
183                                                          guint                         prop_id,
184                                                          const GValue                 *value,
185                                                          GParamSpec                   *pspec);
186 static void     gtk_recent_chooser_default_get_property (GObject                      *object,
187                                                          guint                         prop_id,
188                                                          GValue                       *value,
189                                                          GParamSpec                   *pspec);
190
191 /* GtkRecentChooserIface */
192 static void              gtk_recent_chooser_iface_init                 (GtkRecentChooserIface  *iface);
193 static gboolean          gtk_recent_chooser_default_set_current_uri    (GtkRecentChooser       *chooser,
194                                                                         const gchar            *uri,
195                                                                         GError                **error);
196 static gchar *           gtk_recent_chooser_default_get_current_uri    (GtkRecentChooser       *chooser);
197 static gboolean          gtk_recent_chooser_default_select_uri         (GtkRecentChooser       *chooser,
198                                                                         const gchar            *uri,
199                                                                         GError                **error);
200 static void              gtk_recent_chooser_default_unselect_uri       (GtkRecentChooser       *chooser,
201                                                                         const gchar            *uri);
202 static void              gtk_recent_chooser_default_select_all         (GtkRecentChooser       *chooser);
203 static void              gtk_recent_chooser_default_unselect_all       (GtkRecentChooser       *chooser);
204 static GList *           gtk_recent_chooser_default_get_items          (GtkRecentChooser       *chooser);
205 static GtkRecentManager *gtk_recent_chooser_default_get_recent_manager (GtkRecentChooser       *chooser);
206 static void              gtk_recent_chooser_default_set_sort_func      (GtkRecentChooser       *chooser,
207                                                                         GtkRecentSortFunc       sort_func,
208                                                                         gpointer                sort_data,
209                                                                         GDestroyNotify          data_destroy);
210 static void              gtk_recent_chooser_default_add_filter         (GtkRecentChooser       *chooser,
211                                                                         GtkRecentFilter        *filter);
212 static void              gtk_recent_chooser_default_remove_filter      (GtkRecentChooser       *chooser,
213                                                                         GtkRecentFilter        *filter);
214 static GSList *          gtk_recent_chooser_default_list_filters       (GtkRecentChooser       *chooser);
215
216
217 static void gtk_recent_chooser_default_map      (GtkWidget *widget);
218 static void gtk_recent_chooser_default_show_all (GtkWidget *widget);
219
220 static void set_current_filter        (GtkRecentChooserDefault *impl,
221                                        GtkRecentFilter         *filter);
222
223 static GtkIconTheme *get_icon_theme_for_widget (GtkWidget   *widget);
224 static gint          get_icon_size_for_widget  (GtkWidget   *widget,
225                                                 GtkIconSize  icon_size);
226
227 static void reload_recent_items (GtkRecentChooserDefault *impl);
228 static void chooser_set_model   (GtkRecentChooserDefault *impl);
229
230 static void set_recent_manager (GtkRecentChooserDefault *impl,
231                                 GtkRecentManager        *manager);
232
233 static void chooser_set_sort_type (GtkRecentChooserDefault *impl,
234                                    GtkRecentSortType        sort_type);
235
236 static void recent_manager_changed_cb (GtkRecentManager  *manager,
237                                        gpointer           user_data);
238 static void recent_icon_data_func     (GtkTreeViewColumn *tree_column,
239                                        GtkCellRenderer   *cell,
240                                        GtkTreeModel      *model,
241                                        GtkTreeIter       *iter,
242                                        gpointer           user_data);
243 static void recent_meta_data_func     (GtkTreeViewColumn *tree_column,
244                                        GtkCellRenderer   *cell,
245                                        GtkTreeModel      *model,
246                                        GtkTreeIter       *iter,
247                                        gpointer           user_data);
248
249 static void selection_changed_cb      (GtkTreeSelection  *z,
250                                        gpointer           user_data);
251 static void row_activated_cb          (GtkTreeView       *tree_view,
252                                        GtkTreePath       *tree_path,
253                                        GtkTreeViewColumn *tree_column,
254                                        gpointer           user_data);
255 static void filter_combo_changed_cb   (GtkComboBox       *combo_box,
256                                        gpointer           user_data);
257
258 static void remove_all_activated_cb   (GtkMenuItem       *menu_item,
259                                        gpointer           user_data);
260 static void remove_item_activated_cb  (GtkMenuItem       *menu_item,
261                                        gpointer           user_data);
262 static void show_private_toggled_cb   (GtkCheckMenuItem  *menu_item,
263                                        gpointer           user_data);
264
265 static gboolean recent_view_popup_menu_cb   (GtkWidget      *widget,
266                                              gpointer        user_data);
267 static gboolean recent_view_button_press_cb (GtkWidget      *widget,
268                                              GdkEventButton *event,
269                                              gpointer        user_data);
270
271 static void     recent_view_drag_begin_cb         (GtkWidget        *widget,
272                                                    GdkDragContext   *context,
273                                                    gpointer          user_data);
274 static void     recent_view_drag_data_get_cb      (GtkWidget        *widget,
275                                                    GdkDragContext   *context,
276                                                    GtkSelectionData *selection_data,
277                                                    guint             info,
278                                                    guint32           time_,
279                                                    gpointer          data);
280
281 G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserDefault,
282                          _gtk_recent_chooser_default,
283                          GTK_TYPE_VBOX,
284                          G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
285                                                 gtk_recent_chooser_iface_init))
286
287
288 \f
289
290 static void
291 gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
292 {
293   iface->set_current_uri = gtk_recent_chooser_default_set_current_uri;
294   iface->get_current_uri = gtk_recent_chooser_default_get_current_uri;
295   iface->select_uri = gtk_recent_chooser_default_select_uri;
296   iface->unselect_uri = gtk_recent_chooser_default_unselect_uri;
297   iface->select_all = gtk_recent_chooser_default_select_all;
298   iface->unselect_all = gtk_recent_chooser_default_unselect_all;
299   iface->get_items = gtk_recent_chooser_default_get_items;
300   iface->get_recent_manager = gtk_recent_chooser_default_get_recent_manager;
301   iface->set_sort_func = gtk_recent_chooser_default_set_sort_func;
302   iface->add_filter = gtk_recent_chooser_default_add_filter;
303   iface->remove_filter = gtk_recent_chooser_default_remove_filter;
304   iface->list_filters = gtk_recent_chooser_default_list_filters;
305 }
306
307 static void
308 _gtk_recent_chooser_default_class_init (GtkRecentChooserDefaultClass *klass)
309 {
310   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
311   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
312
313   gobject_class->constructor = gtk_recent_chooser_default_constructor;
314   gobject_class->set_property = gtk_recent_chooser_default_set_property;
315   gobject_class->get_property = gtk_recent_chooser_default_get_property;
316   gobject_class->dispose = gtk_recent_chooser_default_dispose;
317   gobject_class->finalize = gtk_recent_chooser_default_finalize;
318   
319   widget_class->map = gtk_recent_chooser_default_map;
320   widget_class->show_all = gtk_recent_chooser_default_show_all;
321   
322   _gtk_recent_chooser_install_properties (gobject_class);
323 }
324
325 static void
326 _gtk_recent_chooser_default_init (GtkRecentChooserDefault *impl)
327 {
328   gtk_box_set_spacing (GTK_BOX (impl), 6);
329
330   /* by default, we use the global manager */
331   impl->local_manager = FALSE;
332   
333   impl->limit = FALLBACK_ITEM_LIMIT;
334   impl->sort_type = GTK_RECENT_SORT_NONE;
335
336   impl->show_icons = TRUE;
337   impl->show_private = FALSE;
338   impl->show_not_found = TRUE;
339   impl->show_tips = TRUE;
340   impl->select_multiple = FALSE;
341   impl->local_only = TRUE;
342   
343   impl->icon_size = FALLBACK_ICON_SIZE;
344   impl->icon_theme = NULL;
345   
346   impl->current_filter = NULL;
347
348   impl->tooltips = gtk_tooltips_new ();
349   g_object_ref_sink (impl->tooltips);
350   
351   impl->recent_items = NULL;
352   impl->n_recent_items = 0;
353   impl->loaded_items = 0;
354   
355   impl->load_state = LOAD_EMPTY;
356 }
357
358 static GObject *
359 gtk_recent_chooser_default_constructor (GType                  type,
360                                         guint                  n_params,
361                                         GObjectConstructParam *params)
362 {
363   GObjectClass *parent_class;
364   GtkRecentChooserDefault *impl;
365   GObject *object;
366   GtkWidget *scrollw;
367   GtkCellRenderer *renderer;
368
369   parent_class = G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class);
370   object = parent_class->constructor (type, n_params, params);
371   impl = GTK_RECENT_CHOOSER_DEFAULT (object);
372   
373   g_assert (impl->manager);
374   
375   gtk_widget_push_composite_child ();
376   
377   scrollw = gtk_scrolled_window_new (NULL, NULL);
378   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollw),
379                                        GTK_SHADOW_IN);
380   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollw),
381                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
382   gtk_box_pack_start (GTK_BOX (impl), scrollw, TRUE, TRUE, 0);
383   gtk_widget_show (scrollw);
384   
385   impl->recent_view = gtk_tree_view_new ();
386   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->recent_view), FALSE);
387   g_signal_connect (impl->recent_view, "row-activated",
388                     G_CALLBACK (row_activated_cb), impl);
389   g_signal_connect (impl->recent_view, "popup-menu",
390                     G_CALLBACK (recent_view_popup_menu_cb), impl);
391   g_signal_connect (impl->recent_view, "button-press-event",
392                     G_CALLBACK (recent_view_button_press_cb), impl);
393   g_signal_connect (impl->recent_view, "drag_begin",
394                     G_CALLBACK (recent_view_drag_begin_cb), impl);
395   g_signal_connect (impl->recent_view, "drag_data_get",
396                     G_CALLBACK (recent_view_drag_data_get_cb), impl);
397
398   g_object_set_data (G_OBJECT (impl->recent_view),
399                      "GtkRecentChooserDefault", impl);
400   
401   gtk_container_add (GTK_CONTAINER (scrollw), impl->recent_view);
402   gtk_widget_show (impl->recent_view);
403   
404   impl->icon_column = gtk_tree_view_column_new ();
405   gtk_tree_view_column_set_expand (impl->icon_column, FALSE);
406   gtk_tree_view_column_set_resizable (impl->icon_column, FALSE);
407   
408   renderer = gtk_cell_renderer_pixbuf_new ();
409   gtk_tree_view_column_pack_start (impl->icon_column, renderer, FALSE);
410   gtk_tree_view_column_set_cell_data_func (impl->icon_column,
411                                            renderer,
412                                            recent_icon_data_func,
413                                            impl,
414                                            NULL);
415   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->recent_view),
416                                impl->icon_column);
417   
418   impl->meta_column = gtk_tree_view_column_new ();
419   gtk_tree_view_column_set_expand (impl->meta_column, TRUE);
420   gtk_tree_view_column_set_resizable (impl->meta_column, FALSE);
421   
422   impl->meta_renderer = gtk_cell_renderer_text_new ();
423   g_object_set (G_OBJECT (impl->meta_renderer),
424                 "ellipsize", PANGO_ELLIPSIZE_END,
425                 NULL);
426   gtk_tree_view_column_pack_start (impl->meta_column, impl->meta_renderer, TRUE);
427   gtk_tree_view_column_set_cell_data_func (impl->meta_column,
428                                            impl->meta_renderer,
429                                            recent_meta_data_func,
430                                            impl,
431                                            NULL);
432   gtk_tree_view_append_column (GTK_TREE_VIEW (impl->recent_view),
433                                impl->meta_column);
434   
435   impl->selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->recent_view));
436   gtk_tree_selection_set_mode (impl->selection, GTK_SELECTION_SINGLE);
437   g_signal_connect (impl->selection, "changed", G_CALLBACK (selection_changed_cb), impl);
438
439   /* drag and drop */
440   gtk_drag_source_set (impl->recent_view,
441                        GDK_BUTTON1_MASK,
442                        recent_list_source_targets,
443                        G_N_ELEMENTS (recent_list_source_targets),
444                        GDK_ACTION_COPY);
445
446   impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
447   
448   impl->filter_combo = gtk_combo_box_new_text ();
449   gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
450   g_signal_connect (impl->filter_combo, "changed",
451                     G_CALLBACK (filter_combo_changed_cb), impl);
452   gtk_tooltips_set_tip (impl->tooltips,
453                         impl->filter_combo,
454                         _("Select which type of documents are shown"),
455                         NULL);
456   
457   gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox),
458                     impl->filter_combo,
459                     FALSE, FALSE, 0);
460   gtk_widget_show (impl->filter_combo);
461   
462   gtk_box_pack_end (GTK_BOX (impl), impl->filter_combo_hbox, FALSE, FALSE, 0);
463   
464   gtk_widget_pop_composite_child ();
465   
466   impl->recent_store = gtk_list_store_new (N_RECENT_COLUMNS,
467                                            G_TYPE_STRING,       /* uri */
468                                            G_TYPE_STRING,       /* display_name */
469                                            GTK_TYPE_RECENT_INFO /* info */);
470   
471   return object;
472 }
473
474 static void
475 gtk_recent_chooser_default_set_property (GObject      *object,
476                                          guint         prop_id,
477                                          const GValue *value,
478                                          GParamSpec   *pspec)
479 {
480   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
481   
482   switch (prop_id)
483     {
484     case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
485       set_recent_manager (impl, g_value_get_object (value));
486       break;
487     case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
488       impl->show_private = g_value_get_boolean (value);
489       if (impl->recent_popup_menu_show_private_item)
490         {
491           GtkCheckMenuItem *item = GTK_CHECK_MENU_ITEM (impl->recent_popup_menu_show_private_item);
492           g_signal_handlers_block_by_func (item, G_CALLBACK (show_private_toggled_cb), impl);
493           gtk_check_menu_item_set_active (item, impl->show_private);
494           g_signal_handlers_unblock_by_func (item, G_CALLBACK (show_private_toggled_cb), impl);
495         }
496       reload_recent_items (impl);
497       break;
498     case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
499       impl->show_not_found = g_value_get_boolean (value);
500       reload_recent_items (impl);
501       break;
502     case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
503       impl->show_tips = g_value_get_boolean (value);
504
505       if (impl->show_tips)
506         gtk_tooltips_enable (impl->tooltips);
507       else
508         gtk_tooltips_disable (impl->tooltips);
509       break;
510     case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
511       impl->show_icons = g_value_get_boolean (value);
512       gtk_tree_view_column_set_visible (impl->icon_column, impl->show_icons);
513       break;
514     case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
515       impl->select_multiple = g_value_get_boolean (value);
516       
517       if (impl->select_multiple)
518         gtk_tree_selection_set_mode (impl->selection, GTK_SELECTION_MULTIPLE);
519       else
520         gtk_tree_selection_set_mode (impl->selection, GTK_SELECTION_SINGLE);
521       break;
522     case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
523       impl->local_only = g_value_get_boolean (value);
524       reload_recent_items (impl);
525       break;
526     case GTK_RECENT_CHOOSER_PROP_LIMIT:
527       impl->limit = g_value_get_int (value);
528       reload_recent_items (impl);
529       break;
530     case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
531       chooser_set_sort_type (impl, g_value_get_enum (value));
532       break;
533     case GTK_RECENT_CHOOSER_PROP_FILTER:
534       set_current_filter (impl, g_value_get_object (value));
535       break;
536     default:
537       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
538       break;
539     }
540 }
541
542 static void
543 gtk_recent_chooser_default_get_property (GObject    *object,
544                                          guint       prop_id,
545                                          GValue     *value,
546                                          GParamSpec *pspec)
547 {
548   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
549   
550   switch (prop_id)
551     {
552     case GTK_RECENT_CHOOSER_PROP_LIMIT:
553       g_value_set_int (value, impl->limit);
554       break;
555     case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
556       g_value_set_enum (value, impl->sort_type);
557       break;
558     case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
559       g_value_set_boolean (value, impl->show_private);
560       break;
561     case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
562       g_value_set_boolean (value, impl->show_icons);
563       break;
564     case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
565       g_value_set_boolean (value, impl->show_not_found);
566       break;
567     case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
568       g_value_set_boolean (value, impl->show_tips);
569       break;
570     case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
571       g_value_set_boolean (value, impl->local_only);
572       break;
573     case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
574       g_value_set_boolean (value, impl->select_multiple);
575       break;
576     case GTK_RECENT_CHOOSER_PROP_FILTER:
577       g_value_set_object (value, impl->current_filter);
578       break;
579     default:
580       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
581       break;
582     }
583 }
584
585 static void
586 gtk_recent_chooser_default_dispose (GObject *object)
587 {
588   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
589
590   if (impl->load_id)
591     {
592       g_source_remove (impl->load_id);
593       impl->load_state = LOAD_EMPTY;
594       impl->load_id = 0;
595     }
596
597   if (impl->recent_items)
598     {
599       g_list_foreach (impl->recent_items, (GFunc) gtk_recent_info_unref, NULL);
600       g_list_free (impl->recent_items);
601       impl->recent_items = NULL;
602     }
603
604   if (impl->manager && impl->manager_changed_id)
605     {
606       g_signal_handler_disconnect (impl->manager, impl->manager_changed_id);
607       impl->manager_changed_id = 0;
608     }
609
610   if (impl->filters)
611     {
612       g_slist_foreach (impl->filters, (GFunc) g_object_unref, NULL);
613       g_slist_free (impl->filters);
614       impl->filters = NULL;
615     }
616   
617   if (impl->current_filter)
618     {
619       g_object_unref (impl->current_filter);
620       impl->current_filter = NULL;
621     }
622
623   if (impl->recent_store)
624     {
625       g_object_unref (impl->recent_store);
626       impl->recent_store = NULL;
627     }
628
629   if (impl->tooltips)
630     {
631       g_object_unref (impl->tooltips);
632       impl->tooltips = NULL;
633     }
634
635   G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->dispose (object);
636 }
637
638 static void
639 gtk_recent_chooser_default_finalize (GObject *object)
640 {
641   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
642
643   impl->manager = NULL; 
644   
645   if (impl->sort_data_destroy)
646     {
647       impl->sort_data_destroy (impl->sort_data);
648       impl->sort_data_destroy = NULL;
649     }
650   
651   impl->sort_data = NULL;
652   impl->sort_func = NULL;
653   
654   G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->finalize (object);
655 }
656
657 /* override GtkWidget::show_all since we have internal widgets we wish to keep
658  * hidden unless we decide otherwise, like the filter combo box.
659  */
660 static void
661 gtk_recent_chooser_default_show_all (GtkWidget *widget)
662 {
663   gtk_widget_show (widget);
664 }
665
666
667
668 /* Shows an error dialog set as transient for the specified window */
669 static void
670 error_message_with_parent (GtkWindow   *parent,
671                            const gchar *msg,
672                            const gchar *detail)
673 {
674   GtkWidget *dialog;
675
676   dialog = gtk_message_dialog_new (parent,
677                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
678                                    GTK_MESSAGE_ERROR,
679                                    GTK_BUTTONS_OK,
680                                    "%s",
681                                    msg);
682   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
683                                             "%s", detail);
684
685   if (parent->group)
686     gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
687
688   gtk_dialog_run (GTK_DIALOG (dialog));
689   gtk_widget_destroy (dialog);
690 }
691
692 /* Returns a toplevel GtkWindow, or NULL if none */
693 static GtkWindow *
694 get_toplevel (GtkWidget *widget)
695 {
696   GtkWidget *toplevel;
697
698   toplevel = gtk_widget_get_toplevel (widget);
699   if (!GTK_WIDGET_TOPLEVEL (toplevel))
700     return NULL;
701   else
702     return GTK_WINDOW (toplevel);
703 }
704
705 /* Shows an error dialog for the file chooser */
706 static void
707 error_message (GtkRecentChooserDefault *impl,
708                const gchar             *msg,
709                const gchar             *detail)
710 {
711   error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
712 }
713
714 static void
715 set_busy_cursor (GtkRecentChooserDefault *impl,
716                  gboolean                 show_busy_cursor)
717 {
718   GtkWindow *toplevel;
719   GdkDisplay *display;
720   GdkCursor *cursor;
721
722   toplevel = get_toplevel (GTK_WIDGET (impl));
723   if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
724     return;
725   
726   display = gtk_widget_get_display (GTK_WIDGET (toplevel));
727   
728   cursor = NULL;
729   if (show_busy_cursor)
730     cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
731
732   gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
733   gdk_display_flush (display);
734
735   if (cursor)
736     gdk_cursor_unref (cursor);
737 }
738
739 static void
740 chooser_set_model (GtkRecentChooserDefault *impl)
741 {
742   g_assert (impl->recent_store != NULL);
743   g_assert (impl->load_state == LOAD_LOADING);
744
745   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->recent_view),
746                            GTK_TREE_MODEL (impl->recent_store));
747   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->recent_view));
748   gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->recent_view), TRUE);
749   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->recent_view),
750                                    RECENT_DISPLAY_NAME_COLUMN);
751
752   impl->load_state = LOAD_FINISHED;
753 }
754
755 static gboolean
756 load_recent_items (gpointer user_data)
757 {
758   GtkRecentChooserDefault *impl;
759   GtkRecentInfo *info;
760   GtkTreeIter iter;
761   const gchar *uri, *name;
762   gboolean retval;
763   
764   impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
765   
766   g_assert ((impl->load_state == LOAD_EMPTY) ||
767             (impl->load_state == LOAD_PRELOAD));
768   
769   /* store the items for multiple runs */
770   if (!impl->recent_items)
771     {
772       impl->recent_items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (impl));
773       if (!impl->recent_items)
774         {
775           impl->load_state = LOAD_FINISHED;
776           
777           return FALSE;
778         }
779         
780       impl->n_recent_items = g_list_length (impl->recent_items);
781       impl->loaded_items = 0;
782       impl->load_state = LOAD_PRELOAD;
783     }
784   
785   info = (GtkRecentInfo *) g_list_nth_data (impl->recent_items,
786                                             impl->loaded_items);
787   g_assert (info);
788
789   uri = gtk_recent_info_get_uri (info);
790   name = gtk_recent_info_get_display_name (info);
791   
792   /* at this point, everything goes inside the model; operations on the
793    * visualization of items inside the model are done in the cell data
794    * funcs (remember that there are two of those: one for the icon and
795    * one for the text), while the filtering is done only when a filter
796    * is actually loaded. */
797   gtk_list_store_append (impl->recent_store, &iter);
798   gtk_list_store_set (impl->recent_store, &iter,
799                       RECENT_URI_COLUMN, uri,           /* uri  */
800                       RECENT_DISPLAY_NAME_COLUMN, name, /* display_name */
801                       RECENT_INFO_COLUMN, info,         /* info */
802                       -1);
803   
804   impl->loaded_items += 1;
805
806   if (impl->loaded_items == impl->n_recent_items)
807     {
808       /* we have finished loading, so we remove the items cache */
809       impl->load_state = LOAD_LOADING;
810       
811       g_list_foreach (impl->recent_items,
812                       (GFunc) gtk_recent_info_unref,
813                       NULL);
814       g_list_free (impl->recent_items);
815       
816       impl->recent_items = NULL;
817       impl->n_recent_items = 0;
818       impl->loaded_items = 0;
819
820       /* load the filled up model */
821       chooser_set_model (impl);
822
823       retval = FALSE;
824     }
825   else
826     {
827       /* we did not finish, so continue loading */
828       retval = TRUE;
829     }
830   
831   return retval;
832 }
833
834 static void
835 cleanup_after_load (gpointer user_data)
836 {
837   GtkRecentChooserDefault *impl;
838   
839   impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
840
841   if (impl->load_id != 0)
842     {
843       g_assert ((impl->load_state == LOAD_EMPTY) ||
844                 (impl->load_state == LOAD_PRELOAD) ||
845                 (impl->load_state == LOAD_LOADING) ||
846                 (impl->load_state == LOAD_FINISHED));
847       
848       /* we have officialy finished loading all the items,
849        * so we can reset the state machine
850        */
851       g_source_remove (impl->load_id);
852       impl->load_id = 0;
853       impl->load_state = LOAD_EMPTY;
854     }
855   else
856     g_assert ((impl->load_state == LOAD_EMPTY) ||
857               (impl->load_state == LOAD_LOADING) ||
858               (impl->load_state == LOAD_FINISHED));
859
860   set_busy_cursor (impl, FALSE);
861 }
862
863 /* clears the current model and reloads the recently used resources */
864 static void
865 reload_recent_items (GtkRecentChooserDefault *impl)
866 {
867   /* reload is already in progress - do not disturb */
868   if (impl->load_id)
869     return;
870   
871   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->recent_view), NULL);
872   gtk_list_store_clear (impl->recent_store);
873   
874   if (!impl->icon_theme)
875     impl->icon_theme = get_icon_theme_for_widget (GTK_WIDGET (impl));
876
877   impl->icon_size = get_icon_size_for_widget (GTK_WIDGET (impl),
878                                               GTK_ICON_SIZE_BUTTON);
879
880   set_busy_cursor (impl, TRUE);
881
882   impl->load_state = LOAD_EMPTY;
883   impl->load_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
884                                              load_recent_items,
885                                              impl,
886                                              cleanup_after_load);
887 }
888
889 /* taken form gtkfilechooserdialog.c */
890 static void
891 set_default_size (GtkRecentChooserDefault *impl)
892 {
893   GtkWidget *widget;
894   gint width, height;
895   gint font_size;
896   GdkScreen *screen;
897   gint monitor_num;
898   GtkRequisition req;
899   GdkRectangle monitor;
900
901   widget = GTK_WIDGET (impl);
902
903   /* Size based on characters and the icon size */
904   font_size = pango_font_description_get_size (widget->style->font_desc);
905   font_size = PANGO_PIXELS (font_size);
906
907   width = impl->icon_size + font_size * NUM_CHARS;
908   height = (impl->icon_size + font_size) * NUM_LINES;
909
910   /* Use at least the requisition size... */
911   gtk_widget_size_request (widget, &req);
912   width = MAX (width, req.width);
913   height = MAX (height, req.height);
914
915   /* ... but no larger than the monitor */
916   screen = gtk_widget_get_screen (widget);
917   monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
918
919   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
920
921   width = MIN (width, monitor.width * 3 / 4);
922   height = MIN (height, monitor.height * 3 / 4);
923
924   /* Set size */
925   gtk_widget_set_size_request (impl->recent_view, width, height);
926 }
927
928 static void
929 gtk_recent_chooser_default_map (GtkWidget *widget)
930 {
931   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (widget);
932   
933   if (GTK_WIDGET_CLASS (_gtk_recent_chooser_default_parent_class)->map)
934     GTK_WIDGET_CLASS (_gtk_recent_chooser_default_parent_class)->map (widget);
935
936   /* reloads everything */
937   reload_recent_items (impl);
938
939   set_default_size (impl);
940 }
941
942 static void
943 recent_icon_data_func (GtkTreeViewColumn *tree_column,
944                        GtkCellRenderer   *cell,
945                        GtkTreeModel      *model,
946                        GtkTreeIter       *iter,
947                        gpointer           user_data)
948 {
949   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
950   GtkRecentInfo *info = NULL;
951   GdkPixbuf *pixbuf;
952   
953   gtk_tree_model_get (model, iter,
954                       RECENT_INFO_COLUMN, &info,
955                       -1);
956   g_assert (info != NULL);
957   
958   pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
959   
960   g_object_set (cell,
961                 "pixbuf", pixbuf,
962                 NULL);
963   
964   if (pixbuf)  
965     g_object_unref (pixbuf);
966 }
967
968 static void
969 recent_meta_data_func (GtkTreeViewColumn *tree_column,
970                        GtkCellRenderer   *cell,
971                        GtkTreeModel      *model,
972                        GtkTreeIter       *iter,
973                        gpointer           user_data)
974 {
975   GtkRecentInfo *info = NULL;
976   gchar *uri, *name, *str;
977   gchar *escaped_name, *escaped_location;
978   
979   gtk_tree_model_get (model, iter,
980                       RECENT_DISPLAY_NAME_COLUMN, &name,
981                       RECENT_INFO_COLUMN, &info,
982                       -1);
983   g_assert (info != NULL);
984   
985   uri = gtk_recent_info_get_uri_display (info);
986   
987   if (!name)
988     name = gtk_recent_info_get_short_name (info);
989
990   escaped_name = g_markup_printf_escaped ("<b>%s</b>", name);
991   escaped_location = g_markup_printf_escaped ("<small>%s: %s</small>",
992                                               _("Location"),
993                                               uri);
994   str = g_strjoin ("\n", escaped_name, escaped_location, NULL);
995   g_free (escaped_name);
996   g_free (escaped_location);
997   
998   g_object_set (cell, "markup", str, NULL);
999   
1000   g_free (str);
1001   g_free (uri);
1002   g_free (name);
1003   gtk_recent_info_unref (info);
1004 }
1005
1006
1007 static gchar *
1008 gtk_recent_chooser_default_get_current_uri (GtkRecentChooser *chooser)
1009 {
1010   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1011   
1012   g_assert (impl->selection != NULL);
1013   
1014   if (!impl->select_multiple)
1015     {
1016       GtkTreeModel *model;
1017       GtkTreeIter iter;
1018       gchar *uri = NULL;
1019       
1020       if (!gtk_tree_selection_get_selected (impl->selection, &model, &iter))
1021         return NULL;
1022       
1023       gtk_tree_model_get (model, &iter, RECENT_URI_COLUMN, &uri, -1);
1024       
1025       return uri;
1026     }
1027   
1028   return NULL;
1029 }
1030
1031 typedef struct
1032 {
1033   guint found : 1;
1034   guint do_select : 1;
1035   guint do_activate : 1;
1036   
1037   gchar *uri;
1038   
1039   GtkRecentChooserDefault *impl;
1040 } SelectURIData;
1041
1042 static gboolean
1043 scan_for_uri_cb (GtkTreeModel *model,
1044                  GtkTreePath  *path,
1045                  GtkTreeIter  *iter,
1046                  gpointer      user_data)
1047 {
1048   SelectURIData *select_data = (SelectURIData *) user_data;
1049   gchar *uri = NULL;
1050   
1051   if (!select_data)
1052     return TRUE;
1053   
1054   if (select_data->found)
1055     return TRUE;
1056   
1057   gtk_tree_model_get (model, iter, RECENT_URI_COLUMN, &uri, -1);
1058   if (!uri)
1059     return FALSE;
1060   
1061   if (strcmp (uri, select_data->uri) == 0)
1062     {
1063       select_data->found = TRUE;
1064       
1065       if (select_data->do_activate)
1066         gtk_tree_view_row_activated (GTK_TREE_VIEW (select_data->impl->recent_view),
1067                                      path,
1068                                      select_data->impl->meta_column);
1069       
1070       if (select_data->do_select)
1071         gtk_tree_selection_select_path (select_data->impl->selection, path);
1072       else
1073         gtk_tree_selection_unselect_path (select_data->impl->selection, path);
1074
1075       g_free (uri);
1076       
1077       return TRUE;
1078     }
1079
1080   g_free (uri);
1081   
1082   return FALSE;
1083 }
1084
1085 static gboolean
1086 gtk_recent_chooser_default_set_current_uri (GtkRecentChooser  *chooser,
1087                                             const gchar       *uri,
1088                                             GError           **error)
1089 {
1090   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1091   SelectURIData *data;
1092   
1093   data = g_new0 (SelectURIData, 1);
1094   data->uri = g_strdup (uri);
1095   data->impl = impl;
1096   data->found = FALSE;
1097   data->do_activate = TRUE;
1098   data->do_select = TRUE;
1099   
1100   gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
1101                           scan_for_uri_cb,
1102                           data);
1103   
1104   if (!data->found)
1105     {
1106       g_free (data->uri);
1107       g_free (data);
1108       
1109       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
1110                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
1111                    _("No item for URI '%s' found"),
1112                    uri);
1113       return FALSE;
1114     }
1115   
1116   g_free (data->uri);
1117   g_free (data);
1118
1119   return TRUE;
1120 }
1121
1122 static gboolean
1123 gtk_recent_chooser_default_select_uri (GtkRecentChooser  *chooser,
1124                                        const gchar       *uri,
1125                                        GError           **error)
1126 {
1127   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1128   SelectURIData *data;
1129   
1130   data = g_new0 (SelectURIData, 1);
1131   data->uri = g_strdup (uri);
1132   data->impl = impl;
1133   data->found = FALSE;
1134   data->do_activate = FALSE;
1135   data->do_select = TRUE;
1136   
1137   gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
1138                           scan_for_uri_cb,
1139                           data);
1140   
1141   if (!data->found)
1142     {
1143       g_free (data->uri);
1144       g_free (data);
1145       
1146       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
1147                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
1148                    _("No item for URI '%s' found"),
1149                    uri);
1150       return FALSE;
1151     }
1152   
1153   g_free (data->uri);
1154   g_free (data);
1155
1156   return TRUE;
1157 }
1158
1159 static void
1160 gtk_recent_chooser_default_unselect_uri (GtkRecentChooser *chooser,
1161                                          const gchar      *uri)
1162 {
1163   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1164   SelectURIData *data;
1165   
1166   data = g_new0 (SelectURIData, 1);
1167   data->uri = g_strdup (uri);
1168   data->impl = impl;
1169   data->found = FALSE;
1170   data->do_activate = FALSE;
1171   data->do_select = FALSE;
1172   
1173   gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
1174                           scan_for_uri_cb,
1175                           data);
1176   
1177   g_free (data->uri);
1178   g_free (data);
1179 }
1180
1181 static void
1182 gtk_recent_chooser_default_select_all (GtkRecentChooser *chooser)
1183 {
1184   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1185   
1186   if (!impl->select_multiple)
1187     return;
1188   
1189   gtk_tree_selection_select_all (impl->selection);
1190 }
1191
1192 static void
1193 gtk_recent_chooser_default_unselect_all (GtkRecentChooser *chooser)
1194 {
1195   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1196   
1197   gtk_tree_selection_unselect_all (impl->selection);
1198 }
1199
1200 static void
1201 gtk_recent_chooser_default_set_sort_func (GtkRecentChooser  *chooser,
1202                                           GtkRecentSortFunc  sort_func,
1203                                           gpointer           sort_data,
1204                                           GDestroyNotify     data_destroy)
1205 {
1206   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1207   
1208   if (impl->sort_data_destroy)
1209     {
1210       impl->sort_data_destroy (impl->sort_data);
1211       impl->sort_data_destroy = NULL;
1212     }
1213       
1214   impl->sort_func = NULL;
1215   impl->sort_data = NULL;
1216   
1217   if (sort_func)
1218     {
1219       impl->sort_func = sort_func;
1220       impl->sort_data = sort_data;
1221       impl->sort_data_destroy = data_destroy;
1222     }
1223 }
1224
1225 static GList *
1226 gtk_recent_chooser_default_get_items (GtkRecentChooser *chooser)
1227 {
1228   GtkRecentChooserDefault *impl;
1229
1230   impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1231
1232   return _gtk_recent_chooser_get_items (chooser,
1233                                         impl->current_filter,
1234                                         impl->sort_func,
1235                                         impl->sort_data);
1236 }
1237
1238 static GtkRecentManager *
1239 gtk_recent_chooser_default_get_recent_manager (GtkRecentChooser *chooser)
1240 {
1241   return GTK_RECENT_CHOOSER_DEFAULT (chooser)->manager;
1242 }
1243
1244 static void
1245 show_filters (GtkRecentChooserDefault *impl,
1246               gboolean                 show)
1247 {
1248   if (show)
1249     gtk_widget_show (impl->filter_combo_hbox);
1250   else
1251     gtk_widget_hide (impl->filter_combo_hbox);
1252 }
1253
1254 static void
1255 gtk_recent_chooser_default_add_filter (GtkRecentChooser *chooser,
1256                                        GtkRecentFilter  *filter)
1257 {
1258   GtkRecentChooserDefault *impl;
1259   const gchar *name;
1260
1261   impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1262   
1263   if (g_slist_find (impl->filters, filter))
1264     {
1265       g_warning ("gtk_recent_chooser_add_filter() called on filter already in list\n");
1266       return;
1267     }
1268   
1269   g_object_ref_sink (filter);
1270   impl->filters = g_slist_append (impl->filters, filter);
1271   
1272   /* display new filter */
1273   name = gtk_recent_filter_get_name (filter);
1274   if (!name)
1275     name = _("Untitled filter");
1276     
1277   gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
1278   
1279   if (!g_slist_find (impl->filters, impl->current_filter))
1280     set_current_filter (impl, filter);
1281   
1282   show_filters (impl, TRUE);
1283 }
1284
1285 static void
1286 gtk_recent_chooser_default_remove_filter (GtkRecentChooser *chooser,
1287                                           GtkRecentFilter  *filter)
1288 {
1289   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1290   GtkTreeModel *model;
1291   GtkTreeIter iter;
1292   gint filter_idx;
1293   
1294   filter_idx = g_slist_index (impl->filters, filter);
1295   
1296   if (filter_idx < 0)
1297     {
1298       g_warning ("gtk_recent_chooser_remove_filter() called on filter not in list\n");
1299       return;  
1300     }
1301   
1302   impl->filters = g_slist_remove (impl->filters, filter);
1303   
1304   if (filter == impl->current_filter)
1305     {
1306       if (impl->filters)
1307         set_current_filter (impl, impl->filters->data);
1308       else
1309         set_current_filter (impl, NULL);
1310     }
1311   
1312   model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
1313   gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_idx);
1314   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
1315   
1316   g_object_unref (filter);
1317   
1318   if (!impl->filters)
1319     show_filters (impl, FALSE);
1320 }
1321
1322 static GSList *
1323 gtk_recent_chooser_default_list_filters (GtkRecentChooser *chooser)
1324 {
1325   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1326   
1327   return g_slist_copy (impl->filters);
1328 }
1329
1330 static void
1331 set_current_filter (GtkRecentChooserDefault *impl,
1332                     GtkRecentFilter         *filter)
1333 {
1334   if (impl->current_filter != filter)
1335     {
1336       gint filter_idx;
1337       
1338       filter_idx = g_slist_index (impl->filters, filter);
1339       if (impl->filters && filter && filter_idx < 0)
1340         return;
1341       
1342       if (impl->current_filter)
1343         g_object_unref (impl->current_filter);
1344       
1345       impl->current_filter = filter;
1346       
1347       if (impl->current_filter)     
1348         {
1349           g_object_ref_sink (impl->current_filter);
1350         }
1351       
1352       if (impl->filters)
1353         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
1354                                   filter_idx);
1355       
1356       if (impl->recent_store)
1357         reload_recent_items (impl);
1358
1359       g_object_notify (G_OBJECT (impl), "filter");
1360     }
1361 }
1362
1363 static void
1364 chooser_set_sort_type (GtkRecentChooserDefault *impl,
1365                        GtkRecentSortType        sort_type)
1366 {
1367   if (impl->sort_type != sort_type)
1368     {
1369       impl->sort_type = sort_type;
1370       reload_recent_items (impl);
1371
1372       g_object_notify (G_OBJECT (impl), "sort-type");
1373     }
1374 }
1375
1376
1377 static GtkIconTheme *
1378 get_icon_theme_for_widget (GtkWidget *widget)
1379 {
1380   if (gtk_widget_has_screen (widget))
1381     return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1382
1383   return gtk_icon_theme_get_default ();
1384 }
1385
1386 static gint
1387 get_icon_size_for_widget (GtkWidget   *widget,
1388                           GtkIconSize  icon_size)
1389 {
1390   GtkSettings *settings;
1391   gint width, height;
1392
1393   if (gtk_widget_has_screen (widget))
1394     settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1395   else
1396     settings = gtk_settings_get_default ();
1397
1398   if (gtk_icon_size_lookup_for_settings (settings, icon_size,
1399                                          &width, &height))
1400     return MAX (width, height);
1401
1402   return FALLBACK_ICON_SIZE;
1403 }
1404
1405
1406 static void
1407 recent_manager_changed_cb (GtkRecentManager *manager,
1408                            gpointer          user_data)
1409 {
1410   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1411
1412   reload_recent_items (impl);
1413 }
1414
1415 static void
1416 selection_changed_cb (GtkTreeSelection *selection,
1417                       gpointer          user_data)
1418 {
1419   _gtk_recent_chooser_selection_changed (GTK_RECENT_CHOOSER (user_data));
1420 }
1421
1422 static void
1423 row_activated_cb (GtkTreeView       *tree_view,
1424                   GtkTreePath       *tree_path,
1425                   GtkTreeViewColumn *tree_column,
1426                   gpointer           user_data)
1427 {
1428   _gtk_recent_chooser_item_activated (GTK_RECENT_CHOOSER (user_data));
1429 }
1430
1431 static void
1432 filter_combo_changed_cb (GtkComboBox *combo_box,
1433                          gpointer     user_data)
1434 {
1435   GtkRecentChooserDefault *impl;
1436   gint new_index;
1437   GtkRecentFilter *filter;
1438   
1439   impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1440   
1441   new_index = gtk_combo_box_get_active (combo_box);
1442   filter = g_slist_nth_data (impl->filters, new_index);
1443   
1444   set_current_filter (impl, filter);
1445 }
1446
1447 static GdkPixbuf *
1448 get_drag_pixbuf (GtkRecentChooserDefault *impl)
1449 {
1450   GtkRecentInfo *info;
1451   GdkPixbuf *retval;
1452   gint size;
1453   
1454   g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
1455
1456   info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (impl));
1457   if (!info)
1458     return NULL;
1459
1460   size = get_icon_size_for_widget (GTK_WIDGET (impl), GTK_ICON_SIZE_DND);
1461
1462   retval = gtk_recent_info_get_icon (info, size);
1463   gtk_recent_info_unref (info);
1464
1465   return retval;
1466 }
1467
1468 static void
1469 recent_view_drag_begin_cb (GtkWidget      *widget,
1470                            GdkDragContext *context,
1471                            gpointer        user_data)
1472 {
1473   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1474   GdkPixbuf *pixbuf;
1475
1476   pixbuf = get_drag_pixbuf (impl);
1477   if (pixbuf)
1478     {
1479       gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
1480       g_object_unref (pixbuf);
1481     }
1482   else
1483     gtk_drag_set_icon_default (context);
1484 }
1485
1486 typedef struct
1487 {
1488   gchar **uri_list;
1489   gsize next_pos;
1490 } DragData;
1491
1492 static void
1493 append_uri_to_urilist (GtkTreeModel *model,
1494                        GtkTreePath  *path,
1495                        GtkTreeIter  *iter,
1496                        gpointer      user_data)
1497 {
1498   DragData *drag_data = (DragData *) user_data;
1499   GtkTreeModel *child_model;
1500   GtkTreeIter child_iter;
1501   gchar *uri = NULL;
1502   gsize pos;
1503
1504   child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
1505   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
1506                                                     &child_iter,
1507                                                     iter);
1508   gtk_tree_model_get (child_model, &child_iter,
1509                       RECENT_URI_COLUMN, &uri,
1510                       -1);
1511   g_assert (uri != NULL);
1512
1513   pos = drag_data->next_pos;
1514   drag_data->uri_list[pos] = g_strdup (uri);
1515   drag_data->next_pos = pos + 1;
1516 }
1517
1518 static void
1519 recent_view_drag_data_get_cb (GtkWidget        *widget,
1520                               GdkDragContext   *context,
1521                               GtkSelectionData *selection_data,
1522                               guint             info,
1523                               guint32           time_,
1524                               gpointer          data)
1525 {
1526   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (data);
1527   DragData *drag_data;
1528   gsize n_uris;
1529   
1530   n_uris = gtk_tree_selection_count_selected_rows (impl->selection);
1531   if (n_uris == 0)
1532           return;
1533
1534   drag_data = g_new (DragData, 1);
1535   drag_data->uri_list = g_new0 (gchar *, n_uris + 1);
1536   drag_data->next_pos = 0;
1537   
1538   gtk_tree_selection_selected_foreach (impl->selection,
1539                                        append_uri_to_urilist,
1540                                        drag_data);
1541   
1542   gtk_selection_data_set_uris (selection_data, drag_data->uri_list);
1543
1544   g_strfreev (drag_data->uri_list);
1545   g_free (drag_data);
1546 }
1547
1548
1549
1550 static void
1551 remove_selected_from_list (GtkRecentChooserDefault *impl)
1552 {
1553   gchar *uri;
1554   GError *err;
1555   
1556   if (impl->select_multiple)
1557     return;
1558   
1559   uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (impl));
1560   if (!uri)
1561     return;
1562   
1563   err = NULL;
1564   if (!gtk_recent_manager_remove_item (impl->manager, uri, &err))
1565     {
1566       gchar *msg;
1567    
1568       msg = strdup (_("Could not remove item"));
1569       error_message (impl, msg, err->message);
1570       
1571       g_free (msg);
1572       g_error_free (err);
1573     }
1574   
1575   g_free (uri);
1576 }
1577
1578 static void
1579 copy_activated_cb (GtkMenuItem *menu_item,
1580                    gpointer     user_data)
1581 {
1582   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1583   GtkRecentInfo *info;
1584   gchar *utf8_uri;
1585
1586   info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (impl));
1587   if (!info)
1588     return;
1589
1590   utf8_uri = gtk_recent_info_get_uri_display (info);
1591   
1592   gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (impl),
1593                                                     GDK_SELECTION_CLIPBOARD),
1594                           utf8_uri, -1);
1595
1596   g_free (utf8_uri);
1597 }
1598
1599 static void
1600 remove_all_activated_cb (GtkMenuItem *menu_item,
1601                          gpointer     user_data)
1602 {
1603   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1604   GError *err = NULL;
1605   
1606   gtk_recent_manager_purge_items (impl->manager, &err);
1607   if (err)
1608     {
1609        gchar *msg;
1610
1611        msg = g_strdup (_("Could not clear list"));
1612
1613        error_message (impl, msg, err->message);
1614        
1615        g_free (msg);
1616        g_error_free (err);
1617     }
1618 }
1619
1620 static void
1621 remove_item_activated_cb (GtkMenuItem *menu_item,
1622                           gpointer     user_data)
1623 {
1624   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1625   
1626   remove_selected_from_list (impl);
1627 }
1628
1629 static void
1630 show_private_toggled_cb (GtkCheckMenuItem *menu_item,
1631                          gpointer          user_data)
1632 {
1633   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1634   
1635   g_object_set (G_OBJECT (impl),
1636                 "show-private", gtk_check_menu_item_get_active (menu_item),
1637                 NULL);
1638 }
1639
1640 static void
1641 recent_popup_menu_detach_cb (GtkWidget *attach_widget,
1642                              GtkMenu   *menu)
1643 {
1644   GtkRecentChooserDefault *impl;
1645   
1646   impl = g_object_get_data (G_OBJECT (attach_widget), "GtkRecentChooserDefault");
1647   g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
1648   
1649   impl->recent_popup_menu = NULL;
1650   impl->recent_popup_menu_remove_item = NULL;
1651   impl->recent_popup_menu_copy_item = NULL;
1652   impl->recent_popup_menu_clear_item = NULL;
1653   impl->recent_popup_menu_show_private_item = NULL;
1654 }
1655
1656 static void
1657 recent_view_menu_ensure_state (GtkRecentChooserDefault *impl)
1658 {
1659   gint count;
1660   
1661   g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
1662   g_assert (impl->recent_popup_menu != NULL);
1663
1664   if (!impl->manager)
1665     count = 0;
1666   else
1667     g_object_get (G_OBJECT (impl->manager), "size", &count, NULL);
1668
1669   if (count == 0)
1670     {
1671       gtk_widget_set_sensitive (impl->recent_popup_menu_remove_item, FALSE);
1672       gtk_widget_set_sensitive (impl->recent_popup_menu_copy_item, FALSE);
1673       gtk_widget_set_sensitive (impl->recent_popup_menu_clear_item, FALSE);
1674       gtk_widget_set_sensitive (impl->recent_popup_menu_show_private_item, FALSE);
1675     }
1676 }
1677
1678 static void
1679 recent_view_menu_build (GtkRecentChooserDefault *impl)
1680 {
1681   GtkWidget *item;
1682   
1683   if (impl->recent_popup_menu)
1684     {
1685       recent_view_menu_ensure_state (impl);
1686       
1687       return;
1688     }
1689   
1690   impl->recent_popup_menu = gtk_menu_new ();
1691   gtk_menu_attach_to_widget (GTK_MENU (impl->recent_popup_menu),
1692                              impl->recent_view,
1693                              recent_popup_menu_detach_cb);
1694   
1695   item = gtk_image_menu_item_new_with_mnemonic (_("Copy _Location"));
1696   impl->recent_popup_menu_copy_item = item;
1697   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1698                                  gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU));
1699   g_signal_connect (item, "activate",
1700                     G_CALLBACK (copy_activated_cb), impl);
1701   gtk_widget_show (item);
1702   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1703
1704   item = gtk_separator_menu_item_new ();
1705   gtk_widget_show (item);
1706   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1707   
1708   item = gtk_image_menu_item_new_with_mnemonic (_("_Remove From List"));
1709   impl->recent_popup_menu_remove_item = item;
1710   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1711                                  gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
1712   g_signal_connect (item, "activate",
1713                     G_CALLBACK (remove_item_activated_cb), impl);
1714   gtk_widget_show (item);
1715   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1716
1717   item = gtk_image_menu_item_new_with_mnemonic (_("_Clear List"));
1718   impl->recent_popup_menu_clear_item = item;
1719   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1720                                  gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU));
1721   g_signal_connect (item, "activate",
1722                     G_CALLBACK (remove_all_activated_cb), impl);
1723   
1724   gtk_widget_show (item);
1725   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1726   
1727   item = gtk_separator_menu_item_new ();
1728   gtk_widget_show (item);
1729   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1730   
1731   item = gtk_check_menu_item_new_with_mnemonic (_("Show _Private Resources"));
1732   impl->recent_popup_menu_show_private_item = item;
1733   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), impl->show_private);
1734   g_signal_connect (item, "toggled",
1735                     G_CALLBACK (show_private_toggled_cb), impl);
1736   gtk_widget_show (item);
1737   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1738   
1739   recent_view_menu_ensure_state (impl);
1740 }
1741
1742 /* taken from gtkfilechooserdefault.c */
1743 static void
1744 popup_position_func (GtkMenu   *menu,
1745                      gint      *x,
1746                      gint      *y,
1747                      gboolean  *push_in,
1748                      gpointer   user_data)
1749 {
1750   GtkWidget *widget = GTK_WIDGET (user_data);
1751   GdkScreen *screen = gtk_widget_get_screen (widget);
1752   GtkRequisition req;
1753   gint monitor_num;
1754   GdkRectangle monitor;
1755
1756   if (G_UNLIKELY (!GTK_WIDGET_REALIZED (widget)))
1757     return;
1758
1759   gdk_window_get_origin (widget->window, x, y);
1760
1761   gtk_widget_size_request (GTK_WIDGET (menu), &req);
1762
1763   *x += (widget->allocation.width - req.width) / 2;
1764   *y += (widget->allocation.height - req.height) / 2;
1765
1766   monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
1767   gtk_menu_set_monitor (menu, monitor_num);
1768   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1769
1770   *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
1771   *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
1772
1773   *push_in = FALSE;
1774 }
1775
1776
1777 static void
1778 recent_view_menu_popup (GtkRecentChooserDefault *impl,
1779                         GdkEventButton          *event)
1780 {
1781   recent_view_menu_build (impl);
1782   
1783   if (event)
1784     gtk_menu_popup (GTK_MENU (impl->recent_popup_menu),
1785                     NULL, NULL, NULL, NULL,
1786                     event->button, event->time);
1787   else
1788     {
1789       gtk_menu_popup (GTK_MENU (impl->recent_popup_menu),
1790                       NULL, NULL,
1791                       popup_position_func, impl->recent_view,
1792                       0, GDK_CURRENT_TIME);
1793       gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->recent_popup_menu),
1794                                    FALSE);
1795     }
1796 }
1797
1798 static gboolean
1799 recent_view_popup_menu_cb (GtkWidget *widget,
1800                            gpointer   user_data)
1801 {
1802   recent_view_menu_popup (GTK_RECENT_CHOOSER_DEFAULT (user_data), NULL);
1803   return TRUE;
1804 }
1805
1806 static gboolean
1807 recent_view_button_press_cb (GtkWidget      *widget,
1808                              GdkEventButton *event,
1809                              gpointer        user_data)
1810 {
1811   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1812   
1813   if (event->button == 3)
1814     {
1815       GtkTreePath *path;
1816       gboolean res;
1817
1818       if (event->window != gtk_tree_view_get_bin_window (GTK_TREE_VIEW (impl->recent_view)))
1819         return FALSE;
1820
1821       res = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (impl->recent_view),
1822                                            event->x, event->y,
1823                                            &path,
1824                                            NULL, NULL, NULL);
1825       if (!res)
1826         return FALSE;
1827
1828       /* select the path before creating the popup menu */
1829       gtk_tree_selection_select_path (impl->selection, path);
1830       gtk_tree_path_free (path);
1831       
1832       recent_view_menu_popup (impl, event);
1833
1834       return TRUE;
1835     }
1836   
1837   return FALSE;
1838 }
1839
1840 static void
1841 set_recent_manager (GtkRecentChooserDefault *impl,
1842                     GtkRecentManager        *manager)
1843 {
1844   if (impl->manager)
1845     {
1846       if (impl->manager_changed_id)
1847         {
1848           g_signal_handler_disconnect (impl, impl->manager_changed_id);
1849           impl->manager_changed_id = 0;
1850         }
1851
1852       impl->manager = NULL;
1853     }
1854   
1855   if (manager)
1856     impl->manager = manager;
1857   else
1858     impl->manager = gtk_recent_manager_get_default ();
1859   
1860   if (impl->manager)
1861     {
1862       impl->manager_changed_id = g_signal_connect (impl->manager, "changed",
1863                                                    G_CALLBACK (recent_manager_changed_cb),
1864                                                    impl);
1865     }
1866 }
1867
1868 GtkWidget *
1869 _gtk_recent_chooser_default_new (GtkRecentManager *manager)
1870 {
1871   return g_object_new (GTK_TYPE_RECENT_CHOOSER_DEFAULT,
1872                        "recent-manager", manager,
1873                        NULL);
1874 }