1 | /**
|
---|
2 | * Mail.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.actions;
|
---|
20 |
|
---|
21 | import java.util.Collection;
|
---|
22 |
|
---|
23 | import javax.mail.Flags.Flag;
|
---|
24 |
|
---|
25 | import org.expeditee.agents.mail.MailSession;
|
---|
26 | import org.expeditee.core.Colour;
|
---|
27 | import org.expeditee.gio.EcosystemManager;
|
---|
28 | import org.expeditee.gio.gesture.StandardGestureActions;
|
---|
29 | import org.expeditee.gui.AttributeValuePair;
|
---|
30 | import org.expeditee.gui.DisplayController;
|
---|
31 | import org.expeditee.gui.Frame;
|
---|
32 | import org.expeditee.gui.MessageBay;
|
---|
33 | import org.expeditee.items.Item;
|
---|
34 | import org.expeditee.items.Text;
|
---|
35 | import org.expeditee.network.FrameShare;
|
---|
36 |
|
---|
37 | public class Mail {
|
---|
38 | /**
|
---|
39 | * Attempts to connect to mail servers
|
---|
40 | */
|
---|
41 | public static void connectMail() {
|
---|
42 | MailSession.connect();
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Gets any unread mail and attaches it to the cursor for now
|
---|
47 | */
|
---|
48 | public static String getAllMailString() {
|
---|
49 | String allMail = MailSession.getInstance().getMailString(null, null);
|
---|
50 | if (allMail.length() == 0) {
|
---|
51 | return "Mailbox empty";
|
---|
52 | }
|
---|
53 | return allMail;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public static String getNewMailString() {
|
---|
57 | String mail = MailSession.getInstance()
|
---|
58 | .getMailString(Flag.RECENT, true);
|
---|
59 | if (mail.length() == 0) {
|
---|
60 | return "No new mail";
|
---|
61 | }
|
---|
62 | return mail;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public static String getUnreadMailString() {
|
---|
66 | String mail = MailSession.getInstance().getMailString(Flag.SEEN, false);
|
---|
67 | if (mail.length() == 0) {
|
---|
68 | return "No unread mail";
|
---|
69 | }
|
---|
70 | return mail;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public static Collection<Text> getRecentMail(int number) {
|
---|
74 | return getMail(null, null, number);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public static Collection<Text> getUnreadMail(Item clicked, int number) {
|
---|
78 | if (clicked instanceof Text) {
|
---|
79 | AttributeValuePair avp = new AttributeValuePair(clicked.getText());
|
---|
80 | if (avp.hasPair()
|
---|
81 | && avp.getValue().contains(MailSession.UNREAD_MESSAGE)) {
|
---|
82 | avp.setValue("0" + MailSession.UNREAD_MESSAGE + "s");
|
---|
83 | clicked.setText(avp.toString());
|
---|
84 | clicked.setActions(null);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | return getMail(Flag.SEEN, false, number);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public static Collection<Text> getUnreadMail() {
|
---|
92 | return getMail(Flag.SEEN, false, Integer.MAX_VALUE);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static Collection<Text> getAllMail() {
|
---|
96 | return getMail(null, null, Integer.MAX_VALUE);
|
---|
97 | }
|
---|
98 |
|
---|
99 | public static Text getMail(int firstMessage, int lastMessage) {
|
---|
100 | // Swap message numbers if they are around the wrong way
|
---|
101 | if (firstMessage > lastMessage) {
|
---|
102 | int temp = firstMessage;
|
---|
103 | firstMessage = lastMessage;
|
---|
104 | lastMessage = temp;
|
---|
105 | }
|
---|
106 |
|
---|
107 | MessageBay.errorMessage("Not yet supported");
|
---|
108 |
|
---|
109 | return null;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public static Collection<Text> getMail(int count) {
|
---|
113 | return getMail(null, null, count);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public static Text getMailByID(int messageNo) {
|
---|
117 | Text mailItem = MailSession.getInstance().getMail(
|
---|
118 | DisplayController.getCurrentFrame(), DisplayController.getMousePosition(),
|
---|
119 | messageNo - 1);
|
---|
120 | // MessageBay.displayMessage(mailItems.size() + " messages read",
|
---|
121 | // Color.green);
|
---|
122 | if (mailItem == null) {
|
---|
123 | MessageBay
|
---|
124 | .errorMessage("Mail message does not exist: " + messageNo);
|
---|
125 | }
|
---|
126 |
|
---|
127 | return mailItem;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public static Collection<Text> getMail() {
|
---|
131 | return getAllMail();
|
---|
132 | }
|
---|
133 |
|
---|
134 | public static Collection<Text> getNewMail() {
|
---|
135 | return getMail(Flag.RECENT, true, Integer.MAX_VALUE);
|
---|
136 | }
|
---|
137 |
|
---|
138 | private static Collection<Text> getMail(Flag flag, Boolean isPresent,
|
---|
139 | int noOfMessages) {
|
---|
140 | Collection<Text> mailItems = MailSession.getInstance().getMail(flag,
|
---|
141 | isPresent, DisplayController.getCurrentFrame(),
|
---|
142 | DisplayController.getMousePosition(), noOfMessages);
|
---|
143 | // MessageBay.displayMessage(mailItems.size() + " messages read",
|
---|
144 | // Color.green);
|
---|
145 |
|
---|
146 | return mailItems;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public static String getUnreadMailCount() {
|
---|
150 | try {
|
---|
151 | int count = MailSession.getInstance().getFolder()
|
---|
152 | .getUnreadMessageCount();
|
---|
153 | return count + " unread message" + (count == 1 ? "" : "s");
|
---|
154 | } catch (Exception e) {
|
---|
155 | return "Mail Error";
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | public static String getAllMailCount() {
|
---|
160 | try {
|
---|
161 | int count = MailSession.getInstance().getFolder().getMessageCount();
|
---|
162 | return count + " message" + (count == 1 ? "" : "s");
|
---|
163 | } catch (Exception e) {
|
---|
164 | return "Mail Error";
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | public static String getMailCount() {
|
---|
169 | return getAllMailCount();
|
---|
170 | }
|
---|
171 |
|
---|
172 | public static String getNewMailCount() {
|
---|
173 | try {
|
---|
174 | int count = MailSession.getInstance().getFolder()
|
---|
175 | .getNewMessageCount();
|
---|
176 | return count + " new messages" + (count == 1 ? "" : "s");
|
---|
177 | } catch (Exception e) {
|
---|
178 | return "Mail Error";
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | public static void reply(Frame frame, Item reply) {
|
---|
183 | String fromAddress = frame.getAnnotationValue("from");
|
---|
184 | if (fromAddress == null) {
|
---|
185 | return;
|
---|
186 | }
|
---|
187 |
|
---|
188 | reply.setActions(null);
|
---|
189 | StandardGestureActions.tdfc(reply);
|
---|
190 |
|
---|
191 | Frame replyFrame = DisplayController.getCurrentFrame();
|
---|
192 | String titleText = frame.getTitle();
|
---|
193 | // Add Re on the end if it is not already there
|
---|
194 | if (titleText.length() >= 3
|
---|
195 | && !"re:".equals(titleText.substring(0, 3).toLowerCase())) {
|
---|
196 | titleText = "Re: " + titleText;
|
---|
197 | }
|
---|
198 | replyFrame.setTitle(titleText);
|
---|
199 | StandardGestureActions.Drop(null, false);
|
---|
200 |
|
---|
201 | // Add a link to the original message
|
---|
202 | Text original = replyFrame.createNewText("@original");
|
---|
203 | original.setPosition(DisplayController.getMousePosition());
|
---|
204 | original.setLink(frame.getName());
|
---|
205 | StandardGestureActions.Drop(original, false);
|
---|
206 |
|
---|
207 | Text to = replyFrame.createNewText("@to: " + fromAddress);
|
---|
208 | to.setPosition(DisplayController.getMousePosition());
|
---|
209 | to.addAction("MailTree");
|
---|
210 | StandardGestureActions.Drop(to, false);
|
---|
211 | DisplayController.setCursorPosition(DisplayController.getMouseX(), DisplayController.getMouseY() + 15);
|
---|
212 | }
|
---|
213 |
|
---|
214 | public static void sendMessage(String peerName, String message) {
|
---|
215 | if (FrameShare.getInstance().sendMessage(message, peerName)) {
|
---|
216 | MessageBay.displayMessage("Sent message to " + peerName,
|
---|
217 | Colour.GREEN.darker());
|
---|
218 | } else {
|
---|
219 | MessageBay.errorMessage("Could not find " + peerName);
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|