gtkrecentmanager.c 59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 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 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
/* GTK - The GIMP Toolkit
 * gtkrecentmanager.c: a manager for the recently used resources
 *
 * Copyright (C) 2006 Emmanuele Bassi
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 */

#include "config.h"

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

#include "gtkrecentmanager.h"
#include "gtkintl.h"
#include "gtkstock.h"
#include "gtkicontheme.h"
#include "gtktypebuiltins.h"
#include "gtkprivate.h"
#include "gtkmarshalers.h"
#include "gtkalias.h"

#ifdef G_OS_UNIX
#define XDG_PREFIX _gtk_xdg
#include "xdgmime/xdgmime.h"
#endif

/* the file where we store the recently used items */
#define GTK_RECENTLY_USED_FILE	".recently-used.xbel"

/* a poll per second should be enough */
#define POLL_DELTA	1000

/* return all items by default */
#define DEFAULT_LIMIT	-1

/* keep in sync with xdgmime */
#define GTK_RECENT_DEFAULT_MIME	"application/octet-stream"

typedef struct
{
  gchar *name;
  gchar *exec;
  
  guint count;
  
  time_t stamp;
} RecentAppInfo;

struct _GtkRecentInfo
{
  gchar *uri;
  
  gchar *display_name;
  gchar *description;
  
  time_t added;
  time_t modified;
  time_t visited;
  
  gchar *mime_type;
  
  GSList *applications;
  GHashTable *apps_lookup;
  
  GSList *groups;
  
  gboolean is_private;
  
  GdkPixbuf *icon;
  
  gint ref_count;
};

struct _GtkRecentManagerPrivate
{
  gchar *filename;

  guint is_screen_singleton : 1;
  guint is_dirty : 1;
  guint write_in_progress : 1;
  guint read_in_progress : 1;
  
  gint limit;
  gint size;

  GdkScreen *screen;
  
  GBookmarkFile *recent_items;
  
  time_t last_mtime;
  guint poll_timeout;
};

enum
{
  PROP_0,

  PROP_FILENAME,  
  PROP_LIMIT,
  PROP_SIZE
};

static void           gtk_recent_manager_finalize     (GObject               *object);

static void           gtk_recent_manager_set_property (GObject               *object,
						       guint                  prop_id,
						       const GValue          *value,
						       GParamSpec            *pspec);
static void           gtk_recent_manager_get_property (GObject               *object,
						       guint                  prop_id,
						       GValue                *value,
						       GParamSpec            *pspec);
static void           gtk_recent_manager_changed      (GtkRecentManager      *manager);

static void           gtk_recent_manager_real_changed (GtkRecentManager      *manager);
static gboolean       gtk_recent_manager_poll_timeout (gpointer               data);
static void           gtk_recent_manager_set_filename (GtkRecentManager      *manager,
						       const gchar           *filename);

static void           build_recent_items_list         (GtkRecentManager      *manager);
static void           purge_recent_items_list         (GtkRecentManager      *manager,
						       GError               **error);

static RecentAppInfo *recent_app_info_new             (const gchar           *app_name);
static void           recent_app_info_free            (RecentAppInfo         *app_info);

static GtkRecentInfo *gtk_recent_info_new             (const gchar           *uri);
static void           gtk_recent_info_free            (GtkRecentInfo         *recent_info);

static guint signal_changed = 0;

G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT);

GQuark
gtk_recent_manager_error_quark (void)
{
  static GQuark quark = 0;
  if (quark == 0)
    quark = g_quark_from_static_string ("gtk-recent-manager-error-quark");
  return quark;
}


static void
gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  
  gtk_recent_manager_parent_class = g_type_class_peek_parent (klass);
  
  gobject_class->set_property = gtk_recent_manager_set_property;
  gobject_class->get_property = gtk_recent_manager_get_property;
  gobject_class->finalize = gtk_recent_manager_finalize;
  
  /**
   * GtkRecentManager:filename
   *
   * The full path to the file to be used to store and read the recently
   * used resources list
   *
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
		  		   PROP_FILENAME,
				   g_param_spec_string ("filename",
					   		P_("Filename"),
							P_("The full path to the file to be used to store and read the list"),
							NULL,
							(G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE)));
  /**
   * GtkRecentManager:limit
   *
   * The maximum number of items to be returned by the
   * gtk_recent_manager_get_items() function.
   *
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
  				   PROP_LIMIT,
  				   g_param_spec_int ("limit",
  				   		     P_("Limit"),
  				   		     P_("The maximum number of items to be returned by gtk_recent_manager_get_items()"),
  				   		     -1,
  				   		     G_MAXINT,
  				   		     DEFAULT_LIMIT,
  				   		     G_PARAM_READWRITE));
  /**
   * GtkRecentManager:size
   * 
   * The size of the recently used resources list.
   *
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
		  		   PROP_SIZE,
				   g_param_spec_int ("size",
					   	     P_("Size"),
						     P_("The size of the recently used resources list"),
						     -1,
						     G_MAXINT,
						     0,
						     G_PARAM_READABLE));
  
  /**
   * GtkRecentManager::changed
   * @recent_manager: the recent manager
   *
   * Emitted when the current recently used resources manager changes its
   * contents.
   *
   * Since: 2.10
   */
  signal_changed =
    g_signal_new (I_("changed"),
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  
  klass->changed = gtk_recent_manager_real_changed;
  
  g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
}

static void
gtk_recent_manager_init (GtkRecentManager *manager)
{
  GtkRecentManagerPrivate *priv;
  
  priv = g_type_instance_get_private ((GTypeInstance *) manager,
  				      GTK_TYPE_RECENT_MANAGER);
  manager->priv = priv;
  
  priv->filename = g_build_filename (g_get_home_dir (),
				     GTK_RECENTLY_USED_FILE,
				     NULL);
  
  priv->limit = DEFAULT_LIMIT;
  priv->size = 0;
  
  priv->is_screen_singleton = FALSE;
  priv->is_dirty = FALSE;
  priv->write_in_progress = FALSE;
  priv->read_in_progress = FALSE;

  priv->screen = NULL;
}

static void
gtk_recent_manager_set_property (GObject               *object,
				 guint                  prop_id,
				 const GValue          *value,
				 GParamSpec            *pspec)
{
  GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
 
  switch (prop_id)
    {
    case PROP_FILENAME:
      gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
      break;      
    case PROP_LIMIT:
      gtk_recent_manager_set_limit (recent_manager, g_value_get_int (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_recent_manager_get_property (GObject               *object,
				 guint                  prop_id,
				 GValue                *value,
				 GParamSpec            *pspec)
{
  GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
  
  switch (prop_id)
    {
    case PROP_FILENAME:
      g_value_set_string (value, recent_manager->priv->filename);
      break;
    case PROP_LIMIT:
      g_value_set_int (value, recent_manager->priv->limit);
      break;
    case PROP_SIZE:
      g_value_set_int (value, recent_manager->priv->size);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
} 

static void
gtk_recent_manager_finalize (GObject *object)
{
  GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
  GtkRecentManagerPrivate *priv = manager->priv;

  /* remove the poll timeout */
  if (priv->poll_timeout)
    g_source_remove (priv->poll_timeout);
  
  if (priv->filename)
    g_free (priv->filename);
  
  if (priv->recent_items)
    g_bookmark_file_free (priv->recent_items);
  
  /* chain up parent's finalize method */  
  G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
}

static void
gtk_recent_manager_real_changed (GtkRecentManager *manager)
{
  GtkRecentManagerPrivate *priv = manager->priv;

  g_object_freeze_notify (G_OBJECT (manager));

  if (priv->is_dirty)
    {
      GError *write_error;
      struct stat stat_buf;
      
      /* we are marked as dirty, so we dump the content of our
       * recently used items list
       */
      g_assert (priv->filename != NULL);

      priv->write_in_progress = TRUE;

      /* if no container object has been defined, we create a new
       * empty container, and dump it
       */
      if (!priv->recent_items)
        {
          priv->recent_items = g_bookmark_file_new ();
	  priv->size = 0;
	}

      write_error = NULL;
      g_bookmark_file_to_file (priv->recent_items,
		               priv->filename,
			       &write_error);
      if (write_error)
        {
          g_warning ("Attempting to store changes into `%s', "
		     "but failed: %s",
		     priv->filename,
		     write_error->message);
	  g_error_free (write_error);
	}

      /* we have sync'ed our list with the storage file, so we
       * update the file mtime in order to skip the timed check
       * and spare us from a re-read.
       */
      if (g_stat (priv->filename, &stat_buf) < 0)
	{
          g_warning ("Unable to stat() the recently used resources file "
		     "at `%s': %s.",
		     priv->filename,
		     g_strerror (errno));

	  priv->write_in_progress = FALSE;
	  
	  g_object_thaw_notify (G_OBJECT (manager));

	  return;
	}
      
      priv->last_mtime = stat_buf.st_mtime;
      
      /* mark us as clean */
      priv->is_dirty = FALSE;
    }
  else
    {
      /* we are not marked as dirty, so we have been called
       * because the recently used resources file has been
       * changed (and not from us).
       */
      build_recent_items_list (manager);
    }

  g_object_thaw_notify (G_OBJECT (manager));
}

/* timed poll()-ing of the recently used resources file.
 * an event-based system would be more efficient.
 */
static gboolean
gtk_recent_manager_poll_timeout (gpointer data)
{
  GtkRecentManager *manager = GTK_RECENT_MANAGER (data);
  GtkRecentManagerPrivate *priv = manager->priv;
  struct stat stat_buf;
  int stat_res;

  /* wait for the next timeout if we have a read/write in progress */
  if (priv->write_in_progress || priv->read_in_progress)
    return TRUE;

  stat_res = g_stat (priv->filename, &stat_buf);
  if (stat_res < 0)
    {
      /* the file does not exist, yet, so we wait */
      if (errno == ENOENT)
        return TRUE;
      
      g_warning ("Unable to stat() the recently used resources file "
		 "at `%s': %s.",
		 priv->filename,
		 g_strerror (errno));
      
      return TRUE;
    }

  /* the file didn't change from the last poll(), so we bail out */
  if (stat_buf.st_mtime == priv->last_mtime)
    return TRUE;

  /* the file has been changed, hence we emit the "changed" signal */
  gtk_recent_manager_changed (manager);

  return TRUE;
}

static void
gtk_recent_manager_set_filename (GtkRecentManager *manager,
				 const gchar      *filename)
{
  GtkRecentManagerPrivate *priv;
  
  g_assert (GTK_IS_RECENT_MANAGER (manager));
  priv = manager->priv;
  
  if (!filename || filename[0] == '\0')
    return;
  
  g_free (manager->priv->filename);

  if (manager->priv->poll_timeout)
    {
      g_source_remove (manager->priv->poll_timeout);
      manager->priv->poll_timeout = 0;
    }

  build_recent_items_list (manager);
  
  priv->filename = g_strdup (filename);
  priv->poll_timeout = g_timeout_add (POLL_DELTA,
		  		      gtk_recent_manager_poll_timeout,
				      manager);

  priv->is_dirty = FALSE;
}

/* reads the recently used resources file and builds the items list.
 * we keep the items list inside the parser object, and build the
 * RecentInfo object only on user's demand to avoid useless replication.
 */
static void
build_recent_items_list (GtkRecentManager *manager)
{
  GtkRecentManagerPrivate *priv;
  struct stat stat_buf;
  int stat_res;
  gboolean res;
  GError *read_error;
  gint size;

  priv = manager->priv;
  g_assert (priv->filename != NULL);
  
  if (!priv->recent_items)
    {
      priv->recent_items = g_bookmark_file_new ();
      priv->size = 0;
    }

  stat_res = g_stat (priv->filename, &stat_buf);
  if (stat_res < 0)
    {
      /* the file doesn't exists, so we bail out and wait for the first
       * write operation
       */

      if (errno == ENOENT)
        return;
      else
        {
          g_warning ("Attempting to read the recently used resources file "
                     "at `%s', but an error occurred: %s. Aborting.",
                     priv->filename,
                     g_strerror (errno));

          return;
        }
    }

  /* record the last mtime, for later use */
  priv->last_mtime = stat_buf.st_mtime;
  
  priv->read_in_progress = TRUE;

  /* the file exists, and it's valid (we hope); if not, destroy the container
   * object and hope for a better result when the next "changed" signal is
   * fired. */
  read_error = NULL;
  res = g_bookmark_file_load_from_file (priv->recent_items,
		  			priv->filename,
					&read_error);
  if (read_error)
    {
      g_warning ("Attempting to read the recently used resources file "
		 "at `%s', but the parser failed: %s.",
		 priv->filename,
		 read_error->message);

      g_bookmark_file_free (priv->recent_items);
      priv->recent_items = NULL;

      g_error_free (read_error);
    }

  size = g_bookmark_file_get_size (priv->recent_items);
  if (priv->size != size)
    {
      priv->size = size;
      
      g_object_notify (G_OBJECT (manager), "size");
    }

  priv->read_in_progress = FALSE;
}


/********************
 * GtkRecentManager *
 ********************/


/**
 * gtk_recent_manager_new:
 * 
 * Creates a new recent manager object.  Recent manager objects are used to
 * handle the list of recently used resources.  A #GtkRecentManager object
 * monitors the recently used resources list, and emits the "changed" signal
 * each time something inside the list changes.
 *
 * #GtkRecentManager objects are expansive: be sure to create them only when
 * needed. You should use the gtk_recent_manager_new_for_screen() or the
 * gtk_recent_manager_get_default() functions instead.
 *
 * Return value: A newly created #GtkRecentManager object.
 *
 * Since: 2.10
 */
GtkRecentManager *
gtk_recent_manager_new (void)
{
  GtkRecentManager *retval;
  gchar *filename;

  filename = g_build_filename (g_get_home_dir (),
                               GTK_RECENTLY_USED_FILE,
			       NULL);
  
  retval = g_object_new (GTK_TYPE_RECENT_MANAGER,
                         "filename", filename,
			 NULL);
  
  g_free (filename);

  return retval;
}

/**
 * gtk_recent_manager_get_default:
 *
 * Gets the recent manager for the default screen. See
 * gtk_recent_manager_get_for_screen().
 *
 * Return value: A unique #GtkRecentManager associated with the
 *   default screen. This recent manager is associated to the
 *   screen and can be used as long as the screen is open.
 *   Do no ref or unref it.
 *
 * Since: 2.10
 */
GtkRecentManager *
gtk_recent_manager_get_default (void)
{
  return gtk_recent_manager_get_for_screen (gdk_screen_get_default ());
}

/**
 * gtk_recent_manager_get_for_screen:
 * @screen: a #GdkScreen
 *
 * Gets the recent manager object associated with @screen; if this
 * function has not previously been called for the given screen,
 * a new recent manager object will be created and associated with
 * the screen. Recent manager objects are fairly expensive to create,
 * so using this function is usually a better choice than calling 
 * gtk_recent_manager_new() and setting the screen yourself; by using
 * this function a single recent manager object will be shared between
 * users.
 *
 * Return value: A unique #GtkRecentManager associated with the given
 *   screen. This recent manager is associated to the with the screen
 *   and can be used as long as the screen is open. Do not ref or
 *   unref it.
 *
 * Since: 2.10
 */
GtkRecentManager *
gtk_recent_manager_get_for_screen (GdkScreen *screen)
{
  GtkRecentManager *manager;

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
  g_return_val_if_fail (!screen->closed, NULL);

  manager = g_object_get_data (G_OBJECT (screen), "gtk-recent-manager-default");
  if (!manager)
    {
      GtkRecentManagerPrivate *priv;
      
      manager = gtk_recent_manager_new ();
      gtk_recent_manager_set_screen (manager, screen);

      priv = manager->priv;
      priv->is_screen_singleton = TRUE;

      g_object_set_data (G_OBJECT (screen), I_("gtk-recent-manager-default"), manager);
    }

  return manager;
}

static void
display_closed (GdkDisplay       *display,
		gboolean          is_error,
		GtkRecentManager *manager)
{
  GtkRecentManagerPrivate *priv = manager->priv;
  GdkScreen *screen = priv->screen;
  gboolean was_screen_singleton = priv->is_screen_singleton;

  if (was_screen_singleton)
    {
      g_object_set_data (G_OBJECT (screen), I_("gtk-recent-manager-error-quark"), NULL);
      priv->is_screen_singleton = FALSE;
    }

  gtk_recent_manager_set_screen (manager, NULL);

  if (was_screen_singleton)
    g_object_unref (manager);
}

static void
unset_screen (GtkRecentManager *manager)
{
  GtkRecentManagerPrivate *priv = manager->priv;
  GdkDisplay *display;

  if (priv->screen)
    {
      display = gdk_screen_get_display (priv->screen);

      g_signal_handlers_disconnect_by_func (display,
					    (gpointer) display_closed,
					    manager);

      priv->screen = NULL;
    }
}

/**
 * gtk_recent_manager_set_screen:
 * @manager: a #GtkRecentManager
 * @screen: a #GdkScreen
 *
 * Sets the screen for a recent manager; the screen is used to
 * track the user's currently configured recently used documents
 * storage.
 * 
 * Since: 2.10
 */
void
gtk_recent_manager_set_screen (GtkRecentManager *manager,
			       GdkScreen        *screen)
{
  GtkRecentManagerPrivate *priv;
  GdkDisplay *display;

  g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
  g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));

  priv = manager->priv;

  unset_screen (manager);

  if (screen)
    {
      display = gdk_screen_get_display (screen);

      priv->screen = screen;

      g_signal_connect (display, "closed",
		        G_CALLBACK (display_closed), manager);
    }
}

/**
 * gtk_recent_manager_set_limit:
746
 * @manager: a #GtkRecentManager
747 748 749 750 751 752 753 754 755
 * @limit: the maximum number of items to return, or -1.
 *
 * Sets the maximum number of item that the gtk_recent_manager_get_items()
 * function should return.  If @limit is set to -1, then return all the
 * items.
 *
 * Since: 2.10
 */
void
756
gtk_recent_manager_set_limit (GtkRecentManager *manager,
757 758 759 760
			      gint              limit)
{
  GtkRecentManagerPrivate *priv;
  
761
  g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
762
  
763
  priv = manager->priv;
764 765 766 767 768
  priv->limit = limit;
}

/**
 * gtk_recent_manager_get_limit:
769
 * @manager: a #GtkRecentManager
770 771 772 773 774 775 776 777 778
 *
 * Gets the maximum number of items that the gtk_recent_manager_get_items()
 * function should return.
 *
 * Return value: the number of items to return, or -1 for every item.
 *
 * Since: 2.10
 */
gint
779
gtk_recent_manager_get_limit (GtkRecentManager *manager)
780 781 782
{
  GtkRecentManagerPrivate *priv;
  
783
  g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), DEFAULT_LIMIT);
784
  
785
  priv = manager->priv;
786 787 788 789 790
  return priv->limit;
}

/**
 * gtk_recent_manager_add_item:
791
 * @manager: a #GtkRecentManager
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
 * @uri: a valid URI
 * @error: return location for a #GError, or %NULL
 *
 * Adds a new resource, pointed by @uri, into the recently used
 * resources list.
 *
 * This function automatically retrieving some of the needed
 * metadata and setting other metadata to common default values; it
 * then feeds the data to gtk_recent_manager_add_full().
 *
 * See gtk_recent_manager_add_full() if you want to explicitely
 * define the metadata for the resource pointed by @uri.
 *
 * Return value: %TRUE if the new item was successfully added
 *   to the recently used resources list
 *
 * Since: 2.10
 */
gboolean
811
gtk_recent_manager_add_item (GtkRecentManager  *manager,
812 813 814 815 816 817 818
			     const gchar       *uri,
			     GError           **error)
{
  GtkRecentData *recent_data;
  GError *add_error;
  gboolean retval;
  
819
  g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
  g_return_val_if_fail (uri != NULL, FALSE);

  recent_data = g_slice_new (GtkRecentData);
  
  recent_data->display_name = NULL;
  recent_data->description = NULL;
  
#ifdef G_OS_UNIX
  if (g_str_has_prefix (uri, "file://"))
    {
      gchar *filename;
      const gchar *mime_type;
      
      filename = g_filename_from_uri (uri, NULL, NULL);
      mime_type = xdg_mime_get_mime_type_for_file (filename, NULL);
      if (!mime_type)
        recent_data->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
      else
        recent_data->mime_type = g_strdup (mime_type);
      
      g_free (filename);
    }
  else
#endif
    recent_data->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
  
  recent_data->app_name = g_strdup (g_get_application_name ());
  recent_data->app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
  
  recent_data->groups = NULL;
  
  recent_data->is_private = FALSE;
  
  add_error = NULL;
854
  retval = gtk_recent_manager_add_full (manager, uri, recent_data, &add_error);
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
  
  g_free (recent_data->mime_type);
  g_free (recent_data->app_name);
  g_free (recent_data->app_exec);

  g_slice_free (GtkRecentData, recent_data);
  
  if (!retval)
    {
      g_propagate_error (error, add_error);
      
      return FALSE;
    }
  
  return retval;
}

/**
 * gtk_recent_manager_add_full:
874
 * @manager: a #GtkRecentManager
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
 * @uri: a valid URI
 * @recent_data: metadata of the resource
 * @error: return location for a #GError, or %NULL
 *
 * Adds a new resource, pointed by @uri, into the recently used
 * resources list, using the metadata specified inside the #GtkRecentData
 * structure passed in @recent_data.
 *
 * The passed URI will be used to identify this resource inside the
 * list.
 *
 * In order to register the new recently used resource, metadata about
 * the resource must be passed as well as the URI; the metadata is
 * stored in a #GtkRecentData structure, which must contain the MIME
 * type of the resource pointed by the URI; the name of the application
 * that is registering the item, and a command line to be used when
 * launching the item.
 *
 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
 * to be used when viewing the item instead of the last component of the
 * URI; a short description of the item; whether the item should be
 * considered private - that is, should be displayed only by the
 * applications that have registered it.
 *
 * Return value: %TRUE if the new item was successfully added to the
 * recently used resources list, %FALSE otherwise.
 *
 * Since: 2.10
 */
gboolean
905
gtk_recent_manager_add_full (GtkRecentManager     *manager,
906 907 908 909 910 911
			     const gchar          *uri,
			     const GtkRecentData  *data,
			     GError              **error)
{
  GtkRecentManagerPrivate *priv;
  
912
  g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
  g_return_val_if_fail (uri != NULL, FALSE);
  g_return_val_if_fail (data != NULL, FALSE);

  /* sanity checks */
  if ((data->display_name) &&
      (!g_utf8_validate (data->display_name, -1, NULL)))
    {
      g_set_error  (error, GTK_RECENT_MANAGER_ERROR,
          	    GTK_RECENT_MANAGER_ERROR_INVALID_ENCODING,
          	    _("The display name of the recently used resource "
          	      "must be a valid UTF-8 encoded string."));
      return FALSE;
    }
  
  if ((data->description) &&
      (!g_utf8_validate (data->description, -1, NULL)))
    {
      g_set_error (error, GTK_RECENT_MANAGER_ERROR,
          	   GTK_RECENT_MANAGER_ERROR_INVALID_ENCODING,
          	   _("The description of the recently used resource "
          	     "must by a valid UTF-8 encoded string."));
      return FALSE;
    }

 
  if (!data->mime_type)
    {
      g_set_error (error, GTK_RECENT_MANAGER_ERROR,
                   GTK_RECENT_MANAGER_ERROR_INVALID_MIME,
		   _("You must specify the MIME type of the "
		     "resource pointed by `%s'"),
		   uri);
      return FALSE;
    }
  
  if (!data->app_name)
    {
      g_set_error (error, GTK_RECENT_MANAGER_ERROR,
      		   GTK_RECENT_MANAGER_ERROR_NOT_REGISTERED,
      		   _("You must specify the name of the application "
      		     "that is registering the recently used resource "
      		     "pointed by `%s'"),
      		   uri);
      return FALSE;
    }
  
  if (!data->app_exec)
    {
      g_set_error (error, GTK_RECENT_MANAGER_ERROR,
                   GTK_RECENT_MANAGER_ERROR_BAD_EXEC_STRING,
		   _("You must specify a command line to "
		     "be used when launching the resource "
		     "pointed by `%s'"),
		   uri);
      return FALSE;
    }
  
970
  priv = manager->priv;
971 972 973 974 975 976 977 978 979 980 981 982 983 984 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

  if (!priv->recent_items)
    {
      priv->recent_items = g_bookmark_file_new ();
      priv->size = 0;
    }

  if (data->display_name)  
    g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
  
  if (data->description)
    g_bookmark_file_set_description (priv->recent_items, uri, data->description);

  g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
  
  if (data->groups && data->groups[0] != '\0')
    {
      gint j;
      
      for (j = 0; (data->groups)[j] != NULL; j++)
        g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
    }
  
  /* register the application; this will take care of updating the
   * registration count and time in case the application has
   * already registered the same document inside the list
   */
  g_bookmark_file_add_application (priv->recent_items, uri,
		  		   data->app_name,
				   data->app_exec);
  
  g_bookmark_file_set_is_private (priv->recent_items, uri,
		  		  data->is_private);
  
  /* mark us as dirty, so that when emitting the "changed" signal we
   * will dump our changes
   */
  priv->is_dirty = TRUE;
  
1010
  gtk_recent_manager_changed (manager);
1011 1012 1013 1014 1015 1016
  
  return TRUE;
}

/**
 * gtk_recent_manager_remove_item:
1017
 * @manager: a #GtkRecentManager
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
 * @uri: the URI of the item you wish to remove
 * @error: return location for a #GError, or %NULL
 *
 * Removes a resource pointed by @uri from the recently used resources
 * list handled by a recent manager.
 *
 * Return value: %TRUE if the item pointed by @uri has been successfully
 *   removed by the recently used resources list, and %FALSE otherwise.
 *
 * Since: 2.10
 */
gboolean
1030
gtk_recent_manager_remove_item (GtkRecentManager  *manager,
1031 1032 1033 1034 1035 1036
				const gchar       *uri,
				GError           **error)
{
  GtkRecentManagerPrivate *priv;
  GError *remove_error = NULL;

1037
  g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1038 1039 1040
  g_return_val_if_fail (uri != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
  
1041
  priv = manager->priv;
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
  
  if (!priv->recent_items)
    {
      priv->recent_items = g_bookmark_file_new ();
      priv->size = 0;

      g_set_error (error, GTK_RECENT_MANAGER_ERROR,
		   GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
		   _("Unable to find an item with URI '%s'"),
		   uri);

      return FALSE;
    }

  g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
  if (remove_error)
    {
      g_propagate_error (error, remove_error);
      
      return FALSE;
    }

  priv->is_dirty = TRUE;

1066
  gtk_recent_manager_changed (manager);
1067 1068 1069 1070 1071 1072
  
  return TRUE;
}

/**
 * gtk_recent_manager_has_item:
1073
 * @manager: a #GtkRecentManager
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
 * @uri: a URI
 *
 * Checks whether there is a recently used resource registered
 * with @uri inside the recent manager.
 *
 * Return value: %TRUE if the resource was found, %FALSE otherwise.
 *
 * Since: 2.10
 */
gboolean
1084
gtk_recent_manager_has_item (GtkRecentManager *manager,
1085 1086 1087 1088
			     const gchar      *uri)
{
  GtkRecentManagerPrivate *priv;

1089
  g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1090 1091
  g_return_val_if_fail (uri != NULL, FALSE);

1092
  priv = manager->priv;
1093 1094 1095 1096 1097 1098 1099 1100 1101 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 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
  g_return_val_if_fail (priv->recent_items != NULL, FALSE);

  return g_bookmark_file_has_item (priv->recent_items, uri);
}

static gboolean
build_recent_info (GBookmarkFile  *bookmarks,
		   GtkRecentInfo  *info)
{
  gchar **apps, **groups;
  gsize apps_len, groups_len, i;

  g_assert (bookmarks != NULL);
  g_assert (info != NULL);
  
  info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
  info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
  info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
    
  info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
  
  info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
  info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
  info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
  
  groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
  for (i = 0; i < groups_len; i++)
    {
      gchar *group_name = g_strdup (groups[i]);
      
      info->groups = g_slist_append (info->groups, group_name);
    }

  g_strfreev (groups);
  
  apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
  for (i = 0; i < apps_len; i++)
    {
      gchar *app_name, *app_exec;
      guint count;
      time_t stamp;
      RecentAppInfo *app_info;
      gboolean res;
      
      app_name = apps[i];
      
      res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
      					  &app_exec,
      					  &count,
      					  &stamp,
      					  NULL);
      if (!res)
        continue;
      
      app_info = recent_app_info_new (app_name);
      app_info->exec = app_exec;
      app_info->count = count;
      app_info->stamp = stamp;
      
      info->applications = g_slist_append (info->applications,
      					   app_info);
      g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
    }
  
  g_strfreev (apps);
  
  return TRUE; 
}

/**
 * gtk_recent_manager_lookup_item:
1164
 * @manager: a #GtkRecentManager
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
 * @uri: a URI
 * @error: a return location for a #GError, or %NULL
 *
 * Searches for a URI inside the recently used resources list, and
 * returns a structure containing informations about the resource
 * like its MIME type, or its display name.
 *
 * Return value: a #GtkRecentInfo structure containing information
 *   about the resource pointed by @uri, or %NULL if the URI was
 *   not registered in the recently used resources list.  Free with
 *   gtk_recent_info_unref().
 **/
GtkRecentInfo *
1178
gtk_recent_manager_lookup_item (GtkRecentManager  *manager,
1179 1180 1181 1182 1183 1184 1185
				const gchar       *uri,
				GError           **error)
{
  GtkRecentManagerPrivate *priv;
  GtkRecentInfo *info = NULL;
  gboolean res;
  
1186
  g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1187 1188 1189
  g_return_val_if_fail (uri != NULL, NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
  
1190
  priv = manager->priv;
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
  if (!priv->recent_items)
    {
      priv->recent_items = g_bookmark_file_new ();
      priv->size = 0;

      g_set_error (error, GTK_RECENT_MANAGER_ERROR,
		   GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
		   _("Unable to find an item with URI '%s'"),
		   uri);

      return NULL;
    }
  
  if (!g_bookmark_file_has_item (priv->recent_items, uri))
    {
      g_set_error (error, GTK_RECENT_MANAGER_ERROR,
      		   GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
      		   _("Unable to find an item with URI '%s'"),
      		   uri);
      return NULL;
    }
  
  info = gtk_recent_info_new (uri);
  g_return_val_if_fail (info != NULL, NULL);
  
  /* fill the RecentInfo structure with the data retrieved by our
   * parser object from the storage file 
   */
  res = build_recent_info (priv->recent_items, info);
  if (!res)
    {
      gtk_recent_info_free (info);
      
      return NULL;
    }
 
  return gtk_recent_info_ref (info);
}

/**
 * gtk_recent_manager_move_item:
 * @manager: a #GtkRecentManager
 * @uri: the URI of a recently used resource
 * @new_uri: the new URI of the recently used resource, or %NULL to
 *    remove the item pointed by @uri in the list
 * @error: a return location for a #GError, or %NULL
 *
 * Changes the location of a recently used resource from @uri to @new_uri.
 * 
 * Please note that this function will not affect the resource pointed
 * by the URIs, but only the URI used in the recently used resources list.
 *
 * Return value: %TRUE on success.
 *
 * Since: 2.10
 */ 
gboolean
gtk_recent_manager_move_item (GtkRecentManager  *recent_manager,
			      const gchar       *uri,
			      const gchar       *new_uri,
			      GError           **error)
{
  GtkRecentManagerPrivate *priv;
  GError *move_error;
  gboolean res;
  
  g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
  g_return_val_if_fail (uri != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
  
  priv = recent_manager->priv;

  if (!g_bookmark_file_has_item (priv->recent_items, uri))
    {
      g_set_error (error, GTK_RECENT_MANAGER_ERROR,
      		   GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
      		   _("Unable to find an item with URI '%s'"),
      		   uri);
      return FALSE;
    }
  
  move_error = NULL;
  res = g_bookmark_file_move_item (priv->recent_items,
                                   uri, new_uri,
                                   &move_error);
  if (move_error)
    {
      g_propagate_error (error, move_error);
      return FALSE;
    }
  
  priv->is_dirty = TRUE;

  gtk_recent_manager_changed (recent_manager);
  
  return TRUE;
}

/**
 * gtk_recent_manager_get_items:
1291
 * @manager: a #GtkRecentManager
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
 *
 * Gets the list of recently used resources.
 *
 * Return value: a list of newly allocated #GtkRecentInfo objects. Use
 *   gtk_recent_info_unref() on each item inside the list, and then
 *   free the list itself using g_list_free().
 *
 * Since: 2.10
 */
GList *
1302
gtk_recent_manager_get_items (GtkRecentManager *manager)
1303 1304 1305 1306 1307 1308
{
  GtkRecentManagerPrivate *priv;
  GList *retval = NULL;
  gchar **uris;
  gsize uris_len, i;
  
1309
  g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1310
  
1311
  priv = manager->priv;
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 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
  if (!priv->recent_items)
    return NULL;
  
  uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
  for (i = 0; i < uris_len; i++)
    {
      GtkRecentInfo *info;
      gboolean res;
      
      info = gtk_recent_info_new (uris[i]);
      res = build_recent_info (priv->recent_items, info);
      if (!res)
        {
          g_warning ("Unable to create a RecentInfo object for "
                     "item with URI `%s'",
                     uris[i]);
          gtk_recent_info_free (info);
	  
          continue;
        }
      
      retval = g_list_prepend (retval, info);
    }
  
  g_strfreev (uris);
    
  /* clamp the list, if a limit is present */
  if ((priv->limit != -1) &&
      (g_list_length (retval) > priv->limit))
    {
      GList *clamp, *l;
      
      clamp = g_list_nth (retval, priv->limit - 1);
      
      if (!clamp)
        return retval;
      
      l = clamp->next;
      clamp->next = NULL;
      
      g_list_foreach (l, (GFunc) gtk_recent_info_free, NULL);
      g_list_free (l);
    }
  
  return retval;
}

static void
purge_recent_items_list (GtkRecentManager  *manager,
			 GError           **error)
{
  GtkRecentManagerPrivate *priv = manager->priv;

  if (!priv->recent_items)
    return;
  
  g_bookmark_file_free (priv->recent_items);
  priv->recent_items = NULL;
      
  priv->recent_items = g_bookmark_file_new ();
  priv->size = 0;
  priv->is_dirty = TRUE;
      
  /* emit the changed signal, to ensure that the purge is written */
  gtk_recent_manager_changed (manager);
}

/**
 * gtk_recent_manager_purge_items:
1381
 * @manager: a #GtkRecentManager
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
 * @error: a return location for a #GError, or %NULL
 *
 * Purges every item from the recently used resources list.
 *
 * Return value: the number of items that have been removed from the
 *   recently used resources list.
 *
 * Since: 2.10
 */
gint
1392
gtk_recent_manager_purge_items (GtkRecentManager  *manager,
1393 1394 1395 1396 1397
				GError           **error)
{
  GtkRecentManagerPrivate *priv;
  gint count, purged;
  
1398
  g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1399

1400
  priv = manager->priv;
1401 1402 1403 1404 1405 1406 1407
  if (!priv->recent_items)
    return 0;
  
  count = g_bookmark_file_get_size (priv->recent_items);
  if (!count)
    return 0;
  
1408
  purge_recent_items_list (manager, error);
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 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 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 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 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 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
  
  purged = count - g_bookmark_file_get_size (priv->recent_items);

  return purged;
}

static void
gtk_recent_manager_changed (GtkRecentManager *recent_manager)
{
  g_signal_emit (recent_manager, signal_changed, 0);
}

/*****************
 * GtkRecentInfo *
 *****************/
 
GType
gtk_recent_info_get_type (void)
{
  static GType info_type = 0;
  
  if (!info_type)
    info_type = g_boxed_type_register_static ("GtkRecentInfo",
    					      (GBoxedCopyFunc) gtk_recent_info_ref,
    					      (GBoxedFreeFunc) gtk_recent_info_unref);
  return info_type;
}

static GtkRecentInfo *
gtk_recent_info_new (const gchar *uri)
{
  GtkRecentInfo *info;

  g_assert (uri != NULL);

  info = g_new0 (GtkRecentInfo, 1);
  info->uri = g_strdup (uri);
  
  info->applications = NULL;
  info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
  
  info->groups = NULL;
  
  info->ref_count = 1;

  return info;
}

static void
gtk_recent_info_free (GtkRecentInfo *recent_info)
{
  if (!recent_info)
    return;

  g_free (recent_info->uri);
  g_free (recent_info->display_name);
  g_free (recent_info->description);
  g_free (recent_info->mime_type);
  
  if (recent_info->applications)
    {
      g_slist_foreach (recent_info->applications,
      		       (GFunc) recent_app_info_free,
      		       NULL);
      g_slist_free (recent_info->applications);
      
      recent_info->applications = NULL;
    }
  
  if (recent_info->apps_lookup)
    g_hash_table_destroy (recent_info->apps_lookup);

  if (recent_info->groups)
    {
      g_slist_foreach (recent_info->groups,
		       (GFunc) g_free,
		       NULL);
      g_slist_free (recent_info->groups);

      recent_info->groups = NULL;
    }
  
  if (recent_info->icon)
    g_object_unref (recent_info->icon);

  g_free (recent_info);
}

/**
 * gtk_recent_info_ref:
 * @info: a #GtkRecentInfo
 *
 * Increases the reference count of @recent_info by one.
 *
 * Return value: the recent info object with its reference count increased
 *   by one.
 *
 * Since: 2.10
 */
GtkRecentInfo *
gtk_recent_info_ref (GtkRecentInfo *info)
{
  g_return_val_if_fail (info != NULL, NULL);
  g_return_val_if_fail (info->ref_count > 0, NULL);
  
  info->ref_count += 1;
    
  return info;
}

/**
 * gtk_recent_info_unref:
 * @info: a #GtkRecentInfo
 *
 * Decreases the reference count of @info by one.  If the reference
 * count reaches zero, @info is deallocated, and the memory freed.
 *
 * Since: 2.10
 */
void
gtk_recent_info_unref (GtkRecentInfo *info)
{
  g_return_if_fail (info != NULL);
  g_return_if_fail (info->ref_count > 0);

  info->ref_count -= 1;
  
  if (info->ref_count == 0)
    gtk_recent_info_free (info);
}

/**
 * gtk_recent_info_get_uri:
 * @info: a #GtkRecentInfo
 *
 * Gets the URI of the resource.
 *
 * Return value: the URI of the resource.  The returned string is
 *   owned by the recent manager, and should not be freed.
 *
 * Since: 2.10
 */
G_CONST_RETURN gchar *
gtk_recent_info_get_uri (GtkRecentInfo *info)
{
  g_return_val_if_fail (info != NULL, NULL);
  
  return info->uri;
}

/**
 * gtk_recent_info_get_display_name:
 * @info: a #GtkRecentInfo
 *
 * Gets the name of the resource.  If none has been defined, the basename
 * of the resource is obtained.
 *
 * Return value: the display name of the resource.  The returned string
 *   is owned by the recent manager, and should not be freed.
 *
 * Since: 2.10
 */
G_CONST_RETURN gchar *
gtk_recent_info_get_display_name (GtkRecentInfo *info)
{
  g_return_val_if_fail (info != NULL, NULL);
  
  if (!info->display_name)
    info->display_name = gtk_recent_info_get_short_name (info);
  
  return info->display_name;
}

/**
 * gtk_recent_info_get_description:
 * @info: a #GtkRecentInfo
 *
 * Gets the (short) description of the resource.
 *
 * Return value: the description of the resource.  The returned string
 *   is owned by the recent manager, and should not be freed.
 *
 * Since: 2.10
 **/
G_CONST_RETURN gchar *
gtk_recent_info_get_description (GtkRecentInfo *info)
{
  g_return_val_if_fail (info != NULL, NULL);
  
  return info->description;
}

/**
 * gtk_recent_info_get_mime_type:
 * @info: a #GtkRecentInfo
 *
 * Gets the MIME type of the resource.
 *
 * Return value: the MIME type of the resource.  The returned string
 *   is owned by the recent manager, and should not be freed.
 *
 * Since: 2.10
 */
G_CONST_RETURN gchar *
gtk_recent_info_get_mime_type (GtkRecentInfo *info)
{
  g_return_val_if_fail (info != NULL, NULL);
  
  if (!info->mime_type)
    info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
  
  return info->mime_type;
}

/**
 * gtk_recent_info_get_added:
 * @info: a #GtkRecentInfo
 *
 * Gets the timestamp (seconds from system's Epoch) when the resource
 * was added to the recently used resources list.
 *
 * Return value: the number of seconds elapsed from system's Epoch when
 *   the resource was added to the list, or -1 on failure.
 *
 * Since: 2.10
 */
time_t
gtk_recent_info_get_added (GtkRecentInfo *info)
{
  g_return_val_if_fail (info != NULL, (time_t) -1);
  
  return info->added;
}

/**
 * gtk_recent_info_get_modified:
 * @info: a #GtkRecentInfo
 *
 * Gets the timestamp (seconds from system's Epoch) when the resource
 * was last modified.
 *
 * Return value: the number of seconds elapsed from system's Epoch when
 *   the resource was last modified, or -1 on failure.
 *
 * Since: 2.10
 */
time_t
gtk_recent_info_get_modified (GtkRecentInfo *info)
{
  g_return_val_if_fail (info != NULL, (time_t) -1);
  
  return info->modified;
}

/**
 * gtk_recent_info_get_visited:
 * @info: a #GtkRecentInfo
 *
 * Gets the timestamp (seconds from system's Epoch) when the resource
 * was last visited.
 *
 * Return value: the number of seconds elapsed from system's Epoch when
 *   the resource was last visited, or -1 on failure.
 *
 * Since: 2.10
 */
time_t
gtk_recent_info_get_visited (GtkRecentInfo *info)
{
  g_return_val_if_fail (info != NULL, (time_t) -1);
  
  return info->visited;
}

/**
 * gtk_recent_info_get_private_hint:
 * @info: a #GtkRecentInfo
 *
 * Gets the value of the "private" flag.  Resources in the recently used
 * list that have this flag set to %TRUE should only be displayed by the
 * applications that have registered them.
 *
 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
 *
 * Since: 2.10
 */
gboolean
gtk_recent_info_get_private_hint (GtkRecentInfo *info)
{
  g_return_val_if_fail (info != NULL, FALSE);
  
  return info->is_private;
}


static RecentAppInfo *
recent_app_info_new (const gchar *app_name)
{
  RecentAppInfo *app_info;

  g_assert (app_name != NULL);
  
  app_info = g_new0 (RecentAppInfo, 1);
  app_info->name = g_strdup (app_name);
  app_info->exec = NULL;
  app_info->count = 1;
  app_info->stamp = time (NULL);
  
  return app_info;
}

static void
recent_app_info_free (RecentAppInfo *app_info)
{
  if (!app_info)
    return;
  
  if (app_info->name)
    g_free (app_info->name);
  
  if (app_info->exec)
    g_free (app_info->exec);
  
  g_free (app_info);
}

/**
 * gtk_recent_info_get_application_info:
 * @info: a #GtkRecentInfo
 * @app_name: the name of the application that has registered this item
 * @app_exec: return location for the string containing the command line
 * @count: return location for the number of times this item was registered
 * @time: return location for the timestamp this item was last registered
 *    for this application
 *
 * Gets the data regarding the application that has registered the resource
 * pointed by @info.
 *
 * If the command line contains any escape characters defined inside the
 * storage specification, they will be expanded.
 *
 * Return value: %TRUE if an application with @app_name has registered this
 *   resource inside the recently used list, or %FALSE otherwise.  You should
 *   free the returned command line using g_free().
 *
 * Since: 2.10
 */
gboolean
gtk_recent_info_get_application_info (GtkRecentInfo  *info,
				      const gchar    *app_name,
				      gchar         **app_exec,
				      guint          *count,
				      time_t         *time)
{
  RecentAppInfo *ai;
  
  g_return_val_if_fail (info != NULL, FALSE);
  g_return_val_if_fail (app_name != NULL, FALSE);
  
  ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
  					      app_name);
  if (!ai)
    {
      g_warning ("No registered application with name '%s' "
                 "for item with URI '%s' found",
                 app_name,
                 info->uri);
      return FALSE;
    }
  
  if (app_exec)
    *app_exec = ai->exec;
  
  if (count)
    *count = ai->count;
  
  if (time)
    *time = ai->stamp;

  return TRUE;
}

/**
 * gtk_recent_info_get_applications:
 * @info: a #GtkRecentInfo
 * @length: return location for the length of the returned list, or %NULL
 *
 * Retrieves the list of applications that have registered this resource.
 *
 * Return value: a newly allocated %NULL-terminated array of strings.
 *   Use g_strfreev() to free it.
 *
 * Since: 2.10
 */ 			      
gchar **
gtk_recent_info_get_applications (GtkRecentInfo *info,
				  gsize         *length)
{
  GSList *l;
  gchar **retval;
  gsize n_apps, i;
  
  g_return_val_if_fail (info != NULL, NULL);
  
  if (!info->applications)
    {
      if (length)
        *length = 0;
      
      return NULL;    
    }
  
  n_apps = g_slist_length (info->applications);
  
  retval = g_new0 (gchar *, n_apps + 1);
  
  for (l = info->applications, i = 0;
       l != NULL;
       l = l->next)
    {
      RecentAppInfo *ai = (RecentAppInfo *) l->data;
      
      g_assert (ai != NULL);
      
      retval[i++] = g_strdup (ai->name);
    }
  retval[i] = NULL;
  
  if (length)
    *length = i;
  
  return retval;
}

/**
 * gtk_recent_info_has_application:
 * @info: a #GtkRecentInfo
 * @app_name: a string containing an application name
 *
 * Checks whether an application registered this resource using @app_name.
 *
 * Return value: %TRUE if an application with name @app_name was found,
 *   %FALSE otherwise.
 *
 * Since: 2.10
 */
gboolean
gtk_recent_info_has_application (GtkRecentInfo *info,
				 const gchar   *app_name)
{
  g_return_val_if_fail (info != NULL, FALSE);
  g_return_val_if_fail (app_name != NULL, FALSE);
  
  return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
}

/**
 * gtk_recent_info_last_application:
 * @info: a #GtkRecentInfo
 *
 * Gets the name of the last application that have registered the
 * recently used resource represented by @info.
 *
 * Return value: an application name.  Use g_free() to free it.
 *
 * Since: 2.10
 */
gchar *
gtk_recent_info_last_application (GtkRecentInfo  *info)
{
  GSList *l;
  time_t last_stamp = (time_t) -1;
  gchar *name = NULL;
  
  g_return_val_if_fail (info != NULL, NULL);
  
  for (l = info->applications; l != NULL; l = l->next)
    {
      RecentAppInfo *ai = (RecentAppInfo *) l->data;
      
      if (ai->stamp > last_stamp)
        name = ai->name;
    }
  
  return g_strdup (name);
}

typedef struct
{
  gint size;
  GdkPixbuf *pixbuf;
} IconCacheElement;

static void
icon_cache_element_free (IconCacheElement *element)
{
  if (element->pixbuf)
    g_object_unref (element->pixbuf);
  g_free (element);
}

static void
icon_theme_changed (GtkIconTheme     *icon_theme)
{
  GHashTable *cache;

  /* Difference from the initial creation is that we don't
   * reconnect the signal
   */
  cache = g_hash_table_new_full (g_str_hash, g_str_equal,
				 (GDestroyNotify)g_free,
				 (GDestroyNotify)icon_cache_element_free);
  g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
			  cache, (GDestroyNotify)g_hash_table_destroy);
}

/* TODO: use the GtkFileChooser's icon cache instead of our own to reduce
 * the memory footprint
 */
static GdkPixbuf *
get_cached_icon (const gchar *name,
		 gint         pixel_size)
{
  GtkIconTheme *icon_theme;
  GHashTable *cache;
  IconCacheElement *element;

  icon_theme = gtk_icon_theme_get_default ();
  cache = g_object_get_data (G_OBJECT (icon_theme), "gtk-recent-icon-cache");

  if (!cache)
    {
      cache = g_hash_table_new_full (g_str_hash, g_str_equal,
				     (GDestroyNotify)g_free,
				     (GDestroyNotify)icon_cache_element_free);

      g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
			      cache, (GDestroyNotify)g_hash_table_destroy);
      g_signal_connect (icon_theme, "changed",
			G_CALLBACK (icon_theme_changed), NULL);
    }

  element = g_hash_table_lookup (cache, name);
  if (!element)
    {
      element = g_new0 (IconCacheElement, 1);
      g_hash_table_insert (cache, g_strdup (name), element);
    }

  if (element->size != pixel_size)
    {
      if (element->pixbuf)
	g_object_unref (element->pixbuf);

      element->size = pixel_size;
      element->pixbuf = gtk_icon_theme_load_icon (icon_theme, name,
						  pixel_size, 0, NULL);
    }

  return element->pixbuf ? g_object_ref (element->pixbuf) : NULL;
}


static GdkPixbuf *
get_icon_for_mime_type (const char *mime_type,
			gint        pixel_size)
{
  const char *separator;
  GString *icon_name;
  GdkPixbuf *pixbuf;

  separator = strchr (mime_type, '/');
  if (!separator)
    return NULL; /* maybe we should return a GError with "invalid MIME-type" */

  icon_name = g_string_new ("gnome-mime-");
  g_string_append_len (icon_name, mime_type, separator - mime_type);
  g_string_append_c (icon_name, '-');
  g_string_append (icon_name, separator + 1);
  pixbuf = get_cached_icon (icon_name->str, pixel_size);
  g_string_free (icon_name, TRUE);
  if (pixbuf)
    return pixbuf;

  icon_name = g_string_new ("gnome-mime-");
  g_string_append_len (icon_name, mime_type, separator - mime_type);
  pixbuf = get_cached_icon (icon_name->str, pixel_size);
  g_string_free (icon_name, TRUE);

  return pixbuf;
}

static GdkPixbuf *
get_icon_fallback (const gchar *icon_name,
		   gint         size)
{
  GtkIconTheme *icon_theme;
  GdkPixbuf *retval;

  icon_theme = gtk_icon_theme_get_default ();
  
  retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
  				     size,
  				     GTK_ICON_LOOKUP_USE_BUILTIN,
  				     NULL);
  g_assert (retval != NULL);
  
  return retval; 
}

/**
 * gtk_recent_info_get_icon:
 * @info: a #GtkRecentInfo
 * @size: the size of the icon in pixels
 *
 * Retrieves the icon of size @size associated to the resource MIME type.
 *
 * Return value: a #GdkPixbuf containing the icon, or %NULL.
 *
 * Since: 2.10
 */
GdkPixbuf *
gtk_recent_info_get_icon (GtkRecentInfo *info,
			  gint           size)
{
  GdkPixbuf *retval = NULL;
  
  g_return_val_if_fail (info != NULL, NULL);
  
  if (info->mime_type)
    retval = get_icon_for_mime_type (info->mime_type, size);

  /* this should never fail */  
  if (!retval)
    retval = get_icon_fallback (GTK_STOCK_FILE, size);
  
  return retval;
}

/**
 * gtk_recent_info_is_local:
 * @info: a #GtkRecentInfo
 *
 * Checks whether the resource is local or not by looking at the
 * scheme of its URI.
 *
 * Return value: %TRUE if the resource is local.
 *
 * Since: 2.10
 */
gboolean
gtk_recent_info_is_local (GtkRecentInfo *info)
{
  g_return_val_if_fail (info != NULL, FALSE);
  
  return g_str_has_prefix (info->uri, "file://");
}

/**
 * gtk_recent_info_exists:
 * @info: a #GtkRecentInfo
 *
 * Checks whether the resource pointed by @info still exists.  At
 * the moment this check is done only on resources pointing to local files.
 *
 * Return value: %TRUE if the resource exists
 *
 * Since: 2.10
 */
gboolean
gtk_recent_info_exists (GtkRecentInfo *info)
{
  gchar *filename;
  struct stat stat_buf;
  gboolean retval = FALSE;
  
  g_return_val_if_fail (info != NULL, FALSE);
  
  /* we guarantee only local resources */
  if (!gtk_recent_info_is_local (info))
    return FALSE;
  
  filename = g_filename_from_uri (info->uri, NULL, NULL);
  if (filename)
    {
      if (stat (filename, &stat_buf) == 0)
        retval = TRUE;
     
      g_free (filename);
    }
  
  return retval;
}

/**
 * gtk_recent_info_match:
2105 2106
 * @info_a: a #GtkRecentInfo
 * @info_b: a #GtkRecentInfo
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
 *
 * Checks whether two #GtkRecentInfo structures point to the same
 * resource.
 *
 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
 *   resource, %FALSE otherwise.
 *
 * Since: 2.10
 */
gboolean
2117 2118
gtk_recent_info_match (GtkRecentInfo *info_a,
		       GtkRecentInfo *info_b)
2119
{
2120 2121
  g_return_val_if_fail (info_a != NULL, FALSE);
  g_return_val_if_fail (info_b != NULL, FALSE);
2122
  
2123
  return (0 == strcmp (info_a->uri, info_b->uri));
2124 2125 2126 2127
}

/* taken from gnome-vfs-uri.c */
static const gchar *
2128 2129
get_method_string (const gchar  *substring, 
		   gchar       **method_string)
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
{
  const gchar *p;
  char *method;
	
  for (p = substring;
       g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
       p++)
    ;

  if (*p == ':'
#ifdef G_OS_WIN32
                &&
      !(p == substring + 1 && g_ascii_isalpha (*substring))
#endif
							   )
    {
      /* Found toplevel method specification.  */
      method = g_strndup (substring, p - substring);
      *method_string = g_ascii_strdown (method, -1);
      g_free (method);
      p++;
    }
  else
    {
      *method_string = g_strdup ("file");
      p = substring;
    }
  
  return p;
}

/* Stolen from gnome_vfs_make_valid_utf8() */
static char *
make_valid_utf8 (const char *name)
{
  GString *string;
  const char *remainder, *invalid;
  int remaining_bytes, valid_bytes;

  string = NULL;
  remainder = name;
  remaining_bytes = name ? strlen (name) : 0;

  while (remaining_bytes != 0)
    {
      if (g_utf8_validate (remainder, remaining_bytes, &invalid))
        break;
      
      valid_bytes = invalid - remainder;
      
      if (string == NULL)
        string = g_string_sized_new (remaining_bytes);
      
      g_string_append_len (string, remainder, valid_bytes);
      g_string_append_c (string, '?');
      
      remaining_bytes -= valid_bytes + 1;
      remainder = invalid + 1;
    }
  
  if (string == NULL)
    return g_strdup (name);

  g_string_append (string, remainder);
  g_assert (g_utf8_validate (string->str, -1, NULL));

  return g_string_free (string, FALSE);
}

static gchar *
get_uri_shortname_for_display (const gchar *uri)
{
  gchar *name = NULL;
  gboolean validated = FALSE;

  if (g_str_has_prefix (uri, "file://"))
    {
      gchar *local_file;
      
      local_file = g_filename_from_uri (uri, NULL, NULL);
      
      if (local_file != NULL)
        {
          name = g_filename_display_basename (local_file);
          validated = TRUE;
        }
		
      g_free (local_file);
    } 
  else
    {
      gchar *method;
      gchar *local_file;
      const gchar *rest;
      
      rest = get_method_string (uri, &method);
      local_file = g_filename_display_basename (rest);
      
      name = g_strdup_printf ("%s: %s", method, local_file);
      
      g_free (local_file);
      g_free (method);
    }
  
  g_assert (name != NULL);
  
  if (!validated && !g_utf8_validate (name, -1, NULL))
    {
      gchar *utf8_name;
      
      utf8_name = make_valid_utf8 (name);
      g_free (name);
      
      name = utf8_name;
    }

  return name;
}

/**
 * gtk_recent_info_get_short_name:
 * @info: an #GtkRecentInfo
 *
 * Computes a valid UTF-8 string that can be used as the name of the item in a
 * menu or list.  For example, calling this function on an item that refers to
 * "file:///foo/bar.txt" will yield "bar.txt".
 *
 * Return value: A newly-allocated string in UTF-8 encoding; free it with
 *   g_free().
 *
 * Since: 2.10
 */
gchar *
gtk_recent_info_get_short_name (GtkRecentInfo *info)
{
  gchar *short_name;

  g_return_val_if_fail (info != NULL, NULL);

  if (info->uri == NULL)
    return NULL;

  short_name = get_uri_shortname_for_display (info->uri);

  return short_name;
}

/**
 * gtk_recent_info_get_uri_display:
 * @info: a #GtkRecentInfo
 *
 * Gets a displayable version of the resource's URI.
 *
 * Return value: a UTF-8 string containing the resource's URI or %NULL
 *
 * Since: 2.10
 */
gchar *
gtk_recent_info_get_uri_display (GtkRecentInfo *info)
{
  gchar *filename, *filename_utf8;
  
  g_return_val_if_fail (info != NULL, NULL);
  
  filename = g_filename_from_uri (info->uri, NULL, NULL);
  if (!filename)
    return NULL;
      
  filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
  g_free (filename);

  return filename_utf8;
}

/**
 * gtk_recent_info_get_age:
 * @info: a #GtkRecentInfo
 *
 * Gets the number of days elapsed since the last update of the resource
 * pointed by @info.
 *
 * Return value: a positive integer containing the number of days elapsed
 *   since the time this resource was last modified.  On failure, -1 is
 *   returned.
 *
 * Since: 2.10
 */
gint
gtk_recent_info_get_age (GtkRecentInfo *info)
{
  time_t now, delta;
  gint retval;

  g_return_val_if_fail (info != NULL, -1);

  now = time (NULL);
  
  delta = now - info->modified;
  g_assert (delta >= 0);
  
  retval = (gint) (delta / (60 * 60 * 24));
  
  return retval;
}

/**
 * gtk_recent_info_get_groups:
 * @info: a #GtkRecentInfo
 * @length: return location for the number of groups returned, or %NULL
 *
 * Returns all groups registered for the recently used item @info.  The
 * array of returned group names will be %NULL terminated, so length might
 * optionally be %NULL.
 *
 * Return value: a newly allocated %NULL terminated array of strings.  Use
 *   g_strfreev() to free it.
 *
 * Since: 2.10
 */
gchar **
gtk_recent_info_get_groups (GtkRecentInfo *info,
			    gsize         *length)
{
  GSList *l;
  gchar **retval;
  gsize n_groups, i;
  
  g_return_val_if_fail (info != NULL, NULL);
  
  if (!info->groups)
    {
      if (length)
        *length = 0;
      
      return NULL;
    }
  
  n_groups = g_slist_length (info->groups);
  
  retval = g_new0 (gchar *, n_groups + 1);
  
  for (l = info->groups, i = 0;
       l != NULL;
       l = l->next)
    {
      gchar *group_name = (gchar *) l->data;
      
      g_assert (group_name != NULL);
      
      retval[i++] = g_strdup (group_name);
    }
  retval[i] = NULL;
  
  if (length)
    *length = i;
  
  return retval;
}

/**
 * gtk_recent_info_has_group:
 * @info: a #GtkRecentInfo
 * @group_name: name of a group
 *
 * Checks whether @group_name appears inside the groups registered for the
 * recently used item @info.
 *
 * Return value: %TRUE if the group was found.
 *
 * Since: 2.10
 */
gboolean
gtk_recent_info_has_group (GtkRecentInfo *info,
			   const gchar   *group_name)
{
  GSList *l;
  
  g_return_val_if_fail (info != NULL, FALSE);
  g_return_val_if_fail (group_name != NULL, FALSE);

  if (!info->groups)
    return FALSE;

  for (l = info->groups; l != NULL; l = l->next)
    {
      gchar *g = (gchar *) l->data;

      if (strcmp (g, group_name) == 0)
        return TRUE;
    }

  return FALSE;
}

#define __GTK_RECENT_MANAGER_C__
#include "gtkaliasdef.c"