1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as
5
* published by the Free Software Foundation; either version 2 of the
6
* License, or (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18
* James Willcox <jwillcox@cs.indiana.edu>
34
#include <libgnomevfs/gnome-vfs.h>
35
#include <libgnomevfs/gnome-vfs-mime-utils.h>
36
#include <gconf/gconf-client.h>
37
#include "egg-recent-model.h"
38
#include "egg-recent-item.h"
40
#define EGG_RECENT_MODEL_FILE_PATH "/.recently-used"
41
#define EGG_RECENT_MODEL_BUFFER_SIZE 8192
43
#define EGG_RECENT_MODEL_MAX_ITEMS 500
44
#define EGG_RECENT_MODEL_DEFAULT_LIMIT 10
45
#define EGG_RECENT_MODEL_TIMEOUT_LENGTH 200
47
#define EGG_RECENT_MODEL_KEY_DIR "/desktop/gnome/recent_files"
48
#define EGG_RECENT_MODEL_DEFAULT_LIMIT_KEY EGG_RECENT_MODEL_KEY_DIR "/default_limit"
49
#define EGG_RECENT_MODEL_EXPIRE_KEY EGG_RECENT_MODEL_KEY_DIR "/expire"
51
struct _EggRecentModelPrivate {
52
GSList *mime_filter_values; /* list of mime types we allow */
53
GSList *group_filter_values; /* list of groups we allow */
54
GSList *scheme_filter_values; /* list of URI schemes we allow */
56
EggRecentModelSort sort_type; /* type of sorting to be done */
58
int limit; /* soft limit for length of the list */
59
int expire_days; /* number of days to hold an item */
61
char *path; /* path to the file we store stuff in */
65
GnomeVFSMonitorHandle *monitor;
68
gboolean use_default_limit;
70
guint limit_change_notify_id;
71
guint expiration_change_notify_id;
73
guint changed_timeout;
82
static GType model_signals[LAST_SIGNAL] = { 0 };
97
EggRecentItem *current_item;
112
typedef struct _ChangedData {
113
EggRecentModel *model;
117
#define TAG_RECENT_FILES "RecentFiles"
118
#define TAG_RECENT_ITEM "RecentItem"
119
#define TAG_URI "URI"
120
#define TAG_MIME_TYPE "Mime-Type"
121
#define TAG_TIMESTAMP "Timestamp"
122
#define TAG_PRIVATE "Private"
123
#define TAG_GROUPS "Groups"
124
#define TAG_GROUP "Group"
126
static void start_element_handler (GMarkupParseContext *context,
127
const gchar *element_name,
128
const gchar **attribute_names,
129
const gchar **attribute_values,
133
static void end_element_handler (GMarkupParseContext *context,
134
const gchar *element_name,
138
static void text_handler (GMarkupParseContext *context,
144
static void error_handler (GMarkupParseContext *context,
148
static GMarkupParser parser = {start_element_handler, end_element_handler,
154
egg_recent_model_string_match (const GSList *list, const gchar *str)
158
if (list == NULL || str == NULL)
164
if (g_pattern_match_string (tmp->data, str))
174
egg_recent_model_write_raw (EggRecentModel *model, FILE *file,
175
const gchar *content)
183
len = strlen (content);
186
if (fstat (fd, &sbuf) < 0)
187
g_warning ("Couldn't stat XML document.");
189
if ((off_t)len < sbuf.st_size) {
193
if (fputs (content, file) == EOF)
203
egg_recent_model_delete_from_list (GList *list,
214
EggRecentItem *item = tmp->data;
219
if (!strcmp (egg_recent_item_peek_uri (item), uri)) {
220
egg_recent_item_unref (item);
222
list = g_list_remove_link (list, tmp);
233
egg_recent_model_add_new_groups (EggRecentItem *item,
234
EggRecentItem *upd_item)
238
tmp = egg_recent_item_get_groups (upd_item);
241
char *group = tmp->data;
243
if (!egg_recent_item_in_group (item, group))
244
egg_recent_item_add_group (item, group);
251
egg_recent_model_update_item (GList *items, EggRecentItem *upd_item)
256
uri = egg_recent_item_peek_uri (upd_item);
261
EggRecentItem *item = tmp->data;
263
if (gnome_vfs_uris_match (egg_recent_item_peek_uri (item), uri)) {
264
egg_recent_item_set_timestamp (item, (time_t) -1);
266
egg_recent_model_add_new_groups (item, upd_item);
278
egg_recent_model_read_raw (EggRecentModel *model, FILE *file)
281
char buf[EGG_RECENT_MODEL_BUFFER_SIZE];
285
string = g_string_new (NULL);
286
while (fgets (buf, EGG_RECENT_MODEL_BUFFER_SIZE, file)) {
287
string = g_string_append (string, buf);
292
return g_string_free (string, FALSE);
298
parse_info_init (ParseInfo *info)
300
info->states = g_slist_prepend (NULL, STATE_START);
305
parse_info_free (ParseInfo *info)
307
g_slist_free (info->states);
311
push_state (ParseInfo *info,
314
info->states = g_slist_prepend (info->states, GINT_TO_POINTER (state));
318
pop_state (ParseInfo *info)
320
g_return_if_fail (info->states != NULL);
322
info->states = g_slist_remove (info->states, info->states->data);
326
peek_state (ParseInfo *info)
328
g_return_val_if_fail (info->states != NULL, STATE_START);
330
return GPOINTER_TO_INT (info->states->data);
333
#define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)
336
start_element_handler (GMarkupParseContext *context,
337
const gchar *element_name,
338
const gchar **attribute_names,
339
const gchar **attribute_values,
343
ParseInfo *info = (ParseInfo *)user_data;
345
if (ELEMENT_IS (TAG_RECENT_FILES))
346
push_state (info, STATE_RECENT_FILES);
347
else if (ELEMENT_IS (TAG_RECENT_ITEM)) {
348
info->current_item = egg_recent_item_new ();
349
push_state (info, STATE_RECENT_ITEM);
350
} else if (ELEMENT_IS (TAG_URI))
351
push_state (info, STATE_URI);
352
else if (ELEMENT_IS (TAG_MIME_TYPE))
353
push_state (info, STATE_MIME_TYPE);
354
else if (ELEMENT_IS (TAG_TIMESTAMP))
355
push_state (info, STATE_TIMESTAMP);
356
else if (ELEMENT_IS (TAG_PRIVATE)) {
357
push_state (info, STATE_PRIVATE);
358
egg_recent_item_set_private (info->current_item, TRUE);
359
} else if (ELEMENT_IS (TAG_GROUPS))
360
push_state (info, STATE_GROUPS);
361
else if (ELEMENT_IS (TAG_GROUP))
362
push_state (info, STATE_GROUP);
366
list_compare_func_mru (gpointer a, gpointer b)
368
EggRecentItem *item_a = (EggRecentItem *)a;
369
EggRecentItem *item_b = (EggRecentItem *)b;
371
return item_a->timestamp < item_b->timestamp;
375
list_compare_func_lru (gpointer a, gpointer b)
377
EggRecentItem *item_a = (EggRecentItem *)a;
378
EggRecentItem *item_b = (EggRecentItem *)b;
380
return item_a->timestamp > item_b->timestamp;
386
end_element_handler (GMarkupParseContext *context,
387
const gchar *element_name,
391
ParseInfo *info = (ParseInfo *)user_data;
393
switch (peek_state (info)) {
394
case STATE_RECENT_ITEM:
395
info->items = g_list_append (info->items,
397
if (info->current_item->uri == NULL ||
398
strlen (info->current_item->uri) == 0)
399
g_warning ("URI NOT LOADED");
409
text_handler (GMarkupParseContext *context,
415
ParseInfo *info = (ParseInfo *)user_data;
417
switch (peek_state (info)) {
419
case STATE_RECENT_FILES:
420
case STATE_RECENT_ITEM:
425
egg_recent_item_set_uri (info->current_item, text);
427
case STATE_MIME_TYPE:
428
egg_recent_item_set_mime_type (info->current_item,
431
case STATE_TIMESTAMP:
432
egg_recent_item_set_timestamp (info->current_item,
433
(time_t)atoi (text));
436
egg_recent_item_add_group (info->current_item,
444
error_handler (GMarkupParseContext *context,
448
g_warning ("Error in parse: %s", error->message);
452
egg_recent_model_enforce_limit (GList *list, int limit)
457
/* limit < 0 means unlimited */
461
len = g_list_length (list);
466
end = g_list_nth (list, limit-1);
471
EGG_RECENT_ITEM_LIST_UNREF (next);
476
egg_recent_model_sort (EggRecentModel *model, GList *list)
478
switch (model->priv->sort_type) {
479
case EGG_RECENT_MODEL_SORT_MRU:
480
list = g_list_sort (list,
481
(GCompareFunc)list_compare_func_mru);
483
case EGG_RECENT_MODEL_SORT_LRU:
484
list = g_list_sort (list,
485
(GCompareFunc)list_compare_func_lru);
487
case EGG_RECENT_MODEL_SORT_NONE:
495
egg_recent_model_group_match (EggRecentItem *item, GSList *groups)
501
while (tmp != NULL) {
502
const gchar * group = (const gchar *)tmp->data;
504
if (egg_recent_item_in_group (item, group))
514
egg_recent_model_filter (EggRecentModel *model,
518
GList *newlist = NULL;
522
g_return_val_if_fail (list != NULL, NULL);
525
gboolean pass_mime_test = FALSE;
526
gboolean pass_group_test = FALSE;
527
gboolean pass_scheme_test = FALSE;
528
item = (EggRecentItem *)list->data;
531
uri = egg_recent_item_get_uri (item);
533
/* filter by mime type */
534
if (model->priv->mime_filter_values != NULL) {
535
mime_type = egg_recent_item_get_mime_type (item);
537
if (egg_recent_model_string_match
538
(model->priv->mime_filter_values,
540
pass_mime_test = TRUE;
544
pass_mime_test = TRUE;
546
/* filter by group */
547
if (pass_mime_test && model->priv->group_filter_values != NULL) {
548
if (egg_recent_model_group_match
549
(item, model->priv->group_filter_values))
550
pass_group_test = TRUE;
551
} else if (egg_recent_item_get_private (item)) {
552
pass_group_test = FALSE;
554
pass_group_test = TRUE;
556
/* filter by URI scheme */
557
if (pass_mime_test && pass_group_test &&
558
model->priv->scheme_filter_values != NULL) {
561
scheme = gnome_vfs_get_uri_scheme (uri);
563
if (egg_recent_model_string_match
564
(model->priv->scheme_filter_values, scheme))
565
pass_scheme_test = TRUE;
569
pass_scheme_test = TRUE;
571
if (pass_mime_test && pass_group_test && pass_scheme_test)
572
newlist = g_list_prepend (newlist, item);
578
newlist = g_list_reverse (newlist);
590
egg_recent_model_monitor_list_cb (GnomeVFSMonitorHandle *handle,
591
const gchar *monitor_uri,
592
const gchar *info_uri,
593
GnomeVFSMonitorEventType event_type,
596
EggRecentModel *model;
598
model = EGG_RECENT_MODEL (user_data);
600
if (event_type == GNOME_VFS_MONITOR_EVENT_DELETED) {
601
egg_recent_model_delete (model, monitor_uri);
602
g_hash_table_remove (model->priv->monitors, monitor_uri);
609
egg_recent_model_monitor_list (EggRecentModel *model, GList *list)
615
EggRecentItem *item = (EggRecentItem *)tmp->data;
616
GnomeVFSMonitorHandle *handle;
622
uri = egg_recent_item_get_uri (item);
623
if (g_hash_table_lookup (model->priv->monitors, uri)) {
624
/* already monitoring this one */
629
res = gnome_vfs_monitor_add (&handle, uri,
630
GNOME_VFS_MONITOR_FILE,
631
egg_recent_model_monitor_list_cb,
634
if (res == GNOME_VFS_OK)
635
g_hash_table_insert (model->priv->monitors, uri, handle);
644
egg_recent_model_changed_timeout (EggRecentModel *model)
646
egg_recent_model_changed (model);
652
egg_recent_model_monitor_cb (GnomeVFSMonitorHandle *handle,
653
const gchar *monitor_uri,
654
const gchar *info_uri,
655
GnomeVFSMonitorEventType event_type,
658
EggRecentModel *model;
660
g_return_if_fail (user_data != NULL);
661
g_return_if_fail (EGG_IS_RECENT_MODEL (user_data));
662
model = EGG_RECENT_MODEL (user_data);
664
if (event_type == GNOME_VFS_MONITOR_EVENT_CHANGED) {
665
if (model->priv->changed_timeout > 0) {
666
g_source_remove (model->priv->changed_timeout);
669
model->priv->changed_timeout = g_timeout_add (
670
EGG_RECENT_MODEL_TIMEOUT_LENGTH,
671
(GSourceFunc)egg_recent_model_changed_timeout,
677
egg_recent_model_monitor (EggRecentModel *model, gboolean should_monitor)
679
if (should_monitor && model->priv->monitor == NULL) {
681
gnome_vfs_monitor_add (&model->priv->monitor,
683
GNOME_VFS_MONITOR_FILE,
684
egg_recent_model_monitor_cb,
687
/* if the above fails, don't worry about it.
688
* local notifications will still happen
691
} else if (!should_monitor && model->priv->monitor != NULL) {
692
gnome_vfs_monitor_cancel (model->priv->monitor);
693
model->priv->monitor = NULL;
698
egg_recent_model_set_limit_internal (EggRecentModel *model, int limit)
700
model->priv->limit = limit;
703
egg_recent_model_monitor (model, FALSE);
705
egg_recent_model_monitor (model, TRUE);
706
egg_recent_model_changed (model);
711
egg_recent_model_read (EggRecentModel *model, FILE *file)
715
GMarkupParseContext *ctx;
719
content = egg_recent_model_read_raw (model, file);
721
if (strlen (content) <= 0) {
726
parse_info_init (&info);
728
ctx = g_markup_parse_context_new (&parser, 0, &info, NULL);
731
if (!g_markup_parse_context_parse (ctx, content, strlen (content),
733
g_warning (error->message);
734
g_error_free (error);
740
if (!g_markup_parse_context_end_parse (ctx, &error))
743
g_markup_parse_context_free (ctx);
747
parse_info_free (&info);
752
g_print ("Total items: %d\n", g_list_length (list));
760
egg_recent_model_write (EggRecentModel *model, FILE *file, GList *list)
769
string = g_string_new ("<?xml version=\"1.0\"?>\n");
770
string = g_string_append (string, "<" TAG_RECENT_FILES ">\n");
778
item = (EggRecentItem *)list->data;
781
uri = egg_recent_item_get_uri_utf8 (item);
782
escaped_uri = g_markup_escape_text (uri,
786
mime_type = egg_recent_item_get_mime_type (item);
787
timestamp = egg_recent_item_get_timestamp (item);
789
string = g_string_append (string, " <" TAG_RECENT_ITEM ">\n");
791
g_string_append_printf (string,
792
" <" TAG_URI ">%s</" TAG_URI ">\n", escaped_uri);
795
g_string_append_printf (string,
796
" <" TAG_MIME_TYPE ">%s</" TAG_MIME_TYPE ">\n", mime_type);
798
g_string_append_printf (string,
799
" <" TAG_MIME_TYPE "></" TAG_MIME_TYPE ">\n");
802
g_string_append_printf (string,
803
" <" TAG_TIMESTAMP ">%d</" TAG_TIMESTAMP ">\n", (int)timestamp);
805
if (egg_recent_item_get_private (item))
806
string = g_string_append (string,
807
" <" TAG_PRIVATE "/>\n");
809
/* write the groups */
810
string = g_string_append (string,
811
" <" TAG_GROUPS ">\n");
812
groups = egg_recent_item_get_groups (item);
814
if (groups == NULL && egg_recent_item_get_private (item))
815
g_warning ("Item with URI \"%s\" marked as private, but"
816
" does not belong to any groups.\n", uri);
819
const gchar *group = (const gchar *)groups->data;
820
gchar *escaped_group;
822
escaped_group = g_markup_escape_text (group, strlen(group));
824
g_string_append_printf (string,
825
" <" TAG_GROUP ">%s</" TAG_GROUP ">\n",
828
g_free (escaped_group);
830
groups = groups->next;
833
string = g_string_append (string, " </" TAG_GROUPS ">\n");
835
string = g_string_append (string,
836
" </" TAG_RECENT_ITEM ">\n");
839
g_free (escaped_uri);
845
string = g_string_append (string, "</" TAG_RECENT_FILES ">");
847
data = g_string_free (string, FALSE);
849
ret = egg_recent_model_write_raw (model, file, data);
857
egg_recent_model_open_file (EggRecentModel *model)
861
file = fopen (model->priv->path, "r+");
866
file = fopen (model->priv->path, "w+");
868
g_return_val_if_fail (file != NULL, NULL);
875
egg_recent_model_lock_file (FILE *file)
883
/* Attempt to lock the file 5 times,
884
* waiting a random interval (< 1 second)
885
* in between attempts.
886
* We should really be doing asynchronous
887
* locking, but requires substantially larger
895
if (lockf (fd, F_TLOCK, 0) == 0)
898
rand_interval = 1 + (int) (10.0 * rand()/(RAND_MAX + 1.0));
900
g_usleep (100000 * rand_interval);
909
egg_recent_model_unlock_file (FILE *file)
916
return (lockf (fd, F_ULOCK, 0) == 0) ? TRUE : FALSE;
920
egg_recent_model_finalize (GObject *object)
922
EggRecentModel *model = EGG_RECENT_MODEL (object);
924
egg_recent_model_monitor (model, FALSE);
927
g_slist_foreach (model->priv->mime_filter_values,
928
(GFunc) g_pattern_spec_free, NULL);
929
g_slist_free (model->priv->mime_filter_values);
930
model->priv->mime_filter_values = NULL;
932
g_slist_foreach (model->priv->scheme_filter_values,
933
(GFunc) g_pattern_spec_free, NULL);
934
g_slist_free (model->priv->scheme_filter_values);
935
model->priv->scheme_filter_values = NULL;
937
g_slist_foreach (model->priv->group_filter_values,
938
(GFunc) g_free, NULL);
939
g_slist_free (model->priv->group_filter_values);
940
model->priv->group_filter_values = NULL;
943
if (model->priv->limit_change_notify_id)
944
gconf_client_notify_remove (model->priv->client,
945
model->priv->limit_change_notify_id);
946
model->priv->expiration_change_notify_id = 0;
948
if (model->priv->expiration_change_notify_id)
949
gconf_client_notify_remove (model->priv->client,
950
model->priv->expiration_change_notify_id);
951
model->priv->expiration_change_notify_id = 0;
953
g_object_unref (model->priv->client);
954
model->priv->client = NULL;
957
g_free (model->priv->path);
958
model->priv->path = NULL;
960
g_hash_table_destroy (model->priv->monitors);
961
model->priv->monitors = NULL;
964
g_free (model->priv);
968
egg_recent_model_set_property (GObject *object,
973
EggRecentModel *model = EGG_RECENT_MODEL (object);
977
case PROP_MIME_FILTERS:
978
model->priv->mime_filter_values =
979
(GSList *)g_value_get_pointer (value);
982
case PROP_GROUP_FILTERS:
983
model->priv->group_filter_values =
984
(GSList *)g_value_get_pointer (value);
987
case PROP_SCHEME_FILTERS:
988
model->priv->scheme_filter_values =
989
(GSList *)g_value_get_pointer (value);
993
model->priv->sort_type = g_value_get_int (value);
997
egg_recent_model_set_limit (model,
998
g_value_get_int (value));
1002
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1008
egg_recent_model_get_property (GObject *object,
1013
EggRecentModel *model = EGG_RECENT_MODEL (object);
1017
case PROP_MIME_FILTERS:
1018
g_value_set_pointer (value, model->priv->mime_filter_values);
1021
case PROP_GROUP_FILTERS:
1022
g_value_set_pointer (value, model->priv->group_filter_values);
1025
case PROP_SCHEME_FILTERS:
1026
g_value_set_pointer (value, model->priv->scheme_filter_values);
1029
case PROP_SORT_TYPE:
1030
g_value_set_int (value, model->priv->sort_type);
1034
g_value_set_int (value, model->priv->limit);
1038
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1044
egg_recent_model_class_init (EggRecentModelClass * klass)
1046
GObjectClass *object_class;
1048
object_class = G_OBJECT_CLASS (klass);
1049
object_class->set_property = egg_recent_model_set_property;
1050
object_class->get_property = egg_recent_model_get_property;
1051
object_class->finalize = egg_recent_model_finalize;
1053
model_signals[CHANGED] = g_signal_new ("changed",
1054
G_OBJECT_CLASS_TYPE (object_class),
1056
G_STRUCT_OFFSET (EggRecentModelClass, changed),
1058
g_cclosure_marshal_VOID__POINTER,
1063
g_object_class_install_property (object_class,
1065
g_param_spec_pointer ("mime-filters",
1067
"List of mime types to be allowed.",
1068
G_PARAM_READWRITE));
1070
g_object_class_install_property (object_class,
1072
g_param_spec_pointer ("group-filters",
1074
"List of groups to be allowed.",
1075
G_PARAM_READWRITE));
1077
g_object_class_install_property (object_class,
1078
PROP_SCHEME_FILTERS,
1079
g_param_spec_pointer ("scheme-filters",
1081
"List of URI schemes to be allowed.",
1082
G_PARAM_READWRITE));
1084
g_object_class_install_property (object_class,
1086
g_param_spec_int ("sort-type",
1088
"Type of sorting to be done.",
1089
0, EGG_RECENT_MODEL_SORT_NONE,
1090
EGG_RECENT_MODEL_SORT_MRU,
1091
G_PARAM_READWRITE));
1093
g_object_class_install_property (object_class,
1095
g_param_spec_int ("limit",
1097
"Max number of items allowed.",
1098
-1, EGG_RECENT_MODEL_MAX_ITEMS,
1099
EGG_RECENT_MODEL_DEFAULT_LIMIT,
1100
G_PARAM_READWRITE));
1102
klass->changed = NULL;
1108
egg_recent_model_limit_changed (GConfClient *client, guint cnxn_id,
1109
GConfEntry *entry, gpointer user_data)
1111
EggRecentModel *model;
1114
model = EGG_RECENT_MODEL (user_data);
1116
g_return_if_fail (model != NULL);
1118
if (model->priv->use_default_limit == FALSE)
1119
return; /* ignore this key */
1121
/* the key was unset, and the schema has apparently failed */
1125
value = gconf_entry_get_value (entry);
1127
if (value->type != GCONF_VALUE_INT) {
1128
g_warning ("Expected GConfValue of type integer, "
1129
"got something else");
1133
egg_recent_model_set_limit_internal (model, gconf_value_get_int (value));
1137
egg_recent_model_expiration_changed (GConfClient *client, guint cnxn_id,
1138
GConfEntry *entry, gpointer user_data)
1144
egg_recent_model_init (EggRecentModel * model)
1146
if (!gnome_vfs_init ()) {
1147
g_warning ("gnome-vfs initialization failed.");
1152
model->priv = g_new0 (EggRecentModelPrivate, 1);
1154
model->priv->path = g_strdup_printf ("%s" EGG_RECENT_MODEL_FILE_PATH,
1157
model->priv->mime_filter_values = NULL;
1158
model->priv->group_filter_values = NULL;
1159
model->priv->scheme_filter_values = NULL;
1161
model->priv->client = gconf_client_get_default ();
1162
gconf_client_add_dir (model->priv->client, EGG_RECENT_MODEL_KEY_DIR,
1163
GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
1165
model->priv->limit_change_notify_id =
1166
gconf_client_notify_add (model->priv->client,
1167
EGG_RECENT_MODEL_DEFAULT_LIMIT_KEY,
1168
egg_recent_model_limit_changed,
1171
model->priv->expiration_change_notify_id =
1172
gconf_client_notify_add (model->priv->client,
1173
EGG_RECENT_MODEL_EXPIRE_KEY,
1174
egg_recent_model_expiration_changed,
1177
model->priv->expire_days = gconf_client_get_int (
1178
model->priv->client,
1179
EGG_RECENT_MODEL_EXPIRE_KEY,
1183
/* keep this out, for now */
1184
model->priv->limit = gconf_client_get_int (
1185
model->priv->client,
1186
EGG_RECENT_MODEL_DEFAULT_LIMIT_KEY, NULL);
1187
model->priv->use_default_limit = TRUE;
1189
model->priv->limit = EGG_RECENT_MODEL_DEFAULT_LIMIT;
1190
model->priv->use_default_limit = FALSE;
1192
model->priv->monitors = g_hash_table_new_full (
1193
g_str_hash, g_str_equal,
1194
(GDestroyNotify) g_free,
1195
(GDestroyNotify) gnome_vfs_monitor_cancel);
1197
model->priv->monitor = NULL;
1198
egg_recent_model_monitor (model, TRUE);
1203
* egg_recent_model_new:
1204
* @sort: the type of sorting to use
1205
* @limit: maximum number of items in the list
1207
* This creates a new EggRecentModel object.
1209
* Returns: a EggRecentModel object
1212
egg_recent_model_new (EggRecentModelSort sort)
1214
EggRecentModel *model;
1216
model = EGG_RECENT_MODEL (g_object_new (egg_recent_model_get_type (),
1217
"sort-type", sort, NULL));
1219
g_return_val_if_fail (model, NULL);
1225
* egg_recent_model_add_full:
1226
* @model: A EggRecentModel object.
1227
* @item: A EggRecentItem
1229
* This function adds an item to the list of recently used URIs.
1234
egg_recent_model_add_full (EggRecentModel * model, EggRecentItem *item)
1238
gboolean ret = FALSE;
1239
gboolean updated = FALSE;
1243
g_return_val_if_fail (model != NULL, FALSE);
1244
g_return_val_if_fail (EGG_IS_RECENT_MODEL (model), FALSE);
1246
uri = egg_recent_item_get_uri (item);
1247
if (strncmp (uri, "recent-files://", strlen ("recent-files://")) == 0) {
1254
file = egg_recent_model_open_file (model);
1255
g_return_val_if_fail (file != NULL, FALSE);
1258
egg_recent_item_set_timestamp (item, t);
1260
if (egg_recent_model_lock_file (file)) {
1262
/* read existing stuff */
1263
list = egg_recent_model_read (model, file);
1265
/* if it's already there, we just update it */
1266
updated = egg_recent_model_update_item (list, item);
1269
list = g_list_prepend (list, item);
1271
egg_recent_model_enforce_limit (list,
1272
EGG_RECENT_MODEL_MAX_ITEMS);
1275
/* write new stuff */
1276
if (!egg_recent_model_write (model, file, list))
1277
g_warning ("Write failed: %s", strerror (errno));
1280
list = g_list_remove (list, item);
1282
EGG_RECENT_ITEM_LIST_UNREF (list);
1285
g_warning ("Failed to lock: %s", strerror (errno));
1289
if (!egg_recent_model_unlock_file (file))
1290
g_warning ("Failed to unlock: %s", strerror (errno));
1294
if (model->priv->monitor == NULL) {
1295
/* since monitoring isn't working, at least give a
1296
* local notification
1298
egg_recent_model_changed (model);
1305
* egg_recent_model_add:
1306
* @model: A EggRecentModel object.
1307
* @uri: A string URI
1309
* This function adds an item to the list of recently used URIs.
1314
egg_recent_model_add (EggRecentModel *model, const gchar *uri)
1316
EggRecentItem *item;
1317
gboolean ret = FALSE;
1319
g_return_val_if_fail (model != NULL, FALSE);
1320
g_return_val_if_fail (uri != NULL, FALSE);
1322
item = egg_recent_item_new_from_uri (uri);
1324
g_return_val_if_fail (item != NULL, FALSE);
1326
ret = egg_recent_model_add_full (model, item);
1328
egg_recent_item_unref (item);
1336
* egg_recent_model_delete:
1337
* @model: A EggRecentModel object.
1338
* @uri: The URI you want to delete.
1340
* This function deletes a URI from the file of recently used URIs.
1345
egg_recent_model_delete (EggRecentModel * model, const gchar * uri)
1349
unsigned int length;
1350
gboolean ret = FALSE;
1352
g_return_val_if_fail (model != NULL, FALSE);
1353
g_return_val_if_fail (EGG_IS_RECENT_MODEL (model), FALSE);
1354
g_return_val_if_fail (uri != NULL, FALSE);
1356
file = egg_recent_model_open_file (model);
1357
g_return_val_if_fail (file != NULL, FALSE);
1359
if (egg_recent_model_lock_file (file)) {
1360
list = egg_recent_model_read (model, file);
1365
length = g_list_length (list);
1367
list = egg_recent_model_delete_from_list (list, uri);
1369
if (length == g_list_length (list)) {
1370
/* nothing was deleted */
1371
EGG_RECENT_ITEM_LIST_UNREF (list);
1373
egg_recent_model_write (model, file, list);
1374
EGG_RECENT_ITEM_LIST_UNREF (list);
1379
g_warning ("Failed to lock: %s", strerror (errno));
1385
if (!egg_recent_model_unlock_file (file))
1386
g_warning ("Failed to unlock: %s", strerror (errno));
1390
g_hash_table_remove (model->priv->monitors, uri);
1392
if (model->priv->monitor == NULL && ret) {
1393
/* since monitoring isn't working, at least give a
1394
* local notification
1396
egg_recent_model_changed (model);
1404
* egg_recent_model_get_list:
1405
* @model: A EggRecentModel object.
1407
* This function gets the current contents of the file
1412
egg_recent_model_get_list (EggRecentModel *model)
1417
file = egg_recent_model_open_file (model);
1418
g_return_val_if_fail (file != NULL, NULL);
1420
if (egg_recent_model_lock_file (file)) {
1421
list = egg_recent_model_read (model, file);
1424
g_warning ("Failed to lock: %s", strerror (errno));
1429
if (!egg_recent_model_unlock_file (file))
1430
g_warning ("Failed to unlock: %s", strerror (errno));
1433
list = egg_recent_model_filter (model, list);
1434
list = egg_recent_model_sort (model, list);
1436
egg_recent_model_enforce_limit (list, model->priv->limit);
1447
* egg_recent_model_set_limit:
1448
* @model: A EggRecentModel object.
1449
* @limit: The maximum length of the list
1451
* This function sets the maximum length of the list. Note: This only affects
1452
* the length of the list emitted in the "changed" signal, not the list stored
1458
egg_recent_model_set_limit (EggRecentModel *model, int limit)
1460
model->priv->use_default_limit = FALSE;
1462
egg_recent_model_set_limit_internal (model, limit);
1466
* egg_recent_model_get_limit:
1467
* @model: A EggRecentModel object.
1469
* This function gets the maximum length of the list.
1474
egg_recent_model_get_limit (EggRecentModel *model)
1476
return model->priv->limit;
1481
* egg_recent_model_clear:
1482
* @model: A EggRecentModel object.
1484
* This function clears the contents of the file
1489
egg_recent_model_clear (EggRecentModel *model)
1494
file = egg_recent_model_open_file (model);
1495
g_return_if_fail (file != NULL);
1499
if (egg_recent_model_lock_file (file)) {
1502
g_warning ("Failed to lock: %s", strerror (errno));
1506
if (!egg_recent_model_unlock_file (file))
1507
g_warning ("Failed to unlock: %s", strerror (errno));
1514
* egg_recent_model_set_filter_mime_types:
1515
* @model: A EggRecentModel object.
1517
* Sets which mime types are allowed in the list.
1522
egg_recent_model_set_filter_mime_types (EggRecentModel *model,
1526
GSList *list = NULL;
1529
g_return_if_fail (model != NULL);
1531
if (model->priv->mime_filter_values != NULL) {
1532
g_slist_foreach (model->priv->mime_filter_values,
1533
(GFunc) g_pattern_spec_free, NULL);
1534
g_slist_free (model->priv->mime_filter_values);
1535
model->priv->mime_filter_values = NULL;
1538
va_start (valist, model);
1540
str = va_arg (valist, gchar*);
1542
while (str != NULL) {
1543
list = g_slist_prepend (list, g_pattern_spec_new (str));
1545
str = va_arg (valist, gchar*);
1550
model->priv->mime_filter_values = list;
1554
* egg_recent_model_set_filter_groups:
1555
* @model: A EggRecentModel object.
1557
* Sets which groups are allowed in the list.
1562
egg_recent_model_set_filter_groups (EggRecentModel *model,
1566
GSList *list = NULL;
1569
g_return_if_fail (model != NULL);
1571
if (model->priv->group_filter_values != NULL) {
1572
g_slist_foreach (model->priv->group_filter_values, (GFunc)g_free, NULL);
1573
g_slist_free (model->priv->group_filter_values);
1574
model->priv->group_filter_values = NULL;
1577
va_start (valist, model);
1579
str = va_arg (valist, gchar*);
1581
while (str != NULL) {
1582
list = g_slist_prepend (list, g_strdup (str));
1584
str = va_arg (valist, gchar*);
1589
model->priv->group_filter_values = list;
1593
* egg_recent_model_set_filter_uri_schemes:
1594
* @model: A EggRecentModel object.
1596
* Sets which URI schemes (file, http, ftp, etc) are allowed in the list.
1601
egg_recent_model_set_filter_uri_schemes (EggRecentModel *model, ...)
1604
GSList *list = NULL;
1607
g_return_if_fail (model != NULL);
1609
if (model->priv->scheme_filter_values != NULL) {
1610
g_slist_foreach (model->priv->scheme_filter_values,
1611
(GFunc) g_pattern_spec_free, NULL);
1612
g_slist_free (model->priv->scheme_filter_values);
1613
model->priv->scheme_filter_values = NULL;
1616
va_start (valist, model);
1618
str = va_arg (valist, gchar*);
1620
while (str != NULL) {
1621
list = g_slist_prepend (list, g_pattern_spec_new (str));
1623
str = va_arg (valist, gchar*);
1628
model->priv->scheme_filter_values = list;
1632
* egg_recent_model_set_sort:
1633
* @model: A EggRecentModel object.
1634
* @sort: A EggRecentModelSort type
1636
* Sets the type of sorting to be used.
1641
egg_recent_model_set_sort (EggRecentModel *model,
1642
EggRecentModelSort sort)
1644
g_return_if_fail (model != NULL);
1646
model->priv->sort_type = sort;
1650
* egg_recent_model_changed:
1651
* @model: A EggRecentModel object.
1653
* This function causes a "changed" signal to be emitted.
1658
egg_recent_model_changed (EggRecentModel *model)
1662
if (model->priv->limit > 0) {
1663
list = egg_recent_model_get_list (model);
1664
/* egg_recent_model_monitor_list (model, list); */
1666
g_signal_emit (G_OBJECT (model), model_signals[CHANGED], 0,
1671
EGG_RECENT_ITEM_LIST_UNREF (list);
1675
egg_recent_model_remove_expired_list (EggRecentModel *model, GList *list)
1677
time_t current_time;
1680
time (¤t_time);
1681
day_seconds = model->priv->expire_days*24*60*60;
1683
while (list != NULL) {
1684
EggRecentItem *item = list->data;
1687
timestamp = egg_recent_item_get_timestamp (item);
1689
if ((timestamp+day_seconds) < current_time) {
1690
gchar *uri = egg_recent_item_get_uri (item);
1691
egg_recent_model_delete (model, uri);
1702
* egg_recent_model_remove_expired:
1703
* @model: A EggRecentModel object.
1705
* Goes through the entire list, and removes any items that are older than
1706
* the user-specified expiration period.
1711
egg_recent_model_remove_expired (EggRecentModel *model)
1716
g_return_if_fail (model != NULL);
1718
file = egg_recent_model_open_file (model);
1719
g_return_if_fail (file != NULL);
1721
if (egg_recent_model_lock_file (file)) {
1722
list = egg_recent_model_read (model, file);
1725
g_warning ("Failed to lock: %s", strerror (errno));
1729
if (!egg_recent_model_unlock_file (file))
1730
g_warning ("Failed to unlock: %s", strerror (errno));
1733
egg_recent_model_remove_expired_list (model, list);
1734
EGG_RECENT_ITEM_LIST_UNREF (list);
1741
* egg_recent_model_get_type:
1743
* This returns a GType representing a EggRecentModel object.
1748
egg_recent_model_get_type (void)
1750
static GType egg_recent_model_type = 0;
1752
if(!egg_recent_model_type) {
1753
static const GTypeInfo egg_recent_model_info = {
1754
sizeof (EggRecentModelClass),
1755
NULL, /* base init */
1756
NULL, /* base finalize */
1757
(GClassInitFunc)egg_recent_model_class_init, /* class init */
1758
NULL, /* class finalize */
1759
NULL, /* class data */
1760
sizeof (EggRecentModel),
1762
(GInstanceInitFunc) egg_recent_model_init
1765
egg_recent_model_type = g_type_register_static (G_TYPE_OBJECT,
1767
&egg_recent_model_info, 0);
1770
return egg_recent_model_type;