private void DisplayPlugins(object filter = null)
        {
            var isc = new ItSecurityChecker();

            isc.LoadAllowedPlugins();

            if (IsHandleCreated)
            {
                Invoke(new Action(
                           () =>
                {
                    pnlTop.Controls.Clear();

                    if (isc.HasPluginsRestriction)
                    {
                        var secInfo = new PluginsFilterInfo {
                            Dock = DockStyle.Fill
                        };
                        pnlTop.Controls.Add(secInfo);
                    }

                    if (pluginsManager.ValidationErrors?.Any() ?? false)
                    {
                        var invalidInfo = new InvalidPluginsInfo(pluginsManager.ValidationErrors)
                        {
                            Dock = DockStyle.Fill
                        };
                        pnlTop.Controls.Add(invalidInfo);
                    }

                    pnlTop.Visible = pnlTop.Controls.Count > 0;
                }));
            }

            if (!pluginsManager.ValidatedPluginsExt?.Any() ?? false)
            {
                Invoke(new Action(() =>
                {
                    pnlHelp.Visible = true;
                }));

                return;
            }

            var top       = 4;
            int lastWidth = pnlPlugins.Width - 28;

            var categories = pnlCategory.Controls.OfType <LinkLabel>()
                             .Where(ll => ll.LinkColor == Color.Red)
                             .Select(ll => ll.Text)
                             .ToList();

            var availablePlugins = pluginsManager.ValidatedPluginsExt;

            var list = new List <Lazy <IXrmToolBoxPlugin, IPluginMetadataExt> >();

            foreach (var category in categories)
            {
                var matchingPlugins = pluginsManager.ValidatedPluginsExt.Where(p => categoriesList
                                                                               .Where(c => category == c.Key)
                                                                               .SelectMany(c => c.Value)
                                                                               .ToList().Contains(p.Metadata.Name)
                                                                               );

                availablePlugins = availablePlugins.Intersect(matchingPlugins);
            }

            // Search with filter defined
            var filteredPlugins = (filter != null && filter.ToString().Length > 0
                ? availablePlugins.Where(p
                                         => p.Metadata.Name.ToLower().Contains(filter.ToString().ToLower()) ||
                                         p.Metadata.Description.ToLower().Contains(filter.ToString().ToLower()) ||
                                         p.Metadata.Company.ToLower().Contains(filter.ToString().ToLower()))
                : availablePlugins).OrderBy(p => p.Metadata.Name).ToList();

            if (Options.Instance.PluginsDisplayOrder == DisplayOrder.MostUsed)
            {
                foreach (var item in Options.Instance.MostUsedList.OrderByDescending(i => i.Count).ThenBy(i => i.Name))
                {
                    var plugin = filteredPlugins.FirstOrDefault(x => x.Metadata.PluginType == item.Name);
                    if (plugin != null && (Options.Instance.HiddenPlugins == null || !Options.Instance.HiddenPlugins.Contains(plugin.Metadata.Name)))
                    {
                        if (isc.IsPluginAllowed(plugin.Metadata.PluginType))
                        {
                            DisplayOnePlugin(plugin, ref top, lastWidth, item.Count);
                        }
                    }
                }

                foreach (var plugin in filteredPlugins.OrderBy(p => p.Metadata.Name))
                {
                    if (Options.Instance.MostUsedList.All(i => i.Name != plugin.Metadata.PluginType) && (Options.Instance.HiddenPlugins == null || !Options.Instance.HiddenPlugins.Contains(plugin.Metadata.Name)))
                    {
                        if (isc.IsPluginAllowed(plugin.Metadata.PluginType))
                        {
                            DisplayOnePlugin(plugin, ref top, lastWidth);
                        }
                    }
                }
            }
            else if (Options.Instance.PluginsDisplayOrder == DisplayOrder.RecentlyUpdated)
            {
                var pluginAssemblies = Directory.EnumerateFiles(Paths.PluginsPath, "*.dll")
                                       .Select(d => new
                {
                    UpdatedOn = File.GetLastAccessTime(d),
                    FileName  = d.Substring(d.LastIndexOf('\\') + 1)
                })
                                       .OrderByDescending(x => x.UpdatedOn);
                foreach (var pluginAssembly in pluginAssemblies)
                {
                    var plugins =
                        filteredPlugins.Where(
                            x => $"{x.Metadata.AssemblyQualifiedName.Split(',')[1]}.dll".Trim()
                            .Equals(pluginAssembly.FileName, StringComparison.CurrentCultureIgnoreCase))
                        .ToList();
                    foreach (var plugin in plugins)
                    {
                        if (Options.Instance.HiddenPlugins == null ||
                            !Options.Instance.HiddenPlugins.Contains(plugin.Metadata.Name))
                        {
                            if (isc.IsPluginAllowed(plugin.Metadata.PluginType))
                            {
                                DisplayOnePlugin(plugin, ref top, lastWidth);
                            }
                        }
                    }
                }
            }
            else if (Options.Instance.PluginsDisplayOrder == DisplayOrder.Rating)
            {
                if (store == null)
                {
                    store = new StoreFromPortal(Options.Instance.ConnectionControlsAllowPreReleaseUpdates);
                }

                if (store.XrmToolBoxPlugins == null)
                {
                    store.LoadToolsList().Wait();
                }

                var storePlugins = store.XrmToolBoxPlugins.Plugins;

                var filteredList = (from f in filteredPlugins
                                    join s in storePlugins
                                    on f.Metadata.Name equals s.Name
                                    orderby(s.TotalFeedbackRating + 1000000 * s.AverageFeedbackRating) descending
                                    select f).ToList();

                foreach (var plugin in filteredList)
                {
                    if (Options.Instance.HiddenPlugins == null || !Options.Instance.HiddenPlugins.Contains(plugin.Metadata.Name))
                    {
                        if (isc.IsPluginAllowed(plugin.Metadata.PluginType))
                        {
                            DisplayOnePlugin(plugin, ref top, lastWidth);
                        }
                    }
                }
            }
            else
            {
                foreach (var plugin in filteredPlugins.OrderBy(p => p.Metadata.Name))
                {
                    if (Options.Instance.HiddenPlugins == null || !Options.Instance.HiddenPlugins.Contains(plugin.Metadata.Name))
                    {
                        if (isc.IsPluginAllowed(plugin.Metadata.PluginType))
                        {
                            DisplayOnePlugin(plugin, ref top, lastWidth);
                        }
                    }
                }
            }

            Invoke(new Action(() =>
            {
                pnlPlugins.Controls.Clear();

                var pluginsToDisplay = pluginsModels.Where(p => filteredPlugins.Contains((Lazy <IXrmToolBoxPlugin, IPluginMetadataExt>)p.Tag)).ToList();

                foreach (PluginModel ctrl in pluginsToDisplay)
                {
                    ctrl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                    pnlPlugins.Controls.Add(ctrl);
                }

                if (!pluginsToDisplay.Any())
                {
                    var filterCategory = categories.Count == 0 ? "" : $" in categor{(categories.Count > 1 ? "ies" : "y")} {string.Join(", ", categories)}";

                    lblPluginsNotFoundText.Text = string.Format(lblPluginsNotFoundText.Tag.ToString(), filterText, filterCategory);
                    pnlNoPluginFound.Visible    = true;
                    pnlPlugins.Visible          = false;
                }
                else
                {
                    pnlNoPluginFound.Visible = false;
                    pnlPlugins.Visible       = true;
                }

                AdaptPluginControlSize();
            }));
        }
Пример #2
0
        private void DisplayPlugins(object filter = null)
        {
            var isc = new ItSecurityChecker();

            isc.LoadAllowedPlugins();
            Invoke(new Action(() =>
            {
                pnlTop.Controls.Clear();

                if (isc.HasPluginsRestriction)
                {
                    var secInfo = new PluginsFilterInfo {
                        Dock = DockStyle.Fill
                    };
                    pnlTop.Controls.Add(secInfo);
                }

                if (pluginsManager.ValidationErrors?.Any() ?? false)
                {
                    var invalidInfo = new InvalidPluginsInfo(pluginsManager.ValidationErrors)
                    {
                        Dock = DockStyle.Fill
                    };
                    pnlTop.Controls.Add(invalidInfo);
                }

                pnlTop.Visible = pnlTop.Controls.Count > 0;
            }));

            if (!pluginsManager.ValidatedPlugins?.Any() ?? false)
            {
                Invoke(new Action(() =>
                {
                    pnlHelp.Visible = true;
                }));

                return;
            }

            var top       = 4;
            int lastWidth = pnlPlugins.Width - 28;

            // Search with filter defined
            var filteredPlugins = (filter != null && filter.ToString().Length > 0
                ? pluginsManager.ValidatedPlugins.Where(p
                                                        => p.Metadata.Name.ToLower().Contains(filter.ToString().ToLower()) ||
                                                        p.Metadata.Description.ToLower().Contains(filter.ToString().ToLower()) ||
                                                        p.Value.GetType().GetCompany().ToLower().Contains(filter.ToString().ToLower()))
                : pluginsManager.ValidatedPlugins).OrderBy(p => p.Metadata.Name).ToList();

            if (Options.Instance.PluginsDisplayOrder == DisplayOrder.MostUsed)
            {
                foreach (var item in Options.Instance.MostUsedList.OrderByDescending(i => i.Count).ThenBy(i => i.Name))
                {
                    var plugin = filteredPlugins.FirstOrDefault(x => x.Value.GetType().FullName == item.Name);
                    if (plugin != null && (Options.Instance.HiddenPlugins == null || !Options.Instance.HiddenPlugins.Contains(plugin.Metadata.Name)))
                    {
                        if (isc.IsPluginAllowed(plugin.Value.GetType().FullName))
                        {
                            DisplayOnePlugin(plugin, ref top, lastWidth, item.Count);
                        }
                    }
                }

                foreach (var plugin in filteredPlugins.OrderBy(p => p.Metadata.Name))
                {
                    if (Options.Instance.MostUsedList.All(i => i.Name != plugin.Value.GetType().FullName) && (Options.Instance.HiddenPlugins == null || !Options.Instance.HiddenPlugins.Contains(plugin.Metadata.Name)))
                    {
                        if (isc.IsPluginAllowed(plugin.Value.GetType().FullName))
                        {
                            DisplayOnePlugin(plugin, ref top, lastWidth);
                        }
                    }
                }
            }
            else if (Options.Instance.PluginsDisplayOrder == DisplayOrder.RecentlyUpdated)
            {
                var pluginAssemblies = Directory.EnumerateFiles(Paths.PluginsPath, "*.dll")
                                       .Select(d => new
                {
                    UpdatedOn = File.GetLastAccessTime(d),
                    FileName  = d.Substring(d.LastIndexOf('\\') + 1)
                })
                                       .OrderByDescending(x => x.UpdatedOn);
                foreach (var pluginAssembly in pluginAssemblies)
                {
                    var plugins =
                        filteredPlugins.Where(
                            x => $"{x.Value.GetAssemblyQualifiedName().Split(',')[1]}.dll".Trim()
                            .Equals(pluginAssembly.FileName, StringComparison.CurrentCultureIgnoreCase))
                        .ToList();
                    foreach (var plugin in plugins)
                    {
                        if (Options.Instance.HiddenPlugins == null ||
                            !Options.Instance.HiddenPlugins.Contains(plugin.Metadata.Name))
                        {
                            if (isc.IsPluginAllowed(plugin.Value.GetType().FullName))
                            {
                                DisplayOnePlugin(plugin, ref top, lastWidth);
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (var plugin in filteredPlugins.OrderBy(p => p.Metadata.Name))
                {
                    if (Options.Instance.HiddenPlugins == null || !Options.Instance.HiddenPlugins.Contains(plugin.Metadata.Name))
                    {
                        if (isc.IsPluginAllowed(plugin.Value.GetType().FullName))
                        {
                            DisplayOnePlugin(plugin, ref top, lastWidth);
                        }
                    }
                }
            }

            Invoke(new Action(() =>
            {
                pnlPlugins.Controls.Clear();

                var pluginsToDisplay = pluginsModels.Where(p => filteredPlugins.Contains((Lazy <IXrmToolBoxPlugin, IPluginMetadata>)p.Tag)).ToList();

                foreach (PluginModel ctrl in pluginsToDisplay)
                {
                    ctrl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                    pnlPlugins.Controls.Add(ctrl);
                }

                if (!pluginsToDisplay.Any())
                {
                    lblPluginsNotFoundText.Text = string.Format(lblPluginsNotFoundText.Tag.ToString(), filter);
                    pnlNoPluginFound.Visible    = true;
                    pnlPlugins.Visible          = false;
                }
                else
                {
                    pnlNoPluginFound.Visible = false;
                    pnlPlugins.Visible       = true;
                }

                AdaptPluginControlSize();
            }));
        }