Beispiel #1
0
        private static void SaveCacheData()
        {
            BibliographerSettings settings;
            string datadir;

            settings = new BibliographerSettings("apps.bibliographer");
            datadir  = settings.GetString("data-directory");

            Cleanup_invalid_dirs();

            try {
                Monitor.Enter(sections);
                StreamWriter stream = new StreamWriter(new FileStream(datadir + "/cachedata", FileMode.OpenOrCreate, FileAccess.Write));

                for (int section = 0; section < sections.Count; section++)
                {
                    stream.WriteLine("[{0}]", ((CacheSection)sections[section]).section);
                    for (int key = 0; key < ((CacheSection)sections[section]).keys.Count; key++)
                    {
                        stream.WriteLine("{0} {1}", ((KeySection)((CacheSection)sections[section]).keys[key]).key, ((KeySection)((CacheSection)sections[section]).keys[key]).filename);
                    }
                }

                stream.Close();
                Monitor.Exit(sections);
            } catch (DirectoryNotFoundException e) {
                WriteLine(10, e.Message);
                WriteLine(1, "Directory ~/.local/share/bibliographer/ not found! Creating it...");
                Directory.CreateDirectory(datadir);
            } catch (Exception e) {
                WriteLine(1, "Unhandled exception whilst trying to save cache: {0}", e);
            }
        }
Beispiel #2
0
        private static StringArrayList GetTextualExtractor(object mimeType)
        {
            BibliographerSettings settings;

            string []       extractors;
            StringArrayList extractor;

            extractor  = new StringArrayList();
            settings   = new BibliographerSettings("apps.bibliographer.index");
            extractors = settings.GetStrv("textual-extractor");

            if (extractors.Length == 0)
            {
                ArrayList newExtractors;
                newExtractors = new ArrayList();

                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    // Default extractors for Windows systems
                    newExtractors.Add(".pdf:pdftotext:{0} -");
                    newExtractors.Add(".doc:antiword:{0}");
                    newExtractors.Add(".docx:opc_text:{0}");
                }
                else
                {
                    // Default extractors for Gnu/Linux systems
                    newExtractors.Add("application/pdf:pdftotext:{0} -");
                    newExtractors.Add("application/msword:antiword:{0}");
                    newExtractors.Add("application/postscript:pstotext:{0}");
                    newExtractors.Add("text/plain:cat:{0}");
                }

                //TODO: Add default extractors for other systems

                extractors = (string [])newExtractors.ToArray(typeof(string));
                settings.SetStrv("textual-extractor", extractors);
            }
            string [] output;

            foreach (string entry in extractors)
            {
                output = entry.Split(':');
                if (output [0] == mimeType.ToString())
                {
                    extractor.Add(output [1]);
                    extractor.Add(output [2]);
                }
            }

            if (extractor.Count > 0)
            {
                WriteLine(5, "textual extractor determined: " + extractor [0]);
            }

            return(extractor);
        }
Beispiel #3
0
        public LitTreeView(Gtk.ITreeModel model)
        {
            sorter = new Gtk.TreeModelSort(model);

            columnsIconSettings      = new BibliographerSettings("apps.bibliographer.columns.icon");
            columnsAuthorSettings    = new BibliographerSettings("apps.bibliographer.columns.author");
            columnsTitleSettings     = new BibliographerSettings("apps.bibliographer.columns.title");
            columnsYearSettings      = new BibliographerSettings("apps.bibliographer.columns.year");
            columnsJournalSettings   = new BibliographerSettings("apps.bibliographer.columns.journal");
            columnsBibtexKeySettings = new BibliographerSettings("apps.bibliographer.columns.bibtexkey");
            columnsVolumeSettings    = new BibliographerSettings("apps.bibliographer.columns.volume");
            columnsPagesSettings     = new BibliographerSettings("apps.bibliographer.columns.pages");

            Model = sorter;

            Gtk.CellRendererPixbuf columnIconRenderer;
            Gtk.CellRendererText   columnAuthorRenderer;
            Gtk.CellRendererText   columnTitleRenderer;
            Gtk.CellRendererText   columnYearRenderer;
            Gtk.CellRendererText   columnJournalRenderer;
            Gtk.CellRendererText   columnBibtexKeyRenderer;
            Gtk.CellRendererText   columnVolumeRenderer;
            Gtk.CellRendererText   columnPagesRenderer;

            columnIconRenderer      = new Gtk.CellRendererPixbuf();
            columnAuthorRenderer    = new Gtk.CellRendererText();
            columnTitleRenderer     = new Gtk.CellRendererText();
            columnYearRenderer      = new Gtk.CellRendererText();
            columnJournalRenderer   = new Gtk.CellRendererText();
            columnBibtexKeyRenderer = new Gtk.CellRendererText();
            columnVolumeRenderer    = new Gtk.CellRendererText();
            columnPagesRenderer     = new Gtk.CellRendererText();

            AppendColumn("Icon", columnIconRenderer, "image");
            AppendColumn("Author", columnAuthorRenderer, "text");
            AppendColumn("Title", columnTitleRenderer, "text");
            AppendColumn("Year", columnYearRenderer, "text");
            AppendColumn("Journal", columnJournalRenderer, "text");
            AppendColumn("Bibtex Key", columnBibtexKeyRenderer, "text");
            AppendColumn("Volume", columnVolumeRenderer, "text");
            AppendColumn("Pages", columnPagesRenderer, "text");

            HeadersClickable = true;

            var textDataFunc   = new Gtk.TreeCellDataFunc(RenderColumnTextFromBibtexRecord);
            var pixmapDataFunc = new Gtk.TreeCellDataFunc(RenderColumnPixbufFromBibtexRecord);

            int idx = 0;

            foreach (Gtk.TreeViewColumn column in Columns)
            {
                column.Expand      = false;
                column.Reorderable = true;
                column.Resizable   = true;
                column.Clickable   = true;

                if (column.Title == "Icon")
                {
                    column.FixedWidth = columnsIconSettings.GetInt("width");
                    column.Visible    = columnsIconSettings.GetBoolean("visible");
                    column.SetCellDataFunc(column.Cells[0], pixmapDataFunc);
                    column.Sizing      = Gtk.TreeViewColumnSizing.Fixed;
                    column.Resizable   = false;
                    column.Reorderable = false;
                    column.Clickable   = false;
                    column.MinWidth    = 20;
                }
                else if (column.Title == "Author")
                {
                    column.FixedWidth = columnsAuthorSettings.GetInt("width");
                    column.Visible    = columnsAuthorSettings.GetBoolean("visible");
                    column.SetCellDataFunc(column.Cells[0], textDataFunc);
                    column.SortColumnId = 1;
                    sorter.SetSortFunc(1, StringCompareAuthor);
                    column.Clicked += OnColumnSort;
                    if (column != Columns[columnsAuthorSettings.GetInt("order") - 1])
                    {
                        MoveColumnAfter(column, Columns[columnsAuthorSettings.GetInt("order") - 1]);
                    }
                }
                else if (column.Title == "Title")
                {
                    column.Expand     = true;
                    column.FixedWidth = columnsTitleSettings.GetInt("width");
                    column.Visible    = columnsTitleSettings.GetBoolean("visible");
                    column.SetCellDataFunc(column.Cells[0], textDataFunc);
                    column.SortColumnId = 2;
                    sorter.SetSortFunc(2, StringCompare);
                    column.Clicked += OnColumnSort;
                    if (column != Columns[columnsTitleSettings.GetInt("order") - 1])
                    {
                        MoveColumnAfter(column, Columns[columnsTitleSettings.GetInt("order") - 1]);
                    }
                }
                else if (column.Title == "Year")
                {
                    column.FixedWidth = columnsYearSettings.GetInt("width");
                    column.Visible    = columnsYearSettings.GetBoolean("visible");
                    column.SetCellDataFunc(column.Cells[0], textDataFunc);
                    column.SortColumnId = 3;
                    sorter.SetSortFunc(3, StringCompare);
                    column.Clicked += OnColumnSort;
                    if (column != Columns[columnsYearSettings.GetInt("order") - 1])
                    {
                        MoveColumnAfter(column, Columns[columnsYearSettings.GetInt("order") - 1]);
                    }
                }
                else if (column.Title == "Journal")
                {
                    column.FixedWidth = columnsJournalSettings.GetInt("width");
                    column.Visible    = columnsJournalSettings.GetBoolean("visible");
                    column.SetCellDataFunc(column.Cells[0], textDataFunc);
                    column.SortColumnId = 4;
                    sorter.SetSortFunc(4, StringCompare);
                    column.Clicked += OnColumnSort;
                    if (column != Columns[columnsJournalSettings.GetInt("order") - 1])
                    {
                        MoveColumnAfter(column, Columns[columnsJournalSettings.GetInt("order") - 1]);
                    }
                }
                else if (column.Title == "Bibtex Key")
                {
                    column.FixedWidth = columnsBibtexKeySettings.GetInt("width");
                    column.Visible    = columnsBibtexKeySettings.GetBoolean("visible");
                    column.SetCellDataFunc(column.Cells[0], textDataFunc);
                    column.SortColumnId = 5;
                    sorter.SetSortFunc(5, StringCompare);
                    column.Clicked += OnColumnSort;
                    if (column != Columns[columnsBibtexKeySettings.GetInt("order") - 1])
                    {
                        MoveColumnAfter(column, Columns[columnsBibtexKeySettings.GetInt("order") - 1]);
                    }
                }
                else if (column.Title == "Volume")
                {
                    column.FixedWidth = columnsVolumeSettings.GetInt("width");
                    column.Visible    = columnsVolumeSettings.GetBoolean("visible");
                    column.SetCellDataFunc(column.Cells[0], textDataFunc);
                    column.SortColumnId = 6;
                    sorter.SetSortFunc(6, StringCompare);
                    column.Clicked += OnColumnSort;
                    if (column != Columns[columnsVolumeSettings.GetInt("order") - 1])
                    {
                        MoveColumnAfter(column, Columns[columnsVolumeSettings.GetInt("order") - 1]);
                    }
                }
                else if (column.Title == "Pages")
                {
                    column.FixedWidth = columnsPagesSettings.GetInt("width");
                    column.Visible    = columnsPagesSettings.GetBoolean("visible");
                    column.SetCellDataFunc(column.Cells[0], textDataFunc);
                    column.SortColumnId = 7;
                    sorter.SetSortFunc(7, StringCompare);
                    column.Clicked += OnColumnSort;
                    if (column != Columns[columnsPagesSettings.GetInt("order") - 1])
                    {
                        MoveColumnAfter(column, Columns[columnsPagesSettings.GetInt("order") - 1]);
                    }
                }
                idx++;
            }

            //RedrawColumns ();

            // Callbacks for the LitTreeView
            ColumnsChanged += OnColumnsChanged;
            DragMotion     += OnDragMotion;
            RowActivated   += OnRowActivated;
            DragLeave      += OnDragLeave;

            Show();
        }
Beispiel #4
0
        public static string AddToCache(string section, string key)
        {
            BibliographerSettings settings;
            KeySection            kSection;
            CacheSection          cSection;
            Random random;
            string datadir, filename;

            settings = new BibliographerSettings("apps.bibliographer");

            kSection = LookupKey(section, key);
            if (kSection != null)
            {
                return(kSection.filename);
            }

            cSection = LookupSection(section);

            if (cSection == null)
            {
                // add a new section
                sections.Add(new CacheSection(section));
                sections.Sort(new SectionCompare());
                cSection = LookupSection(section);
                if (cSection == null)
                {
                    WriteLine(5, "Failing to add a new section in cache, that's messed up... :-(");
                    return("");
                }
            }

            datadir = settings.GetString("data-directory");
            random  = new Random();
            do
            {
                filename = datadir + "/cache/";

                const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                for (int i = 0; i < 20; i++)
                {
                    filename = filename + chars[random.Next() % chars.Length];
                }

                bool ok = false;
                try {
                    FileStream stream = new FileStream(filename, FileMode.CreateNew, FileAccess.Write);
                    stream.Close();
                    ok = true;
                } catch (DirectoryNotFoundException e) {
                    WriteLine(10, e.Message);

                    try {
                        Directory.CreateDirectory(datadir);
                    } catch (Exception e2) {
                        WriteLine(10, e2.Message);
                    }
                    try {
                        Directory.CreateDirectory(datadir + "/cache");
                    } catch (Exception e2) {
                        WriteLine(10, e2.Message);
                        WriteLine(1, "Failed to create directory {0}", datadir + "/cache");
                    }
                } catch (IOException e) {
                    WriteLine(10, e.Message);
                    // file already exists
                }
                if (ok)
                {
                    break;
                }
            } while (true);
            cSection.keys.Add(new KeySection(key, filename));
            cSection.keys.Sort(new KeyCompare());
            SaveCacheData();
            return(filename);
        }
Beispiel #5
0
        private static void LoadCacheData()
        {
            BibliographerSettings settings;
            CacheSection          curSection;
            StreamReader          stream;
            string datadir;

            settings = new BibliographerSettings("apps.bibliographer");
            datadir  = settings.GetString("data-directory");

            sections = new ArrayList();

            try {
                stream = new StreamReader(new FileStream(datadir + "/cachedata", FileMode.Open, FileAccess.Read));

                // cache opened! let's read some data...
                curSection = null;
                while (stream.Peek() > -1)
                {
                    string line = stream.ReadLine();
                    if (line != "")
                    {
                        if (line[0] == '[')
                        {
                            // new section
                            char[] splits      = { '[', ']' };
                            string sectionName = line.Split(splits)[1];

                            // check that we don't already have this section name
                            bool found = false;
                            for (int i = 0; i < sections.Count; i++)
                            {
                                if (((CacheSection)sections[i]).section == sectionName)
                                {
                                    found = true;
                                    break;
                                }
                            }
                            if (found)
                            {
                                WriteLine(5, "Duplicate section {0} in cache file!", sectionName);
                                curSection = null;
                                continue;
                            }

                            if (curSection != null)
                            {
                                curSection.keys.Sort(new KeyCompare());
                                sections.Add(curSection);
                            }
                            curSection = new CacheSection(sectionName);
                        }
                        else
                        {
                            if (curSection == null)
                            {
                                // no active section, so skip
                                continue;
                            }
                            string[] fields = line.Split(' ');
                            curSection.keys.Add(new KeySection(fields[0], fields[1]));
                        }
                    }
                }
                stream.Close();
                if (curSection != null)
                {
                    sections.Add(curSection);
                }
                sections.Sort(new SectionCompare());
            } catch (DirectoryNotFoundException e) {
                WriteLine(10, e.Message);
                WriteLine(1, "Directory ~/.local/share/bibliographer/ not found! Creating it...");
                Directory.CreateDirectory(datadir);
            } catch (FileNotFoundException e) {
                WriteLine(10, e.Message);
                // no cache, no problem-o :-)
            }
        }