~ubuntu-branches/ubuntu/precise/firefox/precise-proposed

« back to all changes in this revision

Viewing changes to uriloader/exthandler/unix/nsMIMEInfoUnix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ian Jackson
  • Date: 2005-12-02 20:16:18 UTC
  • Revision ID: james.westby@ubuntu.com-20051202201618-txu8o1sikp5e3by5
Tags: 1.4.99+1.5rc3.dfsg-1ubuntu4
* Applied mozilla-1.7.12-2.src.rpm's firefox-1.0-uriloader.patch
  which causes firefox to look first in the Gnome MIME handling registry
  (MIME handling is still very wrong).
* Disabled File / Import from main menu since it does not work at all
  (Ubuntu bugzilla 10339 still applies; patch brought forward).
* Displayed printer names no longer show `CUPS/' prefix.
  (Ubuntu bugzilla 11481 regressed; patch from Breezy enhanced).
* Print command fixed (again) to cope with spaces in printer names.
* Remove incorrect note about Pango from README.Debian.
* Re-fix-up localised startup URLs in en-US region.properties.
* Fix incorrect layout/sizing in Help / About box (a bit hacky).
* Re-forward-port patch to fix printer names with spaces.
* Remove all subdirectories '*.OBJ' during clean.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1
 
3
 *
 
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/
 
8
 *
 
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
 
12
 * License.
 
13
 *
 
14
 * The Original Code is mozilla.org Code.
 
15
 *
 
16
 * The Initial Developer of the Original Code is
 
17
 * Red Hat, Inc.
 
18
 * Portions created by the Initial Developer are Copyright (C) 2005
 
19
 * the Initial Developer. All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 *   Christopher Aillon <caillon@redhat.com> (Original author)
 
23
 *
 
24
 *
 
25
 * ***** END LICENSE BLOCK ***** */
 
26
 
 
27
#include "nsMIMEInfoUnix.h"
 
28
#include "prlink.h"
 
29
#include "prmem.h"
 
30
#include <glib.h>
 
31
#include <glib-object.h>
 
32
 
 
33
static PRLibrary *gnomeLib;
 
34
static PRLibrary *vfsLib;
 
35
 
 
36
typedef struct _GnomeProgram GnomeProgram;
 
37
typedef struct _GnomeModuleInfo GnomeModuleInfo;
 
38
 
 
39
typedef enum {
 
40
  GNOME_VFS_OK // there's more but we don't care about them.
 
41
} GnomeVFSResult;
 
42
 
 
43
typedef GnomeVFSResult (*_gnome_vfs_mime_application_launch_fn)
 
44
                              (GnomeVFSMimeApplication *app,
 
45
                               GList *uris);
 
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 *);
 
55
 
 
56
#define DECL_FUNC_PTR(func) static _##func##_fn _##func
 
57
 
 
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);
 
66
 
 
67
static PRLibrary *
 
68
LoadVersionedLibrary(const char* libName, const char* libVersion)
 
69
{
 
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());
 
75
}
 
76
 
 
77
static void
 
78
Cleanup()
 
79
{
 
80
  // Unload all libraries
 
81
  if (gnomeLib)
 
82
    PR_UnloadLibrary(gnomeLib);
 
83
  if (vfsLib)
 
84
    PR_UnloadLibrary(vfsLib);
 
85
 
 
86
  gnomeLib = vfsLib = nsnull;
 
87
}
 
88
 
 
89
static void
 
90
InitGnomeVFS()
 
91
{
 
92
  static PRBool initialized = PR_FALSE;
 
93
 
 
94
  if (initialized)
 
95
    return;
 
96
 
 
97
  #define ENSURE_LIB(lib) \
 
98
    PR_BEGIN_MACRO \
 
99
    if (!lib) { \
 
100
      Cleanup(); \
 
101
      return; \
 
102
    } \
 
103
    PR_END_MACRO
 
104
 
 
105
  #define GET_LIB_FUNCTION(lib, func, failure) \
 
106
    PR_BEGIN_MACRO \
 
107
    _##func = (_##func##_fn) PR_FindFunctionSymbol(lib##Lib, #func); \
 
108
    if (!_##func) { \
 
109
      failure; \
 
110
    } \
 
111
    PR_END_MACRO
 
112
 
 
113
  // Attempt to open libgnome
 
114
  gnomeLib = LoadVersionedLibrary("gnome-2", ".0");
 
115
  ENSURE_LIB(gnomeLib);
 
116
 
 
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());
 
120
 
 
121
  // Attempt to open libgnomevfs
 
122
  vfsLib = LoadVersionedLibrary("gnomevfs-2", ".0");
 
123
  ENSURE_LIB(vfsLib);
 
124
 
 
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());
 
130
 
 
131
  // Initialize GNOME, if it's not already initialized.  It's not
 
132
  // necessary to tell GNOME about our actual command line arguments.
 
133
 
 
134
  if (!_gnome_program_get()) {
 
135
    char *argv[1] = { "gecko" };
 
136
    _gnome_program_init("Gecko", "1.0", _libgnome_module_info_get(),
 
137
                        1, argv, NULL);
 
138
  }
 
139
 
 
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
 
142
  // crash on exit.  
 
143
}
 
144
 
 
145
void
 
146
nsMIMEInfoUnix::SetDefaultGnomeVFSMimeApplication(GnomeVFSMimeApplication* app)
 
147
{
 
148
  if (_gnome_vfs_mime_application_copy && _gnome_vfs_mime_application_free) {
 
149
    mDefaultVFSApplication = _gnome_vfs_mime_application_copy(app);
 
150
 
 
151
    mPreferredAction = nsIMIMEInfo::useSystemDefault;
 
152
 
 
153
    const gchar * name = _gnome_vfs_mime_application_get_name(mDefaultVFSApplication);
 
154
    if (name) 
 
155
      mDefaultAppDescription = NS_ConvertUTF8toUCS2(name);
 
156
  }
 
157
}
 
158
 
 
159
nsMIMEInfoUnix::~nsMIMEInfoUnix()
 
160
{
 
161
  if (mDefaultVFSApplication)
 
162
    _gnome_vfs_mime_application_free(mDefaultVFSApplication);
 
163
}
 
164
 
 
165
nsresult
 
166
nsMIMEInfoUnix::LaunchDefaultWithFile(nsIFile* aFile)
 
167
{
 
168
  NS_ENSURE_ARG_POINTER(aFile);
 
169
 
 
170
  InitGnomeVFS();
 
171
 
 
172
  if (_gnome_vfs_mime_application_launch && mDefaultVFSApplication) {
 
173
    nsCAutoString nativePath;
 
174
    aFile->GetNativePath(nativePath);
 
175
 
 
176
    gchar *uri = _gnome_vfs_make_uri_from_input(nativePath.get());
 
177
 
 
178
    GList *uris = NULL;
 
179
    uris = g_list_append(uris, uri);
 
180
 
 
181
    GnomeVFSResult result = _gnome_vfs_mime_application_launch(mDefaultVFSApplication, uris);
 
182
 
 
183
    g_free(uri);
 
184
    g_list_free(uris);
 
185
 
 
186
    if (result != GNOME_VFS_OK)
 
187
      return NS_ERROR_FAILURE;
 
188
 
 
189
    return NS_OK;
 
190
  }
 
191
 
 
192
  if (!mDefaultApplication)
 
193
    return NS_ERROR_FILE_NOT_FOUND;
 
194
 
 
195
  return LaunchWithIProcess(mDefaultApplication, aFile);
 
196
}