Add a new error code

2007-04-29  Matthias Clasen  <mclasen@redhat.com>

        * gtk/gtkprintoperation.h: Add a new error code

        * gtk/gtk.symbols:
        * gtk/gtkpagesetup.[hc]:
        * gtk/gtkpapersize.[hc]:
        * gtk/gtkprintsettings.[hc]: Add functions to serialize
        and deserialize page setups and print settings to files
        and key files.  (#344515, Christian Persch)

        * gtk/gtkpagesetupunixdialog.c: Adapt to the new functions.

        * tests/print-editor.c: Use the new functions to persist
        page setup and print settings.


svn path=/trunk/; revision=17697
This commit is contained in:
Matthias Clasen 2007-04-29 06:23:58 +00:00 committed by Matthias Clasen
parent ea3667c49c
commit 3c5bd522b3
13 changed files with 691 additions and 55 deletions

View File

@ -1,3 +1,19 @@
2007-04-29 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkprintoperation.h: Add a new error code
* gtk/gtk.symbols:
* gtk/gtkpagesetup.[hc]:
* gtk/gtkpapersize.[hc]:
* gtk/gtkprintsettings.[hc]: Add functions to serialize
and deserialize page setups and print settings to files
and key files. (#344515, Christian Persch)
* gtk/gtkpagesetupunixdialog.c: Adapt to the new functions.
* tests/print-editor.c: Use the new functions to persist
page setup and print settings.
2007-04-29 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkpagesetupunixdialog.c (gtk_page_setup_unix_dialog_finalize):

View File

@ -1,3 +1,8 @@
2007-04-29 Matthias Clasen <mclasen@redhat.com>
* gtk/gtk-sections.txt: Add new functions to serialize/
deserialize page setups and print settings.
2007-04-29 Matthias Clasen <mclasen@redhat.com>
* gtk/gtk-sections.txt: Add gtk_paper_size_get_builtins

View File

@ -6332,6 +6332,11 @@ GTK_PRINT_SETTINGS_OUTPUT_URI
GTK_PRINT_SETTINGS_WIN32_DRIVER_EXTRA
GTK_PRINT_SETTINGS_WIN32_DRIVER_VERSION
<SUBSECTION Serialization>
gtk_print_settings_new_from_file
gtk_print_settings_new_from_key_file
gtk_print_settings_to_file
gtk_print_settings_to_key_file
<SUBSECTION Standard>
GTK_TYPE_PRINT_SETTINGS
@ -6375,6 +6380,10 @@ gtk_paper_size_get_default_left_margin
gtk_paper_size_get_default_right_margin
gtk_paper_size_get_default
<SUBSECTION Serialization>
gtk_paper_size_new_from_key_file
gtk_paper_size_to_key_file
<SUBSECTION Standard>
GTK_TYPE_PAPER_SIZE
<SUBSECTION Private>
@ -6406,6 +6415,12 @@ gtk_page_setup_get_paper_height
gtk_page_setup_get_page_width
gtk_page_setup_get_page_height
<SUBSECTION Serialization>
gtk_page_setup_new_from_file
gtk_page_setup_new_from_key_file
gtk_page_setup_to_file
gtk_page_setup_to_key_file
<SUBSECTION Standard>
GTK_TYPE_PAGE_SETUP
GTK_PAGE_SETUP

View File

@ -2512,6 +2512,10 @@ gtk_preview_uninit
#if IN_FILE(__GTK_PAGE_SETUP_C__)
gtk_page_setup_get_type G_GNUC_CONST
gtk_page_setup_new
gtk_page_setup_new_from_file
gtk_page_setup_new_from_key_file
gtk_page_setup_to_file
gtk_page_setup_to_key_file
gtk_page_setup_copy
gtk_page_setup_get_orientation
gtk_page_setup_set_orientation
@ -2552,6 +2556,8 @@ gtk_paper_size_get_type G_GNUC_CONST
gtk_paper_size_new
gtk_paper_size_new_from_ppd
gtk_paper_size_new_custom
gtk_paper_size_new_from_key_file
gtk_paper_size_to_key_file
gtk_paper_size_copy
gtk_paper_size_free
gtk_paper_size_is_equal
@ -2772,6 +2778,10 @@ gtk_print_run_page_setup_dialog_async
#if IN_FILE(__GTK_PRINT_SETTINGS_C__)
gtk_print_settings_get_type G_GNUC_CONST
gtk_print_settings_new
gtk_print_settings_new_from_file
gtk_print_settings_new_from_key_file
gtk_print_settings_to_file
gtk_print_settings_to_key_file
gtk_print_settings_copy
gtk_print_settings_has_key
gtk_print_settings_get

View File

@ -22,8 +22,12 @@
#include "gtkpagesetup.h"
#include "gtkprintutils.h"
#include "gtkprintoperation.h" /* for GtkPrintError */
#include "gtkintl.h"
#include "gtktypebuiltins.h"
#include "gtkalias.h"
#define KEYFILE_GROUP_NAME "Page Setup"
typedef struct _GtkPageSetupClass GtkPageSetupClass;
@ -468,5 +472,261 @@ gtk_page_setup_get_page_height (GtkPageSetup *setup,
}
/**
* gtk_page_setup_new_from_file:
* @file_name: the filename to read the page setup from
* @error: return location for an error, or %NULL
*
* Reads the page setup from the file @file_name. Returns a
* new #GtkPageSetup object with the restored page setup,
* or %NULL if an error occurred. See gtk_page_setup_to_file().
*
* Return value: the restored #GtkPageSetup
*
* Since: 2.12
*/
GtkPageSetup *
gtk_page_setup_new_from_file (const gchar *file_name,
GError **error)
{
GtkPageSetup *page_setup;
GKeyFile *key_file;
GError *err = NULL;
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, &err))
{
g_key_file_free (key_file);
g_propagate_error (error, err);
return NULL;
}
page_setup = gtk_page_setup_new_from_key_file (key_file, NULL, error);
g_key_file_free (key_file);
return page_setup;
}
/* something like this should really be in gobject! */
static guint
string_to_enum (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)
retval = value->value;
g_type_class_unref (enum_class);
return retval;
}
/**
* gtk_page_setup_new_from_key_file:
* @key_file: the #GKeyFile to retrieve the page_setup from
* @group_name: the name of the group in the key_file to read, or %NULL
* to use the default name "Page Setup"
* @error: return location for an error, or %NULL
*
* Reads the page setup from the group @group_name in the key file
* @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.12
*/
GtkPageSetup *
gtk_page_setup_new_from_key_file (GKeyFile *key_file,
const gchar *group_name,
GError **error)
{
GtkPageSetup *page_setup = NULL;
GtkPaperSize *paper_size;
gdouble top, bottom, left, right;
char *orientation = NULL, *freeme = NULL;
gboolean retval = TRUE;
GError *err = NULL;
g_return_val_if_fail (key_file != NULL, NULL);
if (!group_name)
group_name = KEYFILE_GROUP_NAME;
if (!g_key_file_has_group (key_file, group_name))
{
g_set_error (error,
GTK_PRINT_ERROR,
GTK_PRINT_ERROR_INVALID_FILE,
_("Not a valid page setup file"));
retval = FALSE;
goto out;
}
#define GET_DOUBLE(kf, group, name, v) \
v = g_key_file_get_double (kf, group, name, &err); \
if (err != NULL) \
{ \
g_propagate_error (error, err);\
retval = FALSE;\
goto out;\
}
GET_DOUBLE (key_file, group_name, "MarginTop", top);
GET_DOUBLE (key_file, group_name, "MarginBottom", bottom);
GET_DOUBLE (key_file, group_name, "MarginLeft", left);
GET_DOUBLE (key_file, group_name, "MarginRight", right);
#undef GET_DOUBLE
paper_size = gtk_paper_size_new_from_key_file (key_file, group_name, &err);
if (!paper_size)
{
g_propagate_error (error, err);
goto out;
}
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, group_name,
"Orientation", NULL);
if (orientation)
{
gtk_page_setup_set_orientation (page_setup,
string_to_enum (GTK_TYPE_PAGE_ORIENTATION,
orientation));
g_free (orientation);
}
out:
g_free (freeme);
return page_setup;
}
/**
* gtk_page_setup_to_file:
* @page_setup: a #GtkPageSetup
* @file_name: the file to save to
* @error: return location for errors, or %NULL
*
* This function saves the print page_setup from @page_setup
* to @file_name.
*
* Return value: %TRUE on success
*
* Since: 2.12
*/
gboolean
gtk_page_setup_to_file (GtkPageSetup *page_setup,
const char *file_name,
GError **error)
{
GKeyFile *key_file;
gboolean retval = FALSE;
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);
key_file = g_key_file_new ();
gtk_page_setup_to_key_file (page_setup, key_file, NULL);
data = g_key_file_to_data (key_file, &len, error);
if (!data)
goto out;
retval = g_file_set_contents (file_name, data, len, error);
out:
g_key_file_free (key_file);
g_free (data);
return retval;
}
/* something like this should really be in gobject! */
static char *
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;
}
/**
* gtk_page_setup_to_key_file:
* @page_setup: a #GtkPageSetup
* @key_file: the #GKeyFile to save the page setup to
* @group_name: the group to add the settings to in @key_file,
* or %NULL to use the default name "Page Setup"
*
* This function adds the page setup from @page_setup to @key_file.
*
* Since: 2.12
*/
void
gtk_page_setup_to_key_file (GtkPageSetup *page_setup,
GKeyFile *key_file,
const gchar *group_name)
{
GtkPaperSize *paper_size;
char *orientation;
g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup));
g_return_if_fail (key_file != NULL);
if (!group_name)
group_name = KEYFILE_GROUP_NAME;
paper_size = gtk_page_setup_get_paper_size (page_setup);
g_assert (paper_size != NULL);
gtk_paper_size_to_key_file (paper_size, key_file, group_name);
g_key_file_set_double (key_file, group_name,
"MarginTop", gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM));
g_key_file_set_double (key_file, group_name,
"MarginBottom", gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM));
g_key_file_set_double (key_file, group_name,
"MarginLeft", gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM));
g_key_file_set_double (key_file, group_name,
"MarginRight", gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM));
orientation = enum_to_string (GTK_TYPE_PAGE_ORIENTATION,
gtk_page_setup_get_orientation (page_setup));
g_key_file_set_string (key_file, group_name,
"Orientation", orientation);
g_free (orientation);
}
#define __GTK_PAGE_SETUP_C__
#include "gtkaliasdef.c"

View File

@ -79,6 +79,18 @@ gdouble gtk_page_setup_get_page_width (GtkPageSetup *setup,
gdouble gtk_page_setup_get_page_height (GtkPageSetup *setup,
GtkUnit unit);
/* Saving and restoring page setup */
GtkPageSetup *gtk_page_setup_new_from_file (const gchar *file_name,
GError **error);
gboolean gtk_page_setup_to_file (GtkPageSetup *setup,
const char *file_name,
GError **error);
GtkPageSetup *gtk_page_setup_new_from_key_file (GKeyFile *key_file,
const gchar *group_name,
GError **error);
void gtk_page_setup_to_key_file (GtkPageSetup *setup,
GKeyFile *key_file,
const gchar *group_name);
G_END_DECLS

View File

@ -204,51 +204,18 @@ load_custom_papers (GtkListStore *store)
groups = g_key_file_get_groups (keyfile, &n_groups);
for (i = 0; i < n_groups; ++i)
{
GError *error = NULL;
gdouble w, h, top, bottom, left, right;
GtkPaperSize *paper_size;
GtkPageSetup *page_setup;
gchar *name;
GtkTreeIter iter;
name = g_key_file_get_value (keyfile, groups[i], "Name", NULL);
if (!name)
page_setup = gtk_page_setup_new_from_key_file (keyfile, groups[i], NULL);
if (!page_setup)
continue;
#define GET_DOUBLE(kf, name, v) \
v = g_key_file_get_double (kf, groups[i], name, &error); \
if (error != NULL) \
{\
g_error_free (error);\
continue;\
}
GET_DOUBLE (keyfile, "Width", w);
GET_DOUBLE (keyfile, "Height", h);
GET_DOUBLE (keyfile, "MarginTop", top);
GET_DOUBLE (keyfile, "MarginBottom", bottom);
GET_DOUBLE (keyfile, "MarginLeft", left);
GET_DOUBLE (keyfile, "MarginRight", right);
#undef GET_DOUBLE
page_setup = gtk_page_setup_new ();
paper_size = gtk_paper_size_new_custom (name, name, w, h, GTK_UNIT_MM);
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);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
0, page_setup,
-1);
g_object_unref (page_setup);
g_free (name);
}
g_strfreev (groups);
@ -271,7 +238,6 @@ save_custom_papers (GtkListStore *store)
{
do
{
GtkPaperSize *paper_size;
GtkPageSetup *page_setup;
gchar group[32];
@ -279,21 +245,7 @@ save_custom_papers (GtkListStore *store)
gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
paper_size = gtk_page_setup_get_paper_size (page_setup);
g_key_file_set_string (keyfile, group, "Name", gtk_paper_size_get_name (paper_size));
g_key_file_set_double (keyfile, group, "Width",
gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM));
g_key_file_set_double (keyfile, group, "Height",
gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM));
g_key_file_set_double (keyfile, group, "MarginTop",
gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM));
g_key_file_set_double (keyfile, group, "MarginBottom",
gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM));
g_key_file_set_double (keyfile, group, "MarginLeft",
gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM));
g_key_file_set_double (keyfile, group, "MarginRight",
gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM));
gtk_page_setup_to_key_file (page_setup, keyfile, group);
++i;
} while (gtk_tree_model_iter_next (model, &iter));

View File

@ -28,6 +28,7 @@
#include "gtkpapersize.h"
#include "gtkprintutils.h"
#include "gtkprintoperation.h" /* for GtkPrintError */
#include "gtkintl.h"
#include "gtkalias.h"
@ -759,6 +760,135 @@ gtk_paper_size_get_default_right_margin (GtkPaperSize *size,
return _gtk_print_convert_from_mm (margin, unit);
}
/**
* gtk_paper_size_new_from_key_file:
* @key_file: the #GKeyFile to retrieve the papersize from
* @group_name: the name ofthe group in the key file to read,
* or %NULL to read the first group
* @error: return location for an error, or %NULL
*
* Reads a paper size from the group @group_name in the key file
* @key_file. Returns a new #GtkPaperSize object with the restored
* paper size, or %NULL if an error occurred.
*
* Since: 2.12
*/
GtkPaperSize *
gtk_paper_size_new_from_key_file (GKeyFile *key_file,
const gchar *group_name,
GError **error)
{
GtkPaperSize *paper_size = NULL;
char *name = NULL, *ppd_name = NULL, *display_name = NULL, *freeme = NULL;
gdouble width, height;
gboolean retval = TRUE;
GError *err = NULL;
g_return_val_if_fail (key_file != NULL, NULL);
if (!group_name)
group_name = freeme = g_key_file_get_start_group (key_file);
if (!group_name || !g_key_file_has_group (key_file, group_name))
{
g_set_error (error,
GTK_PRINT_ERROR,
GTK_PRINT_ERROR_INVALID_FILE,
_("Not a valid page setup file"));
retval = FALSE;
goto out;
}
#define GET_DOUBLE(kf, group, name, v) \
v = g_key_file_get_double (kf, group, name, &err); \
if (err != NULL) \
{\
g_propagate_error (error, err);\
retval = FALSE;\
goto out;\
}
GET_DOUBLE (key_file, group_name, "Width", width);
GET_DOUBLE (key_file, group_name, "Height", height);
#undef GET_DOUBLE
name = g_key_file_get_string (key_file, group_name,
"Name", NULL);
ppd_name = g_key_file_get_string (key_file, group_name,
"PPDName", NULL);
display_name = g_key_file_get_string (key_file, group_name,
"DisplayName", NULL);
/* Fallback for old ~/.gtk-custom-paper entries */
if (!display_name)
display_name = g_strdup (name);
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,
GTK_PRINT_ERROR,
GTK_PRINT_ERROR_INVALID_FILE,
_("Not a valid page setup file"));
retval = FALSE;
goto out;
}
g_assert (paper_size != NULL);
out:
g_free (ppd_name);
g_free (name);
g_free (display_name);
g_free (freeme);
return paper_size;
}
/**
* gtk_paper_size_to_key_file:
* @paper_size: a #GtkPaperSize
* @key_file: the #GKeyFile to save the paper size to
* @group_name: the group to add the settings to in @key_file
*
* This function adds the paper size from @paper_size to @key_file.
*
* Since: 2.12
*/
void
gtk_paper_size_to_key_file (GtkPaperSize *paper_size,
GKeyFile *key_file,
const gchar *group_name)
{
const char *name, *ppd_name, *display_name;
g_return_if_fail (paper_size != NULL);
g_return_if_fail (key_file != 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, group_name,
"PPDName", ppd_name);
else
g_key_file_set_string (key_file, group_name,
"Name", name);
if (display_name)
g_key_file_set_string (key_file, group_name,
"DisplayName", display_name);
g_key_file_set_double (key_file, group_name,
"Width", gtk_paper_size_get_width (paper_size, GTK_UNIT_MM));
g_key_file_set_double (key_file, group_name,
"Height", gtk_paper_size_get_height (paper_size, GTK_UNIT_MM));
}
#define __GTK_PAPER_SIZE_C__
#include "gtkaliasdef.c"

View File

@ -84,6 +84,13 @@ gdouble gtk_paper_size_get_default_right_margin (GtkPaperSize *size,
G_CONST_RETURN gchar *gtk_paper_size_get_default (void);
GtkPaperSize *gtk_paper_size_new_from_key_file (GKeyFile *key_file,
const gchar *group_name,
GError **error);
void gtk_paper_size_to_key_file (GtkPaperSize *size,
GKeyFile *key_file,
const gchar *group_name);
G_END_DECLS
#endif /* __GTK_PAPER_SIZE_H__ */

View File

@ -125,7 +125,8 @@ typedef enum
{
GTK_PRINT_ERROR_GENERAL,
GTK_PRINT_ERROR_INTERNAL_ERROR,
GTK_PRINT_ERROR_NOMEM
GTK_PRINT_ERROR_NOMEM,
GTK_PRINT_ERROR_INVALID_FILE
} GtkPrintError;
GQuark gtk_print_error_quark (void);

View File

@ -45,6 +45,8 @@ struct _GtkPrintSettingsClass
GObjectClass parent_class;
};
#define KEYFILE_GROUP_NAME "Print Settings"
G_DEFINE_TYPE (GtkPrintSettings, gtk_print_settings, G_TYPE_OBJECT)
static void
@ -1499,7 +1501,191 @@ gtk_print_settings_set_output_bin (GtkPrintSettings *settings,
{
gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, output_bin);
}
/**
* gtk_print_settings_new_from_file:
* @file_name: the filename to read the settings from
* @error: return location for errors, or %NULL
*
* Reads the print settings from @filename. Returns a new #GtkPrintSettings
* object with the restored settings, or %NULL if an error occurred.
* See gtk_print_settings_to_file().
*
* Return value: the restored #GtkPrintSettings
*
* Since: 2.12
*/
GtkPrintSettings *
gtk_print_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 = gtk_print_settings_new_from_key_file (key_file, NULL, error);
g_key_file_free (key_file);
return settings;
}
/**
* gtk_print_settings_new_from_key_file:
* @key_file: the #GKeyFile to retrieve the settings from
* @error: return location for errors, or %NULL
*
* 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.12
*/
GtkPrintSettings *
gtk_print_settings_new_from_key_file (GKeyFile *key_file,
const gchar *group_name,
GError **error)
{
GtkPrintSettings *settings;
gchar **keys;
gsize n_keys, i;
GError *err = NULL;
g_return_val_if_fail (key_file != NULL, NULL);
if (!group_name)
group_name = KEYFILE_GROUP_NAME;
keys = g_key_file_get_keys (key_file,
group_name,
&n_keys,
&err);
if (err != NULL)
{
g_propagate_error (error, err);
return NULL;
}
settings = gtk_print_settings_new ();
for (i = 0 ; i < n_keys; ++i)
{
gchar *value;
value = g_key_file_get_string (key_file,
KEYFILE_GROUP_NAME,
keys[i],
NULL);
if (!value)
continue;
gtk_print_settings_set (settings, keys[i], value);
g_free (value);
}
g_strfreev (keys);
return settings;
}
/**
* gtk_print_settings_to_file:
* @settings: a #GtkPrintSettings
* @file_name: the file to save to
* @error: return location for errors, or %NULL
*
* This function saves the print settings from @settings to @file_name.
*
* Return value: %TRUE on success
*
* Since: 2.12
*/
gboolean
gtk_print_settings_to_file (GtkPrintSettings *settings,
const gchar *file_name,
GError **error)
{
GKeyFile *key_file;
gboolean retval = FALSE;
char *data = NULL;
gsize len;
GError *err = NULL;
g_return_val_if_fail (GTK_IS_PRINT_SETTINGS (settings), FALSE);
g_return_val_if_fail (file_name != NULL, FALSE);
key_file = g_key_file_new ();
gtk_print_settings_to_key_file (settings, key_file, NULL);
data = g_key_file_to_data (key_file, &len, &err);
if (!data)
goto out;
retval = g_file_set_contents (file_name, data, len, &err);
out:
if (err != NULL)
g_propagate_error (error, err);
g_key_file_free (key_file);
g_free (data);
return retval;
}
typedef struct {
GKeyFile *key_file;
const gchar *group_name;
} SettingsData;
static void
add_value_to_key_file (const gchar *key,
const gchar *value,
SettingsData *data)
{
g_key_file_set_string (data->key_file, data->group_name, key, value);
}
/**
* gtk_print_settings_to_key_file:
* @settings: a #GtkPrintSettings
* @key_file: the #GKeyFile to save the print settings to
* @group_name: the group to add the settings to in @key_file, or
* %NULL to use the default "Print Settings"
*
* This function adds the print settings from @settings to @key_file.
*
* Since: 2.12
*/
void
gtk_print_settings_to_key_file (GtkPrintSettings *settings,
GKeyFile *key_file,
const gchar *group_name)
{
SettingsData data;
g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
g_return_if_fail (key_file != NULL);
if (!group_name)
group_name = KEYFILE_GROUP_NAME;
data.key_file = key_file;
data.group_name = group_name;
gtk_print_settings_foreach (settings,
(GtkPrintSettingsFunc) add_value_to_key_file,
&data);
}
#define __GTK_PRINT_SETTINGS_C__
#include "gtkaliasdef.c"

View File

@ -46,6 +46,18 @@ GType gtk_print_settings_get_type (void) G_GNUC_CONST;
GtkPrintSettings *gtk_print_settings_new (void);
GtkPrintSettings *gtk_print_settings_copy (GtkPrintSettings *other);
GtkPrintSettings *gtk_print_settings_new_from_file (const gchar *file_name,
GError **error);
gboolean gtk_print_settings_to_file (GtkPrintSettings *settings,
const gchar *file_name,
GError **error);
GtkPrintSettings *gtk_print_settings_new_from_key_file (GKeyFile *key_file,
const gchar *group_name,
GError **error);
void gtk_print_settings_to_key_file (GtkPrintSettings *settings,
GKeyFile *key_file,
const gchar *group_name);
gboolean gtk_print_settings_has_key (GtkPrintSettings *settings,
const gchar *key);
G_CONST_RETURN gchar *gtk_print_settings_get (GtkPrintSettings *settings,

View File

@ -372,9 +372,6 @@ do_page_setup (GtkAction *action)
{
GtkPageSetup *new_page_setup;
if (settings == NULL)
settings = gtk_print_settings_new ();
new_page_setup = gtk_print_run_page_setup_dialog (GTK_WINDOW (main_window),
page_setup, settings);
@ -830,9 +827,14 @@ create_window (void)
GtkWidget *sw;
GtkActionGroup *actions;
GError *error;
GtkWindowGroup *group;
main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
group = gtk_window_group_new ();
gtk_window_group_add_window (group, GTK_WINDOW (main_window));
g_object_unref (group);
gtk_window_set_default_size (GTK_WINDOW (main_window),
400, 600);
@ -929,14 +931,42 @@ create_window (void)
int
main (int argc, char **argv)
{
GError *error = NULL;
g_set_application_name ("Print editor");
gtk_init (&argc, &argv);
settings = gtk_print_settings_new_from_file ("print-settings.ini", &error);
if (error) {
g_print ("Failed to load print settings: %s\n", error->message);
g_clear_error (&error);
settings = gtk_print_settings_new ();
}
g_assert (settings != NULL);
page_setup = gtk_page_setup_new_from_file ("page-setup.ini", &error);
if (error) {
g_print ("Failed to load page setup: %s\n", error->message);
g_clear_error (&error);
}
create_window ();
if (argc == 2)
load_file (argv[1]);
gtk_main ();
if (!gtk_print_settings_to_file (settings, "print-settings.ini", &error)) {
g_print ("Failed to save print settings: %s\n", error->message);
g_clear_error (&error);
}
if (page_setup &&
!gtk_page_setup_to_file (page_setup, "page-setup.ini", &error)) {
g_print ("Failed to save page setup: %s\n", error->message);
g_clear_error (&error);
}
return 0;
}