1
/* ***** BEGIN LICENSE BLOCK *****
4
* The contents of this file are subject to the Mozilla Public License Version
5
* 1.1 (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
7
* http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
* for the specific language governing rights and limitations under the
14
* The Original Code is mozilla.org Code.
16
* The Initial Developer of the Original Code is
18
* Portions created by the Initial Developer are Copyright (C) 2005
19
* the Initial Developer. All Rights Reserved.
22
* Christopher Aillon <caillon@redhat.com> (Original author)
25
* ***** END LICENSE BLOCK ***** */
27
#include "nsMIMEInfoUnix.h"
31
#include <glib-object.h>
33
static PRLibrary *gnomeLib;
34
static PRLibrary *vfsLib;
36
typedef struct _GnomeProgram GnomeProgram;
37
typedef struct _GnomeModuleInfo GnomeModuleInfo;
40
GNOME_VFS_OK // there's more but we don't care about them.
43
typedef GnomeVFSResult (*_gnome_vfs_mime_application_launch_fn)
44
(GnomeVFSMimeApplication *app,
46
typedef void (*_gnome_vfs_mime_application_free_fn)(GnomeVFSMimeApplication *);
47
typedef GnomeVFSMimeApplication * (*_gnome_vfs_mime_application_copy_fn)(GnomeVFSMimeApplication *);
48
typedef GnomeProgram * (*_gnome_program_init_fn)(const char *, const char *,
49
const GnomeModuleInfo *, int,
50
char **, const char *, ...);
51
typedef const char * (*_gnome_vfs_mime_application_get_name_fn)(GnomeVFSMimeApplication *);
52
typedef const GnomeModuleInfo * (*_libgnome_module_info_get_fn)();
53
typedef GnomeProgram * (*_gnome_program_get_fn)();
54
typedef char * (*_gnome_vfs_make_uri_from_input_fn)(const char *);
56
#define DECL_FUNC_PTR(func) static _##func##_fn _##func
58
DECL_FUNC_PTR(gnome_vfs_mime_application_launch);
59
DECL_FUNC_PTR(gnome_vfs_mime_application_free);
60
DECL_FUNC_PTR(gnome_vfs_mime_application_copy);
61
DECL_FUNC_PTR(gnome_vfs_mime_application_get_name);
62
DECL_FUNC_PTR(gnome_program_init);
63
DECL_FUNC_PTR(gnome_program_get);
64
DECL_FUNC_PTR(libgnome_module_info_get);
65
DECL_FUNC_PTR(gnome_vfs_make_uri_from_input);
68
LoadVersionedLibrary(const char* libName, const char* libVersion)
70
char *platformLibName = PR_GetLibraryName(nsnull, libName);
71
nsCAutoString versionLibName(platformLibName);
72
versionLibName.Append(libVersion);
73
PR_Free(platformLibName);
74
return PR_LoadLibrary(versionLibName.get());
80
// Unload all libraries
82
PR_UnloadLibrary(gnomeLib);
84
PR_UnloadLibrary(vfsLib);
86
gnomeLib = vfsLib = nsnull;
92
static PRBool initialized = PR_FALSE;
97
#define ENSURE_LIB(lib) \
105
#define GET_LIB_FUNCTION(lib, func, failure) \
107
_##func = (_##func##_fn) PR_FindFunctionSymbol(lib##Lib, #func); \
113
// Attempt to open libgnome
114
gnomeLib = LoadVersionedLibrary("gnome-2", ".0");
115
ENSURE_LIB(gnomeLib);
117
GET_LIB_FUNCTION(gnome, gnome_program_init, return Cleanup());
118
GET_LIB_FUNCTION(gnome, libgnome_module_info_get, return Cleanup());
119
GET_LIB_FUNCTION(gnome, gnome_program_get, return Cleanup());
121
// Attempt to open libgnomevfs
122
vfsLib = LoadVersionedLibrary("gnomevfs-2", ".0");
125
GET_LIB_FUNCTION(vfs, gnome_vfs_mime_application_launch, /* do nothing */);
126
GET_LIB_FUNCTION(vfs, gnome_vfs_make_uri_from_input, return Cleanup());
127
GET_LIB_FUNCTION(vfs, gnome_vfs_mime_application_get_name, return Cleanup());
128
GET_LIB_FUNCTION(vfs, gnome_vfs_mime_application_free, return Cleanup());
129
GET_LIB_FUNCTION(vfs, gnome_vfs_mime_application_copy, return Cleanup());
131
// Initialize GNOME, if it's not already initialized. It's not
132
// necessary to tell GNOME about our actual command line arguments.
134
if (!_gnome_program_get()) {
135
char *argv[1] = { "gecko" };
136
_gnome_program_init("Gecko", "1.0", _libgnome_module_info_get(),
140
// Note: after GNOME has been initialized, do not ever unload these
141
// libraries. They register atexit handlers, so if they are unloaded, we'll
146
nsMIMEInfoUnix::SetDefaultGnomeVFSMimeApplication(GnomeVFSMimeApplication* app)
148
if (_gnome_vfs_mime_application_copy && _gnome_vfs_mime_application_free) {
149
mDefaultVFSApplication = _gnome_vfs_mime_application_copy(app);
151
mPreferredAction = nsIMIMEInfo::useSystemDefault;
153
const gchar * name = _gnome_vfs_mime_application_get_name(mDefaultVFSApplication);
155
mDefaultAppDescription = NS_ConvertUTF8toUCS2(name);
159
nsMIMEInfoUnix::~nsMIMEInfoUnix()
161
if (mDefaultVFSApplication)
162
_gnome_vfs_mime_application_free(mDefaultVFSApplication);
166
nsMIMEInfoUnix::LaunchDefaultWithFile(nsIFile* aFile)
168
NS_ENSURE_ARG_POINTER(aFile);
172
if (_gnome_vfs_mime_application_launch && mDefaultVFSApplication) {
173
nsCAutoString nativePath;
174
aFile->GetNativePath(nativePath);
176
gchar *uri = _gnome_vfs_make_uri_from_input(nativePath.get());
179
uris = g_list_append(uris, uri);
181
GnomeVFSResult result = _gnome_vfs_mime_application_launch(mDefaultVFSApplication, uris);
186
if (result != GNOME_VFS_OK)
187
return NS_ERROR_FAILURE;
192
if (!mDefaultApplication)
193
return NS_ERROR_FILE_NOT_FOUND;
195
return LaunchWithIProcess(mDefaultApplication, aFile);