From 9c1809a50e4969aa13cfd163f67da064ceb903c3 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 6 Oct 2013 01:28:25 +0100 Subject: [PATCH] Refactored error message handling --- src/ui/core.c | 17 ++-------------- src/ui/window.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ui/window.h | 3 +++ 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/ui/core.c b/src/ui/core.c index 93fc74a0..0e2666bf 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -408,21 +408,8 @@ ui_handle_error_message(const char * const from, const char * const err_msg) if (err_msg == NULL) { cons_show_error("Unknown error received from service."); } else { - win_type_t win_type = ui_current_win_type(); - gboolean handled = FALSE; - - switch (win_type) - { - case WIN_MUC: - if (g_strcmp0(err_msg, "conflict") == 0) { - ui_current_print_line("Nickname already in use."); - handled = TRUE; - } - break; - default: - break; - } - + ProfWin *win = wins_get_current(); + gboolean handled = win->handle_error_message(win, from, err_msg); if (handled != TRUE) { cons_show_error("Error received from server: %s", err_msg); } diff --git a/src/ui/window.c b/src/ui/window.c index 7c6f9266..19fc46d2 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -35,6 +35,11 @@ #include "config/theme.h" #include "ui/window.h" +gboolean _muc_handle_error_message(ProfWin *self, const char * const from, + const char * const err_msg); +gboolean _default_handle_error_message(ProfWin *self, const char * const from, + const char * const err_msg); + ProfWin* win_create(const char * const title, int cols, win_type_t type) { @@ -47,6 +52,17 @@ win_create(const char * const title, int cols, win_type_t type) new_win->unread = 0; new_win->history_shown = 0; new_win->type = type; + + switch (new_win->type) + { + case WIN_MUC: + new_win->handle_error_message = _muc_handle_error_message; + break; + default: + new_win->handle_error_message = _default_handle_error_message; + break; + } + scrollok(new_win->win, TRUE); return new_win; @@ -73,6 +89,23 @@ win_print_time(ProfWin* window, char show_char) g_free(date_fmt); } +void +win_print_line(ProfWin *window, const char * const msg, ...) +{ + va_list arg; + va_start(arg, msg); + GString *fmt_msg = g_string_new(NULL); + g_string_vprintf(fmt_msg, msg, arg); + win_print_time(window, '-'); + wprintw(window->win, "%s\n", fmt_msg->str); + g_string_free(fmt_msg, TRUE); + va_end(arg); + + int rows, cols; + getmaxyx(stdscr, rows, cols); + prefresh(window->win, window->y_pos, 0, 1, 0, rows-3, cols-1); +} + void win_presence_colour_on(ProfWin *window, const char * const presence) { @@ -156,3 +189,24 @@ win_show_contact(ProfWin *window, PContact contact) wprintw(window->win, "\n"); win_presence_colour_off(window, presence); } + +gboolean +_muc_handle_error_message(ProfWin *self, const char * const from, + const char * const err_msg) +{ + gboolean handled = FALSE; + if (g_strcmp0(err_msg, "conflict") == 0) { + win_print_line(self, "Nickname already in use."); + handled = TRUE; + } + + return handled; +} + +gboolean +_default_handle_error_message(ProfWin *self, const char * const from, + const char * const err_msg) +{ + return FALSE; + +} diff --git a/src/ui/window.h b/src/ui/window.h index a4421d1b..ea5335d0 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -52,12 +52,15 @@ typedef struct prof_win_t { int paged; int unread; int history_shown; + gboolean (*handle_error_message)(struct prof_win_t *self, + const char * const from, const char * const err_msg); } ProfWin; ProfWin* win_create(const char * const title, int cols, win_type_t type); void win_free(ProfWin *window); void win_print_time(ProfWin *window, char show_char); +void win_print_line(ProfWin *window, const char * const msg, ...); void win_presence_colour_on(ProfWin *window, const char * const presence); void win_presence_colour_off(ProfWin *window, const char * const presence); void win_show_contact(ProfWin *window, PContact contact);