aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/MozDownload.cpp
blob: 7ad66d27495776e4980cd10ad6a441fdf424a165 (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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2002
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *  Conrad Carlen <ccarlen@netscape.com>
 * 
 * Adapted for epiphany by Marco Pesenti Gritti <marco@gnome.org>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK *****
 *
 * $Id$
 */

#include "mozilla-config.h"

#include "config.h"

#include "mozilla-download.h"
#include "eel-gconf-extensions.h"
#include "ephy-prefs.h"
#include "ephy-file-helpers.h"
#include "ephy-debug.h"
#include "MozDownload.h"
#include "EphyUtils.h"

#include <libgnomevfs/gnome-vfs-utils.h>
#include <glib/gi18n.h>

#include <nsIDOMDocument.h>
#include <nsILocalFile.h>
#include <nsIWebBrowserPersist.h>
#include <nsIObserver.h>
#include <nsIRequest.h>
#include <nsIFileURL.h>
#undef MOZILLA_INTERNAL_API
#include <nsEmbedString.h>
#define MOZILLA_INTERNAL_API 1
#include <nsMemory.h>
#include <nsNetError.h>

#include <stdlib.h>

const char* const persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";

MozDownload::MozDownload() :
    mTotalProgress(-1),
    mCurrentProgress(0),
    mMaxSize(-1),
    mStatus(NS_OK),
    mEmbedPersist(nsnull),
    mDownloadState(EPHY_DOWNLOAD_INITIALISING)
{
    LOG ("MozDownload ctor (%p)", (void *) this);
}

MozDownload::~MozDownload()
{
    LOG ("MozDownload dtor (%p)", (void *) this);

    NS_ASSERTION (!mEphyDownload, "MozillaDownload still alive!");
}

#ifdef HAVE_GECKO_1_8
NS_IMPL_ISUPPORTS3(MozDownload, nsIWebProgressListener, nsIWebProgressListener2, nsITransfer)
#else
NS_IMPL_ISUPPORTS3(MozDownload, nsIWebProgressListener, nsIDownload, nsITransfer)
#endif

#ifdef HAVE_GECKO_1_8
nsresult
MozDownload::InitForEmbed (nsIURI *aSource, nsIURI *aTarget, const nsAString &aDisplayName,
                   nsIMIMEInfo *aMIMEInfo, PRTime aStartTime, nsICancelable *aCancelable,
                   MozillaEmbedPersist *aEmbedPersist, PRInt64 aMaxSize)
{
    mEmbedPersist = aEmbedPersist;
    mMaxSize = aMaxSize;
    return Init (aSource, aTarget, aDisplayName, aMIMEInfo, aStartTime, aCancelable);
}
#else
nsresult
MozDownload::InitForEmbed (nsIURI *aSource, nsIURI *aTarget, const PRUnichar *aDisplayName,
                   nsIMIMEInfo *aMIMEInfo, PRInt64 startTime, nsIWebBrowserPersist *aPersist,
                   MozillaEmbedPersist *aEmbedPersist, PRInt64 aMaxSize)
{
    mEmbedPersist = aEmbedPersist;
    mMaxSize = aMaxSize;
    return Init (aSource, aTarget, aDisplayName, aMIMEInfo, startTime, aPersist);
}
#endif

#ifdef HAVE_GECKO_1_8
/* void init (in nsIURI aSource, in nsIURI aTarget, in AString aDisplayName, in nsIMIMEInfo aMIMEInfo, in PRTime startTime, in nsICancelable aCancelable); */
NS_IMETHODIMP
MozDownload::Init (nsIURI *aSource,
           nsIURI *aTarget,
           const nsAString &aDisplayName,
           nsIMIMEInfo *aMIMEInfo,
           PRTime aStartTime,
           nsICancelable *aCancelable)
#else
/* void init (in nsIURI aSource, in nsIURI aTarget, in wstring aDisplayName, in nsIMIMEInfo aMIMEInfo, in long long startTime, in nsIWebBrowserPersist aPersist); */
NS_IMETHODIMP
MozDownload::Init (nsIURI *aSource,
           nsIURI *aTarget,
           const PRUnichar *aDisplayName,
           nsIMIMEInfo *aMIMEInfo,
           PRInt64 aStartTime,
           nsIWebBrowserPersist *aPersist)
#endif
{
    PRBool addToView = PR_TRUE;

    if (mEmbedPersist)
    {
        EphyEmbedPersistFlags flags;

        flags = ephy_embed_persist_get_flags (EPHY_EMBED_PERSIST (mEmbedPersist));

        addToView = !(flags & EPHY_EMBED_PERSIST_NO_VIEW);
    }

    mSource = aSource;
    mDestination = aTarget;
    mStartTime = aStartTime;
    mTotalProgress = 0;
    mCurrentProgress = 0;
    mPercentComplete = 0;
    mInterval = 200000; /* microsec */
    mLastUpdate = mStartTime;
    mMIMEInfo = aMIMEInfo;

#ifdef HAVE_GECKO_1_8
    /* This will create a refcount cycle, which needs to be broken in ::OnStateChange */
    mCancelable = aCancelable;
#else
    if (aPersist)
    {
        mWebPersist = aPersist;
        aPersist->SetProgressListener(this);
        }
#endif

    if (addToView)
    { 
        DownloaderView *dview;
        dview = EPHY_DOWNLOADER_VIEW
            (ephy_embed_shell_get_downloader_view (embed_shell));
        mEphyDownload = mozilla_download_new (this);
        g_object_add_weak_pointer (G_OBJECT (mEphyDownload),
                       (gpointer *) &mEphyDownload);
        downloader_view_add_download (dview, mEphyDownload);
        g_object_unref (mEphyDownload);
    }
    else
    {
        mEphyDownload = nsnull;
    }

    return NS_OK;
}

NS_IMETHODIMP
MozDownload::GetSource(nsIURI **aSource)
{
    NS_ENSURE_ARG_POINTER(aSource);
    NS_IF_ADDREF(*aSource = mSource);

    return NS_OK;
}

#ifndef HAVE_GECKO_1_8
NS_IMETHODIMP
MozDownload::GetTarget(nsIURI **aTarget)
{
    NS_ENSURE_ARG_POINTER(aTarget);
    NS_IF_ADDREF(*aTarget = mDestination);

    return NS_OK;
}
#endif

NS_IMETHODIMP
MozDownload::GetTargetFile (nsILocalFile** aTargetFile)
{
    nsresult rv;
                                                                                                                              
    nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(mDestination, &rv);
    if (NS_FAILED(rv)) return rv;
                                                                                                                              
    nsCOMPtr<nsIFile> file;
    rv = fileURL->GetFile(getter_AddRefs(file));
    if (NS_SUCCEEDED(rv))
        rv = CallQueryInterface(file, aTargetFile);
    return rv;
}

#ifndef HAVE_GECKO_1_8
NS_IMETHODIMP
MozDownload::GetPersist(nsIWebBrowserPersist **aPersist)
{
    NS_ENSURE_ARG_POINTER(aPersist);
    NS_IF_ADDREF(*aPersist = mWebPersist);

    return NS_OK;
}
#endif

NS_IMETHODIMP
MozDownload::GetPercentComplete(PRInt32 *aPercentComplete)
{
    NS_ENSURE_ARG_POINTER(aPercentComplete);
    *aPercentComplete = mPercentComplete;

    return NS_OK;
}

#ifndef HAVE_GECKO_1_8
NS_IMETHODIMP
MozDownload::GetStartTime(PRInt64 *aStartTime)
{
    NS_ENSURE_ARG_POINTER(aStartTime);
    *aStartTime = mStartTime;

    return NS_OK;
}

NS_IMETHODIMP
MozDownload::GetDisplayName(PRUnichar * *aDisplayName)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
MozDownload::SetDisplayName(const PRUnichar * aDisplayName)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}
#endif /* !HAVE_GECKO_1_8 */

NS_IMETHODIMP
MozDownload::GetTotalProgress(PRInt64 *aTotalProgress)
{
    NS_ENSURE_ARG_POINTER(aTotalProgress);
    *aTotalProgress = mTotalProgress;

    return NS_OK;
}

NS_IMETHODIMP
MozDownload::GetCurrentProgress(PRInt64 *aCurrentProgress)
{
    NS_ENSURE_ARG_POINTER(aCurrentProgress);
    *aCurrentProgress = mCurrentProgress;

    return NS_OK;
}

NS_IMETHODIMP
MozDownload::GetState(EphyDownloadState *aDownloadState)
{
    NS_ENSURE_ARG_POINTER(aDownloadState);
    *aDownloadState = mDownloadState;

    return NS_OK;
}

NS_IMETHODIMP
MozDownload::GetElapsedTime(PRInt64 *aElapsedTime)
{
    NS_ENSURE_ARG_POINTER(aElapsedTime);
    *aElapsedTime = PR_Now() - mStartTime;

    return NS_OK;
}

NS_IMETHODIMP
MozDownload::GetMIMEInfo(nsIMIMEInfo **aMIMEInfo)
{
        NS_ENSURE_ARG_POINTER(aMIMEInfo);
    NS_IF_ADDREF(*aMIMEInfo = mMIMEInfo);

    return NS_OK;
}

#ifndef HAVE_GECKO_1_8
NS_IMETHODIMP
MozDownload::GetListener(nsIWebProgressListener **aListener)
{
    NS_ENSURE_ARG_POINTER(aListener);

    return CallQueryInterface (this, aListener);
}

NS_IMETHODIMP
MozDownload::SetListener(nsIWebProgressListener *aListener)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
MozDownload::GetObserver(nsIObserver **aObserver)
{
    *aObserver = mObserver;
    NS_IF_ADDREF (*aObserver);

    return NS_OK;
}

NS_IMETHODIMP
MozDownload::SetObserver(nsIObserver *aObserver)
{
    mObserver = aObserver;

    return NS_OK;
}
#endif

NS_IMETHODIMP 
MozDownload::OnStateChange (nsIWebProgress *aWebProgress, nsIRequest *aRequest,
                PRUint32 aStateFlags, nsresult aStatus)
{
    nsresult rv;

    if (NS_FAILED(aStatus) && NS_SUCCEEDED(mStatus))
            mStatus = aStatus;

    if (aStateFlags & STATE_START)
    {
        mDownloadState = EPHY_DOWNLOAD_DOWNLOADING;

        if (mEphyDownload)
        {
            g_signal_emit_by_name (mEphyDownload, "changed");
        }
    }

    /* We will get this even in the event of a cancel */
    if (aStateFlags & STATE_STOP)
    {
        /* Keep us alive */
        nsCOMPtr<nsITransfer> kungFuDeathGrip(this);

        mDownloadState = NS_SUCCEEDED (aStatus) ? EPHY_DOWNLOAD_COMPLETED : EPHY_DOWNLOAD_FAILED;
        if (mEphyDownload)
        {
            g_signal_emit_by_name (mEphyDownload, "changed");
        }

#ifdef HAVE_GECKO_1_8
        /* break refcount cycle */
        mCancelable = nsnull;
#else
            if (mWebPersist)
        {
                mWebPersist->SetProgressListener(nsnull);
                mWebPersist = nsnull;
            }
#endif

        if (mEmbedPersist)
        {
            if (NS_SUCCEEDED (aStatus))
            {
                mozilla_embed_persist_completed (mEmbedPersist);
            }
            else
            {
                mozilla_embed_persist_cancelled (mEmbedPersist);
            }
        }
        else if (NS_SUCCEEDED (aStatus))
        {
            GnomeVFSMimeApplication *helperApp;
            nsEmbedCString mimeType;
#ifdef HAVE_GECKO_1_8
            rv = mMIMEInfo->GetMIMEType (mimeType);
            NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

            nsEmbedString description;
            mMIMEInfo->GetApplicationDescription (description);

            nsEmbedCString cDesc;
            NS_UTF16ToCString (description, NS_CSTRING_ENCODING_UTF8, cDesc);
#else
            char *mime = nsnull;
            rv = mMIMEInfo->GetMIMEType (&mime);
            NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

            if (mime)
            {
                mimeType.Assign (mime);
                nsMemory::Free (mime);
            }

            PRUnichar *description;
            mMIMEInfo->GetApplicationDescription (&description);
            NS_ENSURE_TRUE (description, NS_ERROR_FAILURE);

            nsEmbedCString cDesc;
            NS_UTF16ToCString (nsEmbedString(description),
                       NS_CSTRING_ENCODING_UTF8, cDesc);

            nsMemory::Free (description);
#endif

            /* HACK we use the application description to decide
               if we have to open the saved file */
            if (g_str_has_prefix (cDesc.get(), "gnome-default:"))
            {
                /* Format gnome-default:<usertime>:<helperapp id> */
                char **str = g_strsplit (cDesc.get(), ":", -1);
                g_return_val_if_fail (g_strv_length (str) == 3, NS_ERROR_FAILURE);

                char *end;
                guint32 user_time = strtoul (str[1], &end, 0);

                helperApp = gnome_vfs_mime_application_new_from_desktop_id (str[2]);
                if (!helperApp) return NS_ERROR_FAILURE;

                nsEmbedCString aDest;
                rv = mDestination->GetSpec (aDest);
                NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

                ephy_file_launch_application (helperApp, aDest.get (), user_time);

                gnome_vfs_mime_application_free (helperApp);
                g_strfreev (str);
            }
        }
    }
        
    return NS_OK; 
}

#ifdef HAVE_GECKO_1_8
NS_IMETHODIMP
MozDownload::OnProgressChange (nsIWebProgress *aWebProgress,
                   nsIRequest *aRequest,
                   PRInt32 aCurSelfProgress,
                   PRInt32 aMaxSelfProgress,
                   PRInt32 aCurTotalProgress,
                   PRInt32 aMaxTotalProgress)
{
    return OnProgressChange64 (aWebProgress, aRequest,
                   aCurSelfProgress, aMaxSelfProgress,
                   aCurTotalProgress, aMaxTotalProgress);
}

/* void onProgressChange64 (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long long aCurSelfProgress, in long long aMaxSelfProgress, in long long aCurTotalProgress,
 in long long aMaxTotalProgress); */
NS_IMETHODIMP
MozDownload::OnProgressChange64 (nsIWebProgress *aWebProgress,
                 nsIRequest *aRequest,
                 PRInt64 aCurSelfProgress,
                 PRInt64 aMaxSelfProgress,
                 PRInt64 aCurTotalProgress,
                 PRInt64 aMaxTotalProgress)
#else /* !HAVE_GECKO_1_8 */
NS_IMETHODIMP
MozDownload::OnProgressChange (nsIWebProgress *aWebProgress,
                   nsIRequest *aRequest,
                   PRInt32 aCurSelfProgress,
                   PRInt32 aMaxSelfProgress,
                   PRInt32 aCurTotalProgress,
                   PRInt32 aMaxTotalProgress)
#endif /* HAVE_GECKO_1_8 */
{
    if (mMaxSize >= 0 &&
        ((aMaxTotalProgress > 0 && mMaxSize < aMaxTotalProgress) ||
         mMaxSize < aCurTotalProgress))
    {
        Cancel ();
    }

    if (!mRequest)
        mRequest = aRequest;

    PRInt64 now = PR_Now ();

    if ((now - mLastUpdate < mInterval) &&
        (aMaxTotalProgress != -1) &&
        (aCurTotalProgress < aMaxTotalProgress))
        return NS_OK;

    mLastUpdate = now;

    if (aMaxTotalProgress == -1)
    {
            mPercentComplete = -1;
    }
    else
    {
            mPercentComplete = (PRInt32)(((float)aCurTotalProgress / (float)aMaxTotalProgress) * 100.0 + 0.5);
    }

    mTotalProgress = aMaxTotalProgress;
    mCurrentProgress = aCurTotalProgress;

    if (mEphyDownload)
    {
        g_signal_emit_by_name (mEphyDownload, "changed");
    }

    return NS_OK;
}

NS_IMETHODIMP
MozDownload::OnLocationChange (nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location)
{
    return NS_OK;
}

NS_IMETHODIMP 
MozDownload::OnStatusChange (nsIWebProgress *aWebProgress, nsIRequest *aRequest,
                 nsresult aStatus, const PRUnichar *aMessage)
{
    return NS_OK;
}

NS_IMETHODIMP 
MozDownload::OnSecurityChange (nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
{
    return NS_OK;
}

void
MozDownload::Cancel()
{
    if (mDownloadState != EPHY_DOWNLOAD_DOWNLOADING &&
        mDownloadState != EPHY_DOWNLOAD_PAUSED)
    {
        return;
    }

#ifdef HAVE_GECKO_1_8
    if (mCancelable)
    {
        /* FIXME: error code? */
        mCancelable->Cancel (NS_BINDING_ABORTED);
    }
#else
    if (mWebPersist)
    {
        mWebPersist->CancelSave ();
    }

    if (mObserver)
    {
        mObserver->Observe (nsnull, "oncancel", nsnull);
    }
#endif
}

void
MozDownload::Pause()
{
    if (mRequest)
    {
        mRequest->Suspend ();
        mDownloadState = EPHY_DOWNLOAD_PAUSED;
    }
}

void
MozDownload::Resume()
{
    if (mRequest)
    {
        mRequest->Resume ();
        mDownloadState = EPHY_DOWNLOAD_DOWNLOADING;
    }
}

nsresult InitiateMozillaDownload (nsIDOMDocument *domDocument, nsIURI *sourceURI,
                  nsILocalFile* inDestFile, const char *contentType,
                  nsIURI* inOriginalURI, MozillaEmbedPersist *embedPersist,
                  nsIInputStream *postData, nsISupports *aCacheKey,
                  PRInt64 aMaxSize)
{
    nsresult rv = NS_OK;

    EphyEmbedPersistFlags ephy_flags;
    ephy_flags = ephy_embed_persist_get_flags (EPHY_EMBED_PERSIST (embedPersist));

    PRBool isHTML = (contentType &&
            (strcmp (contentType, "text/html") == 0 ||
             strcmp (contentType, "text/xml") == 0 ||
             strcmp (contentType, "application/xhtml+xml") == 0));

    nsCOMPtr<nsIWebBrowserPersist> webPersist (do_CreateInstance(persistContractID, &rv));
    NS_ENSURE_SUCCESS (rv, rv);
  
    PRInt64 timeNow = PR_Now();
  
    nsEmbedString fileDisplayName;
    inDestFile->GetLeafName(fileDisplayName);

    nsCOMPtr<nsIIOService> ioService;
    rv = EphyUtils::GetIOService (getter_AddRefs (ioService));
    NS_ENSURE_SUCCESS (rv, rv);

    nsCOMPtr<nsIURI> destURI;
    ioService->NewFileURI (inDestFile, getter_AddRefs(destURI));

    MozDownload *downloader = new MozDownload ();
    /* dlListener attaches to its progress dialog here, which gains ownership */
    /* FIXME is that still true? */
#ifdef HAVE_GECKO_1_8
    rv = downloader->InitForEmbed (inOriginalURI, destURI, fileDisplayName,
                       nsnull, timeNow, webPersist, embedPersist, aMaxSize);
    NS_ENSURE_SUCCESS (rv, rv);

    rv = webPersist->SetProgressListener (downloader);
#else
    rv = downloader->InitForEmbed (inOriginalURI, destURI, fileDisplayName.get(),
                       nsnull, timeNow, webPersist, embedPersist, aMaxSize);
#endif
    NS_ENSURE_SUCCESS (rv, rv);

    PRInt32 flags = nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES;

    if (!domDocument && !isHTML && !(ephy_flags & EPHY_EMBED_PERSIST_COPY_PAGE) &&
        !(ephy_flags & EPHY_EMBED_PERSIST_DO_CONVERSION))
    {
        flags |= nsIWebBrowserPersist::PERSIST_FLAGS_NO_CONVERSION;
    }
    if (ephy_flags & EPHY_EMBED_PERSIST_COPY_PAGE)
    {
        flags |= nsIWebBrowserPersist::PERSIST_FLAGS_FROM_CACHE;
    }
    webPersist->SetPersistFlags(flags);

    if (!domDocument || !isHTML || ephy_flags & EPHY_EMBED_PERSIST_COPY_PAGE)
    {
        rv = webPersist->SaveURI (sourceURI, aCacheKey, nsnull,
                      postData, nsnull, inDestFile);
    }
    else
    {
        PRInt32 encodingFlags = 0;
        nsCOMPtr<nsILocalFile> filesFolder;

        /**
         * Construct a directory path to hold the associated files; mozilla
         * will create the directory as needed.
         */

        nsEmbedCString cPath;
        inDestFile->GetNativePath (cPath);

        GString *path = g_string_new (cPath.get());
        char *dot_pos = strchr (path->str, '.');
        if (dot_pos)
        {
            g_string_truncate (path, dot_pos - path->str);
        }
        g_string_append (path, " ");
        g_string_append (path, _("Files"));
      
        filesFolder = do_CreateInstance ("@mozilla.org/file/local;1");
        filesFolder->InitWithNativePath (nsEmbedCString(path->str));

        g_string_free (path, TRUE);

        rv = webPersist->SaveDocument (domDocument, inDestFile, filesFolder,
                           contentType, encodingFlags, 80);
    }
  
    return rv;
}

static char*
GetFilePath (const char *filename)
{
    char *path = NULL, *download_dir, *expanded;

    download_dir = eel_gconf_get_string (CONF_STATE_DOWNLOAD_DIR);

    if (download_dir && strcmp (download_dir, "Downloads") == 0)
    {
        g_free (download_dir);
        download_dir = ephy_file_downloads_dir ();
    }
    else if (download_dir && strcmp (download_dir, "Desktop") == 0)
    {
        g_free (download_dir);
        download_dir = ephy_file_desktop_dir ();
    }  
    else if (download_dir)
    {
        char *converted_dp;

        converted_dp = g_filename_from_utf8 (download_dir, -1, NULL, NULL, NULL);
        g_free (download_dir);
        download_dir = converted_dp;
    }

    if (download_dir == NULL)
    {
        /* Emergency download destination */
        download_dir = g_strdup (g_get_home_dir ());
    }

    g_return_val_if_fail (download_dir != NULL, FALSE);

    expanded = gnome_vfs_expand_initial_tilde (download_dir);
    if (ephy_ensure_dir_exists (expanded))
    {
        path = g_build_filename (expanded, filename, NULL);
    }
    g_free (expanded);
    g_free (download_dir);
    
    if (path == NULL)
    {
        path = g_build_filename (g_get_home_dir (), filename, NULL);
    }

    return path;
}

static const char*
file_is_compressed (const char *filename)
{
        int i;
        const char *compression[] = {".gz", ".bz2", ".Z", ".lz", NULL};

        for (i = 0; compression[i] != NULL; i++)
        {
                if (g_str_has_suffix (filename, compression[i]))
                        return compression[i];
        }

        return NULL;
}

static const char*
parse_extension (const char *filename)
{
        const char *compression;

        compression = file_is_compressed (filename);

        /* If the file is compressed we might have a double extension */
        if (compression != NULL)
        {
                int i;
                const char *extensions[] = {"tar", "ps", "xcf", "dvi", "txt", "text", NULL};

                for (i = 0; extensions[i] != NULL; i++)
                {
                        char *suffix;
                        suffix = g_strdup_printf (".%s%s", extensions[i],
                                                  compression);

                        if (g_str_has_suffix (filename, suffix))
                        {
                                char *p;

                                p = g_strrstr (filename, suffix);
                                g_free (suffix);

                                return p;
                        }

                        g_free (suffix);
                }
        }
        
        /* default case */
        return g_strrstr (filename, ".");
}

nsresult BuildDownloadPath (const char *defaultFileName, nsILocalFile **_retval)
{
    char *path;

    path = GetFilePath (defaultFileName);
    
    if (g_file_test (path, G_FILE_TEST_EXISTS))
    {
        int i = 1;
        const char *dot_pos;
                char *serial = NULL;
        GString *tmp_path;
        gssize position;

                dot_pos = parse_extension (defaultFileName);
        if (dot_pos)
        {
            position = dot_pos - defaultFileName;
        }
        else
        {
            position = strlen (defaultFileName);
        }
        tmp_path = g_string_new (NULL);

        do {
            g_free (path);
            g_string_assign (tmp_path, defaultFileName);
            serial = g_strdup_printf ("(%d)", i++);
            g_string_insert (tmp_path, position, serial);
            g_free (serial);
            path = GetFilePath (tmp_path->str);
            
        } while (g_file_test (path, G_FILE_TEST_EXISTS));
        
        g_string_free (tmp_path, TRUE);
    }

    nsCOMPtr <nsILocalFile> destFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
    NS_ENSURE_TRUE (destFile, NS_ERROR_FAILURE);

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

    NS_IF_ADDREF (*_retval = destFile);
    return NS_OK;
}