Example #1
0
        private void BeforeItemMove(object item, Outlook.MAPIFolder moveto, ref bool cancel)
        {
            try
            {
                var mailItem = (Outlook._MailItem)item;
                if (mailItem == null)
                {
                    return;
                }

                // get the item id
                var itemId = mailItem.EntryID;

                // are we currently processing this item?
                if (_mailProcessor.IsProccessing(itemId))
                {
                    return;
                }

                // is it something we even care about
                if (!_mailProcessor.IsUsableClassNameForClassification(mailItem.MessageClass))
                {
                    return;
                }

                // is it permanently deleted?
                if (moveto == null)
                {
                    _logger.LogInformation($"Mail '{mailItem.Subject}' was permanently deleted.");
                    return;
                }

                // do we want to use the message for training?
                if (!_options.ReAutomaticallyTrainMoveMessages)
                {
                    // log that we found nothing.
                    _logger.LogInformation($"Mail '{mailItem.Subject}' was manually moved to folder '{moveto.Name}' but the option is set not to use this for training.");

                    // done
                    return;
                }

                // get the new folder id
                var folderId = moveto.EntryID;

                // and guess where we might be wanting to go.
                var posibleCategories = _categories.FindCategoriesByFolderId(folderId).ToList();
                if (!posibleCategories.Any())
                {
                    // log that we found nothing.
                    _logger.LogInformation($"Mail '{mailItem.Subject}' was manually moved to folder '{moveto.Name}' but I found no matching categories to classify it with.");

                    // if we cannot guess a valid category
                    // then there is nothing we can do.
                    return;
                }

                // log it
                _logger.LogVerbose($"About to move mail '{mailItem.Subject}' to folder '{moveto.Name}' and I found {posibleCategories.Count} posible categories.");

                // we found one or more posible, valid categories.
                // if we have more than one, then we have to ask the user to pick.
                // but if we only have one, then no need to ask anything always select the one.
                if (posibleCategories.Count == 1)
                {
                    // get the one item we selected.
                    var category = posibleCategories.First();

                    // we know this is a user selected item
                    // so we can get the weight from the options.
                    TasksController.Add(_mailProcessor.ClassifyAsync(itemId, category.Id, _options.UserWeight));

                    // log it
                    _logger.LogInformation($"Mail '{mailItem.Subject}' was manually moved to folder '{moveto.Name}' and classified as '{category.Name}'");

                    // done
                    return;
                }

                // do we want to be asked for multiple categories?
                if (!_options.ReConfirmMultipleTainingCategory)
                {
                    // log that we found nothing.
                    _logger.LogInformation($"Mail '{mailItem.Subject}' was manually moved to folder '{moveto.Name}' but the option is set not to use this for training as there were more than one category to choose.");

                    return;
                }

                // we have more than one item
                using (var chooseCategoriesForm = new ChooseCategoryForm(posibleCategories, _categories, _options))
                {
                    if (chooseCategoriesForm.ShowDialog() != DialogResult.OK)
                    { // log it
                        _logger.LogInformation($"Mail '{mailItem.Subject}' was manually moved to folder '{moveto.Name}' but was not classified as the user pressed 'Cancel'.");

                        // done
                        return;
                    }

                    // get the selected category.
                    var category = chooseCategoriesForm.SelectedCategory;
                    if (null == category)
                    { // log it
                        _logger.LogInformation($"Mail '{mailItem.Subject}' was manually moved to folder '{moveto.Name}' but was not classified as the user did not choose a category.");

                        // done
                        return;
                    }

                    // the user selected an item
                    TasksController.Add(_mailProcessor.ClassifyAsync(itemId, category.Id, _options.UserWeight));

                    // log it
                    _logger.LogInformation($"Mail '{mailItem.Subject}' was manually moved to folder '{moveto.Name}' and the user chose to have it classified as '{category.Name}'");
                }
            }
            catch (Exception e)
            {
                _logger.LogException(e);
            }
        }