3 (c) 2009 by Leon Winter
4 (c) 2009-2011 by Hannes Schueller
5 (c) 2009-2010 by Matto Fransen
6 (c) 2010-2011 by Hans-Peter Deifel
7 (c) 2010-2011 by Thomas Adam
14 #include "vimprobable.h"
15 #include "utilities.h"
16 #include "callbacks.h"
17 #include "javascript.h"
19 /* the CLEAN_MOD_*_MASK defines have all the bits set that will be stripped from the modifier bit field */
20 #define CLEAN_MOD_NUMLOCK_MASK (GDK_MOD2_MASK)
21 #define CLEAN_MOD_BUTTON_MASK (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK|GDK_BUTTON3_MASK|GDK_BUTTON4_MASK|GDK_BUTTON5_MASK)
23 /* remove unused bits, numlock symbol and buttons from keymask */
24 #define CLEAN(mask) (mask & (GDK_MODIFIER_MASK) & ~(CLEAN_MOD_NUMLOCK_MASK) & ~(CLEAN_MOD_BUTTON_MASK))
26 #define IS_ESCAPE(event) (IS_ESCAPE_KEY(CLEAN(event->state), event->keyval))
27 #define IS_ESCAPE_KEY(s, k) ((s == 0 && k == GDK_Escape) || \
28 (s == GDK_CONTROL_MASK && k == GDK_bracketleft))
31 static void inputbox_activate_cb(GtkEntry
*entry
, gpointer user_data
);
32 static gboolean
inputbox_keypress_cb(GtkEntry
*entry
, GdkEventKey
*event
);
33 static gboolean
inputbox_keyrelease_cb(GtkEntry
*entry
, GdkEventKey
*event
);
34 static gboolean
inputbox_changed_cb(GtkEditable
*entry
, gpointer user_data
);
35 static WebKitWebView
* inspector_inspect_web_view_cb(gpointer inspector
, WebKitWebView
* web_view
);
36 static gboolean
notify_event_cb(GtkWidget
*widget
, GdkEvent
*event
, gpointer user_data
);
37 static gboolean
webview_console_cb(WebKitWebView
*webview
, char *message
, int line
, char *source
, gpointer user_data
);
38 static gboolean
webview_download_cb(WebKitWebView
*webview
, WebKitDownload
*download
, gpointer user_data
);
39 static void webview_hoverlink_cb(WebKitWebView
*webview
, char *title
, char *link
, gpointer data
);
40 static gboolean
webview_keypress_cb(WebKitWebView
*webview
, GdkEventKey
*event
);
41 static void webview_load_committed_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, gpointer user_data
);
42 static void webview_load_finished_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, gpointer user_data
);
43 static gboolean
webview_mimetype_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, WebKitNetworkRequest
*request
,
44 char *mime_type
, WebKitWebPolicyDecision
*decision
, gpointer user_data
);
45 static gboolean
webview_new_window_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, WebKitNetworkRequest
*request
,
46 WebKitWebNavigationAction
*action
, WebKitWebPolicyDecision
*decision
, gpointer user_data
);
47 static gboolean
webview_open_in_new_window_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, gpointer user_data
);
48 static void webview_progress_changed_cb(WebKitWebView
*webview
, int progress
, gpointer user_data
);
49 static void webview_title_changed_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, char *title
, gpointer user_data
);
50 static void window_destroyed_cb(GtkWidget
*window
, gpointer func_data
);
53 static gboolean
bookmark(const Arg
*arg
);
54 static gboolean
browser_settings(const Arg
*arg
);
55 static gboolean
commandhistoryfetch(const Arg
*arg
);
56 static gboolean
complete(const Arg
*arg
);
57 static gboolean
descend(const Arg
*arg
);
58 gboolean
echo(const Arg
*arg
);
59 static gboolean
focus_input(const Arg
*arg
);
60 static gboolean
input(const Arg
*arg
);
61 static gboolean
navigate(const Arg
*arg
);
62 static gboolean
number(const Arg
*arg
);
63 static gboolean
open_arg(const Arg
*arg
);
64 static gboolean
open_remembered(const Arg
*arg
);
65 static gboolean
paste(const Arg
*arg
);
66 static gboolean
quickmark(const Arg
*arg
);
67 static gboolean
quit(const Arg
*arg
);
68 static gboolean
revive(const Arg
*arg
);
69 static gboolean
print_frame(const Arg
*arg
);
70 static gboolean
search(const Arg
*arg
);
71 static gboolean
set(const Arg
*arg
);
72 static gboolean
script(const Arg
*arg
);
73 static gboolean
scroll(const Arg
*arg
);
74 static gboolean
search_tag(const Arg
*arg
);
75 static gboolean
yank(const Arg
*arg
);
76 static gboolean
view_source(const Arg
* arg
);
77 static gboolean
zoom(const Arg
*arg
);
78 static gboolean
fake_key_event(const Arg
*arg
);
80 static void update_url(const char *uri
);
81 static void setup_modkeys(void);
82 static void setup_gui(void);
83 static void setup_settings(void);
84 static void setup_signals(void);
85 static void ascii_bar(int total
, int state
, char *string
);
86 static gchar
*jsapi_ref_to_string(JSContextRef context
, JSValueRef ref
);
87 static void jsapi_evaluate_script(const gchar
*script
, gchar
**value
, gchar
**message
);
88 static void download_progress(WebKitDownload
*d
, GParamSpec
*pspec
);
89 static void set_widget_font_and_color(GtkWidget
*widget
, const char *font_str
,
90 const char *bg_color_str
, const char *fg_color_str
);
92 static gboolean
history(void);
93 static gboolean
process_set_line(char *line
);
94 void save_command_history(char *line
);
95 void toggle_proxy(gboolean onoff
);
96 void toggle_scrollbars(gboolean onoff
);
98 gboolean
process_keypress(GdkEventKey
*event
);
99 void fill_suggline(char * suggline
, const char * command
, const char *fill_with
);
100 GtkWidget
* fill_eventbox(const char * completion_line
);
101 static void mop_up(void);
106 static GtkWindow
*window
;
107 static GtkWidget
*viewport
;
109 static GtkScrollbar
*scroll_h
;
110 static GtkScrollbar
*scroll_v
;
111 static GtkAdjustment
*adjust_h
;
112 static GtkAdjustment
*adjust_v
;
113 static GtkWidget
*inputbox
;
114 static GtkWidget
*eventbox
;
115 static GtkBox
*statusbar
;
116 static GtkWidget
*status_url
;
117 static GtkWidget
*status_state
;
118 static WebKitWebView
*webview
;
119 static SoupSession
*session
;
120 static GtkClipboard
*clipboards
[2];
123 static unsigned int mode
= ModeNormal
;
124 static unsigned int count
= 0;
125 static float zoomstep
;
126 static char *modkeys
;
127 static char current_modkey
;
128 static char *search_handle
;
129 static gboolean search_direction
;
130 static gboolean echo_active
= TRUE
;
131 WebKitWebInspector
*inspector
;
133 static GdkNativeWindow embed
= 0;
134 static char *configfile
= NULL
;
135 static char *winid
= NULL
;
137 static char rememberedURI
[1024] = "";
138 static char followTarget
[8] = "";
139 char *error_msg
= NULL
;
141 GList
*activeDownloads
;
146 char commandhistory
[COMMANDHISTSIZE
][255];
149 int commandpointer
= 0;
150 KeyList
*keylistroot
= NULL
;
152 /* Cookie support. */
153 #ifdef ENABLE_COOKIE_SUPPORT
154 static SoupCookieJar
*session_cookie_jar
= NULL
;
155 static SoupCookieJar
*file_cookie_jar
= NULL
;
156 static time_t cookie_timeout
= 4800;
157 static char *cookie_store
;
158 static void setup_cookies(void);
159 static const char *get_cookies(SoupURI
*soup_uri
);
160 static void load_all_cookies(void);
161 static void new_generic_request(SoupSession
*soup_ses
, SoupMessage
*soup_msg
, gpointer unused
);
162 static void update_cookie_jar(SoupCookieJar
*jar
, SoupCookie
*old
, SoupCookie
*new);
163 static void handle_cookie_request(SoupMessage
*soup_msg
, gpointer unused
);
167 window_destroyed_cb(GtkWidget
*window
, gpointer func_data
) {
172 webview_title_changed_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, char *title
, gpointer user_data
) {
173 gtk_window_set_title(window
, title
);
177 webview_progress_changed_cb(WebKitWebView
*webview
, int progress
, gpointer user_data
) {
178 #ifdef ENABLE_GTK_PROGRESS_BAR
179 gtk_entry_set_progress_fraction(GTK_ENTRY(inputbox
), progress
== 100 ? 0 : (double)progress
/100);
184 #ifdef ENABLE_WGET_PROGRESS_BAR
186 ascii_bar(int total
, int state
, char *string
) {
189 for (i
= 0; i
< state
; i
++)
190 string
[i
] = progressbartickchar
;
191 string
[i
++] = progressbarcurrent
;
192 for (; i
< total
; i
++)
193 string
[i
] = progressbarspacer
;
199 webview_load_committed_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, gpointer user_data
) {
200 Arg a
= { .i
= Silent
, .s
= g_strdup(JS_SETUP_HINTS
) };
201 const char *uri
= webkit_web_view_get_uri(webview
);
208 webview_load_finished_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, gpointer user_data
) {
209 Arg a
= { .i
= Silent
, .s
= g_strdup(JS_SETUP_INPUT_FOCUS
) };
211 if (HISTORY_MAX_ENTRIES
> 0)
218 webview_open_in_new_window_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, gpointer user_data
) {
219 Arg a
= { .i
= TargetNew
, .s
= (char*)webkit_web_view_get_uri(webview
) };
220 if (strlen(rememberedURI
) > 0) {
228 webview_new_window_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, WebKitNetworkRequest
*request
,
229 WebKitWebNavigationAction
*action
, WebKitWebPolicyDecision
*decision
, gpointer user_data
) {
230 Arg a
= { .i
= TargetNew
, .s
= (char*)webkit_network_request_get_uri(request
) };
232 webkit_web_policy_decision_ignore(decision
);
237 webview_mimetype_cb(WebKitWebView
*webview
, WebKitWebFrame
*frame
, WebKitNetworkRequest
*request
,
238 char *mime_type
, WebKitWebPolicyDecision
*decision
, gpointer user_data
) {
239 if (webkit_web_view_can_show_mime_type(webview
, mime_type
) == FALSE
) {
240 webkit_web_policy_decision_download(decision
);
247 static WebKitWebView
*
248 inspector_inspect_web_view_cb(gpointer inspector
, WebKitWebView
* web_view
) {
249 gchar
* inspector_title
;
250 GtkWidget
* inspector_window
;
251 GtkWidget
* inspector_view
;
253 /* just enough code to show the inspector - no signal handling etc. */
254 inspector_title
= g_strdup_printf("Inspect page - %s - Vimprobable2", webkit_web_view_get_uri(web_view
));
256 inspector_window
= gtk_plug_new(embed
);
258 inspector_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
259 gtk_window_set_wmclass(window
, "vimprobable2", "Vimprobable2");
261 gtk_window_set_title(GTK_WINDOW(inspector_window
), inspector_title
);
262 g_free(inspector_title
);
263 inspector_view
= webkit_web_view_new();
264 gtk_container_add(GTK_CONTAINER(inspector_window
), inspector_view
);
265 gtk_widget_show_all(inspector_window
);
266 return WEBKIT_WEB_VIEW(inspector_view
);
270 webview_download_cb(WebKitWebView
*webview
, WebKitDownload
*download
, gpointer user_data
) {
271 const gchar
*filename
;
276 filename
= webkit_download_get_suggested_filename(download
);
277 if (filename
== NULL
|| strlen(filename
) == 0) {
278 filename
= "vimprobable_download";
280 path
= g_build_filename(g_strdup_printf(DOWNLOADS_PATH
), filename
, NULL
);
281 uri
= g_strconcat("file://", path
, NULL
);
282 webkit_download_set_destination_uri(download
, uri
);
284 size
= (uint32_t)webkit_download_get_total_size(download
);
287 a
.s
= g_strdup_printf("Download %s started (expected size: %u bytes)...", filename
, size
);
289 a
.s
= g_strdup_printf("Download %s started (unknown size)...", filename
);
292 activeDownloads
= g_list_prepend(activeDownloads
, download
);
293 g_signal_connect(download
, "notify::progress", G_CALLBACK(download_progress
), NULL
);
294 g_signal_connect(download
, "notify::status", G_CALLBACK(download_progress
), NULL
);
300 download_progress(WebKitDownload
*d
, GParamSpec
*pspec
) {
302 WebKitDownloadStatus status
= webkit_download_get_status(d
);
304 if (status
!= WEBKIT_DOWNLOAD_STATUS_STARTED
&& status
!= WEBKIT_DOWNLOAD_STATUS_CREATED
) {
305 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
) {
307 a
.s
= g_strdup_printf("Error while downloading %s", webkit_download_get_suggested_filename(d
));
311 a
.s
= g_strdup_printf("Download %s finished", webkit_download_get_suggested_filename(d
));
315 activeDownloads
= g_list_remove(activeDownloads
, d
);
322 process_keypress(GdkEventKey
*event
) {
325 current
= keylistroot
;
326 while (current
!= NULL
) {
327 if (current
->Element
.mask
== CLEAN(event
->state
)
328 && (current
->Element
.modkey
== current_modkey
329 || (!current
->Element
.modkey
&& !current_modkey
)
330 || current
->Element
.modkey
== GDK_VoidSymbol
) /* wildcard */
331 && current
->Element
.key
== event
->keyval
332 && current
->Element
.func
)
333 if (current
->Element
.func(¤t
->Element
.arg
)) {
334 current_modkey
= count
= 0;
338 current
= current
->next
;
344 webview_keypress_cb(WebKitWebView
*webview
, GdkEventKey
*event
) {
345 Arg a
= { .i
= ModeNormal
, .s
= NULL
};
349 if (CLEAN(event
->state
) == 0) {
350 if (IS_ESCAPE(event
)) {
355 } else if (current_modkey
== 0 && ((event
->keyval
>= GDK_1
&& event
->keyval
<= GDK_9
)
356 || (event
->keyval
== GDK_0
&& count
))) {
357 count
= (count
? count
* 10 : 0) + (event
->keyval
- GDK_0
);
360 } else if (strchr(modkeys
, event
->keyval
) && current_modkey
!= event
->keyval
) {
361 current_modkey
= event
->keyval
;
367 if (process_keypress(event
) == TRUE
) return TRUE
;
371 if (IS_ESCAPE(event
)) {
373 a
.s
= g_strdup("hints.clearFocus();");
378 case ModePassThrough
:
379 if (IS_ESCAPE(event
)) {
394 set_widget_font_and_color(GtkWidget
*widget
, const char *font_str
, const char *bg_color_str
,
395 const char *fg_color_str
) {
398 PangoFontDescription
*font
;
400 font
= pango_font_description_from_string(font_str
);
401 gtk_widget_modify_font(widget
, font
);
402 pango_font_description_free(font
);
405 gdk_color_parse(fg_color_str
, &fg_color
);
407 gdk_color_parse(bg_color_str
, &bg_color
);
409 gtk_widget_modify_text(widget
, GTK_STATE_NORMAL
, fg_color_str
? &fg_color
: NULL
);
410 gtk_widget_modify_base(widget
, GTK_STATE_NORMAL
, bg_color_str
? &bg_color
: NULL
);
416 webview_hoverlink_cb(WebKitWebView
*webview
, char *title
, char *link
, gpointer data
) {
417 const char *uri
= webkit_web_view_get_uri(webview
);
419 memset(rememberedURI
, 0, 1024);
421 gtk_label_set_markup(GTK_LABEL(status_url
), g_markup_printf_escaped("<span font=\"%s\">Link: %s</span>", statusfont
, link
));
422 strncpy(rememberedURI
, link
, 1024);
428 webview_console_cb(WebKitWebView
*webview
, char *message
, int line
, char *source
, gpointer user_data
) {
431 /* Don't change internal mode if the browser doesn't have focus to prevent inconsistent states */
432 if (gtk_window_has_toplevel_focus(window
)) {
433 if (!strcmp(message
, "hintmode_off") || !strcmp(message
, "insertmode_off")) {
436 } else if (!strcmp(message
, "insertmode_on")) {
445 inputbox_activate_cb(GtkEntry
*entry
, gpointer user_data
) {
447 guint16 length
= gtk_entry_get_text_length(entry
);
449 gboolean success
= FALSE
, forward
= FALSE
;
451 a
.i
= HideCompletion
;
455 text
= (char*)gtk_entry_get_text(entry
);
456 if (length
> 1 && text
[0] == ':') {
457 success
= process_line((text
+ 1));
458 } else if (length
> 1 && ((forward
= text
[0] == '/') || text
[0] == '?')) {
459 webkit_web_view_unmark_text_matches(webview
);
460 #ifdef ENABLE_MATCH_HIGHLITING
461 webkit_web_view_mark_text_matches(webview
, &text
[1], FALSE
, 0);
462 webkit_web_view_set_highlight_text_matches(webview
, TRUE
);
465 #ifndef ENABLE_INCREMENTAL_SEARCH
467 a
.i
= searchoptions
| (forward
? DirectionForward
: DirectionBackwards
);
470 search_direction
= forward
;
471 search_handle
= g_strdup(&text
[1]);
473 } else if (text
[0] == '.' || text
[0] == ',') {
475 a
.s
= g_strdup_printf("hints.fire();");
481 gtk_entry_set_text(entry
, "");
482 gtk_widget_grab_focus(GTK_WIDGET(webview
));
486 inputbox_keypress_cb(GtkEntry
*entry
, GdkEventKey
*event
) {
490 if (mode
== ModeHints
) {
491 if (event
->keyval
== GDK_Tab
) {
493 a
.s
= g_strdup_printf("hints.focusNextHint();");
498 if (event
->keyval
== GDK_ISO_Left_Tab
) {
500 a
.s
= g_strdup_printf("hints.focusPreviousHint();");
505 if (event
->keyval
== GDK_Return
) {
507 a
.s
= g_strdup_printf("hints.fire();");
513 switch (event
->keyval
) {
514 case GDK_bracketleft
:
516 if (!IS_ESCAPE(event
)) break;
517 a
.i
= HideCompletion
;
528 return commandhistoryfetch(&a
);
532 return commandhistoryfetch(&a
);
534 case GDK_ISO_Left_Tab
:
540 if (mode
== ModeHints
) {
541 if ((CLEAN(event
->state
) & GDK_SHIFT_MASK
) &&
542 (CLEAN(event
->state
) & GDK_CONTROL_MASK
) &&
543 (event
->keyval
== GDK_BackSpace
)) {
546 a
.s
= g_strdup_printf("hints.updateHints(%d);", count
);
552 numval
= g_unichar_digit_value((gunichar
) gdk_keyval_to_unicode(event
->keyval
));
553 if ((numval
>= 1 && numval
<= 9) || (numval
== 0 && count
)) {
554 /* allow a zero as non-first number */
555 count
= (count
? count
* 10 : 0) + numval
;
557 a
.s
= g_strdup_printf("hints.updateHints(%d);", count
);
568 notify_event_cb(GtkWidget
*widget
, GdkEvent
*event
, gpointer user_data
) {
570 if (mode
== ModeNormal
&& event
->type
== GDK_BUTTON_RELEASE
) {
571 /* handle mouse click events */
572 for (i
= 0; i
< LENGTH(mouse
); i
++) {
573 if (mouse
[i
].mask
== CLEAN(event
->button
.state
)
574 && (mouse
[i
].modkey
== current_modkey
575 || (!mouse
[i
].modkey
&& !current_modkey
)
576 || mouse
[i
].modkey
== GDK_VoidSymbol
) /* wildcard */
577 && mouse
[i
].button
== event
->button
.button
579 if (mouse
[i
].func(&mouse
[i
].arg
)) {
580 current_modkey
= count
= 0;
590 static gboolean
inputbox_keyrelease_cb(GtkEntry
*entry
, GdkEventKey
*event
) {
592 guint16 length
= gtk_entry_get_text_length(entry
);
595 a
.i
= HideCompletion
;
603 static gboolean
inputbox_changed_cb(GtkEditable
*entry
, gpointer user_data
) {
605 char *text
= (char*)gtk_entry_get_text(GTK_ENTRY(entry
));
606 guint16 length
= gtk_entry_get_text_length(GTK_ENTRY(entry
));
607 gboolean forward
= FALSE
;
609 /* Update incremental search if the user changes the search text.
611 * Note: gtk_widget_is_focus() is a poor way to check if the change comes
612 * from the user. But if the entry is focused and the text is set
613 * through gtk_entry_set_text() in some asyncrounous operation,
614 * I would consider that a bug.
617 if (gtk_widget_is_focus(GTK_WIDGET(entry
)) && length
> 1 && ((forward
= text
[0] == '/') || text
[0] == '?')) {
618 webkit_web_view_unmark_text_matches(webview
);
619 webkit_web_view_search_text(webview
, &text
[1], searchoptions
& CaseSensitive
, forward
, searchoptions
& Wrapping
);
621 } else if (gtk_widget_is_focus(GTK_WIDGET(entry
)) && length
>= 1 &&
622 (text
[0] == '.' || text
[0] == ',')) {
624 a
.s
= g_strdup("hints.clearHints();");
628 a
.s
= g_strconcat("hints.createHints('", text
+ 1, "');", NULL
);
632 } else if (length
== 0 && followTarget
[0]) {
635 a
.s
= g_strdup("hints.clearHints();");
646 void fill_suggline(char * suggline
, const char * command
, const char *fill_with
) {
647 memset(suggline
, 0, 512);
648 strncpy(suggline
, command
, 512);
649 strncat(suggline
, " ", 1);
650 strncat(suggline
, fill_with
, 512 - strlen(suggline
) - 1);
653 GtkWidget
* fill_eventbox(const char * completion_line
) {
655 GtkWidget
*row_eventbox
, *el
;
659 row
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
660 row_eventbox
= gtk_event_box_new();
661 gdk_color_parse(completionbgcolor
[0], &color
);
662 gtk_widget_modify_bg(row_eventbox
, GTK_STATE_NORMAL
, &color
);
663 el
= gtk_label_new(NULL
);
664 markup
= g_strconcat("<span font=\"", completionfont
[0], "\" color=\"", completioncolor
[0], "\">",
665 g_markup_escape_text(completion_line
, strlen(completion_line
)), "</span>", NULL
);
666 gtk_label_set_markup(GTK_LABEL(el
), markup
);
668 gtk_misc_set_alignment(GTK_MISC(el
), 0, 0);
669 gtk_box_pack_start(row
, el
, TRUE
, TRUE
, 2);
670 gtk_container_add(GTK_CONTAINER(row_eventbox
), GTK_WIDGET(row
));
675 complete(const Arg
*arg
) {
676 char *str
, *p
, *s
, *markup
, *entry
, *searchfor
, command
[32] = "", suggline
[512] = "", **suggurls
;
677 size_t listlen
, len
, cmdlen
;
679 Listelement
*elementlist
= NULL
, *elementpointer
;
680 gboolean highlight
= FALSE
;
682 GtkWidget
*row_eventbox
, *el
;
685 static GtkWidget
*table
, *top_border
;
687 static char **suggestions
;
688 static GtkWidget
**widgets
;
689 static int n
= 0, m
, current
= -1;
691 str
= (char*)gtk_entry_get_text(GTK_ENTRY(inputbox
));
694 /* Get the length of the list of commands for completion. We need this to
695 * malloc/realloc correctly.
697 listlen
= LENGTH(commands
);
699 if ((len
== 0 || str
[0] != ':') && arg
->i
!= HideCompletion
)
702 if (arg
->i
!= HideCompletion
&& widgets
&& current
!= -1 && !strcmp(&str
[1], suggestions
[current
])) {
703 gdk_color_parse(completionbgcolor
[0], &color
);
704 gtk_widget_modify_bg(widgets
[current
], GTK_STATE_NORMAL
, &color
);
705 current
= (n
+ current
+ (arg
->i
== DirectionPrev
? -1 : 1)) % n
;
706 if ((arg
->i
== DirectionNext
&& current
== 0)
707 || (arg
->i
== DirectionPrev
&& current
== n
- 1))
713 gtk_widget_destroy(GTK_WIDGET(table
));
714 gtk_widget_destroy(GTK_WIDGET(top_border
));
721 if (arg
->i
== HideCompletion
)
724 } else if (arg
->i
== HideCompletion
)
727 prefix
= g_strdup_printf(str
);
728 widgets
= malloc(sizeof(GtkWidget
*) * listlen
);
729 suggestions
= malloc(sizeof(char*) * listlen
);
730 top_border
= gtk_event_box_new();
731 gtk_widget_set_size_request(GTK_WIDGET(top_border
), 0, 1);
732 gdk_color_parse(completioncolor
[2], &color
);
733 gtk_widget_modify_bg(top_border
, GTK_STATE_NORMAL
, &color
);
734 table
= gtk_event_box_new();
735 gdk_color_parse(completionbgcolor
[0], &color
);
736 _table
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
738 if (strchr(str
, ' ') == NULL
) {
739 /* command completion */
740 listlen
= LENGTH(commands
);
741 for (i
= 0; i
< listlen
; i
++) {
742 if (commands
[i
].cmd
== NULL
)
744 cmdlen
= strlen(commands
[i
].cmd
);
745 if (!highlight
|| (n
< MAX_LIST_SIZE
&& len
- 1 <= cmdlen
&& !strncmp(&str
[1], commands
[i
].cmd
, len
- 1))) {
746 p
= s
= malloc(sizeof(char*) * (highlight
? sizeof(COMPLETION_TAG_OPEN
) + sizeof(COMPLETION_TAG_CLOSE
) - 1 : 1) + cmdlen
);
748 memcpy(p
, COMPLETION_TAG_OPEN
, sizeof(COMPLETION_TAG_OPEN
) - 1);
749 memcpy((p
+= sizeof(COMPLETION_TAG_OPEN
) - 1), &str
[1], len
- 1);
750 memcpy((p
+= len
- 1), COMPLETION_TAG_CLOSE
, sizeof(COMPLETION_TAG_CLOSE
) - 1);
751 p
+= sizeof(COMPLETION_TAG_CLOSE
) - 1;
753 memcpy(p
, &commands
[i
].cmd
[len
- 1], cmdlen
- len
+ 2);
754 row
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
755 row_eventbox
= gtk_event_box_new();
756 gtk_widget_modify_bg(row_eventbox
, GTK_STATE_NORMAL
, &color
);
757 el
= gtk_label_new(NULL
);
758 markup
= g_strconcat("<span font=\"", completionfont
[0], "\" color=\"", completioncolor
[0], "\">", s
, "</span>", NULL
);
760 gtk_label_set_markup(GTK_LABEL(el
), markup
);
762 gtk_misc_set_alignment(GTK_MISC(el
), 0, 0);
763 gtk_box_pack_start(row
, el
, TRUE
, TRUE
, 2);
764 gtk_container_add(GTK_CONTAINER(row_eventbox
), GTK_WIDGET(row
));
765 gtk_box_pack_start(_table
, GTK_WIDGET(row_eventbox
), FALSE
, FALSE
, 0);
766 suggestions
[n
] = commands
[i
].cmd
;
767 widgets
[n
++] = row_eventbox
;
771 entry
= (char *)malloc(512 * sizeof(char));
775 memset(entry
, 0, 512);
776 suggurls
= malloc(sizeof(char*) * listlen
);
777 if (suggurls
== NULL
) {
780 spacepos
= strcspn(str
, " ");
781 searchfor
= (str
+ spacepos
+ 1);
782 strncpy(command
, (str
+ 1), spacepos
- 1);
783 if (strlen(command
) == 3 && strncmp(command
, "set", 3) == 0) {
784 /* browser settings */
785 listlen
= LENGTH(browsersettings
);
786 for (i
= 0; i
< listlen
; i
++) {
787 if (n
< MAX_LIST_SIZE
&& strstr(browsersettings
[i
].name
, searchfor
) != NULL
) {
789 fill_suggline(suggline
, command
, browsersettings
[i
].name
);
790 suggurls
[n
] = (char *)malloc(sizeof(char) * 512 + 1);
791 strncpy(suggurls
[n
], suggline
, 512);
792 suggestions
[n
] = suggurls
[n
];
793 row_eventbox
= fill_eventbox(suggline
);
794 gtk_box_pack_start(_table
, GTK_WIDGET(row_eventbox
), FALSE
, FALSE
, 0);
795 widgets
[n
++] = row_eventbox
;
799 } else if (strlen(command
) == 2 && strncmp(command
, "qt", 2) == 0) {
800 /* completion on tags */
801 spacepos
= strcspn(str
, " ");
802 searchfor
= (str
+ spacepos
+ 1);
803 elementlist
= complete_list(searchfor
, 1, elementlist
);
805 /* URL completion: bookmarks */
806 elementlist
= complete_list(searchfor
, 0, elementlist
);
807 m
= count_list(elementlist
);
808 if (m
< MAX_LIST_SIZE
) {
809 /* URL completion: history */
810 elementlist
= complete_list(searchfor
, 2, elementlist
);
813 elementpointer
= elementlist
;
814 while (elementpointer
!= NULL
) {
815 fill_suggline(suggline
, command
, elementpointer
->element
);
816 suggurls
[n
] = (char *)malloc(sizeof(char) * 512 + 1);
817 strncpy(suggurls
[n
], suggline
, 512);
818 suggestions
[n
] = suggurls
[n
];
819 row_eventbox
= fill_eventbox(suggline
);
820 gtk_box_pack_start(_table
, GTK_WIDGET(row_eventbox
), FALSE
, FALSE
, 0);
821 widgets
[n
++] = row_eventbox
;
822 elementpointer
= elementpointer
->next
;
823 if (n
>= MAX_LIST_SIZE
)
826 free_list(elementlist
);
827 if (suggurls
!= NULL
) {
836 /* TA: FIXME - this needs rethinking entirely. */
838 GtkWidget
**widgets_temp
= realloc(widgets
, sizeof(*widgets
) * n
);
839 if (widgets_temp
== NULL
&& widgets
== NULL
) {
840 fprintf(stderr
, "Couldn't realloc() widgets\n");
843 widgets
= widgets_temp
;
844 char **suggestions_temp
= realloc(suggestions
, sizeof(*suggestions
) * n
);
845 if (suggestions_temp
== NULL
&& suggestions
== NULL
) {
846 fprintf(stderr
, "Couldn't realloc() suggestions\n");
849 suggestions
= suggestions_temp
;
852 gdk_color_parse(completionbgcolor
[1], &color
);
853 gtk_widget_modify_bg(table
, GTK_STATE_NORMAL
, &color
);
854 el
= gtk_label_new(NULL
);
855 gtk_misc_set_alignment(GTK_MISC(el
), 0, 0);
856 markup
= g_strconcat("<span font=\"", completionfont
[1], "\" color=\"", completioncolor
[1], "\">No Completions</span>", NULL
);
857 gtk_label_set_markup(GTK_LABEL(el
), markup
);
859 gtk_box_pack_start(_table
, GTK_WIDGET(el
), FALSE
, FALSE
, 0);
861 gtk_box_pack_start(box
, GTK_WIDGET(top_border
), FALSE
, FALSE
, 0);
862 gtk_container_add(GTK_CONTAINER(table
), GTK_WIDGET(_table
));
863 gtk_box_pack_start(box
, GTK_WIDGET(table
), FALSE
, FALSE
, 0);
864 gtk_widget_show_all(GTK_WIDGET(window
));
867 current
= arg
->i
== DirectionPrev
? n
- 1 : 0;
870 gdk_color_parse(completionbgcolor
[2], &color
);
871 gtk_widget_modify_bg(GTK_WIDGET(widgets
[current
]), GTK_STATE_NORMAL
, &color
);
872 s
= g_strconcat(":", suggestions
[current
], NULL
);
873 gtk_entry_set_text(GTK_ENTRY(inputbox
), s
);
876 gtk_entry_set_text(GTK_ENTRY(inputbox
), prefix
);
877 gtk_editable_set_position(GTK_EDITABLE(inputbox
), -1);
882 descend(const Arg
*arg
) {
883 char *source
= (char*)webkit_web_view_get_uri(webview
), *p
= &source
[0], *new;
885 count
= count
? count
: 1;
889 if (arg
->i
== Rootdir
) {
890 for (i
= 0; i
< 3; i
++) /* get to the third slash */
891 if (!(p
= strchr(++p
, '/')))
892 return TRUE
; /* if we cannot find it quit */
894 len
= strlen(source
);
895 if (!len
) /* if string is empty quit */
897 p
= source
+ len
; /* start at the end */
898 if (*(p
- 1) == '/') /* /\/$/ is not an additional level */
900 for (i
= 0; i
< count
; i
++)
901 while(*(p
--) != '/' || *p
== '/') /* count /\/+/ as one slash */
902 if (p
== source
) /* if we reach the first char pointer quit */
904 ++p
; /* since we do p-- in the while, we are pointing at
905 the char before the slash, so +1 */
907 len
= p
- source
+ 1; /* new length = end - start + 1 */
908 new = malloc(len
+ 1);
909 memcpy(new, source
, len
);
911 webkit_web_view_load_uri(webview
, new);
917 echo(const Arg
*arg
) {
918 int index
= !arg
->s
? 0 : arg
->i
& (~NoAutoHide
);
920 if (index
< Info
|| index
> Error
)
923 set_widget_font_and_color(inputbox
, urlboxfont
[index
], urlboxbgcolor
[index
], urlboxcolor
[index
]);
924 gtk_entry_set_text(GTK_ENTRY(inputbox
), !arg
->s
? "" : arg
->s
);
930 input(const Arg
*arg
) {
937 /* if inputbox hidden, show it again */
938 if (!gtk_widget_get_visible(inputbox
))
939 gtk_widget_set_visible(inputbox
, TRUE
);
943 /* Set the colour and font back to the default, so that we don't still
944 * maintain a red colour from a warning from an end of search indicator,
947 set_widget_font_and_color(inputbox
, urlboxfont
[index
], urlboxbgcolor
[index
], urlboxcolor
[index
]);
949 if (arg
->s
[0] == '.' || arg
->s
[0] == ',') {
951 memset(followTarget
, 0, 8);
952 strncpy(followTarget
, arg
->s
[0] == '.' ? "current" : "new", 8);
954 a
.s
= g_strdup("hints.createHints();");
958 /* to avoid things like :open URL :open URL2 or :open :open URL */
959 gtk_entry_set_text(GTK_ENTRY(inputbox
), "");
960 gtk_editable_insert_text(GTK_EDITABLE(inputbox
), arg
->s
, -1, &pos
);
961 if (arg
->i
& InsertCurrentURL
&& (url
= webkit_web_view_get_uri(webview
)))
962 gtk_editable_insert_text(GTK_EDITABLE(inputbox
), url
, -1, &pos
);
963 gtk_widget_grab_focus(inputbox
);
964 gtk_editable_set_position(GTK_EDITABLE(inputbox
), -1);
970 navigate(const Arg
*arg
) {
971 if (arg
->i
& NavigationForwardBack
)
972 webkit_web_view_go_back_or_forward(webview
, (arg
->i
== NavigationBack
? -1 : 1) * (count
? count
: 1));
973 else if (arg
->i
& NavigationReloadActions
)
974 (arg
->i
== NavigationReload
? webkit_web_view_reload
: webkit_web_view_reload_bypass_cache
)(webview
);
976 webkit_web_view_stop_loading(webview
);
981 number(const Arg
*arg
) {
982 const char *source
= webkit_web_view_get_uri(webview
);
984 int number
, diff
= (count
? count
: 1) * (arg
->i
== Increment
? 1 : -1);
988 uri
= g_strdup_printf(source
); /* copy string */
990 while(*p
!= '\0') /* goto the end of the string */
993 while(*p
>= '0' && *p
<= '9') /* go back until non number char is reached */
995 if (*(++p
) == '\0') { /* if no numbers were found abort */
999 number
= atoi(p
) + diff
; /* apply diff on number */
1001 new = g_strdup_printf("%s%d", uri
, number
); /* create new uri */
1002 webkit_web_view_load_uri(webview
, new);
1009 open_arg(const Arg
*arg
) {
1011 char *s
= arg
->s
, *p
= NULL
, *new;
1012 Arg a
= { .i
= NavigationReload
};
1014 char *search_uri
, *search_term
;
1030 else if (arg
->i
== TargetCurrent
) {
1031 while(*s
== ' ') /* strip leading whitespace */
1033 p
= (s
+ strlen(s
) - 1);
1034 while(*p
== ' ') /* strip trailing whitespace */
1038 new = NULL
, p
= strchr(s
, ' ');
1039 if (p
) { /* check for search engines */
1041 search_uri
= find_uri_for_searchengine(s
);
1042 if (search_uri
!= NULL
) {
1043 search_term
= soup_uri_encode(p
+1, "&");
1044 new = g_strdup_printf(search_uri
, search_term
);
1045 g_free(search_term
);
1050 if (len
> 3 && strstr(s
, "://")) { /* valid url? */
1051 p
= new = g_malloc(len
+ 1);
1052 while(*s
!= '\0') { /* strip whitespaces */
1058 } else if (strcspn(s
, "/") == 0 || strcspn(s
, "./") == 0) { /* prepend "file://" */
1059 new = g_malloc(sizeof("file://") + len
);
1060 strcpy(new, "file://");
1061 memcpy(&new[sizeof("file://") - 1], s
, len
+ 1);
1062 } else if (p
|| !strchr(s
, '.')) { /* whitespaces or no dot? */
1063 search_uri
= find_uri_for_searchengine(defaultsearch
);
1064 if (search_uri
!= NULL
) {
1065 search_term
= soup_uri_encode(s
, "&");
1066 new = g_strdup_printf(search_uri
, search_term
);
1067 g_free(search_term
);
1069 } else { /* prepend "http://" */
1070 new = g_malloc(sizeof("http://") + len
);
1071 strcpy(new, "http://");
1072 memcpy(&new[sizeof("http://") - 1], s
, len
+ 1);
1075 webkit_web_view_load_uri(webview
, new);
1078 g_spawn_async(NULL
, argv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
, NULL
, NULL
, NULL
);
1083 open_remembered(const Arg
*arg
)
1085 Arg a
= {arg
->i
, rememberedURI
};
1087 if (strcmp(rememberedURI
, "")) {
1094 yank(const Arg
*arg
) {
1095 const char *url
, *feedback
;
1097 if (arg
->i
& SourceURL
) {
1098 url
= webkit_web_view_get_uri(webview
);
1101 feedback
= g_strconcat("Yanked ", url
, NULL
);
1102 give_feedback(feedback
);
1103 if (arg
->i
& ClipboardPrimary
)
1104 gtk_clipboard_set_text(clipboards
[0], url
, -1);
1105 if (arg
->i
& ClipboardGTK
)
1106 gtk_clipboard_set_text(clipboards
[1], url
, -1);
1108 webkit_web_view_copy_clipboard(webview
);
1113 paste(const Arg
*arg
) {
1114 Arg a
= { .i
= arg
->i
& TargetNew
, .s
= NULL
};
1116 /* If we're over a link, open it in a new target. */
1117 if (strlen(rememberedURI
) > 0) {
1118 Arg new_target
= { .i
= TargetNew
, .s
= arg
->s
};
1119 open_arg(&new_target
);
1123 if (arg
->i
& ClipboardPrimary
)
1124 a
.s
= gtk_clipboard_wait_for_text(clipboards
[0]);
1125 if (!a
.s
&& arg
->i
& ClipboardGTK
)
1126 a
.s
= gtk_clipboard_wait_for_text(clipboards
[1]);
1133 quit(const Arg
*arg
) {
1135 const char *filename
;
1136 const char *uri
= webkit_web_view_get_uri(webview
);
1138 /* write last URL into status file for recreation with "u" */
1139 filename
= g_strdup_printf(CLOSED_URL_FILENAME
);
1140 f
= fopen(filename
, "w");
1142 fprintf(f
, "%s", uri
);
1151 revive(const Arg
*arg
) {
1153 const char *filename
;
1154 char buffer
[512] = "";
1155 Arg a
= { .i
= TargetNew
, .s
= NULL
};
1156 /* get the URL of the window which has been closed last */
1157 filename
= g_strdup_printf(CLOSED_URL_FILENAME
);
1158 f
= fopen(filename
, "r");
1160 fgets(buffer
, 512, f
);
1163 if (strlen(buffer
) > 0) {
1172 gboolean
print_frame(const Arg
*arg
)
1174 WebKitWebFrame
*frame
= webkit_web_view_get_main_frame(webview
);
1175 webkit_web_frame_print (frame
);
1180 search(const Arg
*arg
) {
1181 count
= count
? count
: 1;
1182 gboolean success
, direction
= arg
->i
& DirectionPrev
;
1186 free(search_handle
);
1187 search_handle
= g_strdup_printf(arg
->s
);
1191 if (arg
->i
& DirectionAbsolute
)
1192 search_direction
= direction
;
1194 direction
^= search_direction
;
1196 success
= webkit_web_view_search_text(webview
, search_handle
, arg
->i
& CaseSensitive
, direction
, FALSE
);
1198 if (arg
->i
& Wrapping
) {
1199 success
= webkit_web_view_search_text(webview
, search_handle
, arg
->i
& CaseSensitive
, direction
, TRUE
);
1202 a
.s
= g_strdup_printf("search hit %s, continuing at %s",
1203 direction
? "BOTTOM" : "TOP",
1204 direction
? "TOP" : "BOTTOM");
1215 a
.s
= g_strdup_printf("Pattern not found: %s", search_handle
);
1223 set(const Arg
*arg
) {
1224 Arg a
= { .i
= Info
| NoAutoHide
};
1228 if (search_handle
) {
1229 search_handle
= NULL
;
1230 webkit_web_view_unmark_text_matches(webview
);
1232 gtk_entry_set_text(GTK_ENTRY(inputbox
), "");
1233 gtk_widget_grab_focus(GTK_WIDGET(webview
));
1235 case ModePassThrough
:
1236 a
.s
= g_strdup("-- PASS THROUGH --");
1241 a
.s
= g_strdup("-- PASS TROUGH (next) --");
1245 case ModeInsert
: /* should not be called manually but automatically */
1246 a
.s
= g_strdup("-- INSERT --");
1258 jsapi_ref_to_string(JSContextRef context
, JSValueRef ref
) {
1259 JSStringRef string_ref
;
1263 string_ref
= JSValueToStringCopy(context
, ref
, NULL
);
1264 length
= JSStringGetMaximumUTF8CStringSize(string_ref
);
1265 string
= g_new(gchar
, length
);
1266 JSStringGetUTF8CString(string_ref
, string
, length
);
1267 JSStringRelease(string_ref
);
1272 jsapi_evaluate_script(const gchar
*script
, gchar
**value
, gchar
**message
) {
1273 WebKitWebFrame
*frame
= webkit_web_view_get_main_frame(webview
);
1274 JSGlobalContextRef context
= webkit_web_frame_get_global_context(frame
);
1276 JSValueRef val
, exception
;
1278 str
= JSStringCreateWithUTF8CString(script
);
1279 val
= JSEvaluateScript(context
, str
, JSContextGetGlobalObject(context
), NULL
, 0, &exception
);
1280 JSStringRelease(str
);
1282 *message
= jsapi_ref_to_string(context
, exception
);
1284 *value
= jsapi_ref_to_string(context
, val
);
1288 quickmark(const Arg
*a
) {
1291 char *fn
= g_strdup_printf(QUICKMARK_FILE
);
1293 fp
= fopen(fn
, "r");
1296 if (fp
!= NULL
&& b
< 10) {
1297 for( i
=0; i
< b
; ++i
) {
1301 fgets(buf
, 100, fp
);
1303 char *ptr
= strrchr(buf
, '\n');
1305 Arg x
= { .s
= buf
};
1307 return open_arg(&x
);
1310 x
.s
= g_strdup_printf("Quickmark %d not defined", b
);
1315 } else { return false; }
1319 script(const Arg
*arg
) {
1320 gchar
*value
= NULL
, *message
= NULL
;
1324 set_error("Missing argument.");
1327 jsapi_evaluate_script(arg
->s
, &value
, &message
);
1334 if (arg
->i
!= Silent
&& value
) {
1336 a
.s
= g_strdup(value
);
1341 if (strncmp(value
, "fire;", 5) == 0) {
1343 a
.s
= g_strconcat("hints.fire(", (value
+ 5), ");", NULL
);
1346 } else if (strncmp(value
, "open;", 5) == 0) {
1350 if (strncmp(followTarget
, "new", 3) == 0)
1353 a
.i
= TargetCurrent
;
1365 scroll(const Arg
*arg
) {
1366 GtkAdjustment
*adjust
= (arg
->i
& OrientationHoriz
) ? adjust_h
: adjust_v
;
1367 int max
= gtk_adjustment_get_upper(adjust
) - gtk_adjustment_get_page_size(adjust
);
1368 float val
= gtk_adjustment_get_value(adjust
) / max
* 100;
1369 int direction
= (arg
->i
& (1 << 2)) ? 1 : -1;
1371 if ((direction
== 1 && val
< 100) || (direction
== -1 && val
> 0)) {
1372 if (arg
->i
& ScrollMove
)
1373 gtk_adjustment_set_value(adjust
, gtk_adjustment_get_value(adjust
) +
1374 direction
* /* direction */
1375 ((arg
->i
& UnitLine
|| (arg
->i
& UnitBuffer
&& count
)) ? (scrollstep
* (count
? count
: 1)) : (
1376 arg
->i
& UnitBuffer
? gtk_adjustment_get_page_size(adjust
) / 2 :
1377 (count
? count
: 1) * (gtk_adjustment_get_page_size(adjust
) -
1378 (gtk_adjustment_get_page_size(adjust
) > pagingkeep
? pagingkeep
: 0)))));
1380 gtk_adjustment_set_value(adjust
,
1381 ((direction
== 1) ? gtk_adjustment_get_upper
: gtk_adjustment_get_lower
)(adjust
));
1388 zoom(const Arg
*arg
) {
1389 webkit_web_view_set_full_content_zoom(webview
, (arg
->i
& ZoomFullContent
) > 0);
1390 webkit_web_view_set_zoom_level(webview
, (arg
->i
& ZoomOut
) ?
1391 webkit_web_view_get_zoom_level(webview
) +
1392 (((float)(count
? count
: 1)) * (arg
->i
& (1 << 1) ? 1.0 : -1.0) * zoomstep
) :
1393 (count
? (float)count
/ 100.0 : 1.0));
1398 fake_key_event(const Arg
*a
) {
1405 if ( (xdpy
= XOpenDisplay(NULL
)) == NULL
) {
1406 err
.s
= g_strdup("Couldn't find the XDisplay.");
1414 xk
.subwindow
= None
;
1415 xk
.time
= CurrentTime
;
1416 xk
.same_screen
= True
;
1417 xk
.x
= xk
.y
= xk
.x_root
= xk
.y_root
= 1;
1422 err
.s
= g_strdup("Zero pointer as argument! Check your config.h");
1429 if( (keysym
= XStringToKeysym(a
->s
)) == NoSymbol
) {
1430 err
.s
= g_strdup_printf("Couldn't translate %s to keysym", a
->s
);
1436 if( (xk
.keycode
= XKeysymToKeycode(xdpy
, keysym
)) == NoSymbol
) {
1437 err
.s
= g_strdup("Couldn't translate keysym to keycode");
1444 if( !XSendEvent(xdpy
, embed
, True
, KeyPressMask
, (XEvent
*)&xk
) ) {
1445 err
.s
= g_strdup("XSendEvent failed");
1457 commandhistoryfetch(const Arg
*arg
) {
1458 if (arg
->i
== DirectionPrev
) {
1460 if (commandpointer
< 0)
1461 commandpointer
= maxcommands
- 1;
1464 if (commandpointer
== COMMANDHISTSIZE
|| commandpointer
== maxcommands
)
1468 if (commandpointer
< 0)
1471 gtk_entry_set_text(GTK_ENTRY(inputbox
), commandhistory
[commandpointer
]);
1472 gtk_editable_set_position(GTK_EDITABLE(inputbox
), -1);
1478 bookmark(const Arg
*arg
) {
1480 const char *filename
;
1481 const char *uri
= webkit_web_view_get_uri(webview
);
1482 const char *title
= webkit_web_view_get_title(webview
);
1483 filename
= g_strdup_printf(BOOKMARKS_STORAGE_FILENAME
);
1484 f
= fopen(filename
, "a");
1485 if (uri
== NULL
|| strlen(uri
) == 0) {
1486 set_error("No URI found to bookmark.");
1490 fprintf(f
, "%s", uri
);
1491 if (title
!= NULL
) {
1492 fprintf(f
, "%s", " ");
1493 fprintf(f
, "%s", title
);
1495 if (arg
->s
&& strlen(arg
->s
)) {
1496 build_taglist(arg
, f
);
1498 fprintf(f
, "%s", "\n");
1500 give_feedback( "Bookmark saved" );
1503 set_error("Bookmarks file not found.");
1511 const char *filename
;
1512 const char *uri
= webkit_web_view_get_uri(webview
);
1513 const char *title
= webkit_web_view_get_title(webview
);
1514 char *entry
, buffer
[512], *new;
1516 gboolean finished
= FALSE
;
1518 if (title
!= NULL
) {
1519 entry
= malloc((strlen(uri
) + strlen(title
) + 2) * sizeof(char));
1520 memset(entry
, 0, strlen(uri
) + strlen(title
) + 2);
1522 entry
= malloc((strlen(uri
) + 1) * sizeof(char));
1523 memset(entry
, 0, strlen(uri
) + 1);
1525 if (entry
!= NULL
) {
1526 strncpy(entry
, uri
, strlen(uri
));
1527 if (title
!= NULL
) {
1528 strncat(entry
, " ", 1);
1529 strncat(entry
, title
, strlen(title
));
1532 filename
= g_strdup_printf(HISTORY_STORAGE_FILENAME
);
1533 f
= fopen(filename
, "r");
1535 new = (char *)malloc(HISTORY_MAX_ENTRIES
* 512 * sizeof(char) + 1);
1537 memset(new, 0, HISTORY_MAX_ENTRIES
* 512 * sizeof(char) + 1);
1538 /* newest entries go on top */
1539 strncpy(new, entry
, strlen(entry
));
1540 strncat(new, "\n", 1);
1541 /* retain at most HISTORY_MAX_ENTIRES - 1 old entries */
1542 while (finished
!= TRUE
) {
1543 if ((char *)NULL
== fgets(buffer
, 512, f
)) {
1544 /* check if end of file was reached / error occured */
1548 /* end of file reached */
1552 /* compare line (-1 because of newline character) */
1553 if (n
!= strlen(buffer
) - 1 || strncmp(entry
, buffer
, n
) != 0) {
1554 /* if the URI is already in history; we put it on top and skip it here */
1555 strncat(new, buffer
, 512);
1558 if ((i
+ 1) >= HISTORY_MAX_ENTRIES
) {
1564 f
= fopen(filename
, "w");
1566 fprintf(f
, "%s", new);
1575 if (entry
!= NULL
) {
1584 view_source(const Arg
* arg
) {
1585 gboolean current_mode
= webkit_web_view_get_view_source_mode(webview
);
1586 webkit_web_view_set_view_source_mode(webview
, !current_mode
);
1587 webkit_web_view_reload(webview
);
1592 focus_input(const Arg
*arg
) {
1595 a
.s
= g_strconcat("hints.focusInput();", NULL
);
1603 browser_settings(const Arg
*arg
) {
1606 set_error("Missing argument.");
1609 strncpy(line
, arg
->s
, 254);
1610 if (process_set_line(line
))
1613 set_error("Invalid setting.");
1619 search_word(int whichword
) {
1621 static char word
[240];
1622 char *c
= my_pair
.line
;
1624 while (isspace(*c
) && *c
)
1627 switch (whichword
) {
1629 while (*c
&& !isspace (*c
) && *c
!= '=' && k
< 240) {
1634 strncpy(my_pair
.what
, word
, 20);
1637 while (*c
&& k
< 240) {
1642 strncpy(my_pair
.value
, word
, 240);
1650 process_set_line(char *line
) {
1654 WebKitWebSettings
*settings
;
1656 settings
= webkit_web_view_get_settings(webview
);
1657 my_pair
.line
= line
;
1659 if (!strlen(my_pair
.what
))
1662 while (isspace(*c
) && *c
)
1665 if (*c
== ':' || *c
== '=')
1671 listlen
= LENGTH(browsersettings
);
1672 for (i
= 0; i
< listlen
; i
++) {
1673 if (strlen(browsersettings
[i
].name
) == strlen(my_pair
.what
) && strncmp(browsersettings
[i
].name
, my_pair
.what
, strlen(my_pair
.what
)) == 0) {
1674 /* mandatory argument not provided */
1675 if (strlen(my_pair
.value
) == 0)
1677 /* process qmark? */
1678 if (strlen(my_pair
.what
) == 5 && strncmp("qmark", my_pair
.what
, 5) == 0) {
1679 return (process_save_qmark(my_pair
.value
, webview
));
1681 /* interpret boolean values */
1682 if (browsersettings
[i
].boolval
) {
1683 if (strncmp(my_pair
.value
, "on", 2) == 0 || strncmp(my_pair
.value
, "true", 4) == 0 || strncmp(my_pair
.value
, "ON", 2) == 0 || strncmp(my_pair
.value
, "TRUE", 4) == 0) {
1685 } else if (strncmp(my_pair
.value
, "off", 3) == 0 || strncmp(my_pair
.value
, "false", 5) == 0 || strncmp(my_pair
.value
, "OFF", 3) == 0 || strncmp(my_pair
.value
, "FALSE", 5) == 0) {
1690 } else if (browsersettings
[i
].colourval
) {
1691 /* interpret as hexadecimal colour */
1692 if (!parse_colour(my_pair
.value
)) {
1696 if (browsersettings
[i
].var
!= NULL
) {
1697 /* write value into internal variable */
1698 /*if (browsersettings[i].intval) {
1699 browsersettings[i].var = atoi(my_pair.value);
1701 strncpy(browsersettings
[i
].var
, my_pair
.value
, MAX_SETTING_SIZE
);
1702 if (strlen(my_pair
.value
) > MAX_SETTING_SIZE
- 1) {
1703 /* in this case, \0 will not have been copied */
1704 browsersettings
[i
].var
[MAX_SETTING_SIZE
- 1] = '\0';
1705 /* in case this string is also used for a webkit setting, make sure it's consistent */
1706 my_pair
.value
[MAX_SETTING_SIZE
- 1] = '\0';
1707 give_feedback("String too long; automatically truncated!");
1711 if (strlen(browsersettings
[i
].webkit
) > 0) {
1712 /* activate appropriate webkit setting */
1713 if (browsersettings
[i
].boolval
) {
1714 g_object_set((GObject
*)settings
, browsersettings
[i
].webkit
, boolval
, NULL
);
1715 } else if (browsersettings
[i
].intval
) {
1716 g_object_set((GObject
*)settings
, browsersettings
[i
].webkit
, atoi(my_pair
.value
), NULL
);
1718 g_object_set((GObject
*)settings
, browsersettings
[i
].webkit
, my_pair
.value
, NULL
);
1720 webkit_web_view_set_settings(webview
, settings
);
1723 /* process acceptlanguage*/
1724 if (strlen(my_pair
.what
) == 14 && strncmp("acceptlanguage", my_pair
.what
, 14) == 0) {
1725 g_object_set(G_OBJECT(session
), "accept-language", acceptlanguage
, NULL
);
1728 /* toggle proxy usage? */
1729 if (strlen(my_pair
.what
) == 5 && strncmp("proxy", my_pair
.what
, 5) == 0) {
1730 toggle_proxy(boolval
);
1733 /* Toggle scrollbars. */
1734 if (strlen(my_pair
.what
) == 10 && strncmp("scrollbars", my_pair
.what
, 10) == 0)
1735 toggle_scrollbars(boolval
);
1737 /* Toggle widgets */
1738 if (strlen(my_pair
.what
) == 9 && strncmp("statusbar", my_pair
.what
, 9) == 0)
1739 gtk_widget_set_visible(GTK_WIDGET(statusbar
), boolval
);
1740 if (strlen(my_pair
.what
) == 8 && strncmp("inputbox", my_pair
.what
, 8) == 0)
1741 gtk_widget_set_visible(inputbox
, boolval
);
1743 /* case sensitivity of completion */
1744 if (strlen(my_pair
.what
) == 14 && strncmp("completioncase", my_pair
.what
, 14) == 0)
1745 complete_case_sensitive
= boolval
;
1748 if (browsersettings
[i
].reload
)
1749 webkit_web_view_reload(webview
);
1757 process_line(char *line
) {
1760 size_t len
, length
= strlen(line
);
1761 gboolean found
= FALSE
, success
= FALSE
;
1766 /* Ignore blank lines. */
1769 for (i
= 0; i
< LENGTH(commands
); i
++) {
1770 if (commands
[i
].cmd
== NULL
)
1772 len
= strlen(commands
[i
].cmd
);
1773 if (length
>= len
&& !strncmp(c
, commands
[i
].cmd
, len
) && (c
[len
] == ' ' || !c
[len
])) {
1775 a
.i
= commands
[i
].arg
.i
;
1776 a
.s
= length
> len
+ 1 ? &c
[len
+ 1] : commands
[i
].arg
.s
;
1777 success
= commands
[i
].func(&a
);
1781 save_command_history(c
);
1784 a
.s
= g_strdup_printf("Not a browser command: %s", c
);
1786 } else if (!success
) {
1788 if (error_msg
!= NULL
) {
1789 a
.s
= g_strdup_printf("%s", error_msg
);
1793 a
.s
= g_strdup_printf("Unknown error. Please file a bug report!");
1802 search_tag(const Arg
* a
) {
1804 const char *filename
;
1805 const char *tag
= a
->s
;
1806 char s
[BUFFERSIZE
], foundtag
[40], url
[BUFFERSIZE
];
1810 /* The user must give us something to load up. */
1811 set_error("Bookmark tag required with this option.");
1815 if (strlen(tag
) > MAXTAGSIZE
) {
1816 set_error("Tag too long.");
1820 filename
= g_strdup_printf(BOOKMARKS_STORAGE_FILENAME
);
1821 f
= fopen(filename
, "r");
1823 set_error("Couldn't open bookmarks file.");
1826 while (fgets(s
, BUFFERSIZE
-1, f
)) {
1829 while (isspace(s
[t
]))
1831 if (s
[t
] != ']') continue;
1844 foundtag
[i
++] = s
[k
++];
1846 /* foundtag now contains the tag */
1847 if (strlen(foundtag
) < MAXTAGSIZE
&& strcmp(tag
, foundtag
) == 0) {
1849 while (isspace(s
[i
])) i
++;
1851 while (s
[i
] && !isspace(s
[i
])) url
[k
++] = s
[i
++];
1853 Arg x
= { .i
= TargetNew
, .s
= url
};
1867 toggle_proxy(gboolean onoff
) {
1869 char *filename
, *new;
1872 if (onoff
== FALSE
) {
1873 g_object_set(session
, "proxy-uri", NULL
, NULL
);
1874 give_feedback("Proxy deactivated");
1876 filename
= (char *)g_getenv("http_proxy");
1878 /* Fallthrough to checking HTTP_PROXY as well, since this can also be
1881 if (filename
== NULL
)
1882 filename
= (char *)g_getenv("HTTP_PROXY");
1884 if (filename
!= NULL
&& 0 < (len
= strlen(filename
))) {
1885 if (strstr(filename
, "://") == NULL
) {
1886 /* prepend http:// */
1887 new = g_malloc(sizeof("http://") + len
);
1888 strcpy(new, "http://");
1889 memcpy(&new[sizeof("http://") - 1], filename
, len
+ 1);
1890 proxy_uri
= soup_uri_new(new);
1892 proxy_uri
= soup_uri_new(filename
);
1894 g_object_set(session
, "proxy-uri", proxy_uri
, NULL
);
1895 give_feedback("Proxy activated");
1901 toggle_scrollbars(gboolean onoff
) {
1902 if (onoff
== TRUE
) {
1903 adjust_h
= gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(viewport
));
1904 adjust_v
= gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewport
));
1907 adjust_v
= gtk_range_get_adjustment(GTK_RANGE(scroll_v
));
1908 adjust_h
= gtk_range_get_adjustment(GTK_RANGE(scroll_h
));
1910 gtk_widget_set_scroll_adjustments (GTK_WIDGET(webview
), adjust_h
, adjust_v
);
1916 update_url(const char *uri
) {
1917 gboolean ssl
= g_str_has_prefix(uri
, "https://");
1919 #ifdef ENABLE_HISTORY_INDICATOR
1920 char before
[] = " [";
1922 gboolean back
= webkit_web_view_can_go_back(webview
);
1923 gboolean fwd
= webkit_web_view_can_go_forward(webview
);
1926 before
[0] = after
[0] = '\0';
1928 gtk_label_set_markup(GTK_LABEL(status_url
), g_markup_printf_escaped(
1929 #ifdef ENABLE_HISTORY_INDICATOR
1930 "<span font=\"%s\">%s%s%s%s%s</span>", statusfont
, uri
,
1931 before
, back
? "+" : "", fwd
? "-" : "", after
1933 "<span font=\"%s\">%s</span>", statusfont
, uri
1936 gdk_color_parse(ssl
? sslbgcolor
: statusbgcolor
, &color
);
1937 gtk_widget_modify_bg(eventbox
, GTK_STATE_NORMAL
, &color
);
1938 gdk_color_parse(ssl
? sslcolor
: statuscolor
, &color
);
1939 gtk_widget_modify_fg(GTK_WIDGET(status_url
), GTK_STATE_NORMAL
, &color
);
1940 gtk_widget_modify_fg(GTK_WIDGET(status_state
), GTK_STATE_NORMAL
, &color
);
1946 int download_count
= g_list_length(activeDownloads
);
1947 GString
*status
= g_string_new("");
1949 /* construct the status line */
1951 /* count, modkey and input buffer */
1952 g_string_append_printf(status
, "%.0d", count
);
1953 if (current_modkey
) g_string_append_c(status
, current_modkey
);
1955 /* the number of active downloads */
1956 if (activeDownloads
) {
1957 g_string_append_printf(status
, " %d active %s", download_count
,
1958 (download_count
== 1) ? "download" : "downloads");
1961 #ifdef ENABLE_WGET_PROGRESS_BAR
1962 /* the progressbar */
1965 char progressbar
[progressbartick
+ 1];
1967 if (activeDownloads
) {
1971 for (ptr
= activeDownloads
; ptr
; ptr
= g_list_next(ptr
)) {
1972 progress
+= 100 * webkit_download_get_progress(ptr
->data
);
1975 progress
/= download_count
;
1977 } else if (webkit_web_view_get_load_status(webview
) != WEBKIT_LOAD_FINISHED
1978 && webkit_web_view_get_load_status(webview
) != WEBKIT_LOAD_FAILED
) {
1980 progress
= webkit_web_view_get_progress(webview
) * 100;
1983 if (progress
>= 0) {
1984 ascii_bar(progressbartick
, progress
* progressbartick
/ 100, progressbar
);
1985 g_string_append_printf(status
, " %c%s%c",
1986 progressborderleft
, progressbar
, progressborderright
);
1991 /* and the current scroll position */
1993 int max
= gtk_adjustment_get_upper(adjust_v
) - gtk_adjustment_get_page_size(adjust_v
);
1994 int val
= (int)(gtk_adjustment_get_value(adjust_v
) / max
* 100);
1997 g_string_append(status
, " All");
1999 g_string_append(status
, " Top");
2000 else if (val
== 100)
2001 g_string_append(status
, " Bot");
2003 g_string_append_printf(status
, " %d%%", val
);
2007 markup
= g_markup_printf_escaped("<span font=\"%s\">%s</span>", statusfont
, status
->str
);
2008 gtk_label_set_markup(GTK_LABEL(status_state
), markup
);
2010 g_string_free(status
, TRUE
);
2016 modkeys
= calloc(LENGTH(keys
) + 1, sizeof(char));
2017 char *ptr
= modkeys
;
2019 for (i
= 0; i
< LENGTH(keys
); i
++)
2020 if (keys
[i
].modkey
&& !strchr(modkeys
, keys
[i
].modkey
))
2021 *(ptr
++) = keys
[i
].modkey
;
2022 modkeys
= realloc(modkeys
, &ptr
[0] - &modkeys
[0] + 1);
2027 scroll_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
2028 scroll_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
2029 adjust_h
= gtk_range_get_adjustment(GTK_RANGE(scroll_h
));
2030 adjust_v
= gtk_range_get_adjustment(GTK_RANGE(scroll_v
));
2032 window
= GTK_WINDOW(gtk_plug_new(embed
));
2034 window
= GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL
));
2035 gtk_window_set_wmclass(GTK_WINDOW(window
), "vimprobable2", "Vimprobable2");
2037 gtk_window_set_default_size(GTK_WINDOW(window
), 640, 480);
2038 box
= GTK_BOX(gtk_vbox_new(FALSE
, 0));
2039 inputbox
= gtk_entry_new();
2040 webview
= (WebKitWebView
*)webkit_web_view_new();
2041 statusbar
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
2042 eventbox
= gtk_event_box_new();
2043 status_url
= gtk_label_new(NULL
);
2044 status_state
= gtk_label_new(NULL
);
2046 PangoFontDescription
*font
;
2047 GdkGeometry hints
= { 1, 1 };
2048 inspector
= webkit_web_view_get_inspector(WEBKIT_WEB_VIEW(webview
));
2050 clipboards
[0] = gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
2051 clipboards
[1] = gtk_clipboard_get(GDK_NONE
);
2053 gdk_color_parse(statusbgcolor
, &bg
);
2054 gtk_widget_modify_bg(eventbox
, GTK_STATE_NORMAL
, &bg
);
2055 gtk_widget_set_name(GTK_WIDGET(window
), "Vimprobable2");
2056 gtk_window_set_geometry_hints(window
, NULL
, &hints
, GDK_HINT_MIN_SIZE
);
2058 #ifdef DISABLE_SCROLLBAR
2059 viewport
= gtk_scrolled_window_new(NULL
, NULL
);
2060 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(viewport
), GTK_POLICY_NEVER
, GTK_POLICY_NEVER
);
2062 /* Ensure we still see scrollbars. */
2063 GtkWidget
*viewport
= gtk_scrolled_window_new(adjust_h
, adjust_v
);
2067 gtk_container_add(GTK_CONTAINER(viewport
), GTK_WIDGET(webview
));
2069 /* Ensure we set the scroll adjustments now, so that if we're not drawing
2070 * titlebars, we can still scroll.
2072 gtk_widget_set_scroll_adjustments(GTK_WIDGET(webview
), adjust_h
, adjust_v
);
2074 font
= pango_font_description_from_string(urlboxfont
[0]);
2075 gtk_widget_modify_font(GTK_WIDGET(inputbox
), font
);
2076 pango_font_description_free(font
);
2077 gtk_entry_set_inner_border(GTK_ENTRY(inputbox
), NULL
);
2078 gtk_misc_set_alignment(GTK_MISC(status_url
), 0.0, 0.0);
2079 gtk_misc_set_alignment(GTK_MISC(status_state
), 1.0, 0.0);
2080 gtk_box_pack_start(statusbar
, status_url
, TRUE
, TRUE
, 2);
2081 gtk_box_pack_start(statusbar
, status_state
, FALSE
, FALSE
, 2);
2082 gtk_container_add(GTK_CONTAINER(eventbox
), GTK_WIDGET(statusbar
));
2083 gtk_box_pack_start(box
, viewport
, TRUE
, TRUE
, 0);
2084 gtk_box_pack_start(box
, eventbox
, FALSE
, FALSE
, 0);
2085 gtk_entry_set_has_frame(GTK_ENTRY(inputbox
), FALSE
);
2086 gtk_box_pack_end(box
, inputbox
, FALSE
, FALSE
, 0);
2087 gtk_container_add(GTK_CONTAINER(window
), GTK_WIDGET(box
));
2088 gtk_widget_grab_focus(GTK_WIDGET(webview
));
2089 gtk_widget_show_all(GTK_WIDGET(window
));
2094 WebKitWebSettings
*settings
= (WebKitWebSettings
*)webkit_web_settings_new();
2096 char *filename
, *new;
2099 session
= webkit_get_default_session();
2100 g_object_set(G_OBJECT(settings
), "default-font-size", DEFAULT_FONT_SIZE
, NULL
);
2101 g_object_set(G_OBJECT(settings
), "enable-scripts", enablePlugins
, NULL
);
2102 g_object_set(G_OBJECT(settings
), "enable-plugins", enablePlugins
, NULL
);
2103 g_object_set(G_OBJECT(settings
), "enable-java-applet", enableJava
, NULL
);
2104 g_object_set(G_OBJECT(settings
), "enable-page-cache", enablePagecache
, NULL
);
2105 filename
= g_strdup_printf(USER_STYLESHEET
);
2106 filename
= g_strdup_printf("file://%s", filename
);
2107 g_object_set(G_OBJECT(settings
), "user-stylesheet-uri", filename
, NULL
);
2108 g_object_set(G_OBJECT(settings
), "user-agent", useragent
, NULL
);
2109 g_object_get(G_OBJECT(settings
), "zoom-step", &zoomstep
, NULL
);
2110 webkit_web_view_set_settings(webview
, settings
);
2113 if (use_proxy
== TRUE
) {
2114 filename
= (char *)g_getenv("http_proxy");
2115 if (filename
!= NULL
&& 0 < (len
= strlen(filename
))) {
2116 if (strstr(filename
, "://") == NULL
) {
2117 /* prepend http:// */
2118 new = g_malloc(sizeof("http://") + len
);
2119 strcpy(new, "http://");
2120 memcpy(&new[sizeof("http://") - 1], filename
, len
+ 1);
2121 proxy_uri
= soup_uri_new(new);
2123 proxy_uri
= soup_uri_new(filename
);
2125 g_object_set(session
, "proxy-uri", proxy_uri
, NULL
);
2132 #ifdef ENABLE_COOKIE_SUPPORT
2134 g_signal_connect_after(G_OBJECT(session
), "request-started", G_CALLBACK(new_generic_request
), NULL
);
2136 /* Accept-language header */
2137 g_object_set(G_OBJECT(session
), "accept-language", acceptlanguage
, NULL
);
2139 g_object_connect(G_OBJECT(window
),
2140 "signal::destroy", G_CALLBACK(window_destroyed_cb
), NULL
,
2143 g_object_connect(G_OBJECT(webview
),
2144 "signal::title-changed", G_CALLBACK(webview_title_changed_cb
), NULL
,
2145 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), NULL
,
2146 "signal::load-committed", G_CALLBACK(webview_load_committed_cb
), NULL
,
2147 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), NULL
,
2148 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_navigation_cb
), NULL
,
2149 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_new_window_cb
), NULL
,
2150 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), NULL
,
2151 "signal::download-requested", G_CALLBACK(webview_download_cb
), NULL
,
2152 "signal::key-press-event", G_CALLBACK(webview_keypress_cb
), NULL
,
2153 "signal::hovering-over-link", G_CALLBACK(webview_hoverlink_cb
), NULL
,
2154 "signal::console-message", G_CALLBACK(webview_console_cb
), NULL
,
2155 "signal::create-web-view", G_CALLBACK(webview_open_in_new_window_cb
), NULL
,
2156 "signal::event", G_CALLBACK(notify_event_cb
), NULL
,
2158 /* webview adjustment */
2159 g_object_connect(G_OBJECT(adjust_v
),
2160 "signal::value-changed", G_CALLBACK(webview_scroll_cb
), NULL
,
2163 g_object_connect(G_OBJECT(inputbox
),
2164 "signal::activate", G_CALLBACK(inputbox_activate_cb
), NULL
,
2165 "signal::key-press-event", G_CALLBACK(inputbox_keypress_cb
), NULL
,
2166 "signal::key-release-event", G_CALLBACK(inputbox_keyrelease_cb
), NULL
,
2167 #ifdef ENABLE_INCREMENTAL_SEARCH
2168 "signal::changed", G_CALLBACK(inputbox_changed_cb
), NULL
,
2172 g_signal_connect(G_OBJECT(inspector
),
2173 "inspect-web-view", G_CALLBACK(inspector_inspect_web_view_cb
), NULL
);
2176 #ifdef ENABLE_COOKIE_SUPPORT
2180 if (file_cookie_jar
)
2181 g_object_unref(file_cookie_jar
);
2183 if (session_cookie_jar
)
2184 g_object_unref(session_cookie_jar
);
2186 session_cookie_jar
= soup_cookie_jar_new();
2188 cookie_store
= g_strdup_printf(COOKIES_STORAGE_FILENAME
);
2192 g_signal_connect(G_OBJECT(file_cookie_jar
), "changed",
2193 G_CALLBACK(update_cookie_jar
), NULL
);
2198 /* TA: XXX - we should be using this callback for any header-requests we
2199 * receive (hence the name "new_generic_request" -- but for now, its use
2200 * is limited to handling cookies.
2203 new_generic_request(SoupSession
*session
, SoupMessage
*soup_msg
, gpointer unused
) {
2204 SoupMessageHeaders
*soup_msg_h
;
2206 const char *cookie_str
;
2208 soup_msg_h
= soup_msg
->request_headers
;
2209 soup_message_headers_remove(soup_msg_h
, "Cookie");
2210 uri
= soup_message_get_uri(soup_msg
);
2211 if( (cookie_str
= get_cookies(uri
)) )
2212 soup_message_headers_append(soup_msg_h
, "Cookie", cookie_str
);
2214 g_signal_connect_after(G_OBJECT(soup_msg
), "got-headers", G_CALLBACK(handle_cookie_request
), NULL
);
2220 get_cookies(SoupURI
*soup_uri
) {
2221 const char *cookie_str
;
2223 cookie_str
= soup_cookie_jar_get_cookies(file_cookie_jar
, soup_uri
, TRUE
);
2229 handle_cookie_request(SoupMessage
*soup_msg
, gpointer unused
)
2231 GSList
*resp_cookie
= NULL
;
2234 for(resp_cookie
= soup_cookies_from_response(soup_msg
);
2236 resp_cookie
= g_slist_next(resp_cookie
))
2238 SoupDate
*soup_date
;
2239 cookie
= soup_cookie_copy((SoupCookie
*)resp_cookie
->data
);
2241 if (cookie_timeout
&& cookie
->expires
== NULL
) {
2242 soup_date
= soup_date_new_from_time_t(time(NULL
) + cookie_timeout
* 10);
2243 soup_cookie_set_expires(cookie
, soup_date
);
2245 soup_cookie_jar_add_cookie(file_cookie_jar
, cookie
);
2252 update_cookie_jar(SoupCookieJar
*jar
, SoupCookie
*old
, SoupCookie
*new)
2255 /* Nothing to do. */
2260 copy
= soup_cookie_copy(new);
2262 soup_cookie_jar_add_cookie(session_cookie_jar
, copy
);
2268 load_all_cookies(void)
2270 file_cookie_jar
= soup_cookie_jar_text_new(cookie_store
, COOKIES_STORAGE_READONLY
);
2272 /* Put them back in the session store. */
2273 GSList
*cookies_from_file
= soup_cookie_jar_all_cookies(file_cookie_jar
);
2275 for (; cookies_from_file
;
2276 cookies_from_file
= cookies_from_file
->next
)
2278 soup_cookie_jar_add_cookie(session_cookie_jar
, cookies_from_file
->data
);
2281 soup_cookies_free(cookies_from_file
);
2290 /* Free up any nasty globals before exiting. */
2291 #ifdef ENABLE_COOKIE_SUPPORT
2293 g_free(cookie_store
);
2299 main(int argc
, char *argv
[]) {
2301 static char url
[256] = "";
2302 static gboolean ver
= false;
2303 static gboolean configfile_exists
= FALSE
;
2304 static const char *cfile
= NULL
;
2305 char *searchengines_file
;
2306 static GOptionEntry opts
[] = {
2307 { "version", 'v', 0, G_OPTION_ARG_NONE
, &ver
, "print version", NULL
},
2308 { "embed", 'e', 0, G_OPTION_ARG_STRING
, &winid
, "embedded", NULL
},
2309 { "configfile", 'c', 0, G_OPTION_ARG_STRING
, &cfile
, "config file", NULL
},
2315 /* command line argument: version */
2316 if (!gtk_init_with_args(&argc
, &argv
, "[<uri>]", opts
, NULL
, &err
)) {
2317 g_printerr("can't init gtk: %s\n", err
->message
);
2319 return EXIT_FAILURE
;
2323 printf("%s\n", INTERNAL_VERSION
);
2324 return EXIT_SUCCESS
;
2328 configfile
= g_strdup_printf(cfile
);
2330 configfile
= g_strdup_printf(RCFILE
);
2332 if (!g_thread_supported())
2333 g_thread_init(NULL
);
2336 embed
= atoi(winid
);
2341 #ifdef ENABLE_COOKIE_SUPPORT
2345 /* Check if the specified file exists. */
2346 /* And only warn the user, if they explicitly asked for a config on the
2349 if (!(access(configfile
, F_OK
) == 0) && cfile
) {
2352 feedback_str
= g_strdup_printf("Config file '%s' doesn't exist", cfile
);
2353 give_feedback(feedback_str
);
2354 } else if ((access(configfile
, F_OK
) == 0))
2355 configfile_exists
= true;
2357 /* read config file */
2358 /* But only report errors if we failed, and the file existed. */
2359 if (!read_rcfile(configfile
) && configfile_exists
) {
2361 a
.s
= g_strdup_printf("Error in config file '%s'", configfile
);
2367 make_searchengines_list(searchengines
, LENGTH(searchengines
));
2369 /* read searchengines. */
2370 searchengines_file
= g_strdup_printf(SEARCHENGINES_STORAGE_FILENAME
);
2371 switch (read_searchengines(searchengines_file
)) {
2374 a
.s
= g_strdup_printf("Syntax error in searchengines file '%s'", searchengines_file
);
2377 case READING_FAILED
:
2379 a
.s
= g_strdup_printf("Could not read searchengines file '%s'", searchengines_file
);
2385 g_free(searchengines_file
);
2387 /* command line argument: URL */
2389 strncpy(url
, argv
[argc
- 1], 255);
2391 strncpy(url
, startpage
, 255);
2394 a
.i
= TargetCurrent
;
2401 return EXIT_SUCCESS
;