about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2016-07-24 17:02:09 +0100
committerJames Booth <boothj5@gmail.com>2016-07-24 17:02:09 +0100
commit6cc4abedc58f294ccb36632cb86464c247687288 (patch)
treeb766b5f51c301f79076718ea9720b8dff2f0cfb2 /src
parent37742d71b638ba5343af7398842f84b530d11c29 (diff)
downloadprofani-tty-6cc4abedc58f294ccb36632cb86464c247687288.tar.gz
Move window functions to window_list.c
Diffstat (limited to 'src')
-rw-r--r--src/common.c73
-rw-r--r--src/common.h4
-rw-r--r--src/ui/window_list.c99
3 files changed, 88 insertions, 88 deletions
diff --git a/src/common.c b/src/common.c
index 035a0505..d3f09a56 100644
--- a/src/common.c
+++ b/src/common.c
@@ -420,79 +420,6 @@ p_sha1_hash(char *str)
     return g_base64_encode(digest, sizeof(digest));
 }
 
-int
-cmp_win_num(gconstpointer a, gconstpointer b)
-{
-    int real_a = GPOINTER_TO_INT(a);
-    int real_b = GPOINTER_TO_INT(b);
-
-    if (real_a == 0) {
-        real_a = 10;
-    }
-
-    if (real_b == 0) {
-        real_b = 10;
-    }
-
-    if (real_a < real_b) {
-        return -1;
-    } else if (real_a == real_b) {
-        return 0;
-    } else {
-        return 1;
-    }
-}
-
-int
-get_next_available_win_num(GList *used)
-{
-    // only console used
-    if (g_list_length(used) == 1) {
-        return 2;
-    } else {
-        GList *sorted = NULL;
-        GList *curr = used;
-        while (curr) {
-            sorted = g_list_insert_sorted(sorted, curr->data, cmp_win_num);
-            curr = g_list_next(curr);
-        }
-
-        int result = 0;
-        int last_num = 1;
-        curr = sorted;
-        // skip console
-        curr = g_list_next(curr);
-        while (curr) {
-            int curr_num = GPOINTER_TO_INT(curr->data);
-
-            if (((last_num != 9) && ((last_num + 1) != curr_num)) ||
-                    ((last_num == 9) && (curr_num != 0))) {
-                result = last_num + 1;
-                if (result == 10) {
-                    result = 0;
-                }
-                g_list_free(sorted);
-                return (result);
-
-            } else {
-                last_num = curr_num;
-                if (last_num == 0) {
-                    last_num = 10;
-                }
-            }
-            curr = g_list_next(curr);
-        }
-        result = last_num + 1;
-        if (result == 10) {
-            result = 0;
-        }
-
-        g_list_free(sorted);
-        return result;
-    }
-}
-
-
 static size_t
 _data_callback(void *ptr, size_t size, size_t nmemb, void *data)
 {
diff --git a/src/common.h b/src/common.h
index 9d8c99a7..c463d62c 100644
--- a/src/common.h
+++ b/src/common.h
@@ -108,6 +108,7 @@ int str_contains(const char str[], int size, char ch);
 gboolean strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg);
 int utf8_display_len(const char *const str);
 char* file_getline(FILE *stream);
+
 char* release_get_latest(void);
 gboolean release_is_new(char *found_version);
 
@@ -115,9 +116,6 @@ char* p_sha1_hash(char *str);
 char* create_unique_id(char *prefix);
 void reset_unique_id(void);
 
-int cmp_win_num(gconstpointer a, gconstpointer b);
-int get_next_available_win_num(GList *used);
-
 char* get_file_or_linked(char *loc, char *basedir);
 char* strip_arg_quotes(const char *const input);
 gboolean is_notify_enabled(void);
diff --git a/src/ui/window_list.c b/src/ui/window_list.c
index 20994e34..402fbdc1 100644
--- a/src/ui/window_list.c
+++ b/src/ui/window_list.c
@@ -55,6 +55,9 @@ static int current;
 static Autocomplete wins_ac;
 static Autocomplete wins_close_ac;
 
+static int _wins_cmp_num(gconstpointer a, gconstpointer b);
+static int _wins_get_next_available_num(GList *used);
+
 void
 wins_init(void)
 {
@@ -424,7 +427,7 @@ wins_get_next(void)
 {
     // get and sort win nums
     GList *keys = g_hash_table_get_keys(windows);
-    keys = g_list_sort(keys, cmp_win_num);
+    keys = g_list_sort(keys, _wins_cmp_num);
     GList *curr = keys;
 
     // find our place in the list
@@ -453,7 +456,7 @@ wins_get_previous(void)
 {
     // get and sort win nums
     GList *keys = g_hash_table_get_keys(windows);
-    keys = g_list_sort(keys, cmp_win_num);
+    keys = g_list_sort(keys, _wins_cmp_num);
     GList *curr = keys;
 
     // find our place in the list
@@ -612,7 +615,7 @@ ProfWin*
 wins_new_xmlconsole(void)
 {
     GList *keys = g_hash_table_get_keys(windows);
-    int result = get_next_available_win_num(keys);
+    int result = _wins_get_next_available_num(keys);
     g_list_free(keys);
     ProfWin *newwin = win_create_xmlconsole();
     g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin);
@@ -625,7 +628,7 @@ ProfWin*
 wins_new_chat(const char *const barejid)
 {
     GList *keys = g_hash_table_get_keys(windows);
-    int result = get_next_available_win_num(keys);
+    int result = _wins_get_next_available_num(keys);
     g_list_free(keys);
     ProfWin *newwin = win_create_chat(barejid);
     g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin);
@@ -648,7 +651,7 @@ ProfWin*
 wins_new_muc(const char *const roomjid)
 {
     GList *keys = g_hash_table_get_keys(windows);
-    int result = get_next_available_win_num(keys);
+    int result = _wins_get_next_available_num(keys);
     g_list_free(keys);
     ProfWin *newwin = win_create_muc(roomjid);
     g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin);
@@ -661,7 +664,7 @@ ProfWin*
 wins_new_muc_config(const char *const roomjid, DataForm *form)
 {
     GList *keys = g_hash_table_get_keys(windows);
-    int result = get_next_available_win_num(keys);
+    int result = _wins_get_next_available_num(keys);
     g_list_free(keys);
     ProfWin *newwin = win_create_muc_config(roomjid, form);
     g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin);
@@ -672,7 +675,7 @@ ProfWin*
 wins_new_private(const char *const fulljid)
 {
     GList *keys = g_hash_table_get_keys(windows);
-    int result = get_next_available_win_num(keys);
+    int result = _wins_get_next_available_num(keys);
     g_list_free(keys);
     ProfWin *newwin = win_create_private(fulljid);
     g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin);
@@ -685,7 +688,7 @@ ProfWin *
 wins_new_plugin(const char *const plugin_name, const char * const tag)
 {
     GList *keys = g_hash_table_get_keys(windows);
-    int result = get_next_available_win_num(keys);
+    int result = _wins_get_next_available_num(keys);
     g_list_free(keys);
     ProfWin *newwin = win_create_plugin(plugin_name, tag);
     g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin);
@@ -896,23 +899,95 @@ wins_swap(int source_win, int target_win)
     }
 }
 
+static int
+_wins_cmp_num(gconstpointer a, gconstpointer b)
+{
+    int real_a = GPOINTER_TO_INT(a);
+    int real_b = GPOINTER_TO_INT(b);
+
+    if (real_a == 0) {
+        real_a = 10;
+    }
+
+    if (real_b == 0) {
+        real_b = 10;
+    }
+
+    if (real_a < real_b) {
+        return -1;
+    } else if (real_a == real_b) {
+        return 0;
+    } else {
+        return 1;
+    }
+}
+
+static int
+_wins_get_next_available_num(GList *used)
+{
+    // only console used
+    if (g_list_length(used) == 1) {
+        return 2;
+    } else {
+        GList *sorted = NULL;
+        GList *curr = used;
+        while (curr) {
+            sorted = g_list_insert_sorted(sorted, curr->data, _wins_cmp_num);
+            curr = g_list_next(curr);
+        }
+
+        int result = 0;
+        int last_num = 1;
+        curr = sorted;
+        // skip console
+        curr = g_list_next(curr);
+        while (curr) {
+            int curr_num = GPOINTER_TO_INT(curr->data);
+
+            if (((last_num != 9) && ((last_num + 1) != curr_num)) ||
+                    ((last_num == 9) && (curr_num != 0))) {
+                result = last_num + 1;
+                if (result == 10) {
+                    result = 0;
+                }
+                g_list_free(sorted);
+                return (result);
+
+            } else {
+                last_num = curr_num;
+                if (last_num == 0) {
+                    last_num = 10;
+                }
+            }
+            curr = g_list_next(curr);
+        }
+        result = last_num + 1;
+        if (result == 10) {
+            result = 0;
+        }
+
+        g_list_free(sorted);
+        return result;
+    }
+}
+
 gboolean
 wins_tidy(void)
 {
     gboolean tidy_required = FALSE;
     // check for gaps
     GList *keys = g_hash_table_get_keys(windows);
-    keys = g_list_sort(keys, cmp_win_num);
+    keys = g_list_sort(keys, _wins_cmp_num);
 
     // get last used
     GList *last = g_list_last(keys);
     int last_num = GPOINTER_TO_INT(last->data);
 
     // find first free num TODO - Will sort again
-    int next_available = get_next_available_win_num(keys);
+    int next_available = _wins_get_next_available_num(keys);
 
     // found gap (next available before last window)
-    if (cmp_win_num(GINT_TO_POINTER(next_available), GINT_TO_POINTER(last_num)) < 0) {
+    if (_wins_cmp_num(GINT_TO_POINTER(next_available), GINT_TO_POINTER(last_num)) < 0) {
         tidy_required = TRUE;
     }
 
@@ -966,7 +1041,7 @@ wins_create_summary(gboolean unread)
     GSList *result = NULL;
 
     GList *keys = g_hash_table_get_keys(windows);
-    keys = g_list_sort(keys, cmp_win_num);
+    keys = g_list_sort(keys, _wins_cmp_num);
     GList *curr = keys;
 
     while (curr) {