Example #1
0
        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            FeatureTrackingManager.Instance.UseFeature(Features.Library_ImportFromThirdParty);

            IEnumerable <AugmentedBindable <BibTeXEntry> > allEntries = GetEntries().Where(x => x.Underlying.Selected);

            if (!allEntries.Any())
            {
                MessageBoxes.Error("Please select at least one entry to import, by checking the checkbox.");
                return;
            }

            StatusManager.Instance.UpdateStatus("ImportFromThirdParty", "Started importing documents");

            List <FilenameWithMetadataImport> filename_and_bibtex_imports = new List <FilenameWithMetadataImport>();

            foreach (AugmentedBindable <BibTeXEntry> entry in allEntries)
            {
                FilenameWithMetadataImport filename_with_metadata_import = new FilenameWithMetadataImport
                {
                    filename = entry.Underlying.Filename,
                    bibtex   = entry.Underlying.BibTeX,
                    tags     = entry.Underlying.Tags,
                    notes    = entry.Underlying.Notes
                };

                filename_and_bibtex_imports.Add(filename_with_metadata_import);
            }

            ImportingIntoLibrary.AddNewPDFDocumentsToLibraryWithMetadata_ASYNCHRONOUS(_library, false, false, filename_and_bibtex_imports.ToArray());

            MessageBoxes.Info("{0} files are now being imported - this may take a little while.  You can track the import progress in the status bar.", filename_and_bibtex_imports.Count);

            Close();
        }
Example #2
0
        internal static EndnoteDatabaseDetails DetectEndnoteDatabaseDetails()
        {
            EndnoteDatabaseDetails edd = new EndnoteDatabaseDetails();

            foreach (string endnote_database_filename in GetRecentEndnoteDatabases())
            {
                try
                {
                    Logging.Info("Reading Endnote database '{0}'", endnote_database_filename);
                    using (MemoryStream ms = MYDDatabase.OpenMYDDatabase(endnote_database_filename))
                    {
                        ++edd.databases_found;

                        MYDBinaryReader br = new MYDBinaryReader(ms);
                        foreach (MYDRecord record in MYDRecordReader.Records(br))
                        {
                            try
                            {
                                FilenameWithMetadataImport fwmi = ConvertEndnoteToFilenameWithMetadataImport(endnote_database_filename, record);
                                edd.metadata_imports.Add(fwmi);

                                // Update statistics
                                ++edd.documents_found;
                                if (null != fwmi.filename)
                                {
                                    ++edd.pdfs_found;
                                }
                            }
                            catch (Exception ex)
                            {
                                Logging.Warn(ex, "Problem processing Endnote record.");
                            }
                        }
                    }
                }

                catch (Exception ex)
                {
                    Logging.Warn(ex, "Exception while reading Endnote database '{0}'", endnote_database_filename);
                }
            }

            return(edd);
        }
Example #3
0
        internal static MendeleyDatabaseDetails DetectMendeleyDatabaseDetails()
        {
            MendeleyDatabaseDetails mdd = new MendeleyDatabaseDetails();

            string BASE_DIR_FOR_MENDELEY_DATABASE = Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Mendeley Ltd/Mendeley Desktop"));

            if (!Directory.Exists(BASE_DIR_FOR_MENDELEY_DATABASE))
            {
                Logging.Info("Mendeley not found.");
                mdd.databases_found = 0;
                mdd.documents_found = 0;
                mdd.pdfs_found      = 0;
                return(mdd);
            }

            try
            {
                string[] sqlite_filenames = Directory.GetFiles(BASE_DIR_FOR_MENDELEY_DATABASE, "*.sqlite", SearchOption.TopDirectoryOnly);
                foreach (string sqlite_filename in sqlite_filenames)
                {
                    // Skip the monitor database
                    if (sqlite_filename.EndsWith("monitor.sqlite"))
                    {
                        continue;
                    }

                    try
                    {
                        using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + sqlite_filename))
                        {
                            // Turn on extended result codes
                            connection.SetExtendedResultCodes(true);

                            connection.Open();

                            // Build the authors lookup
                            Dictionary <long, string> authors_lookup = new Dictionary <long, string>();
                            {
                                string command_string = "SELECT * FROM DocumentContributors";
                                using (var command = new SQLiteCommand(command_string, connection))
                                {
                                    SQLiteDataReader reader = command.ExecuteReader();
                                    while (reader.Read())
                                    {
                                        long   document_id   = (long)reader["documentId"];
                                        string surname       = reader["lastName"] as string;
                                        string firstnames    = reader["firstNames"] as string;
                                        string compound_name = (String.IsNullOrEmpty(surname)) ? firstnames : (String.IsNullOrEmpty(firstnames) ? surname : (surname + ", " + firstnames));
                                        if (!String.IsNullOrEmpty(compound_name))
                                        {
                                            if (!authors_lookup.ContainsKey(document_id))
                                            {
                                                authors_lookup[document_id] = compound_name;
                                            }
                                            else
                                            {
                                                authors_lookup[document_id] = authors_lookup[document_id] + " AND " + compound_name;
                                            }
                                        }
                                    }
                                }
                            }

                            Dictionary <long, HashSet <string> > tags_lookup = new Dictionary <long, HashSet <string> >();
                            {
                                string command_string = "SELECT * FROM DocumentKeywords";
                                using (var command = new SQLiteCommand(command_string, connection))
                                {
                                    SQLiteDataReader reader = command.ExecuteReader();
                                    while (reader.Read())
                                    {
                                        long   document_id = (long)reader["documentId"];
                                        string keyword     = reader["keyword"] as string;
                                        if (!String.IsNullOrEmpty(keyword))
                                        {
                                            if (!tags_lookup.ContainsKey(document_id))
                                            {
                                                tags_lookup[document_id] = new HashSet <string>();
                                            }

                                            tags_lookup[document_id].Add(keyword);
                                        }
                                    }
                                }
                            }

                            // Get the bibtexes
                            {
                                //string command_string = "SELECT * FROM Documents WHERE 1=1 ";
                                string command_string =
                                    ""
                                    + "SELECT * "
                                    + "FROM Documents "
                                    + "LEFT OUTER JOIN DocumentFiles ON Documents.id == DocumentFiles.documentId "
                                    + "LEFT OUTER JOIN Files ON DocumentFiles.Hash = Files.Hash "
                                ;

                                using (var command = new SQLiteCommand(command_string, connection))
                                {
                                    SQLiteDataReader reader = command.ExecuteReader();

                                    ++mdd.databases_found;

                                    while (reader.Read())
                                    {
                                        try
                                        {
                                            BibTexItem bibtex_item = new BibTexItem();

                                            bibtex_item.Type = reader["type"] as string;
                                            bibtex_item.Key  = reader["citationKey"] as string;
                                            if (String.IsNullOrEmpty(bibtex_item.Key))
                                            {
                                                bibtex_item.Key = BibTexTools.GenerateRandomBibTeXKey();
                                            }

                                            PopulateTentativeField(bibtex_item, reader, "title");
                                            PopulateTentativeField(bibtex_item, reader, "abstract");
                                            PopulateTentativeField(bibtex_item, reader, "advisor");
                                            PopulateTentativeField(bibtex_item, reader, "city");
                                            PopulateTentativeField(bibtex_item, reader, "country");
                                            PopulateTentativeField(bibtex_item, reader, "day");
                                            PopulateTentativeField(bibtex_item, reader, "month");
                                            PopulateTentativeField(bibtex_item, reader, "dateAccessed", "accessed");
                                            PopulateTentativeField(bibtex_item, reader, "department");
                                            PopulateTentativeField(bibtex_item, reader, "doi");
                                            PopulateTentativeField(bibtex_item, reader, "edition");
                                            PopulateTentativeField(bibtex_item, reader, "institution");
                                            PopulateTentativeField(bibtex_item, reader, "isbn");
                                            PopulateTentativeField(bibtex_item, reader, "issn");
                                            PopulateTentativeField(bibtex_item, reader, "issue");
                                            PopulateTentativeField(bibtex_item, reader, "medium");
                                            PopulateTentativeField(bibtex_item, reader, "pages");
                                            PopulateTentativeField(bibtex_item, reader, "pmid");
                                            PopulateTentativeField(bibtex_item, reader, "publication");
                                            PopulateTentativeField(bibtex_item, reader, "publisher");
                                            PopulateTentativeField(bibtex_item, reader, "sections", "section");
                                            PopulateTentativeField(bibtex_item, reader, "series");
                                            PopulateTentativeField(bibtex_item, reader, "session");
                                            PopulateTentativeField(bibtex_item, reader, "volume");
                                            PopulateTentativeField(bibtex_item, reader, "year");

                                            long document_id = (long)reader["id"];
                                            if (authors_lookup.ContainsKey(document_id))
                                            {
                                                bibtex_item["author"] = authors_lookup[document_id];
                                            }

                                            FilenameWithMetadataImport fwmi = new FilenameWithMetadataImport();
                                            fwmi.tags.Add("import_mendeley");
                                            fwmi.bibtex = bibtex_item.ToBibTex();

                                            string filename = reader["localUrl"] as string;
                                            if (!String.IsNullOrEmpty(filename))
                                            {
                                                const string FILE_PREFIX = "file:///";
                                                if (filename.StartsWith(FILE_PREFIX))
                                                {
                                                    filename = filename.Substring(FILE_PREFIX.Length);
                                                }

                                                filename = Uri.UnescapeDataString(filename);
                                                //filename = filename.Replace('/', '\\');

                                                fwmi.filename = Path.GetFullPath(filename);

                                                ++mdd.pdfs_found;
                                            }

                                            if (tags_lookup.ContainsKey(document_id))
                                            {
                                                foreach (string tag in tags_lookup[document_id])
                                                {
                                                    fwmi.tags.Add(tag);
                                                }
                                            }

                                            string note = reader["note"] as string;
                                            if (!String.IsNullOrEmpty(note))
                                            {
                                                note = note.Replace("<m:italic>", "");
                                                note = note.Replace("</m:italic>", "");
                                                note = note.Replace("<m:bold>", "");
                                                note = note.Replace("</m:bold>", "");
                                                note = note.Replace("<m:note>", "");
                                                note = note.Replace("</m:note>", "");
                                                note = note.Replace("<m:underline>", "");
                                                note = note.Replace("</m:underline>", "");
                                                note = note.Replace("<m:right>", "");
                                                note = note.Replace("</m:right>", "");
                                                note = note.Replace("<m:center>", "");
                                                note = note.Replace("</m:center>", "");
                                                note = note.Replace("<m:linebreak/>", "\n");

                                                fwmi.notes = note;
                                            }

                                            mdd.metadata_imports.Add(fwmi);

                                            ++mdd.documents_found;
                                        }

                                        catch (Exception ex)
                                        {
                                            Logging.Error(ex, "Exception while extracting a Mendeley document.");
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.Error(ex, "Exception while exploring for Mendeley instance in file '{0}'.", sqlite_filename);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Error(ex, "Exception while exploring for Mendeley instances.");
            }

            Logging.Info("Got {0} libraries with {1} documents and {2} PDFs.", mdd.databases_found, mdd.documents_found, mdd.pdfs_found);

            return(mdd);
        }
Example #4
0
        private static FilenameWithMetadataImport ConvertEndnoteToFilenameWithMetadataImport(string endnote_database_filename, MYDRecord record)
        {
            BibTexItem bibtex_item = new BibTexItem();

            string type = "article";

            TransformType(record.reference_type, ref type);
            bibtex_item.Type = type;
            bibtex_item.Key  = BibTexTools.GenerateRandomBibTeXKey();

            foreach (var pair in record.fields)
            {
                string key   = pair.Key;
                string value = pair.Value;

                TransformKeyValue(record.reference_type, ref key, ref value);

                if ("notes" == key)
                {
                    continue;
                }
                if ("keywords" == key)
                {
                    continue;
                }
                if ("link_to_pdf" == key)
                {
                    continue;
                }

                bibtex_item[key] = value;
            }

            FilenameWithMetadataImport fwmi = new FilenameWithMetadataImport();

            fwmi.tags.Add("import_endnote");
            fwmi.tags.Add("import_endnote_" + Path.GetFileNameWithoutExtension(endnote_database_filename));
            fwmi.bibtex = bibtex_item.ToBibTex();

            if (record.fields.ContainsKey("notes"))
            {
                fwmi.notes = record.fields["notes"];
            }

            if (record.fields.ContainsKey("keywords"))
            {
                string   keywords = record.fields["keywords"];
                string[] tags     = keywords.Split(new char[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string tag in tags)
                {
                    fwmi.tags.Add(tag);
                }
            }

            // Handle the attachments
            if (record.fields.ContainsKey("link_to_pdf"))
            {
                string   links_string = record.fields["link_to_pdf"];
                string[] links        = links_string.Split(new string[] { ",", "internal-pdf://", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                // Build up the list of candidates
                string        base_directory = Path.GetFullPath(endnote_database_filename.Substring(0, endnote_database_filename.Length - 4) + @".Data/PDF");
                List <string> pdf_links      = new List <string>();

                // First candidates are those in the subdirectory corresponding to the .ENL file
                foreach (string link in links)
                {
                    pdf_links.Add(Path.GetFullPath(Path.Combine(base_directory, link)));
                }

                // Second candidates are raw pathnames
                foreach (string link in links)
                {
                    pdf_links.Add(link);
                }

                // Use the first PDF file that exists in the file system
                foreach (string pdf_link in pdf_links)
                {
                    if (pdf_link.ToLower().EndsWith(".pdf") && File.Exists(pdf_link))
                    {
                        fwmi.filename = pdf_link;
                        break;
                    }
                }
            }

            return(fwmi);
        }