Create Associativity between file extension & application? - c++

I am creating an application using Qt. The files saved from my application have their own file format & extension. I want to associate this file format with my application.
So, when a user double clicks say myfile.ext then the OS should automatically run my application (how to specify its path?) & pass path of file as an argument.
I have only used Windows until now so I am talking in terms of it. I dont know how other OS's behave. Any guidance for the same would be helpful?
PS: Even though I am using Qt which apparently has no special functionality to do what is asked here (AFAIK), it would be awesome to know how to do this in standard C++.

For Windows see the MSDN page "File Type Handlers" for how to register file type handlers.
On many popular Linux desktops, so-called .desktop files are used to define what application to use for opening a file. See the "Desktop Entry Specification" for the structure of those .desktop files. They are quite simple really, changes are you can just take an existing file from any of the popular Linux desktop environments (e.g. KDE) and modify it a bit.

It's a function of the OS, not of the C++ language.
You'll need to figure it out on each platform you are supporting.

Related

Qt Single Application file opened

I am making an Qt Application.
And I have 2 problems that kind hard for me to figure out :
I create my own file type for my application document, so let say "something.ap". I want my OSes (Windows and Mac, maybe linux), when double click those file on file explorer, it will open using my application. How to implement this on Qt in easy way? On Windows I saw we need to edit the registry, but if there is a good way that will be best thing. And I don't have idea on Mac and Linux.
Related to number one. When I already open my "something.ap", and then I double click the same file ("something.ap"), I won't my application open again on new instance. So the behavior I want is to check if already file opened, it will raise the window of the application. I saw there is QtSingleApplication, but when I check the repository on gitorous can not be access.
Thank you for any help, and sorry if you can't understand my English.
You need to register mime type. check this
Singleton application is available here. It's not maintained any more :P. You can download source as zip or clone the git repository.

What is the best way to use IE webBrowser control without requiring MS/Visual studio run times?

What I am trying to do is create an exe (not .hta) that can be programmed to open a local html file and have it work just like it was in IE (handle styles, scripts and etc), only without actually loading IE. The exe must also be run without installation requirements, and no dependencies (other than recent IE obviously), needed on the host machine.
The purpose is to create an interactive e-book type document, that can be edited without recompile, and also be run from cd/usb on the fly.
I can't just open IE, because it will look unprofessional with the big E, the toolbar and tabs all at the top. know what I mean?
I have experience with HTA, and this was my first suggestion, however I would like to take advantage of the auto run from cd, and also to make the form border a little different, less plain.
I also have experience in VB. Which I could accomplish the look and functionality, however those vbruntimes are not installed on some machines :(
Is it possible to use the IE webBrowser control from c++ or even assembly?
Thanks in advance!
It appears, Lucian Wischik's Webform can be a good starting point to meet your minimalistic project requirements. Alternatively, you could host WebBrowser ActiveX control directly using ATL AxHost. ATL can be linked statically and adds a very low size overhead to the final EXE. Here is a sample project of WebBrowser host app which only depends on ATL.
Yes, you can use the browser component from C++.
C++ however will again need runtime libraries, similar to VB. The good news is, you can statically link those runtime libraries with the executable.
You will not be able to statically link the browser component with your executable, but that wont be an issue unless you try to support ancient Windows versions (pre NT 4.0). For all more recent Windows versions, you can pretty much assume that this component is available.
For more details on that component, you may refer to the CHtmlView class reference - that is the MFC wrapped version of that WebBrowser activeX component.
Using that component from assembler certainly is possible as well, but unless you are deeeeeeep into system programming on the windows platform, you will fail.

cross platform native open/save file dialogs

I'm writing a UI for my program using opengl with SDL in a combination of Lua and C++
What I need now is some library that will allow me to call a function that presents the user with a file select dialog for opening/saving a file. But if the OS offers native functionality for such a dialog, then I want to use that dialog (eg Window's GetOpenFileName).
The only platforms I need to support are Windows and Linux, but I want to be able to still use most of the SDL & openGL code I've already written.
What options are available?
tinyfiledialogs offers many modal dialogs and popup notifications (both for graphic and console modes). It is a cross-platform C file (with a header) to add to your C or C++ project. It aims to be extremely easy to use, has no init, no main loop, and no external dependencies. It is used by hundreds of projects on GitHub and of course, I am the author. Get it here: http://tinyfiledialogs.sourceforge.net
Usage example:
char const * selection = tinyfd_openFileDialog( // there is also a wchar_t version
"Select file", // title
"C:\\", // optional initial directory
2, // number of filter patterns
lFilterPatterns, // char const * lFilterPatterns[2] = { "*.txt", "*.jpg" };
NULL, // optional filter description
0 // forbid multiple selections
);
You should take a good look at Native File Dialog. It is a small, portable C library that lets you use load and save dialogs in a cross platform manner without linking huge dependencies like qt or wxWidgets.
I am the author, and I use it with SDL2 and OpenGL on Linux, Mac and Windows.
https://github.com/mlabbe/nativefiledialog
In my opinion, wxWidgets is the open source, battle tested, mature, cross platform, cross language, free, open source, GUI library. Beside wxWidgets, there are also many other such cross platform libraries, such as:
Tk
FLTK
FOX
XUL
AbiWord's cross platform layer
... and more.
You will most likely find you're looking for in the AbiWord source code. Look for "FileOpen" inside the src directory:
./af/xap/cocoa/xap_CocoaDlg_FileOpenSaveAs.cpp
./af/xap/cocoa/xap_CocoaDlg_FileOpenSaveAs.h
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib/classes.nib
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib/info.nib
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib/keyedobjects.nib
./af/xap/gtk/xap_UnixDlg_FileOpenSaveAs.cpp
./af/xap/gtk/xap_UnixDlg_FileOpenSaveAs.h
./af/xap/win/xap_Win32Dlg_FileOpenSaveAs.cpp
./af/xap/win/xap_Win32Dlg_FileOpenSaveAs.h
./af/xap/xp/xap_Dlg_FileOpenSaveAs.cpp
./af/xap/xp/xap_Dlg_FileOpenSaveAs.h
The Windows version does indeed use GetOpenFileName().
Of course, as Andre points out while downvoting a valid alternative answer, there's also Qt, as you already know. Refer to raj's answer for a link.
https://doc.qt.io/qt-5/qfiledialog.html provides a good abstraction of os file open services
I have used another good library:
https://github.com/samhocevar/portable-file-dialogs
It is a single header library with support for Linux / Mac / Windows

Is it posible to open a cind of browse for file window to get the filepath of a file in c++?

And if this is possible pleas explain how to do this.
If you are just looking for a path and are on Windows, you probably want something like SHBrowseForFolder. If you are using a framework that abstracts away a lot of low-level Windows API stuff (MFC, Qt, WxWidgets, etc), then it may have some other more convenient function or class that does the same thing.
If you're not on Windows, then I can't help except by suggesting you search the operating system's API and/or the documentation of whatever GUI framework you use.

Is there a non-java, cross platform way to launch the associated application for a certain file type?

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.
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:mail#example.com 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.
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.
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).