about summary refs log tree commit diff stats
path: root/src/ui
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-05-17 00:33:00 +0100
committerJames Booth <boothj5@gmail.com>2013-05-17 00:33:00 +0100
commit905571bfb741630c6e82382d1750a4797e18e3db (patch)
tree678abdb5f0246af73ecb1e9eeed845623cff0075 /src/ui
parentaa1f8b655c7d88e249962ae98e08dc9468c2e7bc (diff)
downloadprofani-tty-905571bfb741630c6e82382d1750a4797e18e3db.tar.gz
Added "tidy" and "prune" options to /wins command
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/core.c95
-rw-r--r--src/ui/ui.h4
2 files changed, 98 insertions, 1 deletions
diff --git a/src/ui/core.c b/src/ui/core.c
index 717966b4..9a539ed4 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -84,6 +84,7 @@ static void _win_resize_all(void);
 static void _win_show_history(WINDOW *win, int win_index,
     const char * const contact);
 static void _ui_draw_win_title(void);
+gboolean _tidy(void);
 
 void
 ui_init(void)
@@ -508,6 +509,28 @@ ui_handle_special_keys(const wint_t * const ch, const char * const inp,
 }
 
 void
+ui_close_connected_win(int index)
+{
+    win_type_t win_type = ui_win_type(index);
+    if (win_type == WIN_MUC) {
+        char *room_jid = ui_recipient(index);
+        presence_leave_chat_room(room_jid);
+    } else if ((win_type == WIN_CHAT) || (win_type == WIN_PRIVATE)) {
+
+        if (prefs_get_boolean(PREF_STATES)) {
+            char *recipient = ui_recipient(index);
+
+            // send <gone/> chat state before closing
+            if (chat_session_get_recipient_supports(recipient)) {
+                chat_session_set_gone(recipient);
+                message_send_gone(recipient);
+                chat_session_end(recipient);
+            }
+        }
+    }
+}
+
+void
 ui_switch_win(const int i)
 {
     ui_current_page_off();
@@ -555,7 +578,8 @@ ui_close_current(void)
     current_win_dirty = TRUE;
 }
 
-void ui_close_win(int index)
+void
+ui_close_win(int index)
 {
     win_free(windows[index]);
     windows[index] = NULL;
@@ -571,6 +595,43 @@ void ui_close_win(int index)
     current_win_dirty = TRUE;
 }
 
+void
+ui_tidy_wins(void)
+{
+    gboolean tidied = _tidy();
+
+    if (tidied) {
+        cons_show("Windows tidied.");
+    } else {
+        cons_show("No tidy needed.");
+    }
+}
+
+void
+ui_prune_wins(void)
+{
+    jabber_conn_status_t conn_status = jabber_get_connection_status();
+    int curr = 0;
+    gboolean pruned = FALSE;
+
+    for (curr = 1; curr <= 9; curr++) {
+        if (ui_win_exists(curr) && (ui_win_unread(curr) == 0)) {
+            if (conn_status == JABBER_CONNECTED) {
+                ui_close_connected_win(curr);
+            }
+            ui_close_win(curr);
+            pruned = TRUE;
+        }
+    }
+
+    _tidy();
+    if (pruned) {
+        cons_show("Windows pruned.");
+    } else {
+        cons_show("No prune needed.");
+    }
+}
+
 win_type_t
 ui_current_win_type(void)
 {
@@ -1568,3 +1629,35 @@ _set_current(int index)
     current = windows[current_index];
 }
 
+gboolean
+_tidy(void)
+{
+    int gap = 1;
+    int filler = 1;
+    gboolean tidied = FALSE;
+
+    for (gap = 1; gap < NUM_WINS; gap++) {
+        // if a gap
+        if (!ui_win_exists(gap)) {
+
+            // find next used window and move into gap
+            for (filler = gap + 1; filler < NUM_WINS; filler++) {
+                if (ui_win_exists(filler)) {
+                    windows[gap] = windows[filler];
+                    if (windows[gap]->unread > 0) {
+                        status_bar_new(gap);
+                    } else {
+                        status_bar_active(gap);
+                    }
+                    windows[filler] = NULL;
+                    status_bar_inactive(filler);
+                    tidied = TRUE;
+                    break;
+                }
+            }
+        }
+    }
+
+    return tidied;
+}
+
diff --git a/src/ui/ui.h b/src/ui/ui.h
index e165b353..97b53b6d 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -64,6 +64,7 @@ void ui_print_error_from_recipient(const char * const from, const char *err_msg)
 void ui_print_system_msg_from_recipient(const char * const from, const char *message);
 gint ui_unread(void);
 void ui_console_dirty(void);
+void ui_close_connected_win(int index);
 
 // current window actions
 void ui_close_current(void);
@@ -123,6 +124,9 @@ void ui_duck(const char * const query);
 void ui_duck_result(const char * const result);
 gboolean ui_duck_exists(void);
 
+void ui_tidy_wins(void);
+void ui_prune_wins(void);
+
 // create windows
 void create_title_bar(void);
 void create_status_bar(void);