change seen within a transaction in FileBasedMailbox
[org.ibex.mail.git] / src / org / ibex / mail / target / FileBasedMailbox.java
index daac252..4322673 100644 (file)
@@ -78,18 +78,7 @@ public class FileBasedMailbox extends Mailbox.Default {
             } }.start();
         }
 
-        public static class Drop implements Transaction {
-            int uid;
-            public Drop(int uid) { this.uid = uid; }
-            public void executeOn(Object o, Date now) {
-                Cache c = (Cache)o;
-                Entry e = c.get(uid);
-                c.byname.remove(e.name);
-                c.linear.remove(e);
-                c.byuid.remove(uid);
-            } 
-        }
-        
+       
         public synchronized int size() { return linear.size(); }
         public synchronized int uidNext(boolean increment) { return increment ? uidNext++ : uidNext; }
         public synchronized Entry get(int uid) { return byuid.get(uid); }
@@ -108,13 +97,14 @@ public class FileBasedMailbox extends Mailbox.Default {
             public MIME.Headers headers() { return new MIME.Headers(new Stream(new ByteArrayInputStream(header)), true); }
             public Entry(Cache cache, Prevayler prevayler, String name) throws IOException {
                 File f = new File(cache.dir.getParent()+slash+name);
-                seen = f.lastModified() == MAGIC_DATE;
+                final boolean seen = f.lastModified() == MAGIC_DATE;
                 this.name = name;
                 header = new MIME.Headers(new Stream(new FileInputStream(f)), true).toString().getBytes();
                 prevayler.execute(new Transaction() {
                         public void executeOn(Object o, Date now) {
                             Cache cache = (Cache)o;
                             synchronized(cache) {
+                                Entry.this.seen = seen;
                                 Entry.this.uid = cache.uidNext(true);
                                 cache.linear.add(Entry.this);
                                 cache.byuid.put(Entry.this.uid, Entry.this);
@@ -125,11 +115,10 @@ public class FileBasedMailbox extends Mailbox.Default {
             public int uid() { return uid; }
             public boolean seen() { return seen; }
             public void seen(Cache cache, boolean seen) {
-                this.seen = seen;
                 String base = cache.dir.getParent() + slash + name;
                 File target = new File(base);
                 if (!target.exists()) target = new File(base + "s");
-                target.setLastModified(seen ? System.currentTimeMillis() : MAGIC_DATE);
+                target.setLastModified(seen ? MAGIC_DATE : System.currentTimeMillis());
             }
             public void delete(Cache cache) {
                 String base = cache.dir.getParent() + slash + name;
@@ -201,13 +190,14 @@ public class FileBasedMailbox extends Mailbox.Default {
         Log.info(path, message.summary());
         try {
             String name; String fullname; File target; File f;
-            do {
+            while(true) {
                 name = cache.uidNext(true) + ".";
                 fullname = path.getAbsolutePath() + slash + name;
                 target = new File(fullname);
                 f = new File(target.getCanonicalPath() + "-");
+                if (!f.exists() && !target.exists()) break;
                 Log.error(this, "aieeee!!!! target of add() already exists: " + target.getAbsolutePath());
-            } while (f.exists() || target.exists());
+            }
             FileOutputStream fo = new FileOutputStream(f);
             Stream stream = new Stream(fo);
             if (message.envelope != null) {
@@ -232,19 +222,31 @@ public class FileBasedMailbox extends Mailbox.Default {
         public int num() { return cur+1; }  // EUDORA insists that message numbers start at 1, not 0
         public int uid() { return done() ? -1 : entry().uid(); }
         public Message cur() { return done() ? null : entry().message(cache); }
-        public void seen(boolean seen) { prevayler.execute(new Seen(uid(), seen)); }
-        public void delete() { if (!done()) prevayler.execute(new Delete(uid())); }
+        public void seen(boolean seen) {
+            entry().seen((Cache)prevayler.prevalentSystem(), seen);
+            prevayler.execute(new Seen(uid(), seen));
+        }
+        public void delete() {
+            entry().delete((Cache)prevayler.prevalentSystem());
+            prevayler.execute(new Drop(entry().uid()));
+        }
     }
 
-    private static class Delete implements Transaction {
-        private int uid;
-        public Delete(int uid) { this.uid = uid; }
-        public void executeOn(Object c, Date d) { ((Cache)c).get(uid).delete((Cache)c); }
-    }
     private static class Seen implements Transaction {
         private int uid;
         private boolean seen;
         public Seen(int uid, boolean seen) { this.uid = uid; this.seen = seen; }
-        public void executeOn(Object c, Date d) { ((Cache)c).get(uid).seen((Cache)c, seen); }
+        public void executeOn(Object c, Date d) { ((Cache)c).get(uid).seen = seen; }
+    }
+    public static class Drop implements Transaction {
+        int uid;
+        public Drop(int uid) { this.uid = uid; }
+        public void executeOn(Object o, Date now) {
+            Cache c = (Cache)o;
+            Cache.Entry e = c.get(uid);
+            c.byname.remove(e.name);
+            c.linear.remove(e);
+            c.byuid.remove(uid);
+        } 
     }
 }