PDA

View Full Version : Associating file extensions



fullmetalcoder
9th May 2006, 13:59
Anyone know how to programatically associate a file extension with a app? I searched the web a bit but didn't find anything relevant...:confused: :crying:

I'd need tips to do that under Win, Linux and Mac...

wysota
9th May 2006, 14:13
What do you mean? You want to register a file type in your operating system? If so, I don't think this is an appropriate forum for such question. Especially that it has no relation to Qt.

fullmetalcoder
9th May 2006, 14:27
I thought it could have...
I find a static functions in QSettings that was called : registerFormat()
It sounded nice but it finally appeared to be a way to create another setting storage format (the built-in ones being INI, system registery under win, and some xml stuff under mac). :(
I am very disappointed that there are no nice way to do it under Qt and I agree with you, this is not the right place : move it to c++ programming... :rolleyes:

wysota
9th May 2006, 14:29
I meant more that it is not the right site to ask that question. Nupul already asked something simmilar here (you can find his posts about it) and did not come to any obvious solutions. Each window manager (or maybe desktop manager, I don't know) has its own system for keeping those associations.

munna
9th May 2006, 14:34
I am not sure but I think the installer that you use to deploy your application does the job of associating file extensions.

fullmetalcoder
9th May 2006, 14:34
Two obvious facts :
- Window$ provides some way to do that but the only apps I found providing a run-time file association dialog (that would need such a facility) weren't Open Source...
- KDE and Gnome are Open Source and the file association code could be borrowed if portable but I don't feel like browsing megs of source code and linking to KDE or Gnome libs is unacceptable for a cross-platform and lightweight project...

:crying:

fullmetalcoder
9th May 2006, 14:36
I am not sure but I think the installer that you use to deploy your application does the job of associating file extensions.

Linux doesn't use any installer and I'd like to provide dynamic association ( the user could choose what extension to associate)

nupul
9th May 2006, 19:33
Anyone know how to programatically associate a file extension with a app? I searched the web a bit but didn't find anything relevant...:confused: :crying:

I'd need tips to do that under Win, Linux and Mac...

well as wysota mentioned...i've already put forth this doubt...and here's the answer

on KDE you have need to look in $XDG_DATA_DIR environ variable...this is where all the apps are installed...

eg: /usr/share/applications

look in mimeinfo.cache in this folder...
you will find a list of file formats and the corresponding apps that can be used for opening them i.e. ".desktop" files that will be found in the same directory.

Now if you want to associate a particular extension with an app you need to add the relevant details to the mimeinfo.cache and provide the apt .desktop file for handling it.

on GNOME you have a file by the name gnome-application-registry (something similar ;) )
where you store the similar type of data. (use the locate tool to find a file of this name)

if you want more (rather lots more info...i'can mail them to you later...)

There is no need to look in the sources...just go on www.freedesktop.org and look for shared-mime-info-spec.....you'll get all the info you need (only if you read that with some concentration and an open mind ;) )

Nupul

fullmetalcoder
10th May 2006, 12:15
Interesting but, as you've already played with that, have you some code snippets ??? And what about Mac and Win? No info at all???:(

nupul
10th May 2006, 13:12
Interesting but, as you've already played with that, have you some code snippets ??? And what about Mac and Win? No info at all???


hmmm...I took another work around in linux...

for KDE I used "kfmclient"

for GNOME i used "gnome-open"

just pass the --help as an arguement to see how to use the system

Now as for working for associating a file is concerned...you need to do this:



1. download the latest freedesktop-mime-database.
2. create your own file following the standard and add it to database
3. run update-mime-database command
4. run update-desktop-database command


so you see there is technically no code snippet ;) just a series of commands...

now re-read the info-spec i pointed out previously....you'll understand more completely...what i just said will act as a glue to merge all the not-so-clear spec ;)

Now for Windows: You'll have to play with MFCs/Win32 API for this...look through the docs...am not very well versed this :( But here is the shortcut....create a custom extension, open it through explorer...and choose app to open that file!!!!....you could also physically do it by setting the "file type" from the tool>options etc....you know the story)

MAC: hmmmm well MAC was a closed source system...they have now started to release out the API, so try getting your hands on it. I haven't used MAC much, but from what i know, it's a harrowing task :eek: to find the API...but i think some googling around would help...

NOTE: The concept of MIME types is present in all systems (i.e. all systems are merging towards this)...try exploiting this...

in one of the threads by munna, he asked how to open a a .swf by using the default app...here's the link....someone put a code snippet that was damn good...it could be of use to you...

here's the link:

http://www.qtcentre.org/forum/showthread.php?t=2029

see how the registry is accesed using Qt and you could use it to your benefit!!

Nupul