about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-10-06 01:28:25 +0100
committerJames Booth <boothj5@gmail.com>2013-10-06 01:28:25 +0100
commit9c1809a50e4969aa13cfd163f67da064ceb903c3 (patch)
tree06ff9ccfd0adfb07c86763a0d25b2c96507bd02d /src
parent18b7def422e1347906304cdf0de0ba3601f7d411 (diff)
downloadprofani-tty-9c1809a50e4969aa13cfd163f67da064ceb903c3.tar.gz
Refactored error message handling
Diffstat (limited to 'src')
-rw-r--r--src/ui/core.c17
-rw-r--r--src/ui/window.c54
-rw-r--r--src/ui/window.h3
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;
@@ -74,6 +90,23 @@ win_print_time(ProfWin* window, char show_char)
 }
 
 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)
 {
     if (g_strcmp0(presence, "online") == 0) {
@@ -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);