1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <dns_sd.h>
21 #include <string>
22 #include <list>
23 #include <DebugServices.h>
24 
25 class CPrinterSetupWizardSheet;
26 
27 #define kDefaultPriority    50
28 #define kDefaultQTotal      1
29 
30 namespace PrinterSetupWizard
31 {
32 struct Printer;
33 struct Service;
34 struct Queue;
35 struct Manufacturer;
36 struct Model;
37 
38 typedef std::list<Queue*>   Queues;
39 typedef std::list<Printer*> Printers;
40 typedef std::list<Service*> Services;
41 typedef std::list<Model*>   Models;
42 
43 struct Printer
44 {
45     Printer();
46 
47     ~Printer();
48 
49     Service*
50     LookupService
51     (
52         const std::string   &   type
53     );
54 
55     CPrinterSetupWizardSheet    *   window;
56     HTREEITEM item;
57 
58     //
59     // These are from the browse reply
60     //
61     std::string name;
62     CString displayName;
63     CString actualName;
64 
65     //
66     // These keep track of the different services associated with this printer.
67     // the services are ordered according to preference.
68     //
69     Services services;
70 
71     //
72     // these are derived from the printer matching code
73     //
74     // if driverInstalled is false, then infFileName should
75     // have an absolute path to the printers inf file.  this
76     // is used to install the printer from printui.dll
77     //
78     // if driverInstalled is true, then model is the name
79     // of the driver to use in AddPrinter
80     //
81     bool driverInstalled;
82     CString infFileName;
83     CString manufacturer;
84     CString displayModelName;
85     CString modelName;
86     CString portName;
87     bool deflt;
88 
89     // This let's us know that this printer was discovered via OSX Printer Sharing.
90     // We use this knowledge to workaround a problem with OS X Printer sharing.
91 
92     bool isCUPSPrinter;
93 
94     //
95     // state
96     //
97     unsigned resolving;
98     bool installed;
99 };
100 
101 
102 struct Service
103 {
104     Service();
105 
106     ~Service();
107 
108     Queue*
109     SelectedQueue();
110 
111     void
112     EmptyQueues();
113 
114     Printer     *   printer;
115     uint32_t ifi;
116     std::string type;
117     std::string domain;
118 
119     //
120     // these are from the resolve
121     //
122     DNSServiceRef serviceRef;
123     CString hostname;
124     unsigned short portNumber;
125     CString protocol;
126     unsigned short qtotal;
127 
128     //
129     // There will usually one be one of these, however
130     // this will handle printers that have multiple
131     // queues.  These are ordered according to preference.
132     //
133     Queues queues;
134 
135     //
136     // Reference count
137     //
138     unsigned refs;
139 };
140 
141 
142 struct Queue
143 {
144     Queue();
145 
146     ~Queue();
147 
148     CString name;
149     uint32_t priority;
150     CString pdl;
151     CString usb_MFG;
152     CString usb_MDL;
153     CString description;
154     CString location;
155     CString product;
156 };
157 
158 
159 struct Manufacturer
160 {
161     CString name;
162     Models models;
163 
164     Model*
165     find( const CString & name );
166 };
167 
168 
169 struct Model
170 {
171     bool driverInstalled;
172     CString infFileName;
173     CString displayName;
174     CString name;
175 };
176 
177 
178 inline
Printer()179 Printer::Printer()
180     :
181     isCUPSPrinter( false )
182 {
183 }
184 
185 inline
~Printer()186 Printer::~Printer()
187 {
188     while ( services.size() > 0 )
189     {
190         Service * service = services.front();
191         services.pop_front();
192         delete service;
193     }
194 }
195 
196 inline Service*
LookupService(const std::string & type)197 Printer::LookupService
198 (
199     const std::string   &   type
200 )
201 {
202     Services::iterator it;
203 
204     for ( it = services.begin(); it != services.end(); it++ )
205     {
206         Service * service = *it;
207 
208         if ( strcmp(service->type.c_str(), type.c_str()) == 0 )
209         {
210             return service;
211         }
212     }
213 
214     return NULL;
215 }
216 
217 inline
Service()218 Service::Service()
219     :
220     qtotal(kDefaultQTotal)
221 {
222 }
223 
224 inline
~Service()225 Service::~Service()
226 {
227     check( serviceRef == NULL );
228 
229     EmptyQueues();
230 }
231 
232 inline Queue*
SelectedQueue()233 Service::SelectedQueue()
234 {
235     return queues.front();
236 }
237 
238 inline void
EmptyQueues()239 Service::EmptyQueues()
240 {
241     while ( queues.size() > 0 )
242     {
243         Queue * q = queues.front();
244         queues.pop_front();
245         delete q;
246     }
247 }
248 
249 inline
Queue()250 Queue::Queue()
251     :
252     priority(kDefaultPriority)
253 {
254 }
255 
256 inline
~Queue()257 Queue::~Queue()
258 {
259 }
260 
261 inline Model*
find(const CString & name)262 Manufacturer::find( const CString & name )
263 {
264     Models::iterator it;
265 
266     for ( it = models.begin(); it != models.end(); it++ )
267     {
268         Model * model = *it;
269 
270         if ( model->name == name )
271         {
272             return model;
273         }
274     }
275 
276     return NULL;
277 }
278 }
279 
280 
281