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 
G_DEFINE_TYPE_WITH_PRIVATE(EvHistory,ev_history,G_TYPE_OBJECT)41 G_DEFINE_TYPE_WITH_PRIVATE (EvHistory, ev_history, G_TYPE_OBJECT)
42 
43 static void
44 ev_history_init (EvHistory *history)
45 {
46 	history->priv = ev_history_get_instance_private (history);
47 
48 	history->priv->links = NULL;
49 }
50 
51 static void
free_links_list(GList * l)52 free_links_list (GList *l)
53 {
54 	g_list_foreach (l, (GFunc)g_object_unref, NULL);
55 	g_list_free (l);
56 }
57 
58 static void
ev_history_finalize(GObject * object)59 ev_history_finalize (GObject *object)
60 {
61 	EvHistory *history = EV_HISTORY (object);
62 
63 	free_links_list (history->priv->links);
64 
65 	G_OBJECT_CLASS (ev_history_parent_class)->finalize (object);
66 }
67 
68 static void
ev_history_class_init(EvHistoryClass * class)69 ev_history_class_init (EvHistoryClass *class)
70 {
71 	GObjectClass *object_class = G_OBJECT_CLASS (class);
72 
73 	object_class->finalize = ev_history_finalize;
74 
75 	signals[HISTORY_CHANGED] =
76 		    g_signal_new ("changed",
77 		 	          G_OBJECT_CLASS_TYPE (object_class),
78 				  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
79 				  G_STRUCT_OFFSET (EvHistoryClass, changed),
80 				  NULL, NULL,
81 				  g_cclosure_marshal_VOID__VOID,
82 				  G_TYPE_NONE, 0);
83 }
84 
85 #define HISTORY_LENGTH   7
86 
87 void
ev_history_add_link(EvHistory * history,EvLink * link)88 ev_history_add_link (EvHistory *history, EvLink *link)
89 {
90 	GList *l;
91 
92 	g_return_if_fail (EV_IS_HISTORY (history));
93 	g_return_if_fail (EV_IS_LINK (link));
94 
95 	for (l = history->priv->links; l; l = l->next) {
96 		if (!strcmp (ev_link_get_title (EV_LINK (l->data)), ev_link_get_title (link))) {
97 			g_object_unref (G_OBJECT (l->data));
98 			history->priv->links = g_list_delete_link (history->priv->links, l);
99 			break;
100 		}
101 	}
102 
103 	g_object_ref (link);
104 	history->priv->links = g_list_append (history->priv->links,
105 					      link);
106 
107 	if (g_list_length (history->priv->links) > HISTORY_LENGTH) {
108 		g_object_unref (G_OBJECT (history->priv->links->data));
109 		history->priv->links = g_list_delete_link (history->priv->links,
110 							   history->priv->links);
111 	}
112 
113 	g_signal_emit (history, signals[HISTORY_CHANGED], 0);
114 }
115 
116 EvLink *
ev_history_get_link_nth(EvHistory * history,int index)117 ev_history_get_link_nth	(EvHistory *history, int index)
118 {
119 	g_return_val_if_fail (EV_IS_HISTORY (history), NULL);
120 
121 	return EV_LINK (g_list_nth_data (history->priv->links, index));
122 }
123 
124 int
ev_history_get_n_links(EvHistory * history)125 ev_history_get_n_links (EvHistory *history)
126 {
127 	g_return_val_if_fail (EV_IS_HISTORY (history), -1);
128 
129 	return g_list_length (history->priv->links);
130 }
131 
132 EvHistory *
ev_history_new(void)133 ev_history_new (void)
134 {
135 	return EV_HISTORY (g_object_new (EV_TYPE_HISTORY, NULL));
136 }
137 
138