1/* GTK - The GIMP Toolkit
2 * gtkprintbackendprivate.h: Abstract printer backend interfaces
3 * Copyright (C) 2006, Red Hat, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "config.h"
20#include <string.h>
21#include <glib.h>
22#include <gmodule.h>
23
24#include "gtkprinteroptionset.h"
25#include "gtkintl.h"
26
27/*****************************************
28 * GtkPrinterOptionSet *
29 *****************************************/
30
31enum {
32 CHANGED,
33 LAST_SIGNAL
34};
35
36static guint signals[LAST_SIGNAL] = { 0 };
37
38/* ugly side-effect of aliasing */
39#undef gtk_printer_option_set
40
41G_DEFINE_TYPE (GtkPrinterOptionSet, gtk_printer_option_set, G_TYPE_OBJECT)
42
43static void
44gtk_printer_option_set_finalize (GObject *object)
45{
46 GtkPrinterOptionSet *set = GTK_PRINTER_OPTION_SET (object);
47
48 g_hash_table_destroy (hash_table: set->hash);
49 g_ptr_array_foreach (array: set->array, func: (GFunc)g_object_unref, NULL);
50 g_ptr_array_free (array: set->array, TRUE);
51
52 G_OBJECT_CLASS (gtk_printer_option_set_parent_class)->finalize (object);
53}
54
55static void
56gtk_printer_option_set_init (GtkPrinterOptionSet *set)
57{
58 set->array = g_ptr_array_new ();
59 set->hash = g_hash_table_new (hash_func: g_str_hash, key_equal_func: g_str_equal);
60}
61
62static void
63gtk_printer_option_set_class_init (GtkPrinterOptionSetClass *class)
64{
65 GObjectClass *gobject_class = (GObjectClass *)class;
66
67 gobject_class->finalize = gtk_printer_option_set_finalize;
68
69 signals[CHANGED] =
70 g_signal_new (I_("changed"),
71 G_TYPE_FROM_CLASS (gobject_class),
72 signal_flags: G_SIGNAL_RUN_LAST,
73 G_STRUCT_OFFSET (GtkPrinterOptionSetClass, changed),
74 NULL, NULL,
75 NULL,
76 G_TYPE_NONE, n_params: 0);
77}
78
79
80static void
81emit_changed (GtkPrinterOptionSet *set)
82{
83 g_signal_emit (instance: set, signal_id: signals[CHANGED], detail: 0);
84}
85
86GtkPrinterOptionSet *
87gtk_printer_option_set_new (void)
88{
89 return g_object_new (GTK_TYPE_PRINTER_OPTION_SET, NULL);
90}
91
92void
93gtk_printer_option_set_remove (GtkPrinterOptionSet *set,
94 GtkPrinterOption *option)
95{
96 int i;
97
98 for (i = 0; i < set->array->len; i++)
99 {
100 if (g_ptr_array_index (set->array, i) == option)
101 {
102 g_ptr_array_remove_index (array: set->array, index_: i);
103 g_hash_table_remove (hash_table: set->hash, key: option->name);
104 g_signal_handlers_disconnect_by_func (option, emit_changed, set);
105
106 g_object_unref (object: option);
107 break;
108 }
109 }
110}
111
112void
113gtk_printer_option_set_add (GtkPrinterOptionSet *set,
114 GtkPrinterOption *option)
115{
116 g_object_ref (option);
117
118 if (gtk_printer_option_set_lookup (set, name: option->name))
119 gtk_printer_option_set_remove (set, option);
120
121 g_ptr_array_add (array: set->array, data: option);
122 g_hash_table_insert (hash_table: set->hash, key: option->name, value: option);
123 g_signal_connect_object (instance: option, detailed_signal: "changed", G_CALLBACK (emit_changed), gobject: set, connect_flags: G_CONNECT_SWAPPED);
124}
125
126GtkPrinterOption *
127gtk_printer_option_set_lookup (GtkPrinterOptionSet *set,
128 const char *name)
129{
130 gpointer ptr;
131
132 ptr = g_hash_table_lookup (hash_table: set->hash, key: name);
133
134 return GTK_PRINTER_OPTION (ptr);
135}
136
137void
138gtk_printer_option_set_clear_conflicts (GtkPrinterOptionSet *set)
139{
140 gtk_printer_option_set_foreach (set,
141 func: (GtkPrinterOptionSetFunc)gtk_printer_option_clear_has_conflict,
142 NULL);
143}
144
145/**
146 * gtk_printer_option_set_get_groups:
147 * @set: a `GtkPrinterOptionSet`
148 *
149 * Gets the groups in this set.
150 *
151 * Returns: (element-type utf8) (transfer full): a list of group names.
152 */
153GList *
154gtk_printer_option_set_get_groups (GtkPrinterOptionSet *set)
155{
156 GtkPrinterOption *option;
157 GList *list = NULL;
158 int i;
159
160 for (i = 0; i < set->array->len; i++)
161 {
162 option = g_ptr_array_index (set->array, i);
163
164 if (g_list_find_custom (list, data: option->group, func: (GCompareFunc)g_strcmp0) == NULL)
165 list = g_list_prepend (list, data: g_strdup (str: option->group));
166 }
167
168 return g_list_reverse (list);
169}
170
171void
172gtk_printer_option_set_foreach_in_group (GtkPrinterOptionSet *set,
173 const char *group,
174 GtkPrinterOptionSetFunc func,
175 gpointer user_data)
176{
177 GtkPrinterOption *option;
178 int i;
179
180 for (i = 0; i < set->array->len; i++)
181 {
182 option = g_ptr_array_index (set->array, i);
183
184 if (group == NULL || g_strcmp0 (str1: group, str2: option->group) == 0)
185 func (option, user_data);
186 }
187}
188
189void
190gtk_printer_option_set_foreach (GtkPrinterOptionSet *set,
191 GtkPrinterOptionSetFunc func,
192 gpointer user_data)
193{
194 gtk_printer_option_set_foreach_in_group (set, NULL, func, user_data);
195}
196

source code of gtk/gtk/gtkprinteroptionset.c