PDA

View Full Version : Opening swf file in the default browser



munna
2nd May 2006, 05:52
Hi,

I used ShellExecuteW with the path to the swf file but it is not opening it. This is probably because the swf file is not associated with the browser.

I believe to do this I would first have to know that path to the default browser and then use QProcess to open the swf file with the default browser.

Can anyone plese tell me how to do this ?

Thanks a lot.

nupul
2nd May 2006, 07:40
Hi,

I used ShellExecuteW with the path to the swf file but it is not opening it. This is probably because the swf file is not associated with the browser.

I believe to do this I would first have to know that path to the default browser and then use QProcess to open the swf file with the default browser.

Can anyone plese tell me how to do this ?

Thanks a lot.

This is easy on Unix/X11:

for KDE:

$> kfmclient exec <filename>

this will open the file with the default binding if at all it exists...i.e. any app that associates itself with .swf MIME type.

for GNOME:

$>gnome-open <filename>

for WINDOWS:

I can't remember at all!! :D

Nupul

SkripT
2nd May 2006, 14:59
Hi,

I used ShellExecuteW with the path to the swf file but it is not opening it. This is probably because the swf file is not associated with the browser.

I believe to do this I would first have to know that path to the default browser and then use QProcess to open the swf file with the default browser.

Can anyone plese tell me how to do this ?

Thanks a lot.

If no application is associated with the SWF file then is normal that ShellExecuteW doesn't open nothing... Have you tried it associating the swf file to a browser?

munna
2nd May 2006, 16:46
Have you tried it associating the swf file to a browser?

No, I did not try this because even if I associate, the user of the application will not have swf files associated with the browser.

Is there a way by which I can know that path to the default browser?

Thanks a lot.

nupul
2nd May 2006, 19:23
No, I did not try this because even if I associate, the user of the application will not have swf files associated with the browser.

Is there a way by which I can know that path to the default browser?

Thanks a lot.

why do you need to do this? :confused: I gave you a method above. It will open the .swf file on the user's comp if at all he has some default application to do so!

as for checking the default browser is considered....an swf file will open in any compatible browser, so you don't need to worry about this!

and if you are really hell bent on wanting to show your application in the default browser then go through the man pages for 'kfmclient' maybe you could tweak around with certain options and get what you are looking for!

On Windows...........................:p i can't think of a way to do this...maybe some one could help ;)

Nupul

munna
2nd May 2006, 19:27
Unfortunately I want it for windows.

nupul
3rd May 2006, 18:46
Unfortunately I want it for windows.


I too will wait for this soln ;)

if you find it out before I do, post it here! else...............................I will :D

Cheers

Nupul.

Chicken Blood Machine
3rd May 2006, 19:05
I too will wait for this soln ;)

if you find it out before I do, post it here! else...............................I will :D

Cheers

Nupul.

The Windows ShellExecuteW() solution does exactly the same as the solutions you have provided for linux. They launch the associated application if there is already an association for that file. If the user has not yet made an association, either by creating it herself ar installing a program that makes that association, there is very little you can do about that.

nupul
4th May 2006, 03:42
The Windows ShellExecuteW() solution .....

That's It!!!!!!!!!!

This is what I was trying to remember for all these days!!!!! I overlooked it in the question and didn't put much thought...I thought there was something else (there isn't, right?)

Thanks :D (the biggest smile.......no smiley for that!)

Nupul

Chicken Blood Machine
4th May 2006, 05:45
Well Munna did mention it quite clearly at the beginning of this thread! :rolleyes:

munna
4th May 2006, 06:15
So should I assume that what I want to achieve is just not possible ?

Chicken Blood Machine
4th May 2006, 06:27
So should I assume that what I want to achieve is just not possible ?

That depends. What do you mean by default browser? Default browser of what?

munna
4th May 2006, 06:32
Default browser of the user's system which is running a Windows OS.

Chicken Blood Machine
4th May 2006, 06:34
Default browser of the user's system which is running a Windows OS.

Yes, but default browser of what? What does it browse? What is the kind of data, the file type?

munna
4th May 2006, 06:49
ok, let me explain you the whole thing again.

My application reads a text file which has paths to a few swf files on user's system and shows the list.

Usually swf files are not associated with any application but if you open them in a browser (Internet Explorer,Firefox....) which has the flash pluing installed, the swf file will play.

When user wants to play any of these swf files he clicks on the list item. ShellExecuteW will not work because the swf files are not associated with any application. Therefore if I know the path to the user's default broswer(IE or Firefox or what ever it is) I can use QProcess to open it.

One solution that I found was to embedded the swf into a html file and then open it.

But my question is that, is there a way by which I can find the path to the user's default internet browser.

Chicken Blood Machine
4th May 2006, 17:22
ok, let me explain you the whole thing again.

My application reads a text file which has paths to a few swf files on user's system and shows the list.

Usually swf files are not associated with any application but if you open them in a browser (Internet Explorer,Firefox....) which has the flash pluing installed, the swf file will play.

When user wants to play any of these swf files he clicks on the list item. ShellExecuteW will not work because the swf files are not associated with any application. Therefore if I know the path to the user's default broswer(IE or Firefox or what ever it is) I can use QProcess to open it.

One solution that I found was to embedded the swf into a html file and then open it.

But my question is that, is there a way by which I can find the path to the user's default internet browser.

Munna,

I understood your problem exactly as you described it. But, I don't think you understand what you are asking for. What is a 'default browser'? What you actually mean is the default program for viewing HTML files. This of course is easy to find (as you have seen) with using ShellExecuteW() on a HTML file. Why is this? becasue at some time in the past, the user associated files of type HTML with a certain browser, or more likely, the browser did it itself when it was installed.

Now I hope you understand. There is no such thing as a default "browser", just a default program associated with a particular file type. Your problem is that there is no program associated with the swf file type. Therefore you will have to use Windows APIs to hack the registry yourself and associate it with the program used to open HTML files (this may not even work if the web browser doesn't have the plugin that you need).

I wouldn't recommend this at all. Embedding the file in a HTML file as you have done is probably the best way to go, or bringing up the Windows dialog that prompts the user to locate a program to open a particular file type (or search the internet for a compatible program). I don't know how to do that though. Try MSDN.

Xagen
5th May 2006, 09:33
Hi! This is my old code but it works and maybe you'll find it useful:

this class works only on Windows. It tries to launch the specified filename in this way:
- takes the filename extention and look the associated program in the registry
- launches this app with our file.

This code is not final and is dirty cause it was written in the very fast way and needs to be modified. So take it as example!

=== winfileexecutor.h ===


#ifndef WINFILEEXECUTOR_H_
#define WINFILEEXECUTOR_H_

#include <QString>

/*!
\class WinFileExecutor

\brief The WinFileExecutor class provides support to launch book in associated program.
*/

class WinFileExecutor
{
public:
WinFileExecutor(const QString &fileName);
virtual ~WinFileExecutor() {}

QString getKeyPath(const QString _ext) const;
QString getOpenExe(const QString _keyPath) const;

private:
QString _fileName;

};

#endif

=== winfileexecutor.cpp ===


#include "winfileexecutor.h"

#include <qt_windows.h>
#include <QMessageBox>
#include <QFileInfo>
#include <QProcess>

/*!
How to find program associated with this file type?
It's easy!

ONLY FOR WINDOWS OS!
We must access regestry.
The in hkcr find the decription of the extention of the file.
Then we must find the description key and read the open command!

*/

static QString getWindowsRegString( HKEY key, const QString &subKey )
{
QString s;
QT_WA( {
char buf[1024];
DWORD bsz = sizeof(buf);
int r = RegQueryValueExW( key, (TCHAR*)subKey.utf16(), 0, 0, (LPBYTE)buf, &bsz );
if ( r == ERROR_SUCCESS ) {
s = QString::fromUtf16( (unsigned short *)buf );
} else if ( r == ERROR_MORE_DATA ) {
char *ptr = new char[bsz+1];
r = RegQueryValueEx( key, (TCHAR*)subKey.utf16(), 0, 0, (LPBYTE)ptr, &bsz );
if ( r == ERROR_SUCCESS )
s = ptr;
delete [] ptr;
}
} , {
char buf[512];
DWORD bsz = sizeof(buf);
int r = RegQueryValueExA( key, subKey.toLocal8Bit(), 0, 0, (LPBYTE)buf, &bsz );
if ( r == ERROR_SUCCESS ) {
s = buf;
} else if ( r == ERROR_MORE_DATA ) {
char *ptr = new char[bsz+1];
r = RegQueryValueExA( key, subKey.toLocal8Bit(), 0, 0, (LPBYTE)ptr, &bsz );
if ( r == ERROR_SUCCESS )
s = ptr;
delete [] ptr;
}
} );
return s;
}

WinFileExecutor::WinFileExecutor(const QString &fileName)
: _fileName(fileName)
{
if (_fileName.isEmpty())
return;

QFileInfo fi(_fileName);
//QString ext = fi.completeSuffix();
QString ext = fi.suffix().prepend(".");

QMessageBox::warning(0, ext, _fileName );

QString _prog = getOpenExe( getKeyPath(ext) );

if (_prog.isEmpty())
return;

QProcess::startDetached( _prog, QStringList() << fileName );
}

QString WinFileExecutor::getKeyPath(const QString _ext) const
{
HKEY k;
int r;
QString s;

QT_WA( {
r = RegOpenKeyExW( HKEY_CLASSES_ROOT, reinterpret_cast<const wchar_t *>(_ext.utf16()),
0, KEY_READ, &k );
} , {
r = RegOpenKeyExA( HKEY_CLASSES_ROOT, _ext.toLocal8Bit(),
0, KEY_READ, &k );
} );

if ( r == ERROR_SUCCESS )
{
s = getWindowsRegString( k, QString::null );
RegCloseKey( k );
}
else
{
RegCloseKey( k );
}

QMessageBox::warning(0, s, s );

return s;
}

QString WinFileExecutor::getOpenExe(const QString _keyPath) const
{
HKEY k;
int r;
QString s;

QString _fullPath = _keyPath + "\\shell\\Open\\Command";

QT_WA( {
r = RegOpenKeyExW( HKEY_CLASSES_ROOT, reinterpret_cast<const wchar_t *>(_fullPath.utf16()),
0, KEY_READ, &k );
} , {
r = RegOpenKeyExA( HKEY_CLASSES_ROOT, _fullPath.toLocal8Bit(),
0, KEY_READ, &k );
} );

if ( r == ERROR_SUCCESS )
{
s = getWindowsRegString( k, QString::null );
RegCloseKey( k );
}
else
{
RegCloseKey( k );
}

QStringList lst = s.split(" \"");
QString _progName = lst[ 0 ].remove("\"");


QMessageBox::warning(0, s, _progName );

return _progName;
}