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