8cdaa906431daac76429c3b8166982db30938979
[WebKit-https.git] / WebKit / gtk / webkit / webkitwebhistoryitem.cpp
1 /*
2  * Copyright (C) 2008 Jan Michael C. Alonzo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20
21 #include "config.h"
22
23 #include "webkitwebhistoryitem.h"
24 #include "webkitprivate.h"
25
26 #include <glib.h>
27
28 #include "CString.h"
29 #include "HistoryItem.h"
30 #include "PlatformString.h"
31
32 /**
33  * SECTION:webkitwebhistoryitem
34  * @short_description: One item of the #WebKitWebBackForwardList and or global history
35  * @see_also: #WebKitWebBackForwardList
36  *
37  * A history item consists out of a title and a uri. It can be part of the
38  * #WebKitWebBackForwardList and the global history. The global history is used
39  * for coloring the links of visited sites.  #WebKitHistoryItem's constructed with
40  * #webkit_web_history_item_new and #webkit_web_history_item_new_with_data are
41  * automatically added to the global history.
42  *
43  * <informalexample><programlisting>
44  * /<!-- -->* Inject a visited page into the global history *<!-- -->/
45  * webkit_web_history_item_new_with_data("http://www.gnome.org/", "GNOME: The Free Software Desktop Project");
46  * webkit_web_history_item_new_with_data("http://www.webkit.org/", "The WebKit Open Source Project");
47  * </programlisting></informalexample>
48  *
49  */
50
51 using namespace WebKit;
52
53 extern "C" {
54
55 struct _WebKitWebHistoryItemPrivate {
56     WTF::RefPtr<WebCore::HistoryItem> historyItem;
57
58     WebCore::CString title;
59     WebCore::CString alternateTitle;
60     WebCore::CString uri;
61     WebCore::CString originalUri;
62 };
63
64 #define WEBKIT_WEB_HISTORY_ITEM_GET_PRIVATE(obj)    (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_HISTORY_ITEM, WebKitWebHistoryItemPrivate))
65
66 enum {
67     PROP_0,
68
69     PROP_TITLE,
70     PROP_ALTERNATE_TITLE,
71     PROP_URI,
72     PROP_ORIGINAL_URI,
73     PROP_LAST_VISITED_TIME
74 };
75
76 G_DEFINE_TYPE(WebKitWebHistoryItem, webkit_web_history_item, G_TYPE_OBJECT);
77
78 static void webkit_web_history_item_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
79
80 static void webkit_web_history_item_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec);
81
82 static GHashTable* webkit_history_items()
83 {
84     static GHashTable* historyItems = g_hash_table_new(g_direct_hash, g_direct_equal);
85     return historyItems;
86 }
87
88 static void webkit_history_item_add(WebKitWebHistoryItem* webHistoryItem, WebCore::HistoryItem* historyItem)
89 {
90     g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
91
92     GHashTable* table = webkit_history_items();
93
94     g_hash_table_insert(table, historyItem, g_object_ref(webHistoryItem));
95 }
96
97 static void webkit_history_item_remove(WebCore::HistoryItem* historyItem)
98 {
99     GHashTable* table = webkit_history_items();
100     WebKitWebHistoryItem* webHistoryItem = (WebKitWebHistoryItem*) g_hash_table_lookup(table, historyItem);
101
102     g_return_if_fail(webHistoryItem != NULL);
103
104     g_hash_table_remove(table, historyItem);
105     g_object_unref(webHistoryItem);
106 }
107
108 static void webkit_web_history_item_dispose(GObject* object)
109 {
110     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
111
112     webkit_history_item_remove(core(webHistoryItem));
113
114     /* destroy table if empty */
115     GHashTable* table = webkit_history_items();
116     if (!g_hash_table_size(table))
117         g_hash_table_destroy(table);
118
119     G_OBJECT_CLASS(webkit_web_history_item_parent_class)->dispose(object);
120 }
121
122 static void webkit_web_history_item_finalize(GObject* object)
123 {
124     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
125     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
126
127     priv->title = WebCore::CString();
128     priv->alternateTitle = WebCore::CString();
129     priv->uri = WebCore::CString();
130     priv->originalUri = WebCore::CString();
131
132     G_OBJECT_CLASS(webkit_web_history_item_parent_class)->finalize(object);
133 }
134
135 static void webkit_web_history_item_class_init(WebKitWebHistoryItemClass* klass)
136 {
137     GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
138
139     gobject_class->dispose = webkit_web_history_item_dispose;
140     gobject_class->finalize = webkit_web_history_item_finalize;
141     gobject_class->set_property = webkit_web_history_item_set_property;
142     gobject_class->get_property = webkit_web_history_item_get_property;
143
144     /**
145     * WebKitWebHistoryItem:title:
146     *
147     * The title of the history item.
148     *
149     * Since: 1.0.2
150     */
151     g_object_class_install_property(gobject_class,
152                                     PROP_TITLE,
153                                     g_param_spec_string(
154                                     "title",
155                                     "Title",
156                                     "The title of the history item",
157                                     NULL,
158                                     WEBKIT_PARAM_READABLE));
159
160     /**
161     * WebKitWebHistoryItem:alternate-title:
162     *
163     * The alternate title of the history item.
164     *
165     * Since: 1.0.2
166     */
167     g_object_class_install_property(gobject_class,
168                                     PROP_ALTERNATE_TITLE,
169                                     g_param_spec_string(
170                                     "alternate-title",
171                                     "Alternate Title",
172                                     "The alternate title of the history item",
173                                     NULL,
174                                     WEBKIT_PARAM_READWRITE));
175
176     /**
177     * WebKitWebHistoryItem:uri:
178     *
179     * The URI of the history item.
180     *
181     * Since: 1.0.2
182     */
183     g_object_class_install_property(gobject_class,
184                                     PROP_URI,
185                                     g_param_spec_string(
186                                     "uri",
187                                     "URI",
188                                     "The URI of the history item",
189                                     NULL,
190                                     WEBKIT_PARAM_READABLE));
191
192     /**
193     * WebKitWebHistoryItem:original-uri:
194     *
195     * The original URI of the history item.
196     *
197     * Since: 1.0.2
198     */
199     g_object_class_install_property(gobject_class,
200                                     PROP_ORIGINAL_URI,
201                                     g_param_spec_string(
202                                     "original-uri",
203                                     "Original URI",
204                                     "The original URI of the history item",
205                                     NULL,
206                                     WEBKIT_PARAM_READABLE));
207
208    /**
209     * WebKitWebHistoryItem:last-visited-time:
210     *
211     * The time at which the history item was last visited.
212     *
213     * Since: 1.0.2
214     */
215     g_object_class_install_property(gobject_class,
216                                     PROP_LAST_VISITED_TIME,
217                                     g_param_spec_double(
218                                     "last-visited-time",
219                                     "Last visited Time",
220                                     "The time at which the history item was last visited",
221                                     0, G_MAXDOUBLE, 0,
222                                     WEBKIT_PARAM_READABLE));
223
224     g_type_class_add_private(gobject_class, sizeof(WebKitWebHistoryItemPrivate));
225 }
226
227 static void webkit_web_history_item_init(WebKitWebHistoryItem* webHistoryItem)
228 {
229     webHistoryItem->priv = WEBKIT_WEB_HISTORY_ITEM_GET_PRIVATE(webHistoryItem);
230 }
231
232 static void webkit_web_history_item_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
233 {
234     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
235
236     switch(prop_id) {
237     case PROP_ALTERNATE_TITLE:
238         webkit_web_history_item_set_alternate_title(webHistoryItem, g_value_get_string(value));
239         break;
240     default:
241         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
242         break;
243     }
244 }
245
246 static void webkit_web_history_item_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
247 {
248     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
249
250     switch (prop_id) {
251     case PROP_TITLE:
252         g_value_set_string(value, webkit_web_history_item_get_title(webHistoryItem));
253         break;
254     case PROP_ALTERNATE_TITLE:
255         g_value_set_string(value, webkit_web_history_item_get_alternate_title(webHistoryItem));
256         break;
257     case PROP_URI:
258         g_value_set_string(value, webkit_web_history_item_get_uri(webHistoryItem));
259         break;
260     case PROP_ORIGINAL_URI:
261         g_value_set_string(value, webkit_web_history_item_get_original_uri(webHistoryItem));
262         break;
263     case PROP_LAST_VISITED_TIME:
264         g_value_set_double(value, webkit_web_history_item_get_last_visited_time(webHistoryItem));
265         break;
266     default:
267         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
268         break;
269     }
270 }
271
272 /* Helper function to create a new WebHistoryItem instance when needed */
273 WebKitWebHistoryItem* webkit_web_history_item_new_with_core_item(WebCore::HistoryItem* item)
274 {
275     WebKitWebHistoryItem* webHistoryItem = kit(item);
276
277     if (webHistoryItem)
278         g_object_ref(webHistoryItem);
279     else {
280         webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
281         WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
282
283         priv->historyItem = item;
284         webkit_history_item_add(webHistoryItem, priv->historyItem.get());
285     }
286
287     return webHistoryItem;
288 }
289
290
291 /**
292  * webkit_web_history_item_new:
293  *
294  * Creates a new #WebKitWebHistoryItem instance
295  *
296  * Return value: the new #WebKitWebHistoryItem
297  */
298 WebKitWebHistoryItem* webkit_web_history_item_new()
299 {
300     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
301     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
302
303     priv->historyItem = WebCore::HistoryItem::create();
304     webkit_history_item_add(webHistoryItem, priv->historyItem.get());
305
306     return webHistoryItem;
307 }
308
309 /**
310  * webkit_web_history_item_new_with_data:
311  * @uri: the uri of the page
312  * @title: the title of the page
313  *
314  * Creates a new #WebKitWebHistoryItem with the given URI and title
315  *
316  * Return value: the new #WebKitWebHistoryItem
317  */
318 WebKitWebHistoryItem* webkit_web_history_item_new_with_data(const gchar* uri, const gchar* title)
319 {
320     WebCore::KURL historyUri(uri);
321     WebCore::String historyTitle = WebCore::String::fromUTF8(title);
322
323     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
324     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
325
326     priv->historyItem = WebCore::HistoryItem::create(historyUri, historyTitle, 0);
327     webkit_history_item_add(webHistoryItem, priv->historyItem.get());
328
329     return webHistoryItem;
330 }
331
332 /**
333  * webkit_web_history_item_get_title:
334  * @web_history_item: a #WebKitWebHistoryItem
335  *
336  * Returns: the page title of @web_history_item
337  */
338 G_CONST_RETURN gchar* webkit_web_history_item_get_title(WebKitWebHistoryItem* webHistoryItem)
339 {
340     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
341
342     WebCore::HistoryItem* item = core(webHistoryItem);
343
344     g_return_val_if_fail(item != NULL, NULL);
345
346     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
347     priv->title = item->title().utf8();
348
349     return priv->title.data();
350 }
351
352 /**
353  * webkit_web_history_item_get_alternate_title:
354  * @web_history_item: a #WebKitWebHistoryItem
355  *
356  * Returns the alternate title of @web_history_item
357  *
358  * Return value: the alternate title of @web_history_item
359  */
360 G_CONST_RETURN gchar* webkit_web_history_item_get_alternate_title(WebKitWebHistoryItem* webHistoryItem)
361 {
362     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
363
364     WebCore::HistoryItem* item = core(webHistoryItem);
365
366     g_return_val_if_fail(item != NULL, NULL);
367
368     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
369     priv->alternateTitle = item->alternateTitle().utf8();
370
371     return priv->alternateTitle.data();
372 }
373
374 /**
375  * webkit_web_history_item_set_alternate_title:
376  * @web_history_item: a #WebKitWebHistoryItem
377  * @title: the alternate title for @this history item
378  *
379  * Sets an alternate title for @web_history_item
380  */
381 void webkit_web_history_item_set_alternate_title(WebKitWebHistoryItem* webHistoryItem, const gchar* title)
382 {
383     g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
384     g_return_if_fail(title);
385
386     WebCore::HistoryItem* item = core(webHistoryItem);
387
388     item->setAlternateTitle(WebCore::String::fromUTF8(title));
389     g_object_notify(G_OBJECT(webHistoryItem), "alternate-title");
390 }
391
392 /**
393  * webkit_web_history_item_get_uri:
394  * @web_history_item: a #WebKitWebHistoryItem
395  *
396  * Returns the URI of @this
397  *
398  * Return value: the URI of @web_history_item
399  */
400 G_CONST_RETURN gchar* webkit_web_history_item_get_uri(WebKitWebHistoryItem* webHistoryItem)
401 {
402     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
403
404     WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
405
406     g_return_val_if_fail(item != NULL, NULL);
407
408     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
409     priv->uri = item->urlString().utf8();
410
411     return priv->uri.data();
412 }
413
414 /**
415  * webkit_web_history_item_get_original_uri:
416  * @web_history_item: a #WebKitWebHistoryItem
417  *
418  * Returns the original URI of @web_history_item.
419  *
420  * Return value: the original URI of @web_history_item
421  */
422 G_CONST_RETURN gchar* webkit_web_history_item_get_original_uri(WebKitWebHistoryItem* webHistoryItem)
423 {
424     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
425
426     WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
427
428     g_return_val_if_fail(item != NULL, NULL);
429
430     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
431     priv->originalUri = item->originalURLString().utf8();
432
433     return webHistoryItem->priv->originalUri.data();
434 }
435
436 /**
437  * webkit_web_history_item_get_last_visisted_time :
438  * @web_history_item: a #WebKitWebHistoryItem
439  *
440  * Returns the last time @web_history_item was visited
441  *
442  * Return value: the time in seconds this @web_history_item was last visited
443  */
444 gdouble webkit_web_history_item_get_last_visited_time(WebKitWebHistoryItem* webHistoryItem)
445 {
446     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), 0);
447
448     WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
449
450     g_return_val_if_fail(item != NULL, 0);
451
452     return item->lastVisitedTime();
453 }
454
455 } /* end extern "C" */
456
457 WebCore::HistoryItem* WebKit::core(WebKitWebHistoryItem* webHistoryItem)
458 {
459     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
460
461     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
462     WTF::RefPtr<WebCore::HistoryItem> historyItem = priv->historyItem;
463
464     return historyItem ? historyItem.get() : 0;
465 }
466
467 WebKitWebHistoryItem* WebKit::kit(WebCore::HistoryItem* historyItem)
468 {
469     g_return_val_if_fail(historyItem != NULL, NULL);
470
471     WebKitWebHistoryItem* webHistoryItem;
472     GHashTable* table = webkit_history_items();
473
474     webHistoryItem = (WebKitWebHistoryItem*) g_hash_table_lookup(table, historyItem);
475     return webHistoryItem;
476 }
477