File: printerfactory.cpp

package info (click to toggle)
hplip 3.22.10%2Bdfsg0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 171,296 kB
  • sloc: python: 83,373; ansic: 71,016; cpp: 65,202; sh: 4,408; perl: 4,397; makefile: 937
file content (527 lines) | stat: -rw-r--r-- 17,115 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*****************************************************************************\
  Copyright (c) 2002 - 2015, HP Co.
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  3. Neither the name of HP nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*****************************************************************************/
// PrinterFactory.cpp - Morpheus component code base

#include "header.h"
#include <string.h>
#include "printerfactory.h"

APDK_BEGIN_NAMESPACE

PrinterFactory* PrinterFactory::s_Instance = NULL;   // enforce the singleton
PrinterFactory::ProxyListElement* PrinterFactory::s_ProxyList = NULL;
unsigned int PrinterFactory::s_uFamilyCount = 0;
unsigned int PrinterFactory::s_uPrinterCount = 0;

extern void HP_strcat(char* str1, const char* str2);

//PrinterFactory
//! Constructor for PrinterFactory
/*!
******************************************************************************/
PrinterFactory::PrinterFactory
(
)
{
    TRACE("PF::PF\n");
    // the static factory is coming on-line, do what you need here.
    TRACE("PF - static factory construction complete\n");
} //PrinterFactory


//~PrinterFactory
//! Destructor for PrinterFactory
/*!
******************************************************************************/
PrinterFactory::~PrinterFactory
(
)
{
    TRACE("PF::~PF - static factory destructing\n");
    ProxyListElement* t_ListEntry;
    while (s_ProxyList)
    {
        TRACE(" - deleting %s from factory list\n", s_ProxyList->printerProxyElement->GetFamilyName());
        t_ListEntry = s_ProxyList;
        s_ProxyList = t_ListEntry->next;
        delete t_ListEntry;
    }
    s_Instance = NULL;  // factory is gone
    TRACE("finished PF::~PF\n");
} //~PrinterFactory


//Register
//! Register proxy classes for printer families
/*!
This method is used by the constuctor of the PrinterProxy subclasses to
register themselves with the factory.  They call Register(this) to pass
themselves to the factory.
******************************************************************************/
void PrinterFactory::Register
(
    const PrinterProxy* thePrinterProxy         //!< [in]pointer to the PrinterProxy
)
{
    // use the first registration of a printer proxy to force the instantiation
    // of the only PrinterFactory
    if (s_Instance == NULL)
    {
        TRACE("PF::Register - asking for new factory\n");
        // create the singleton
        s_Instance = new PrinterFactory;
    }

    TRACE("PF::Register\n");
    // we know we'll need an entry in the list so create it first
    ProxyListElement* t_ListEntry = new(ProxyListElement);
    if (t_ListEntry == NULL)
    {
        // we have a problem - the driver is not running yet
        // the RTL is registering these static printer proxy servers and
        // the main code hasn't started yet.
        return;
    }
    // this list works so simply because we don't care about the order
    // and we always insert the next entry before the current head.
    t_ListEntry->printerProxyElement = thePrinterProxy;
    t_ListEntry->next = s_ProxyList;
    s_ProxyList = t_ListEntry;
    s_uFamilyCount++;
    s_uPrinterCount += thePrinterProxy->GetModelCount();

    TRACE("PF::Register %s registered with PrinterFactory\n", thePrinterProxy->GetFamilyName());
    TRACE("Printers supported by class are:\n");
    MODEL_HANDLE myHandle = thePrinterProxy->StartModelNameEnum();
    const char* printerName;
    while ((printerName = thePrinterProxy->GetNextModelName(myHandle)))
    {
        TRACE("     %s\n", printerName);
    }
} //Register


//UnRegister
//! UnRegister proxy classes for printer families
/*!
PrinterProxy subclasses use this to unregister themselves with their destuctor
executes.
******************************************************************************/
void PrinterFactory::UnRegister
(
    const PrinterProxy* thePrinterProxy     //!< [in]pointer to the PrinterProxy to unregister
)
{
    // use the first registration of a printer proxy to force the instantiation
    // of the only PrinterFactory
    if (s_Instance == NULL)
    {
        TRACE("Unregistering a PrinterProxy when factory does not exist\n");
        return;
    }

    TRACE("PF::UnRegister %s\n", thePrinterProxy->GetFamilyName());
    if (s_ProxyList == NULL)
    {
        TRACE("PF::UnRegister - Printer Factory printer list is empty!\n");
    }

    ProxyListElement* prevListEntry = NULL;
    ProxyListElement* t_ListEntry = s_ProxyList;
    bool bFound = false;

    while ((t_ListEntry) && (!bFound))
    {
        if (t_ListEntry->printerProxyElement == thePrinterProxy)
        {
            TRACE("We found the matching proxy by address\n");
            bFound = true;
            if (prevListEntry != NULL) //somewhere in list
            {
                prevListEntry->next = t_ListEntry->next;
            }
            else    // at head of list
            {
                s_ProxyList = t_ListEntry->next;
            }
            delete t_ListEntry;
            s_uFamilyCount--;
            s_uPrinterCount -= thePrinterProxy->GetModelCount();
        }
        prevListEntry = t_ListEntry;
        t_ListEntry = t_ListEntry->next;
    }
    TRACE("PF::UnRegister removed %s from PrinterFactory\n", thePrinterProxy->GetFamilyName());
    TRACE("PF::UnRegister - factory now supports %d families and %d printers\n",
        s_uFamilyCount,
        s_uPrinterCount);

    if (s_ProxyList == NULL)
    {
        TRACE("PF::UnRegister asking for deletion of empty Factory\n");
        delete PrinterFactory::GetInstance();
    }
} //UnRegister


//GetNextFamilyName
//! return the next familiy name that is in the factory
/*!
Move to the next family in the factory and then return the name of that family.
This moved to the next family first!
FAMILY_HANDLE is changed to the next family.  If there are no more families in
the factory then FAMILY_HANDLE will be NULL.

\return
name of the next family in the factory<br>
NULL if there are no more families in the factory
******************************************************************************/
const char* PrinterFactory::GetNextFamilyName
(
    FAMILY_HANDLE& theFamilyHandle          //!< [in][out]handle to the current family
) const
{
    TRACE("PF::GetNextFamilyName()\n");
    if (nextFamily(theFamilyHandle))
    {
        return getPrinterProxy(theFamilyHandle)->GetFamilyName();
    }
    else
    {
        return NULL;
    }
} //GetNextFamilyName


//GetFamilyName
//! return the family namne that supports the printer
/*!
\return
the name of the family that supports the printer name passed in.  May be NULL
if there is no family in the factory that supports the printer name.
******************************************************************************/
const char* PrinterFactory::GetFamilyName
(
    const char* thePrinterName                  //!< [in] name of the printer model
) const
{
    const char* szFamilyName = NULL;
    FAMILY_HANDLE myFamilyHandle = FindDevIdMatch(thePrinterName);
    if (myFamilyHandle)
    {
        szFamilyName = getPrinterProxy(myFamilyHandle)->GetFamilyName();
    }
    return szFamilyName;
} //GetFamilyName

//NextModel
//! return the next model name for the current family that is in the factory
/*!
MODEL_HANDLE is updated durint this processes
\return
Name of the next model that this family supports.  Will be NULL at the end of
the model list.
******************************************************************************/
const char* PrinterFactory::GetNextModelName
(
    FAMILY_HANDLE theFamilyHandle,          //!< [in] handle to the current family
    MODEL_HANDLE& theModelHandle            //!< [in][out] handle to the current model
) const
{
    TRACE("PF::GetNextModelName()\n");
    const char* szModelName = NULL;
    if (theFamilyHandle == NULL)
    {
        theModelHandle = NULL;
    }
    else
    {
        szModelName = getPrinterProxy(theFamilyHandle)->GetNextModelName(theModelHandle);
    }
    return szModelName;
} //GetNextModelName


//GetNextPrinterName
//! return the next printer name in the factory
/*!
This returns the next printer (model) name supported in the factory.  This
iterates over the whole factory without regard to families.  PRINTER_HANDLE is
updated during this process.

\return
Name of the printer in the factory.  Will be NULL after all the printers in the
factory have been enumerated.
******************************************************************************/
const char* PrinterFactory::GetNextPrinterName
(
    PRINTER_HANDLE& thePrinterHandle        //!< [in][out] handle to the printer
) const
{
    TRACE("PF::GetNextPrinterName()\n");
    static FAMILY_HANDLE familyHandle;
    nextPrinter(familyHandle, thePrinterHandle);
    return reinterpret_cast<const char*>(thePrinterHandle);
} //GetNextPrinterName


//nextFamily
//! moves the family handle to the next family in the factory
/*!
updates the FAMILY_HANDLE to the next family in the factory.  After the last
family in the factory the FAMILY_HANDLE will be NULL.

\return
true - there was another family in the factory (ie FAMILY_HANDLE is not NULL)<br>
false - there were no more families in the factory (ie FAMILY_HANDLE is NULL)
******************************************************************************/
bool PrinterFactory::nextFamily
(
    FAMILY_HANDLE& theFamilyHandle          //!< [in][out] handle to the family
) const
{
    TRACE("PF::nextFamily()\n");
    bool bMore = true;
    const ProxyListElement* proxyList = getProxyListElement(theFamilyHandle);
    if (proxyList == NULL)
    {
        TRACE("PF::nextFamily - reset to head of list\n");
        theFamilyHandle = s_ProxyList;
    }
    else
    {
        TRACE("PF::nextFamily - next element\n");
        theFamilyHandle = proxyList->next;
    }
    if (theFamilyHandle == NULL)
    {
        TRACE("PF::nextFamily - NULL (end of list)\n");
        bMore = false;
    }
    return bMore;
} //nextFamily


//EnumDevices
//! moves the family handle to the next family in the factory
/*!
updates the FAMILY_HANDLE to the next family in the factory.  After the last
family in the factory the FAMILY_HANDLE will be NULL.

\return
true - there was another family in the factory (ie FAMILY_HANDLE is not NULL)<br>
false - there were no more families in the factory (ie FAMILY_HANDLE is NULL)
******************************************************************************/
const PRINTER_TYPE PrinterFactory::EnumDevices
(
	FAMILY_HANDLE& theFamilyHandle
) const
{
    TRACE("PF::EnumDevices()\n");
    if (nextFamily(theFamilyHandle))
    {
        return getPrinterProxy(theFamilyHandle)->GetPrinterType();
    }
    else
    {
        return UNSUPPORTED;
    }
}

//nextPrinter
//! determines of there is another printer in the factory
/*! moves the modle handle to the next model in the family.  If there are no more
models in the family then it moves the family handle to the next family and the
model handle to the start of that families model list.
both the family and model handels can be updated during this process.  Both will
be NULL if there are no more printers in this iteration of the factory.

\return
true - there was another printer in the factory and the family/model handle references it<br>
false - there were no more printers in the factory and both handles are NULL
******************************************************************************/
bool PrinterFactory::nextPrinter
(
    FAMILY_HANDLE& theFamilyHandle,         //!< [in][out] current family handle
    MODEL_HANDLE& theModelHandle            //!< [in][out] current model handle
) const
{
    TRACE("PF::nextPrinter(,)\n");
    bool bMore = true;
    if (theFamilyHandle == NULL)   //this is the first time through
    {
        theFamilyHandle = StartFamilyNameEnum();
        if (nextFamily(theFamilyHandle))
        {
            theModelHandle = StartModelNameEnum(theFamilyHandle);
        }
        else
        {
            bMore = false;      // no families in the list
            goto EXIT;
        }
    }
    if (GetNextModelName(theFamilyHandle, theModelHandle) == NULL) //end of model list
    {
        if(nextFamily(theFamilyHandle))
        {
            theModelHandle = StartModelNameEnum(theFamilyHandle);
            if (GetNextModelName(theFamilyHandle, theModelHandle) == NULL)
            {
                bMore = false;
            }
        }
        else
        {
            bMore = false;
        }
    }

EXIT:
    {
        return bMore;
    }
} //nextPrinter


//FindDevIdMatch
//! Find a match for a Device ID string in the factory
/*!
The device ID string can be a device ID string retrived from a printer or it
can be a simple model name or family name.  This will walk through each family
in the factory and will check to see if it is a simple family or model string
and if not then will check for a valid device ID string.

\return
A family handle based on the device ID string passed in.<br>
NULL if there is no match.
******************************************************************************/
const FAMILY_HANDLE PrinterFactory::FindDevIdMatch
(
    const char* szDevIdString           //!< [in] well formed device id string
) const
{
    TRACE("PF::FindDevIdMatch(%s)\n", szDevIdString);
    FAMILY_HANDLE myFamilyHandle = StartFamilyNameEnum();

	bool  bDevIDString = TRUE;
    if ((!strstr(szDevIdString, "MFG:") &&
        !strstr(szDevIdString+2, "MFG:") &&
        !strstr(szDevIdString, "MANUFACTURER:") &&
        !strstr(szDevIdString+2, "MANUFACTURER:")) ||
        (!strstr(szDevIdString, "MDL:") &&
        !strstr(szDevIdString+2, "MDL:") &&
        !strstr(szDevIdString, "MODEL:") &&
        !strstr(szDevIdString+2, "MODEL:")) ||
        ((szDevIdString[0] == '\0') && (szDevIdString[1] == '\0')))
    {
        bDevIDString = FALSE;
    }

    while (nextFamily(myFamilyHandle))
    {
		if (bDevIDString)
		{
			// check to see if a full device ID String match
			if (getPrinterProxy(myFamilyHandle)->DeviceMatchQuery(szDevIdString))
			{
				break;  // found it!
			}
		}
		else
		{
			// check the family and see if they passed a simple model string
			if (getPrinterProxy(myFamilyHandle)->ModelMatchQuery(szDevIdString))
			{
				break;  // found it!
			}
		}
    }
    // if we never had a match (i.e. did the break) then myFamilyHandle is NULL
    return myFamilyHandle;
} //FindDevIdMatch

//GetModelBits
//! Get the bitwise return based on the registered family
/*!

\return
A bitwise int based on the family registered.<br
0 if there is no printer registered.
******************************************************************************/
const unsigned int PrinterFactory::GetModelBits
(
) const
{
	unsigned int bits=0;
    TRACE("PF::GetModelBits\n");
    FAMILY_HANDLE myFamilyHandle = StartFamilyNameEnum();

    while (nextFamily(myFamilyHandle))
    {
		if (myFamilyHandle)
		{
			bits = bits | getPrinterProxy(myFamilyHandle)->GetModelBit();
		}
    }
    return bits;
} //GetModelBits

//GetModelString
//! Get the bitwise return based on the registered family
/*!

\return
concatnated string based on the family registered.<br
null string if there is no printer registered.
******************************************************************************/
const void PrinterFactory::GetModelString
(
	char* mresult,
    int   mresult_length
) const
{
	assert(mresult);
	mresult[0] = '\0';

    TRACE("PF::GetModelString\n");
    FAMILY_HANDLE myFamilyHandle = StartFamilyNameEnum();
    int    i = 2;
    const char   *p;
    while (nextFamily(myFamilyHandle))
    {
        p = getPrinterProxy(myFamilyHandle)->GetFamilyName();
        i += strlen(p) + 1;
        if (i > mresult_length) break;
		HP_strcat(mresult, p);
		HP_strcat(mresult, " ");
    }
} //GetModelBits


APDK_END_NAMESPACE