gtkrecentchooserdefault.c 56.7 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 Jardón's avatar
Javier Jardón 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
    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,
687
		 gboolean                 busy)
688 689 690 691 692 693
{
  GtkWindow *toplevel;
  GdkDisplay *display;
  GdkCursor *cursor;

  toplevel = get_toplevel (GTK_WIDGET (impl));
694
  if (!toplevel || !gtk_widget_get_realized (GTK_WIDGET (toplevel)))
695
    return;
696

697 698
  display = gtk_widget_get_display (GTK_WIDGET (toplevel));

699 700 701 702 703 704 705 706 707 708
  if (busy)
    {
      cursor = gdk_cursor_new_from_name (display, "left_ptr_watch");
      if (cursor == NULL)
        cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
    }
  else
    cursor = NULL;

  gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (toplevel)), cursor);
709 710 711
  gdk_display_flush (display);

  if (cursor)
712
    g_object_unref (cursor);
713 714 715 716 717
}

static void
chooser_set_model (GtkRecentChooserDefault *impl)
{
718 719
  g_assert (impl->priv->recent_store != NULL);
  g_assert (impl->priv->load_state == LOAD_LOADING);
720

721 722 723 724 725
  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),
726 727
  				   RECENT_DISPLAY_NAME_COLUMN);

728
  impl->priv->load_state = LOAD_FINISHED;
729 730 731 732 733 734 735 736 737 738 739 740 741
}

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);
  
742 743
  g_assert ((impl->priv->load_state == LOAD_EMPTY) ||
            (impl->priv->load_state == LOAD_PRELOAD));
744 745
  
  /* store the items for multiple runs */
746
  if (!impl->priv->recent_items)
747
    {
748 749
      impl->priv->recent_items = gtk_recent_chooser_get_items (GTK_RECENT_CHOOSER (impl));
      if (!impl->priv->recent_items)
750
        {
751
	  impl->priv->load_state = LOAD_FINISHED;
752
          impl->priv->load_id = 0;
753 754 755
          return FALSE;
        }
        
756 757 758
      impl->priv->n_recent_items = g_list_length (impl->priv->recent_items);
      impl->priv->loaded_items = 0;
      impl->priv->load_state = LOAD_PRELOAD;
759 760
    }
  
761 762
  info = (GtkRecentInfo *) g_list_nth_data (impl->priv->recent_items,
                                            impl->priv->loaded_items);
763 764 765 766 767 768 769 770 771 772
  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. */
773 774
  gtk_list_store_append (impl->priv->recent_store, &iter);
  gtk_list_store_set (impl->priv->recent_store, &iter,
775 776 777 778 779
  		      RECENT_URI_COLUMN, uri,           /* uri  */
  		      RECENT_DISPLAY_NAME_COLUMN, name, /* display_name */
  		      RECENT_INFO_COLUMN, info,         /* info */
  		      -1);
  
780
  impl->priv->loaded_items += 1;
781

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

793 794 795 796
      /* load the filled up model */
      chooser_set_model (impl);

      retval = FALSE;
797
      impl->priv->load_id = 0;
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
    }
  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);

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

  set_busy_cursor (impl, FALSE);
}

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

842
  /* reload is already in progress - do not disturb */
843
  if (impl->priv->load_id)
844 845
    return;
  
846 847
  widget = GTK_WIDGET (impl);

848 849
  gtk_tree_view_set_model (GTK_TREE_VIEW (impl->priv->recent_view), NULL);
  gtk_list_store_clear (impl->priv->recent_store);
850
  
851 852
  if (!impl->priv->icon_theme)
    impl->priv->icon_theme = get_icon_theme_for_widget (widget);
853

854
  impl->priv->icon_size = get_icon_size_for_widget (widget,
855 856
		  			      GTK_ICON_SIZE_BUTTON);

857
  if (!impl->priv->limit_set)
858
    impl->priv->limit = DEFAULT_RECENT_FILES_LIMIT;
859

860 861
  set_busy_cursor (impl, TRUE);

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

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

  widget = GTK_WIDGET (impl);
886 887
  context = gtk_widget_get_style_context (widget);
  state = gtk_widget_get_state_flags (widget);
888 889

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

892 893
  width = impl->priv->icon_size + font_size * NUM_CHARS + 0.5;
  height = (impl->priv->icon_size + font_size) * NUM_LINES + 0.5;
894 895

  /* Use at least the requisition size... */
896
  gtk_widget_get_preferred_size (widget, &req, NULL);
897 898 899 900 901
  width = MAX (width, req.width);
  height = MAX (height, req.height);

  /* ... but no larger than the monitor */
  screen = gtk_widget_get_screen (widget);
902 903
  monitor_num = gdk_screen_get_monitor_at_window (screen,
                                                  gtk_widget_get_window (widget));
904

905
  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
906 907 908 909 910

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

  /* Set size */
911
  scrollw = GTK_SCROLLED_WINDOW (gtk_widget_get_parent (impl->priv->recent_view));
912 913
  gtk_scrolled_window_set_min_content_width (scrollw, width);
  gtk_scrolled_window_set_min_content_height (scrollw, height);
914 915 916 917 918 919
}

static void
gtk_recent_chooser_default_map (GtkWidget *widget)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (widget);
920 921

  GTK_WIDGET_CLASS (_gtk_recent_chooser_default_parent_class)->map (widget);
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936

  /* 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;
937 938 939
  GIcon *icon;

  gtk_tree_model_get (model, iter, RECENT_INFO_COLUMN, &info, -1);
940
  g_assert (info != NULL);
941 942 943 944 945 946

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

  if (icon != NULL)
    g_object_unref (icon);
947 948

  gtk_recent_info_unref (info);
949 950 951 952 953 954 955 956 957 958
}

static void
recent_meta_data_func (GtkTreeViewColumn *tree_column,
		       GtkCellRenderer   *cell,
		       GtkTreeModel      *model,
		       GtkTreeIter       *iter,
		       gpointer           user_data)
{
  GtkRecentInfo *info = NULL;
959
  gchar *name;
960 961 962 963 964 965 966 967 968
  
  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);
969

970
  g_object_set (cell, "text", name, NULL);
971 972 973 974 975 976 977 978 979 980 981
  
  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);
  
982
  g_assert (impl->priv->selection != NULL);
983
  
984
  if (!impl->priv->select_multiple)
985 986 987 988 989
    {
      GtkTreeModel *model;
      GtkTreeIter iter;
      gchar *uri = NULL;
      
990
      if (!gtk_tree_selection_get_selected (impl->priv->selection, &model, &iter))
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
        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;
1019
  gchar *uri = NULL;
1020 1021 1022 1023 1024 1025 1026 1027
  
  if (!select_data)
    return TRUE;
  
  if (select_data->found)
    return TRUE;
  
  gtk_tree_model_get (model, iter, RECENT_URI_COLUMN, &uri, -1);
1028 1029 1030 1031
  if (!uri)
    return FALSE;
  
  if (strcmp (uri, select_data->uri) == 0)
1032 1033 1034 1035
    {
      select_data->found = TRUE;
      
      if (select_data->do_activate)
1036
        gtk_tree_view_row_activated (GTK_TREE_VIEW (select_data->impl->priv->recent_view),
1037
        			     path,
1038
        			     select_data->impl->priv->meta_column);
1039 1040
      
      if (select_data->do_select)
1041
        gtk_tree_selection_select_path (select_data->impl->priv->selection, path);
1042
      else
1043
        gtk_tree_selection_unselect_path (select_data->impl->priv->selection, path);
1044 1045

      g_free (uri);
1046 1047 1048
      
      return TRUE;
    }
1049 1050

  g_free (uri);
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
  
  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;
  
1070
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->priv->recent_store),
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 1103 1104 1105 1106
  			  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;
  
1107
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->priv->recent_store),
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 1139 1140 1141 1142
  			  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;
  
1143
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->priv->recent_store),
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
  			  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);
  
1156
  if (!impl->priv->select_multiple)
1157 1158
    return;
  
1159
  gtk_tree_selection_select_all (impl->priv->selection);
1160 1161 1162 1163 1164 1165 1166
}

static void
gtk_recent_chooser_default_unselect_all (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
1167
  gtk_tree_selection_unselect_all (impl->priv->selection);
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
}

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);
  
1178
  if (impl->priv->sort_data_destroy)
1179
    {
1180 1181
      impl->priv->sort_data_destroy (impl->priv->sort_data);
      impl->priv->sort_data_destroy = NULL;
1182
    }
1183
      
1184 1185
  impl->priv->sort_func = NULL;
  impl->priv->sort_data = NULL;
1186 1187 1188
  
  if (sort_func)
    {
1189 1190 1191
      impl->priv->sort_func = sort_func;
      impl->priv->sort_data = sort_data;
      impl->priv->sort_data_destroy = data_destroy;
1192 1193 1194 1195 1196 1197 1198 1199
    }
}

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

1200
  impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1201

1202
  return _gtk_recent_chooser_get_items (chooser,
1203 1204 1205
                                        impl->priv->current_filter,
                                        impl->priv->sort_func,
                                        impl->priv->sort_data);
1206 1207 1208 1209 1210
}

static GtkRecentManager *
gtk_recent_chooser_default_get_recent_manager (GtkRecentChooser *chooser)
{
1211
  return GTK_RECENT_CHOOSER_DEFAULT (chooser)->priv->manager;
1212 1213 1214 1215 1216 1217 1218
}

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

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

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

1247
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->priv->filter_combo), name);
1248

1249
  if (!g_slist_find (impl->priv->filters, impl->priv->current_filter))
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
    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;
  
1264
  filter_idx = g_slist_index (impl->priv->filters, filter);
1265 1266 1267 1268 1269 1270 1271
  
  if (filter_idx < 0)
    {
      g_warning ("gtk_recent_chooser_remove_filter() called on filter not in list\n");
      return;  
    }
  
1272
  impl->priv->filters = g_slist_remove (impl->priv->filters, filter);
1273
  
1274
  if (filter == impl->priv->current_filter)
1275
    {
1276 1277
      if (impl->priv->filters)
        set_current_filter (impl, impl->priv->filters->data);
1278 1279 1280 1281
      else
        set_current_filter (impl, NULL);
    }
  
1282
  model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->priv->filter_combo));
1283 1284 1285 1286 1287
  gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_idx);
  gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
  
  g_object_unref (filter);
  
1288
  if (!impl->priv->filters)
1289 1290 1291 1292 1293 1294 1295 1296
    show_filters (impl, FALSE);
}

static GSList *
gtk_recent_chooser_default_list_filters (GtkRecentChooser *chooser)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
  
1297
  return g_slist_copy (impl->priv->filters);
1298 1299 1300 1301 1302 1303
}

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

1329 1330 1331 1332
      g_object_notify (G_OBJECT (impl), "filter");
    }
}

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


1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
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;

1361
  if (gtk_icon_size_lookup (icon_size, &width, &height))
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 1399 1400 1401 1402
    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);
1403
  filter = g_slist_nth_data (impl->priv->filters, new_index);
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 1459 1460 1461 1462
  
  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;

1463
  gtk_tree_model_get (model, iter,
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
  		      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);
1482
  DragData drag_data;
1483 1484
  gsize n_uris;
  
1485
  n_uris = gtk_tree_selection_count_selected_rows (impl->priv->selection);
1486
  if (n_uris == 0)
1487
    return;
1488

1489 1490
  drag_data.uri_list = g_new0 (gchar *, n_uris + 1);
  drag_data.next_pos = 0;
1491
  
1492
  gtk_tree_selection_selected_foreach (impl->priv->selection,
1493
      				       append_uri_to_urilist,
1494
      				       &drag_data);
1495
  
1496
  gtk_selection_data_set_uris (selection_data, drag_data.uri_list);
1497

1498
  g_strfreev (drag_data.uri_list);
1499 1500
}

1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
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;
1515

1516
  if (!impl->priv->show_tips)
1517 1518
    return FALSE;

1519
  tree_view = GTK_TREE_VIEW (impl->priv->recent_view);
1520 1521 1522 1523 1524 1525 1526 1527

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

1528
  if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->priv->recent_store), &iter, path))
1529 1530 1531 1532 1533
    {
      gtk_tree_path_free (path);
      return FALSE;
    }

1534
  gtk_tree_model_get (GTK_TREE_MODEL (impl->priv->recent_store), &iter,
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
                      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;
}
1549 1550 1551 1552 1553 1554 1555

static void
remove_selected_from_list (GtkRecentChooserDefault *impl)
{
  gchar *uri;
  GError *err;
  
1556
  if (impl->priv->select_multiple)
1557 1558 1559 1560 1561 1562 1563
    return;
  
  uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (impl));
  if (!uri)
    return;
  
  err = NULL;
1564
  if (!gtk_recent_manager_remove_item (impl->priv->manager, uri, &err))
1565 1566 1567
    {
      gchar *msg;
   
1568
      msg = g_strdup (_("Could not remove item"));
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
      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);

1596
  gtk_recent_info_unref (info);
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
  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;
  
1607
  gtk_recent_manager_purge_items (impl->priv->manager, &err);
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 1646 1647 1648 1649
  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));
  
1650 1651 1652 1653 1654
  impl->priv->recent_popup_menu = NULL;
  impl->priv->recent_popup_menu_remove_item = NULL;
  impl->priv->recent_popup_menu_copy_item = NULL;
  impl->priv->recent_popup_menu_clear_item = NULL;
  impl->priv->recent_popup_menu_show_private_item = NULL;
1655 1656 1657 1658 1659 1660 1661 1662
}

static void
recent_view_menu_ensure_state (GtkRecentChooserDefault *impl)
{
  gint count;
  
  g_assert (GTK_IS_RECENT_CHOOSER_DEFAULT (impl));
1663
  g_assert (impl->priv->recent_popup_menu != NULL);