1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /*
3  *  Copyright © 2011 Igalia S.L.
4  *
5  *  This file is part of Epiphany.
6  *
7  *  Epiphany is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  Epiphany is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <glib.h>
22 
23 #include "ephy-history-types.h"
24 
25 EphyHistoryPageVisit *
ephy_history_page_visit_new_with_url(EphyHistoryURL * url,gint64 visit_time,EphyHistoryPageVisitType visit_type)26 ephy_history_page_visit_new_with_url (EphyHistoryURL           *url,
27                                       gint64                    visit_time,
28                                       EphyHistoryPageVisitType  visit_type)
29 {
30   EphyHistoryPageVisit *visit = g_new0 (EphyHistoryPageVisit, 1);
31   visit->id = -1;
32   visit->url = url;
33   visit->visit_time = visit_time;
34   visit->visit_type = visit_type;
35   return visit;
36 }
37 
38 EphyHistoryPageVisit *
ephy_history_page_visit_new(const char * url,gint64 visit_time,EphyHistoryPageVisitType visit_type)39 ephy_history_page_visit_new (const char               *url,
40                              gint64                    visit_time,
41                              EphyHistoryPageVisitType  visit_type)
42 {
43   return ephy_history_page_visit_new_with_url (ephy_history_url_new (url, url, 0, 0, 0),
44                                                visit_time, visit_type);
45 }
46 
47 void
ephy_history_page_visit_free(EphyHistoryPageVisit * visit)48 ephy_history_page_visit_free (EphyHistoryPageVisit *visit)
49 {
50   if (visit == NULL)
51     return;
52 
53   ephy_history_url_free (visit->url);
54   g_free (visit);
55 }
56 
57 EphyHistoryPageVisit *
ephy_history_page_visit_copy(EphyHistoryPageVisit * visit)58 ephy_history_page_visit_copy (EphyHistoryPageVisit *visit)
59 {
60   EphyHistoryPageVisit *copy = ephy_history_page_visit_new_with_url (0, visit->visit_time, visit->visit_type);
61   copy->id = visit->id;
62   copy->url = ephy_history_url_copy (visit->url);
63   return copy;
64 }
65 
66 GList *
ephy_history_page_visit_list_copy(GList * original)67 ephy_history_page_visit_list_copy (GList *original)
68 {
69   GList *new = g_list_copy (original);
70   GList *current = new;
71   while (current) {
72     current->data = ephy_history_page_visit_copy ((EphyHistoryPageVisit *)current->data);
73     current = current->next;
74   }
75   return new;
76 }
77 
78 void
ephy_history_page_visit_list_free(GList * list)79 ephy_history_page_visit_list_free (GList *list)
80 {
81   g_list_free_full (list, (GDestroyNotify)ephy_history_page_visit_free);
82 }
83 
84 EphyHistoryHost *
ephy_history_host_new(const char * url,const char * title,int visit_count,double zoom_level)85 ephy_history_host_new (const char *url,
86                        const char *title,
87                        int         visit_count,
88                        double      zoom_level)
89 {
90   EphyHistoryHost *host = g_new0 (EphyHistoryHost, 1);
91 
92   host->id = -1;
93   host->url = g_strdup (url);
94   host->title = g_strdup (title);
95   host->visit_count = visit_count;
96   host->zoom_level = zoom_level;
97 
98   return host;
99 }
100 
101 EphyHistoryHost *
ephy_history_host_copy(EphyHistoryHost * original)102 ephy_history_host_copy (EphyHistoryHost *original)
103 {
104   EphyHistoryHost *host;
105 
106   if (original == NULL)
107     return NULL;
108 
109   host = ephy_history_host_new (original->url,
110                                 original->title,
111                                 original->visit_count,
112                                 original->zoom_level);
113   host->id = original->id;
114 
115   return host;
116 }
117 
118 void
ephy_history_host_free(EphyHistoryHost * host)119 ephy_history_host_free (EphyHistoryHost *host)
120 {
121   if (host == NULL)
122     return;
123 
124   g_free (host->url);
125   g_free (host->title);
126   g_free (host);
127 }
128 
129 void
ephy_history_host_list_free(GList * list)130 ephy_history_host_list_free (GList *list)
131 {
132   g_list_free_full (list, (GDestroyNotify)ephy_history_host_free);
133 }
134 
135 EphyHistoryURL *
ephy_history_url_new(const char * url,const char * title,int visit_count,int typed_count,gint64 last_visit_time)136 ephy_history_url_new (const char *url,
137                       const char *title,
138                       int         visit_count,
139                       int         typed_count,
140                       gint64      last_visit_time)
141 {
142   EphyHistoryURL *history_url = g_new0 (EphyHistoryURL, 1);
143   history_url->id = -1;
144   history_url->url = g_strdup (url);
145   history_url->title = g_strdup (title);
146   history_url->sync_id = NULL;
147   history_url->visit_count = visit_count;
148   history_url->typed_count = typed_count;
149   history_url->last_visit_time = last_visit_time;
150   history_url->host = NULL;
151   history_url->notify_visit = TRUE;
152   history_url->notify_delete = TRUE;
153   return history_url;
154 }
155 
156 EphyHistoryURL *
ephy_history_url_copy(EphyHistoryURL * url)157 ephy_history_url_copy (EphyHistoryURL *url)
158 {
159   EphyHistoryURL *copy;
160   if (url == NULL)
161     return NULL;
162 
163   copy = ephy_history_url_new (url->url,
164                                url->title,
165                                url->visit_count,
166                                url->typed_count,
167                                url->last_visit_time);
168   copy->id = url->id;
169   copy->sync_id = g_strdup (url->sync_id);
170   copy->hidden = url->hidden;
171   copy->host = ephy_history_host_copy (url->host);
172   copy->notify_visit = url->notify_visit;
173   copy->notify_delete = url->notify_delete;
174 
175   return copy;
176 }
177 
178 void
ephy_history_url_free(EphyHistoryURL * url)179 ephy_history_url_free (EphyHistoryURL *url)
180 {
181   if (url == NULL)
182     return;
183 
184   g_free (url->url);
185   g_free (url->title);
186   g_free (url->sync_id);
187   ephy_history_host_free (url->host);
188   g_free (url);
189 }
190 
191 GList *
ephy_history_url_list_copy(GList * original)192 ephy_history_url_list_copy (GList *original)
193 {
194   GList *new = NULL, *last;
195 
196   if (original) {
197     new = last = g_list_append (NULL, ephy_history_url_copy (original->data));
198     original = original->next;
199 
200     while (original) {
201       last = g_list_append (last, ephy_history_url_copy (original->data));
202       last = last->next;
203       original = original->next;
204     }
205   }
206 
207   return new;
208 }
209 
210 void
ephy_history_url_list_free(GList * list)211 ephy_history_url_list_free (GList *list)
212 {
213   g_list_free_full (list, (GDestroyNotify)ephy_history_url_free);
214 }
215 
216 EphyHistoryQuery *
ephy_history_query_new(void)217 ephy_history_query_new (void)
218 {
219   return g_new0 (EphyHistoryQuery, 1);
220 }
221 
222 void
ephy_history_query_free(EphyHistoryQuery * query)223 ephy_history_query_free (EphyHistoryQuery *query)
224 {
225   g_list_free_full (query->substring_list, g_free);
226   g_free (query);
227 }
228 
229 EphyHistoryQuery *
ephy_history_query_copy(EphyHistoryQuery * query)230 ephy_history_query_copy (EphyHistoryQuery *query)
231 {
232   GList *iter;
233   EphyHistoryQuery *copy = ephy_history_query_new ();
234   copy->from = query->from;
235   copy->to = query->to;
236   copy->limit = query->limit;
237   copy->sort_type = query->sort_type;
238   copy->ignore_hidden = query->ignore_hidden;
239   copy->ignore_local = query->ignore_local;
240   copy->host = query->host;
241 
242   for (iter = query->substring_list; iter != NULL; iter = iter->next) {
243     copy->substring_list = g_list_prepend (copy->substring_list, g_strdup (iter->data));
244   }
245   copy->substring_list = g_list_reverse (copy->substring_list);
246 
247   return copy;
248 }
249