pub struct MimeDb { /* private fields */ }Expand description
Everything the machine says about types and the applications that open them, read once.
Reading it is a few small files; doing it per double-click would be a
few small files per double-click, so a host builds one of these and
keeps it. It is a snapshot: installing an application while a window
is open will not show up until MimeDb::load is called again.
Implementations§
Source§impl MimeDb
impl MimeDb
Sourcepub fn load() -> MimeDb
pub fn load() -> MimeDb
Reads the database from the XDG directories.
Never fails: a missing file is a machine without that piece,
which is a smaller answer rather than an error — the same
distinction the config loaders draw. What a caller can act on is
MimeDb::knows_types.
Sourcepub fn load_from(data_dirs: &[PathBuf], mimeapps: &[PathBuf]) -> MimeDb
pub fn load_from(data_dirs: &[PathBuf], mimeapps: &[PathBuf]) -> MimeDb
MimeDb::load from given directories — the seam the tests use,
and what lets a caller point this at a fixture instead of the
machine it is running on.
Sourcepub fn knows_types(&self) -> bool
pub fn knows_types(&self) -> bool
Whether any filename rules were found at all. false means the
machine has no shared-mime-info, which a chooser should say
rather than reporting every file as unknown.
Sourcepub fn type_of(&self, path: &Path) -> Option<&str>
pub fn type_of(&self, path: &Path) -> Option<&str>
The type of a file by name alone, doing no I/O. None when
no rule matches — see globs::Globs::type_of for why that is
not octet-stream.
This is the one to call from a UI thread, and the one to call
about a file that may not exist yet (a save dialog’s filename
box). MimeDb::sniff is the same question asked properly, at
the cost of reading the file.
Sourcepub fn sniff(&self, path: &Path) -> Found
pub fn sniff(&self, path: &Path) -> Found
What a file actually is: the name, its contents, and what kind
of thing it is on disk, in the order lookup documents.
Reads the file, so it belongs off the UI thread for anything that might be on a slow mount.
Sourcepub fn is_subclass_of(&self, mime: &str, parent: &str) -> bool
pub fn is_subclass_of(&self, mime: &str, parent: &str) -> bool
Whether an application registered for parent should be able to
open a mime.
Sourcepub fn icon_names(&self, mime: &str) -> Vec<String>
pub fn icon_names(&self, mime: &str) -> Vec<String>
Every icon name worth asking an icon theme for, for mime, best
first — see icons. Names only; which file each one is, is the
icon theme’s question.
Sourcepub fn canonical<'a>(&'a self, mime: &'a str) -> &'a str
pub fn canonical<'a>(&'a self, mime: &'a str) -> &'a str
The name the database uses for a type, resolving an alias.
Sourcepub fn lookup(&self) -> &Lookup
pub fn lookup(&self) -> &Lookup
Everything about types, for a caller that needs the parts
directly — the mimetype command does.
Sourcepub fn set_default(&self, mime: &str, app: &str) -> Result<()>
pub fn set_default(&self, mime: &str, app: &str) -> Result<()>
Makes app the default for mime, resolving an alias first.
A method rather than the free set_default because the read
half of this database canonicalizes (default_for, apps_for)
and the write half did not: a caller naming a type by an alias
wrote a line nothing would ever read back, and the next reload
showed the old default — which reads as “the setting didn’t
take” with nothing anywhere reporting a failure.
Sourcepub fn clear_default(&self, mime: &str) -> Result<()>
pub fn clear_default(&self, mime: &str) -> Result<()>
Forgets the default for mime, resolving an alias first — the
counterpart to MimeDb::set_default, for a person undoing a
choice rather than making a different one.
Worth telling apart from setting a different application: with no line at all, the desktop falls back to whatever registered for the type, which is where a fresh machine starts. Setting “none” is not expressible, so “put it back how it was” has no other spelling.
This edits the one file this crate writes. A default that came
from a system-wide mimeapps.list is not this suite’s to remove,
and clearing will leave it standing — see MimeDb::chosen_types
for the read side of the same distinction.
Sourcepub fn known_types(&self) -> Vec<&str>
pub fn known_types(&self) -> Vec<&str>
Every type this machine has a name rule for, or that something registered for, or that a default was recorded for. Sorted, and each type once.
Aliases are left out: each resolves to a name already in the list, and offering both makes one row of a search look like two settings that could disagree.
Sourcepub fn chosen_types(&self) -> Vec<&str>
pub fn chosen_types(&self) -> Vec<&str>
The types somebody has actually chosen an application for, from
every mimeapps.list that applies.
The list nothing on this desktop shows. A mimeapps.list
accumulates over years, and an entry that has gone stale — the
fstl case in MimeDb::default_for — stays invisible until a
file quietly fails to open. Sorted, since it comes out of a
BTreeMap.
Sourcepub fn apps_for(&self, mime: &str) -> Vec<&App>
pub fn apps_for(&self, mime: &str) -> Vec<&App>
Every installed application registered for mime, the default
first and the rest in the order the desktop lists them.
Applications whose program is missing are left out: this is the
list a person is about to pick from, and an entry that cannot
run is a dead end rather than a choice. MimeDb::default_for
still reports a missing default, because “your default is gone”
is worth saying out loud.
Sourcepub fn apps_for_including_parents(&self, mime: &str) -> Vec<&App>
pub fn apps_for_including_parents(&self, mime: &str) -> Vec<&App>
MimeDb::apps_for, then the applications for every type this
one is a kind of — an archive manager can open a 3MF, and a
text editor can open a shell script.
The reference implementation’s mime_applications_all. Most
callers want MimeDb::candidates instead, which says which of
the two each application is.
Sourcepub fn candidates(&self, mime: &str) -> Vec<Candidate<'_>>
pub fn candidates(&self, mime: &str) -> Vec<Candidate<'_>>
Everything that could open this type, ranked, and labelled with why it is offered.
One query rather than a ranking each caller works out for
itself. The file manager’s chooser and mimeopen both ask “what
can open this”, and each used to diff apps_for against
apps_for_including_parents to recover the distinction — with
the result that the two already disagreed about the order they
offered. A third consumer (a viewer, a portal) would have made
it three.
Sourcepub fn default_for(&self, mime: &str) -> Option<&App>
pub fn default_for(&self, mime: &str) -> Option<&App>
The application that opens mime today, whether or not it is
installed — so a caller can say “your default is fstl, which
isn’t installed any more” instead of quietly offering something
else.