private int DisplayPluginControl(UserControl plugin)
        {
            var tabIndex = 0;

            try
            {
                var control       = (Lazy <IXrmToolBoxPlugin, IPluginMetadata>)plugin.Tag;
                var pluginControl = (UserControl)control.Value.GetControl();

                if (service != null)
                {
                    var clonedService = (OrganizationService)currentConnectionDetail.GetOrganizationService();
                    ((OrganizationServiceProxy)clonedService.InnerService).SdkClientVersion = currentConnectionDetail.OrganizationVersion;

                    ((IXrmToolBoxPluginControl)pluginControl).UpdateConnection(clonedService,
                                                                               currentConnectionDetail);
                }

                // ReSharper disable once SuspiciousTypeConversion.Global
                var host = pluginControl as IMessageBusHost;
                if (host != null)
                {
                    host.OnOutgoingMessage += MainForm_MessageBroker;
                }

                ((IXrmToolBoxPluginControl)pluginControl).OnRequestConnection += MainForm_OnRequestConnection;
                ((IXrmToolBoxPluginControl)pluginControl).OnCloseTool         += MainForm_OnCloseTool;

                string name = string.Format("{0} ({1})", control.Metadata.Name,
                                            currentConnectionDetail != null
                        ? currentConnectionDetail.ConnectionName
                        : "Not connected");

                var newTab = new TabPage(name);
                tabControl1.TabPages.Add(newTab);

                pluginControl.Dock   = DockStyle.Fill;
                pluginControl.Width  = newTab.Width;
                pluginControl.Height = newTab.Height;

                newTab.Controls.Add(pluginControl);

                tabIndex = tabControl1.TabPages.Count - 1;

                tabControl1.SelectTab(tabIndex);

                var pluginInOption =
                    currentOptions.MostUsedList.FirstOrDefault(i => i.Name == pluginControl.GetType().FullName);
                if (pluginInOption == null)
                {
                    pluginInOption = new PluginUseCount {
                        Name = pluginControl.GetType().FullName, Count = 0
                    };
                    currentOptions.MostUsedList.Add(pluginInOption);
                }

                pluginInOption.Count++;

                var p1 = plugin as SmallPluginModel;
                if (p1 != null)
                {
                    p1.UpdateCount(pluginInOption.Count);
                }
                else
                {
                    var p2 = plugin as LargePluginModel;
                    if (p2 != null)
                    {
                        p2.UpdateCount(pluginInOption.Count);
                    }
                }

                if (currentOptions.LastAdvertisementDisplay == new DateTime() ||
                    currentOptions.LastAdvertisementDisplay > DateTime.Now ||
                    currentOptions.LastAdvertisementDisplay.AddDays(7) < DateTime.Now)
                {
                    bool displayAdvertisement = true;
                    try
                    {
                        var assembly =
                            Assembly.LoadFile(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory +
                                              "\\McTools.StopAdvertisement.dll");
                        if (assembly != null)
                        {
                            Type type = assembly.GetType("McTools.StopAdvertisement.LicenseManager");
                            if (type != null)
                            {
                                MethodInfo methodInfo = type.GetMethod("IsValid");
                                if (methodInfo != null)
                                {
                                    object classInstance = Activator.CreateInstance(type, null);

                                    if ((bool)methodInfo.Invoke(classInstance, null))
                                    {
                                        displayAdvertisement = false;
                                    }
                                }
                            }
                        }
                    }
                    catch (FileNotFoundException)
                    {
                    }

                    if (displayAdvertisement)
                    {
                        var sc = new SupportScreen(currentReleaseNote);
                        sc.ShowDialog(this);
                        currentOptions.LastAdvertisementDisplay = DateTime.Now;
                    }
                }

                if (currentOptions.AllowLogUsage.HasValue && currentOptions.AllowLogUsage.Value)
                {
                    LogUsage.DoLog(control);
                }

                currentOptions.Save();
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured when trying to display this plugin: " + error.Message, "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(tabIndex);
        }
        private int DisplayPluginControl(Lazy <IXrmToolBoxPlugin, IPluginMetadata> plugin)
        {
            var  tabIndex = 0;
            Guid pluginControlInstanceId = Guid.NewGuid();

            try
            {
                var pluginControl = (UserControl)plugin.Value.GetControl();

                // ReSharper disable once SuspiciousTypeConversion.Global
                var host = pluginControl as IMessageBusHost;
                if (host != null)
                {
                    host.OnOutgoingMessage += MainForm_MessageBroker;
                }

                var statusBarMessager_old = pluginControl as IStatusBarMessager;
                if (statusBarMessager_old != null)
                {
                    statusBarMessager_old.SendMessageToStatusBar += StatusBarMessager_SendMessageToStatusBar;
                }

                var statusBarMessager = pluginControl as IStatusBarMessenger;
                if (statusBarMessager != null)
                {
                    statusBarMessager.SendMessageToStatusBar += StatusBarMessager_SendMessageToStatusBar;
                }

                if (service != null)
                {
                    var crmSvcClient = currentConnectionDetail.GetCrmServiceClient();

                    var clonedService          = crmSvcClient.OrganizationServiceProxy;
                    var clonedWebClientService = crmSvcClient.OrganizationWebProxyClient;
                    if (clonedService != null)
                    {
                        clonedService.SdkClientVersion = currentConnectionDetail.OrganizationVersion;
                    }
                    if (clonedWebClientService != null)
                    {
                        clonedWebClientService.SdkClientVersion = currentConnectionDetail.OrganizationVersion;
                    }

                    var earlyBoundProxiedControl = pluginControl as IEarlyBoundProxy;
                    if (earlyBoundProxiedControl != null)
                    {
                        clonedService?.EnableProxyTypes(earlyBoundProxiedControl.GetEarlyBoundProxyAssembly());
                    }

                    if (clonedService != null)
                    {
                        ((IXrmToolBoxPluginControl)pluginControl).UpdateConnection(clonedService, currentConnectionDetail);
                    }
                    else
                    {
                        ((IXrmToolBoxPluginControl)pluginControl).UpdateConnection(clonedWebClientService, currentConnectionDetail);
                    }
                }

                ((IXrmToolBoxPluginControl)pluginControl).OnRequestConnection += MainForm_OnRequestConnection;
                ((IXrmToolBoxPluginControl)pluginControl).OnCloseTool         += MainForm_OnCloseTool;

                string name = string.Format("{0} ({1})", plugin.Metadata.Name,
                                            currentConnectionDetail != null
                        ? currentConnectionDetail.ConnectionName
                        : "Not connected");

                var newTab = new TabPage(name)
                {
                    Tag = plugin
                };
                tabControl1.TabPages.Add(newTab);

                pluginControl.Dock   = DockStyle.Fill;
                pluginControl.Width  = newTab.Width;
                pluginControl.Height = newTab.Height;
                pluginControl.Tag    = pluginControlInstanceId;
                pluginControl.Parent = newTab;

                newTab.Controls.Add(pluginControl);
                newTab.Controls.Add(new NotificationArea {
                    Name = "NotifPanel", Visible = false, Dock = DockStyle.Top, Parent = newTab
                });

                tabIndex = tabControl1.TabPages.Count - 1;

                tabControl1.SelectTab(tabIndex);

                var pluginInOption = currentOptions.MostUsedList.FirstOrDefault(i => i.Name == plugin.Value.GetType().FullName);
                if (pluginInOption == null)
                {
                    pluginInOption = new PluginUseCount {
                        Name = plugin.Value.GetType().FullName, Count = 0
                    };
                    currentOptions.MostUsedList.Add(pluginInOption);
                }

                pluginInOption.Count++;

                if (currentOptions.LastAdvertisementDisplay == new DateTime() ||
                    currentOptions.LastAdvertisementDisplay > DateTime.Now ||
                    currentOptions.LastAdvertisementDisplay.AddDays(7) < DateTime.Now)
                {
                    bool displayAdvertisement = true;
                    try
                    {
                        var assembly = Assembly.LoadFile(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory +
                                                         "\\McTools.StopAdvertisement.dll");
                        if (assembly != null)
                        {
                            Type type = assembly.GetType("McTools.StopAdvertisement.LicenseManager");
                            if (type != null)
                            {
                                MethodInfo methodInfo = type.GetMethod("IsValid");
                                if (methodInfo != null)
                                {
                                    object classInstance = Activator.CreateInstance(type, null);

                                    if ((bool)methodInfo.Invoke(classInstance, null))
                                    {
                                        displayAdvertisement = false;
                                    }
                                }
                            }
                        }
                    }
                    catch (FileNotFoundException)
                    {
                    }

                    if (displayAdvertisement)
                    {
                        pnlSupport.Visible = true;
                        currentOptions.LastAdvertisementDisplay = DateTime.Now;
                    }
                }

                if (currentOptions.AllowLogUsage.HasValue && currentOptions.AllowLogUsage.Value)
                {
#pragma warning disable CS4014 // Dans la mesure où cet appel n'est pas attendu, l'exécution de la méthode actuelle continue avant la fin de l'appel
                    LogUsage.DoLog(plugin);
#pragma warning restore CS4014 // Dans la mesure où cet appel n'est pas attendu, l'exécution de la méthode actuelle continue avant la fin de l'appel
                }

                currentOptions.Save();

                if (pnlConnectLoading.Visible)
                {
                    pnlConnectLoading.SendToBack();
                    pnlConnectLoading.Visible = false;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured when trying to display this plugin: " + error.Message, "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(tabIndex);
        }
Exemplo n.º 3
0
        private void DisplayPluginControl(UserControl plugin)
        {
            try
            {
                var controlType   = (Type)plugin.Tag;
                var pluginControl =
                    (UserControl)PluginManager.CreateInstance(controlType.Assembly.Location, controlType.FullName);

                if (service != null)
                {
                    var clonedService = new OrganizationService(CrmConnection.Parse(currentConnectionDetail.GetOrganizationCrmConnectionString()));
                    ((OrganizationServiceProxy)clonedService.InnerService).SdkClientVersion = currentConnectionDetail.OrganizationVersion;

                    ((IMsCrmToolsPluginUserControl)pluginControl).UpdateConnection(clonedService,
                                                                                   currentConnectionDetail);
                }

                ((IMsCrmToolsPluginUserControl)pluginControl).OnRequestConnection += MainForm_OnRequestConnection;
                ((IMsCrmToolsPluginUserControl)pluginControl).OnCloseTool         += MainForm_OnCloseTool;

                string name = string.Format("{0} ({1})", pluginControl.GetType().GetTitle(),
                                            currentConnectionDetail != null
                        ? currentConnectionDetail.ConnectionName
                        : "Not connected");

                var newTab = new TabPage(name);
                tabControl1.TabPages.Add(newTab);

                pluginControl.Dock   = DockStyle.Fill;
                pluginControl.Width  = newTab.Width;
                pluginControl.Height = newTab.Height;

                newTab.Controls.Add(pluginControl);

                tabControl1.SelectTab(tabControl1.TabPages.Count - 1);

                var pluginInOption =
                    currentOptions.MostUsedList.FirstOrDefault(i => i.Name == pluginControl.GetType().FullName);
                if (pluginInOption == null)
                {
                    pluginInOption = new PluginUseCount {
                        Name = pluginControl.GetType().FullName, Count = 0
                    };
                    currentOptions.MostUsedList.Add(pluginInOption);
                }

                pluginInOption.Count++;

                var p1 = plugin as SmallPluginModel;
                if (p1 != null)
                {
                    p1.UpdateCount(pluginInOption.Count);
                }
                else
                {
                    var p2 = plugin as PluginModel;
                    if (p2 != null)
                    {
                        p2.UpdateCount(pluginInOption.Count);
                    }
                }

                if (currentOptions.LastAdvertisementDisplay == new DateTime() ||
                    currentOptions.LastAdvertisementDisplay > DateTime.Now ||
                    currentOptions.LastAdvertisementDisplay.AddDays(7) < DateTime.Now)
                {
                    bool displayAdvertisement = true;
                    try
                    {
                        var assembly =
                            Assembly.LoadFile(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory +
                                              "\\McTools.StopAdvertisement.dll");
                        if (assembly != null)
                        {
                            Type type = assembly.GetType("McTools.StopAdvertisement.LicenseManager");
                            if (type != null)
                            {
                                MethodInfo methodInfo = type.GetMethod("IsValid");
                                if (methodInfo != null)
                                {
                                    object classInstance = Activator.CreateInstance(type, null);

                                    if ((bool)methodInfo.Invoke(classInstance, null))
                                    {
                                        displayAdvertisement = false;
                                    }
                                }
                            }
                        }
                    }
                    catch (FileNotFoundException)
                    {
                    }

                    if (displayAdvertisement)
                    {
                        var sc = new SupportScreen(currentReleaseNote);
                        sc.ShowDialog(this);
                        currentOptions.LastAdvertisementDisplay = DateTime.Now;
                    }
                }

                currentOptions.Save();
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured when trying to display this plugin: " + error.Message, "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }