2 * Copyright (C) 2008, 2009 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 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(PassRefPtr<WebCore::HistoryItem> historyItem)
275 return kit(historyItem);
280 * webkit_web_history_item_new:
282 * Creates a new #WebKitWebHistoryItem instance
284 * Return value: the new #WebKitWebHistoryItem
286 WebKitWebHistoryItem* webkit_web_history_item_new()
288 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
289 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
291 RefPtr<WebCore::HistoryItem> item = WebCore::HistoryItem::create();
292 priv->historyItem = item.release().releaseRef();
293 webkit_history_item_add(webHistoryItem, priv->historyItem);
295 return webHistoryItem;
299 * webkit_web_history_item_new_with_data:
300 * @uri: the uri of the page
301 * @title: the title of the page
303 * Creates a new #WebKitWebHistoryItem with the given URI and title
305 * Return value: the new #WebKitWebHistoryItem
307 WebKitWebHistoryItem* webkit_web_history_item_new_with_data(const gchar* uri, const gchar* title)
309 WebCore::KURL historyUri(uri);
310 WebCore::String historyTitle = WebCore::String::fromUTF8(title);
312 WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
313 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
315 RefPtr<WebCore::HistoryItem> item = WebCore::HistoryItem::create(historyUri, historyTitle, 0);
316 priv->historyItem = item.release().releaseRef();
317 webkit_history_item_add(webHistoryItem, priv->historyItem);
319 return webHistoryItem;
323 * webkit_web_history_item_get_title:
324 * @web_history_item: a #WebKitWebHistoryItem
326 * Returns: the page title of @web_history_item
328 G_CONST_RETURN gchar* webkit_web_history_item_get_title(WebKitWebHistoryItem* webHistoryItem)
330 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
332 WebCore::HistoryItem* item = core(webHistoryItem);
334 g_return_val_if_fail(item != NULL, NULL);
336 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
337 priv->title = item->title().utf8();
339 return priv->title.data();
343 * webkit_web_history_item_get_alternate_title:
344 * @web_history_item: a #WebKitWebHistoryItem
346 * Returns the alternate title of @web_history_item
348 * Return value: the alternate title of @web_history_item
350 G_CONST_RETURN gchar* webkit_web_history_item_get_alternate_title(WebKitWebHistoryItem* webHistoryItem)
352 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
354 WebCore::HistoryItem* item = core(webHistoryItem);
356 g_return_val_if_fail(item != NULL, NULL);
358 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
359 priv->alternateTitle = item->alternateTitle().utf8();
361 return priv->alternateTitle.data();
365 * webkit_web_history_item_set_alternate_title:
366 * @web_history_item: a #WebKitWebHistoryItem
367 * @title: the alternate title for @this history item
369 * Sets an alternate title for @web_history_item
371 void webkit_web_history_item_set_alternate_title(WebKitWebHistoryItem* webHistoryItem, const gchar* title)
373 g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
374 g_return_if_fail(title);
376 WebCore::HistoryItem* item = core(webHistoryItem);
378 item->setAlternateTitle(WebCore::String::fromUTF8(title));
379 g_object_notify(G_OBJECT(webHistoryItem), "alternate-title");
383 * webkit_web_history_item_get_uri:
384 * @web_history_item: a #WebKitWebHistoryItem
386 * Returns the URI of @this
388 * Return value: the URI of @web_history_item
390 G_CONST_RETURN gchar* webkit_web_history_item_get_uri(WebKitWebHistoryItem* webHistoryItem)
392 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
394 WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
396 g_return_val_if_fail(item != NULL, NULL);
398 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
399 priv->uri = item->urlString().utf8();
401 return priv->uri.data();
405 * webkit_web_history_item_get_original_uri:
406 * @web_history_item: a #WebKitWebHistoryItem
408 * Returns the original URI of @web_history_item.
410 * Return value: the original URI of @web_history_item
412 G_CONST_RETURN gchar* webkit_web_history_item_get_original_uri(WebKitWebHistoryItem* webHistoryItem)
414 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
416 WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
418 g_return_val_if_fail(item != NULL, NULL);
420 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
421 priv->originalUri = item->originalURLString().utf8();
423 return webHistoryItem->priv->originalUri.data();
427 * webkit_web_history_item_get_last_visisted_time :
428 * @web_history_item: a #WebKitWebHistoryItem
430 * Returns the last time @web_history_item was visited
432 * Return value: the time in seconds this @web_history_item was last visited
434 gdouble webkit_web_history_item_get_last_visited_time(WebKitWebHistoryItem* webHistoryItem)
436 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), 0);
438 WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
440 g_return_val_if_fail(item != NULL, 0);
442 return item->lastVisitedTime();
445 /* private methods */
447 G_CONST_RETURN gchar* webkit_web_history_item_get_target(WebKitWebHistoryItem* webHistoryItem)
449 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
451 WebCore::HistoryItem* item = core(webHistoryItem);
453 g_return_val_if_fail(item != NULL, NULL);
455 WebCore::CString t = item->target().utf8();
456 return g_strdup(t.data());
459 gboolean webkit_web_history_item_is_target_item(WebKitWebHistoryItem* webHistoryItem)
461 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
463 WebCore::HistoryItem* item = core(webHistoryItem);
465 g_return_val_if_fail(item != NULL, NULL);
467 return item->isTargetItem();
470 GList* webkit_web_history_item_get_children(WebKitWebHistoryItem* webHistoryItem)
472 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
474 WebCore::HistoryItem* item = core(webHistoryItem);
476 g_return_val_if_fail(item != NULL, NULL);
478 const WebCore::HistoryItemVector& children = item->children();
479 if (!children.size())
482 unsigned size = children.size();
484 for (unsigned i = 0; i < size; ++i)
485 kids = g_list_prepend(kids, kit(children[i].get()));
487 return g_list_reverse(kids);
490 } /* end extern "C" */
492 WebCore::HistoryItem* WebKit::core(WebKitWebHistoryItem* webHistoryItem)
494 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
496 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
498 return priv->historyItem ? priv->historyItem : 0;
501 WebKitWebHistoryItem* WebKit::kit(PassRefPtr<WebCore::HistoryItem> historyItem)
503 g_return_val_if_fail(historyItem != NULL, NULL);
505 RefPtr<WebCore::HistoryItem> item = historyItem;
507 WebKitWebHistoryItem* webHistoryItem;
508 GHashTable* table = webkit_history_items();
510 webHistoryItem = (WebKitWebHistoryItem*) g_hash_table_lookup(table, item.get());
512 if (!webHistoryItem) {
513 webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
514 WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
516 priv->historyItem = item.release().releaseRef();
517 webkit_history_item_add(webHistoryItem, priv->historyItem);
520 return webHistoryItem;