aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ephy-request-about.c
blob: 5cd55b9f10d5c6a82b46b404ebf6f75dd3a83e10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * ephy-request-about.c: about: URI request object
 *
 * Copyright (C) 2011, Igalia S.L.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gio/gio.h>
#include <glib/gi18n.h>
#include <libsoup/soup-uri.h>
#include <webkit/webkit.h>

#include "ephy-file-helpers.h"
#include "ephy-request-about.h"

G_DEFINE_TYPE (EphyRequestAbout, ephy_request_about, SOUP_TYPE_REQUEST)

struct _EphyRequestAboutPrivate {
    gssize content_length;
    gchar *css_style;
};

static void
ephy_request_about_init (EphyRequestAbout *about)
{
    about->priv = G_TYPE_INSTANCE_GET_PRIVATE (about, EPHY_TYPE_REQUEST_ABOUT, EphyRequestAboutPrivate);
    about->priv->content_length = 0;
    about->priv->css_style = NULL;
}

static void
ephy_request_about_finalize (GObject *obj)
{
    g_free (EPHY_REQUEST_ABOUT(obj)->priv->css_style);

    G_OBJECT_CLASS (ephy_request_about_parent_class)->finalize (obj);
}

static gboolean
ephy_request_about_check_uri (SoupRequest  *request,
                  SoupURI      *uri,
                  GError      **error)
{
    return uri->host == NULL;
}

static void
read_css_style (EphyRequestAbout *about)
{
    GError *error = NULL;

    if (!g_file_get_contents (ephy_file("about.css"), &(about->priv->css_style), NULL, &error))
        g_debug (error->message);
}

static GInputStream *
ephy_request_about_send (SoupRequest          *request,
             GCancellable         *cancellable,
             GError              **error)
{
    EphyRequestAbout *about = EPHY_REQUEST_ABOUT (request);
    SoupURI *uri = soup_request_get_uri (request);
    GString *data_str = g_string_new("<html>");

    if (!about->priv->css_style)
        read_css_style (about);

    g_string_append_printf (data_str, "<head><title>%s</title>" \
                "<style type=\"text/css\">%s</style></head><body>",
                _("Installed plugins"),
                about->priv->css_style);

    if (!g_strcmp0 (uri->path, "plugins")) {
        WebKitWebPluginDatabase* database = webkit_get_web_plugin_database ();
        GSList *plugin_list, *p;

        g_string_append_printf(data_str, "<h1>%s</h1>", _("Installed plugins"));
        plugin_list = webkit_web_plugin_database_get_plugins (database);
        for (p = plugin_list; p; p = p->next) {
            WebKitWebPlugin *plugin = WEBKIT_WEB_PLUGIN (p->data);
            GSList *m, *mime_types;

            g_string_append_printf(data_str, "<h2>%s</h2>%s<br>%s: <b>%s</b>"\
                           "<table id=\"plugin-table\">" \
                           "<thead><tr><th>%s</th><th>%s</th><th>%s</th></tr></thead><tbody>",
                           webkit_web_plugin_get_name (plugin),
                           webkit_web_plugin_get_description (plugin),
                           _("Enabled"), webkit_web_plugin_get_enabled (plugin) ? _("Yes") : _("No"),
                           _("MIME type"), _("Description"), _("Suffixes"));

            mime_types = webkit_web_plugin_get_mimetypes (plugin);
            for (m = mime_types; m; m = m->next) {
                WebKitWebPluginMIMEType *mime_type = (WebKitWebPluginMIMEType*) m->data;
                guint i;

                g_string_append_printf (data_str, "<tr><td>%s</td><td>%s</td><td>",
                            mime_type->name, mime_type->description);

                for (i = 0; mime_type->extensions[i] != NULL; i++)
                    g_string_append_printf (data_str, "%s%c", mime_type->extensions[i],
                                mime_type->extensions[i + 1] ? ',' : ' ');

                g_string_append(data_str, "</td></tr>");
            }
            g_string_append(data_str, "</tbody></table>");
        }
        webkit_web_plugin_database_plugins_list_free (plugin_list);
    }

    g_string_append(data_str, "</body></html>");
    about->priv->content_length = data_str->len;
    return g_memory_input_stream_new_from_data (g_string_free(data_str, false), about->priv->content_length, g_free);
}

static goffset
ephy_request_about_get_content_length (SoupRequest *request)
{
    return  EPHY_REQUEST_ABOUT (request)->priv->content_length;
}

static const char *
ephy_request_about_get_content_type (SoupRequest *request)
{
    return "text/html";
}

static const char *about_schemes[] = { EPHY_ABOUT_SCHEME, NULL };

static void
ephy_request_about_class_init (EphyRequestAboutClass *request_about_class)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (request_about_class);
    SoupRequestClass *request_class = SOUP_REQUEST_CLASS (request_about_class);

    gobject_class->finalize = ephy_request_about_finalize;

    request_class->schemes = about_schemes;
    request_class->check_uri = ephy_request_about_check_uri;
    request_class->send = ephy_request_about_send;
    request_class->get_content_length = ephy_request_about_get_content_length;
    request_class->get_content_type = ephy_request_about_get_content_type;

    g_type_class_add_private (request_about_class, sizeof (EphyRequestAboutPrivate));
}