]> Pileus Git - ~andy/gtk/blob - gtk/gtkprinteroptionset.c
gtk/: fully remove gtkalias hacks
[~andy/gtk] / gtk / gtkprinteroptionset.c
1 /* GTK - The GIMP Toolkit
2  * gtkprintbackend.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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include <string.h>
23 #include <glib.h>
24 #include <gmodule.h>
25
26 #include "gtkprinteroptionset.h"
27
28 /*****************************************
29  *         GtkPrinterOptionSet    *
30  *****************************************/
31
32 enum {
33   CHANGED,
34   LAST_SIGNAL
35 };
36
37 static guint signals[LAST_SIGNAL] = { 0 };
38
39 /* ugly side-effect of aliasing */
40 #undef gtk_printer_option_set
41
42 G_DEFINE_TYPE (GtkPrinterOptionSet, gtk_printer_option_set, G_TYPE_OBJECT)
43
44 static void
45 gtk_printer_option_set_finalize (GObject *object)
46 {
47   GtkPrinterOptionSet *set = GTK_PRINTER_OPTION_SET (object);
48
49   g_hash_table_destroy (set->hash);
50   g_ptr_array_foreach (set->array, (GFunc)g_object_unref, NULL);
51   g_ptr_array_free (set->array, TRUE);
52   
53   G_OBJECT_CLASS (gtk_printer_option_set_parent_class)->finalize (object);
54 }
55
56 static void
57 gtk_printer_option_set_init (GtkPrinterOptionSet *set)
58 {
59   set->array = g_ptr_array_new ();
60   set->hash = g_hash_table_new (g_str_hash, g_str_equal);
61 }
62
63 static void
64 gtk_printer_option_set_class_init (GtkPrinterOptionSetClass *class)
65 {
66   GObjectClass *gobject_class = (GObjectClass *)class;
67
68   gobject_class->finalize = gtk_printer_option_set_finalize;
69
70   signals[CHANGED] =
71     g_signal_new ("changed",
72                   G_TYPE_FROM_CLASS (gobject_class),
73                   G_SIGNAL_RUN_LAST,
74                   G_STRUCT_OFFSET (GtkPrinterOptionSetClass, changed),
75                   NULL, NULL,
76                   g_cclosure_marshal_VOID__VOID,
77                   G_TYPE_NONE, 0);
78 }
79
80
81 static void
82 emit_changed (GtkPrinterOptionSet *set)
83 {
84   g_signal_emit (set, signals[CHANGED], 0);
85 }
86
87 GtkPrinterOptionSet *
88 gtk_printer_option_set_new (void)
89 {
90   return g_object_new (GTK_TYPE_PRINTER_OPTION_SET, NULL);
91 }
92
93 void
94 gtk_printer_option_set_remove (GtkPrinterOptionSet *set,
95                                GtkPrinterOption    *option)
96 {
97   int i;
98   
99   for (i = 0; i < set->array->len; i++)
100     {
101       if (g_ptr_array_index (set->array, i) == option)
102         {
103           g_ptr_array_remove_index (set->array, i);
104           g_hash_table_remove (set->hash, option->name);
105           g_signal_handlers_disconnect_by_func (option, emit_changed, set);
106
107           g_object_unref (option);
108           break;
109         }
110     }
111 }
112
113 void
114 gtk_printer_option_set_add (GtkPrinterOptionSet *set,
115                             GtkPrinterOption    *option)
116 {
117   g_object_ref (option);
118   
119   if (gtk_printer_option_set_lookup (set, option->name))
120     gtk_printer_option_set_remove (set, option);
121     
122   g_ptr_array_add (set->array, option);
123   g_hash_table_insert (set->hash, option->name, option);
124   g_signal_connect_object (option, "changed", G_CALLBACK (emit_changed), set, G_CONNECT_SWAPPED);
125 }
126
127 GtkPrinterOption *
128 gtk_printer_option_set_lookup (GtkPrinterOptionSet *set,
129                                const char          *name)
130 {
131   gpointer ptr;
132
133   ptr = g_hash_table_lookup (set->hash, name);
134
135   return GTK_PRINTER_OPTION (ptr);
136 }
137
138 void
139 gtk_printer_option_set_clear_conflicts (GtkPrinterOptionSet *set)
140 {
141   gtk_printer_option_set_foreach (set,
142                                   (GtkPrinterOptionSetFunc)gtk_printer_option_clear_has_conflict,
143                                   NULL);
144 }
145
146 /**
147  * gtk_printer_option_set_get_groups:
148  *
149  * Return value: (element-type utf8) (transfer full):
150  */
151 GList *
152 gtk_printer_option_set_get_groups (GtkPrinterOptionSet *set)
153 {
154   GtkPrinterOption *option;
155   GList *list = NULL;
156   int i;
157
158   for (i = 0; i < set->array->len; i++)
159     {
160       option = g_ptr_array_index (set->array, i);
161
162       if (g_list_find_custom (list, option->group, (GCompareFunc)g_strcmp0) == NULL)
163         list = g_list_prepend (list, g_strdup (option->group));
164     }
165
166   return g_list_reverse (list);
167 }
168
169 void
170 gtk_printer_option_set_foreach_in_group (GtkPrinterOptionSet     *set,
171                                          const char              *group,
172                                          GtkPrinterOptionSetFunc  func,
173                                          gpointer                 user_data)
174 {
175   GtkPrinterOption *option;
176   int i;
177
178   for (i = 0; i < set->array->len; i++)
179     {
180       option = g_ptr_array_index (set->array, i);
181
182       if (group == NULL || g_strcmp0 (group, option->group) == 0)
183         func (option, user_data);
184     }
185 }
186
187 void
188 gtk_printer_option_set_foreach (GtkPrinterOptionSet *set,
189                                 GtkPrinterOptionSetFunc func,
190                                 gpointer user_data)
191 {
192   gtk_printer_option_set_foreach_in_group (set, NULL, func, user_data);
193 }