예제 #1
0
        private void UpdateBestFolderWrapper(FolderWrapper value)
        {
            _bestFolderWrapper = value;
            // Update Panel
            control.RefreshSelection(_bestFolderWrapper); // null will be ignored.

            // Update Ribbon
            RibbonButton button = null;

            if (explorer != null)
            {
                button = Globals.Ribbons[explorer].ExplorerRibbon?.guessButton;
            }
            else
            {
                // The ribbon is not loaded until after the New Inspector event.
                button = Globals.Ribbons[inspector].MailReadRibbon?.guessButton;
            }
            if (button != null)
            {
                if (_bestFolderWrapper?.folder == null)
                {
                    button.Label   = "Move";
                    button.Enabled = false;
                }
                else
                {
                    button.Label   = _bestFolderWrapper.folder.Name;
                    button.Enabled = true;
                }
            }
        }
예제 #2
0
        public async void UpdateFolderListAsync()
        {
            if (folderTree is null)
            {
                Outlook.Folder root = Globals.ThisAddIn.Application.Session.DefaultStore.GetRootFolder() as Outlook.Folder;
                // or loop over Application.Session.Stores

                folderTree        = new FolderWrapper(root, null, await GetDefaultFoldersCachedAsync(false));
                foldersCollection = new ObservableCollection <FolderWrapper>(); //Change to incremental update later
            }
            else
            {
                // Clear collection
                while (foldersCollection.Count > 0)
                {
                    foldersCollection.RemoveAt(foldersCollection.Count - 1);
                }
            }

            // Build or re-build collection
            foreach (FolderWrapper fw in folderTree.Flattened())
            {
                foldersCollection.Add(fw);
            }

            // Update selection on control
            foreach (var pair in TaskPaneContexts)
            {
                pair.Value.Refresh();
            }
        }
예제 #3
0
        internal void RefreshSelection(FolderWrapper folder = null)
        {
            //listCollectionView.Refresh();
            this.Dispatcher.Invoke(() => // Must be on main thread
            {
                if (folder != null)
                {
                    listBox.SelectedItem = folder;
                }

                if (listBox.SelectedIndex < 0)
                {
                    listBox.SelectedIndex = 0;
                }
            });
        }
예제 #4
0
        public async void Folders_FolderAdd(Outlook.MAPIFolder new_folder)
        {
            try
            {
                Logger.Debug("FolderAdd Starting");

                FolderWrapper fw = new FolderWrapper(new_folder as Outlook.Folder, this, await Globals.ThisAddIn.GetDefaultFoldersCachedAsync(false));
                //children.Insert(0,fw);
                //collection.Insert(collection.IndexOf(this) + 1, fw);

                Globals.ThisAddIn.UpdateFolderListAsync();

                Logger.Debug("FolderAdd Done.");
            }
            catch (Exception err)
            {
                MessageBox.Show("Unexpected error processing FolderAdd.\n" + err.Message, "Fast File Error");
                Logger.Error(err, "Unexpected error processing FolderAdd.");
            }
        }
예제 #5
0
        public FolderWrapper(Outlook.Folder folder, FolderWrapper parent = null, List <Outlook.Folder> blacklist = null)
        {
            this.folder = folder;

            this.parent  = parent;
            this.folders = folder.Folders;

            depth = 0;
            path  = "\\";
            var p = folder.Parent;

            while (p is Outlook.Folder)
            {
                depth += 1;
                String tmpPath = (p as Outlook.Folder).Name;
                p = (p as Outlook.Folder).Parent;
                if (p is Outlook.Folder)
                {
                    path = "\\" + tmpPath + path;
                }
            }

            children = new List <FolderWrapper>(folders.Count);
            foreach (Outlook.Folder child in folders)
            {
                if (blacklist != null && blacklist.FindIndex(f => f.EntryID == child.EntryID) >= 0)
                {
                    continue;
                }
                var fw = new FolderWrapper(child, this, blacklist);
                children.Add(fw);
            }

            // Listeners
            folders.FolderAdd    += new Outlook.FoldersEvents_FolderAddEventHandler(Folders_FolderAdd);
            folders.FolderChange += new Outlook.FoldersEvents_FolderChangeEventHandler(Folders_FolderChange);
            folders.FolderRemove += new Outlook.FoldersEvents_FolderRemoveEventHandler(Folders_FolderRemove);
        }
예제 #6
0
        internal async Task GuessBestFolder(bool yieldPeriodically = false)
        {
            if (Globals.ThisAddIn.foldersCollection == null)
            {
                // Plugin is still initializing.
                return;
            }

            // Which folder contains the most messages from the conversation?
            Dictionary <String, Tuple <Outlook.Folder, int> > folderVotes = new Dictionary <String, Tuple <Outlook.Folder, int> >();

            void processItem(Outlook.MailItem mailItem)
            {
                Outlook.Conversation conv        = null;
                Outlook.SimpleItems  simpleItems = null;
                try
                {
                    conv = mailItem.GetConversation();
                    if (conv != null)
                    {
                        simpleItems = conv.GetRootItems();
                    }
                }
                catch (COMException)
                {
                    // GetConversation is supposed to return null if there is no converstaion but actually throws and exception.
                    // GetRootItems throws an error for come conversations that have meeting invitations.
                }

                if (simpleItems != null)
                {
                    // Obtain root items and enumerate the conversation.
                    EnumerateConversation(simpleItems, conv);
                }
            }

            void EnumerateConversation(Outlook.SimpleItems items, Outlook.Conversation conversation)
            {
                if (items.Count > 0)
                {
                    foreach (object myItem in items)
                    {
                        if (myItem is Outlook.MailItem)
                        {
                            Outlook.MailItem mailItem = myItem as Outlook.MailItem;
                            Outlook.Folder   inFolder = mailItem.Parent as Outlook.Folder;

                            if (!folderVotes.TryGetValue(inFolder.EntryID, out Tuple <Outlook.Folder, int> value))
                            {
                                value = new Tuple <Outlook.Folder, int>(inFolder, 0);
                            }
                            folderVotes[inFolder.EntryID] = new Tuple <Outlook.Folder, int>(inFolder, value.Item2 + 1);
                        }
                        // Continue recursion.
                        Outlook.SimpleItems children;
                        try
                        {
                            children = conversation.GetChildren(myItem);
                        }
                        catch (COMException err)
                        {
                            var subject = myItem is Outlook.MailItem ? (myItem as Outlook.MailItem).Subject : "<Unknown item>";
                            Logger.Error(err, "Unable to get conversation children for subject {subject}", subject);
                            // I see this with Drafts, meeting invites, and other times.
                            continue;
                        }
                        EnumerateConversation(children, conversation);
                    }
                }
            }

            foreach (Outlook.MailItem mailItem in GetSelectedMailItems())
            {
                processItem(mailItem);
                if (yieldPeriodically)
                {
                    await Dispatcher.Yield(DispatcherPriority.Background);

                    if (guessBestFolderQueued)
                    {
                        return;
                    }
                }
            }

            // Remove distracting folders from consideration.
            var folderBlacklist = new List <Outlook.Folder>(await Globals.ThisAddIn.GetDefaultFoldersCachedAsync(false));

            if (explorer != null)
            {
                folderBlacklist.Add(explorer.CurrentFolder as Outlook.Folder);
            }
            else // inspector
            {
                if (inspector.CurrentItem is Outlook.MailItem)
                {
                    var mailItem = inspector.CurrentItem as Outlook.MailItem;
                    if (mailItem.Parent is Outlook.Folder)
                    {
                        folderBlacklist.Add(mailItem.Parent as Outlook.Folder);
                    }
                }
            }

            // Select best folder
            var sortedFolders = folderVotes.OrderBy(key => - key.Value.Item2);

            Outlook.Folder bestFolder = null;
            foreach (var v in sortedFolders)
            {
                Outlook.Folder folder = v.Value.Item1;
                if (folderBlacklist.FindIndex(f => f.EntryID == folder.EntryID) >= 0)
                {
                    // on blacklist
                    continue;
                }
                bestFolder = folder;
                break;
            }

            // Return folder wrapper
            FolderWrapper best = null;

            if (bestFolder != null)
            {
                try
                {
                    best = Globals.ThisAddIn.foldersCollection.Single(fw => fw.folder.EntryID == bestFolder.EntryID);
                }
                catch (InvalidOperationException err)
                {
                    Logger.Error(err, "Unable to find folder {folderName}.", bestFolder.Name);
                }
            }

            UpdateBestFolderWrapper(best);
        }