views:

176

answers:

3

First, I found a couple of java specific questions and answers for this. I am looking for more "native", but cross platform solution, using C, C++, some kind of shell scripts, or, in my case, Qt.

So the question is, are there standard, cross platform, ways to programmatically open the associated application for certain file types. Or at least to find out if there are associated applications and be able to locate and launch them?

By cross platform I mean Windows, OSX and linux (gnome/kde). The use case is having a database with stored files as blobs that will be read on the three different targets.

+1  A: 

There is the system call in C, for example:

system("main.cpp");

This will open the file using the default editor (Visual Studio in my case). I'm not sure about Linux and Mac, you may need to write "open main.cpp" there (which can be taken care of by #ifdef constructs).

StackedCrooked
Ok so this works on windows (your test) and mac (I just tested it). If there is a linux variant, then this will be the answer I guess. I tried the system("bla.txt") on my osx and my linux boxes, and it did not work, but with "open bla.txt" it worked on my mac.
FeatureCreep
+2  A: 

I don't know of any cross-platform way.

In Windows, there is the start command, which will launch the associated default application. (E.g. start foo.doc will launch the default Word document editor, start http://StackOverflow.Com/ the default web browser and start mailto:[email protected] the default mail app.)

In OS X there is the open command, which does the same thing.

Linux is just an Operating System kernel. OS kernels don't know anything about "filetypes" or "MIME types" or "associated applications" or anything like that. Therefore, such a thing simply cannot exist for Linux.

The Freedesktop Group has a specification for an xdg-open command, which works on all Freedesktop-compliant graphical desktops (be they Linux, FreeBSD, NetBSD, OpenBSD, DragonflyBSD, OpenSolaris or otherwise). However, it is obviously not guaranteed to work on non-Freedesktop systems and it is certainly not guaranteed to work on non-graphical systems.

In all three cases, this is a command line application, not a C or C++ API, but you can obviously call it via system.

Jörg W Mittag
Thanks, xdg-open was the missing part. Also, forgive my use of "linux" as a general term for that flavor of operating system. I think everyone here knows that it is a kernel, but still understand what is meant.
FeatureCreep
I know that I can be a bit pedantic about such things, but in my opinion it is important to be correct about such things: the absolute vast overwhelming majority of Linux computers *does not* support `xdg-open`! My mobile phone, DVD player, cable TV decoder, HDD recorder, NAS box and PDA all run Linux, but none of them has `xdg-open`. Also, every single TiVo on the planet, and almost every WiFi or broadband router runs Linux, and none of them have `xdg-open`. OTOH, a lot of FreeBSD, NetBSD, … desktops, which *don't* run Linux but are XDG-compliant *do* have `xdg-open`.
Jörg W Mittag
So, it really has everything to do with the Freedesktop specification and nothing to do with Linux.
Jörg W Mittag
Yes, I understand that your answer refers to freedesktop, and I think your reply is clear and well written. Though, given the context, I do not think many people who read the _question_ would think that the word "linux" is used to literally mean the linux kernel, but instead as a pointer to a flavor of operating systems. That is at least how I and many people I know use the word on a day to day basis.
FeatureCreep
+1  A: 

Since you have noted that you are using Qt, it's worth mentioning the QDesktopServices class, and especially the openUrl(QUrl) method. It does pretty much what you've described on all platforms supported by Qt.

IgKh
Thanks, this solution actually fits my particular problem better than the "system(...)" solution, though that one is probably more general.
FeatureCreep