sync'ing up for 3.0alpha20 release
[amitay/samba.git] / source3 / printing / notify.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2
4    printing backend routines
5    Copyright (C) Tim Potter, 2002
6    Copyright (C) Gerald Carter,         2002
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "printing.h"
24
25 static TALLOC_CTX *send_ctx;
26
27 static struct notify_queue {
28         struct notify_queue *next, *prev;
29         void *buf;
30         size_t buflen;
31 } *notify_queue_head = NULL;
32
33 /*******************************************************************
34  Used to decide if we need a short select timeout.
35 *******************************************************************/
36
37 BOOL print_notify_messages_pending(void)
38 {
39         return (notify_queue_head != NULL);
40 }
41
42 /*******************************************************************
43  Actually send the batched messages.
44 *******************************************************************/
45
46 void print_notify_send_messages(void)
47 {
48         TDB_CONTEXT *tdb;
49         char *buf;
50         struct notify_queue *pq;
51         size_t msg_count = 0, offset = 0;
52
53         if (!print_notify_messages_pending())
54                 return;
55
56         if (!send_ctx)
57                 return;
58
59         tdb = conn_tdb_ctx();
60
61         if (!tdb) {
62                 DEBUG(3, ("Failed to open connections database in send_spoolss_notify2_msg\n"));
63                 return;
64         }
65         
66         /* Count the space needed to send the messages. */
67         for (pq = notify_queue_head; pq; pq = pq->next, msg_count++)
68                 offset += (pq->buflen + 4);
69                 
70         offset += 4; /* For count. */
71
72         buf = talloc(send_ctx, offset);
73         if (!buf) {
74                 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
75                 talloc_destroy_pool(send_ctx);
76                 return;
77         }
78
79         offset = 0;
80         SIVAL(buf,offset,msg_count);
81         offset += 4;
82         for (pq = notify_queue_head; pq; pq = pq->next) {
83                 SIVAL(buf,offset,pq->buflen);
84                 offset += 4;
85                 memcpy(buf + offset, pq->buf, pq->buflen);
86                 offset += pq->buflen;
87         }
88
89         DEBUG(5, ("print_notify_send_messages: sending %d print notify message%s\n", 
90                   msg_count, msg_count != 1 ? "s" : ""));
91
92         message_send_all(tdb, MSG_PRINTER_NOTIFY2, buf, offset, False, NULL);
93         talloc_destroy_pool(send_ctx);
94         notify_queue_head = NULL;
95 }
96
97 /*******************************************************************
98  Batch up print notify messages.
99 *******************************************************************/
100
101 static void send_spoolss_notify2_msg(struct spoolss_notify_msg *msg)
102 {
103         char *buf = NULL;
104         size_t buflen = 0, len;
105         struct notify_queue *pnqueue, *tmp_ptr;
106
107         /* Let's not waste any time with this */
108
109         if (lp_disable_spoolss())
110                 return;
111
112         if (!send_ctx)
113                 send_ctx = talloc_init_named("print notify queue");
114
115         if (!send_ctx)
116                 goto fail;
117
118         /* Flatten data into a message */
119
120 again:
121         len = 0;
122
123         /* Pack header */
124
125         len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
126
127         len += tdb_pack(buf + len, buflen - len, "ddddd",
128                         msg->type, msg->field, msg->id, msg->len, msg->flags);
129
130         /* Pack data */
131
132         if (msg->len == 0)
133                 len += tdb_pack(buf + len, buflen - len, "dd",
134                                 msg->notify.value[0], msg->notify.value[1]);
135         else
136                 len += tdb_pack(buf + len, buflen - len, "B",
137                                 msg->len, msg->notify.data);
138
139         if (buflen != len) {
140                 buf = talloc_realloc(send_ctx, buf, len);
141                 if (!buf)
142                         goto fail;
143                 buflen = len;
144                 goto again;
145         }
146
147         /* Store the message on the pending queue. */
148
149         pnqueue = talloc(send_ctx, sizeof(*pnqueue));
150         if (!pnqueue)
151                 goto fail;
152
153         pnqueue->buf = buf;
154         pnqueue->buflen = buflen;
155
156         DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x to notify_queue_head\n", msg->type, msg->field));
157                   
158         /* Note we add to the end of the list to ensure
159          * the messages are sent in the order they were received. JRA.
160          */
161         DLIST_ADD_END(notify_queue_head, pnqueue, tmp_ptr);
162
163         return;
164
165   fail:
166
167         DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
168 }
169
170 static void send_notify_field_values(const char *printer_name, uint32 type,
171                                      uint32 field, uint32 id, uint32 value1, 
172                                      uint32 value2, uint32 flags)
173 {
174         struct spoolss_notify_msg msg;
175
176         ZERO_STRUCT(msg);
177
178         fstrcpy(msg.printer, printer_name);
179         msg.type = type;
180         msg.field = field;
181         msg.id = id;
182         msg.notify.value[0] = value1;
183         msg.notify.value[1] = value2;
184         msg.flags = flags;
185
186         send_spoolss_notify2_msg(&msg);
187 }
188
189 static void send_notify_field_buffer(const char *printer_name, uint32 type,
190                                      uint32 field, uint32 id, uint32 len,
191                                      char *buffer)
192 {
193         struct spoolss_notify_msg msg;
194
195         ZERO_STRUCT(msg);
196
197         fstrcpy(msg.printer, printer_name);
198         msg.type = type;
199         msg.field = field;
200         msg.id = id;
201         msg.len = len;
202         msg.notify.data = buffer;
203
204         send_spoolss_notify2_msg(&msg);
205 }
206
207 /* Send a message that the printer status has changed */
208
209 void notify_printer_status_byname(const char *printer_name, uint32 status)
210 {
211         /* Printer status stored in value1 */
212
213         send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE, 
214                                  PRINTER_NOTIFY_STATUS, 0, 
215                                  status, 0, 0);
216 }
217
218 void notify_printer_status(int snum, uint32 status)
219 {
220         const char *printer_name = SERVICE(snum); 
221
222         if (printer_name)
223                 notify_printer_status_byname(printer_name, status);
224 }
225
226 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
227                               uint32 flags)
228 {
229         /* Job id stored in id field, status in value1 */
230
231         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
232                                  JOB_NOTIFY_STATUS, jobid,
233                                  status, 0, flags);
234 }
235
236 void notify_job_status(int snum, uint32 jobid, uint32 status)
237 {
238         const char *printer_name = SERVICE(snum);
239
240         notify_job_status_byname(printer_name, jobid, status, 0);
241 }
242
243 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
244 {
245         const char *printer_name = SERVICE(snum);
246
247         /* Job id stored in id field, status in value1 */
248
249         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
250                                  JOB_NOTIFY_TOTAL_BYTES, jobid,
251                                  size, 0, 0);
252 }
253
254 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
255 {
256         const char *printer_name = SERVICE(snum);
257
258         /* Job id stored in id field, status in value1 */
259
260         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
261                                  JOB_NOTIFY_TOTAL_PAGES, jobid,
262                                  pages, 0, 0);
263 }
264
265 void notify_job_username(int snum, uint32 jobid, char *name)
266 {
267         const char *printer_name = SERVICE(snum);
268
269         send_notify_field_buffer(
270                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
271                 jobid, strlen(name) + 1, name);
272 }
273
274 void notify_job_name(int snum, uint32 jobid, char *name)
275 {
276         const char *printer_name = SERVICE(snum);
277
278         send_notify_field_buffer(
279                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
280                 jobid, strlen(name) + 1, name);
281 }
282
283 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
284 {
285         const char *printer_name = SERVICE(snum);
286
287         send_notify_field_buffer(
288                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
289                 jobid, sizeof(submitted), (char *)&submitted);
290 }
291
292 void notify_printer_driver(int snum, char *driver_name)
293 {
294         const char *printer_name = SERVICE(snum);
295
296         send_notify_field_buffer(
297                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_DRIVER_NAME,
298                 snum, strlen(driver_name) + 1, driver_name);
299 }
300
301 void notify_printer_comment(int snum, char *comment)
302 {
303         const char *printer_name = SERVICE(snum);
304
305         send_notify_field_buffer(
306                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_COMMENT,
307                 snum, strlen(comment) + 1, comment);
308 }
309
310 void notify_printer_sharename(int snum, char *share_name)
311 {
312         const char *printer_name = SERVICE(snum);
313
314         send_notify_field_buffer(
315                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_SHARE_NAME,
316                 snum, strlen(share_name) + 1, share_name);
317 }
318
319 void notify_printer_port(int snum, char *port_name)
320 {
321         const char *printer_name = SERVICE(snum);
322
323         send_notify_field_buffer(
324                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_PORT_NAME,
325                 snum, strlen(port_name) + 1, port_name);
326 }
327
328 void notify_printer_location(int snum, char *location)
329 {
330         const char *printer_name = SERVICE(snum);
331
332         send_notify_field_buffer(
333                 printer_name, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_LOCATION,
334                 snum, strlen(location) + 1, location);
335 }