Exemple #1
0
        public override Dictionary <long, string> Synchronize(FolderCollection foldersTree)
        {
            DbStorage        dbStorage = DbStorageCreator.CreateDatabaseStorage(_account);
            FolderCollection folders   = new FolderCollection();

            FolderCollection.CreateFolderListFromTree(ref folders, foldersTree);
            ArrayList pop3Uids             = new ArrayList();
            bool      downloadErrorOccured = false;

            Folder fld = folders[FolderType.Inbox];

            if (fld != null)
            {
                if (fld.SyncType == FolderSyncType.DirectMode)
                {
                    if (!_updatedFolders.ContainsKey(fld.ID))
                    {
                        _updatedFolders.Add(fld.ID, fld.FullPath);
                    }
                    return(_updatedFolders);
                }
                if (fld.SyncType == FolderSyncType.DontSync)
                {
                    return(_updatedFolders);
                }

                try
                {
                    dbStorage.Connect();

                    // get new messages start index
                    string[]  pop3DbUids     = dbStorage.GetUids();
                    string[]  pop3ServerUids = _pop3Obj.GetMessageUids();
                    ArrayList dbUidsToDelete = new ArrayList();
                    ArrayList pop3ServerIndexesToDownload = new ArrayList();

                    foreach (string pop3DbUid in pop3DbUids)
                    {
                        if (Array.IndexOf(pop3ServerUids, pop3DbUid) == -1)
                        {
                            dbUidsToDelete.Add(pop3DbUid);
                        }
                    }

                    foreach (string pop3ServerUid in pop3ServerUids)
                    {
                        if (Array.IndexOf(pop3DbUids, pop3ServerUid) == -1)
                        {
                            pop3ServerIndexesToDownload.Add(_pop3Obj.GetMessageIndexFromUid(pop3ServerUid));
                        }
                    }

                    if ((_account.MailMode == MailMode.KeepMessagesOnServer) ||
                        (_account.MailMode == MailMode.KeepMessagesOnServerAndDeleteMessageWhenItsRemovedFromTrash))
                    {
                        object[] serverUidsToDelete = dbStorage.GetOldMessagesUids(_account.MailsOnServerDays);
                        // delete from server
                        if (serverUidsToDelete.Length > 0)
                        {
                            DeleteMessages(serverUidsToDelete, fld);
                        }
                    }
                    // retrieve new messages
                    GetNewMessagesFromServerAndSaveToDb(dbStorage, fld, (int[])pop3ServerIndexesToDownload.ToArray(typeof(int)), out downloadErrorOccured, ref pop3Uids);

                    if ((fld.SyncType == FolderSyncType.NewEntireMessages) ||
                        (fld.SyncType == FolderSyncType.NewHeadersOnly))
                    {
                        return(_updatedFolders);
                    }
                    // delete messages from db
                    if (dbUidsToDelete.Count > 0)
                    {
                        WebMailMessageCollection msgsToDelete = dbStorage.LoadMessagesByUids((string[])dbUidsToDelete.ToArray(typeof(string)), fld, false);
                        dbStorage.DeleteMessages(msgsToDelete.ToIDsCollection(), fld);
                        if (!_updatedFolders.ContainsKey(fld.ID))
                        {
                            _updatedFolders.Add(fld.ID, fld.FullPath);
                        }
                    }
                }
                finally
                {
                    if (!downloadErrorOccured)
                    {
                        // get all uids and save it to db
                        pop3Uids = ArrayList.Adapter(_pop3Obj.GetMessageUids());
                        dbStorage.ReplaceUids(pop3Uids.ToArray());
                    }
                    else
                    {
                        dbStorage.SaveUids(pop3Uids.ToArray());
                    }
                    dbStorage.Disconnect();
                }
            }
            return(_updatedFolders);
        }