]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkrecentmanager.c
gtk/gtkmenushell.c: use accessor functions to access GtkWidget
[~andy/gtk] / gtk / gtkrecentmanager.c
index c7ee3b3280bf18d3eabfd02f692bb4e520c3306c..6e05a05336cc54ea4d5b948365bbdbd3f172978f 100644 (file)
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  */
 
+/**
+ * SECTION:gtkrecentmanager
+ * @Title: GtkRecentManager
+ * @short_description: Managing recently used files
+ * @See_Also: #GBookmarkFile, #GtkSettings, #GtkRecentChooser
+ *
+ * #GtkRecentManager provides a facility for adding, removing and
+ * looking up recently used files. Each recently used file is
+ * identified by its URI, and has meta-data associated to it, like
+ * the names and command lines of the applications that have
+ * registered it, the number of time each application has registered
+ * the same file, the mime type of the file and whether the file
+ * should be displayed only by the applications that have
+ * registered it.
+ *
+ * <note><para>The recently used files list is per user.</para></note>
+ *
+ * The #GtkRecentManager acts like a database of all the recently
+ * used files. You can create new #GtkRecentManager objects, but
+ * it is more efficient to use the default manager created by GTK+.
+ *
+ * Adding a new recently used file is as simple as:
+ *
+ * |[
+ * GtkRecentManager *manager;
+ *
+ * manager = gtk_recent_manager_get_default ();
+ * gtk_recent_manager_add_item (manager, file_uri);
+ * ]|
+ *
+ * The #GtkRecentManager will try to gather all the needed information
+ * from the file itself through GIO.
+ *
+ * Looking up the meta-data associated with a recently used file
+ * given its URI requires calling gtk_recent_manager_lookup_item():
+ *
+ * |[
+ * GtkRecentManager *manager;
+ * GtkRecentInfo *info;
+ * GError *error = NULL;
+ *
+ * manager = gtk_recent_manager_get_default ();
+ * info = gtk_recent_manager_lookup_item (manager, file_uri, &amp;error);
+ * if (error)
+ *   {
+ *     g_warning ("Could not find the file: &percnt;s", error-&gt;message);
+ *     g_error_free (error);
+ *   }
+ * else
+ *  {
+ *    /&ast; Use the info object &ast;/
+ *    gtk_recent_info_unref (info);
+ *  }
+ * ]|
+ *
+ * In order to retrieve the list of recently used files, you can use
+ * gtk_recent_manager_get_items(), which returns a list of #GtkRecentInfo
+ * structures.
+ *
+ * A #GtkRecentManager is the model used to populate the contents of
+ * one, or more #GtkRecentChooser implementations.
+ *
+ * <note><para>The maximum age of the recently used files list is
+ * controllable through the #GtkSettings:gtk-recent-files-max-age
+ * property.</para></note>
+ *
+ * Recently used files are supported since GTK+ 2.10.
+ */
+
 #include "config.h"
 
 #include <sys/types.h>
 #include "gtktypebuiltins.h"
 #include "gtkprivate.h"
 #include "gtkmarshalers.h"
-#include "gtkalias.h"
 
 /* the file where we store the recently used items */
 #define GTK_RECENTLY_USED_FILE ".recently-used.xbel"
@@ -60,6 +128,17 @@ typedef struct
   time_t stamp;
 } RecentAppInfo;
 
+/**
+ * GtkRecentInfo:
+ *
+ * #GtkRecentInfo is an opaque data structure
+ * whose members can only be accessed using the provided API.
+ *
+ * #GtkRecentInfo constains all the meta-data
+ * associated with an entry in the recently used files list.
+ *
+ * Since: 2.10
+ */
 struct _GtkRecentInfo
 {
   gchar *uri;
@@ -85,15 +164,12 @@ struct _GtkRecentInfo
   gint ref_count;
 };
 
-#define GTK_RECENT_MANAGER_GET_PRIVATE(obj)     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RECENT_MANAGER, GtkRecentManagerPrivate))
-
 struct _GtkRecentManagerPrivate
 {
   gchar *filename;
 
   guint is_dirty : 1;
   
-  gint limit;
   gint size;
 
   GBookmarkFile *recent_items;
@@ -215,23 +291,7 @@ gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
                                                        P_("The full path to the file to be used to store and read the list"),
                                                        NULL,
                                                        (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)));
-  /**
-   * 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
    * 
@@ -254,7 +314,8 @@ gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
    * @recent_manager: the recent manager
    *
    * Emitted when the current recently used resources manager changes its
-   * contents.
+   * contents, either by calling gtk_recent_manager_add_item() or by another
+   * application.
    *
    * Since: 2.10
    */
@@ -277,11 +338,12 @@ gtk_recent_manager_init (GtkRecentManager *manager)
 {
   GtkRecentManagerPrivate *priv;
 
-  manager->priv = priv = GTK_RECENT_MANAGER_GET_PRIVATE (manager);
-  
-  priv->limit = DEFAULT_LIMIT;
-  priv->size = 0;
+  manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
+                                               GTK_TYPE_RECENT_MANAGER,
+                                               GtkRecentManagerPrivate);
+  priv = manager->priv;
 
+  priv->size = 0;
   priv->filename = NULL;
 }
 
@@ -297,9 +359,6 @@ gtk_recent_manager_set_property (GObject               *object,
     {
     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);
@@ -320,9 +379,6 @@ gtk_recent_manager_get_property (GObject               *object,
     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;
@@ -416,6 +472,14 @@ gtk_recent_manager_real_changed (GtkRecentManager *manager)
          g_error_free (write_error);
        }
 
+      if (g_chmod (priv->filename, 0600) < 0)
+        {
+          filename_warning ("Attempting to set the permissions of `%s', "
+                            "but failed: %s",
+                            priv->filename,
+                            g_strerror (errno));
+        }
+
       /* mark us as clean */
       priv->is_dirty = FALSE;
     }
@@ -619,7 +683,7 @@ gtk_recent_manager_new (void)
  * in your application without caring about memory management. The
  * returned instance will be freed when you application terminates.
  *
- * Return value: A unique #GtkRecentManager. Do not ref or unref it.
+ * Return value: (transfer none): A unique #GtkRecentManager. Do not ref or unref it.
  *
  * Since: 2.10
  */
@@ -632,102 +696,6 @@ gtk_recent_manager_get_default (void)
   return recent_manager_singleton;
 }
 
-/**
- * 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.
- *
- * Deprecated: 2.12: This function has been deprecated and should
- *   not be used in newly written code. Calling this function is
- *   equivalent to calling gtk_recent_manager_get_default().
- *
- * Since: 2.10
- */
-GtkRecentManager *
-gtk_recent_manager_get_for_screen (GdkScreen *screen)
-{
-  return gtk_recent_manager_get_default ();
-}
-
-/**
- * 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
- *
- * Deprecated: 2.12: This function has been deprecated and should
- *   not be used in newly written code. Calling this function has
- *   no effect.
- */
-void
-gtk_recent_manager_set_screen (GtkRecentManager *manager,
-                              GdkScreen        *screen)
-{
-
-}
-
-/**
- * gtk_recent_manager_set_limit:
- * @manager: a #GtkRecentManager
- * @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
-gtk_recent_manager_set_limit (GtkRecentManager *manager,
-                             gint              limit)
-{
-  GtkRecentManagerPrivate *priv;
-  
-  g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
-  
-  priv = manager->priv;
-  priv->limit = limit;
-}
-
-/**
- * gtk_recent_manager_get_limit:
- * @manager: a #GtkRecentManager
- *
- * 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
-gtk_recent_manager_get_limit (GtkRecentManager *manager)
-{
-  GtkRecentManagerPrivate *priv;
-  
-  g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), DEFAULT_LIMIT);
-  
-  priv = manager->priv;
-  return priv->limit;
-}
 
 static void
 gtk_recent_manager_add_item_query_info (GObject      *source_object,
@@ -978,7 +946,7 @@ gtk_recent_manager_add_full (GtkRecentManager     *manager,
  * gtk_recent_manager_remove_item:
  * @manager: a #GtkRecentManager
  * @uri: the URI of the item you wish to remove
- * @error: return location for a #GError, or %NULL
+ * @error: (allow-none): return location for a #GError, or %NULL
  *
  * Removes a resource pointed by @uri from the recently used resources
  * list handled by a recent manager.
@@ -1127,7 +1095,7 @@ build_recent_info (GBookmarkFile  *bookmarks,
  * gtk_recent_manager_lookup_item:
  * @manager: a #GtkRecentManager
  * @uri: a URI
- * @error: a return location for a #GError, or %NULL
+ * @error: (allow-none): 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
@@ -1190,9 +1158,9 @@ gtk_recent_manager_lookup_item (GtkRecentManager  *manager,
  * 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
+ * @new_uri: (allow-none): 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
+ * @error: (allow-none): a return location for a #GError, or %NULL
  *
  * Changes the location of a recently used resource from @uri to @new_uri.
  * 
@@ -1265,7 +1233,8 @@ gtk_recent_manager_move_item (GtkRecentManager  *recent_manager,
  *
  * Gets the list of recently used resources.
  *
- * Return value: a list of newly allocated #GtkRecentInfo objects. Use
+ * Return value:  (element-type GtkRecentInfo) (transfer full): 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().
  *
@@ -1285,17 +1254,11 @@ gtk_recent_manager_get_items (GtkRecentManager *manager)
   if (!priv->recent_items)
     return NULL;
 
-  if (priv->limit == 0)
-    return NULL;
-  
   uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
   for (i = 0; i < uris_len; i++)
     {
       GtkRecentInfo *info;
       
-      if (priv->limit != -1 && i == priv->limit)
-        break;
-      
       info = gtk_recent_info_new (uris[i]);
       build_recent_info (priv->recent_items, info);
       
@@ -1330,7 +1293,7 @@ purge_recent_items_list (GtkRecentManager  *manager,
 /**
  * gtk_recent_manager_purge_items:
  * @manager: a #GtkRecentManager
- * @error: a return location for a #GError, or %NULL
+ * @error: (allow-none): a return location for a #GError, or %NULL
  *
  * Purges every item from the recently used resources list.
  *
@@ -1404,17 +1367,9 @@ gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
  * GtkRecentInfo *
  *****************/
  
-GType
-gtk_recent_info_get_type (void)
-{
-  static GType info_type = 0;
-  
-  if (!info_type)
-    info_type = g_boxed_type_register_static (I_("GtkRecentInfo"),
-                                             (GBoxedCopyFunc) gtk_recent_info_ref,
-                                             (GBoxedFreeFunc) gtk_recent_info_unref);
-  return info_type;
-}
+G_DEFINE_BOXED_TYPE (GtkRecentInfo, gtk_recent_info,
+                     gtk_recent_info_ref,
+                     gtk_recent_info_unref)
 
 static GtkRecentInfo *
 gtk_recent_info_new (const gchar *uri)
@@ -1715,9 +1670,9 @@ recent_app_info_free (RecentAppInfo *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
+ * @app_exec: (transfer none) (out): return location for the string containing the command line
+ * @count: (out): return location for the number of times this item was registered
+ * @time_: (out): return location for the timestamp this item was last registered
  *    for this application
  *
  * Gets the data regarding the application that has registered the resource
@@ -1727,15 +1682,16 @@ recent_app_info_free (RecentAppInfo *app_info)
  * 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().
+ *   resource inside the recently used list, or %FALSE otherwise. The
+ *   @app_exec string is owned by the #GtkRecentInfo and should not be
+ *   modified or freed
  *
  * Since: 2.10
  */
 gboolean
 gtk_recent_info_get_application_info (GtkRecentInfo  *info,
                                      const gchar    *app_name,
-                                     gchar         **app_exec,
+                                     const gchar   **app_exec,
                                      guint          *count,
                                      time_t         *time_)
 {
@@ -1770,15 +1726,15 @@ gtk_recent_info_get_application_info (GtkRecentInfo  *info,
 /**
  * gtk_recent_info_get_applications:
  * @info: a #GtkRecentInfo
- * @length: return location for the length of the returned list, or %NULL
+ * @length: (out) (allow-none): return location for the length of the returned list
  *
  * 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.
+ * Return value: (array length=length zero-terminated=1): 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)
@@ -2265,14 +2221,14 @@ gtk_recent_info_get_age (GtkRecentInfo *info)
 /**
  * gtk_recent_info_get_groups:
  * @info: a #GtkRecentInfo
- * @length: return location for the number of groups returned, or %NULL
+ * @length: (out) (allow-none): return location for the number of groups returned
  *
  * 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.
+ * Return value:  (array length=length zero-terminated=1): a newly allocated
+ * %NULL terminated array of strings.  Use g_strfreev() to free it.
  *
  * Since: 2.10
  */
@@ -2366,6 +2322,3 @@ _gtk_recent_manager_sync (void)
       gtk_recent_manager_real_changed (recent_manager_singleton);
     }
 }
-
-#define __GTK_RECENT_MANAGER_C__
-#include "gtkaliasdef.c"