1 module areas.User;
2 
3 import areas.MessageArea;
4 
5 import gtk.Box;
6 import gtk.ListBox;
7 import gtk.Label;
8 import gtk.TextView;
9 import libdnet.dclient;
10 import gtk.Label;
11 import std.string;
12 import gtk.Button;
13 import gtk.Tooltip;
14 import gtk.Widget;
15 import gtk.ScrolledWindow;
16 import gtk.Button;
17 import gtk.Entry;
18 import UserNode;
19 
20 import pango.PgAttributeList;
21 import pango.PgAttribute;
22 import Connection;
23 
24 import gogga;
25 
26 public final class User : MessageArea
27 {
28     private DClient client;
29     private Connection connection;
30 
31     /**
32     * Username
33     */
34     private string username;
35 
36     /**
37     * UI components
38     *
39     */
40     // private ListBox users;
41     private ListBox textArea;
42     private Entry textInput;
43     
44 
45     /* TODO: No mutexes should be needed (same precaution) as the GTK lock provides safety */
46     // private string[] usersString;
47 
48     this(Connection connection, string username)
49     {
50         this.client = connection.getClient();
51         this.connection = connection;
52         this.username = username;
53         
54         initializeBox();
55     }
56 
57     public string getUsername()
58     {
59         return username;
60     }
61 
62     private void initializeBox()
63     {
64         box = new Box(GtkOrientation.HORIZONTAL, 1);
65 
66         // box.add(new Label("poes"));
67         /* The text box */
68         Box textBox = new Box(GtkOrientation.VERTICAL, 1);
69 
70         /* Channel title */
71         Label channelTitleLabel = new Label(username);
72         channelTitleLabel.setMarkup("<span size=\"large\"><b>"~username~"</b></span>");
73         textBox.add(channelTitleLabel);
74 
75         /* The messages box */
76         textArea = new ListBox();
77         ScrolledWindow scrollTextChats = new ScrolledWindow(textArea);
78         textBox.add(scrollTextChats);
79         
80 
81         /* The Box for the whole |attach button| text field| send button| */
82         Box textInputBox = new Box(GtkOrientation.HORIZONTAL, 1);
83 
84         import gtk.Image;
85 
86         /* The attachment button */
87         Button attachFileButton = new Button("Upload");
88         Image attachFileButtonIcon = new Image("user-available", GtkIconSize.BUTTON); /* TODO: Fix icon now showing */
89         attachFileButton.setImage(attachFileButtonIcon);
90         attachFileButton.addOnClicked(&uploadFileDialog);
91         textInputBox.add(attachFileButton);
92 
93         /* The text input */
94         textInput = new Entry();
95         textInput.addOnActivate(&sendMessageEnter);
96         textInput.addOnChanged(&textChangd);
97         
98         textInputBox.packStart(textInput,1,1,0);
99         
100 
101         /* The send button */
102         Button sendButton = new Button("Send");
103         sendButton.addOnClicked(&sendMessageBtn);
104         textInputBox.add(sendButton);
105         textBox.add(textInputBox);
106         
107         box.add(textBox);
108         // box.packEnd(userBox,0,0,0);
109 
110         textBox.setChildPacking(scrollTextChats, true, true, 0, GtkPackType.START);
111         box.setChildPacking(textBox, true, true, 0, GtkPackType.START);
112 
113     }
114 
115 
116     import gtk.EditableIF;
117     private void textChangd(EditableIF)
118     {
119         /* If the text box just became empty stop ssending typing notifications */
120         /* Send typing stats */
121         // client.sendIsTyping(channelName, true);
122         /* TODO: Client implement wiht different tag? */
123     }
124 
125     private void sendMessageEnter(Entry)
126     {
127         /* Retrieve the message */
128         string message = textInput.getBuffer().getText();
129 
130         /* TODO: Add the message to our log (as it won't be delivered to us) */
131         sendMessage(message);
132 
133         /* Send the message */
134         client.sendMessage(1, username, message);
135 
136         /* Clear the text box */
137         textInput.getBuffer().setText("",0);
138 
139         box.showAll();
140     }
141 
142     private void sendMessageBtn(Button)
143     {
144         /* Retrieve the message */
145         string message = textInput.getBuffer().getText();
146 
147         /* TODO: Add the message to our log (as it won't be delivered to us) */
148         sendMessage(message);
149 
150         /* Send the message */
151         client.sendMessage(1, username, message);
152 
153         /* Clear the text box */
154         textInput.getBuffer().setText("",0);
155 
156         box.showAll();
157     }
158 
159      public void sendMessage(string message)
160     {
161         /* TOOD: Pass in connection perhaps */
162         string username = "Yourself";
163 
164         /* Create the MessageBox */
165         Box messageBox = new Box(GtkOrientation.VERTICAL, 1);
166 
167         /* Create and add the username */
168         Label usernameLabel = new Label("");
169         usernameLabel.setMarkup("<b>"~username~"</b>");
170         usernameLabel.setHalign(GtkAlign.END);
171         messageBox.add(usernameLabel);
172 
173         /* Create and add the message */
174         Label messageLabel = new Label(message);
175         messageLabel.setHalign(GtkAlign.END);
176         messageLabel.setSelectable(true);
177         messageBox.add(messageLabel);
178 
179         /* Add the message to the log */
180         textArea.add(messageBox);
181     }
182 
183     public void receiveMessage(string username, string message)
184     {
185         /* Create the MessageBox */
186         Box messageBox = new Box(GtkOrientation.VERTICAL, 1);
187 
188         /* Create and add the username */
189         Label usernameLabel = new Label("");
190         usernameLabel.setMarkup("<b>"~username~"</b>");
191         usernameLabel.setHalign(GtkAlign.START);
192         messageBox.add(usernameLabel);
193 
194         /* Create and add the message */
195         Label messageLabel = new Label(message);
196         messageLabel.setHalign(GtkAlign.START);
197         messageLabel.setSelectable(true);
198         messageBox.add(messageLabel);
199 
200         // import gtk.Image;
201         // Image d = new Image("/home/deavmi/Downloads/5207740.jpg");
202         // messageBox.add(d);
203 
204         /* Add the message to the log */
205         textArea.add(messageBox);
206     }
207 
208     private void uploadFileDialog(Button e)
209     {
210         import gtk.FileChooserDialog; /* TODO: Set parent */
211         FileChooserDialog fileChooser = new FileChooserDialog("Send file to "~username, null, FileChooserAction.OPEN);
212         fileChooser.run();
213         gprintln("Selected file: "~fileChooser.getFilename());
214     }
215 }