diff options
author | Xan Lopez <xlopez@igalia.com> | 2011-09-04 00:21:38 +0800 |
---|---|---|
committer | Xan Lopez <xlopez@igalia.com> | 2011-09-04 00:21:38 +0800 |
commit | 4f6da94c96d77a296e185c0fcf35ca74ddeee3a2 (patch) | |
tree | 147b536d62e8bfb92988380aef475f5a9f216487 /embed/ephy-web-app-utils.c | |
parent | 20b38fa45d7b9f3e4c302d0cb0090f72ae56bb78 (diff) | |
download | gsoc2013-epiphany-4f6da94c96d77a296e185c0fcf35ca74ddeee3a2.tar gsoc2013-epiphany-4f6da94c96d77a296e185c0fcf35ca74ddeee3a2.tar.gz gsoc2013-epiphany-4f6da94c96d77a296e185c0fcf35ca74ddeee3a2.tar.bz2 gsoc2013-epiphany-4f6da94c96d77a296e185c0fcf35ca74ddeee3a2.tar.lz gsoc2013-epiphany-4f6da94c96d77a296e185c0fcf35ca74ddeee3a2.tar.xz gsoc2013-epiphany-4f6da94c96d77a296e185c0fcf35ca74ddeee3a2.tar.zst gsoc2013-epiphany-4f6da94c96d77a296e185c0fcf35ca74ddeee3a2.zip |
ephy-web-app-utils: move the web app creation code here
Diffstat (limited to 'embed/ephy-web-app-utils.c')
-rw-r--r-- | embed/ephy-web-app-utils.c | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/embed/ephy-web-app-utils.c b/embed/ephy-web-app-utils.c index d499552f7..8a427c0ef 100644 --- a/embed/ephy-web-app-utils.c +++ b/embed/ephy-web-app-utils.c @@ -23,9 +23,13 @@ #include "ephy-web-app-utils.h" +#include "ephy-debug.h" #include "ephy-file-helpers.h" #include "ephy-web-view.h" +#include <glib/gstdio.h> +#include <libsoup/soup-gnome.h> + static gboolean _g_directory_delete_recursively (GFile *directory, GError **error) { @@ -152,3 +156,177 @@ out: return return_value; } + +#define EPHY_WEB_APP_TOOLBAR "<?xml version=\"1.0\"?>" \ + "<toolbars version=\"1.1\">" \ + " <toolbar name=\"DefaultToolbar\" hidden=\"true\" editable=\"false\">" \ + " <toolitem name=\"NavigationBack\"/>" \ + " <toolitem name=\"NavigationForward\"/>" \ + " <toolitem name=\"ViewReload\"/>" \ + " <toolitem name=\"ViewCancel\"/>" \ + " </toolbar>" \ + "</toolbars>" + +#define EPHY_TOOLBARS_XML_FILE "epiphany-toolbars-3.xml" + +static char * +create_desktop_file (EphyWebView *view, + const char *profile_dir, + const char *title, + GdkPixbuf *icon) +{ + GKeyFile *file; + char *exec_string; + char *data; + char *filename, *desktop_file_path; + char *link_path; + GFile *link; + + g_return_val_if_fail (profile_dir, NULL); + + file = g_key_file_new (); + g_key_file_set_value (file, "Desktop Entry", "Name", title); + exec_string = g_strdup_printf ("epiphany --application-mode --profile=\"%s\" %s", + profile_dir, + webkit_web_view_get_uri (WEBKIT_WEB_VIEW (view))); + g_key_file_set_value (file, "Desktop Entry", "Exec", exec_string); + g_free (exec_string); + g_key_file_set_value (file, "Desktop Entry", "StartupNotification", "true"); + g_key_file_set_value (file, "Desktop Entry", "Terminal", "false"); + g_key_file_set_value (file, "Desktop Entry", "Type", "Application"); + + if (icon) { + GOutputStream *stream; + char *path; + GFile *image; + + path = g_build_filename (profile_dir, "app-icon.png", NULL); + image = g_file_new_for_path (path); + + stream = (GOutputStream*)g_file_create (image, 0, NULL, NULL); + gdk_pixbuf_save_to_stream (icon, stream, "png", NULL, NULL, NULL); + g_key_file_set_value (file, "Desktop Entry", "Icon", path); + + g_object_unref (stream); + g_object_unref (image); + g_free (path); + } + + g_key_file_set_value (file, "Desktop Entry", "StartupWMClass", title); + + data = g_key_file_to_data (file, NULL, NULL); + filename = g_strconcat (title, ".desktop", NULL); + desktop_file_path = g_build_filename (profile_dir, filename, NULL); + g_key_file_free (file); + + if (!g_file_set_contents (desktop_file_path, data, -1, NULL)) { + g_free (desktop_file_path); + desktop_file_path = NULL; + } + + g_free (data); + + /* Create a symlink in XDG_DATA_DIR/applications for the Shell to + * pick up this application. */ + link_path = g_build_filename (g_get_user_data_dir (), "applications", filename, NULL); + link = g_file_new_for_path (link_path); + g_free (link_path); + g_file_make_symbolic_link (link, desktop_file_path, NULL, NULL); + g_object_unref (link); + g_free (filename); + + return desktop_file_path; +} + +static void +create_cookie_jar_for_domain (EphyWebView *view, const char *directory) +{ + SoupSession *session; + GSList *cookies, *p; + SoupCookieJar *current_jar, *new_jar; + char *domain, *filename; + SoupURI *uri; + + /* Create the new cookie jar */ + filename = g_build_filename (directory, "cookies.sqlite", NULL); + new_jar = (SoupCookieJar*)soup_cookie_jar_sqlite_new (filename, FALSE); + g_free (filename); + + /* The app domain for the current view */ + uri = soup_uri_new (webkit_web_view_get_uri (WEBKIT_WEB_VIEW (view))); + domain = uri->host; + + /* The current cookies */ + session = webkit_get_default_session (); + current_jar = (SoupCookieJar*)soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR); + cookies = soup_cookie_jar_all_cookies (current_jar); + + for (p = cookies; p; p = p->next) { + SoupCookie *cookie = (SoupCookie*)p->data; + + if (g_str_has_suffix (cookie->domain, domain)) + soup_cookie_jar_add_cookie (new_jar, cookie); + else + soup_cookie_free (cookie); + } + + soup_uri_free (uri); + g_slist_free (cookies); +} + +/** + * ephy_web_application_create: + * @view: an #EphyWebView + * @title: the title for the new web application + * @icon: the icon for the new web application + * + * Creates a new Web Application from the currently loaded URI in the @view. + * + * Returns: (transfer-full): the path to the desktop file representing the new application + **/ +char * +ephy_web_application_create (EphyWebView *view, const char *title, GdkPixbuf *icon) +{ + char *profile_dir = NULL; + char *toolbar_path = NULL; + char *desktop_file_path = NULL; + + g_return_val_if_fail (EPHY_IS_WEB_VIEW (view), NULL); + + /* If there's already a WebApp profile for the contents of this + * view, do nothing. TODO: create a method to check this and use it + * to ask the user if she wants to overwrite the existing WebApp. */ + profile_dir = ephy_web_application_get_profile_directory (title); + if (g_file_test (profile_dir, G_FILE_TEST_IS_DIR)) + goto out; + + /* Create the profile directory, populate it. */ + if (g_mkdir (profile_dir, 488) == -1) { + LOG ("Failed to create directory %s", profile_dir); + goto out; + } + + /* Things we need in a WebApp's profile: + - Toolbar layout + - Our own cookies file, copying the relevant cookies for the + app's domain. + */ + toolbar_path = g_build_filename (profile_dir, EPHY_TOOLBARS_XML_FILE, NULL); + if (!g_file_set_contents (toolbar_path, EPHY_WEB_APP_TOOLBAR, -1, NULL)) + goto out; + + create_cookie_jar_for_domain (view, profile_dir); + + /* Create the deskop file. */ + desktop_file_path = create_desktop_file (view, profile_dir, title, icon); + +out: + if (toolbar_path) + g_free (toolbar_path); + + if (profile_dir) + g_free (profile_dir); + + return desktop_file_path; +} + |