aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/ContentHandler.cpp
blob: 0b7f2f65384df14c222525981e8d2dde162b58fe (plain) (blame)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 *  Copyright (C) 2001 Philip Langdale
 *  Copyright (C) 2003 Marco Pesenti Gritti
 *  Copyright (C) 2003 Xan Lopez
 *  Copyright (C) 2004 Christian Persch
 *
 *  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$
 */

#include "mozilla-config.h"

#include "config.h"

#include <gtk/gtkdialog.h>
#include <gtk/gtkmessagedialog.h>
#include <gtk/gtkstock.h>
#include <gtk/gtkimage.h>
#include <gtk/gtkbutton.h>
#include <libgnomevfs/gnome-vfs-mime.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <glib/gi18n.h>

#include <nsMemory.h>
#include <nsIURL.h>
#include <nsILocalFile.h>
#include <nsIMIMEInfo.h>
#include <nsIInterfaceRequestorUtils.h>
#include <nsCExternalHandlerService.h>

#ifdef ALLOW_PRIVATE_API
#include <nsIServiceManager.h>
#endif

#include "ephy-prefs.h"
#include "ephy-embed-single.h"
#include "ephy-embed-shell.h"
#include "ephy-file-chooser.h"
#include "ephy-file-helpers.h"
#include "ephy-stock-icons.h"
#include "ephy-gui.h"
#include "ephy-debug.h"
#include "eel-gconf-extensions.h"

#include "ContentHandler.h"
#include "MozDownload.h"
#include "EphyUtils.h"

#ifdef MOZ_NSIMIMEINFO_NSACSTRING_
GContentHandler::GContentHandler()
{
    LOG ("GContentHandler ctor (%p)", this)
}
#else
GContentHandler::GContentHandler() : mMimeType(nsnull)
{
    LOG ("GContentHandler ctor (%p)", this)
}
#endif

GContentHandler::~GContentHandler()
{
    LOG ("GContentHandler dtor (%p)", this)

#ifndef MOZ_NSIMIMEINFO_NSACSTRING_
    if (mMimeType)
    {
        nsMemory::Free (mMimeType);
    }
#endif
}

NS_IMPL_ISUPPORTS1(GContentHandler, nsIHelperAppLauncherDialog)

/* void show (in nsIHelperAppLauncher aLauncher, in nsISupports aContext); */
NS_IMETHODIMP GContentHandler::Show(nsIHelperAppLauncher *aLauncher,
                    nsISupports *aContext,
                    PRBool aForced)
{
    nsresult rv;
    EphyEmbedSingle *single;
    gboolean handled = FALSE;

    mContext = aContext;
    mLauncher = aLauncher;
    rv = Init ();
    NS_ENSURE_SUCCESS (rv, rv);

    single = EPHY_EMBED_SINGLE (ephy_embed_shell_get_embed_single (embed_shell));
#ifdef MOZ_NSIMIMEINFO_NSACSTRING_
    g_signal_emit_by_name (single, "handle_content", mMimeType.get(),
                   mUrl.get(), &handled);
#else
    g_signal_emit_by_name (single, "handle_content", mMimeType,
                   mUrl.get(), &handled);
#endif

    if (!handled)
    {
        MIMEInitiateAction ();
    }
    else
    {
        mLauncher->Cancel ();
    }

    return NS_OK;
}

/* nsILocalFile promptForSaveToFile (in nsISupports aWindowContext, in wstring aDefaultFile, in wstring aSuggestedFileExtension); */
NS_IMETHODIMP GContentHandler::PromptForSaveToFile(
                    nsIHelperAppLauncher *aLauncher,                
                    nsISupports *aWindowContext,
                    const PRUnichar *aDefaultFile,
                    const PRUnichar *aSuggestedFileExtension,
                    nsILocalFile **_retval)
{
    EphyFileChooser *dialog;
    gint response;
    char *filename = NULL;
    nsEmbedCString defaultFile;

    NS_UTF16ToCString (nsEmbedString (aDefaultFile),
               NS_CSTRING_ENCODING_UTF8, defaultFile);

    if (mAction != CONTENT_ACTION_SAVEAS)
    {
        return BuildDownloadPath (defaultFile.get(), _retval);
    }

    nsCOMPtr<nsIDOMWindow> parentDOMWindow = do_GetInterface (aWindowContext);
    GtkWidget *parentWindow = GTK_WIDGET (EphyUtils::FindGtkParent (parentDOMWindow));

    dialog = ephy_file_chooser_new (_("Save"), parentWindow,
                    GTK_FILE_CHOOSER_ACTION_SAVE,
                    CONF_STATE_SAVE_DIR,
                    EPHY_FILE_FILTER_ALL);
    gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), defaultFile.get());

    if (parentWindow)
    {
        gtk_window_group_add_window (ephy_gui_ensure_window_group (GTK_WINDOW (parentWindow)),
                         GTK_WINDOW (dialog));
    }

    /* FIXME: modal -- mozilla sucks! */
    do
    {
        g_free (filename);
        response = gtk_dialog_run (GTK_DIALOG (dialog));
        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
    } while (response == GTK_RESPONSE_ACCEPT
         && !ephy_gui_confirm_overwrite_file (GTK_WIDGET (dialog), filename));

    if (response == GTK_RESPONSE_ACCEPT)
    {
        nsCOMPtr <nsILocalFile> destFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
        NS_ENSURE_TRUE (destFile, NS_ERROR_FAILURE);

        destFile->InitWithNativePath (nsEmbedCString (filename));
        g_free (filename);

        NS_IF_ADDREF (*_retval = destFile);

        gtk_widget_destroy (GTK_WIDGET (dialog));

        return NS_OK;
    }
    else
    {
        gtk_widget_destroy (GTK_WIDGET (dialog));
        g_free (filename);

        return NS_ERROR_FAILURE;
    }
}

NS_METHOD GContentHandler::Init ()
{
    nsresult rv;

    NS_ENSURE_TRUE (mLauncher, NS_ERROR_FAILURE);

    nsCOMPtr<nsIMIMEInfo> MIMEInfo;
    mLauncher->GetMIMEInfo (getter_AddRefs(MIMEInfo));
    NS_ENSURE_TRUE (MIMEInfo, NS_ERROR_FAILURE);

#ifdef MOZ_NSIMIMEINFO_NSACSTRING_
    rv = MIMEInfo->GetMIMEType (mMimeType);
#else
    rv = MIMEInfo->GetMIMEType (&mMimeType);
#endif

    nsCOMPtr<nsIURI> uri;
    mLauncher->GetSource (getter_AddRefs(uri));
    NS_ENSURE_TRUE (uri, NS_ERROR_FAILURE);
    
    uri->GetSpec (mUrl);

    return NS_OK;
}

static void
response_cb (GtkWidget *dialog,
         int response,
         GContentHandler *self)
{
    gtk_widget_destroy (dialog);

    if (response > 0)
    {
        self->mAction = (ContentAction) response;
    }
    else
    {
        self->mAction = CONTENT_ACTION_NONE;
    }

    self->MIMEDoAction ();
}

static void
release_cb (GContentHandler *data)
{
    NS_RELEASE (data);
}

NS_METHOD GContentHandler::MIMEConfirmAction ()
{
    GtkWidget *dialog, *button, *image;
    const char *action_label;

    nsCOMPtr<nsIDOMWindow> parentDOMWindow = do_GetInterface (mContext);
    GtkWindow *parentWindow = GTK_WINDOW (EphyUtils::FindGtkParent(parentDOMWindow));

    action_label =  (mAction == CONTENT_ACTION_OPEN) ||
            (mAction == CONTENT_ACTION_OPEN_TMP) ?
            GTK_STOCK_OPEN : EPHY_STOCK_DOWNLOAD;

    if (mPermission == EPHY_MIME_PERMISSION_UNSAFE && mHelperApp)
    {
        dialog = gtk_message_dialog_new
            (parentWindow, GTK_DIALOG_DESTROY_WITH_PARENT,
             GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
             _("Download the unsafe file?"));
        gtk_message_dialog_format_secondary_text
            (GTK_MESSAGE_DIALOG (dialog),
             _("This type of file could potentially damage "
                "your documents or invade your privacy. "
                "It's not safe to open it directly. "
                "You can save it instead."));
    }
    else if (mAction == CONTENT_ACTION_OPEN_TMP && mHelperApp)
    {
        dialog = gtk_message_dialog_new
            (parentWindow, GTK_DIALOG_DESTROY_WITH_PARENT,
             GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
             /* translators: %s is the name of the application */
             _("Open this file with \"%s\"?"),
             mHelperApp->name);
        gtk_message_dialog_format_secondary_markup
            (GTK_MESSAGE_DIALOG (dialog),
             /* translators: %s is the name of the application */
             _("It's not possible to view this file type "
               "directly in the browser. You can open it with "
               "\"%s\" or save it."),
               mHelperApp->name);
    }
    else
    {
        dialog = gtk_message_dialog_new
            (parentWindow, GTK_DIALOG_DESTROY_WITH_PARENT,
             GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
             _("Download the file?"));
        gtk_message_dialog_format_secondary_text
            (GTK_MESSAGE_DIALOG (dialog),
             _("It's not possible to view this file because "
               "there is no application installed that can open"
               " it. You can save it instead."));
    }

    button = gtk_button_new_with_label (_("_Save As..."));
    image = gtk_image_new_from_stock (GTK_STOCK_SAVE_AS, GTK_ICON_SIZE_BUTTON);
    gtk_button_set_image (GTK_BUTTON (button), image);
    gtk_widget_show (image);
    gtk_widget_show (button);
    gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, CONTENT_ACTION_SAVEAS);

    gtk_dialog_add_button (GTK_DIALOG (dialog),
                   GTK_STOCK_CANCEL, CONTENT_ACTION_NONE);
    gtk_dialog_add_button (GTK_DIALOG (dialog),
                   action_label, mAction);

    gtk_window_set_icon_name (GTK_WINDOW (dialog), "web-browser");

    gtk_dialog_set_default_response (GTK_DIALOG (dialog), (guint) mAction);

    NS_ADDREF (this);
    g_signal_connect_data (dialog, "response",
                   G_CALLBACK (response_cb), this,
                   (GClosureNotify) release_cb, (GConnectFlags) 0);
    gtk_window_present (GTK_WINDOW (dialog));

    return NS_OK;
}

NS_METHOD GContentHandler::MIMEInitiateAction (void)
{
    gboolean auto_downloads;

    if (eel_gconf_get_boolean (CONF_LOCKDOWN_DISABLE_SAVE_TO_DISK)) return NS_OK;

    auto_downloads = eel_gconf_get_boolean (CONF_AUTO_DOWNLOADS);

#ifdef MOZ_NSIMIMEINFO_NSACSTRING_
    mHelperApp = gnome_vfs_mime_get_default_application (mMimeType.get());
    mPermission = ephy_file_check_mime (mMimeType.get());
#else
    mHelperApp = gnome_vfs_mime_get_default_application (mMimeType);
    mPermission = ephy_file_check_mime (mMimeType);
#endif

    if (auto_downloads)
    {
        mAction = CONTENT_ACTION_OPEN;
    }
    else
    {
        mAction = CONTENT_ACTION_OPEN_TMP;
    }

    if (!mHelperApp || mPermission != EPHY_MIME_PERMISSION_SAFE)
    {
        mAction = CONTENT_ACTION_DOWNLOAD;
    }

    if (!auto_downloads || mAction == CONTENT_ACTION_DOWNLOAD)
    {
        MIMEConfirmAction ();
    }
    else
    {
        MIMEDoAction ();
    }

    return NS_OK;
}

NS_METHOD GContentHandler::MIMEDoAction (void)
{
    nsCOMPtr<nsIMIMEInfo> mimeInfo;
    mLauncher->GetMIMEInfo(getter_AddRefs(mimeInfo));
    NS_ENSURE_TRUE (mimeInfo, NS_ERROR_FAILURE);

    if (mAction == CONTENT_ACTION_OPEN)
    {
        nsEmbedString desc;

        NS_CStringToUTF16 (nsEmbedCString ("gnome-default"),
                       NS_CSTRING_ENCODING_UTF8, desc);

        /* HACK we use the application description to ask
           MozDownload to open the file when download
           is finished */
#ifdef MOZ_NSIMIMEINFO_NSACSTRING_
        mimeInfo->SetApplicationDescription (desc);
#else
        mimeInfo->SetApplicationDescription (desc.get());
#endif
    }
    else
    {
#ifdef MOZ_NSIMIMEINFO_NSACSTRING_
        mimeInfo->SetApplicationDescription (nsEmbedString ());
#else
        mimeInfo->SetApplicationDescription (nsnull);
#endif
    }

    if (mAction == CONTENT_ACTION_OPEN)
    {
        mLauncher->SaveToDisk (nsnull, PR_FALSE);
    }
    else if (mAction == CONTENT_ACTION_OPEN_TMP)
    {
        mLauncher->LaunchWithApplication (nsnull, PR_FALSE);
    }
    else if (mAction == CONTENT_ACTION_NONE)
    {
        mLauncher->Cancel ();
    }
    else
    {
        mLauncher->SaveToDisk (nsnull, PR_FALSE);
    }

    return NS_OK;
}