2 * Copyright (C) 2008 Jan Michael C. Alonzo
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.
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.
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.
23 #include "webkitwebhistoryitem.h"
24 #include "webkitprivate.h"
29 #include "HistoryItem.h"
30 #include "PlatformString.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. #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.
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;
55 struct _WebKitWebHistoryItemPrivate {
56 WTF::RefPtr<WebCore::HistoryItem> historyItem;
58 WebCore::CString title;
59 WebCore::CString alternateTitle;
61 WebCore::CString originalUri;
64 #define WEBKIT_WEB_HISTORY_ITEM_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_HISTORY_ITEM, WebKitWebHistoryItemPrivate))
73 PROP_LAST_VISITED_TIME
76 G_DEFINE_TYPE(WebKitWebHistoryItem, webkit_web_history_item, G_TYPE_OBJECT);
78 static void webkit_web_history_item_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
80 static void webkit_web_history_item_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec);
82 static GHashTable* webkit_history_items()
84 static GHashTable* historyItems = g_hash_table_new(g_direct_hash, g_direct_equal);
88 static void webkit_history_item_add(WebKitWebHistoryItem* webHistoryItem, WebCore::HistoryItem* historyItem)
90 g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
92 GHashTable* table = webkit_history_items();
94 g_hash_table_insert(table, historyItem, g_object_ref(webHistoryItem));
97 static void webkit_history_item_remove(WebCore::HistoryItem* historyItem)
99 GHashTable* table = webkit_history_items();
100 WebKitWebHistoryItem* webHistoryItem = (WebKitWebHistoryItem*) g_hash_table_lookup(table, historyItem);
102 g_return_if_fail(webHistoryItem != NULL);
104 g_hash_table_remove(table, historyItem);
105 g_object_unref(webHistoryItem);
108 static void webkit_web_history_item_dispose(GObject* object)
110 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
112 webkit_history_item_remove(core(webHistoryItem));
114 /* destroy table if empty */
115 GHashTable* table = webkit_history_items();
116 if (!g_hash_table_size(table))
117 g_hash_table_destroy(table);
119 G_OBJECT_CLASS(webkit_web_history_item_parent_class)->dispose(object);
122 static void webkit_web_history_item_finalize(GObject* object)
124 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
125 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
127 priv->title = WebCore::CString();
128 priv->alternateTitle = WebCore::CString();
129 priv->uri = WebCore::CString();
130 priv->originalUri = WebCore::CString();
132 G_OBJECT_CLASS(webkit_web_history_item_parent_class)->finalize(object);
135 static void webkit_web_history_item_class_init(WebKitWebHistoryItemClass* klass)
137 GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
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;
145 * WebKitWebHistoryItem:title:
147 * The title of the history item.
151 g_object_class_install_property(gobject_class,
156 "The title of the history item",
158 WEBKIT_PARAM_READABLE));
161 * WebKitWebHistoryItem:alternate-title:
163 * The alternate title of the history item.
167 g_object_class_install_property(gobject_class,
168 PROP_ALTERNATE_TITLE,
172 "The alternate title of the history item",
174 WEBKIT_PARAM_READWRITE));
177 * WebKitWebHistoryItem:uri:
179 * The URI of the history item.
183 g_object_class_install_property(gobject_class,
188 "The URI of the history item",
190 WEBKIT_PARAM_READABLE));
193 * WebKitWebHistoryItem:original-uri:
195 * The original URI of the history item.
199 g_object_class_install_property(gobject_class,
204 "The original URI of the history item",
206 WEBKIT_PARAM_READABLE));
209 * WebKitWebHistoryItem:last-visited-time:
211 * The time at which the history item was last visited.
215 g_object_class_install_property(gobject_class,
216 PROP_LAST_VISITED_TIME,
220 "The time at which the history item was last visited",
222 WEBKIT_PARAM_READABLE));
224 g_type_class_add_private(gobject_class, sizeof(WebKitWebHistoryItemPrivate));
227 static void webkit_web_history_item_init(WebKitWebHistoryItem* webHistoryItem)
229 webHistoryItem->priv = WEBKIT_WEB_HISTORY_ITEM_GET_PRIVATE(webHistoryItem);
232 static void webkit_web_history_item_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
234 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
237 case PROP_ALTERNATE_TITLE:
238 webkit_web_history_item_set_alternate_title(webHistoryItem, g_value_get_string(value));
241 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
246 static void webkit_web_history_item_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
248 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
252 g_value_set_string(value, webkit_web_history_item_get_title(webHistoryItem));
254 case PROP_ALTERNATE_TITLE:
255 g_value_set_string(value, webkit_web_history_item_get_alternate_title(webHistoryItem));
258 g_value_set_string(value, webkit_web_history_item_get_uri(webHistoryItem));
260 case PROP_ORIGINAL_URI:
261 g_value_set_string(value, webkit_web_history_item_get_original_uri(webHistoryItem));
263 case PROP_LAST_VISITED_TIME:
264 g_value_set_double(value, webkit_web_history_item_get_last_visited_time(webHistoryItem));
267 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
272 /* Helper function to create a new WebHistoryItem instance when needed */
273 WebKitWebHistoryItem* webkit_web_history_item_new_with_core_item(WebCore::HistoryItem* item)
275 WebKitWebHistoryItem* webHistoryItem = kit(item);
278 g_object_ref(webHistoryItem);
280 webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
281 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
283 priv->historyItem = item;
284 webkit_history_item_add(webHistoryItem, priv->historyItem.get());
287 return webHistoryItem;
292 * webkit_web_history_item_new:
294 * Creates a new #WebKitWebHistoryItem instance
296 * Return value: the new #WebKitWebHistoryItem
298 WebKitWebHistoryItem* webkit_web_history_item_new()
300 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
301 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
303 priv->historyItem = WebCore::HistoryItem::create();
304 webkit_history_item_add(webHistoryItem, priv->historyItem.get());
306 return webHistoryItem;
310 * webkit_web_history_item_new_with_data:
311 * @uri: the uri of the page
312 * @title: the title of the page
314 * Creates a new #WebKitWebHistoryItem with the given URI and title
316 * Return value: the new #WebKitWebHistoryItem
318 WebKitWebHistoryItem* webkit_web_history_item_new_with_data(const gchar* uri, const gchar* title)
320 WebCore::KURL historyUri(uri);
321 WebCore::String historyTitle = WebCore::String::fromUTF8(title);
323 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
324 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
326 priv->historyItem = WebCore::HistoryItem::create(historyUri, historyTitle, 0);
327 webkit_history_item_add(webHistoryItem, priv->historyItem.get());
329 return webHistoryItem;
333 * webkit_web_history_item_get_title:
334 * @web_history_item: a #WebKitWebHistoryItem
336 * Returns: the page title of @web_history_item
338 G_CONST_RETURN gchar* webkit_web_history_item_get_title(WebKitWebHistoryItem* webHistoryItem)
340 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
342 WebCore::HistoryItem* item = core(webHistoryItem);
344 g_return_val_if_fail(item != NULL, NULL);
346 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
347 priv->title = item->title().utf8();
349 return priv->title.data();
353 * webkit_web_history_item_get_alternate_title:
354 * @web_history_item: a #WebKitWebHistoryItem
356 * Returns the alternate title of @web_history_item
358 * Return value: the alternate title of @web_history_item
360 G_CONST_RETURN gchar* webkit_web_history_item_get_alternate_title(WebKitWebHistoryItem* webHistoryItem)
362 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
364 WebCore::HistoryItem* item = core(webHistoryItem);
366 g_return_val_if_fail(item != NULL, NULL);
368 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
369 priv->alternateTitle = item->alternateTitle().utf8();
371 return priv->alternateTitle.data();
375 * webkit_web_history_item_set_alternate_title:
376 * @web_history_item: a #WebKitWebHistoryItem
377 * @title: the alternate title for @this history item
379 * Sets an alternate title for @web_history_item
381 void webkit_web_history_item_set_alternate_title(WebKitWebHistoryItem* webHistoryItem, const gchar* title)
383 g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
384 g_return_if_fail(title);
386 WebCore::HistoryItem* item = core(webHistoryItem);
388 item->setAlternateTitle(WebCore::String::fromUTF8(title));
389 g_object_notify(G_OBJECT(webHistoryItem), "alternate-title");
393 * webkit_web_history_item_get_uri:
394 * @web_history_item: a #WebKitWebHistoryItem
396 * Returns the URI of @this
398 * Return value: the URI of @web_history_item
400 G_CONST_RETURN gchar* webkit_web_history_item_get_uri(WebKitWebHistoryItem* webHistoryItem)
402 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
404 WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
406 g_return_val_if_fail(item != NULL, NULL);
408 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
409 priv->uri = item->urlString().utf8();
411 return priv->uri.data();
415 * webkit_web_history_item_get_original_uri:
416 * @web_history_item: a #WebKitWebHistoryItem
418 * Returns the original URI of @web_history_item.
420 * Return value: the original URI of @web_history_item
422 G_CONST_RETURN gchar* webkit_web_history_item_get_original_uri(WebKitWebHistoryItem* webHistoryItem)
424 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
426 WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
428 g_return_val_if_fail(item != NULL, NULL);
430 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
431 priv->originalUri = item->originalURLString().utf8();
433 return webHistoryItem->priv->originalUri.data();
437 * webkit_web_history_item_get_last_visisted_time :
438 * @web_history_item: a #WebKitWebHistoryItem
440 * Returns the last time @web_history_item was visited
442 * Return value: the time in seconds this @web_history_item was last visited
444 gdouble webkit_web_history_item_get_last_visited_time(WebKitWebHistoryItem* webHistoryItem)
446 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), 0);
448 WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
450 g_return_val_if_fail(item != NULL, 0);
452 return item->lastVisitedTime();
455 } /* end extern "C" */
457 WebCore::HistoryItem* WebKit::core(WebKitWebHistoryItem* webHistoryItem)
459 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
461 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
462 WTF::RefPtr<WebCore::HistoryItem> historyItem = priv->historyItem;
464 return historyItem ? historyItem.get() : 0;
467 WebKitWebHistoryItem* WebKit::kit(WebCore::HistoryItem* historyItem)
469 g_return_val_if_fail(historyItem != NULL, NULL);
471 WebKitWebHistoryItem* webHistoryItem;
472 GHashTable* table = webkit_history_items();
474 webHistoryItem = (WebKitWebHistoryItem*) g_hash_table_lookup(table, historyItem);
475 return webHistoryItem;