summaryrefslogtreecommitdiff
path: root/src/html.c
blob: 21ee8ad3be9faa7f64ba103e6d2fa1282c343d5c (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
465
466
467
468
469
470
471
472
/*
 * Copyright (c) 2010-2012 Stefan Bolte <portix@gmx.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <string.h>
#include "dwb.h"
#include "html.h"
#include "util.h"

#define HTML_REMOVE_BUTTON "<div style='float:right;cursor:pointer;' navigation='%s %s' onclick='location.reload();'>&times</div>"
typedef struct _HtmlTable HtmlTable;

struct _HtmlTable {
  const char *uri;
  const char *title;
  const char *file;
  int type;
  DwbStatus (*func)(GList *, HtmlTable *);
};

static gboolean html_key_changed(WebKitDOMElement *target);
void html_settings_changed(WebKitDOMElement *el);

DwbStatus html_bookmarks(GList *, HtmlTable *);
DwbStatus html_history(GList *, HtmlTable *);
DwbStatus html_searchengines(GList *gl, HtmlTable *table);
DwbStatus html_quickmarks(GList *, HtmlTable *);
DwbStatus html_downloads(GList *, HtmlTable *);
DwbStatus html_settings(GList *, HtmlTable *);
DwbStatus html_startpage(GList *, HtmlTable *);
DwbStatus html_keys(GList *, HtmlTable *);


static HtmlTable table[] = {
  { "dwb:bookmarks",        "Bookmarks",      INFO_FILE,      0, html_bookmarks,  },
  { "dwb:quickmarks",       "Quickmarks",     INFO_FILE,      0, html_quickmarks, },
  { "dwb:history",          "History",        INFO_FILE,      0, html_history,    },
  { "dwb:searchengines",    "Searchengines",  INFO_FILE,      0, html_searchengines,    },
  { "dwb:downloads",        "Downloads",      INFO_FILE,      0, html_downloads, },
  { "dwb:keys",             "Keys",           INFO_FILE,      0, html_keys },
  { "dwb:settings",         "Settings",       INFO_FILE,      0, html_settings },
  { "dwb:startpage",         NULL,            NULL,           0, html_startpage },
};

static char current_uri[BUFFER_LENGTH];
DwbStatus
html_load_page(WebKitWebView *wv, HtmlTable *t, char *panel) {
  char *filecontent;
  GString *content = g_string_new(NULL);
  char *path = util_get_data_file(t->file, "lib");
  char *headpath = util_get_data_file(HEAD_FILE, "lib");
  DwbStatus ret = STATUS_ERROR;

  if (path && headpath) {
    /* load head */
    g_file_get_contents(headpath, &filecontent, NULL, NULL);
    g_string_append_printf(content, filecontent, t->title);
    g_free(filecontent);
    /* load content */
    g_file_get_contents(path, &filecontent, NULL, NULL);
    if (panel) 
      g_string_append_printf(content, filecontent, panel);
    webkit_web_frame_load_alternate_string(webkit_web_view_get_main_frame(wv), content->str, current_uri, current_uri);
    g_string_free(content, true);
    g_free(filecontent);
    ret = STATUS_OK;
  }
  g_free(headpath);
  g_free(path);
  return ret;
}

gboolean
html_remove_item_cb(WebKitDOMElement *el, WebKitDOMEvent *ev, GList *gl) {
  WebKitDOMEventTarget *target = webkit_dom_event_get_target(ev);
  if (webkit_dom_element_has_attribute((void*)target, "navigation")) {
    char *navigation = webkit_dom_element_get_attribute(WEBKIT_DOM_ELEMENT(target), "navigation");
    const char *uri = webkit_web_view_get_uri(WEBVIEW(gl));
    if (!g_strcmp0(uri, "dwb:history")) {
      dwb_remove_history(navigation);
    }
    else if (!g_strcmp0(uri, "dwb:bookmarks")) {
      dwb_remove_bookmark(navigation);
    }
    else if (!g_strcmp0(uri, "dwb:quickmarks")) {
      dwb_remove_quickmark(navigation);
    }
    else if (!g_strcmp0(uri, "dwb:downloads")) {
      dwb_remove_download(navigation);
    }
    else if (!g_strcmp0(uri, "dwb:searchengines")) {
      dwb_remove_search_engine(navigation);
    }
  }
  else if (webkit_dom_element_has_attribute((void*) target, "href")) {
    char *href = webkit_dom_element_get_attribute((void*) target, "href");
    dwb_load_uri(gl, href);
    webkit_dom_event_prevent_default(ev);
    return true;
  }
  return false;
}
void
html_load_status_cb(WebKitWebView *web, GParamSpec *p, GList *gl) {
  WebKitLoadStatus s = webkit_web_view_get_load_status(web);
  if (s == WEBKIT_LOAD_FINISHED) {
    WebKitDOMDocument *doc = webkit_web_view_get_dom_document(web);
    WebKitDOMHTMLElement *body = webkit_dom_document_get_body(doc);
    webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(body), "click", G_CALLBACK(html_remove_item_cb), true, gl);
    g_signal_handlers_disconnect_by_func(web, html_load_status_cb, gl);
  }
  else if (s == WEBKIT_LOAD_FAILED) {
    g_signal_handlers_disconnect_by_func(web, html_load_status_cb, gl);
  }

}
DwbStatus
html_navigation(GList *gl, GList *data, HtmlTable *table) {
  int i=0;
  WebKitWebView *wv = WEBVIEW(gl);
  DwbStatus ret;

  GString *panels = g_string_new(NULL);
  for (GList *l = data; l; l=l->next, i++, i%=2) {
    Navigation *n = l->data;
    g_string_append_printf(panels, "\n\
        <tr class='dwb_table_row'>\n\
          <td class=dwb_table_cell_left>\n\
            <a href='%s'>\n\
              %s\n\
            </a>\n\
          </td>\n\
          <td class='dwb_table_cell_middle'>\n\
            <div class='dwb_ellipsize'>\n\
              <a style='color:inherit;font:inherit;' href='%s'>\n\
                %s\n\
              </a>\n\
            </div>\n\
          </td>\n\
          <td class='dwb_table_cell_right' style='cursor:pointer;' navigation='%s %s' onclick='location.reload()'>\n\
            &times\n\
          </td>\n\
        </tr>\n",  n->first, n->second && g_strcmp0(n->second, "(null)") ? n->second : n->first, n->first, n->first, n->first, n->second);
  }
  if ( (ret = html_load_page(wv, table, panels->str)) == STATUS_OK) 
    g_signal_connect(wv, "notify::load-status", G_CALLBACK(html_load_status_cb), gl); 
  g_string_free(panels, true);
  return ret;
}
DwbStatus
html_bookmarks(GList *gl, HtmlTable *table) {
  dwb.fc.bookmarks = g_list_sort(dwb.fc.bookmarks, (GCompareFunc)util_navigation_compare_second);
  return html_navigation(gl, dwb.fc.bookmarks, table);
}
DwbStatus
html_searchengines(GList *gl, HtmlTable *table) {
  WebKitWebView *wv = WEBVIEW(gl);
  DwbStatus ret;

  GString *panels = g_string_new(NULL);
  int i=0;
  for (GList *l = dwb.fc.se_completion; l; l=l->next, i++, i%=2) {
    Navigation *n = l->data;
    g_string_append_printf(panels, "\n\
        <tr class='dwb_table_row'>\n\
          <td class='dwb_table_cell_left'>\n\
            <a href='%s'>\n\
              %s\n\
            </a>\n\
          </td>\n\
          <td class='dwb_table_cell_middle'>\n\
            <div class='dwb_qm'>\n\
              %s\n\
            </div>\n\
          </td>\
          <td class='dwb_table_cell_right' style='cursor:pointer;' navigation='%s %s' onclick='location.reload()'>&times</td>\n\
        </tr>\n", n->second, n->second, n->first, n->first, n->second);
  }
  if ( (ret = html_load_page(wv, table, panels->str)) == STATUS_OK) 
    g_signal_connect(wv, "notify::load-status", G_CALLBACK(html_load_status_cb), gl); 
  g_string_free(panels, true);
  return ret;
}
DwbStatus
html_history(GList *gl, HtmlTable *table) {
  return html_navigation(gl, dwb.fc.history, table);
}
void
html_settings_changed(WebKitDOMElement *el) {
  char *id = webkit_dom_html_element_get_id(WEBKIT_DOM_HTML_ELEMENT(el));
  char *value = NULL;
  char *type;
  if (WEBKIT_DOM_IS_HTML_INPUT_ELEMENT(el)) {
    type = webkit_dom_element_get_attribute(el, "type");
    if (!g_strcmp0(type, "checkbox")) {
      /* We need to dup "true" and "false", otherwise g_strstrip in
       * util_char_to_arg will cause a segfault */
      if (webkit_dom_html_input_element_get_checked(WEBKIT_DOM_HTML_INPUT_ELEMENT(el)) ) 
        value = g_strdup("true");
      else 
        value = g_strdup("false");
    }
    else  
      value = webkit_dom_html_input_element_get_value(WEBKIT_DOM_HTML_INPUT_ELEMENT(el));
  }
  else if (WEBKIT_DOM_IS_HTML_SELECT_ELEMENT(el)) {
    value = webkit_dom_html_select_element_get_value(WEBKIT_DOM_HTML_SELECT_ELEMENT(el));
  }
  dwb_set_setting(id, value, SETTING_GLOBAL);
  webkit_dom_element_blur(el);
}

gboolean
html_settings_changed_cb(WebKitDOMElement *el, WebKitDOMEvent *ev, WebKitWebView *wv) {
  WebKitDOMEventTarget *target = webkit_dom_event_get_target(ev);
  html_settings_changed(WEBKIT_DOM_ELEMENT(target));
  return true;
}
gboolean
html_settings_really_changed_cb(WebKitDOMElement *el, WebKitDOMEvent *ev, WebKitWebView *wv) {
  webkit_dom_element_blur(el);
  return true;
}
gboolean
html_keydown_settings_cb(WebKitDOMElement *el, WebKitDOMEvent *ev, WebKitWebView *wv) {
  glong val = webkit_dom_ui_event_get_key_code(WEBKIT_DOM_UI_EVENT(ev));
  if (val == 13) {
    WebKitDOMEventTarget *target = webkit_dom_event_get_target(ev);
    if (target != NULL) 
      html_settings_changed(WEBKIT_DOM_ELEMENT(target));
    dwb_change_mode(NORMAL_MODE, false);
    return true;
  }
  return false;
}
gboolean
html_keydown_cb(WebKitDOMElement *el, WebKitDOMEvent *ev, WebKitWebView *wv) {
  glong val = webkit_dom_ui_event_get_key_code(WEBKIT_DOM_UI_EVENT(ev));
  if (val == 13) {
    WebKitDOMEventTarget *target = webkit_dom_event_get_target(ev);
    if (target != NULL) {
      return html_key_changed(WEBKIT_DOM_ELEMENT(target));
    }
  }
  return false;
}
void
html_settings_fill(char *key, WebSettings *s, WebKitWebView *wv) {
  char *value = util_arg_to_char(&s->arg, s->type);
  WebKitDOMDocument *doc = webkit_web_view_get_dom_document(wv);
  //WebKitDOMDOMWindow *win = webkit_dom_document_get_default_view(doc);
  WebKitDOMElement *e = webkit_dom_document_get_element_by_id(doc, key);
  if (s->type == BOOLEAN) {
    webkit_dom_html_input_element_set_checked(WEBKIT_DOM_HTML_INPUT_ELEMENT(e), s->arg.b);
  }
  else if (WEBKIT_DOM_IS_HTML_INPUT_ELEMENT(e)) {
      webkit_dom_html_input_element_set_value(WEBKIT_DOM_HTML_INPUT_ELEMENT(e), value ? value : "");
  }
  else if (WEBKIT_DOM_IS_HTML_SELECT_ELEMENT(e)) {
    char *lower = g_utf8_strdown(value, -1);
    webkit_dom_html_select_element_set_value(WEBKIT_DOM_HTML_SELECT_ELEMENT(e), lower);
    g_free(lower);
  }
  g_free(value);
}
void 
html_settings_load_cb(WebKitWebView *wv, GParamSpec *p, HtmlTable *table) {
  WebKitLoadStatus status = webkit_web_view_get_load_status(wv);
  if (status == WEBKIT_LOAD_FINISHED) {
    WebKitDOMDocument *doc = webkit_web_view_get_dom_document(wv);
    WebKitDOMDOMWindow *win = webkit_dom_document_get_default_view(doc);
    webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(win), "keydown", G_CALLBACK(html_keydown_settings_cb), true, wv);
    webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(win), "change", G_CALLBACK(html_settings_changed_cb), true, wv);
    g_hash_table_foreach(dwb.settings, (GHFunc)html_settings_fill, wv);
    g_signal_handlers_disconnect_by_func(wv, html_settings_load_cb, table);
  }
}
DwbStatus
html_settings(GList *gl, HtmlTable *table) {
  char *content = NULL;
  DwbStatus ret = STATUS_ERROR;
  WebKitWebView *wv = WEBVIEW(gl);

  char *path = util_get_data_file(SETTINGS_FILE, "lib");
  if  (path != NULL) {
    g_file_get_contents(path, &content, NULL, NULL);
    ret = html_load_page(wv, table, content);
    g_free(path);
    g_signal_connect(wv, "notify::load-status", G_CALLBACK(html_settings_load_cb), table);
  }
  g_free(content);
  return ret;
}

static gboolean
html_custom_keys_changed_cb(WebKitDOMElement *target, WebKitDOMEvent *e, gpointer data) {
  WebKitDOMDocument *doc = webkit_dom_node_get_owner_document(WEBKIT_DOM_NODE(target));
  WebKitDOMElement *text_area = webkit_dom_document_get_element_by_id(doc, "dwb_custom_keys_area");
  char *value = webkit_dom_html_text_area_element_get_value(WEBKIT_DOM_HTML_TEXT_AREA_ELEMENT(text_area));
  util_set_file_content(dwb.files.custom_keys, value);
  g_free(value);
  dwb_init_custom_keys(true);
  dwb_set_normal_message(dwb.state.fview, true, "Custom keys saved");
  dwb_change_mode(NORMAL_MODE, false);
  return true;
}
static gboolean 
html_key_changed(WebKitDOMElement *target) {
  g_return_val_if_fail(target != NULL, false);
  char *value;
  if (WEBKIT_DOM_IS_HTML_TEXT_AREA_ELEMENT(target)) 
    value = webkit_dom_html_text_area_element_get_value(WEBKIT_DOM_HTML_TEXT_AREA_ELEMENT(target));
  else 
    value = webkit_dom_html_input_element_get_value(WEBKIT_DOM_HTML_INPUT_ELEMENT(target));

  char *id = webkit_dom_html_element_get_id(WEBKIT_DOM_HTML_ELEMENT(target));
  if (g_strcmp0(id, "dwb_custom_keys_area")) {
    dwb_set_key(id, value);
    webkit_dom_element_blur(target);
    return true;
  }
  return false;
}
static gboolean
html_changed_cb(WebKitDOMElement *input, WebKitDOMEvent *e, gpointer data) {
  WebKitDOMEventTarget *target = webkit_dom_event_get_target(e);
  return html_key_changed(WEBKIT_DOM_ELEMENT(target));
}
void 
html_keys_load_cb(WebKitWebView *wv, GParamSpec *p, HtmlTable *table) {
  KeyMap *km;
  Navigation n;
  WebKitDOMElement *input;
  char *mod, *value;

  if (webkit_web_view_get_load_status(wv) == WEBKIT_LOAD_FINISHED) {
    WebKitDOMDocument *doc = webkit_web_view_get_dom_document(wv);
    WebKitDOMDOMWindow *win = webkit_dom_document_get_default_view(doc);
    for (GList *l = dwb.keymap; l; l=l->next) {
      km = l->data; 
      n = km->map->n;
      input = webkit_dom_document_get_element_by_id(doc, n.first);
      if (input != NULL) {
        mod = dwb_modmask_to_string(km->mod);
        value = g_strdup_printf("%s%s%s", mod, strlen(mod) > 0 ? " " : "", km->key ? km->key : "");
        webkit_dom_html_input_element_set_value(WEBKIT_DOM_HTML_INPUT_ELEMENT(input), value);
        g_free(mod);
        g_free(value);
      }
    }
    webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(win), "change", G_CALLBACK(html_changed_cb), true, wv);
    webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(win), "keydown", G_CALLBACK(html_keydown_cb), true, wv);
    WebKitDOMElement *textarea = webkit_dom_document_get_element_by_id(doc, "dwb_custom_keys_area");
    WebKitDOMElement *button = webkit_dom_document_get_element_by_id(doc, "dwb_custom_keys_submit");
    char *content = util_get_file_content(dwb.files.custom_keys);
    if (content != NULL && *content != '\0') {
      webkit_dom_html_text_area_element_set_value(WEBKIT_DOM_HTML_TEXT_AREA_ELEMENT(textarea), content);
    }
    webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(button), "click", G_CALLBACK(html_custom_keys_changed_cb), false, wv);
    FREE0(content);
    g_signal_handlers_disconnect_by_func(wv, html_keys_load_cb, table);
  }
}
DwbStatus
html_keys(GList *gl, HtmlTable *table) {
  DwbStatus ret = STATUS_ERROR;
  char *content = NULL;
  WebKitWebView *wv = WEBVIEW(gl);
  char *path = util_get_data_file(KEY_FILE, "lib");
  if (path != NULL) {
    g_signal_connect(wv, "notify::load-status", G_CALLBACK(html_keys_load_cb), table);
    g_file_get_contents(path, &content, NULL, NULL);
    ret = html_load_page(wv, table, content);
    g_free(path);
  }
  g_free(content);
  return ret;
}
DwbStatus
html_quickmarks(GList *gl, HtmlTable *table) {
  DwbStatus ret;
  WebKitWebView *wv = WEBVIEW(gl);
  GString *panels = g_string_new(NULL);

  for (GList *gl = dwb.fc.quickmarks; gl; gl=gl->next) {
    Quickmark *q = gl->data;
    g_string_append_printf(panels, "\n\
        <tr class='dwb_table_row'>\n\
          <td class='dwb_table_cell_left'>\n\
            <div>\n\
              <div class='dwb_qm'>%s</div>\n\
              <a href='%s'>\n\
                %s\n\
              </a>\n\
            </div>\n\
          </td>\n\
          <td class='dwb_table_cell_middle'>\n\
            <div class='dwb_ellipsize'>\n\
              <a style='color:inherit;font:inherit;' href='%s'>\n\
                %s\n\
              </a>\n\
            </div>\n\
          </td>\n\
          <td class='dwb_table_cell_right' style='cursor:pointer;' navigation='%s %s %s' onclick='location.reload()'>&times</td>\n\
        </tr>", q->key, q->nav->first, q->nav->second && g_strcmp0(q->nav->second, "(null)") ? q->nav->second : q->nav->first,
        q->nav->first, q->nav->first, q->key, q->nav->first, q->nav->second);
  }
  if ( (ret = html_load_page(wv, table, panels->str)) == STATUS_OK) 
    g_signal_connect(wv, "notify::load-status", G_CALLBACK(html_load_status_cb), gl); 
  g_string_free(panels, true);
  return ret;
}
DwbStatus
html_downloads(GList *gl, HtmlTable *table) {
  DwbStatus ret;
  WebKitWebView *wv = WEBVIEW(gl);
  GString *panels = g_string_new(NULL);
  for (GList *gl = dwb.fc.downloads; gl; gl=gl->next) {
    Navigation *n = gl->data;
    g_string_append_printf(panels, "\n\
        <tr class='dwb_table_row'>\n\
          <td class='dwb_table_cell_left'>\n\
            <a href='%s'>\n\
              %s\n\
            </a>\n\
          </td>\
          <td class='dwb_table_cell_middle'>\n\
            <a href='%s'>\n\
              restart\n\
            </a>\n\
          </td>\
          <td class='dwb_table_cell_right' style='cursor:pointer;' navigation='%s %s' onclick='location.reload()'>&times</td>\n\
        </tr>", n->second, n->second, n->first, n->first, n->second);
  }
  if ( (ret = html_load_page(wv, table, panels->str)) == STATUS_OK) 
    g_signal_connect(wv, "notify::load-status", G_CALLBACK(html_load_status_cb), gl); 
  g_string_free(panels, true);
  return ret;
}
DwbStatus
html_startpage(GList *gl, HtmlTable *table) {
  return dwb_open_startpage(gl);
}

gboolean 
html_load(GList *gl, const char *uri) {
  gboolean ret = false;
  for (int i=0; i<LENGTH(table); i++) {
    if (!strncmp(table[i].uri, uri, strlen(table[i].uri))) {
      g_strlcpy(current_uri, uri, BUFFER_LENGTH - 1);
      if (table[i].func(gl, &table[i]) == STATUS_OK)  {
        ret = true;
        break;
      }
    }
  }
  return ret;
}