private void SyncNewMessageHeaders(Account account, string mailboxName, ImapClient connectedImapClient, int firstUid, int lastUid) { Trace.WriteLine("SyncNewMessageHeaders: Mailbox(" + mailboxName + "), UIDs(" + firstUid + ", " + lastUid + ")"); // Decrement this at every exit point. openSyncOps.AddOrUpdate(account.AccountName, 1, (k, v) => v + 1); int messagesCount = lastUid - firstUid + 1; int downloadChunk = this.downloadChunkSize; int startUid = lastUid - downloadChunk + 1; int endUid = lastUid; while (endUid >= firstUid) { int abort = 0; if (abortLatches.TryGetValue(account.AccountName, out abort) && abort == 1) { Trace.WriteLine("Aborting sync..."); openSyncOps.AddOrUpdate(account.AccountName, 0, (k, v) => v - 1); return; } if (startUid < firstUid) { startUid = firstUid; } List <Message> msgs = connectedImapClient.FetchHeaders(startUid, endUid - startUid + 1, true); if (msgs.Count > 0) { foreach (Message msg in msgs) { msg.AccountName = account.AccountName; msg.MailboxPath = mailboxName; if (!MessagesDico.ContainsKey(msg.UniqueKeyString)) { MessagesDico.Add(msg.UniqueKeyString, msg); OnMessageAdded(msg); } } DatabaseManager.StoreMessages(msgs); } endUid = startUid - 1; startUid = endUid - downloadChunk + 1; } openSyncOps.AddOrUpdate(account.AccountName, 0, (k, v) => v - 1); }
private void SyncExistingMessageHeaders(Account account, string mailboxName, ImapClient imapClientOnSelectedStatus, int firstUid, int lastUid) { Trace.WriteLine("SyncExistingMessageHeaders: Mailbox(" + mailboxName + "), UIDs(" + firstUid + ", " + lastUid + ")"); // Decrement this at every exit point. openSyncOps.AddOrUpdate(account.AccountName, 1, (k, v) => v + 1); // Delete all messages that have been deleted from server. List <int> serverUids = new List <int>(); bool searchSuccess = imapClientOnSelectedStatus.SearchUids("ALL", serverUids); if (searchSuccess) { List <string> keysToDelete = new List <string>(); foreach (KeyValuePair <string, Message> entry in MessagesDico) { Message msg = entry.Value; if (msg.AccountName == account.AccountName && msg.MailboxPath == mailboxName && !serverUids.Contains(msg.Uid)) { keysToDelete.Add(entry.Key); } } List <Message> msgsDeleted = new List <Message>(); foreach (string key in keysToDelete) { Message msg = MessagesDico[key]; MessagesDico.Remove(key); OnMessageRemoved(msg); msgsDeleted.Add(msg); } DatabaseManager.DeleteMessages(msgsDeleted); } // Now synchronize the remaining messages. int messagesCount = lastUid - firstUid + 1; int downloadChunk = this.downloadChunkSize; int startUid = lastUid - downloadChunk + 1; int endUid = lastUid; while (endUid >= firstUid) { int abort = 0; if (abortLatches.TryGetValue(account.AccountName, out abort) && abort == 1) { Trace.WriteLine("Aborting sync..."); openSyncOps.AddOrUpdate(account.AccountName, 0, (k, v) => v - 1); return; } if (startUid < firstUid) { startUid = firstUid; } List <Message> serverMsgs = imapClientOnSelectedStatus.FetchHeaders(startUid, endUid - startUid + 1, true); if (serverMsgs.Count > 0) { List <Message> messagesAdded = new List <Message>(); foreach (Message serverMsg in serverMsgs) { Message localMsg; if (MessagesDico.TryGetValue(serverMsg.UniqueKeyString, out localMsg)) { // A local copy exists. Update its flags. if (localMsg.FlagString != serverMsg.FlagString) { localMsg.FlagString = serverMsg.FlagString; OnMessageModified(localMsg); } } else { MessagesDico.Add(serverMsg.UniqueKeyString, serverMsg); OnMessageAdded(serverMsg); messagesAdded.Add(serverMsg); } } DatabaseManager.StoreMessages(messagesAdded); } endUid = startUid - 1; startUid = endUid - downloadChunk + 1; } openSyncOps.AddOrUpdate(account.AccountName, 0, (k, v) => v - 1); }
private void SyncExistingMessageHeaders(Account account, string mailboxName, ImapClient imapClientOnSelectedStatus, int firstUid, int lastUid) { Trace.WriteLine("SyncExistingMessageHeaders: Mailbox(" + mailboxName + "), UIDs(" + firstUid + ", " + lastUid + ")"); // Decrement this at every exit point. openSyncOps.AddOrUpdate(account.AccountName, 1, (k, v) => v + 1); // Delete all messages that have been deleted from server. List<int> serverUids = new List<int>(); bool searchSuccess = imapClientOnSelectedStatus.SearchUids("ALL", serverUids); if (searchSuccess) { List<string> keysToDelete = new List<string>(); foreach (KeyValuePair<string, Message> entry in MessagesDico) { Message msg = entry.Value; if (msg.AccountName == account.AccountName && msg.MailboxPath == mailboxName && !serverUids.Contains(msg.Uid)) { keysToDelete.Add(entry.Key); } } List<Message> msgsDeleted = new List<Message>(); foreach (string key in keysToDelete) { Message msg = MessagesDico[key]; MessagesDico.Remove(key); OnMessageRemoved(msg); msgsDeleted.Add(msg); } DatabaseManager.DeleteMessages(msgsDeleted); } // Now synchronize the remaining messages. int messagesCount = lastUid - firstUid + 1; int downloadChunk = this.downloadChunkSize; int startUid = lastUid - downloadChunk + 1; int endUid = lastUid; while (endUid >= firstUid) { int abort = 0; if (abortLatches.TryGetValue(account.AccountName, out abort) && abort == 1) { Trace.WriteLine("Aborting sync..."); openSyncOps.AddOrUpdate(account.AccountName, 0, (k, v) => v - 1); return; } if (startUid < firstUid) { startUid = firstUid; } List<Message> serverMsgs = imapClientOnSelectedStatus.FetchHeaders(startUid, endUid - startUid + 1, true); if (serverMsgs.Count > 0) { List<Message> messagesAdded = new List<Message>(); foreach (Message serverMsg in serverMsgs) { Message localMsg; if (MessagesDico.TryGetValue(serverMsg.UniqueKeyString, out localMsg)) { // A local copy exists. Update its flags. if (localMsg.FlagString != serverMsg.FlagString) { localMsg.FlagString = serverMsg.FlagString; OnMessageModified(localMsg); } } else { MessagesDico.Add(serverMsg.UniqueKeyString, serverMsg); OnMessageAdded(serverMsg); messagesAdded.Add(serverMsg); } } DatabaseManager.StoreMessages(messagesAdded); } endUid = startUid - 1; startUid = endUid - downloadChunk + 1; } openSyncOps.AddOrUpdate(account.AccountName, 0, (k, v) => v - 1); }
private void SyncNewMessageHeaders(Account account, string mailboxName, ImapClient imapClientOnSelectedStatus, int firstUid, int lastUid) { Trace.WriteLine("SyncNewMessageHeaders: Mailbox(" + mailboxName + "), UIDs(" + firstUid + ", " + lastUid + ")"); // Decrement this at every exit point. openSyncOps.AddOrUpdate(account.AccountName, 1, (k, v) => v + 1); int messagesCount = lastUid - firstUid + 1; int downloadChunk = this.downloadChunkSize; int startUid = lastUid - downloadChunk + 1; int endUid = lastUid; while (endUid >= firstUid) { int abort = 0; if (abortLatches.TryGetValue(account.AccountName, out abort) && abort == 1) { Trace.WriteLine("Aborting sync..."); openSyncOps.AddOrUpdate(account.AccountName, 0, (k, v) => v - 1); return; } if (startUid < firstUid) { startUid = firstUid; } List<Message> msgs = imapClientOnSelectedStatus.FetchHeaders(startUid, endUid - startUid + 1, true); if (msgs.Count > 0) { foreach (Message msg in msgs) { if (!MessagesDico.ContainsKey(msg.UniqueKeyString)) { MessagesDico.Add(msg.UniqueKeyString, msg); OnMessageAdded(msg); } } DatabaseManager.StoreMessages(msgs); } endUid = startUid - 1; startUid = endUid - downloadChunk + 1; } openSyncOps.AddOrUpdate(account.AccountName, 0, (k, v) => v - 1); }