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