diff options
-rw-r--r-- | ChangeLog | 15 | ||||
-rw-r--r-- | embed/ephy-embed-shell.c | 42 | ||||
-rw-r--r-- | lib/ephy-print-utils.c | 441 | ||||
-rw-r--r-- | lib/ephy-print-utils.h | 60 | ||||
-rw-r--r-- | lib/ephy-string.c | 104 | ||||
-rw-r--r-- | lib/ephy-string.h | 13 |
6 files changed, 667 insertions, 8 deletions
@@ -1,3 +1,18 @@ +2006-08-05 Christian Persch <chpe@cvs.gnome.org> + + * embed/ephy-embed-shell.c: (ephy_embed_shell_set_page_setup), + (ephy_embed_shell_get_page_setup), + (ephy_embed_shell_set_print_settings), + (ephy_embed_shell_get_print_settings): + A lib/ephy-print-utils.c: + A lib/ephy-print-utils.h: + * lib/ephy-string.c: (ephy_string_flags_from_string), + (ephy_string_flags_to_string), (ephy_string_enum_from_string), + (ephy_string_enum_to_string): + * lib/ephy-string.h: + + Persist print settings and page setup. + 2006-07-26 Luca Ferretti <elle.uca@libero.it> * lib/ephy-stock-icons.c: diff --git a/embed/ephy-embed-shell.c b/embed/ephy-embed-shell.c index d4a1e4c38..9a40d7c6b 100644 --- a/embed/ephy-embed-shell.c +++ b/embed/ephy-embed-shell.c @@ -32,10 +32,14 @@ #include "ephy-encodings.h" #include "ephy-debug.h" #include "ephy-adblock-manager.h" +#include "ephy-print-utils.h" #include <glib/gi18n.h> #include <gtk/gtkmessagedialog.h> +#define PAGE_SETUP_FILENAME "page-setup.ini" +#define PRINT_SETTINGS_FILENAME "print-settings.ini" + #define EPHY_EMBED_SHELL_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_EMBED_SHELL, EphyEmbedShellPrivate)) struct _EphyEmbedShellPrivate @@ -378,6 +382,7 @@ ephy_embed_shell_set_page_setup (EphyEmbedShell *shell, GtkPageSetup *page_setup) { EphyEmbedShellPrivate *priv; + char *path; g_return_if_fail (EPHY_IS_EMBED_SHELL (shell)); priv = shell->priv; @@ -392,9 +397,11 @@ ephy_embed_shell_set_page_setup (EphyEmbedShell *shell, g_object_unref (priv->page_setup); } - /* FIXME: save settings to disk! */ + priv->page_setup = page_setup ? page_setup : gtk_page_setup_new (); - priv->page_setup = page_setup; + path = g_build_filename (ephy_dot_dir (), PAGE_SETUP_FILENAME, NULL); + ephy_print_utils_page_setup_to_file (page_setup, path, NULL); + g_free (path); } GtkPageSetup * @@ -407,8 +414,16 @@ ephy_embed_shell_get_page_setup (EphyEmbedShell *shell) if (priv->page_setup == NULL) { - /* FIXME: load stored settings! */ - priv->page_setup = gtk_page_setup_new (); + char *path; + + path = g_build_filename (ephy_dot_dir (), PAGE_SETUP_FILENAME, NULL); + priv->page_setup = ephy_print_utils_page_setup_new_from_file (path, &error); + g_free (path); + + if (priv->page_setup == NULL) + { + priv->page_setup = gtk_page_setup_new (); + } } return priv->page_setup; @@ -419,6 +434,7 @@ ephy_embed_shell_set_print_settings (EphyEmbedShell *shell, GtkPrintSettings *settings) { EphyEmbedShellPrivate *priv; + char *path; g_return_if_fail (EPHY_IS_EMBED_SHELL (shell)); priv = shell->priv; @@ -433,9 +449,11 @@ ephy_embed_shell_set_print_settings (EphyEmbedShell *shell, g_object_unref (priv->print_settings); } - /* FIXME: save settings to disk! */ + priv->print_settings = settings ? settings : gtk_print_settings_new (); - priv->print_settings = settings; + path = g_build_filename (ephy_dot_dir (), PRINT_SETTINGS_FILENAME, NULL); + ephy_print_utils_settings_to_file (settings, path, NULL); + g_free (path); } GtkPrintSettings * @@ -448,8 +466,16 @@ ephy_embed_shell_get_print_settings (EphyEmbedShell *shell) if (priv->print_settings == NULL) { - /* FIXME: load stored settings! */ - priv->print_settings = gtk_print_settings_new (); + char *path; + + path = g_build_filename (ephy_dot_dir (), PRINT_SETTINGS_FILENAME, NULL); + priv->print_settings = ephy_print_utils_settings_new_from_file (path, &error); + g_free (path); + + if (priv->print_settings == NULL) + { + priv->print_settings = gtk_print_settings_new (); + } } return priv->print_settings; diff --git a/lib/ephy-print-utils.c b/lib/ephy-print-utils.c new file mode 100644 index 000000000..2d306206a --- /dev/null +++ b/lib/ephy-print-utils.c @@ -0,0 +1,441 @@ +/* + * Copyright (C) 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$ + */ + +#include "config.h" + +#include <string.h> + +#include <glib.h> +#include <gtk/gtktypebuiltins.h> + +#include "ephy-debug.h" +#include "ephy-string.h" + +#include "ephy-print-utils.h" + +#define PRINT_SETTINGS_GROUP "Print Settings" +#define PAGE_SETUP_GROUP "Page Setup" +#define PAPER_SIZE_GROUP "Paper Size" + +#define ERROR_QUARK (g_quark_from_static_string ("ephy-print-utils-error")) + +/** + * ephy_print_utils_settings_new_from_file: + * @file_name: the filename to read the settings from + * @error: + * + * Reads the print settings from @filename. Returns a new #GtkPrintSettings + * object with the restored settings, or %NULL if an error occurred. + * + * Return value: the restored #GtkPrintSettings + * + * Since: 2.10 + */ +GtkPrintSettings * +ephy_print_utils_settings_new_from_file (const gchar *file_name, + GError **error) +{ + GtkPrintSettings *settings; + GKeyFile *key_file; + + g_return_val_if_fail (file_name != NULL, NULL); + + key_file = g_key_file_new (); + if (!g_key_file_load_from_file (key_file, file_name, 0, error)) + { + g_key_file_free (key_file); + return NULL; + } + + settings = ephy_print_utils_settings_new_from_key_file (key_file, error); + g_key_file_free (key_file); + + return settings; +} + +/** + * ephy_print_utils_settings_new_from_key_file: + * @key_file: the #GKeyFile to retrieve the settings from + * @error: + * + * Reads the print settings from @key_file. Returns a new #GtkPrintSettings + * object with the restored settings, or %NULL if an error occurred. + * + * Return value: the restored #GtkPrintSettings + * + * Since: 2.10 + */ +GtkPrintSettings * +ephy_print_utils_settings_new_from_key_file (GKeyFile *key_file, + GError **error) +{ + GtkPrintSettings *settings; + gchar **keys; + gsize n_keys, i; + + g_return_val_if_fail (key_file != NULL, NULL); + + keys = g_key_file_get_keys (key_file, + PRINT_SETTINGS_GROUP, + &n_keys, + error); + if (error) + return NULL; + + settings = gtk_print_settings_new (); + + for (i = 0 ; i < n_keys; ++i) + { + gchar *value; + + value = g_key_file_get_string (key_file, + PRINT_SETTINGS_GROUP, + keys[i], + NULL); + if (!value) + continue; + + gtk_print_settings_set (settings, keys[i], value); + g_free (value); + } + + g_strfreev (keys); + + return NULL; +} + +/** + * ephy_print_utils_settings_to_file: + * @settings: a #GtkPrintSettings + * @file_name: the file to save to + * @error: + * + * This function saves the print settings from @settings to @file_name. + * + * Return value: %TRUE on success + * + * Since: 2.10 + */ +gboolean +ephy_print_utils_settings_to_file (GtkPrintSettings *settings, + const char *file_name, + GError **error) +{ + GKeyFile *keyfile; + gboolean retval; + char *data = NULL; + gsize len; + + g_return_val_if_fail (GTK_IS_PRINT_SETTINGS (settings), FALSE); + g_return_val_if_fail (file_name != NULL, FALSE); + + keyfile = g_key_file_new (); + retval = ephy_print_utils_settings_to_key_file (settings, keyfile, error); + if (!retval) goto out; + + data = g_key_file_to_data (keyfile, &len, error); + if (!data) goto out; + + retval = g_file_set_contents (file_name, data, len, error); + +out: + g_key_file_free (keyfile); + g_free (data); + + return retval; +} + +static void +add_value_to_keyfile (const gchar *key, + const gchar *value, + GKeyFile *keyfile) +{ + g_key_file_set_string (keyfile, PRINT_SETTINGS_GROUP, key, value); +} + +/** + * ephy_print_utils_settings_to_key_file: + * @settings: a #GtkPrintSettings + * @key_file: the #GKeyFile to save the print settings to + * @error: + * + * This function adds the print settings from @settings to @key_file. + * + * Return value: %TRUE on success + * + * Since: 2.10 + */ +gboolean +ephy_print_utils_settings_to_key_file (GtkPrintSettings *settings, + GKeyFile *key_file, + GError **error) +{ + g_return_val_if_fail (GTK_IS_PRINT_SETTINGS (settings), FALSE); + g_return_val_if_fail (key_file != NULL, FALSE); + + gtk_print_settings_foreach (settings, + (GtkPrintSettingsFunc) add_value_to_keyfile, + key_file); + + return TRUE; +} + + + + + + + + +/** + * ephy_print_utils_page_setup_new_from_file: + * @file_name: the filename to read the page_setup from + * @error: + * + * Reads the print page_setup from @filename. Returns a new #GtkPageSetup + * object with the restored page_setup, or %NULL if an error occurred. + * + * Return value: the restored #GtkPageSetup + * + * Since: 2.10 + */ +GtkPageSetup * +ephy_print_utils_page_setup_new_from_file (const gchar *file_name, + GError **error) +{ + GtkPageSetup *page_setup; + GKeyFile *key_file; + + g_return_val_if_fail (file_name != NULL, NULL); + + key_file = g_key_file_new (); + if (!g_key_file_load_from_file (key_file, file_name, 0, error)) + { + g_key_file_free (key_file); + return NULL; + } + + page_setup = ephy_print_utils_page_setup_new_from_key_file (key_file, error); + g_key_file_free (key_file); + + return page_setup; +} + +/** + * ephy_print_utils_page_setup_new_from_key_file: + * @key_file: the #GKeyFile to retrieve the page_setup from + * @error: + * + * Reads the print page_setup from @key_file. Returns a new #GtkPageSetup + * object with the restored page_setup, or %NULL if an error occurred. + * + * Return value: the restored #GtkPageSetup + * + * Since: 2.10 + */ +GtkPageSetup * +ephy_print_utils_page_setup_new_from_key_file (GKeyFile *key_file, + GError **error) +{ + GtkPageSetup *page_setup = NULL; + GtkPaperSize *paper_size; + gdouble width, height, top, bottom, left, right; + char *name, *ppd_name, *display_name, *orientation; + gboolean retval = TRUE; + + g_return_val_if_fail (key_file != NULL, NULL); + + if (!g_key_file_has_group (key_file, PAGE_SETUP_GROUP) || + !g_key_file_has_group (key_file, PAPER_SIZE_GROUP)) { + g_set_error (error, ERROR_QUARK, 0, "Not a valid epiphany page setup file"); + retval = FALSE; + goto out; + } + +#define GET_DOUBLE(kf, group, name, v) \ +v = g_key_file_get_double (kf, group, name, error); \ +if (*error != NULL) {\ + retval = FALSE;\ + goto out;\ +} + + GET_DOUBLE (key_file, PAPER_SIZE_GROUP, "Width", width); + GET_DOUBLE (key_file, PAPER_SIZE_GROUP, "Height", height); + GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginTop", top); + GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginBottom", bottom); + GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginLeft", left); + GET_DOUBLE (key_file, PAGE_SETUP_GROUP, "MarginRight", right); + +#undef GET_DOUBLE + + name = g_key_file_get_string (key_file, PAPER_SIZE_GROUP, + "Name", NULL); + ppd_name = g_key_file_get_string (key_file, PAPER_SIZE_GROUP, + "PPDName", NULL); + display_name = g_key_file_get_string (key_file, PAPER_SIZE_GROUP, + "DisplayName", NULL); + + if (ppd_name != NULL) { + paper_size = gtk_paper_size_new_from_ppd (ppd_name, display_name, + width, height); + } else if (name != NULL) { + paper_size = gtk_paper_size_new_custom (name, display_name, + width, height, GTK_UNIT_MM); + } else { + g_set_error (error, ERROR_QUARK, 0, "Not a valid epiphany page setup file"); + retval = FALSE; + goto out; + } + + g_assert (paper_size != NULL); + + page_setup = gtk_page_setup_new (); + gtk_page_setup_set_paper_size (page_setup, paper_size); + gtk_paper_size_free (paper_size); + + gtk_page_setup_set_top_margin (page_setup, top, GTK_UNIT_MM); + gtk_page_setup_set_bottom_margin (page_setup, bottom, GTK_UNIT_MM); + gtk_page_setup_set_left_margin (page_setup, left, GTK_UNIT_MM); + gtk_page_setup_set_right_margin (page_setup, right, GTK_UNIT_MM); + + orientation = g_key_file_get_string (key_file, PAGE_SETUP_GROUP, + "Orientation", NULL); + if (orientation != NULL) { + gtk_page_setup_set_orientation (page_setup, + ephy_string_enum_from_string (GTK_TYPE_PAGE_ORIENTATION, + orientation)); + g_free (orientation); + } + +out: + g_free (name); + g_free (display_name); + g_free (ppd_name); + + return page_setup; +} + +/** + * ephy_print_utils_page_setup_to_file: + * @page_setup: a #GtkPageSetup + * @file_name: the file to save to + * @error: + * + * This function saves the print page_setup from @page_setup to @file_name. + * + * Return value: %TRUE on success + * + * Since: 2.10 + */ +gboolean +ephy_print_utils_page_setup_to_file (GtkPageSetup *page_setup, + const char *file_name, + GError **error) +{ + GKeyFile *keyfile; + gboolean retval; + char *data = NULL; + gsize len; + + g_return_val_if_fail (GTK_IS_PAGE_SETUP (page_setup), FALSE); + g_return_val_if_fail (file_name != NULL, FALSE); + + keyfile = g_key_file_new (); + retval = ephy_print_utils_page_setup_to_key_file (page_setup, keyfile, error); + if (!retval) goto out; + + data = g_key_file_to_data (keyfile, &len, error); + if (!data) goto out; + + retval = g_file_set_contents (file_name, data, len, error); + +out: + g_key_file_free (keyfile); + g_free (data); + + return retval; +} + +/** + * ephy_print_utils_page_setup_to_key_file: + * @page_setup: a #GtkPageSetup + * @key_file: the #GKeyFile to save the print page_setup to + * @error: + * + * This function adds the print page_setup from @page_setup to @key_file. + * + * Return value: %TRUE on success + * + * Since: 2.10 + */ +gboolean +ephy_print_utils_page_setup_to_key_file (GtkPageSetup *page_setup, + GKeyFile *key_file, + GError **error) +{ + GtkPaperSize *paper_size; + const char *name, *ppd_name, *display_name; + char *orientation; + + g_return_val_if_fail (GTK_IS_PAGE_SETUP (page_setup), FALSE); + g_return_val_if_fail (key_file != NULL, FALSE); + + paper_size = gtk_page_setup_get_paper_size (page_setup); + g_assert (paper_size != NULL); + + name = gtk_paper_size_get_name (paper_size); + display_name = gtk_paper_size_get_display_name (paper_size); + ppd_name = gtk_paper_size_get_ppd_name (paper_size); + + if (ppd_name != NULL) { + g_key_file_set_string (key_file, PAPER_SIZE_GROUP, + "PPDName", ppd_name); + } else { + g_key_file_set_string (key_file, PAPER_SIZE_GROUP, + "Name", name); + } + + if (display_name) { + g_key_file_set_string (key_file, PAPER_SIZE_GROUP, + "DisplayName", display_name); + } + + g_key_file_set_double (key_file, PAPER_SIZE_GROUP, + "Width", gtk_paper_size_get_width (paper_size, GTK_UNIT_MM)); + g_key_file_set_double (key_file, PAPER_SIZE_GROUP, + "Height", gtk_paper_size_get_height (paper_size, GTK_UNIT_MM)); + + g_key_file_set_double (key_file, PAGE_SETUP_GROUP, + "MarginTop", gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM)); + g_key_file_set_double (key_file, PAGE_SETUP_GROUP, + "MarginBottom", gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM)); + g_key_file_set_double (key_file, PAGE_SETUP_GROUP, + "MarginLeft", gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM)); + g_key_file_set_double (key_file, PAGE_SETUP_GROUP, + "MarginRight", gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM)); + + orientation = ephy_string_enum_to_string (GTK_TYPE_PAGE_ORIENTATION, + gtk_page_setup_get_orientation (page_setup)); + g_key_file_set_string (key_file, PAGE_SETUP_GROUP, + "Orientation", orientation); + g_free (orientation); + + return TRUE; +} diff --git a/lib/ephy-print-utils.h b/lib/ephy-print-utils.h new file mode 100644 index 000000000..6804df7dc --- /dev/null +++ b/lib/ephy-print-utils.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2006 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$ + */ + +#ifndef EPHY_PRINT_UTILS_H +#define EPHY_PRINT_UTILS_H + +#include <glib.h> +#include <gtk/gtkprintsettings.h> +#include <gtk/gtkpagesetup.h> + +G_BEGIN_DECLS + +GtkPrintSettings *ephy_print_utils_settings_new_from_file (const gchar *file_name, + GError **error); + +GtkPrintSettings *ephy_print_utils_settings_new_from_key_file (GKeyFile *key_file, + GError **error); + +gboolean ephy_print_utils_settings_to_file (GtkPrintSettings *settings, + const char *file_name, + GError **error); + +gboolean ephy_print_utils_settings_to_key_file (GtkPrintSettings *settings, + GKeyFile *key_file, + GError **error); + +GtkPageSetup *ephy_print_utils_page_setup_new_from_file (const gchar *file_name, + GError **error); + +GtkPageSetup *ephy_print_utils_page_setup_new_from_key_file (GKeyFile *key_file, + GError **error); + +gboolean ephy_print_utils_page_setup_to_file (GtkPageSetup *setup, + const char *file_name, + GError **error); + +gboolean ephy_print_utils_page_setup_to_key_file (GtkPageSetup *setup, + GKeyFile *key_file, + GError **error); + +G_END_DECLS + +#endif diff --git a/lib/ephy-string.c b/lib/ephy-string.c index 3eb991573..baec076b1 100644 --- a/lib/ephy-string.c +++ b/lib/ephy-string.c @@ -182,3 +182,107 @@ ephy_string_collate_key_for_domain (const char *str, return g_string_free (result, FALSE); } + +guint +ephy_string_flags_from_string (GType type, + const char *flags_string) +{ + GFlagsClass *flags_class; + const GFlagsValue *value; + gchar **flags; + guint retval = 0, i; + + g_return_val_if_fail (flags_string != NULL, 0); + + flags = g_strsplit (flags_string, "|", -1); + if (!flags) return 0; + + flags_class = g_type_class_ref (type); + + for (i = 0; flags[i] != NULL; ++i) + { + value = g_flags_get_value_by_nick (flags_class, flags[i]); + if (value != NULL) + { + retval |= value->value; + } + } + + g_type_class_unref (flags_class); + + return retval; +} + +char * +ephy_string_flags_to_string (GType type, + guint flags_value) +{ + GFlagsClass *flags_class; + GString *string; + gboolean first = TRUE; + guint i; + + string = g_string_sized_new (128); + + flags_class = g_type_class_ref (type); + + for (i = 0; i < flags_class->n_values; ++i) + { + if (flags_value & flags_class->values[i].value) + { + if (!first) + { + g_string_append_c (string, '|'); + } + first = FALSE; + g_string_append (string, flags_class->values[i].value_nick); + } + } + + g_type_class_unref (flags_class); + + return g_string_free (string, FALSE); +} + +guint +ephy_string_enum_from_string (GType type, + const char *enum_string) +{ + GEnumClass *enum_class; + const GEnumValue *value; + guint retval = 0; + + g_return_val_if_fail (enum_string != NULL, 0); + + enum_class = g_type_class_ref (type); + value = g_enum_get_value_by_nick (enum_class, enum_string); + if (value != NULL) + { + retval = value->value; + } + + g_type_class_unref (enum_class); + + return retval; +} + +char * +ephy_string_enum_to_string (GType type, + guint enum_value) +{ + GEnumClass *enum_class; + GEnumValue *value; + char *retval = NULL; + + enum_class = g_type_class_ref (type); + + value = g_enum_get_value (enum_class, enum_value); + if (value) + { + retval = g_strdup (value->value_nick); + } + + g_type_class_unref (enum_class); + + return retval; +} diff --git a/lib/ephy-string.h b/lib/ephy-string.h index 6bb50ff3e..2ed7d708a 100644 --- a/lib/ephy-string.h +++ b/lib/ephy-string.h @@ -22,6 +22,7 @@ #define EPHY_STRING_H #include <glib.h> +#include <glib-object.h> G_BEGIN_DECLS @@ -36,6 +37,18 @@ char *ephy_string_shorten (char *str, char *ephy_string_collate_key_for_domain (const char *host, gssize len); +guint ephy_string_flags_from_string (GType type, + const char *flags_string); + +char *ephy_string_flags_to_string (GType type, + guint flags_value); + +guint ephy_string_enum_from_string (GType type, + const char *enum_string); + +char *ephy_string_enum_to_string (GType type, + guint enum_value); + G_END_DECLS #endif |