1/*
2    Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
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#include "config.h"
21#include "QtWebIconDatabaseClient.h"
22
23#include "QtWebContext.h"
24#include <QtCore/QHash>
25#include <QtCore/QObject>
26#include <QtCore/QUrl>
27#include <QtGui/QImage>
28#include <WKContext.h>
29#include <WKContextPrivate.h>
30#include <WKIconDatabaseQt.h>
31#include <WKRetainPtr.h>
32#include <WKStringQt.h>
33#include <WKURLQt.h>
34
35namespace WebKit {
36
37static unsigned s_updateId = 0;
38
39static inline QtWebIconDatabaseClient* toQtWebIconDatabaseClient(const void* clientInfo)
40{
41    ASSERT(clientInfo);
42    return reinterpret_cast<QtWebIconDatabaseClient*>(const_cast<void*>(clientInfo));
43}
44
45QtWebIconDatabaseClient::QtWebIconDatabaseClient(WKContextRef context)
46{
47    m_iconDatabase = WKContextGetIconDatabase(context);
48
49    WKIconDatabaseClient iconDatabaseClient;
50    memset(&iconDatabaseClient, 0, sizeof(WKIconDatabaseClient));
51    iconDatabaseClient.version = kWKIconDatabaseClientCurrentVersion;
52    iconDatabaseClient.clientInfo = this;
53    iconDatabaseClient.didChangeIconForPageURL = didChangeIconForPageURL;
54    WKIconDatabaseSetIconDatabaseClient(m_iconDatabase, &iconDatabaseClient);
55    // Triggers the startup of the icon database.
56    WKRetainPtr<WKStringRef> path = adoptWK(WKStringCreateWithQString(QtWebContext::preparedStoragePath(QtWebContext::IconDatabaseStorage)));
57    WKContextSetIconDatabasePath(context, path.get());
58}
59
60QtWebIconDatabaseClient::~QtWebIconDatabaseClient()
61{
62    WKIconDatabaseClose(m_iconDatabase);
63    WKIconDatabaseSetIconDatabaseClient(m_iconDatabase, 0);
64}
65
66unsigned QtWebIconDatabaseClient::updateID()
67{
68    return s_updateId;
69}
70
71void QtWebIconDatabaseClient::didChangeIconForPageURL(WKIconDatabaseRef, WKURLRef pageURL, const void* clientInfo)
72{
73    ++s_updateId;
74    emit toQtWebIconDatabaseClient(clientInfo)->iconChangedForPageURL(WKURLCopyQString(pageURL));
75}
76
77QImage QtWebIconDatabaseClient::iconImageForPageURL(const QString& pageURL)
78{
79    return WKIconDatabaseTryGetQImageForURL(m_iconDatabase, adoptWK(WKURLCreateWithQString(pageURL)).get());
80}
81
82void QtWebIconDatabaseClient::retainIconForPageURL(const QString& pageURL)
83{
84    WKIconDatabaseRetainIconForURL(m_iconDatabase, adoptWK(WKURLCreateWithQString(pageURL)).get());
85}
86
87void QtWebIconDatabaseClient::releaseIconForPageURL(const QString& pageURL)
88{
89    WKIconDatabaseReleaseIconForURL(m_iconDatabase, adoptWK(WKURLCreateWithQString(pageURL)).get());
90}
91
92} // namespace WebKit
93
94#include "moc_QtWebIconDatabaseClient.cpp"
95
96