static async Task <List <FetchResult> > FetchReferencesByFileAsync(MainForm mainForm, string directory, IProgress <PercentageAndTextProgressInfo> progress, CancellationToken cancellationToken)
        {
            var results = new List <FetchResult>();

            var pathes = Path2.GetFilesSafe(new DirectoryInfo(directory), "*.pdf", SearchOption.AllDirectories).Select(fileInfo => fileInfo.FullName).ToList();

            var fileImportSupport = new FileImportSupport();

            for (var i = 0; i < pathes.Count; i++)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var path = pathes[i];

                var referencesFromFile = await fileImportSupport.ImportFilesAsync(mainForm.Project, mainForm.Project.Engine.TransformerManager, new List <string>() { path }, AttachmentAction.Copy);

                if ((bool)referencesFromFile?.Any())
                {
                    results.Add(new FetchResult {
                        Path = path, References = referencesFromFile
                    });
                }

                progress.Report(new PercentageAndTextProgressInfo {
                    Percentage = Convert.ToInt32(100 / pathes.Count * i)
                });
            }

            return(results);
        }
示例#2
0
        public static void Run(MainForm mainForm)
        {
            var counter = 0;

            string sourcePath;

            using (var folderDialog = new FolderBrowserDialog {
                Description = ImportPdfsAndCategorySystemResource.FolderBrowserDialogDescription
            })
            {
                if (folderDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                sourcePath = folderDialog.SelectedPath;
            }

            var action        = AttachmentAction.Copy;
            var directoryInfo = new DirectoryInfo(sourcePath);
            var fileInfos     = Path2.GetFilesSafe(directoryInfo, "*.pdf", SearchOption.AllDirectories).ToList();
            var newReferences = new List <Reference>();

            foreach (var filePath in fileInfos.Select(info => info.FullName).ToList())
            {
                var referencesFromFile = new FileImportSupport().ImportFiles(mainForm.Project, mainForm.Project.Engine.TransformerManager, new List <string>()
                {
                    filePath
                }, action);

                if (referencesFromFile != null && referencesFromFile.Any())
                {
                    var referencesFromFileAdded = mainForm.Project.References.AddRange(referencesFromFile);

                    var fileName = Path.GetFileName(filePath);
                    AddCategories(referencesFromFileAdded, filePath.Substring(sourcePath.Length, filePath.Length - sourcePath.Length - fileName.Length));
                    counter++;
                }
            }

            MessageBox.Show(mainForm, ImportPdfsAndCategorySystemResource.MacroResultMessage.FormatString(counter), mainForm.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
示例#3
0
    public static void Main()
    {
        if (Program.ProjectShells.Count == 0)
        {
            return;                                                     //no project open
        }
        if (IsBackupAvailable() == false)
        {
            return;                                                             //user wants to backup his/her project first
        }
        int counter = 0;

        try
        {
            string sourcePath;

            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.Description = "Select a root folder for the PDF files to be imported ...";
            DialogResult folderResult = folderDialog.ShowDialog();
            if (folderResult == DialogResult.OK)
            {
                sourcePath = folderDialog.SelectedPath;
            }
            else
            {
                return;
            }

            AttachmentAction action = AttachmentAction.Link;             //suggested: .Link or .Copy

            DirectoryInfo   dir       = new DirectoryInfo(sourcePath);
            List <FileInfo> fileInfos = Path2.GetFilesSafe(dir, "*.pdf", SearchOption.AllDirectories).ToList();

            List <Reference>             newReferences = new List <Reference>();
            SwissAcademic.Citavi.Project activeProject = Program.ActiveProjectShell.Project;

            List <string> filePaths = fileInfos.Select <FileInfo, string>(info => info.FullName).ToList();
            foreach (string filePath in filePaths)
            {
                DebugMacro.WriteLine("START IMPORT: " + filePath);
                List <Reference> referencesFromFile = new FileImportSupport().ImportFiles(activeProject, activeProject.Engine.TransformerManager, new List <string>()
                {
                    filePath
                }, action);

                if (referencesFromFile != null && referencesFromFile.Any())
                {
                    var referencesFromFileAdded = activeProject.References.AddRange(referencesFromFile);

                    var fileName = Path.GetFileName(filePath);
                    AddCategories(referencesFromFileAdded, filePath.Substring(sourcePath.Length, filePath.Length - sourcePath.Length - fileName.Length));
                    DebugMacro.WriteLine("SUCCESS");
                    counter++;
                }
                else
                {
                    DebugMacro.WriteLine("ERROR importing file");
                }
            }
        }         //end try

        finally
        {
            MessageBox.Show(string.Format("Macro has finished execution.\r\n{0} changes were made.", counter.ToString()), "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Information);
        } //end finally
    }     //end main()