/// <summary>
        /// Check whether there is an addon that can handle the given action
        /// having the actual addon configuration.
        /// </summary>
        public static bool CanDispatchAction(ActionRequest request, ref bool automatic)
        {
            if (request != null)
            {
                switch (request.ActionType)
                {
                case ActionType.ActionSaveProperties:
                    return(propertyAddon != null);

                case ActionType.ActionBeginEdit:
                    return(SelectPropertyAddon(request.Items) != null);

                case ActionType.ActionBeginPreview:
                    PreviewAddon addon = SelectPreviewAddon(request.Items);

                    if (addon != null)
                    {
                        PreviewBaseCtl previewCtl = addon.AddonPanel as PreviewBaseCtl;
                        if (previewCtl != null)
                        {
                            automatic = previewCtl.SupportAutoPreview;
                            return(true);
                        }
                    }

                    return(false);
                }
            }

            return(false);
        }
示例#2
0
        /// <summary>
        /// Efefctively handles the addon selection.
        /// </summary>
        private Addon InternalSelectAddon(string extension)
        {
            Addon genericAddon     = null;
            Addon specializedAddon = null;

            foreach (KeyValuePair <string, Addon> kv in Addons)
            {
                PreviewAddon addon = kv.Value as PreviewAddon;

                if (addon != null)
                {
                    bool canHandleAllRequiredExtensions = true;
                    if (addon.AddonPanel.HandledFileTypes != null)
                    {
                        if (!addon.AddonPanel.HandledFileTypes.Contains(extension))
                        {
                            // Cannot handle the requested extension.
                            canHandleAllRequiredExtensions = false;
                            continue;
                        }
                    }

                    if (!canHandleAllRequiredExtensions)
                    {
                        // It cannot handle all required extensions.
                        continue;
                    }

                    // Seems it fulfills all conditions.

                    // (addons that do not apply to all file types).
                    if (addon.AddonPanel.HandledFileTypes != null)
                    {
                        // It's specialized (it does not apply to all extensions).
                        specializedAddon = addon;
                        break;
                    }
                    else
                    {
                        // It's not specialized, but maybe there can be found a specialized one.
                        genericAddon = addon;
                        continue;
                    }
                }
            }

            // As much as possbile, try to select specialized addons because they will
            // give more details than the generic ones.
            if (specializedAddon != null)
            {
                return(specializedAddon);
            }
            else
            {
                return(genericAddon);
            }
        }
示例#3
0
        /// <summary>
        /// Loads the preview add-ons
        /// </summary>
        protected override void  Load()
        {
            // Initialize each of preview addons. Careful not to break the loop
            // on eventual exceptions. It's important to try loading as much as
            // possible registered addons.
            if (AddonsConfig.PreviewAddons != null)
            {
                foreach (string addonName in AddonsConfig.PreviewAddons)
                {
                    try
                    {
                        Logger.LogTrace("Loading preview addon: {0} ...", addonName);

                        PreviewAddon navAddon = new PreviewAddon(addonName);
                        Addons.Add(addonName, navAddon);
                    }
                    catch (Exception ex)
                    {
                        ErrorDispatcher.DispatchError($"Could not load addon: {addonName}.\nError: {ex.Message}", false);
                    }
                }
            }
        }
        /// <summary>
        /// Selects the proper addon that can handle the action request.
        /// </summary>
        public static ActionResponse DispatchAction(ActionRequest request)
        {
            ActionResponse response = null;

            switch (request.ActionType)
            {
            case ActionType.ActionSaveProperties:
                if (propertyAddon != null)
                {
                    PropBaseCtl propCtl = (propertyAddon.AddonPanel as PropBaseCtl);
                    if (propCtl != null)
                    {
                        propCtl.SaveProperties();
                    }
                }
                break;

            case ActionType.ActionBeginEdit:
            {
                // Stop editing properties if it's the case.
                if (propertyAddon != null)
                {
                    PropBaseCtl propCtl = (propertyAddon.AddonPanel as PropBaseCtl);
                    if (propCtl != null)
                    {
                        propCtl.EndEdit();
                        propertyAddon = null;
                    }
                }

                propertyAddon = SelectPropertyAddon(request.Items);
                response      = new ActionResponse(request, propertyAddon);
            }
            break;

            case ActionType.ActionBeginPreview:
            {
                // Stop previewing if it's the case.
                if (request.ActionType == ActionType.ActionBeginPreview &&
                    previewAddon != null)
                {
                    PreviewBaseCtl previewCtl = (previewAddon.AddonPanel as PreviewBaseCtl);
                    if (previewCtl != null)
                    {
                        previewCtl.EndPreview();
                        AddonsCore.Instance.FirePreviewEnded();
                        previewAddon = null;
                    }
                }

                previewAddon = SelectPreviewAddon(request.Items);
                response     = new ActionResponse(request, previewAddon);
            }
            break;

            default:
                response = null;
                break;
            }

            return(response);
        }
        /// <summary>
        /// Handles the NavigationAction event of the navigation addon.
        /// </summary>
        void OnNavigationAction(object sender, NavigationActionEventArgs args)
        {
            try
            {
                Logger.LogTrace("MainForm handling OnNavigationAction ...");

                Application.UseWaitCursor = true;
                this.SuspendLayoutEx();

                bool           isVoidAction    = false;
                bool           isPreviewAction = false;
                ActionRequest  request         = new ActionRequest();
                ActionResponse response        = null;

                Logger.LogTrace("Action is of type: " + args.ActionType.ToString());

                pnlPreview.SuspendLayoutEx();
                pnlProperties.SuspendLayoutEx();

                lblNoPreview.Text = Translator.Translate("TXT_THEREARENOITEMS");

                prepareAutoPreview =
                    (args.ActionType == NavActionType.ActionPrepareAutoPreview &&
                     args.ActionType != NavActionType.ActionCancelAutoPreview);

                switch (args.ActionType)
                {
                case NavActionType.ActionSaveProperties:
                    // It is a save property request.
                    request.ActionType = ActionManagement.ActionType.ActionSaveProperties;
                    request.Items      = args.Paths; // don't matter any way
                    break;

                case NavActionType.ActionSelectDirectory:
                case NavActionType.ActionSelectFile:
                case NavActionType.ActionSelectMultipleItems:
                    // It is a property request.
                    request.ActionType = ActionManagement.ActionType.ActionBeginEdit;
                    request.Items      = args.Paths;
                    break;

                case NavActionType.ActionDoubleClickFile:
                    // It is a regular preview request.
                    request.ActionType = ActionManagement.ActionType.ActionBeginPreview;
                    request.Items      = args.Paths;
                    isPreviewAction    = true;
                    break;

                case NavActionType.ActionNotifyNonPreviewableItem:
                    lblNoPreview.Text = Translator.Translate("TXT_NOTIFY_NON_PREVIEW");
                    isPreviewAction   = true;
                    break;

                case NavActionType.ActionNotifyPreviewableItem:
                    lblNoPreview.Text = Translator.Translate("TXT_NOTIFY_PREVIEW");
                    isPreviewAction   = true;
                    break;

                case NavActionType.ActionPrepareAutoPreview:
                    lblNoPreview.Text  = Translator.Translate("TXT_PREPARE_PREVIEW");
                    prepareAutoPreview = true;
                    isPreviewAction    = true;
                    break;

                case NavActionType.ActionCancelAutoPreview:
                    if (args.AdditionalData == null && prepareAutoPreview)
                    {
                        lblNoPreview.Text = Translator.Translate("TXT_PREVIEW_CANCELED");
                    }
                    else
                    {
                        lblNoPreview.Text = Translator.Translate("TXT_NOTIFY_PREVIEW");
                    }
                    isPreviewAction = true;
                    break;

                case NavActionType.ActionReloadNavigation:
                {
                    // Don't reselct addons, send command to the ones already selected.
                    isVoidAction = true;

                    NavBaseCtl ctl = GetNavigationControl();
                    if (ctl != null)
                    {
                        ctl.Reload(args.AdditionalData);
                    }
                }
                break;

                case NavActionType.ActionReloadPreview:
                {
                    // Don't reselct addons, send command to the ones already selected.
                    isVoidAction = true;

                    PreviewBaseCtl ctl = GetPreviewControl();
                    if (ctl != null)
                    {
                        ctl.Reload(args.AdditionalData);
                    }
                }
                break;

                case NavActionType.ActionReloadProperties:
                {
                    // Don't reselct addons, send command to the ones already selected.
                    isVoidAction = true;

                    PropBaseCtl ctl = GetPropertiesControl();
                    if (ctl != null)
                    {
                        ctl.Reload(args.AdditionalData);
                    }
                }
                break;

                case NavActionType.ActionDoubleClickDirectory:
                default:
                    // No actions currently taken in this situation.
                    isVoidAction = true;
                    break;
                }

                if (!isVoidAction)
                {
                    response = AddonsCore.Instance.DispatchAction(request);
                    bool failed = ActionResponse.IsFailedAction(response);

                    if (isPreviewAction)
                    {
                        PreviewBaseCtl previewCtl = GetPreviewControl();
                        if (previewCtl != null)
                        {
                            previewCtl.EndPreview();
                            previewCtl.NavigationAction -=
                                new BaseAddonCtl.NavigationActionEventHandler(OnNavigationAction);
                        }

                        if (failed)
                        {
                            Logger.LogTrace("Preview action has failed.");
                            if (!pnlPreview.Controls.Contains(lblNoPreview))
                            {
                                pnlPreview.Controls.Clear();
                                pnlPreview.Controls.Add(lblNoPreview);
                            }
                        }
                        else
                        {
                            // Note that only a single item can be previewed.
                            PreviewAddon addon = response.TargetAddon as PreviewAddon;
                            if (!pnlPreview.Controls.Contains(addon.AddonPanel))
                            {
                                pnlPreview.Controls.Clear();
                                addon.AddonPanel.Dock     = DockStyle.Fill;
                                addon.AddonPanel.Location = new System.Drawing.Point(10, 10);
                                addon.AddonPanel.TabIndex = 0;
                                pnlPreview.Controls.Add(addon.AddonPanel);
                            }

                            previewCtl = GetPreviewControl();
                            if (previewCtl != null)
                            {
                                previewCtl.NavigationAction +=
                                    new BaseAddonCtl.NavigationActionEventHandler(OnNavigationAction);

                                previewCtl.BeginPreview(args.Paths[0], args.AdditionalData);
                            }
                        }
                    }
                    else
                    {
                        PropBaseCtl propCtl = GetPropertiesControl();
                        if (propCtl != null)
                        {
                            propCtl.NavigationAction -=
                                new BaseAddonCtl.NavigationActionEventHandler(OnNavigationAction);
                        }

                        if (failed)
                        {
                            Logger.LogTrace("View Properties Action has failed.");
                            pnlProperties.Controls.Clear();
                            pnlProperties.Controls.Add(lblNoProperties);
                        }
                        else
                        {
                            PropertyAddon addon = response.TargetAddon as PropertyAddon;
                            if (!pnlProperties.Controls.Contains(addon.AddonPanel))
                            {
                                pnlProperties.Controls.Clear();
                                addon.AddonPanel.Dock = DockStyle.Fill;
                                pnlProperties.Controls.Add(addon.AddonPanel);
                            }

                            propCtl = GetPropertiesControl();
                            if (propCtl != null)
                            {
                                propCtl.NavigationAction +=
                                    new BaseAddonCtl.NavigationActionEventHandler(OnNavigationAction);

                                propCtl.BeginEdit(args.Paths, args.AdditionalData);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorDispatcher.DispatchError(ex, false);
            }
            finally
            {
                pnlPreview.ResumeLayoutEx();
                pnlProperties.ResumeLayoutEx();
                this.ResumeLayoutEx();
                Application.UseWaitCursor = false;

                Logger.LogTrace("MainForm handled OnNavigationAction.");
            }
        }