Esempio n. 1
0
        static public ICollection CreateFiltersFromPath(string path)
        {
            string guessed_mime_type = XdgMime.GetMimeType(path);
            string extension         = Path.GetExtension(path);

            return(CreateFilters(UriFu.PathToFileUri(path), extension, guessed_mime_type));
        }
Esempio n. 2
0
        protected override void LoadIcon(Gtk.Image image, int size)
        {
            base.LoadIcon(image, size);

            string parent_mime_type = XdgMime.GetMimeTypeFromFileName(Hit.EscapedParentUri);

            if (parent_mime_type == null)
            {
                return;
            }

            Gdk.Pixbuf emblem = WidgetFu.LoadMimeIcon(parent_mime_type, 24);

            if (emblem == null)
            {
                return;
            }

            Gdk.Pixbuf icon = image.Pixbuf.Copy();

            emblem.Composite(icon,
                             0,                                             // dest_x
                             icon.Height - emblem.Height,                   // dest_y
                             emblem.Width,                                  // dest_width
                             emblem.Height,                                 // dest_height
                             0,                                             // offset_x
                             icon.Height - emblem.Height,                   // offset_y
                             1, 1,                                          // scale
                             Gdk.InterpType.Bilinear, 255);

            image.Pixbuf.Dispose();
            image.Pixbuf = icon;
        }
Esempio n. 3
0
        private ArchiveEntry GetNextEntrySingle()
        {
            if (handled_single)
            {
                return(null);
            }

            ArchiveEntry entry = new ArchiveEntry();

            // If there is a ExactFilename property, get name using that,
            // else use the FileInfo name
            string exact_filename = null;

            foreach (Property p in Indexable.Properties)
            {
                if (p.Key == Property.ExactFilenamePropKey)
                {
                    exact_filename = p.Value;
                    break;
                }
            }

            if (exact_filename != null)
            {
                entry.Name = Path.GetFileNameWithoutExtension(exact_filename);
            }
            else
            {
                entry.Name = Path.GetFileNameWithoutExtension(FileInfo.Name);
            }

            entry.Modified = FileInfo.LastWriteTimeUtc;

            entry.TempFile = StoreStreamInTempFile(archive_stream, GetExtension(entry.Name), entry.Modified);

            if (entry.TempFile != null)
            {
                entry.Size     = new FileInfo(entry.TempFile).Length;
                entry.MimeType = XdgMime.GetMimeType(entry.TempFile);
            }

            handled_single = true;

            return(entry);
        }
Esempio n. 4
0
        private ArchiveEntry GetNextEntryZip()
        {
            ZipInputStream zip_stream = (ZipInputStream)archive_stream;
            ZipEntry       zip_entry;

            do
            {
                zip_entry = zip_stream.GetNextEntry();
            } while (zip_entry != null && zip_entry.IsDirectory);

            // End of the entries.
            if (zip_entry == null)
            {
                return(null);
            }

            ArchiveEntry entry = new ArchiveEntry();

            entry.Name     = zip_entry.Name;
            entry.Modified = zip_entry.DateTime;             // FIXME: Not sure zip_entry.DateTime is UTC.
            entry.Comment  = zip_entry.Comment;
            entry.Size     = zip_entry.Size;

            // Only index smaller subfiles, to avoid filling /tmp
            if (entry.Size > MAX_SINGLE_FILE)
            {
                Log.Debug("Skipping over large file {0} in {1}", entry.Name, Indexable.DisplayUri.ToString());
                return(entry);
            }

            entry.TempFile = StoreStreamInTempFile(archive_stream, GetExtension(entry.Name), entry.Modified);
            if (entry.TempFile != null)
            {
                entry.MimeType = XdgMime.GetMimeType(entry.TempFile);
            }

            return(entry);
        }
Esempio n. 5
0
        private ArchiveEntry GetNextEntryTar()
        {
            TarInputStream tar_stream = (TarInputStream)archive_stream;
            TarEntry       tar_entry;

            do
            {
                tar_entry = tar_stream.GetNextEntry();
            } while (tar_entry != null && tar_entry.IsDirectory);

            // End of the entries;
            if (tar_entry == null)
            {
                return(null);
            }

            ArchiveEntry entry = new ArchiveEntry();

            entry.Name     = tar_entry.Name;
            entry.Modified = tar_entry.ModTime;
            entry.Size     = tar_entry.Size;

            // Only index smaller subfiles, to avoid filling /tmp
            if (entry.Size > MAX_SINGLE_FILE)
            {
                Log.Debug("Skipping over large file {0} in {1}", entry.Name, Indexable.DisplayUri.ToString());
                return(entry);
            }

            entry.TempFile = StoreStreamInTempFile(archive_stream, GetExtension(entry.Name), entry.Modified);
            if (entry.TempFile != null)
            {
                entry.MimeType = XdgMime.GetMimeType(entry.TempFile);
            }

            return(entry);
        }
Esempio n. 6
0
        /* Returns false if content can't/needn't be indexed.
         * If AlreadyFiltered, then we don't return a filter but return true.
         */
        static public bool FilterIndexable(Indexable indexable, TextCache text_cache, out Filter filter)
        {
            filter = null;
            ICollection filters = null;

            if (indexable.Filtering == IndexableFiltering.AlreadyFiltered)
            {
                return(true);
            }

            if (!ShouldWeFilterThis(indexable))
            {
                return(false);
            }

            string path = null;

            // First, figure out which filter we should use to deal with
            // the indexable.

            // If a specific mime type is specified, try to index as that type.
            if (indexable.MimeType != null)
            {
                filters = CreateFiltersFromMimeType(indexable.MimeType);
            }

            if (indexable.ContentUri.IsFile)
            {
                path = indexable.ContentUri.LocalPath;

                // Otherwise, set the mime type for a directory,
                // or sniff it from the file.
                if (indexable.MimeType == null)
                {
                    if (Directory.Exists(path))
                    {
                        indexable.MimeType  = "inode/directory";
                        indexable.NoContent = true;
                    }
                    else if (File.Exists(path))
                    {
                        indexable.MimeType = XdgMime.GetMimeType(path);
                    }
                    else
                    {
                        Log.Warn("Unable to filter {0}.  {1} not found.", indexable.DisplayUri, path);
                        return(false);
                    }
                }

                // Set the timestamp to the last write time, if it isn't
                // set by the backend.
                if (!indexable.ValidTimestamp && indexable.IsNonTransient)
                {
                    indexable.Timestamp = FileSystem.GetLastWriteTimeUtc(path);
                }

                // Check the timestamp to make sure the file hasn't
                // disappeared from underneath us.
                if (!FileSystem.ExistsByDateTime(indexable.Timestamp))
                {
                    Log.Warn("Unable to filter {0}.  {1} appears to have disappeared from underneath us", indexable.DisplayUri, path);
                    return(false);
                }

                if (filters == null || filters.Count == 0)
                {
                    filters = CreateFiltersFromIndexable(indexable);
                }
            }

            // We don't know how to filter this, so there is nothing else to do.
            if (filters.Count == 0)
            {
                if (!indexable.NoContent)
                {
                    Logger.Log.Debug("No filter for {0} ({1}) [{2}]", indexable.DisplayUri, path, indexable.MimeType);
                }

                return(false);
            }

            foreach (Filter candidate_filter in filters)
            {
                if (Debug)
                {
                    Logger.Log.Debug("Testing filter: {0}", candidate_filter);
                }

                // Hook up the snippet writer.
                if (candidate_filter.SnippetMode && text_cache != null)
                {
                    if (candidate_filter.OriginalIsText && indexable.IsNonTransient)
                    {
                        text_cache.MarkAsSelfCached(indexable.Uri);
                    }
                    else if (indexable.CacheContent)
                    {
                        TextWriter writer = text_cache.GetWriter(indexable.Uri);
                        candidate_filter.AttachSnippetWriter(writer);
                    }
                }

                // Set the indexable on the filter.
                candidate_filter.Indexable = indexable;

                // Open the filter, copy the file's properties to the indexable,
                // and hook up the TextReaders.

                bool       successful_open = false;
                TextReader text_reader;
                Stream     binary_stream;

                if (path != null)
                {
                    successful_open = candidate_filter.Open(path);
                }
                else if ((text_reader = indexable.GetTextReader()) != null)
                {
                    successful_open = candidate_filter.Open(text_reader);
                }
                else if ((binary_stream = indexable.GetBinaryStream()) != null)
                {
                    successful_open = candidate_filter.Open(binary_stream);
                }

                if (successful_open)
                {
                    // Set FileType
                    indexable.AddProperty(Property.NewKeyword("beagrep:FileType", candidate_filter.FileType));

                    indexable.SetTextReader(candidate_filter.GetTextReader());
                    indexable.SetHotTextReader(candidate_filter.GetHotTextReader());

                    if (Debug)
                    {
                        Logger.Log.Debug("Successfully filtered {0} with {1}", path, candidate_filter);
                    }

                    filter = candidate_filter;
                    return(true);
                }
                else
                {
                    Log.Warn("Error in filtering {0} with {1}, falling back", path, candidate_filter);
                    candidate_filter.Cleanup();
                }
            }

            if (Debug)
            {
                Logger.Log.Debug("None of the matching filters could process the file: {0}", path);
            }

            return(false);
        }