2 * Copyright © 2001 Philip Langdale
3 * Copyright © 2003 Marco Pesenti Gritti
4 * Copyright © 2003 Xan Lopez
5 * Copyright © 2004 Christian Persch
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "mozilla-config.h"
27 #include <glib/gi18n.h>
28 #include <gtk/gtkbutton.h>
29 #include <gtk/gtkdialog.h>
30 #include <gtk/gtkimage.h>
31 #include <gtk/gtkmain.h>
32 #include <gtk/gtkmessagedialog.h>
33 #include <gtk/gtkstock.h>
34 #include <libgnomevfs/gnome-vfs-mime.h>
35 #include <libgnomevfs/gnome-vfs-utils.h>
37 #include <nsStringAPI.h>
39 #include <nsCExternalHandlerService.h>
40 #include <nsComponentManagerUtils.h>
41 #include <nsIDOMWindow.h>
42 #include <nsIInterfaceRequestorUtils.h>
43 #include <nsILocalFile.h>
44 #include <nsIMIMEInfo.h>
47 #include <nsNetError.h>
48 #include <nsServiceManagerUtils.h>
50 #include "eel-gconf-extensions.h"
51 #include "ephy-debug.h"
52 #include "ephy-embed-shell.h"
53 #include "ephy-embed-single.h"
54 #include "ephy-file-chooser.h"
55 #include "ephy-file-helpers.h"
57 #include "ephy-prefs.h"
58 #include "ephy-stock-icons.h"
60 #include "AutoJSContextStack.h"
61 #include "AutoWindowModalState.h"
62 #include "EphyUtils.h"
63 #include "MozDownload.h"
65 #include "ContentHandler.h"
67 /* FIXME: we don't generally have a timestamp for the user action which initiated this
70 GContentHandler::GContentHandler()
73 LOG ("GContentHandler ctor (%p)", this);
76 GContentHandler::~GContentHandler()
78 LOG ("GContentHandler dtor (%p)", this);
81 NS_IMPL_ISUPPORTS1(GContentHandler, nsIHelperAppLauncherDialog)
83 /* void show (in nsIHelperAppLauncher aLauncher, in nsISupports aContext, in unsigned long aReason); */
85 GContentHandler::Show (nsIHelperAppLauncher *aLauncher,
86 nsISupports *aContext,
90 EphyEmbedSingle *single;
91 gboolean handled = FALSE;
93 /* FIXME: handle aForced / aReason argument in some way? */
97 /* Check for a potential veto */
98 nsCOMPtr<nsIDOMWindow> window (do_GetInterface (aContext));
99 GtkWidget *embed = EphyUtils::FindEmbed (window);
100 if (EPHY_IS_EMBED (embed))
102 if (g_object_get_data (G_OBJECT (embed), "content-handler-deny"))
108 mLauncher = aLauncher;
110 NS_ENSURE_SUCCESS (rv, rv);
112 single = EPHY_EMBED_SINGLE (ephy_embed_shell_get_embed_single (embed_shell));
113 g_signal_emit_by_name (single, "handle_content", mMimeType.get(),
114 mUrl.get(), &handled);
118 MIMEInitiateAction ();
122 mLauncher->Cancel (NS_BINDING_ABORTED);
128 /* nsILocalFile promptForSaveToFile (in nsISupports aWindowContext, in wstring aDefaultFile, in wstring aSuggestedFileExtension); */
129 NS_IMETHODIMP GContentHandler::PromptForSaveToFile(
130 nsIHelperAppLauncher *aLauncher,
131 nsISupports *aWindowContext,
132 const PRUnichar *aDefaultFile,
133 const PRUnichar *aSuggestedFileExtension,
134 nsILocalFile **_retval)
136 EphyFileChooser *dialog;
138 char *filename = NULL;
139 nsCString defaultFile;
141 NS_UTF16ToCString (nsString (aDefaultFile),
142 NS_CSTRING_ENCODING_UTF8, defaultFile);
144 if (mAction != CONTENT_ACTION_SAVEAS)
146 return BuildDownloadPath (defaultFile.get(), _retval);
150 AutoJSContextStack stack;
152 if (NS_FAILED (rv)) return rv;
154 nsCOMPtr<nsIDOMWindow> parentDOMWindow (do_GetInterface (aWindowContext));
155 GtkWidget *parentWindow = GTK_WIDGET (EphyUtils::FindGtkParent (parentDOMWindow));
157 AutoWindowModalState modalState (parentDOMWindow);
159 dialog = ephy_file_chooser_new (_("Save"), parentWindow,
160 GTK_FILE_CHOOSER_ACTION_SAVE,
162 EPHY_FILE_FILTER_ALL);
163 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
164 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), defaultFile.get());
168 gtk_window_group_add_window (ephy_gui_ensure_window_group (GTK_WINDOW (parentWindow)),
169 GTK_WINDOW (dialog));
172 /* FIXME: this will only be the real user time if we came from ::Show */
173 ephy_gui_window_update_user_time (GTK_WIDGET (dialog), (guint32) mUserTime);
175 /* FIXME: modal -- mozilla sucks! */
179 response = gtk_dialog_run (GTK_DIALOG (dialog));
180 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
181 } while (response == GTK_RESPONSE_ACCEPT
182 && !ephy_gui_check_location_writable (GTK_WIDGET (dialog), filename));
184 if (response == GTK_RESPONSE_ACCEPT)
186 nsCOMPtr <nsILocalFile> destFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
187 NS_ENSURE_TRUE (destFile, NS_ERROR_FAILURE);
189 destFile->InitWithNativePath (nsCString (filename));
192 NS_IF_ADDREF (*_retval = destFile);
194 gtk_widget_destroy (GTK_WIDGET (dialog));
200 gtk_widget_destroy (GTK_WIDGET (dialog));
203 return NS_ERROR_FAILURE;
207 NS_METHOD GContentHandler::Init ()
211 NS_ENSURE_TRUE (mLauncher, NS_ERROR_FAILURE);
213 nsCOMPtr<nsIMIMEInfo> MIMEInfo;
214 mLauncher->GetMIMEInfo (getter_AddRefs(MIMEInfo));
215 NS_ENSURE_TRUE (MIMEInfo, NS_ERROR_FAILURE);
217 rv = MIMEInfo->GetMIMEType (mMimeType);
219 nsCOMPtr<nsIURI> uri;
220 mLauncher->GetSource (getter_AddRefs(uri));
221 NS_ENSURE_TRUE (uri, NS_ERROR_FAILURE);
229 response_cb (GtkWidget *dialog,
231 GContentHandler *self)
233 gtk_widget_destroy (dialog);
237 self->mAction = (ContentAction) response;
241 self->mAction = CONTENT_ACTION_NONE;
244 self->MIMEDoAction ();
248 release_cb (GContentHandler *data)
253 NS_METHOD GContentHandler::MIMEConfirmAction ()
255 GtkWidget *dialog, *button, *image;
256 const char *action_label;
257 const char *mime_description;
260 nsCOMPtr<nsIDOMWindow> parentDOMWindow = do_GetInterface (mContext);
261 GtkWindow *parentWindow = GTK_WINDOW (EphyUtils::FindGtkParent(parentDOMWindow));
263 action_label = (mAction == CONTENT_ACTION_OPEN) ||
264 (mAction == CONTENT_ACTION_OPEN_TMP) ?
265 GTK_STOCK_OPEN : STOCK_DOWNLOAD;
267 mime_description = gnome_vfs_mime_get_description (mMimeType.get());
268 if (mime_description == NULL)
270 /* Translators: The text before the "|" is context to help you decide on
271 * the correct translation. You MUST OMIT it in the translated string. */
272 mime_description = Q_("File Type:|Unknown");
275 /* We have one tiny, minor issue, the filename can be empty (""),
276 is that severe enough to be completely fixed ? */
279 mLauncher->GetSuggestedFileName (suggested);
280 NS_UTF16ToCString (suggested,
281 NS_CSTRING_ENCODING_UTF8, file_name);
283 if (mPermission != EPHY_MIME_PERMISSION_SAFE && mHelperApp)
285 dialog = gtk_message_dialog_new
286 (parentWindow, GTK_DIALOG_DESTROY_WITH_PARENT,
287 GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
288 _("Download this potentially unsafe file?"));
290 gtk_message_dialog_format_secondary_text
291 (GTK_MESSAGE_DIALOG (dialog),
292 /* translators: First %s is the file type description,
293 Second %s is the file name */
294 _("File Type: “%s”.\n\nIt is unsafe to open “%s” as "
295 "it could potentially damage your documents or "
296 "invade your privacy. You can download it instead."),
297 mime_description, file_name.get());
299 else if (mAction == CONTENT_ACTION_OPEN_TMP && mHelperApp)
301 dialog = gtk_message_dialog_new
302 (parentWindow, GTK_DIALOG_DESTROY_WITH_PARENT,
303 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
304 _("Open this file?"));
306 gtk_message_dialog_format_secondary_text
307 (GTK_MESSAGE_DIALOG (dialog),
308 /* translators: First %s is the file type description,
309 Second %s is the file name,
310 Third %s is the application used to open the file */
311 _("File Type: “%s”.\n\nYou can open “%s” using “%s” or save it."),
312 mime_description, file_name.get(), mHelperApp->name);
316 dialog = gtk_message_dialog_new
317 (parentWindow, GTK_DIALOG_DESTROY_WITH_PARENT,
318 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
319 _("Download this file?"));
321 gtk_message_dialog_format_secondary_text
322 (GTK_MESSAGE_DIALOG (dialog),
323 /* translators: First %s is the file type description,
324 Second %s is the file name */
325 _("File Type: “%s”.\n\nYou have no application able to open “%s”. "
326 "You can download it instead."),
327 mime_description, file_name.get());
330 button = gtk_button_new_with_label (_("_Save As..."));
331 image = gtk_image_new_from_stock (GTK_STOCK_SAVE_AS, GTK_ICON_SIZE_BUTTON);
332 gtk_button_set_image (GTK_BUTTON (button), image);
333 /* don't show the image! see bug #307818 */
334 gtk_widget_show (button);
335 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, CONTENT_ACTION_SAVEAS);
337 gtk_dialog_add_button (GTK_DIALOG (dialog),
338 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
339 gtk_dialog_add_button (GTK_DIALOG (dialog),
340 action_label, mAction);
342 gtk_window_set_icon_name (GTK_WINDOW (dialog), EPHY_STOCK_EPHY);
344 int defaultResponse = mAction == CONTENT_ACTION_NONE
345 ? (int) GTK_RESPONSE_CANCEL
347 gtk_dialog_set_default_response (GTK_DIALOG (dialog), defaultResponse);
350 g_signal_connect_data (dialog, "response",
351 G_CALLBACK (response_cb), this,
352 (GClosureNotify) release_cb, (GConnectFlags) 0);
354 /* FIXME: should find a way to get the user time of the user action which
355 * initiated this content handler
357 gtk_window_present (GTK_WINDOW (dialog));
362 NS_METHOD GContentHandler::MIMEInitiateAction (void)
364 gboolean auto_downloads;
366 if (eel_gconf_get_boolean (CONF_LOCKDOWN_DISABLE_SAVE_TO_DISK)) return NS_OK;
368 auto_downloads = eel_gconf_get_boolean (CONF_AUTO_DOWNLOADS);
370 mHelperApp = gnome_vfs_mime_get_default_application (mMimeType.get());
371 mPermission = ephy_file_check_mime (mMimeType.get());
373 /* HACK! Check that this 'helper application' isn't Epiphany itself,
378 const char *id = gnome_vfs_mime_application_get_desktop_id (mHelperApp);
380 /* FIXME! menu editing can make this check fail!!!! */
381 if (id && strcmp (id, "epiphany.desktop") == 0)
389 mAction = CONTENT_ACTION_OPEN;
393 mAction = CONTENT_ACTION_OPEN_TMP;
396 if (!mHelperApp || mPermission != EPHY_MIME_PERMISSION_SAFE)
398 mAction = CONTENT_ACTION_DOWNLOAD;
401 if (!auto_downloads || mAction == CONTENT_ACTION_DOWNLOAD)
403 MIMEConfirmAction ();
413 NS_METHOD GContentHandler::MIMEDoAction (void)
415 /* This is okay, since we either clicked on a button, or we get 0 */
416 mUserTime = gtk_get_current_event_time ();
418 nsCOMPtr<nsIMIMEInfo> mimeInfo;
419 mLauncher->GetMIMEInfo(getter_AddRefs(mimeInfo));
420 NS_ENSURE_TRUE (mimeInfo, NS_ERROR_FAILURE);
424 if (mAction == CONTENT_ACTION_OPEN)
426 g_return_val_if_fail (mHelperApp, NS_ERROR_FAILURE);
429 id = gnome_vfs_mime_application_get_desktop_id (mHelperApp);
431 /* The current time is fine here as the user has just clicked
432 * a button (it is used as the time for the application opening)
434 info = g_strdup_printf ("gnome-default:%d:%s", gtk_get_current_event_time(), id);
436 else if (mAction == CONTENT_ACTION_DOWNLOAD)
438 info = g_strdup_printf ("gnome-browse-to-file:%d", gtk_get_current_event_time());
444 NS_CStringToUTF16 (nsCString (info),
445 NS_CSTRING_ENCODING_UTF8, desc);
448 /* HACK we use the application description to ask
449 MozDownload to open the file when download
451 mimeInfo->SetApplicationDescription (desc);
455 mimeInfo->SetApplicationDescription (nsString ());
458 if (mAction == CONTENT_ACTION_OPEN)
460 mLauncher->SaveToDisk (nsnull, PR_FALSE);
462 else if (mAction == CONTENT_ACTION_OPEN_TMP)
464 mLauncher->LaunchWithApplication (nsnull, PR_FALSE);
466 else if (mAction == CONTENT_ACTION_NONE)
468 mLauncher->Cancel (NS_BINDING_ABORTED);
472 mLauncher->SaveToDisk (nsnull, PR_FALSE);