Skip to content

Commit cd143c4

Browse files
committed
Graph: Handle special folders based on well known names
git-svn-id: https://svn.code.sf.net/p/davmail/code/trunk@3683 3d1905a2-6b24-0410-a738-b14d5a86fcbd
1 parent d32e000 commit cd143c4

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

src/java/davmail/exchange/ExchangeSession.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public abstract class ExchangeSession {
7070
*/
7171
public static final String CONTACTS = "contacts";
7272
protected static final String ADDRESSBOOK = "addressbook";
73+
protected static final String ARCHIVE = "Archive";
7374
protected static final String INBOX = "INBOX";
7475
protected static final String LOWER_CASE_INBOX = "inbox";
7576
protected static final String MIXED_CASE_INBOX = "Inbox";

src/java/davmail/exchange/graph/GraphExchangeSession.java

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,27 @@ public class GraphExchangeSession extends ExchangeSession {
7272
*/
7373
protected class Folder extends ExchangeSession.Folder {
7474
public FolderId folderId;
75+
protected String specialFlag = "";
76+
77+
protected void setSpecialFlag(String specialFlag) {
78+
this.specialFlag = "\\"+specialFlag+" ";
79+
}
80+
81+
/**
82+
* Get IMAP folder flags.
83+
*
84+
* @return folder flags in IMAP format
85+
*/
86+
@Override
87+
public String getFlags() {
88+
if (noInferiors) {
89+
return specialFlag + "\\NoInferiors";
90+
} else if (hasChildren) {
91+
return specialFlag + "\\HasChildren";
92+
} else {
93+
return specialFlag + "\\HasNoChildren";
94+
}
95+
}
7596
}
7697

7798
// special folders https://learn.microsoft.com/en-us/graph/api/resources/mailfolder
@@ -85,6 +106,17 @@ public enum WellKnownFolderName {
85106
searchfolders
86107
}
87108

109+
// https://www.rfc-editor.org/rfc/rfc6154.html map well known names to special flags
110+
protected static HashMap<String, String> wellKnownFolderMap = new HashMap<>();
111+
static {
112+
wellKnownFolderMap.put(WellKnownFolderName.inbox.name(), ExchangeSession.INBOX);
113+
wellKnownFolderMap.put(WellKnownFolderName.archive.name(), ExchangeSession.ARCHIVE);
114+
wellKnownFolderMap.put(WellKnownFolderName.drafts.name(), ExchangeSession.DRAFTS);
115+
wellKnownFolderMap.put(WellKnownFolderName.junkemail.name(), ExchangeSession.JUNK);
116+
wellKnownFolderMap.put(WellKnownFolderName.sentitems.name(), ExchangeSession.SENT);
117+
wellKnownFolderMap.put(WellKnownFolderName.deleteditems.name(), ExchangeSession.TRASH);
118+
}
119+
88120
protected static final HashSet<FieldURI> IMAP_MESSAGE_ATTRIBUTES = new HashSet<>();
89121

90122
static {
@@ -730,19 +762,32 @@ public void appendTo(StringBuilder buffer) {
730762
}
731763
}
732764

765+
static class NotCondition extends ExchangeSession.NotCondition {
766+
767+
protected NotCondition(Condition condition) {
768+
super(condition);
769+
}
770+
771+
@Override
772+
public void appendTo(StringBuilder buffer) {
773+
buffer.append("not ");
774+
condition.appendTo(buffer);
775+
}
776+
}
777+
733778
@Override
734779
public MultiCondition and(Condition... condition) {
735780
return new MultiCondition(Operator.And, condition);
736781
}
737782

738783
@Override
739784
public MultiCondition or(Condition... condition) {
740-
return null;
785+
return new MultiCondition(Operator.Or, condition);
741786
}
742787

743788
@Override
744789
public Condition not(Condition condition) {
745-
return null;
790+
return new NotCondition(condition);
746791
}
747792

748793
@Override
@@ -917,8 +962,18 @@ private Folder buildFolder(JSONObject jsonResponse) throws IOException {
917962
// calendar
918963
folder.displayName = EwsExchangeSession.encodeFolderName(jsonResponse.optString("name"));
919964
} else {
920-
// TODO: reevaluate folder name encoding over graph
921-
folder.displayName = EwsExchangeSession.encodeFolderName(jsonResponse.getString("displayName"));
965+
String wellKnownName = wellKnownFolderMap.get(jsonResponse.optString("wellKnownName"));
966+
if (ExchangeSession.INBOX.equals(wellKnownName)) {
967+
folder.displayName = wellKnownName;
968+
} else {
969+
if (wellKnownName != null) {
970+
folder.setSpecialFlag(wellKnownName);
971+
}
972+
973+
// TODO: reevaluate folder name encoding over graph
974+
folder.displayName = EwsExchangeSession.encodeFolderName(jsonResponse.getString("displayName"));
975+
}
976+
922977
folder.count = jsonResponse.optInt("totalItemCount");
923978
folder.unreadCount = jsonResponse.optInt("unreadItemCount");
924979
// fake recent value
@@ -1077,7 +1132,7 @@ private FolderId getWellKnownFolderId(String mailbox, WellKnownFolderName wellKn
10771132
*/
10781133
protected FolderId getSubFolderByName(FolderId currentFolderId, String folderName) throws IOException {
10791134
// TODO rename davSearchEncode
1080-
GraphRequestBuilder httpRequestBuilder = null;
1135+
GraphRequestBuilder httpRequestBuilder;
10811136
if ("IPF.Appointment".equals(currentFolderId.folderClass)) {
10821137
httpRequestBuilder = new GraphRequestBuilder()
10831138
.setMethod("GET")

0 commit comments

Comments
 (0)