Blame SOURCES/xchat-2.8.6-change-page-activity.patch

7a7ec5
diff -paur xchat2.orig/src/common/inbound.c xchat2/src/common/inbound.c
7a7ec5
--- xchat2.orig/src/common/inbound.c	2008-07-20 17:31:44.898468115 +0200
7a7ec5
+++ xchat2/src/common/inbound.c	2008-07-19 19:57:09.799906780 +0200
7a7ec5
@@ -297,7 +297,10 @@ is_hilight (char *from, char *text, sess
7a7ec5
 	{
7a7ec5
 		g_free (text);
7a7ec5
 		if (sess != current_tab)
7a7ec5
+		{
7a7ec5
 			sess->nick_said = TRUE;
7a7ec5
+			lastact_update(sess);
7a7ec5
+		}
7a7ec5
 		fe_set_hilight (sess);
7a7ec5
 		return 1;
7a7ec5
 	}
7a7ec5
@@ -344,6 +347,7 @@ inbound_action (session *sess, char *cha
7a7ec5
 			sess->msg_said = TRUE;
7a7ec5
 			sess->new_data = FALSE;
7a7ec5
 		}
7a7ec5
+		lastact_update(sess);
7a7ec5
 	}
7a7ec5
 
7a7ec5
 	user = userlist_find (sess, from);
7a7ec5
@@ -395,6 +399,7 @@ inbound_chanmsg (server *serv, session *
7a7ec5
 	{
7a7ec5
 		sess->msg_said = TRUE;
7a7ec5
 		sess->new_data = FALSE;
7a7ec5
+		lastact_update(sess);
7a7ec5
 	}
7a7ec5
 
7a7ec5
 	user = userlist_find (sess, from);
7a7ec5
diff -paur xchat2.orig/src/common/xchat.c xchat2/src/common/xchat.c
7a7ec5
--- xchat2.orig/src/common/xchat.c	2008-07-20 17:31:44.900468825 +0200
7a7ec5
+++ xchat2/src/common/xchat.c	2008-07-20 17:33:57.089468218 +0200
7a7ec5
@@ -71,6 +71,23 @@ GSList *usermenu_list = 0;
7a7ec5
 GSList *urlhandler_list = 0;
7a7ec5
 GSList *tabmenu_list = 0;
7a7ec5
 
7a7ec5
+/*
7a7ec5
+ * This array contains 5 double linked lists, one for each priority in the
7a7ec5
+ * "interesting session" queue ("channel" stands for everything but
7a7ec5
+ * SESS_DIALOG):
7a7ec5
+ *
7a7ec5
+ * [0] queries with hilight
7a7ec5
+ * [1] queries
7a7ec5
+ * [2] channels with hilight
7a7ec5
+ * [3] channels with dialogue
7a7ec5
+ * [4] channels with other data
7a7ec5
+ *
7a7ec5
+ * Each time activity happens the corresponding session is put at the
7a7ec5
+ * beginning of one of the lists.  The aim is to be able to switch to the
7a7ec5
+ * session with the most important/recent activity.
7a7ec5
+ */
7a7ec5
+GList *sess_list_by_lastact[5] = {NULL, NULL, NULL, NULL, NULL};
7a7ec5
+
7a7ec5
 static int in_xchat_exit = FALSE;
7a7ec5
 int xchat_is_quitting = FALSE;
7a7ec5
 /* command-line args */
7a7ec5
@@ -93,6 +110,105 @@ struct xchatprefs prefs;
7a7ec5
 SSL_CTX *ctx = NULL;
7a7ec5
 #endif
7a7ec5
 
7a7ec5
+/*
7a7ec5
+ * Update the priority queue of the "interesting sessions"
7a7ec5
+ * (sess_list_by_lastact).
7a7ec5
+ */
7a7ec5
+void
7a7ec5
+lastact_update(session *sess)
7a7ec5
+{
7a7ec5
+	int newidx;
7a7ec5
+
7a7ec5
+	/* Find the priority (for the order see before) */
7a7ec5
+	if (sess->type == SESS_DIALOG)
7a7ec5
+	{
7a7ec5
+		if (sess->nick_said)
7a7ec5
+			newidx = LACT_QUERY_HI;
7a7ec5
+		else if (sess->msg_said)
7a7ec5
+			newidx = LACT_QUERY;
7a7ec5
+		else if (sess->new_data)
7a7ec5
+			newidx = LACT_QUERY;
7a7ec5
+		else
7a7ec5
+			newidx = LACT_NONE;
7a7ec5
+	}
7a7ec5
+	else
7a7ec5
+	{
7a7ec5
+		if (sess->nick_said)
7a7ec5
+			newidx = LACT_CHAN_HI;
7a7ec5
+		else if (sess->msg_said)
7a7ec5
+			newidx = LACT_CHAN;
7a7ec5
+		else if (sess->new_data)
7a7ec5
+			newidx = LACT_CHAN_DATA;
7a7ec5
+		else
7a7ec5
+			newidx = LACT_NONE;
7a7ec5
+	}
7a7ec5
+
7a7ec5
+	/* Check if this update is a no-op */
7a7ec5
+	if (sess->lastact_idx == newidx && 
7a7ec5
+			((newidx != LACT_NONE && sess->lastact_elem == sess_list_by_lastact[newidx]) ||
7a7ec5
+			 (newidx == LACT_NONE)))
7a7ec5
+		return;
7a7ec5
+
7a7ec5
+	/* Remove from the old position (and, if no new position, return */
7a7ec5
+	else if (sess->lastact_idx != LACT_NONE && sess->lastact_elem)
7a7ec5
+	{
7a7ec5
+		sess_list_by_lastact[sess->lastact_idx] = g_list_remove_link(
7a7ec5
+				sess_list_by_lastact[sess->lastact_idx],
7a7ec5
+				sess->lastact_elem);
7a7ec5
+		if (newidx == LACT_NONE)
7a7ec5
+		{
7a7ec5
+			sess->lastact_idx = newidx;
7a7ec5
+			return;
7a7ec5
+		}
7a7ec5
+	}
7a7ec5
+
7a7ec5
+	/* No previous position, allocate new */
7a7ec5
+	else if (!sess->lastact_elem)
7a7ec5
+		sess->lastact_elem = g_list_prepend(sess->lastact_elem, sess);
7a7ec5
+
7a7ec5
+	sess->lastact_idx = newidx;
7a7ec5
+	sess_list_by_lastact[newidx] = g_list_concat(
7a7ec5
+			sess->lastact_elem, sess_list_by_lastact[newidx]);
7a7ec5
+}
7a7ec5
+
7a7ec5
+/*
7a7ec5
+ * Extract the first session from the priority queue of sessions with recent
7a7ec5
+ * activity. Return NULL if no such session can be found.
7a7ec5
+ *
7a7ec5
+ * If filter is specified, skip a session if filter(session) returns 0. This
7a7ec5
+ * can be used for UI-specific needs, e.g. in fe-gtk we want to filter out
7a7ec5
+ * detached sessions.
7a7ec5
+ */
7a7ec5
+session *
7a7ec5
+lastact_getfirst(int (*filter) (session *sess))
7a7ec5
+{
7a7ec5
+	int i;
7a7ec5
+	session *sess = NULL;
7a7ec5
+	GList *curitem;
7a7ec5
+
7a7ec5
+	/* 5 is the number of priority classes LACT_ */
7a7ec5
+	for (i = 0; i < 5 && !sess; i++)
7a7ec5
+	{
7a7ec5
+		curitem = sess_list_by_lastact[i];
7a7ec5
+		while (curitem && !sess)
7a7ec5
+		{
7a7ec5
+			sess = g_list_nth_data(curitem, 0);
7a7ec5
+			if (!sess || (filter && !filter(sess)))
7a7ec5
+			{
7a7ec5
+				sess = NULL;
7a7ec5
+				curitem = g_list_next(curitem);
7a7ec5
+			}
7a7ec5
+		}
7a7ec5
+
7a7ec5
+		if (sess)
7a7ec5
+		{
7a7ec5
+			sess_list_by_lastact[i] = g_list_remove_link(sess_list_by_lastact[i], curitem);
7a7ec5
+			sess->lastact_idx = LACT_NONE;
7a7ec5
+		}
7a7ec5
+	}
7a7ec5
+	
7a7ec5
+	return sess;
7a7ec5
+}
7a7ec5
 
7a7ec5
 int
7a7ec5
 is_session (session * sess)
7a7ec5
@@ -362,6 +478,9 @@ session_new (server *serv, char *from, i
7a7ec5
 
7a7ec5
 	sess_list = g_slist_prepend (sess_list, sess);
7a7ec5
 
7a7ec5
+	sess->lastact_elem = NULL;
7a7ec5
+	sess->lastact_idx = LACT_NONE;
7a7ec5
+
7a7ec5
 	fe_new_window (sess, focus);
7a7ec5
 
7a7ec5
 	return sess;
7a7ec5
@@ -533,6 +652,16 @@ session_free (session *killsess)
7a7ec5
 			current_sess = sess_list->data;
7a7ec5
 	}
7a7ec5
 
7a7ec5
+	if (killsess->lastact_elem)
7a7ec5
+	{
7a7ec5
+		if (killsess->lastact_idx != LACT_NONE)
7a7ec5
+			sess_list_by_lastact[killsess->lastact_idx] = g_list_delete_link(
7a7ec5
+					sess_list_by_lastact[killsess->lastact_idx],
7a7ec5
+					killsess->lastact_elem);
7a7ec5
+		else
7a7ec5
+			g_list_free_1(killsess->lastact_elem);
7a7ec5
+	}
7a7ec5
+
7a7ec5
 	free (killsess);
7a7ec5
 
7a7ec5
 	if (!sess_list && !in_xchat_exit)
7a7ec5
diff -paur xchat2.orig/src/common/xchat.h xchat2/src/common/xchat.h
7a7ec5
--- xchat2.orig/src/common/xchat.h	2008-07-20 17:31:44.901467675 +0200
7a7ec5
+++ xchat2/src/common/xchat.h	2008-07-20 17:33:28.240467970 +0200
7a7ec5
@@ -320,6 +320,15 @@ struct xchatprefs
7a7ec5
 #define SET_ON 1
7a7ec5
 #define SET_DEFAULT 2 /* use global setting */
7a7ec5
 
7a7ec5
+/* Priorities in the "interesting sessions" priority queue
7a7ec5
+ * (see xchat.c:sess_list_by_lastact) */
7a7ec5
+#define LACT_NONE		-1		/* no queues */
7a7ec5
+#define LACT_QUERY_HI	0		/* query with hilight */
7a7ec5
+#define LACT_QUERY		1		/* query with messages */
7a7ec5
+#define LACT_CHAN_HI	2		/* channel with hilight */
7a7ec5
+#define LACT_CHAN		3		/* channel with messages */
7a7ec5
+#define LACT_CHAN_DATA	4		/* channel with other data */
7a7ec5
+
7a7ec5
 typedef struct session
7a7ec5
 {
7a7ec5
 	/* Per-Channel Alerts */
7a7ec5
@@ -369,6 +378,10 @@ typedef struct session
7a7ec5
 
7a7ec5
 	int type;					/* SESS_* */
7a7ec5
 
7a7ec5
+	GList *lastact_elem;	/* our GList element in sess_list_by_lastact */
7a7ec5
+	int lastact_idx;		/* the sess_list_by_lastact[] index of the list we're in.
7a7ec5
+							 * For valid values, see defines of LACT_*. */
7a7ec5
+
7a7ec5
 	int new_data:1;			/* new data avail? (purple tab) */
7a7ec5
 	int nick_said:1;		/* your nick mentioned? (blue tab) */
7a7ec5
 	int msg_said:1;			/* new msg available? (red tab) */
7a7ec5
diff -paur xchat2.orig/src/common/xchatc.h xchat2/src/common/xchatc.h
7a7ec5
--- xchat2.orig/src/common/xchatc.h	2008-07-20 17:31:44.901467675 +0200
7a7ec5
+++ xchat2/src/common/xchatc.h	2008-07-20 11:43:36.673967630 +0200
7a7ec5
@@ -25,10 +25,13 @@ extern GSList *ignore_list;
7a7ec5
 extern GSList *usermenu_list;
7a7ec5
 extern GSList *urlhandler_list;
7a7ec5
 extern GSList *tabmenu_list;
7a7ec5
+extern GList *sess_list_by_lastact[];
7a7ec5
 
7a7ec5
 session * find_channel (server *serv, char *chan);
7a7ec5
 session * find_dialog (server *serv, char *nick);
7a7ec5
 session * new_ircwindow (server *serv, char *name, int type, int focus);
7a7ec5
+void lastact_update (session * sess);
7a7ec5
+session * lastact_getfirst (int (*filter) (session *sess));
7a7ec5
 int is_session (session * sess);
7a7ec5
 void session_free (session *killsess);
7a7ec5
 void lag_check (void);
7a7ec5
diff -paur xchat2.orig/src/fe-gtk/fe-gtk.c xchat2/src/fe-gtk/fe-gtk.c
7a7ec5
--- xchat2.orig/src/fe-gtk/fe-gtk.c	2008-07-20 17:31:44.958466232 +0200
7a7ec5
+++ xchat2/src/fe-gtk/fe-gtk.c	2008-07-19 19:58:57.431961788 +0200
7a7ec5
@@ -603,6 +603,7 @@ fe_print_text (struct session *sess, cha
7a7ec5
 		 sess->gui->is_tab && !sess->nick_said && stamp == 0)
7a7ec5
 	{
7a7ec5
 		sess->new_data = TRUE;
7a7ec5
+		lastact_update(sess);
7a7ec5
 		if (sess->msg_said)
7a7ec5
 			fe_set_tab_color (sess, 2);
7a7ec5
 		else
7a7ec5
diff -paur xchat2.orig/src/fe-gtk/fkeys.c xchat2/src/fe-gtk/fkeys.c
7a7ec5
--- xchat2.orig/src/fe-gtk/fkeys.c	2008-07-20 17:31:44.960465847 +0200
7a7ec5
+++ xchat2/src/fe-gtk/fkeys.c	2008-07-20 12:20:50.186930065 +0200
7a7ec5
@@ -158,7 +158,7 @@ static const struct key_action key_actio
7a7ec5
 	{key_action_handle_command, "Run Command",
7a7ec5
 	 N_("The \002Run Command\002 action runs the data in Data 1 as if it has been typed into the entry box where you pressed the key sequence. Thus it can contain text (which will be sent to the channel/person), commands or user commands. When run all \002\\n\002 characters in Data 1 are used to deliminate seperate commands so it is possible to run more than one command. If you want a \002\\\002 in the actual text run then enter \002\\\\\002")},
7a7ec5
 	{key_action_page_switch, "Change Page",
7a7ec5
-	 N_("The \002Change Page\002 command switches between pages in the notebook. Set Data 1 to the page you want to switch to. If Data 2 is set to anything then the switch will be relative to the current position")},
7a7ec5
+	 N_("The \002Change Page\002 command switches between pages in the notebook. Set Data 1 to the page you want to switch to. If Data 2 is set to anything then the switch will be relative to the current position. Set Data 1 to auto to switch to the page with the most recent and important activity (queries first, then channels with hilight, channels with dialogue, channels with other data)")},
7a7ec5
 	{key_action_insert, "Insert in Buffer",
7a7ec5
 	 N_("The \002Insert in Buffer\002 command will insert the contents of Data 1 into the entry where the key sequence was pressed at the current cursor position")},
7a7ec5
 	{key_action_scroll_page, "Scroll Page",
7a7ec5
@@ -402,6 +402,7 @@ key_load_defaults ()
7a7ec5
 		"A\n3\nChange Page\nD1:3\nD2!\n\n"\
7a7ec5
 		"A\n2\nChange Page\nD1:2\nD2!\n\n"\
7a7ec5
 		"A\n1\nChange Page\nD1:1\nD2!\n\n"\
7a7ec5
+		"A\ngrave\nChange Page\nD1:auto\nD2!\n\n"\
7a7ec5
 		"C\no\nInsert in Buffer\nD1:?\nD2!\n\n"\
7a7ec5
 		"C\nb\nInsert in Buffer\nD1:?\nD2!\n\n"\
7a7ec5
 		"C\nk\nInsert in Buffer\nD1:?\nD2!\n\n"\
7a7ec5
@@ -1196,6 +1197,20 @@ key_action_handle_command (GtkWidget * w
7a7ec5
 	return 0;
7a7ec5
 }
7a7ec5
 
7a7ec5
+/*
7a7ec5
+ * Check if the given session is inside the main window. This predicate
7a7ec5
+ * is passed to lastact_pop as a way to filter out detached sessions.
7a7ec5
+ * XXX: Consider moving this in a different file?
7a7ec5
+ */
7a7ec5
+static int
7a7ec5
+session_check_is_tab(session *sess)
7a7ec5
+{
7a7ec5
+	if (!sess || !sess->gui)
7a7ec5
+		return FALSE;
7a7ec5
+
7a7ec5
+	return (sess->gui->is_tab);
7a7ec5
+}
7a7ec5
+
7a7ec5
 static int
7a7ec5
 key_action_page_switch (GtkWidget * wid, GdkEventKey * evt, char *d1,
7a7ec5
 								char *d2, struct session *sess)
7a7ec5
@@ -1209,6 +1224,30 @@ key_action_page_switch (GtkWidget * wid,
7a7ec5
 	if (!len)
7a7ec5
 		return 1;
7a7ec5
 
7a7ec5
+	if (strcasecmp(d1, "auto") == 0)
7a7ec5
+	{
7a7ec5
+		/* Auto switch makes no sense in detached sessions */
7a7ec5
+		if (!sess->gui->is_tab)
7a7ec5
+			return 1;
7a7ec5
+
7a7ec5
+		/* Obtain a session with recent activity */
7a7ec5
+		session *newsess = lastact_getfirst(session_check_is_tab);
7a7ec5
+
7a7ec5
+		if (newsess)
7a7ec5
+		{
7a7ec5
+			/*
7a7ec5
+			 * Only sessions in the current window should be considered (i.e.
7a7ec5
+			 * we don't want to move the focus on a different window). This
7a7ec5
+			 * call could, in theory, do this, but we checked before that
7a7ec5
+			 * newsess->gui->is_tab and sess->gui->is_tab.
7a7ec5
+			 */
7a7ec5
+			mg_bring_tofront_sess(newsess);
7a7ec5
+			return 0;
7a7ec5
+		}
7a7ec5
+		else
7a7ec5
+			return 1;
7a7ec5
+	}
7a7ec5
+
7a7ec5
 	for (i = 0; i < len; i++)
7a7ec5
 	{
7a7ec5
 		if (d1[i] < '0' || d1[i] > '9')
7a7ec5
diff -paur xchat2.orig/src/fe-gtk/maingui.c xchat2/src/fe-gtk/maingui.c
7a7ec5
--- xchat2.orig/src/fe-gtk/maingui.c	2008-07-20 17:31:44.964469466 +0200
7a7ec5
+++ xchat2/src/fe-gtk/maingui.c	2008-07-19 19:58:58.255909127 +0200
7a7ec5
@@ -359,6 +359,7 @@ fe_set_tab_color (struct session *sess, 
7a7ec5
 				
7a7ec5
 			break;
7a7ec5
 		}
7a7ec5
+		lastact_update(sess);
7a7ec5
 	}
7a7ec5
 }
7a7ec5
 
7a7ec5
@@ -643,6 +644,7 @@ mg_focus (session *sess)
7a7ec5
 		sess->nick_said = FALSE;
7a7ec5
 		sess->msg_said = FALSE;
7a7ec5
 		sess->new_data = FALSE;
7a7ec5
+		lastact_update(sess);
7a7ec5
 		/* when called via mg_changui_new, is_tab might be true, but
7a7ec5
 			sess->res->tab is still NULL. */
7a7ec5
 		if (sess->res->tab)