1
0
Fork 0

Move window functions to window_list.c

This commit is contained in:
James Booth 2016-07-24 17:02:09 +01:00
parent 37742d71b6
commit 6cc4abedc5
6 changed files with 88 additions and 403 deletions

View File

@ -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)
{

View File

@ -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);

View File

@ -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) {

View File

@ -172,283 +172,6 @@ void replace_when_new_null(void **state)
free(result);
}
void compare_win_nums_less(void **state)
{
gconstpointer a = GINT_TO_POINTER(2);
gconstpointer b = GINT_TO_POINTER(3);
int result = cmp_win_num(a, b);
assert_true(result < 0);
}
void compare_win_nums_equal(void **state)
{
gconstpointer a = GINT_TO_POINTER(5);
gconstpointer b = GINT_TO_POINTER(5);
int result = cmp_win_num(a, b);
assert_true(result == 0);
}
void compare_win_nums_greater(void **state)
{
gconstpointer a = GINT_TO_POINTER(7);
gconstpointer b = GINT_TO_POINTER(6);
int result = cmp_win_num(a, b);
assert_true(result > 0);
}
void compare_0s_equal(void **state)
{
gconstpointer a = GINT_TO_POINTER(0);
gconstpointer b = GINT_TO_POINTER(0);
int result = cmp_win_num(a, b);
assert_true(result == 0);
}
void compare_0_greater_than_1(void **state)
{
gconstpointer a = GINT_TO_POINTER(0);
gconstpointer b = GINT_TO_POINTER(1);
int result = cmp_win_num(a, b);
assert_true(result > 0);
}
void compare_1_less_than_0(void **state)
{
gconstpointer a = GINT_TO_POINTER(1);
gconstpointer b = GINT_TO_POINTER(0);
int result = cmp_win_num(a, b);
assert_true(result < 0);
}
void compare_0_less_than_11(void **state)
{
gconstpointer a = GINT_TO_POINTER(0);
gconstpointer b = GINT_TO_POINTER(11);
int result = cmp_win_num(a, b);
assert_true(result < 0);
}
void compare_11_greater_than_0(void **state)
{
gconstpointer a = GINT_TO_POINTER(11);
gconstpointer b = GINT_TO_POINTER(0);
int result = cmp_win_num(a, b);
assert_true(result > 0);
}
void compare_0_greater_than_9(void **state)
{
gconstpointer a = GINT_TO_POINTER(0);
gconstpointer b = GINT_TO_POINTER(9);
int result = cmp_win_num(a, b);
assert_true(result > 0);
}
void compare_9_less_than_0(void **state)
{
gconstpointer a = GINT_TO_POINTER(9);
gconstpointer b = GINT_TO_POINTER(0);
int result = cmp_win_num(a, b);
assert_true(result < 0);
}
void next_available_when_only_console(void **state)
{
GList *used = NULL;
used = g_list_append(used, GINT_TO_POINTER(1));
int result = get_next_available_win_num(used);
assert_int_equal(2, result);
}
void next_available_3_at_end(void **state)
{
GList *used = NULL;
used = g_list_append(used, GINT_TO_POINTER(1));
used = g_list_append(used, GINT_TO_POINTER(2));
int result = get_next_available_win_num(used);
assert_int_equal(3, result);
}
void next_available_9_at_end(void **state)
{
GList *used = NULL;
used = g_list_append(used, GINT_TO_POINTER(1));
used = g_list_append(used, GINT_TO_POINTER(2));
used = g_list_append(used, GINT_TO_POINTER(3));
used = g_list_append(used, GINT_TO_POINTER(4));
used = g_list_append(used, GINT_TO_POINTER(5));
used = g_list_append(used, GINT_TO_POINTER(6));
used = g_list_append(used, GINT_TO_POINTER(7));
used = g_list_append(used, GINT_TO_POINTER(8));
int result = get_next_available_win_num(used);
assert_int_equal(9, result);
}
void next_available_0_at_end(void **state)
{
GList *used = NULL;
used = g_list_append(used, GINT_TO_POINTER(1));
used = g_list_append(used, GINT_TO_POINTER(2));
used = g_list_append(used, GINT_TO_POINTER(3));
used = g_list_append(used, GINT_TO_POINTER(4));
used = g_list_append(used, GINT_TO_POINTER(5));
used = g_list_append(used, GINT_TO_POINTER(6));
used = g_list_append(used, GINT_TO_POINTER(7));
used = g_list_append(used, GINT_TO_POINTER(8));
used = g_list_append(used, GINT_TO_POINTER(9));
int result = get_next_available_win_num(used);
assert_int_equal(0, result);
}
void next_available_2_in_first_gap(void **state)
{
GList *used = NULL;
used = g_list_append(used, GINT_TO_POINTER(1));
used = g_list_append(used, GINT_TO_POINTER(3));
used = g_list_append(used, GINT_TO_POINTER(4));
used = g_list_append(used, GINT_TO_POINTER(5));
used = g_list_append(used, GINT_TO_POINTER(9));
used = g_list_append(used, GINT_TO_POINTER(0));
int result = get_next_available_win_num(used);
assert_int_equal(2, result);
}
void next_available_9_in_first_gap(void **state)
{
GList *used = NULL;
used = g_list_append(used, GINT_TO_POINTER(1));
used = g_list_append(used, GINT_TO_POINTER(2));
used = g_list_append(used, GINT_TO_POINTER(3));
used = g_list_append(used, GINT_TO_POINTER(4));
used = g_list_append(used, GINT_TO_POINTER(5));
used = g_list_append(used, GINT_TO_POINTER(6));
used = g_list_append(used, GINT_TO_POINTER(7));
used = g_list_append(used, GINT_TO_POINTER(8));
used = g_list_append(used, GINT_TO_POINTER(0));
used = g_list_append(used, GINT_TO_POINTER(11));
used = g_list_append(used, GINT_TO_POINTER(12));
used = g_list_append(used, GINT_TO_POINTER(13));
used = g_list_append(used, GINT_TO_POINTER(20));
int result = get_next_available_win_num(used);
assert_int_equal(9, result);
}
void next_available_0_in_first_gap(void **state)
{
GList *used = NULL;
used = g_list_append(used, GINT_TO_POINTER(1));
used = g_list_append(used, GINT_TO_POINTER(2));
used = g_list_append(used, GINT_TO_POINTER(3));
used = g_list_append(used, GINT_TO_POINTER(4));
used = g_list_append(used, GINT_TO_POINTER(5));
used = g_list_append(used, GINT_TO_POINTER(6));
used = g_list_append(used, GINT_TO_POINTER(7));
used = g_list_append(used, GINT_TO_POINTER(8));
used = g_list_append(used, GINT_TO_POINTER(9));
used = g_list_append(used, GINT_TO_POINTER(11));
used = g_list_append(used, GINT_TO_POINTER(12));
used = g_list_append(used, GINT_TO_POINTER(13));
used = g_list_append(used, GINT_TO_POINTER(20));
int result = get_next_available_win_num(used);
assert_int_equal(0, result);
}
void next_available_11_in_first_gap(void **state)
{
GList *used = NULL;
used = g_list_append(used, GINT_TO_POINTER(1));
used = g_list_append(used, GINT_TO_POINTER(2));
used = g_list_append(used, GINT_TO_POINTER(3));
used = g_list_append(used, GINT_TO_POINTER(4));
used = g_list_append(used, GINT_TO_POINTER(5));
used = g_list_append(used, GINT_TO_POINTER(6));
used = g_list_append(used, GINT_TO_POINTER(7));
used = g_list_append(used, GINT_TO_POINTER(8));
used = g_list_append(used, GINT_TO_POINTER(9));
used = g_list_append(used, GINT_TO_POINTER(0));
used = g_list_append(used, GINT_TO_POINTER(12));
used = g_list_append(used, GINT_TO_POINTER(13));
used = g_list_append(used, GINT_TO_POINTER(20));
int result = get_next_available_win_num(used);
assert_int_equal(11, result);
}
void next_available_24_first_big_gap(void **state)
{
GList *used = NULL;
used = g_list_append(used, GINT_TO_POINTER(1));
used = g_list_append(used, GINT_TO_POINTER(2));
used = g_list_append(used, GINT_TO_POINTER(3));
used = g_list_append(used, GINT_TO_POINTER(4));
used = g_list_append(used, GINT_TO_POINTER(5));
used = g_list_append(used, GINT_TO_POINTER(6));
used = g_list_append(used, GINT_TO_POINTER(7));
used = g_list_append(used, GINT_TO_POINTER(8));
used = g_list_append(used, GINT_TO_POINTER(9));
used = g_list_append(used, GINT_TO_POINTER(0));
used = g_list_append(used, GINT_TO_POINTER(11));
used = g_list_append(used, GINT_TO_POINTER(12));
used = g_list_append(used, GINT_TO_POINTER(13));
used = g_list_append(used, GINT_TO_POINTER(14));
used = g_list_append(used, GINT_TO_POINTER(15));
used = g_list_append(used, GINT_TO_POINTER(16));
used = g_list_append(used, GINT_TO_POINTER(17));
used = g_list_append(used, GINT_TO_POINTER(18));
used = g_list_append(used, GINT_TO_POINTER(19));
used = g_list_append(used, GINT_TO_POINTER(20));
used = g_list_append(used, GINT_TO_POINTER(21));
used = g_list_append(used, GINT_TO_POINTER(22));
used = g_list_append(used, GINT_TO_POINTER(23));
used = g_list_append(used, GINT_TO_POINTER(51));
used = g_list_append(used, GINT_TO_POINTER(52));
used = g_list_append(used, GINT_TO_POINTER(53));
used = g_list_append(used, GINT_TO_POINTER(89));
used = g_list_append(used, GINT_TO_POINTER(90));
used = g_list_append(used, GINT_TO_POINTER(100));
used = g_list_append(used, GINT_TO_POINTER(101));
used = g_list_append(used, GINT_TO_POINTER(102));
int result = get_next_available_win_num(used);
assert_int_equal(24, result);
}
void test_online_is_valid_resource_presence_string(void **state)
{
assert_true(valid_resource_presence_string("online"));

View File

@ -11,25 +11,6 @@ void replace_when_sub_empty(void **state);
void replace_when_sub_null(void **state);
void replace_when_new_empty(void **state);
void replace_when_new_null(void **state);
void compare_win_nums_less(void **state);
void compare_win_nums_equal(void **state);
void compare_win_nums_greater(void **state);
void compare_0s_equal(void **state);
void compare_0_greater_than_1(void **state);
void compare_1_less_than_0(void **state);
void compare_0_less_than_11(void **state);
void compare_11_greater_than_0(void **state);
void compare_0_greater_than_9(void **state);
void compare_9_less_than_0(void **state);
void next_available_when_only_console(void **state);
void next_available_3_at_end(void **state);
void next_available_9_at_end(void **state);
void next_available_0_at_end(void **state);
void next_available_2_in_first_gap(void **state);
void next_available_9_in_first_gap(void **state);
void next_available_0_in_first_gap(void **state);
void next_available_11_in_first_gap(void **state);
void next_available_24_first_big_gap(void **state);
void test_online_is_valid_resource_presence_string(void **state);
void test_chat_is_valid_resource_presence_string(void **state);
void test_away_is_valid_resource_presence_string(void **state);

View File

@ -51,25 +51,6 @@ int main(int argc, char* argv[]) {
unit_test(replace_when_sub_null),
unit_test(replace_when_new_empty),
unit_test(replace_when_new_null),
unit_test(compare_win_nums_less),
unit_test(compare_win_nums_equal),
unit_test(compare_win_nums_greater),
unit_test(compare_0s_equal),
unit_test(compare_0_greater_than_1),
unit_test(compare_1_less_than_0),
unit_test(compare_0_less_than_11),
unit_test(compare_11_greater_than_0),
unit_test(compare_0_greater_than_9),
unit_test(compare_9_less_than_0),
unit_test(next_available_when_only_console),
unit_test(next_available_3_at_end),
unit_test(next_available_9_at_end),
unit_test(next_available_0_at_end),
unit_test(next_available_2_in_first_gap),
unit_test(next_available_9_in_first_gap),
unit_test(next_available_0_in_first_gap),
unit_test(next_available_11_in_first_gap),
unit_test(next_available_24_first_big_gap),
unit_test(test_online_is_valid_resource_presence_string),
unit_test(test_chat_is_valid_resource_presence_string),
unit_test(test_away_is_valid_resource_presence_string),