browse

browse.git
git clone git://git.lenczewski.org/browse.git
Log | Files | Refs | README | LICENSE

api.c (2724B)


      1 #include "browse.h"
      2 
      3 static char *
      4 shellcmd(char const *cmd)
      5 {
      6 	FILE *pstdout = popen(cmd, "r");
      7 	if (!pstdout) return NULL;
      8 
      9 	char *line = NULL;
     10 	size_t len;
     11 
     12 	if (getline(&line, &len, pstdout) == -1) {
     13 		pclose(pstdout);
     14 		return NULL;
     15 	}
     16 
     17 	pclose(pstdout);
     18 
     19 	(void) len;
     20 
     21 	return line;
     22 }
     23 
     24 void
     25 stopload(struct browse_client *client, union browse_keybind_arg const *arg)
     26 {
     27 	assert(client);
     28 
     29 	(void) arg;
     30 
     31 	webkit_web_view_stop_loading(client->webview);
     32 }
     33 
     34 void
     35 reload(struct browse_client *client, union browse_keybind_arg const *arg)
     36 {
     37 	assert(client);
     38 	assert(arg);
     39 
     40 	if (arg->i) {
     41 		webkit_web_view_reload_bypass_cache(client->webview);
     42 	} else {
     43 		webkit_web_view_reload(client->webview);
     44 	}
     45 }
     46 
     47 void
     48 navigate(struct browse_client *client, union browse_keybind_arg const *arg)
     49 {
     50 	assert(client);
     51 	assert(arg);
     52 
     53 	if (arg->i < 0) {
     54 		webkit_web_view_go_back(client->webview);
     55 	} else if (arg->i > 0) {
     56 		webkit_web_view_go_forward(client->webview);
     57 	}
     58 }
     59 
     60 static void
     61 clipboard_cb(GObject *src, GAsyncResult *res, void *user_data)
     62 {
     63 	(void) src;
     64 
     65 	struct browse_client *client = user_data;
     66 
     67 	char *text;
     68 	if ((text = gdk_clipboard_read_text_finish(GDK_CLIPBOARD(src), res, NULL))) {
     69 		browse_load_uri(client, text);
     70 	}
     71 }
     72 
     73 void
     74 clipboard(struct browse_client *client, union browse_keybind_arg const *arg)
     75 {
     76 	assert(client);
     77 	assert(arg);
     78 
     79 	if (arg->i) {
     80 		gdk_clipboard_read_text_async(client->clipboard, NULL, clipboard_cb, client);
     81 	} else {
     82 		gdk_clipboard_set_text(client->clipboard, webkit_web_view_get_uri(client->webview));
     83 	}
     84 }
     85 
     86 void
     87 javascript(struct browse_client *client, union browse_keybind_arg const *arg)
     88 {
     89 	assert(client);
     90 	assert(arg);
     91 
     92 	webkit_web_view_evaluate_javascript(client->webview, arg->s, -1,
     93 					    NULL, /* world */
     94 					    NULL, /* source_uri */
     95 					    NULL, /* cancellable */
     96 					    NULL, /* callback */
     97 					    NULL  /* user data */);
     98 }
     99 
    100 void
    101 search(struct browse_client *client, union browse_keybind_arg const *arg)
    102 {
    103 	assert(client);
    104 	assert(arg);
    105 
    106 	char *uri = shellcmd(arg->s);
    107 	if (!uri) return;
    108 
    109 	browse_load_uri(client, uri);
    110 
    111 	free(uri);
    112 }
    113 
    114 void
    115 download(struct browse_client *client, union browse_keybind_arg const *arg)
    116 {
    117 	assert(client);
    118 	assert(arg);
    119 
    120 	browse_download_uri(client, client->url);
    121 }
    122 
    123 void
    124 toggle(struct browse_client *client, union browse_keybind_arg const *arg)
    125 {
    126 	assert(client);
    127 	assert(arg);
    128 
    129 	union browse_prop *prop = &client->props[arg->i];
    130 
    131 	switch (arg->i) {
    132 	case BROWSE_PROP_STRICT_TLS:
    133 		prop->b = !prop->b;
    134 		webkit_network_session_set_tls_errors_policy(client->webnetsession,
    135 				prop->b ? WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
    136 		break;
    137 	}
    138 
    139 	webkit_web_view_reload(client->webview);
    140 	browse_update_title(client);
    141 }