gchar *search_text;
gchar *fifopath;
char **closed_tabs;
+size_t num_closed = 0;
+
+void
+allocfail(void)
+{
+ fprintf(stderr, "chorizo: fatal: alloc failed\n");
+ exit(EXIT_FAILURE);
+}
void
togglejs(GtkButton *jsbutton, gpointer data)
idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
if (idx == -1)
- fprintf(stderr, "chorizo: Tab index was -1, bamboozled\n");
+ fprintf(stderr, "chorizo: warning: tab index was -1\n");
else
gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
if (!cfg.private && WEBKIT_IS_WEB_VIEW(c->web_view)) {
- int len = sizeof(closed_tabs) / sizeof(char *);
const char *uri =
webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
- closed_tabs = (char **)realloc(closed_tabs,
- len * sizeof(closed_tabs[0]));
- closed_tabs[len] = strdup(uri);
+
+ // TODO: Shift everything left if over certain amount
+ num_closed++;
+ if (num_closed > cfg_max_tabs_closed) {
+ memmove(closed_tabs, closed_tabs,
+ cfg_max_tabs_closed - 1);
+ num_closed = cfg_max_tabs_closed;
+ } else {
+ closed_tabs =
+ realloc(closed_tabs,
+ num_closed * sizeof(closed_tabs[0]));
+ if (!closed_tabs) allocfail();
+ }
+ closed_tabs[num_closed - 1] = strdup(uri);
}
free(c);
return NULL;
}
c = calloc(1, sizeof(struct Client));
- if (!c) {
- fprintf(stderr, "chorizo: fatal: calloc failed\n");
- exit(EXIT_FAILURE);
- }
+ if (!c) allocfail();
c->focus_new_tab = focus_tab;
if (related_wv == NULL) {
webkit_user_script_unref(wkscript);
}
g_free(path);
- if (source)
- g_free(source);
+ if (source) g_free(source);
}
g_dir_close(dir);
}
c->settings =
webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
webkit_settings_set_enable_javascript(c->settings, cfg_js_default);
- if (cfg.verbose) {
+ if (cfg.verbose)
webkit_settings_set_enable_write_console_messages_to_stdout(
c->settings, true);
- }
webkit_settings_set_enable_developer_extras(c->settings, TRUE);
g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
clients++;
client_arr = realloc(client_arr, (clients + 1) * sizeof(client_arr[0]));
+ if (!client_arr) allocfail();
+
client_arr[clients] = c;
- if (clients == 1 || uri == NULL)
- gtk_widget_grab_focus(c->location);
+ if (clients == 1 || uri == NULL) gtk_widget_grab_focus(c->location);
return WEBKIT_WEB_VIEW(c->web_view);
}
size_t len;
snprintf(tmp, sizeof(tmp), "%s", dir);
len = strlen(tmp);
- if (tmp[len - 1] == '/')
- tmp[len - 1] = 0;
+ if (tmp[len - 1] == '/') tmp[len - 1] = 0;
for (p = tmp + 1; *p; p++)
if (*p == '/') {
*p = 0;
mkdirp(dirname(fifopath), 0600);
g_free(fifofilename);
- if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
- mkfifo(fifopath, 0600);
+ if (!g_file_test(fifopath, G_FILE_TEST_EXISTS)) mkfifo(fifopath, 0600);
cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
if (!cooperative_pipe_fp) {
- fprintf(stderr, "chorizo: Can't open FIFO at all.\n");
+ fprintf(stderr, "chorizo: error: can't open FIFO\n");
} else {
if (write(cooperative_pipe_fp, "", 0) == -1) {
/*
t = t[0] == 0 ? u : t;
gchar *name = malloc(strlen(t) + 4);
+ if (!name) allocfail();
+
gboolean mute =
webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
gchar *muted = (mute) ? "[m] " : "";
fprintf(fp, "%s\n", t);
fclose(fp);
} else {
- perror("chorizo: Error opening history file");
+ perror("chorizo: error: could not open history file");
}
}
g_free(history_file);
js_result = webkit_web_view_run_javascript_finish(
WEBKIT_WEB_VIEW(object), result, &err);
if (!js_result) {
- fprintf(stderr, "chorizo: Error running javascript: %s\n",
+ fprintf(stderr,
+ "chorizo: error: error running javascript: %s\n",
err->message);
g_error_free(err);
return;
jsc_context_get_exception(jsc_value_get_context(value));
if (exception != NULL) {
fprintf(stderr,
- "chorizo: Error running javascript: %s\n",
+ "chorizo: warning: error running javascript: %s\n",
jsc_exception_get_message(exception));
} else {
c->feed_html = str_value;
c->hover_uri = NULL;
}
- if (!gtk_widget_is_focus(c->location))
- set_uri(to_show, c);
+ if (!gtk_widget_is_focus(c->location)) set_uri(to_show, c);
}
void
WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
WebKitFindController *fc =
webkit_web_view_get_find_controller(web_view);
- if (search_text == NULL)
- return;
+ if (search_text == NULL) return;
switch (direction) {
case 0:
client_new(cfg_home_uri, NULL, TRUE, TRUE);
return TRUE;
} else if (GDK_KEY_bracketleft == key) {
- if (!closed_tabs)
- return TRUE;
- int len = sizeof(closed_tabs) / sizeof(char *);
- if (len < 1)
- return TRUE;
- client_new(closed_tabs[len], NULL, TRUE, TRUE);
- free(closed_tabs[len]);
- closed_tabs = (char **)realloc(
+ if (num_closed == 0) return TRUE;
+ client_new(closed_tabs[num_closed - 1], NULL,
+ TRUE, TRUE);
+ num_closed--;
+ closed_tabs = realloc(
closed_tabs,
- (len - 1) * sizeof(closed_tabs[0]));
+ num_closed * sizeof(closed_tabs[0]));
+ if (!closed_tabs) allocfail();
return TRUE;
} else if (GDK_KEY_i == key) {
gtk_notebook_next_page(
{
struct Client *c = (struct Client *)data;
const gchar *t;
- if (key_common(widget, event, data))
- return TRUE;
+ if (key_common(widget, event, data)) return TRUE;
if (event->type == GDK_KEY_PRESS) {
int key = ((GdkEventKey *)event)->keyval;
gtk_widget_grab_focus(c->web_view);
t = gtk_entry_get_text(GTK_ENTRY(c->location));
if (t != NULL && t[0] == 's' && t[1] == '/') {
- if (search_text != NULL)
- g_free(search_text);
+ if (search_text != NULL) g_free(search_text);
search_text = g_strdup(t + 2);
search(c, 0);
} else if (t != NULL && t[0] == 'w' && t[1] == '/') {
int len = strlen(cfg_search_engine) +
strlen(t) - 2;
gchar *f = malloc(len + 1);
+ if (!f) allocfail();
snprintf(f, len + 1, "%s%s", cfg_search_engine,
t + 2);
webkit_web_view_load_uri(
struct Client *c = (struct Client *)data;
gdouble dx, dy;
gfloat z;
- if (key_common(widget, event, data))
- return TRUE;
+ if (key_common(widget, event, data)) return TRUE;
if (event->type == GDK_KEY_PRESS) {
if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape) {
gchar *priv = (cfg.private) ? "-private" : "";
gchar *title = malloc(strlen(priv) + 7);
+ if (!title) allocfail();
sprintf(title, "%s%s", "chorizo", priv);
gtk_window_set_title(GTK_WINDOW(mw.win), title);
g_free(title);
GtkWidget *child, *widg, *tablabel;
const gchar *text;
child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
- if (child == NULL)
- return;
+ if (child == NULL) return;
widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg),
g_free(absfile);
if (cert == NULL)
fprintf(stderr,
- "chorizo: Could not load trusted cert '%s'\n",
+ "chorizo: warning: could not load trusted cert: %s\n",
file);
else
webkit_web_context_allow_tls_certificate_for_host(
}
}
+void
+version(void)
+{
+ printf("%s %s\n", "chorizo", VERSION);
+}
+
int
main(int argc, char **argv)
{
//TODO:pretty this
cfg.noncooperative_instances = FALSE;
cfg.cooperative_alone = TRUE;
+ closed_tabs = malloc(0);
+ if (!closed_tabs) allocfail();
while ((opt = getopt(argc, argv, "cpvV")) != -1) {
switch (opt) {
cfg.verbose = TRUE;
break;
case 'V':
- printf("%s %s\n", "chorizo", VERSION);
+ version();
exit(0);
default:
fprintf(stderr,
- "Usage: chorizo [OPTION]... [URI]...\n");
+ "usage: chorizo [OPTION]... [URI]...\n");
exit(EXIT_FAILURE);
}
}
+ if (cfg.verbose) version();
+
gtk_init(&argc, &argv);
//Keep clipboard contents after program closes
gtk_clipboard_store(gtk_clipboard_get_for_display(
gdk_display_get_default(), GDK_SELECTION_CLIPBOARD));
- if (!cfg.noncooperative_instances)
- cooperation_setup();
+ if (!cfg.noncooperative_instances) cooperation_setup();
if (cfg.noncooperative_instances || cfg.cooperative_alone)
init_default_web_context();
mainwindow_setup();
client_arr = malloc(sizeof(struct Client *));
+ if (!client_arr) allocfail();
+
if (optind >= argc) {
client_new(cfg_home_uri, NULL, TRUE, TRUE);
} else {
gtk_main();
remove(fifopath);
}
- for (int i = 0; i < clients; i++) {
- free(&(client_arr[i]));
- }
+ for (int i = 0; i < clients; i++) { free(&(client_arr[i])); }
exit(EXIT_SUCCESS);
}
GtkToolButton *tb;
WebKitDownload *download;
};
+
gboolean
key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
{
webkit_download_cancel(payload->download);
}
+const char *
+download_get_path(struct DownloadItem *payload)
+{
+ const char *path = webkit_download_get_destination(payload->download);
+ // Offset by 7 to remove "file://"
+ return path += 7;
+}
+
void
-download_remove(GtkMenuItem *tb, gpointer data)
+download_hide(GtkMenuItem *tb, gpointer data)
{
struct DownloadItem *payload = data;
g_object_unref(payload->download);
gtk_widget_destroy(GTK_WIDGET(payload->tb));
}
+void
+download_delete(GtkMenuItem *tb, gpointer data)
+{
+ remove(download_get_path((struct DownloadItem *)data));
+ download_hide(tb, data);
+}
+
void
download_copy_url(GtkMenuItem *tb, gpointer data)
{
void
download_copy_path(GtkMenuItem *tb, gpointer data)
{
- struct DownloadItem *payload = data;
- const gchar *path = webkit_download_get_destination(payload->download);
+ const char *path = download_get_path((struct DownloadItem *)data);
gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
path + 7, strlen(path) - 7);
- //Offset by 7 to remove "file://"
}
void
gtk_widget_show(option);
gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), option);
} else {
- option = gtk_menu_item_new_with_label("Remove download");
+ option =
+ gtk_menu_item_new_with_label("Hide download from list");
+ g_signal_connect(G_OBJECT(option), "activate",
+ G_CALLBACK(download_hide), data);
+ gtk_widget_show(option);
+ gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), option);
+
+ option = gtk_menu_item_new_with_label("Delete downloaded file");
g_signal_connect(G_OBJECT(option), "activate",
- G_CALLBACK(download_remove), data);
+ G_CALLBACK(download_delete), data);
gtk_widget_show(option);
gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), option);
gboolean
downloadmanager_delete(GtkWidget *obj, gpointer data)
{
- if (!quit_if_nothing_active())
- gtk_widget_hide(dm.win);
+ if (!quit_if_nothing_active()) gtk_widget_hide(dm.win);
return TRUE;
}