Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / embed / mozilla / .svn / text-base / ContentHandler.cpp.svn-base
blob248db6056f5f94be2b79718ba4463cfe097942d8
1 /*
2  *  Copyright © 2001 Philip Langdale
3  *  Copyright © 2003 Marco Pesenti Gritti
4  *  Copyright © 2003 Xan Lopez
5  *  Copyright © 2004 Christian Persch
6  *
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)
10  *  any later version.
11  *
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.
16  *
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.
20  *
21  *  $Id$
22  */
24 #include "mozilla-config.h"
25 #include "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>
45 #include <nsIURL.h>
46 #include <nsMemory.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"
56 #include "ephy-gui.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
68  * content handler.
69  */
70 GContentHandler::GContentHandler()
71 : mUserTime(0)
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); */
84 NS_IMETHODIMP
85 GContentHandler::Show (nsIHelperAppLauncher *aLauncher,
86                        nsISupports *aContext,
87                        PRUint32 aReason)
89         nsresult rv;
90         EphyEmbedSingle *single;
91         gboolean handled = FALSE;
93         /* FIXME: handle aForced / aReason argument in some way? */
95         mContext = aContext;
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))
101         {
102                 if (g_object_get_data (G_OBJECT (embed), "content-handler-deny"))
103                 {
104                         return NS_OK;
105                 }
106         }
108         mLauncher = aLauncher;
109         rv = Init ();
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);
116         if (!handled)
117         {
118                 MIMEInitiateAction ();
119         }
120         else
121         {
122                 mLauncher->Cancel (NS_BINDING_ABORTED);
123         }
125         return NS_OK;
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;
137         int response;
138         char *filename = NULL;
139         nsCString defaultFile;
141         NS_UTF16ToCString (nsString (aDefaultFile),
142                            NS_CSTRING_ENCODING_UTF8, defaultFile);
144         if (mAction != CONTENT_ACTION_SAVEAS)
145         {
146                 return BuildDownloadPath (defaultFile.get(), _retval);
147         }
149         nsresult rv;
150         AutoJSContextStack stack;
151         rv = stack.Init ();
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,
161                                         CONF_STATE_SAVE_DIR,
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());
166         if (parentWindow)
167         {
168                 gtk_window_group_add_window (ephy_gui_ensure_window_group (GTK_WINDOW (parentWindow)),
169                                              GTK_WINDOW (dialog));
170         }
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! */
176         do
177         {
178                 g_free (filename);
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)
185         {
186                 nsCOMPtr <nsILocalFile> destFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
187                 NS_ENSURE_TRUE (destFile, NS_ERROR_FAILURE);
189                 destFile->InitWithNativePath (nsCString (filename));
190                 g_free (filename);
192                 NS_IF_ADDREF (*_retval = destFile);
194                 gtk_widget_destroy (GTK_WIDGET (dialog));
196                 return NS_OK;
197         }
198         else
199         {
200                 gtk_widget_destroy (GTK_WIDGET (dialog));
201                 g_free (filename);
203                 return NS_ERROR_FAILURE;
204         }
207 NS_METHOD GContentHandler::Init ()
209         nsresult rv;
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);
222         
223         uri->GetSpec (mUrl);
225         return NS_OK;
228 static void
229 response_cb (GtkWidget *dialog,
230              int response,
231              GContentHandler *self)
233         gtk_widget_destroy (dialog);
235         if (response > 0)
236         {
237                 self->mAction = (ContentAction) response;
238         }
239         else
240         {
241                 self->mAction = CONTENT_ACTION_NONE;
242         }
244         self->MIMEDoAction ();
247 static void
248 release_cb (GContentHandler *data)
250         NS_RELEASE (data);
253 NS_METHOD GContentHandler::MIMEConfirmAction ()
255         GtkWidget *dialog, *button, *image;
256         const char *action_label;
257         const char *mime_description;
258         nsCString file_name;
259                         
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)
269         {
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");
273         }
275         /* We have one tiny, minor issue, the filename can be empty (""),
276            is that severe enough to be completely fixed ? */
277         nsString suggested;
278                 
279         mLauncher->GetSuggestedFileName (suggested);
280         NS_UTF16ToCString (suggested,
281                            NS_CSTRING_ENCODING_UTF8, file_name);
283         if (mPermission != EPHY_MIME_PERMISSION_SAFE && mHelperApp)
284         {
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?"));
289                            
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());           
298         }
299         else if (mAction == CONTENT_ACTION_OPEN_TMP && mHelperApp)
300         {
301                 dialog = gtk_message_dialog_new
302                         (parentWindow, GTK_DIALOG_DESTROY_WITH_PARENT,
303                          GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
304                          _("Open this file?"));
305                            
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);                 
313         }
314         else
315         {
316                 dialog = gtk_message_dialog_new
317                         (parentWindow, GTK_DIALOG_DESTROY_WITH_PARENT,
318                          GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
319                          _("Download this file?"));
320                 
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());                   
328         }
329         
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
346                                 : (int) mAction;
347         gtk_dialog_set_default_response (GTK_DIALOG (dialog), defaultResponse);
349         NS_ADDREF_THIS();
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
356          */
357         gtk_window_present (GTK_WINDOW (dialog));
359         return NS_OK;
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,
374          * see bug #310023.
375          */
376         if (mHelperApp)
377         {
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)
382                 {
383                         mHelperApp = nsnull;
384                 }
385         }
387         if (auto_downloads)
388         {
389                 mAction = CONTENT_ACTION_OPEN;
390         }
391         else
392         {
393                 mAction = CONTENT_ACTION_OPEN_TMP;
394         }
396         if (!mHelperApp || mPermission != EPHY_MIME_PERMISSION_SAFE)
397         {
398                 mAction = CONTENT_ACTION_DOWNLOAD;
399         }
401         if (!auto_downloads || mAction == CONTENT_ACTION_DOWNLOAD)
402         {
403                 MIMEConfirmAction ();
404         }
405         else
406         {
407                 MIMEDoAction ();
408         }
410         return NS_OK;
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);
422         char *info = NULL;
424         if (mAction == CONTENT_ACTION_OPEN)
425         {
426                 g_return_val_if_fail (mHelperApp, NS_ERROR_FAILURE);
428                 const char *id;
429                 id = gnome_vfs_mime_application_get_desktop_id (mHelperApp);
430                 
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)
433                  */
434                 info = g_strdup_printf ("gnome-default:%d:%s", gtk_get_current_event_time(), id);
435         }
436         else if (mAction == CONTENT_ACTION_DOWNLOAD)
437         {
438                 info = g_strdup_printf ("gnome-browse-to-file:%d", gtk_get_current_event_time());
439         }
441         if (info != NULL)
442         {
443                 nsString desc;
444                 NS_CStringToUTF16 (nsCString (info),
445                                    NS_CSTRING_ENCODING_UTF8, desc);
446                 g_free (info);
448                 /* HACK we use the application description to ask
449                    MozDownload to open the file when download
450                    is finished */
451                 mimeInfo->SetApplicationDescription (desc);
452         }
453         else
454         {
455                 mimeInfo->SetApplicationDescription (nsString ());
456         }
458         if (mAction == CONTENT_ACTION_OPEN)
459         {
460                 mLauncher->SaveToDisk (nsnull, PR_FALSE);
461         }
462         else if (mAction == CONTENT_ACTION_OPEN_TMP)
463         {
464                 mLauncher->LaunchWithApplication (nsnull, PR_FALSE);
465         }
466         else if (mAction == CONTENT_ACTION_NONE)
467         {
468                 mLauncher->Cancel (NS_BINDING_ABORTED);
469         }
470         else
471         {
472                 mLauncher->SaveToDisk (nsnull, PR_FALSE);
473         }
475         return NS_OK;