1 module gui;
2 
3 import core.thread;
4 import gtk.MainWindow;
5 import gtk.ListBox;
6 import gtk.Label;
7 import gtk.Notebook;
8 import gdk.Threads : te = threadsEnter, tl = threadsLeave;
9 import gtk.MenuBar;
10 import gtk.Box;
11 import gtk.Menu;
12 import gtk.MenuItem;
13 import std.stdio;
14 import gtk.Statusbar;
15 import gtk.Toolbar;
16 
17 import Connection;
18 import std.socket;
19 
20 import std.conv;
21 
22 public class GUI : Thread
23 {
24     /* Main window (GUI homepage) */
25     public MainWindow mainWindow;
26     private MenuBar menuBar;
27     private Toolbar toolbar;
28     public Notebook notebook;
29 
30     private Statusbar statusBar;
31 
32 
33     private Connection[] connections;
34 
35     private ListBox list;
36     
37 
38     this()
39     {
40         super(&worker);
41     }
42 
43     private void worker()
44     {
45         initializeGUI();
46         te();
47 
48         tl();
49         writeln("brg");
50         while(true)
51         {
52 
53         }
54     }
55 
56     private void initializeGUI()
57     {
58         initializeMainWindow();
59 
60         /* Test adding a connection */
61         for(uint i = 0; i < 5; i++)
62         {
63             // connections ~= new Connection(this, parseAddress("0.0.0.0", 7777));
64         }
65 
66         connections ~= new Connection(this, parseAddress("0.0.0.0", 7777), ["testGustav"~to!(string)(connections.length), "bruh"]);
67         
68         
69     }
70 
71     /**
72     * Initializes the main home screen window
73     */
74     private void initializeMainWindow()
75     {
76         /* Get GTK lock */
77         te();
78 
79         /* Create a window */
80         mainWindow = new MainWindow("unamed");
81 
82         /**
83         * Create a Box in vertical layout mode
84         * and adds it to the window
85         *
86         * This lays out components like so:
87         *
88         * |component 1|
89         * |component 2|
90         */
91         Box box = new Box(GtkOrientation.VERTICAL, 1);
92 
93         /**
94         * Add needed components
95         *
96         * Menubar, tabbed pane switcher, statusbar
97         */
98         menuBar = initializeMenuBar();
99         box.add(menuBar);
100 
101         toolbar = getToolbar();
102         box.add(toolbar);
103 
104         notebook = new Notebook();
105         notebook.setScrollable(true);
106         box.add(notebook);
107         statusBar = new Statusbar();
108         statusBar.add(new Label("Gustav: Bruh"));
109         box.setChildPacking(notebook, true, true, 0, GtkPackType.START);
110         box.packEnd(statusBar, 0, 0, 0);
111         //notebook.add(createServerTab());
112         
113 
114 
115 
116         /* Add the Box to main window */
117         mainWindow.add(box);
118 
119         mainWindow.showAll();
120 
121         /* Unlock GTK lock */
122         tl();
123 
124         writeln("unlock gui setup");
125     }
126 
127     private Toolbar getToolbar()
128     {
129         Toolbar toolbar = new Toolbar();
130 
131         /* Status selector dropdown */
132         import gtk.ComboBox;
133         import gtk.ToolButton;
134 
135         // Menu menu = new Menu();
136         // menu.add(new MenuItem(""));
137         ComboBox statusBox = new ComboBox();
138         statusBox.setTitle("Status");
139 
140 
141         ToolButton setAvail = new ToolButton("");
142         setAvail.setLabel("available");
143         setAvail.setIconName("user-available");
144         toolbar.add(setAvail);
145 
146         ToolButton setAway = new ToolButton("");
147         setAway.setLabel("away");
148         setAway.setIconName("user-away");
149         toolbar.add(setAway);
150 
151         ToolButton setBusy = new ToolButton("");
152         setBusy.setLabel("busy");
153         setBusy.setIconName("user-busy");
154         toolbar.add(setBusy);
155 
156 
157         setAvail.addOnClicked(&setStatus);
158         setAway.addOnClicked(&setStatus);
159         setBusy.addOnClicked(&setStatus);
160         
161 
162         
163         //toolbar.add(new ToolButton("user-available,""Available"));
164         // toolbar.add(new ToolButton("Away"));
165         // toolbar.add(new ToolButton("Busy"));
166         // toolbar.add(new Label("Away"));
167         // toolbar.add(new Label("Busy"));
168         // import gtk.ToolItem;
169         
170         // ToolItem toolItem = new ToolItem();
171         // toolItem.add(new Label("Available"));
172 
173 
174         // statusBox.add()
175 
176         //toolbar.add(statusBox);
177 
178       
179 
180         return toolbar;
181     }
182 
183     import gtk.ToolButton;
184     private void setStatus(ToolButton x)
185     {
186         /* Get the current connection */
187         Connection currentConnection = connections[notebook.getCurrentPage()];
188 
189         /* Set the status */
190         currentConnection.getClient().setStatus(x.getLabel());
191     }
192 
193     private MenuBar initializeMenuBar()
194     {
195         MenuBar menuBar = new MenuBar();
196 
197         /* Gustav menu */
198         MenuItem gustavMenuItem = new MenuItem();
199         gustavMenuItem.setLabel("Gustav");
200         Menu gustavMenu = new Menu();
201         gustavMenuItem.setSubmenu(gustavMenu);
202         
203         /* Connect option */
204         MenuItem connectItem = new MenuItem();
205         connectItem.setLabel("Connect");
206         connectItem.addOnActivate(&connectButton);
207         gustavMenu.add(connectItem);
208 
209         /* Exit option */
210         MenuItem exitItem = new MenuItem();
211         exitItem.setLabel("Exit");
212         exitItem.addOnActivate(&exitButton);
213         gustavMenu.add(exitItem);
214 
215         
216 
217         
218 
219         /* Add all menues */
220         menuBar.add(gustavMenuItem);
221 
222         return menuBar;
223     }
224 
225     private void exitButton(MenuItem)
226     {
227         writeln("bruh");
228 
229         /* TODO: Implement exit */
230 
231         
232 // tl();
233         //te();
234 
235         
236         shutdownConnections();
237 
238 
239        // mainWindow.showAll();
240 
241        // tl();
242     }
243 
244     private void connectButton(MenuItem)
245     {
246        connections ~= new Connection(this, parseAddress("0.0.0.0", 7777), ["testGustav"~to!(string)(connections.length), "bruh"]);
247     }
248 
249     private void shutdownConnections()
250     {
251         foreach(Connection connection; connections)
252         {
253             /**
254             * TODO: This is called by signal handler, we need no mutexes for signal handler
255             * hence it means that connection
256             */
257             connection.shutdown();
258             Thread.sleep(dur!("seconds")(2));
259         }
260     }
261 
262     private void newServer()
263     {
264 
265     }
266 
267     private Box createServerTab()
268     {
269         Box serverTab = new Box(GtkOrientation.HORIZONTAL, 1);
270 
271         serverTab.add(new Label("hello"));
272 
273         // serverTab.add();
274 
275         return serverTab;
276     }
277 }