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