aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/mozilla-embed-persist.cpp
blob: 7a20863ef366ae975af9c793ce0309feac410d51 (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
/*
 *  Copyright (C) 2000, 2001, 2002 Marco Pesenti Gritti
 *
 *  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 "mozilla-embed-persist.h"
#include "mozilla-embed.h"
#include "ephy-embed-shell.h"
#include "EphyBrowser.h"
#include "EphyHeaderSniffer.h"
#include "MozDownload.h"

#include <stddef.h>
#include <nsIWebBrowserPersist.h>
#include <nsString.h>
#include <nsCWebBrowserPersist.h>
#include <nsNetUtil.h>
#include <nsIHistoryEntry.h>
#include <nsISHEntry.h>
#include <nsIDocumentEncoder.h>

static void
mozilla_embed_persist_class_init (MozillaEmbedPersistClass *klass);
static void
mozilla_embed_persist_init (MozillaEmbedPersist *ges);
static void
mozilla_embed_persist_finalize (GObject *object);

#define MOZILLA_EMBED_PERSIST_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), MOZILLA_TYPE_EMBED_PERSIST, MozillaEmbedPersistPrivate))

struct MozillaEmbedPersistPrivate
{
    nsCOMPtr<nsIWebBrowserPersist> mPersist;
};

static GObjectClass *parent_class = NULL;

GType
mozilla_embed_persist_get_type (void)
{
       static GType mozilla_embed_persist_type = 0;

        if (mozilla_embed_persist_type == 0)
        {
                static const GTypeInfo our_info =
                {
                        sizeof (MozillaEmbedPersistClass),
                        NULL, /* base_init */
                        NULL, /* base_finalize */
                        (GClassInitFunc) mozilla_embed_persist_class_init,
                        NULL, /* class_finalize */
                        NULL, /* class_data */
                        sizeof (MozillaEmbedPersist),
                        0,    /* n_preallocs */
                        (GInstanceInitFunc) mozilla_embed_persist_init
                };

                mozilla_embed_persist_type = 
                g_type_register_static (EPHY_TYPE_EMBED_PERSIST,
                                                        "MozillaEmbedPersist",
                                                        &our_info, (GTypeFlags)0);
        }

        return mozilla_embed_persist_type;
}

static void
mozilla_embed_persist_init (MozillaEmbedPersist *persist)
{
        persist->priv = MOZILLA_EMBED_PERSIST_GET_PRIVATE (persist);
        persist->priv->mPersist = do_CreateInstance (NS_WEBBROWSERPERSIST_CONTRACTID);
}

static void
mozilla_embed_persist_finalize (GObject *object)
{
        MozillaEmbedPersist *persist = MOZILLA_EMBED_PERSIST (object);

    persist->priv->mPersist = nsnull;

        G_OBJECT_CLASS (parent_class)->finalize (object);
}

void
mozilla_embed_persist_completed (MozillaEmbedPersist *persist)
{
    g_signal_emit_by_name (persist, "completed");
    g_object_unref (persist);
}

void
mozilla_embed_persist_cancelled (MozillaEmbedPersist *persist)
{
    g_object_unref (persist);
}

static void
impl_cancel (EphyEmbedPersist *persist)
{
    nsCOMPtr<nsIWebBrowserPersist> bpersist =
        MOZILLA_EMBED_PERSIST (persist)->priv->mPersist;
    if (bpersist)
    {
        bpersist->CancelSave ();
    }

    g_object_unref (persist);
}

static gboolean
impl_save (EphyEmbedPersist *persist)
{
    nsresult rv;
    char *filename;
    char *uri;
    int max_size;
    EphyEmbed *embed;
    EmbedPersistFlags flags;
    PRUint32 persistFlags = 0;

    /* FIXME implement max size */

    g_object_ref (persist);
    
    g_object_get (persist,
              "source", &uri,        
              "dest", &filename,
              "flags", &flags,
              "embed", &embed,
              "max_size", &max_size,
              NULL);

    g_return_val_if_fail (!(flags & EMBED_PERSIST_COPY_PAGE)
                  || embed != NULL, FALSE); 

    EphyBrowser *browser = NULL;
    if (embed)
    {
        browser = (EphyBrowser *) _mozilla_embed_get_ephy_browser (MOZILLA_EMBED(embed));
        NS_ENSURE_TRUE (browser, FALSE);
    }
    /* we must have one of uri or browser */
    g_assert (browser != NULL || uri != NULL);

    /* Get a temp filename to save to */
    nsCOMPtr<nsIProperties> dirService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
    NS_ENSURE_TRUE (dirService, FALSE);

    nsCOMPtr<nsIFile> tmpFile;
    dirService->Get("TmpD", NS_GET_IID(nsIFile), getter_AddRefs(tmpFile));
    if (!tmpFile) return FALSE;

    /* FIXME: make this filename non-guessable */
    static short unsigned int tmpRandom = 0;
    nsAutoString tmpNo;
    tmpNo.AppendInt(tmpRandom++);
    nsAutoString saveFile(NS_LITERAL_STRING("-sav"));
    saveFile += tmpNo;
    saveFile += NS_LITERAL_STRING("tmp");
    tmpFile->Append(saveFile);

    /* Get the uri to save to */
    nsCOMPtr<nsIURI> inURI;
    nsCAutoString sURI;
    if (uri)
    {
        sURI.Assign (uri);
    }
    else
    {
        rv = browser->GetDocumentUrl (sURI);
        NS_ENSURE_SUCCESS (rv, FALSE);
    }
        NS_NewURI(getter_AddRefs(inURI), sURI);
    if (!inURI) return FALSE;

    /* Get post data */
    nsCOMPtr<nsIInputStream> postData;
    if (browser)
    {
        PRInt32 sindex;

        nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(browser->mWebBrowser));
        nsCOMPtr<nsISHistory> sessionHistory;
        webNav->GetSessionHistory(getter_AddRefs(sessionHistory));
        nsCOMPtr<nsIHistoryEntry> entry;
        sessionHistory->GetIndex(&sindex);
        sessionHistory->GetEntryAtIndex(sindex, PR_FALSE, getter_AddRefs(entry));
        nsCOMPtr<nsISHEntry> shEntry(do_QueryInterface(entry));
        if (shEntry)
        {
            shEntry->GetPostData(getter_AddRefs(postData));
        }
    }

    /* Get the DOM document if a uri is not specified */
    nsCOMPtr<nsIDOMDocument> DOMDocument;
    if (!uri)
    {       
        if (flags & EMBED_PERSIST_MAINDOC)
        {
                    browser->GetDocument (getter_AddRefs(DOMDocument));
        }
            else
        {
                    browser->GetTargetDocument (getter_AddRefs(DOMDocument));
        }
            NS_ENSURE_TRUE (DOMDocument, FALSE);
    }


    /* Get the current page descriptor */
    nsCOMPtr<nsISupports> pageDescriptor;
    if (flags & EMBED_PERSIST_COPY_PAGE)
    {
            browser->GetPageDescriptor(getter_AddRefs(pageDescriptor));
        NS_ENSURE_TRUE (pageDescriptor, FALSE);
    }

    if (filename == NULL)
    {
        /* Create an header sniffer and do the save */
        nsCOMPtr<nsIWebBrowserPersist> webPersist =
            MOZILLA_EMBED_PERSIST (persist)->priv->mPersist;
        NS_ENSURE_TRUE (webPersist, FALSE);

        EphyHeaderSniffer* sniffer = new EphyHeaderSniffer
            (webPersist, MOZILLA_EMBED_PERSIST (persist),
             tmpFile, inURI, DOMDocument, postData);
        if (!sniffer) return FALSE;
 
        webPersist->SetProgressListener(sniffer);
        rv = webPersist->SaveURI(inURI, nsnull, nsnull, nsnull, nsnull, tmpFile);
        if (NS_FAILED (rv)) return FALSE;
    }
    else
    {
        /* Filename to save to */
        nsCOMPtr<nsILocalFile> destFile;
        NS_NewNativeLocalFile (nsDependentCString(filename),
                       PR_TRUE, getter_AddRefs(destFile));
            NS_ENSURE_TRUE (destFile, FALSE);

        rv =  InitiateMozillaDownload (DOMDocument, inURI, destFile,
                           nsnull, inURI, MOZILLA_EMBED_PERSIST (persist),
                           postData, pageDescriptor);
        if (NS_FAILED (rv)) return FALSE;
    }

    g_free (uri);
    g_free (filename);

    return TRUE;
}

static char *
impl_to_string (EphyEmbedPersist *persist)
{
    EphyEmbed *embed;
    nsCOMPtr<nsIDOMDocument> DOMDocument;
    EmbedPersistFlags flags;
    EphyBrowser *browser;
    nsresult rv = NS_OK;

    g_object_ref (persist);
    
    g_object_get (persist,
                  "flags", &flags,
                  "embed", &embed,
                  NULL);
    g_object_unref (persist);
    g_assert (embed != NULL);

    browser = (EphyBrowser *) _mozilla_embed_get_ephy_browser (MOZILLA_EMBED(embed));
    g_assert (browser != NULL);

    if (flags & EMBED_PERSIST_MAINDOC)
    {
        rv = browser->GetDocument (getter_AddRefs(DOMDocument));
    }
        else
    {
        rv = browser->GetTargetDocument (getter_AddRefs(DOMDocument));
    }
    if (NS_FAILED(rv) || !DOMDocument) return NULL;

    nsCOMPtr<nsIDocument> doc = do_QueryInterface(DOMDocument);
    if(!doc) return NULL;

    nsCOMPtr<nsIDocumentEncoder> encoder = do_CreateInstance(NS_DOC_ENCODER_CONTRACTID_BASE "text/html");
    NS_ENSURE_SUCCESS(rv, NULL);

    rv = encoder->Init(doc, NS_LITERAL_STRING("text/html"), nsIDocumentEncoder::OutputAbsoluteLinks);
    NS_ENSURE_SUCCESS(rv, NULL);

    nsAutoString aOutputString;
    encoder->EncodeToString(aOutputString);

    return g_strdup (NS_ConvertUCS2toUTF8(aOutputString).get());
}


static GObject *
mozilla_embed_persist_constructor (GType type, guint n_construct_properties,
                       GObjectConstructParam *construct_params)
{
    /* we depend on single because of mozilla initialization */
    ephy_embed_shell_get_embed_single (embed_shell);

    return parent_class->constructor (type, n_construct_properties,
                      construct_params);
}

static void
mozilla_embed_persist_class_init (MozillaEmbedPersistClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
    EphyEmbedPersistClass *persist_class = EPHY_EMBED_PERSIST_CLASS (klass);
    
        parent_class = (GObjectClass *) g_type_class_peek_parent (klass);
    
        object_class->finalize = mozilla_embed_persist_finalize;
    object_class->constructor = mozilla_embed_persist_constructor;

    persist_class->save = impl_save;
    persist_class->cancel = impl_cancel;
    persist_class->to_string = impl_to_string;

    g_type_class_add_private (object_class, sizeof(MozillaEmbedPersistPrivate));
}