]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchooserdefault.c
Move filtering of the recent files list into the shared implementation; do
[~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_id = 0;
594     }
595
596   if (impl->recent_items)
597     {
598       g_list_foreach (impl->recent_items, (GFunc) gtk_recent_info_unref, NULL);
599       g_list_free (impl->recent_items);
600       impl->recent_items = NULL;
601     }
602
603   if (impl->manager_changed_id)
604     {
605       g_signal_handler_disconnect (impl->manager, impl->manager_changed_id);
606       impl->manager_changed_id = 0;
607     }
608
609   if (impl->filters)
610     {
611       g_slist_foreach (impl->filters, (GFunc) g_object_unref, NULL);
612       g_slist_free (impl->filters);
613       impl->filters = NULL;
614     }
615   
616   if (impl->current_filter)
617     {
618       g_object_unref (impl->current_filter);
619       impl->current_filter = NULL;
620     }
621
622   if (impl->recent_store)
623     {
624       g_object_unref (impl->recent_store);
625       impl->recent_store = NULL;
626     }
627
628   if (impl->tooltips)
629     {
630       g_object_unref (impl->tooltips);
631       impl->tooltips = NULL;
632     }
633
634   G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->dispose (object);
635 }
636
637 static void
638 gtk_recent_chooser_default_finalize (GObject *object)
639 {
640   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
641
642   impl->manager = NULL; 
643   
644   if (impl->sort_data_destroy)
645     {
646       impl->sort_data_destroy (impl->sort_data);
647       impl->sort_data_destroy = NULL;
648     }
649   
650   impl->sort_data = NULL;
651   impl->sort_func = NULL;
652   
653   G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->finalize (object);
654 }
655
656 /* override GtkWidget::show_all since we have internal widgets we wish to keep
657  * hidden unless we decide otherwise, like the filter combo box.
658  */
659 static void
660 gtk_recent_chooser_default_show_all (GtkWidget *widget)
661 {
662   gtk_widget_show (widget);
663 }
664
665
666
667 /* Shows an error dialog set as transient for the specified window */
668 static void
669 error_message_with_parent (GtkWindow   *parent,
670                            const gchar *msg,
671                            const gchar *detail)
672 {
673   GtkWidget *dialog;
674
675   dialog = gtk_message_dialog_new (parent,
676                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
677                                    GTK_MESSAGE_ERROR,
678                                    GTK_BUTTONS_OK,
679                                    "%s",
680                                    msg);
681   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
682                                             "%s", detail);
683
684   if (parent->group)
685     gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
686
687   gtk_dialog_run (GTK_DIALOG (dialog));
688   gtk_widget_destroy (dialog);
689 }
690
691 /* Returns a toplevel GtkWindow, or NULL if none */
692 static GtkWindow *
693 get_toplevel (GtkWidget *widget)
694 {
695   GtkWidget *toplevel;
696
697   toplevel = gtk_widget_get_toplevel (widget);
698   if (!GTK_WIDGET_TOPLEVEL (toplevel))
699     return NULL;
700   else
701     return GTK_WINDOW (toplevel);
702 }
703
704 /* Shows an error dialog for the file chooser */
705 static void
706 error_message (GtkRecentChooserDefault *impl,
707                const gchar             *msg,
708                const gchar             *detail)
709 {
710   error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
711 }
712
713 static void
714 set_busy_cursor (GtkRecentChooserDefault *impl,
715                  gboolean                 show_busy_cursor)
716 {
717   GtkWindow *toplevel;
718   GdkDisplay *display;
719   GdkCursor *cursor;
720
721   toplevel = get_toplevel (GTK_WIDGET (impl));
722   if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
723     return;
724   
725   display = gtk_widget_get_display (GTK_WIDGET (toplevel));
726   
727   cursor = NULL;
728   if (show_busy_cursor)
729     cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
730
731   gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
732   gdk_display_flush (display);
733
734   if (cursor)
735     gdk_cursor_unref (cursor);
736 }
737
738 static void
739 chooser_set_model (GtkRecentChooserDefault *impl)
740 {
741   g_assert (impl->recent_store != NULL);
742   g_assert (impl->load_state == LOAD_LOADING);
743
744   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->recent_view),
745                            GTK_TREE_MODEL (impl->recent_store));
746   gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->recent_view));
747   gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->recent_view), TRUE);
748   gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->recent_view),
749                                    RECENT_DISPLAY_NAME_COLUMN);
750
751   impl->load_state = LOAD_FINISHED;
752 }
753
754 static gboolean
755 load_recent_items (gpointer user_data)
756 {
757   GtkRecentChooserDefault *impl;
758   GtkRecentInfo *info;
759   GtkTreeIter iter;
760   const gchar *uri, *name;
761   gboolean retval;
762   
763   impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
764   
765   g_assert ((impl->load_state == LOAD_EMPTY) ||
766             (impl->load_state == LOAD_PRELOAD));
767   
768   /* store the items for multiple runs */
769   if (!impl->recent_items)
770     {
771       impl->recent_items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (impl));
772       if (!impl->recent_items)
773         {
774           impl->load_state = LOAD_FINISHED;
775           
776           return FALSE;
777         }
778         
779       impl->n_recent_items = g_list_length (impl->recent_items);
780       impl->loaded_items = 0;
781       impl->load_state = LOAD_PRELOAD;
782     }
783   
784   info = (GtkRecentInfo *) g_list_nth_data (impl->recent_items,
785                                             impl->loaded_items);
786   g_assert (info);
787
788   uri = gtk_recent_info_get_uri (info);
789   name = gtk_recent_info_get_display_name (info);
790   
791   /* at this point, everything goes inside the model; operations on the
792    * visualization of items inside the model are done in the cell data
793    * funcs (remember that there are two of those: one for the icon and
794    * one for the text), while the filtering is done only when a filter
795    * is actually loaded. */
796   gtk_list_store_append (impl->recent_store, &iter);
797   gtk_list_store_set (impl->recent_store, &iter,
798                       RECENT_URI_COLUMN, uri,           /* uri  */
799                       RECENT_DISPLAY_NAME_COLUMN, name, /* display_name */
800                       RECENT_INFO_COLUMN, info,         /* info */
801                       -1);
802   
803   impl->loaded_items += 1;
804
805   if (impl->loaded_items == impl->n_recent_items)
806     {
807       /* we have finished loading, so we remove the items cache */
808       impl->load_state = LOAD_LOADING;
809       
810       g_list_foreach (impl->recent_items,
811                       (GFunc) gtk_recent_info_unref,
812                       NULL);
813       g_list_free (impl->recent_items);
814       
815       impl->recent_items = NULL;
816       impl->n_recent_items = 0;
817       impl->loaded_items = 0;
818
819       /* load the filled up model */
820       chooser_set_model (impl);
821
822       retval = FALSE;
823     }
824   else
825     {
826       /* we did not finish, so continue loading */
827       retval = TRUE;
828     }
829   
830   return retval;
831 }
832
833 static void
834 cleanup_after_load (gpointer user_data)
835 {
836   GtkRecentChooserDefault *impl;
837   
838   impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
839
840   if (impl->load_id != 0)
841     {
842       g_assert ((impl->load_state == LOAD_PRELOAD) ||
843                 (impl->load_state == LOAD_LOADING) ||
844                 (impl->load_state == LOAD_FINISHED));
845       
846       /* we have officialy finished loading all the items,
847        * so we can reset the state machine
848        */
849       g_source_remove (impl->load_id);
850       impl->load_id = 0;
851       impl->load_state = LOAD_EMPTY;
852     }
853   else
854     g_assert ((impl->load_state == LOAD_EMPTY) ||
855               (impl->load_state == LOAD_LOADING) ||
856               (impl->load_state == LOAD_FINISHED));
857
858   set_busy_cursor (impl, FALSE);
859 }
860
861 /* clears the current model and reloads the recently used resources */
862 static void
863 reload_recent_items (GtkRecentChooserDefault *impl)
864 {
865   /* reload is already in progress - do not disturb */
866   if (impl->load_id)
867     return;
868   
869   gtk_tree_view_set_model (GTK_TREE_VIEW (impl->recent_view), NULL);
870   gtk_list_store_clear (impl->recent_store);
871   
872   if (!impl->icon_theme)
873     impl->icon_theme = get_icon_theme_for_widget (GTK_WIDGET (impl));
874
875   impl->icon_size = get_icon_size_for_widget (GTK_WIDGET (impl),
876                                               GTK_ICON_SIZE_BUTTON);
877
878   set_busy_cursor (impl, TRUE);
879
880   impl->load_state = LOAD_EMPTY;
881   impl->load_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
882                                              load_recent_items,
883                                              impl,
884                                              cleanup_after_load);
885 }
886
887 /* taken form gtkfilechooserdialog.c */
888 static void
889 set_default_size (GtkRecentChooserDefault *impl)
890 {
891   GtkWidget *widget;
892   gint width, height;
893   gint font_size;
894   GdkScreen *screen;
895   gint monitor_num;
896   GtkRequisition req;
897   GdkRectangle monitor;
898
899   widget = GTK_WIDGET (impl);
900
901   /* Size based on characters and the icon size */
902   font_size = pango_font_description_get_size (widget->style->font_desc);
903   font_size = PANGO_PIXELS (font_size);
904
905   width = impl->icon_size + font_size * NUM_CHARS;
906   height = (impl->icon_size + font_size) * NUM_LINES;
907
908   /* Use at least the requisition size... */
909   gtk_widget_size_request (widget, &req);
910   width = MAX (width, req.width);
911   height = MAX (height, req.height);
912
913   /* ... but no larger than the monitor */
914   screen = gtk_widget_get_screen (widget);
915   monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
916
917   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
918
919   width = MIN (width, monitor.width * 3 / 4);
920   height = MIN (height, monitor.height * 3 / 4);
921
922   /* Set size */
923   gtk_widget_set_size_request (impl->recent_view, width, height);
924 }
925
926 static void
927 gtk_recent_chooser_default_map (GtkWidget *widget)
928 {
929   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (widget);
930   
931   if (GTK_WIDGET_CLASS (_gtk_recent_chooser_default_parent_class)->map)
932     GTK_WIDGET_CLASS (_gtk_recent_chooser_default_parent_class)->map (widget);
933
934   /* reloads everything */
935   reload_recent_items (impl);
936
937   set_default_size (impl);
938 }
939
940 static void
941 recent_icon_data_func (GtkTreeViewColumn *tree_column,
942                        GtkCellRenderer   *cell,
943                        GtkTreeModel      *model,
944                        GtkTreeIter       *iter,
945                        gpointer           user_data)
946 {
947   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
948   GtkRecentInfo *info = NULL;
949   GdkPixbuf *pixbuf;
950   
951   gtk_tree_model_get (model, iter,
952                       RECENT_INFO_COLUMN, &info,
953                       -1);
954   g_assert (info != NULL);
955   
956   pixbuf = gtk_recent_info_get_icon (info, impl->icon_size);
957   
958   g_object_set (cell,
959                 "pixbuf", pixbuf,
960                 NULL);
961   
962   if (pixbuf)  
963     g_object_unref (pixbuf);
964 }
965
966 static void
967 recent_meta_data_func (GtkTreeViewColumn *tree_column,
968                        GtkCellRenderer   *cell,
969                        GtkTreeModel      *model,
970                        GtkTreeIter       *iter,
971                        gpointer           user_data)
972 {
973   GtkRecentInfo *info = NULL;
974   gchar *uri;
975   gchar *name;
976   GString *data;
977   
978   data = g_string_new (NULL);
979   
980   gtk_tree_model_get (model, iter,
981                       RECENT_DISPLAY_NAME_COLUMN, &name,
982                       RECENT_INFO_COLUMN, &info,
983                       -1);
984   g_assert (info != NULL);
985   
986   uri = gtk_recent_info_get_uri_display (info);
987   
988   if (!name)
989     name = gtk_recent_info_get_short_name (info);
990  
991   g_string_append_printf (data,
992                           "<b>%s</b>\n"
993                           "<small>Location: %s</small>",
994                           name,
995                           uri);
996   
997   g_object_set (cell,
998                 "markup", data->str,
999                 NULL);
1000   
1001   g_string_free (data, TRUE);
1002   g_free (uri);
1003   g_free (name);
1004   gtk_recent_info_unref (info);
1005 }
1006
1007
1008 static gchar *
1009 gtk_recent_chooser_default_get_current_uri (GtkRecentChooser *chooser)
1010 {
1011   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1012   
1013   g_assert (impl->selection != NULL);
1014   
1015   if (!impl->select_multiple)
1016     {
1017       GtkTreeModel *model;
1018       GtkTreeIter iter;
1019       gchar *uri = NULL;
1020       
1021       if (!gtk_tree_selection_get_selected (impl->selection, &model, &iter))
1022         return NULL;
1023       
1024       gtk_tree_model_get (model, &iter, RECENT_URI_COLUMN, &uri, -1);
1025       
1026       return uri;
1027     }
1028   
1029   return NULL;
1030 }
1031
1032 typedef struct
1033 {
1034   guint found : 1;
1035   guint do_select : 1;
1036   guint do_activate : 1;
1037   
1038   gchar *uri;
1039   
1040   GtkRecentChooserDefault *impl;
1041 } SelectURIData;
1042
1043 static gboolean
1044 scan_for_uri_cb (GtkTreeModel *model,
1045                  GtkTreePath  *path,
1046                  GtkTreeIter  *iter,
1047                  gpointer      user_data)
1048 {
1049   SelectURIData *select_data = (SelectURIData *) user_data;
1050   gchar *uri = NULL;
1051   
1052   if (!select_data)
1053     return TRUE;
1054   
1055   if (select_data->found)
1056     return TRUE;
1057   
1058   gtk_tree_model_get (model, iter, RECENT_URI_COLUMN, &uri, -1);
1059   if (!uri)
1060     return FALSE;
1061   
1062   if (strcmp (uri, select_data->uri) == 0)
1063     {
1064       select_data->found = TRUE;
1065       
1066       if (select_data->do_activate)
1067         gtk_tree_view_row_activated (GTK_TREE_VIEW (select_data->impl->recent_view),
1068                                      path,
1069                                      select_data->impl->meta_column);
1070       
1071       if (select_data->do_select)
1072         gtk_tree_selection_select_path (select_data->impl->selection, path);
1073       else
1074         gtk_tree_selection_unselect_path (select_data->impl->selection, path);
1075
1076       g_free (uri);
1077       
1078       return TRUE;
1079     }
1080
1081   g_free (uri);
1082   
1083   return FALSE;
1084 }
1085
1086 static gboolean
1087 gtk_recent_chooser_default_set_current_uri (GtkRecentChooser  *chooser,
1088                                             const gchar       *uri,
1089                                             GError           **error)
1090 {
1091   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1092   SelectURIData *data;
1093   
1094   data = g_new0 (SelectURIData, 1);
1095   data->uri = g_strdup (uri);
1096   data->impl = impl;
1097   data->found = FALSE;
1098   data->do_activate = TRUE;
1099   data->do_select = TRUE;
1100   
1101   gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
1102                           scan_for_uri_cb,
1103                           data);
1104   
1105   if (!data->found)
1106     {
1107       g_free (data->uri);
1108       g_free (data);
1109       
1110       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
1111                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
1112                    _("No item for URI '%s' found"),
1113                    uri);
1114       return FALSE;
1115     }
1116   
1117   g_free (data->uri);
1118   g_free (data);
1119
1120   return TRUE;
1121 }
1122
1123 static gboolean
1124 gtk_recent_chooser_default_select_uri (GtkRecentChooser  *chooser,
1125                                        const gchar       *uri,
1126                                        GError           **error)
1127 {
1128   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1129   SelectURIData *data;
1130   
1131   data = g_new0 (SelectURIData, 1);
1132   data->uri = g_strdup (uri);
1133   data->impl = impl;
1134   data->found = FALSE;
1135   data->do_activate = FALSE;
1136   data->do_select = TRUE;
1137   
1138   gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
1139                           scan_for_uri_cb,
1140                           data);
1141   
1142   if (!data->found)
1143     {
1144       g_free (data->uri);
1145       g_free (data);
1146       
1147       g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
1148                    GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
1149                    _("No item for URI '%s' found"),
1150                    uri);
1151       return FALSE;
1152     }
1153   
1154   g_free (data->uri);
1155   g_free (data);
1156
1157   return TRUE;
1158 }
1159
1160 static void
1161 gtk_recent_chooser_default_unselect_uri (GtkRecentChooser *chooser,
1162                                          const gchar      *uri)
1163 {
1164   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1165   SelectURIData *data;
1166   
1167   data = g_new0 (SelectURIData, 1);
1168   data->uri = g_strdup (uri);
1169   data->impl = impl;
1170   data->found = FALSE;
1171   data->do_activate = FALSE;
1172   data->do_select = FALSE;
1173   
1174   gtk_tree_model_foreach (GTK_TREE_MODEL (impl->recent_store),
1175                           scan_for_uri_cb,
1176                           data);
1177   
1178   g_free (data->uri);
1179   g_free (data);
1180 }
1181
1182 static void
1183 gtk_recent_chooser_default_select_all (GtkRecentChooser *chooser)
1184 {
1185   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1186   
1187   if (!impl->select_multiple)
1188     return;
1189   
1190   gtk_tree_selection_select_all (impl->selection);
1191 }
1192
1193 static void
1194 gtk_recent_chooser_default_unselect_all (GtkRecentChooser *chooser)
1195 {
1196   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1197   
1198   gtk_tree_selection_unselect_all (impl->selection);
1199 }
1200
1201 static void
1202 gtk_recent_chooser_default_set_sort_func (GtkRecentChooser  *chooser,
1203                                           GtkRecentSortFunc  sort_func,
1204                                           gpointer           sort_data,
1205                                           GDestroyNotify     data_destroy)
1206 {
1207   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1208   
1209   if (impl->sort_data_destroy)
1210     {
1211       impl->sort_data_destroy (impl->sort_data);
1212       impl->sort_data_destroy = NULL;
1213     }
1214       
1215   impl->sort_func = NULL;
1216   impl->sort_data = NULL;
1217   
1218   if (sort_func)
1219     {
1220       impl->sort_func = sort_func;
1221       impl->sort_data = sort_data;
1222       impl->sort_data_destroy = data_destroy;
1223     }
1224 }
1225
1226 static GList *
1227 gtk_recent_chooser_default_get_items (GtkRecentChooser *chooser)
1228 {
1229   GtkRecentChooserDefault *impl;
1230
1231   impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1232
1233   return _gtk_recent_chooser_get_items (chooser,
1234                                         impl->current_filter,
1235                                         impl->sort_func,
1236                                         impl->sort_data);
1237 }
1238
1239 static GtkRecentManager *
1240 gtk_recent_chooser_default_get_recent_manager (GtkRecentChooser *chooser)
1241 {
1242   return GTK_RECENT_CHOOSER_DEFAULT (chooser)->manager;
1243 }
1244
1245 static void
1246 show_filters (GtkRecentChooserDefault *impl,
1247               gboolean                 show)
1248 {
1249   if (show)
1250     gtk_widget_show (impl->filter_combo_hbox);
1251   else
1252     gtk_widget_hide (impl->filter_combo_hbox);
1253 }
1254
1255 static void
1256 gtk_recent_chooser_default_add_filter (GtkRecentChooser *chooser,
1257                                        GtkRecentFilter  *filter)
1258 {
1259   GtkRecentChooserDefault *impl;
1260   const gchar *name;
1261
1262   impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1263   
1264   if (g_slist_find (impl->filters, filter))
1265     {
1266       g_warning ("gtk_recent_chooser_add_filter() called on filter already in list\n");
1267       return;
1268     }
1269   
1270   g_object_ref_sink (filter);
1271   impl->filters = g_slist_append (impl->filters, filter);
1272   
1273   /* display new filter */
1274   name = gtk_recent_filter_get_name (filter);
1275   if (!name)
1276     name = "Untitled filter";
1277     
1278   gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
1279   
1280   if (!g_slist_find (impl->filters, impl->current_filter))
1281     set_current_filter (impl, filter);
1282   
1283   show_filters (impl, TRUE);
1284 }
1285
1286 static void
1287 gtk_recent_chooser_default_remove_filter (GtkRecentChooser *chooser,
1288                                           GtkRecentFilter  *filter)
1289 {
1290   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1291   GtkTreeModel *model;
1292   GtkTreeIter iter;
1293   gint filter_idx;
1294   
1295   filter_idx = g_slist_index (impl->filters, filter);
1296   
1297   if (filter_idx < 0)
1298     {
1299       g_warning ("gtk_recent_chooser_remove_filter() called on filter not in list\n");
1300       return;  
1301     }
1302   
1303   impl->filters = g_slist_remove (impl->filters, filter);
1304   
1305   if (filter == impl->current_filter)
1306     {
1307       if (impl->filters)
1308         set_current_filter (impl, impl->filters->data);
1309       else
1310         set_current_filter (impl, NULL);
1311     }
1312   
1313   model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
1314   gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_idx);
1315   gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
1316   
1317   g_object_unref (filter);
1318   
1319   if (!impl->filters)
1320     show_filters (impl, FALSE);
1321 }
1322
1323 static GSList *
1324 gtk_recent_chooser_default_list_filters (GtkRecentChooser *chooser)
1325 {
1326   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1327   
1328   return g_slist_copy (impl->filters);
1329 }
1330
1331 static void
1332 set_current_filter (GtkRecentChooserDefault *impl,
1333                     GtkRecentFilter         *filter)
1334 {
1335   if (impl->current_filter != filter)
1336     {
1337       gint filter_idx;
1338       
1339       filter_idx = g_slist_index (impl->filters, filter);
1340       if (impl->filters && filter && filter_idx < 0)
1341         return;
1342       
1343       if (impl->current_filter)
1344         g_object_unref (impl->current_filter);
1345       
1346       impl->current_filter = filter;
1347       
1348       if (impl->current_filter)     
1349         {
1350           g_object_ref_sink (impl->current_filter);
1351         }
1352       
1353       if (impl->filters)
1354         gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
1355                                   filter_idx);
1356       
1357       if (impl->recent_store)
1358         reload_recent_items (impl);
1359
1360       g_object_notify (G_OBJECT (impl), "filter");
1361     }
1362 }
1363
1364 static void
1365 chooser_set_sort_type (GtkRecentChooserDefault *impl,
1366                        GtkRecentSortType        sort_type)
1367 {
1368   if (impl->sort_type != sort_type)
1369     {
1370       impl->sort_type = sort_type;
1371       reload_recent_items (impl);
1372
1373       g_object_notify (G_OBJECT (impl), "sort-type");
1374     }
1375 }
1376
1377
1378 static GtkIconTheme *
1379 get_icon_theme_for_widget (GtkWidget *widget)
1380 {
1381   if (gtk_widget_has_screen (widget))
1382     return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1383
1384   return gtk_icon_theme_get_default ();
1385 }
1386
1387 static gint
1388 get_icon_size_for_widget (GtkWidget   *widget,
1389                           GtkIconSize  icon_size)
1390 {
1391   GtkSettings *settings;
1392   gint width, height;
1393
1394   if (gtk_widget_has_screen (widget))
1395     settings = gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
1396   else
1397     settings = gtk_settings_get_default ();
1398
1399   if (gtk_icon_size_lookup_for_settings (settings, icon_size,
1400                                          &width, &height))
1401     return MAX (width, height);
1402
1403   return FALLBACK_ICON_SIZE;
1404 }
1405
1406
1407 static void
1408 recent_manager_changed_cb (GtkRecentManager *manager,
1409                            gpointer          user_data)
1410 {
1411   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1412
1413   reload_recent_items (impl);
1414 }
1415
1416 static void
1417 selection_changed_cb (GtkTreeSelection *selection,
1418                       gpointer          user_data)
1419 {
1420   _gtk_recent_chooser_selection_changed (GTK_RECENT_CHOOSER (user_data));
1421 }
1422
1423 static void
1424 row_activated_cb (GtkTreeView       *tree_view,
1425                   GtkTreePath       *tree_path,
1426                   GtkTreeViewColumn *tree_column,
1427                   gpointer           user_data)
1428 {
1429   _gtk_recent_chooser_item_activated (GTK_RECENT_CHOOSER (user_data));
1430 }
1431
1432 static void
1433 filter_combo_changed_cb (GtkComboBox *combo_box,
1434                          gpointer     user_data)
1435 {
1436   GtkRecentChooserDefault *impl;
1437   gint new_index;
1438   GtkRecentFilter *filter;
1439   
1440   impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1441   
1442   new_index = gtk_combo_box_get_active (combo_box);
1443   filter = g_slist_nth_data (impl->filters, new_index);
1444   
1445   set_current_filter (impl, filter);
1446 }
1447
1448 static GdkPixbuf *
1449 get_drag_pixbuf (GtkRecentChooserDefault *impl)
1450 {
1451   GtkRecentInfo *info;
1452   GdkPixbuf *retval;
1453   gint size;
1454   
1455   g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
1456
1457   info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (impl));
1458   if (!info)
1459     return NULL;
1460
1461   size = get_icon_size_for_widget (GTK_WIDGET (impl), GTK_ICON_SIZE_DND);
1462
1463   retval = gtk_recent_info_get_icon (info, size);
1464   gtk_recent_info_unref (info);
1465
1466   return retval;
1467 }
1468
1469 static void
1470 recent_view_drag_begin_cb (GtkWidget      *widget,
1471                            GdkDragContext *context,
1472                            gpointer        user_data)
1473 {
1474   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1475   GdkPixbuf *pixbuf;
1476
1477   pixbuf = get_drag_pixbuf (impl);
1478   if (pixbuf)
1479     {
1480       gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
1481       g_object_unref (pixbuf);
1482     }
1483   else
1484     gtk_drag_set_icon_default (context);
1485 }
1486
1487 typedef struct
1488 {
1489   gchar **uri_list;
1490   gsize next_pos;
1491 } DragData;
1492
1493 static void
1494 append_uri_to_urilist (GtkTreeModel *model,
1495                        GtkTreePath  *path,
1496                        GtkTreeIter  *iter,
1497                        gpointer      user_data)
1498 {
1499   DragData *drag_data = (DragData *) user_data;
1500   GtkTreeModel *child_model;
1501   GtkTreeIter child_iter;
1502   gchar *uri = NULL;
1503   gsize pos;
1504
1505   child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
1506   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
1507                                                     &child_iter,
1508                                                     iter);
1509   gtk_tree_model_get (child_model, &child_iter,
1510                       RECENT_URI_COLUMN, &uri,
1511                       -1);
1512   g_assert (uri != NULL);
1513
1514   pos = drag_data->next_pos;
1515   drag_data->uri_list[pos] = g_strdup (uri);
1516   drag_data->next_pos = pos + 1;
1517 }
1518
1519 static void
1520 recent_view_drag_data_get_cb (GtkWidget        *widget,
1521                               GdkDragContext   *context,
1522                               GtkSelectionData *selection_data,
1523                               guint             info,
1524                               guint32           time_,
1525                               gpointer          data)
1526 {
1527   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (data);
1528   DragData *drag_data;
1529   gsize n_uris;
1530   
1531   n_uris = gtk_tree_selection_count_selected_rows (impl->selection);
1532   if (n_uris == 0)
1533           return;
1534
1535   drag_data = g_new (DragData, 1);
1536   drag_data->uri_list = g_new0 (gchar *, n_uris + 1);
1537   drag_data->next_pos = 0;
1538   
1539   gtk_tree_selection_selected_foreach (impl->selection,
1540                                        append_uri_to_urilist,
1541                                        drag_data);
1542   
1543   gtk_selection_data_set_uris (selection_data, drag_data->uri_list);
1544
1545   g_strfreev (drag_data->uri_list);
1546   g_free (drag_data);
1547 }
1548
1549
1550
1551 static void
1552 remove_selected_from_list (GtkRecentChooserDefault *impl)
1553 {
1554   gchar *uri;
1555   GError *err;
1556   
1557   if (impl->select_multiple)
1558     return;
1559   
1560   uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (impl));
1561   if (!uri)
1562     return;
1563   
1564   err = NULL;
1565   if (!gtk_recent_manager_remove_item (impl->manager, uri, &err))
1566     {
1567       gchar *msg;
1568    
1569       msg = strdup (_("Could not remove item"));
1570       error_message (impl, msg, err->message);
1571       
1572       g_free (msg);
1573       g_error_free (err);
1574     }
1575   
1576   g_free (uri);
1577 }
1578
1579 static void
1580 copy_activated_cb (GtkMenuItem *menu_item,
1581                    gpointer     user_data)
1582 {
1583   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1584   GtkRecentInfo *info;
1585   gchar *utf8_uri;
1586
1587   info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (impl));
1588   if (!info)
1589     return;
1590
1591   utf8_uri = gtk_recent_info_get_uri_display (info);
1592   
1593   gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (impl),
1594                                                     GDK_SELECTION_CLIPBOARD),
1595                           utf8_uri, -1);
1596
1597   g_free (utf8_uri);
1598 }
1599
1600 static void
1601 remove_all_activated_cb (GtkMenuItem *menu_item,
1602                          gpointer     user_data)
1603 {
1604   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1605   GError *err = NULL;
1606   
1607   gtk_recent_manager_purge_items (impl->manager, &err);
1608   if (err)
1609     {
1610        gchar *msg;
1611
1612        msg = g_strdup (_("Could not clear list"));
1613
1614        error_message (impl, msg, err->message);
1615        
1616        g_free (msg);
1617        g_error_free (err);
1618     }
1619 }
1620
1621 static void
1622 remove_item_activated_cb (GtkMenuItem *menu_item,
1623                           gpointer     user_data)
1624 {
1625   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1626   
1627   remove_selected_from_list (impl);
1628 }
1629
1630 static void
1631 show_private_toggled_cb (GtkCheckMenuItem *menu_item,
1632                          gpointer          user_data)
1633 {
1634   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1635   
1636   g_object_set (G_OBJECT (impl),
1637                 "show-private", gtk_check_menu_item_get_active (menu_item),
1638                 NULL);
1639 }
1640
1641 static void
1642 recent_popup_menu_detach_cb (GtkWidget *attach_widget,
1643                              GtkMenu   *menu)
1644 {
1645   GtkRecentChooserDefault *impl;
1646   
1647   impl = g_object_get_data (G_OBJECT (attach_widget), "GtkRecentChooserDefault");
1648   g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
1649   
1650   impl->recent_popup_menu = NULL;
1651   impl->recent_popup_menu_remove_item = NULL;
1652   impl->recent_popup_menu_copy_item = NULL;
1653   impl->recent_popup_menu_clear_item = NULL;
1654   impl->recent_popup_menu_show_private_item = NULL;
1655 }
1656
1657 static void
1658 recent_view_menu_ensure_state (GtkRecentChooserDefault *impl)
1659 {
1660   gint count;
1661   
1662   g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
1663   g_assert (impl->recent_popup_menu != NULL);
1664
1665   if (!impl->manager)
1666     count = 0;
1667   else
1668     g_object_get (G_OBJECT (impl->manager), "size", &count, NULL);
1669
1670   if (count == 0)
1671     {
1672       gtk_widget_set_sensitive (impl->recent_popup_menu_remove_item, FALSE);
1673       gtk_widget_set_sensitive (impl->recent_popup_menu_copy_item, FALSE);
1674       gtk_widget_set_sensitive (impl->recent_popup_menu_clear_item, FALSE);
1675       gtk_widget_set_sensitive (impl->recent_popup_menu_show_private_item, FALSE);
1676     }
1677 }
1678
1679 static void
1680 recent_view_menu_build (GtkRecentChooserDefault *impl)
1681 {
1682   GtkWidget *item;
1683   
1684   if (impl->recent_popup_menu)
1685     {
1686       recent_view_menu_ensure_state (impl);
1687       
1688       return;
1689     }
1690   
1691   impl->recent_popup_menu = gtk_menu_new ();
1692   gtk_menu_attach_to_widget (GTK_MENU (impl->recent_popup_menu),
1693                              impl->recent_view,
1694                              recent_popup_menu_detach_cb);
1695   
1696   item = gtk_image_menu_item_new_with_mnemonic (_("Copy _Location"));
1697   impl->recent_popup_menu_copy_item = item;
1698   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1699                                  gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU));
1700   g_signal_connect (item, "activate",
1701                     G_CALLBACK (copy_activated_cb), impl);
1702   gtk_widget_show (item);
1703   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1704
1705   item = gtk_separator_menu_item_new ();
1706   gtk_widget_show (item);
1707   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1708   
1709   item = gtk_image_menu_item_new_with_mnemonic (_("_Remove From List"));
1710   impl->recent_popup_menu_remove_item = item;
1711   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1712                                  gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
1713   g_signal_connect (item, "activate",
1714                     G_CALLBACK (remove_item_activated_cb), impl);
1715   gtk_widget_show (item);
1716   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1717
1718   item = gtk_image_menu_item_new_with_mnemonic (_("_Clear List"));
1719   impl->recent_popup_menu_clear_item = item;
1720   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1721                                  gtk_image_new_from_stock (GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU));
1722   g_signal_connect (item, "activate",
1723                     G_CALLBACK (remove_all_activated_cb), impl);
1724   
1725   gtk_widget_show (item);
1726   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1727   
1728   item = gtk_separator_menu_item_new ();
1729   gtk_widget_show (item);
1730   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1731   
1732   item = gtk_check_menu_item_new_with_mnemonic (_("Show _Private Resources"));
1733   impl->recent_popup_menu_show_private_item = item;
1734   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), impl->show_private);
1735   g_signal_connect (item, "toggled",
1736                     G_CALLBACK (show_private_toggled_cb), impl);
1737   gtk_widget_show (item);
1738   gtk_menu_shell_append (GTK_MENU_SHELL (impl->recent_popup_menu), item);
1739   
1740   recent_view_menu_ensure_state (impl);
1741 }
1742
1743 /* taken from gtkfilechooserdefault.c */
1744 static void
1745 popup_position_func (GtkMenu   *menu,
1746                      gint      *x,
1747                      gint      *y,
1748                      gboolean  *push_in,
1749                      gpointer   user_data)
1750 {
1751   GtkWidget *widget = GTK_WIDGET (user_data);
1752   GdkScreen *screen = gtk_widget_get_screen (widget);
1753   GtkRequisition req;
1754   gint monitor_num;
1755   GdkRectangle monitor;
1756
1757   if (G_UNLIKELY (!GTK_WIDGET_REALIZED (widget)))
1758     return;
1759
1760   gdk_window_get_origin (widget->window, x, y);
1761
1762   gtk_widget_size_request (GTK_WIDGET (menu), &req);
1763
1764   *x += (widget->allocation.width - req.width) / 2;
1765   *y += (widget->allocation.height - req.height) / 2;
1766
1767   monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
1768   gtk_menu_set_monitor (menu, monitor_num);
1769   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1770
1771   *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
1772   *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
1773
1774   *push_in = FALSE;
1775 }
1776
1777
1778 static void
1779 recent_view_menu_popup (GtkRecentChooserDefault *impl,
1780                         GdkEventButton          *event)
1781 {
1782   recent_view_menu_build (impl);
1783   
1784   if (event)
1785     gtk_menu_popup (GTK_MENU (impl->recent_popup_menu),
1786                     NULL, NULL, NULL, NULL,
1787                     event->button, event->time);
1788   else
1789     {
1790       gtk_menu_popup (GTK_MENU (impl->recent_popup_menu),
1791                       NULL, NULL,
1792                       popup_position_func, impl->recent_view,
1793                       0, GDK_CURRENT_TIME);
1794       gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->recent_popup_menu),
1795                                    FALSE);
1796     }
1797 }
1798
1799 static gboolean
1800 recent_view_popup_menu_cb (GtkWidget *widget,
1801                            gpointer   user_data)
1802 {
1803   recent_view_menu_popup (GTK_RECENT_CHOOSER_DEFAULT (user_data), NULL);
1804   return TRUE;
1805 }
1806
1807 static gboolean
1808 recent_view_button_press_cb (GtkWidget      *widget,
1809                              GdkEventButton *event,
1810                              gpointer        user_data)
1811 {
1812   GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
1813   
1814   if (event->button == 3)
1815     {
1816       GtkTreePath *path;
1817       gboolean res;
1818
1819       if (event->window != gtk_tree_view_get_bin_window (GTK_TREE_VIEW (impl->recent_view)))
1820         return FALSE;
1821
1822       res = gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (impl->recent_view),
1823                                            event->x, event->y,
1824                                            &path,
1825                                            NULL, NULL, NULL);
1826       if (!res)
1827         return FALSE;
1828
1829       /* select the path before creating the popup menu */
1830       gtk_tree_selection_select_path (impl->selection, path);
1831       gtk_tree_path_free (path);
1832       
1833       recent_view_menu_popup (impl, event);
1834
1835       return TRUE;
1836     }
1837   
1838   return FALSE;
1839 }
1840
1841 static void
1842 set_recent_manager (GtkRecentChooserDefault *impl,
1843                     GtkRecentManager        *manager)
1844 {
1845   if (impl->manager)
1846     {
1847       g_signal_handler_disconnect (impl, impl->manager_changed_id);
1848       impl->manager_changed_id = 0;
1849
1850       impl->manager = NULL;
1851     }
1852   
1853   if (manager)
1854     impl->manager = manager;
1855   else
1856     impl->manager = gtk_recent_manager_get_default ();
1857   
1858   if (impl->manager)
1859     impl->manager_changed_id = g_signal_connect (impl->manager, "changed",
1860                                                  G_CALLBACK (recent_manager_changed_cb),
1861                                                  impl);
1862 }
1863
1864 GtkWidget *
1865 _gtk_recent_chooser_default_new (GtkRecentManager *manager)
1866 {
1867   return g_object_new (GTK_TYPE_RECENT_CHOOSER_DEFAULT,
1868                        "recent-manager", manager,
1869                        NULL);
1870 }