1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Initial code refactor for growable window list

This commit is contained in:
James Booth 2013-08-20 23:08:21 +01:00
parent ef511438b6
commit 601a88037a
7 changed files with 841 additions and 548 deletions

View File

@ -13,6 +13,7 @@ profanity_SOURCES = \
src/ui/ui.h src/ui/window.c src/ui/window.h src/ui/core.c \
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
src/ui/console.c src/ui/notifier.c src/ui/notifier.h \
src/ui/windows.c src/ui/windows.h \
src/command/command.h src/command/command.c src/command/history.c \
src/command/history.h src/tools/parser.c \
src/tools/parser.h \
@ -39,6 +40,7 @@ tests_testsuite_SOURCES = \
src/ui/ui.h src/ui/window.c src/ui/window.h src/ui/core.c \
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
src/ui/console.c src/ui/notifier.c src/ui/notifier.h \
src/ui/windows.c src/ui/windows.h \
src/command/command.h src/command/command.c src/command/history.c \
src/command/history.h src/tools/parser.c \
src/tools/parser.h \

View File

@ -35,6 +35,7 @@
#include "config/theme.h"
#include "ui/notifier.h"
#include "ui/window.h"
#include "ui/windows.h"
#include "ui/ui.h"
#include "xmpp/xmpp.h"
@ -57,14 +58,14 @@ void
cons_show_time(void)
{
win_print_time(console, '-');
ui_console_dirty();
wins_refresh_console();
}
void
cons_show_word(const char * const word)
{
wprintw(console->win, "%s", word);
ui_console_dirty();
wins_refresh_console();
}
void
@ -80,7 +81,7 @@ cons_debug(const char * const msg, ...)
g_string_free(fmt_msg, TRUE);
va_end(arg);
ui_console_dirty();
wins_refresh_console();
cons_alert();
ui_current_page_off();
@ -99,7 +100,7 @@ cons_show(const char * const msg, ...)
wprintw(console->win, "%s\n", fmt_msg->str);
g_string_free(fmt_msg, TRUE);
va_end(arg);
ui_console_dirty();
wins_refresh_console();
}
void
@ -116,7 +117,7 @@ cons_show_error(const char * const msg, ...)
g_string_free(fmt_msg, TRUE);
va_end(arg);
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -136,7 +137,7 @@ cons_show_typing(const char * const barejid)
wprintw(console->win, "!! %s is typing a message...\n", display_usr);
wattroff(console->win, COLOUR_TYPING);
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -152,7 +153,7 @@ cons_show_incoming_message(const char * const short_from, const int win_index)
wprintw(console->win, "<< incoming from %s (%d)\n", short_from, ui_index);
wattroff(console->win, COLOUR_INCOMING);
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -197,7 +198,7 @@ cons_about(void)
prefresh(console->win, 0, 0, 1, 0, rows-3, cols-1);
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -225,7 +226,7 @@ cons_check_version(gboolean not_available_msg)
}
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
}
@ -246,91 +247,27 @@ cons_show_login_success(ProfAccount *account)
wprintw(console->win, " (priority %d)",
accounts_get_priority_for_presence_type(account->name, presence));
wprintw(console->win, ".\n");
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
void
cons_show_wins(void)
{
int i = 0;
int count = 0;
int ui_index = 0;
cons_show("");
cons_show("Active windows:");
win_print_time(console, '-');
wprintw(console->win, "1: Console\n");
GSList *window_strings = wins_create_summary();
for (i = 1; i < NUM_WINS; i++) {
if (windows[i] != NULL) {
count++;
}
}
if (count != 0) {
for (i = 1; i < NUM_WINS; i++) {
if (windows[i] != NULL) {
ProfWin *window = windows[i];
win_print_time(console, '-');
ui_index = i + 1;
if (ui_index == 10) {
ui_index = 0;
}
switch (window->type)
{
case WIN_CHAT:
wprintw(console->win, "%d: Chat %s", ui_index, window->from);
PContact contact = roster_get_contact(window->from);
if (contact != NULL) {
if (p_contact_name(contact) != NULL) {
wprintw(console->win, " (%s)", p_contact_name(contact));
}
wprintw(console->win, " - %s", p_contact_presence(contact));
}
if (window->unread > 0) {
wprintw(console->win, ", %d unread", window->unread);
}
break;
case WIN_PRIVATE:
wprintw(console->win, "%d: Private %s", ui_index, window->from);
if (window->unread > 0) {
wprintw(console->win, ", %d unread", window->unread);
}
break;
case WIN_MUC:
wprintw(console->win, "%d: Room %s", ui_index, window->from);
if (window->unread > 0) {
wprintw(console->win, ", %d unread", window->unread);
}
break;
case WIN_DUCK:
wprintw(console->win, "%d: DuckDuckGo search", ui_index);
break;
default:
break;
}
wprintw(console->win, "\n");
}
}
GSList *curr = window_strings;
while (curr != NULL) {
win_print_time(console, '-');
wprintw(console->win, curr->data);
wprintw(console->win, "\n");
curr = g_slist_next(curr);
}
cons_show("");
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -348,7 +285,7 @@ cons_show_room_invites(GSList *invites)
}
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -480,7 +417,7 @@ cons_show_info(PContact pcontact)
ordered_resources = g_list_next(ordered_resources);
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -554,7 +491,7 @@ cons_show_caps(const char * const contact, Resource *resource)
}
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -580,7 +517,7 @@ cons_show_software_version(const char * const jid, const char * const presence,
cons_show("OS : %s", os);
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -599,6 +536,9 @@ cons_show_received_subs(void)
}
g_slist_free_full(received, g_free);
}
wins_refresh_console();
cons_alert();
}
void
@ -618,6 +558,9 @@ cons_show_sent_subs(void)
} else {
cons_show("No pending requests sent.");
}
wins_refresh_console();
cons_alert();
}
void
@ -639,7 +582,7 @@ cons_show_room_list(GSList *rooms, const char * const conference_node)
cons_show("No chat rooms at %s", conference_node);
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -681,7 +624,7 @@ cons_show_disco_info(const char *jid, GSList *identities, GSList *features)
features = g_slist_next(features);
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
}
@ -706,7 +649,7 @@ cons_show_disco_items(GSList *items, const char * const jid)
cons_show("");
cons_show("No service discovery items for %s", jid);
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -720,7 +663,7 @@ cons_show_status(const char * const barejid)
} else {
cons_show("No such contact \"%s\" in roster.", barejid);
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -757,7 +700,7 @@ cons_show_room_invite(const char * const invitor, const char * const room,
FREE_SET_NULL(display_from);
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -786,7 +729,7 @@ cons_show_account_list(gchar **accounts)
cons_show("");
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -899,7 +842,7 @@ cons_show_account(ProfAccount *account)
}
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -992,7 +935,7 @@ cons_show_ui_prefs(void)
cons_statuses_setting();
cons_titlebar_setting();
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1036,7 +979,7 @@ cons_show_desktop_prefs(void)
cons_show("");
cons_notify_setting();
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1100,7 +1043,7 @@ cons_show_chat_prefs(void)
cons_gone_setting();
cons_history_setting();
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1137,7 +1080,7 @@ cons_show_log_prefs(void)
cons_chlog_setting();
cons_grlog_setting();
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1173,7 +1116,7 @@ cons_show_presence_prefs(void)
cons_show("");
cons_autoaway_setting();
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1218,7 +1161,7 @@ cons_show_connection_prefs(void)
cons_reconnect_setting();
cons_autoping_setting();
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1237,7 +1180,7 @@ cons_show_themes(GSList *themes)
}
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1258,7 +1201,7 @@ cons_prefs(void)
cons_show_connection_prefs();
cons_show("");
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1281,7 +1224,7 @@ cons_help(void)
cons_show("/help [command] - Detailed help on a specific command.");
cons_show("");
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1303,7 +1246,7 @@ cons_navigation_help(void)
cons_show("PAGE UP, PAGE DOWN : Page the main window.");
cons_show("");
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1394,7 +1337,7 @@ cons_show_roster_group(const char * const group, GSList *list)
}
_show_roster_contacts(list, FALSE);
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1405,7 +1348,7 @@ cons_show_roster(GSList *list)
cons_show("Roster:");
_show_roster_contacts(list, TRUE);
ui_console_dirty();
wins_refresh_console();
cons_alert();
}
@ -1423,7 +1366,7 @@ cons_show_contacts(GSList *list)
curr = g_slist_next(curr);
}
ui_console_dirty();
wins_refresh_console();
cons_alert();
}

File diff suppressed because it is too large Load Diff

View File

@ -40,10 +40,6 @@
#include "xmpp/xmpp.h"
#define INP_WIN_MAX 1000
#define NUM_WINS 10
// holds console at index 0 and chat wins 1 through to 9
ProfWin* windows[NUM_WINS];
// ui startup and control
void ui_init(void);
@ -63,7 +59,6 @@ void ui_new_chat_win(const char * const to);
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

View File

@ -23,6 +23,14 @@
#ifndef WINDOW_H
#define WINDOW_H
#include "config.h"
#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#endif
#include "contact.h"
#define PAD_SIZE 1000

456
src/ui/windows.c Normal file
View File

@ -0,0 +1,456 @@
/*
* windows.c
*
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <string.h>
#include <glib.h>
#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#endif
#include "config/theme.h"
#include "ui/ui.h"
#include "ui/window.h"
#include "ui/windows.h"
#define NUM_WINS 10
static ProfWin* windows[NUM_WINS];
static int current;
static int max_cols;
//static GHashTable *windows;
void
wins_init(void)
{
/*
windows = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
window_free);
int cols = getmaxx(stdscr);
ProfWin *console = win_create(CONS_WIN_TITLE, cols, WIN_CONSOLE);
g_hash_table_insert(GINT_TO_POINTER(1), console);
current = console;
*/
int i;
for (i = 0; i < NUM_WINS; i++) {
windows[i] = NULL;
}
max_cols = getmaxx(stdscr);
int cols = getmaxx(stdscr);
windows[0] = win_create("_cons", cols, WIN_CONSOLE);
current = 0;
}
ProfWin *
wins_get_console(void)
{
return windows[0];
// return g_hash_table_lookup(windows, GINT_TO_POINTER(1));
}
ProfWin *
wins_get_current(void)
{
return windows[current];
}
void
wins_set_current_by_num(int i)
{
if (i < NUM_WINS) {
current = i;
}
}
ProfWin *
wins_get_by_num(int i)
{
if (i >= NUM_WINS) {
return NULL;
} else {
return windows[i];
}
}
ProfWin *
wins_get_by_recipient(const char * const recipient)
{
int i = 0;
for (i = 1; i < NUM_WINS; i++) {
if ((windows[i] != NULL) && g_strcmp0(windows[i]->from, recipient) == 0) {
return windows[i];
}
}
return NULL;
}
int
wins_get_num(ProfWin *window)
{
int i = 0;
for (i = 0; i < NUM_WINS; i++) {
if ((windows[i] != NULL) && g_strcmp0(windows[i]->from, window->from) == 0) {
return i;
}
}
return 0;
}
int
wins_get_current_num(void)
{
return current;
}
void
wins_close_current(void)
{
wins_close_by_num(current);
}
void
wins_close_by_num(int i)
{
if (i > 0 && i < NUM_WINS) {
win_free(windows[i]);
windows[i] = NULL;
if (i == current) {
current = 0;
wins_refresh_current();
}
}
}
void
wins_refresh_current(void)
{
int rows, cols;
getmaxyx(stdscr, rows, cols);
prefresh(windows[current]->win, windows[current]->y_pos, 0, 1, 0, rows-3, cols-1);
}
void
wins_clear_current(void)
{
werase(windows[current]->win);
wins_refresh_current();
}
gboolean
wins_is_current(ProfWin *window)
{
if (g_strcmp0(windows[current]->from, window->from) == 0) {
return TRUE;
} else {
return FALSE;
}
}
ProfWin *
wins_new(const char * const from, win_type_t type)
{
int i;
for (i = 1; i < NUM_WINS; i++) {
if (windows[i] == NULL) {
break;
}
}
if (i != NUM_WINS) {
int cols = getmaxx(stdscr);
windows[i] = win_create(from, cols, type);
return windows[i];
} else {
return NULL;
}
}
int
wins_get_total_unread(void)
{
int i;
int result = 0;
for (i = 0; i < NUM_WINS; i++) {
if (windows[i] != NULL) {
result += windows[i]->unread;
}
}
return result;
}
void
wins_resize_all(void)
{
int rows, cols;
getmaxyx(stdscr, rows, cols);
// only make the pads bigger, to avoid data loss on cropping
if (cols > max_cols) {
max_cols = cols;
int i;
for (i = 0; i < NUM_WINS; i++) {
if (windows[i] != NULL) {
wresize(windows[i]->win, PAD_SIZE, cols);
}
}
}
prefresh(windows[current]->win, windows[current]->y_pos, 0, 1, 0, rows-3, cols-1);
}
void
wins_refresh_console(void)
{
if (current == 0) {
wins_refresh_current();
}
}
gboolean
wins_full(void)
{
int i;
for (i = 1; i < NUM_WINS; i++) {
if (windows[i] == NULL) {
return FALSE;
}
}
return TRUE;
}
gboolean
wins_duck_exists(void)
{
int i;
for (i = 1; i < NUM_WINS; i++) {
if (windows[i] != NULL) {
if (windows[i]->type == WIN_DUCK)
return TRUE;
}
}
return FALSE;
}
GSList *
wins_get_chat_recipients(void)
{
GSList *result = NULL;
int i;
for (i = 1; i < NUM_WINS; i++) {
if (windows[i] != NULL && windows[i]->type == WIN_CHAT) {
result = g_slist_append(result, windows[i]->from);
}
}
return result;
}
GSList *
wins_get_prune_recipients(void)
{
GSList *result = NULL;
int i;
for (i = 1; i < NUM_WINS; i++) {
if (windows[i] != NULL && windows[i]->unread == 0 && windows[i]->type != WIN_MUC) {
result = g_slist_append(result, windows[i]->from);
}
}
return result;
}
void
wins_lost_connection(void)
{
int i;
// show message in all active chats
for (i = 1; i < NUM_WINS; i++) {
if (windows[i] != NULL) {
ProfWin *window = windows[i];
win_print_time(window, '-');
wattron(window->win, COLOUR_ERROR);
wprintw(window->win, "%s\n", "Lost connection.");
wattroff(window->win, COLOUR_ERROR);
// if current win, set current_win_dirty
if (wins_is_current(window)) {
wins_refresh_current();
}
}
}
}
gboolean
wins_tidy(void)
{
int gap = 1;
int filler = 1;
gboolean tidied = FALSE;
for (gap = 1; gap < NUM_WINS; gap++) {
// if a gap
if (windows[gap] == NULL) {
// find next used window and move into gap
for (filler = gap + 1; filler < NUM_WINS; filler++) {
if (windows[filler] != NULL) {
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;
}
GSList *
wins_create_summary(void)
{
GSList *result = NULL;
result = g_slist_append(result, strdup("1: Console"));
int count = 0;
int ui_index = 0;
int i;
for (i = 1; i < NUM_WINS; i++) {
if (windows[i] != NULL) {
count++;
}
}
if (count != 0) {
for (i = 1; i < NUM_WINS; i++) {
if (windows[i] != NULL) {
ProfWin *window = windows[i];
ui_index = i + 1;
if (ui_index == 10) {
ui_index = 0;
}
GString *chat_string;
GString *priv_string;
GString *muc_string;
GString *duck_string;
switch (window->type)
{
case WIN_CHAT:
chat_string = g_string_new("");
g_string_printf(chat_string, "%d: Chat %s", ui_index, window->from);
PContact contact = roster_get_contact(window->from);
if (contact != NULL) {
if (p_contact_name(contact) != NULL) {
GString *chat_name = g_string_new("");
g_string_printf(chat_name, " (%s)", p_contact_name(contact));
g_string_append(chat_string, chat_name->str);
g_string_free(chat_name, TRUE);
}
GString *chat_presence = g_string_new("");
g_string_printf(chat_presence, " - %s", p_contact_presence(contact));
g_string_append(chat_string, chat_presence->str);
g_string_free(chat_presence, TRUE);
}
if (window->unread > 0) {
GString *chat_unread = g_string_new("");
g_string_printf(chat_unread, ", %d unread", window->unread);
g_string_append(chat_string, chat_unread->str);
g_string_free(chat_unread, TRUE);
}
result = g_slist_append(result, strdup(chat_string->str));
g_string_free(chat_string, TRUE);
break;
case WIN_PRIVATE:
priv_string = g_string_new("");
g_string_printf(priv_string, "%d: Private %s", ui_index, window->from);
if (window->unread > 0) {
GString *priv_unread = g_string_new("");
g_string_printf(priv_unread, ", %d unread", window->unread);
g_string_append(priv_string, priv_unread->str);
g_string_free(priv_unread, TRUE);
}
result = g_slist_append(result, strdup(priv_string->str));
g_string_free(priv_string, TRUE);
break;
case WIN_MUC:
muc_string = g_string_new("");
g_string_printf(muc_string, "%d: Room %s", ui_index, window->from);
if (window->unread > 0) {
GString *muc_unread = g_string_new("");
g_string_printf(muc_unread, ", %d unread", window->unread);
g_string_append(muc_string, muc_unread->str);
g_string_free(muc_unread, TRUE);
}
result = g_slist_append(result, strdup(muc_string->str));
g_string_free(muc_string, TRUE);
break;
case WIN_DUCK:
duck_string = g_string_new("");
g_string_printf(duck_string, "%d: DuckDuckGo search", ui_index);
result = g_slist_append(result, strdup(duck_string->str));
g_string_free(duck_string, TRUE);
break;
default:
break;
}
}
}
}
return result;
}

51
src/ui/windows.h Normal file
View File

@ -0,0 +1,51 @@
/*
* windows.h
*
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef WINDOWS_H
#define WINDOWS_H
void wins_init(void);
ProfWin * wins_get_console(void);
ProfWin * wins_get_current(void);
void wins_set_current_by_num(int i);
ProfWin * wins_get_by_num(int i);
ProfWin * wins_get_by_recipient(const char * const recipient);
int wins_get_num(ProfWin *window);
int wins_get_current_num(void);
void wins_close_current(void);
void wins_close_by_num(int i);
void wins_refresh_current(void);
void wins_refresh_console(void);
void wins_clear_current(void);
gboolean wins_is_current(ProfWin *window);
ProfWin * wins_new(const char * const from, win_type_t type);
int wins_get_total_unread(void);
void wins_resize_all(void);
gboolean wins_full(void);
gboolean wins_duck_exists(void);
GSList * wins_get_chat_recipients(void);
GSList * wins_get_prune_recipients(void);
void wins_lost_connection(void);
gboolean wins_tidy(void);
GSList * wins_create_summary(void);
#endif