1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/*
* Copyright (C) 2001 Philip Langdale
* 2003 Marco Pesenti Gritti, Xan Lopez
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ContentHandler.h"
#include "MozillaPrivate.h"
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsIURI.h"
#include "nsIURL.h"
#include "nsILocalFile.h"
#include "nsIMIMEInfo.h"
#include "ephy-prefs.h"
#include "eel-gconf-extensions.h"
#include "ephy-embed-shell.h"
#include <libgnomevfs/gnome-vfs-mime.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <glib/gi18n.h>
class GContentHandler;
/* Implementation file */
NS_IMPL_ISUPPORTS1(GContentHandler, nsIHelperAppLauncherDialog)
GContentHandler::GContentHandler() : mUri(nsnull),
mMimeType(nsnull)
{
NS_INIT_ISUPPORTS();
/* member initializers and constructor code */
}
GContentHandler::~GContentHandler()
{
/* destructor code */
g_free (mUri);
g_free (mMimeType);
}
////////////////////////////////////////////////////////////////////////////////
// begin nsIHelperAppLauncher impl
////////////////////////////////////////////////////////////////////////////////
#if MOZILLA_SNAPSHOT > 9
/* void show (in nsIHelperAppLauncher aLauncher, in nsISupports aContext); */
NS_IMETHODIMP GContentHandler::Show(nsIHelperAppLauncher *aLauncher,
nsISupports *aContext,
PRBool aForced)
#else
NS_IMETHODIMP GContentHandler::Show(nsIHelperAppLauncher *aLauncher,
nsISupports *aContext)
#endif
{
/* aForced reflects if the content being sent is normally viewable
* in mozilla or not. That fact doesn't affect us, so ignore it
*/
nsresult rv;
mLauncher = aLauncher;
mContext = aContext;
rv = Init ();
if (NS_FAILED (rv)) return rv;
MIMEAskAction ();
return NS_OK;
}
/* nsILocalFile promptForSaveToFile (in nsISupports aWindowContext, in wstring aDefaultFile, in wstring aSuggestedFileExtension); */
NS_IMETHODIMP GContentHandler::PromptForSaveToFile(
#if MOZILLA_SNAPSHOT > 10
nsIHelperAppLauncher *aLauncher,
#endif
nsISupports *aWindowContext,
const PRUnichar *aDefaultFile,
const PRUnichar *aSuggestedFileExtension,
nsILocalFile **_retval)
{
char *path, *download_dir;
download_dir = eel_gconf_get_string (CONF_STATE_DOWNLOAD_DIR);
if (!download_dir)
{
/* Emergency download destination */
download_dir = g_strdup (g_get_home_dir ());
}
if (!strcmp (download_dir, "Desktop"))
{
if (eel_gconf_get_boolean (CONF_DESKTOP_IS_HOME_DIR))
{
path = g_build_filename
(g_get_home_dir (),
NS_ConvertUCS2toUTF8 (aDefaultFile).get(),
NULL);
}
else
{
path = g_build_filename
(g_get_home_dir (), "Desktop",
NS_ConvertUCS2toUTF8 (aDefaultFile).get(),
NULL);
}
}
else
{
path = g_build_filename
(gnome_vfs_expand_initial_tilde (download_dir),
NS_ConvertUCS2toUTF8 (aDefaultFile).get(),
NULL);
}
g_free (download_dir);
nsCOMPtr <nsILocalFile> destFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
destFile->InitWithNativePath (nsDependentCString (path));
g_free (path);
NS_IF_ADDREF (*_retval = destFile);
return NS_OK;
}
#if MOZILLA_SNAPSHOT < 10
/* void showProgressDialog (in nsIHelperAppLauncher aLauncher, in nsISupports aContext); */
NS_METHOD GContentHandler::ShowProgressDialog(nsIHelperAppLauncher *aLauncher,
nsISupports *aContext)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// begin local public methods impl
////////////////////////////////////////////////////////////////////////////////
NS_METHOD GContentHandler::FindHelperApp (void)
{
if (mUrlHelper)
{
return LaunchHelperApp ();
}
else
{
if (NS_SUCCEEDED (SynchroniseMIMEInfo()))
{
return mLauncher->LaunchWithApplication (nsnull, PR_FALSE);
}
else
{
return NS_ERROR_FAILURE;
}
}
}
NS_METHOD GContentHandler::LaunchHelperApp (void)
{
if (mMimeType)
{
nsresult rv;
nsCOMPtr<nsIExternalHelperAppService> helperService =
do_GetService (NS_EXTERNALHELPERAPPSERVICE_CONTRACTID);
nsCOMPtr<nsPIExternalAppLauncher> appLauncher =
do_QueryInterface (helperService, &rv);
if (NS_SUCCEEDED (rv))
{
appLauncher->DeleteTemporaryFileOnExit(mTempFile);
}
nsString uFileName;
mTempFile->GetPath(uFileName);
const nsCString &aFileName = NS_ConvertUCS2toUTF8(uFileName);
const nsCString &document = (mUrlHelper) ? mUrl : aFileName;
char *param = g_strdup (document.get());
GList *params = NULL;
params = g_list_append (params, param);
gnome_vfs_mime_application_launch (mHelperApp, params);
g_free (param);
g_list_free (params);
if(mUrlHelper) mLauncher->Cancel();
}
else
{
mLauncher->Cancel ();
}
return NS_OK;
}
NS_METHOD GContentHandler::GetLauncher (nsIHelperAppLauncher * *_retval)
{
NS_IF_ADDREF (*_retval = mLauncher);
return NS_OK;
}
static gboolean
application_support_scheme (GnomeVFSMimeApplication *app, const nsCString &aScheme)
{
GList *l;
g_return_val_if_fail (app != NULL, FALSE);
g_return_val_if_fail (!aScheme.IsEmpty(), FALSE);
if (app->expects_uris != GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_URIS)
return FALSE;
for (l = app->supported_uri_schemes; l != NULL; l = l->next)
{
char *uri_scheme = (char *)l->data;
g_return_val_if_fail (uri_scheme != NULL, FALSE);
if (aScheme.Equals (uri_scheme)) return TRUE;
}
return FALSE;
}
NS_METHOD GContentHandler::SetHelperApp(GnomeVFSMimeApplication *aHelperApp,
PRBool alwaysUse)
{
mHelperApp = aHelperApp;
mUrlHelper = application_support_scheme (aHelperApp, mScheme);
return NS_OK;
}
NS_METHOD GContentHandler::SynchroniseMIMEInfo (void)
{
nsresult rv;
char *command_with_path;
nsCOMPtr<nsIMIMEInfo> mimeInfo;
rv = mLauncher->GetMIMEInfo(getter_AddRefs(mimeInfo));
if(NS_FAILED (rv)) return NS_ERROR_FAILURE;
command_with_path = g_find_program_in_path (mHelperApp->command);
if (command_with_path == NULL) return NS_ERROR_FAILURE;
nsCOMPtr<nsILocalFile> helperFile;
rv = NS_NewNativeLocalFile (nsDependentCString(command_with_path),
PR_TRUE,
getter_AddRefs(helperFile));
if(NS_FAILED (rv)) return NS_ERROR_FAILURE;
g_free (command_with_path);
rv = mimeInfo->SetPreferredApplicationHandler(helperFile);
if(NS_FAILED (rv)) return NS_ERROR_FAILURE;
nsMIMEInfoHandleAction mimeInfoAction;
mimeInfoAction = nsIMIMEInfo::useHelperApp;
if(mHelperApp->requires_terminal) //Information passing kludge!
{
rv = mimeInfo->SetApplicationDescription
(NS_LITERAL_STRING("runInTerminal").get());
if(NS_FAILED (rv)) return NS_ERROR_FAILURE;
}
rv = mimeInfo->SetPreferredAction(mimeInfoAction);
if(NS_FAILED (rv)) return NS_ERROR_FAILURE;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// begin local private methods impl
////////////////////////////////////////////////////////////////////////////////
NS_METHOD GContentHandler::Init (void)
{
nsresult rv;
nsCOMPtr<nsIMIMEInfo> MIMEInfo;
rv = mLauncher->GetMIMEInfo (getter_AddRefs(MIMEInfo));
rv = MIMEInfo->GetMIMEType (&mMimeType);
#if MOZILLA_SNAPSHOT > 11
rv = mLauncher->GetSource (getter_AddRefs(mUri));
rv = mLauncher->GetTargetFile (getter_AddRefs(mTempFile));
#else
rv = mLauncher->GetDownloadInfo (getter_AddRefs(mUri),
&mTimeDownloadStarted,
getter_AddRefs(mTempFile));
#endif
rv = mUri->GetSpec (mUrl);
rv = mUri->GetScheme (mScheme);
ProcessMimeInfo ();
return NS_OK;
}
NS_METHOD GContentHandler::ProcessMimeInfo (void)
{
if (mMimeType == NULL ||
!nsCRT::strcmp(mMimeType, "application/octet-stream"))
{
nsresult rv;
nsCOMPtr<nsIURL> url = do_QueryInterface(mUri, &rv);
if (NS_SUCCEEDED(rv) && url)
{
nsCAutoString uriFileName;
url->GetFileName(uriFileName);
mMimeType = g_strdup
(gnome_vfs_mime_type_from_name
(uriFileName.get()));
}
else
{
mMimeType = g_strdup ("application/octet-stream");
}
}
return NS_OK;
}
NS_METHOD GContentHandler::MIMEAskAction (void)
{
nsresult rv;
gboolean auto_open;
/* FIXME can we assume mime is not NULL ? */
auto_open = eel_gconf_get_boolean (CONF_AUTO_OPEN_DOWNLOADS);
GContentHandler *mContentHandler = this;
GnomeVFSMimeApplication *DefaultApp = gnome_vfs_mime_get_default_application(mMimeType);
EphyMimePermission permission;
permission = ephy_embed_shell_check_mime (embed_shell, mMimeType);
if (!auto_open || !DefaultApp || permission != EPHY_MIME_PERMISSION_SAFE)
{
nsCOMPtr<nsIHelperAppLauncher> launcher;
rv = mContentHandler->GetLauncher (getter_AddRefs(launcher));
if (NS_FAILED (rv)) return rv;
launcher->SaveToDisk (nsnull,PR_FALSE);
}
else
{
rv = mContentHandler->SetHelperApp (DefaultApp, FALSE);
if (NS_FAILED (rv)) return rv;
rv = mContentHandler->FindHelperApp ();
if (NS_FAILED (rv)) return rv;
}
return NS_OK;
}
|