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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
* HardInfo - Displays System Information
* Copyright (C) 2003-2007 Leandro A. F. Pereira <leandro@hardinfo.org>
*
* 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, version 2.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct _CUPSDest CUPSDest;
typedef struct _CUPSOption CUPSOption;
struct _CUPSOption {
char *name, *value;
};
struct _CUPSDest {
char *name, *instance;
int is_default;
int num_options;
CUPSOption *options;
};
static int (*cups_dests_get) (CUPSDest **dests) = NULL;
static int (*cups_dests_free) (int num_dests, CUPSDest *dests) = NULL;
static gboolean cups_init = FALSE;
static gboolean
remove_printer_devices(gpointer key, gpointer value, gpointer data)
{
return g_str_has_prefix(key, "PRN");
}
static void
__init_cups(void)
{
static GModule *cups = NULL;
const char *libcups[] = { "libcups", "libcups.so", "libcups.so.1", "libcups.so.2", NULL };
if (!(cups_dests_get && cups_dests_free)) {
int i;
for (i = 0; libcups[i] != NULL; i++) {
cups = g_module_open(libcups[i], G_MODULE_BIND_LAZY);
if (cups)
break;
}
if (!cups) {
cups_init = FALSE;
return;
}
if (!g_module_symbol(cups, "cupsGetDests", (gpointer) & cups_dests_get)
|| !g_module_symbol(cups, "cupsFreeDests", (gpointer) & cups_dests_free)) {
g_module_close(cups);
cups_init = FALSE;
}
}
cups_init = TRUE;
}
gchar *__cups_callback_ptype(gchar *strvalue)
{
if (strvalue) {
unsigned value = atoi(strvalue);
gchar *output = g_strdup("\n");
if (value & 0x0004)
output = h_strdup_cprintf("\342\232\254 Can do black and white printing=\n", output);
if (value & 0x0008)
output = h_strdup_cprintf("\342\232\254 Can do color printing=\n", output);
if (value & 0x0010)
output = h_strdup_cprintf("\342\232\254 Can do duplexing=\n", output);
if (value & 0x0020)
output = h_strdup_cprintf("\342\232\254 Can do staple output=\n", output);
if (value & 0x0040)
output = h_strdup_cprintf("\342\232\254 Can do copies=\n", output);
if (value & 0x0080)
output = h_strdup_cprintf("\342\232\254 Can collate copies=\n", output);
if (value & 0x80000)
output = h_strdup_cprintf("\342\232\254 Printer is rejecting jobs=\n", output);
if (value & 0x1000000)
output = h_strdup_cprintf("\342\232\254 Printer was automatically discovered and added=\n", output);
return output;
} else {
return g_strdup("Unknown");
}
}
gchar *__cups_callback_state(gchar *value)
{
if (!value) {
return g_strdup("Unknown");
}
if (g_str_equal(value, "3")) {
return g_strdup("Idle");
} else if (g_str_equal(value, "4")) {
return g_strdup("Printing a Job");
} else if (g_str_equal(value, "5")) {
return g_strdup("Stopped");
} else {
return g_strdup("Unknown");
}
}
gchar *__cups_callback_state_change_time(gchar *value)
{
struct tm tm;
char buf[255];
if (value) {
strptime(value, "%s", &tm);
strftime(buf, sizeof(buf), "%c", &tm);
return g_strdup(buf);
} else {
return g_strdup("Unknown");
}
}
gchar *__cups_callback_boolean(gchar *value)
{
if (value) {
return g_strdup(g_str_equal(value, "1") ? "Yes" : "No");
} else {
return g_strdup("Unknown");
}
}
const struct {
char *key, *name;
gchar *(*callback)(gchar *value);
} cups_fields[] = {
{ "Printer Information", NULL, NULL },
{ "printer-info", "Destination Name", NULL },
{ "printer-make-and-model", "Make and Model", NULL },
{ "Capabilities", NULL, NULL },
{ "printer-type", "#", __cups_callback_ptype },
{ "Printer State", NULL, NULL },
{ "printer-state", "State", __cups_callback_state },
{ "printer-state-change-time", "Change Time", __cups_callback_state_change_time },
{ "printer-state-reasons", "State Reasons" },
{ "Sharing Information", NULL, NULL },
{ "printer-is-shared", "Shared?", __cups_callback_boolean },
{ "printer-location", "Physical Location" },
{ "auth-info-required", "Authentication Required", __cups_callback_boolean },
{ "Jobs", NULL, NULL },
{ "job-hold-until", "Hold Until", NULL },
{ "job-priority", "Priority", NULL },
{ "printer-is-accepting-jobs", "Accepting Jobs", __cups_callback_boolean },
{ "Media", NULL, NULL },
{ "media", "Media", NULL },
{ "finishings", "Finishings", NULL },
{ "copies", "Copies", NULL },
};
void
__scan_printers(void)
{
int num_dests, i, j;
CUPSDest *dests;
gchar *prn_id, *prn_moreinfo;
g_free(printer_list);
g_free(printer_icons);
if (!cups_init) {
__init_cups();
printer_icons = g_strdup("");
printer_list = g_strdup("[Printers]\n"
"No suitable CUPS library found=");
return;
}
/* remove old devices from global device table */
g_hash_table_foreach_remove(moreinfo, remove_printer_devices, NULL);
num_dests = cups_dests_get(&dests);
if (num_dests > 0) {
printer_list = g_strdup_printf("[Printers (CUPS)]\n");
printer_icons = g_strdup("");
for (i = 0; i < num_dests; i++) {
GHashTable *options;
options = g_hash_table_new(g_str_hash, g_str_equal);
for (j = 0; j < dests[i].num_options; j++) {
g_hash_table_insert(options,
g_strdup(dests[i].options[j].name),
g_strdup(dests[i].options[j].value));
}
prn_id = g_strdup_printf("PRN%d", i);
printer_list = h_strdup_cprintf("\n$%s$%s=%s\n",
printer_list,
prn_id,
dests[i].name,
dests[i].is_default ? "<i>Default</i>" : "");
printer_icons = h_strdup_cprintf("\nIcon$%s$%s=printer.png",
printer_icons,
prn_id,
dests[i].name);
prn_moreinfo = g_strdup("");
for (j = 0; j < G_N_ELEMENTS(cups_fields); j++) {
if (!cups_fields[j].name) {
prn_moreinfo = h_strdup_cprintf("[%s]\n",
prn_moreinfo,
cups_fields[j].key);
} else {
gchar *temp;
temp = g_hash_table_lookup(options, cups_fields[j].key);
if (cups_fields[j].callback) {
temp = cups_fields[j].callback(temp);
} else {
if (temp) {
/* FIXME Do proper escaping */
temp = g_strdup(strreplacechr(temp, "&=", ' '));
} else {
temp = g_strdup("Unknown");
}
}
prn_moreinfo = h_strdup_cprintf("%s=%s\n",
prn_moreinfo,
cups_fields[j].name,
temp);
g_free(temp);
}
}
g_hash_table_insert(moreinfo, prn_id, prn_moreinfo);
g_hash_table_destroy(options);
}
cups_dests_free(num_dests, dests);
} else {
printer_list = g_strdup("[Printers]\n"
"No printers found=\n");
}
}
|