gustav/source/Channel.d

297 lines
7.3 KiB
D
Raw Normal View History

2020-10-17 18:33:18 +00:00
/**
* Channel
*
* Represents a channel which is a collection
* of the channel name the users list widget,
* the title widget and the chat list box widget
* along with the input box state
*/
import gtk.Box;
import gtk.ListBox;
import gtk.Label;
import gtk.TextView;
import libdnet.dclient;
import gtk.Label;
2020-10-18 12:26:50 +00:00
import std.string;
2020-10-18 14:29:49 +00:00
import gtk.Button;
2020-10-20 06:04:11 +00:00
import gtk.Tooltip;
import gtk.Widget;
import gtk.ScrolledWindow;
import gtk.Button;
2020-10-17 18:33:18 +00:00
public final class Channel
{
private DClient client;
/**
* Channel details
*/
private string channelName;
/**
* The container for this Channel
*/
private Box box;
/**
* UI components
*
* Users's box
* - Label users
* - ListBox users
*/
private ListBox users;
private ListBox textArea;
private TextView textInput;
2020-10-18 12:26:50 +00:00
/* TODO: No mutexes should be needed (same precaution) as the GTK lock provides safety */
private string[] usersString;
this(DClient client, string channelName)
{
this.client = client;
this.channelName = channelName;
initializeBox();
}
private void initializeBox()
{
box = new Box(GtkOrientation.HORIZONTAL, 1);
/* The user's box */
Box userBox = new Box(GtkOrientation.VERTICAL, 1);
/* The user's list */
users = new ListBox();
userBox.add(new Label("Users"));
2020-10-18 14:29:49 +00:00
// import gtk.Expander;
// Expander g = new Expander("Bruh");
// g.setExpanded(true)
// g.add(users);
userBox.add(users);
/* The text box */
Box textBox = new Box(GtkOrientation.VERTICAL, 1);
textBox.add(new Label(channelName));
textArea = new ListBox();
2020-10-20 06:04:11 +00:00
ScrolledWindow scrollTextChats = new ScrolledWindow(textArea);
textBox.add(scrollTextChats);
textInput = new TextView();
2020-10-18 14:29:49 +00:00
Box textInputBox = new Box(GtkOrientation.HORIZONTAL, 1);
textInputBox.packStart(textInput,1,1,0);
2020-10-20 06:04:11 +00:00
2020-10-18 14:29:49 +00:00
/* The send button */
Button sendButton = new Button("Send");
sendButton.addOnClicked(&sendMessageBtn);
textInputBox.add(sendButton);
textBox.add(textInputBox);
// import gtk.TextView;
// TextView f = new TextView();
// textBox.add(f);
box.add(textBox);
box.packEnd(userBox,0,0,0);
textBox.setChildPacking(scrollTextChats, true, true, 0, GtkPackType.START);
box.setChildPacking(textBox, true, true, 0, GtkPackType.START);
}
2020-10-18 14:29:49 +00:00
private void sendMessageBtn(Button)
{
2020-10-18 14:31:21 +00:00
/* Retrieve the message */
string message = textInput.getBuffer().getText();
/* TODO: Add the message to our log (as it won't be delivered to us) */
2020-10-18 14:34:41 +00:00
addMessage(message);
2020-10-18 14:31:21 +00:00
/* Send the message */
client.sendMessage(0, channelName, message);
/* Clear the text box */
textInput.getBuffer().setText("");
2020-10-18 14:34:41 +00:00
box.showAll();
2020-10-18 14:29:49 +00:00
}
public Box getBox()
{
2020-10-20 16:59:10 +00:00
import std.stdio;
writeln("boz");
writeln("yolo: "~channelName);
return box;
}
2020-10-17 18:33:18 +00:00
public string getName()
{
return channelName;
}
/**
* Returns a Label with the tooltip event such
* that it will run that handler on hover
*/
private Label getUserLabel(string username)
{
2020-10-20 06:04:11 +00:00
/* Create a label */
Label userLabel = new Label(username);
/* Enable the tooltip */
userLabel.setHasTooltip(true);
/* Set the handler to run on hover */
2020-10-20 06:04:49 +00:00
userLabel.addOnQueryTooltip(&userLabelHoverHandler);
2020-10-20 06:04:11 +00:00
return userLabel;
}
2020-10-20 06:07:35 +00:00
/**
* Event handler to be run when you hover over a user's
* username in the Users sidebar list which will show
* the status as text (and in an icon format), the user's
* username and also their status message
*/
private bool userLabelHoverHandler(int, int, bool, Tooltip tooltip, Widget userLabel)
2020-10-18 16:01:03 +00:00
{
/* The username hovered over */
2020-10-20 09:26:49 +00:00
string userHover = (cast(Label)userLabel).getText();
2020-10-18 16:01:03 +00:00
/* Fetch the status message */
2020-10-18 19:09:34 +00:00
string[] statusMessage = split(client.getMemberInfo(userHover), ",");
/* First one is prescence */
string prescence = client.getProperty(userHover, "pres");//statusMessage[0];
2020-10-19 12:06:14 +00:00
/* Netx is status message */
string status = client.getProperty(userHover, "status");//statusMessage[1];
2020-10-20 06:06:16 +00:00
/* Set the icon */
tooltip.setIconFromIconName(statusToGtkIcon(prescence), GtkIconSize.DIALOG);
2020-10-18 16:01:03 +00:00
2020-10-20 06:06:16 +00:00
/* Set the text */
tooltip.setMarkup("<b>"~userHover~"</b>\n"~prescence~"\n<i>"~status~"</i>");
2020-10-18 16:01:03 +00:00
2020-10-20 06:06:16 +00:00
/* TODO: Point of return value? */
2020-10-18 16:01:03 +00:00
return 1;
}
2020-10-20 06:04:49 +00:00
public void populateUsersList()
{
string[] memberList = client.getMembers(channelName);
foreach(string member; memberList)
{
Label bruh = getUserLabel(member);
users.add(bruh);
usersString~=member;
}
}
private static string statusToGtkIcon(string status)
{
/* The GTK icon */
string gtkIcon = "image-missing";
if(cmp(status, "available") == 0)
{
gtkIcon = "user-available";
}
else if(cmp(status, "away") == 0)
{
gtkIcon = "user-away";
}
else if(cmp(status, "busy") == 0)
{
gtkIcon = "user-busy";
}
/* TODO: This doesn't make sense */
else if(cmp(status, "offline") == 0)
{
gtkIcon = "user-offline";
}
return gtkIcon;
}
2020-10-18 12:26:50 +00:00
public void channelJoin(string username)
{
/* The label to add */
/* TODO: Bababooey these `-->` being parsed as HTML for Pango */
2020-10-19 11:48:15 +00:00
Label joinLabel = new Label("--> <i>"~username~" joined the channel</i>");
joinLabel.setHalign(GtkAlign.START);
2020-10-19 11:48:15 +00:00
joinLabel.setUseMarkup(true);
2020-10-18 12:26:50 +00:00
/* Add join message to message log */
textArea.add(joinLabel);
2020-10-18 12:26:50 +00:00
/* Add user to user list */
users.add(getUserLabel(username));
2020-10-18 12:26:50 +00:00
usersString~=username;
}
public void channelLeave(string username)
{
/* The label to add */
/* TODO: Bababooey these `-->` being parsed as HTML for Pango */
2020-10-19 11:48:39 +00:00
Label leaveLabel = new Label("<-- <i>"~username~" left the channel</i>");
leaveLabel.setHalign(GtkAlign.START);
2020-10-19 11:48:39 +00:00
leaveLabel.setUseMarkup(true);
2020-10-18 12:26:50 +00:00
/* Add leave message to message log */
textArea.add(leaveLabel);
2020-10-18 12:26:50 +00:00
/* TODO: Better way with just removing one dude */
/* Remove the user form users list */
string[] newUsers;
foreach(string currentUser; usersString)
{
if(cmp(currentUser, username))
{
newUsers ~= currentUser;
}
}
2020-10-18 12:26:50 +00:00
usersString = newUsers;
/* Clear list */
users.removeAll();
foreach(string currentUser; usersString)
{
users.add(getUserLabel(currentUser));
2020-10-18 12:26:50 +00:00
}
/* Remove user from user list */
/* TODO: Do this better */
// foreach(Label label; users.get)
// users.add(new Label(username));
// users.showAll();
// box.showAll();+
}
2020-10-18 12:26:50 +00:00
public void addMessage(string s)
{
2020-10-18 14:34:41 +00:00
textArea.add(new Label(s));
}
2020-10-17 18:33:18 +00:00
}