1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.mercury.maildir.uid;
14
15 import java.io.File;
16 import java.io.IOException;
17
18 import javax.mail.MessagingException;
19 import javax.mail.internet.MimeMessage;
20
21 import org.abstracthorizon.mercury.maildir.MaildirFolderData;
22 import org.abstracthorizon.mercury.maildir.MaildirMessage;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class UIDMaildirMessage extends MaildirMessage implements UIDMessage {
48
49
50 private UID uid;
51
52
53
54
55
56
57
58
59
60 protected UIDMaildirMessage(MaildirFolderData folder, MimeMessage message, int msgnum) throws MessagingException, IOException {
61 super(folder, message, msgnum);
62 }
63
64
65
66
67
68
69
70
71
72 public UIDMaildirMessage(MaildirFolderData folder, File file, int msgnum) throws MessagingException {
73 super(folder, file, msgnum, false);
74
75 String name = file.getName();
76
77 int i = name.indexOf('.');
78 if (i > 0) {
79 int j = name.indexOf('.', i+1);
80 if (name.charAt(i+1) == 'V') {
81 int k = name.indexOf('U', i+1);
82 if ((k > 0) && (k < j)) {
83 try {
84 int folderHash = Integer.parseInt(name.substring(i+2, k));
85 long uidx = Long.parseLong(name.substring(k+1, j));
86 if ((getFolder().hashCode() & 511) == folderHash) {
87 uid = new UID(uidx);
88 }
89 } catch (NumberFormatException ignore) {
90 }
91 }
92 }
93 }
94
95 if (uid == null) {
96 File oldFile = file;
97 String filename = file.getName();
98 String flags = null;
99
100 int j = filename.lastIndexOf(FLAGS_SEPERATOR);
101 if (j >= 0) {
102 flags = filename.substring(j+2);
103 }
104
105 this.file = new File(file.getParentFile(), createFileName(flags));
106 long oldDate = oldFile.lastModified();
107 if (!oldFile.renameTo(this.file)) {
108 throw new MessagingException("Cannot rename "+oldFile.getAbsolutePath()+" to "+this.file.getAbsolutePath());
109 } else {
110 this.file.setLastModified(oldDate);
111 }
112 }
113 initialise();
114 }
115
116
117
118
119
120
121
122 protected String createFileName(String flags) throws MessagingException {
123 String time = Long.toString(System.currentTimeMillis());
124
125 uid = ((UIDMaildirFolderData)folder).getNextUID();
126
127 String folderHash = Integer.toString(getFolder().hashCode() & 511);
128
129 baseName = time + ".V" + folderHash + 'U' + uid.getUID() + '.' + host;
130
131 if ((flags == null) || (flags.length() == 0)) {
132 return baseName;
133 } else {
134 return baseName + infoSeparator + FLAGS_SEPERATOR + flags;
135 }
136 }
137
138
139
140
141
142 public UID getUID() {
143 return uid;
144 }
145
146
147
148
149
150
151
152 public boolean equals(Object o) {
153 if (o instanceof UIDMaildirMessage) {
154 long u1 = uid.getUID();
155 long u2 = ((UIDMaildirMessage)o).getUID().getUID();
156 if ((u1 == u2) && (getFolder().getFullName().equals(((UIDMaildirMessage)o).getFolder().getFullName()))) {
157 return true;
158 }
159 }
160 return false;
161 }
162
163
164
165
166
167
168
169 public int compareTo(MaildirMessage o) {
170 if (o instanceof UIDMaildirMessage) {
171 long u1 = uid.getUID();
172 long u2 = ((UIDMaildirMessage)o).getUID().getUID();
173 if (u1 == u2) {
174 return 0;
175 } else if (u1 < u2) {
176 return -1;
177 } else {
178 return 1;
179 }
180 }
181 return -1;
182 }
183 }