1 module ConnectionAssistant;
2 
3 /**
4 * ConnectionAssistant
5 *
6 * This provides a graphical utility for
7 * configuring new connections
8 */
9 
10 import gtk.Assistant;
11 import gtk.Label;
12 import gtk.Box;
13 import gtk.Entry;
14 import gtk.Image;
15 import gui;
16 import std.conv;
17 import std.string : cmp, strip;
18 
19 public final class ConnectionAssistant : Assistant
20 {
21     /* Associated GUI instance */
22     private GUI gui;
23 
24     private Entry serverAddress;
25     private Entry serverPort;
26     private Entry username;
27     private Entry password;
28 
29     /* Summary box */
30     Box summaryBox;
31 
32 
33     this(GUI gui)
34     {
35         this.gui = gui;
36 
37         initWindow();
38     }
39 
40     private void initWindow()
41     {   
42         Assistant connectionAssistant = new Assistant();
43 
44         /* Welcome page */
45         Box welcomeBox = new Box(GtkOrientation.VERTICAL, 1);
46         Image logo = new Image("user-available", GtkIconSize.DIALOG);
47         logo.setPixelSize(250);
48         welcomeBox.add(logo);
49 
50         Label title = new Label("<span size=\"100\">Gustav</span>");
51         title.setMarkup("<span font_desc=\"Open Sans Extrabold\" size=\"50000\">Gustav</span>");
52         welcomeBox.add(title);
53 
54         Label hello = new Label("");
55         hello.setMarkup("<span size=\"15000\">Welcome to the connection setup</span>");
56         welcomeBox.add(hello);
57 
58         connectionAssistant.insertPage(welcomeBox, 0);
59         connectionAssistant.setPageTitle(welcomeBox, "Welcome");
60 
61         /* Configure a server */
62         Box serverBox = new Box(GtkOrientation.VERTICAL, 1);
63         Label serverBoxTitle = new Label("");
64         serverBoxTitle.setMarkup("<span size=\"15000\">Server details</span>");
65         serverBox.packStart(serverBoxTitle,0,0,30);
66         serverAddress = new Entry();
67         serverBox.add(serverAddress);
68         serverAddress.setPlaceholderText("DNET server address");
69         serverPort = new Entry();
70         serverBox.add(serverPort);
71         serverPort.setPlaceholderText("DNET server port");
72         
73         
74         connectionAssistant.insertPage(serverBox, 1);
75         connectionAssistant.setPageTitle(serverBox, "Network");
76 
77         /* Configure your profile details */
78         Box profileBox = new Box(GtkOrientation.VERTICAL, 1);
79         Label profileBoxTitle = new Label("");
80         profileBoxTitle.setMarkup("<span size=\"15000\">Account details</span>");
81         profileBox.packStart(profileBoxTitle,0,0,30);
82         username = new Entry();
83         profileBox.add(username);
84         username.setPlaceholderText("username");
85         password = new Entry();
86         profileBox.add(password);
87         password.setPlaceholderText("password");
88 
89         connectionAssistant.insertPage(profileBox, 2);
90         connectionAssistant.setPageTitle(profileBox, "Account");
91         
92         /* TODO: We should actually verify inputs before doing this */
93         connectionAssistant.setPageComplete(welcomeBox, true);
94         connectionAssistant.setPageComplete(serverBox, true);
95         connectionAssistant.setPageComplete(profileBox, true);
96 
97 
98         /* Summary */
99         summaryBox = new Box(GtkOrientation.VERTICAL, 1);
100         Label summaryBoxTitle = new Label("");
101         summaryBoxTitle.setMarkup("<span size=\"15000\">Summary</span>");
102         summaryBox.packStart(summaryBoxTitle,0,0,30);
103 
104         
105         
106 
107 
108         connectionAssistant.insertPage(summaryBox, 4);
109         connectionAssistant.setPageType(summaryBox, GtkAssistantPageType.SUMMARY);
110         
111 
112         connectionAssistant.addOnClose(&assistentComplete);
113         connectionAssistant.addOnCancel(&assistenctCancel);
114 
115         connectionAssistant.showAll();
116     }
117 
118     private void assistenctCancel(Assistant e)
119     {
120          /* TODO: Get this to work */
121          /* TODO: The `.close()` doesn't seem to work */
122     }
123 
124     /* TODO: I want this code to run when we are on the summary page */
125     private void kak()
126     {
127         /* Summary data */
128         Label serverAddressLabel = new Label("");
129         serverAddressLabel.setMarkup("<b>Server Address:</b> "~serverAddress.getBuffer().getText());
130 
131         Label serverPortLabel = new Label("");
132         serverPortLabel.setMarkup("<b>Server Port:</b> "~serverPort.getBuffer().getText());
133 
134         Label accountUsername = new Label("");
135         accountUsername.setMarkup("<b>Account username:</b> "~username.getBuffer().getText());
136 
137         Label accountPassword = new Label("");
138         accountPassword.setMarkup("<b>Account password:</b> "~password.getBuffer().getText());
139 
140         
141         
142         summaryBox.add(serverAddressLabel);
143         summaryBox.add(serverPortLabel);
144         summaryBox.add(accountUsername);
145         summaryBox.add(accountPassword);
146     }
147 
148     private void assistentComplete(Assistant)
149     {
150         /* Get the server details */
151         string serverAddress = strip(serverAddress.getBuffer().getText());
152         string serverPort = strip(serverPort.getBuffer().getText());
153 
154         /* Get the account details */
155         string accountUsername = strip(username.getBuffer().getText());
156         string accountPassword = password.getBuffer().getText();
157 
158         /* TODO: Check for emptiness */
159         if(cmp(serverAddress, "") == 0 || cmp(serverPort, "") == 0 || cmp(accountUsername, "") == 0 || cmp(accountPassword, "") == 0)
160         {
161             /* TODO: Handle error here */
162         }
163         else
164         {
165             /* Create a new Connection */
166             gui.connectServer(serverAddress, to!(ushort)(serverPort), [accountUsername, accountPassword]);
167         }
168     }
169 }