2 * Copyright © 2000-2003 Marco Pesenti Gritti
3 * Copyright © 2003 Christian Persch
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * $Id: ephy-embed-persist.c 6952 2007-03-11 19:42:02Z chpe $
24 #include "ephy-embed-persist.h"
25 #include "mozilla-embed-persist.h"
26 #include "ephy-embed-type-builtins.h"
27 #include "ephy-debug.h"
29 #include <gtk/gtkmain.h>
36 PROP_FILECHOOSER_TITLE
,
37 PROP_FILECHOOSER_PARENT
,
46 #define EPHY_EMBED_PERSIST_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_EMBED_PERSIST, EphyEmbedPersistPrivate))
48 struct _EphyEmbedPersistPrivate
56 EphyEmbedPersistFlags flags
;
61 static void ephy_embed_persist_class_init (EphyEmbedPersistClass
*klass
);
62 static void ephy_embed_persist_init (EphyEmbedPersist
*ges
);
64 static GObjectClass
*parent_class
= NULL
;
67 ephy_embed_persist_get_type (void)
69 static GType type
= 0;
71 if (G_UNLIKELY (type
== 0))
73 const GTypeInfo our_info
=
75 sizeof (EphyEmbedPersistClass
),
77 NULL
, /* base_finalize */
78 (GClassInitFunc
) ephy_embed_persist_class_init
,
79 NULL
, /* class_finalize */
80 NULL
, /* class_data */
81 sizeof (EphyEmbedPersist
),
83 (GInstanceInitFunc
) ephy_embed_persist_init
86 type
= g_type_register_static (G_TYPE_OBJECT
,
88 &our_info
, G_TYPE_FLAG_ABSTRACT
);
95 * ephy_embed_persist_set_dest:
96 * @persist: an #EphyEmbedPersist
97 * @value: the path to which @persist should save data
99 * Sets the path to which @persist should save data.
102 ephy_embed_persist_set_dest (EphyEmbedPersist
*persist
,
105 g_return_if_fail (EPHY_IS_EMBED_PERSIST (persist
));
107 persist
->priv
->dest
= g_strdup (value
);
111 * ephy_embed_persist_set_embed:
112 * @persist: an #EphyEmbedPersist
113 * @value: a parent #EphyEmbed
115 * Sets the #EphyEmbed from which @persist will download data.
117 * An #EphyEmbed is absolutely required to download if @persist's
118 * #EphyEmbedPersistFlags include %EPHY_EMBED_PERSIST_COPY_PAGE. Regardless, an
119 * #EphyEmbed should be set for <emphasis>every</emphasis> #EphyEmbedPersist,
120 * since it determines request information such as the referring page.
123 ephy_embed_persist_set_embed (EphyEmbedPersist
*persist
,
126 g_return_if_fail (EPHY_IS_EMBED_PERSIST (persist
));
128 persist
->priv
->embed
= value
;
132 * ephy_embed_persist_set_fc_title:
133 * @persist: an #EphyEmbedPersist
134 * @value: the title to be displayed by the filechooser
136 * Sets the title of the filechooser window. The filechooser will only be
137 * displayed if %EPHY_EMBED_PERSIST_ASK_DESTINATION has been set with
138 * ephy_embed_persist_set_flags().
141 ephy_embed_persist_set_fc_title (EphyEmbedPersist
*persist
,
144 g_return_if_fail (EPHY_IS_EMBED_PERSIST (persist
));
146 persist
->priv
->fc_title
= g_strdup (value
);
150 * ephy_embed_persist_set_fc_parent:
151 * @persist: an #EphyEmbedPersist
152 * @value: the #EphyWindow which should be the filechooser's parent
154 * Sets the #EphyWindow which should be @persist's filechooser's parent. The
155 * filechooser will only be displayed if %EPHY_EMBED_PERSIST_ASK_DESTINATION has been
156 * set with ephy_embed_persist_set_flags().
159 ephy_embed_persist_set_fc_parent (EphyEmbedPersist
*persist
,
162 EphyEmbedPersistPrivate
*priv
;
165 g_return_if_fail (EPHY_IS_EMBED_PERSIST (persist
));
167 priv
= persist
->priv
;
169 priv
->fc_parent
= value
;
170 wptr
= &priv
->fc_parent
;
171 g_object_add_weak_pointer (G_OBJECT (priv
->fc_parent
),
176 * ephy_embed_persist_set_flags:
177 * @persist: an #EphyEmbedPersist
178 * @value: the desired #EphyEmbedPersistFlags
180 * Sets the flags to be used for @persist's download.
183 ephy_embed_persist_set_flags (EphyEmbedPersist
*persist
,
184 EphyEmbedPersistFlags value
)
186 g_return_if_fail (EPHY_IS_EMBED_PERSIST (persist
));
188 persist
->priv
->flags
= value
;
192 * ephy_embed_persist_set_max_size:
193 * @persist: an #EphyEmbedPersist
194 * @value: maximum size of requested download, in bytes
196 * Sets the maximum size of @persist's download.
198 * If the requested file is discovered to be larger than @value, the download
199 * will be aborted. Note that @persist will have to actually begin downloading
200 * before it can abort, since it doesn't know the filesize before the download
204 ephy_embed_persist_set_max_size (EphyEmbedPersist
*persist
,
207 g_return_if_fail (EPHY_IS_EMBED_PERSIST (persist
));
209 persist
->priv
->max_size
= value
;
213 * ephy_embed_persist_set_persist_key:
214 * @persist: an #EphyEmbedPersist
215 * @value: a GConf key
217 * Sets the GConf key from which @persist will determine the default download
221 ephy_embed_persist_set_persist_key (EphyEmbedPersist
*persist
,
224 g_return_if_fail (EPHY_IS_EMBED_PERSIST (persist
));
226 persist
->priv
->persist_key
= g_strdup (value
);
230 * ephy_embed_persist_set_source:
231 * @persist: an #EphyEmbedPersist
232 * @value: the URL from which @persist should download
234 * Sets the URL from which @persist should download. This should be used in
235 * conjunction with ephy_embed_persist_set_embed().
238 ephy_embed_persist_set_source (EphyEmbedPersist
*persist
,
241 g_return_if_fail (EPHY_IS_EMBED_PERSIST (persist
));
243 persist
->priv
->source
= g_strdup (value
);
247 * ephy_embed_persist_set_user_time:
248 * @persist: an #EphyEmbedPersist
249 * @user_time: a timestamp, or 0
251 * Sets the time stamp of the user action which created @persist.
252 * Defaults to gtk_get_current_event_time() when @persist is created.
255 ephy_embed_persist_set_user_time (EphyEmbedPersist
*persist
,
258 g_return_if_fail (EPHY_IS_EMBED_PERSIST (persist
));
260 LOG ("ephy_embed_persist_set_user_time persist %p user-time %d",
263 persist
->priv
->user_time
= user_time
;
267 * ephy_embed_persist_get_dest:
268 * @persist: an #EphyEmbedPersist
270 * Returns the destination filename to which @persist will save its download.
272 * Return value: @persist's destination filename
275 ephy_embed_persist_get_dest (EphyEmbedPersist
*persist
)
277 g_return_val_if_fail (EPHY_IS_EMBED_PERSIST (persist
), NULL
);
279 return persist
->priv
->dest
;
283 * ephy_embed_persist_get_embed:
284 * @persist: an #EphyEmbedPersist
286 * Returns the #EphyEmbed from which @persist will download.
288 * Return value: the #EphyEmbed from which @persist will download
291 ephy_embed_persist_get_embed (EphyEmbedPersist
*persist
)
293 g_return_val_if_fail (EPHY_IS_EMBED_PERSIST (persist
), NULL
);
295 return persist
->priv
->embed
;
299 * ephy_embed_persist_get_fc_title:
300 * @persist: an #EphyEmbedPersist
302 * Returns the title to be displayed in @persist's filechooser.
304 * Return value: the title to be displayed in @persist's filechooser
307 ephy_embed_persist_get_fc_title (EphyEmbedPersist
*persist
)
309 g_return_val_if_fail (EPHY_IS_EMBED_PERSIST (persist
), NULL
);
311 return persist
->priv
->fc_title
;
315 * ephy_embed_persist_get_fc_parent:
316 * @persist: an #EphyEmbedPersist
318 * Returns the #EphyWindow which should serve as a parent for @persist's
321 * Return value: the #EphyWindow parent for @persist's filechooser
324 ephy_embed_persist_get_fc_parent (EphyEmbedPersist
*persist
)
326 g_return_val_if_fail (EPHY_IS_EMBED_PERSIST (persist
), NULL
);
328 return persist
->priv
->fc_parent
;
332 * ephy_embed_persist_get_flags:
333 * @persist: an #EphyEmbedPersist
335 * Returns @persist's #EphyEmbedPersistFlags.
337 * Return value: @persist's #EphyEmbedPersistFlags
339 EphyEmbedPersistFlags
340 ephy_embed_persist_get_flags (EphyEmbedPersist
*persist
)
342 g_return_val_if_fail (EPHY_IS_EMBED_PERSIST (persist
), 0);
344 return persist
->priv
->flags
;
348 * ephy_embed_persist_get_max_size:
349 * @persist: an #EphyEmbedPersist
351 * Returns the maximum size of @persist's requested download, in bytes.
353 * Return value: the maximum size of @persist's requested download, in bytes
356 ephy_embed_persist_get_max_size (EphyEmbedPersist
*persist
)
358 g_return_val_if_fail (EPHY_IS_EMBED_PERSIST (persist
), 0);
360 return persist
->priv
->max_size
;
364 * ephy_embed_persist_get_persist_key:
365 * @persist: an #EphyEmbedPersist
367 * Returns the GConf key which determines Epiphany's default download directory.
369 * Return value: the GConf key to the default download directory
372 ephy_embed_persist_get_persist_key (EphyEmbedPersist
*persist
)
374 g_return_val_if_fail (EPHY_IS_EMBED_PERSIST (persist
), NULL
);
376 return persist
->priv
->persist_key
;
380 * ephy_embed_persist_get_source:
381 * @persist: an #EphyEmbedPersist
383 * Returns the source URL of the file to download.
385 * Return value: a source URL
388 ephy_embed_persist_get_source (EphyEmbedPersist
*persist
)
390 g_return_val_if_fail (EPHY_IS_EMBED_PERSIST (persist
), NULL
);
392 return persist
->priv
->source
;
396 * ephy_embed_persist_get_user_time:
397 * @persist: an #EphyEmbedPersist
399 * Returns the timestamp of the user action which created @persist.
400 * If not set explicitly, defaults to gtk_get_current_event_time ()
401 * at the time of creation of @persist.
403 * Return value: a timestamp, or 0
406 ephy_embed_persist_get_user_time (EphyEmbedPersist
*persist
)
408 g_return_val_if_fail (EPHY_IS_EMBED_PERSIST (persist
), 0);
410 return persist
->priv
->user_time
;
414 ephy_embed_persist_set_property (GObject
*object
,
419 EphyEmbedPersist
*persist
= EPHY_EMBED_PERSIST (object
);
424 ephy_embed_persist_set_dest (persist
, g_value_get_string (value
));
427 ephy_embed_persist_set_embed (persist
, g_value_get_object (value
));
429 case PROP_FILECHOOSER_TITLE
:
430 ephy_embed_persist_set_fc_title (persist
, g_value_get_string (value
));
432 case PROP_FILECHOOSER_PARENT
:
433 ephy_embed_persist_set_fc_parent (persist
, g_value_get_object (value
));
436 ephy_embed_persist_set_flags (persist
, g_value_get_flags (value
));
439 ephy_embed_persist_set_max_size (persist
, g_value_get_int64 (value
));
441 case PROP_PERSISTKEY
:
442 ephy_embed_persist_set_persist_key (persist
, g_value_get_string (value
));
445 ephy_embed_persist_set_source (persist
, g_value_get_string (value
));
448 ephy_embed_persist_set_user_time (persist
, g_value_get_uint (value
));
454 ephy_embed_persist_get_property (GObject
*object
,
459 EphyEmbedPersist
*persist
= EPHY_EMBED_PERSIST (object
);
464 g_value_set_string (value
, ephy_embed_persist_get_dest (persist
));
467 g_value_set_object (value
, ephy_embed_persist_get_embed (persist
));
469 case PROP_FILECHOOSER_TITLE
:
470 g_value_set_string (value
, ephy_embed_persist_get_fc_title (persist
));
472 case PROP_FILECHOOSER_PARENT
:
473 g_value_set_object (value
, ephy_embed_persist_get_fc_parent (persist
));
476 g_value_set_flags (value
, ephy_embed_persist_get_flags (persist
));
479 g_value_set_int64 (value
, ephy_embed_persist_get_max_size (persist
));
481 case PROP_PERSISTKEY
:
482 g_value_set_string (value
, ephy_embed_persist_get_persist_key (persist
));
485 g_value_set_string (value
, ephy_embed_persist_get_source (persist
));
488 g_value_set_uint (value
, ephy_embed_persist_get_user_time (persist
));
494 ephy_embed_persist_init (EphyEmbedPersist
*persist
)
496 persist
->priv
= EPHY_EMBED_PERSIST_GET_PRIVATE (persist
);
498 LOG ("EphyEmbedPersist initialising %p", persist
);
500 persist
->priv
->max_size
= -1;
502 ephy_embed_persist_set_user_time (persist
, gtk_get_current_event_time ());
506 ephy_embed_persist_finalize (GObject
*object
)
508 EphyEmbedPersist
*persist
= EPHY_EMBED_PERSIST (object
);
509 EphyEmbedPersistPrivate
*priv
= persist
->priv
;
513 g_free (priv
->source
);
514 g_free (priv
->fc_title
);
515 g_free (priv
->persist_key
);
517 if (priv
->fc_parent
!= NULL
)
519 wptr
= &priv
->fc_parent
;
520 g_object_remove_weak_pointer (G_OBJECT (priv
->fc_parent
),
524 LOG ("EphyEmbedPersist finalised %p", object
);
526 G_OBJECT_CLASS (parent_class
)->finalize (object
);
530 ephy_embed_persist_class_init (EphyEmbedPersistClass
*klass
)
532 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
534 parent_class
= g_type_class_peek_parent (klass
);
536 object_class
->finalize
= ephy_embed_persist_finalize
;
537 object_class
->set_property
= ephy_embed_persist_set_property
;
538 object_class
->get_property
= ephy_embed_persist_get_property
;
542 * EphyEmbedPersist::completed:
544 * The ::completed signal is emitted when @persist has finished downloading. The
545 * download must have started with ephy_embed_persist_save().
547 g_signal_new ("completed",
548 G_OBJECT_CLASS_TYPE (object_class
),
550 G_STRUCT_OFFSET (EphyEmbedPersistClass
, completed
),
552 g_cclosure_marshal_VOID__VOID
,
556 * EphyEmbedPersist::cancelled:
558 * The ::cancelled signal is emitted when @persist's download has been
559 * cancelled with ephy_embed_persist_cancel().
561 g_signal_new ("cancelled",
562 G_OBJECT_CLASS_TYPE (object_class
),
564 G_STRUCT_OFFSET (EphyEmbedPersistClass
, cancelled
),
566 g_cclosure_marshal_VOID__VOID
,
570 g_object_class_install_property (object_class
,
572 g_param_spec_string ("dest",
574 "Destination file path",
576 G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
578 g_object_class_install_property (object_class
,
580 g_param_spec_object ("embed",
582 "The embed containing the document",
584 G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
|
585 G_PARAM_CONSTRUCT_ONLY
));
587 g_object_class_install_property (object_class
,
588 PROP_FILECHOOSER_TITLE
,
589 g_param_spec_string ("filechooser-title",
591 "Title to use if showing filechooser",
593 G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
595 g_object_class_install_property (object_class
,
596 PROP_FILECHOOSER_PARENT
,
597 g_param_spec_object ("filechooser-parent",
598 "Filechooser parent",
599 "The parent window for the filechooser",
601 G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
603 g_object_class_install_property (object_class
,
605 g_param_spec_flags ("flags",
608 EPHY_TYPE_EMBED_PERSIST_FLAGS
,
610 G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
612 g_object_class_install_property (object_class
,
614 g_param_spec_int64 ("max-size",
616 "Maximum size of the file",
620 G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
622 g_object_class_install_property (object_class
,
624 g_param_spec_string ("persist-key",
626 "Path persistence gconf key",
628 G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
630 g_object_class_install_property (object_class
,
632 g_param_spec_string ("source",
634 "Url of the document to save",
636 G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
638 g_object_class_install_property (object_class
,
640 g_param_spec_uint ("user-time",
646 G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
648 g_type_class_add_private (object_class
, sizeof(EphyEmbedPersistPrivate
));
652 * ephy_embed_persist_cancel:
653 * @persist: an #EphyEmbedPersist
655 * Cancels @persist's download. This will not delete the partially downloaded
659 ephy_embed_persist_cancel (EphyEmbedPersist
*persist
)
661 EphyEmbedPersistClass
*klass
= EPHY_EMBED_PERSIST_GET_CLASS (persist
);
662 klass
->cancel (persist
);
666 * ephy_embed_persist_save:
667 * @persist: an #EphyEmbedPersist
669 * Begins saving the file specified in @persist.
671 * If @persist's #EphyEmbedPersistFlags include %EPHY_EMBED_PERSIST_ASK_DESTINATION, a
672 * filechooser dialog will be shown first.
674 * The file will continue to download in the background until either the
675 * ::completed or the ::cancelled signals are emitted by @persist.
677 * Return value: %TRUE if the download began successfully
680 ephy_embed_persist_save (EphyEmbedPersist
*persist
)
682 EphyEmbedPersistClass
*klass
= EPHY_EMBED_PERSIST_GET_CLASS (persist
);
683 return klass
->save (persist
);
687 * ephy_embed_persist_to_string:
688 * @persist: an #EphyEmbedPersist
690 * Returns the download specified by @persist as a string instead of downloading
693 * The download is synchronous. An #EphyEmbed must be specified with
694 * ephy_embed_persist_set_embed(). The function implicitly assumes that
695 * @persist's #EphyEmbedPersistFlags include %EPHY_EMBED_PERSIST_COPY_PAGE. If @persist's
696 * #EphyEmbed has not finished downloading, this function will only return the
697 * portion of data which has already been downloaded.
699 * The document will be modified: it will only include absolute links and it
700 * will be encoded as UTF-8.
702 * Return value: The contents of @persist's #EphyEmbed's web page
705 ephy_embed_persist_to_string (EphyEmbedPersist
*persist
)
707 EphyEmbedPersistClass
*klass
= EPHY_EMBED_PERSIST_GET_CLASS (persist
);
708 return klass
->to_string (persist
);