IMAP: Implement custom IMAP flags to keywords mapping in settings

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2051 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2012-10-11 21:57:18 +00:00
parent 0cf9f1ce80
commit aa1898d138
2 changed files with 68 additions and 26 deletions

View File

@ -22,10 +22,7 @@ import davmail.ui.tray.DavGatewayTray;
import org.apache.log4j.*;
import java.io.*;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.TreeSet;
import java.util.*;
/**
* Settings facade.
@ -465,6 +462,32 @@ public final class Settings {
}
}
/**
* Get all properties that are in the specified scope, that is, that start with '<scope>.'.
*
* @param scope start of property name
* @return properties
*/
public static synchronized Properties getSubProperties(String scope) {
final String keyStart;
if (scope == null || scope.length() == 0) {
keyStart = "";
} else if (scope.endsWith(".")) {
keyStart = scope;
} else {
keyStart = scope + '.';
}
Properties result = new Properties();
for (Map.Entry entry : SETTINGS.entrySet()) {
String key = (String)entry.getKey();
if (key.startsWith(keyStart)) {
String value = (String)entry.getValue();
result.setProperty(key.substring(keyStart.length()), value);
}
}
return result;
}
/**
* Set Log4J logging level for the category
*

View File

@ -187,13 +187,13 @@ public abstract class ExchangeSession {
if (preAuthUsername == null) {
// Searches for the delimiter in configured username for the pre-auth user.
// The double-quote is not allowed inside email addresses anyway.
int doubleQuoteIndex = this.userName.indexOf('"');
int doubleQuoteIndex = this.userName.indexOf('"');
if (doubleQuoteIndex > 0) {
preAuthUsername = this.userName.substring(0, doubleQuoteIndex);
this.userName = this.userName.substring(doubleQuoteIndex + 1);
} else {
// No doublequote: the pre-auth user is the full username, or it is not used at all.
preAuthUsername = this.userName;
preAuthUsername = this.userName;
}
}
@ -513,7 +513,7 @@ public abstract class ExchangeSession {
protected HttpMethod postLogonMethod(HttpClient httpClient, HttpMethod logonMethod, String userName, String password) throws IOException {
setAuthFormFields(logonMethod, httpClient, password);
setAuthFormFields(logonMethod, httpClient, password);
// add exchange 2010 PBack cookie in compatibility mode
httpClient.getState().addCookie(new Cookie(httpClient.getHostConfiguration().getHost(), "PBack", "0", "/", null, false));
@ -1472,39 +1472,58 @@ public abstract class ExchangeSession {
/**
* Convert keyword value to IMAP flag.
*
* @param value keyword value
* @return IMAP flag
*/
public String convertKeywordToFlag(String value) {
String result = value;
// convert flags to Thunderbird flags
ResourceBundle flagBundle = ResourceBundle.getBundle("imapflags");
Enumeration<String> flagEnumeration = flagBundle.getKeys();
while (flagEnumeration.hasMoreElements()) {
String key = flagEnumeration.nextElement();
if (value.equalsIgnoreCase(flagBundle.getString(key))) {
result = key;
// first test for keyword in settings
Properties flagSettings = Settings.getSubProperties("davmail.imapFlags");
Enumeration flagSettingsEnum = flagSettings.propertyNames();
while (flagSettingsEnum.hasMoreElements()) {
String key = (String) flagSettingsEnum.nextElement();
if (value.equalsIgnoreCase(flagSettings.getProperty(key))) {
return key;
}
}
return result;
ResourceBundle flagBundle = ResourceBundle.getBundle("imapflags");
Enumeration<String> flagBundleEnum = flagBundle.getKeys();
while (flagBundleEnum.hasMoreElements()) {
String key = flagBundleEnum.nextElement();
if (value.equalsIgnoreCase(flagBundle.getString(key))) {
return key;
}
}
// fall back to raw value
return value;
}
/**
* Convert IMAP flag to keyword value.
*
* @param value IMAP flag
* @return keyword value
*/
public String convertFlagToKeyword(String value) {
String result = value;
// convert flags to Thunderbird flags
// first test for flag in settings
Properties flagSettings = Settings.getSubProperties("davmail.imapFlags");
String flagValue = flagSettings.getProperty(value);
if (flagValue != null) {
return flagValue;
}
// fall back to predefined flags
ResourceBundle flagBundle = ResourceBundle.getBundle("imapflags");
try {
result = flagBundle.getString(value);
return flagBundle.getString(value);
} catch (MissingResourceException e) {
// ignore
}
return result;
// fall back to raw value
return value;
}
/**
@ -1668,8 +1687,8 @@ public abstract class ExchangeSession {
*
* @return imap uid list
*/
public TreeMap<Long,String> getImapFlagMap() {
TreeMap<Long,String> imapFlagMap = new TreeMap<Long,String>();
public TreeMap<Long, String> getImapFlagMap() {
TreeMap<Long, String> imapFlagMap = new TreeMap<Long, String>();
for (ExchangeSession.Message message : messages) {
imapFlagMap.put(message.getImapUid(), message.getImapFlags());
}
@ -1864,7 +1883,7 @@ public abstract class ExchangeSession {
buffer.append("$Forwarded ");
}
if (keywords != null) {
for (String keyword:keywords.split(",")) {
for (String keyword : keywords.split(",")) {
buffer.append(convertKeywordToFlag(keyword)).append(" ");
}
}
@ -1891,7 +1910,7 @@ public abstract class ExchangeSession {
mimeBody.reset();
// workaround for Exchange 2003 ActiveSync bug
if (mimeMessage.getHeader("MAIL FROM") != null) {
mimeBody = (SharedByteArrayInputStream)mimeMessage.getRawInputStream();
mimeBody = (SharedByteArrayInputStream) mimeMessage.getRawInputStream();
mimeMessage = new MimeMessage(null, mimeBody);
mimeBody.reset();
}
@ -1913,7 +1932,7 @@ public abstract class ExchangeSession {
}
public Enumeration getMatchingHeaderLines(String[] headerNames) throws MessagingException, IOException {
Enumeration result = null;
Enumeration result = null;
if (mimeMessage == null) {
// message not loaded, try to get headers only
InputStream headers = getMimeHeaders();
@ -2780,7 +2799,7 @@ public abstract class ExchangeSession {
}
return andCondition;
} catch (ParseException e) {
throw new IOException(e+" "+e.getMessage());
throw new IOException(e + " " + e.getMessage());
}
}