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

650 lines
18 KiB
C
Raw Normal View History

/*
2013-02-02 14:57:46 -05:00
* statusbar.c
2019-11-13 06:11:05 -05:00
* vim: expandtab:ts=4:sts=4:sw=4
2012-02-20 15:07:38 -05:00
*
2019-01-22 05:31:45 -05:00
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
2021-01-08 10:36:30 -05:00
* Copyright (C) 2019 - 2021 Michael Vetter <jubalh@iodoru.org>
*
2012-02-20 15:07:38 -05:00
* 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
2016-07-23 20:14:49 -04:00
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
2012-02-20 15:07:38 -05:00
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
2012-02-20 15:07:38 -05:00
*/
2016-03-31 16:05:02 -04:00
#include "config.h"
2012-09-08 11:51:09 -04:00
2013-08-23 17:39:03 -04:00
#include <assert.h>
#include <string.h>
#include <stdlib.h>
2016-03-31 16:05:02 -04:00
#ifdef HAVE_NCURSESW_NCURSES_H
2013-01-02 15:27:37 -05:00
#include <ncursesw/ncurses.h>
2016-03-31 16:05:02 -04:00
#elif HAVE_NCURSES_H
#include <ncurses.h>
#elif HAVE_CURSES_H
#include <curses.h>
2012-09-08 11:51:09 -04:00
#endif
#include "config/theme.h"
#include "config/preferences.h"
#include "ui/ui.h"
#include "ui/statusbar.h"
2014-04-07 16:50:28 -04:00
#include "ui/inputwin.h"
#include "ui/screen.h"
#include "xmpp/roster_list.h"
#include "xmpp/contact.h"
2012-02-12 17:20:21 -05:00
2020-07-07 08:18:57 -04:00
typedef struct _status_bar_tab_t
{
2018-03-09 16:11:59 -05:00
win_type_t window_type;
2020-07-07 08:18:57 -04:00
char* identifier;
2018-03-08 14:55:17 -05:00
gboolean highlight;
2020-07-07 08:18:57 -04:00
char* display_name;
2018-03-08 14:55:17 -05:00
} StatusBarTab;
2020-07-07 08:18:57 -04:00
typedef struct _status_bar_t
{
gchar* time;
char* prompt;
char* fulljid;
GHashTable* tabs;
2018-03-08 14:55:17 -05:00
int current_tab;
2018-03-07 15:58:07 -05:00
} StatusBar;
2020-07-07 08:18:57 -04:00
static GTimeZone* tz;
static StatusBar* statusbar;
static WINDOW* statusbar_win;
2018-03-08 14:55:17 -05:00
2018-03-09 19:32:56 -05:00
static int _status_bar_draw_time(int pos);
2018-03-10 17:41:55 -05:00
static void _status_bar_draw_maintext(int pos);
2018-03-09 19:32:56 -05:00
static int _status_bar_draw_bracket(gboolean current, int pos, char* ch);
2018-03-09 20:30:28 -05:00
static int _status_bar_draw_extended_tabs(int pos);
2020-07-07 08:18:57 -04:00
static int _status_bar_draw_tab(StatusBarTab* tab, int pos, int num);
static void _destroy_tab(StatusBarTab* tab);
2018-03-08 18:11:49 -05:00
static int _tabs_width(void);
2020-07-07 08:18:57 -04:00
static char* _display_name(StatusBarTab* tab);
2018-03-09 20:30:28 -05:00
static gboolean _extended_new(void);
2018-03-08 14:55:17 -05:00
2014-04-07 16:12:30 -04:00
void
2018-03-07 16:29:41 -05:00
status_bar_init(void)
2012-02-12 17:20:21 -05:00
{
2018-03-07 16:29:41 -05:00
tz = g_time_zone_new_local();
2018-03-07 15:58:07 -05:00
statusbar = malloc(sizeof(StatusBar));
2018-03-07 16:29:41 -05:00
statusbar->time = NULL;
2018-03-10 17:16:52 -05:00
statusbar->prompt = NULL;
2018-03-10 17:41:55 -05:00
statusbar->fulljid = NULL;
2018-03-08 14:55:17 -05:00
statusbar->tabs = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)_destroy_tab);
2020-07-07 08:18:57 -04:00
StatusBarTab* console = calloc(1, sizeof(StatusBarTab));
2018-03-09 16:11:59 -05:00
console->window_type = WIN_CONSOLE;
console->identifier = strdup("console");
console->display_name = NULL;
2018-03-08 14:55:17 -05:00
g_hash_table_insert(statusbar->tabs, GINT_TO_POINTER(1), console);
statusbar->current_tab = 1;
2018-03-07 16:29:41 -05:00
int row = screen_statusbar_row();
2018-03-07 15:58:07 -05:00
int cols = getmaxx(stdscr);
statusbar_win = newwin(1, cols, row, 0);
2018-03-07 16:29:41 -05:00
2018-03-10 17:16:52 -05:00
status_bar_draw();
2012-02-12 17:20:21 -05:00
}
2018-03-07 16:29:41 -05:00
void
status_bar_close(void)
{
if (tz) {
g_time_zone_unref(tz);
}
if (statusbar) {
if (statusbar->time) {
g_free(statusbar->time);
}
2018-03-10 17:16:52 -05:00
if (statusbar->prompt) {
free(statusbar->prompt);
2018-03-07 17:12:15 -05:00
}
2018-03-10 17:41:55 -05:00
if (statusbar->fulljid) {
free(statusbar->fulljid);
}
if (statusbar->tabs) {
g_hash_table_destroy(statusbar->tabs);
}
2018-03-07 17:12:15 -05:00
free(statusbar);
2018-03-07 16:29:41 -05:00
}
}
2014-04-07 16:12:30 -04:00
void
status_bar_resize(void)
2012-04-21 20:25:19 -04:00
{
int cols = getmaxx(stdscr);
2018-03-07 15:58:07 -05:00
werase(statusbar_win);
int row = screen_statusbar_row();
2018-03-07 15:58:07 -05:00
wresize(statusbar_win, 1, cols);
mvwin(statusbar_win, row, 0);
2018-03-07 16:37:29 -05:00
2018-03-10 17:16:52 -05:00
status_bar_draw();
2012-04-21 20:25:19 -04:00
}
2012-02-12 17:34:31 -05:00
2014-04-07 16:12:30 -04:00
void
status_bar_set_all_inactive(void)
{
2018-03-08 14:55:17 -05:00
g_hash_table_remove_all(statusbar->tabs);
}
2014-04-07 16:12:30 -04:00
void
status_bar_current(int i)
2013-10-02 18:33:48 -04:00
{
2018-03-08 14:55:17 -05:00
if (i == 0) {
statusbar->current_tab = 10;
} else {
statusbar->current_tab = i;
}
2018-03-10 17:16:52 -05:00
status_bar_draw();
2013-10-02 18:33:48 -04:00
}
2014-04-07 16:12:30 -04:00
void
status_bar_inactive(const int win)
2012-02-12 17:20:21 -05:00
{
2018-03-08 14:55:17 -05:00
int true_win = win;
if (true_win == 0) {
true_win = 10;
}
g_hash_table_remove(statusbar->tabs, GINT_TO_POINTER(true_win));
2018-03-10 17:16:52 -05:00
status_bar_draw();
2012-02-12 17:20:21 -05:00
}
2014-04-07 16:12:30 -04:00
void
2020-07-07 08:18:57 -04:00
_create_tab(const int win, win_type_t wintype, char* identifier, gboolean highlight)
2012-02-12 17:20:21 -05:00
{
2018-03-08 14:55:17 -05:00
int true_win = win;
if (true_win == 0) {
true_win = 10;
}
2020-07-07 08:18:57 -04:00
StatusBarTab* tab = malloc(sizeof(StatusBarTab));
2018-03-09 16:11:59 -05:00
tab->identifier = strdup(identifier);
tab->highlight = highlight;
2018-03-09 16:11:59 -05:00
tab->window_type = wintype;
tab->display_name = NULL;
if (tab->window_type == WIN_CHAT) {
PContact contact = NULL;
if (roster_exists()) {
contact = roster_get_contact(tab->identifier);
}
if (contact && p_contact_name(contact)) {
tab->display_name = strdup(p_contact_name(contact));
} else {
2020-07-07 08:18:57 -04:00
char* pref = prefs_get_string(PREF_STATUSBAR_CHAT);
if (g_strcmp0("user", pref) == 0) {
2020-07-07 08:18:57 -04:00
Jid* jidp = jid_create(tab->identifier);
if (jidp) {
2020-07-07 08:18:57 -04:00
tab->display_name = jidp->localpart != NULL ? strdup(jidp->localpart) : strdup(jidp->barejid);
jid_destroy(jidp);
} else {
tab->display_name = strdup(tab->identifier);
}
} else {
tab->display_name = strdup(tab->identifier);
}
g_free(pref);
}
}
2018-03-08 14:55:17 -05:00
g_hash_table_replace(statusbar->tabs, GINT_TO_POINTER(true_win), tab);
2018-03-10 17:16:52 -05:00
status_bar_draw();
2012-02-12 17:20:21 -05:00
}
2014-04-07 16:12:30 -04:00
void
2020-07-07 08:18:57 -04:00
status_bar_active(const int win, win_type_t wintype, char* identifier)
{
_create_tab(win, wintype, identifier, FALSE);
}
2018-03-08 14:55:17 -05:00
void
status_bar_new(const int win, win_type_t wintype, char* identifier)
{
_create_tab(win, wintype, identifier, TRUE);
2012-02-12 17:20:21 -05:00
}
2014-04-07 16:12:30 -04:00
void
2020-07-07 08:18:57 -04:00
status_bar_set_prompt(const char* const prompt)
2012-02-12 17:20:21 -05:00
{
2018-03-10 17:16:52 -05:00
if (statusbar->prompt) {
free(statusbar->prompt);
statusbar->prompt = NULL;
2018-03-07 17:12:15 -05:00
}
2018-03-10 17:16:52 -05:00
statusbar->prompt = strdup(prompt);
2018-03-07 17:12:15 -05:00
2018-03-10 17:16:52 -05:00
status_bar_draw();
2012-02-12 17:20:21 -05:00
}
2014-04-07 16:12:30 -04:00
void
2018-03-10 17:16:52 -05:00
status_bar_clear_prompt(void)
2012-02-12 17:34:31 -05:00
{
2018-03-10 17:16:52 -05:00
if (statusbar->prompt) {
free(statusbar->prompt);
statusbar->prompt = NULL;
2018-03-08 14:55:17 -05:00
}
2018-03-10 17:16:52 -05:00
status_bar_draw();
2012-02-12 17:34:31 -05:00
}
2018-03-10 17:41:55 -05:00
void
2020-07-07 08:18:57 -04:00
status_bar_set_fulljid(const char* const fulljid)
2018-03-10 17:41:55 -05:00
{
if (statusbar->fulljid) {
free(statusbar->fulljid);
statusbar->fulljid = NULL;
}
statusbar->fulljid = strdup(fulljid);
status_bar_draw();
}
void
status_bar_clear_fulljid(void)
{
if (statusbar->fulljid) {
free(statusbar->fulljid);
statusbar->fulljid = NULL;
}
status_bar_draw();
}
2014-04-07 16:12:30 -04:00
void
2018-03-10 17:16:52 -05:00
status_bar_draw(void)
2014-04-05 20:33:00 -04:00
{
2018-03-08 17:27:49 -05:00
werase(statusbar_win);
2018-03-07 16:37:29 -05:00
wbkgd(statusbar_win, theme_attrs(THEME_STATUS_TEXT));
2018-03-07 17:12:15 -05:00
int pos = 1;
2018-03-09 19:32:56 -05:00
pos = _status_bar_draw_time(pos);
2018-03-07 16:29:41 -05:00
2018-03-10 17:41:55 -05:00
_status_bar_draw_maintext(pos);
2018-03-07 16:29:41 -05:00
2018-03-09 19:32:56 -05:00
pos = getmaxx(stdscr) - _tabs_width();
if (pos < 0) {
pos = 0;
}
2018-03-09 19:32:56 -05:00
gint max_tabs = prefs_get_statusbartabs();
for (int i = 1; i <= max_tabs; i++) {
2020-07-07 08:18:57 -04:00
StatusBarTab* tab = g_hash_table_lookup(statusbar->tabs, GINT_TO_POINTER(i));
2018-03-09 19:32:56 -05:00
if (tab) {
2018-03-09 20:30:28 -05:00
pos = _status_bar_draw_tab(tab, pos, i);
2018-03-07 17:12:15 -05:00
}
2018-03-07 16:29:41 -05:00
}
2019-07-22 08:16:10 -04:00
_status_bar_draw_extended_tabs(pos);
2018-03-08 14:55:17 -05:00
2018-03-09 19:32:56 -05:00
wnoutrefresh(statusbar_win);
inp_put_back();
}
2018-03-09 20:30:28 -05:00
static gboolean
_extended_new(void)
{
gint max_tabs = prefs_get_statusbartabs();
int tabs_count = g_hash_table_size(statusbar->tabs);
if (tabs_count <= max_tabs) {
return FALSE;
}
for (int i = max_tabs + 1; i <= tabs_count; i++) {
2020-07-07 08:18:57 -04:00
StatusBarTab* tab = g_hash_table_lookup(statusbar->tabs, GINT_TO_POINTER(i));
2018-03-09 20:30:28 -05:00
if (tab && tab->highlight) {
return TRUE;
}
}
return FALSE;
}
2018-03-09 19:32:56 -05:00
static int
2018-03-09 20:30:28 -05:00
_status_bar_draw_extended_tabs(int pos)
2018-03-09 19:32:56 -05:00
{
2018-03-09 20:30:28 -05:00
gint max_tabs = prefs_get_statusbartabs();
2018-03-10 17:54:02 -05:00
if (max_tabs == 0) {
return pos;
}
2018-03-09 20:30:28 -05:00
if (g_hash_table_size(statusbar->tabs) > max_tabs) {
gboolean is_current = statusbar->current_tab > max_tabs;
pos = _status_bar_draw_bracket(is_current, pos, "[");
int status_attrs;
if (is_current) {
2019-09-29 09:47:11 -04:00
// currently selected
status_attrs = theme_attrs(THEME_STATUS_CURRENT);
} else if (_extended_new()) {
2019-09-29 09:47:11 -04:00
// new one
2018-03-09 20:30:28 -05:00
status_attrs = theme_attrs(THEME_STATUS_NEW);
} else {
2019-09-29 09:47:11 -04:00
// all other
status_attrs = theme_attrs(THEME_STATUS_ACTIVE);
2018-03-09 20:30:28 -05:00
}
wattron(statusbar_win, status_attrs);
mvwprintw(statusbar_win, 0, pos, ">");
wattroff(statusbar_win, status_attrs);
pos++;
pos = _status_bar_draw_bracket(is_current, pos, "]");
}
return pos;
}
static int
2020-07-07 08:18:57 -04:00
_status_bar_draw_tab(StatusBarTab* tab, int pos, int num)
2018-03-09 20:30:28 -05:00
{
int display_num = num == 10 ? 0 : num;
gboolean is_current = num == statusbar->current_tab;
2018-03-09 18:44:28 -05:00
gboolean show_number = prefs_get_boolean(PREF_STATUSBAR_SHOW_NUMBER);
2018-03-08 17:27:49 -05:00
gboolean show_name = prefs_get_boolean(PREF_STATUSBAR_SHOW_NAME);
gboolean show_read = prefs_get_boolean(PREF_STATUSBAR_SHOW_READ);
// dont show this
if (!show_read && !is_current && !tab->highlight)
return pos;
2018-03-08 17:27:49 -05:00
2018-03-09 19:32:56 -05:00
pos = _status_bar_draw_bracket(is_current, pos, "[");
2018-03-08 14:55:17 -05:00
int status_attrs;
if (is_current) {
status_attrs = theme_attrs(THEME_STATUS_CURRENT);
} else if (tab->highlight) {
2018-03-09 19:32:56 -05:00
status_attrs = theme_attrs(THEME_STATUS_NEW);
} else {
status_attrs = theme_attrs(THEME_STATUS_ACTIVE);
2018-03-08 14:55:17 -05:00
}
2018-03-09 19:32:56 -05:00
wattron(statusbar_win, status_attrs);
if (show_number) {
mvwprintw(statusbar_win, 0, pos, "%d", display_num);
pos++;
}
if (show_number && show_name) {
mvwprintw(statusbar_win, 0, pos, ":");
pos++;
}
if (show_name) {
2020-07-07 08:18:57 -04:00
char* display_name = _display_name(tab);
2018-03-09 19:32:56 -05:00
mvwprintw(statusbar_win, 0, pos, display_name);
2018-03-10 19:46:27 -05:00
pos += utf8_display_len(display_name);
2018-03-09 19:32:56 -05:00
free(display_name);
}
wattroff(statusbar_win, status_attrs);
pos = _status_bar_draw_bracket(is_current, pos, "]");
2018-03-08 14:55:17 -05:00
2018-03-09 19:32:56 -05:00
return pos;
}
static int
_status_bar_draw_bracket(gboolean current, int pos, char* ch)
{
int bracket_attrs = theme_attrs(THEME_STATUS_BRACKET);
2018-03-08 14:55:17 -05:00
wattron(statusbar_win, bracket_attrs);
2018-03-09 19:32:56 -05:00
if (current) {
mvwprintw(statusbar_win, 0, pos, "-");
} else {
mvwprintw(statusbar_win, 0, pos, ch);
}
2018-03-08 14:55:17 -05:00
wattroff(statusbar_win, bracket_attrs);
pos++;
2018-03-09 19:32:56 -05:00
return pos;
}
static int
_status_bar_draw_time(int pos)
{
2020-07-07 08:18:57 -04:00
char* time_pref = prefs_get_string(PREF_TIME_STATUSBAR);
2018-03-09 19:32:56 -05:00
if (g_strcmp0(time_pref, "off") == 0) {
g_free(time_pref);
2018-03-09 19:32:56 -05:00
return pos;
}
if (statusbar->time) {
g_free(statusbar->time);
statusbar->time = NULL;
}
2020-07-07 08:18:57 -04:00
GDateTime* datetime = g_date_time_new_now(tz);
statusbar->time = g_date_time_format(datetime, time_pref);
2018-03-09 19:32:56 -05:00
assert(statusbar->time != NULL);
g_date_time_unref(datetime);
int bracket_attrs = theme_attrs(THEME_STATUS_BRACKET);
int time_attrs = theme_attrs(THEME_STATUS_TIME);
size_t len = strlen(statusbar->time);
wattron(statusbar_win, bracket_attrs);
mvwaddch(statusbar_win, 0, pos, '[');
2018-03-08 14:55:17 -05:00
pos++;
2018-03-09 19:32:56 -05:00
wattroff(statusbar_win, bracket_attrs);
wattron(statusbar_win, time_attrs);
mvwprintw(statusbar_win, 0, pos, statusbar->time);
pos += len;
wattroff(statusbar_win, time_attrs);
2018-03-08 14:55:17 -05:00
wattron(statusbar_win, bracket_attrs);
2018-03-09 19:32:56 -05:00
mvwaddch(statusbar_win, 0, pos, ']');
2018-03-08 14:55:17 -05:00
wattroff(statusbar_win, bracket_attrs);
2018-03-09 19:32:56 -05:00
pos += 2;
2018-03-08 14:55:17 -05:00
g_free(time_pref);
2018-03-09 19:32:56 -05:00
return pos;
}
static void
2018-03-10 17:41:55 -05:00
_status_bar_draw_maintext(int pos)
2018-03-09 19:32:56 -05:00
{
2018-03-10 17:16:52 -05:00
if (statusbar->prompt) {
mvwprintw(statusbar_win, 0, pos, statusbar->prompt);
2018-03-10 17:41:55 -05:00
return;
}
gboolean stop = FALSE;
2018-03-10 17:41:55 -05:00
if (statusbar->fulljid) {
2020-07-07 08:18:57 -04:00
char* pref = prefs_get_string(PREF_STATUSBAR_SELF);
2018-03-10 17:41:55 -05:00
if (g_strcmp0(pref, "off") == 0) {
stop = true;
} else if (g_strcmp0(pref, "user") == 0) {
2020-07-07 08:18:57 -04:00
Jid* jidp = jid_create(statusbar->fulljid);
2018-03-10 17:41:55 -05:00
mvwprintw(statusbar_win, 0, pos, jidp->localpart);
jid_destroy(jidp);
stop = true;
} else if (g_strcmp0(pref, "barejid") == 0) {
2020-07-07 08:18:57 -04:00
Jid* jidp = jid_create(statusbar->fulljid);
2018-03-10 17:41:55 -05:00
mvwprintw(statusbar_win, 0, pos, jidp->barejid);
jid_destroy(jidp);
stop = true;
}
g_free(pref);
if (stop) {
2018-03-10 17:41:55 -05:00
return;
}
mvwprintw(statusbar_win, 0, pos, statusbar->fulljid);
2018-03-09 19:32:56 -05:00
}
2014-07-18 18:17:42 -04:00
}
2018-03-08 18:11:49 -05:00
static void
2020-07-07 08:18:57 -04:00
_destroy_tab(StatusBarTab* tab)
2018-03-08 18:11:49 -05:00
{
if (tab) {
2018-03-09 16:11:59 -05:00
if (tab->identifier) {
free(tab->identifier);
2018-03-08 18:11:49 -05:00
}
if (tab->display_name) {
free(tab->display_name);
}
2018-03-08 18:11:49 -05:00
free(tab);
}
}
static int
_tabs_width(void)
{
2018-03-09 18:44:28 -05:00
gboolean show_number = prefs_get_boolean(PREF_STATUSBAR_SHOW_NUMBER);
2018-03-08 18:11:49 -05:00
gboolean show_name = prefs_get_boolean(PREF_STATUSBAR_SHOW_NAME);
gboolean show_read = prefs_get_boolean(PREF_STATUSBAR_SHOW_READ);
2018-03-08 18:11:49 -05:00
gint max_tabs = prefs_get_statusbartabs();
2018-03-09 18:44:28 -05:00
if (show_name && show_number) {
2018-03-09 20:30:28 -05:00
int width = g_hash_table_size(statusbar->tabs) > max_tabs ? 4 : 1;
for (int i = 1; i <= max_tabs; i++) {
2020-07-07 08:18:57 -04:00
StatusBarTab* tab = g_hash_table_lookup(statusbar->tabs, GINT_TO_POINTER(i));
2018-03-09 17:42:20 -05:00
if (tab) {
gboolean is_current = i == statusbar->current_tab;
// dont calculate this in because not shown
if (!show_read && !is_current && !tab->highlight)
continue;
2020-07-07 08:18:57 -04:00
char* display_name = _display_name(tab);
2018-03-10 19:46:27 -05:00
width += utf8_display_len(display_name);
2018-03-09 17:42:20 -05:00
width += 4;
free(display_name);
2018-03-08 18:11:49 -05:00
}
}
2018-03-09 17:42:20 -05:00
return width;
2018-03-08 18:11:49 -05:00
}
2018-03-09 18:44:28 -05:00
if (show_name && !show_number) {
2018-03-09 20:30:28 -05:00
int width = g_hash_table_size(statusbar->tabs) > max_tabs ? 4 : 1;
for (int i = 1; i <= max_tabs; i++) {
2020-07-07 08:18:57 -04:00
StatusBarTab* tab = g_hash_table_lookup(statusbar->tabs, GINT_TO_POINTER(i));
2018-03-09 18:44:28 -05:00
if (tab) {
gboolean is_current = i == statusbar->current_tab;
// dont calculate this in because not shown
if (!show_read && !is_current && !tab->highlight)
continue;
2020-07-07 08:18:57 -04:00
char* display_name = _display_name(tab);
2018-03-10 19:46:27 -05:00
width += utf8_display_len(display_name);
2018-03-09 18:44:28 -05:00
width += 2;
free(display_name);
}
}
return width;
}
2018-03-09 20:30:28 -05:00
if (g_hash_table_size(statusbar->tabs) > max_tabs) {
return max_tabs * 3 + (g_hash_table_size(statusbar->tabs) > max_tabs ? 4 : 1);
}
return g_hash_table_size(statusbar->tabs) * 3 + (g_hash_table_size(statusbar->tabs) > max_tabs ? 4 : 1);
2018-03-08 18:11:49 -05:00
}
2018-03-09 16:11:59 -05:00
static char*
2020-07-07 08:18:57 -04:00
_display_name(StatusBarTab* tab)
2018-03-09 16:11:59 -05:00
{
2020-07-07 08:18:57 -04:00
char* fullname = NULL;
2018-03-10 20:18:46 -05:00
2018-03-09 16:11:59 -05:00
if (tab->window_type == WIN_CONSOLE) {
2018-03-10 20:18:46 -05:00
fullname = strdup("console");
} else if (tab->window_type == WIN_XML) {
fullname = strdup("xmlconsole");
} else if (tab->window_type == WIN_PLUGIN) {
fullname = strdup(tab->identifier);
} else if (tab->window_type == WIN_CHAT) {
if (tab && tab->display_name) {
fullname = strdup(tab->display_name);
2018-03-09 16:11:59 -05:00
}
2018-03-10 20:18:46 -05:00
} else if (tab->window_type == WIN_MUC) {
2020-07-07 08:18:57 -04:00
char* pref = prefs_get_string(PREF_STATUSBAR_ROOM);
2018-03-09 16:11:59 -05:00
if (g_strcmp0("room", pref) == 0) {
2020-07-07 08:18:57 -04:00
Jid* jidp = jid_create(tab->identifier);
char* room = strdup(jidp->localpart);
2018-03-09 16:11:59 -05:00
jid_destroy(jidp);
2018-03-10 20:18:46 -05:00
fullname = room;
2018-03-09 16:11:59 -05:00
} else {
2018-03-10 20:18:46 -05:00
fullname = strdup(tab->identifier);
2018-03-09 16:11:59 -05:00
}
g_free(pref);
} else if (tab->window_type == WIN_CONFIG) {
2020-07-07 08:18:57 -04:00
char* pref = prefs_get_string(PREF_STATUSBAR_ROOM);
GString* display_str = g_string_new("");
2018-03-09 16:11:59 -05:00
if (g_strcmp0("room", pref) == 0) {
2020-07-07 08:18:57 -04:00
Jid* jidp = jid_create(tab->identifier);
2018-03-09 16:11:59 -05:00
g_string_append(display_str, jidp->localpart);
jid_destroy(jidp);
} else {
g_string_append(display_str, tab->identifier);
}
g_free(pref);
2018-03-09 16:11:59 -05:00
g_string_append(display_str, " conf");
2020-07-07 08:18:57 -04:00
char* result = strdup(display_str->str);
2018-03-09 16:11:59 -05:00
g_string_free(display_str, TRUE);
2018-03-10 20:18:46 -05:00
fullname = result;
} else if (tab->window_type == WIN_PRIVATE) {
2020-07-07 08:18:57 -04:00
char* pref = prefs_get_string(PREF_STATUSBAR_ROOM);
2018-03-09 16:11:59 -05:00
if (g_strcmp0("room", pref) == 0) {
2020-07-07 08:18:57 -04:00
GString* display_str = g_string_new("");
Jid* jidp = jid_create(tab->identifier);
2018-03-09 16:11:59 -05:00
g_string_append(display_str, jidp->localpart);
g_string_append(display_str, "/");
g_string_append(display_str, jidp->resourcepart);
jid_destroy(jidp);
2020-07-07 08:18:57 -04:00
char* result = strdup(display_str->str);
2018-03-09 16:11:59 -05:00
g_string_free(display_str, TRUE);
2018-03-10 20:18:46 -05:00
fullname = result;
2018-03-09 16:11:59 -05:00
} else {
2018-03-10 20:18:46 -05:00
fullname = strdup(tab->identifier);
2018-03-09 16:11:59 -05:00
}
g_free(pref);
2018-03-10 20:18:46 -05:00
} else {
fullname = strdup("window");
2018-03-09 16:11:59 -05:00
}
2018-03-10 20:18:46 -05:00
gint tablen = prefs_get_statusbartablen();
if (tablen == 0) {
return fullname;
}
int namelen = utf8_display_len(fullname);
if (namelen < tablen) {
return fullname;
}
2020-07-07 08:18:57 -04:00
gchar* trimmed = g_utf8_substring(fullname, 0, tablen);
2018-03-10 20:18:46 -05:00
free(fullname);
2020-07-07 08:18:57 -04:00
char* trimmedname = strdup(trimmed);
2018-03-10 20:18:46 -05:00
g_free(trimmed);
return trimmedname;
2018-03-09 16:11:59 -05:00
}