Ejemplo n.º 1
0
        private void OnGalleryImportClick(object sender, EventArgs e)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            Globals.Chem4WordV3.Telemetry.Write(module, "Action", "Triggered");

            try
            {
                int           fileCount = 0;
                StringBuilder sb;

                // Start with V2 Add-In data path
                string importFolder = Path.Combine(Globals.Chem4WordV3.AddInInfo.AppDataPath, @"Chemistry Add-In for Word");
                if (Directory.Exists(importFolder))
                {
                    if (Directory.Exists(Path.Combine(importFolder, "Chemistry Gallery")))
                    {
                        importFolder = Path.Combine(importFolder, "Chemistry Gallery");
                    }
                }
                else
                {
                    importFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                }

                if (Directory.Exists(importFolder))
                {
                    // Fix scrolling to selected item by using code from https://social.msdn.microsoft.com/Forums/expression/en-US/1257aebc-22a6-44f6-975b-74f5067728bc/autoposition-showfolder-dialog?forum=vbgeneral

                    VistaFolderBrowserDialog browser = new VistaFolderBrowserDialog();

                    browser.Description            = "Select a folder to import cml files from";
                    browser.UseDescriptionForTitle = true;
                    browser.RootFolder             = Environment.SpecialFolder.Desktop;
                    browser.ShowNewFolderButton    = false;
                    browser.SelectedPath           = importFolder;
                    DialogResult dr = browser.ShowDialog();

                    if (dr == DialogResult.OK)
                    {
                        string selectedFolder = browser.SelectedPath;
                        string doneFile       = Path.Combine(selectedFolder, "library-import-done.txt");

                        sb = new StringBuilder();
                        sb.AppendLine("Do you want to import the Gallery structures into the Library?");
                        sb.AppendLine("(This cannot be undone.)");
                        dr = AskUserYesNo(sb.ToString());
                        if (dr == DialogResult.Yes)
                        {
                            if (File.Exists(doneFile))
                            {
                                sb = new StringBuilder();
                                sb.AppendLine($"All files have been imported already from '{selectedFolder}'");
                                sb.AppendLine("Do you want to rerun the import?");
                                dr = AskUserYesNo(sb.ToString());
                                if (dr == DialogResult.Yes)
                                {
                                    File.Delete(doneFile);
                                }
                            }
                        }

                        if (dr == DialogResult.Yes)
                        {
                            Progress pb = new Progress();

                            try
                            {
                                var xmlFiles = Directory.GetFiles(selectedFolder, "*.cml");

                                pb.Maximum = xmlFiles.Length;
                                pb.TopLeft = new Point(TopLeft.X + Constants.TopLeftOffset, TopLeft.Y + Constants.TopLeftOffset);
                                pb.Show();

                                foreach (string cmlFile in xmlFiles)
                                {
                                    pb.Message = cmlFile.Replace(selectedFolder, ".");
                                    pb.Increment(1);

                                    var cml = File.ReadAllText(cmlFile);
                                    if (LibraryModel.ImportCml(cml))
                                    {
                                        fileCount++;
                                    }
                                }

                                pb.Hide();
                                pb.Close();

                                File.WriteAllText(doneFile, $"{fileCount} cml files imported into library");
                                FileInfo fi = new FileInfo(doneFile);
                                fi.Attributes = FileAttributes.Hidden;

                                Globals.Chem4WordV3.LibraryNames = LibraryModel.GetLibraryNames();

                                InformUser($"Successfully imported {fileCount} structures from '{selectedFolder}'.");
                            }
                            catch (Exception ex)
                            {
                                pb.Hide();
                                pb.Close();
                                new ReportError(Globals.Chem4WordV3.Telemetry, TopLeft, module, ex).ShowDialog();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, TopLeft, module, ex).ShowDialog();
            }
        }