Skip to content

Commit

Permalink
Refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
aarnt committed Nov 14, 2021
1 parent 979532d commit 731dac8
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 145 deletions.
14 changes: 0 additions & 14 deletions badwolf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1155,20 +1155,6 @@ notebookCb_switch__page(GtkNotebook *notebook, GtkWidget *page, guint page_num,

// TODO: Maybe find a better way to store the title
gtk_window_set_title(GTK_WINDOW(window->main_window), gtk_widget_get_tooltip_text(label));

/*set_kiosk_mode(browser);
gchar *location = strdup(gtk_entry_get_text(GTK_ENTRY(browser->location)));
if (strcmp(location, "about:blank") == 0)
{
//There is no site being displayed, let's focus the location bar
gtk_widget_grab_focus(browser->location);
}
else
{
//There is a site being displayed, so we can set the focus on webview
gtk_widget_grab_focus(GTK_WIDGET(browser->webView));
}*/
}

/*
Expand Down
276 changes: 148 additions & 128 deletions keybindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <glib/gi18n.h> /* _() */

static void open_site_on_new_tab(struct Window *window, const gchar *url, gboolean jsEnabled);
static void refreshWebView(WebKitWebView* webView);
static void goto_prev_tab(GtkNotebook *notebook);
static void goto_next_tab(GtkNotebook *notebook);
static void web_view_get_selected_text(WebKitWebView *web_view, struct Window *window);

static gboolean
about_dialogCb_activate_link(GtkAboutDialog *about_dialog, gchar *uri, gpointer user_data)
Expand Down Expand Up @@ -64,133 +68,6 @@ toggle_caret_browsing(WebKitWebView *webView)
webkit_web_view_set_settings(webView, settings);
}

/*
* Goto the previous tab in notebook. If current is the second one, goto the last tab!
*/
static void
goto_prev_tab(GtkNotebook *notebook)
{
gint npages = gtk_notebook_get_n_pages(notebook);
gint curr = gtk_notebook_get_current_page(notebook);
if (curr-1 == 0)
gtk_notebook_set_current_page(notebook, npages-1);
else gtk_notebook_prev_page(notebook);
}

/*
* Goto the next tab in notebook. If current is the last one, goto the second tab!
*/
static void
goto_next_tab(GtkNotebook *notebook)
{
gint npages = gtk_notebook_get_n_pages(notebook);
gint curr = gtk_notebook_get_current_page(notebook);
if (curr+1 == npages)
gtk_notebook_set_current_page(notebook, 1);
else gtk_notebook_next_page(notebook);
}

/*
* Removes a substring sub from a given string str
*/
gchar *
strremove(gchar *str, const gchar *sub)
{
size_t len = strlen(sub);
if (len > 0) {
char *p = str;
while ((p = strstr(p, sub)) != NULL)
{
memmove(p, p + len, strlen(p + len) + 1);
}
}
return str;
}

/*
* Retrives given system LANGUAGE code, based on LC_TIME environment var
*/
gchar *
getLangCode()
{
gchar *result = "";
result = strremove(getenv("LC_TIME"), ".UTF-8");

char *pos = strchr(result, '_');
if(pos != NULL)
{
result = strremove(strdup(result), pos);
}

return result;
}

static void
web_view_javascript_get_selected_text_finished(GObject *object,
GAsyncResult *result,
gpointer user_data)
{
WebKitJavascriptResult *js_result;
JSCValue *value;
GError *error = NULL;
struct Window *window = (struct Window*)user_data;
gchar *lang = NULL;

js_result = webkit_web_view_run_javascript_finish (WEBKIT_WEB_VIEW (object), result, &error);
if (!js_result) {
g_warning ("Error running javascript: %s", error->message);
g_error_free (error);
return;
}

value = webkit_javascript_result_get_js_value (js_result);
if(jsc_value_is_undefined(value))
{
printf("Value is undefined!\n\n");
}
else if(jsc_value_is_string (value))
{
JSCException *exception;
gchar *str_value;

str_value = jsc_value_to_string (value);
exception = jsc_context_get_exception (jsc_value_get_context (value));

if (!exception)
{
lang = getLangCode();

gchar gtrans[64]="";
gchar *gsite="https://translate.google.com/?sl=auto&tl=";
strcat(gtrans, gsite);
strcat(gtrans, lang);
strcat(gtrans, "&q=");

char **split = g_strsplit(str_value, " ", -1);
g_free(str_value);
str_value = g_strjoinv("+", split);
g_strfreev(split);

gchar *url=g_strconcat(gtrans, str_value, NULL);
open_site_on_new_tab(window, url, true);
g_free(url);
}

g_free (str_value);

webkit_javascript_result_unref (js_result);
}
}

static void
web_view_get_selected_text(WebKitWebView *web_view, struct Window *window)
{
const gchar *script = "window.getSelection().toString();";

webkit_web_view_run_javascript (WEBKIT_WEB_VIEW(web_view),
script, NULL, web_view_javascript_get_selected_text_finished, window);
}

/* commonCb_key_press_event: Global callback for keybindings
*
* These shortcuts should be avoided as much as possible:
Expand Down Expand Up @@ -361,7 +238,7 @@ commonCb_key_press_event(struct Window *window, GdkEvent *event, struct Client *
{
case GDK_KEY_Escape: webkit_web_view_stop_loading(browser->webView); return TRUE;
case GDK_KEY_F4: toggle_dark_mode(browser->webView); return TRUE;
case GDK_KEY_F5: webkit_web_view_reload(browser->webView); return TRUE;
case GDK_KEY_F5: refreshWebView(browser->webView); return TRUE;
case GDK_KEY_F7: toggle_caret_browsing(browser->webView); return TRUE;
case GDK_KEY_F11: toggle_kiosk_mode(browser); return TRUE;
case GDK_KEY_F12:
Expand Down Expand Up @@ -422,6 +299,16 @@ main_windowCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_

//-------------------------------- LRRH changes -------------------------------------//

/*
Whenever user presses F5 key...
*/
static void
refreshWebView(WebKitWebView* webView)
{
webkit_web_view_reload(webView);
set_dark_mode(webView);
}

/*
* Opens given url on new tab and focus webview widget
*/
Expand All @@ -442,3 +329,136 @@ open_site_on_new_tab(struct Window *window, const gchar *url, gboolean jsEnabled

gtk_widget_grab_focus(GTK_WIDGET(nbrowser->webView));
}

/*
* Goto the previous tab in notebook. If current is the second one, goto the last tab!
*/
static void
goto_prev_tab(GtkNotebook *notebook)
{
gint npages = gtk_notebook_get_n_pages(notebook);
gint curr = gtk_notebook_get_current_page(notebook);
if (curr-1 == 0)
gtk_notebook_set_current_page(notebook, npages-1);
else gtk_notebook_prev_page(notebook);
}

/*
* Goto the next tab in notebook. If current is the last one, goto the second tab!
*/
static void
goto_next_tab(GtkNotebook *notebook)
{
gint npages = gtk_notebook_get_n_pages(notebook);
gint curr = gtk_notebook_get_current_page(notebook);
if (curr+1 == npages)
gtk_notebook_set_current_page(notebook, 1);
else gtk_notebook_next_page(notebook);
}

/*
* Removes a substring sub from a given string str
*/
gchar *
strremove(gchar *str, const gchar *sub)
{
size_t len = strlen(sub);
if (len > 0) {
char *p = str;
while ((p = strstr(p, sub)) != NULL)
{
memmove(p, p + len, strlen(p + len) + 1);
}
}
return str;
}

/*
* Retrives given system LANGUAGE code, based on LC_TIME environment var
*/
gchar *
getLangCode()
{
gchar *result = "";
result = strremove(getenv("LC_TIME"), ".UTF-8");

char *pos = strchr(result, '_');
if(pos != NULL)
{
result = strremove(strdup(result), pos);
}

return result;
}

/*
Opens the Google translate site to get a translated version of the user selected content
*/
static void
web_view_javascript_get_selected_text_finished(GObject *object,
GAsyncResult *result,
gpointer user_data)
{
WebKitJavascriptResult *js_result;
JSCValue *value;
GError *error = NULL;
struct Window *window = (struct Window*)user_data;
gchar *lang = NULL;

js_result = webkit_web_view_run_javascript_finish (WEBKIT_WEB_VIEW (object), result, &error);
if (!js_result) {
g_warning ("Error running javascript: %s", error->message);
g_error_free (error);
return;
}

value = webkit_javascript_result_get_js_value (js_result);
if(jsc_value_is_undefined(value))
{
printf("Value is undefined!\n\n");
}
else if(jsc_value_is_string (value))
{
JSCException *exception;
gchar *str_value;

str_value = jsc_value_to_string (value);
exception = jsc_context_get_exception (jsc_value_get_context (value));

if (!exception)
{
lang = getLangCode();

gchar gtrans[64]="";
gchar *gsite="https://translate.google.com/?sl=auto&tl=";
strcat(gtrans, gsite);
strcat(gtrans, lang);
strcat(gtrans, "&q=");

char **split = g_strsplit(str_value, " ", -1);
g_free(str_value);
str_value = g_strjoinv("+", split);
g_strfreev(split);

gchar *url=g_strconcat(gtrans, str_value, NULL);
open_site_on_new_tab(window, url, true);
g_free(url);
}

g_free (str_value);

webkit_javascript_result_unref (js_result);
}
}

/*
Calls a javascript script to execute some computation over the user selected text
*/
static void
web_view_get_selected_text(WebKitWebView *web_view, struct Window *window)
{
const gchar *script = "window.getSelection().toString();";

webkit_web_view_run_javascript (WEBKIT_WEB_VIEW(web_view),
script, NULL, web_view_javascript_get_selected_text_finished, window);
}
6 changes: 3 additions & 3 deletions po/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Badwolf 1.0.3+gb23349c.main\n"
"Project-Id-Version: Badwolf 1.0.3+g979532d.main\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2021-11-14 08:38-0300\n"
"POT-Creation-Date: 2021-11-14 09:19-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -37,7 +37,7 @@ msgstr ""
msgid "%02i:%02i:%02i Downloading…"
msgstr ""

#: keybindings.c:31
#: keybindings.c:35
#, c-format
msgid ""
"Another tiny & fast & functional WebKitGTK browser\n"
Expand Down

0 comments on commit 731dac8

Please sign in to comment.