gtkrecentchooserdefault.c 55.5 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
#include "gtkcssiconthemevalueprivate.h"
42
#include "gtkdragsource.h"
43 44 45 46
#include "gtkentry.h"
#include "gtkeventbox.h"
#include "gtkexpander.h"
#include "gtkframe.h"
Matthias Clasen's avatar
Matthias Clasen committed
47 48
#include "gtkbox.h"
#include "gtkpaned.h"
49 50 51 52 53 54 55 56
#include "gtkimage.h"
#include "gtkintl.h"
#include "gtklabel.h"
#include "gtkmenuitem.h"
#include "gtkmessagedialog.h"
#include "gtkscrolledwindow.h"
#include "gtkseparatormenuitem.h"
#include "gtksizegroup.h"
57
#include "gtksizerequest.h"
58
#include "gtkstylecontextprivate.h"
59 60 61 62
#include "gtktreemodelsort.h"
#include "gtktreemodelfilter.h"
#include "gtktreeselection.h"
#include "gtktreestore.h"
63
#include "gtktooltip.h"
64
#include "gtktypebuiltins.h"
65
#include "gtkorientable.h"
66
#include "gtkwindowgroup.h"
67
#include "deprecated/gtkactivatable.h"
68 69 70 71 72 73 74 75

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

76

77 78 79 80 81 82 83 84 85
enum 
{
  PROP_0,

  /* activatable properties */
  PROP_ACTIVATABLE_RELATED_ACTION,
  PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
};

86
typedef struct
87 88 89 90
{
  GtkRecentManager *manager;
  gulong manager_changed_id;
  guint local_manager : 1;
91

92 93 94
  gint icon_size;

  /* RecentChooser properties */
95
  gint limit;
96 97 98 99 100 101 102
  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;
103 104

  guint limit_set : 1;
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  
  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;
121
  GtkCellRenderer *icon_renderer;
122 123 124 125 126 127 128 129 130 131 132 133 134 135
  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;
136 137 138 139 140 141 142
} GtkRecentChooserDefaultPrivate;

struct _GtkRecentChooserDefault
{
  GtkBox parent_instance;

  GtkRecentChooserDefaultPrivate *priv;
143 144 145 146
};

typedef struct _GtkRecentChooserDefaultClass
{
147
  GtkBoxClass parent_class;
148 149
} GtkRecentChooserDefaultClass;

150
/* Keep inline with GtkTreeStore defined in gtkrecentchooserdefault.ui */
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
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

172 173
#define DEFAULT_RECENT_FILES_LIMIT 50

174 175 176


/* GObject */
177 178
static void     _gtk_recent_chooser_default_class_init  (GtkRecentChooserDefaultClass *klass);
static void     _gtk_recent_chooser_default_init        (GtkRecentChooserDefault      *impl);
179
static void     gtk_recent_chooser_default_finalize     (GObject                      *object);
180
static void     gtk_recent_chooser_default_dispose      (GObject                      *object);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
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);
279 280 281 282 283 284 285
static gboolean recent_view_query_tooltip_cb      (GtkWidget        *widget,
                                                   gint              x,
                                                   gint              y,
                                                   gboolean          keyboard_tip,
                                                   GtkTooltip       *tooltip,
                                                   gpointer          user_data);

286
static void gtk_recent_chooser_activatable_iface_init (GtkActivatableIface  *iface);
287
static void gtk_recent_chooser_update                 (GtkActivatable       *activatable,
288 289
						       GtkAction            *action,
						       const gchar          *property_name);
290
static void gtk_recent_chooser_sync_action_properties (GtkActivatable       *activatable,
291
						       GtkAction            *action);
292

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



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;
}

323 324
static void
gtk_recent_chooser_activatable_iface_init (GtkActivatableIface *iface)
325

326 327 328
{
  iface->update = gtk_recent_chooser_update;
  iface->sync_action_properties = gtk_recent_chooser_sync_action_properties;
329 330
}

331
static void
332
_gtk_recent_chooser_default_class_init (GtkRecentChooserDefaultClass *klass)
333 334 335 336 337 338
{
  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;
339 340
  gobject_class->dispose = gtk_recent_chooser_default_dispose;
  gobject_class->finalize = gtk_recent_chooser_default_finalize;
341 342 343 344 345
  
  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);
346 347 348

  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");
349 350 351 352

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

355 356 357 358 359 360 361 362 363
  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);
364 365 366 367 368 369 370 371 372

  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);
373 374 375
}

static void
376
_gtk_recent_chooser_default_init (GtkRecentChooserDefault *impl)
377
{
378
  GtkRecentChooserDefaultPrivate *priv;
379

380
  impl->priv = priv = _gtk_recent_chooser_default_get_instance_private (impl);
381

382
  /* by default, we use the global manager */
383
  priv->local_manager = FALSE;
384

385 386
  priv->limit = FALLBACK_ITEM_LIMIT;
  priv->sort_type = GTK_RECENT_SORT_NONE;
387

388 389 390 391 392 393
  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;
394
  
395 396
  priv->icon_size = FALLBACK_ICON_SIZE;
  priv->icon_theme = NULL;
397
  
398
  priv->current_filter = NULL;
399

400 401 402
  priv->recent_items = NULL;
  priv->n_recent_items = 0;
  priv->loaded_items = 0;
403
  
404
  priv->load_state = LOAD_EMPTY;
405

406 407 408
  gtk_widget_init_template (GTK_WIDGET (impl));

  g_object_set_data (G_OBJECT (priv->recent_view),
409
                     "GtkRecentChooserDefault", impl);
410 411 412

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

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:
442 443 444 445 446 447 448 449 450 451 452 453
      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);
454 455 456
        }
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
457 458 459 460 461 462
      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);
        }
463 464
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
465 466 467 468 469
      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);
        }
470 471
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
472 473 474 475 476 477
      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);
        }
478 479
      break;
    case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
480 481 482 483 484 485 486 487 488
      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);
        }
489 490
      break;
    case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
491 492 493 494 495 496
      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);
        }
497 498
      break;
    case GTK_RECENT_CHOOSER_PROP_LIMIT:
499 500 501 502 503 504 505
      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);
        }
506 507 508 509 510 511 512
      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;
513 514 515 516 517 518
    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;
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
    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:
536
      g_value_set_int (value, impl->priv->limit);
537 538
      break;
    case GTK_RECENT_CHOOSER_PROP_SORT_TYPE:
539
      g_value_set_enum (value, impl->priv->sort_type);
540 541
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE:
542
      g_value_set_boolean (value, impl->priv->show_private);
543 544
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_ICONS:
545
      g_value_set_boolean (value, impl->priv->show_icons);
546 547
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND:
548
      g_value_set_boolean (value, impl->priv->show_not_found);
549 550
      break;
    case GTK_RECENT_CHOOSER_PROP_SHOW_TIPS:
551
      g_value_set_boolean (value, impl->priv->show_tips);
552 553
      break;
    case GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY:
554
      g_value_set_boolean (value, impl->priv->local_only);
555 556
      break;
    case GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE:
557
      g_value_set_boolean (value, impl->priv->select_multiple);
558 559
      break;
    case GTK_RECENT_CHOOSER_PROP_FILTER:
560
      g_value_set_object (value, impl->priv->current_filter);
561
      break;
562 563 564 565 566 567
    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;
568 569 570 571 572 573 574
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
575
gtk_recent_chooser_default_dispose (GObject *object)
576 577 578
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (object);

579
  if (impl->priv->load_id)
580
    {
581 582 583
      g_source_remove (impl->priv->load_id);
      impl->priv->load_state = LOAD_EMPTY;
      impl->priv->load_id = 0;
584 585
    }

586 587
  g_list_free_full (impl->priv->recent_items, (GDestroyNotify) gtk_recent_info_unref);
  impl->priv->recent_items = NULL;
588

589
  if (impl->priv->manager && impl->priv->manager_changed_id)
590
    {
591 592
      g_signal_handler_disconnect (impl->priv->manager, impl->priv->manager_changed_id);
      impl->priv->manager_changed_id = 0;
593 594
    }

595
  if (impl->priv->filters)
596
    {
Matthias Clasen's avatar
Matthias Clasen committed
597
      g_slist_free_full (impl->priv->filters, g_object_unref);
598
      impl->priv->filters = NULL;
599 600
    }
  
601
  if (impl->priv->current_filter)
602
    {
603 604
      g_object_unref (impl->priv->current_filter);
      impl->priv->current_filter = NULL;
605
    }
606

607 608 609 610 611 612 613 614
  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);

615
  impl->priv->manager = NULL; 
616
  
617
  if (impl->priv->sort_data_destroy)
618
    {
619 620
      impl->priv->sort_data_destroy (impl->priv->sort_data);
      impl->priv->sort_data_destroy = NULL;
621 622
    }
  
623 624
  impl->priv->sort_data = NULL;
  impl->priv->sort_func = NULL;
625
  
626
  G_OBJECT_CLASS (_gtk_recent_chooser_default_parent_class)->finalize (object);
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 655 656
}

/* 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);

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

  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);
672
  if (!gtk_widget_is_toplevel (toplevel))
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
    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,
689
		 gboolean                 busy)
690 691 692 693 694 695
{
  GtkWindow *toplevel;
  GdkDisplay *display;
  GdkCursor *cursor;

  toplevel = get_toplevel (GTK_WIDGET (impl));
696
  if (!toplevel || !gtk_widget_get_realized (GTK_WIDGET (toplevel)))
697
    return;
698

699 700
  display = gtk_widget_get_display (GTK_WIDGET (toplevel));

701
  if (busy)
702
    cursor = gdk_cursor_new_from_name (display, "progress");
703 704 705 706
  else
    cursor = NULL;

  gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (toplevel)), cursor);
707 708 709
  gdk_display_flush (display);

  if (cursor)
710
    g_object_unref (cursor);
711 712 713 714 715
}

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

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

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

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

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

791 792 793 794
      /* load the filled up model */
      chooser_set_model (impl);

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

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

  set_busy_cursor (impl, FALSE);
}

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

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

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

852
  impl->priv->icon_size = get_icon_size_for_widget (widget,
853 854
		  			      GTK_ICON_SIZE_BUTTON);

855
  if (!impl->priv->limit_set)
856
    impl->priv->limit = DEFAULT_RECENT_FILES_LIMIT;
857

858 859
  set_busy_cursor (impl, TRUE);

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

/* taken form gtkfilechooserdialog.c */
static void
set_default_size (GtkRecentChooserDefault *impl)
{
872
  GtkScrolledWindow *scrollw;
873 874
  GtkWidget *widget;
  gint width, height;
875
  double font_size;
876 877
  GdkDisplay *display;
  GdkMonitor *monitor;
878
  GtkRequisition req;
879
  GdkRectangle workarea;
880
  GtkStyleContext *context;
881 882

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

  /* Size based on characters and the icon size */
886
  gtk_style_context_get (context, gtk_style_context_get_state (context), "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
  width = MAX (width, req.width);
  height = MAX (height, req.height);

  /* ... but no larger than the monitor */
897 898 899
  display = gtk_widget_get_display (widget);
  monitor = gdk_display_get_monitor_at_window (display, gtk_widget_get_window (widget));
  gdk_monitor_get_workarea (monitor, &workarea);
900

901 902
  width = MIN (width, workarea.width * 3 / 4);
  height = MIN (height, workarea.height * 3 / 4);
903 904

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

static void
gtk_recent_chooser_default_map (GtkWidget *widget)
{
  GtkRecentChooserDefault *impl = GTK_RECENT_CHOOSER_DEFAULT (widget);
914 915

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

  /* 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;
931 932 933
  GIcon *icon;

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

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

  if (icon != NULL)
    g_object_unref (icon);
941 942

  gtk_recent_info_unref (info);
943 944 945 946 947 948 949 950 951 952
}

static void
recent_meta_data_func (GtkTreeViewColumn *tree_column,
		       GtkCellRenderer   *cell,
		       GtkTreeModel      *model,
		       GtkTreeIter       *iter,
		       gpointer           user_data)
{
  GtkRecentInfo *info = NULL;
953
  gchar *name;
954 955 956 957 958 959 960 961 962
  
  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);
963

964
  g_object_set (cell, "text", name, NULL);
965 966 967 968 969 970 971 972 973 974 975
  
  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);
  
976
  g_assert (impl->priv->selection != NULL);
977
  
978
  if (!impl->priv->select_multiple)
979 980 981 982 983
    {
      GtkTreeModel *model;
      GtkTreeIter iter;
      gchar *uri = NULL;
      
984
      if (!gtk_tree_selection_get_selected (impl->priv->selection, &model, &iter))
985 986 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
        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;
1013
  gchar *uri = NULL;
1014 1015 1016 1017 1018 1019 1020 1021
  
  if (!select_data)
    return TRUE;
  
  if (select_data->found)
    return TRUE;
  
  gtk_tree_model_get (model, iter, RECENT_URI_COLUMN, &uri, -1);
1022 1023 1024 1025
  if (!uri)
    return FALSE;
  
  if (strcmp (uri, select_data->uri) == 0)
1026 1027 1028 1029
    {
      select_data->found = TRUE;
      
      if (select_data->do_activate)
1030
        gtk_tree_view_row_activated (GTK_TREE_VIEW (select_data->impl->priv->recent_view),
1031
        			     path,
1032
        			     select_data->impl->priv->meta_column);
1033 1034
      
      if (select_data->do_select)
1035
        gtk_tree_selection_select_path (select_data->impl->priv->selection, path);
1036
      else
1037
        gtk_tree_selection_unselect_path (select_data->impl->priv->selection, path);
1038 1039

      g_free (uri);
1040 1041 1042
      
      return TRUE;
    }
1043 1044

  g_free (uri);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
  
  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;
  
1064
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->priv->recent_store),
1065 1066 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
  			  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;
  
1101
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->priv->recent_store),
1102 1103 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
  			  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;
  
1137
  gtk_tree_model_foreach (GTK_TREE_MODEL (impl->priv->recent_store),
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
  			  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);
  
1150
  if (!impl->priv->select_multiple)
1151 1152
    return;
  
1153
  gtk_tree_selection_select_all (impl->priv->selection);
1154 1155 1156 1157 1158 1159 1160
}

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

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);
  
1172
  if (impl->priv->sort_data_destroy)
1173
    {
1174 1175
      impl->priv->sort_data_destroy (impl->priv->sort_data);
      impl->priv->sort_data_destroy = NULL;
1176
    }
1177
      
1178 1179
  impl->priv->sort_func = NULL;
  impl->priv->sort_data = NULL;
1180 1181 1182
  
  if (sort_func)
    {
1183 1184 1185
      impl->priv->sort_func = sort_func;
      impl->priv->sort_data = sort_data;
      impl->priv->sort_data_destroy = data_destroy;
1186 1187 1188 1189 1190 1191 1192 1193
    }
}

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

1194
  impl = GTK_RECENT_CHOOSER_DEFAULT (chooser);
1195

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

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

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

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

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

1241
  gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (impl->priv->filter_combo), name);
1242

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

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

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

1323 1324 1325 1326
      g_object_notify (G_OBJECT (impl), "filter");
    }
}

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


1340 1341 1342
static GtkIconTheme *
get_icon_theme_for_widget (GtkWidget *widget)
{
1343 1344 1345
  return gtk_css_icon_theme_value_get_icon_theme
    (_gtk_style_context_peek_property (gtk_widget_get_style_context (widget),
                                       GTK_CSS_PROPERTY_ICON_THEME));
1346 1347 1348 1349 1350 1351 1352 1353
}

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

1354
  if (gtk_icon_size_lookup (icon_size, &width, &height))
1355 1356 1357 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
    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);
1396
  filter = g_slist_nth_data (impl->priv->filters, new_index);
1397 1398 1399 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
  
  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;

1456
  gtk_tree_model_get (model, iter,
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
  		      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);
1475
  DragData drag_data;
1476 1477
  gsize n_uris;
  
1478
  n_uris = gtk_tree_selection_count_selected_rows (impl->priv->selection);
1479
  if (n_uris == 0)
1480
    return;
1481

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

1491
  g_strfreev (drag_data.uri_list);
1492 1493
}

1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
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;
1508

1509
  if (!impl->priv->show_tips)
1510 1511
    return FALSE;

1512
  tree_view = GTK_TREE_VIEW (impl->priv->recent_view);
1513 1514 1515 1516 1517 1518 1519 1520

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

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

1527
  gtk_tree_model_get (GTK_TREE_MODEL (impl->priv->recent_store), &iter,
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
                      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;
}
1542 1543 1544 1545 1546 1547 1548

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

1589
  gtk_recent_info_unref (info);
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
  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;
  
1600
  gtk_recent_manager_purge_items (impl->priv->manager, &err);
1601 1602 1603 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
  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));
  
Tristan Van Berkom's avatar