gustav/source/Channel.d

327 lines
8.4 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-25 21:58:19 +00:00
import UserNode;
2020-10-23 06:53:33 +00:00
import pango.PgAttributeList;
import pango.PgAttribute;
2020-10-25 21:43:43 +00:00
import Connection;
2020-10-23 06:53:33 +00:00
2020-10-17 18:33:18 +00:00
public final class Channel
{
private DClient client;
2020-10-25 21:43:43 +00:00
private Connection connection;
/**
* 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;
2020-10-25 21:43:43 +00:00
this(Connection connection, string channelName)
{
2020-10-25 21:43:43 +00:00
this.client = connection.getClient();
this.connection = connection;
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;
}
2020-10-25 21:58:19 +00:00
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-25 21:58:19 +00:00
2020-10-18 16:01:03 +00:00
2020-10-20 06:04:49 +00:00
public void populateUsersList()
{
string[] memberList = client.getMembers(channelName);
foreach(string member; memberList)
{
2020-10-25 21:58:19 +00:00
/* Create the user entry in the list */
UserNode userNode = new UserNode(connection, member);
users.add(userNode.getBox());
2020-10-25 21:43:43 +00:00
2020-10-25 21:58:19 +00:00
/* Add the user to the tracking list */
usersString~=member;
2020-10-20 06:04:49 +00:00
}
}
2020-10-25 21:58:19 +00:00
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
2020-10-25 21:58:19 +00:00
/* Create the user entry in the list */
UserNode userNode = new UserNode(connection, username);
users.add(userNode.getBox());
2020-10-18 12:26:50 +00:00
2020-10-25 21:58:19 +00:00
/* Add the user to the tracking list */
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)
{
2020-10-25 21:58:19 +00:00
/* Create the user entry in the list */
UserNode userNode = new UserNode(connection, currentUser);
users.add(userNode.getBox());
2020-10-18 12:26:50 +00:00
}
}
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
}