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

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 6.3 KB
Line 
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
19package org.expeditee.actions;
20
21import java.awt.Color;
22import java.util.Collection;
23
24import javax.mail.Flags.Flag;
25
26import org.expeditee.agents.mail.MailSession;
27import org.expeditee.gui.AttributeValuePair;
28import org.expeditee.gui.DisplayIO;
29import org.expeditee.gui.Frame;
30import org.expeditee.gui.FrameKeyboardActions;
31import org.expeditee.gui.FrameMouseActions;
32import org.expeditee.gui.MessageBay;
33import org.expeditee.items.Item;
34import org.expeditee.items.Text;
35import org.expeditee.network.FrameShare;
36
37public 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 DisplayIO.getCurrentFrame(), FrameMouseActions.getPosition(),
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, DisplayIO.getCurrentFrame(),
142 FrameMouseActions.getPosition(), 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 FrameMouseActions.tdfc(reply);
190
191 Frame replyFrame = DisplayIO.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 FrameKeyboardActions.Drop(null, false);
200
201 // Add a link to the original message
202 Text original = replyFrame.createNewText("@original");
203 original.setPosition(FrameMouseActions.getPosition());
204 original.setLink(frame.getName());
205 FrameKeyboardActions.Drop(original, false);
206
207 Text to = replyFrame.createNewText("@to: " + fromAddress);
208 to.setPosition(FrameMouseActions.getPosition());
209 to.addAction("MailTree");
210 FrameKeyboardActions.Drop(to, false);
211 DisplayIO.setCursorPosition(FrameMouseActions.MouseX,
212 FrameMouseActions.MouseY + 15);
213 }
214
215 public static void sendMessage(String peerName, String message) {
216 if (FrameShare.getInstance().sendMessage(message, peerName)) {
217 MessageBay.displayMessage("Sent message to " + peerName,
218 Color.green.darker());
219 } else {
220 MessageBay.errorMessage("Could not find " + peerName);
221 }
222 }
223}
Note: See TracBrowser for help on using the repository browser.