1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Refactored error message handling

This commit is contained in:
James Booth 2013-10-06 01:28:25 +01:00
parent 18b7def422
commit 9c1809a50e
3 changed files with 59 additions and 15 deletions

View File

@ -408,21 +408,8 @@ ui_handle_error_message(const char * const from, const char * const err_msg)
if (err_msg == NULL) { if (err_msg == NULL) {
cons_show_error("Unknown error received from service."); cons_show_error("Unknown error received from service.");
} else { } else {
win_type_t win_type = ui_current_win_type(); ProfWin *win = wins_get_current();
gboolean handled = FALSE; gboolean handled = win->handle_error_message(win, from, err_msg);
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;
}
if (handled != TRUE) { if (handled != TRUE) {
cons_show_error("Error received from server: %s", err_msg); cons_show_error("Error received from server: %s", err_msg);
} }

View File

@ -35,6 +35,11 @@
#include "config/theme.h" #include "config/theme.h"
#include "ui/window.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* ProfWin*
win_create(const char * const title, int cols, win_type_t type) 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->unread = 0;
new_win->history_shown = 0; new_win->history_shown = 0;
new_win->type = type; 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); scrollok(new_win->win, TRUE);
return new_win; return new_win;
@ -73,6 +89,23 @@ win_print_time(ProfWin* window, char show_char)
g_free(date_fmt); 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 void
win_presence_colour_on(ProfWin *window, const char * const presence) 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"); wprintw(window->win, "\n");
win_presence_colour_off(window, presence); 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;
}

View File

@ -52,12 +52,15 @@ typedef struct prof_win_t {
int paged; int paged;
int unread; int unread;
int history_shown; int history_shown;
gboolean (*handle_error_message)(struct prof_win_t *self,
const char * const from, const char * const err_msg);
} ProfWin; } ProfWin;
ProfWin* win_create(const char * const title, int cols, win_type_t type); ProfWin* win_create(const char * const title, int cols, win_type_t type);
void win_free(ProfWin *window); void win_free(ProfWin *window);
void win_print_time(ProfWin *window, char show_char); 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_on(ProfWin *window, const char * const presence);
void win_presence_colour_off(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); void win_show_contact(ProfWin *window, PContact contact);