From 96aa53b88efde5fad902183eb0c32ca68af76adc Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Sat, 26 Jan 2008 11:01:15 -0800 Subject: [PATCH] [Terminal] Use the configuration system to retrieve startup values This hooks into the configuration system to get all of the property startup values, currently out of the defaults in the configuration system. --- tilda-terminal.c | 258 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 246 insertions(+), 12 deletions(-) diff --git a/tilda-terminal.c b/tilda-terminal.c index 14567da..16ac539 100644 --- a/tilda-terminal.c +++ b/tilda-terminal.c @@ -1,10 +1,36 @@ #include "tilda.h" #include "tilda-terminal.h" +#include "tilda-config.h" #include "tilda-terminal-dbus-glue.h" +#include + #define DINGUS1 "(((news|telnet|nttp|file|http|ftp|https)://)|(www|ftp)[-A-Za-z0-9]*\\.)[-A-Za-z0-9\\.]+(:[0-9]*)?" #define DINGUS2 "(((news|telnet|nttp|file|http|ftp|https)://)|(www|ftp)[-A-Za-z0-9]*\\.)[-A-Za-z0-9\\.]+(:[0-9]*)?/[-A-Za-z0-9_\\$\\.\\+\\!\\*\\(\\),;:@&=\\?/~\\#\\%]*[^]'\\.}>\\) ,\\\"]" +typedef enum +{ + HOLD_OPEN, + RESTART_COMMAND, + EXIT, +} TildaExitActions; + +typedef enum +{ + NOT_DISPLAYED, + AFTER_INITIAL, + BEFORE_INITIAL, + REPLACE_INITIAL, +} TildaDynamicTitleActions; + +typedef enum +{ + /* NOT_DISPLAYED */ + LEFT = 1, + RIGHT, +} TildaScrollbarPositions; + + static void tilda_terminal_dbus_register_object (TildaTerminal *tt) @@ -135,22 +161,18 @@ tilda_terminal_child_exited_cb (GtkWidget *widget, gpointer data) TildaTerminal *self = TILDA_TERMINAL(data); - /* These can stay here. They don't need to go into a header because - * they are only used at this point in the code. */ - enum exit_actions { HOLD_TERMINAL_OPEN, RESTART_COMMAND, EXIT_TERMINAL }; - /* Check the user's preference for what to do when the child terminal * is closed. Take the appropriate action */ switch (self->exit_action) { - case EXIT_TERMINAL: + case EXIT: tilda_window_remove_terminal (TILDA_WINDOW(self->parent_window), self->number); break; case RESTART_COMMAND: vte_terminal_feed (VTE_TERMINAL(self->vte_term), "\r\n\r\n", 4); tilda_terminal_start_shell (self); break; - case HOLD_TERMINAL_OPEN: + case HOLD_OPEN: break; default: break; @@ -174,7 +196,6 @@ tilda_terminal_window_title_changed_cb (GtkWidget *widget, gpointer data) const gchar *vte_title; gchar *new_title; - enum dynamic_titles { NOT_DISPLAYED, AFTER_INITIAL, BEFORE_INITIAL, REPLACE_INITIAL }; label = gtk_notebook_get_tab_label (GTK_NOTEBOOK(parent_window->notebook), self->hbox); /* If we aren't using a dynamic title -- NOT_DISPLAYED -- then just @@ -329,27 +350,212 @@ tilda_terminal_set_scrollbar_position (TildaTerminal *self) debug_enter (); debug_assert (TILDA_IS_TERMINAL(self)); - enum scrollbar_positions { DISABLED, LEFT, RIGHT }; switch (self->scrollbar_position) { - case LEFT: + case RIGHT: gtk_box_reorder_child (GTK_BOX(self->hbox), self->scrollbar, 0); gtk_widget_show (self->scrollbar); break; - case RIGHT: + case LEFT: gtk_box_reorder_child (GTK_BOX(self->hbox), self->scrollbar, 1); gtk_widget_show (self->scrollbar); break; default: debug_printf ("ERROR: Bad scrollbar position\n"); - case DISABLED: + case NOT_DISPLAYED: gtk_widget_hide (self->scrollbar); break; } } +/******************************************************************************* + * All configuration subsystem code is below + ******************************************************************************/ + +/** + * Lookup a setting in the config file for this TildaTerminal. + * + * The returned string MUST be g_strdup()'d if you want to actually use + * it as a value. It is owned by the config system. You MUST NOT g_free() it. + */ +static gchar * +tilda_terminal_lookup_from_config (TildaTerminal *self, const gchar key[]) +{ + debug_enter (); + debug_assert (TILDA_IS_TERMINAL(self)); + debug_assert (key != NULL); + + GError *error = NULL; + gchar *group_name; + gpointer value; + + /* Do the bottom-most lookup */ + group_name = g_strdup_printf ("Window%d/Terminal%d", + TILDA_WINDOW(self->parent_window)->number, + self->number); + value = g_key_file_get_string (config_userprefs, group_name, key, &error); + g_free (group_name); + + if (!error) + return value; + else + g_clear_error (&error); + + /* Do the next highest lookup */ + group_name = g_strdup_printf ("Window%d", + TILDA_WINDOW(self->parent_window)->number); + value = g_key_file_get_string (config_userprefs, group_name, key, &error); + g_free (group_name); + + if (!error) + return value; + else + g_clear_error (&error); + + /* Do the global lookup */ + value = g_key_file_get_string (config_userprefs, "Global", key, &error); + + if (!error) + return value; + else + g_clear_error (&error); + + /* Look in the defaults */ + if (!g_hash_table_lookup_extended (config_defaults, key, NULL, &value)) + { + /* If this happened, the developers forgot to set the default for + * a key they added. Please email them. */ + g_critical ("Error: unable to find the default for key=%s\n", key); + exit (1); + } + + /* Return the default key */ + return value; +} + +/** + * Set one of TildaTerminal's integer properties from the config file or defaults. + */ +static void +tilda_terminal_config_int_property (TildaTerminal *self, const gchar *property) +{ + debug_enter (); + debug_assert (TILDA_IS_TERMINAL(self)); + debug_assert (property != NULL); + + gint config_value; + gchar *config_value_raw; + + config_value_raw = tilda_terminal_lookup_from_config (self, property); + config_value = atoi (config_value_raw); + g_object_set (G_OBJECT(self), property, config_value, NULL); +} + +static void +tilda_terminal_config_enum_property (TildaTerminal *self, const gchar *property) +{ + debug_enter (); + debug_assert (TILDA_IS_TERMINAL(self)); + debug_assert (property != NULL); + + gint config_value; + gchar *config_value_raw; + + /* Copy, then strip spaces off of the string from the config */ + config_value_raw = g_strstrip (g_strdup (tilda_terminal_lookup_from_config (self, property))); + +begin_parsing: + + /* Just try all of the possible enums */ + if (g_ascii_strcasecmp (config_value_raw, "LEFT") == 0) + config_value = LEFT; + else if (g_ascii_strcasecmp (config_value_raw, "RIGHT") == 0) + config_value = RIGHT; + else if (g_ascii_strcasecmp (config_value_raw, "HOLD-OPEN") == 0) + config_value = HOLD_OPEN; + else if (g_ascii_strcasecmp (config_value_raw, "RESTART-COMMAND") == 0) + config_value = RESTART_COMMAND; + else if (g_ascii_strcasecmp (config_value_raw, "EXIT") == 0) + config_value = EXIT; + else if (g_ascii_strcasecmp (config_value_raw, "VTE-ERASE-AUTO") == 0) + config_value = VTE_ERASE_AUTO; + else if (g_ascii_strcasecmp (config_value_raw, "VTE-ERASE-ASCII-BACKSPACE") == 0) + config_value = VTE_ERASE_ASCII_BACKSPACE; + else if (g_ascii_strcasecmp (config_value_raw, "VTE-ERASE-ASCII-DELETE") == 0) + config_value = VTE_ERASE_ASCII_DELETE; + else if (g_ascii_strcasecmp (config_value_raw, "VTE-ERASE-DELETE-SEQUENCE") == 0) + config_value = VTE_ERASE_DELETE_SEQUENCE; + else if (g_ascii_strcasecmp (config_value_raw, "NOT-DISPLAYED") == 0) + config_value = NOT_DISPLAYED; + else if (g_ascii_strcasecmp (config_value_raw, "AFTER-INITIAL") == 0) + config_value = AFTER_INITIAL; + else if (g_ascii_strcasecmp (config_value_raw, "BEFORE-INITIAL") == 0) + config_value = BEFORE_INITIAL; + else if (g_ascii_strcasecmp (config_value_raw, "REPLACE-INITIAL") == 0) + config_value = REPLACE_INITIAL; + else + { + g_critical ("Unable to parse: '%s' as an enum\n", config_value_raw); + + /* Use the default -- which I sure hope is valid (famous last words) */ + config_value_raw = g_hash_table_lookup (config_defaults, property); + goto begin_parsing; + } + + /* Free the value from the config system */ + g_free (config_value_raw); + + g_object_set (G_OBJECT(self), property, config_value, NULL); +} + +static void +tilda_terminal_config_string_property (TildaTerminal *self, const gchar *property) +{ + debug_enter (); + debug_assert (TILDA_IS_TERMINAL(self)); + debug_assert (property != NULL); + + gchar *config_value_raw; + + config_value_raw = tilda_terminal_lookup_from_config (self, property); + + /* The property system g_strdup()s strings for us! */ + g_object_set (G_OBJECT(self), property, config_value_raw, NULL); +} + +static void +tilda_terminal_config_boolean_property (TildaTerminal *self, const gchar *property) +{ + debug_enter (); + debug_assert (TILDA_IS_TERMINAL(self)); + debug_assert (property != NULL); + + gboolean config_value; + gchar *config_value_raw; + + config_value_raw = tilda_terminal_lookup_from_config (self, property); + +begin_parsing: + + if (g_ascii_strcasecmp (config_value_raw, "true") == 0) + config_value = TRUE; + else if (g_ascii_strcasecmp (config_value_raw, "false") == 0) + config_value = FALSE; + else + { + g_critical ("Unable to parse: '%s' as a boolean\n", config_value_raw); + + /* Use the default -- which I sure hope is valid (famous last words) */ + config_value_raw = g_hash_table_lookup (config_defaults, property); + goto begin_parsing; + } + + g_object_set (G_OBJECT(self), property, config_value, NULL); +} + + /******************************************************************************* * All GObject stuff is below. You probably don't need to change this... ******************************************************************************/ @@ -738,7 +944,35 @@ tilda_terminal_constructor (GType type, g_signal_connect (G_OBJECT(self->vte_term), "button-press-event", G_CALLBACK(tilda_terminal_button_press_cb), self); - tilda_terminal_start_shell (self); + /* Setup all of the defaults from the config file */ + tilda_terminal_config_string_property (self, "background-image"); + tilda_terminal_config_string_property (self, "shell"); + tilda_terminal_config_string_property (self, "font"); + tilda_terminal_config_string_property (self, "title"); + tilda_terminal_config_string_property (self, "working-directory"); + tilda_terminal_config_string_property (self, "web-browser"); + + tilda_terminal_config_int_property (self, "scrollback-lines"); + tilda_terminal_config_int_property (self, "transparency-percent"); + + tilda_terminal_config_enum_property (self, "backspace-binding"); + tilda_terminal_config_enum_property (self, "delete-binding"); + tilda_terminal_config_enum_property (self, "dynamic-title"); + tilda_terminal_config_enum_property (self, "exit-action"); + tilda_terminal_config_enum_property (self, "scrollbar-position"); + + tilda_terminal_config_boolean_property (self, "scroll-background"); + tilda_terminal_config_boolean_property (self, "scroll-on-output"); + tilda_terminal_config_boolean_property (self, "scroll-on-keystroke"); + tilda_terminal_config_boolean_property (self, "antialiased"); + tilda_terminal_config_boolean_property (self, "allow-bold-text"); + tilda_terminal_config_boolean_property (self, "cursor-blinks"); + tilda_terminal_config_boolean_property (self, "audible-bell"); + tilda_terminal_config_boolean_property (self, "visible-bell"); + tilda_terminal_config_boolean_property (self, "double-buffered"); + tilda_terminal_config_boolean_property (self, "mouse-autohide"); + + /* All right! We're all ready to go, so register with DBus, and lets start! */ tilda_terminal_dbus_register_object (self); return obj; -- 2.34.1