aboutsummaryrefslogtreecommitdiff
path: root/rose.c
blob: 75f90f7c35eb0983760eb29cb60ba07a7888d65a (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include <stdbool.h>
#include <stdlib.h> // necessary for free, malloc.
#include <string.h>
#include <webkit2/webkit2.h>

#include "config.h"

#include "plugins/libre_redirect/libre_redirect.h"
#include "plugins/readability/readability.h"
#include "plugins/style/style.h"

// #include "plugins/stand_in/stand_in.h"

int LIBRE_REDIRECT_ENABLED = true;
int READABILITY_ENABLED = true;
int CUSTOM_STYLE_ENABLED = true;
int CUSTOM_USER_AGENT = true;

// to enable plugins,
// 1. Enable them:
//   - uncomment their #include statement
//   - set their variable to true
//   - in build.sh, uncomment: REQS= #./plugins/*/*.c
// 2. Remove stand_in code;
//   - Add an #include "plugins/stand_in/stand_in.h" line, and modify
//   stand_in.c so as to include stand-in functions for the excluded plugin
//   - In the make file, include the stand_in.c file and exclude the
//   relevant plugin

#define CACHE                                                                \
    "base-cache-directory", CACHE_DIR, "base-data-directory", CACHE_DIR,     \
        "disk-cache-directory", CACHE_DIR, "dom-cache-directory", CACHE_DIR, \
        "hsts-cache-directory", CACHE_DIR, "indexeddb-directory", CACHE_DIR, \
        "itp-directory", CACHE_DIR, "local-storage-directory", CACHE_DIR,    \
        "offline-application-cache-directory", CACHE_DIR,                    \
        "service-worker-registrations-directory", CACHE_DIR

enum { _SEARCH,
    _FIND,
    _HIDDEN };

static int entry_mode;
static GtkWindow* window;
static GtkHeaderBar* bar;
static GtkEntryBuffer* search_buf;
static GtkEntry* search;

WebKitWebView* webview_new()
{
    char* style;
    WebKitSettings* settings;
    WebKitWebContext* web_context;
    WebKitCookieManager* cookiemanager;
    WebKitUserContentManager* contentmanager;

    settings = webkit_settings_new_with_settings(WEBKIT, NULL);
    if (CUSTOM_USER_AGENT) {
        webkit_settings_set_user_agent(
            settings,
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, "
            "like Gecko) Chrome/110.0.0.0 Safari/537.36");
        // See: <https://www.useragents.me/> for some common user agents
    }
    web_context = webkit_web_context_new_with_website_data_manager(
        webkit_website_data_manager_new(CACHE, NULL));
    contentmanager = webkit_user_content_manager_new();
    cookiemanager = webkit_web_context_get_cookie_manager(web_context);

    webkit_cookie_manager_set_persistent_storage(
        cookiemanager, CACHE_DIR "/cookies.sqlite",
        WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);

    webkit_cookie_manager_set_accept_policy(cookiemanager,
        WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);

    webkit_web_context_set_process_model(
        web_context, WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);

    if (g_file_get_contents("~/.config/rose/style.css", &style, NULL, NULL))
        webkit_user_content_manager_add_style_sheet(
            contentmanager, webkit_user_style_sheet_new(style, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL));

    return g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "web-context",
        web_context, "user-content-manager", contentmanager,
        NULL);
}

WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
{
    return WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page(
        notebook, gtk_notebook_get_current_page(notebook)));
}

void load_uri(WebKitWebView* view, const char* uri)
{
    if (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:")) {
        webkit_web_view_load_uri(view, uri);
    } else {
        // webkit_web_view_load_uri(view, uri);
        char tmp[strlen(uri) + strlen(SEARCH)];
        snprintf(tmp, sizeof(tmp), SEARCH, uri);
        webkit_web_view_load_uri(view, tmp);
    }
}

void redirect_if_annoying(WebKitWebView* view, const char* uri)
{
    int l = LIBRE_N + strlen(uri) + 1;
    char uri_filtered[l];
    str_init(uri_filtered, l);

    int check = libre_redirect(uri, uri_filtered);

    if (check == 2) {
        webkit_web_view_load_uri(view, uri_filtered);
    }
}

void load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
    GtkNotebook* notebook)
{
    switch (load_event) {
        /* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html>
     */
    case WEBKIT_LOAD_STARTED:
        if (CUSTOM_STYLE_ENABLED) {
            char* style_js = malloc(STYLE_N + 1);
            read_style_js(style_js);
            webkit_web_view_run_javascript(notebook_get_webview(notebook), style_js,
                NULL, NULL, NULL);
            free(style_js);
        }
        if (LIBRE_REDIRECT_ENABLED) {
            redirect_if_annoying(self, webkit_web_view_get_uri(self));
        }
        break;
    case WEBKIT_LOAD_REDIRECTED:
        if (LIBRE_REDIRECT_ENABLED) {
            redirect_if_annoying(self, webkit_web_view_get_uri(self));
        }
        break;
    case WEBKIT_LOAD_COMMITTED:
        if (LIBRE_REDIRECT_ENABLED) {
            redirect_if_annoying(self, webkit_web_view_get_uri(self));
        }
        if (CUSTOM_STYLE_ENABLED) {
            char* style_js = malloc(STYLE_N + 1);
            read_style_js(style_js);
            webkit_web_view_run_javascript(notebook_get_webview(notebook), style_js,
                NULL, NULL, NULL);
            free(style_js);
        }
        break;
    case WEBKIT_LOAD_FINISHED: {
        /* Add gtk tab title */
        const char* webpage_title = webkit_web_view_get_title(self);
        const int max_length = 25;
        char tab_title[max_length + 1];
        if (webpage_title != NULL) {
            for (int i = 0; i < (max_length); i++) {
                tab_title[i] = webpage_title[i];
                if (webpage_title[i] == '\0') {
                    break;
                }
            }
            tab_title[max_length] = '\0';
        }

        gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self),
            webpage_title == NULL ? "—" : tab_title);
        // gtk_widget_hide(GTK_WIDGET(bar));
    }
    }
}

void notebook_append(GtkNotebook* notebook, const char* uri);
/* notebook_append calls handle_create, but  handle_create also calls
 * notebook_append. Therefore we need to declare notebook_append, so that
 * handle_create_new_tab knows its type.
 */

GtkWidget* handle_create_new_tab(WebKitWebView* self,
    WebKitNavigationAction* navigation_action,
    GtkNotebook* notebook)
{
    WebKitURIRequest* uri_request = webkit_navigation_action_get_request(navigation_action);
    const char* uri = webkit_uri_request_get_uri(uri_request);
    printf("Creating new window: %s\n", uri);
    notebook_append(notebook, uri);
    gtk_notebook_set_show_tabs(notebook, true);
    return NULL;
    /* WebKitGTK documentation recommends returning the new webview.
   * I imagine that this might allow e.g., to go back in a new tab
   * or generally to keep track of history.
   * However, this would require either modifying notebook_append
   * or duplicating its contents, for unclear gain.
   */
}

void notebook_append(GtkNotebook* notebook, const char* uri)
{
    GdkScreen* screen = gtk_window_get_screen(GTK_WINDOW(window));
    GdkVisual* rgba_visual = gdk_screen_get_rgba_visual(screen);
    GdkRGBA rgba;

    gdk_rgba_parse(&rgba, BG_COLOR);

    WebKitWebView* view = webview_new();

    gtk_widget_set_visual(GTK_WIDGET(window), rgba_visual);
    g_signal_connect(view, "load_changed", G_CALLBACK(load_changed), notebook);
    g_signal_connect(view, "create", G_CALLBACK(handle_create_new_tab), notebook);

    int n = gtk_notebook_append_page(notebook, GTK_WIDGET(view), NULL);
    gtk_notebook_set_tab_reorderable(notebook, GTK_WIDGET(view), true);
    gtk_widget_show_all(GTK_WIDGET(window));
    gtk_widget_hide(GTK_WIDGET(bar));
    webkit_web_view_set_background_color(view, &rgba);
    load_uri(view, (uri) ? uri : HOME);
    gtk_notebook_set_current_page(notebook, n);
    gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(view), "-");
    webkit_web_view_set_zoom_level(view, ZOOM);
}

void show_bar(GtkNotebook* notebook)
{
    if (entry_mode == _SEARCH) {
        const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook));
        gtk_entry_set_placeholder_text(search, "Search");
        gtk_entry_buffer_set_text(search_buf, url, strlen(url));
        gtk_widget_show(GTK_WIDGET(bar));
        gtk_window_set_focus(window, GTK_WIDGET(search));
    } else if (entry_mode == _HIDDEN) {
        gtk_widget_hide(GTK_WIDGET(bar));
    } else {
        const char* search_text = webkit_find_controller_get_search_text(
            webkit_web_view_get_find_controller(notebook_get_webview(notebook)));

        if (search_text != NULL)
            gtk_entry_buffer_set_text(