2 Copyright (C) 2009-2010 ProFUSION embedded systems
3 Copyright (C) 2009-2010 Samsung Electronics
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 "ewk_settings.h"
26 #include "DatabaseTracker.h"
28 #include "IconDatabase.h"
32 #include "ewk_private.h"
35 #include <eina_safety_checks.h>
39 #include <sys/types.h>
40 #include <sys/utsname.h>
42 #include <wtf/text/CString.h>
43 #include <wtf/text/StringConcatenate.h>
46 #include "ResourceHandle.h"
47 #include <libsoup/soup.h>
50 static const char* _ewk_default_web_database_path = 0;
51 static const char* _ewk_icon_database_path = 0;
52 static uint64_t _ewk_default_web_database_quota = 1 * 1024 * 1024;
54 static WTF::String _ewk_settings_webkit_platform_get()
56 WTF::String ua_platform;
60 ua_platform = "Unknown";
65 static WTF::String _ewk_settings_webkit_os_version_get()
67 WTF::String ua_os_version;
70 if (uname(&name) != -1)
71 ua_os_version = WTF::String(name.sysname) + " " + WTF::String(name.machine);
73 ua_os_version = "Unknown";
79 * Returns the default quota for Web Database databases. By default
82 * @return the current default database quota in bytes
84 uint64_t ewk_settings_web_database_default_quota_get()
86 return _ewk_default_web_database_quota;
90 * Sets the current path to the directory WebKit will write Web
93 * @path: the new database directory path
96 void ewk_settings_web_database_path_set(const char *path)
99 WTF::String corePath = WTF::String::fromUTF8(path);
100 WebCore::DatabaseTracker::tracker().setDatabaseDirectoryPath(corePath);
101 if (!_ewk_default_web_database_path)
102 _ewk_default_web_database_path = eina_stringshare_add(corePath.utf8().data());
104 eina_stringshare_replace(&_ewk_default_web_database_path, corePath.utf8().data());
110 * Return directory path where web database is stored.
112 * @return database path or NULL if none or web database is not supported.
113 * This is guaranteed to be eina_stringshare, so whenever possible
114 * save yourself some cpu cycles and use
115 * eina_stringshare_ref() instead of eina_stringshare_add() or
118 const char *ewk_settings_web_database_path_get()
121 return _ewk_default_web_database_path;
128 * Sets directory where to store icon database, opening database.
130 * @param directory where to store icon database, must be
131 * write-able. If @c NULL is given, then database is closed.
133 * @return @c EINA_TRUE on success, @c EINA_FALSE on errors.
135 Eina_Bool ewk_settings_icon_database_path_set(const char *directory)
137 WebCore::iconDatabase().delayDatabaseCleanup();
142 if (stat(directory, &st)) {
143 ERR("could not stat(%s): %s", directory, strerror(errno));
147 if (!S_ISDIR(st.st_mode)) {
148 ERR("not a directory: %s", directory);
152 if (access(directory, R_OK | W_OK)) {
153 ERR("could not access directory '%s' for read and write: %s",
154 directory, strerror(errno));
158 WebCore::iconDatabase().setEnabled(true);
159 WebCore::iconDatabase().open(WTF::String::fromUTF8(directory));
160 if (!_ewk_icon_database_path)
161 _ewk_icon_database_path = eina_stringshare_add(directory);
163 eina_stringshare_replace(&_ewk_icon_database_path, directory);
165 WebCore::iconDatabase().setEnabled(false);
166 WebCore::iconDatabase().close();
167 if (_ewk_icon_database_path) {
168 eina_stringshare_del(_ewk_icon_database_path);
169 _ewk_icon_database_path = 0;
176 * Return directory path where icon database is stored.
178 * @return database path or @c NULL if none is set or database is closed.
179 * This is guaranteed to be eina_stringshare, so whenever possible
180 * save yourself some cpu cycles and use
181 * eina_stringshare_ref() instead of eina_stringshare_add() or
184 const char* ewk_settings_icon_database_path_get(void)
186 if (!WebCore::iconDatabase().isEnabled())
188 if (!WebCore::iconDatabase().isOpen())
191 return _ewk_icon_database_path;
195 * Remove all known icons from database.
197 * Database must be opened with ewk_settings_icon_database_path_set()
200 * @return @c EINA_TRUE on success or @c EINA_FALSE otherwise, like
203 Eina_Bool ewk_settings_icon_database_clear(void)
205 if (!WebCore::iconDatabase().isEnabled())
207 if (!WebCore::iconDatabase().isOpen())
210 WebCore::iconDatabase().removeAllIcons();
215 * Query icon for given URL, returning associated cairo surface.
217 * @note in order to have this working, one must open icon database
218 * with ewk_settings_icon_database_path_set().
220 * @param url which url to query icon.
222 * @return cairo surface if any, or NULL on failure.
224 cairo_surface_t* ewk_settings_icon_database_icon_surface_get(const char *url)
226 EINA_SAFETY_ON_NULL_RETURN_VAL(url, 0);
228 WebCore::KURL kurl(WebCore::KURL(), WTF::String::fromUTF8(url));
229 WebCore::Image *icon = WebCore::iconDatabase().iconForPageURL(kurl.string(), WebCore::IntSize(16, 16));
232 ERR("no icon for url %s", url);
236 return icon->nativeImageForCurrentFrame();
240 * Create Evas_Object of type image representing the given URL.
242 * This is an utility function that creates an Evas_Object of type
243 * image set to have fill always match object size
244 * (evas_object_image_filled_add()), saving some code to use it from Evas.
246 * @note in order to have this working, one must open icon database
247 * with ewk_settings_icon_database_path_set().
249 * @param url which url to query icon.
250 * @param canvas evas instance where to add resulting object.
252 * @return newly allocated Evas_Object instance or @c NULL on
253 * errors. Delete the object with evas_object_del().
255 Evas_Object* ewk_settings_icon_database_icon_object_add(const char* url, Evas* canvas)
257 EINA_SAFETY_ON_NULL_RETURN_VAL(url, 0);
258 EINA_SAFETY_ON_NULL_RETURN_VAL(canvas, 0);
260 WebCore::KURL kurl(WebCore::KURL(), WTF::String::fromUTF8(url));
261 WebCore::Image* icon = WebCore::iconDatabase().iconForPageURL(kurl.string(), WebCore::IntSize(16, 16));
262 cairo_surface_t* surface;
265 ERR("no icon for url %s", url);
269 surface = icon->nativeImageForCurrentFrame();
270 return ewk_util_image_from_cairo_surface_add(canvas, surface);
274 * Sets the given proxy URI to network backend.
278 void ewk_settings_proxy_uri_set(const char* proxy)
281 SoupSession* session = WebCore::ResourceHandle::defaultSession();
284 ERR("no proxy uri. remove proxy feature in soup.");
285 soup_session_remove_feature_by_type(session, SOUP_TYPE_PROXY_RESOLVER);
289 SoupURI* uri = soup_uri_new(proxy);
290 EINA_SAFETY_ON_NULL_RETURN(uri);
292 g_object_set(session, SOUP_SESSION_PROXY_URI, uri, NULL);
295 EINA_SAFETY_ON_TRUE_RETURN(1);
300 * Gets the proxy URI from the network backend.
302 * @return current proxy URI or @c 0 if it's not set.
304 const char* ewk_settings_proxy_uri_get()
308 SoupSession* session = WebCore::ResourceHandle::defaultSession();
309 g_object_get(session, SOUP_SESSION_PROXY_URI, &uri, NULL);
316 WTF::String proxy = soup_uri_to_string(uri, EINA_FALSE);
317 return eina_stringshare_add(proxy.utf8().data());
319 EINA_SAFETY_ON_TRUE_RETURN_VAL(1, NULL);
325 * Gets the default user agent string.
327 * @return A pointer to an eina_stringshare containing the user agent string.
329 const char* ewk_settings_default_user_agent_get()
331 WTF::String ua_version = makeString(String::number(WEBKIT_USER_AGENT_MAJOR_VERSION), '.', String::number(WEBKIT_USER_AGENT_MINOR_VERSION), '+');
332 WTF::String static_ua = makeString("Mozilla/5.0 (", _ewk_settings_webkit_platform_get(), "; ", _ewk_settings_webkit_os_version_get(), ") AppleWebKit/", ua_version) + makeString(" (KHTML, like Gecko) Version/5.0 Safari/", ua_version);
334 return eina_stringshare_add(static_ua.utf8().data());