Skip to main content

MimeDb

Struct MimeDb 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn is_subclass_of(&self, mime: &str, parent: &str) -> bool

Whether an application registered for parent should be able to open a mime.

Source

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.

Source

pub fn canonical<'a>(&'a self, mime: &'a str) -> &'a str

The name the database uses for a type, resolving an alias.

Source

pub fn lookup(&self) -> &Lookup

Everything about types, for a caller that needs the parts directly — the mimetype command does.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn all_apps(&self) -> Vec<&App>

Every installed application, for the “Other…” list — a person opening a file with something that never registered for its type is a normal thing to want.

Source

pub fn app(&self, id: &str) -> Option<&App>

One application by its entry’s file name.

Trait Implementations§

Source§

impl Clone for MimeDb

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MimeDb

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MimeDb

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for MimeDb

Source§

impl PartialEq for MimeDb

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for MimeDb

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.