gustav/source/Connection.d

371 lines
8.9 KiB
D
Raw Normal View History

2020-10-17 11:54:07 +00:00
module Connection;
import core.thread;
import gui;
import gdk.Threads : te = threadsEnter, tl = threadsLeave;
import gtk.Box;
import std.stdio;
import libdnet.dclient;
import std.socket;
import gtk.ListBox;
import gtk.Label;
2020-10-17 19:05:21 +00:00
import Channel;
import std.string;
2020-10-17 19:05:21 +00:00
import core.sync.mutex;
2020-10-17 11:54:07 +00:00
public final class Connection : Thread
{
private GUI gui;
private Box box;
private ListBox channels;
private ListBox users;
private ListBox textArea;
private DClient client;
private Address address;
private string[] auth;
2020-10-17 11:54:07 +00:00
2020-10-17 12:26:17 +00:00
/* TODO: Check if we need to protect */
/* TODO: So far usage is in signal handlers (mutex safved) and within te-tl lock for notifications */
2020-10-17 18:26:23 +00:00
private string currentChannel; /* TODO: Used to track what notificaitons come throug */
private Label currentChannelLabel; /* TODO: Using getChild would be nicer, but yeah, this is for the title */
2020-10-17 12:26:17 +00:00
2020-10-17 19:05:21 +00:00
/**
* All joined Channel-s in this Connection
*/
private Channel[] chans; /*TODO: Technically locking by GTK would make this not needed */
private Mutex chansLock;
private Channel focusedChan;
2020-10-17 19:05:21 +00:00
this(GUI gui, Address address, string[] auth)
2020-10-17 11:54:07 +00:00
{
super(&worker);
this.gui = gui;
this.address = address;
this.auth = auth;
/* Initialize locks */
initializeLocks();
/* Start the notification atcher */
2020-10-17 11:54:07 +00:00
start();
}
/**
* Initializes all locks (other than GDK)
*/
private void initializeLocks()
{
chansLock = new Mutex();
}
2020-10-17 11:54:07 +00:00
private void worker()
{
2020-10-17 18:26:23 +00:00
/* Create a new Label */
currentChannelLabel = new Label("CHANNEL NAME GOES HERE");
2020-10-17 12:32:26 +00:00
/**
* Setup the tab for this connection
*/
2020-10-17 11:54:07 +00:00
te();
box = getChatPane();
gui.notebook.add(box);
2020-10-17 18:26:23 +00:00
//gui.notebook setChildPacking(box, true, true, 0, GtkPackType.START);
// gui.mainWindow.
gui.notebook.setTabLabelText(box, auth[0]~"@"~address.toString());
2020-10-17 12:29:41 +00:00
gui.notebook.showAll();
2020-10-17 11:54:07 +00:00
tl();
2020-10-17 12:32:26 +00:00
/**
* Connects and logs in
*/
2020-10-17 11:54:07 +00:00
client = new DClient(address);
client.auth(auth[0], auth[1]); /* TODO: DO this without auth (the list in the loop, crahses server) */
2020-10-17 11:54:07 +00:00
2020-10-17 12:32:26 +00:00
/* Display all channels */
2020-10-17 11:54:07 +00:00
channelList();
/**
* Notification loop
*
* Awaits notifications and then displays them
*/
while(true)
{
/* Receive a notification */
byte[] notificationData = client.awaitNotification();
writeln(notificationData);
te();
import std.conv;
textArea.add(new Label(to!(string)(notificationData)));
2020-10-17 12:26:17 +00:00
textArea.showAll();
2020-10-17 11:54:07 +00:00
process(notificationData);
2020-10-17 12:10:04 +00:00
//gui.mainWindow.showAll();
2020-10-17 11:54:07 +00:00
tl();
//Thread.sleep(dur!("seconds")(2));
}
}
/**
* Processes an incoming notification
* accordingly
*/
private void process(byte[] data)
{
/* TODO: Implement me */
/* TODO: Check notification type */
ubyte notificationType = data[0];
/* For normal message (to channel or user) */
if(notificationType == 0)
{
/* TODO: Decode using tristanable */
writeln("new message");
}
/* Channel notification (ntype=1) */
else if(notificationType == 1)
{
/* TODO: Decode using tristanable */
/* TODO: Get the username of the user that left */
//writeln("user left/join message");
/* Get the sub-type */
ubyte subType = data[1];
/* If the notification was leave (stype=0) */
if(subType == 0)
{
string username = cast(string)data[2..data.length];
// textArea.add(new Label(("<-- "~username~" left the channel")));
// textArea.showAll();
2020-10-17 11:54:07 +00:00
}
/* If the notification was join (stype=1) */
else if(subType == 1)
{
string username = cast(string)data[2..data.length];
2020-10-17 12:15:48 +00:00
/* Show joined message */
// textArea.add(new Label(("--> "~username~" joined the channel")));
// textArea.showAll();
2020-10-17 12:15:48 +00:00
// /* Add the joined user to the members list */
// users.add(new Label(username));
// users.showAll();
2020-10-17 11:54:07 +00:00
}
/* TODO: Unknown */
else
{
}
}
}
private void channelList()
{
te();
channelList_unsafe();
tl();
}
/**
* Lists all channels and displays them
*
* Only to be aclled when locked (i.e. by the event
* loop signal dispatch or when we lock it
* i.e. `channelList`)
*/
private void channelList_unsafe()
{
string[] channelList = client.list();
foreach(string channel; channelList)
{
channels.add(new Label(channel));
2020-10-17 12:10:04 +00:00
channels.showAll();
2020-10-17 11:54:07 +00:00
}
}
private Channel findChannel(string channelName)
{
Channel result;
chansLock.lock();
foreach(Channel channel; chans)
{
if(cmp(channel.getName(), channelName))
{
result = channel;
break;
}
}
chansLock.unlock();
return result;
}
private void addChannel(Channel newChannel)
{
chansLock.lock();
chans ~= newChannel;
chansLock.unlock();
}
2020-10-17 11:54:07 +00:00
private void selectChannel(ListBox s)
{
2020-10-17 12:06:38 +00:00
/* Get the name of the channel selected */
string channelSelected = (cast(Label)(s.getSelectedRow().getChild())).getText();
/* Check if we have joined this channel already */
Channel foundChannel = findChannel(channelSelected);
/* If we have joined this channel before */
if(foundChannel)
{
/* TODO: Switch to */
}
/* If we haven't joined this channel before */
else
{
/* Join the channel */
client.join(channelSelected);
2020-10-17 12:06:38 +00:00
/* Create the Channel object */
Channel newChannel = new Channel(channelSelected);
2020-10-17 12:26:17 +00:00
/* Add the channel */
addChannel(newChannel);
2020-10-17 12:06:38 +00:00
/* Set as the `foundChannel` */
foundChannel = newChannel;
2020-10-17 12:06:38 +00:00
}
2020-10-17 17:32:31 +00:00
/* TODO: Now add the widget */
// /* Set this as the currently selected channel */
// currentChannel = channelSelected;
// currentChannelLabel.setText(currentChannel);
// // currentChannelLabel.show();
// // box.show();
// /* Fetch a list of members */
// string[] members = client.getMembers(channelSelected);
// /* Display the members */
// users.removeAll();
// foreach(string member; members)
// {
// users.add(new Label(member));
// users.showAll();
// }
// /* Clear the text area */
// textArea.removeAll();
// textArea.showAll();
2020-10-17 11:54:07 +00:00
}
/**
* Creates a message box
*
* A message box consists of two labels
* one being the name of the person who sent
* the message and the next being the message
* itself
*/
private Box createMessageBox()
{
return null;
}
private Box getChatPane()
{
/* The main page of the tab */
Box box = new Box(GtkOrientation.HORIZONTAL, 1);
/* The channels box */
Box channelBox = new Box(GtkOrientation.VERTICAL, 1);
/* The channel's list */
channels = new ListBox();
channels.addOnSelectedRowsChanged(&selectChannel);
channelBox.add(new Label("Channels"));
channelBox.add(channels);
// /* The user's box */
// Box userBox = new Box(GtkOrientation.VERTICAL, 1);
2020-10-17 11:54:07 +00:00
// /* The user's list */
// users = new ListBox();
2020-10-17 11:54:07 +00:00
// userBox.add(new Label("Users"));
// userBox.add(users);
2020-10-17 11:54:07 +00:00
// /* The text box */
// Box textBox = new Box(GtkOrientation.VERTICAL, 1);
// textBox.add(currentChannelLabel);
// textArea = new ListBox();
// import gtk.ScrolledWindow;
// ScrolledWindow scrollTextChats = new ScrolledWindow(textArea);
// textBox.add(scrollTextChats);
// import gtk.TextView;
// textBox.add(new TextView());
2020-10-17 19:05:21 +00:00
2020-10-17 11:54:07 +00:00
2020-10-17 18:33:18 +00:00
// import gtk.TextView;
// TextView f = new TextView();
// textBox.add(f);
2020-10-17 11:54:07 +00:00
box.add(channelBox);
// 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-17 11:54:07 +00:00
return box;
}
2020-10-17 12:26:17 +00:00
private int getPageNum()
{
return gui.notebook.pageNum(box);
}
2020-10-17 11:54:07 +00:00
public void shutdown()
{
/* This is called from gui.d */
2020-10-17 12:26:17 +00:00
int pageNum = getPageNum();
2020-10-17 11:54:07 +00:00
if(pageNum == -1)
{
/* TODO: Error handling */
}
else
{
gui.notebook.removePage(pageNum);
2020-10-17 12:28:54 +00:00
gui.notebook.showAll();
2020-10-17 11:54:07 +00:00
}
}
}