gustav/source/Channel.d

275 lines
6.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-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();
import gtk.ScrolledWindow;
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);
import gtk.Button;
/* 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()
{
return box;
}
2020-10-17 18:33:18 +00:00
public string getName()
{
return channelName;
}
public void populateUsersList()
{
string[] memberList = client.getMembers(channelName);
foreach(string member; memberList)
{
2020-10-18 16:01:03 +00:00
Label bruh = new Label(member);
bruh.setHasTooltip(true);
// import gtk.Window;
// Window k = new Window(GtkWindowType.POPUP);
// bruh.setTooltipWindow(k);
bruh.addOnQueryTooltip(&kak);
users.add(bruh);
2020-10-18 12:26:50 +00:00
usersString~=member;
}
}
2020-10-18 16:01:03 +00:00
import gtk.Tooltip;
import gtk.Widget;
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, "idle") == 0)
{
gtkIcon = "user-idle";
}
/* TODO: This doesn't make sense */
else if(cmp(status, "offline") == 0)
{
gtkIcon = "user-offline";
}
return gtkIcon;
}
2020-10-18 16:01:03 +00:00
private bool kak(int,int,bool, Tooltip d, Widget poes)
{
import std.stdio;
writeln("ttoltip activatd");
2020-10-18 16:01:03 +00:00
/* The username hovered over */
string userHover = (cast(Label)poes).getText();
/* Fetch the status message */
2020-10-18 19:09:34 +00:00
string[] statusMessage = split(client.getMemberInfo(userHover), ",");
/* First one is prescence */
string prescence = statusMessage[0];
d.setIconFromIconName(statusToGtkIcon(prescence), GtkIconSize.DIALOG);
2020-10-18 16:01:03 +00:00
d.setText(userHover~"\n"~prescence);
2020-10-18 16:01:03 +00:00
// /* The notification box */
// Box notificationBox = new Box(GtkOrientation.VERTICAL, 1);
// Label title = new Label((cast(Label)poes).getText());
// Label status = new Label("status goes here");
// notificationBox.add(title);
// notificationBox.add(status);
// import gtk.Style;
// // title.setStyle(new Style());
// d.setCustom(notificationBox);
return 1;
}
2020-10-18 12:26:50 +00:00
public void channelJoin(string username)
{
/* Add join message to message log */
textArea.add(new Label("--> "~username~" joined the channel"));
/* Add user to user list */
users.add(new Label(username));
usersString~=username;
}
2020-10-18 14:29:49 +00:00
2020-10-18 12:26:50 +00:00
public void channelLeave(string username)
{
/* Add leave message to message log */
textArea.add(new Label("<-- "~username~" left the channel"));
/* 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(new Label(currentUser));
}
/* Remove user from user list */
/* TODO: Do this better */
// foreach(Label label; users.get)
// users.add(new Label(username));
}
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
}