Revert manifest to default one
[external/cups.git] / cups / notify.c
1 /*
2  * "$Id: notify.c 9042 2010-03-24 00:45:34Z mike $"
3  *
4  *   Notification routines for CUPS.
5  *
6  *   Copyright 2007-2010 by Apple Inc.
7  *   Copyright 2005-2006 by Easy Software Products.
8  *
9  *   These coded instructions, statements, and computer programs are the
10  *   property of Apple Inc. and are protected by Federal copyright
11  *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
12  *   which should have been included with this file.  If this file is
13  *   file is missing or damaged, see the license at "http://www.cups.org/".
14  *
15  *   This file is subject to the Apple OS-Developed Software exception.
16  *
17  * Contents:
18  *
19  *   cupsNotifySubject() - Return the subject for the given notification
20  *                         message.
21  *   cupsNotifyText()    - Return the text for the given notification message.
22  */
23
24 /*
25  * Include necessary headers...
26  */
27
28 #include "cups-private.h"
29
30
31 /*
32  * 'cupsNotifySubject()' - Return the subject for the given notification message.
33  *
34  * The returned string must be freed by the caller using @code free@.
35  *
36  * @since CUPS 1.2/Mac OS X 10.5@
37  */
38
39 char *                                  /* O - Subject string or @code NULL@ */
40 cupsNotifySubject(cups_lang_t *lang,    /* I - Language data */
41                   ipp_t       *event)   /* I - Event data */
42 {
43   char                  buffer[1024];   /* Subject buffer */
44   const char            *prefix,        /* Prefix on subject */
45                         *state;         /* Printer/job state string */
46   ipp_attribute_t       *job_id,        /* notify-job-id */
47                         *job_name,      /* job-name */
48                         *job_state,     /* job-state */
49                         *printer_name,  /* printer-name */
50                         *printer_state, /* printer-state */
51                         *printer_uri,   /* notify-printer-uri */
52                         *subscribed;    /* notify-subscribed-event */
53
54
55  /*
56   * Range check input...
57   */
58
59   if (!event || !lang)
60     return (NULL);
61
62  /*
63   * Get the required attributes...
64   */
65
66   job_id        = ippFindAttribute(event, "notify-job-id", IPP_TAG_INTEGER);
67   job_name      = ippFindAttribute(event, "job-name", IPP_TAG_NAME);
68   job_state     = ippFindAttribute(event, "job-state", IPP_TAG_ENUM);
69   printer_name  = ippFindAttribute(event, "printer-name", IPP_TAG_NAME);
70   printer_state = ippFindAttribute(event, "printer-state", IPP_TAG_ENUM);
71   printer_uri   = ippFindAttribute(event, "notify-printer-uri", IPP_TAG_URI);
72   subscribed    = ippFindAttribute(event, "notify-subscribed-event",
73                                    IPP_TAG_KEYWORD);
74
75
76   if (job_id && printer_name && printer_uri && job_state)
77   {
78    /*
79     * Job event...
80     */
81
82     prefix = _cupsLangString(lang, _("Print Job:"));
83
84     switch (job_state->values[0].integer)
85     {
86       case IPP_JOB_PENDING :
87           state = _cupsLangString(lang, _("pending"));
88           break;
89       case IPP_JOB_HELD :
90           state = _cupsLangString(lang, _("held"));
91           break;
92       case IPP_JOB_PROCESSING :
93           state = _cupsLangString(lang, _("processing"));
94           break;
95       case IPP_JOB_STOPPED :
96           state = _cupsLangString(lang, _("stopped"));
97           break;
98       case IPP_JOB_CANCELED :
99           state = _cupsLangString(lang, _("canceled"));
100           break;
101       case IPP_JOB_ABORTED :
102           state = _cupsLangString(lang, _("aborted"));
103           break;
104       case IPP_JOB_COMPLETED :
105           state = _cupsLangString(lang, _("completed"));
106           break;
107       default :
108           state = _cupsLangString(lang, _("unknown"));
109           break;
110     }
111
112     snprintf(buffer, sizeof(buffer), "%s %s-%d (%s) %s",
113              prefix,
114              printer_name->values[0].string.text,
115              job_id->values[0].integer,
116              job_name ? job_name->values[0].string.text :
117                  _cupsLangString(lang, _("untitled")),
118              state);
119   }
120   else if (printer_uri && printer_name && printer_state)
121   {
122    /*
123     * Printer event...
124     */
125
126     prefix = _cupsLangString(lang, _("Printer:"));
127
128     switch (printer_state->values[0].integer)
129     {
130       case IPP_PRINTER_IDLE :
131           state = _cupsLangString(lang, _("idle"));
132           break;
133       case IPP_PRINTER_PROCESSING :
134           state = _cupsLangString(lang, _("processing"));
135           break;
136       case IPP_PRINTER_STOPPED :
137           state = _cupsLangString(lang, _("stopped"));
138           break;
139       default :
140           state = _cupsLangString(lang, _("unknown"));
141           break;
142     }
143
144     snprintf(buffer, sizeof(buffer), "%s %s %s",
145              prefix,
146              printer_name->values[0].string.text,
147              state);
148   }
149   else if (subscribed)
150     strlcpy(buffer, subscribed->values[0].string.text, sizeof(buffer));
151   else
152     return (NULL);
153
154  /*
155   * Duplicate and return the subject string...
156   */
157
158   return (strdup(buffer));
159 }
160
161
162 /*
163  * 'cupsNotifyText()' - Return the text for the given notification message.
164  *
165  * The returned string must be freed by the caller using @code free@.
166  *
167  * @since CUPS 1.2/Mac OS X 10.5@
168  */
169
170 char *                                  /* O - Message text or @code NULL@ */
171 cupsNotifyText(cups_lang_t *lang,       /* I - Language data */
172                ipp_t       *event)      /* I - Event data */
173 {
174   ipp_attribute_t       *notify_text;   /* notify-text */
175
176
177  /*
178   * Range check input...
179   */
180
181   if (!event || !lang)
182     return (NULL);
183
184  /*
185   * Get the notify-text attribute from the server...
186   */
187
188   if ((notify_text = ippFindAttribute(event, "notify-text",
189                                       IPP_TAG_TEXT)) == NULL)
190     return (NULL);
191
192  /*
193   * Return a copy...
194   */
195
196   return (strdup(notify_text->values[0].string.text));
197 }
198
199
200 /*
201  * End of "$Id: notify.c 9042 2010-03-24 00:45:34Z mike $".
202  */