/**
* Mail.java
* Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.expeditee.actions;
import java.util.Collection;
import javax.mail.Flags.Flag;
import org.expeditee.agents.mail.MailSession;
import org.expeditee.core.Colour;
import org.expeditee.gio.EcosystemManager;
import org.expeditee.gio.gesture.StandardGestureActions;
import org.expeditee.gui.AttributeValuePair;
import org.expeditee.gui.DisplayController;
import org.expeditee.gui.Frame;
import org.expeditee.gui.MessageBay;
import org.expeditee.items.Item;
import org.expeditee.items.Text;
import org.expeditee.network.FrameShare;
public class Mail {
/**
* Attempts to connect to mail servers
*/
public static void connectMail() {
MailSession.connect();
}
/**
* Gets any unread mail and attaches it to the cursor for now
*/
public static String getAllMailString() {
String allMail = MailSession.getInstance().getMailString(null, null);
if (allMail.length() == 0) {
return "Mailbox empty";
}
return allMail;
}
public static String getNewMailString() {
String mail = MailSession.getInstance()
.getMailString(Flag.RECENT, true);
if (mail.length() == 0) {
return "No new mail";
}
return mail;
}
public static String getUnreadMailString() {
String mail = MailSession.getInstance().getMailString(Flag.SEEN, false);
if (mail.length() == 0) {
return "No unread mail";
}
return mail;
}
public static Collection getRecentMail(int number) {
return getMail(null, null, number);
}
public static Collection getUnreadMail(Item clicked, int number) {
if (clicked instanceof Text) {
AttributeValuePair avp = new AttributeValuePair(clicked.getText());
if (avp.hasPair()
&& avp.getValue().contains(MailSession.UNREAD_MESSAGE)) {
avp.setValue("0" + MailSession.UNREAD_MESSAGE + "s");
clicked.setText(avp.toString());
clicked.setActions(null);
}
}
return getMail(Flag.SEEN, false, number);
}
public static Collection getUnreadMail() {
return getMail(Flag.SEEN, false, Integer.MAX_VALUE);
}
public static Collection getAllMail() {
return getMail(null, null, Integer.MAX_VALUE);
}
public static Text getMail(int firstMessage, int lastMessage) {
// Swap message numbers if they are around the wrong way
if (firstMessage > lastMessage) {
int temp = firstMessage;
firstMessage = lastMessage;
lastMessage = temp;
}
MessageBay.errorMessage("Not yet supported");
return null;
}
public static Collection getMail(int count) {
return getMail(null, null, count);
}
public static Text getMailByID(int messageNo) {
Text mailItem = MailSession.getInstance().getMail(
DisplayController.getCurrentFrame(), DisplayController.getMousePosition(),
messageNo - 1);
// MessageBay.displayMessage(mailItems.size() + " messages read",
// Color.green);
if (mailItem == null) {
MessageBay
.errorMessage("Mail message does not exist: " + messageNo);
}
return mailItem;
}
public static Collection getMail() {
return getAllMail();
}
public static Collection getNewMail() {
return getMail(Flag.RECENT, true, Integer.MAX_VALUE);
}
private static Collection getMail(Flag flag, Boolean isPresent,
int noOfMessages) {
Collection mailItems = MailSession.getInstance().getMail(flag,
isPresent, DisplayController.getCurrentFrame(),
DisplayController.getMousePosition(), noOfMessages);
// MessageBay.displayMessage(mailItems.size() + " messages read",
// Color.green);
return mailItems;
}
public static String getUnreadMailCount() {
try {
int count = MailSession.getInstance().getFolder()
.getUnreadMessageCount();
return count + " unread message" + (count == 1 ? "" : "s");
} catch (Exception e) {
return "Mail Error";
}
}
public static String getAllMailCount() {
try {
int count = MailSession.getInstance().getFolder().getMessageCount();
return count + " message" + (count == 1 ? "" : "s");
} catch (Exception e) {
return "Mail Error";
}
}
public static String getMailCount() {
return getAllMailCount();
}
public static String getNewMailCount() {
try {
int count = MailSession.getInstance().getFolder()
.getNewMessageCount();
return count + " new messages" + (count == 1 ? "" : "s");
} catch (Exception e) {
return "Mail Error";
}
}
public static void reply(Frame frame, Item reply) {
String fromAddress = frame.getAnnotationValue("from");
if (fromAddress == null) {
return;
}
reply.setActions(null);
StandardGestureActions.tdfc(reply);
Frame replyFrame = DisplayController.getCurrentFrame();
String titleText = frame.getTitle();
// Add Re on the end if it is not already there
if (titleText.length() >= 3
&& !"re:".equals(titleText.substring(0, 3).toLowerCase())) {
titleText = "Re: " + titleText;
}
replyFrame.setTitle(titleText);
StandardGestureActions.Drop(null, false);
// Add a link to the original message
Text original = replyFrame.createNewText("@original");
original.setPosition(DisplayController.getMousePosition());
original.setLink(frame.getName());
StandardGestureActions.Drop(original, false);
Text to = replyFrame.createNewText("@to: " + fromAddress);
to.setPosition(DisplayController.getMousePosition());
to.addAction("MailTree");
StandardGestureActions.Drop(to, false);
DisplayController.setCursorPosition(DisplayController.getMouseX(), DisplayController.getMouseY() + 15);
}
public static void sendMessage(String peerName, String message) {
if (FrameShare.getInstance().sendMessage(message, peerName)) {
MessageBay.displayMessage("Sent message to " + peerName,
Colour.GREEN.darker());
} else {
MessageBay.errorMessage("Could not find " + peerName);
}
}
}