[/trunk] Changeset 95 by mikael
* Switch from buddies.* to roster.*
Seems to work fine...
--- a/mcabber/src/Makefile.am Sat Apr 16 21:13:59 2005 +0000
+++ b/mcabber/src/Makefile.am Sat Apr 16 22:28:55 2005 +0000
@@ -1,7 +1,6 @@
bin_PROGRAMS = mcabber
mcabber_SOURCES = main.c jabglue.c jabglue.h commands.c commands.h \
roster.c roster.h hbuf.c hbuf.h screen.c screen.h \
- buddies.c buddies.h \
parsecfg.c parsecfg.h utf8.c utf8.h \
utils.c utils.h lang.c lang.h list.h harddefines.h
--- a/mcabber/src/buddies.c Sat Apr 16 21:13:59 2005 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,280 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ncurses.h>
-#include <panel.h>
-#include "screen.h"
-#include "buddies.h"
-
-#include "lang.h"
-#include "utils.h"
-#include "list.h"
-
-#define STR_EMPTY(s) ((s)[0] == '\0')
-
-/* global vars for BUDDIES.C */
-int buddySelected = 0; /* Hold the selected Buddy */
-int buddyOffset = 0; /* Hold the roster offset */
-
-static LIST_HEAD(buddy_list);
-static LIST_HEAD(sorted_buddies);
-
-#define buddy_entry(n) list_entry(n, buddy_entry_t, list)
-
-
-void bud_SetBuddyStatus(char *jidfrom, enum imstatus status)
-{
- struct list_head *pos, *n;
- buddy_entry_t *tmp;
- enum imstatus oldstatus;
- int changed = 0;
-
- list_for_each_safe(pos, n, &buddy_list) {
- tmp = buddy_entry(pos);
- if (!strcasecmp(tmp->jid, jidfrom)) {
- if ((unsigned)tmp->flags != status) {
- oldstatus = tmp->flags;
- tmp->flags = status;
- changed = 1;
- }
- break;
- }
- }
- if (changed) {
- bud_DrawRoster(scr_GetRosterWindow());
- scr_LogPrint("Buddy status has changed: [%c>%c] <%s>",
- imstatus2char[oldstatus], imstatus2char[status], jidfrom);
- }
-}
-
-int compara(buddy_entry_t * t1, buddy_entry_t * t2)
-{
- const char *s1 =
- (const char *) (STR_EMPTY(t1->name) ? t1->jid : t1->name);
- const char *s2 =
- (const char *) (STR_EMPTY(t2->name) ? t2->jid : t2->name);
- return strcasecmp(s1, s2);
-}
-
-void bud_SortRoster(void)
-{
- buddy_entry_t *indice, *tmp;
- struct list_head *pos, *n;
-
- while (!list_empty(&buddy_list)) {
- indice = NULL;
- tmp = NULL;
- list_for_each_safe(pos, n, &buddy_list) {
- if (!indice) {
- indice = buddy_entry(pos);
- tmp = buddy_entry(pos);
- } else {
- tmp = buddy_entry(pos);
- if (compara(indice, tmp) > 0) {
- indice = tmp;
- }
- }
- }
- list_move_tail(&indice->list, &sorted_buddies);
- }
- list_splice(&sorted_buddies, &buddy_list);
-
- update_roster = TRUE;
-}
-
-/* Desc: Destroy (and free) buddy list
- *
- * In : none
- * Out: none
- *
- * Note: none
- */
-void bud_TerminateBuddies(void)
-{
-}
-
-/* Desc: Count elements in buddy list
- *
- * In : none
- * Out: number of buddies
- *
- * Note: none
- */
-int bud_BuddyCount(void)
-{
- int i = 0;
- struct list_head *pos, *n;
-
- list_for_each_safe(pos, n, &buddy_list) {
- i++;
- }
- return i;
-}
-
-/* Desc: Draw the roster in roster window
- *
- * In : roster window
- * Out: none
- *
- * Note: none
- */
-void bud_DrawRoster(WINDOW * win)
-{
- buddy_entry_t *tmp = NULL;
- struct list_head *pos, *nn;
- int i = 1;
- int n;
- int maxx, maxy;
- int fakeOffset = buddyOffset;
- char name[ROSTER_WIDTH];
-
- getmaxyx(win, maxy, maxx);
- maxx --; // last char is for vertical border
- name[ROSTER_WIDTH-8] = 0;
-
- /* cleanup of roster window */
- wattrset(win, COLOR_PAIR(COLOR_GENERAL));
- for (i = 0; i < maxy; i++) {
- mvwprintw(win, i, 0, "");
- for (n = 0; n < maxx; n++)
- waddch(win, ' ');
- }
-
- i = 0;
- list_for_each_safe(pos, nn, &buddy_list) {
-
- char status = '?';
- char pending = ' ';
-
- if (fakeOffset > 0) {
- fakeOffset--;
- continue;
- }
-
- tmp = buddy_entry(pos);
- if (scr_IsHiddenMessage(tmp->jid)) {
- pending = '#';
- }
-
- if (tmp->flags >= 0 && tmp->flags < imstatus_size) {
- status = imstatus2char[tmp->flags];
- }
- /*{
- if (i == (buddySelected - buddyOffset))
- wattrset(win, COLOR_PAIR(COLOR_BD_CONSEL));
- else
- wattrset(win, COLOR_PAIR(COLOR_BD_CON));
- } else*/ {
- if (i == (buddySelected - buddyOffset))
- wattrset(win, COLOR_PAIR(COLOR_BD_DESSEL));
- else
- wattrset(win, COLOR_PAIR(COLOR_BD_DES));
- }
- mvwprintw(win, i, 0, "");
- for (n = 2; n < maxx; n++)
- waddch(win, ' ');
- strncpy(name, tmp->name, ROSTER_WIDTH-8);
- mvwprintw(win, i, 0, " %c[%c] %s", pending, status, name);
- i++;
- if (i >= maxy - 1)
- break;
- }
- update_panels();
- doupdate();
-
- update_roster = FALSE;
-}
-
-/* Desc: Change selected buddy (one position down)
- *
- * In : none
- * Out: none
- *
- * Note: none
- */
-void bud_RosterDown(void)
-{
- int x, y;
- getmaxyx(scr_GetRosterWindow(), y, x);
-
- if (buddySelected+1 < bud_BuddyCount()) {
- buddySelected++;
- if (buddySelected > y)
- buddyOffset++;
- bud_DrawRoster(scr_GetRosterWindow());
- }
-}
-
-/* Desc: Change selected buddy (one position up)
- *
- * In : none
- * Out: none
- *
- * Note: none
- */
-void bud_RosterUp(void)
-{
- if (buddySelected > 0) {
- buddySelected--;
- if (buddySelected < buddyOffset)
- buddyOffset--;
- bud_DrawRoster(scr_GetRosterWindow());
- }
-}
-
-/* Desc: Retrieve info for selected buddy
- *
- * In : none
- * Out: (buddy_entry_t *) of selected buddy
- *
- * Note: none
- */
-buddy_entry_t *bud_SelectedInfo(void)
-{
- struct list_head *pos, *n;
- buddy_entry_t *tmp = NULL;
-
- int i = 0;
-
- list_for_each_safe(pos, n, &buddy_list) {
- tmp = buddy_entry(pos);
- if (i == buddySelected) {
- return tmp;
- }
- i++;
- }
- return NULL;
-}
-
-buddy_entry_t *bud_AddBuddy(const char *bjid, const char *bname)
-{
- char *p, *str;
- buddy_entry_t *tmp;
-
- tmp = calloc(1, sizeof(buddy_entry_t));
- tmp->jid = strdup(bjid);
-
- if (bname) {
- tmp->name = strdup(bname);
- } else {
- str = strdup(bjid);
- p = strstr(str, "/");
- if (p) *p = '\0';
- tmp->name = strdup(str);
- free(str);
- }
-
- ut_WriteLog("Adding buddy: %s <%s>\n", tmp->name, tmp->jid);
-
- list_add_tail(&tmp->list, &buddy_list);
- bud_DrawRoster(scr_GetRosterWindow());
-
- return tmp;
-}
-
-void bud_DeleteBuddy(buddy_entry_t *buddy)
-{
- list_del(&buddy->list);
- buddySelected = 1;
- bud_DrawRoster(scr_GetRosterWindow());
-}
--- a/mcabber/src/buddies.h Sat Apr 16 21:13:59 2005 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-#ifndef __BUDDIES_H__
-#define __BUDDIES_H__ 1
-
-#include <ncurses.h>
-#include "jabglue.h"
-#include "list.h"
-
-/* Definición de tipos */
-typedef struct _buddy_entry_t {
- char *jid;
- char *name;
- char *group;
- char *resource;
- int flags;
- struct list_head list;
-} buddy_entry_t;
-
-void bud_DrawRoster(WINDOW * win);
-void bud_RosterDown(void);
-void bud_RosterUp(void);
-void bud_TerminateBuddies(void);
-int bud_BuddyCount(void);
-void bud_SetBuddyStatus(char *jidfrom, enum imstatus status);
-void bud_SortRoster(void);
-buddy_entry_t *bud_SelectedInfo(void);
-
-buddy_entry_t *bud_AddBuddy(const char *bjid, const char *bname);
-void bud_DeleteBuddy(buddy_entry_t *buddy);
-
-#endif
--- a/mcabber/src/commands.c Sat Apr 16 21:13:59 2005 +0000
+++ b/mcabber/src/commands.c Sat Apr 16 22:28:55 2005 +0000
@@ -21,10 +21,10 @@
#include "commands.h"
#include "jabglue.h"
+#include "roster.h"
#include "screen.h"
+#include "utf8.h"
#include "utils.h"
-#include "buddies.h"
-#include "utf8.h"
// send_message(msg)
@@ -33,14 +33,25 @@
void send_message(char *msg)
{
char *buffer;
- buddy_entry_t *tmp = bud_SelectedInfo();
+ const char *jid;
+
+ if (!current_buddy) {
+ scr_LogPrint("No buddy currently selected.");
+ return;
+ }
+
+ jid = CURRENT_JID;
+ if (!jid) {
+ scr_LogPrint("No buddy currently selected.");
+ return;
+ }
// UI part
- scr_WriteOutgoingMessage(tmp->jid, msg);
+ scr_WriteOutgoingMessage(jid, msg);
// Network part
buffer = utf8_encode(msg);
- jb_send_msg(tmp->jid, buffer);
+ jb_send_msg(jid, buffer);
free(buffer);
}
--- a/mcabber/src/jabglue.c Sat Apr 16 21:13:59 2005 +0000
+++ b/mcabber/src/jabglue.c Sat Apr 16 22:28:55 2005 +0000
@@ -23,9 +23,9 @@
#include "../libjabber/jabber.h"
#include "jabglue.h"
+#include "roster.h"
#include "screen.h"
#include "utils.h"
-#include "buddies.h"
#define JABBERPORT 5222
#define JABBERSSLPORT 5223
@@ -251,6 +251,7 @@
//setautostatus(jhook.manualstatus);
setjabberstatus(1, "I'm here!");
+ buddylist_build();
/*
for (i = 0; i < clist.count; i++) {
c = (icqcontact *) clist.at(i);
@@ -315,7 +316,7 @@
const char *alias = xmlnode_get_attrib(y, "jid");
//const char *sub = xmlnode_get_attrib(y, "subscription"); // TODO Not used
const char *name = xmlnode_get_attrib(y, "name");
- const char *group = 0;
+ const char *group = NULL;
z = xmlnode_get_tag(y, "group");
if (z) group = xmlnode_get_data(z);
@@ -327,13 +328,12 @@
else
buddyname = jidtodisp(alias);
- bud_AddBuddy(alias, buddyname);
+ roster_add_user(alias, buddyname, group, ROSTER_TYPE_USER);
if (!name)
free(buddyname);
}
}
- bud_SortRoster();
postlogin();
}
@@ -627,9 +627,11 @@
if (type && !strcmp(type, "unavailable")) {
ust = offline;
}
- //scr_LogPrint("New status: ust=%d (%s)", ust, from);
+ // scr_LogPrint("New status: ust=%d (%s)", ust, from);
- bud_SetBuddyStatus(jidtodisp(from), ust);
+ roster_setstatus(jidtodisp(from), ust); // XXX memory leak
+ buddylist_build();
+ scr_DrawRoster();
/*
if (x = xmlnode_get_tag(packet->x, "status"))
if (p = xmlnode_get_data(x))
--- a/mcabber/src/main.c Sat Apr 16 21:13:59 2005 +0000
+++ b/mcabber/src/main.c Sat Apr 16 22:28:55 2005 +0000
@@ -6,13 +6,12 @@
#include <termios.h>
#include <getopt.h>
-#include "utils.h"
+#include "jabglue.h"
#include "screen.h"
-#include "buddies.h"
#include "parsecfg.h"
#include "lang.h"
+#include "utils.h"
#include "harddefines.h"
-#include "jabglue.h"
void sig_handler(int signum)
@@ -23,7 +22,7 @@
break;
case SIGTERM:
- bud_TerminateBuddies();
+ // bud_TerminateBuddies();
scr_TerminateCurses();
jb_disconnect();
printf("Killed by SIGTERM\nBye!\n");
@@ -186,19 +185,19 @@
ut_WriteLog("Entering into main loop...\n\n");
ut_WriteLog("Ready to send/receive messages...\n");
+ keypad(scr_GetInputWindow(), TRUE);
while (ret != 255) {
alarm(ping);
- keypad(scr_GetInputWindow(), TRUE);
key = scr_Getch();
if (key != ERR)
ret = process_key(key);
jb_main();
if (update_roster)
- bud_DrawRoster(scr_GetRosterWindow());
+ scr_DrawRoster();
}
jb_disconnect();
- bud_TerminateBuddies();
+ //bud_TerminateBuddies();
scr_TerminateCurses();
printf("\n\nHave a nice day!\nBye!\n");
--- a/mcabber/src/roster.c Sat Apr 16 21:13:59 2005 +0000
+++ b/mcabber/src/roster.c Sat Apr 16 22:28:55 2005 +0000
@@ -247,8 +247,14 @@
{
GSList *sl_roster_elt = groups;
roster *roster_elt;
+ roster *roster_current_buddy = NULL;
int pending_group;
+ // We need to remember which buddy is selected.
+ if (current_buddy)
+ roster_current_buddy = BUDDATA(current_buddy);
+ current_buddy = NULL;
+
// Destroy old buddylist
if (buddylist) {
g_list_free(buddylist);
@@ -296,9 +302,12 @@
sl_roster_elt = g_slist_next(sl_roster_elt);
}
+ // Check if we can find our saved current_buddy...
+ if (roster_current_buddy)
+ current_buddy = g_list_find(buddylist, roster_current_buddy);
// current_buddy initialization
if (!current_buddy || (g_list_position(buddylist, current_buddy) == -1))
- current_buddy = buddylist;
+ current_buddy = g_list_first(buddylist);
}
// buddy_hide_group(roster, hide)
--- a/mcabber/src/screen.c Sat Apr 16 21:13:59 2005 +0000
+++ b/mcabber/src/screen.c Sat Apr 16 22:28:55 2005 +0000
@@ -8,14 +8,14 @@
#include <locale.h>
#include "screen.h"
-#include "utils.h"
+#include "hbuf.h"
#include "commands.h"
-#include "buddies.h"
+#include "roster.h"
#include "parsecfg.h"
#include "lang.h"
+#include "utf8.h"
+#include "utils.h"
#include "list.h"
-#include "utf8.h"
-#include "hbuf.h"
#define window_entry(n) list_entry(n, window_entry_t, list)
@@ -26,7 +26,6 @@
PANEL *panel;
char *name;
GList *hbuf;
- int hidden_msg;
struct list_head list;
} window_entry_t;
@@ -202,10 +201,6 @@
return tmp;
}
-void scr_RoolWindow(void)
-{
-}
-
window_entry_t *scr_SearchWindow(const char *winId)
{
struct list_head *pos, *n;
@@ -271,7 +266,7 @@
top_panel(win_entry->panel);
currentWindow = win_entry;
chatmode = TRUE;
- win_entry->hidden_msg = FALSE;
+ roster_setflags(winId, ROSTER_FLAG_MSG, FALSE);
update_roster = TRUE;
// Refresh the window entry
@@ -288,9 +283,9 @@
void scr_ShowBuddyWindow(void)
{
- buddy_entry_t *tmp = bud_SelectedInfo();
- if (tmp->jid != NULL)
- scr_ShowWindow(tmp->jid);
+ const gchar *jid = CURRENT_JID;
+ if (jid != NULL)
+ scr_ShowWindow(jid);
top_panel(inputPanel);
}
@@ -341,7 +336,7 @@
update_panels();
doupdate();
} else {
- win_entry->hidden_msg = TRUE;
+ roster_setflags(winId, ROSTER_FLAG_MSG, TRUE);
update_roster = TRUE;
}
}
@@ -366,6 +361,14 @@
return;
}
+void scr_TerminateCurses(void)
+{
+ clear();
+ refresh();
+ endwin();
+ return;
+}
+
void scr_DrawMainWindow(void)
{
int l;
@@ -397,18 +400,81 @@
inputWnd = newwin(1, maxX, maxY-1, 0);
inputPanel = new_panel(inputWnd);
- bud_DrawRoster(rosterWnd);
+ scr_DrawRoster();
update_panels();
doupdate();
return;
}
-void scr_TerminateCurses(void)
+void scr_DrawRoster(void)
{
- clear();
- refresh();
- endwin();
- return;
+ static guint offset = 0;
+ char name[ROSTER_WIDTH];
+ int maxx, maxy;
+ GList *buddy;
+ int i, n;
+ int rOffset;
+
+ getmaxyx(rosterWnd, maxy, maxx);
+ maxx --; // last char is for vertical border
+ name[ROSTER_WIDTH-7] = 0;
+
+ // cleanup of roster window
+ wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL));
+ for (i = 0; i < maxy; i++) {
+ mvwprintw(rosterWnd, i, 0, "");
+ for (n = 0; n < maxx; n++)
+ waddch(rosterWnd, ' ');
+ }
+
+ // Leave now if buddylist is empty
+ if (!buddylist) {
+ offset = 0;
+ return;
+ }
+
+ // TODO: update offset if necessary
+
+ buddy = buddylist;
+ rOffset = offset;
+
+ for (i=0; i<maxy && buddy; i++, buddy = g_list_next(buddy)) {
+
+ char status = '?';
+ char pending = ' ';
+ enum imstatus budstate;
+
+ if (rOffset > 0) {
+ rOffset--;
+ continue;
+ }
+
+ if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG) {
+ pending = '#';
+ }
+
+ budstate = buddy_getstatus(BUDDATA(buddy));
+ if (budstate >= 0 && budstate < imstatus_size)
+ status = imstatus2char[budstate];
+ if (buddy == current_buddy) {
+ wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL));
+ // The 3 following lines aim to color the whole line
+ wmove(rosterWnd, i, 0);
+ for (n = 0; n < maxx; n++)
+ waddch(rosterWnd, ' ');
+ } else {
+ wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES));
+ }
+
+ strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7);
+ // TODO: status is meaningless for groups:
+ if (buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP) status='G';
+ mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name);
+ }
+
+ update_panels();
+ doupdate();
+ update_roster = FALSE;
}
void scr_WriteMessage(const char *jid, const char *text, char *prefix)
@@ -457,6 +523,28 @@
return inputWnd;
}
+void scr_RosterUp()
+{
+ if (current_buddy) {
+ if (g_list_previous(current_buddy)) {
+ current_buddy = g_list_previous(current_buddy);
+ scr_DrawRoster();
+ }
+ }
+ // XXX We should rebuild the buddylist but perhaps not everytime?
+}
+
+void scr_RosterDown()
+{
+ if (current_buddy) {
+ if (g_list_next(current_buddy)) {
+ current_buddy = g_list_next(current_buddy);
+ scr_DrawRoster();
+ }
+ }
+ // XXX We should rebuild the buddylist but perhaps not everytime?
+}
+
// scr_LogPrint(...)
// Display a message in the log window.
void scr_LogPrint(const char *fmt, ...)
@@ -482,19 +570,6 @@
doupdate();
}
-// scr_IsHiddenMessage(jid)
-// Returns TRUE if there is a hidden message in the window
-// for the jid contact.
-int scr_IsHiddenMessage(const char *jid) {
- window_entry_t *wintmp;
-
- wintmp = scr_SearchWindow(jid);
- if ((wintmp) && (wintmp->hidden_msg))
- return TRUE;
-
- return FALSE;
-}
-
// check_offset(int direction)
// Check inputline_offset value, and make sure the cursor is inside the
// screen.
@@ -574,12 +649,12 @@
inputline_offset = 0;
break;
case KEY_UP:
- bud_RosterUp();
+ scr_RosterUp();
if (chatmode)
scr_ShowBuddyWindow();
break;
case KEY_DOWN:
- bud_RosterDown();
+ scr_RosterDown();
if (chatmode)
scr_ShowBuddyWindow();
break;
--- a/mcabber/src/screen.h Sat Apr 16 21:13:59 2005 +0000
+++ b/mcabber/src/screen.h Sat Apr 16 22:28:55 2005 +0000
@@ -20,17 +20,13 @@
void scr_InitCurses(void);
void scr_DrawMainWindow(void);
+void scr_DrawRoster(void);
void scr_TerminateCurses(void);
void scr_WriteIncomingMessage(const char *jidfrom, const char *text);
void scr_WriteOutgoingMessage(const char *jidto, const char *text);
-void scr_RoolWindow(void);
void scr_ShowBuddyWindow(void);
void scr_LogPrint(const char *fmt, ...);
-int scr_IsHiddenMessage(const char *jid);
-
-WINDOW *scr_GetRosterWindow(void);
-WINDOW *scr_GetStatusWindow(void);
WINDOW *scr_GetInputWindow(void);
int scr_Getch(void);
--- a/mcabber/src/utils.c Sat Apr 16 21:13:59 2005 +0000
+++ b/mcabber/src/utils.c Sat Apr 16 22:28:55 2005 +0000
@@ -4,7 +4,6 @@
#include <stdarg.h>
#include <time.h>
-/* Variables globales a UTILS.C */
static int DebugEnabled;
static char *FName;
--- a/mcabber/src/utils.h Sat Apr 16 21:13:59 2005 +0000
+++ b/mcabber/src/utils.h Sat Apr 16 22:28:55 2005 +0000
@@ -1,10 +1,7 @@
#ifndef __UTILS_H__
#define __UTILS_H__ 1
-#include <ncurses.h>
-
void ut_InitDebug(int level);
void ut_WriteLog(const char *fmt, ...);
-
#endif