From e8b2b7196ca3f291cf68592f517a31bff18c8f83 Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 21 Nov 2012 21:24:10 +0000 Subject: [PATCH 01/16] Separated colours from main preferences --- Makefile.am | 3 +- src/preferences.c | 314 ++------------------------------------- src/preferences.h | 27 +--- src/profanity.c | 3 + src/theme.c | 371 ++++++++++++++++++++++++++++++++++++++++++++++ src/theme.h | 65 ++++++++ src/windows.c | 43 +++--- 7 files changed, 478 insertions(+), 348 deletions(-) create mode 100644 src/theme.c create mode 100644 src/theme.h diff --git a/Makefile.am b/Makefile.am index e0e3b01b..61ac0a2f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -8,7 +8,8 @@ profanity_SOURCES = src/command.c src/contact.c src/history.c src/jabber.h \ src/main.c src/profanity.h src/prof_history.h src/chat_log.c \ src/chat_log.h src/tinyurl.c src/tinyurl.h src/chat_session.c \ src/chat_session.h src/release.c src/release.h src/room_chat.c \ - src/room_chat.h src/stanza.c src/stanza.h src/parser.c src/parser.h + src/room_chat.h src/stanza.c src/stanza.h src/parser.c src/parser.h \ + src/theme.c src/theme.h TESTS = tests/testsuite check_PROGRAMS = tests/testsuite diff --git a/src/preferences.c b/src/preferences.c index 9bfacd88..a44af242 100644 --- a/src/preferences.c +++ b/src/preferences.c @@ -44,56 +44,6 @@ gint log_maxsize = 0; static PAutocomplete login_ac; static PAutocomplete boolean_choice_ac; -struct colour_string_t { - char *str; - NCURSES_COLOR_T colour; -}; - -static int num_colours = 9; -static struct colour_string_t colours[] = { - { "default", -1 }, - { "white", COLOR_WHITE }, - { "green", COLOR_GREEN }, - { "red", COLOR_RED }, - { "yellow", COLOR_YELLOW }, - { "blue", COLOR_BLUE }, - { "cyan", COLOR_CYAN }, - { "black", COLOR_BLACK }, - { "magenta", COLOR_MAGENTA }, -}; - -// colour preferences -static struct colours_t { - NCURSES_COLOR_T bkgnd; - NCURSES_COLOR_T titlebar; - NCURSES_COLOR_T statusbar; - NCURSES_COLOR_T titlebartext; - NCURSES_COLOR_T titlebarbrackets; - NCURSES_COLOR_T statusbartext; - NCURSES_COLOR_T statusbarbrackets; - NCURSES_COLOR_T statusbaractive; - NCURSES_COLOR_T statusbarnew; - NCURSES_COLOR_T maintext; - NCURSES_COLOR_T splashtext; - NCURSES_COLOR_T online; - NCURSES_COLOR_T away; - NCURSES_COLOR_T xa; - NCURSES_COLOR_T dnd; - NCURSES_COLOR_T chat; - NCURSES_COLOR_T offline; - NCURSES_COLOR_T typing; - NCURSES_COLOR_T gone; - NCURSES_COLOR_T error; - NCURSES_COLOR_T incoming; - NCURSES_COLOR_T roominfo; - NCURSES_COLOR_T me; - NCURSES_COLOR_T them; -} colour_prefs; - -static NCURSES_COLOR_T _lookup_colour(const char * const colour); -static void _set_colour(gchar *val, NCURSES_COLOR_T *pref, - NCURSES_COLOR_T def); -static void _load_colours(void); static void _save_prefs(void); void @@ -125,8 +75,6 @@ prefs_load(void) } free(jids); - _load_colours(); - err = NULL; log_maxsize = g_key_file_get_integer(prefs, "log", "maxsize", &err); if (err != NULL) { @@ -147,111 +95,6 @@ prefs_close(void) g_key_file_free(prefs); } -static NCURSES_COLOR_T -_lookup_colour(const char * const colour) -{ - int i; - for (i = 0; i < num_colours; i++) { - if (strcmp(colours[i].str, colour) == 0) { - return colours[i].colour; - } - } - - return -99; -} - -static void -_set_colour(gchar *val, NCURSES_COLOR_T *pref, - NCURSES_COLOR_T def) -{ - if(!val) { - *pref = def; - } else { - NCURSES_COLOR_T col = _lookup_colour(val); - if (col == -99) { - *pref = def; - } else { - *pref = col; - } - } -} - -static void -_load_colours(void) -{ - gchar *bkgnd_val = g_key_file_get_string(prefs, "colours", "bkgnd", NULL); - _set_colour(bkgnd_val, &colour_prefs.bkgnd, -1); - - gchar *titlebar_val = g_key_file_get_string(prefs, "colours", "titlebar", NULL); - _set_colour(titlebar_val, &colour_prefs.titlebar, COLOR_BLUE); - - gchar *statusbar_val = g_key_file_get_string(prefs, "colours", "statusbar", NULL); - _set_colour(statusbar_val, &colour_prefs.statusbar, COLOR_BLUE); - - gchar *titlebartext_val = g_key_file_get_string(prefs, "colours", "titlebartext", NULL); - _set_colour(titlebartext_val, &colour_prefs.titlebartext, COLOR_WHITE); - - gchar *titlebarbrackets_val = g_key_file_get_string(prefs, "colours", "titlebarbrackets", NULL); - _set_colour(titlebarbrackets_val, &colour_prefs.titlebarbrackets, COLOR_CYAN); - - gchar *statusbartext_val = g_key_file_get_string(prefs, "colours", "statusbartext", NULL); - _set_colour(statusbartext_val, &colour_prefs.statusbartext, COLOR_WHITE); - - gchar *statusbarbrackets_val = g_key_file_get_string(prefs, "colours", "statusbarbrackets", NULL); - _set_colour(statusbarbrackets_val, &colour_prefs.statusbarbrackets, COLOR_CYAN); - - gchar *statusbaractive_val = g_key_file_get_string(prefs, "colours", "statusbaractive", NULL); - _set_colour(statusbaractive_val, &colour_prefs.statusbaractive, COLOR_CYAN); - - gchar *statusbarnew_val = g_key_file_get_string(prefs, "colours", "statusbarnew", NULL); - _set_colour(statusbarnew_val, &colour_prefs.statusbarnew, COLOR_WHITE); - - gchar *maintext_val = g_key_file_get_string(prefs, "colours", "maintext", NULL); - _set_colour(maintext_val, &colour_prefs.maintext, COLOR_WHITE); - - gchar *splashtext_val = g_key_file_get_string(prefs, "colours", "splashtext", NULL); - _set_colour(splashtext_val, &colour_prefs.splashtext, COLOR_CYAN); - - gchar *online_val = g_key_file_get_string(prefs, "colours", "online", NULL); - _set_colour(online_val, &colour_prefs.online, COLOR_GREEN); - - gchar *away_val = g_key_file_get_string(prefs, "colours", "away", NULL); - _set_colour(away_val, &colour_prefs.away, COLOR_CYAN); - - gchar *chat_val = g_key_file_get_string(prefs, "colours", "chat", NULL); - _set_colour(chat_val, &colour_prefs.chat, COLOR_GREEN); - - gchar *dnd_val = g_key_file_get_string(prefs, "colours", "dnd", NULL); - _set_colour(dnd_val, &colour_prefs.dnd, COLOR_RED); - - gchar *xa_val = g_key_file_get_string(prefs, "colours", "xa", NULL); - _set_colour(xa_val, &colour_prefs.xa, COLOR_CYAN); - - gchar *offline_val = g_key_file_get_string(prefs, "colours", "offline", NULL); - _set_colour(offline_val, &colour_prefs.offline, COLOR_RED); - - gchar *typing_val = g_key_file_get_string(prefs, "colours", "typing", NULL); - _set_colour(typing_val, &colour_prefs.typing, COLOR_YELLOW); - - gchar *gone_val = g_key_file_get_string(prefs, "colours", "gone", NULL); - _set_colour(gone_val, &colour_prefs.gone, COLOR_RED); - - gchar *error_val = g_key_file_get_string(prefs, "colours", "error", NULL); - _set_colour(error_val, &colour_prefs.error, COLOR_RED); - - gchar *incoming_val = g_key_file_get_string(prefs, "colours", "incoming", NULL); - _set_colour(incoming_val, &colour_prefs.incoming, COLOR_YELLOW); - - gchar *roominfo_val = g_key_file_get_string(prefs, "colours", "roominfo", NULL); - _set_colour(roominfo_val, &colour_prefs.roominfo, COLOR_YELLOW); - - gchar *me_val = g_key_file_get_string(prefs, "colours", "me", NULL); - _set_colour(me_val, &colour_prefs.me, COLOR_YELLOW); - - gchar *them_val = g_key_file_get_string(prefs, "colours", "them", NULL); - _set_colour(them_val, &colour_prefs.them, COLOR_GREEN); -} - char * prefs_find_login(char *prefix) { @@ -289,6 +132,19 @@ prefs_set_beep(gboolean value) _save_prefs(); } +gchar * +prefs_get_theme(void) +{ + return g_key_file_get_string(prefs, "ui", "theme", NULL); +} + +void +prefs_set_theme(gchar *value) +{ + g_key_file_set_string(prefs, "ui", "theme", value); + _save_prefs(); +} + gboolean prefs_get_states(void) { @@ -511,147 +367,3 @@ _save_prefs(void) char *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL); g_file_set_contents(prefs_loc->str, g_prefs_data, g_data_size, NULL); } - -NCURSES_COLOR_T -prefs_get_bkgnd() -{ - return colour_prefs.bkgnd; -} - -NCURSES_COLOR_T -prefs_get_titlebar() -{ - return colour_prefs.titlebar; -} - -NCURSES_COLOR_T -prefs_get_statusbar() -{ - return colour_prefs.statusbar; -} - -NCURSES_COLOR_T -prefs_get_titlebartext() -{ - return colour_prefs.titlebartext; -} - -NCURSES_COLOR_T -prefs_get_titlebarbrackets() -{ - return colour_prefs.titlebarbrackets; -} - -NCURSES_COLOR_T -prefs_get_statusbartext() -{ - return colour_prefs.statusbartext; -} - -NCURSES_COLOR_T -prefs_get_statusbarbrackets() -{ - return colour_prefs.statusbarbrackets; -} - -NCURSES_COLOR_T -prefs_get_statusbaractive() -{ - return colour_prefs.statusbaractive; -} - -NCURSES_COLOR_T -prefs_get_statusbarnew() -{ - return colour_prefs.statusbarnew; -} - -NCURSES_COLOR_T -prefs_get_maintext() -{ - return colour_prefs.maintext; -} - -NCURSES_COLOR_T -prefs_get_splashtext() -{ - return colour_prefs.splashtext; -} - -NCURSES_COLOR_T -prefs_get_online() -{ - return colour_prefs.online; -} - -NCURSES_COLOR_T -prefs_get_away() -{ - return colour_prefs.away; -} - -NCURSES_COLOR_T -prefs_get_chat() -{ - return colour_prefs.chat; -} - -NCURSES_COLOR_T -prefs_get_dnd() -{ - return colour_prefs.dnd; -} - -NCURSES_COLOR_T -prefs_get_xa() -{ - return colour_prefs.xa; -} - -NCURSES_COLOR_T -prefs_get_offline() -{ - return colour_prefs.offline; -} - -NCURSES_COLOR_T -prefs_get_typing() -{ - return colour_prefs.typing; -} - -NCURSES_COLOR_T -prefs_get_gone() -{ - return colour_prefs.gone; -} - -NCURSES_COLOR_T -prefs_get_error() -{ - return colour_prefs.error; -} - -NCURSES_COLOR_T -prefs_get_incoming() -{ - return colour_prefs.incoming; -} - -NCURSES_COLOR_T -prefs_get_roominfo() -{ - return colour_prefs.roominfo; -} - -NCURSES_COLOR_T -prefs_get_me() -{ - return colour_prefs.me; -} - -NCURSES_COLOR_T -prefs_get_them() -{ - return colour_prefs.them; -} diff --git a/src/preferences.h b/src/preferences.h index bb45ee07..f4547b07 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -63,6 +63,8 @@ gboolean prefs_get_states(void); void prefs_set_states(gboolean value); gboolean prefs_get_outtype(void); void prefs_set_outtype(gboolean value); +gchar * prefs_get_theme(void); +void prefs_set_theme(gchar *value); void prefs_set_notify_message(gboolean value); gboolean prefs_get_notify_message(void); @@ -77,29 +79,4 @@ gint prefs_get_priority(void); void prefs_add_login(const char *jid); -NCURSES_COLOR_T prefs_get_bkgnd(); -NCURSES_COLOR_T prefs_get_titlebar(); -NCURSES_COLOR_T prefs_get_statusbar(); -NCURSES_COLOR_T prefs_get_titlebartext(); -NCURSES_COLOR_T prefs_get_titlebarbrackets(); -NCURSES_COLOR_T prefs_get_statusbartext(); -NCURSES_COLOR_T prefs_get_statusbarbrackets(); -NCURSES_COLOR_T prefs_get_statusbaractive(); -NCURSES_COLOR_T prefs_get_statusbarnew(); -NCURSES_COLOR_T prefs_get_maintext(); -NCURSES_COLOR_T prefs_get_splashtext(); -NCURSES_COLOR_T prefs_get_online(); -NCURSES_COLOR_T prefs_get_away(); -NCURSES_COLOR_T prefs_get_chat(); -NCURSES_COLOR_T prefs_get_dnd(); -NCURSES_COLOR_T prefs_get_xa(); -NCURSES_COLOR_T prefs_get_offline(); -NCURSES_COLOR_T prefs_get_typing(); -NCURSES_COLOR_T prefs_get_gone(); -NCURSES_COLOR_T prefs_get_error(); -NCURSES_COLOR_T prefs_get_incoming(); -NCURSES_COLOR_T prefs_get_roominfo(); -NCURSES_COLOR_T prefs_get_me(); -NCURSES_COLOR_T prefs_get_them(); - #endif diff --git a/src/profanity.c b/src/profanity.c index 1b30296e..e98443eb 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -39,6 +39,7 @@ #include "preferences.h" #include "profanity.h" #include "room_chat.h" +#include "theme.h" #include "jabber.h" #include "ui.h" @@ -446,6 +447,7 @@ _init(const int disable_tls, char *log_level) log_info("Starting Profanity (%s)...", PACKAGE_VERSION); chat_log_init(); prefs_load(); + theme_load(prefs_get_theme()); gui_init(); jabber_init(disable_tls); cmd_init(); @@ -462,6 +464,7 @@ _shutdown(void) gui_close(); chat_log_close(); prefs_close(); + theme_close(); cmd_close(); log_close(); } diff --git a/src/theme.c b/src/theme.c new file mode 100644 index 00000000..267a88fb --- /dev/null +++ b/src/theme.c @@ -0,0 +1,371 @@ +/* + * theme.c + * + * Copyright (C) 2012 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#include "config.h" + +#include +#include + +#include +#ifdef HAVE_NCURSES_H +#include +#endif +#ifdef HAVE_NCURSES_NCURSES_H +#include +#endif + +#include "log.h" + +static GString *theme_loc; +static GKeyFile *theme; + +struct colour_string_t { + char *str; + NCURSES_COLOR_T colour; +}; + +static int num_colours = 9; +static struct colour_string_t colours[] = { + { "default", -1 }, + { "white", COLOR_WHITE }, + { "green", COLOR_GREEN }, + { "red", COLOR_RED }, + { "yellow", COLOR_YELLOW }, + { "blue", COLOR_BLUE }, + { "cyan", COLOR_CYAN }, + { "black", COLOR_BLACK }, + { "magenta", COLOR_MAGENTA }, +}; + +// colour preferences +static struct colours_t { + NCURSES_COLOR_T bkgnd; + NCURSES_COLOR_T titlebar; + NCURSES_COLOR_T statusbar; + NCURSES_COLOR_T titlebartext; + NCURSES_COLOR_T titlebarbrackets; + NCURSES_COLOR_T statusbartext; + NCURSES_COLOR_T statusbarbrackets; + NCURSES_COLOR_T statusbaractive; + NCURSES_COLOR_T statusbarnew; + NCURSES_COLOR_T maintext; + NCURSES_COLOR_T splashtext; + NCURSES_COLOR_T online; + NCURSES_COLOR_T away; + NCURSES_COLOR_T xa; + NCURSES_COLOR_T dnd; + NCURSES_COLOR_T chat; + NCURSES_COLOR_T offline; + NCURSES_COLOR_T typing; + NCURSES_COLOR_T gone; + NCURSES_COLOR_T error; + NCURSES_COLOR_T incoming; + NCURSES_COLOR_T roominfo; + NCURSES_COLOR_T me; + NCURSES_COLOR_T them; +} colour_prefs; + +static NCURSES_COLOR_T _lookup_colour(const char * const colour); +static void _set_colour(gchar *val, NCURSES_COLOR_T *pref, + NCURSES_COLOR_T def); +static void _load_colours(void); + +void +theme_load(const char * const theme_name) +{ + log_info("Loading theme"); + theme = g_key_file_new(); + + if (theme_name != NULL) { + theme_loc = g_string_new(getenv("HOME")); + g_string_append(theme_loc, "/.profanity/themes/"); + g_string_append(theme_loc, theme_name); + g_key_file_load_from_file(theme, theme_loc->str, G_KEY_FILE_KEEP_COMMENTS, + NULL); + } + + _load_colours(); +} + +void +theme_close(void) +{ + g_key_file_free(theme); +} + +static NCURSES_COLOR_T +_lookup_colour(const char * const colour) +{ + int i; + for (i = 0; i < num_colours; i++) { + if (strcmp(colours[i].str, colour) == 0) { + return colours[i].colour; + } + } + + return -99; +} + +static void +_set_colour(gchar *val, NCURSES_COLOR_T *pref, + NCURSES_COLOR_T def) +{ + if(!val) { + *pref = def; + } else { + NCURSES_COLOR_T col = _lookup_colour(val); + if (col == -99) { + *pref = def; + } else { + *pref = col; + } + } +} + +static void +_load_colours(void) +{ + gchar *bkgnd_val = g_key_file_get_string(theme, "colours", "bkgnd", NULL); + _set_colour(bkgnd_val, &colour_prefs.bkgnd, -1); + + gchar *titlebar_val = g_key_file_get_string(theme, "colours", "titlebar", NULL); + _set_colour(titlebar_val, &colour_prefs.titlebar, COLOR_BLUE); + + gchar *statusbar_val = g_key_file_get_string(theme, "colours", "statusbar", NULL); + _set_colour(statusbar_val, &colour_prefs.statusbar, COLOR_BLUE); + + gchar *titlebartext_val = g_key_file_get_string(theme, "colours", "titlebartext", NULL); + _set_colour(titlebartext_val, &colour_prefs.titlebartext, COLOR_WHITE); + + gchar *titlebarbrackets_val = g_key_file_get_string(theme, "colours", "titlebarbrackets", NULL); + _set_colour(titlebarbrackets_val, &colour_prefs.titlebarbrackets, COLOR_CYAN); + + gchar *statusbartext_val = g_key_file_get_string(theme, "colours", "statusbartext", NULL); + _set_colour(statusbartext_val, &colour_prefs.statusbartext, COLOR_WHITE); + + gchar *statusbarbrackets_val = g_key_file_get_string(theme, "colours", "statusbarbrackets", NULL); + _set_colour(statusbarbrackets_val, &colour_prefs.statusbarbrackets, COLOR_CYAN); + + gchar *statusbaractive_val = g_key_file_get_string(theme, "colours", "statusbaractive", NULL); + _set_colour(statusbaractive_val, &colour_prefs.statusbaractive, COLOR_CYAN); + + gchar *statusbarnew_val = g_key_file_get_string(theme, "colours", "statusbarnew", NULL); + _set_colour(statusbarnew_val, &colour_prefs.statusbarnew, COLOR_WHITE); + + gchar *maintext_val = g_key_file_get_string(theme, "colours", "maintext", NULL); + _set_colour(maintext_val, &colour_prefs.maintext, COLOR_WHITE); + + gchar *splashtext_val = g_key_file_get_string(theme, "colours", "splashtext", NULL); + _set_colour(splashtext_val, &colour_prefs.splashtext, COLOR_CYAN); + + gchar *online_val = g_key_file_get_string(theme, "colours", "online", NULL); + _set_colour(online_val, &colour_prefs.online, COLOR_GREEN); + + gchar *away_val = g_key_file_get_string(theme, "colours", "away", NULL); + _set_colour(away_val, &colour_prefs.away, COLOR_CYAN); + + gchar *chat_val = g_key_file_get_string(theme, "colours", "chat", NULL); + _set_colour(chat_val, &colour_prefs.chat, COLOR_GREEN); + + gchar *dnd_val = g_key_file_get_string(theme, "colours", "dnd", NULL); + _set_colour(dnd_val, &colour_prefs.dnd, COLOR_RED); + + gchar *xa_val = g_key_file_get_string(theme, "colours", "xa", NULL); + _set_colour(xa_val, &colour_prefs.xa, COLOR_CYAN); + + gchar *offline_val = g_key_file_get_string(theme, "colours", "offline", NULL); + _set_colour(offline_val, &colour_prefs.offline, COLOR_RED); + + gchar *typing_val = g_key_file_get_string(theme, "colours", "typing", NULL); + _set_colour(typing_val, &colour_prefs.typing, COLOR_YELLOW); + + gchar *gone_val = g_key_file_get_string(theme, "colours", "gone", NULL); + _set_colour(gone_val, &colour_prefs.gone, COLOR_RED); + + gchar *error_val = g_key_file_get_string(theme, "colours", "error", NULL); + _set_colour(error_val, &colour_prefs.error, COLOR_RED); + + gchar *incoming_val = g_key_file_get_string(theme, "colours", "incoming", NULL); + _set_colour(incoming_val, &colour_prefs.incoming, COLOR_YELLOW); + + gchar *roominfo_val = g_key_file_get_string(theme, "colours", "roominfo", NULL); + _set_colour(roominfo_val, &colour_prefs.roominfo, COLOR_YELLOW); + + gchar *me_val = g_key_file_get_string(theme, "colours", "me", NULL); + _set_colour(me_val, &colour_prefs.me, COLOR_YELLOW); + + gchar *them_val = g_key_file_get_string(theme, "colours", "them", NULL); + _set_colour(them_val, &colour_prefs.them, COLOR_GREEN); +} +/* +static void +_save_prefs(void) +{ + gsize g_data_size; + char *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL); + g_file_set_contents(prefs_loc->str, g_prefs_data, g_data_size, NULL); +} +*/ + +NCURSES_COLOR_T +theme_get_bkgnd() +{ + return colour_prefs.bkgnd; +} + +NCURSES_COLOR_T +theme_get_titlebar() +{ + return colour_prefs.titlebar; +} + +NCURSES_COLOR_T +theme_get_statusbar() +{ + return colour_prefs.statusbar; +} + +NCURSES_COLOR_T +theme_get_titlebartext() +{ + return colour_prefs.titlebartext; +} + +NCURSES_COLOR_T +theme_get_titlebarbrackets() +{ + return colour_prefs.titlebarbrackets; +} + +NCURSES_COLOR_T +theme_get_statusbartext() +{ + return colour_prefs.statusbartext; +} + +NCURSES_COLOR_T +theme_get_statusbarbrackets() +{ + return colour_prefs.statusbarbrackets; +} + +NCURSES_COLOR_T +theme_get_statusbaractive() +{ + return colour_prefs.statusbaractive; +} + +NCURSES_COLOR_T +theme_get_statusbarnew() +{ + return colour_prefs.statusbarnew; +} + +NCURSES_COLOR_T +theme_get_maintext() +{ + return colour_prefs.maintext; +} + +NCURSES_COLOR_T +theme_get_splashtext() +{ + return colour_prefs.splashtext; +} + +NCURSES_COLOR_T +theme_get_online() +{ + return colour_prefs.online; +} + +NCURSES_COLOR_T +theme_get_away() +{ + return colour_prefs.away; +} + +NCURSES_COLOR_T +theme_get_chat() +{ + return colour_prefs.chat; +} + +NCURSES_COLOR_T +theme_get_dnd() +{ + return colour_prefs.dnd; +} + +NCURSES_COLOR_T +theme_get_xa() +{ + return colour_prefs.xa; +} + +NCURSES_COLOR_T +theme_get_offline() +{ + return colour_prefs.offline; +} + +NCURSES_COLOR_T +theme_get_typing() +{ + return colour_prefs.typing; +} + +NCURSES_COLOR_T +theme_get_gone() +{ + return colour_prefs.gone; +} + +NCURSES_COLOR_T +theme_get_error() +{ + return colour_prefs.error; +} + +NCURSES_COLOR_T +theme_get_incoming() +{ + return colour_prefs.incoming; +} + +NCURSES_COLOR_T +theme_get_roominfo() +{ + return colour_prefs.roominfo; +} + +NCURSES_COLOR_T +theme_get_me() +{ + return colour_prefs.me; +} + +NCURSES_COLOR_T +theme_get_them() +{ + return colour_prefs.them; +} diff --git a/src/theme.h b/src/theme.h new file mode 100644 index 00000000..f045f09a --- /dev/null +++ b/src/theme.h @@ -0,0 +1,65 @@ +/* + * theme.h + * + * Copyright (C) 2012 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#ifndef THEME_H +#define THEME_H + +#include "config.h" + +#include + +#ifdef HAVE_NCURSES_H +#include +#endif +#ifdef HAVE_NCURSES_NCURSES_H +#include +#endif + +void theme_load(const char * const theme_name); +void theme_close(void); + +NCURSES_COLOR_T theme_get_bkgnd(); +NCURSES_COLOR_T theme_get_titlebar(); +NCURSES_COLOR_T theme_get_statusbar(); +NCURSES_COLOR_T theme_get_titlebartext(); +NCURSES_COLOR_T theme_get_titlebarbrackets(); +NCURSES_COLOR_T theme_get_statusbartext(); +NCURSES_COLOR_T theme_get_statusbarbrackets(); +NCURSES_COLOR_T theme_get_statusbaractive(); +NCURSES_COLOR_T theme_get_statusbarnew(); +NCURSES_COLOR_T theme_get_maintext(); +NCURSES_COLOR_T theme_get_splashtext(); +NCURSES_COLOR_T theme_get_online(); +NCURSES_COLOR_T theme_get_away(); +NCURSES_COLOR_T theme_get_chat(); +NCURSES_COLOR_T theme_get_dnd(); +NCURSES_COLOR_T theme_get_xa(); +NCURSES_COLOR_T theme_get_offline(); +NCURSES_COLOR_T theme_get_typing(); +NCURSES_COLOR_T theme_get_gone(); +NCURSES_COLOR_T theme_get_error(); +NCURSES_COLOR_T theme_get_incoming(); +NCURSES_COLOR_T theme_get_roominfo(); +NCURSES_COLOR_T theme_get_me(); +NCURSES_COLOR_T theme_get_them(); + +#endif diff --git a/src/windows.c b/src/windows.c index 80adeed7..8438a3ec 100644 --- a/src/windows.c +++ b/src/windows.c @@ -45,6 +45,7 @@ #include "preferences.h" #include "release.h" #include "room_chat.h" +#include "theme.h" #include "ui.h" #define CONS_WIN_TITLE "_cons" @@ -113,39 +114,39 @@ gui_init(void) start_color(); // main text - init_pair(1, prefs_get_maintext(), prefs_get_bkgnd()); - init_pair(2, prefs_get_splashtext(), prefs_get_bkgnd()); - init_pair(3, prefs_get_error(), prefs_get_bkgnd()); - init_pair(4, prefs_get_incoming(), prefs_get_bkgnd()); + init_pair(1, theme_get_maintext(), theme_get_bkgnd()); + init_pair(2, theme_get_splashtext(), theme_get_bkgnd()); + init_pair(3, theme_get_error(), theme_get_bkgnd()); + init_pair(4, theme_get_incoming(), theme_get_bkgnd()); // title bar - init_pair(10, prefs_get_titlebartext(), prefs_get_titlebar()); - init_pair(11, prefs_get_titlebarbrackets(), prefs_get_titlebar()); + init_pair(10, theme_get_titlebartext(), theme_get_titlebar()); + init_pair(11, theme_get_titlebarbrackets(), theme_get_titlebar()); // status bar - init_pair(20, prefs_get_statusbartext(), prefs_get_statusbar()); - init_pair(21, prefs_get_statusbarbrackets(), prefs_get_statusbar()); - init_pair(22, prefs_get_statusbaractive(), prefs_get_statusbar()); - init_pair(23, prefs_get_statusbarnew(), prefs_get_statusbar()); + init_pair(20, theme_get_statusbartext(), theme_get_statusbar()); + init_pair(21, theme_get_statusbarbrackets(), theme_get_statusbar()); + init_pair(22, theme_get_statusbaractive(), theme_get_statusbar()); + init_pair(23, theme_get_statusbarnew(), theme_get_statusbar()); // chat - init_pair(30, prefs_get_me(), prefs_get_bkgnd()); - init_pair(31, prefs_get_them(), prefs_get_bkgnd()); + init_pair(30, theme_get_me(), theme_get_bkgnd()); + init_pair(31, theme_get_them(), theme_get_bkgnd()); // room chat - init_pair(40, prefs_get_roominfo(), prefs_get_bkgnd()); + init_pair(40, theme_get_roominfo(), theme_get_bkgnd()); // statuses - init_pair(50, prefs_get_online(), prefs_get_bkgnd()); - init_pair(51, prefs_get_offline(), prefs_get_bkgnd()); - init_pair(52, prefs_get_away(), prefs_get_bkgnd()); - init_pair(53, prefs_get_chat(), prefs_get_bkgnd()); - init_pair(54, prefs_get_dnd(), prefs_get_bkgnd()); - init_pair(55, prefs_get_xa(), prefs_get_bkgnd()); + init_pair(50, theme_get_online(), theme_get_bkgnd()); + init_pair(51, theme_get_offline(), theme_get_bkgnd()); + init_pair(52, theme_get_away(), theme_get_bkgnd()); + init_pair(53, theme_get_chat(), theme_get_bkgnd()); + init_pair(54, theme_get_dnd(), theme_get_bkgnd()); + init_pair(55, theme_get_xa(), theme_get_bkgnd()); // states - init_pair(60, prefs_get_typing(), prefs_get_bkgnd()); - init_pair(61, prefs_get_gone(), prefs_get_bkgnd()); + init_pair(60, theme_get_typing(), theme_get_bkgnd()); + init_pair(61, theme_get_gone(), theme_get_bkgnd()); } refresh(); From cd4465394b65e824123b69f3a97bd1e8be5a03d0 Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 21 Nov 2012 22:33:07 +0000 Subject: [PATCH 02/16] Added theme files support with /theme command --- docs/profanity.1 | 50 ++++++++++++++++++++++++++++++++++++------------ src/command.c | 28 +++++++++++++++++++++++++++ src/theme.c | 44 +++++++++++++++++++++++++++++++++--------- src/theme.h | 1 + src/ui.h | 1 + src/windows.c | 24 ++++++++++++++--------- 6 files changed, 118 insertions(+), 30 deletions(-) diff --git a/docs/profanity.1 b/docs/profanity.1 index 552613c5..a1ad39ca 100644 --- a/docs/profanity.1 +++ b/docs/profanity.1 @@ -1,17 +1,17 @@ .TH Profanity 1 "August 2012" "Profanity XMPP client" .SH NAME -Profanity \- a simple console based XMPP chat client for Linux. +Profanity \- a simple console based XMPP chat client. .SH SYNOPSIS .B profanity [-vhd] [-l level] .SH DESCRIPTION .B Profanity is a simple lightweight console based XMPP chat client. It's emphasis is -on having a simple and configurable UI, see the homepage +on having a simple and configurable command driven UI, see the homepage at: .br .PP - + .SH OPTIONS .TP .BI "\-v, \-\-version" @@ -66,6 +66,8 @@ beep=false .br flash=false .br +theme=mytheme +.br showsplash=false .br notify=true @@ -81,6 +83,26 @@ remind=15 vercheck=true .br .PP +[connections] +.br +logins=user1@server1.org;myuser@server2.com; +.PP +.RE +The [ui] section contains basic preferences, you can find more help on these +settings by typing "/help beep" for example in Profanity. +.PP +The [connections] section is the list of users that will appear in tab +completion for the /connect command. This list is automatically populated +when you successfully log in as a user. +.SH THEMES +Themes files for +.B Profanity +are stored in +.I ~/.profanity/themes +, see the following example: +.br +.RS +.PP [colours] .br bkgnd=default @@ -117,23 +139,27 @@ xa=cyan .br offline=red .br -.PP -[connections] +typing=yellow +.br +gone=red +.br +error=red +.br +incoming=yellow +.br +roominfo=yellow +.br +me=yellow +.br +them=green .br -logins=user1@server1.org;myuser@server2.com; .PP .RE -The [ui] section contains basic preferences, you can find more help on these -settings by typing "/help beep" for example in Profanity. -.PP The [colours] section allows you to tailor the colours used in Profanity to suit your terminal preferences. Available colours are black, white, red, green, blue, yellow, cyan, magenta and default, where default is the default forground or background colour on your terminal. .PP -The [connections] section is the list of users that will appear in tab -completion for the /connect command. This list is automatically populated -when you successfully log in as a user. .SH BUGS Bugs can either be reported by sending a mail directly to: .br diff --git a/src/command.c b/src/command.c index 715ad0ee..52f7fac9 100644 --- a/src/command.c +++ b/src/command.c @@ -40,6 +40,7 @@ #include "preferences.h" #include "prof_autocomplete.h" #include "profanity.h" +#include "theme.h" #include "tinyurl.h" #include "ui.h" @@ -124,6 +125,7 @@ static gboolean _cmd_xa(gchar **args, struct cmd_help_t help); static gboolean _cmd_info(gchar **args, struct cmd_help_t help); static gboolean _cmd_wins(gchar **args, struct cmd_help_t help); static gboolean _cmd_nick(gchar **args, struct cmd_help_t help); +static gboolean _cmd_theme(gchar **args, struct cmd_help_t help); /* * The commands are broken down into three groups: @@ -191,6 +193,18 @@ static struct cmd_t main_commands[] = "you will need to restart Profanity for config file edits to take effect.", NULL } } }, + { "/theme", + _cmd_theme, parse_args, 1, 1, + { "/theme [theme-name]", "Change colour theme.", + { "/theme [theme-name]", + "--------------", + "Change the colour setting as defined in:", + "", + " ~/.profanity/themes/theme-name", + "", + "Using \"default\" as the theme name will reset to the default colours.", + NULL } } }, + { "/msg", _cmd_msg, parse_args_with_freetext, 2, 2, { "/msg user@host mesg", "Send mesg to user.", @@ -1077,6 +1091,20 @@ _cmd_prefs(gchar **args, struct cmd_help_t help) return TRUE; } +static gboolean +_cmd_theme(gchar **args, struct cmd_help_t help) +{ + if (theme_change(args[0])) { + win_load_colours(); + prefs_set_theme(args[0]); + cons_show("Loaded theme: %s", args[0]); + } else { + cons_show("Couldn't find theme: %s", args[0]); + } + + return TRUE; +} + static gboolean _cmd_who(gchar **args, struct cmd_help_t help) { diff --git a/src/theme.c b/src/theme.c index 267a88fb..0792222a 100644 --- a/src/theme.c +++ b/src/theme.c @@ -106,6 +106,41 @@ theme_load(const char * const theme_name) _load_colours(); } +gboolean +theme_change(const char * const theme_name) +{ + // use default theme + if (strcmp(theme_name, "default") == 0) { + g_key_file_free(theme); + theme = g_key_file_new(); + _load_colours(); + return TRUE; + } else { + GString *new_theme_file = g_string_new(getenv("HOME")); + g_string_append(new_theme_file, "/.profanity/themes/"); + g_string_append(new_theme_file, theme_name); + + // no theme file found + if (!g_file_test(new_theme_file->str, G_FILE_TEST_EXISTS)) { + log_info("Theme does not exist \"%s\"", theme_name); + g_string_free(new_theme_file, TRUE); + return FALSE; + + // load from theme file + } else { + g_string_free(theme_loc, TRUE); + theme_loc = new_theme_file; + log_info("Changing theme to \"%s\"", theme_name); + g_key_file_free(theme); + theme = g_key_file_new(); + g_key_file_load_from_file(theme, theme_loc->str, G_KEY_FILE_KEEP_COMMENTS, + NULL); + _load_colours(); + return TRUE; + } + } +} + void theme_close(void) { @@ -216,15 +251,6 @@ _load_colours(void) gchar *them_val = g_key_file_get_string(theme, "colours", "them", NULL); _set_colour(them_val, &colour_prefs.them, COLOR_GREEN); } -/* -static void -_save_prefs(void) -{ - gsize g_data_size; - char *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL); - g_file_set_contents(prefs_loc->str, g_prefs_data, g_data_size, NULL); -} -*/ NCURSES_COLOR_T theme_get_bkgnd() diff --git a/src/theme.h b/src/theme.h index f045f09a..37b49c0b 100644 --- a/src/theme.h +++ b/src/theme.h @@ -35,6 +35,7 @@ #endif void theme_load(const char * const theme_name); +gboolean theme_change(const char * const theme_name); void theme_close(void); NCURSES_COLOR_T theme_get_bkgnd(); diff --git a/src/ui.h b/src/ui.h index 3e30fd1a..2d441abf 100644 --- a/src/ui.h +++ b/src/ui.h @@ -147,6 +147,7 @@ void win_show_room_member_nick_change(const char * const room, void win_show_room_nick_change(const char * const room, const char * const nick); void win_show_room_member_presence(const char * const room, const char * const nick, const char * const show, const char * const status); +void win_load_colours(void); // console window actions void cons_about(void); diff --git a/src/windows.c b/src/windows.c index 8438a3ec..58b8b80d 100644 --- a/src/windows.c +++ b/src/windows.c @@ -109,6 +109,21 @@ gui_init(void) cbreak(); keypad(stdscr, TRUE); + win_load_colours(); + + refresh(); + + create_title_bar(); + create_status_bar(); + create_input_window(); + _create_windows(); + + dirty = TRUE; +} + +void +win_load_colours(void) +{ if (has_colors()) { use_default_colors(); start_color(); @@ -148,15 +163,6 @@ gui_init(void) init_pair(60, theme_get_typing(), theme_get_bkgnd()); init_pair(61, theme_get_gone(), theme_get_bkgnd()); } - - refresh(); - - create_title_bar(); - create_status_bar(); - create_input_window(); - _create_windows(); - - dirty = TRUE; } void From 29f72283c44dbc72bfe735b90f2267862aef08eb Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 21 Nov 2012 23:18:21 +0000 Subject: [PATCH 03/16] Moved colour initialisation to theme --- src/theme.c | 184 +++++++++++--------------------------------------- src/theme.h | 26 +------ src/windows.c | 36 +--------- 3 files changed, 42 insertions(+), 204 deletions(-) diff --git a/src/theme.c b/src/theme.c index 0792222a..71c2d5f4 100644 --- a/src/theme.c +++ b/src/theme.c @@ -34,6 +34,7 @@ #endif #include "log.h" +#include "theme.h" static GString *theme_loc; static GKeyFile *theme; @@ -147,6 +148,45 @@ theme_close(void) g_key_file_free(theme); } +void +theme_init_colours(void) +{ + // main text + init_pair(1, colour_prefs.maintext, colour_prefs.bkgnd); + init_pair(2, colour_prefs.splashtext, colour_prefs.bkgnd); + init_pair(3, colour_prefs.error, colour_prefs.bkgnd); + init_pair(4, colour_prefs.incoming, colour_prefs.bkgnd); + + // title bar + init_pair(10, colour_prefs.titlebartext, colour_prefs.titlebar); + init_pair(11, colour_prefs.titlebarbrackets, colour_prefs.titlebar); + + // status bar + init_pair(20, colour_prefs.statusbartext, colour_prefs.statusbar); + init_pair(21, colour_prefs.statusbarbrackets, colour_prefs.statusbar); + init_pair(22, colour_prefs.statusbaractive, colour_prefs.statusbar); + init_pair(23, colour_prefs.statusbarnew, colour_prefs.statusbar); + + // chat + init_pair(30, colour_prefs.me, colour_prefs.bkgnd); + init_pair(31, colour_prefs.them, colour_prefs.bkgnd); + + // room chat + init_pair(40, colour_prefs.roominfo, colour_prefs.bkgnd); + + // statuses + init_pair(50, colour_prefs.online, colour_prefs.bkgnd); + init_pair(51, colour_prefs.offline, colour_prefs.bkgnd); + init_pair(52, colour_prefs.away, colour_prefs.bkgnd); + init_pair(53, colour_prefs.chat, colour_prefs.bkgnd); + init_pair(54, colour_prefs.dnd, colour_prefs.bkgnd); + init_pair(55, colour_prefs.xa, colour_prefs.bkgnd); + + // states + init_pair(60, colour_prefs.typing, colour_prefs.bkgnd); + init_pair(61, colour_prefs.gone, colour_prefs.bkgnd); +} + static NCURSES_COLOR_T _lookup_colour(const char * const colour) { @@ -251,147 +291,3 @@ _load_colours(void) gchar *them_val = g_key_file_get_string(theme, "colours", "them", NULL); _set_colour(them_val, &colour_prefs.them, COLOR_GREEN); } - -NCURSES_COLOR_T -theme_get_bkgnd() -{ - return colour_prefs.bkgnd; -} - -NCURSES_COLOR_T -theme_get_titlebar() -{ - return colour_prefs.titlebar; -} - -NCURSES_COLOR_T -theme_get_statusbar() -{ - return colour_prefs.statusbar; -} - -NCURSES_COLOR_T -theme_get_titlebartext() -{ - return colour_prefs.titlebartext; -} - -NCURSES_COLOR_T -theme_get_titlebarbrackets() -{ - return colour_prefs.titlebarbrackets; -} - -NCURSES_COLOR_T -theme_get_statusbartext() -{ - return colour_prefs.statusbartext; -} - -NCURSES_COLOR_T -theme_get_statusbarbrackets() -{ - return colour_prefs.statusbarbrackets; -} - -NCURSES_COLOR_T -theme_get_statusbaractive() -{ - return colour_prefs.statusbaractive; -} - -NCURSES_COLOR_T -theme_get_statusbarnew() -{ - return colour_prefs.statusbarnew; -} - -NCURSES_COLOR_T -theme_get_maintext() -{ - return colour_prefs.maintext; -} - -NCURSES_COLOR_T -theme_get_splashtext() -{ - return colour_prefs.splashtext; -} - -NCURSES_COLOR_T -theme_get_online() -{ - return colour_prefs.online; -} - -NCURSES_COLOR_T -theme_get_away() -{ - return colour_prefs.away; -} - -NCURSES_COLOR_T -theme_get_chat() -{ - return colour_prefs.chat; -} - -NCURSES_COLOR_T -theme_get_dnd() -{ - return colour_prefs.dnd; -} - -NCURSES_COLOR_T -theme_get_xa() -{ - return colour_prefs.xa; -} - -NCURSES_COLOR_T -theme_get_offline() -{ - return colour_prefs.offline; -} - -NCURSES_COLOR_T -theme_get_typing() -{ - return colour_prefs.typing; -} - -NCURSES_COLOR_T -theme_get_gone() -{ - return colour_prefs.gone; -} - -NCURSES_COLOR_T -theme_get_error() -{ - return colour_prefs.error; -} - -NCURSES_COLOR_T -theme_get_incoming() -{ - return colour_prefs.incoming; -} - -NCURSES_COLOR_T -theme_get_roominfo() -{ - return colour_prefs.roominfo; -} - -NCURSES_COLOR_T -theme_get_me() -{ - return colour_prefs.me; -} - -NCURSES_COLOR_T -theme_get_them() -{ - return colour_prefs.them; -} diff --git a/src/theme.h b/src/theme.h index 37b49c0b..1f348d61 100644 --- a/src/theme.h +++ b/src/theme.h @@ -35,32 +35,8 @@ #endif void theme_load(const char * const theme_name); +void theme_init_colours(void); gboolean theme_change(const char * const theme_name); void theme_close(void); -NCURSES_COLOR_T theme_get_bkgnd(); -NCURSES_COLOR_T theme_get_titlebar(); -NCURSES_COLOR_T theme_get_statusbar(); -NCURSES_COLOR_T theme_get_titlebartext(); -NCURSES_COLOR_T theme_get_titlebarbrackets(); -NCURSES_COLOR_T theme_get_statusbartext(); -NCURSES_COLOR_T theme_get_statusbarbrackets(); -NCURSES_COLOR_T theme_get_statusbaractive(); -NCURSES_COLOR_T theme_get_statusbarnew(); -NCURSES_COLOR_T theme_get_maintext(); -NCURSES_COLOR_T theme_get_splashtext(); -NCURSES_COLOR_T theme_get_online(); -NCURSES_COLOR_T theme_get_away(); -NCURSES_COLOR_T theme_get_chat(); -NCURSES_COLOR_T theme_get_dnd(); -NCURSES_COLOR_T theme_get_xa(); -NCURSES_COLOR_T theme_get_offline(); -NCURSES_COLOR_T theme_get_typing(); -NCURSES_COLOR_T theme_get_gone(); -NCURSES_COLOR_T theme_get_error(); -NCURSES_COLOR_T theme_get_incoming(); -NCURSES_COLOR_T theme_get_roominfo(); -NCURSES_COLOR_T theme_get_me(); -NCURSES_COLOR_T theme_get_them(); - #endif diff --git a/src/windows.c b/src/windows.c index 58b8b80d..902d023e 100644 --- a/src/windows.c +++ b/src/windows.c @@ -127,41 +127,7 @@ win_load_colours(void) if (has_colors()) { use_default_colors(); start_color(); - - // main text - init_pair(1, theme_get_maintext(), theme_get_bkgnd()); - init_pair(2, theme_get_splashtext(), theme_get_bkgnd()); - init_pair(3, theme_get_error(), theme_get_bkgnd()); - init_pair(4, theme_get_incoming(), theme_get_bkgnd()); - - // title bar - init_pair(10, theme_get_titlebartext(), theme_get_titlebar()); - init_pair(11, theme_get_titlebarbrackets(), theme_get_titlebar()); - - // status bar - init_pair(20, theme_get_statusbartext(), theme_get_statusbar()); - init_pair(21, theme_get_statusbarbrackets(), theme_get_statusbar()); - init_pair(22, theme_get_statusbaractive(), theme_get_statusbar()); - init_pair(23, theme_get_statusbarnew(), theme_get_statusbar()); - - // chat - init_pair(30, theme_get_me(), theme_get_bkgnd()); - init_pair(31, theme_get_them(), theme_get_bkgnd()); - - // room chat - init_pair(40, theme_get_roominfo(), theme_get_bkgnd()); - - // statuses - init_pair(50, theme_get_online(), theme_get_bkgnd()); - init_pair(51, theme_get_offline(), theme_get_bkgnd()); - init_pair(52, theme_get_away(), theme_get_bkgnd()); - init_pair(53, theme_get_chat(), theme_get_bkgnd()); - init_pair(54, theme_get_dnd(), theme_get_bkgnd()); - init_pair(55, theme_get_xa(), theme_get_bkgnd()); - - // states - init_pair(60, theme_get_typing(), theme_get_bkgnd()); - init_pair(61, theme_get_gone(), theme_get_bkgnd()); + theme_init_colours(); } } From 1adbc171949066a8716d17f550fe15696ce19410 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 22 Nov 2012 01:49:50 +0000 Subject: [PATCH 04/16] Added theme to prefs output --- src/windows.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/windows.c b/src/windows.c index 902d023e..27c73cb4 100644 --- a/src/windows.c +++ b/src/windows.c @@ -1010,6 +1010,8 @@ cons_prefs(void) else cons_show("Terminal flash : OFF"); + cons_show("Theme : %s", prefs_get_theme()); + if (prefs_get_intype()) cons_show("Show typing : ON"); else From c5b4fc4a80563390610938e36c6e9dc71337c754 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 22 Nov 2012 02:01:49 +0000 Subject: [PATCH 05/16] Added input text to theme --- src/input_win.c | 3 ++- src/status_bar.c | 1 + src/theme.c | 5 +++++ src/theme.h | 23 +++++++++++++++++++++++ src/title_bar.c | 1 + src/ui.h | 22 ---------------------- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/input_win.c b/src/input_win.c index 7d5a7485..5ad64d80 100644 --- a/src/input_win.c +++ b/src/input_win.c @@ -58,6 +58,7 @@ #include "log.h" #include "preferences.h" #include "profanity.h" +#include "theme.h" #include "ui.h" static WINDOW *inp_win; @@ -79,7 +80,7 @@ create_input_window(void) getmaxyx(stdscr, rows, cols); inp_win = newpad(1, INP_WIN_MAX); - wbkgd(inp_win, COLOR_PAIR(1)); + wbkgd(inp_win, COLOUR_INPUT_TEXT); keypad(inp_win, TRUE); wmove(inp_win, 0, 0); prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); diff --git a/src/status_bar.c b/src/status_bar.c index 37a9bce4..efc12dde 100644 --- a/src/status_bar.c +++ b/src/status_bar.c @@ -32,6 +32,7 @@ #include #endif +#include "theme.h" #include "ui.h" static WINDOW *status_bar; diff --git a/src/theme.c b/src/theme.c index 71c2d5f4..b6e18afa 100644 --- a/src/theme.c +++ b/src/theme.c @@ -69,6 +69,7 @@ static struct colours_t { NCURSES_COLOR_T statusbaractive; NCURSES_COLOR_T statusbarnew; NCURSES_COLOR_T maintext; + NCURSES_COLOR_T inputtext; NCURSES_COLOR_T splashtext; NCURSES_COLOR_T online; NCURSES_COLOR_T away; @@ -156,6 +157,7 @@ theme_init_colours(void) init_pair(2, colour_prefs.splashtext, colour_prefs.bkgnd); init_pair(3, colour_prefs.error, colour_prefs.bkgnd); init_pair(4, colour_prefs.incoming, colour_prefs.bkgnd); + init_pair(5, colour_prefs.inputtext, colour_prefs.bkgnd); // title bar init_pair(10, colour_prefs.titlebartext, colour_prefs.titlebar); @@ -252,6 +254,9 @@ _load_colours(void) gchar *splashtext_val = g_key_file_get_string(theme, "colours", "splashtext", NULL); _set_colour(splashtext_val, &colour_prefs.splashtext, COLOR_CYAN); + gchar *inputtext_val = g_key_file_get_string(theme, "colours", "inputtext", NULL); + _set_colour(inputtext_val, &colour_prefs.inputtext, COLOR_WHITE); + gchar *online_val = g_key_file_get_string(theme, "colours", "online", NULL); _set_colour(online_val, &colour_prefs.online, COLOR_GREEN); diff --git a/src/theme.h b/src/theme.h index 1f348d61..459135d6 100644 --- a/src/theme.h +++ b/src/theme.h @@ -34,6 +34,29 @@ #include #endif +#define COLOUR_TEXT COLOR_PAIR(1) +#define COLOUR_SPLASH COLOR_PAIR(2) +#define COLOUR_ERROR COLOR_PAIR(3) +#define COLOUR_INCOMING COLOR_PAIR(4) +#define COLOUR_INPUT_TEXT COLOR_PAIR(5) +#define COLOUR_TITLE_TEXT COLOR_PAIR(10) +#define COLOUR_TITLE_BRACKET COLOR_PAIR(11) +#define COLOUR_STATUS_TEXT COLOR_PAIR(20) +#define COLOUR_STATUS_BRACKET COLOR_PAIR(21) +#define COLOUR_STATUS_ACTIVE COLOR_PAIR(22) +#define COLOUR_STATUS_NEW COLOR_PAIR(23) +#define COLOUR_ME COLOR_PAIR(30) +#define COLOUR_THEM COLOR_PAIR(31) +#define COLOUR_ROOMINFO COLOR_PAIR(40) +#define COLOUR_ONLINE COLOR_PAIR(50) +#define COLOUR_OFFLINE COLOR_PAIR(51) +#define COLOUR_AWAY COLOR_PAIR(52) +#define COLOUR_CHAT COLOR_PAIR(53) +#define COLOUR_DND COLOR_PAIR(54) +#define COLOUR_XA COLOR_PAIR(55) +#define COLOUR_TYPING COLOR_PAIR(60) +#define COLOUR_GONE COLOR_PAIR(61) + void theme_load(const char * const theme_name); void theme_init_colours(void); gboolean theme_change(const char * const theme_name); diff --git a/src/title_bar.c b/src/title_bar.c index 1ebe4f09..08c8c6db 100644 --- a/src/title_bar.c +++ b/src/title_bar.c @@ -24,6 +24,7 @@ #include #include "common.h" +#include "theme.h" #include "ui.h" static WINDOW *title_bar; diff --git a/src/ui.h b/src/ui.h index 2d441abf..5aebf546 100644 --- a/src/ui.h +++ b/src/ui.h @@ -38,28 +38,6 @@ #define INP_WIN_MAX 1000 -#define COLOUR_TEXT COLOR_PAIR(1) -#define COLOUR_SPLASH COLOR_PAIR(2) -#define COLOUR_ERROR COLOR_PAIR(3) -#define COLOUR_INCOMING COLOR_PAIR(4) -#define COLOUR_TITLE_TEXT COLOR_PAIR(10) -#define COLOUR_TITLE_BRACKET COLOR_PAIR(11) -#define COLOUR_STATUS_TEXT COLOR_PAIR(20) -#define COLOUR_STATUS_BRACKET COLOR_PAIR(21) -#define COLOUR_STATUS_ACTIVE COLOR_PAIR(22) -#define COLOUR_STATUS_NEW COLOR_PAIR(23) -#define COLOUR_ME COLOR_PAIR(30) -#define COLOUR_THEM COLOR_PAIR(31) -#define COLOUR_ROOMINFO COLOR_PAIR(40) -#define COLOUR_ONLINE COLOR_PAIR(50) -#define COLOUR_OFFLINE COLOR_PAIR(51) -#define COLOUR_AWAY COLOR_PAIR(52) -#define COLOUR_CHAT COLOR_PAIR(53) -#define COLOUR_DND COLOR_PAIR(54) -#define COLOUR_XA COLOR_PAIR(55) -#define COLOUR_TYPING COLOR_PAIR(60) -#define COLOUR_GONE COLOR_PAIR(61) - typedef enum { WIN_UNUSED, WIN_CONSOLE, From 45a26b111f8bdfe9fb534368eb29bb8963353d8a Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 22 Nov 2012 02:34:49 +0000 Subject: [PATCH 06/16] Added time to themes, and some example themes --- src/theme.c | 5 +++++ src/theme.h | 1 + src/windows.c | 6 ++++++ themes/aqua | 27 +++++++++++++++++++++++++++ themes/hacker | 27 +++++++++++++++++++++++++++ themes/headache | 27 +++++++++++++++++++++++++++ themes/original | 27 +++++++++++++++++++++++++++ themes/redsplash | 2 ++ themes/whiteness | 27 +++++++++++++++++++++++++++ themes/yellowsplash | 2 ++ 10 files changed, 151 insertions(+) create mode 100644 themes/aqua create mode 100644 themes/hacker create mode 100644 themes/headache create mode 100644 themes/original create mode 100644 themes/redsplash create mode 100644 themes/whiteness create mode 100644 themes/yellowsplash diff --git a/src/theme.c b/src/theme.c index b6e18afa..aafbd859 100644 --- a/src/theme.c +++ b/src/theme.c @@ -70,6 +70,7 @@ static struct colours_t { NCURSES_COLOR_T statusbarnew; NCURSES_COLOR_T maintext; NCURSES_COLOR_T inputtext; + NCURSES_COLOR_T timetext; NCURSES_COLOR_T splashtext; NCURSES_COLOR_T online; NCURSES_COLOR_T away; @@ -158,6 +159,7 @@ theme_init_colours(void) init_pair(3, colour_prefs.error, colour_prefs.bkgnd); init_pair(4, colour_prefs.incoming, colour_prefs.bkgnd); init_pair(5, colour_prefs.inputtext, colour_prefs.bkgnd); + init_pair(6, colour_prefs.timetext, colour_prefs.bkgnd); // title bar init_pair(10, colour_prefs.titlebartext, colour_prefs.titlebar); @@ -257,6 +259,9 @@ _load_colours(void) gchar *inputtext_val = g_key_file_get_string(theme, "colours", "inputtext", NULL); _set_colour(inputtext_val, &colour_prefs.inputtext, COLOR_WHITE); + gchar *timetext_val = g_key_file_get_string(theme, "colours", "timetext", NULL); + _set_colour(timetext_val, &colour_prefs.timetext, COLOR_WHITE); + gchar *online_val = g_key_file_get_string(theme, "colours", "online", NULL); _set_colour(online_val, &colour_prefs.online, COLOR_GREEN); diff --git a/src/theme.h b/src/theme.h index 459135d6..ca1c2245 100644 --- a/src/theme.h +++ b/src/theme.h @@ -39,6 +39,7 @@ #define COLOUR_ERROR COLOR_PAIR(3) #define COLOUR_INCOMING COLOR_PAIR(4) #define COLOUR_INPUT_TEXT COLOR_PAIR(5) +#define COLOUR_TIME COLOR_PAIR(6) #define COLOUR_TITLE_TEXT COLOR_PAIR(10) #define COLOUR_TITLE_BRACKET COLOR_PAIR(11) #define COLOUR_STATUS_TEXT COLOR_PAIR(20) diff --git a/src/windows.c b/src/windows.c index 27c73cb4..e87109c8 100644 --- a/src/windows.c +++ b/src/windows.c @@ -393,7 +393,9 @@ win_show_incomming_msg(const char * const from, const char * const message, } else { GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); + wattron(win, COLOUR_TIME); wprintw(win, "%s - ", date_fmt); + wattroff(win, COLOUR_TIME); g_date_time_unref(time); g_free(date_fmt); } @@ -430,7 +432,9 @@ win_show_incomming_msg(const char * const from, const char * const message, } else { GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); + wattron(win, COLOUR_TIME); wprintw(win, "%s - ", date_fmt); + wattroff(win, COLOUR_TIME); g_date_time_unref(time); g_free(date_fmt); } @@ -1521,7 +1525,9 @@ _win_show_time(WINDOW *win) { GDateTime *time = g_date_time_new_now_local(); gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); + wattron(win, COLOUR_TIME); wprintw(win, "%s - ", date_fmt); + wattroff(win, COLOUR_TIME); g_date_time_unref(time); g_free(date_fmt); } diff --git a/themes/aqua b/themes/aqua new file mode 100644 index 00000000..a96ae9e8 --- /dev/null +++ b/themes/aqua @@ -0,0 +1,27 @@ +[colours] +bkgnd=default +titlebar=blue +statusbar=blue +titlebartext=white +titlebarbrackets=white +statusbartext=white +statusbarbrackets=white +statusbaractive=cyan +statusbarnew=white +maintext=blue +inputtext=white +timetext=cyan +splashtext=white +online=white +away=cyan +chat=white +dnd=blue +xa=cyan +offline=blue +typing=white +gone=blue +error=white +incoming=white +roominfo=white +me=white +them=cyan diff --git a/themes/hacker b/themes/hacker new file mode 100644 index 00000000..fc38c529 --- /dev/null +++ b/themes/hacker @@ -0,0 +1,27 @@ +[colours] +bkgnd=default +titlebar=green +statusbar=green +titlebartext=black +titlebarbrackets=black +statusbartext=black +statusbarbrackets=black +statusbaractive=black +statusbarnew=black +maintext=green +inputtext=green +timetext=green +splashtext=green +online=green +away=green +chat=green +dnd=green +xa=green +offline=green +typing=green +gone=green +error=red +incoming=green +roominfo=green +me=green +them=green diff --git a/themes/headache b/themes/headache new file mode 100644 index 00000000..350b589a --- /dev/null +++ b/themes/headache @@ -0,0 +1,27 @@ +[colours] +bkgnd=default +titlebar=magenta +statusbar=default +titlebartext=white +titlebarbrackets=white +statusbartext=white +statusbarbrackets=red +statusbaractive=cyan +statusbarnew=white +maintext=blue +inputtext=yellow +timetext=green +splashtext=red +online=red +away=cyan +chat=green +dnd=megenta +xa=cyan +offline=green +typing=magenta +gone=yellow +error=red +incoming=yellow +roominfo=white +me=white +them=white diff --git a/themes/original b/themes/original new file mode 100644 index 00000000..7c401040 --- /dev/null +++ b/themes/original @@ -0,0 +1,27 @@ +[colours] +bkgnd=default +titlebar=blue +statusbar=blue +titlebartext=white +titlebarbrackets=cyan +statusbartext=white +statusbarbrackets=cyan +statusbaractive=cyan +statusbarnew=white +maintext=white +inputtext=white +timetext=white +splashtext=cyan +online=green +away=cyan +chat=green +dnd=red +xa=cyan +offline=red +typing=yellow +gone=red +error=red +incoming=yellow +roominfo=yellow +me=yellow +them=green diff --git a/themes/redsplash b/themes/redsplash new file mode 100644 index 00000000..6c6a5999 --- /dev/null +++ b/themes/redsplash @@ -0,0 +1,2 @@ +[colours] +splashtext=red diff --git a/themes/whiteness b/themes/whiteness new file mode 100644 index 00000000..0795cdf6 --- /dev/null +++ b/themes/whiteness @@ -0,0 +1,27 @@ +[colours] +bkgnd=white +titlebar=blue +statusbar=blue +titlebartext=white +titlebarbrackets=white +statusbartext=white +statusbarbrackets=white +statusbaractive=megenta +statusbarnew=red +maintext=black +inputtext=black +timetext=black +splashtext=black +online=green +away=cyan +chat=green +dnd=red +xa=cyan +offline=red +typing=yellow +gone=red +error=red +incoming=yellow +roominfo=yellow +me=yellow +them=green diff --git a/themes/yellowsplash b/themes/yellowsplash new file mode 100644 index 00000000..46510a06 --- /dev/null +++ b/themes/yellowsplash @@ -0,0 +1,2 @@ +[colours] +splashtext=yellow From 2a69f8d23f8e3e120604560818d86658ee66f088 Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 23 Nov 2012 00:19:29 +0000 Subject: [PATCH 07/16] Allocate memory for windows when needed --- Makefile.am | 2 +- src/ui.h | 1 + src/window.c | 67 +++++++ src/window.h | 37 ++++ src/windows.c | 487 ++++++++++++++++++++++++-------------------------- 5 files changed, 339 insertions(+), 255 deletions(-) create mode 100644 src/window.c create mode 100644 src/window.h diff --git a/Makefile.am b/Makefile.am index 61ac0a2f..181ffcb2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -9,7 +9,7 @@ profanity_SOURCES = src/command.c src/contact.c src/history.c src/jabber.h \ src/chat_log.h src/tinyurl.c src/tinyurl.h src/chat_session.c \ src/chat_session.h src/release.c src/release.h src/room_chat.c \ src/room_chat.h src/stanza.c src/stanza.h src/parser.c src/parser.h \ - src/theme.c src/theme.h + src/theme.c src/theme.h src/window.c src/window.h TESTS = tests/testsuite check_PROGRAMS = tests/testsuite diff --git a/src/ui.h b/src/ui.h index 5aebf546..2723289b 100644 --- a/src/ui.h +++ b/src/ui.h @@ -37,6 +37,7 @@ #include "jabber.h" #define INP_WIN_MAX 1000 +#define PAD_SIZE 1000 typedef enum { WIN_UNUSED, diff --git a/src/window.c b/src/window.c new file mode 100644 index 00000000..6f162eb4 --- /dev/null +++ b/src/window.c @@ -0,0 +1,67 @@ +/* + * window.c + * + * Copyright (C) 2012 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#include "config.h" + +#include +#include + +#include +#ifdef HAVE_NCURSES_H +#include +#endif +#ifdef HAVE_NCURSES_NCURSES_H +#include +#endif + +#include "theme.h" +#include "window.h" + +#define CONS_WIN_TITLE "_cons" + +ProfWin* +window_create(const char * const title, int cols, win_type_t type) +{ + ProfWin *new_win = malloc(sizeof(struct prof_win_t)); + new_win->from = strdup(title); + new_win->win = newpad(PAD_SIZE, cols); + wbkgd(new_win->win, COLOUR_TEXT); + new_win->y_pos = 0; + new_win->paged = 0; + new_win->unread = 0; + new_win->history_shown = 0; + new_win->type = type; + scrollok(new_win->win, TRUE); + + return new_win; +} + +void +window_free(ProfWin* window) +{ + delwin(window->win); + free(window->from); + window->from = NULL; + window->win = NULL; + free(window); + window = NULL; +} diff --git a/src/window.h b/src/window.h new file mode 100644 index 00000000..516b73a0 --- /dev/null +++ b/src/window.h @@ -0,0 +1,37 @@ +/* + * window.h + * + * Copyright (C) 2012 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#include "ui.h" + +typedef struct prof_win_t { + char *from; + WINDOW *win; + win_type_t type; + int y_pos; + int paged; + int unread; + int history_shown; +} ProfWin; + + +ProfWin* window_create(const char * const title, int cols, win_type_t type); +void window_free(ProfWin *window); diff --git a/src/windows.c b/src/windows.c index e87109c8..75154b64 100644 --- a/src/windows.c +++ b/src/windows.c @@ -47,19 +47,18 @@ #include "room_chat.h" #include "theme.h" #include "ui.h" +#include "window.h" #define CONS_WIN_TITLE "_cons" -#define PAD_SIZE 1000 #define NUM_WINS 10 // holds console at index 0 and chat wins 1 through to 9 -static struct prof_win _wins[NUM_WINS]; +static ProfWin* windows[NUM_WINS]; // the window currently being displayed -static int _curr_prof_win = 0; - -// shortcut pointer to console window -static WINDOW * _cons_win = NULL; +static int current_index = 0; +static ProfWin *current; +static ProfWin *console; // current window state static int dirty; @@ -67,6 +66,7 @@ static int dirty; // max columns for main windows, never resize below static int max_cols = 0; +static void _set_current(int index); static void _create_windows(void); static void _cons_splash_logo(void); static void _cons_show_basic_help(void); @@ -170,17 +170,14 @@ gui_resize(const int ch, const char * const input, const int size) void win_close_win(void) { - // reset the chat win to unused - strcpy(_wins[_curr_prof_win].from, ""); - wclear(_wins[_curr_prof_win].win); - _wins[_curr_prof_win].history_shown = 0; - _wins[_curr_prof_win].type = WIN_UNUSED; + window_free(current); + windows[current_index] = NULL; // set it as inactive in the status bar - status_bar_inactive(_curr_prof_win); + status_bar_inactive(current_index); // go back to console window - _curr_prof_win = 0; + _set_current(0); title_bar_title(); dirty = TRUE; @@ -189,19 +186,19 @@ win_close_win(void) int win_in_chat(void) { - return (_wins[_curr_prof_win].type == WIN_CHAT); + return (current->type == WIN_CHAT); } int win_in_groupchat(void) { - return (_wins[_curr_prof_win].type == WIN_MUC); + return (current->type == WIN_MUC); } int win_in_private_chat(void) { - return (_wins[_curr_prof_win].type == WIN_PRIVATE); + return (current->type == WIN_PRIVATE); } void @@ -211,7 +208,7 @@ win_show_wins(void) int count = 0; for (i = 1; i < NUM_WINS; i++) { - if (_wins[i].type != WIN_UNUSED) { + if (windows[i] != NULL) { count++; } } @@ -228,42 +225,43 @@ win_show_wins(void) if (count != 0) { for (i = 1; i < NUM_WINS; i++) { - if (_wins[i].type != WIN_UNUSED) { - _win_show_time(_cons_win); + if (windows[i] != NULL) { + ProfWin *window = windows[i]; + _win_show_time(console->win); - switch (_wins[i].type) + switch (window->type) { case WIN_CHAT: - wprintw(_cons_win, "%d: chat %s", i + 1, _wins[i].from); - PContact contact = contact_list_get_contact(_wins[i].from); + wprintw(console->win, "%d: chat %s", i + 1, window->from); + PContact contact = contact_list_get_contact(window->from); if (contact != NULL) { if (p_contact_name(contact) != NULL) { - wprintw(_cons_win, " (%s)", p_contact_name(contact)); + wprintw(console->win, " (%s)", p_contact_name(contact)); } - wprintw(_cons_win, " - %s", p_contact_presence(contact)); + wprintw(console->win, " - %s", p_contact_presence(contact)); } - if (_wins[i].unread > 0) { - wprintw(_cons_win, ", %d unread", _wins[i].unread); + if (window->unread > 0) { + wprintw(console->win, ", %d unread", window->unread); } break; case WIN_PRIVATE: - wprintw(_cons_win, "%d: private %s", i + 1, _wins[i].from); + wprintw(console->win, "%d: private %s", i + 1, window->from); - if (_wins[i].unread > 0) { - wprintw(_cons_win, ", %d unread", _wins[i].unread); + if (window->unread > 0) { + wprintw(console->win, ", %d unread", window->unread); } break; case WIN_MUC: - wprintw(_cons_win, "%d: room %s", i + 1, _wins[i].from); + wprintw(console->win, "%d: room %s", i + 1, window->from); - if (_wins[i].unread > 0) { - wprintw(_cons_win, ", %d unread", _wins[i].unread); + if (window->unread > 0) { + wprintw(console->win, ", %d unread", window->unread); } break; @@ -272,7 +270,7 @@ win_show_wins(void) break; } - wprintw(_cons_win, "\n"); + wprintw(console->win, "\n"); } } } @@ -281,9 +279,8 @@ win_show_wins(void) char * win_get_recipient(void) { - struct prof_win current = _wins[_curr_prof_win]; - char *recipient = (char *) malloc(sizeof(char) * (strlen(current.from) + 1)); - strcpy(recipient, current.from); + char *recipient = (char *) malloc(sizeof(char) * (strlen(current->from) + 1)); + strcpy(recipient, current->from); return recipient; } @@ -298,7 +295,7 @@ win_show_typing(const char * const from) _cons_show_typing(from); // have chat window but not currently in it - } else if (win_index != _curr_prof_win) { + } else if (win_index != current_index) { _cons_show_typing(from); dirty = TRUE; @@ -349,8 +346,8 @@ win_no_activity(void) // loop through regular chat windows and update states for (i = 1; i < NUM_WINS; i++) { - if (_wins[i].type == WIN_CHAT) { - char *recipient = _wins[i].from; + if ((windows[i] != NULL) && (windows[i]->type == WIN_CHAT)) { + char *recipient = windows[i]->from; chat_session_no_activity(recipient); if (chat_session_is_gone(recipient) && @@ -384,10 +381,10 @@ win_show_incomming_msg(const char * const from, const char * const message, if (win_index == NUM_WINS) win_index = _new_prof_win(from, win_type); - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; // currently viewing chat window with sender - if (win_index == _curr_prof_win) { + if (win_index == current_index) { if (tv_stamp == NULL) { _win_show_time(win); } else { @@ -422,7 +419,7 @@ win_show_incomming_msg(const char * const from, const char * const message, if (prefs_get_flash()) flash(); - _wins[win_index].unread++; + windows[win_index]->unread++; if (prefs_get_chlog() && prefs_get_history()) { _win_show_history(win, win_index, from); } @@ -471,10 +468,10 @@ win_show_error_msg(const char * const from, const char *err_msg) win_index = _find_prof_win_index(from); // chat window exists if (win_index < NUM_WINS) { - win = _wins[win_index].win; + win = windows[win_index]->win; _win_show_time(win); _win_show_error_msg(win, err_msg); - if (win_index == _curr_prof_win) { + if (win_index == current_index) { dirty = TRUE; } } @@ -492,14 +489,14 @@ win_show_gone(const char * const from) win_index = _find_prof_win_index(from); // chat window exists if (win_index < NUM_WINS) { - win = _wins[win_index].win; + win = windows[win_index]->win; _win_show_time(win); wattron(win, COLOUR_GONE); wprintw(win, "*%s ", from); wprintw(win, "has left the conversation."); wprintw(win, "\n"); wattroff(win, COLOUR_GONE); - if (win_index == _curr_prof_win) { + if (win_index == current_index) { dirty = TRUE; } } @@ -525,13 +522,13 @@ win_show_system_msg(const char * const from, const char *message) status_bar_active(win_index); dirty = TRUE; } - win = _wins[win_index].win; + win = windows[win_index]->win; _win_show_time(win); wprintw(win, "*%s %s\n", bare_jid, message); // this is the current window - if (win_index == _curr_prof_win) { + if (win_index == current_index) { dirty = TRUE; } } @@ -617,7 +614,7 @@ win_show_outgoing_msg(const char * const from, const char * const to, win_index = _new_prof_win(to, WIN_CHAT); } - win = _wins[win_index].win; + win = windows[win_index]->win; if (prefs_get_chlog() && prefs_get_history()) { _win_show_history(win, win_index, to); @@ -633,7 +630,7 @@ win_show_outgoing_msg(const char * const from, const char * const to, // use existing window } else { - win = _wins[win_index].win; + win = windows[win_index]->win; } _win_show_time(win); @@ -667,7 +664,7 @@ void win_show_room_roster(const char * const room) { int win_index = _find_prof_win_index(room); - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; GList *roster = room_get_roster(room); @@ -693,7 +690,7 @@ win_show_room_roster(const char * const room) wattroff(win, COLOUR_ONLINE); } - if (win_index == _curr_prof_win) + if (win_index == current_index) dirty = TRUE; } @@ -701,14 +698,14 @@ void win_show_room_member_offline(const char * const room, const char * const nick) { int win_index = _find_prof_win_index(room); - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; _win_show_time(win); wattron(win, COLOUR_OFFLINE); wprintw(win, "-- %s has left the room.\n", nick); wattroff(win, COLOUR_OFFLINE); - if (win_index == _curr_prof_win) + if (win_index == current_index) dirty = TRUE; } @@ -717,14 +714,14 @@ win_show_room_member_online(const char * const room, const char * const nick, const char * const show, const char * const status) { int win_index = _find_prof_win_index(room); - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; _win_show_time(win); wattron(win, COLOUR_ONLINE); wprintw(win, "++ %s has joined the room.\n", nick); wattroff(win, COLOUR_ONLINE); - if (win_index == _curr_prof_win) + if (win_index == current_index) dirty = TRUE; } @@ -734,11 +731,11 @@ win_show_room_member_presence(const char * const room, const char * const nick, { int win_index = _find_prof_win_index(room); if (win_index != NUM_WINS) { - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; _show_status_string(win, nick, show, status, "++", "online"); } - if (win_index == _curr_prof_win) + if (win_index == current_index) dirty = TRUE; } @@ -747,14 +744,14 @@ win_show_room_member_nick_change(const char * const room, const char * const old_nick, const char * const nick) { int win_index = _find_prof_win_index(room); - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; _win_show_time(win); wattron(win, COLOUR_THEM); wprintw(win, "** %s is now known as %s\n", old_nick, nick); wattroff(win, COLOUR_THEM); - if (win_index == _curr_prof_win) + if (win_index == current_index) dirty = TRUE; } @@ -762,14 +759,14 @@ void win_show_room_nick_change(const char * const room, const char * const nick) { int win_index = _find_prof_win_index(room); - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; _win_show_time(win); wattron(win, COLOUR_ME); wprintw(win, "** You are now known as %s\n", nick); wattroff(win, COLOUR_ME); - if (win_index == _curr_prof_win) + if (win_index == current_index) dirty = TRUE; } @@ -778,7 +775,7 @@ win_show_room_history(const char * const room_jid, const char * const nick, GTimeVal tv_stamp, const char * const message) { int win_index = _find_prof_win_index(room_jid); - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; GDateTime *time = g_date_time_new_from_timeval_utc(&tv_stamp); gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); @@ -795,7 +792,7 @@ win_show_room_history(const char * const room_jid, const char * const nick, _win_show_message(win, message); } - if (win_index == _curr_prof_win) + if (win_index == current_index) dirty = TRUE; } @@ -804,7 +801,7 @@ win_show_room_message(const char * const room_jid, const char * const nick, const char * const message) { int win_index = _find_prof_win_index(room_jid); - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; _win_show_time(win); if (strcmp(nick, room_get_nick_for_room(room_jid)) != 0) { @@ -833,7 +830,7 @@ win_show_room_message(const char * const room_jid, const char * const nick, } // currently in groupchat window - if (win_index == _curr_prof_win) { + if (win_index == current_index) { status_bar_active(win_index); dirty = TRUE; @@ -841,7 +838,7 @@ win_show_room_message(const char * const room_jid, const char * const nick, } else { status_bar_new(win_index); _cons_show_incoming_message(nick, win_index); - if (_curr_prof_win == 0) { + if (current_index == 0) { dirty = TRUE; } @@ -851,7 +848,7 @@ win_show_room_message(const char * const room_jid, const char * const nick, } } - _wins[win_index].unread++; + windows[win_index]->unread++; } if (strcmp(nick, room_get_nick_for_room(room_jid)) != 0) { @@ -870,7 +867,7 @@ void win_show_room_subject(const char * const room_jid, const char * const subject) { int win_index = _find_prof_win_index(room_jid); - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; wattron(win, COLOUR_ROOMINFO); wprintw(win, "Room subject: "); @@ -878,7 +875,7 @@ win_show_room_subject(const char * const room_jid, const char * const subject) wprintw(win, "%s\n", subject); // currently in groupchat window - if (win_index == _curr_prof_win) { + if (win_index == current_index) { status_bar_active(win_index); dirty = TRUE; @@ -892,7 +889,7 @@ void win_show_room_broadcast(const char * const room_jid, const char * const message) { int win_index = _find_prof_win_index(room_jid); - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; wattron(win, COLOUR_ROOMINFO); wprintw(win, "Room message: "); @@ -900,7 +897,7 @@ win_show_room_broadcast(const char * const room_jid, const char * const message) wprintw(win, "%s\n", message); // currently in groupchat window - if (win_index == _curr_prof_win) { + if (win_index == current_index) { status_bar_active(win_index); dirty = TRUE; @@ -913,7 +910,7 @@ win_show_room_broadcast(const char * const room_jid, const char * const message) void win_show(const char * const msg) { - WINDOW *win = _wins[_curr_prof_win].win; + WINDOW *win = current->win; _win_show_time(win); wprintw(win, "%s\n", msg); @@ -923,7 +920,7 @@ win_show(const char * const msg) void win_bad_show(const char * const msg) { - WINDOW *win = _wins[_curr_prof_win].win; + WINDOW *win = current->win; _win_show_time(win); wattron(win, COLOUR_ERROR); wprintw(win, "%s\n", msg); @@ -936,15 +933,15 @@ void win_contact_online(const char * const from, const char * const show, const char * const status) { - _show_status_string(_cons_win, from, show, status, "++", "online"); + _show_status_string(console->win, from, show, status, "++", "online"); int win_index = _find_prof_win_index(from); if (win_index != NUM_WINS) { - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; _show_status_string(win, from, show, status, "++", "online"); } - if (win_index == _curr_prof_win) + if (win_index == current_index) dirty = TRUE; } @@ -952,15 +949,15 @@ void win_contact_offline(const char * const from, const char * const show, const char * const status) { - _show_status_string(_cons_win, from, show, status, "--", "offline"); + _show_status_string(console->win, from, show, status, "--", "offline"); int win_index = _find_prof_win_index(from); if (win_index != NUM_WINS) { - WINDOW *win = _wins[win_index].win; + WINDOW *win = windows[win_index]->win; _show_status_string(win, from, show, status, "--", "offline"); } - if (win_index == _curr_prof_win) + if (win_index == current_index) dirty = TRUE; } @@ -982,15 +979,15 @@ win_disconnected(void) int i; // show message in all active chats for (i = 1; i < NUM_WINS; i++) { - if (strcmp(_wins[i].from, "") != 0) { - WINDOW *win = _wins[i].win; + if (windows[i] != NULL) { + WINDOW *win = windows[i]->win; _win_show_time(win); wattron(win, COLOUR_ERROR); wprintw(win, "%s\n", "Lost connection."); wattroff(win, COLOUR_ERROR); // if current win, set dirty - if (i == _curr_prof_win) { + if (i == current_index) { dirty = TRUE; } } @@ -1076,7 +1073,7 @@ cons_prefs(void) cons_show(""); - if (_curr_prof_win == 0) + if (current_index == 0) dirty = TRUE; } @@ -1109,7 +1106,7 @@ cons_help(void) cons_show("/help [command] - Detailed help on a specific command."); cons_show(""); - if (_curr_prof_win == 0) + if (current_index == 0) dirty = TRUE; } @@ -1120,7 +1117,7 @@ cons_basic_help(void) cons_show("Basic Commands:"); _cons_show_basic_help(); - if (_curr_prof_win == 0) + if (current_index == 0) dirty = TRUE; } @@ -1140,7 +1137,7 @@ cons_settings_help(void) cons_show(""); - if (_curr_prof_win == 0) + if (current_index == 0) dirty = TRUE; } @@ -1160,7 +1157,7 @@ cons_presence_help(void) cons_show(""); - if (_curr_prof_win == 0) + if (current_index == 0) dirty = TRUE; } @@ -1179,7 +1176,7 @@ cons_navigation_help(void) cons_show("PAGE UP, PAGE DOWN : Page the main window."); cons_show(""); - if (_curr_prof_win == 0) + if (current_index == 0) dirty = TRUE; } @@ -1204,21 +1201,21 @@ cons_bad_show(const char * const msg, ...) va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); - _win_show_time(_cons_win); - wattron(_cons_win, COLOUR_ERROR); - wprintw(_cons_win, "%s\n", fmt_msg->str); - wattroff(_cons_win, COLOUR_ERROR); + _win_show_time(console->win); + wattron(console->win, COLOUR_ERROR); + wprintw(console->win, "%s\n", fmt_msg->str); + wattroff(console->win, COLOUR_ERROR); g_string_free(fmt_msg, TRUE); va_end(arg); - if (_curr_prof_win == 0) + if (current_index == 0) dirty = TRUE; } void cons_show_time(void) { - _win_show_time(_cons_win); + _win_show_time(console->win); } void @@ -1228,31 +1225,31 @@ cons_show(const char * const msg, ...) va_start(arg, msg); GString *fmt_msg = g_string_new(NULL); g_string_vprintf(fmt_msg, msg, arg); - _win_show_time(_cons_win); - wprintw(_cons_win, "%s\n", fmt_msg->str); + _win_show_time(console->win); + wprintw(console->win, "%s\n", fmt_msg->str); g_string_free(fmt_msg, TRUE); va_end(arg); - if (_curr_prof_win == 0) + if (current_index == 0) dirty = TRUE; } void cons_show_word(const char * const word) { - wprintw(_cons_win, "%s", word); + wprintw(console->win, "%s", word); - if (_curr_prof_win == 0) + if (current_index == 0) dirty = TRUE; } void cons_bad_command(const char * const cmd) { - _win_show_time(_cons_win); - wprintw(_cons_win, "Unknown command: %s\n", cmd); + _win_show_time(console->win); + wprintw(console->win, "Unknown command: %s\n", cmd); - if (_curr_prof_win == 0) + if (current_index == 0) dirty = TRUE; } @@ -1267,15 +1264,17 @@ void win_page_off(void) { int rows = getmaxy(stdscr); - _wins[_curr_prof_win].paged = 0; + ProfWin *window = windows[current_index]; - int y = getcury(_wins[_curr_prof_win].win); + window->paged = 0; + + int y = getcury(window->win); int size = rows - 3; - _wins[_curr_prof_win].y_pos = y - (size - 1); - if (_wins[_curr_prof_win].y_pos < 0) - _wins[_curr_prof_win].y_pos = 0; + window->y_pos = y - (size - 1); + if (window->y_pos < 0) + window->y_pos = 0; dirty = TRUE; } @@ -1285,38 +1284,10 @@ _create_windows(void) { int cols = getmaxx(stdscr); max_cols = cols; - - // create the console window in 0 - struct prof_win cons; - strcpy(cons.from, CONS_WIN_TITLE); - cons.win = newpad(PAD_SIZE, cols); - wbkgd(cons.win, COLOUR_TEXT); - cons.y_pos = 0; - cons.paged = 0; - cons.unread = 0; - cons.history_shown = 0; - cons.type = WIN_CONSOLE; - scrollok(cons.win, TRUE); - - _wins[0] = cons; - + windows[0] = window_create(CONS_WIN_TITLE, cols, WIN_CONSOLE); + console = windows[0]; + current = console; cons_about(); - - // create the chat windows - int i; - for (i = 1; i < NUM_WINS; i++) { - struct prof_win chat; - strcpy(chat.from, ""); - chat.win = newpad(PAD_SIZE, cols); - wbkgd(chat.win, COLOUR_TEXT); - chat.y_pos = 0; - chat.paged = 0; - chat.unread = 0; - chat.history_shown = 0; - chat.type = WIN_UNUSED; - scrollok(chat.win, TRUE); - _wins[i] = chat; - } } void @@ -1325,42 +1296,40 @@ cons_about(void) int rows, cols; getmaxyx(stdscr, rows, cols); - _cons_win = _wins[0].win; - if (prefs_get_showsplash()) { _cons_splash_logo(); } else { - _win_show_time(_cons_win); + _win_show_time(console->win); if (strcmp(PACKAGE_STATUS, "development") == 0) { - wprintw(_cons_win, "Welcome to Profanity, version %sdev\n", PACKAGE_VERSION); + wprintw(console->win, "Welcome to Profanity, version %sdev\n", PACKAGE_VERSION); } else { - wprintw(_cons_win, "Welcome to Profanity, version %s\n", PACKAGE_VERSION); + wprintw(console->win, "Welcome to Profanity, version %s\n", PACKAGE_VERSION); } } - _win_show_time(_cons_win); - wprintw(_cons_win, "Copyright (C) 2012 James Booth <%s>.\n", PACKAGE_BUGREPORT); - _win_show_time(_cons_win); - wprintw(_cons_win, "License GPLv3+: GNU GPL version 3 or later \n"); - _win_show_time(_cons_win); - wprintw(_cons_win, "\n"); - _win_show_time(_cons_win); - wprintw(_cons_win, "This is free software; you are free to change and redistribute it.\n"); - _win_show_time(_cons_win); - wprintw(_cons_win, "There is NO WARRANTY, to the extent permitted by law.\n"); - _win_show_time(_cons_win); - wprintw(_cons_win, "\n"); - _win_show_time(_cons_win); - wprintw(_cons_win, "Type '/help' to show complete help.\n"); - _win_show_time(_cons_win); - wprintw(_cons_win, "\n"); + _win_show_time(console->win); + wprintw(console->win, "Copyright (C) 2012 James Booth <%s>.\n", PACKAGE_BUGREPORT); + _win_show_time(console->win); + wprintw(console->win, "License GPLv3+: GNU GPL version 3 or later \n"); + _win_show_time(console->win); + wprintw(console->win, "\n"); + _win_show_time(console->win); + wprintw(console->win, "This is free software; you are free to change and redistribute it.\n"); + _win_show_time(console->win); + wprintw(console->win, "There is NO WARRANTY, to the extent permitted by law.\n"); + _win_show_time(console->win); + wprintw(console->win, "\n"); + _win_show_time(console->win); + wprintw(console->win, "Type '/help' to show complete help.\n"); + _win_show_time(console->win); + wprintw(console->win, "\n"); if (prefs_get_vercheck()) { cons_check_version(FALSE); } - prefresh(_cons_win, 0, 0, 1, 0, rows-3, cols-1); + prefresh(console->win, 0, 0, 1, 0, rows-3, cols-1); dirty = TRUE; } @@ -1375,13 +1344,13 @@ cons_check_version(gboolean not_available_msg) if (relase_valid) { if (_new_release(latest_release)) { - _win_show_time(_cons_win); - wprintw(_cons_win, "A new version of Profanity is available: %s", latest_release); - _win_show_time(_cons_win); - wprintw(_cons_win, "Check for details.\n"); + _win_show_time(console->win); + wprintw(console->win, "A new version of Profanity is available: %s", latest_release); + _win_show_time(console->win); + wprintw(console->win, "Check for details.\n"); free(latest_release); - _win_show_time(_cons_win); - wprintw(_cons_win, "\n"); + _win_show_time(console->win); + wprintw(console->win, "\n"); } else { if (not_available_msg) { cons_show("No new version available."); @@ -1421,62 +1390,63 @@ _new_release(char *found_version) static void _cons_splash_logo(void) { - _win_show_time(_cons_win); - wprintw(_cons_win, "Welcome to\n"); + _win_show_time(console->win); + wprintw(console->win, "Welcome to\n"); - _win_show_time(_cons_win); - wattron(_cons_win, COLOUR_SPLASH); - wprintw(_cons_win, " ___ _ \n"); - wattroff(_cons_win, COLOUR_SPLASH); + _win_show_time(console->win); + wattron(console->win, COLOUR_SPLASH); + wprintw(console->win, " ___ _ \n"); + wattroff(console->win, COLOUR_SPLASH); - _win_show_time(_cons_win); - wattron(_cons_win, COLOUR_SPLASH); - wprintw(_cons_win, " / __) (_)_ \n"); - wattroff(_cons_win, COLOUR_SPLASH); + _win_show_time(console->win); + wattron(console->win, COLOUR_SPLASH); + wprintw(console->win, " / __) (_)_ \n"); + wattroff(console->win, COLOUR_SPLASH); - _win_show_time(_cons_win); - wattron(_cons_win, COLOUR_SPLASH); - wprintw(_cons_win, " ____ ____ ___ | |__ ____ ____ _| |_ _ _ \n"); - wattroff(_cons_win, COLOUR_SPLASH); + _win_show_time(console->win); + wattron(console->win, COLOUR_SPLASH); + wprintw(console->win, " ____ ____ ___ | |__ ____ ____ _| |_ _ _ \n"); + wattroff(console->win, COLOUR_SPLASH); - _win_show_time(_cons_win); - wattron(_cons_win, COLOUR_SPLASH); - wprintw(_cons_win, "| _ \\ / ___) _ \\| __) _ | _ \\| | _) | | |\n"); - wattroff(_cons_win, COLOUR_SPLASH); + _win_show_time(console->win); + wattron(console->win, COLOUR_SPLASH); + wprintw(console->win, "| _ \\ / ___) _ \\| __) _ | _ \\| | _) | | |\n"); + wattroff(console->win, COLOUR_SPLASH); - _win_show_time(_cons_win); - wattron(_cons_win, COLOUR_SPLASH); - wprintw(_cons_win, "| | | | | | |_| | | ( ( | | | | | | |_| |_| |\n"); - wattroff(_cons_win, COLOUR_SPLASH); + _win_show_time(console->win); + wattron(console->win, COLOUR_SPLASH); + wprintw(console->win, "| | | | | | |_| | | ( ( | | | | | | |_| |_| |\n"); + wattroff(console->win, COLOUR_SPLASH); - _win_show_time(_cons_win); - wattron(_cons_win, COLOUR_SPLASH); - wprintw(_cons_win, "| ||_/|_| \\___/|_| \\_||_|_| |_|_|\\___)__ |\n"); - wattroff(_cons_win, COLOUR_SPLASH); + _win_show_time(console->win); + wattron(console->win, COLOUR_SPLASH); + wprintw(console->win, "| ||_/|_| \\___/|_| \\_||_|_| |_|_|\\___)__ |\n"); + wattroff(console->win, COLOUR_SPLASH); - _win_show_time(_cons_win); - wattron(_cons_win, COLOUR_SPLASH); - wprintw(_cons_win, "|_| (____/ \n"); - wattroff(_cons_win, COLOUR_SPLASH); + _win_show_time(console->win); + wattron(console->win, COLOUR_SPLASH); + wprintw(console->win, "|_| (____/ \n"); + wattroff(console->win, COLOUR_SPLASH); - _win_show_time(_cons_win); - wprintw(_cons_win, "\n"); - _win_show_time(_cons_win); + _win_show_time(console->win); + wprintw(console->win, "\n"); + _win_show_time(console->win); if (strcmp(PACKAGE_STATUS, "development") == 0) { - wprintw(_cons_win, "Version %sdev\n", PACKAGE_VERSION); + wprintw(console->win, "Version %sdev\n", PACKAGE_VERSION); } else { - wprintw(_cons_win, "Version %s\n", PACKAGE_VERSION); + wprintw(console->win, "Version %s\n", PACKAGE_VERSION); } } static int _find_prof_win_index(const char * const contact) { - // find the chat window for recipient int i; - for (i = 1; i < NUM_WINS; i++) - if (strcmp(_wins[i].from, contact) == 0) + for (i = 1; i < NUM_WINS; i++) { + if ((windows[i] != NULL) && (strcmp(windows[i]->from, contact) == 0)) { break; + } + } return i; } @@ -1485,15 +1455,14 @@ static int _new_prof_win(const char * const contact, win_type_t type) { int i; - // find the first unused one - for (i = 1; i < NUM_WINS; i++) - if (strcmp(_wins[i].from, "") == 0) + for (i = 1; i < NUM_WINS; i++) { + if (windows[i] == NULL) { break; + } + } - // set it up - strcpy(_wins[i].from, contact); - wclear(_wins[i].win); - _wins[i].type = type; + int cols = getmaxx(stdscr); + windows[i] = window_create(contact, cols, type); return i; } @@ -1502,16 +1471,17 @@ static void _win_switch_if_active(const int i) { win_page_off(); - if (strcmp(_wins[i].from, "") != 0) { - _curr_prof_win = i; + if (windows[i] != NULL) { + current_index = i; + current = windows[current_index]; win_page_off(); - _wins[i].unread = 0; + current->unread = 0; if (i == 0) { title_bar_title(); } else { - title_bar_set_recipient(_wins[i].from); + title_bar_set_recipient(current->from); title_bar_draw();; status_bar_active(i); } @@ -1566,8 +1536,7 @@ _current_window_refresh(void) int rows, cols; getmaxyx(stdscr, rows, cols); - WINDOW *current = _wins[_curr_prof_win].win; - prefresh(current, _wins[_curr_prof_win].y_pos, 0, 1, 0, rows-3, cols-1); + prefresh(current->win, current->y_pos, 0, 1, 0, rows-3, cols-1); } void @@ -1582,12 +1551,13 @@ _win_resize_all(void) int i; for (i = 0; i < NUM_WINS; i++) { - wresize(_wins[i].win, PAD_SIZE, cols); + if (windows[i] != NULL) { + wresize(windows[i]->win, PAD_SIZE, cols); + } } } - WINDOW *current = _wins[_curr_prof_win].win; - prefresh(current, _wins[_curr_prof_win].y_pos, 0, 1, 0, rows-3, cols-1); + prefresh(current->win, current->y_pos, 0, 1, 0, rows-3, cols-1); } static void @@ -1653,19 +1623,19 @@ _show_status_string(WINDOW *win, const char * const from, static void _cons_show_typing(const char * const short_from) { - _win_show_time(_cons_win); - wattron(_cons_win, COLOUR_TYPING); - wprintw(_cons_win, "!! %s is typing a message...\n", short_from); - wattroff(_cons_win, COLOUR_TYPING); + _win_show_time(console->win); + wattron(console->win, COLOUR_TYPING); + wprintw(console->win, "!! %s is typing a message...\n", short_from); + wattroff(console->win, COLOUR_TYPING); } static void _cons_show_incoming_message(const char * const short_from, const int win_index) { - _win_show_time(_cons_win); - wattron(_cons_win, COLOUR_INCOMING); - wprintw(_cons_win, "<< incoming from %s (%d)\n", short_from, win_index + 1); - wattroff(_cons_win, COLOUR_INCOMING); + _win_show_time(console->win); + wattron(console->win, COLOUR_INCOMING); + wprintw(console->win, "<< incoming from %s (%d)\n", short_from, win_index + 1); + wattroff(console->win, COLOUR_INCOMING); } static void @@ -1676,48 +1646,48 @@ _cons_show_contact(PContact contact) const char *presence = p_contact_presence(contact); const char *status = p_contact_status(contact); - _win_show_time(_cons_win); + _win_show_time(console->win); if (strcmp(presence, "online") == 0) { - wattron(_cons_win, COLOUR_ONLINE); + wattron(console->win, COLOUR_ONLINE); } else if (strcmp(presence, "away") == 0) { - wattron(_cons_win, COLOUR_AWAY); + wattron(console->win, COLOUR_AWAY); } else if (strcmp(presence, "chat") == 0) { - wattron(_cons_win, COLOUR_CHAT); + wattron(console->win, COLOUR_CHAT); } else if (strcmp(presence, "dnd") == 0) { - wattron(_cons_win, COLOUR_DND); + wattron(console->win, COLOUR_DND); } else if (strcmp(presence, "xa") == 0) { - wattron(_cons_win, COLOUR_XA); + wattron(console->win, COLOUR_XA); } else { - wattron(_cons_win, COLOUR_OFFLINE); + wattron(console->win, COLOUR_OFFLINE); } - wprintw(_cons_win, "%s", jid); + wprintw(console->win, "%s", jid); if (name != NULL) { - wprintw(_cons_win, " (%s)", name); + wprintw(console->win, " (%s)", name); } - wprintw(_cons_win, " is %s", presence); + wprintw(console->win, " is %s", presence); if (status != NULL) { - wprintw(_cons_win, ", \"%s\"", p_contact_status(contact)); + wprintw(console->win, ", \"%s\"", p_contact_status(contact)); } - wprintw(_cons_win, "\n"); + wprintw(console->win, "\n"); if (strcmp(presence, "online") == 0) { - wattroff(_cons_win, COLOUR_ONLINE); + wattroff(console->win, COLOUR_ONLINE); } else if (strcmp(presence, "away") == 0) { - wattroff(_cons_win, COLOUR_AWAY); + wattroff(console->win, COLOUR_AWAY); } else if (strcmp(presence, "chat") == 0) { - wattroff(_cons_win, COLOUR_CHAT); + wattroff(console->win, COLOUR_CHAT); } else if (strcmp(presence, "dnd") == 0) { - wattroff(_cons_win, COLOUR_DND); + wattroff(console->win, COLOUR_DND); } else if (strcmp(presence, "xa") == 0) { - wattroff(_cons_win, COLOUR_XA); + wattroff(console->win, COLOUR_XA); } else { - wattroff(_cons_win, COLOUR_OFFLINE); + wattroff(console->win, COLOUR_OFFLINE); } } @@ -1751,10 +1721,10 @@ static void _win_handle_page(const int * const ch) { int rows = getmaxy(stdscr); - int y = getcury(_wins[_curr_prof_win].win); + int y = getcury(current->win); int page_space = rows - 4; - int *page_start = &_wins[_curr_prof_win].y_pos; + int *page_start = &(current->y_pos); // page up if (*ch == KEY_PPAGE) { @@ -1764,7 +1734,7 @@ _win_handle_page(const int * const ch) if (*page_start < 0) *page_start = 0; - _wins[_curr_prof_win].paged = 1; + current->paged = 1; dirty = TRUE; // page down @@ -1779,7 +1749,7 @@ _win_handle_page(const int * const ch) else if (*page_start >= y) *page_start = y - page_space; - _wins[_curr_prof_win].paged = 1; + current->paged = 1; dirty = TRUE; } } @@ -1790,7 +1760,9 @@ _win_get_unread(void) int i; gint result = 0; for (i = 0; i < NUM_WINS; i++) { - result += _wins[i].unread; + if (windows[i] != NULL) { + result += windows[i]->unread; + } } return result; } @@ -1798,16 +1770,23 @@ _win_get_unread(void) static void _win_show_history(WINDOW *win, int win_index, const char * const contact) { - if (!_wins[win_index].history_shown) { + if (!windows[win_index]->history_shown) { GSList *history = NULL; history = chat_log_get_previous(jabber_get_jid(), contact, history); while (history != NULL) { wprintw(win, "%s\n", history->data); history = g_slist_next(history); } - _wins[win_index].history_shown = 1; + windows[win_index]->history_shown = 1; g_slist_free_full(history, free); } } +void +_set_current(int index) +{ + current_index = index; + current = windows[current_index]; +} + From 721df8ca48b8cbf0026f3a36a3d17b45cb82b850 Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 23 Nov 2012 02:00:51 +0000 Subject: [PATCH 08/16] Stop ctrl-c quitting with /ctrlc off --- src/command.c | 19 +++++++++++++++++++ src/input_win.c | 22 +++++++++++++++++++--- src/preferences.c | 13 +++++++++++++ src/preferences.h | 2 ++ src/windows.c | 7 ++++++- 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/command.c b/src/command.c index 52f7fac9..93f4e4fa 100644 --- a/src/command.c +++ b/src/command.c @@ -116,6 +116,7 @@ static gboolean _cmd_set_chlog(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_history(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_states(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_outtype(gchar **args, struct cmd_help_t help); +static gboolean _cmd_set_ctrlc(gchar **args, struct cmd_help_t help); static gboolean _cmd_vercheck(gchar **args, struct cmd_help_t help); static gboolean _cmd_away(gchar **args, struct cmd_help_t help); static gboolean _cmd_online(gchar **args, struct cmd_help_t help); @@ -485,6 +486,15 @@ static struct cmd_t setting_commands[] = "Config file value : maxsize=bytes", NULL } } }, + { "/ctrlc", + _cmd_set_ctrlc, parse_args, 1, 1, + { "/ctrlc on|off", "Set ctrl-c to quit profanity.", + { "/ctrlc on|off", + "---------------", + "Setting this option will allow you to quit using ctrl-c.", + NULL } } }, + + { "/priority", _cmd_set_priority, parse_args, 1, 1, { "/priority ", "Set priority for connection.", @@ -855,6 +865,8 @@ _cmd_complete_parameters(char *input, int *size) prefs_autocomplete_boolean_choice); _parameter_autocomplete(input, size, "/states", prefs_autocomplete_boolean_choice); + _parameter_autocomplete(input, size, "/ctrlc", + prefs_autocomplete_boolean_choice); _parameter_autocomplete(input, size, "/outtype", prefs_autocomplete_boolean_choice); _parameter_autocomplete(input, size, "/flash", @@ -1389,6 +1401,13 @@ _cmd_set_states(gchar **args, struct cmd_help_t help) "Sending chat states", prefs_set_states); } +static gboolean +_cmd_set_ctrlc(gchar **args, struct cmd_help_t help) +{ + return _cmd_set_boolean_preference(args, help, "/ctrlc", + "Ctrl-C quit", prefs_set_ctrlc); +} + static gboolean _cmd_set_outtype(gchar **args, struct cmd_help_t help) { diff --git a/src/input_win.c b/src/input_win.c index 5ad64d80..ee6317ac 100644 --- a/src/input_win.c +++ b/src/input_win.c @@ -241,17 +241,33 @@ _handle_edit(const int ch, char *input, int *size) char *next = NULL; int inp_y = 0; int inp_x = 0; + int next_ch; getmaxyx(stdscr, rows, cols); getyx(inp_win, inp_y, inp_x); switch(ch) { - case 27: // ESC - *size = 0; - inp_clear(); + case 3: // CTRL-C + if (prefs_get_ctrlc()) { + exit(0); + } return 1; + case 27: + next_ch = wgetch(inp_win); + + // ESC + if (next_ch == ERR) { + *size = 0; + inp_clear(); + return 1; + + // ALT- + } else { + return 1; + } + case 127: case KEY_BACKSPACE: contact_list_reset_search_attempts(); diff --git a/src/preferences.c b/src/preferences.c index a44af242..d7626655 100644 --- a/src/preferences.c +++ b/src/preferences.c @@ -132,6 +132,19 @@ prefs_set_beep(gboolean value) _save_prefs(); } +gboolean +prefs_get_ctrlc(void) +{ + return g_key_file_get_boolean(prefs, "ui", "ctrlc", NULL); +} + +void +prefs_set_ctrlc(gboolean value) +{ + g_key_file_set_boolean(prefs, "ui", "ctrlc", value); + _save_prefs(); +} + gchar * prefs_get_theme(void) { diff --git a/src/preferences.h b/src/preferences.h index f4547b07..84eadd1c 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -65,6 +65,8 @@ gboolean prefs_get_outtype(void); void prefs_set_outtype(gboolean value); gchar * prefs_get_theme(void); void prefs_set_theme(gchar *value); +gboolean prefs_get_ctrlc(void); +void prefs_set_ctrlc(gboolean value); void prefs_set_notify_message(gboolean value); gboolean prefs_get_notify_message(void); diff --git a/src/windows.c b/src/windows.c index 75154b64..7d92a419 100644 --- a/src/windows.c +++ b/src/windows.c @@ -106,7 +106,7 @@ gui_init(void) { log_info("Initialising UI"); initscr(); - cbreak(); + raw(); keypad(stdscr, TRUE); win_load_colours(); @@ -1050,6 +1050,11 @@ cons_prefs(void) else cons_show("Version checking : OFF"); + if (prefs_get_ctrlc()) + cons_show("Ctrl-c quits : ON"); + else + cons_show("Ctrl-c quits : OFF"); + if (prefs_get_notify_message()) cons_show("Message notifications : ON"); else From faeea56bb5a500247f85f95f1d39845c7915be7f Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 23 Nov 2012 02:54:35 +0000 Subject: [PATCH 09/16] Revert "Stop ctrl-c quitting with /ctrlc off" This reverts commit 721df8ca48b8cbf0026f3a36a3d17b45cb82b850. --- src/command.c | 19 ------------------- src/input_win.c | 22 +++------------------- src/preferences.c | 13 ------------- src/preferences.h | 2 -- src/windows.c | 7 +------ 5 files changed, 4 insertions(+), 59 deletions(-) diff --git a/src/command.c b/src/command.c index 93f4e4fa..52f7fac9 100644 --- a/src/command.c +++ b/src/command.c @@ -116,7 +116,6 @@ static gboolean _cmd_set_chlog(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_history(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_states(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_outtype(gchar **args, struct cmd_help_t help); -static gboolean _cmd_set_ctrlc(gchar **args, struct cmd_help_t help); static gboolean _cmd_vercheck(gchar **args, struct cmd_help_t help); static gboolean _cmd_away(gchar **args, struct cmd_help_t help); static gboolean _cmd_online(gchar **args, struct cmd_help_t help); @@ -486,15 +485,6 @@ static struct cmd_t setting_commands[] = "Config file value : maxsize=bytes", NULL } } }, - { "/ctrlc", - _cmd_set_ctrlc, parse_args, 1, 1, - { "/ctrlc on|off", "Set ctrl-c to quit profanity.", - { "/ctrlc on|off", - "---------------", - "Setting this option will allow you to quit using ctrl-c.", - NULL } } }, - - { "/priority", _cmd_set_priority, parse_args, 1, 1, { "/priority ", "Set priority for connection.", @@ -865,8 +855,6 @@ _cmd_complete_parameters(char *input, int *size) prefs_autocomplete_boolean_choice); _parameter_autocomplete(input, size, "/states", prefs_autocomplete_boolean_choice); - _parameter_autocomplete(input, size, "/ctrlc", - prefs_autocomplete_boolean_choice); _parameter_autocomplete(input, size, "/outtype", prefs_autocomplete_boolean_choice); _parameter_autocomplete(input, size, "/flash", @@ -1401,13 +1389,6 @@ _cmd_set_states(gchar **args, struct cmd_help_t help) "Sending chat states", prefs_set_states); } -static gboolean -_cmd_set_ctrlc(gchar **args, struct cmd_help_t help) -{ - return _cmd_set_boolean_preference(args, help, "/ctrlc", - "Ctrl-C quit", prefs_set_ctrlc); -} - static gboolean _cmd_set_outtype(gchar **args, struct cmd_help_t help) { diff --git a/src/input_win.c b/src/input_win.c index ee6317ac..5ad64d80 100644 --- a/src/input_win.c +++ b/src/input_win.c @@ -241,33 +241,17 @@ _handle_edit(const int ch, char *input, int *size) char *next = NULL; int inp_y = 0; int inp_x = 0; - int next_ch; getmaxyx(stdscr, rows, cols); getyx(inp_win, inp_y, inp_x); switch(ch) { - case 3: // CTRL-C - if (prefs_get_ctrlc()) { - exit(0); - } + case 27: // ESC + *size = 0; + inp_clear(); return 1; - case 27: - next_ch = wgetch(inp_win); - - // ESC - if (next_ch == ERR) { - *size = 0; - inp_clear(); - return 1; - - // ALT- - } else { - return 1; - } - case 127: case KEY_BACKSPACE: contact_list_reset_search_attempts(); diff --git a/src/preferences.c b/src/preferences.c index d7626655..a44af242 100644 --- a/src/preferences.c +++ b/src/preferences.c @@ -132,19 +132,6 @@ prefs_set_beep(gboolean value) _save_prefs(); } -gboolean -prefs_get_ctrlc(void) -{ - return g_key_file_get_boolean(prefs, "ui", "ctrlc", NULL); -} - -void -prefs_set_ctrlc(gboolean value) -{ - g_key_file_set_boolean(prefs, "ui", "ctrlc", value); - _save_prefs(); -} - gchar * prefs_get_theme(void) { diff --git a/src/preferences.h b/src/preferences.h index 84eadd1c..f4547b07 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -65,8 +65,6 @@ gboolean prefs_get_outtype(void); void prefs_set_outtype(gboolean value); gchar * prefs_get_theme(void); void prefs_set_theme(gchar *value); -gboolean prefs_get_ctrlc(void); -void prefs_set_ctrlc(gboolean value); void prefs_set_notify_message(gboolean value); gboolean prefs_get_notify_message(void); diff --git a/src/windows.c b/src/windows.c index 7d92a419..75154b64 100644 --- a/src/windows.c +++ b/src/windows.c @@ -106,7 +106,7 @@ gui_init(void) { log_info("Initialising UI"); initscr(); - raw(); + cbreak(); keypad(stdscr, TRUE); win_load_colours(); @@ -1050,11 +1050,6 @@ cons_prefs(void) else cons_show("Version checking : OFF"); - if (prefs_get_ctrlc()) - cons_show("Ctrl-c quits : ON"); - else - cons_show("Ctrl-c quits : OFF"); - if (prefs_get_notify_message()) cons_show("Message notifications : ON"); else From 7c7e95ea4ecc943d64fe8f77eaec2c43cbc4f94e Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 23 Nov 2012 02:56:38 +0000 Subject: [PATCH 10/16] Switched to ncurses raw mode --- src/windows.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/windows.c b/src/windows.c index 75154b64..9c52d483 100644 --- a/src/windows.c +++ b/src/windows.c @@ -106,7 +106,7 @@ gui_init(void) { log_info("Initialising UI"); initscr(); - cbreak(); + raw(); keypad(stdscr, TRUE); win_load_colours(); From f7ad1c03358b1d927c252360366f332226753289 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 24 Nov 2012 00:23:24 +0000 Subject: [PATCH 11/16] Don't output ctrl characters --- src/input_win.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/input_win.c b/src/input_win.c index 5ad64d80..9a85a99b 100644 --- a/src/input_win.c +++ b/src/input_win.c @@ -66,6 +66,7 @@ static int pad_start = 0; static int _handle_edit(const int ch, char *input, int *size); static int _printable(const int ch); +static gboolean _special_key(const int ch); void create_input_window(void) @@ -373,6 +374,13 @@ _printable(const int ch) ch != KEY_F(4) && ch != KEY_F(5) && ch != KEY_F(6) && ch != KEY_F(7) && ch != KEY_F(8) && ch != KEY_F(9) && ch != KEY_F(10) && ch!= KEY_F(11) && ch != KEY_F(12) && - ch != KEY_IC && ch != KEY_EIC && ch != KEY_RESIZE); + ch != KEY_IC && ch != KEY_EIC && ch != KEY_RESIZE && + !_special_key(ch)); } +static gboolean +_special_key(const int ch) +{ + char *str = unctrl(ch); + return ((strlen(str) > 1) && g_str_has_prefix(str, "^")); +} From d30d31b57dfaa0967a28574c509641c1cbddd65b Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 24 Nov 2012 01:57:24 +0000 Subject: [PATCH 12/16] Allow alt-num to select window --- src/input_win.c | 54 +++++++++++++++++++++++++++++++++++++++++++++--- src/status_bar.c | 24 ++++++++++----------- src/ui.h | 2 ++ src/windows.c | 29 +++++++++++++------------- 4 files changed, 79 insertions(+), 30 deletions(-) diff --git a/src/input_win.c b/src/input_win.c index 9a85a99b..accfc687 100644 --- a/src/input_win.c +++ b/src/input_win.c @@ -216,6 +216,12 @@ inp_put_back(void) prefresh(inp_win, 0, pad_start, rows-1, 0, rows-1, cols-1); } +int +inp_get_next_char(void) +{ + return wgetch(inp_win); +} + void inp_replace_input(char *input, const char * const new_input, int *size) { @@ -242,6 +248,7 @@ _handle_edit(const int ch, char *input, int *size) char *next = NULL; int inp_y = 0; int inp_x = 0; + int next_ch; getmaxyx(stdscr, rows, cols); getyx(inp_win, inp_y, inp_x); @@ -249,9 +256,50 @@ _handle_edit(const int ch, char *input, int *size) switch(ch) { case 27: // ESC - *size = 0; - inp_clear(); - return 1; + // check for ALT-num + next_ch = inp_get_next_char(); + if (next_ch != ERR) { + switch (next_ch) + { + case '1': + win_switch_if_active(0); + break; + case '2': + win_switch_if_active(1); + break; + case '3': + win_switch_if_active(2); + break; + case '4': + win_switch_if_active(3); + break; + case '5': + win_switch_if_active(4); + break; + case '6': + win_switch_if_active(5); + break; + case '7': + win_switch_if_active(6); + break; + case '8': + win_switch_if_active(7); + break; + case '9': + win_switch_if_active(8); + break; + case '0': + win_switch_if_active(9); + break; + default: + break; + } + return 1; + } else { + *size = 0; + inp_clear(); + return 1; + } case 127: case KEY_BACKSPACE: diff --git a/src/status_bar.c b/src/status_bar.c index efc12dde..d700a129 100644 --- a/src/status_bar.c +++ b/src/status_bar.c @@ -37,7 +37,7 @@ static WINDOW *status_bar; static char *message = NULL; -static char _active[29] = "[ ][ ][ ][ ][ ][ ][ ][ ][ ]"; +static char _active[29] = "[ ][ ][ ][ ][ ][ ][ ][ ][ ]"; static int is_active[9]; static int is_new[9]; static int dirty; @@ -59,7 +59,7 @@ create_status_bar(void) status_bar = newwin(1, cols, rows-2, 0); wbkgd(status_bar, COLOUR_STATUS_TEXT); wattron(status_bar, COLOUR_STATUS_BRACKET); - mvwprintw(status_bar, 0, cols - 29, _active); + mvwprintw(status_bar, 0, cols - 28, _active); wattroff(status_bar, COLOUR_STATUS_BRACKET); last_time = g_date_time_new_now_local(); @@ -99,7 +99,7 @@ status_bar_resize(void) wbkgd(status_bar, COLOUR_STATUS_TEXT); wclear(status_bar); wattron(status_bar, COLOUR_STATUS_BRACKET); - mvwprintw(status_bar, 0, cols - 29, _active); + mvwprintw(status_bar, 0, cols - 28, _active); wattroff(status_bar, COLOUR_STATUS_BRACKET); for(i = 0; i < 9; i++) { @@ -126,9 +126,9 @@ status_bar_inactive(const int win) int cols = getmaxx(stdscr); - mvwaddch(status_bar, 0, cols - 29 + active_pos, ' '); + mvwaddch(status_bar, 0, cols - 28 + active_pos, ' '); if (win == 9) - mvwaddch(status_bar, 0, cols - 29 + active_pos + 1, ' '); + mvwaddch(status_bar, 0, cols - 28 + active_pos + 1, ' '); dirty = TRUE; } @@ -145,9 +145,9 @@ status_bar_active(const int win) wattron(status_bar, COLOUR_STATUS_ACTIVE); if (win < 9) - mvwprintw(status_bar, 0, cols - 29 + active_pos, "%d", win+1); + mvwprintw(status_bar, 0, cols - 28 + active_pos, "%d", win+1); else - mvwprintw(status_bar, 0, cols - 29 + active_pos, "10"); + mvwprintw(status_bar, 0, cols - 28 + active_pos, "0"); wattroff(status_bar, COLOUR_STATUS_ACTIVE); dirty = TRUE; @@ -166,9 +166,9 @@ status_bar_new(const int win) wattron(status_bar, COLOUR_STATUS_NEW); wattron(status_bar, A_BLINK); if (win < 9) - mvwprintw(status_bar, 0, cols - 29 + active_pos, "%d", win+1); + mvwprintw(status_bar, 0, cols - 28 + active_pos, "%d", win+1); else - mvwprintw(status_bar, 0, cols - 29 + active_pos, "10"); + mvwprintw(status_bar, 0, cols - 28 + active_pos, "0"); wattroff(status_bar, COLOUR_STATUS_NEW); wattroff(status_bar, A_BLINK); @@ -199,7 +199,7 @@ status_bar_print_message(const char * const msg) int cols = getmaxx(stdscr); wattron(status_bar, COLOUR_STATUS_BRACKET); - mvwprintw(status_bar, 0, cols - 29, _active); + mvwprintw(status_bar, 0, cols - 28, _active); wattroff(status_bar, COLOUR_STATUS_BRACKET); int i; @@ -232,7 +232,7 @@ status_bar_clear(void) int cols = getmaxx(stdscr); wattron(status_bar, COLOUR_STATUS_BRACKET); - mvwprintw(status_bar, 0, cols - 29, _active); + mvwprintw(status_bar, 0, cols - 28, _active); wattroff(status_bar, COLOUR_STATUS_BRACKET); dirty = TRUE; @@ -251,7 +251,7 @@ status_bar_clear_message(void) int cols = getmaxx(stdscr); wattron(status_bar, COLOUR_STATUS_BRACKET); - mvwprintw(status_bar, 0, cols - 29, _active); + mvwprintw(status_bar, 0, cols - 28, _active); wattroff(status_bar, COLOUR_STATUS_BRACKET); int i; diff --git a/src/ui.h b/src/ui.h index 2723289b..bab4d47c 100644 --- a/src/ui.h +++ b/src/ui.h @@ -103,6 +103,7 @@ void win_bad_show(const char * const msg); void win_remind(void); void win_activity(void); void win_no_activity(void); +void win_switch_if_active(const int i); void win_join_chat(const char * const room, const char * const nick); void win_show_room_roster(const char * const room); @@ -166,5 +167,6 @@ void inp_non_block(void); void inp_block(void); void inp_get_password(char *passwd); void inp_replace_input(char *input, const char * const new_input, int *size); +int inp_get_next_char(void); #endif diff --git a/src/windows.c b/src/windows.c index 9c52d483..b06cdbcd 100644 --- a/src/windows.c +++ b/src/windows.c @@ -74,7 +74,6 @@ static void _cons_show_contact(PContact contact); static int _find_prof_win_index(const char * const contact); static int _new_prof_win(const char * const contact, win_type_t type); static void _current_window_refresh(void); -static void _win_switch_if_active(const int i); static void _win_show_time(WINDOW *win); static void _win_show_user(WINDOW *win, const char * const user, const int colour); static void _win_show_message(WINDOW *win, const char * const message); @@ -644,7 +643,7 @@ win_show_outgoing_msg(const char * const from, const char * const to, _win_show_user(win, from, 0); _win_show_message(win, message); } - _win_switch_if_active(win_index); + win_switch_if_active(win_index); } void @@ -657,7 +656,7 @@ win_join_chat(const char * const room, const char * const nick) win_index = _new_prof_win(room, WIN_MUC); } - _win_switch_if_active(win_index); + win_switch_if_active(win_index); } void @@ -1467,8 +1466,8 @@ _new_prof_win(const char * const contact, win_type_t type) return i; } -static void -_win_switch_if_active(const int i) +void +win_switch_if_active(const int i) { win_page_off(); if (windows[i] != NULL) { @@ -1695,25 +1694,25 @@ static void _win_handle_switch(const int * const ch) { if (*ch == KEY_F(1)) { - _win_switch_if_active(0); + win_switch_if_active(0); } else if (*ch == KEY_F(2)) { - _win_switch_if_active(1); + win_switch_if_active(1); } else if (*ch == KEY_F(3)) { - _win_switch_if_active(2); + win_switch_if_active(2); } else if (*ch == KEY_F(4)) { - _win_switch_if_active(3); + win_switch_if_active(3); } else if (*ch == KEY_F(5)) { - _win_switch_if_active(4); + win_switch_if_active(4); } else if (*ch == KEY_F(6)) { - _win_switch_if_active(5); + win_switch_if_active(5); } else if (*ch == KEY_F(7)) { - _win_switch_if_active(6); + win_switch_if_active(6); } else if (*ch == KEY_F(8)) { - _win_switch_if_active(7); + win_switch_if_active(7); } else if (*ch == KEY_F(9)) { - _win_switch_if_active(8); + win_switch_if_active(8); } else if (*ch == KEY_F(10)) { - _win_switch_if_active(9); + win_switch_if_active(9); } } From 4e94654122525fb2b03426c172e8731033f39eca Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 24 Nov 2012 20:41:27 +0000 Subject: [PATCH 13/16] Added console to statusbar window list --- src/status_bar.c | 80 +++++++++++++++++++++++++----------------------- src/windows.c | 1 + 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/status_bar.c b/src/status_bar.c index d700a129..a00a2ad3 100644 --- a/src/status_bar.c +++ b/src/status_bar.c @@ -37,9 +37,9 @@ static WINDOW *status_bar; static char *message = NULL; -static char _active[29] = "[ ][ ][ ][ ][ ][ ][ ][ ][ ]"; -static int is_active[9]; -static int is_new[9]; +static char _active[31] = "[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]"; +static int is_active[10]; +static int is_new[10]; static int dirty; static GDateTime *last_time; @@ -51,7 +51,9 @@ create_status_bar(void) int rows, cols, i; getmaxyx(stdscr, rows, cols); - for (i = 0; i < 9; i++) { + is_active[0] = TRUE; + is_new[0] = FALSE; + for (i = 1; i < 10; i++) { is_active[i] = FALSE; is_new[i] = FALSE; } @@ -59,7 +61,7 @@ create_status_bar(void) status_bar = newwin(1, cols, rows-2, 0); wbkgd(status_bar, COLOUR_STATUS_TEXT); wattron(status_bar, COLOUR_STATUS_BRACKET); - mvwprintw(status_bar, 0, cols - 28, _active); + mvwprintw(status_bar, 0, cols - 31, _active); wattroff(status_bar, COLOUR_STATUS_BRACKET); last_time = g_date_time_new_now_local(); @@ -99,18 +101,18 @@ status_bar_resize(void) wbkgd(status_bar, COLOUR_STATUS_TEXT); wclear(status_bar); wattron(status_bar, COLOUR_STATUS_BRACKET); - mvwprintw(status_bar, 0, cols - 28, _active); + mvwprintw(status_bar, 0, cols - 31, _active); wattroff(status_bar, COLOUR_STATUS_BRACKET); - for(i = 0; i < 9; i++) { + for(i = 0; i < 10; i++) { if (is_new[i]) - status_bar_new(i+1); + status_bar_new(i); else if (is_active[i]) - status_bar_active(i+1); + status_bar_active(i); } if (message != NULL) - mvwprintw(status_bar, 0, 9, message); + mvwprintw(status_bar, 0, 10, message); last_time = g_date_time_new_now_local(); dirty = TRUE; @@ -119,16 +121,14 @@ status_bar_resize(void) void status_bar_inactive(const int win) { - is_active[win-1] = FALSE; - is_new[win-1] = FALSE; + is_active[win] = FALSE; + is_new[win] = FALSE; - int active_pos = 1 + ((win -1) * 3); + int active_pos = 1 + (win * 3); int cols = getmaxx(stdscr); - mvwaddch(status_bar, 0, cols - 28 + active_pos, ' '); - if (win == 9) - mvwaddch(status_bar, 0, cols - 28 + active_pos + 1, ' '); + mvwaddch(status_bar, 0, cols - 31 + active_pos, ' '); dirty = TRUE; } @@ -136,18 +136,18 @@ status_bar_inactive(const int win) void status_bar_active(const int win) { - is_active[win-1] = TRUE; - is_new[win-1] = FALSE; + is_active[win] = TRUE; + is_new[win] = FALSE; - int active_pos = 1 + ((win -1) * 3); + int active_pos = 1 + (win * 3); int cols = getmaxx(stdscr); wattron(status_bar, COLOUR_STATUS_ACTIVE); - if (win < 9) - mvwprintw(status_bar, 0, cols - 28 + active_pos, "%d", win+1); + if (win < 10) + mvwprintw(status_bar, 0, cols - 31 + active_pos, "%d", win+1); else - mvwprintw(status_bar, 0, cols - 28 + active_pos, "0"); + mvwprintw(status_bar, 0, cols - 31 + active_pos, "0"); wattroff(status_bar, COLOUR_STATUS_ACTIVE); dirty = TRUE; @@ -156,19 +156,19 @@ status_bar_active(const int win) void status_bar_new(const int win) { - is_active[win-1] = TRUE; - is_new[win-1] = TRUE; + is_active[win] = TRUE; + is_new[win] = TRUE; - int active_pos = 1 + ((win -1) * 3); + int active_pos = 1 + (win * 3); int cols = getmaxx(stdscr); wattron(status_bar, COLOUR_STATUS_NEW); wattron(status_bar, A_BLINK); - if (win < 9) - mvwprintw(status_bar, 0, cols - 28 + active_pos, "%d", win+1); + if (win < 10) + mvwprintw(status_bar, 0, cols - 31 + active_pos, "%d", win+1); else - mvwprintw(status_bar, 0, cols - 28 + active_pos, "0"); + mvwprintw(status_bar, 0, cols - 31 + active_pos, "0"); wattroff(status_bar, COLOUR_STATUS_NEW); wattroff(status_bar, A_BLINK); @@ -194,20 +194,20 @@ status_bar_print_message(const char * const msg) message = (char *) malloc((strlen(msg) + 1) * sizeof(char)); strcpy(message, msg); - mvwprintw(status_bar, 0, 9, message); + mvwprintw(status_bar, 0, 10, message); int cols = getmaxx(stdscr); wattron(status_bar, COLOUR_STATUS_BRACKET); - mvwprintw(status_bar, 0, cols - 28, _active); + mvwprintw(status_bar, 0, cols - 31, _active); wattroff(status_bar, COLOUR_STATUS_BRACKET); int i; - for(i = 0; i < 9; i++) { + for(i = 0; i < 10; i++) { if (is_new[i]) - status_bar_new(i+1); + status_bar_new(i); else if (is_active[i]) - status_bar_active(i+1); + status_bar_active(i); } dirty = TRUE; @@ -222,7 +222,9 @@ status_bar_clear(void) } int i; - for (i = 0; i < 9; i++) { + is_active[0] = TRUE; + is_new[0] = FALSE; + for (i = 1; i < 10; i++) { is_active[i] = FALSE; is_new[i] = FALSE; } @@ -232,7 +234,7 @@ status_bar_clear(void) int cols = getmaxx(stdscr); wattron(status_bar, COLOUR_STATUS_BRACKET); - mvwprintw(status_bar, 0, cols - 28, _active); + mvwprintw(status_bar, 0, cols - 31, _active); wattroff(status_bar, COLOUR_STATUS_BRACKET); dirty = TRUE; @@ -251,15 +253,15 @@ status_bar_clear_message(void) int cols = getmaxx(stdscr); wattron(status_bar, COLOUR_STATUS_BRACKET); - mvwprintw(status_bar, 0, cols - 28, _active); + mvwprintw(status_bar, 0, cols - 31, _active); wattroff(status_bar, COLOUR_STATUS_BRACKET); int i; - for(i = 0; i < 9; i++) { + for(i = 0; i < 10; i++) { if (is_new[i]) - status_bar_new(i+1); + status_bar_new(i); else if (is_active[i]) - status_bar_active(i+1); + status_bar_active(i); } dirty = TRUE; diff --git a/src/windows.c b/src/windows.c index b06cdbcd..d0d47065 100644 --- a/src/windows.c +++ b/src/windows.c @@ -114,6 +114,7 @@ gui_init(void) create_title_bar(); create_status_bar(); + status_bar_active(0); create_input_window(); _create_windows(); From a20c0569a6b059eed751135c934dcce795ac32e9 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 24 Nov 2012 23:36:43 +0000 Subject: [PATCH 14/16] Status bar active on console messages when not in console --- src/windows.c | 79 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/src/windows.c b/src/windows.c index d0d47065..44b9f0b1 100644 --- a/src/windows.c +++ b/src/windows.c @@ -178,6 +178,7 @@ win_close_win(void) // go back to console window _set_current(0); + status_bar_active(0); title_bar_title(); dirty = TRUE; @@ -207,22 +208,17 @@ win_show_wins(void) int i = 0; int count = 0; + cons_show(""); + cons_show("Active windows:"); + _win_show_time(console->win); + wprintw(console->win, "1: Console\n"); + for (i = 1; i < NUM_WINS; i++) { if (windows[i] != NULL) { count++; } } - cons_show(""); - - if (count == 0) { - cons_show("No active windows."); - } else if (count == 1) { - cons_show("1 active window:"); - } else { - cons_show("%d active windows:", count); - } - if (count != 0) { for (i = 1; i < NUM_WINS; i++) { if (windows[i] != NULL) { @@ -1073,8 +1069,11 @@ cons_prefs(void) cons_show(""); - if (current_index == 0) + if (current_index == 0) { dirty = TRUE; + } else { + status_bar_new(0); + } } static void @@ -1106,8 +1105,11 @@ cons_help(void) cons_show("/help [command] - Detailed help on a specific command."); cons_show(""); - if (current_index == 0) + if (current_index == 0) { dirty = TRUE; + } else { + status_bar_new(0); + } } void @@ -1117,8 +1119,11 @@ cons_basic_help(void) cons_show("Basic Commands:"); _cons_show_basic_help(); - if (current_index == 0) + if (current_index == 0) { dirty = TRUE; + } else { + status_bar_new(0); + } } void @@ -1137,8 +1142,11 @@ cons_settings_help(void) cons_show(""); - if (current_index == 0) + if (current_index == 0) { dirty = TRUE; + } else { + status_bar_new(0); + } } void @@ -1157,8 +1165,11 @@ cons_presence_help(void) cons_show(""); - if (current_index == 0) + if (current_index == 0) { dirty = TRUE; + } else { + status_bar_new(0); + } } void @@ -1176,8 +1187,11 @@ cons_navigation_help(void) cons_show("PAGE UP, PAGE DOWN : Page the main window."); cons_show(""); - if (current_index == 0) + if (current_index == 0) { dirty = TRUE; + } else { + status_bar_new(0); + } } void @@ -1208,8 +1222,11 @@ cons_bad_show(const char * const msg, ...) g_string_free(fmt_msg, TRUE); va_end(arg); - if (current_index == 0) + if (current_index == 0) { dirty = TRUE; + } else { + status_bar_new(0); + } } void @@ -1230,8 +1247,11 @@ cons_show(const char * const msg, ...) g_string_free(fmt_msg, TRUE); va_end(arg); - if (current_index == 0) + if (current_index == 0) { dirty = TRUE; + } else { + status_bar_new(0); + } } void @@ -1239,8 +1259,11 @@ cons_show_word(const char * const word) { wprintw(console->win, "%s", word); - if (current_index == 0) + if (current_index == 0) { dirty = TRUE; + } else { + status_bar_new(0); + } } void @@ -1249,8 +1272,11 @@ cons_bad_command(const char * const cmd) _win_show_time(console->win); wprintw(console->win, "Unknown command: %s\n", cmd); - if (current_index == 0) + if (current_index == 0) { dirty = TRUE; + } else { + status_bar_new(0); + } } void @@ -1331,7 +1357,11 @@ cons_about(void) prefresh(console->win, 0, 0, 1, 0, rows-3, cols-1); - dirty = TRUE; + if (current_index == 0) { + dirty = TRUE; + } else { + status_bar_new(0); + } } void @@ -1357,6 +1387,12 @@ cons_check_version(gboolean not_available_msg) cons_show(""); } } + + if (current_index == 0) { + dirty = TRUE; + } else { + status_bar_new(0); + } } } } @@ -1480,6 +1516,7 @@ win_switch_if_active(const int i) if (i == 0) { title_bar_title(); + status_bar_active(0); } else { title_bar_set_recipient(current->from); title_bar_draw();; From 66d3e6ee02a28d9a93976da00cc12f8906dcf44a Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 24 Nov 2012 23:58:10 +0000 Subject: [PATCH 15/16] Messages not lost when run out of windows Incoming messages are shown in the console when all windows are full --- src/status_bar.c | 4 +- src/windows.c | 141 ++++++++++++++++++++++++++++++----------------- 2 files changed, 92 insertions(+), 53 deletions(-) diff --git a/src/status_bar.c b/src/status_bar.c index a00a2ad3..52d90439 100644 --- a/src/status_bar.c +++ b/src/status_bar.c @@ -144,7 +144,7 @@ status_bar_active(const int win) int cols = getmaxx(stdscr); wattron(status_bar, COLOUR_STATUS_ACTIVE); - if (win < 10) + if (win+1 < 10) mvwprintw(status_bar, 0, cols - 31 + active_pos, "%d", win+1); else mvwprintw(status_bar, 0, cols - 31 + active_pos, "0"); @@ -165,7 +165,7 @@ status_bar_new(const int win) wattron(status_bar, COLOUR_STATUS_NEW); wattron(status_bar, A_BLINK); - if (win < 10) + if (win+1 < 10) mvwprintw(status_bar, 0, cols - 31 + active_pos, "%d", win+1); else mvwprintw(status_bar, 0, cols - 31 + active_pos, "0"); diff --git a/src/windows.c b/src/windows.c index 44b9f0b1..c105c48e 100644 --- a/src/windows.c +++ b/src/windows.c @@ -377,70 +377,106 @@ win_show_incomming_msg(const char * const from, const char * const message, if (win_index == NUM_WINS) win_index = _new_prof_win(from, win_type); - WINDOW *win = windows[win_index]->win; - - // currently viewing chat window with sender - if (win_index == current_index) { + // no spare windows left + if (win_index == 0) { if (tv_stamp == NULL) { - _win_show_time(win); + _win_show_time(console->win); } else { GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); - wattron(win, COLOUR_TIME); - wprintw(win, "%s - ", date_fmt); - wattroff(win, COLOUR_TIME); + wattron(console->win, COLOUR_TIME); + wprintw(console->win, "%s - ", date_fmt); + wattroff(console->win, COLOUR_TIME); g_date_time_unref(time); g_free(date_fmt); } if (strncmp(message, "/me ", 4) == 0) { - wattron(win, COLOUR_THEM); - wprintw(win, "*%s ", from); - wprintw(win, message + 4); - wprintw(win, "\n"); - wattroff(win, COLOUR_THEM); + wattron(console->win, COLOUR_THEM); + wprintw(console->win, "*%s ", from); + wprintw(console->win, message + 4); + wprintw(console->win, "\n"); + wattroff(console->win, COLOUR_THEM); } else { - _win_show_user(win, from, 1); - _win_show_message(win, message); + _win_show_user(console->win, from, 1); + _win_show_message(console->win, message); } - title_bar_set_typing(FALSE); - title_bar_draw(); - status_bar_active(win_index); - dirty = TRUE; - // not currently viewing chat window with sender + cons_bad_show("Windows all used, close a window to respond."); + + if (current_index == 0) { + dirty = TRUE; + } else { + status_bar_new(0); + } + + // window found or created } else { - status_bar_new(win_index); - _cons_show_incoming_message(from, win_index); - if (prefs_get_flash()) - flash(); + WINDOW *win = windows[win_index]->win; - windows[win_index]->unread++; - if (prefs_get_chlog() && prefs_get_history()) { - _win_show_history(win, win_index, from); - } + // currently viewing chat window with sender + if (win_index == current_index) { + if (tv_stamp == NULL) { + _win_show_time(win); + } else { + GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); + gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); + wattron(win, COLOUR_TIME); + wprintw(win, "%s - ", date_fmt); + wattroff(win, COLOUR_TIME); + g_date_time_unref(time); + g_free(date_fmt); + } - if (tv_stamp == NULL) { - _win_show_time(win); + if (strncmp(message, "/me ", 4) == 0) { + wattron(win, COLOUR_THEM); + wprintw(win, "*%s ", from); + wprintw(win, message + 4); + wprintw(win, "\n"); + wattroff(win, COLOUR_THEM); + } else { + _win_show_user(win, from, 1); + _win_show_message(win, message); + } + title_bar_set_typing(FALSE); + title_bar_draw(); + status_bar_active(win_index); + dirty = TRUE; + + // not currently viewing chat window with sender } else { - GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); - gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); - wattron(win, COLOUR_TIME); - wprintw(win, "%s - ", date_fmt); - wattroff(win, COLOUR_TIME); - g_date_time_unref(time); - g_free(date_fmt); - } + status_bar_new(win_index); + _cons_show_incoming_message(from, win_index); + if (prefs_get_flash()) + flash(); - if (strncmp(message, "/me ", 4) == 0) { - wattron(win, COLOUR_THEM); - wprintw(win, "*%s ", from); - wprintw(win, message + 4); - wprintw(win, "\n"); - wattroff(win, COLOUR_THEM); - } else { - _win_show_user(win, from, 1); - _win_show_message(win, message); + windows[win_index]->unread++; + if (prefs_get_chlog() && prefs_get_history()) { + _win_show_history(win, win_index, from); + } + + if (tv_stamp == NULL) { + _win_show_time(win); + } else { + GDateTime *time = g_date_time_new_from_timeval_utc(tv_stamp); + gchar *date_fmt = g_date_time_format(time, "%H:%M:%S"); + wattron(win, COLOUR_TIME); + wprintw(win, "%s - ", date_fmt); + wattroff(win, COLOUR_TIME); + g_date_time_unref(time); + g_free(date_fmt); + } + + if (strncmp(message, "/me ", 4) == 0) { + wattron(win, COLOUR_THEM); + wprintw(win, "*%s ", from); + wprintw(win, message + 4); + wprintw(win, "\n"); + wattroff(win, COLOUR_THEM); + } else { + _win_show_user(win, from, 1); + _win_show_message(win, message); + } } } @@ -1497,10 +1533,13 @@ _new_prof_win(const char * const contact, win_type_t type) } } - int cols = getmaxx(stdscr); - windows[i] = window_create(contact, cols, type); - - return i; + if (i != NUM_WINS) { + int cols = getmaxx(stdscr); + windows[i] = window_create(contact, cols, type); + return i; + } else { + return 0; + } } void From 921f026cba6bae0f7450eea0c32b5ea36716cd0a Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 25 Nov 2012 02:14:38 +0000 Subject: [PATCH 16/16] Added auto reconnect, and /reconnect command to control interval --- src/chat_session.c | 30 ++++--------------- src/command.c | 35 ++++++++++++++++++++++ src/jabber.c | 72 ++++++++++++++++++++++++++++++++++++++++------ src/preferences.c | 13 +++++++++ src/preferences.h | 2 ++ src/windows.c | 9 ++++++ 6 files changed, 129 insertions(+), 32 deletions(-) diff --git a/src/chat_session.c b/src/chat_session.c index 9a62c298..a2650b10 100644 --- a/src/chat_session.c +++ b/src/chat_session.c @@ -95,9 +95,7 @@ chat_session_set_composing(const char * const recipient) { ChatSession session = g_hash_table_lookup(sessions, recipient); - if (session == NULL) { - log_error("No chat session found for %s.", recipient); - } else { + if (session != NULL) { if (session->state != CHAT_STATE_COMPOSING) { session->sent = FALSE; } @@ -111,9 +109,7 @@ chat_session_no_activity(const char * const recipient) { ChatSession session = g_hash_table_lookup(sessions, recipient); - if (session == NULL) { - log_error("No chat session found for %s.", recipient); - } else { + if (session != NULL) { if (session->active_timer != NULL) { gdouble elapsed = g_timer_elapsed(session->active_timer, NULL); @@ -146,9 +142,7 @@ chat_session_set_sent(const char * const recipient) { ChatSession session = g_hash_table_lookup(sessions, recipient); - if (session == NULL) { - log_error("No chat session found for %s.", recipient); - } else { + if (session != NULL) { session->sent = TRUE; } } @@ -159,7 +153,6 @@ chat_session_get_sent(const char * const recipient) ChatSession session = g_hash_table_lookup(sessions, recipient); if (session == NULL) { - log_error("No chat session found for %s.", recipient); return FALSE; } else { return session->sent; @@ -178,7 +171,6 @@ chat_session_is_inactive(const char * const recipient) ChatSession session = g_hash_table_lookup(sessions, recipient); if (session == NULL) { - log_error("No chat session found for %s.", recipient); return FALSE; } else { return (session->state == CHAT_STATE_INACTIVE); @@ -191,7 +183,6 @@ chat_session_is_active(const char * const recipient) ChatSession session = g_hash_table_lookup(sessions, recipient); if (session == NULL) { - log_error("No chat session found for %s.", recipient); return FALSE; } else { return (session->state == CHAT_STATE_ACTIVE); @@ -203,9 +194,7 @@ chat_session_set_active(const char * const recipient) { ChatSession session = g_hash_table_lookup(sessions, recipient); - if (session == NULL) { - log_error("No chat session found for %s.", recipient); - } else { + if (session != NULL) { session->state = CHAT_STATE_ACTIVE; g_timer_start(session->active_timer); session->sent = TRUE; @@ -218,7 +207,6 @@ chat_session_is_paused(const char * const recipient) ChatSession session = g_hash_table_lookup(sessions, recipient); if (session == NULL) { - log_error("No chat session found for %s.", recipient); return FALSE; } else { return (session->state == CHAT_STATE_PAUSED); @@ -231,7 +219,6 @@ chat_session_is_gone(const char * const recipient) ChatSession session = g_hash_table_lookup(sessions, recipient); if (session == NULL) { - log_error("No chat session found for %s.", recipient); return FALSE; } else { return (session->state == CHAT_STATE_GONE); @@ -243,9 +230,7 @@ chat_session_set_gone(const char * const recipient) { ChatSession session = g_hash_table_lookup(sessions, recipient); - if (session == NULL) { - log_error("No chat session found for %s.", recipient); - } else { + if (session != NULL) { session->state = CHAT_STATE_GONE; } } @@ -256,7 +241,6 @@ chat_session_get_recipient_supports(const char * const recipient) ChatSession session = g_hash_table_lookup(sessions, recipient); if (session == NULL) { - log_error("No chat session found for %s.", recipient); return FALSE; } else { return session->recipient_supports; @@ -269,9 +253,7 @@ chat_session_set_recipient_supports(const char * const recipient, { ChatSession session = g_hash_table_lookup(sessions, recipient); - if (session == NULL) { - log_error("No chat session found for %s.", recipient); - } else { + if (session != NULL) { session->recipient_supports = recipient_supports; } } diff --git a/src/command.c b/src/command.c index 52f7fac9..facecd62 100644 --- a/src/command.c +++ b/src/command.c @@ -109,6 +109,7 @@ static gboolean _cmd_set_beep(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_notify(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_log(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_priority(gchar **args, struct cmd_help_t help); +static gboolean _cmd_set_reconnect(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_intype(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_flash(gchar **args, struct cmd_help_t help); static gboolean _cmd_set_showsplash(gchar **args, struct cmd_help_t help); @@ -485,6 +486,18 @@ static struct cmd_t setting_commands[] = "Config file value : maxsize=bytes", NULL } } }, + { "/reconnect", + _cmd_set_reconnect, parse_args, 1, 1, + { "/reconnect seconds", "Set reconnect interval.", + { "/reconnect seconds", + "--------------------", + "Set the reconnect attempt interval in seconds for when the connection is lost.", + "A value of 0 will switch of reconnect attempts.", + "", + "Config file section : [jabber]", + "Config file value : reconnect=seconds", + NULL } } }, + { "/priority", _cmd_set_priority, parse_args, 1, 1, { "/priority ", "Set priority for connection.", @@ -1467,6 +1480,28 @@ _cmd_set_log(gchar **args, struct cmd_help_t help) return TRUE; } +static gboolean +_cmd_set_reconnect(gchar **args, struct cmd_help_t help) +{ + char *value = args[0]; + int intval; + + if (_strtoi(value, &intval, 0, INT_MAX) == 0) { + prefs_set_reconnect(intval); + if (intval == 0) { + cons_show("Reconnect disabled.", intval); + } else { + cons_show("Reconnect interval set to %d seconds.", intval); + } + } else { + cons_show("Usage: %s", help.usage); + } + + /* TODO: make 'level' subcommand for debug level */ + + return TRUE; +} + static gboolean _cmd_set_priority(gchar **args, struct cmd_help_t help) { diff --git a/src/jabber.c b/src/jabber.c index bb5f192f..84f5db40 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -48,6 +48,11 @@ static struct _jabber_conn_t { int priority; } jabber_conn; +// for auto reconnect +static char *saved_user; +static char *saved_password; +static GTimer *reconnect_timer; + static log_level_t _get_log_level(xmpp_log_level_t xmpp_level); static xmpp_log_level_t _get_xmpp_log_level(); static void _xmpp_file_logger(void * const userdata, @@ -98,15 +103,22 @@ jabber_conn_status_t jabber_connect(const char * const user, const char * const passwd) { - log_info("Connecting as %s", user); + if (saved_user == NULL) { + saved_user = strdup(user); + } + if (saved_password == NULL) { + saved_password = strdup(passwd); + } + + log_info("Connecting as %s", saved_user); xmpp_initialize(); jabber_conn.log = _xmpp_get_file_logger(); jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log); jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx); - xmpp_conn_set_jid(jabber_conn.conn, user); - xmpp_conn_set_pass(jabber_conn.conn, passwd); + xmpp_conn_set_jid(jabber_conn.conn, saved_user); + xmpp_conn_set_pass(jabber_conn.conn, saved_password); if (jabber_conn.tls_disabled) xmpp_conn_disable_tls(jabber_conn.conn); @@ -141,10 +153,21 @@ jabber_disconnect(void) void jabber_process_events(void) { + // run xmpp event loop if connected, connecting or disconnecting if (jabber_conn.conn_status == JABBER_CONNECTED || jabber_conn.conn_status == JABBER_CONNECTING - || jabber_conn.conn_status == JABBER_DISCONNECTING) + || jabber_conn.conn_status == JABBER_DISCONNECTING) { xmpp_run_once(jabber_conn.ctx, 10); + + // check timer and reconnect if disconnected and timer set + } else if ((jabber_conn.conn_status == JABBER_DISCONNECTED) && + (reconnect_timer != NULL)) { + if (g_timer_elapsed(reconnect_timer, NULL) > (prefs_get_reconnect() * 1.0)) { + log_debug("Attempting reconncet as %s", saved_user); + jabber_connect(saved_user, saved_password); + } + } + } void @@ -400,6 +423,8 @@ jabber_get_status(void) void jabber_free_resources(void) { + saved_user = NULL; + saved_password = NULL; chat_sessions_clear(); xmpp_conn_release(jabber_conn.conn); xmpp_ctx_free(jabber_conn.ctx); @@ -624,6 +649,7 @@ _connection_handler(xmpp_conn_t * const conn, { xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + // login success if (status == XMPP_CONN_CONNECT) { const char *jid = xmpp_conn_get_jid(conn); prof_handle_login_success(jid); @@ -637,26 +663,56 @@ _connection_handler(xmpp_conn_t * const conn, _jabber_roster_request(); jabber_conn.conn_status = JABBER_CONNECTED; jabber_conn.presence = PRESENCE_ONLINE; + + if (reconnect_timer != NULL) { + g_timer_destroy(reconnect_timer); + reconnect_timer = NULL; + } + } else { // received close stream response from server after disconnect if (jabber_conn.conn_status == JABBER_DISCONNECTING) { jabber_conn.conn_status = JABBER_DISCONNECTED; jabber_conn.presence = PRESENCE_OFFLINE; + if (saved_user != NULL) { + free(saved_user); + saved_user = NULL; + } + if (saved_password != NULL) { + free(saved_password); + saved_password = NULL; + } // lost connection for unkown reason } else if (jabber_conn.conn_status == JABBER_CONNECTED) { prof_handle_lost_connection(); + reconnect_timer = g_timer_new(); xmpp_stop(ctx); jabber_conn.conn_status = JABBER_DISCONNECTED; jabber_conn.presence = PRESENCE_OFFLINE; // login attempt failed } else { - prof_handle_failed_login(); - xmpp_stop(ctx); - jabber_conn.conn_status = JABBER_DISCONNECTED; - jabber_conn.presence = PRESENCE_OFFLINE; + if (reconnect_timer == NULL) { + prof_handle_failed_login(); + if (saved_user != NULL) { + free(saved_user); + saved_user = NULL; + } + if (saved_password != NULL) { + free(saved_password); + saved_password = NULL; + } + xmpp_stop(ctx); + jabber_conn.conn_status = JABBER_DISCONNECTED; + jabber_conn.presence = PRESENCE_OFFLINE; + } else { + xmpp_stop(ctx); + g_timer_start(reconnect_timer); + jabber_conn.conn_status = JABBER_DISCONNECTED; + jabber_conn.presence = PRESENCE_OFFLINE; + } } } } diff --git a/src/preferences.c b/src/preferences.c index a44af242..b2db3123 100644 --- a/src/preferences.c +++ b/src/preferences.c @@ -240,6 +240,19 @@ prefs_set_priority(gint value) _save_prefs(); } +gint +prefs_get_reconnect(void) +{ + return g_key_file_get_integer(prefs, "jabber", "reconnect", NULL); +} + +void +prefs_set_reconnect(gint value) +{ + g_key_file_set_integer(prefs, "jabber", "reconnect", value); + _save_prefs(); +} + gboolean prefs_get_vercheck(void) { diff --git a/src/preferences.h b/src/preferences.h index f4547b07..bde18e59 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -76,6 +76,8 @@ void prefs_set_max_log_size(gint value); gint prefs_get_max_log_size(void); void prefs_set_priority(gint value); gint prefs_get_priority(void); +void prefs_set_reconnect(gint value); +gint prefs_get_reconnect(void); void prefs_add_login(const char *jid); diff --git a/src/windows.c b/src/windows.c index c105c48e..70a2d310 100644 --- a/src/windows.c +++ b/src/windows.c @@ -1103,6 +1103,15 @@ cons_prefs(void) cons_show("Priority : %d", prefs_get_priority()); + gint reconnect_interval = prefs_get_reconnect(); + if (reconnect_interval == 0) { + cons_show("Reconnect interval : OFF"); + } else if (remind_period == 1) { + cons_show("Reconnect interval : 1 second"); + } else { + cons_show("Reconnect interval : %d seconds", reconnect_interval); + } + cons_show(""); if (current_index == 0) {