about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-05-12 22:57:36 +0100
committerJames Booth <boothj5@gmail.com>2013-05-12 23:00:29 +0100
commit57e64bebe58f051eafc90b27ebe328128fe47095 (patch)
tree54b3e7b6daac3c9a3afcf258ee59474df0fb9892
parentc1ee75da40b5fa633cb7a3efd699c217a3c88ff0 (diff)
downloadprofani-tty-57e64bebe58f051eafc90b27ebe328128fe47095.tar.gz
Added parameter to /close
2,3,4,5,6,7,8,9,0 will close specified window.
'all' will close all windows.

closes #159
-rw-r--r--src/command/command.c95
-rw-r--r--src/ui/core.c39
-rw-r--r--src/ui/ui.h6
3 files changed, 114 insertions, 26 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 0d56ab26..b986239f 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -481,12 +481,14 @@ static struct cmd_t main_commands[] =
           NULL } } },
 
     { "/close",
-        _cmd_close, parse_args, 0, 0,
-        { "/close", "Close current chat window.",
-        { "/close",
-          "------",
-          "Close the current chat window, no message is sent to the recipient,",
-          "The chat window will become available for new chats.",
+        _cmd_close, parse_args, 0, 1,
+        { "/close [win|all]", "Close a window window.",
+        { "/close [win|all]",
+          "----------------",
+          "Passing no argument will close the current window.",
+          "Passing 2,3,4,5,6,7,8,9 or 0 will close the specified window.",
+          "Passing 'all' will close all currently open windows.",
+          "The console window cannot be closed.",
           "If in a chat room, you will leave the room.",
           NULL } } },
 
@@ -2476,40 +2478,81 @@ _cmd_clear(gchar **args, struct cmd_help_t help)
     return TRUE;
 }
 
+static void _close_connected_win(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);
+            }
+        }
+    }
+}
+
 static gboolean
 _cmd_close(gchar **args, struct cmd_help_t help)
 {
     jabber_conn_status_t conn_status = jabber_get_connection_status();
-    win_type_t win_type = ui_current_win_type();
+    int index = 0;
+    int curr = 0;
+
+    if (args[0] == NULL) {
+        index = ui_current_win_index();
+    } else if (strcmp(args[0], "all") == 0) {
+        for (curr = 1; curr <= 9; curr++) {
+            if (ui_win_exists(curr)) {
+                if (conn_status == JABBER_CONNECTED) {
+                    _close_connected_win(curr);
+                }
+                ui_close_win(curr);
+            }
+        }
+
+        cons_show("Closed all windows.");
+        return TRUE;
+    } else {
+        index = atoi(args[0]);
+        if (index == 0) {
+            index = 9;
+        } else {
+            index--;
+        }
+    }
 
-    // cannot close console window
-    if (win_type == WIN_CONSOLE) {
+    if (index == 0) {
         cons_show("Cannot close console window.");
         return TRUE;
     }
 
-    // handle leaving rooms, or chat
-    if (conn_status == JABBER_CONNECTED) {
-        if (win_type == WIN_MUC) {
-            char *room_jid = ui_current_recipient();
-            presence_leave_chat_room(room_jid);
-        } else if ((win_type == WIN_CHAT) || (win_type == WIN_PRIVATE)) {
+    if (index > 9 || index < 0) {
+        cons_show("No such window exists.");
+        return TRUE;
+    }
 
-            if (prefs_get_boolean(PREF_STATES)) {
-                char *recipient = ui_current_recipient();
+    if (!ui_win_exists(index)) {
+        cons_show("Window is not open.");
+        return TRUE;
+    }
 
-                // 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);
-                }
-            }
-        }
+
+    // handle leaving rooms, or chat
+    if (conn_status == JABBER_CONNECTED) {
+        _close_connected_win(index);
     }
 
     // close the window
-    ui_close_current();
+    ui_close_win(index);
+    cons_show("Closed window %d", index + 1);
 
     return TRUE;
 }
diff --git a/src/ui/core.c b/src/ui/core.c
index 1e9d18e4..992a5701 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -205,6 +205,11 @@ ui_windows_full(void)
     return TRUE;
 }
 
+gboolean ui_win_exists(int index)
+{
+    return (windows[index] != NULL);
+}
+
 gboolean
 ui_duck_exists(void)
 {
@@ -550,12 +555,46 @@ ui_close_current(void)
     current_win_dirty = TRUE;
 }
 
+void ui_close_win(int index)
+{
+    win_free(windows[index]);
+    windows[index] = NULL;
+    status_bar_inactive(index);
+
+    if (index == current_index) {
+        _set_current(0);
+    }
+
+    status_bar_active(0);
+    title_bar_title();
+
+    current_win_dirty = TRUE;
+}
+
 win_type_t
 ui_current_win_type(void)
 {
     return current->type;
 }
 
+int
+ui_current_win_index(void)
+{
+    return current_index;
+}
+
+win_type_t
+ui_win_type(int index)
+{
+    return windows[index]->type;
+}
+
+char *
+ui_recipient(int index)
+{
+    return strdup(windows[index]->from);
+}
+
 char *
 ui_current_recipient(void)
 {
diff --git a/src/ui/ui.h b/src/ui/ui.h
index 6d110100..83441311 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -69,11 +69,17 @@ void ui_console_dirty(void);
 void ui_close_current(void);
 void ui_clear_current(void);
 win_type_t ui_current_win_type(void);
+int ui_current_win_index(void);
 char* ui_current_recipient(void);
 void ui_current_print_line(const char * const msg, ...);
 void ui_current_error_line(const char * const msg);
 void ui_current_page_off(void);
 
+win_type_t ui_win_type(int index);
+char * ui_recipient(int index);
+void ui_close_win(int index);
+gboolean ui_win_exists(int index);
+
 // ui events
 void ui_contact_typing(const char * const from);
 void ui_incoming_msg(const char * const from, const char * const message,