gtkrecentchooserdefault.c 56.6 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3
 * gtkrecentchooserdefault.c
 * Copyright (C) 2005-2006, Emmanuele Bassi
4
 *
5 6 7 8 9 10 11 12 13 14 15
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardon's avatar
Javier Jardon committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 */

#include "config.h"

#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "gtkicontheme.h"
#include "gtksettings.h"
#include "gtktreeview.h"
#include "gtkliststore.h"
#include "gtkbutton.h"
#include "gtkcelllayout.h"
#include "gtkcellrendererpixbuf.h"
#include "gtkcellrenderertext.h"
#include "gtkcheckmenuitem.h"
#include "gtkclipboard.h"
40
#include "gtkcomboboxtext.h"
41 42 43 44
#include "gtkentry.h"
#include "gtkeventbox.h"
#include "gtkexpander.h"
#include "gtkframe.h"
Matthias Clasen's avatar
Matthias Clasen committed
45 46
#include "gtkbox.h"
#include "gtkpaned.h"
47 48 49 50 51 52 53 54
#include "gtkimage.h"
#include "gtkintl.h"
#include "gtklabel.h"
#include "gtkmenuitem.h"
#include "gtkmessagedialog.h"
#include "gtkscrolledwindow.h"
#include "gtkseparatormenuitem.h"
#include "gtksizegroup.h"
55
#include "gtksizerequest.h"
56 57 58 59
#include "gtktreemodelsort.h"
#include "gtktreemodelfilter.h"
#include "gtktreeselection.h"
#include "gtktreestore.h"
60
#include "gtktooltip.h"
61
#include "gtktypebuiltins.h"
62
#include "gtkorientable.h"
63
#include "gtkwindowgroup.h"
64
#include "deprecated/gtkactivatable.h"
65 66 67 68 69 70 71 72

#include "gtkrecentmanager.h"
#include "gtkrecentfilter.h"
#include "gtkrecentchooser.h"
#include "gtkrecentchooserprivate.h"
#include "gtkrecentchooserutils.h"
#include "gtkrecentchooserdefault.h"

73

74 75 76 77 78 79 80 81 82
enum 
{
  PROP_0,

  /* activatable properties */
  PROP_ACTIVATABLE_RELATED_ACTION,
  PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
};

83
typedef struct
84 85 86 87
{
  GtkRecentManager *manager;
  gulong manager_changed_id;
  guint local_manager : 1;
88

89 90 91
  gint icon_size;

  /* RecentChooser properties */
92
  gint limit;
93 94 95 96 97 98 99
  GtkRecentSortType sort_type;
  guint show_private : 1;
  guint show_not_found : 1;
  guint select_multiple : 1;
  guint show_tips : 1;
  guint show_icons : 1;
  guint local_only : 1;
100 101

  guint limit_set : 1;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  
  GSList *filters;
  GtkRecentFilter *current_filter;
  GtkWidget *filter_combo_hbox;
  GtkWidget *filter_combo;
  
  GtkRecentSortFunc sort_func;
  gpointer sort_data;
  GDestroyNotify sort_data_destroy;

  GtkIconTheme *icon_theme;
  
  GtkWidget *recent_view;
  GtkListStore *recent_store;
  GtkTreeViewColumn *icon_column;
  GtkTreeViewColumn *meta_column;
118
  GtkCellRenderer *icon_renderer;
119 120 121 122 123 124 125 126 127 128 129 130 131 132
  GtkCellRenderer *meta_renderer;
  GtkTreeSelection *selection;
  
  GtkWidget *recent_popup_menu;
  GtkWidget *recent_popup_menu_copy_item;
  GtkWidget *recent_popup_menu_remove_item;
  GtkWidget *recent_popup_menu_clear_item;
  GtkWidget *recent_popup_menu_show_private_item;
 
  guint load_id;
  GList *recent_items;
  gint n_recent_items;
  gint loaded_items;
  guint load_state;
133 134 135 136 137 138 139
} GtkRecentChooserDefaultPrivate;

struct _GtkRecentChooserDefault
{
  GtkBox parent_instance;

  GtkRecentChooserDefaultPrivate *priv;
140 141 142 143
};

typedef struct _GtkRecentChooserDefaultClass
{
144
  GtkBoxClass parent_class;
145 146
} GtkRecentChooserDefaultClass;

147
/* Keep inline with GtkTreeStore defined in gtkrecentchooserdefault.ui */
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
enum {
  RECENT_URI_COLUMN,
  RECENT_DISPLAY_NAME_COLUMN,
  RECENT_INFO_COLUMN,
  N_RECENT_COLUMNS
};

enum {
  LOAD_EMPTY,    /* initial state: the model is empty */
  LOAD_PRELOAD,  /* the model is loading and not inserted in the tree yet */
  LOAD_LOADING,  /* the model is fully loaded but not inserted */
  LOAD_FINISHED  /* the model is fully loaded and inserted */
};

/* Icon size for if we can't get it from the theme */
#define FALLBACK_ICON_SIZE  48
#define FALLBACK_ITEM_LIMIT 20

#define NUM_CHARS 40
#define NUM_LINES 9

169 170
#define DEFAULT_RECENT_FILES_LIMIT 50

171 172 173


/* GObject */
174 175
static void     _gtk_recent_chooser_default_class_init  (GtkRecentChooserDefaultClass *klass);
static void     _gtk_recent_chooser_default_init        (GtkRecentChooserDefault      *impl);
176
static void     gtk_recent_chooser_default_finalize     (GObject                      *object);
177
static void     gtk_recent_chooser_default_dispose      (GObject                      *object);
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static void     gtk_recent_chooser_default_set_property (GObject                      *object,
						         guint                         prop_id,
						         const GValue                 *value,
						         GParamSpec                   *pspec);
static void     gtk_recent_chooser_default_get_property (GObject                      *object,
						         guint                         prop_id,
						         GValue                       *value,
						         GParamSpec                   *pspec);

/* GtkRecentChooserIface */
static void              gtk_recent_chooser_iface_init                 (GtkRecentChooserIface  *iface);
static gboolean          gtk_recent_chooser_default_set_current_uri    (GtkRecentChooser       *chooser,
								        const gchar            *uri,
								        GError                **error);
static gchar *           gtk_recent_chooser_default_get_current_uri    (GtkRecentChooser       *chooser);
static gboolean          gtk_recent_chooser_default_select_uri         (GtkRecentChooser       *chooser,
								        const gchar            *uri,
								        GError                **error);
static void              gtk_recent_chooser_default_unselect_uri       (GtkRecentChooser       *chooser,
								        const gchar            *uri);
static void              gtk_recent_chooser_default_select_all         (GtkRecentChooser       *chooser);
static void              gtk_recent_chooser_default_unselect_all       (GtkRecentChooser       *chooser);
static GList *           gtk_recent_chooser_default_get_items          (GtkRecentChooser       *chooser);
static GtkRecentManager *gtk_recent_chooser_default_get_recent_manager (GtkRecentChooser       *chooser);
static void              gtk_recent_chooser_default_set_sort_func      (GtkRecentChooser       *chooser,
									GtkRecentSortFunc       sort_func,
									gpointer                sort_data,
									GDestroyNotify          data_destroy);
static void              gtk_recent_chooser_default_add_filter         (GtkRecentChooser       *chooser,
								        GtkRecentFilter        *filter);
static void              gtk_recent_chooser_default_remove_filter      (GtkRecentChooser       *chooser,
								        GtkRecentFilter        *filter);
static GSList *          gtk_recent_chooser_default_list_filters       (GtkRecentChooser       *chooser);


static void gtk_recent_chooser_default_map      (GtkWidget *widget);
static void gtk_recent_chooser_default_show_all (GtkWidget *widget);

static void set_current_filter        (GtkRecentChooserDefault *impl,
				       GtkRecentFilter         *filter);

static GtkIconTheme *get_icon_theme_for_widget (GtkWidget   *widget);
static gint          get_icon_size_for_widget  (GtkWidget   *widget,
						GtkIconSize  icon_size);

static void reload_recent_items (GtkRecentChooserDefault *impl);
static void chooser_set_model   (GtkRecentChooserDefault *impl);

static void set_recent_manager (GtkRecentChooserDefault *impl,
				GtkRecentManager        *manager);

static void chooser_set_sort_type (GtkRecentChooserDefault *impl,
				   GtkRecentSortType        sort_type);

static void recent_manager_changed_cb (GtkRecentManager  *manager,
			               gpointer           user_data);
static void recent_icon_data_func     (GtkTreeViewColumn *tree_column,
				       GtkCellRenderer   *cell,
				       GtkTreeModel      *model,
				       GtkTreeIter       *iter,
				       gpointer           user_data);
static void recent_meta_data_func     (GtkTreeViewColumn *tree_column,
				       GtkCellRenderer   *cell,
				       GtkTreeModel      *model,
				       GtkTreeIter       *iter,
				       gpointer           user_data);

static void selection_changed_cb      (GtkTreeSelection  *z,
				       gpointer           user_data);
static void row_activated_cb          (GtkTreeView       *tree_view,
				       GtkTreePath       *tree_path,
				       GtkTreeViewColumn *tree_column,
				       gpointer           user_data);
static void filter_combo_changed_cb   (GtkComboBox       *combo_box,
				       gpointer           user_data);

static void remove_all_activated_cb   (GtkMenuItem       *menu_item,
				       gpointer           user_data);
static void remove_item_activated_cb  (GtkMenuItem       *menu_item,
				       gpointer           user_data);
static void show_private_toggled_cb   (GtkCheckMenuItem  *menu_item,
				       gpointer           user_data);

static gboolean recent_view_popup_menu_cb   (GtkWidget      *widget,
					     gpointer        user_data);
static gboolean recent_view_button_press_cb (GtkWidget      *widget,
					     GdkEventButton *event,
					     gpointer        user_data);

static void     recent_view_drag_begin_cb         (GtkWidget        *widget,
						   GdkDragContext   *context,
						   gpointer          user_data);
static void     recent_view_drag_data_get_cb      (GtkWidget        *widget,
						   GdkDragContext   *context,
						   GtkSelectionData *selection_data,
						   guint             info,
						   guint32           time_,
						   gpointer          data);
276 277 278 279 280 281 282
static gboolean recent_view_query_tooltip_cb      (GtkWidget        *widget,
                                                   gint              x,
                                                   gint              y,
                                                   gboolean          keyboard_tip,
                                                   GtkTooltip       *tooltip,
                                                   gpointer          user_data);

283
static void gtk_recent_chooser_activatable_iface_init (GtkActivatableIface  *iface);
284
static void gtk_recent_chooser_update                 (GtkActivatable       *activatable,
285 286
						       GtkAction            *action,
						       const gchar          *property_name);
287
static void gtk_recent_chooser_sync_action_properties (GtkActivatable       *activatable,
288
						       GtkAction            *action);
289

290
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
291
G_DEFINE_TYPE_WITH_CODE (GtkRecentChooserDefault,
292
			 _gtk_recent_chooser_default,
293
			 GTK_TYPE_BOX,
294
                         G_ADD_PRIVATE (GtkRecentChooserDefault)
295
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_RECENT_CHOOSER,
296 297 298
				 		gtk_recent_chooser_iface_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
				 		gtk_recent_chooser_activatable_iface_init))
299
G_GNUC_END_IGNORE_DEPRECATIONS;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319



static void
gtk_recent_chooser_iface_init (GtkRecentChooserIface *iface)
{
  iface->set_current_uri = gtk_recent_chooser_default_set_current_uri;
  iface->get_current_uri = gtk_recent_chooser_default_get_current_uri;
  iface->select_uri = gtk_recent_chooser_default_select_uri;
  iface->unselect_uri = gtk_recent_chooser_default_unselect_uri;
  iface->select_all = gtk_recent_chooser_default_select_all;
  iface->unselect_all = gtk_recent_chooser_default_unselect_all;
  iface->get_items = gtk_recent_chooser_default_get_items;
  iface->get_recent_manager = gtk_recent_chooser_default_get_recent_manager;
  iface->set_sort_func = gtk_recent_chooser_default_set_sort_func;
  iface->add_filter = gtk_recent_chooser_default_add_filter;
  iface->remove_filter = gtk_recent_chooser_default_remove_filter;
  iface->list_filters = gtk_recent_chooser_default_list_filters;
}

320 321
static void
gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface)
322

323 324 325
{
  iface->update = gtk_recent_chooser_update;
  iface->sync_action_properties = gtk_recent_chooser_sync_action_properties;
326 327
}

328
static void
329
_gtk_recent_chooser_default_class_init (GtkRecentChooserDefaultClass *klass)
330 331 332 333 334 335
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gobject_class->set_property = gtk_recent_chooser_default_set_property;
  gobject_class->get_property = gtk_recent_chooser_default_get_property;
336 337
  gobject_class->dispose = gtk_recent_chooser_default_dispose;
  gobject_class->finalize = gtk_recent_chooser_default_finalize;
338 339 340 341 342
  
  widget_class->map = gtk_recent_chooser_default_map;
  widget_class->show_all = gtk_recent_chooser_default_show_all;
  
  _gtk_recent_chooser_install_properties (gobject_class);
343 344 345

  g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
  g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
346 347 348 349

  /* Bind class to template
   */
  gtk_widget_class_set_template_from_resource (widget_class,
350
					       "/org/gtk/libgtk/ui/gtkrecentchooserdefault.ui");
351

352 353 354 355 356 357 358 359 360
  gtk_widget_class_bind_template_child_private (widget_class, GtkRecentChooserDefault, filter_combo_hbox);
  gtk_widget_class_bind_template_child_private (widget_class, GtkRecentChooserDefault, filter_combo);
  gtk_widget_class_bind_template_child_private (widget_class, GtkRecentChooserDefault, recent_view);
  gtk_widget_class_bind_template_child_private (widget_class, GtkRecentChooserDefault, recent_store);
  gtk_widget_class_bind_template_child_private (widget_class, GtkRecentChooserDefault, icon_column);
  gtk_widget_class_bind_template_child_private (widget_class, GtkRecentChooserDefault, meta_column);
  gtk_widget_class_bind_template_child_private (widget_class, GtkRecentChooserDefault, icon_renderer);
  gtk_widget_class_bind_template_child_private (widget_class, GtkRecentChooserDefault, meta_renderer);
  gtk_widget_class_bind_template_child_private (widget_class, GtkRecentChooserDefault, selection);
361 362 363 364 365 366 367 368 369

  gtk_widget_class_bind_template_callback (widget_class, selection_changed_cb);
  gtk_widget_class_bind_template_callback (widget_class, row_activated_cb);
  gtk_widget_class_bind_template_callback (widget_class, filter_combo_changed_cb);
  gtk_widget_class_bind_template_callback (widget_class, recent_view_popup_menu_cb);
  gtk_widget_class_bind_template_callback (widget_class, recent_view_button_press_cb);
  gtk_widget_class_bind_template_callback (widget_class, recent_view_drag_begin_cb);
  gtk_widget_class_bind_template_callback (widget_class, recent_view_drag_data_get_cb);
  gtk_widget_class_bind_template_callback (widget_class, recent_view_query_tooltip_cb);
370 371 372
}

static void
373
_gtk_recent_chooser_default_init (GtkRecentChooserDefault *impl)
374
{
375
  GtkRecentChooserDefaultPrivate *priv;
376

377
  impl->priv = priv = _gtk_recent_chooser_default_get_instance_private (impl);
378

379
  /* by default, we use the global manager */
380
  priv->local_manager = FALSE;
381

382 383
  priv->limit = FALLBACK_ITEM_LIMIT;
  priv->sort_type = GTK_RECENT_SORT_NONE;
384

385 386 387 388 389 390
  priv->show_icons = TRUE;
  priv->show_private = FALSE;
  priv->show_not_found = TRUE;
  priv->show_tips = FALSE;
  priv->select_multiple = FALSE;
  priv->local_only = TRUE;
391
  
392 393
  priv->icon_size = FALLBACK_ICON_SIZE;
  priv->icon_theme = NULL;
394
  
395
  priv->current_filter = NULL;
396

397 398 399
  priv->recent_items = NULL;
  priv->n_recent_items = 0;
  priv->loaded_items = 0;
400
  
401
  priv->load_state = LOAD_EMPTY;
402

403 404 405
  gtk_widget_init_template (GTK_WIDGET (impl));

  g_object_set_data (G_OBJECT (priv->recent_view),
406
                     "GtkRecentChooserDefault", impl);
407 408 409

  gtk_tree_view_column_set_cell_data_func (priv->icon_column,
  					   priv->icon_renderer,
410 411 412
  					   recent_icon_data_func,
  					   impl,
  					   NULL);
413 414
  gtk_tree_view_column_set_cell_data_func (priv->meta_column,
  					   priv->meta_renderer,
415 416 417
  					   recent_meta_data_func,
  					   impl,
  					   NULL);
418
  gtk_drag_source_set (priv->recent_view,
419
		       GDK_BUTTON1_MASK,
420
		       NULL, 0,
421
		       GDK_ACTION_COPY);
422
  gtk_drag_source_add_uri_targets (priv->recent_view);
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
}

static void
gtk_recent_chooser_default_set_property (GObject      *object,
				         guint         prop_id,
					 const GValue *value,
					 GParamSpec   *pspec)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
  
  switch (prop_id)
    {
    case GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER:
      set_recent_manager (impl, g_value_get_object (value));
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
439 440 441 442 443 444 445 446 447 448 449 450
      if (impl->priv->show_private != g_value_get_boolean (value))
        {
          impl->priv->show_private = g_value_get_boolean (value);
          if (impl->priv->recent_popup_menu_show_private_item)
            {
              GtkCheckMenuItem *item = GTK_CHECK_MENU_ITEM (impl->priv->recent_popup_menu_show_private_item);
              g_signal_handlers_block_by_func (item, G_CALLBACK (show_private_toggled_cb), impl);
              gtk_check_menu_item_set_active (item, impl->priv->show_private);
              g_signal_handlers_unblock_by_func (item, G_CALLBACK (show_private_toggled_cb), impl);
            }
          reload_recent_items (impl);
          g_object_notify_by_pspec (object, pspec);
451 452 453
        }
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
454 455 456 457 458 459
      if (impl->priv->show_not_found != g_value_get_boolean (value))
        {
          impl->priv->show_not_found = g_value_get_boolean (value);
          reload_recent_items (impl);
          g_object_notify_by_pspec (object, pspec);
        }
460 461
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
462 463 464 465 466
      if (impl->priv->show_tips != g_value_get_boolean (value))
        {
          impl->priv->show_tips = g_value_get_boolean (value);
          g_object_notify_by_pspec (object, pspec);
        }
467 468
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
469 470 471 472 473 474
      if (impl->priv->show_icons != g_value_get_boolean (value))
        {
          impl->priv->show_icons = g_value_get_boolean (value);
          gtk_tree_view_column_set_visible (impl->priv->icon_column, impl->priv->show_icons);
          g_object_notify_by_pspec (object, pspec);
        }
475 476
      break;
    case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
477 478 479 480 481 482 483 484 485
      if (impl->priv->select_multiple != g_value_get_boolean (value))
        {
          impl->priv->select_multiple = g_value_get_boolean (value);
          if (impl->priv->select_multiple)
            gtk_tree_selection_set_mode (impl->priv->selection, GTK_SELECTION_MULTIPLE);
          else
            gtk_tree_selection_set_mode (impl->priv->selection, GTK_SELECTION_SINGLE);
          g_object_notify_by_pspec (object, pspec);
        }
486 487
      break;
    case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
488 489 490 491 492 493
      if (impl->priv->local_only != g_value_get_boolean (value))
        {
          impl->priv->local_only = g_value_get_boolean (value);
          reload_recent_items (impl);
          g_object_notify_by_pspec (object, pspec);
        }
494 495
      break;
    case GTK_RECENT_CHOOSER_PROP_LIMIT:
496 497 498 499 500 501 502
      if (impl->priv->limit != g_value_get_int (value))
        {
          impl->priv->limit = g_value_get_int (value);
          impl->priv->limit_set = TRUE;
          reload_recent_items (impl);
          g_object_notify_by_pspec (object, pspec);
        }
503 504 505 506 507 508 509
      break;
    case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
      chooser_set_sort_type (impl, g_value_get_enum (value));
      break;
    case GTK_RECENT_CHOOSER_PROP_FILTER:
      set_current_filter (impl, g_value_get_object (value));
      break;
510 511 512 513 514 515
    case PROP_ACTIVATABLE_RELATED_ACTION:
      _gtk_recent_chooser_set_related_action (GTK_RECENT_CHOOSER (impl), g_value_get_object (value));
      break;
    case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE: 
      _gtk_recent_chooser_set_use_action_appearance (GTK_RECENT_CHOOSER (impl), g_value_get_boolean (value));
      break;
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_recent_chooser_default_get_property (GObject    *object,
					 guint       prop_id,
					 GValue     *value,
					 GParamSpec *pspec)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);
  
  switch (prop_id)
    {
    case GTK_RECENT_CHOOSER_PROP_LIMIT:
533
      g_value_set_int (value, impl->priv->limit);
534 535
      break;
    case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
536
      g_value_set_enum (value, impl->priv->sort_type);
537 538
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
539
      g_value_set_boolean (value, impl->priv->show_private);
540 541
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
542
      g_value_set_boolean (value, impl->priv->show_icons);
543 544
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
545
      g_value_set_boolean (value, impl->priv->show_not_found);
546 547
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
548
      g_value_set_boolean (value, impl->priv->show_tips);
549 550
      break;
    case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
551
      g_value_set_boolean (value, impl->priv->local_only);
552 553
      break;
    case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
554
      g_value_set_boolean (value, impl->priv->select_multiple);
555 556
      break;
    case GTK_RECENT_CHOOSER_PROP_FILTER:
557
      g_value_set_object (value, impl->priv->current_filter);
558
      break;
559 560 561 562 563 564
    case PROP_ACTIVATABLE_RELATED_ACTION:
      g_value_set_object (value, _gtk_recent_chooser_get_related_action (GTK_RECENT_CHOOSER (impl)));
      break;
    case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE: 
      g_value_set_boolean (value, _gtk_recent_chooser_get_use_action_appearance (GTK_RECENT_CHOOSER (impl)));
      break;
565 566 567 568 569 570 571
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
572
gtk_recent_chooser_default_dispose (GObject *object)
573 574 575
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);

576
  if (impl->priv->load_id)
577
    {
578 579 580
      g_source_remove (impl->priv->load_id);
      impl->priv->load_state = LOAD_EMPTY;
      impl->priv->load_id = 0;
581 582
    }

583 584
  g_list_free_full (impl->priv->recent_items, (GDestroyNotify) gtk_recent_info_unref);
  impl->priv->recent_items = NULL;
585

586
  if (impl->priv->manager && impl->priv->manager_changed_id)
587
    {
588 589
      g_signal_handler_disconnect (impl->priv->manager, impl->priv->manager_changed_id);
      impl->priv->manager_changed_id = 0;
590 591
    }

592
  if (impl->priv->filters)
593
    {
594 595 596
      g_slist_foreach (impl->priv->filters, (GFunc) g_object_unref, NULL);
      g_slist_free (impl->priv->filters);
      impl->priv->filters = NULL;
597 598
    }
  
599
  if (impl->priv->current_filter)
600
    {
601 602
      g_object_unref (impl->priv->current_filter);
      impl->priv->current_filter = NULL;
603
    }
604

605 606 607 608 609 610 611 612
  G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->dispose (object);
}

static void
gtk_recent_chooser_default_finalize (GObject *object)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);

613
  impl->priv->manager = NULL; 
614
  
615
  if (impl->priv->sort_data_destroy)
616
    {
617 618
      impl->priv->sort_data_destroy (impl->priv->sort_data);
      impl->priv->sort_data_destroy = NULL;
619 620
    }
  
621 622
  impl->priv->sort_data = NULL;
  impl->priv->sort_func = NULL;
623
  
624
  G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->finalize (object);
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
}

/* override GtkWidget::show_all since we have internal widgets we wish to keep
 * hidden unless we decide otherwise, like the filter combo box.
 */
static void
gtk_recent_chooser_default_show_all (GtkWidget *widget)
{
  gtk_widget_show (widget);
}



/* Shows an error dialog set as transient for the specified window */
static void
error_message_with_parent (GtkWindow   *parent,
			   const gchar *msg,
			   const gchar *detail)
{
  GtkWidget *dialog;

  dialog = gtk_message_dialog_new (parent,
				   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				   GTK_MESSAGE_ERROR,
				   GTK_BUTTONS_OK,
				   "%s",
				   msg);
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
					    "%s", detail);

655 656 657
  if (gtk_window_has_group (parent))
    gtk_window_group_add_window (gtk_window_get_group (parent),
                                 GTK_WINDOW (dialog));
658 659 660 661 662 663 664 665 666 667 668 669

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

/* Returns a toplevel GtkWindow, or NULL if none */
static GtkWindow *
get_toplevel (GtkWidget *widget)
{
  GtkWidget *toplevel;

  toplevel = gtk_widget_get_toplevel (widget);
670
  if (!gtk_widget_is_toplevel (toplevel))
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
    return NULL;
  else
    return GTK_WINDOW (toplevel);
}

/* Shows an error dialog for the file chooser */
static void
error_message (GtkRecentChooserDefault *impl,
	       const gchar             *msg,
	       const gchar             *detail)
{
  error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
}

static void
set_busy_cursor (GtkRecentChooserDefault *impl,
		 gboolean                 show_busy_cursor)
{
  GtkWindow *toplevel;
  GdkDisplay *display;
  GdkCursor *cursor;

  toplevel = get_toplevel (GTK_WIDGET (impl));
694
  if (!toplevel || !gtk_widget_get_realized (GTK_WIDGET (toplevel)))
695 696 697 698 699 700 701 702
    return;
  
  display = gtk_widget_get_display (GTK_WIDGET (toplevel));
  
  cursor = NULL;
  if (show_busy_cursor)
    cursor = gdk_cursor_new_for_display (display, GDK_WATCH);

703 704
  gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (toplevel)),
                         cursor);
705 706 707
  gdk_display_flush (display);

  if (cursor)
708
    g_object_unref (cursor);
709 710 711 712 713
}

static void
chooser_set_model (GtkRecentChooserDefault *impl)
{
714 715
  g_assert (impl->priv->recent_store != NULL);
  g_assert (impl->priv->load_state == LOAD_LOADING);
716

717 718 719 720 721
  gtk_tree_view_set_model (GTK_TREE_VIEW (impl->priv->recent_view),
                           GTK_TREE_MODEL (impl->priv->recent_store));
  gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->priv->recent_view));
  gtk_tree_view_set_enable_search (GTK_TREE_VIEW (impl->priv->recent_view), TRUE);
  gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->priv->recent_view),
722 723
  				   RECENT_DISPLAY_NAME_COLUMN);

724
  impl->priv->load_state = LOAD_FINISHED;
725 726 727 728 729 730 731 732 733 734 735 736 737
}

static gboolean
load_recent_items (gpointer user_data)
{
  GtkRecentChooserDefault *impl;
  GtkRecentInfo *info;
  GtkTreeIter iter;
  const gchar *uri, *name;
  gboolean retval;
  
  impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  
738 739
  g_assert ((impl->priv->load_state == LOAD_EMPTY) ||
            (impl->priv->load_state == LOAD_PRELOAD));
740 741
  
  /* store the items for multiple runs */
742
  if (!impl->priv->recent_items)
743
    {
744 745
      impl->priv->recent_items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (impl));
      if (!impl->priv->recent_items)
746
        {
747
	  impl->priv->load_state = LOAD_FINISHED;
748
          impl->priv->load_id = 0;
749 750 751
          return FALSE;
        }
        
752 753 754
      impl->priv->n_recent_items = g_list_length (impl->priv->recent_items);
      impl->priv->loaded_items = 0;
      impl->priv->load_state = LOAD_PRELOAD;
755 756
    }
  
757 758
  info = (GtkRecentInfo *) g_list_nth_data (impl->priv->recent_items,
                                            impl->priv->loaded_items);
759 760 761 762 763 764 765 766 767 768
  g_assert (info);

  uri = gtk_recent_info_get_uri (info);
  name = gtk_recent_info_get_display_name (info);
  
  /* at this point, everything goes inside the model; operations on the
   * visualization of items inside the model are done in the cell data
   * funcs (remember that there are two of those: one for the icon and
   * one for the text), while the filtering is done only when a filter
   * is actually loaded. */
769 770
  gtk_list_store_append (impl->priv->recent_store, &iter);
  gtk_list_store_set (impl->priv->recent_store, &iter,
771 772 773 774 775
  		      RECENT_URI_COLUMN, uri,           /* uri  */
  		      RECENT_DISPLAY_NAME_COLUMN, name, /* display_name */
  		      RECENT_INFO_COLUMN, info,         /* info */
  		      -1);
  
776
  impl->priv->loaded_items += 1;
777

778
  if (impl->priv->loaded_items == impl->priv->n_recent_items)
779 780
    {
      /* we have finished loading, so we remove the items cache */
781
      impl->priv->load_state = LOAD_LOADING;
782
      
783
      g_list_free_full (impl->priv->recent_items, (GDestroyNotify) gtk_recent_info_unref);
784
      
785 786 787
      impl->priv->recent_items = NULL;
      impl->priv->n_recent_items = 0;
      impl->priv->loaded_items = 0;
788

789 790 791 792
      /* load the filled up model */
      chooser_set_model (impl);

      retval = FALSE;
793
      impl->priv->load_id = 0;
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
    }
  else
    {
      /* we did not finish, so continue loading */
      retval = TRUE;
    }
  
  return retval;
}

static void
cleanup_after_load (gpointer user_data)
{
  GtkRecentChooserDefault *impl;
  
  impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);

811
  if (impl->priv->load_id != 0)
812
    {
813 814 815 816
      g_assert ((impl->priv->load_state == LOAD_EMPTY) ||
                (impl->priv->load_state == LOAD_PRELOAD) ||
		(impl->priv->load_state == LOAD_LOADING) ||
		(impl->priv->load_state == LOAD_FINISHED));
817 818 819 820
      
      /* we have officialy finished loading all the items,
       * so we can reset the state machine
       */
821 822
      impl->priv->load_id = 0;
      impl->priv->load_state = LOAD_EMPTY;
823 824
    }
  else
825 826 827
    g_assert ((impl->priv->load_state == LOAD_EMPTY) ||
	      (impl->priv->load_state == LOAD_LOADING) ||
	      (impl->priv->load_state == LOAD_FINISHED));
828 829 830 831 832 833 834 835

  set_busy_cursor (impl, FALSE);
}

/* clears the current model and reloads the recently used resources */
static void
reload_recent_items (GtkRecentChooserDefault *impl)
{
836 837
  GtkWidget *widget;

838
  /* reload is already in progress - do not disturb */
839
  if (impl->priv->load_id)
840 841
    return;
  
842 843
  widget = GTK_WIDGET (impl);

844 845
  gtk_tree_view_set_model (GTK_TREE_VIEW (impl->priv->recent_view), NULL);
  gtk_list_store_clear (impl->priv->recent_store);
846
  
847 848
  if (!impl->priv->icon_theme)
    impl->priv->icon_theme = get_icon_theme_for_widget (widget);
849

850
  impl->priv->icon_size = get_icon_size_for_widget (widget,
851 852
		  			      GTK_ICON_SIZE_BUTTON);

853
  if (!impl->priv->limit_set)
854
    impl->priv->limit = DEFAULT_RECENT_FILES_LIMIT;
855

856 857
  set_busy_cursor (impl, TRUE);

858 859
  impl->priv->load_state = LOAD_EMPTY;
  impl->priv->load_id = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 30,
860 861 862
                                             load_recent_items,
                                             impl,
                                             cleanup_after_load);
863
  g_source_set_name_by_id (impl->priv->load_id, "[gtk+] load_recent_items");
864 865 866 867 868 869
}

/* taken form gtkfilechooserdialog.c */
static void
set_default_size (GtkRecentChooserDefault *impl)
{
870
  GtkScrolledWindow *scrollw;
871 872
  GtkWidget *widget;
  gint width, height;
873
  double font_size;
874 875 876 877
  GdkScreen *screen;
  gint monitor_num;
  GtkRequisition req;
  GdkRectangle monitor;
878 879
  GtkStyleContext *context;
  GtkStateFlags state;
880 881

  widget = GTK_WIDGET (impl);
882 883
  context = gtk_widget_get_style_context (widget);
  state = gtk_widget_get_state_flags (widget);
884 885

  /* Size based on characters and the icon size */
886
  gtk_style_context_get (context, state, "font-size", &font_size, NULL);
887

888 889
  width = impl->priv->icon_size + font_size * NUM_CHARS + 0.5;
  height = (impl->priv->icon_size + font_size) * NUM_LINES + 0.5;
890 891

  /* Use at least the requisition size... */
892
  gtk_widget_get_preferred_size (widget, &req, NULL);
893 894 895 896 897
  width = MAX (width, req.width);
  height = MAX (height, req.height);

  /* ... but no larger than the monitor */
  screen = gtk_widget_get_screen (widget);
898 899
  monitor_num = gdk_screen_get_monitor_at_window (screen,
                                                  gtk_widget_get_window (widget));
900

901
  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
902 903 904 905 906

  width = MIN (width, monitor.width * 3 / 4);
  height = MIN (height, monitor.height * 3 / 4);

  /* Set size */
907
  scrollw = GTK_SCROLLED_WINDOW (gtk_widget_get_parent (impl->priv->recent_view));
908 909
  gtk_scrolled_window_set_min_content_width (scrollw, width);
  gtk_scrolled_window_set_min_content_height (scrollw, height);
910 911 912 913 914 915
}

static void
gtk_recent_chooser_default_map (GtkWidget *widget)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (widget);
916 917

  GTK_WIDGET_CLASS (_gtk_recent_chooser_default_parent_class)->map (widget);
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932

  /* reloads everything */
  reload_recent_items (impl);

  set_default_size (impl);
}

static void
recent_icon_data_func (GtkTreeViewColumn *tree_column,
		       GtkCellRenderer   *cell,
		       GtkTreeModel      *model,
		       GtkTreeIter       *iter,
		       gpointer           user_data)
{
  GtkRecentInfo *info = NULL;
933 934 935
  GIcon *icon;

  gtk_tree_model_get (model, iter, RECENT_INFO_COLUMN, &info, -1);
936
  g_assert (info != NULL);
937 938 939 940 941 942

  icon = gtk_recent_info_get_gicon (info);
  g_object_set (cell, "gicon", icon, NULL);

  if (icon != NULL)
    g_object_unref (icon);
943 944

  gtk_recent_info_unref (info);
945 946 947 948 949 950 951 952 953 954
}

static void
recent_meta_data_func (GtkTreeViewColumn *tree_column,
		       GtkCellRenderer   *cell,
		       GtkTreeModel      *model,
		       GtkTreeIter       *iter,
		       gpointer           user_data)
{
  GtkRecentInfo *info = NULL;
955
  gchar *name;
956 957 958 959 960 961 962 963 964
  
  gtk_tree_model_get (model, iter,
                      RECENT_DISPLAY_NAME_COLUMN, &name,
                      RECENT_INFO_COLUMN, &info,
                      -1);
  g_assert (info != NULL);
  
  if (!name)
    name = gtk_recent_info_get_short_name (info);
965

966
  g_object_set (cell, "text", name, NULL);
967 968 969 970 971 972 973 974 975 976 977
  
  g_free (name);
  gtk_recent_info_unref (info);
}


static gchar *
gtk_recent_chooser_default_get_current_uri (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
978
  g_assert (impl->priv->selection != NULL);
979
  
980
  if (!impl->priv->select_multiple)
981 982 983 984 985
    {
      GtkTreeModel *model;
      GtkTreeIter iter;
      gchar *uri = NULL;
      
986
      if (!gtk_tree_selection_get_selected (impl->priv->selection, &model, &iter))
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
        return NULL;
      
      gtk_tree_model_get (model, &iter, RECENT_URI_COLUMN, &uri, -1);
      
      return uri;
    }
  
  return NULL;
}

typedef struct
{
  guint found : 1;
  guint do_select : 1;
  guint do_activate : 1;
  
  gchar *uri;
  
  GtkRecentChooserDefault *impl;
} SelectURIData;

static gboolean
scan_for_uri_cb (GtkTreeModel *model,
		 GtkTreePath  *path,
		 GtkTreeIter  *iter,
		 gpointer      user_data)
{
  SelectURIData *select_data = (SelectURIData *) user_data;
1015
  gchar *uri = NULL;
1016 1017 1018 1019 1020 1021 1022 1023
  
  if (!select_data)
    return TRUE;
  
  if (select_data->found)
    return TRUE;
  
  gtk_tree_model_get (model, iter, RECENT_URI_COLUMN, &uri, -1);
1024 1025 1026 1027
  if (!uri)
    return FALSE;
  
  if (strcmp (uri, select_data->uri) == 0)
1028 1029 1030 1031
    {
      select_data->found = TRUE;
      
      if (select_data->do_activate)
1032
        gtk_tree_view_row_activated (GTK_TREE_VIEW (select_data->impl->priv->recent_view),
1033
        			     path,
1034
        			     select_data->impl->priv->meta_column);
1035 1036
      
      if (select_data->do_select)
1037
        gtk_tree_selection_select_path (select_data->impl->priv->selection, path);
1038
      else
1039
        gtk_tree_selection_unselect_path (select_data->impl->priv->selection, path);
1040 1041

      g_free (uri);
1042 1043 1044
      
      return TRUE;
    }
1045 1046

  g_free (uri);
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
  
  return FALSE;
}

static gboolean
gtk_recent_chooser_default_set_current_uri (GtkRecentChooser  *chooser,
					    const gchar       *uri,
					    GError           **error)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  SelectURIData *data;
  
  data = g_new0 (SelectURIData, 1);
  data->uri = g_strdup (uri);
  data->impl = impl;
  data->found = FALSE;
  data->do_activate = TRUE;
  data->do_select = TRUE;
  
1066
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->priv->recent_store),
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
  			  scan_for_uri_cb,
  			  data);
  
  if (!data->found)
    {
      g_free (data->uri);
      g_free (data);
      
      g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
      		   GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
      		   _("No item for URI '%s' found"),
      		   uri);
      return FALSE;
    }
  
  g_free (data->uri);
  g_free (data);

  return TRUE;
}

static gboolean
gtk_recent_chooser_default_select_uri (GtkRecentChooser  *chooser,
				       const gchar       *uri,
				       GError           **error)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  SelectURIData *data;
  
  data = g_new0 (SelectURIData, 1);
  data->uri = g_strdup (uri);
  data->impl = impl;
  data->found = FALSE;
  data->do_activate = FALSE;
  data->do_select = TRUE;
  
1103
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->priv->recent_store),
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
  			  scan_for_uri_cb,
  			  data);
  
  if (!data->found)
    {
      g_free (data->uri);
      g_free (data);
      
      g_set_error (error, GTK_RECENT_CHOOSER_ERROR,
      		   GTK_RECENT_CHOOSER_ERROR_NOT_FOUND,
      		   _("No item for URI '%s' found"),
      		   uri);
      return FALSE;
    }
  
  g_free (data->uri);
  g_free (data);

  return TRUE;
}

static void
gtk_recent_chooser_default_unselect_uri (GtkRecentChooser *chooser,
					 const gchar      *uri)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  SelectURIData *data;
  
  data = g_new0 (SelectURIData, 1);
  data->uri = g_strdup (uri);
  data->impl = impl;
  data->found = FALSE;
  data->do_activate = FALSE;
  data->do_select = FALSE;
  
1139
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->priv->recent_store),
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
  			  scan_for_uri_cb,
  			  data);
  
  g_free (data->uri);
  g_free (data);
}

static void
gtk_recent_chooser_default_select_all (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
1152
  if (!impl->priv->select_multiple)
1153 1154
    return;
  
1155
  gtk_tree_selection_select_all (impl->priv->selection);
1156 1157 1158 1159 1160 1161 1162
}

static void
gtk_recent_chooser_default_unselect_all (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
1163
  gtk_tree_selection_unselect_all (impl->priv->selection);
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
}

static void
gtk_recent_chooser_default_set_sort_func (GtkRecentChooser  *chooser,
					  GtkRecentSortFunc  sort_func,
					  gpointer           sort_data,
					  GDestroyNotify     data_destroy)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
1174
  if (impl->priv->sort_data_destroy)
1175
    {
1176 1177
      impl->priv->sort_data_destroy (impl->priv->sort_data);
      impl->priv->sort_data_destroy = NULL;
1178
    }
1179
      
1180 1181
  impl->priv->sort_func = NULL;
  impl->priv->sort_data = NULL;
1182 1183 1184
  
  if (sort_func)
    {
1185 1186 1187
      impl->priv->sort_func = sort_func;
      impl->priv->sort_data = sort_data;
      impl->priv->sort_data_destroy = data_destroy;
1188 1189 1190 1191 1192 1193 1194 1195
    }
}

static GList *
gtk_recent_chooser_default_get_items (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl;

1196
  impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1197

1198
  return _gtk_recent_chooser_get_items (chooser,
1199 1200 1201
                                        impl->priv->current_filter,
                                        impl->priv->sort_func,
                                        impl->priv->sort_data);
1202 1203 1204 1205 1206
}

static GtkRecentManager *
gtk_recent_chooser_default_get_recent_manager (GtkRecentChooser *chooser)
{
1207
  return GTK_RECENT_CHOOSER_DEFAULT (chooser)->priv->manager;
1208 1209 1210 1211 1212 1213 1214
}

static void
show_filters (GtkRecentChooserDefault *impl,
              gboolean                 show)
{
  if (show)
1215
    gtk_widget_show (impl->priv->filter_combo_hbox);
1216
  else
1217
    gtk_widget_hide (impl->priv->filter_combo_hbox);
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
}

static void
gtk_recent_chooser_default_add_filter (GtkRecentChooser *chooser,
				       GtkRecentFilter  *filter)
{
  GtkRecentChooserDefault *impl;
  const gchar *name;

  impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
1229
  if (g_slist_find (impl->priv->filters, filter))
1230 1231 1232 1233 1234 1235
    {
      g_warning ("gtk_recent_chooser_add_filter() called on filter already in list\n");
      return;
    }
  
  g_object_ref_sink (filter);
1236
  impl->priv->filters = g_slist_append (impl->priv->filters, filter);
1237 1238 1239 1240
  
  /* display new filter */
  name = gtk_recent_filter_get_name (filter);
  if (!name)
1241
    name = _("Untitled filter");
1242

1243
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->priv->filter_combo), name);
1244

1245
  if (!g_slist_find (impl->priv->filters, impl->priv->current_filter))
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
    set_current_filter (impl, filter);
  
  show_filters (impl, TRUE);
}

static void
gtk_recent_chooser_default_remove_filter (GtkRecentChooser *chooser,
					  GtkRecentFilter  *filter)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  GtkTreeModel *model;
  GtkTreeIter iter;
  gint filter_idx;
  
1260
  filter_idx = g_slist_index (impl->priv->filters, filter);
1261 1262 1263 1264 1265 1266 1267
  
  if (filter_idx < 0)
    {
      g_warning ("gtk_recent_chooser_remove_filter() called on filter not in list\n");
      return;  
    }
  
1268
  impl->priv->filters = g_slist_remove (impl->priv->filters, filter);
1269
  
1270
  if (filter == impl->priv->current_filter)
1271
    {
1272 1273
      if (impl->priv->filters)
        set_current_filter (impl, impl->priv->filters->data);
1274 1275 1276 1277
      else
        set_current_filter (impl, NULL);
    }
  
1278
  model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->priv->filter_combo));
1279 1280 1281 1282 1283
  gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_idx);
  gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
  
  g_object_unref (filter);
  
1284
  if (!impl->priv->filters)
1285 1286 1287 1288 1289 1290 1291 1292
    show_filters (impl, FALSE);
}

static GSList *
gtk_recent_chooser_default_list_filters (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
1293
  return g_slist_copy (impl->priv->filters);
1294 1295 1296 1297 1298 1299
}

static void
set_current_filter (GtkRecentChooserDefault *impl,
		    GtkRecentFilter         *filter)
{
1300
  if (impl->priv->current_filter != filter)
1301 1302 1303
    {
      gint filter_idx;
      
1304 1305
      filter_idx = g_slist_index (impl->priv->filters, filter);
      if (impl->priv->filters && filter && filter_idx < 0)
1306 1307
        return;
      
1308 1309
      if (impl->priv->current_filter)
        g_object_unref (impl->priv->current_filter);
1310
      
1311
      impl->priv->current_filter = filter;
1312
      
1313
      if (impl->priv->current_filter)     
1314
        {
1315
          g_object_ref_sink (impl->priv->current_filter);
1316 1317
        }
      
1318 1319
      if (impl->priv->filters)
        gtk_combo_box_set_active (GTK_COMBO_BOX (impl->priv->filter_combo),
1320 1321
                                  filter_idx);
      
1322
      if (impl->priv->recent_store)
1323 1324
        reload_recent_items (impl);

1325 1326 1327 1328
      g_object_notify (G_OBJECT (impl), "filter");
    }
}

1329 1330 1331 1332
static void
chooser_set_sort_type (GtkRecentChooserDefault *impl,
		       GtkRecentSortType        sort_type)
{
1333
  if (impl->priv->sort_type != sort_type)
1334
    {
1335
      impl->priv->sort_type = sort_type;
1336 1337 1338
      reload_recent_items (impl);
      g_object_notify (G_OBJECT (impl), "sort-type");
    }
1339 1340 1341
}


1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
static GtkIconTheme *
get_icon_theme_for_widget (GtkWidget *widget)
{
  if (gtk_widget_has_screen (widget))
    return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));

  return gtk_icon_theme_get_default ();
}

static gint
get_icon_size_for_widget (GtkWidget   *widget,
			  GtkIconSize  icon_size)
{
  gint width, height;

1357
  if (gtk_icon_size_lookup (icon_size, &width, &height))
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
    return MAX (width, height);

  return FALLBACK_ICON_SIZE;
}

static void
recent_manager_changed_cb (GtkRecentManager *manager,
			   gpointer          user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);

  reload_recent_items (impl);
}

static void
selection_changed_cb (GtkTreeSelection *selection,
		      gpointer          user_data)
{
  _gtk_recent_chooser_selection_changed (GTK_RECENT_CHOOSER (user_data));
}

static void
row_activated_cb (GtkTreeView       *tree_view,
		  GtkTreePath       *tree_path,
		  GtkTreeViewColumn *tree_column,
		  gpointer           user_data)
{
  _gtk_recent_chooser_item_activated (GTK_RECENT_CHOOSER (user_data));
}

static void
filter_combo_changed_cb (GtkComboBox *combo_box,
			 gpointer     user_data)
{
  GtkRecentChooserDefault *impl;
  gint new_index;
  GtkRecentFilter *filter;
  
  impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  
  new_index = gtk_combo_box_get_active (combo_box);
1399
  filter = g_slist_nth_data (impl->priv->filters, new_index);
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
  
  set_current_filter (impl, filter);
}

static GdkPixbuf *
get_drag_pixbuf (GtkRecentChooserDefault *impl)
{
  GtkRecentInfo *info;
  GdkPixbuf *retval;
  gint size;
  
  g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));

  info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (impl));
  if (!info)
    return NULL;

  size = get_icon_size_for_widget (GTK_WIDGET (impl), GTK_ICON_SIZE_DND);

  retval = gtk_recent_info_get_icon (info, size);
  gtk_recent_info_unref (info);

  return retval;
}

static void
recent_view_drag_begin_cb (GtkWidget      *widget,
			   GdkDragContext *context,
			   gpointer        user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  GdkPixbuf *pixbuf;

  pixbuf = get_drag_pixbuf (impl);
  if (pixbuf)
    {
      gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
      g_object_unref (pixbuf);
    }
  else
    gtk_drag_set_icon_default (context);
}

typedef struct
{
  gchar **uri_list;
  gsize next_pos;
} DragData;

static void
append_uri_to_urilist (GtkTreeModel *model,
		       GtkTreePath  *path,
		       GtkTreeIter  *iter,
		       gpointer      user_data)
{
  DragData *drag_data = (DragData *) user_data;
  gchar *uri = NULL;
  gsize pos;

1459
  gtk_tree_model_get (model, iter,
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
  		      RECENT_URI_COLUMN, &uri,
  		      -1);
  g_assert (uri != NULL);

  pos = drag_data->next_pos;
  drag_data->uri_list[pos] = g_strdup (uri);
  drag_data->next_pos = pos + 1;
}

static void
recent_view_drag_data_get_cb (GtkWidget        *widget,
			      GdkDragContext   *context,
			      GtkSelectionData *selection_data,
			      guint             info,
			      guint32           time_,
			      gpointer          data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (data);
1478
  DragData drag_data;
1479 1480
  gsize n_uris;
  
1481
  n_uris = gtk_tree_selection_count_selected_rows (impl->priv->selection);
1482
  if (n_uris == 0)
1483
    return;
1484

1485 1486
  drag_data.uri_list = g_new0 (gchar *, n_uris + 1);
  drag_data.next_pos = 0;
1487
  
1488
  gtk_tree_selection_selected_foreach (impl->priv->selection,
1489
      				       append_uri_to_urilist,
1490
      				       &drag_data);
1491
  
1492
  gtk_selection_data_set_uris (selection_data, drag_data.uri_list);
1493

1494
  g_strfreev (drag_data.uri_list);
1495 1496
}

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
static gboolean
recent_view_query_tooltip_cb (GtkWidget  *widget,
                              gint        x,
                              gint        y,
                              gboolean    keyboard_tip,
                              GtkTooltip *tooltip,
                              gpointer    user_data)
{
  GtkRecentChooserDefault *impl = user_data;
  GtkTreeView *tree_view;
  GtkTreeIter iter;
  GtkTreePath *path = NULL;
  GtkRecentInfo *info = NULL;
  gchar *uri_display;
1511

1512
  if (!impl->priv->show_tips)
1513 1514
    return FALSE;

1515
  tree_view = GTK_TREE_VIEW (impl->priv->recent_view);
1516 1517 1518 1519 1520 1521 1522 1523

  gtk_tree_view_get_tooltip_context (tree_view,
                                     &x, &y,
                                     keyboard_tip,
                                     NULL, &path, NULL);
  if (!path)
    return FALSE;

1524
  if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->priv->recent_store), &iter, path))
1525 1526 1527 1528 1529
    {
      gtk_tree_path_free (path);
      return FALSE;
    }

1530
  gtk_tree_model_get (GTK_TREE_MODEL (impl->priv->recent_store), &iter,
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
                      RECENT_INFO_COLUMN, &info,
                      -1);

  uri_display = gtk_recent_info_get_uri_display (info);
  
  gtk_tooltip_set_text (tooltip, uri_display);
  gtk_tree_view_set_tooltip_row (tree_view, tooltip, path);

  g_free (uri_display);
  gtk_tree_path_free (path);
  gtk_recent_info_unref (info);

  return TRUE;
}
1545 1546 1547 1548 1549 1550 1551

static void
remove_selected_from_list (GtkRecentChooserDefault *impl)
{
  gchar *uri;
  GError *err;
  
1552
  if (impl->priv->select_multiple)
1553 1554 1555 1556 1557 1558 1559
    return;
  
  uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (impl));
  if (!uri)
    return;
  
  err = NULL;
1560
  if (!gtk_recent_manager_remove_item (impl->priv->manager, uri, &err))
1561 1562 1563
    {
      gchar *msg;
   
1564
      msg = g_strdup (_("Could not remove item"));
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
      error_message (impl, msg, err->message);
      
      g_free (msg);
      g_error_free (err);
    }
  
  g_free (uri);
}

static void
copy_activated_cb (GtkMenuItem *menu_item,
		   gpointer     user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  GtkRecentInfo *info;
  gchar *utf8_uri;

  info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (impl));
  if (!info)
    return;

  utf8_uri = gtk_recent_info_get_uri_display (info);
  
  gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (impl),
			  			    GDK_SELECTION_CLIPBOARD),
                          utf8_uri, -1);

1592
  gtk_recent_info_unref (info);
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
  g_free (utf8_uri);
}

static void
remove_all_activated_cb (GtkMenuItem *menu_item,
			 gpointer     user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  GError *err = NULL;
  
1603
  gtk_recent_manager_purge_items (impl->priv->manager, &err);
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
  if (err)
    {
       gchar *msg;

       msg = g_strdup (_("Could not clear list"));

       error_message (impl, msg, err->message);
       
       g_free (msg);
       g_error_free (err);
    }
}

static void
remove_item_activated_cb (GtkMenuItem *menu_item,
			  gpointer     user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  
  remove_selected_from_list (impl);
}

static void
show_private_toggled_cb (GtkCheckMenuItem *menu_item,
			 gpointer          user_data)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (user_data);
  
  g_object_set (G_OBJECT (impl),
  		"show-private", gtk_check_menu_item_get_active (menu_item),
  		NULL);
}

static void
recent_popup_menu_detach_cb (GtkWidget *attach_widget,
			     GtkMenu   *menu)
{
  GtkRecentChooserDefault *impl;
  
  impl = g_object_get_data (G_OBJECT (attach_widget), "GtkRecentChooserDefault");
  g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
  
1646 1647 1648 1649 1650
  impl->priv->