summaryrefslogtreecommitdiff
path: root/gtk/gtkprinteroptionset.c
blob: 5748565c7f0a67dc5d5ea43633abd59b853bb10c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* GTK - The GIMP Toolkit
 * gtkprintbackend.h: Abstract printer backend interfaces
 * Copyright (C) 2006, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <string.h>
#include <glib.h>
#include <gmodule.h>

#include "gtkprinteroptionset.h"
#include "gtkintl.h"

/*****************************************
 *         GtkPrinterOptionSet    *
 *****************************************/

enum {
  CHANGED,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

/* ugly side-effect of aliasing */
#undef gtk_printer_option_set

G_DEFINE_TYPE (GtkPrinterOptionSet, gtk_printer_option_set, G_TYPE_OBJECT)

static void
gtk_printer_option_set_finalize (GObject *object)
{
  GtkPrinterOptionSet *set = GTK_PRINTER_OPTION_SET (object);

  g_hash_table_destroy (set->hash);
  g_ptr_array_foreach (set->array, (GFunc)g_object_unref, NULL);
  g_ptr_array_free (set->array, TRUE);
  
  G_OBJECT_CLASS (gtk_printer_option_set_parent_class)->finalize (object);
}

static void
gtk_printer_option_set_init (GtkPrinterOptionSet *set)
{
  set->array = g_ptr_array_new ();
  set->hash = g_hash_table_new (g_str_hash, g_str_equal);
}

static void
gtk_printer_option_set_class_init (GtkPrinterOptionSetClass *class)
{
  GObjectClass *gobject_class = (GObjectClass *)class;

  gobject_class->finalize = gtk_printer_option_set_finalize;

  signals[CHANGED] =
    g_signal_new (I_("changed"),
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrinterOptionSetClass, changed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
}


static void
emit_changed (GtkPrinterOptionSet *set)
{
  g_signal_emit (set, signals[CHANGED], 0);
}

GtkPrinterOptionSet *
gtk_printer_option_set_new (void)
{
  return g_object_new (GTK_TYPE_PRINTER_OPTION_SET, NULL);
}

void
gtk_printer_option_set_remove (GtkPrinterOptionSet *set,
			       GtkPrinterOption    *option)
{
  int i;
  
  for (i = 0; i < set->array->len; i++)
    {
      if (g_ptr_array_index (set->array, i) == option)
	{
	  g_ptr_array_remove_index (set->array, i);
	  g_hash_table_remove (set->hash, option->name);
	  g_signal_handlers_disconnect_by_func (option, emit_changed, set);

	  g_object_unref (option);
	  break;
	}
    }
}

void
gtk_printer_option_set_add (GtkPrinterOptionSet *set,
			    GtkPrinterOption    *option)
{
  g_object_ref (option);
  
  if (gtk_printer_option_set_lookup (set, option->name))
    gtk_printer_option_set_remove (set, option);
    
  g_ptr_array_add (set->array, option);
  g_hash_table_insert (set->hash, option->name, option);
  g_signal_connect_object (option, "changed", G_CALLBACK (emit_changed), set, G_CONNECT_SWAPPED);
}

GtkPrinterOption *
gtk_printer_option_set_lookup (GtkPrinterOptionSet *set,
			       const char          *name)
{
  gpointer ptr;

  ptr = g_hash_table_lookup (set->hash, name);

  return GTK_PRINTER_OPTION (ptr);
}

void
gtk_printer_option_set_clear_conflicts (GtkPrinterOptionSet *set)
{
  gtk_printer_option_set_foreach (set,
				  (GtkPrinterOptionSetFunc)gtk_printer_option_clear_has_conflict,
				  NULL);
}

/**
 * gtk_printer_option_set_get_groups:
 * @set: a #GtkPrinterOptionSet
 *
 * Gets the groups in this set.
 *
 * Returns: (element-type utf8) (transfer full): a list of group names.
 */
GList *
gtk_printer_option_set_get_groups (GtkPrinterOptionSet *set)
{
  GtkPrinterOption *option;
  GList *list = NULL;
  int i;

  for (i = 0; i < set->array->len; i++)
    {
      option = g_ptr_array_index (set->array, i);

      if (g_list_find_custom (list, option->group, (GCompareFunc)g_strcmp0) == NULL)
	list = g_list_prepend (list, g_strdup (option->group));
    }

  return g_list_reverse (list);
}

void
gtk_printer_option_set_foreach_in_group (GtkPrinterOptionSet     *set,
					 const char              *group,
					 GtkPrinterOptionSetFunc  func,
					 gpointer                 user_data)
{
  GtkPrinterOption *option;
  int i;

  for (i = 0; i < set->array->len; i++)
    {
      option = g_ptr_array_index (set->array, i);

      if (group == NULL || g_strcmp0 (group, option->group) == 0)
	func (option, user_data);
    }
}

void
gtk_printer_option_set_foreach (GtkPrinterOptionSet *set,
				GtkPrinterOptionSetFunc func,
				gpointer user_data)
{
  gtk_printer_option_set_foreach_in_group (set, NULL, func, user_data);
}