]> www.fi.muni.cz Git - evince.git/blob - shell/ev-history.c
window: use the primary-toolbar style class for the window toolbar
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
28 enum
29 {
30         HISTORY_CHANGED,
31         N_SIGNALS
32 };
33
34 static guint signals[N_SIGNALS] = {0, };
35
36 struct _EvHistoryPrivate
37 {
38         GList *links;
39 };
40
41 static void ev_history_init       (EvHistory *history);
42 static void ev_history_class_init (EvHistoryClass *class);
43
44 G_DEFINE_TYPE (EvHistory, ev_history, G_TYPE_OBJECT)
45
46 #define EV_HISTORY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_HISTORY, EvHistoryPrivate))
47
48 static void
49 ev_history_init (EvHistory *history)
50 {
51         history->priv = EV_HISTORY_GET_PRIVATE (history);
52
53         history->priv->links = NULL;
54 }
55
56 static void
57 free_links_list (GList *l)
58 {
59         g_list_foreach (l, (GFunc)g_object_unref, NULL);
60         g_list_free (l);
61 }
62
63 static void
64 ev_history_finalize (GObject *object)
65 {
66         EvHistory *history = EV_HISTORY (object);
67
68         free_links_list (history->priv->links);
69
70         G_OBJECT_CLASS (ev_history_parent_class)->finalize (object);
71 }
72
73 static void
74 ev_history_class_init (EvHistoryClass *class)
75 {
76         GObjectClass *object_class = G_OBJECT_CLASS (class);
77
78         object_class->finalize = ev_history_finalize;
79
80         signals[HISTORY_CHANGED] = 
81                     g_signal_new ("changed",
82                                   G_OBJECT_CLASS_TYPE (object_class),
83                                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
84                                   G_STRUCT_OFFSET (EvHistoryClass, changed),
85                                   NULL, NULL,
86                                   g_cclosure_marshal_VOID__VOID,
87                                   G_TYPE_NONE, 0);
88
89         g_type_class_add_private (object_class, sizeof (EvHistoryPrivate));
90 }
91
92 #define HISTORY_LENGTH   7
93
94 void
95 ev_history_add_link (EvHistory *history, EvLink *link)
96 {
97         GList *l;
98
99         g_return_if_fail (EV_IS_HISTORY (history));
100         g_return_if_fail (EV_IS_LINK (link));
101
102         for (l = history->priv->links; l; l = l->next) {
103                 if (!strcmp (ev_link_get_title (EV_LINK (l->data)), ev_link_get_title (link))) {
104                         g_object_unref (G_OBJECT (l->data));
105                         history->priv->links = g_list_delete_link (history->priv->links, l);
106                         break;
107                 }
108         }
109
110         g_object_ref (link);
111         history->priv->links = g_list_append (history->priv->links,
112                                               link);
113                                               
114         if (g_list_length (history->priv->links) > HISTORY_LENGTH) {
115                 g_object_unref (G_OBJECT (history->priv->links->data));
116                 history->priv->links = g_list_delete_link (history->priv->links, 
117                                                            history->priv->links);
118         }
119         
120         g_signal_emit (history, signals[HISTORY_CHANGED], 0);
121 }
122
123 EvLink *
124 ev_history_get_link_nth (EvHistory *history, int index)
125 {
126         GList *l;
127
128         g_return_val_if_fail (EV_IS_HISTORY (history), NULL);
129
130         l = g_list_nth (history->priv->links, index);
131
132         return EV_LINK (l->data);
133 }
134
135 int
136 ev_history_get_n_links (EvHistory *history)
137 {
138         g_return_val_if_fail (EV_IS_HISTORY (history), -1);
139
140         return g_list_length (history->priv->links);
141 }
142
143 EvHistory *
144 ev_history_new (void)
145 {
146         return EV_HISTORY (g_object_new (EV_TYPE_HISTORY, NULL));
147 }
148