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