aboutsummaryrefslogtreecommitdiff
path: root/callbacks.c
blob: 27bdaf13e935346436717c5b0a5523673041c448 (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
#include <gdk/gdkkeysyms.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <webkit/webkit.h>
#include <glib/gstdio.h>
#include "defaults.h"
#include "callbacks.h"

#include "sb.h"
/* download callback */
void cb_download(WebKitWebView *web_view, GObject *d, gpointer user_data) {
const gchar *c = webkit_download_get_uri(WEBKIT_DOWNLOAD(d));
gchar *command = g_strconcat(DEFAULT_DOWNLOAD, g_get_home_dir(), "/ ", g_strdup(c), NULL);
g_spawn_command_line_async(command, NULL);
g_free(command);
}


/* entry callback */
void cb_entry (GtkWidget* entry, gpointer data) {
struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook));
load_uri(g_strdup(gtk_entry_get_text (GTK_ENTRY (w.bar))));
gtk_widget_grab_focus(GTK_WIDGET(t->view));

if(w.hide) {
  gtk_widget_hide(w.bar);
  }
w.hide = FALSE;

}


/* go forward or backwards, simple enough */
void cb_go(gboolean b) {
struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook));
if (b) {  webkit_web_view_go_forward(t->view); }
else { webkit_web_view_go_back(t->view); }
}

/* when the page load is commited, call this function */
void cb_commit_load (WebKitWebView* page, WebKitWebFrame* frame, tab *t) {

const gchar* uri = webkit_web_frame_get_uri(frame);

if (gtk_notebook_get_current_page(w.notebook) ==  gtk_notebook_page_num(w.notebook, t->scroll)) {
gtk_entry_set_text (GTK_ENTRY (w.bar), uri);
}
FILE *history  = fopen(g_build_filename(g_get_home_dir(), DEFAULT_HISTORY_FILE, NULL), "a+");
fprintf(history, "%s \n", uri);
fclose(history);

}


/* title change callback */
void cb_title_changed(WebKitWebView *v, WebKitWebFrame *f, const char *title, tab *t) {
gchar *tabtitle;

if (gtk_notebook_get_current_page(w.notebook) ==  gtk_notebook_page_num(w.notebook, t->scroll)) {
gtk_window_set_title(GTK_WINDOW(w.win), g_strconcat(title, NULL));
}
if(strlen(title) <  DEFAULT_TAB_LENGTH ) { tabtitle = g_strdup(title); } 
 else {	 tabtitle = g_strndup(title, DEFAULT_TAB_LENGTH); strcat(tabtitle, "..."); }


gtk_label_set_label(GTK_LABEL(t->label), tabtitle);
}