source: trunk/src/org/expeditee/agents/MailTree.java@ 1480

Last change on this file since 1480 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.2 KB
Line 
1/**
2 * MailTree.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.agents;
20
21import java.util.Collection;
22import java.util.Map;
23
24import org.expeditee.actions.Misc;
25import org.expeditee.agents.mail.MailSession;
26import org.expeditee.gui.AttributeValuePair;
27import org.expeditee.gui.Frame;
28import org.expeditee.gui.FrameIO;
29import org.expeditee.gui.FreeItems;
30import org.expeditee.items.Item;
31import org.expeditee.items.Text;
32import org.expeditee.stats.Formatter;
33
34public class MailTree extends DefaultAgent {
35
36 @Override
37 public boolean initialise(Frame frame, Item item) {
38 return super.initialise(frame, item);
39 }
40
41 private String getFrameText(Frame frame, String prefix) {
42 StringBuffer sb = new StringBuffer();
43 // Get the text to mail
44 for (Text t : frame.getBodyTextItems(false)) {
45 if (t.hasLink()) {
46 Frame linkedFrame = FrameIO.LoadFrame(t.getAbsoluteLink());
47 if (linkedFrame == null)
48 continue;
49
50 String frameTitle = linkedFrame.getTitle();
51 sb.append('\n').append(prefix).append(frameTitle).append('\n')
52 .append(prefix);
53 // for (int i = 0; i < frameTitle.length(); i++) {
54 // sb.append('-');
55 // }
56 sb.append("----------------");
57
58 sb.append("\n");
59 sb.append(getFrameText(linkedFrame, prefix));
60 } else {
61 sb.append(prefix).append(t.getText()).append("\n\n");
62 }
63 }
64
65 Text original = frame.getAnnotation("original");
66 if (original != null) {
67 Frame linkedFrame = FrameIO.LoadFrame(original.getAbsoluteLink());
68 if (linkedFrame != null) {
69 sb.append("\n\n");
70 String date = linkedFrame.getAnnotationValue("date");
71 if (date != null) {
72 sb.append("On ").append(date);
73 }
74 String from = linkedFrame.getAnnotationValue("from");
75 if (from != null) {
76 if (sb.length() > 0)
77 sb.append(", ");
78 //else
79 // sb.append("> ");
80 sb.append(from);
81 sb.append(" wrote:");
82 }
83 sb.append("\n\n");
84 sb.append(getFrameText(linkedFrame, ">"));
85 }
86 }
87
88 return sb.toString();
89 }
90
91 /**
92 * Email's a frame. The email addy's the mail will be sent to are taken from
93 * the frame tags of the form \@to:<AddressList>, \@cc:<AddressList> or
94 * \@bcc:<AddressList>. If such frame tags do not exist the agent looks for
95 * items on the end of the cursor. Different types of addresses can be
96 * grouped in boxes with a corner labelled, cc, bc or to. If a box does not
97 * have a label it is assumed to contain SendTo addresses.
98 */
99 @Override
100 protected Frame process(Frame frame) {
101 String subject = frame.getTitle();
102
103 // Get the text to mail
104 String body = getFrameText(frame, "");
105
106 String to = frame.getAnnotationValue("to");
107 String cc = frame.getAnnotationValue("cc");
108 String bcc = frame.getAnnotationValue("bcc");
109
110 // Check for the address on the end of the cursor
111 if (to == null) {
112 StringBuffer ccString = new StringBuffer();
113 StringBuffer toString = new StringBuffer();
114 StringBuffer bccString = new StringBuffer();
115 // Check for a group of text items attached to the cursor
116 Map<String, Collection<String>> groupedText = FreeItems
117 .getGroupedText();
118 Collection<String> ccStrings = groupedText.get("cc");
119 Collection<String> toStrings = groupedText.get("to");
120 Collection<String> bccStrings = groupedText.get("bcc");
121
122 if (cc == null && ccStrings != null) {
123 for (String address : ccStrings) {
124 if (address.charAt(0) == (AttributeValuePair.ANNOTATION_CHAR))
125 address = address.substring(1);
126 ccString.append(address);
127 ccString.append(',');
128 }
129 }
130
131 if (bcc == null && bccStrings != null) {
132 for (String address : bccStrings) {
133 if (address.charAt(0) == (AttributeValuePair.ANNOTATION_CHAR))
134 address = address.substring(1);
135 bccString.append(address);
136 bccString.append(',');
137 }
138 }
139
140 if (toStrings != null) {
141 for (String address : toStrings) {
142 if (address.charAt(0) == (AttributeValuePair.ANNOTATION_CHAR))
143 address = address.substring(1);
144 toString.append(address);
145 toString.append(',');
146 }
147 } else {
148 Collection<String> otherStrings = groupedText.get("");
149 for (String address : otherStrings) {
150 if (address.charAt(0) == (AttributeValuePair.ANNOTATION_CHAR))
151 address = address.substring(1);
152 if (address.startsWith("cc:") && address.length() > 5) {
153 ccString.append(address.substring(3).trim());
154 ccString.append(',');
155 } else if (address.startsWith("bcc:")
156 && address.length() > 6) {
157 bccString.append(address.substring(4).trim());
158 bccString.append(',');
159 } else {
160 toString.append(address);
161 toString.append(',');
162 }
163 }
164 }
165 if (toString.length() > 0) {
166 toString.deleteCharAt(toString.length() - 1);
167 to = toString.toString();
168 }
169
170 if (ccString.length() > 0) {
171 ccString.deleteCharAt(ccString.length() - 1);
172 cc = ccString.toString();
173 }
174
175 if (bccString.length() > 0) {
176 bccString.deleteCharAt(ccString.length() - 1);
177 bcc = bccString.toString();
178 }
179 }
180
181 FreeItems.getInstance().clear();
182
183 //Produce output for the user to put down on their frame
184 StringBuffer sb = new StringBuffer();
185 sb.append("@Sent: ").append(Formatter.getDateTime());
186
187 if(to != null){
188 sb.append("\nTo: " + to);
189 }
190
191 if(cc != null){
192 sb.append("\nCc: " + cc);
193 }
194
195 if(bcc != null){
196 sb.append("\nBcc: " + bcc);
197 }
198
199 Misc.attachStatsToCursor(sb.toString());
200
201 // Last chance for the user to stop
202 if (_stop)
203 return null;
204 // Allow the user to do other stuff while the message gets sent
205 _running = false;
206 MailSession.sendTextMessage(to, cc, bcc, subject, body, null);
207
208 return null;
209 }
210}
Note: See TracBrowser for help on using the repository browser.