]> git.armaanb.net Git - chorizo.git/blobdiff - src/browser.c
Patch leaks
[chorizo.git] / src / browser.c
index 1d25b77db41240d0fd9b065a58a32c491ae1cdd5..5b71040154eddf0a83accf0ca5d1e57a144e6ed5 100644 (file)
@@ -81,6 +81,8 @@ struct MainWindow {
 } mw;
 
 gint clients = 0;
+struct Client **client_arr;
+
 int cooperative_pipe_fp = 0;
 gchar *search_text;
 
@@ -122,10 +124,9 @@ client_destroy(GtkWidget *widget, gpointer data) {
 
 void
 set_uri(const char *uri, struct Client *c) {
-    gtk_widget_grab_focus(c->location);
-    const char *goal = (strcmp(cfg.home_uri, uri) == 0) ? cfg.default_uri : uri;
-    gtk_entry_set_text(GTK_ENTRY(c->location), goal);
-    gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
+    if (!gtk_widget_is_focus(c->location)) {
+        gtk_entry_set_text(GTK_ENTRY(c->location), uri);
+    }
 }
 
 WebKitWebView *
@@ -201,20 +202,20 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
     GtkWidget *locbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
 
     c->jsbutton = gtk_toggle_button_new_with_label("JS");
+    gtk_widget_set_tooltip_text(c->jsbutton, "Toggle JavaScript execution");
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->jsbutton),
                                  !cfg.javascript_disabled);
     g_signal_connect(G_OBJECT(c->jsbutton), "toggled", G_CALLBACK(togglejs), c);
 
     c->imgbutton = gtk_toggle_button_new_with_label("IMG");
+    gtk_widget_set_tooltip_text(c->imgbutton, "Toggle image loading");
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->imgbutton),
                                  !cfg.images_disabled);
     g_signal_connect(G_OBJECT(c->imgbutton), "toggled", G_CALLBACK(toggleimg),
                      c);
 
     c->location = gtk_entry_new();
-    gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 5);
-    gtk_box_pack_end(GTK_BOX(locbox), c->jsbutton, FALSE, FALSE, 0);
-    gtk_box_pack_end(GTK_BOX(locbox), c->imgbutton, FALSE, FALSE, 0);
+    gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 0);
 
     if (cfg.private) {
         GtkWidget *privindicator = gtk_label_new("Private mode");
@@ -224,6 +225,9 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
         gtk_box_pack_end(GTK_BOX(locbox), privindicator, FALSE, FALSE, 5);
     }
 
+    gtk_box_pack_start(GTK_BOX(locbox), c->jsbutton, FALSE, FALSE, 5);
+    gtk_box_pack_start(GTK_BOX(locbox), c->imgbutton, FALSE, FALSE, 0);
+
     g_signal_connect(G_OBJECT(c->location), "key-press-event",
                      G_CALLBACK(key_location), c);
     g_signal_connect(G_OBJECT(c->location), "icon-release",
@@ -292,6 +296,9 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
     set_uri(uri, c);
 
     clients++;
+    client_arr = realloc(client_arr, (clients + 1) * sizeof(client_arr[0]));
+    client_arr[clients] = c;
+
     return WEBKIT_WEB_VIEW(c->web_view);
 }
 
@@ -320,8 +327,8 @@ cooperation_setup(void) {
         fprintf(stderr, __NAME__ ": Can't open FIFO at all.\n");
     } else {
         if (write(cooperative_pipe_fp, "", 0) == -1) {
-            /* Could not do an empty write to the FIFO which means there's no
-             * one listening. */
+            /* Could not do an empty write to the FIFO which means there's
+             * no one listening. */
             close(cooperative_pipe_fp);
             towatch = g_io_channel_new_file(fifopath, "r+", NULL);
             g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
@@ -423,9 +430,9 @@ changed_title(GObject *obj, GParamSpec *pspec, gpointer data) {
     gboolean mute = webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
     gchar *muted = (mute) ? "[m] " : "";
     sprintf(name, "%s%s", muted, t);
-
     gtk_label_set_text(GTK_LABEL(c->tablabel), name);
     g_free(name);
+
     gtk_widget_set_tooltip_text(c->tablabel, t);
     mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
 }
@@ -443,7 +450,7 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data) {
      * now. Not updating the location bar in this scenario is important,
      * because we would override the "WEB PROCESS CRASHED" message. */
     if (t != NULL && strlen(t) > 0) {
-        gtk_entry_set_text(GTK_ENTRY(c->location), t);
+        set_uri(t, c);
 
         if (cfg.history_file != NULL && !cfg.private) {
             fp = fopen(cfg.history_file, "a");
@@ -589,7 +596,8 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data) {
     /* This was taken almost verbatim from the example in WebKit's
      * documentation:
      *
-     * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html */
+     * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html
+     */
 
     js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
                                                       result, &err);
@@ -641,7 +649,7 @@ hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht,
     }
 
     if (!gtk_widget_is_focus(c->location))
-        gtk_entry_set_text(GTK_ENTRY(c->location), to_show);
+        set_uri(to_show, c);
 }
 
 void
@@ -726,6 +734,7 @@ init_default_web_context(void) {
         WebKitCookiePersistentStorage type =
             WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE;
         webkit_cookie_manager_set_persistent_storage(cm, fname, type);
+        g_free(fname);
     }
 
     const gchar *const languages[2] = {(const gchar *)cfg.spellcheck_language,
@@ -802,7 +811,12 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) {
                 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
                 return TRUE;
             } else if (def_key("location", GDK_KEY_t) == key) {
-                set_uri(uri, c);
+                gtk_widget_grab_focus(c->location);
+                const char *goal = (strcmp(cfg.home_uri, uri) == 0 || !uri)
+                                       ? cfg.default_uri
+                                       : uri;
+                gtk_entry_set_text(GTK_ENTRY(c->location), goal);
+                gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
                 return TRUE;
             } else if (def_key("print", GDK_KEY_Print) == key) {
                 WebKitPrintOperation *operation =
@@ -962,12 +976,11 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data) {
                 search_text = g_strdup(t + 2);
                 search(c, 0);
             } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
-                const char *engine = cfg.search_engine;
-                int len = strlen(engine) + strlen(t) - 2;
-                char *f = (char *)malloc(len);
-                snprintf(f, len + 1, "%s%s", engine, t + 2);
+                int len = strlen(cfg.search_engine) + strlen(t) - 2;
+                gchar *f = malloc(len + 1);
+                snprintf(f, len + 1, "%s%s", cfg.search_engine, t + 2);
                 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
-                free(f);
+                g_free(f);
             } else {
                 f = ensure_uri_scheme(t);
                 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
@@ -1064,6 +1077,7 @@ mainwindow_setup(void) {
     gchar *title = malloc(strlen(priv) + strlen(__NAME__) + 1);
     sprintf(title, "%s%s", __NAME__, priv);
     gtk_window_set_title(GTK_WINDOW(mw.win), title);
+    g_free(title);
 
     mw.notebook = gtk_notebook_new();
     gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
@@ -1173,17 +1187,20 @@ show_web_view(WebKitWebView *web_view, gpointer data) {
 void
 trust_user_certs(WebKitWebContext *wc) {
     GTlsCertificate *cert;
-    const gchar *basedir, *file, *absfile;
+    gchar *basedir, *absfile;
+    const gchar *file;
     GDir *dir;
 
     basedir = g_build_filename(g_get_user_data_dir(), __NAME__, "certs", NULL);
     dir = g_dir_open(basedir, 0, NULL);
+    g_free(basedir);
     if (dir != NULL) {
         file = g_dir_read_name(dir);
         while (file != NULL) {
             absfile = g_build_filename(g_get_user_data_dir(), __NAME__, "certs",
                                        file, NULL);
             cert = g_tls_certificate_new_from_file(absfile, NULL);
+            g_free(absfile);
             if (cert == NULL)
                 fprintf(stderr, __NAME__ ": Could not load trusted cert '%s'\n",
                         file);
@@ -1202,17 +1219,16 @@ get_ini(void) {
     config = g_key_file_new();
 
     // Load user config
-    if (!g_key_file_load_from_file(config,
-                                   g_build_filename(g_get_user_config_dir(),
-                                                    __NAME__, "chorizo.ini",
-                                                    NULL),
-                                   flags, NULL)) {
+    gchar *fname = g_build_filename(g_get_user_config_dir(), __NAME__,
+                                    "chorizo.ini", NULL);
+    if (!g_key_file_load_from_file(config, fname, flags, NULL)) {
         // Load global config
         if (!g_key_file_load_from_file(config, "/etc/chorizo.ini", flags,
                                        NULL)) {
             fprintf(stderr, "Could not load chorizo.ini");
         }
     }
+    g_free(fname);
     return config;
 }
 
@@ -1249,6 +1265,7 @@ main(int argc, char **argv) {
     downloadmanager_setup();
     mainwindow_setup();
 
+    client_arr = malloc(sizeof(struct Client *));
     if (optind >= argc) {
         client_new(cfg.home_uri, NULL, TRUE, TRUE);
     } else {
@@ -1259,5 +1276,9 @@ main(int argc, char **argv) {
     if (!cfg.cooperative_instances || cfg.cooperative_alone)
         gtk_main();
 
+    for (int i = 0; i < clients; i++) {
+        free(&(client_arr[i]));
+    }
+
     exit(EXIT_SUCCESS);
 }