]> www.fi.muni.cz Git - evince.git/blob - shell/ev-history.c
Implements another history variant
[evince.git] / shell / ev-history.c
1 /*
2  *  Copyright (C) 2005 Marco Pesenti Gritti
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19
20 #include "config.h"
21
22 #include <glib/gi18n.h>
23 #include <string.h>
24
25 #include "ev-history.h"
26
27 struct _EvHistoryPrivate
28 {
29         GList *links;
30 };
31
32 static void ev_history_init       (EvHistory *history);
33 static void ev_history_class_init (EvHistoryClass *class);
34
35 G_DEFINE_TYPE (EvHistory, ev_history, G_TYPE_OBJECT)
36
37 #define EV_HISTORY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_HISTORY, EvHistoryPrivate))
38
39 static void
40 ev_history_init (EvHistory *history)
41 {
42         history->priv = EV_HISTORY_GET_PRIVATE (history);
43
44         history->priv->links = NULL;
45 }
46
47 static void
48 free_links_list (GList *l)
49 {
50         g_list_foreach (l, (GFunc)g_object_unref, NULL);
51         g_list_free (l);
52 }
53
54 static void
55 ev_history_finalize (GObject *object)
56 {
57         EvHistory *history = EV_HISTORY (object);
58
59         free_links_list (history->priv->links);
60
61         G_OBJECT_CLASS (ev_history_parent_class)->finalize (object);
62 }
63
64 static void
65 ev_history_class_init (EvHistoryClass *class)
66 {
67         GObjectClass *object_class = G_OBJECT_CLASS (class);
68
69         object_class->finalize = ev_history_finalize;
70
71         g_type_class_add_private (object_class, sizeof (EvHistoryPrivate));
72 }
73
74 #define HISTORY_LENGTH   7
75
76 void
77 ev_history_add_link (EvHistory *history, EvLink *link)
78 {
79         GList *l;
80
81         g_return_if_fail (EV_IS_HISTORY (history));
82         g_return_if_fail (EV_IS_LINK (link));
83
84         for (l = history->priv->links; l; l = l->next) {
85                 if (!strcmp (ev_link_get_title (EV_LINK (l->data)), ev_link_get_title (link))) {
86                         g_object_unref (G_OBJECT (l->data));
87                         history->priv->links = g_list_delete_link (history->priv->links, l);
88                         break;
89                 }
90         }
91
92         g_object_ref (link);
93         history->priv->links = g_list_append (history->priv->links,
94                                               link);
95                                               
96         if (g_list_length (history->priv->links) > HISTORY_LENGTH) {
97                 g_object_unref (G_OBJECT (history->priv->links->data));
98                 history->priv->links = g_list_delete_link (history->priv->links, 
99                                                            history->priv->links);
100         }
101 }
102
103 EvLink *
104 ev_history_get_link_nth (EvHistory *history, int index)
105 {
106         GList *l;
107
108         g_return_val_if_fail (EV_IS_HISTORY (history), NULL);
109
110         l = g_list_nth (history->priv->links, index);
111
112         return EV_LINK (l->data);
113 }
114
115 int
116 ev_history_get_n_links (EvHistory *history)
117 {
118         g_return_val_if_fail (EV_IS_HISTORY (history), -1);
119
120         return g_list_length (history->priv->links);
121 }
122
123 EvHistory *
124 ev_history_new (void)
125 {
126         return EV_HISTORY (g_object_new (EV_TYPE_HISTORY, NULL));
127 }
128