private bool CheckFolder()
        {
            string folder = BuildFolder();

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            DirectoryInfo dir1 = new DirectoryInfo(folder);
            DirectoryInfo dir0 = new DirectoryInfo(TaxonUtils.GetTaxonPath());

            return(dir0.FullName.ToLower() != dir1.FullName.ToLower());
        }
 private void buttonBrowse_Click(object sender, EventArgs e)
 {
     using (var fbd = new FolderBrowserDialog())
     {
         fbd.Description  = "Select Folder where new database will be saved";
         fbd.SelectedPath = TaxonUtils.GetTaxonPath();
         DialogResult result = fbd.ShowDialog();
         if (result != DialogResult.OK || string.IsNullOrWhiteSpace(fbd.SelectedPath))
         {
             return;
         }
         labelFolder.Text = fbd.SelectedPath;
     }
 }
 public GenerateNewDatabaseDialog(TaxonTreeNode _taxon, GenerateNewDatabaseConfig _config)
 {
     InitializeComponent();
     _Taxon  = _taxon;
     _Config = _config;
     if (_Taxon == null)
     {
         return;
     }
     labelTaxon.Text = _Taxon.Desc.RefMainName;
     labelTaxon.Tag  = new Localization.Tag {
         Ignore = true
     };
     labelFolder.Text = Path.Combine(TaxonUtils.GetTaxonPath(), "NewDatas");
     ApplyConfig();
 }
        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            if (!CheckFolder())
            {
                return;
            }

            using (ProgressDialog progressDlg = new ProgressDialog())
            {
                progressDlg.StartPosition = FormStartPosition.CenterScreen;
                progressDlg.Show();

                string folder   = BuildFolder();
                string filename = labelTaxon.Text + ".tol";

                string logFile     = Path.Combine(folder, "generate.log");
                string treeFileTol = Path.Combine(folder, filename);
                string treeFileXml = Path.ChangeExtension(treeFileTol, "xml");

                bool comments   = checkBoxExportComments.Checked;
                bool photos     = checkBoxExportPhotos.Checked;
                bool sounds     = checkBoxExportSounds.Checked;
                bool exe        = checkBoxExe.Checked;
                bool ascendants = checkBoxExportAscendants.Checked;


                using (StreamWriter log = new StreamWriter(logFile))
                {
                    log.WriteLine("Start generate data for ");
                    log.WriteLine("  taxon   : " + _Taxon.Desc.RefMainName);
                    log.WriteLine("  in graph: " + TaxonUtils.MainGraph?.Description);

                    TaxonTreeNode nodeToExport = _Taxon;
                    if (ascendants)
                    {
                        TaxonTreeNode Current = _Taxon;
                        while (Current != null && Current.Father != null)
                        {
                            TaxonTreeNode fatherNode = new TaxonTreeNode();
                            fatherNode.Father = Current.Father.Father;
                            fatherNode.Desc   = Current.Father.Desc;
                            fatherNode.Children.Add(nodeToExport);
                            nodeToExport = fatherNode;
                            Current      = Current.Father;
                        }
                    }

                    ProgressItem piSaveData = progressDlg.Add("Saving main file", null, 0, 3);
                    log.WriteLine("");
                    log.WriteLine("Save tree in " + treeFileXml);
                    TaxonUtils.Save(nodeToExport, treeFileXml);
                    piSaveData.Update(1);
                    log.WriteLine("Save tree in " + treeFileTol);
                    TaxonUtils.Save(nodeToExport, treeFileTol);
                    piSaveData.Update(2);

                    int count = 0;
                    nodeToExport.ParseNode((node) => { count++; });
                    piSaveData.Update(3);
                    piSaveData.End();

                    log.WriteLine("");
                    log.WriteLine("Total nodes    : " + count);
                    log.WriteLine("Export ascendants: " + ascendants);
                    log.WriteLine("Export comments: " + comments);
                    log.WriteLine("Export images  : " + photos);
                    log.WriteLine("Export sounds  : " + sounds);

                    ExportData exportData = new ExportData(TaxonUtils.GetTaxonPath(), folder, log);

                    if (comments)
                    {
                        log.WriteLine("");
                        log.WriteLine("Exporting comments");
                        exportData.ProgressItem = progressDlg.Add("Exporting comments", "", 0, count - 1);
                        nodeToExport.ParseNode((node) => ExportComments(node, exportData));
                        exportData.ProgressItem.End();
                    }

                    if (photos)
                    {
                        log.WriteLine("");
                        log.WriteLine("Exporting photos");
                        exportData.ProgressItem = progressDlg.Add("Exporting photos", "", 0, count - 1);
                        nodeToExport.ParseNodeDesc((desc) => ExportPhotos(desc, exportData));
                        exportData.ProgressItem.End();
                    }

                    if (sounds)
                    {
                        log.WriteLine("");
                        log.WriteLine("Exporting sounds");
                        exportData.ProgressItem = progressDlg.Add("Exporting sounds", "", 0, count - 1);
                        nodeToExport.ParseNodeDesc((desc) => ExportSounds(desc, exportData));
                        exportData.ProgressItem.End();
                    }

                    if (exe)
                    {
                        exportData.ProgressItem = progressDlg.Add("Exporting exe/config", "", 0, 2);
                        string sourceExe = Assembly.GetEntryAssembly().Location;
                        string destExe   = Path.Combine(exportData.NewPath, Path.GetFileName(sourceExe));
                        File.Copy(sourceExe, destExe);
                        exportData.ProgressItem.Update(1);

                        string sourceConfig = TaxonUtils.GetConfigFileName(TaxonUtils.MyConfig.Name);
                        string destConfig   = Path.Combine(exportData.NewPath, "Config", Path.GetFileName(sourceConfig));

                        string saveFileName = TaxonUtils.MyConfig.TaxonFileName;
                        string savePath     = TaxonUtils.MyConfig.TaxonPath;
                        TaxonUtils.MyConfig.TaxonFileName = filename;
                        TaxonUtils.MyConfig.TaxonPath     = folder;
                        TaxonUtils.MyConfig.Save(destConfig, destExe);
                        TaxonUtils.MyConfig.TaxonFileName = saveFileName;
                        TaxonUtils.MyConfig.TaxonPath     = savePath;
                        exportData.ProgressItem.Update(2);
                        exportData.ProgressItem.End();
                    }
                }

                MessageBox.Show("Generate done");
            }
            Close();
        }
Exemple #5
0
        public void Activate()
        {
            Task taskDeleteOldCollection             = null;
            Task taskGatherExistentSpecies           = null;
            Dictionary <string, int> existentSpecies = new Dictionary <string, int>();

            ImageCollection arkiveCollection = TaxonImages.Manager.GetByName("Arkive");

            if (arkiveCollection != null)
            {
                QuestionDialog dlg = new QuestionDialog
                                     (
                    "Arkive collection already exists !\nWhat do you want to do ?",
                    "Confirm ... ",
                    new TaxonDialog.AnswersDesc().
                    Add("Delete", "delete all content of old arkive collection", 0).
                    Add("Merge", "import only image for species newly found", 1).
                    Add("Cancel", "stop the generation of collection", 2)
                                     );
                dlg.ShowDialog();
                OneAnswerDesc answer = dlg.Answer;

                if (answer == null || answer.ID == 2)
                {
                    return;
                }

                /*string message = "Arkive collection exist, remove it ?";
                 * DialogResult result = MessageBox.Show(message, "Confirm ...", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                 * if (result == DialogResult.Cancel)
                 *  return;*/

                if (answer.ID == 0)
                {
                    taskDeleteOldCollection = Task.Factory.StartNew(() => Directory.Delete(arkiveCollection.Path, true));
                }
                else
                {
                    taskGatherExistentSpecies = Task.Factory.StartNew(() =>
                    {
                        foreach (string file in Directory.EnumerateFiles(arkiveCollection.Path))
                        {
                            string species = Path.GetFileNameWithoutExtension(file).Split('_')[0];
                            if (!existentSpecies.ContainsKey(species))
                            {
                                existentSpecies[species] = 0;
                            }
                            existentSpecies[species] = existentSpecies[species] + 1;
                        }
                    });
                }
            }

            string folderArkive;

            using (var fbd = new FolderBrowserDialog())
            {
                fbd.Description  = "Select Folder where Arkive images are stored";
                fbd.SelectedPath = TaxonUtils.GetTaxonPath();
                DialogResult result = fbd.ShowDialog();
                if (result != DialogResult.OK || string.IsNullOrWhiteSpace(fbd.SelectedPath))
                {
                    return;
                }
                folderArkive = fbd.SelectedPath;
            }

            int lengthFolderArkive = folderArkive.Length;

            using (ProgressDialog progressDlg = new ProgressDialog())
            {
                progressDlg.StartPosition = FormStartPosition.CenterScreen;
                progressDlg.Show();

                ProgressItem piInitSearch = progressDlg.Add("Initialize searching", "", 0, 2);
                TaxonSearch  searching    = new TaxonSearch(TaxonUtils.OriginalRoot, true, true);
                piInitSearch.Update(1);
                string[] foldersLevel1 = Directory.GetDirectories(folderArkive);
                piInitSearch.Update(2);
                piInitSearch.End();

                Dictionary <string, string>        unknownFolder = new Dictionary <string, string>();
                Dictionary <string, TaxonTreeNode> knownFolder   = new Dictionary <string, TaxonTreeNode>();
                int missedPhotos          = 0;
                int importedPhotos        = 0;
                int alreadyImportedPhotos = 0;
                int newlyImportedPhotos   = 0;

                ProgressItem piParse = progressDlg.Add("Parse arkive folders", "", 0, foldersLevel1.Length);
                for (uint i = 0; i < foldersLevel1.Length; i++)
                {
                    piParse.Update(i, foldersLevel1[i].Substring(lengthFolderArkive + 1));
                    int folder1Length = foldersLevel1[i].Length + 1;

                    string[] foldersLevel2 = Directory.GetDirectories(foldersLevel1[i]);
                    foreach (string folder2 in foldersLevel2)
                    {
                        //if (folder2.ToLower().Contains("acropora"))
                        //    Console.WriteLine(folder2);

                        string[] photos = Directory.GetFiles(folder2, "*.jpg");
                        if (photos.Length == 0)
                        {
                            continue;
                        }

                        string        name = folder2.Substring(folder1Length).Replace('-', ' ').ToLower().Trim();
                        TaxonTreeNode node = searching.FindOne(name);
                        if (node == null)
                        {
                            unknownFolder[folder2] = name;
                            missedPhotos          += photos.Length;
                        }
                        else
                        {
                            knownFolder[folder2] = node;
                        }
                    }
                }
                piParse.Update(piParse.Max, knownFolder.Count.ToString() + " found, " + unknownFolder.Count.ToString() + " not.");

                if (taskDeleteOldCollection != null && !taskDeleteOldCollection.IsCompleted)
                {
                    ProgressItem piClean = progressDlg.Add("Clean old collection", "", 0, 1);
                    taskDeleteOldCollection.Wait();
                    piClean.Update(1);
                    piClean.End();
                }

                if (taskGatherExistentSpecies != null && !taskGatherExistentSpecies.IsCompleted)
                {
                    ProgressItem piAnalyseOld = progressDlg.Add("Analyse old collection", "", 0, 1);
                    taskGatherExistentSpecies.Wait();
                    piAnalyseOld.Update(1);
                    piAnalyseOld.End();
                }

                arkiveCollection = TaxonImages.Manager.GetOrCreateCollection("Arkive");
                if (arkiveCollection == null)
                {
                    return;
                }
                arkiveCollection.Desc  = "Collection generated from images taken from Arkive site : http://www.arkive.org";
                arkiveCollection.Desc += "Generated date : " + DateTime.Now.ToString();
                arkiveCollection.SaveInfos();
                ProgressItem piPopulate = progressDlg.Add("Populate collection", "", 0, knownFolder.Count);
                foreach (KeyValuePair <string, TaxonTreeNode> pair in knownFolder)
                {
                    string speciesName = pair.Value.Desc.RefMultiName.Main;
                    piPopulate.Update(piPopulate.Current + 1, speciesName);

                    string[] photos = Directory.GetFiles(pair.Key, "*.jpg");
                    importedPhotos += photos.Length;

                    if (existentSpecies.ContainsKey(speciesName))
                    {
                        if (existentSpecies[speciesName] == photos.Length)
                        {
                            alreadyImportedPhotos += photos.Length;
                            continue;
                        }
                        File.Delete(arkiveCollection.Path + Path.DirectorySeparatorChar + speciesName + "*.*");
                    }

                    newlyImportedPhotos += photos.Length;

                    for (int index = 0; index < photos.Length; index++)
                    {
                        string newName = speciesName + "_" + index.ToString() + ".jpg";
                        File.Copy(photos[index], arkiveCollection.Path + Path.DirectorySeparatorChar + newName);
                    }
                }
                piPopulate.Update(piPopulate.Max, importedPhotos.ToString() + " photos imported.");

                string message0 = (unknownFolder.Count + knownFolder.Count).ToString() + " total folders found\n";
                string message1 = knownFolder.Count.ToString() + " with associated taxons ( " + importedPhotos.ToString() + " photos imported )";
                if (newlyImportedPhotos != importedPhotos)
                {
                    message1 += " ( merging : " + newlyImportedPhotos + " new photos";
                }
                string message2 = unknownFolder.Count.ToString() + " names not found ( " + missedPhotos.ToString() + " photos left behind )";
                string message  = message0 + "\n" + message1 + "\n" + message2 + "\n\n" + "for more details, look at GenerateArkiveCollection.log file";
                if (unknownFolder.Count > 0)
                {
                    message += "\nA list of taxons is generated with all name not found : " + Path.Combine(TaxonList.GetFolder(), "ArkiveNames.txt");
                }
                Loggers.WriteInformation(LogTags.Data, message);

                try
                {
                    List <KeyValuePair <string, string> > unknowns = unknownFolder.ToList();
                    unknowns.Sort((x, y) => x.Value.CompareTo(y.Value));

                    string file = Path.Combine(TaxonUtils.GetLogPath(), "GenerateArkiveCollection.log");
                    if (File.Exists(file))
                    {
                        File.Delete(file);
                    }
                    using (StreamWriter outfile = new StreamWriter(file))
                    {
                        outfile.WriteLine("GenerateArkiveCollection results:");
                        outfile.WriteLine();
                        outfile.WriteLine("  " + message0);
                        outfile.WriteLine("  " + message1);
                        outfile.WriteLine("  " + message2);
                        outfile.WriteLine();
                        if (unknowns.Count > 0)
                        {
                            outfile.WriteLine("List of not found names:");
                            outfile.WriteLine();

                            int maxLength = 0;
                            foreach (KeyValuePair <string, string> pair in unknowns)
                            {
                                maxLength = Math.Max(maxLength, pair.Value.Length);
                            }
                            foreach (KeyValuePair <string, string> pair in unknowns)
                            {
                                outfile.WriteLine("  " + pair.Value.PadRight(maxLength) + " => " + pair.Key);
                            }
                            outfile.WriteLine();
                        }
                    }

                    if (unknowns.Count > 0)
                    {
                        file = Path.Combine(TaxonList.GetFolder(), "ArkiveNames.txt");
                        if (File.Exists(file))
                        {
                            File.Delete(file);
                        }
                        using (StreamWriter outfile = new StreamWriter(file))
                        {
                            foreach (KeyValuePair <string, string> pair in unknowns)
                            {
                                outfile.WriteLine(pair.Value);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    string error = "Exception while saving results in GenerateArkiveCollection.log: \n\n";
                    error += e.Message;
                    if (e.InnerException != null)
                    {
                        error += "\n" + e.InnerException.Message;
                    }
                    Loggers.WriteError(LogTags.Data, error);
                }
            }
        }