Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / embed / ephy-embed-shell.c
blob99ad7d27a1b235a67440146726a9fea5f628831f
1 /*
2 * Copyright © 2000-2003 Marco Pesenti Gritti
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * $Id: ephy-embed-shell.c 6952 2007-03-11 19:42:02Z chpe $
21 #include "config.h"
23 #include "ephy-embed-shell.h"
24 #include "ephy-embed-single.h"
25 #include "ephy-embed-factory.h"
26 #include "ephy-marshal.h"
27 #include "ephy-file-helpers.h"
28 #include "ephy-history.h"
29 #include "ephy-favicon-cache.h"
30 #include "mozilla-embed-single.h"
31 #include "downloader-view.h"
32 #include "ephy-encodings.h"
33 #include "ephy-debug.h"
34 #include "ephy-adblock-manager.h"
35 #include "ephy-print-utils.h"
37 #include <glib/gi18n.h>
38 #include <gtk/gtkmessagedialog.h>
40 #define PAGE_SETUP_FILENAME "page-setup.ini"
41 #define PRINT_SETTINGS_FILENAME "print-settings.ini"
43 #define EPHY_EMBED_SHELL_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_EMBED_SHELL, EphyEmbedShellPrivate))
45 struct _EphyEmbedShellPrivate
47 EphyHistory *global_history;
48 DownloaderView *downloader_view;
49 EphyFaviconCache *favicon_cache;
50 EphyEmbedSingle *embed_single;
51 EphyEncodings *encodings;
52 EphyAdBlockManager *adblock_manager;
53 GtkPageSetup *page_setup;
54 GtkPrintSettings *print_settings;
55 guint single_initialised : 1;
58 enum
60 PREPARE_CLOSE,
61 LAST_SIGNAL
64 static guint signals[LAST_SIGNAL] = { 0 };
66 static void ephy_embed_shell_class_init (EphyEmbedShellClass *klass);
67 static void ephy_embed_shell_init (EphyEmbedShell *shell);
69 EphyEmbedShell *embed_shell = NULL;
71 static GObjectClass *parent_class = NULL;
73 GType
74 ephy_embed_shell_get_type (void)
76 static GType type = 0;
78 if (G_UNLIKELY (type == 0))
80 const GTypeInfo our_info =
82 sizeof (EphyEmbedShellClass),
83 NULL, /* base_init */
84 NULL, /* base_finalize */
85 (GClassInitFunc) ephy_embed_shell_class_init,
86 NULL, /* class_finalize */
87 NULL, /* class_data */
88 sizeof (EphyEmbedShell),
89 0, /* n_preallocs */
90 (GInstanceInitFunc) ephy_embed_shell_init
93 type = g_type_register_static (G_TYPE_OBJECT,
94 "EphyEmbedShell",
95 &our_info, 0);
98 return type;
101 static void
102 ephy_embed_shell_dispose (GObject *object)
104 EphyEmbedShell *shell = EPHY_EMBED_SHELL (object);
105 EphyEmbedShellPrivate *priv = shell->priv;
107 if (priv->downloader_view != NULL)
109 DownloaderView **downloader_view = &priv->downloader_view;
110 LOG ("Unref downloader");
111 g_object_remove_weak_pointer
112 (G_OBJECT (priv->downloader_view),
113 (gpointer *) downloader_view);
114 g_object_unref (priv->downloader_view);
115 priv->downloader_view = NULL;
118 if (priv->favicon_cache != NULL)
120 LOG ("Unref favicon cache");
121 g_object_unref (priv->favicon_cache);
122 priv->favicon_cache = NULL;
125 if (priv->encodings != NULL)
127 LOG ("Unref encodings");
128 g_object_unref (priv->encodings);
129 priv->encodings = NULL;
132 if (priv->page_setup != NULL)
134 g_object_unref (priv->page_setup);
135 priv->page_setup = NULL;
138 if (priv->print_settings != NULL)
140 g_object_unref (priv->print_settings);
141 priv->print_settings = NULL;
144 G_OBJECT_CLASS (parent_class)->dispose (object);
147 static void
148 ephy_embed_shell_finalize (GObject *object)
150 EphyEmbedShell *shell = EPHY_EMBED_SHELL (object);
152 if (shell->priv->global_history)
154 LOG ("Unref history");
155 g_object_unref (shell->priv->global_history);
158 if (shell->priv->embed_single)
160 LOG ("Unref embed single");
161 g_object_unref (G_OBJECT (shell->priv->embed_single));
164 if (shell->priv->adblock_manager != NULL)
166 LOG ("Unref adblock manager");
167 g_object_unref (shell->priv->adblock_manager);
168 shell->priv->adblock_manager = NULL;
171 G_OBJECT_CLASS (parent_class)->finalize (object);
175 * ephy_embed_shell_get_favicon_cache:
176 * @shell: the #EphyEmbedShell
178 * Returns the favicons cache.
180 * Return value: the favicons cache
182 GObject *
183 ephy_embed_shell_get_favicon_cache (EphyEmbedShell *shell)
185 g_return_val_if_fail (EPHY_IS_EMBED_SHELL (shell), NULL);
187 if (shell->priv->favicon_cache == NULL)
189 shell->priv->favicon_cache = ephy_favicon_cache_new ();
192 return G_OBJECT (shell->priv->favicon_cache);
195 GObject *
196 ephy_embed_shell_get_global_history (EphyEmbedShell *shell)
198 g_return_val_if_fail (EPHY_IS_EMBED_SHELL (shell), NULL);
200 if (shell->priv->global_history == NULL)
202 shell->priv->global_history = ephy_history_new ();
205 return G_OBJECT (shell->priv->global_history);
208 GObject *
209 ephy_embed_shell_get_downloader_view (EphyEmbedShell *shell)
211 g_return_val_if_fail (EPHY_IS_EMBED_SHELL (shell), NULL);
213 if (shell->priv->downloader_view == NULL)
215 DownloaderView **downloader_view;
216 shell->priv->downloader_view = downloader_view_new ();
217 downloader_view = &shell->priv->downloader_view;
218 g_object_add_weak_pointer
219 (G_OBJECT(shell->priv->downloader_view),
220 (gpointer *) downloader_view);
223 return G_OBJECT (shell->priv->downloader_view);
226 GObject *
227 ephy_embed_shell_get_downloader_view_nocreate (EphyEmbedShell *shell)
229 g_return_val_if_fail (EPHY_IS_EMBED_SHELL (shell), NULL);
231 return (GObject *) shell->priv->downloader_view;
234 static GObject *
235 impl_get_embed_single (EphyEmbedShell *shell)
237 EphyEmbedShellPrivate *priv;
239 g_return_val_if_fail (EPHY_IS_EMBED_SHELL (shell), NULL);
241 priv = shell->priv;
243 if (priv->embed_single != NULL &&
244 !priv->single_initialised)
246 g_warning ("ephy_embed_shell_get_embed_single called while the single is being initialised!\n");
247 return G_OBJECT (priv->embed_single);
250 if (priv->embed_single == NULL)
252 priv->embed_single = EPHY_EMBED_SINGLE
253 (ephy_embed_factory_new_object (EPHY_TYPE_EMBED_SINGLE));
255 if (!ephy_embed_single_init (priv->embed_single))
257 GtkWidget *dialog;
259 dialog = gtk_message_dialog_new
260 (NULL,
261 GTK_DIALOG_MODAL,
262 GTK_MESSAGE_ERROR,
263 GTK_BUTTONS_CLOSE,
264 _("Epiphany can't be used now. "
265 "Mozilla initialization failed."));
266 gtk_dialog_run (GTK_DIALOG (dialog));
268 exit (0);
271 priv->single_initialised = TRUE;
274 return G_OBJECT (shell->priv->embed_single);
277 GObject *
278 ephy_embed_shell_get_embed_single (EphyEmbedShell *shell)
280 EphyEmbedShellClass *klass = EPHY_EMBED_SHELL_GET_CLASS (shell);
282 return klass->get_embed_single (shell);
285 GObject *
286 ephy_embed_shell_get_encodings (EphyEmbedShell *shell)
288 g_return_val_if_fail (EPHY_IS_EMBED_SHELL (shell), NULL);
290 if (shell->priv->encodings == NULL)
292 shell->priv->encodings = ephy_encodings_new ();
295 return G_OBJECT (shell->priv->encodings);
298 void
299 ephy_embed_shell_prepare_close (EphyEmbedShell *shell)
301 g_signal_emit (shell, signals[PREPARE_CLOSE], 0);
304 static void
305 ephy_embed_shell_init (EphyEmbedShell *shell)
307 shell->priv = EPHY_EMBED_SHELL_GET_PRIVATE (shell);
309 /* globally accessible singleton */
310 g_assert (embed_shell == NULL);
311 embed_shell = shell;
314 static void
315 ephy_embed_shell_class_init (EphyEmbedShellClass *klass)
317 GObjectClass *object_class = G_OBJECT_CLASS (klass);
319 parent_class = (GObjectClass *) g_type_class_peek_parent (klass);
321 object_class->dispose = ephy_embed_shell_dispose;
322 object_class->finalize = ephy_embed_shell_finalize;
324 klass->get_embed_single = impl_get_embed_single;
327 * EphyEmbed::prepare-close:
328 * @shell:
330 * The ::prepare-close signal is emitted when epiphany is preparing to
331 * quit on command from the session manager. You can use it when you need
332 * to do something special (shut down a service, for example).
334 signals[PREPARE_CLOSE] =
335 g_signal_new ("prepare-close",
336 EPHY_TYPE_EMBED_SHELL,
337 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST,
338 G_STRUCT_OFFSET (EphyEmbedShellClass, prepare_close),
339 NULL, NULL,
340 g_cclosure_marshal_VOID__VOID,
341 G_TYPE_NONE, 0);
343 g_type_class_add_private (object_class, sizeof (EphyEmbedShellPrivate));
347 * ephy_embed_shell_get_default:
349 * Retrieves the default #EphyEmbedShell object
351 * ReturnValue: the default #EphyEmbedShell
353 EphyEmbedShell *
354 ephy_embed_shell_get_default (void)
356 return embed_shell;
360 * ephy_embed_shell_get_adblock_manager:
361 * @shell: the #EphyEmbedShell
363 * Returns the adblock manager.
365 * Return value: the adblock manager
367 GObject *
368 ephy_embed_shell_get_adblock_manager (EphyEmbedShell *shell)
370 g_return_val_if_fail (EPHY_IS_EMBED_SHELL (shell), NULL);
372 if (shell->priv->adblock_manager == NULL)
374 shell->priv->adblock_manager = g_object_new (EPHY_TYPE_ADBLOCK_MANAGER, NULL);
377 return G_OBJECT (shell->priv->adblock_manager);
380 void
381 ephy_embed_shell_set_page_setup (EphyEmbedShell *shell,
382 GtkPageSetup *page_setup)
384 EphyEmbedShellPrivate *priv;
385 char *path;
387 g_return_if_fail (EPHY_IS_EMBED_SHELL (shell));
388 priv = shell->priv;
390 if (page_setup != NULL)
392 g_object_ref (page_setup);
395 if (priv->page_setup != NULL)
397 g_object_unref (priv->page_setup);
400 priv->page_setup = page_setup ? page_setup : gtk_page_setup_new ();
402 path = g_build_filename (ephy_dot_dir (), PAGE_SETUP_FILENAME, NULL);
403 ephy_print_utils_page_setup_to_file (page_setup, path, NULL);
404 g_free (path);
407 GtkPageSetup *
408 ephy_embed_shell_get_page_setup (EphyEmbedShell *shell)
410 EphyEmbedShellPrivate *priv;
412 g_return_val_if_fail (EPHY_IS_EMBED_SHELL (shell), NULL);
413 priv = shell->priv;
415 if (priv->page_setup == NULL)
417 char *path;
419 path = g_build_filename (ephy_dot_dir (), PAGE_SETUP_FILENAME, NULL);
420 priv->page_setup = ephy_print_utils_page_setup_new_from_file (path, NULL);
421 g_free (path);
423 if (priv->page_setup == NULL)
425 priv->page_setup = gtk_page_setup_new ();
429 return priv->page_setup;
432 void
433 ephy_embed_shell_set_print_settings (EphyEmbedShell *shell,
434 GtkPrintSettings *settings)
436 EphyEmbedShellPrivate *priv;
437 char *path;
439 g_return_if_fail (EPHY_IS_EMBED_SHELL (shell));
440 priv = shell->priv;
442 if (settings != NULL)
444 g_object_ref (settings);
447 if (priv->print_settings != NULL)
449 g_object_unref (priv->print_settings);
452 priv->print_settings = settings ? settings : gtk_print_settings_new ();
454 path = g_build_filename (ephy_dot_dir (), PRINT_SETTINGS_FILENAME, NULL);
455 ephy_print_utils_settings_to_file (settings, path, NULL);
456 g_free (path);
459 GtkPrintSettings *
460 ephy_embed_shell_get_print_settings (EphyEmbedShell *shell)
462 EphyEmbedShellPrivate *priv;
464 g_return_val_if_fail (EPHY_IS_EMBED_SHELL (shell), NULL);
465 priv = shell->priv;
467 if (priv->print_settings == NULL)
469 char *path;
471 path = g_build_filename (ephy_dot_dir (), PRINT_SETTINGS_FILENAME, NULL);
472 priv->print_settings = ephy_print_utils_settings_new_from_file (path, NULL);
473 g_free (path);
475 if (priv->print_settings == NULL)
477 priv->print_settings = gtk_print_settings_new ();
481 return priv->print_settings;