source: trunk/src/org/expeditee/actions/Mail.java@ 573

Last change on this file since 573 was 348, checked in by ra33, 16 years ago
File size: 5.5 KB
Line 
1package org.expeditee.actions;
2
3import java.awt.Color;
4import java.util.Collection;
5
6import javax.mail.Flags.Flag;
7
8import org.expeditee.agents.mail.MailSession;
9import org.expeditee.gui.AttributeValuePair;
10import org.expeditee.gui.DisplayIO;
11import org.expeditee.gui.Frame;
12import org.expeditee.gui.FrameKeyboardActions;
13import org.expeditee.gui.FrameMouseActions;
14import org.expeditee.gui.MessageBay;
15import org.expeditee.items.Item;
16import org.expeditee.items.Text;
17import org.expeditee.network.FrameShare;
18
19public class Mail {
20 /**
21 * Attempts to connect to mail servers
22 */
23 public static void connectMail() {
24 MailSession.connect();
25 }
26
27 /**
28 * Gets any unread mail and attaches it to the cursor for now
29 */
30 public static String getAllMailString() {
31 String allMail = MailSession.getInstance().getMailString(null, null);
32 if (allMail.length() == 0) {
33 return "Mailbox empty";
34 }
35 return allMail;
36 }
37
38 public static String getNewMailString() {
39 String mail = MailSession.getInstance()
40 .getMailString(Flag.RECENT, true);
41 if (mail.length() == 0) {
42 return "No new mail";
43 }
44 return mail;
45 }
46
47 public static String getUnreadMailString() {
48 String mail = MailSession.getInstance().getMailString(Flag.SEEN, false);
49 if (mail.length() == 0) {
50 return "No unread mail";
51 }
52 return mail;
53 }
54
55 public static Collection<Text> getRecentMail(int number) {
56 return getMail(null, null, number);
57 }
58
59 public static Collection<Text> getUnreadMail(Item clicked, int number) {
60 if (clicked instanceof Text) {
61 AttributeValuePair avp = new AttributeValuePair(clicked.getText());
62 if (avp.hasPair()
63 && avp.getValue().contains(MailSession.UNREAD_MESSAGE)) {
64 avp.setValue("0" + MailSession.UNREAD_MESSAGE + "s");
65 clicked.setText(avp.toString());
66 clicked.setActions(null);
67 }
68 }
69
70 return getMail(Flag.SEEN, false, number);
71 }
72
73 public static Collection<Text> getUnreadMail() {
74 return getMail(Flag.SEEN, false, Integer.MAX_VALUE);
75 }
76
77 public static Collection<Text> getAllMail() {
78 return getMail(null, null, Integer.MAX_VALUE);
79 }
80
81 public static Text getMail(int firstMessage, int lastMessage) {
82 // Swap message numbers if they are around the wrong way
83 if (firstMessage > lastMessage) {
84 int temp = firstMessage;
85 firstMessage = lastMessage;
86 lastMessage = temp;
87 }
88
89 MessageBay.errorMessage("Not yet supported");
90
91 return null;
92 }
93
94 public static Collection<Text> getMail(int count) {
95 return getMail(null, null, count);
96 }
97
98 public static Text getMailByID(int messageNo) {
99 Text mailItem = MailSession.getInstance().getMail(
100 DisplayIO.getCurrentFrame(), FrameMouseActions.getPosition(),
101 messageNo - 1);
102 // MessageBay.displayMessage(mailItems.size() + " messages read",
103 // Color.green);
104 if (mailItem == null) {
105 MessageBay
106 .errorMessage("Mail message does not exist: " + messageNo);
107 }
108
109 return mailItem;
110 }
111
112 public static Collection<Text> getMail() {
113 return getAllMail();
114 }
115
116 public static Collection<Text> getNewMail() {
117 return getMail(Flag.RECENT, true, Integer.MAX_VALUE);
118 }
119
120 private static Collection<Text> getMail(Flag flag, Boolean isPresent,
121 int noOfMessages) {
122 Collection<Text> mailItems = MailSession.getInstance().getMail(flag,
123 isPresent, DisplayIO.getCurrentFrame(),
124 FrameMouseActions.getPosition(), noOfMessages);
125 // MessageBay.displayMessage(mailItems.size() + " messages read",
126 // Color.green);
127
128 return mailItems;
129 }
130
131 public static String getUnreadMailCount() {
132 try {
133 int count = MailSession.getInstance().getFolder()
134 .getUnreadMessageCount();
135 return count + " unread message" + (count == 1 ? "" : "s");
136 } catch (Exception e) {
137 return "Mail Error";
138 }
139 }
140
141 public static String getAllMailCount() {
142 try {
143 int count = MailSession.getInstance().getFolder().getMessageCount();
144 return count + " message" + (count == 1 ? "" : "s");
145 } catch (Exception e) {
146 return "Mail Error";
147 }
148 }
149
150 public static String getMailCount() {
151 return getAllMailCount();
152 }
153
154 public static String getNewMailCount() {
155 try {
156 int count = MailSession.getInstance().getFolder()
157 .getNewMessageCount();
158 return count + " new messages" + (count == 1 ? "" : "s");
159 } catch (Exception e) {
160 return "Mail Error";
161 }
162 }
163
164 public static void reply(Frame frame, Item reply) {
165 String fromAddress = frame.getAnnotationValue("from");
166 if (fromAddress == null) {
167 return;
168 }
169
170 reply.setActions(null);
171 FrameMouseActions.tdfc(reply);
172
173 Frame replyFrame = DisplayIO.getCurrentFrame();
174 String titleText = frame.getTitle();
175 // Add Re on the end if it is not already there
176 if (titleText.length() >= 3
177 && !"re:".equals(titleText.substring(0, 3).toLowerCase())) {
178 titleText = "Re: " + titleText;
179 }
180 replyFrame.setTitle(titleText);
181 FrameKeyboardActions.Drop(null, false);
182
183 // Add a link to the original message
184 Text original = replyFrame.createNewText("@original");
185 original.setPosition(FrameMouseActions.getPosition());
186 original.setLink(frame.getName());
187 FrameKeyboardActions.Drop(original, false);
188
189 Text to = replyFrame.createNewText("@to: " + fromAddress);
190 to.setPosition(FrameMouseActions.getPosition());
191 to.addAction("MailTree");
192 FrameKeyboardActions.Drop(to, false);
193 DisplayIO.setCursorPosition(FrameMouseActions.MouseX,
194 FrameMouseActions.MouseY + 15);
195 }
196
197 public static void sendMessage(String peerName, String message) {
198 if (FrameShare.getInstance().sendMessage(message, peerName)) {
199 MessageBay.displayMessage("Sent message to " + peerName,
200 Color.green.darker());
201 } else {
202 MessageBay.errorMessage("Could not find " + peerName);
203 }
204 }
205}
Note: See TracBrowser for help on using the repository browser.