MIME type for apps

I packaged TeXmacs for Haiku (with great support from @Begasus). However there is still no associated MIME types at the system level for TeXmacs documents. What is the preferred way for an application (ported) to install new MIME types. Can it be done while the package in installed?

1 Like

New MIME types are created by adding the appropriate file in the data/mime_db/ directory, which should then automatically get picked up. Your app’s resources can then declare it supports reading those files.

I think there are some applications in HaikuPorts which export mime types, but I’m not sure which ones, so I’m not sure what the mechanism chosen to export those is. @Diver may know more.

2 Likes

I’m not sure if that’s what you need but maybe this could give you some hint https://github.com/haikuports/haikuports/blob/master/www-client/otter-browser/additional-files/otter.rdef.in#L16

1 Like

Where I can read about creating those files in data/mime_db? It seems they contains only metadata in attributes. I then understand that I can can declare the mime types my app handles via the .rdef file. However how to have all the files with .tm extension to be handled that way? TeXmacs is a Qt app, so I would maybe need to add some code to help the OS identify the files it handles. Where I can read more about this?

Some more context for my question: TeXmacs has a xml file describing the mime types for documents here https://github.com/texmacs/texmacs/blob/master/TeXmacs/misc/mime/texmacs.xml It is used on Linux desktop environments. It would be great if there is an automatic way to create the relative mime entries in Haiku’s database.

You can check Haiku’s repository for the rdef files in the mime_db directory. I guess this one is a good example: https://github.com/haiku/haiku/blob/master/src/data/mime_db/application/vnd.ms-xpsdocument

I think there is a reference somewhere for the “sniff rule” syntax but I’m not sure where. You can also add custom icons (some of the other “application” mimetypes do; otherwise they will inherit the supertype’s icon.)

2 Likes

In Darkwyrm’s book Programming With Haiku. We don’t have migrated it to the official docs yet, we probably should.

The important detail here: file associations in Haiku are not handled by file extensions, but by MIME types. The MIME type is stored as an extended attribute of each file. Well-behaved apps should set it when saving a file. But since unidentified files happen (for example, you copied them from another filesystem where it’s not possible to store the MIME type), we additionally have “sniffing rules” that will allow identifying a file from its contents. They do simple pattern-matching on the first 512 bytes of the file data. Detecting .tex file can probably done reliably enough by looking at a { there?

1 Like

Thanks, things are clearer now. I was not sure how files which are saved from an app which is not “native” and therefore do not set attributes, were handled. When this association of MIME attributes takes place? When the file is created in the filesystem? When I double click on it on the UI? What is the preferred way to create the mime description files to put into the database? (Where the translations of the various descriptions there goes???).

Btw, TeXmacs use its own format (does not depend on TeX in any way, despite of the name…). We use the ‘text/x-texmacs’ string for the mime type of the documents.

The sniffing is done when double clicking in the UI if the file is not identified yet. We will probably make it happen in the background for all files in an open Tracker window (and in particular, file open dialogs which may filter things by MIME type). It can be done from the command line using the mimeset tool as well.

Registering a MIME type can be done from the app itself if you want to write platform specific code: https://www.haiku-os.org/legacy-docs/bebook/BMimeType.html or as mentionned above you could simply ship the mimetype file in your application package so that it is put in data/mime_db, which is probably how ported applications will do this. I don’t have further details on the latter because I never did that myself, you’ll have to search haikuports for an example, I guess.

1 Like

I don’t think Haiku supports localized description for filetypes.

For reference, here’s the description of the Sniff Rule format, extracted from the R5 BeBook:

Sniffer Rule

The sniffer rule is a string that follows this format:

   rating
[begin:end] ( [begin:end] 'pattern1' | [begin:end] 'pattern2' |
... )

  • rating is a mandatory float between 0.0 and 1.0 that describes the priority of the rule, compared to other rules for the same type.

  • [begin:end] is the optional "global byte range" declaration. The sniffer will look for a matching pattern that begins within this range of data. The [begin:end] that appears outside the parenthesis (i.e. just after rating) applies to all the patterns for this rule. If a pattern must begin at a particular byte offset, you can exclude the ":end" declaration.

  • 'pattern1', 'pattern2', etc. These are the patterns themselves. You can OR patterns with the "|" operator. (You can't AND patterns.) An immediately preceding byte range declaration applies to the following pattern (only), and overrides the global byte range declaration.

If no byte range declaration is provided, [0:0] is assumed—i.e. the pattern(s) must start at the first byte.

For example, the following rule strings says that the content of an examined file must begin with the characters 'ABCD':

   "1.0 ('ABCD')"

This rule says that the content must contain the characters 'ABCD' or 'abcd' starting somewhere in the first three bytes:

   "1.0 [0:3]
('ABCD' | 'abcd')"

Here the content must contain 'ABCD' or 'abcd' starting in the first three bytes, or it must contain 'EFGH' starting at byte 13:

   "1.0 [0:3] ('ABCD' |
'abcd' | [13] 'EFGH')"



You can use ‘setmime’ to manage mimetypes. Try:

   setmime -dump text/html

to see a good example of a mime_db entry. The output is in the form of the command line that would be needed to generate the entry. You should be able to substitute your own values for TexMacs.

4 Likes

Strangely, while scouring the web, I found the relevant chapter of DarkWyrm’s book… on haiku-os.org! And somewhat stranger, there seems to be no way to reach it from the home page itself!

Anyway, as it has quite a bit more detail than the original BeBook description, here’s the full link to the PDF:

1 Like

Works here. Both books are linked under “Learning to Code in C++” at Development | Haiku Project .

A reorganization of the website is long overdue, in any case.

As for the book, Darkwyrm relicensed it to allow updates and corrections and sent me the sources for it. I’ve been converting the odt files to asciidoc and putting the results here: https://github.com/pulkomandy/programming-with-haiku

This means we can make updates and corrections to it, should that ever be needed, and share it in different formats.

1 Like

Hah! So it is… I was looking more in ‘Documents’ Thanks.

I just looked up that format, and it seems to have a strong underlying similarity to Haiku’s mime_db. (If much more bloated, as is usual from the Linux world :slightly_smiling_face: ) A translator might be an interesting project. Maybe GSoC?

GSoC projects are for things that require about 3 months of work. This is something that could probable be done in a week or so, right?

Probably… :slightly_smiling_face: It was not a heavily researched suggestion. Not something I’m particularly interested in doing myself.

In any case, it looks like the sniff rule feature is almost never used – Texmacs is one of the few. I found the mime-info XML files in my Linux setup,and none of them use it! All based on the extension.

I’ve tried to add mime definitions for the documents in the TeXmacs package in Haikuports. The package now contains the files /system/data/mime_db/text/x-texmacs.{doc,sty} however when I install the package they are not picked up by the system and do not appear in FileTypes

You can check my changes to the recipe here

I would appreciate some hints on what I’m doing wrong…

IIRC, sometimes you need to reboot for FIleTypes to pick up new entries.