private static void SynchronizeForward(MailsTask task) { Int64 toUid, fromUid; Int32 amountOfMails = 0; toUid = task.NextUidForward; fromUid = MailsTasksHandler.GetFromUid(toUid, task.HighestUidLocal); // En el forward no queremos el HighestUidLocal porque ya lo tenemos task.MailAccount.FetchAndSaveMails(task.Label, fromUid, toUid, ref amountOfMails); task.Dirty = true; task.NextUidForward = MailsTasksHandler.GetFollowingNextUid(fromUid); task.FetchedAmount += amountOfMails; }
private static void SynchronizeBackward(MailsTask task) { Int64 toUid, fromUid; Int32 amountOfMails = 0; toUid = task.NextUidBackward; fromUid = MailsTasksHandler.GetFromUid(toUid, task.LowestUidExternal - 1); // En el backward queremos que traiga el LowestUidExternal task.MailAccount.FetchAndSaveMails(task.Label, fromUid, toUid, ref amountOfMails); task.Dirty = true; task.NextUidBackward = MailsTasksHandler.GetFollowingNextUid(fromUid); task.FetchedAmount += amountOfMails; }
public static void SynchronizeTrash(String mailAddress) { MailAccount mailAccountTrash; MailsTask newTaskTrash; String mailAddressTrash = mailAddress + "/TRASH"; if (MailsTasksHandler.IsWorking(mailAddressTrash)) { return; } using (ISession session = NHibernateManager.OpenSession()) { mailAccountTrash = MailAccount.FindByAddress(mailAddress, session, false); try { mailAccountTrash.ConnectFull(session); } catch (NullReferenceException exc) { Log.LogException(exc, "Direccion inexistente en la base de datos: " + mailAccountTrash + "."); return; } catch (InvalidAuthenticationException exc) { Log.LogException(exc, "No se pudo conectar a imap con la direccion: " + mailAddress + ", ha cambiado el password."); return; } catch (SocketException exc) { Log.LogException(exc, "No se pudo conectar a imap con la direccion: " + mailAddress + "."); return; } Label trashLabel = Label.FindBySystemName(mailAccountTrash, "Trash", session); newTaskTrash = new MailsTask(mailAccountTrash.GetUIDLocal(session, trashLabel.Entity.SystemName, true), mailAccountTrash.GetUIDLocal(session, trashLabel.Entity.SystemName, false), mailAccountTrash.GetUIDExternalFrom(trashLabel.Entity.Name, true), mailAccountTrash.GetUIDExternalFrom(trashLabel.Entity.Name, false), trashLabel, mailAccountTrash); session.Close(); } if (!newTaskTrash.HasFinished) { MailsTasksHandler.LockTasksList(); MailsTasksHandler.TasksList[mailAddressTrash] = newTaskTrash; MailsTasksHandler.UnlockTasksList(); MailsTasksHandler.StartMailsTask(newTaskTrash); } }
private static void SynchronizeAccount(MailsTask task) { try { if (!task.HasFinishedForward) { MailsTasksHandler.SynchronizeForward(task); } else if (!task.HasFinishedBackward) { MailsTasksHandler.SynchronizeBackward(task); } if (task.HasFinished) { MailsTasksHandler.EndSynchronization(task); return; } MailsTasksHandler.StartMailsTask(task); } catch (Exception exc) { Log.LogException(exc, "Error sincronizando cuenta, parametros: mailAccount:" + task.MailAccount.Entity.Address + " lowestUidLocal:" + task.LowestUidLocal.ToString() + " lowestUidExternal:" + task.LowestUidExternal.ToString() + " highestUidLocal:" + task.HighestUidLocal.ToString() + " highestUidExternal:" + task.HighestUidExternal.ToString() + " hasFinished:" + task.HasFinished.ToString() + " hasFinishedForward:" + task.HasFinishedForward.ToString() + " nextUidForward:" + task.NextUidForward.ToString() + " hasFinishedBackward:" + task.HasFinishedBackward.ToString() + " nextUidBackward:" + task.NextUidBackward.ToString() + " labelName:" + task.Label.Entity.Name); if (MailsTasksHandler.forceContinueOnError) { task.SkipCicle(); MailsTasksHandler.StartMailsTask(task); } else { MailsTasksHandler.EndSynchronization(task); throw; } } }
private static void EndSynchronization(MailsTask task) { task.Working = false; task.MailAccount.Disconnect(); }
public static void StartSynchronization(String mailAddress, Boolean forwardOnly = true) { MailAccount mailAccountAll; MailsTask newAllTask; MailsTask newTaskTrash; MailsTask newSpamTask; String mailAddressAll = mailAddress + "/ALL"; String mailAddressTrash = mailAddress + "/TRASH"; String mailAddressSpam = mailAddress + "/SPAM"; if (MailsTasksHandler.IsAnyWorking(new String[] { mailAddressAll, mailAddressTrash, mailAddressSpam })) { return; //alguna tarea se encuentra trabajando } using (ISession session = NHibernateManager.OpenSession()) { mailAccountAll = MailAccount.FindByAddress(mailAddress, session, false); try { mailAccountAll.ConnectFull(session); } catch (NullReferenceException exc) { Log.LogException(exc, "Direccion inexistente en la base de datos: " + mailAccountAll + "."); return; } catch (InvalidAuthenticationException exc) { Log.LogException(exc, "No se pudo conectar a imap con la direccion: " + mailAddress + ", ha cambiado el password."); return; } catch (SocketException exc) { Log.LogException(exc, "No se pudo conectar a imap con la direccion: " + mailAddress + "."); return; } Label allLabel = Label.FindBySystemName(mailAccountAll, "All", session); Label trashLabel = Label.FindBySystemName(mailAccountAll, "Trash", session); Label spamLabel = Label.FindBySystemName(mailAccountAll, "Junk", session); newAllTask = new MailsTask(mailAccountAll.GetUIDLocal(session, allLabel.Entity.SystemName, true), //lastUidLocal mailAccountAll.GetUIDLocal(session, allLabel.Entity.SystemName, false), //firstUidLocal mailAccountAll.GetUIDExternalFrom(allLabel.Entity.Name, true), //lastUidExternal mailAccountAll.GetUIDExternalFrom(allLabel.Entity.Name, false), //firstUidExternal allLabel, mailAccountAll); newTaskTrash = new MailsTask(mailAccountAll.GetUIDLocal(session, trashLabel.Entity.SystemName, true), //lastUidLocal mailAccountAll.GetUIDLocal(session, trashLabel.Entity.SystemName, false), //firstUidLocal mailAccountAll.GetUIDExternalFrom(trashLabel.Entity.Name, true), //lastUidExternal mailAccountAll.GetUIDExternalFrom(trashLabel.Entity.Name, false), //firstUidExternal trashLabel, null); newSpamTask = new MailsTask(mailAccountAll.GetUIDLocal(session, spamLabel.Entity.SystemName, true), //lastUidLocal mailAccountAll.GetUIDLocal(session, spamLabel.Entity.SystemName, false), //firstUidLocal mailAccountAll.GetUIDExternalFrom(spamLabel.Entity.Name, true), //lastUidExternal mailAccountAll.GetUIDExternalFrom(spamLabel.Entity.Name, false), //firstUidExternal spamLabel, null); session.Close(); } if (!newAllTask.HasFinished) //puede ser que no se necesite sincronizar dependiendo de los numeros { if (newAllTask.HighestUidLocal != 0) //si no es la primera vez, que sincronice hacia adelante sin limites { newAllTask.SetUnlimitedForwarding(true); } newAllTask.SetForwardOnly(forwardOnly); MailsTasksHandler.LockTasksList(); MailsTasksHandler.TasksList[mailAddressAll] = newAllTask; MailsTasksHandler.UnlockTasksList(); MailsTasksHandler.StartMailsTask(newAllTask); } if (!newTaskTrash.HasFinished) { MailAccount mailAccountTrash = mailAccountAll.Clone(); //crear otra conexion a IMAP newTaskTrash.SetMailAccount(mailAccountTrash); MailsTasksHandler.LockTasksList(); MailsTasksHandler.TasksList[mailAddressTrash] = newTaskTrash; MailsTasksHandler.UnlockTasksList(); MailsTasksHandler.StartMailsTask(newTaskTrash); } if (!newSpamTask.HasFinished) { MailAccount mailAccountSpam = mailAccountAll.Clone(); //crear otra conexion a IMAP newSpamTask.SetMailAccount(mailAccountSpam); MailsTasksHandler.LockTasksList(); MailsTasksHandler.TasksList[mailAddressSpam] = newSpamTask; MailsTasksHandler.UnlockTasksList(); MailsTasksHandler.StartMailsTask(newSpamTask); } }
private static void StartMailsTask(MailsTask task) { Task.Factory.StartNew(() => SynchronizeAccount(task)); }