private void tsbInstall_Click(object sender, EventArgs e)
        {
            var packages =
                lvPlugins.CheckedItems.Cast <ListViewItem>()
                .Where(l => l.Tag is XtbPlugin)
                .Select(l => (XtbPlugin)l.Tag)
                .ToList();

            if (packages.Count == 0)
            {
                packages =
                    lvPlugins.SelectedItems.Cast <ListViewItem>()
                    .Where(l => l.Tag is XtbPlugin)
                    .Select(l => (XtbPlugin)l.Tag)
                    .ToList();
            }

            if (packages.Count == 0)
            {
                return;
            }

            var licensedPlugins = packages.Where(p => p.RequireLicenseAcceptance ?? false).ToList();

            if (licensedPlugins.Any())
            {
                var licenseAcceptanceForm = new LicenseAcceptanceForm(licensedPlugins);
                if (licenseAcceptanceForm.ShowDialog(this) != DialogResult.Yes)
                {
                    return;
                }
            }

            tsMain.Enabled    = false;
            lvPlugins.Enabled = false;

            foreach (var p in packages)
            {
                ai.WritePluginEvent(p.Name, p.Version, "Plugin-Install");
            }

            var bw = new BackgroundWorker {
                WorkerReportsProgress = true
            };

            bw.DoWork += (sbw, evt) =>
            {
                var updates = store.PrepareInstallationPackages((List <XtbPlugin>)evt.Argument, (BackgroundWorker)sbw);
                evt.Result = store.PerformInstallation(updates, this);
            };
            bw.ProgressChanged += (sbw, evt) =>
            {
                tssProgress.Style    = ProgressBarStyle.Continuous;
                tssProgress.Visible  = true;
                tssProgress.Value    = evt.ProgressPercentage;
                tssLabel.Visible     = true;
                tssLabel.Text        = $@"Downloading {evt.UserState.ToString()}...";
                tssPluginsCount.Text = string.Empty;
            };
            bw.RunWorkerCompleted += (sbw, evt) =>
            {
                if (!Application.OpenForms.OfType <StoreFormFromPortal>().Any())
                {
                    return;
                }

                tsMain.Enabled      = true;
                lvPlugins.Enabled   = true;
                tssProgress.Visible = false;
                tssLabel.Text       = string.Empty;

                if (evt.Error != null)
                {
                    MessageBox.Show(this, $@"An error occured: {evt.Error.Message}", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if ((bool)evt.Result)
                {
                    // Refresh plugins list when installation is done
                    RefreshPluginsList();
                    MessageBox.Show(this, @"Installation done!", @"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                var size = store.CalculateCacheFolderSize();
                tsbCleanCacheFolder.ToolTipText = $@"Clean XrmToolBox Tool Library cache folder

Current cache folder size: {size}MB";
            };
            bw.RunWorkerAsync(packages);
        }
Exemplo n.º 2
0
        private async void tsbInstall_Click(object sender, EventArgs e)
        {
            var packages =
                lvPlugins.CheckedItems.Cast <ListViewItem>()
                .Where(l => l.Tag is XtbPlugin)
                .Select(l => (XtbPlugin)l.Tag)
                .ToList();

            if (packages.Count == 0)
            {
                packages =
                    lvPlugins.SelectedItems.Cast <ListViewItem>()
                    .Where(l => l.Tag is XtbPlugin)
                    .Select(l => (XtbPlugin)l.Tag)
                    .ToList();
            }

            if (packages.Count == 0)
            {
                return;
            }

            var licensedPlugins = packages.Where(p => p.RequireLicenseAcceptance ?? false).ToList();

            if (licensedPlugins.Any())
            {
                var licenseAcceptanceForm = new LicenseAcceptanceForm(licensedPlugins);
                if (licenseAcceptanceForm.ShowDialog(this) != DialogResult.Yes)
                {
                    return;
                }
            }

            tsMain.Enabled    = false;
            lvPlugins.Enabled = false;

            foreach (var p in packages)
            {
                ai.WritePluginEvent(p.Name, p.Version, "Plugin-Install");
            }

            try
            {
                var updates = await store.PrepareInstallationPackages(packages);

                bool result = store.PerformInstallation(updates, this);

                if (!Application.OpenForms.OfType <StoreFormFromPortal>().Any())
                {
                    return;
                }

                if (result)
                {
                    // Refresh plugins list when installation is done
                    RefreshPluginsList();
                    MessageBox.Show(this, @"Installation done!", @"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                var size = store.CalculateCacheFolderSize();
                tsbCleanCacheFolder.ToolTipText = $@"Clean XrmToolBox Tool Library cache folder

Current cache folder size: {size}MB";
            }
            catch (Exception error)
            {
                MessageBox.Show(this, $@"An error occured: {error.Message}", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                tsMain.Enabled      = true;
                lvPlugins.Enabled   = true;
                tssProgress.Visible = false;
                tssLabel.Text       = string.Empty;
            }
        }