1 module UserNode; 2 3 import Connection; 4 import libdnet.dclient; 5 import gtk.Box; 6 import gtk.Button; 7 import gtk.Image; 8 import gtk.Label; 9 import gtk.Tooltip; 10 import gtk.Widget; 11 import std.string; 12 import ProfileWIndow; 13 14 public final class UserNode 15 { 16 private Connection connection; 17 private string username; 18 19 private Box box; 20 21 this(Connection connection, string username) 22 { 23 this.connection = connection; 24 this.username = username; 25 26 initBox(); 27 } 28 29 private void userButtonClick(Button) 30 { 31 /* Create a new ProfileWindow */ 32 ProfileWindow profileWindow = new ProfileWindow(connection, username); 33 } 34 35 private void initBox() 36 { 37 /* Create a new Box */ 38 box = new Box(GtkOrientation.HORIZONTAL, 10); 39 40 /* Layout [Button (Prescence Icon)] - Label <username> - [Button (Reply Icon)]*/ 41 Button userButton = new Button(); 42 Image userButtonImg = new Image("user-available", GtkIconSize.BUTTON); 43 userButton.setImage(userButtonImg); 44 45 /* Set the handler for on click */ 46 userButton.addOnClicked(&userButtonClick); 47 48 /* Create a label */ 49 Label userLabel = new Label(username); 50 51 /* Enable the tooltip */ 52 userLabel.setHasTooltip(true); 53 54 /* Set the handler to run on hover */ 55 userLabel.addOnQueryTooltip(&userLabelHoverHandler); 56 57 /* TODO: Implement me */ 58 59 /* Add both components */ 60 box.add(userButton); 61 box.add(userLabel); 62 63 64 /* Add the direct message button */ 65 Button messageButton = new Button(); 66 Image messageButtonImg = new Image("mail-forward", GtkIconSize.BUTTON); 67 messageButton.setImage(messageButtonImg); 68 69 /* Set the handler for on click */ 70 messageButton.addOnClicked(&newDirectMessage); 71 72 box.add(messageButton); 73 } 74 75 /** 76 * This is the handler for when the "direct message" button is clicked 77 * 78 * It will call `addDirectMessage_unsafe` with the username specified 79 */ 80 private void newDirectMessage(Button) 81 { 82 connection.addDirectMessage_unsafe(username); 83 } 84 85 86 /** 87 * Event handler to be run when you hover over a user's 88 * username in the Users sidebar list which will show 89 * the status as text (and in an icon format), the user's 90 * username and also their status message 91 */ 92 private bool userLabelHoverHandler(int, int, bool, Tooltip tooltip, Widget userLabel) 93 { 94 /* Get the client */ 95 DClient client = connection.getClient(); 96 97 /* The username hovered over */ 98 string userHover = (cast(Label)userLabel).getText(); 99 100 /* The final tooltip */ 101 string toolTipText = "<b>"~userHover~"</b>"; 102 103 /* Check if there is a `precensce` message */ 104 if(client.isProperty(userHover, "pres")) 105 { 106 /* Fetch the precensce */ 107 string prescence = client.getProperty(userHover, "pres"); 108 109 /* Set the icon */ 110 tooltip.setIconFromIconName(statusToGtkIcon(prescence), GtkIconSize.DIALOG); 111 112 /* Append the precesnee to the tooltip text */ 113 toolTipText ~= "\n"~prescence; 114 } 115 116 /* Check if there is a `status` message */ 117 if(client.isProperty(userHover, "status")) 118 { 119 /* Next is status message */ 120 string status = client.getProperty(userHover, "status"); 121 122 /* Append the status to the tooltip text */ 123 toolTipText ~= "\n<i>"~status~"</i>"; 124 } 125 126 /* Set the tooltip text */ 127 tooltip.setMarkup(toolTipText); 128 129 /* TODO: Point of return value? */ 130 return 1; 131 } 132 133 private static string statusToGtkIcon(string status) 134 { 135 /* The GTK icon */ 136 string gtkIcon = "image-missing"; 137 138 if(cmp(status, "available") == 0) 139 { 140 gtkIcon = "user-available"; 141 } 142 else if(cmp(status, "away") == 0) 143 { 144 gtkIcon = "user-away"; 145 } 146 else if(cmp(status, "busy") == 0) 147 { 148 gtkIcon = "user-busy"; 149 } 150 /* TODO: This doesn't make sense */ 151 else if(cmp(status, "offline") == 0) 152 { 153 gtkIcon = "user-offline"; 154 } 155 156 return gtkIcon; 157 } 158 159 public Box getBox() 160 { 161 return box; 162 /* TODO: Implement me */ 163 } 164 }