OSDN Git Service

Merge "Partially revert change from 96777"
[android-x86/external-webkit.git] / Source / WebKit / gtk / webkit / webkitwebhistoryitem.cpp
1 /*
2  * Copyright (C) 2008, 2009 Jan Michael C. Alonzo
3  * Copyright (C) 2009 Igalia S.L.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22 #include "webkitwebhistoryitem.h"
23
24 #include "HistoryItem.h"
25 #include "PlatformString.h"
26 #include "webkitglobalsprivate.h"
27 #include "webkitwebhistoryitemprivate.h"
28 #include <glib.h>
29 #include <glib/gi18n-lib.h>
30 #include <wtf/text/CString.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.  #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.
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 struct _WebKitWebHistoryItemPrivate {
54     WebCore::HistoryItem* historyItem;
55
56     WTF::CString title;
57     WTF::CString alternateTitle;
58     WTF::CString uri;
59     WTF::CString originalUri;
60
61     gboolean disposed;
62 };
63
64 enum {
65     PROP_0,
66
67     PROP_TITLE,
68     PROP_ALTERNATE_TITLE,
69     PROP_URI,
70     PROP_ORIGINAL_URI,
71     PROP_LAST_VISITED_TIME
72 };
73
74 G_DEFINE_TYPE(WebKitWebHistoryItem, webkit_web_history_item, G_TYPE_OBJECT);
75
76 static void webkit_web_history_item_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
77
78 static void webkit_web_history_item_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec);
79
80 GHashTable* webkit_history_items()
81 {
82     static GHashTable* historyItems = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_object_unref);
83     return historyItems;
84 }
85
86 void webkit_history_item_add(WebKitWebHistoryItem* webHistoryItem, WebCore::HistoryItem* historyItem)
87 {
88     g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
89
90     GHashTable* table = webkit_history_items();
91     g_hash_table_insert(table, historyItem, webHistoryItem);
92 }
93
94 static void webkit_web_history_item_dispose(GObject* object)
95 {
96     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
97     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
98
99     if (!priv->disposed) {
100         WebCore::HistoryItem* item = core(webHistoryItem);
101         item->deref();
102         priv->disposed = true;
103     }
104
105     G_OBJECT_CLASS(webkit_web_history_item_parent_class)->dispose(object);
106 }
107
108 static void webkit_web_history_item_finalize(GObject* object)
109 {
110     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
111     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
112
113     priv->title = WTF::CString();
114     priv->alternateTitle = WTF::CString();
115     priv->uri = WTF::CString();
116     priv->originalUri = WTF::CString();
117
118     G_OBJECT_CLASS(webkit_web_history_item_parent_class)->finalize(object);
119 }
120
121 static void webkit_web_history_item_class_init(WebKitWebHistoryItemClass* klass)
122 {
123     GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
124
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;
129
130     webkitInit();
131
132     /**
133     * WebKitWebHistoryItem:title:
134     *
135     * The title of the history item.
136     *
137     * Since: 1.0.2
138     */
139     g_object_class_install_property(gobject_class,
140                                     PROP_TITLE,
141                                     g_param_spec_string(
142                                     "title",
143                                     _("Title"),
144                                     _("The title of the history item"),
145                                     NULL,
146                                     WEBKIT_PARAM_READABLE));
147
148     /**
149     * WebKitWebHistoryItem:alternate-title:
150     *
151     * The alternate title of the history item.
152     *
153     * Since: 1.0.2
154     */
155     g_object_class_install_property(gobject_class,
156                                     PROP_ALTERNATE_TITLE,
157                                     g_param_spec_string(
158                                     "alternate-title",
159                                     _("Alternate Title"),
160                                     _("The alternate title of the history item"),
161                                     NULL,
162                                     WEBKIT_PARAM_READWRITE));
163
164     /**
165     * WebKitWebHistoryItem:uri:
166     *
167     * The URI of the history item.
168     *
169     * Since: 1.0.2
170     */
171     g_object_class_install_property(gobject_class,
172                                     PROP_URI,
173                                     g_param_spec_string(
174                                     "uri",
175                                     _("URI"),
176                                     _("The URI of the history item"),
177                                     NULL,
178                                     WEBKIT_PARAM_READABLE));
179
180     /**
181     * WebKitWebHistoryItem:original-uri:
182     *
183     * The original URI of the history item.
184     *
185     * Since: 1.0.2
186     */
187     g_object_class_install_property(gobject_class,
188                                     PROP_ORIGINAL_URI,
189                                     g_param_spec_string(
190                                     "original-uri",
191                                     _("Original URI"),
192                                     _("The original URI of the history item"),
193                                     NULL,
194                                     WEBKIT_PARAM_READABLE));
195
196    /**
197     * WebKitWebHistoryItem:last-visited-time:
198     *
199     * The time at which the history item was last visited.
200     *
201     * Since: 1.0.2
202     */
203     g_object_class_install_property(gobject_class,
204                                     PROP_LAST_VISITED_TIME,
205                                     g_param_spec_double(
206                                     "last-visited-time",
207                                     _("Last visited Time"),
208                                     _("The time at which the history item was last visited"),
209                                     0, G_MAXDOUBLE, 0,
210                                     WEBKIT_PARAM_READABLE));
211
212     g_type_class_add_private(gobject_class, sizeof(WebKitWebHistoryItemPrivate));
213 }
214
215 static void webkit_web_history_item_init(WebKitWebHistoryItem* webHistoryItem)
216 {
217     webHistoryItem->priv = G_TYPE_INSTANCE_GET_PRIVATE(webHistoryItem, WEBKIT_TYPE_WEB_HISTORY_ITEM, WebKitWebHistoryItemPrivate);
218 }
219
220 static void webkit_web_history_item_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
221 {
222     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
223
224     switch(prop_id) {
225     case PROP_ALTERNATE_TITLE:
226         webkit_web_history_item_set_alternate_title(webHistoryItem, g_value_get_string(value));
227         break;
228     default:
229         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
230         break;
231     }
232 }
233
234 static void webkit_web_history_item_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
235 {
236     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(object);
237
238     switch (prop_id) {
239     case PROP_TITLE:
240         g_value_set_string(value, webkit_web_history_item_get_title(webHistoryItem));
241         break;
242     case PROP_ALTERNATE_TITLE:
243         g_value_set_string(value, webkit_web_history_item_get_alternate_title(webHistoryItem));
244         break;
245     case PROP_URI:
246         g_value_set_string(value, webkit_web_history_item_get_uri(webHistoryItem));
247         break;
248     case PROP_ORIGINAL_URI:
249         g_value_set_string(value, webkit_web_history_item_get_original_uri(webHistoryItem));
250         break;
251     case PROP_LAST_VISITED_TIME:
252         g_value_set_double(value, webkit_web_history_item_get_last_visited_time(webHistoryItem));
253         break;
254     default:
255         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
256         break;
257     }
258 }
259
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)
262 {
263     return kit(historyItem);
264 }
265
266
267 /**
268  * webkit_web_history_item_new:
269  *
270  * Creates a new #WebKitWebHistoryItem instance
271  *
272  * Return value: the new #WebKitWebHistoryItem
273  */
274 WebKitWebHistoryItem* webkit_web_history_item_new()
275 {
276     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
277     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
278
279     RefPtr<WebCore::HistoryItem> item = WebCore::HistoryItem::create();
280     priv->historyItem = item.release().releaseRef();
281     webkit_history_item_add(webHistoryItem, priv->historyItem);
282
283     return webHistoryItem;
284 }
285
286 /**
287  * webkit_web_history_item_new_with_data:
288  * @uri: the uri of the page
289  * @title: the title of the page
290  *
291  * Creates a new #WebKitWebHistoryItem with the given URI and title
292  *
293  * Return value: the new #WebKitWebHistoryItem
294  */
295 WebKitWebHistoryItem* webkit_web_history_item_new_with_data(const gchar* uri, const gchar* title)
296 {
297     WebKitWebHistoryItem* webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
298     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
299
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);
305
306     return webHistoryItem;
307 }
308
309 /**
310  * webkit_web_history_item_get_title:
311  * @web_history_item: a #WebKitWebHistoryItem
312  *
313  * Returns: the page title of @web_history_item
314  */
315 G_CONST_RETURN gchar* webkit_web_history_item_get_title(WebKitWebHistoryItem* webHistoryItem)
316 {
317     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
318
319     WebCore::HistoryItem* item = core(webHistoryItem);
320
321     g_return_val_if_fail(item, NULL);
322
323     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
324     priv->title = item->title().utf8();
325
326     return priv->title.data();
327 }
328
329 /**
330  * webkit_web_history_item_get_alternate_title:
331  * @web_history_item: a #WebKitWebHistoryItem
332  *
333  * Returns the alternate title of @web_history_item
334  *
335  * Return value: the alternate title of @web_history_item
336  */
337 G_CONST_RETURN gchar* webkit_web_history_item_get_alternate_title(WebKitWebHistoryItem* webHistoryItem)
338 {
339     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
340
341     WebCore::HistoryItem* item = core(webHistoryItem);
342
343     g_return_val_if_fail(item, NULL);
344
345     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
346     priv->alternateTitle = item->alternateTitle().utf8();
347
348     return priv->alternateTitle.data();
349 }
350
351 /**
352  * webkit_web_history_item_set_alternate_title:
353  * @web_history_item: a #WebKitWebHistoryItem
354  * @title: the alternate title for @this history item
355  *
356  * Sets an alternate title for @web_history_item
357  */
358 void webkit_web_history_item_set_alternate_title(WebKitWebHistoryItem* webHistoryItem, const gchar* title)
359 {
360     g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
361     g_return_if_fail(title);
362
363     WebCore::HistoryItem* item = core(webHistoryItem);
364
365     item->setAlternateTitle(WTF::String::fromUTF8(title));
366     g_object_notify(G_OBJECT(webHistoryItem), "alternate-title");
367 }
368
369 /**
370  * webkit_web_history_item_get_uri:
371  * @web_history_item: a #WebKitWebHistoryItem
372  *
373  * Returns the URI of @this
374  *
375  * Return value: the URI of @web_history_item
376  */
377 G_CONST_RETURN gchar* webkit_web_history_item_get_uri(WebKitWebHistoryItem* webHistoryItem)
378 {
379     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
380
381     WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
382
383     g_return_val_if_fail(item, NULL);
384
385     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
386     priv->uri = item->urlString().utf8();
387
388     return priv->uri.data();
389 }
390
391 /**
392  * webkit_web_history_item_get_original_uri:
393  * @web_history_item: a #WebKitWebHistoryItem
394  *
395  * Returns the original URI of @web_history_item.
396  *
397  * Return value: the original URI of @web_history_item
398  */
399 G_CONST_RETURN gchar* webkit_web_history_item_get_original_uri(WebKitWebHistoryItem* webHistoryItem)
400 {
401     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
402
403     WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
404
405     g_return_val_if_fail(item, NULL);
406
407     WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
408     priv->originalUri = item->originalURLString().utf8();
409
410     return webHistoryItem->priv->originalUri.data();
411 }
412
413 /**
414  * webkit_web_history_item_get_last_visisted_time :
415  * @web_history_item: a #WebKitWebHistoryItem
416  *
417  * Returns the last time @web_history_item was visited
418  *
419  * Return value: the time in seconds this @web_history_item was last visited
420  */
421 gdouble webkit_web_history_item_get_last_visited_time(WebKitWebHistoryItem* webHistoryItem)
422 {
423     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), 0);
424
425     WebCore::HistoryItem* item = core(WEBKIT_WEB_HISTORY_ITEM(webHistoryItem));
426
427     g_return_val_if_fail(item, 0);
428
429     return item->lastVisitedTime();
430 }
431
432 /**
433  * webkit_web_history_item_copy:
434  * @web_history_item: a #WebKitWebHistoryItem
435  *
436  * Makes a copy of the item for use with other WebView objects.
437  *
438  * Since: 1.1.18
439  *
440  * Return value: (transfer full): the new #WebKitWebHistoryItem.
441  */
442 WebKitWebHistoryItem* webkit_web_history_item_copy(WebKitWebHistoryItem* self)
443 {
444     WebKitWebHistoryItemPrivate* selfPrivate = self->priv;
445
446     WebKitWebHistoryItem* item = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, 0));
447     WebKitWebHistoryItemPrivate* priv = item->priv;
448
449     priv->title = selfPrivate->title;
450     priv->alternateTitle = selfPrivate->alternateTitle;
451     priv->uri = selfPrivate->uri;
452     priv->originalUri = selfPrivate->originalUri;
453
454     priv->historyItem = selfPrivate->historyItem->copy().releaseRef();
455
456     return item;
457 }
458
459 /* private methods */
460
461 G_CONST_RETURN gchar* webkit_web_history_item_get_target(WebKitWebHistoryItem* webHistoryItem)
462 {
463     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
464
465     WebCore::HistoryItem* item = core(webHistoryItem);
466
467     g_return_val_if_fail(item, NULL);
468
469     WTF::CString t = item->target().utf8();
470     return g_strdup(t.data());
471 }
472
473 gboolean webkit_web_history_item_is_target_item(WebKitWebHistoryItem* webHistoryItem)
474 {
475     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), false);
476
477     WebCore::HistoryItem* item = core(webHistoryItem);
478
479     g_return_val_if_fail(item, false);
480
481     return item->isTargetItem();
482 }
483
484 GList* webkit_web_history_item_get_children(WebKitWebHistoryItem* webHistoryItem)
485 {
486     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
487
488     WebCore::HistoryItem* item = core(webHistoryItem);
489
490     g_return_val_if_fail(item, NULL);
491
492     const WebCore::HistoryItemVector& children = item->children();
493     if (!children.size())
494         return NULL;
495
496     unsigned size = children.size();
497     GList* kids = NULL;
498     for (unsigned i = 0; i < size; ++i)
499         kids = g_list_prepend(kids, kit(children[i].get()));
500
501     return g_list_reverse(kids);
502 }
503
504 WebCore::HistoryItem* WebKit::core(WebKitWebHistoryItem* webHistoryItem)
505 {
506     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), NULL);
507
508     return webHistoryItem->priv->historyItem;
509 }
510
511 WebKitWebHistoryItem* WebKit::kit(PassRefPtr<WebCore::HistoryItem> historyItem)
512 {
513     g_return_val_if_fail(historyItem, NULL);
514
515     RefPtr<WebCore::HistoryItem> item = historyItem;
516     GHashTable* table = webkit_history_items();
517     WebKitWebHistoryItem* webHistoryItem = (WebKitWebHistoryItem*) g_hash_table_lookup(table, item.get());
518
519     if (!webHistoryItem) {
520         webHistoryItem = WEBKIT_WEB_HISTORY_ITEM(g_object_new(WEBKIT_TYPE_WEB_HISTORY_ITEM, NULL));
521         WebKitWebHistoryItemPrivate* priv = webHistoryItem->priv;
522
523         priv->historyItem = item.release().releaseRef();
524         webkit_history_item_add(webHistoryItem, priv->historyItem);
525     }
526
527     return webHistoryItem;
528 }
529