gustav/source/Channel.d

405 lines
11 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-21 20:29:23 +00:00
import gtk.Entry;
2020-10-23 06:53:33 +00:00
import pango.PgAttributeList;
import pango.PgAttribute;
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;
2020-10-21 20:29:23 +00:00
private Entry 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);
/* Channel title */
Label channelTitleLabel = new Label(channelName);
channelTitleLabel.setMarkup("<span size=\"large\"><b>"~channelName~"</b></span>");
textBox.add(channelTitleLabel);
2020-10-23 06:53:33 +00:00
/* The messages box */
textArea = new ListBox();
ScrolledWindow scrollTextChats = new ScrolledWindow(textArea);
textBox.add(scrollTextChats);
2020-10-23 06:53:33 +00:00
/* The text input */
2020-10-21 20:29:23 +00:00
textInput = new Entry();
textInput.addOnActivate(&sendMessageEnter);
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-21 20:29:23 +00:00
private void sendMessageEnter(Entry)
{
/* Retrieve the message */
string message = textInput.getBuffer().getText();
/* TODO: Add the message to our log (as it won't be delivered to us) */
sendMessage(message);
/* Send the message */
client.sendMessage(0, channelName, message);
/* Clear the text box */
textInput.getBuffer().setText("",0);
box.showAll();
}
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-20 22:27:03 +00:00
sendMessage(message);
2020-10-18 14:31:21 +00:00
/* Send the message */
client.sendMessage(0, channelName, message);
/* Clear the text box */
2020-10-21 20:29:23 +00:00
textInput.getBuffer().setText("",0);
2020-10-18 14:34:41 +00:00
box.showAll();
2020-10-18 14:29:49 +00:00
}
public Box getBox()
{
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-23 12:02:07 +00:00
2020-10-23 12:20:33 +00:00
private Box getUserListItem(string username)
{
/* This is an item for a username in this Channel's user list */
Box box = new Box(GtkOrientation.HORIZONTAL, 1);
import gtk.IconView;
IconView icon = new IconView();
import gtk.StatusIcon;
StatusIcon d = new StatusIcon("user-available");
return box;
}
2020-10-23 12:02:07 +00:00
// private bool userLabelPopup(Widget)
// {
// import std.stdio;
// writeln("NOWNOWNOWNOWNOW");
// return true;
// }
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
2020-10-20 19:11:24 +00:00
/* The final tooltip */
string toolTipText = "<b>"~userHover~"</b>";
2020-10-20 19:11:24 +00:00
/* Check if there is a `precensce` message */
if(client.isProperty(userHover, "pres"))
{
/* Fetch the precensce */
string prescence = client.getProperty(userHover, "pres");
/* Set the icon */
tooltip.setIconFromIconName(statusToGtkIcon(prescence), GtkIconSize.DIALOG);
/* Append the precesnee to the tooltip text */
toolTipText ~= "\n"~prescence;
}
/* Check if there is a `status` message */
if(client.isProperty(userHover, "status"))
{
/* Next is status message */
string status = client.getProperty(userHover, "status");
2020-10-20 19:11:24 +00:00
/* Append the status to the tooltip text */
toolTipText ~= "\n<i>"~status~"</i>";
}
2020-10-18 16:01:03 +00:00
2020-10-20 19:11:24 +00:00
/* Set the tooltip text */
tooltip.setMarkup(toolTipText);
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 */
2020-10-23 06:56:52 +00:00
Label joinLabel = new Label("--> "~username~" joined the channel");
joinLabel.setHalign(GtkAlign.START);
2020-10-23 06:56:52 +00:00
PgAttributeList joinLabelAttrs = new PgAttributeList();
PgAttribute joinLabelAttr = PgAttribute.styleNew(PangoStyle.ITALIC);
joinLabelAttrs.insert(joinLabelAttr);
joinLabel.setAttributes(joinLabelAttrs);
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 */
2020-10-23 06:53:33 +00:00
Label leaveLabel = new Label("<-- "~username~" left the channel");
leaveLabel.setHalign(GtkAlign.START);
2020-10-23 06:53:33 +00:00
PgAttributeList leaveLabelAttrs = new PgAttributeList();
PgAttribute leaveLabelAttr = PgAttribute.styleNew(PangoStyle.ITALIC);
leaveLabelAttrs.insert(leaveLabelAttr);
leaveLabel.setAttributes(leaveLabelAttrs);
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();+
}
public void sendMessage(string message)
{
2020-10-20 22:27:03 +00:00
/* TOOD: Pass in connection perhaps */
string username = "Yourself";
2020-10-20 22:27:03 +00:00
/* Create the MessageBox */
Box messageBox = new Box(GtkOrientation.VERTICAL, 1);
/* Create and add the username */
Label usernameLabel = new Label("");
usernameLabel.setMarkup("<b>"~username~"</b>");
usernameLabel.setHalign(GtkAlign.END);
messageBox.add(usernameLabel);
/* Create and add the message */
Label messageLabel = new Label(message);
messageLabel.setHalign(GtkAlign.END);
messageLabel.setSelectable(true);
2020-10-20 22:27:03 +00:00
messageBox.add(messageLabel);
2020-10-21 05:53:47 +00:00
/* Add the message to the log */
2020-10-20 22:27:03 +00:00
textArea.add(messageBox);
}
public void receiveMessage(string username, string message)
{
/* Create the MessageBox */
Box messageBox = new Box(GtkOrientation.VERTICAL, 1);
/* Create and add the username */
Label usernameLabel = new Label("");
usernameLabel.setMarkup("<b>"~username~"</b>");
usernameLabel.setHalign(GtkAlign.START);
messageBox.add(usernameLabel);
/* Create and add the message */
Label messageLabel = new Label(message);
messageLabel.setHalign(GtkAlign.START);
messageLabel.setSelectable(true);
messageBox.add(messageLabel);
2020-10-21 05:53:47 +00:00
// import gtk.Image;
// Image d = new Image("/home/deavmi/Downloads/5207740.jpg");
// messageBox.add(d);
2020-10-20 22:27:03 +00:00
2020-10-21 05:53:47 +00:00
/* Add the message to the log */
textArea.add(messageBox);
}
2020-10-17 18:33:18 +00:00
}