2 * Copyright (C) 2008, 2009 Jan Michael C. Alonzo
3 * Copyright (C) 2009 Igalia S.L.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #include "webkitwebhistoryitem.h"
24 #include "HistoryItem.h"
25 #include "PlatformString.h"
26 #include "webkitglobalsprivate.h"
27 #include "webkitwebhistoryitemprivate.h"
29 #include <glib/gi18n-lib.h>
30 #include <wtf/text/CString.h>
33 * SECTION:webkitwebhistoryitem
34 * @short_description: One item of the #WebKitWebBackForwardList and or global history
35 * @see_also: #WebKitWebBackForwardList
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. #WebKitWebHistoryItem'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.
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>
51 using namespace WebKit;
53 struct _WebKitWebHistoryItemPrivate {
54 WebCore::HistoryItem* historyItem;
57 WTF::CString alternateTitle;
59 WTF::CString originalUri;
71 PROP_LAST_VISITED_TIME
74 G_DEFINE_TYPE(WebKitWebHistoryItem, webkit_web_history_item, G_TYPE_OBJECT);
76 static void webkit_web_history_item_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
78 static void webkit_web_history_item_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec);
80 GHashTable* webkit_history_items()
82 static GHashTable* historyItems = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_object_unref);
86 void webkit_history_item_add(WebKitWebHistoryItem* webHistoryItem, WebCore::HistoryItem* historyItem)
88 g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
90 GHashTable* table = webkit_history_items();
91 g_hash_table_insert(table, historyItem, webHistoryItem);
94 static void webkit_web_history_item_dispose(GObject* object)
96 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
97 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
99 if (!priv->disposed) {
100 WebCore::HistoryItem* item = core(webHistoryItem);
102 priv->disposed = true;
105 G_OBJECT_CLASS(webkit_web_history_item_parent_class)->dispose(object);
108 static void webkit_web_history_item_finalize(GObject* object)
110 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
111 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
113 priv->title = WTF::CString();
114 priv->alternateTitle = WTF::CString();
115 priv->uri = WTF::CString();
116 priv->originalUri = WTF::CString();
118 G_OBJECT_CLASS(webkit_web_history_item_parent_class)->finalize(object);
121 static void webkit_web_history_item_class_init(WebKitWebHistoryItemClass* klass)
123 GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
125 gobject_class->dispose = webkit_web_history_item_dispose;
126 gobject_class->finalize = webkit_web_history_item_finalize;
127 gobject_class->set_property = webkit_web_history_item_set_property;
128 gobject_class->get_property = webkit_web_history_item_get_property;
133 * WebKitWebHistoryItem:title:
135 * The title of the history item.
139 g_object_class_install_property(gobject_class,
144 _("The title of the history item"),
146 WEBKIT_PARAM_READABLE));
149 * WebKitWebHistoryItem:alternate-title:
151 * The alternate title of the history item.
155 g_object_class_install_property(gobject_class,
156 PROP_ALTERNATE_TITLE,
159 _("Alternate Title"),
160 _("The alternate title of the history item"),
162 WEBKIT_PARAM_READWRITE));
165 * WebKitWebHistoryItem:uri:
167 * The URI of the history item.
171 g_object_class_install_property(gobject_class,
176 _("The URI of the history item"),
178 WEBKIT_PARAM_READABLE));
181 * WebKitWebHistoryItem:original-uri:
183 * The original URI of the history item.
187 g_object_class_install_property(gobject_class,
192 _("The original URI of the history item"),
194 WEBKIT_PARAM_READABLE));
197 * WebKitWebHistoryItem:last-visited-time:
199 * The time at which the history item was last visited.
203 g_object_class_install_property(gobject_class,
204 PROP_LAST_VISITED_TIME,
207 _("Last visited Time"),
208 _("The time at which the history item was last visited"),
210 WEBKIT_PARAM_READABLE));
212 g_type_class_add_private(gobject_class, sizeof(WebKitWebHistoryItemPrivate));
215 static void webkit_web_history_item_init(WebKitWebHistoryItem* webHistoryItem)
217 webHistoryItem->priv = G_TYPE_INSTANCE_GET_PRIVATE(webHistoryItem, WEBKIT_TYPE_WEB_HISTORY_ITEM, WebKitWebHistoryItemPrivate);
220 static void webkit_web_history_item_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
222 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
225 case PROP_ALTERNATE_TITLE:
226 webkit_web_history_item_set_alternate_title(webHistoryItem, g_value_get_string(value));
229 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
234 static void webkit_web_history_item_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
236 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
240 g_value_set_string(value, webkit_web_history_item_get_title(webHistoryItem));
242 case PROP_ALTERNATE_TITLE:
243 g_value_set_string(value, webkit_web_history_item_get_alternate_title(webHistoryItem));
246 g_value_set_string(value, webkit_web_history_item_get_uri(webHistoryItem));
248 case PROP_ORIGINAL_URI:
249 g_value_set_string(value, webkit_web_history_item_get_original_uri(webHistoryItem));
251 case PROP_LAST_VISITED_TIME:
252 g_value_set_double(value, webkit_web_history_item_get_last_visited_time(webHistoryItem));
255 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
260 /* Helper function to create a new WebHistoryItem instance when needed */
261 WebKitWebHistoryItem* webkit_web_history_item_new_with_core_item(PassRefPtr<WebCore::HistoryItem> historyItem)
263 return kit(historyItem);
268 * webkit_web_history_item_new:
270 * Creates a new #WebKitWebHistoryItem instance
272 * Return value: the new #WebKitWebHistoryItem
274 WebKitWebHistoryItem* webkit_web_history_item_new()
276 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
277 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
279 RefPtr<WebCore::HistoryItem> item = WebCore::HistoryItem::create();
280 priv->historyItem = item.release().releaseRef();
281 webkit_history_item_add(webHistoryItem, priv->historyItem);
283 return webHistoryItem;
287 * webkit_web_history_item_new_with_data:
288 * @uri: the uri of the page
289 * @title: the title of the page
291 * Creates a new #WebKitWebHistoryItem with the given URI and title
293 * Return value: the new #WebKitWebHistoryItem
295 WebKitWebHistoryItem* webkit_web_history_item_new_with_data(const gchar* uri, const gchar* title)
297 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
298 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
300 WebCore::KURL historyUri(WebCore::KURL(), uri);
301 WTF::String historyTitle = WTF::String::fromUTF8(title);
302 RefPtr<WebCore::HistoryItem> item = WebCore::HistoryItem::create(historyUri, historyTitle, 0);
303 priv->historyItem = item.release().releaseRef();
304 webkit_history_item_add(webHistoryItem, priv->historyItem);
306 return webHistoryItem;
310 * webkit_web_history_item_get_title:
311 * @web_history_item: a #WebKitWebHistoryItem
313 * Returns: the page title of @web_history_item
315 G_CONST_RETURN gchar* webkit_web_history_item_get_title(WebKitWebHistoryItem* webHistoryItem)
317 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
319 WebCore::HistoryItem* item = core(webHistoryItem);
321 g_return_val_if_fail(item, NULL);
323 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
324 priv->title = item->title().utf8();
326 return priv->title.data();
330 * webkit_web_history_item_get_alternate_title:
331 * @web_history_item: a #WebKitWebHistoryItem
333 * Returns the alternate title of @web_history_item
335 * Return value: the alternate title of @web_history_item
337 G_CONST_RETURN gchar* webkit_web_history_item_get_alternate_title(WebKitWebHistoryItem* webHistoryItem)
339 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
341 WebCore::HistoryItem* item = core(webHistoryItem);
343 g_return_val_if_fail(item, NULL);
345 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
346 priv->alternateTitle = item->alternateTitle().utf8();
348 return priv->alternateTitle.data();
352 * webkit_web_history_item_set_alternate_title:
353 * @web_history_item: a #WebKitWebHistoryItem
354 * @title: the alternate title for @this history item
356 * Sets an alternate title for @web_history_item
358 void webkit_web_history_item_set_alternate_title(WebKitWebHistoryItem* webHistoryItem, const gchar* title)
360 g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
361 g_return_if_fail(title);
363 WebCore::HistoryItem* item = core(webHistoryItem);
365 item->setAlternateTitle(WTF::String::fromUTF8(title));
366 g_object_notify(G_OBJECT(webHistoryItem), "alternate-title");
370 * webkit_web_history_item_get_uri:
371 * @web_history_item: a #WebKitWebHistoryItem
373 * Returns the URI of @this
375 * Return value: the URI of @web_history_item
377 G_CONST_RETURN gchar* webkit_web_history_item_get_uri(WebKitWebHistoryItem* webHistoryItem)
379 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
381 WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
383 g_return_val_if_fail(item, NULL);
385 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
386 priv->uri = item->urlString().utf8();
388 return priv->uri.data();
392 * webkit_web_history_item_get_original_uri:
393 * @web_history_item: a #WebKitWebHistoryItem
395 * Returns the original URI of @web_history_item.
397 * Return value: the original URI of @web_history_item
399 G_CONST_RETURN gchar* webkit_web_history_item_get_original_uri(WebKitWebHistoryItem* webHistoryItem)
401 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
403 WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
405 g_return_val_if_fail(item, NULL);
407 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
408 priv->originalUri = item->originalURLString().utf8();
410 return webHistoryItem->priv->originalUri.data();
414 * webkit_web_history_item_get_last_visisted_time :
415 * @web_history_item: a #WebKitWebHistoryItem
417 * Returns the last time @web_history_item was visited
419 * Return value: the time in seconds this @web_history_item was last visited
421 gdouble webkit_web_history_item_get_last_visited_time(WebKitWebHistoryItem* webHistoryItem)
423 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), 0);
425 WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
427 g_return_val_if_fail(item, 0);
429 return item->lastVisitedTime();
433 * webkit_web_history_item_copy:
434 * @web_history_item: a #WebKitWebHistoryItem
436 * Makes a copy of the item for use with other WebView objects.
440 * Return value: (transfer full): the new #WebKitWebHistoryItem.
442 WebKitWebHistoryItem* webkit_web_history_item_copy(WebKitWebHistoryItem* self)
444 WebKitWebHistoryItemPrivate* selfPrivate = self->priv;
446 WebKitWebHistoryItem* item = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, 0));
447 WebKitWebHistoryItemPrivate* priv = item->priv;
449 priv->title = selfPrivate->title;
450 priv->alternateTitle = selfPrivate->alternateTitle;
451 priv->uri = selfPrivate->uri;
452 priv->originalUri = selfPrivate->originalUri;
454 priv->historyItem = selfPrivate->historyItem->copy().releaseRef();
459 /* private methods */
461 G_CONST_RETURN gchar* webkit_web_history_item_get_target(WebKitWebHistoryItem* webHistoryItem)
463 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
465 WebCore::HistoryItem* item = core(webHistoryItem);
467 g_return_val_if_fail(item, NULL);
469 WTF::CString t = item->target().utf8();
470 return g_strdup(t.data());
473 gboolean webkit_web_history_item_is_target_item(WebKitWebHistoryItem* webHistoryItem)
475 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), false);
477 WebCore::HistoryItem* item = core(webHistoryItem);
479 g_return_val_if_fail(item, false);
481 return item->isTargetItem();
484 GList* webkit_web_history_item_get_children(WebKitWebHistoryItem* webHistoryItem)
486 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
488 WebCore::HistoryItem* item = core(webHistoryItem);
490 g_return_val_if_fail(item, NULL);
492 const WebCore::HistoryItemVector& children = item->children();
493 if (!children.size())
496 unsigned size = children.size();
498 for (unsigned i = 0; i < size; ++i)
499 kids = g_list_prepend(kids, kit(children[i].get()));
501 return g_list_reverse(kids);
504 WebCore::HistoryItem* WebKit::core(WebKitWebHistoryItem* webHistoryItem)
506 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
508 return webHistoryItem->priv->historyItem;
511 WebKitWebHistoryItem* WebKit::kit(PassRefPtr<WebCore::HistoryItem> historyItem)
513 g_return_val_if_fail(historyItem, NULL);
515 RefPtr<WebCore::HistoryItem> item = historyItem;
516 GHashTable* table = webkit_history_items();
517 WebKitWebHistoryItem* webHistoryItem = (WebKitWebHistoryItem*) g_hash_table_lookup(table, item.get());
519 if (!webHistoryItem) {
520 webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
521 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
523 priv->historyItem = item.release().releaseRef();
524 webkit_history_item_add(webHistoryItem, priv->historyItem);
527 return webHistoryItem;