Merge of print notify fixes from APPLIANCE_HEAD.
[tprouty/samba.git] / source / printing / notify.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2
4    printing backend routines
5    Copyright (C) Tim Potter, 2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "printing.h"
23
24 /*
25  * Print notification routines
26  */
27
28 static void send_spoolss_notify2_msg(struct spoolss_notify_msg *msg)
29 {
30         char *buf = NULL;
31         int buflen = 0, len;
32         TDB_CONTEXT *tdb;
33
34         /* Let's not waste any time with this */
35
36         if (lp_disable_spoolss())
37                 return;
38
39         /* Flatten data into a message */
40
41 again:
42         len = 0;
43
44         /* Pack header */
45
46         len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
47
48         len += tdb_pack(buf + len, buflen - len, "ddddd",
49                         msg->type, msg->field, msg->id, msg->len, msg->flags);
50
51         /* Pack data */
52
53         if (msg->len == 0)
54                 len += tdb_pack(buf + len, buflen - len, "dd",
55                                 msg->notify.value[0], msg->notify.value[1]);
56         else
57                 len += tdb_pack(buf + len, buflen - len, "B",
58                                 msg->len, msg->notify.data);
59
60         if (buflen != len) {
61                 buf = Realloc(buf, len);
62                 buflen = len;
63                 goto again;
64         }
65
66         /* Send message */
67
68         tdb = conn_tdb_ctx();
69
70         if (!tdb) {
71                 DEBUG(3, ("Failed to open connections database in send_spoolss_notify2_msg\n"));
72                 goto done;
73         }
74         
75         message_send_all(tdb, MSG_PRINTER_NOTIFY2, buf, buflen, False, NULL);
76
77 done:
78         SAFE_FREE(buf);
79 }
80
81 static void send_notify_field_values(const char *printer_name, uint32 type,
82                                      uint32 field, uint32 id, uint32 value1, 
83                                      uint32 value2, uint32 flags)
84 {
85         struct spoolss_notify_msg msg;
86
87         ZERO_STRUCT(msg);
88
89         fstrcpy(msg.printer, printer_name);
90         msg.type = type;
91         msg.field = field;
92         msg.id = id;
93         msg.notify.value[0] = value1;
94         msg.notify.value[1] = value2;
95         msg.flags = flags;
96
97         send_spoolss_notify2_msg(&msg);
98 }
99
100 static void send_notify_field_buffer(const char *printer_name, uint32 type,
101                                      uint32 field, uint32 id, uint32 len,
102                                      char *buffer)
103 {
104         struct spoolss_notify_msg msg;
105
106         ZERO_STRUCT(msg);
107
108         fstrcpy(msg.printer, printer_name);
109         msg.type = type;
110         msg.field = field;
111         msg.id = id;
112         msg.len = len;
113         msg.notify.data = buffer;
114
115         send_spoolss_notify2_msg(&msg);
116 }
117
118 /* Send a message that the printer status has changed */
119
120 void notify_printer_status_byname(const char *printer_name, uint32 status)
121 {
122         /* Printer status stored in value1 */
123
124         send_notify_field_values(printer_name, PRINTER_NOTIFY_TYPE, 
125                                  PRINTER_NOTIFY_STATUS, 0, 
126                                  status, 0, 0);
127 }
128
129 void notify_printer_status(int snum, uint32 status)
130 {
131         const char *printer_name = PRINTERNAME(snum);
132
133         if (printer_name)
134                 notify_printer_status_byname(printer_name, status);
135 }
136
137 void notify_job_status_byname(const char *printer_name, uint32 jobid, uint32 status,
138                               uint32 flags)
139 {
140         /* Job id stored in id field, status in value1 */
141
142         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
143                                  JOB_NOTIFY_STATUS, jobid,
144                                  status, 0, flags);
145 }
146
147 void notify_job_status(int snum, uint32 jobid, uint32 status)
148 {
149         const char *printer_name = PRINTERNAME(snum);
150
151         notify_job_status_byname(printer_name, jobid, status, 0);
152 }
153
154 void notify_job_total_bytes(int snum, uint32 jobid, uint32 size)
155 {
156         const char *printer_name = PRINTERNAME(snum);
157
158         /* Job id stored in id field, status in value1 */
159
160         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
161                                  JOB_NOTIFY_TOTAL_BYTES, jobid,
162                                  size, 0, 0);
163 }
164
165 void notify_job_total_pages(int snum, uint32 jobid, uint32 pages)
166 {
167         const char *printer_name = PRINTERNAME(snum);
168
169         /* Job id stored in id field, status in value1 */
170
171         send_notify_field_values(printer_name, JOB_NOTIFY_TYPE,
172                                  JOB_NOTIFY_TOTAL_PAGES, jobid,
173                                  pages, 0, 0);
174 }
175
176 void notify_job_username(int snum, uint32 jobid, char *name)
177 {
178         const char *printer_name = PRINTERNAME(snum);
179
180         send_notify_field_buffer(
181                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_USER_NAME,
182                 jobid, strlen(name) + 1, name);
183 }
184
185 void notify_job_name(int snum, uint32 jobid, char *name)
186 {
187         const char *printer_name = PRINTERNAME(snum);
188
189         send_notify_field_buffer(
190                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_DOCUMENT,
191                 jobid, strlen(name) + 1, name);
192 }
193
194 void notify_job_submitted(int snum, uint32 jobid, time_t submitted)
195 {
196         const char *printer_name = PRINTERNAME(snum);
197
198         send_notify_field_buffer(
199                 printer_name, JOB_NOTIFY_TYPE, JOB_NOTIFY_SUBMITTED,
200                 jobid, sizeof(submitted), (char *)&submitted);
201 }
202
203 void notify_printer_delete(char *printer_name)
204 {
205 }
206
207 void notify_printer_add(char *printer_name)
208 {
209 }
210
211 void notify_printer_driver(int num, char *driver_name)
212 {
213 }
214
215 void notify_printer_comment(int num, char *comment)
216 {
217 }
218
219 void notify_printer_sharename(int num, char *share_name)
220 {
221 }
222
223 void notify_printer_port(int num, char *port_name)
224 {
225 }
226
227 void notify_printer_location(int num, char *location)
228 {
229 }